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 = 85,
21 GB_VIDEO_MODE_3_LENGTH_BASE = 167,
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 (*drawRange)(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj** objOnLine, size_t nObj);
58 void (*finishScanline)(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 x;
91 int ly;
92 GBRegisterSTAT stat;
93
94 int mode;
95
96 int32_t nextEvent;
97 int32_t eventDiff;
98
99 int32_t nextMode;
100 int32_t dotCounter;
101
102 uint8_t* vram;
103 uint8_t* vramBank;
104
105 union GBOAM oam;
106 struct GBObj* objThisLine[10];
107 int objMax;
108
109 int32_t frameCounter;
110 int frameskip;
111 int frameskipCounter;
112};
113
114void GBVideoInit(struct GBVideo* video);
115void GBVideoReset(struct GBVideo* video);
116void GBVideoDeinit(struct GBVideo* video);
117void GBVideoAssociateRenderer(struct GBVideo* video, struct GBVideoRenderer* renderer);
118int32_t GBVideoProcessEvents(struct GBVideo* video, int32_t cycles);
119void GBVideoProcessDots(struct GBVideo* video);
120
121void GBVideoWriteLCDC(struct GBVideo* video, GBRegisterLCDC value);
122void GBVideoWriteSTAT(struct GBVideo* video, GBRegisterSTAT value);
123
124#endif