include/mgba/internal/ds/video.h (view raw)
1/* Copyright (c) 2013-2017 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#ifndef DS_VIDEO_H
7#define DS_VIDEO_H
8
9#include <mgba-util/common.h>
10
11CXX_GUARD_START
12
13#include <mgba/core/log.h>
14#include <mgba/core/timing.h>
15#include <mgba/internal/gba/video.h>
16
17mLOG_DECLARE_CATEGORY(DS_VIDEO);
18
19enum {
20 DS_VIDEO_HORIZONTAL_PIXELS = 256,
21 DS_VIDEO_HBLANK_PIXELS = 99,
22 DS7_VIDEO_HBLANK_LENGTH = 1613,
23 DS9_VIDEO_HBLANK_LENGTH = 1606,
24 DS_VIDEO_HORIZONTAL_LENGTH = (DS_VIDEO_HORIZONTAL_PIXELS + DS_VIDEO_HBLANK_PIXELS) * 6,
25
26 DS_VIDEO_VERTICAL_PIXELS = 192,
27 DS_VIDEO_VBLANK_PIXELS = 71,
28 DS_VIDEO_VERTICAL_TOTAL_PIXELS = DS_VIDEO_VERTICAL_PIXELS + DS_VIDEO_VBLANK_PIXELS,
29
30 DS_VIDEO_TOTAL_LENGTH = DS_VIDEO_HORIZONTAL_LENGTH * DS_VIDEO_VERTICAL_TOTAL_PIXELS,
31};
32
33union DSOAM {
34 union GBAOAM oam[2];
35 uint16_t raw[1024];
36};
37
38DECL_BITFIELD(DSRegisterDISPCNT, uint32_t);
39DECL_BITS(DSRegisterDISPCNT, Mode, 0, 3);
40DECL_BIT(DSRegisterDISPCNT, 3D, 3);
41DECL_BIT(DSRegisterDISPCNT, TileObjMapping, 4);
42DECL_BIT(DSRegisterDISPCNT, BitmapObj2D, 5);
43DECL_BIT(DSRegisterDISPCNT, BitmapObjMapping, 6);
44DECL_BIT(DSRegisterDISPCNT, ForcedBlank, 7);
45DECL_BIT(DSRegisterDISPCNT, Bg0Enable, 8);
46DECL_BIT(DSRegisterDISPCNT, Bg1Enable, 9);
47DECL_BIT(DSRegisterDISPCNT, Bg2Enable, 10);
48DECL_BIT(DSRegisterDISPCNT, Bg3Enable, 11);
49DECL_BIT(DSRegisterDISPCNT, ObjEnable, 12);
50DECL_BIT(DSRegisterDISPCNT, Win0Enable, 13);
51DECL_BIT(DSRegisterDISPCNT, Win1Enable, 14);
52DECL_BIT(DSRegisterDISPCNT, ObjwinEnable, 15);
53DECL_BITS(DSRegisterDISPCNT, DispMode, 16, 2);
54DECL_BITS(DSRegisterDISPCNT, VRAMBlock, 18, 2);
55DECL_BITS(DSRegisterDISPCNT, TileBoundary, 20, 2);
56// TODO
57DECL_BITS(DSRegisterDISPCNT, CharBase, 24, 3);
58DECL_BITS(DSRegisterDISPCNT, ScreenBase, 27, 3);
59// TODO
60DECL_BIT(DSRegisterDISPCNT, BgExtPalette, 30);
61DECL_BIT(DSRegisterDISPCNT, ObjExtPalette, 31);
62
63DECL_BITFIELD(DSRegisterDISPCAPCNT, uint32_t);
64DECL_BITS(DSRegisterDISPCAPCNT, EVA, 0, 4);
65DECL_BITS(DSRegisterDISPCAPCNT, EVB, 8, 4);
66DECL_BITS(DSRegisterDISPCAPCNT, WriteBlock, 16, 2);
67DECL_BITS(DSRegisterDISPCAPCNT, WriteOffset, 18, 2);
68DECL_BITS(DSRegisterDISPCAPCNT, CaptureSize, 20, 2);
69DECL_BIT(DSRegisterDISPCAPCNT, SourceA, 24);
70DECL_BIT(DSRegisterDISPCAPCNT, SourceB, 25);
71DECL_BITS(DSRegisterDISPCAPCNT, ReadOffset, 26, 2);
72DECL_BITS(DSRegisterDISPCAPCNT, CaptureSource, 29, 2);
73DECL_BIT(DSRegisterDISPCAPCNT, Enable, 31);
74
75DECL_BITFIELD(DSRegisterPOWCNT1, uint16_t);
76// TODO
77DECL_BIT(DSRegisterPOWCNT1, Swap, 15);
78
79DECL_BIT(GBARegisterBGCNT, ExtendedMode0, 2);
80DECL_BIT(GBARegisterBGCNT, ExtendedMode1, 7);
81DECL_BIT(GBARegisterBGCNT, ExtPaletteSlot, 13);
82
83DECL_BITFIELD(DSRegisterMASTER_BRIGHT, uint16_t);
84DECL_BITS(DSRegisterMASTER_BRIGHT, Y, 0, 5);
85DECL_BITS(DSRegisterMASTER_BRIGHT, Mode, 14, 2);
86
87struct DSGX;
88struct DSVideoRenderer {
89 void (*init)(struct DSVideoRenderer* renderer);
90 void (*reset)(struct DSVideoRenderer* renderer);
91 void (*deinit)(struct DSVideoRenderer* renderer);
92
93 uint16_t (*writeVideoRegister)(struct DSVideoRenderer* renderer, uint32_t address, uint16_t value);
94 void (*writePalette)(struct DSVideoRenderer* renderer, uint32_t address, uint16_t value);
95 void (*writeOAM)(struct DSVideoRenderer* renderer, uint32_t oam);
96 void (*invalidateExtPal)(struct DSVideoRenderer* renderer, bool obj, bool engB, int slot);
97 void (*drawScanline)(struct DSVideoRenderer* renderer, int y);
98 void (*finishFrame)(struct DSVideoRenderer* renderer);
99
100 void (*getPixels)(struct DSVideoRenderer* renderer, size_t* stride, const void** pixels);
101 void (*putPixels)(struct DSVideoRenderer* renderer, size_t stride, const void* pixels);
102
103 uint16_t* palette;
104 uint16_t* vram;
105 uint16_t* vramABG[32];
106 uint16_t* vramAOBJ[32];
107 uint16_t* vramABGExtPal[4];
108 uint16_t* vramAOBJExtPal;
109 uint16_t* vramBBG[32];
110 uint16_t* vramBOBJ[32];
111 uint16_t* vramBBGExtPal[4];
112 uint16_t* vramBOBJExtPal;
113 union DSOAM* oam;
114 struct DSGX* gx;
115};
116
117struct DS;
118struct DSVideo {
119 struct DS* p;
120 struct DSVideoRenderer* renderer;
121 struct mTimingEvent event7;
122 struct mTimingEvent event9;
123
124 int vcount;
125 bool inCapture;
126
127 uint16_t palette[1024];
128 uint16_t* vram;
129 uint16_t* vramABG[32];
130 uint16_t* vramAOBJ[32];
131 uint16_t* vramABGExtPal[4];
132 uint16_t* vramAOBJExtPal;
133 uint16_t* vramBBG[32];
134 uint16_t* vramBOBJ[32];
135 uint16_t* vramBBGExtPal[4];
136 uint16_t* vramBOBJExtPal;
137 union DSOAM oam;
138
139 int32_t frameCounter;
140 int frameskip;
141 int frameskipCounter;
142};
143
144void DSVideoInit(struct DSVideo* video);
145void DSVideoReset(struct DSVideo* video);
146void DSVideoDeinit(struct DSVideo* video);
147void DSVideoAssociateRenderer(struct DSVideo* video, struct DSVideoRenderer* renderer);
148
149struct DSCommon;
150void DSVideoWriteDISPSTAT(struct DSCommon* dscore, uint16_t value);
151
152struct DSMemory;
153void DSVideoConfigureVRAM(struct DS* ds, int index, uint8_t value, uint8_t oldValue);
154
155CXX_GUARD_START
156
157#endif