-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyayacemu.cpp
224 lines (193 loc) · 4.38 KB
/
yayacemu.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
#include "Vchip8.h"
#include "Vchip8__Dpi.h"
#include "svdpi.h"
#include "verilated.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_quit.h>
#include <SDL2/SDL_timer.h>
#include <inttypes.h>
#include <iostream>
#include <memory.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define EMULATION_HZ 100000
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *texture;
int keys[16];
bool is_beeping;
void init_screen() {
SDL_Init(SDL_INIT_EVERYTHING);
window = SDL_CreateWindow(
"Yet Another Yet Another Chip-8 Emulator", // creates a window
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH * 10,
SCREEN_HEIGHT * 10, 0);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_STREAMING, SCREEN_WIDTH,
SCREEN_HEIGHT);
}
void draw_screen(const svLogicVecVal *vram) {
uint32_t *screen = (uint32_t *)malloc(SCREEN_WIDTH * SCREEN_HEIGHT * 32);
for (int i = 0; i < 1024; i++) {
uint8_t line_byte = (uint8_t)vram[i].aval;
for (int j = 0; j < 8; j++) {
uint8_t pixel_val = (line_byte >> (7 - j)) & 1;
screen[(i * 8) + j] = pixel_val == 1 ? 0xFFFFFFFF : (is_beeping ? 0xFF000000 : 0x00000000);
}
}
SDL_UpdateTexture(texture, NULL, screen, sizeof(screen[0]) * SCREEN_WIDTH);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
free(screen);
}
uint8_t get_key(uint8_t col) {
SDL_Event event;
uint8_t res = 0xFF;
while (SDL_PollEvent(&event)) {
uint8_t down = 0;
switch (event.type) {
case SDL_KEYDOWN:
down = 1;
case SDL_KEYUP:
switch (event.key.keysym.sym) {
case SDLK_1:
keys[1] = down;
break;
case SDLK_2:
keys[2] = down;
break;
case SDLK_3:
keys[3] = down;
break;
case SDLK_a:
keys[10] = down;
break;
case SDLK_4:
keys[4] = down;
break;
case SDLK_5:
keys[5] = down;
break;
case SDLK_6:
keys[6] = down;
break;
case SDLK_7:
keys[7] = down;
break;
case SDLK_b:
keys[11] = down;
break;
case SDLK_8:
keys[8] = down;
break;
case SDLK_9:
keys[9] = down;
break;
case SDLK_c:
keys[12] = down;
break;
case SDLK_0:
keys[0] = down;
break;
case SDLK_e:
keys[14] = down;
break;
case SDLK_f:
keys[15] = down;
break;
case SDLK_d:
keys[13] = down;
break;
}
}
}
if (keys[0] == 1) {
if (col == 0b1110)
res = res & 0b0111;
}
if (keys[1] == 1) {
if (col == 0b1101)
res = res & 0b1110;
}
if (keys[2] == 1) {
if (col == 0b1110)
res = res & 0b1110;
}
if (keys[3] == 1) {
if (col == 0b1011)
res = res & 0b1110;
}
if (keys[4] == 1) {
if (col == 0b1101)
res = res & 0b1101;
}
if (keys[5] == 1) {
if (col == 0b1110)
res = res & 0b1101;
}
if (keys[6] == 1) {
if (col == 0b1011)
res = res & 0b1101;
}
if (keys[7] == 1) {
if (col == 0b1101)
res = res & 0b1011;
}
if (keys[8] == 1) {
if (col == 0b1110)
res = res & 0b1011;
}
if (keys[9] == 1) {
if (col == 0b1011)
res = res & 0b1011;
}
if (keys[10] == 1) {
if (col == 0b1101)
res = res & 0b0111;
}
if (keys[11] == 1) {
if (col == 0b1011)
res = res & 0b0111;
}
if (keys[12] == 1) {
if (col == 0b0111)
res = res & 0b1110;
}
if (keys[13] == 1) {
if (col == 0b0111)
res = res & 0b1101;
}
if (keys[14] == 1) {
if (col == 0b0111)
res = res & 0b1011;
}
if (keys[15] == 1) {
if (col == 0b0111)
res = res & 0b0111;
}
return res;
}
int main(int argc, char **argv) {
VerilatedContext *contextp = new VerilatedContext;
contextp->commandArgs(argc, argv);
Vchip8 *dut = new Vchip8{contextp};
dut->rst_in = 0;
dut->fpga_clk = 1;
while (true) {
dut->row = get_key(dut->col);
dut->fpga_clk ^= 1;
dut->eval();
is_beeping = dut->beep == 1;
if (SDL_QuitRequested()) {
std::cout << "Goodbye!" << '\n';
break;
}
}
fflush(stdout);
delete dut;
delete contextp;
return 0;
}