aboutsummaryrefslogtreecommitdiffstats
path: root/src/uxn/devices/ppu.c
blob: d9208a578b4383ea69afd427abc7c4a247913904 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "ppu.h"

/*
Copyright (c) 2021 Devine Lu Linvega
Copyright (c) 2021 Andrew Alderwick
Copyright (c) 2021 Adrian "asie" Siekierka
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 Uint8 font[][8] = {
    {0x00, 0x7c, 0x82, 0x82, 0x82, 0x82, 0x82, 0x7c},
    {0x00, 0x30, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10},
    {0x00, 0x7c, 0x82, 0x02, 0x7c, 0x80, 0x80, 0xfe},
    {0x00, 0x7c, 0x82, 0x02, 0x1c, 0x02, 0x82, 0x7c},
    {0x00, 0x0c, 0x14, 0x24, 0x44, 0x84, 0xfe, 0x04},
    {0x00, 0xfe, 0x80, 0x80, 0x7c, 0x02, 0x82, 0x7c},
    {0x00, 0x7c, 0x82, 0x80, 0xfc, 0x82, 0x82, 0x7c},
    {0x00, 0x7c, 0x82, 0x02, 0x1e, 0x02, 0x02, 0x02},
    {0x00, 0x7c, 0x82, 0x82, 0x7c, 0x82, 0x82, 0x7c},
    {0x00, 0x7c, 0x82, 0x82, 0x7e, 0x02, 0x82, 0x7c},
    {0x00, 0x7c, 0x82, 0x02, 0x7e, 0x82, 0x82, 0x7e},
    {0x00, 0xfc, 0x82, 0x82, 0xfc, 0x82, 0x82, 0xfc},
    {0x00, 0x7c, 0x82, 0x80, 0x80, 0x80, 0x82, 0x7c},
    {0x00, 0xfc, 0x82, 0x82, 0x82, 0x82, 0x82, 0xfc},
    {0x00, 0x7c, 0x82, 0x80, 0xf0, 0x80, 0x82, 0x7c},
    {0x00, 0x7c, 0x82, 0x80, 0xf0, 0x80, 0x80, 0x80}};

u32 *backbuffer_bg0;
u32 *backbuffer_bg1;
static u8 dirty_tiles[20][30] = {0};

void
putcolors(Ppu *p, Uint8 *addr) {
    int i;
    for(i = 0; i < 4; ++i) {
        Uint8
            r = (*(addr + i / 2) >> (!(i % 2) << 2)) & 0x0f,
            g = (*(addr + 2 + i / 2) >> (!(i % 2) << 2)) & 0x0f,
            b = (*(addr + 4 + i / 2) >> (!(i % 2) << 2)) & 0x0f;
        PAL_BUFFER_BG[i] = rgb15(
                (r << 1) | (r >> 3),
                (g << 1) | (g >> 3),
                (b << 1) | (b >> 3));
    }
}

void
putpixel(Ppu *p, Uint32 *layer, Uint16 x, Uint16 y, Uint8 color) {
    Uint32 pos = ((y & 7) + (((x >> 3) + (y >> 3) * 30) * 8));
    Uint32 shift = (x & 7) << 2;
    layer[pos] = (layer[pos] & (~(0xF << shift))) | (color << shift);
    dirty_tiles[y >> 3][x >> 3] = 1;
}

void
puticn(Ppu *p, Uint32 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color,
        Uint8 flipx, Uint8 flipy) {
    Uint16 v, h;
    for(v = 0; v < 8; v++) {
        for(h = 0; h < 8; h++) {
            Uint8 ch1 = ((sprite[v] >> (7 - h)) & 0x1);
            // if(ch1 == 1 || (color != 0x05 && color != 0x0a && color != 0x0f))
                // putpixel(p,
                //     layer,
                //     x + (flipx ? 7 - h : h),
                //     y + (flipy ? 7 - v : v),
                //     ch1 ? color % 4 : color / 4);
            // TODO: Add flip options directly to the screenblock?
            putpixel(p,
                layer,
                x + h,
                y + v,
                ch1 ? color & 0x3 : color >> 2);
        }
    }
}

void
putchr(Ppu *p, Uint32 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color,
        Uint8 flipx, Uint8 flipy) {
    Uint16 v, h;
    for(v = 0; v < 8; v++)
        for(h = 0; h < 8; h++) {
            Uint8 ch1 = ((sprite[v] >> (7 - h)) & 0x1) * color;
            Uint8 ch2 = ((sprite[v + 8] >> (7 - h)) & 0x1) * color;
            putpixel(p,
                layer,
                x + (flipx ? 7 - h : h),
                y + (flipy ? 7 - v : v),
                (((ch1 + ch2 * 2) + color / 4) & 0x3));
        }
}

void
flipbuf(Ppu *p) {
    Tile *mem_fg = &TILE_MEM[0];
    Tile *mem_bg = &TILE_MEM[2];
    for (size_t j = 0; j < 20; ++j) {
        for (size_t i = 0; i < 30; ++i) {
            if (dirty_tiles[j][i] != 0) {
                Tile *tile_fg = p->fg;
                Tile *tile_bg = p->bg;
                mem_fg[i + j * 30] = tile_fg[i + j * 30];
                mem_bg[i + j * 30] = tile_bg[i + j * 30];
                dirty_tiles[j][i] = 0;
            }
        }
    }
}

int
initppu(Ppu *p, Uint8 hor, Uint8 ver, Uint8 pad) {
    p->hor = hor;
    p->ver = ver;
    p->pad = pad;
    p->width = (8 * p->hor + p->pad * 2);
    p->height = (8 * p->ver + p->pad * 2);

    // Initialize display mode and bg palette.
    DISP_CTRL = DISP_MODE_0 | DISP_BG_0 | DISP_BG_1;

    // Initialize backgrounds.
    u8 cb_fg = 0;
    u8 cb_bg = 2;
    u8 sb_idx = 10;
    BG_CTRL(0) = BG_CHARBLOCK(cb_fg) | BG_SCREENBLOCK(sb_idx) | BG_PRIORITY(1);
    BG_CTRL(1) = BG_CHARBLOCK(cb_bg) | BG_SCREENBLOCK(sb_idx) | BG_PRIORITY(2);

    backbuffer_bg0 = malloc(30 * 20 * sizeof(Tile));
    backbuffer_bg1 = malloc(30 * 20 * sizeof(Tile));

    // Clear tile memory.
    p->fg = &TILE_MEM[cb_fg];
    p->bg = &TILE_MEM[cb_bg];
    p->fg = backbuffer_bg0;
    p->bg = backbuffer_bg1;
    for (size_t i = 0; i < 30 * 20 * 8; ++i) {
        p->fg[i] = 0;
        p->bg[i] = 0;
    }

    // Initialize default palette.
    PAL_BUFFER_BG[0] = COLOR_BLACK;
    PAL_BUFFER_BG[1] = COLOR_WHITE;
    PAL_BUFFER_BG[2] = COLOR_RED;
    PAL_BUFFER_BG[3] = COLOR_BLUE;

    // Initialize memory map.
    u16 *mem_map = SCREENBLOCK_MEM[sb_idx];
    size_t k = 0;
    for (size_t j = 0; j < 20; ++j) {
        for (size_t i = 0; i < 30; ++i, ++k) {
            mem_map[i + j * 32] = k;
        }
    }

    return 1;
}