include/mgba/internal/gb/video.h (view raw)
1/* Copyright (c) 2013-2016 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 GB_VIDEO_H
7#define GB_VIDEO_H
8
9#include <mgba-util/common.h>
10
11CXX_GUARD_START
12
13#include <mgba/core/timing.h>
14#include <mgba/gb/interface.h>
15
16enum {
17 GB_VIDEO_HORIZONTAL_PIXELS = 160,
18 GB_VIDEO_VERTICAL_PIXELS = 144,
19 GB_VIDEO_VBLANK_PIXELS = 10,
20 GB_VIDEO_VERTICAL_TOTAL_PIXELS = 154,
21
22 // TODO: Figure out exact lengths
23 GB_VIDEO_MODE_2_LENGTH = 76,
24 GB_VIDEO_MODE_3_LENGTH_BASE = 171,
25 GB_VIDEO_MODE_0_LENGTH_BASE = 209,
26
27 GB_VIDEO_HORIZONTAL_LENGTH = 456,
28
29 GB_VIDEO_MODE_1_LENGTH = 65664,
30 GB_VIDEO_TOTAL_LENGTH = 70224,
31
32 GB_BASE_MAP = 0x1800,
33 GB_SIZE_MAP = 0x0400
34};
35
36DECL_BITFIELD(GBObjAttributes, uint8_t);
37DECL_BITS(GBObjAttributes, CGBPalette, 0, 3);
38DECL_BIT(GBObjAttributes, Bank, 3);
39DECL_BIT(GBObjAttributes, Palette, 4);
40DECL_BIT(GBObjAttributes, XFlip, 5);
41DECL_BIT(GBObjAttributes, YFlip, 6);
42DECL_BIT(GBObjAttributes, Priority, 7);
43
44struct GBObj {
45 uint8_t y;
46 uint8_t x;
47 uint8_t tile;
48 GBObjAttributes attr;
49};
50
51union GBOAM {
52 struct GBObj obj[40];
53 uint8_t raw[160];
54};
55
56struct mTileCache;
57struct GBVideoRenderer {
58 void (*init)(struct GBVideoRenderer* renderer, enum GBModel model);
59 void (*deinit)(struct GBVideoRenderer* renderer);
60
61 uint8_t (*writeVideoRegister)(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value);
62 void (*writeVRAM)(struct GBVideoRenderer* renderer, uint16_t address);
63 void (*writePalette)(struct GBVideoRenderer* renderer, int index, uint16_t value);
64 void (*drawRange)(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* objOnLine, size_t nObj);
65 void (*finishScanline)(struct GBVideoRenderer* renderer, int y);
66 void (*finishFrame)(struct GBVideoRenderer* renderer);
67
68 void (*getPixels)(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels);
69 void (*putPixels)(struct GBVideoRenderer* renderer, size_t stride, const void* pixels);
70
71 uint8_t* vram;
72 union GBOAM* oam;
73 struct mTileCache* cache;
74
75 bool disableBG;
76 bool disableOBJ;
77 bool disableWIN;
78};
79
80DECL_BITFIELD(GBRegisterLCDC, uint8_t);
81DECL_BIT(GBRegisterLCDC, BgEnable, 0);
82DECL_BIT(GBRegisterLCDC, ObjEnable, 1);
83DECL_BIT(GBRegisterLCDC, ObjSize, 2);
84DECL_BIT(GBRegisterLCDC, TileMap, 3);
85DECL_BIT(GBRegisterLCDC, TileData, 4);
86DECL_BIT(GBRegisterLCDC, Window, 5);
87DECL_BIT(GBRegisterLCDC, WindowTileMap, 6);
88DECL_BIT(GBRegisterLCDC, Enable, 7);
89
90DECL_BITFIELD(GBRegisterSTAT, uint8_t);
91DECL_BITS(GBRegisterSTAT, Mode, 0, 2);
92DECL_BIT(GBRegisterSTAT, LYC, 2);
93DECL_BIT(GBRegisterSTAT, HblankIRQ, 3);
94DECL_BIT(GBRegisterSTAT, VblankIRQ, 4);
95DECL_BIT(GBRegisterSTAT, OAMIRQ, 5);
96DECL_BIT(GBRegisterSTAT, LYCIRQ, 6);
97
98struct GBVideo {
99 struct GB* p;
100 struct GBVideoRenderer* renderer;
101
102 int x;
103 int ly;
104 GBRegisterSTAT stat;
105
106 int mode;
107
108 struct mTimingEvent modeEvent;
109 struct mTimingEvent frameEvent;
110
111 uint32_t dotClock;
112
113 uint8_t* vram;
114 uint8_t* vramBank;
115 int vramCurrentBank;
116
117 union GBOAM oam;
118 struct GBObj objThisLine[10];
119 int objMax;
120
121 int bcpIndex;
122 bool bcpIncrement;
123 int ocpIndex;
124 bool ocpIncrement;
125
126 uint16_t dmgPalette[4];
127 uint16_t palette[64];
128
129 int32_t frameCounter;
130 int frameskip;
131 int frameskipCounter;
132};
133
134void GBVideoInit(struct GBVideo* video);
135void GBVideoReset(struct GBVideo* video);
136void GBVideoDeinit(struct GBVideo* video);
137void GBVideoAssociateRenderer(struct GBVideo* video, struct GBVideoRenderer* renderer);
138void GBVideoProcessDots(struct GBVideo* video);
139
140void GBVideoWriteLCDC(struct GBVideo* video, GBRegisterLCDC value);
141void GBVideoWriteSTAT(struct GBVideo* video, GBRegisterSTAT value);
142void GBVideoWriteLYC(struct GBVideo* video, uint8_t value);
143void GBVideoWritePalette(struct GBVideo* video, uint16_t address, uint8_t value);
144void GBVideoSwitchBank(struct GBVideo* video, uint8_t value);
145
146void GBVideoSetPalette(struct GBVideo* video, unsigned index, uint16_t color);
147
148struct GBSerializedState;
149void GBVideoSerialize(const struct GBVideo* video, struct GBSerializedState* state);
150void GBVideoDeserialize(struct GBVideo* video, const struct GBSerializedState* state);
151
152CXX_GUARD_END
153
154#endif