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
|
|
|
|
|
|
|
#define SCREEN_WIDTH 64
|
|
|
|
#define SCREEN_HEIGHT 32
|
2024-01-31 19:53:10 +00:00
|
|
|
#define EMULATION_HZ 2000
|
2024-01-31 01:54:35 +00:00
|
|
|
|
|
|
|
FILE *rom_file;
|
|
|
|
SDL_Window *window;
|
|
|
|
SDL_Renderer *renderer;
|
|
|
|
SDL_Texture *texture;
|
2024-02-01 03:04:52 +00:00
|
|
|
char *rom_name;
|
2024-01-31 01:54:35 +00:00
|
|
|
|
|
|
|
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-02-01 02:57:11 +00:00
|
|
|
std::cout << "INF_EMU: Screen initialized" << '\n';
|
2024-01-31 01:54:35 +00:00
|
|
|
}
|
|
|
|
|
2024-01-31 21:53:31 +00:00
|
|
|
void set_beep(const svBit beep) {
|
2024-02-01 02:57:11 +00:00
|
|
|
if (beep == 1)
|
2024-02-01 03:04:52 +00:00
|
|
|
SDL_SetTextureColorMod(texture, 255, 0, 0);
|
2024-01-31 19:53:10 +00:00
|
|
|
else
|
2024-02-01 03:04:52 +00:00
|
|
|
SDL_SetTextureColorMod(texture, 255, 255, 255);
|
2024-01-31 21:53:31 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 03:04:52 +00:00
|
|
|
void draw_screen(const svLogicVecVal *vram) {
|
|
|
|
uint32_t *screen = (uint32_t *)malloc(SCREEN_WIDTH * SCREEN_HEIGHT * 32);
|
|
|
|
for (int i = 0; i < SCREEN_WIDTH * SCREEN_HEIGHT; i++) {
|
|
|
|
screen[i] = vram[i].aval;
|
2024-01-31 21:53:31 +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);
|
|
|
|
std::cout << "INF_EMU: Drawing Frame" << '\n';
|
2024-01-31 01:54:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int load_rom() {
|
2024-02-01 03:04:52 +00:00
|
|
|
std::cout << "INF_EMU: Loading ROM " << rom_name << '\n';
|
2024-01-31 01:54:35 +00:00
|
|
|
rom_file = fopen(rom_name, "r");
|
|
|
|
|
|
|
|
if (rom_file == NULL) {
|
2024-02-01 03:04:52 +00:00
|
|
|
std::cout << "INF_EMU: Error reading ROM file. Panicing." << '\n';
|
|
|
|
exit(1);
|
2024-01-31 01:54:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fseek(rom_file, 0L, SEEK_END);
|
|
|
|
int rom_size = ftell(rom_file);
|
|
|
|
fseek(rom_file, 0L, SEEK_SET);
|
2024-02-01 02:57:11 +00:00
|
|
|
std::cout << "INF_EMU: ROM size is %d bytes " << rom_size << '\n';
|
2024-01-31 01:54:35 +00:00
|
|
|
|
|
|
|
return rom_size;
|
|
|
|
}
|
|
|
|
|
2024-02-01 03:04:52 +00:00
|
|
|
svBitVecVal get_next_instr() { return (uint8_t)fgetc(rom_file); }
|
2024-01-31 01:54:35 +00:00
|
|
|
|
2024-02-01 03:04:52 +00:00
|
|
|
void close_rom() { fclose(rom_file); }
|
2024-01-31 01:54:35 +00:00
|
|
|
|
2024-01-31 19:09:58 +00:00
|
|
|
svBitVecVal get_key() {
|
2024-02-01 03:04:52 +00:00
|
|
|
SDL_Event event;
|
|
|
|
uint8_t down = 0;
|
|
|
|
|
|
|
|
while (SDL_PollEvent(&event)) {
|
|
|
|
switch (event.type) {
|
|
|
|
case SDL_KEYDOWN:
|
|
|
|
down = 128;
|
|
|
|
case SDL_KEYUP:
|
|
|
|
switch (event.key.keysym.sym) {
|
|
|
|
case SDLK_0:
|
|
|
|
return down | (uint8_t)0;
|
|
|
|
case SDLK_1:
|
|
|
|
return down | (uint8_t)1;
|
|
|
|
case SDLK_2:
|
|
|
|
return down | (uint8_t)2;
|
|
|
|
case SDLK_3:
|
|
|
|
return down | (uint8_t)3;
|
|
|
|
case SDLK_4:
|
|
|
|
return down | (uint8_t)4;
|
|
|
|
case SDLK_5:
|
|
|
|
return down | (uint8_t)5;
|
|
|
|
case SDLK_6:
|
|
|
|
return down | (uint8_t)6;
|
|
|
|
case SDLK_7:
|
|
|
|
return down | (uint8_t)7;
|
|
|
|
case SDLK_8:
|
|
|
|
return down | (uint8_t)8;
|
|
|
|
case SDLK_9:
|
|
|
|
return down | (uint8_t)9;
|
|
|
|
case SDLK_a:
|
|
|
|
return down | (uint8_t)10;
|
|
|
|
case SDLK_b:
|
|
|
|
return down | (uint8_t)11;
|
|
|
|
case SDLK_c:
|
|
|
|
return down | (uint8_t)12;
|
|
|
|
case SDLK_d:
|
|
|
|
return down | (uint8_t)13;
|
|
|
|
case SDLK_e:
|
|
|
|
return down | (uint8_t)14;
|
|
|
|
case SDLK_f:
|
|
|
|
return down | (uint8_t)15;
|
|
|
|
default:
|
|
|
|
return 255;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 255;
|
2024-01-31 19:09:58 +00:00
|
|
|
}
|
2024-02-01 03:04:52 +00:00
|
|
|
}
|
|
|
|
return 255;
|
2024-01-31 19:09:58 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 03:04:52 +00:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
if (argc < 2) {
|
|
|
|
std::cout << "Use: yayacemu [ROM_NAME]" << '\n';
|
|
|
|
exit(1);
|
|
|
|
}
|
2024-02-01 02:57:11 +00:00
|
|
|
|
2024-02-01 03:04:52 +00:00
|
|
|
rom_name = argv[1];
|
2024-02-01 02:57:11 +00:00
|
|
|
|
2024-02-01 03:04:52 +00:00
|
|
|
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-02-01 03:04:52 +00:00
|
|
|
while (true) {
|
|
|
|
dut->clk_in ^= 1;
|
|
|
|
dut->eval();
|
|
|
|
usleep(1000000 / EMULATION_HZ);
|
|
|
|
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
|
|
|
}
|