-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpu.cpp
277 lines (210 loc) · 7.06 KB
/
gpu.cpp
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include <iostream>
#include <SFML/Graphics.hpp>
#include "gpu.h"
using namespace std;
/*
This class represent the GPU of the emulator. It implements all the methods necesserary
to execute the GPU and Keyboard interrupts opcodes.
*/
Gpu::Gpu(std::string filename){
string title;
title = "Chip 8 : " + filename;
// Create the window
this->window.create(sf::VideoMode(640, 320), title);
this->window.clear(sf::Color::Black);
// Create CHIP 8 pixels. gfx stores the state of the pixel and pixels the SFML squares
// representing the pixels (there, the screen is scaled by 10 to be more visible)
for(int i = 0; i < 64; i++){
for(int j = 0; j < 32; j++){
gfx[i][j] = 0;
pixels[i][j].setSize(sf::Vector2f(10, 10));
pixels[i][j].setFillColor(sf::Color(0, 0, 0));
pixels[i][j].setPosition(sf::Vector2f(i*10,j*10));
this->window.draw(this->pixels[i][j]);
}
}
this->window.display();
}
// Function that draw in Xor mode
unsigned char Gpu::draw(unsigned char x, unsigned char y, unsigned char height, vector<unsigned char> sprite){
unsigned char VF(0);
int xi,yi;
for (int j = 0; j < height; j++)
{
yi = (y + j);
for(int i = 0; i < 8; i++)
{
xi = (x + i);
if((sprite[j] & (0x80 >> i)) != 0)
{
if (xi < 64 && yi < 32){
if(this->gfx[xi][yi] == 1){
VF = 1;
}
this->gfx[xi][yi] ^= 1;
}
}
}
}
for(int i = 0; i < 64; i++){
for(int j = 0; j < 32; j++){
if(this->gfx[i][j] == 0){
this->pixels[i][j].setFillColor(sf::Color(0, 0, 0));
this->window.draw(this->pixels[i][j]);
}
else{
this->pixels[i][j].setFillColor(sf::Color(255, 255, 255));
this->window.draw(this->pixels[i][j]);
}
}
}
this->window.display();
return VF;
}
// Check whether the window is openned
bool Gpu::isOpen(){
return this->window.isOpen();
}
// Wipe the screen
void Gpu::clear(){
this->window.clear();
for(int i = 0; i < 64; i++){
for(int j = 0; j < 32; j++){
this->pixels[i][j].setFillColor(sf::Color(0, 0, 0));
this->gfx[i][j] = 0;
this->window.draw(this->pixels[i][j]);
}
}
this->window.display();
}
// Wait for an event to occur
unsigned char Gpu::waitEvent(sf::Event *event){
while(this->window.waitEvent(*event)){
if(event->type == sf::Event::Closed && this->window.waitEvent(*event) == true){
this->window.close();
}
if(event->type == sf::Event::KeyPressed){
switch(event->key.code){
case sf::Keyboard::Num1:
return 0x01;
case sf::Keyboard::Num2:
return 0x02;
case sf::Keyboard::Num3:
return 0x03;
case sf::Keyboard::Num4:
return 0x0C;
case sf::Keyboard::A:
return 0x04;
case sf::Keyboard::Z:
return 0x05;
case sf::Keyboard::E:
return 0x06;
case sf::Keyboard::R:
return 0x0D;
case sf::Keyboard::Q:
return 0x07;
case sf::Keyboard::S:
return 0x08;
case sf::Keyboard::D:
return 0x09;
case sf::Keyboard::F:
return 0x0E;
case sf::Keyboard::X:
return 0x00;
case sf::Keyboard::C:
return 0x0B;
case sf::Keyboard::V:
return 0x0F;
case sf::Keyboard::W:
return 0x0A;
default:
break;
}
}
}
return 0xFF;
}
// Check if an event occured and return its value
unsigned short Gpu::pollEvent(sf::Event *event){
bool check = this->window.pollEvent(*event);
if(event->type == sf::Event::Closed && check == true){
this->window.close();
}
if(event->type == sf::Event::KeyReleased){
switch(event->key.code){
case sf::Keyboard::Num1:
return 0x0001;
case sf::Keyboard::Num2:
return 0x0002;
case sf::Keyboard::Num3:
return 0x0003;
case sf::Keyboard::Num4:
return 0x000C;
case sf::Keyboard::A:
return 0x0004;
case sf::Keyboard::Z:
return 0x0005;
case sf::Keyboard::E:
return 0x0006;
case sf::Keyboard::R:
return 0x000D;
case sf::Keyboard::Q:
return 0x0007;
case sf::Keyboard::S:
return 0x0008;
case sf::Keyboard::D:
return 0x0009;
case sf::Keyboard::F:
return 0x000E;
case sf::Keyboard::W:
return 0x000A;
case sf::Keyboard::X:
return 0x0000;
case sf::Keyboard::C:
return 0x000B;
case sf::Keyboard::V:
return 0x000F;
default:
return 0x00FF;
}
}
else if(event->type == sf::Event::KeyPressed){
switch(event->key.code){
case sf::Keyboard::Num1:
return 0x0101;
case sf::Keyboard::Num2:
return 0x0102;
case sf::Keyboard::Num3:
return 0x0103;
case sf::Keyboard::Num4:
return 0x010C;
case sf::Keyboard::A:
return 0x0104;
case sf::Keyboard::Z:
return 0x0105;
case sf::Keyboard::E:
return 0x0106;
case sf::Keyboard::R:
return 0x010D;
case sf::Keyboard::Q:
return 0x0107;
case sf::Keyboard::S:
return 0x0108;
case sf::Keyboard::D:
return 0x0109;
case sf::Keyboard::F:
return 0x010E;
case sf::Keyboard::W:
return 0x010A;
case sf::Keyboard::X:
return 0x0100;
case sf::Keyboard::C:
return 0x010B;
case sf::Keyboard::V:
return 0x010F;
default:
return 0x00FF;
}
}
return 0x00FF;
}