all repos — mgba @ 2d02719fa57d4e4a33168955a8e0c34dfabc301a

mGBA Game Boy Advance Emulator

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