aboutsummaryrefslogtreecommitdiff
path: root/GPU2D.cpp
blob: 6b954bf865ebb521b5d8b36f7604d1149ff02c23 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
    Copyright 2016-2017 StapleButter

    This file is part of melonDS.

    melonDS is free software: you can redistribute it and/or modify it under
    the terms of the GNU General Public License as published by the Free
    Software Foundation, either version 3 of the License, or (at your option)
    any later version.

    melonDS is distributed in the hope that it will be useful, but WITHOUT ANY
    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with melonDS. If not, see http://www.gnu.org/licenses/.
*/

#include <stdio.h>
#include <string.h>
#include "NDS.h"
#include "GPU.h"


GPU2D::GPU2D(u32 num)
{
    Num = num;
}

GPU2D::~GPU2D()
{
}

void GPU2D::Reset()
{
    DispCnt = 0;
    memset(BGCnt, 0, 4*2);
}

void GPU2D::SetFramebuffer(u16* buf)
{
    // framebuffer is 256x192 16bit.
    // might eventually support other framebuffer types/sizes
    Framebuffer = buf;
}


u8 GPU2D::Read8(u32 addr)
{
    printf("!! GPU2D READ8 %08X\n", addr);
    return 0;
}

u16 GPU2D::Read16(u32 addr)
{
    switch (addr & 0x00000FFF)
    {
    case 0x000: return DispCnt&0xFFFF;
    case 0x002: return DispCnt>>16;

    case 0x008: return BGCnt[0];
    case 0x00A: return BGCnt[1];
    case 0x00C: return BGCnt[2];
    case 0x00E: return BGCnt[3];
    }

    printf("unknown GPU read16 %08X\n", addr);
    return 0;
}

u32 GPU2D::Read32(u32 addr)
{
    switch (addr & 0x00000FFF)
    {
    case 0x000: return DispCnt;
    }

    return Read16(addr) | (Read16(addr+2) << 16);
}

void GPU2D::Write8(u32 addr, u8 val)
{
    printf("!! GPU2D WRITE8 %08X %02X\n", addr, val);
}

void GPU2D::Write16(u32 addr, u16 val)
{
    switch (addr & 0x00000FFF)
    {
    case 0x000:
        DispCnt = (DispCnt & 0xFFFF0000) | val;
        return;
    case 0x002:
        DispCnt = (DispCnt & 0x0000FFFF) | (val << 16);
        return;

    case 0x008: BGCnt[0] = val; return;
    case 0x00A: BGCnt[1] = val; return;
    case 0x00C: BGCnt[2] = val; return;
    case 0x00E: BGCnt[3] = val; return;
    }

    printf("unknown GPU write16 %08X %04X\n", addr, val);
}

void GPU2D::Write32(u32 addr, u32 val)
{
    switch (addr & 0x00000FFF)
    {
    case 0x000:
        DispCnt = val;
        return;
    }

    Write16(addr, val&0xFFFF);
    Write16(addr+2, val>>16);
}


void GPU2D::DrawScanline(u32 line)
{
    u16* dst = &Framebuffer[256*line];

    u32 dispmode = DispCnt >> 16;
    dispmode &= (Num ? 0x1 : 0x3);

    switch (dispmode)
    {
    case 0: // screen off
        {
            for (int i = 0; i < 256>>1; i++)
                ((u32*)dst)[i] = 0x7FFF7FFF;
        }
        break;

    case 1: // regular display
        {
            DrawScanline_Mode1(line, dst);
        }
        break;

    case 2: // VRAM display
        {
            u32* vram = (u32*)GPU::VRAM[(DispCnt >> 18) & 0x3];
            vram = &vram[line << 7];

            for (int i = 0; i < 256>>1; i++)
                ((u32*)dst)[i] = vram[i];
        }
        break;

    case 3: // FIFO display
        {
            // uh, is there even anything that uses this?
        }
        break;
    }
}

void GPU2D::DrawScanline_Mode1(u32 line, u16* dst)
{
    for (int i = 0; i < 256>>1; i++)
        ((u32*)dst)[i] = 0; // TODO: backdrop

    switch (DispCnt & 0x7)
    {
    case 0:
        for (int i = 3; i >= 0; i--)
        {
            // TODO other BGs
            if ((BGCnt[0] & 0x3) == i)
            {
                DrawBG_Text_4bpp(line, dst, 0);
                // todo: sprites
            }
        }
        break;
    }
}

// char   06218000
// screen 06208000
void GPU2D::DrawBG_Text_4bpp(u32 line, u16* dst, u32 bgnum)
{
    u16 bgcnt = BGCnt[bgnum];

    u8* tileset;
    u16* tilemap;
    u16* pal;

    // TODO scroll
    u16 xoff = 0;
    u16 yoff = line;

    u32 widexmask = (bgcnt & 0x4000) ? 0x100 : 0;
    //u32 ymask = (bgcnt & 0x8000) ? 0x1FF : 0xFF;

    if (Num)
    {
        tileset = (u8*)GPU::VRAM_BBG[((bgcnt & 0x000C) >> 2)];
        tilemap = (u16*)GPU::VRAM_BBG[((bgcnt & 0x1800) >> 11)];
        tilemap += ((bgcnt & 0x0700) << 2);

        pal = (u16*)&GPU::Palette[0x400];
    }
    else
    {
        tileset = (u8*)GPU::VRAM_ABG[((DispCnt & 0x07000000) >> 22) + ((bgcnt & 0x003C) >> 2)];
        tilemap = (u16*)GPU::VRAM_ABG[((DispCnt & 0x38000000) >> 27) + ((bgcnt & 0x1800) >> 11)];
        tilemap += ((bgcnt & 0x0700) << 2);

        pal = (u16*)&GPU::Palette[0];
    }

    // adjust Y position in tilemap
    if (bgcnt & 0x8000)
    {
        tilemap += ((yoff & 0x1F8) << 2);
        if (bgcnt & 0x4000)
            tilemap += ((yoff & 0x100) << 2);
    }
    else
        tilemap += ((yoff & 0xF8) << 2);

    u16 curtile;
    u16* curpal;
    u8* pixels;

    // preload shit as needed
    if (xoff & 0x7)
    {
        // load a new tile
        curtile = tilemap[((xoff & 0xFF) >> 3) + ((xoff & widexmask) << 2)];
        curpal = pal + ((curtile & 0xF000) >> 8);
        pixels = tileset + ((curtile & 0x01FF) << 5) + ((yoff & 0x7) << 2);
        pixels += ((xoff & 0x7) >> 1);
    }

    for (int i = 0; i < 256; i++)
    {
        if (!(xoff & 0x7))
        {
            // load a new tile
            curtile = tilemap[((xoff & 0xFF) >> 3) + ((xoff & widexmask) << 2)];
            curpal = pal + ((curtile & 0xF000) >> 8);
            pixels = tileset + ((curtile & 0x01FF) << 5) + ((yoff & 0x7) << 2);
        }

        // draw pixel
        u8 color;
        if (xoff & 0x1)
        {
            color = *pixels >> 4;
            pixels++;
        }
        else
        {
            color = *pixels & 0x0F;
        }
        //color = (i >> 4) + ((line >> 4) << 4);
        //if (Num) color = 0;
        dst[i] = curpal[color];

        xoff++;
    }
}