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 GB_VIDEO_MODE_0_LENGTH = 203, // Estimates, figure out with more precision
20 GB_VIDEO_MODE_2_LENGTH = 81,
21 GB_VIDEO_MODE_3_LENGTH = 172,
22
23 GB_VIDEO_HORIZONTAL_LENGTH = GB_VIDEO_MODE_0_LENGTH + GB_VIDEO_MODE_2_LENGTH + GB_VIDEO_MODE_3_LENGTH,
24
25 GB_VIDEO_MODE_1_LENGTH = GB_VIDEO_HORIZONTAL_LENGTH * GB_VIDEO_VBLANK_PIXELS,
26 GB_VIDEO_TOTAL_LENGTH = GB_VIDEO_HORIZONTAL_LENGTH * GB_VIDEO_VERTICAL_TOTAL_PIXELS,
27};
28
29struct GBVideoRenderer {
30 void (*init)(struct GBVideoRenderer* renderer);
31 void (*reset)(struct GBVideoRenderer* renderer);
32 void (*deinit)(struct GBVideoRenderer* renderer);
33
34 uint8_t (*writeVideoRegister)(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value);
35 void (*writeVRAM)(struct GBVideoRenderer* renderer, uint16_t address);
36 void (*drawScanline)(struct GBVideoRenderer* renderer, int y);
37 void (*finishFrame)(struct GBVideoRenderer* renderer);
38
39 void (*getPixels)(struct GBVideoRenderer* renderer, unsigned* stride, const void** pixels);
40 void (*putPixels)(struct GBVideoRenderer* renderer, unsigned stride, void* pixels);
41
42 uint8_t* vram;
43};
44
45DECL_BITFIELD(GBRegisterLCDC, uint8_t);
46DECL_BIT(GBRegisterLCDC, Enable, 7);
47
48DECL_BITFIELD(GBRegisterSTAT, uint8_t);
49
50struct GBVideo {
51 struct GB* p;
52 struct GBVideoRenderer* renderer;
53
54 int ly;
55 int mode;
56
57 int32_t nextEvent;
58 int32_t eventDiff;
59
60 int32_t nextMode;
61
62 uint8_t* vram;
63
64 int32_t frameCounter;
65 int frameskip;
66 int frameskipCounter;
67};
68
69void GBVideoInit(struct GBVideo* video);
70void GBVideoReset(struct GBVideo* video);
71void GBVideoDeinit(struct GBVideo* video);
72void GBVideoAssociateRenderer(struct GBVideo* video, struct GBVideoRenderer* renderer);
73int32_t GBVideoProcessEvents(struct GBVideo* video, int32_t cycles);
74
75void GBVideoWriteLCDC(struct GBVideo* video, GBRegisterLCDC value);
76void GBVideoWriteSTAT(struct GBVideo* video, GBRegisterSTAT value);
77
78#endif