aboutsummaryrefslogtreecommitdiff
path: root/src/ppusim
diff options
context:
space:
mode:
Diffstat (limited to 'src/ppusim')
-rw-r--r--src/ppusim/input.c13
-rw-r--r--src/ppusim/mem.c4
-rw-r--r--src/ppusim/pixel.c77
-rw-r--r--src/ppusim/sim.c16
-rw-r--r--src/ppusim/work.c35
5 files changed, 69 insertions, 76 deletions
diff --git a/src/ppusim/input.c b/src/ppusim/input.c
index bdb94ac..08bc382 100644
--- a/src/ppusim/input.c
+++ b/src/ppusim/input.c
@@ -2,15 +2,14 @@
#include "input.h"
-hh_s_gamepad g_hh_controller_p1 = { 0 };
-hh_s_gamepad g_hh_controller_p2 = { 0 };
+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];
+ 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];
}
-
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..449b78d 100644
--- a/src/ppusim/sim.c
+++ b/src/ppusim/sim.c
@@ -1,6 +1,6 @@
-#include <stdlib.h>
-#include <stdbool.h>
#include <SDL2/SDL.h>
+#include <stdbool.h>
+#include <stdlib.h>
#include "main.h"
#include "ppu/ppu.h"
@@ -8,16 +8,17 @@
#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);
@@ -36,8 +37,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/work.c b/src/ppusim/work.c
index 3b9cee6..96d15aa 100644
--- a/src/ppusim/work.c
+++ b/src/ppusim/work.c
@@ -2,12 +2,12 @@
#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;
@@ -15,29 +15,26 @@ hh_s_ppusim_screen g_hh_ppusim_screen;
#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;
+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,
+ });
}
}
}