all repos — mgba @ 9f425c53c8467f03fc998a0905a198e896559388

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 (*init)(struct GBAVideoRenderer* renderer);
 93	void (*deinit)(struct GBAVideoRenderer* renderer);
 94
 95	uint16_t (*writeVideoRegister)(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
 96	void (*drawScanline)(struct GBAVideoRenderer* renderer, int y);
 97	void (*finishFrame)(struct GBAVideoRenderer* renderer);
 98
 99	uint16_t* palette;
100	uint16_t* vram;
101	union GBAOAM* oam;
102};
103
104struct GBAVideo {
105	struct GBA* p;
106	struct GBAVideoRenderer* renderer;
107
108	// DISPSTAT
109	int inHblank;
110	int inVblank;
111	int vcounter;
112	int vblankIRQ;
113	int hblankIRQ;
114	int vcounterIRQ;
115	int vcountSetting;
116
117	// VCOUNT
118	int vcount;
119
120	int32_t lastHblank;
121	int32_t nextHblank;
122	int32_t nextEvent;
123	int32_t eventDiff;
124
125	int32_t nextHblankIRQ;
126	int32_t nextVblankIRQ;
127	int32_t nextVcounterIRQ;
128
129	uint16_t palette[SIZE_PALETTE_RAM >> 1];
130	uint16_t vram[SIZE_VRAM >> 1];
131	union GBAOAM oam;
132};
133
134void GBAVideoInit(struct GBAVideo* video);
135void GBAVideoAssociateRenderer(struct GBAVideo* video, struct GBAVideoRenderer* renderer);
136int32_t GBAVideoProcessEvents(struct GBAVideo* video, int32_t cycles);
137
138void GBAVideoWriteDISPSTAT(struct GBAVideo* video, uint16_t value);
139uint16_t GBAVideoReadDISPSTAT(struct GBAVideo* video);
140
141#endif