all repos — mgba @ 0fdc3436895a2b21afdf17069e24681f3093d211

mGBA Game Boy Advance Emulator

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 = 80,
 24	GB_VIDEO_MODE_3_LENGTH_BASE = 172,
 25	GB_VIDEO_MODE_0_LENGTH_BASE = 204,
 26
 27	GB_VIDEO_HORIZONTAL_LENGTH = 456,
 28
 29	GB_VIDEO_TOTAL_LENGTH = 70224,
 30
 31	GB_BASE_MAP = 0x1800,
 32	GB_SIZE_MAP = 0x0400,
 33
 34	SGB_SIZE_CHAR_RAM = 0x2000,
 35	SGB_SIZE_MAP_RAM = 0x1000,
 36	SGB_SIZE_PAL_RAM = 0x1000,
 37	SGB_SIZE_ATF_RAM = 0x1000
 38};
 39
 40DECL_BITFIELD(GBObjAttributes, uint8_t);
 41DECL_BITS(GBObjAttributes, CGBPalette, 0, 3);
 42DECL_BIT(GBObjAttributes, Bank, 3);
 43DECL_BIT(GBObjAttributes, Palette, 4);
 44DECL_BIT(GBObjAttributes, XFlip, 5);
 45DECL_BIT(GBObjAttributes, YFlip, 6);
 46DECL_BIT(GBObjAttributes, Priority, 7);
 47
 48DECL_BITFIELD(SGBBgAttributes, uint16_t);
 49DECL_BITS(SGBBgAttributes, Tile, 0, 10);
 50DECL_BITS(SGBBgAttributes, Palette, 10, 3);
 51DECL_BIT(SGBBgAttributes, Priority, 13);
 52DECL_BIT(SGBBgAttributes, XFlip, 14);
 53DECL_BIT(SGBBgAttributes, YFlip, 15);
 54
 55struct GBObj {
 56	uint8_t y;
 57	uint8_t x;
 58	uint8_t tile;
 59	GBObjAttributes attr;
 60};
 61
 62union GBOAM {
 63	struct GBObj obj[40];
 64	uint8_t raw[160];
 65};
 66
 67struct mCacheSet;
 68struct GBVideoRenderer {
 69	void (*init)(struct GBVideoRenderer* renderer, enum GBModel model, bool borders);
 70	void (*deinit)(struct GBVideoRenderer* renderer);
 71
 72	uint8_t (*writeVideoRegister)(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value);
 73	void (*writeSGBPacket)(struct GBVideoRenderer* renderer, uint8_t* data);
 74	void (*writeVRAM)(struct GBVideoRenderer* renderer, uint16_t address);
 75	void (*writePalette)(struct GBVideoRenderer* renderer, int index, uint16_t value);
 76	void (*writeOAM)(struct GBVideoRenderer* renderer, uint16_t oam);
 77	void (*drawRange)(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* objOnLine, size_t nObj);
 78	void (*finishScanline)(struct GBVideoRenderer* renderer, int y);
 79	void (*finishFrame)(struct GBVideoRenderer* renderer);
 80	void (*enableSGBBorder)(struct GBVideoRenderer* renderer, bool enable);
 81
 82	void (*getPixels)(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels);
 83	void (*putPixels)(struct GBVideoRenderer* renderer, size_t stride, const void* pixels);
 84
 85	uint8_t* vram;
 86	union GBOAM* oam;
 87	struct mCacheSet* cache;
 88
 89	uint8_t* sgbCharRam;
 90	uint8_t* sgbMapRam;
 91	uint8_t* sgbPalRam;
 92	int sgbRenderMode;
 93	uint8_t* sgbAttributes;
 94	uint8_t* sgbAttributeFiles;
 95
 96	bool disableBG;
 97	bool disableOBJ;
 98	bool disableWIN;
 99};
100
101DECL_BITFIELD(GBRegisterLCDC, uint8_t);
102DECL_BIT(GBRegisterLCDC, BgEnable, 0);
103DECL_BIT(GBRegisterLCDC, ObjEnable, 1);
104DECL_BIT(GBRegisterLCDC, ObjSize, 2);
105DECL_BIT(GBRegisterLCDC, TileMap, 3);
106DECL_BIT(GBRegisterLCDC, TileData, 4);
107DECL_BIT(GBRegisterLCDC, Window, 5);
108DECL_BIT(GBRegisterLCDC, WindowTileMap, 6);
109DECL_BIT(GBRegisterLCDC, Enable, 7);
110
111DECL_BITFIELD(GBRegisterSTAT, uint8_t);
112DECL_BITS(GBRegisterSTAT, Mode, 0, 2);
113DECL_BIT(GBRegisterSTAT, LYC, 2);
114DECL_BIT(GBRegisterSTAT, HblankIRQ, 3);
115DECL_BIT(GBRegisterSTAT, VblankIRQ, 4);
116DECL_BIT(GBRegisterSTAT, OAMIRQ, 5);
117DECL_BIT(GBRegisterSTAT, LYCIRQ, 6);
118
119struct GBVideo {
120	struct GB* p;
121	struct GBVideoRenderer* renderer;
122
123	int x;
124	int ly;
125	GBRegisterSTAT stat;
126
127	int mode;
128
129	struct mTimingEvent modeEvent;
130	struct mTimingEvent frameEvent;
131
132	int32_t dotClock;
133
134	uint8_t* vram;
135	uint8_t* vramBank;
136	int vramCurrentBank;
137
138	union GBOAM oam;
139	struct GBObj objThisLine[10];
140	int objMax;
141
142	int bcpIndex;
143	bool bcpIncrement;
144	int ocpIndex;
145	bool ocpIncrement;
146	uint8_t sgbCommandHeader;
147	int sgbBufferIndex;
148	uint8_t sgbPacketBuffer[128];
149
150	uint16_t dmgPalette[12];
151	uint16_t palette[64];
152
153	bool sgbBorders;
154
155	int32_t frameCounter;
156	int frameskip;
157	int frameskipCounter;
158};
159
160void GBVideoInit(struct GBVideo* video);
161void GBVideoReset(struct GBVideo* video);
162void GBVideoDeinit(struct GBVideo* video);
163void GBVideoAssociateRenderer(struct GBVideo* video, struct GBVideoRenderer* renderer);
164void GBVideoSkipBIOS(struct GBVideo* video);
165void GBVideoProcessDots(struct GBVideo* video, uint32_t cyclesLate);
166
167void GBVideoWriteLCDC(struct GBVideo* video, GBRegisterLCDC value);
168void GBVideoWriteSTAT(struct GBVideo* video, GBRegisterSTAT value);
169void GBVideoWriteLYC(struct GBVideo* video, uint8_t value);
170void GBVideoWritePalette(struct GBVideo* video, uint16_t address, uint8_t value);
171void GBVideoSwitchBank(struct GBVideo* video, uint8_t value);
172
173void GBVideoDisableCGB(struct GBVideo* video);
174void GBVideoSetPalette(struct GBVideo* video, unsigned index, uint32_t color);
175
176void GBVideoWriteSGBPacket(struct GBVideo* video, uint8_t* data);
177
178struct GBSerializedState;
179void GBVideoSerialize(const struct GBVideo* video, struct GBSerializedState* state);
180void GBVideoDeserialize(struct GBVideo* video, const struct GBSerializedState* state);
181
182CXX_GUARD_END
183
184#endif