all repos — mgba @ 55679df8fc49fca3d5f8584d16b90e9bb734a922

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