all repos — mgba @ dbdd61cdcdef83d47402c187acd1ff6e16ff9ccb

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 "gb/memory.h"
 12
 13enum {
 14	GB_VIDEO_HORIZONTAL_PIXELS = 160,
 15	GB_VIDEO_VERTICAL_PIXELS = 144,
 16	GB_VIDEO_VBLANK_PIXELS = 10,
 17	GB_VIDEO_VERTICAL_TOTAL_PIXELS = GB_VIDEO_VERTICAL_PIXELS + GB_VIDEO_VBLANK_PIXELS,
 18
 19	GB_VIDEO_MODE_0_LENGTH = 203, // Estimates, figure out with more precision
 20	GB_VIDEO_MODE_2_LENGTH = 81,
 21	GB_VIDEO_MODE_3_LENGTH = 172,
 22
 23	GB_VIDEO_HORIZONTAL_LENGTH = GB_VIDEO_MODE_0_LENGTH + GB_VIDEO_MODE_2_LENGTH + GB_VIDEO_MODE_3_LENGTH,
 24
 25	GB_VIDEO_MODE_1_LENGTH = GB_VIDEO_HORIZONTAL_LENGTH * GB_VIDEO_VBLANK_PIXELS,
 26	GB_VIDEO_TOTAL_LENGTH = GB_VIDEO_HORIZONTAL_LENGTH * GB_VIDEO_VERTICAL_TOTAL_PIXELS,
 27
 28	GB_BASE_MAP = 0x1800,
 29	GB_SIZE_MAP = 0x0400
 30};
 31
 32DECL_BITFIELD(GBObjAttributes, uint8_t);
 33DECL_BIT(GBObjAttributes, Palette, 4);
 34DECL_BIT(GBObjAttributes, XFlip, 5);
 35DECL_BIT(GBObjAttributes, YFlip, 6);
 36DECL_BIT(GBObjAttributes, Priority, 7);
 37
 38struct GBObj {
 39	uint8_t y;
 40	uint8_t x;
 41	uint8_t tile;
 42	GBObjAttributes attr;
 43};
 44
 45union GBOAM {
 46	struct GBObj obj[40];
 47	uint8_t raw[160];
 48};
 49
 50struct GBVideoRenderer {
 51	void (*init)(struct GBVideoRenderer* renderer);
 52	void (*reset)(struct GBVideoRenderer* renderer);
 53	void (*deinit)(struct GBVideoRenderer* renderer);
 54
 55	uint8_t (*writeVideoRegister)(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value);
 56	void (*writeVRAM)(struct GBVideoRenderer* renderer, uint16_t address);
 57	void (*writeOAM)(struct GBVideoRenderer* renderer, uint8_t oam);
 58	void (*drawScanline)(struct GBVideoRenderer* renderer, int y);
 59	void (*finishFrame)(struct GBVideoRenderer* renderer);
 60
 61	void (*getPixels)(struct GBVideoRenderer* renderer, unsigned* stride, const void** pixels);
 62	void (*putPixels)(struct GBVideoRenderer* renderer, unsigned stride, void* pixels);
 63
 64	uint8_t* vram;
 65	union GBOAM* oam;
 66};
 67
 68DECL_BITFIELD(GBRegisterLCDC, uint8_t);
 69DECL_BIT(GBRegisterLCDC, BgEnable, 0);
 70DECL_BIT(GBRegisterLCDC, ObjEnable, 1);
 71DECL_BIT(GBRegisterLCDC, ObjSize, 2);
 72DECL_BIT(GBRegisterLCDC, TileMap, 3);
 73DECL_BIT(GBRegisterLCDC, TileData, 4);
 74DECL_BIT(GBRegisterLCDC, Window, 5);
 75DECL_BIT(GBRegisterLCDC, WindowTileMap, 6);
 76DECL_BIT(GBRegisterLCDC, Enable, 7);
 77
 78DECL_BITFIELD(GBRegisterSTAT, uint8_t);
 79DECL_BITS(GBRegisterSTAT, Mode, 0, 2);
 80DECL_BIT(GBRegisterSTAT, LYC, 2);
 81DECL_BIT(GBRegisterSTAT, HblankIRQ, 3);
 82DECL_BIT(GBRegisterSTAT, VblankIRQ, 4);
 83DECL_BIT(GBRegisterSTAT, OAMIRQ, 5);
 84DECL_BIT(GBRegisterSTAT, LYCIRQ, 6);
 85
 86struct GBVideo {
 87	struct GB* p;
 88	struct GBVideoRenderer* renderer;
 89
 90	int ly;
 91	GBRegisterSTAT stat;
 92
 93	int mode;
 94
 95	int32_t nextEvent;
 96	int32_t eventDiff;
 97
 98	int32_t nextMode;
 99
100	uint8_t* vram;
101	uint8_t* vramBank;
102
103	union GBOAM oam;
104
105	int32_t frameCounter;
106	int frameskip;
107	int frameskipCounter;
108};
109
110void GBVideoInit(struct GBVideo* video);
111void GBVideoReset(struct GBVideo* video);
112void GBVideoDeinit(struct GBVideo* video);
113void GBVideoAssociateRenderer(struct GBVideo* video, struct GBVideoRenderer* renderer);
114int32_t GBVideoProcessEvents(struct GBVideo* video, int32_t cycles);
115
116void GBVideoWriteLCDC(struct GBVideo* video, GBRegisterLCDC value);
117void GBVideoWriteSTAT(struct GBVideo* video, GBRegisterSTAT value);
118
119#endif