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 "core/interface.h"
12#include "gb/interface.h"
13#include "gb/memory.h"
14
15enum {
16 GB_VIDEO_HORIZONTAL_PIXELS = 160,
17 GB_VIDEO_VERTICAL_PIXELS = 144,
18 GB_VIDEO_VBLANK_PIXELS = 10,
19 GB_VIDEO_VERTICAL_TOTAL_PIXELS = GB_VIDEO_VERTICAL_PIXELS + GB_VIDEO_VBLANK_PIXELS,
20
21 // TODO: Figure out exact lengths
22 GB_VIDEO_MODE_2_LENGTH = 76,
23 GB_VIDEO_MODE_3_LENGTH_BASE = 171,
24 GB_VIDEO_MODE_0_LENGTH_BASE = 209,
25
26 GB_VIDEO_HORIZONTAL_LENGTH = GB_VIDEO_MODE_0_LENGTH_BASE + GB_VIDEO_MODE_2_LENGTH + GB_VIDEO_MODE_3_LENGTH_BASE,
27
28 GB_VIDEO_MODE_1_LENGTH = GB_VIDEO_HORIZONTAL_LENGTH * GB_VIDEO_VBLANK_PIXELS,
29 GB_VIDEO_TOTAL_LENGTH = GB_VIDEO_HORIZONTAL_LENGTH * GB_VIDEO_VERTICAL_TOTAL_PIXELS,
30
31 GB_BASE_MAP = 0x1800,
32 GB_SIZE_MAP = 0x0400
33};
34
35DECL_BITFIELD(GBObjAttributes, uint8_t);
36DECL_BITS(GBObjAttributes, CGBPalette, 0, 3);
37DECL_BIT(GBObjAttributes, Bank, 3);
38DECL_BIT(GBObjAttributes, Palette, 4);
39DECL_BIT(GBObjAttributes, XFlip, 5);
40DECL_BIT(GBObjAttributes, YFlip, 6);
41DECL_BIT(GBObjAttributes, Priority, 7);
42
43struct GBObj {
44 uint8_t y;
45 uint8_t x;
46 uint8_t tile;
47 GBObjAttributes attr;
48};
49
50union GBOAM {
51 struct GBObj obj[40];
52 uint8_t raw[160];
53};
54
55enum GBModel;
56struct mTileCache;
57struct GBVideoRenderer {
58 void (*init)(struct GBVideoRenderer* renderer, enum GBModel model);
59 void (*deinit)(struct GBVideoRenderer* renderer);
60
61 uint8_t (*writeVideoRegister)(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value);
62 void (*writeVRAM)(struct GBVideoRenderer* renderer, uint16_t address);
63 void (*writePalette)(struct GBVideoRenderer* renderer, int index, uint16_t value);
64 void (*drawRange)(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* objOnLine, size_t nObj);
65 void (*finishScanline)(struct GBVideoRenderer* renderer, int y);
66 void (*finishFrame)(struct GBVideoRenderer* renderer);
67
68 void (*getPixels)(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels);
69 void (*putPixels)(struct GBVideoRenderer* renderer, size_t stride, const void* pixels);
70
71 uint8_t* vram;
72 union GBOAM* oam;
73 struct mTileCache* cache;
74};
75
76DECL_BITFIELD(GBRegisterLCDC, uint8_t);
77DECL_BIT(GBRegisterLCDC, BgEnable, 0);
78DECL_BIT(GBRegisterLCDC, ObjEnable, 1);
79DECL_BIT(GBRegisterLCDC, ObjSize, 2);
80DECL_BIT(GBRegisterLCDC, TileMap, 3);
81DECL_BIT(GBRegisterLCDC, TileData, 4);
82DECL_BIT(GBRegisterLCDC, Window, 5);
83DECL_BIT(GBRegisterLCDC, WindowTileMap, 6);
84DECL_BIT(GBRegisterLCDC, Enable, 7);
85
86DECL_BITFIELD(GBRegisterSTAT, uint8_t);
87DECL_BITS(GBRegisterSTAT, Mode, 0, 2);
88DECL_BIT(GBRegisterSTAT, LYC, 2);
89DECL_BIT(GBRegisterSTAT, HblankIRQ, 3);
90DECL_BIT(GBRegisterSTAT, VblankIRQ, 4);
91DECL_BIT(GBRegisterSTAT, OAMIRQ, 5);
92DECL_BIT(GBRegisterSTAT, LYCIRQ, 6);
93
94struct GBVideo {
95 struct GB* p;
96 struct GBVideoRenderer* renderer;
97
98 int x;
99 int ly;
100 GBRegisterSTAT stat;
101
102 int mode;
103
104 int32_t nextEvent;
105 int32_t eventDiff;
106
107 int32_t nextMode;
108 int32_t dotCounter;
109
110 int32_t nextFrame;
111
112 uint8_t* vram;
113 uint8_t* vramBank;
114 int vramCurrentBank;
115
116 union GBOAM oam;
117 struct GBObj objThisLine[10];
118 int objMax;
119
120 int bcpIndex;
121 bool bcpIncrement;
122 int ocpIndex;
123 bool ocpIncrement;
124
125 uint16_t palette[64];
126
127 int32_t frameCounter;
128 int frameskip;
129 int frameskipCounter;
130};
131
132void GBVideoInit(struct GBVideo* video);
133void GBVideoReset(struct GBVideo* video);
134void GBVideoDeinit(struct GBVideo* video);
135void GBVideoAssociateRenderer(struct GBVideo* video, struct GBVideoRenderer* renderer);
136int32_t GBVideoProcessEvents(struct GBVideo* video, int32_t cycles);
137void GBVideoProcessDots(struct GBVideo* video);
138
139void GBVideoWriteLCDC(struct GBVideo* video, GBRegisterLCDC value);
140void GBVideoWriteSTAT(struct GBVideo* video, GBRegisterSTAT value);
141void GBVideoWriteLYC(struct GBVideo* video, uint8_t value);
142void GBVideoWritePalette(struct GBVideo* video, uint16_t address, uint8_t value);
143void GBVideoSwitchBank(struct GBVideo* video, uint8_t value);
144
145struct GBSerializedState;
146void GBVideoSerialize(const struct GBVideo* video, struct GBSerializedState* state);
147void GBVideoDeserialize(struct GBVideo* video, const struct GBSerializedState* state);
148
149#endif