yayacemu/yayacemu.cpp

231 lines
4.6 KiB
C++
Raw Normal View History

2024-02-01 02:57:11 +00:00
#include "Vchip8.h"
2024-02-01 03:04:52 +00:00
#include "Vchip8__Dpi.h"
#include "svdpi.h"
2024-01-31 01:54:35 +00:00
#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>
2024-02-01 03:04:52 +00:00
#include <iostream>
#include <memory.h>
2024-01-31 01:54:35 +00:00
2024-04-15 14:32:48 +00:00
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
2024-04-15 15:32:43 +00:00
#define EMULATION_HZ 100000
2024-01-31 01:54:35 +00:00
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *texture;
void init_screen() {
SDL_Init(SDL_INIT_EVERYTHING);
2024-02-01 03:04:52 +00:00
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);
2024-01-31 01:54:35 +00:00
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888,
2024-02-01 03:04:52 +00:00
SDL_TEXTUREACCESS_STREAMING, SCREEN_WIDTH,
SCREEN_HEIGHT);
}
2024-04-15 15:35:09 +00:00
bool is_beeping;
2024-04-07 02:36:24 +00:00
void draw_screen(const svLogicVecVal *vram) {
2024-02-01 03:04:52 +00:00
uint32_t *screen = (uint32_t *)malloc(SCREEN_WIDTH * SCREEN_HEIGHT * 32);
2024-04-07 02:36:24 +00:00
for (int i = 0; i < 1024; i++) {
2024-04-15 14:32:48 +00:00
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;
2024-04-15 15:35:09 +00:00
screen[(i * 8) + j] = pixel_val == 1 ? 0xFFFFFFFF : (is_beeping ? 0xFF000000 : 0x00000000);
2024-04-15 14:32:48 +00:00
}
}
2024-02-01 02:57:11 +00:00
SDL_UpdateTexture(texture, NULL, screen, sizeof(screen[0]) * SCREEN_WIDTH);
2024-01-31 01:54:35 +00:00
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
2024-02-01 02:57:11 +00:00
free(screen);
2024-04-15 14:32:48 +00:00
// std::cout << "INF_EMU: Drawing Frame" << '\n';
2024-01-31 01:54:35 +00:00
}
2024-04-15 14:32:48 +00:00
// 1 2 3 A
// 4 5 6 B
// 7 8 9 C
// * 0 # D
//
int keys[16];
uint8_t get_key(uint8_t col) {
2024-02-01 03:04:52 +00:00
SDL_Event event;
2024-04-15 14:32:48 +00:00
uint8_t res = 0xFF;
// col 1000 - r1
2024-02-01 03:04:52 +00:00
while (SDL_PollEvent(&event)) {
2024-04-15 14:32:48 +00:00
uint8_t down = 0;
2024-02-01 03:04:52 +00:00
switch (event.type) {
case SDL_KEYDOWN:
2024-04-15 14:32:48 +00:00
down = 1;
2024-02-01 03:04:52 +00:00
case SDL_KEYUP:
switch (event.key.keysym.sym) {
2024-04-15 14:32:48 +00:00
2024-02-01 03:04:52 +00:00
case SDLK_1:
2024-04-15 14:32:48 +00:00
keys[1] = down;
break;
2024-02-01 03:04:52 +00:00
case SDLK_2:
2024-04-15 14:32:48 +00:00
keys[2] = down;
break;
2024-02-01 03:04:52 +00:00
case SDLK_3:
2024-04-15 14:32:48 +00:00
keys[3] = down;
break;
case SDLK_a:
keys[10] = down;
break;
2024-02-01 03:04:52 +00:00
case SDLK_4:
2024-04-15 14:32:48 +00:00
keys[4] = down;
break;
2024-02-01 03:04:52 +00:00
case SDLK_5:
2024-04-15 14:32:48 +00:00
keys[5] = down;
break;
2024-02-01 03:04:52 +00:00
case SDLK_6:
2024-04-15 14:32:48 +00:00
keys[6] = down;
break;
2024-02-01 03:04:52 +00:00
case SDLK_7:
2024-04-15 14:32:48 +00:00
keys[7] = down;
break;
case SDLK_b:
keys[11] = down;
break;
2024-02-01 03:04:52 +00:00
case SDLK_8:
2024-04-15 14:32:48 +00:00
keys[8] = down;
break;
2024-02-01 03:04:52 +00:00
case SDLK_9:
2024-04-15 14:32:48 +00:00
keys[9] = down;
break;
2024-02-01 03:04:52 +00:00
case SDLK_c:
2024-04-15 14:32:48 +00:00
keys[12] = down;
break;
case SDLK_0:
keys[0] = down;
break;
2024-02-01 03:04:52 +00:00
case SDLK_e:
2024-04-15 14:32:48 +00:00
keys[14] = down;
break;
2024-02-01 03:04:52 +00:00
case SDLK_f:
2024-04-15 14:32:48 +00:00
keys[15] = down;
break;
case SDLK_d:
keys[13] = down;
break;
2024-02-01 03:04:52 +00:00
}
2024-01-31 19:09:58 +00:00
}
2024-02-01 03:04:52 +00:00
}
2024-04-15 14:32:48 +00:00
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;
}
//if (res != 0)
// printf("%04b %04b\n", res, col);
return res;
2024-01-31 19:09:58 +00:00
}
2024-02-01 03:04:52 +00:00
int main(int argc, char **argv) {
VerilatedContext *contextp = new VerilatedContext;
contextp->commandArgs(argc, argv);
2024-02-01 02:57:11 +00:00
2024-02-01 03:04:52 +00:00
Vchip8 *dut = new Vchip8{contextp};
2024-02-01 02:57:11 +00:00
2024-04-08 04:39:15 +00:00
dut->rst_in = 0;
dut->fpga_clk = 1;
2024-02-01 03:04:52 +00:00
while (true) {
2024-04-15 14:32:48 +00:00
dut->row = get_key(dut->col);
2024-04-08 04:39:15 +00:00
dut->fpga_clk ^= 1;
2024-02-01 03:04:52 +00:00
dut->eval();
2024-04-08 04:39:15 +00:00
2024-04-15 15:32:43 +00:00
2024-04-15 15:35:09 +00:00
is_beeping = dut->beep == 1;
2024-04-08 04:39:15 +00:00
2024-02-01 03:04:52 +00:00
if (SDL_QuitRequested()) {
std::cout << "INF_EMU: Received Quit from SDL. Goodbye!" << '\n';
break;
2024-01-31 01:54:35 +00:00
}
2024-02-01 03:04:52 +00:00
}
2024-02-01 02:57:11 +00:00
2024-02-01 03:04:52 +00:00
fflush(stdout);
delete dut;
delete contextp;
return 0;
2024-01-31 01:54:35 +00:00
}