aboutsummaryrefslogtreecommitdiffstats
path: root/src/ppu.c
blob: a8d7dc676764f5dbf160d9aaa01414159b59de19 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <string.h>

#include "ppu.h"

/*
Copyright (c) 2021 Devine Lu Linvega
Copyright (c) 2021 Andrew Alderwick
Copyright (c) 2021 Bad Diode

Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE.
*/

static size_t screen_width = 0;
static size_t screen_height = 0;

static u32 *framebuffer = 0;

static u32 palette[16];
static u8 *pixels_fg;
static u8 *pixels_bg;
static u8 *dirty_lines;
static u8 reqdraw = 0;
// TODO: Probably should consider this
static u32 rgb_order = 0;

static u8 blending[5][16] = {
    {0, 0, 0, 0, 1, 0, 1, 1, 2, 2, 0, 2, 3, 3, 3, 0},
    {0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3},
    {1, 2, 3, 1, 1, 2, 3, 1, 1, 2, 3, 1, 1, 2, 3, 1},
    {2, 3, 1, 2, 2, 3, 1, 2, 2, 3, 1, 2, 2, 3, 1, 2},
    {1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0}};

void
ppu_pixel(u8 *layer, u16 x, u16 y, u8 color) {
	if(x < screen_width && y < screen_height) {
		Uint32 i = x + y *screen_width;
		if(color != layer[i]) {
			layer[i] = color;
		}
	}
    dirty_lines[y] |= 1;
}

void
ppu_1bpp(u8 *layer, u16 x, u16 y, u8 *sprite, u8 color, u8 flipx, u8 flipy) {
    u16 v, h;
    for(v = 0; v < 8; v++)
        for(h = 0; h < 8; h++) {
            u8 ch1 = (sprite[v] >> (7 - h)) & 0x1;
            if(ch1 || blending[4][color])
                ppu_pixel(layer,
                    x + (flipx ? 7 - h : h),
                    y + (flipy ? 7 - v : v),
                    blending[ch1][color]);
        }
}

void
ppu_2bpp(u8 *layer, u16 x, u16 y, u8 *sprite, u8 color, u8 flipx, u8 flipy) {
    u16 v, h;
    for(v = 0; v < 8; v++)
        for(h = 0; h < 8; h++) {
            u8 ch1 = ((sprite[v] >> (7 - h)) & 0x1);
            u8 ch2 = ((sprite[v + 8] >> (7 - h)) & 0x1);
            u8 ch = ch1 + ch2 * 2;
            if(ch || blending[4][color])
                ppu_pixel(layer,
                    x + (flipx ? 7 - h : h),
                    y + (flipy ? 7 - v : v),
                    blending[ch][color]);
        }
}

void
redraw_screen(void) {
    for (size_t j = 0; j < screen_height; j++) {
        dirty_lines[j] = 1;
    }
}

int
ppu_init(void) {
	// Open frambuffer and get the size.
	int fb = open("/dev/fb0", O_RDWR);
	if (fb <= 0) {
		fprintf(stderr, "couldn't open the framebuffer\n");
		exit(EXIT_FAILURE);
	}
	struct fb_var_screeninfo info;
	if (ioctl(fb, FBIOGET_VSCREENINFO, &info) != 0) {
		fprintf(stderr, "couldn't get the framebuffer size\n");
		exit(EXIT_FAILURE);
	}

	// Mmap the framebuffer to a buffer object.
	screen_width = info.xres;
	screen_height = info.yres;
	size_t len = 4 * screen_width * screen_height;
	framebuffer = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fb, 0);
	if (framebuffer == MAP_FAILED) {
		fprintf(stderr, "couldn't mmap the framebuffer\n");
		exit(EXIT_FAILURE);
	}

	// Allocate intermediate buffers.
	pixels_fg = malloc(screen_width * screen_height);
	pixels_bg = malloc(screen_width * screen_height);
	dirty_lines = malloc(screen_height);

    // Initialize default palette.
    palette[0] = 0x444444;
    palette[1] = 0xffffff;
    palette[2] = 0x7777ff;
    palette[3] = 0xff7777;

    // Clear pixel buffer memory.
    memset(pixels_fg, 0, screen_width * screen_height);
    memset(pixels_bg, 0, screen_width * screen_height);
    memset(dirty_lines, 1, screen_height);

    // Make sure we perform an initial screen drawing.
    reqdraw = 1;
    redraw_screen();

    return 1;
}

void
blit_framebuffer(void) {
    if (reqdraw == 0) {
        return;
    }
    for (size_t j = 0; j < screen_height; j++) {
        if (dirty_lines[j] != 0) {
            for (size_t i = 0; i < screen_width; i++) {
                size_t idx = i + j * screen_width;
                framebuffer[idx] = palette[pixels_fg[idx] << 2 | pixels_bg[idx]];
            }
        }
        dirty_lines[j] = 0;
    }
    reqdraw = 0;
}