all repos — mgba @ cb48145ea3911a438d3bc214e0622341ccbb9f15

mGBA Game Boy Advance Emulator

src/gba/gba-video.h (view raw)

  1#ifndef GBA_VIDEO_H
  2#define GBA_VIDEO_H
  3
  4#include "gba-memory.h"
  5
  6#include <stdint.h>
  7
  8enum {
  9	VIDEO_CYCLES_PER_PIXEL = 4,
 10
 11	VIDEO_HORIZONTAL_PIXELS = 240,
 12	VIDEO_HBLANK_PIXELS = 68,
 13	VIDEO_HDRAW_LENGTH = 1006,
 14	VIDEO_HBLANK_LENGTH = 226,
 15	VIDEO_HORIZONTAL_LENGTH = 1232,
 16
 17	VIDEO_VERTICAL_PIXELS = 160,
 18	VIDEO_VBLANK_PIXELS = 68,
 19	VIDEO_VERTICAL_TOTAL_PIXELS = 228,
 20
 21	VIDEO_TOTAL_LENGTH = 280896,
 22
 23	REG_DISPSTAT_MASK = 0xFF38
 24};
 25
 26enum ObjMode {
 27	OBJ_MODE_NORMAL = 0,
 28	OBJ_MODE_SEMITRANSPARENT = 1,
 29	OBJ_MODE_OBJWIN = 2
 30};
 31
 32enum ObjShape {
 33	OBJ_SHAPE_SQUARE = 0,
 34	OBJ_SHAPE_HORIZONTAL = 1,
 35	OBJ_SHAPE_VERTICAL = 2
 36};
 37
 38union GBAOAM {
 39	struct {
 40		int y : 8;
 41		unsigned transformed : 1;
 42		union {
 43			unsigned doublesize : 1;
 44			unsigned disable : 1;
 45		};
 46		enum ObjMode mode : 2;
 47		unsigned mosaic : 1;
 48		unsigned multipalette : 1;
 49		enum ObjShape shape : 2;
 50
 51		int x : 9;
 52		union {
 53			unsigned matIndex : 5;
 54			struct {
 55				int : 3;
 56				unsigned hflip : 1;
 57				unsigned vflip : 1;
 58			};
 59		};
 60		unsigned size : 2;
 61
 62		unsigned tile : 10;
 63		unsigned priority : 2;
 64		unsigned palette : 4;
 65
 66		int : 16;
 67	} obj[128];
 68
 69	struct {
 70		int : 16;
 71		int : 16;
 72		int : 16;
 73		int a : 16;
 74		int : 16;
 75		int : 16;
 76		int : 16;
 77		int b : 16;
 78		int : 16;
 79		int : 16;
 80		int : 16;
 81		int c : 16;
 82		int : 16;
 83		int : 16;
 84		int : 16;
 85		int d : 16;
 86	} mat[32];
 87
 88	uint16_t raw[512];
 89};
 90
 91struct GBAVideoRenderer {
 92	void (*deinit)(struct GBAVideoRenderer* renderer);
 93
 94	uint16_t (*writeVideoRegister)(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
 95	void (*drawScanline)(struct GBAVideoRenderer* renderer, int y);
 96	void (*finishFrame)(struct GBAVideoRenderer* renderer);
 97
 98	uint16_t palette[SIZE_PALETTE_RAM >> 1];
 99	uint16_t vram[SIZE_VRAM >> 1];
100	union GBAOAM oam;
101};
102
103struct GBAVideo {
104	struct GBA* p;
105	struct GBAVideoRenderer* renderer;
106
107	// DISPSTAT
108	int inHblank;
109	int inVblank;
110	int vcounter;
111	int vblankIRQ;
112	int hblankIRQ;
113	int vcounterIRQ;
114	int vcountSetting;
115
116	// VCOUNT
117	int vcount;
118
119	int32_t lastHblank;
120	int32_t nextHblank;
121	int32_t nextEvent;
122	int32_t eventDiff;
123
124	int32_t nextHblankIRQ;
125	int32_t nextVblankIRQ;
126	int32_t nextVcounterIRQ;
127};
128
129void GBAVideoInit(struct GBAVideo* video);
130int32_t GBAVideoProcessEvents(struct GBAVideo* video, int32_t cycles);
131
132void GBAVideoWriteDISPSTAT(struct GBAVideo* video, uint16_t value);
133uint16_t GBAVideoReadDISPSTAT(struct GBAVideo* video);
134
135#endif