all repos — mgba @ bf9be29ad55ea558b8f7bae307e2930be265feb3

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
 11#include "core/interface.h"
 12#include "core/timing.h"
 13#include "gb/interface.h"
 14#include "gb/memory.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 = GB_VIDEO_VERTICAL_PIXELS + GB_VIDEO_VBLANK_PIXELS,
 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 = GB_VIDEO_MODE_0_LENGTH_BASE + GB_VIDEO_MODE_2_LENGTH + GB_VIDEO_MODE_3_LENGTH_BASE,
 28
 29	GB_VIDEO_MODE_1_LENGTH = GB_VIDEO_HORIZONTAL_LENGTH * GB_VIDEO_VBLANK_PIXELS,
 30	GB_VIDEO_TOTAL_LENGTH = GB_VIDEO_HORIZONTAL_LENGTH * GB_VIDEO_VERTICAL_TOTAL_PIXELS,
 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
 56enum GBModel;
 57struct mTileCache;
 58struct GBVideoRenderer {
 59	void (*init)(struct GBVideoRenderer* renderer, enum GBModel model);
 60	void (*deinit)(struct GBVideoRenderer* renderer);
 61
 62	uint8_t (*writeVideoRegister)(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value);
 63	void (*writeVRAM)(struct GBVideoRenderer* renderer, uint16_t address);
 64	void (*writePalette)(struct GBVideoRenderer* renderer, int index, uint16_t value);
 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
 77DECL_BITFIELD(GBRegisterLCDC, uint8_t);
 78DECL_BIT(GBRegisterLCDC, BgEnable, 0);
 79DECL_BIT(GBRegisterLCDC, ObjEnable, 1);
 80DECL_BIT(GBRegisterLCDC, ObjSize, 2);
 81DECL_BIT(GBRegisterLCDC, TileMap, 3);
 82DECL_BIT(GBRegisterLCDC, TileData, 4);
 83DECL_BIT(GBRegisterLCDC, Window, 5);
 84DECL_BIT(GBRegisterLCDC, WindowTileMap, 6);
 85DECL_BIT(GBRegisterLCDC, Enable, 7);
 86
 87DECL_BITFIELD(GBRegisterSTAT, uint8_t);
 88DECL_BITS(GBRegisterSTAT, Mode, 0, 2);
 89DECL_BIT(GBRegisterSTAT, LYC, 2);
 90DECL_BIT(GBRegisterSTAT, HblankIRQ, 3);
 91DECL_BIT(GBRegisterSTAT, VblankIRQ, 4);
 92DECL_BIT(GBRegisterSTAT, OAMIRQ, 5);
 93DECL_BIT(GBRegisterSTAT, LYCIRQ, 6);
 94
 95struct GBVideo {
 96	struct GB* p;
 97	struct GBVideoRenderer* renderer;
 98
 99	int x;
100	int ly;
101	GBRegisterSTAT stat;
102
103	int mode;
104
105	struct mTimingEvent modeEvent;
106	struct mTimingEvent frameEvent;
107
108	uint32_t dotClock;
109
110	uint8_t* vram;
111	uint8_t* vramBank;
112	int vramCurrentBank;
113
114	union GBOAM oam;
115	struct GBObj objThisLine[10];
116	int objMax;
117
118	int bcpIndex;
119	bool bcpIncrement;
120	int ocpIndex;
121	bool ocpIncrement;
122
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);
134int32_t GBVideoProcessEvents(struct GBVideo* video, int32_t cycles);
135void GBVideoProcessDots(struct GBVideo* video);
136
137void GBVideoWriteLCDC(struct GBVideo* video, GBRegisterLCDC value);
138void GBVideoWriteSTAT(struct GBVideo* video, GBRegisterSTAT value);
139void GBVideoWriteLYC(struct GBVideo* video, uint8_t value);
140void GBVideoWritePalette(struct GBVideo* video, uint16_t address, uint8_t value);
141void GBVideoSwitchBank(struct GBVideo* video, uint8_t value);
142
143struct GBSerializedState;
144void GBVideoSerialize(const struct GBVideo* video, struct GBSerializedState* state);
145void GBVideoDeserialize(struct GBVideo* video, const struct GBSerializedState* state);
146
147#endif