all repos — mgba @ e27963bd29653427b99a97742c48a79e7d3a887a

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