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 // TODO: Figure out exact lengths
20 GB_VIDEO_MODE_2_LENGTH = 84,
21 GB_VIDEO_MODE_3_LENGTH_BASE = 168,
22 GB_VIDEO_MODE_0_LENGTH_BASE = 204,
23
24 GB_VIDEO_HORIZONTAL_LENGTH = GB_VIDEO_MODE_0_LENGTH_BASE + GB_VIDEO_MODE_2_LENGTH + GB_VIDEO_MODE_3_LENGTH_BASE,
25
26 GB_VIDEO_MODE_1_LENGTH = GB_VIDEO_HORIZONTAL_LENGTH * GB_VIDEO_VBLANK_PIXELS,
27 GB_VIDEO_TOTAL_LENGTH = GB_VIDEO_HORIZONTAL_LENGTH * GB_VIDEO_VERTICAL_TOTAL_PIXELS,
28
29 GB_BASE_MAP = 0x1800,
30 GB_SIZE_MAP = 0x0400
31};
32
33DECL_BITFIELD(GBObjAttributes, uint8_t);
34DECL_BIT(GBObjAttributes, Palette, 4);
35DECL_BIT(GBObjAttributes, XFlip, 5);
36DECL_BIT(GBObjAttributes, YFlip, 6);
37DECL_BIT(GBObjAttributes, Priority, 7);
38
39struct GBObj {
40 uint8_t y;
41 uint8_t x;
42 uint8_t tile;
43 GBObjAttributes attr;
44};
45
46union GBOAM {
47 struct GBObj obj[40];
48 uint8_t raw[160];
49};
50
51struct GBVideoRenderer {
52 void (*init)(struct GBVideoRenderer* renderer);
53 void (*reset)(struct GBVideoRenderer* renderer);
54 void (*deinit)(struct GBVideoRenderer* renderer);
55
56 uint8_t (*writeVideoRegister)(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value);
57 void (*drawDot)(struct GBVideoRenderer* renderer, int x, int y, struct GBObj** objOnLine, size_t nObj);
58 void (*finishFrame)(struct GBVideoRenderer* renderer);
59
60 void (*getPixels)(struct GBVideoRenderer* renderer, unsigned* stride, const void** pixels);
61 void (*putPixels)(struct GBVideoRenderer* renderer, unsigned stride, void* pixels);
62
63 uint8_t* vram;
64 union GBOAM* oam;
65};
66
67DECL_BITFIELD(GBRegisterLCDC, uint8_t);
68DECL_BIT(GBRegisterLCDC, BgEnable, 0);
69DECL_BIT(GBRegisterLCDC, ObjEnable, 1);
70DECL_BIT(GBRegisterLCDC, ObjSize, 2);
71DECL_BIT(GBRegisterLCDC, TileMap, 3);
72DECL_BIT(GBRegisterLCDC, TileData, 4);
73DECL_BIT(GBRegisterLCDC, Window, 5);
74DECL_BIT(GBRegisterLCDC, WindowTileMap, 6);
75DECL_BIT(GBRegisterLCDC, Enable, 7);
76
77DECL_BITFIELD(GBRegisterSTAT, uint8_t);
78DECL_BITS(GBRegisterSTAT, Mode, 0, 2);
79DECL_BIT(GBRegisterSTAT, LYC, 2);
80DECL_BIT(GBRegisterSTAT, HblankIRQ, 3);
81DECL_BIT(GBRegisterSTAT, VblankIRQ, 4);
82DECL_BIT(GBRegisterSTAT, OAMIRQ, 5);
83DECL_BIT(GBRegisterSTAT, LYCIRQ, 6);
84
85struct GBVideo {
86 struct GB* p;
87 struct GBVideoRenderer* renderer;
88
89 int x;
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 int32_t nextDot;
100
101 uint8_t* vram;
102 uint8_t* vramBank;
103
104 union GBOAM oam;
105 struct GBObj* objThisLine[10];
106 int objMax;
107
108 int32_t frameCounter;
109 int frameskip;
110 int frameskipCounter;
111};
112
113void GBVideoInit(struct GBVideo* video);
114void GBVideoReset(struct GBVideo* video);
115void GBVideoDeinit(struct GBVideo* video);
116void GBVideoAssociateRenderer(struct GBVideo* video, struct GBVideoRenderer* renderer);
117int32_t GBVideoProcessEvents(struct GBVideo* video, int32_t cycles);
118
119void GBVideoWriteLCDC(struct GBVideo* video, GBRegisterLCDC value);
120void GBVideoWriteSTAT(struct GBVideo* video, GBRegisterSTAT value);
121
122#endif