all repos — mgba @ 1cb054ec67ea7b1e7e9162c1e74bd4ab254efb74

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 "gb/interface.h"
 13#include "gb/memory.h"
 14
 15enum {
 16	GB_VIDEO_HORIZONTAL_PIXELS = 160,
 17	GB_VIDEO_VERTICAL_PIXELS = 144,
 18	GB_VIDEO_VBLANK_PIXELS = 10,
 19	GB_VIDEO_VERTICAL_TOTAL_PIXELS = GB_VIDEO_VERTICAL_PIXELS + GB_VIDEO_VBLANK_PIXELS,
 20
 21	// TODO: Figure out exact lengths
 22	GB_VIDEO_MODE_2_LENGTH = 78,
 23	GB_VIDEO_MODE_3_LENGTH_BASE = 160,
 24	GB_VIDEO_MODE_0_LENGTH_BASE = 218,
 25
 26	GB_VIDEO_HORIZONTAL_LENGTH = GB_VIDEO_MODE_0_LENGTH_BASE + GB_VIDEO_MODE_2_LENGTH + GB_VIDEO_MODE_3_LENGTH_BASE,
 27
 28	GB_VIDEO_MODE_1_LENGTH = GB_VIDEO_HORIZONTAL_LENGTH * GB_VIDEO_VBLANK_PIXELS,
 29	GB_VIDEO_TOTAL_LENGTH = GB_VIDEO_HORIZONTAL_LENGTH * GB_VIDEO_VERTICAL_TOTAL_PIXELS,
 30
 31	GB_BASE_MAP = 0x1800,
 32	GB_SIZE_MAP = 0x0400
 33};
 34
 35DECL_BITFIELD(GBObjAttributes, uint8_t);
 36DECL_BITS(GBObjAttributes, CGBPalette, 0, 3);
 37DECL_BIT(GBObjAttributes, Bank, 3);
 38DECL_BIT(GBObjAttributes, Palette, 4);
 39DECL_BIT(GBObjAttributes, XFlip, 5);
 40DECL_BIT(GBObjAttributes, YFlip, 6);
 41DECL_BIT(GBObjAttributes, Priority, 7);
 42
 43struct GBObj {
 44	uint8_t y;
 45	uint8_t x;
 46	uint8_t tile;
 47	GBObjAttributes attr;
 48};
 49
 50union GBOAM {
 51	struct GBObj obj[40];
 52	uint8_t raw[160];
 53};
 54
 55enum GBModel;
 56struct GBVideoRenderer {
 57	void (*init)(struct GBVideoRenderer* renderer, enum GBModel model);
 58	void (*deinit)(struct GBVideoRenderer* renderer);
 59
 60	uint8_t (*writeVideoRegister)(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value);
 61	void (*writePalette)(struct GBVideoRenderer* renderer, int index, uint16_t value);
 62	void (*drawRange)(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* objOnLine, size_t nObj);
 63	void (*finishScanline)(struct GBVideoRenderer* renderer, int y);
 64	void (*finishFrame)(struct GBVideoRenderer* renderer);
 65
 66	void (*getPixels)(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels);
 67	void (*putPixels)(struct GBVideoRenderer* renderer, size_t stride, const void* pixels);
 68
 69	uint8_t* vram;
 70	union GBOAM* oam;
 71};
 72
 73DECL_BITFIELD(GBRegisterLCDC, uint8_t);
 74DECL_BIT(GBRegisterLCDC, BgEnable, 0);
 75DECL_BIT(GBRegisterLCDC, ObjEnable, 1);
 76DECL_BIT(GBRegisterLCDC, ObjSize, 2);
 77DECL_BIT(GBRegisterLCDC, TileMap, 3);
 78DECL_BIT(GBRegisterLCDC, TileData, 4);
 79DECL_BIT(GBRegisterLCDC, Window, 5);
 80DECL_BIT(GBRegisterLCDC, WindowTileMap, 6);
 81DECL_BIT(GBRegisterLCDC, Enable, 7);
 82
 83DECL_BITFIELD(GBRegisterSTAT, uint8_t);
 84DECL_BITS(GBRegisterSTAT, Mode, 0, 2);
 85DECL_BIT(GBRegisterSTAT, LYC, 2);
 86DECL_BIT(GBRegisterSTAT, HblankIRQ, 3);
 87DECL_BIT(GBRegisterSTAT, VblankIRQ, 4);
 88DECL_BIT(GBRegisterSTAT, OAMIRQ, 5);
 89DECL_BIT(GBRegisterSTAT, LYCIRQ, 6);
 90
 91struct GBVideo {
 92	struct GB* p;
 93	struct GBVideoRenderer* renderer;
 94
 95	int x;
 96	int ly;
 97	GBRegisterSTAT stat;
 98
 99	int mode;
100
101	int32_t nextEvent;
102	int32_t eventDiff;
103
104	int32_t nextMode;
105	int32_t dotCounter;
106
107	int32_t nextFrame;
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 palette[64];
123
124	int32_t frameCounter;
125	int frameskip;
126	int frameskipCounter;
127};
128
129void GBVideoInit(struct GBVideo* video);
130void GBVideoReset(struct GBVideo* video);
131void GBVideoDeinit(struct GBVideo* video);
132void GBVideoAssociateRenderer(struct GBVideo* video, struct GBVideoRenderer* renderer);
133int32_t GBVideoProcessEvents(struct GBVideo* video, int32_t cycles);
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
142struct GBSerializedState;
143void GBVideoSerialize(const struct GBVideo* video, struct GBSerializedState* state);
144void GBVideoDeserialize(struct GBVideo* video, const struct GBSerializedState* state);
145
146#endif