all repos — mgba @ 871c21fb6cf9041b4ac7c7f7074e0b2eb77484fe

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_MODE_1_LENGTH = 65664,
 30	GB_VIDEO_TOTAL_LENGTH = 70224,
 31
 32	GB_BASE_MAP = 0x1800,
 33	GB_SIZE_MAP = 0x0400,
 34
 35	SGB_SIZE_CHAR_RAM = 0x2000,
 36	SGB_SIZE_MAP_RAM = 0x1000,
 37	SGB_SIZE_PAL_RAM = 0x1000,
 38	SGB_SIZE_ATF_RAM = 0x1000
 39};
 40
 41DECL_BITFIELD(GBObjAttributes, uint8_t);
 42DECL_BITS(GBObjAttributes, CGBPalette, 0, 3);
 43DECL_BIT(GBObjAttributes, Bank, 3);
 44DECL_BIT(GBObjAttributes, Palette, 4);
 45DECL_BIT(GBObjAttributes, XFlip, 5);
 46DECL_BIT(GBObjAttributes, YFlip, 6);
 47DECL_BIT(GBObjAttributes, Priority, 7);
 48
 49DECL_BITFIELD(SGBBgAttributes, uint16_t);
 50DECL_BITS(SGBBgAttributes, Tile, 0, 10);
 51DECL_BITS(SGBBgAttributes, Palette, 10, 3);
 52DECL_BIT(SGBBgAttributes, Priority, 13);
 53DECL_BIT(SGBBgAttributes, XFlip, 14);
 54DECL_BIT(SGBBgAttributes, YFlip, 15);
 55
 56struct GBObj {
 57	uint8_t y;
 58	uint8_t x;
 59	uint8_t tile;
 60	GBObjAttributes attr;
 61};
 62
 63union GBOAM {
 64	struct GBObj obj[40];
 65	uint8_t raw[160];
 66};
 67
 68struct mTileCache;
 69struct GBVideoRenderer {
 70	void (*init)(struct GBVideoRenderer* renderer, enum GBModel model);
 71	void (*deinit)(struct GBVideoRenderer* renderer);
 72
 73	uint8_t (*writeVideoRegister)(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value);
 74	void (*writeSGBPacket)(struct GBVideoRenderer* renderer, uint8_t* data);
 75	void (*writeVRAM)(struct GBVideoRenderer* renderer, uint16_t address);
 76	void (*writePalette)(struct GBVideoRenderer* renderer, int index, uint16_t value);
 77	void (*writeOAM)(struct GBVideoRenderer* renderer, uint16_t oam);
 78	void (*drawRange)(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* objOnLine, size_t nObj);
 79	void (*finishScanline)(struct GBVideoRenderer* renderer, int y);
 80	void (*finishFrame)(struct GBVideoRenderer* renderer);
 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 mTileCache* 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	uint32_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
148	uint16_t dmgPalette[4];
149	uint16_t palette[64];
150
151	int32_t frameCounter;
152	int frameskip;
153	int frameskipCounter;
154};
155
156void GBVideoInit(struct GBVideo* video);
157void GBVideoReset(struct GBVideo* video);
158void GBVideoDeinit(struct GBVideo* video);
159void GBVideoAssociateRenderer(struct GBVideo* video, struct GBVideoRenderer* renderer);
160void GBVideoProcessDots(struct GBVideo* video);
161
162void GBVideoWriteLCDC(struct GBVideo* video, GBRegisterLCDC value);
163void GBVideoWriteSTAT(struct GBVideo* video, GBRegisterSTAT value);
164void GBVideoWriteLYC(struct GBVideo* video, uint8_t value);
165void GBVideoWritePalette(struct GBVideo* video, uint16_t address, uint8_t value);
166void GBVideoSwitchBank(struct GBVideo* video, uint8_t value);
167
168void GBVideoSetPalette(struct GBVideo* video, unsigned index, uint32_t color);
169
170void GBVideoWriteSGBPacket(struct GBVideo* video, uint8_t* data);
171
172struct GBSerializedState;
173void GBVideoSerialize(const struct GBVideo* video, struct GBSerializedState* state);
174void GBVideoDeserialize(struct GBVideo* video, const struct GBSerializedState* state);
175
176CXX_GUARD_END
177
178#endif