aboutsummaryrefslogtreecommitdiff
path: root/src/ppusim
diff options
context:
space:
mode:
Diffstat (limited to 'src/ppusim')
-rw-r--r--src/ppusim/input.c16
-rw-r--r--src/ppusim/mem.c4
-rw-r--r--src/ppusim/pixel.c77
-rw-r--r--src/ppusim/sim.c45
-rw-r--r--src/ppusim/sim.h3
-rw-r--r--src/ppusim/work.c40
-rw-r--r--src/ppusim/work.h1
7 files changed, 117 insertions, 69 deletions
diff --git a/src/ppusim/input.c b/src/ppusim/input.c
new file mode 100644
index 0000000..5323fb1
--- /dev/null
+++ b/src/ppusim/input.c
@@ -0,0 +1,16 @@
+#include <SDL2/SDL.h>
+
+#include "input.h"
+
+hh_s_gamepad g_hh_controller_p1 = {0};
+hh_s_gamepad g_hh_controller_p2 = {0};
+
+void hh_input_read() {
+ // SDL_PumpEvents();
+ const Uint8 *kb = SDL_GetKeyboardState(NULL);
+ g_hh_controller_p1.dpad_up = kb[SDL_SCANCODE_W];
+ g_hh_controller_p1.dpad_down = kb[SDL_SCANCODE_S];
+ g_hh_controller_p1.dpad_left = kb[SDL_SCANCODE_A];
+ g_hh_controller_p1.dpad_right = kb[SDL_SCANCODE_D];
+ g_hh_controller_p1.button_primary = kb[SDL_SCANCODE_SPACE];
+}
diff --git a/src/ppusim/mem.c b/src/ppusim/mem.c
index e3f41f5..bd8606e 100644
--- a/src/ppusim/mem.c
+++ b/src/ppusim/mem.c
@@ -1,8 +1,8 @@
-#include <stdlib.h>
#include <stdio.h>
+#include <stdlib.h>
-#include "ppusim/mem.h"
#include "ppu/internals.h"
+#include "ppusim/mem.h"
hh_ppu_data_t *g_hh_ppusim_vram = NULL;
diff --git a/src/ppusim/pixel.c b/src/ppusim/pixel.c
index 6e915e6..0457d55 100644
--- a/src/ppusim/pixel.c
+++ b/src/ppusim/pixel.c
@@ -1,10 +1,10 @@
#include <SDL2/SDL.h>
#include <stdbool.h>
-#include "ppusim/work.h"
+#include "ppu/consts.h"
#include "ppu/internals.h"
#include "ppusim/mem.h"
-#include "ppu/consts.h"
+#include "ppusim/work.h"
/* transform xy if tile is flipped */
static uint16_t hh_ppusim_apply_transform(unsigned x, unsigned y, bool fliph, bool flipv) {
@@ -15,25 +15,25 @@ static uint16_t hh_ppusim_apply_transform(unsigned x, unsigned y, bool fliph, bo
/* @brief get current bg pixel cidx */
static uint8_t hh_ppusim_bg_pixel(unsigned x, unsigned y) {
- hh_ppu_data_t* aux = &g_hh_ppusim_vram[HH_PPU_VRAM_AUX_OFFSET];
- unsigned bg_shift_y = HH_RESIZE(aux[0], 7, 0);
- unsigned bg_shift_x = HH_RESIZE(aux[0], 15, 8) | HH_RESIZE(aux[1], 0, 0) << 8;
- unsigned abs_x = bg_shift_x + x;
- unsigned abs_y = bg_shift_y + y;
- unsigned grid_x = abs_x / HH_PPU_SPRITE_WIDTH;
- unsigned grid_y = abs_y / HH_PPU_SPRITE_HEIGHT;
- unsigned loc_x = abs_x - grid_x * HH_PPU_SPRITE_WIDTH;
- unsigned loc_y = abs_y - grid_y * HH_PPU_SPRITE_HEIGHT;
- unsigned bam_offset = grid_y * HH_PPU_BG_CANVAS_TILES_H + grid_x;
- hh_ppu_data_t bam = g_hh_ppusim_vram[HH_PPU_VRAM_BAM_OFFSET + bam_offset];
- uint8_t cidx = 0;
+ hh_ppu_data_t *aux = &g_hh_ppusim_vram[HH_PPU_VRAM_AUX_OFFSET];
+ unsigned bg_shift_y = HH_RESIZE(aux[0], 7, 0);
+ unsigned bg_shift_x = HH_RESIZE(aux[0], 15, 8) | HH_RESIZE(aux[1], 0, 0) << 8;
+ unsigned abs_x = bg_shift_x + x;
+ unsigned abs_y = bg_shift_y + y;
+ unsigned grid_x = abs_x / HH_PPU_SPRITE_WIDTH;
+ unsigned grid_y = abs_y / HH_PPU_SPRITE_HEIGHT;
+ unsigned loc_x = abs_x - grid_x * HH_PPU_SPRITE_WIDTH;
+ unsigned loc_y = abs_y - grid_y * HH_PPU_SPRITE_HEIGHT;
+ unsigned bam_offset = grid_y * HH_PPU_BG_CANVAS_TILES_H + grid_x;
+ hh_ppu_data_t bam = g_hh_ppusim_vram[HH_PPU_VRAM_BAM_OFFSET + bam_offset];
+ uint8_t cidx = 0;
uint16_t tile_pixel_idx = hh_ppusim_apply_transform(loc_x, loc_y, HH_RESIZE(bam, 14, 14), HH_RESIZE(bam, 13, 13));
- uint16_t tile_idx = HH_RESIZE(bam, 9, 0);
- hh_ppu_addr_t ttm_addr = tile_idx * HH_PPU_VRAM_TMM_SPRITE_SIZE + tile_pixel_idx / 5;
- uint8_t word_bit_addr = (tile_pixel_idx % 5) * 3;
- hh_ppu_data_t tmm = g_hh_ppusim_vram[HH_PPU_VRAM_TMM_OFFSET + ttm_addr];
+ uint16_t tile_idx = HH_RESIZE(bam, 9, 0);
+ hh_ppu_addr_t ttm_addr = tile_idx * HH_PPU_VRAM_TMM_SPRITE_SIZE + tile_pixel_idx / 5;
+ uint8_t word_bit_addr = (tile_pixel_idx % 5) * 3;
+ hh_ppu_data_t tmm = g_hh_ppusim_vram[HH_PPU_VRAM_TMM_OFFSET + ttm_addr];
cidx |= HH_RESIZE(bam, 12, 10) << 3;
- cidx |= HH_RESIZE(tmm, word_bit_addr+2, word_bit_addr) << 0;
+ cidx |= HH_RESIZE(tmm, word_bit_addr + 2, word_bit_addr) << 0;
return cidx;
}
@@ -44,42 +44,37 @@ static uint8_t hh_ppusim_fg_pixel(unsigned x, unsigned y) {
uint8_t cidx = 0;
for (unsigned i = 0; i < HH_PPU_FG_SPRITE_COUNT; i++) {
unsigned fam_offset = i * HH_PPU_VRAM_FAM_ENTRY_SIZE;
- hh_ppu_data_t* fam = &g_hh_ppusim_vram[HH_PPU_VRAM_FAM_OFFSET + fam_offset];
- unsigned sprite_y = HH_RESIZE(fam[0], 15, 13) | HH_RESIZE(fam[1], 4, 0) << 3;
- unsigned sprite_x = HH_RESIZE(fam[1], 13, 5);
+ hh_ppu_data_t *fam = &g_hh_ppusim_vram[HH_PPU_VRAM_FAM_OFFSET + fam_offset];
+ unsigned sprite_y = HH_RESIZE(fam[0], 15, 13) | HH_RESIZE(fam[1], 4, 0) << 3;
+ unsigned sprite_x = HH_RESIZE(fam[1], 13, 5);
if (x < sprite_x) continue;
if (x >= sprite_x + HH_PPU_SPRITE_WIDTH) continue;
if (y < sprite_y) continue;
if (y >= sprite_y + HH_PPU_SPRITE_HEIGHT) continue;
- unsigned loc_x = x - sprite_x;
- unsigned loc_y = y - sprite_y;
+ unsigned loc_x = x - sprite_x;
+ unsigned loc_y = y - sprite_y;
uint16_t tile_pixel_idx = hh_ppusim_apply_transform(loc_x, loc_y, HH_RESIZE(fam[1], 15, 15), HH_RESIZE(fam[1], 14, 14));
- uint16_t tile_idx = HH_RESIZE(fam[0], 9, 0);
- hh_ppu_addr_t ttm_addr = tile_idx * HH_PPU_VRAM_TMM_SPRITE_SIZE + tile_pixel_idx / 5;
- uint8_t word_bit_addr = (tile_pixel_idx % 5) * 3;
- hh_ppu_data_t tmm = g_hh_ppusim_vram[HH_PPU_VRAM_TMM_OFFSET + ttm_addr];
- unsigned cidx_col = HH_RESIZE(tmm, word_bit_addr+2, word_bit_addr);
+ uint16_t tile_idx = HH_RESIZE(fam[0], 9, 0);
+ hh_ppu_addr_t ttm_addr = tile_idx * HH_PPU_VRAM_TMM_SPRITE_SIZE + tile_pixel_idx / 5;
+ uint8_t word_bit_addr = (tile_pixel_idx % 5) * 3;
+ hh_ppu_data_t tmm = g_hh_ppusim_vram[HH_PPU_VRAM_TMM_OFFSET + ttm_addr];
+ unsigned cidx_col = HH_RESIZE(tmm, word_bit_addr + 2, word_bit_addr);
if (cidx_col == 0) continue;
unsigned cidx_pal = HH_RESIZE(fam[0], 12, 10);
- cidx = (cidx_col << 0) | (cidx_pal << 3);
+ cidx = (cidx_col << 0) | (cidx_pal << 3);
break;
}
return cidx;
}
-void hh_ppusim_pixel(uint8_t* s, unsigned x, unsigned y) {
- uint8_t bg_cidx = hh_ppusim_bg_pixel(x, y);
- uint8_t fg_cidx = hh_ppusim_fg_pixel(x, y);
- uint8_t cidx = (fg_cidx & HH_MASK(3)) == 0 ? bg_cidx : fg_cidx;
- hh_ppu_data_t pal_rgb = g_hh_ppusim_vram[HH_PPU_VRAM_PAL_OFFSET + cidx];
- hh_ppu_rgb_color_t rgb = {
- HH_RESIZE(pal_rgb, 11, 8),
- HH_RESIZE(pal_rgb, 7, 4),
- HH_RESIZE(pal_rgb, 3, 0)
- };
+void hh_ppusim_pixel(uint8_t *s, unsigned x, unsigned y) {
+ uint8_t bg_cidx = hh_ppusim_bg_pixel(x, y);
+ uint8_t fg_cidx = hh_ppusim_fg_pixel(x, y);
+ uint8_t cidx = (fg_cidx & HH_MASK(3)) == 0 ? bg_cidx : fg_cidx;
+ hh_ppu_data_t pal_rgb = g_hh_ppusim_vram[HH_PPU_VRAM_PAL_OFFSET + cidx];
+ hh_ppu_rgb_color_t rgb = {HH_RESIZE(pal_rgb, 11, 8), HH_RESIZE(pal_rgb, 7, 4), HH_RESIZE(pal_rgb, 3, 0)};
s[0] = rgb[0] << 4;
s[1] = rgb[1] << 4;
s[2] = rgb[2] << 4;
}
-
diff --git a/src/ppusim/sim.c b/src/ppusim/sim.c
index 2816b31..a5fec45 100644
--- a/src/ppusim/sim.c
+++ b/src/ppusim/sim.c
@@ -1,26 +1,56 @@
-#include <stdlib.h>
-#include <stdbool.h>
#include <SDL2/SDL.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <stdio.h>
#include "main.h"
#include "ppu/ppu.h"
+#include "ppu/consts.h"
+#include "ppu/internals.h"
#include "ppusim/mem.h"
#include "ppusim/sim.h"
#include "ppusim/work.h"
-SDL_Window *g_hh_window = NULL;
+SDL_Window *g_hh_window = NULL;
SDL_Renderer *g_hh_renderer = NULL;
void hh_ppu_init() {
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
- g_hh_window = SDL_CreateWindow("ppusim", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, HH_PPU_SCREEN_WIDTH * HH_PPUSIM_UPSCALE_FACTOR, HH_PPU_SCREEN_HEIGHT * HH_PPUSIM_UPSCALE_FACTOR, SDL_WINDOW_SHOWN);
+ g_hh_window = SDL_CreateWindow("ppusim", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, HH_PPU_SCREEN_WIDTH * HH_PPUSIM_UPSCALE_FACTOR,
+ HH_PPU_SCREEN_HEIGHT * HH_PPUSIM_UPSCALE_FACTOR, SDL_WINDOW_SHOWN);
g_hh_renderer = SDL_CreateRenderer(g_hh_window, -1, SDL_RENDERER_ACCELERATED);
g_hh_ppusim_core_count = SDL_GetCPUCount();
- g_hh_ppusim_threads = malloc(sizeof(pthread_t) * g_hh_ppusim_core_count);
+ g_hh_ppusim_threads = malloc(sizeof(pthread_t) * g_hh_ppusim_core_count);
g_hh_ppusim_vram = malloc(sizeof(hh_ppu_data_t) * 0xffff);
memset(g_hh_ppusim_vram, 0x0000, 0xffff);
+ hh_ppu_load_tilemap();
+}
+
+void hh_ppu_load_tilemap() {
+ char* filename = "../test/bin/tiles.bin";
+ FILE* fp = fopen(filename,"rb");
+ if (!fp){
+ fprintf(stderr,"Error: Failed to load tiles.");
+ return;//error
+ }
+ int sprite_size = (HH_PPU_SPRITE_WIDTH * HH_PPU_SPRITE_HEIGHT);
+ fseek(fp, 0, SEEK_END);//goto EOF
+ int _size = ftell(fp)/sprite_size;
+ fseek(fp, 0, 0);//goto start of file
+
+ for (int i = 0; i < _size; i++) {
+ uint8_t data[sprite_size];
+
+ fread(data,sizeof(uint8_t),sprite_size,fp);
+
+ hh_s_ppu_vram_data sprite = hh_ppu_2nat_sprite(data);
+ sprite.offset = i*HH_PPU_VRAM_TMM_SPRITE_SIZE;
+ hh_ppu_vram_write(sprite);
+ free(sprite.data);
+ }
+ fclose(fp);
}
void hh_ppu_deinit() {
@@ -36,8 +66,9 @@ void hh_loop() {
SDL_Event e;
while (g_hh_run) {
uint32_t start = SDL_GetTicks();
- while (SDL_PollEvent(&e)) if (e.type == SDL_QUIT) exit(0);
-
+ while (SDL_PollEvent(&e))
+ if (e.type == SDL_QUIT) exit(0);
+
hh_ppu_vblank_interrupt();
SDL_RenderClear(g_hh_renderer);
diff --git a/src/ppusim/sim.h b/src/ppusim/sim.h
index 73f4b23..4d1d718 100644
--- a/src/ppusim/sim.h
+++ b/src/ppusim/sim.h
@@ -4,3 +4,6 @@
#define HH_PPUSIM_UPSCALE_FACTOR 3
/** @brief max framerate for PPUSIM */
#define HH_PPUSIM_FRAMERATE 60
+
+/** @brief pump tilemap from rom to ppu ram */
+void hh_ppu_load_tilemap(); //ppu sim?
diff --git a/src/ppusim/work.c b/src/ppusim/work.c
index fcb80ca..96d15aa 100644
--- a/src/ppusim/work.c
+++ b/src/ppusim/work.c
@@ -2,39 +2,41 @@
#include <pthread.h>
#include <stdio.h>
-#include "ppusim/work.h"
-#include "ppusim/sim.h"
#include "ppu/consts.h"
#include "ppusim/pixel.h"
+#include "ppusim/sim.h"
+#include "ppusim/work.h"
-pthread_t* g_hh_ppusim_threads;
+pthread_t *g_hh_ppusim_threads;
unsigned g_hh_ppusim_core_count;
hh_s_ppusim_screen g_hh_ppusim_screen;
-void* hh_ppusim_draw_thread(void* arg) {
- unsigned core = (unsigned long) arg;
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
+#pragma GCC diagnostic ignored "-Wpointer-to-int-cast"
+
+void *hh_ppusim_draw_thread(void *arg) {
+ unsigned core = (unsigned long)arg;
for (unsigned y = core; y < HH_PPU_SCREEN_HEIGHT; y += g_hh_ppusim_core_count)
- for (unsigned x = 0; x < HH_PPU_SCREEN_WIDTH; x++)
- hh_ppusim_pixel(g_hh_ppusim_screen[y][x], x, y);
+ for (unsigned x = 0; x < HH_PPU_SCREEN_WIDTH; x++) hh_ppusim_pixel(g_hh_ppusim_screen[y][x], x, y);
return NULL;
}
-void hh_ppusim_draw_frame(SDL_Renderer* renderer) {
- for (unsigned core = 0; core < g_hh_ppusim_core_count; core++)
- pthread_create(&g_hh_ppusim_threads[core], NULL, hh_ppusim_draw_thread, (void*)(unsigned long)core);
- for (unsigned core = 0; core < g_hh_ppusim_core_count; core++)
- pthread_join(g_hh_ppusim_threads[core], NULL);
-
+void hh_ppusim_draw_frame(SDL_Renderer *renderer) {
+ for (unsigned core = 0; core < g_hh_ppusim_core_count; core++) pthread_create(&g_hh_ppusim_threads[core], NULL, hh_ppusim_draw_thread, (void *)(unsigned long)core);
+ for (unsigned core = 0; core < g_hh_ppusim_core_count; core++) pthread_join(g_hh_ppusim_threads[core], NULL);
+
for (unsigned x = 0; x < HH_PPU_SCREEN_WIDTH; x++) {
for (unsigned y = 0; y < HH_PPU_SCREEN_HEIGHT; y++) {
SDL_SetRenderDrawColor(renderer, g_hh_ppusim_screen[y][x][0], g_hh_ppusim_screen[y][x][1], g_hh_ppusim_screen[y][x][2], 255);
- SDL_RenderFillRect(renderer, &(SDL_Rect) {
- .x = x * HH_PPUSIM_UPSCALE_FACTOR,
- .y = y * HH_PPUSIM_UPSCALE_FACTOR,
- .w = HH_PPUSIM_UPSCALE_FACTOR,
- .h = HH_PPUSIM_UPSCALE_FACTOR,
- });
+ SDL_RenderFillRect(renderer, &(SDL_Rect){
+ .x = x * HH_PPUSIM_UPSCALE_FACTOR,
+ .y = y * HH_PPUSIM_UPSCALE_FACTOR,
+ .w = HH_PPUSIM_UPSCALE_FACTOR,
+ .h = HH_PPUSIM_UPSCALE_FACTOR,
+ });
}
}
}
+#pragma GCC diagnostic pop
diff --git a/src/ppusim/work.h b/src/ppusim/work.h
index 6f72357..3a165ff 100644
--- a/src/ppusim/work.h
+++ b/src/ppusim/work.h
@@ -1,6 +1,7 @@
#pragma once
#include <SDL2/SDL.h>
+#include <pthread.h>
#include "ppu/consts.h"