aboutsummaryrefslogtreecommitdiff
path: root/src/ppu/ppu.c
blob: e32c188208a0889d525cc9bf741a85331b415f76 (plain)
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
#include <stdlib.h>

#include "ppu/consts.h"
#include "ppu/internals.h"
#include "ppu/ppu.h"

hh_s_ppu_loc_aux g_hh_aux = { 0 };

void hh_ppu_update_foreground(unsigned index, hh_s_ppu_loc_fam_entry e) {
	hh_s_ppu_vram_data s = hh_ppu_2nat_fam(e);
	s.offset			 = HH_PPU_VRAM_FAM_OFFSET + HH_PPU_VRAM_FAM_ENTRY_SIZE * index;
	hh_ppu_vram_write(s);
	free(s.data);
}

void hh_ppu_update_background(unsigned index, hh_s_ppu_loc_bam_entry e) {
	hh_s_ppu_vram_data s = hh_ppu_2nat_bam(e);
	s.offset			 = HH_PPU_VRAM_BAM_OFFSET + HH_PPU_VRAM_BAM_ENTRY_SIZE * index;
	hh_ppu_vram_write(s);
	free(s.data);
}

void hh_ppu_update_sprite(unsigned tilemap_index, const hh_s_ppu_loc_sprite sprite) {
	if (tilemap_index < 16) g_hh_aux.fg_fetch = true;
	hh_s_ppu_vram_data s = hh_ppu_2nat_sprite(sprite);
	s.offset			 = HH_PPU_VRAM_TMM_OFFSET + HH_PPU_VRAM_TMM_SPRITE_SIZE * tilemap_index;
	hh_ppu_vram_write(s);
	free(s.data);
}

void hh_ppu_update_background_pos(uint16_t shift_x, uint16_t shift_y) {
	g_hh_aux.bg_shift_x = shift_x;
	g_hh_aux.bg_shift_y = shift_y;
}

void hh_ppu_update_palette_table(hh_ppu_loc_palette_table_t table) {
	for (unsigned i = 0; i < HH_PPU_PALETTE_COUNT; i++) hh_ppu_update_palette(i, table[i]);
}

void hh_ppu_update_palette(unsigned palette_index, hh_ppu_loc_palette_data_t palette) {
	for (unsigned i = 0; i < HH_PPU_PALETTE_COLOR_COUNT; i++) hh_ppu_update_color(palette_index, i, palette[i]);
}

void hh_ppu_update_color(unsigned palette_index, unsigned color_index, hh_ppu_rgb_color_t color) {
	hh_s_ppu_vram_data c = hh_ppu_2nat_color(color);
	c.offset			 = HH_PPU_VRAM_PAL_OFFSET + palette_index * HH_PPU_VRAM_PAL_ENTRY_SIZE * HH_PPU_PALETTE_COLOR_COUNT + color_index * HH_PPU_VRAM_PAL_ENTRY_SIZE;
	hh_ppu_vram_write(c);
	free(c.data);
}

void hh_ppu_flush() {
	hh_ppu_update_aux(g_hh_aux);
	if (g_hh_aux.fg_fetch) {
		g_hh_aux.fg_fetch = false;
		hh_ppu_update_aux(g_hh_aux);
	}
	hh_ppu_vram_flush();
}