all repos — mgba @ ba65740b159a2ce3bceecf1088f859f326d69b34

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 = 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
 76DECL_BITFIELD(GBRegisterLCDC, uint8_t);
 77DECL_BIT(GBRegisterLCDC, BgEnable, 0);
 78DECL_BIT(GBRegisterLCDC, ObjEnable, 1);
 79DECL_BIT(GBRegisterLCDC, ObjSize, 2);
 80DECL_BIT(GBRegisterLCDC, TileMap, 3);
 81DECL_BIT(GBRegisterLCDC, TileData, 4);
 82DECL_BIT(GBRegisterLCDC, Window, 5);
 83DECL_BIT(GBRegisterLCDC, WindowTileMap, 6);
 84DECL_BIT(GBRegisterLCDC, Enable, 7);
 85
 86DECL_BITFIELD(GBRegisterSTAT, uint8_t);
 87DECL_BITS(GBRegisterSTAT, Mode, 0, 2);
 88DECL_BIT(GBRegisterSTAT, LYC, 2);
 89DECL_BIT(GBRegisterSTAT, HblankIRQ, 3);
 90DECL_BIT(GBRegisterSTAT, VblankIRQ, 4);
 91DECL_BIT(GBRegisterSTAT, OAMIRQ, 5);
 92DECL_BIT(GBRegisterSTAT, LYCIRQ, 6);
 93
 94struct GBVideo {
 95	struct GB* p;
 96	struct GBVideoRenderer* renderer;
 97
 98	int x;
 99	int ly;
100	GBRegisterSTAT stat;
101
102	int mode;
103
104	struct mTimingEvent modeEvent;
105	struct mTimingEvent frameEvent;
106
107	uint32_t dotClock;
108
109	uint8_t* vram;
110	uint8_t* vramBank;
111	int vramCurrentBank;
112
113	union GBOAM oam;
114	struct GBObj objThisLine[10];
115	int objMax;
116
117	int bcpIndex;
118	bool bcpIncrement;
119	int ocpIndex;
120	bool ocpIncrement;
121
122	uint16_t dmgPalette[4];
123	uint16_t palette[64];
124
125	int32_t frameCounter;
126	int frameskip;
127	int frameskipCounter;
128};
129
130void GBVideoInit(struct GBVideo* video);
131void GBVideoReset(struct GBVideo* video);
132void GBVideoDeinit(struct GBVideo* video);
133void GBVideoAssociateRenderer(struct GBVideo* video, struct GBVideoRenderer* renderer);
134void GBVideoProcessDots(struct GBVideo* video);
135
136void GBVideoWriteLCDC(struct GBVideo* video, GBRegisterLCDC value);
137void GBVideoWriteSTAT(struct GBVideo* video, GBRegisterSTAT value);
138void GBVideoWriteLYC(struct GBVideo* video, uint8_t value);
139void GBVideoWritePalette(struct GBVideo* video, uint16_t address, uint8_t value);
140void GBVideoSwitchBank(struct GBVideo* video, uint8_t value);
141
142void GBVideoSetPalette(struct GBVideo* video, unsigned index, uint16_t color);
143
144struct GBSerializedState;
145void GBVideoSerialize(const struct GBVideo* video, struct GBSerializedState* state);
146void GBVideoDeserialize(struct GBVideo* video, const struct GBSerializedState* state);
147
148CXX_GUARD_END
149
150#endif