aboutsummaryrefslogtreecommitdiffstats
path: root/src/ppu.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ppu.c')
-rw-r--r--src/ppu.c85
1 files changed, 80 insertions, 5 deletions
diff --git a/src/ppu.c b/src/ppu.c
index 2e0404f..aeb3699 100644
--- a/src/ppu.c
+++ b/src/ppu.c
@@ -1,14 +1,85 @@
1#include "common.h" 1#include "common.h"
2#include "ppu.h"
3
4
5/*
6Copyright (c) 2021 Devine Lu Linvega
7Copyright (c) 2021 Andrew Alderwick
8Copyright (c) 2021 Bad Diode
9
10Permission to use, copy, modify, and distribute this software for any
11purpose with or without fee is hereby granted, provided that the above
12copyright notice and this permission notice appear in all copies.
13
14THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15WITH REGARD TO THIS SOFTWARE.
16*/
17
18#define SCREEN_WIDTH 64 * 8 / 2
19#define SCREEN_HEIGHT 40 * 8 / 2
2 20
3static u32 *framebuffer = 0; 21static u32 *framebuffer = 0;
4 22
5#define SCREEN_WIDTH 800 23static u32 palette[16];
6#define SCREEN_HEIGHT 600 24static u8 pixels_buf[SCREEN_WIDTH * SCREEN_HEIGHT];
7 25
8static u32 palette[4]; 26static Uint8 blending[5][16] = {
27 {0, 0, 0, 0, 1, 0, 1, 1, 2, 2, 0, 2, 3, 3, 3, 0},
28 {0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3},
29 {1, 2, 3, 1, 1, 2, 3, 1, 1, 2, 3, 1, 1, 2, 3, 1},
30 {2, 3, 1, 2, 2, 3, 1, 2, 2, 3, 1, 2, 2, 3, 1, 2},
31 {1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0}};
9 32
10void 33void
11ppu_init(void) { 34ppu_pixel(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 color)
35{
36 size_t idx = y * p->width + x;
37 Uint8 *pixel = &p->pixels[idx], shift = layer * 2;
38 if(x < p->width && y < p->height) {
39 *pixel = (*pixel & ~(0x3 << shift)) | (color << shift);
40 // framebuffer[idx] = palette[*pixel];
41 }
42}
43
44void
45ppu_1bpp(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
46{
47 Uint16 v, h;
48 for(v = 0; v < 8; v++)
49 for(h = 0; h < 8; h++) {
50 Uint8 ch1 = (sprite[v] >> (7 - h)) & 0x1;
51 if(ch1 || blending[4][color])
52 ppu_pixel(p,
53 layer,
54 x + (flipx ? 7 - h : h),
55 y + (flipy ? 7 - v : v),
56 blending[ch1][color]);
57 }
58}
59
60void
61ppu_2bpp(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
62{
63 Uint16 v, h;
64 for(v = 0; v < 8; v++)
65 for(h = 0; h < 8; h++) {
66 Uint8 ch1 = ((sprite[v] >> (7 - h)) & 0x1);
67 Uint8 ch2 = ((sprite[v + 8] >> (7 - h)) & 0x1);
68 Uint8 ch = ch1 + ch2 * 2;
69 if(ch || blending[4][color])
70 ppu_pixel(p,
71 layer,
72 x + (flipx ? 7 - h : h),
73 y + (flipy ? 7 - v : v),
74 blending[ch][color]);
75 }
76}
77
78int
79ppu_init(Ppu *p, Uint8 hor, Uint8 ver) {
80 p->width = 8 * hor;
81 p->height = 8 * ver;
82
12 // Initialize the framebuffer. 83 // Initialize the framebuffer.
13 static MboxFramebufferRequest fb_request = { 84 static MboxFramebufferRequest fb_request = {
14 .buf_size = 96, 85 .buf_size = 96,
@@ -53,12 +124,16 @@ ppu_init(void) {
53 framebuffer = (u32*)((uintptr_t)fb_request.framebuffer_tag.fb_addr); 124 framebuffer = (u32*)((uintptr_t)fb_request.framebuffer_tag.fb_addr);
54 } else { 125 } else {
55 uart_puts("Unable initialize framebuffer\n"); 126 uart_puts("Unable initialize framebuffer\n");
56 return; 127 return 0;
57 } 128 }
58 129
130 p->pixels = pixels_buf;
131
59 // Initialize default palette. 132 // Initialize default palette.
60 palette[0] = 0x00444444; 133 palette[0] = 0x00444444;
61 palette[1] = 0x00ffffff; 134 palette[1] = 0x00ffffff;
62 palette[2] = 0x007777ff; 135 palette[2] = 0x007777ff;
63 palette[3] = 0x00ff7777; 136 palette[3] = 0x00ff7777;
137
138 return 1;
64} 139}