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);
49DECL_BITS(GBRegisterSTAT, Mode, 0, 2);
50DECL_BIT(GBRegisterSTAT, LYC, 2);
51DECL_BIT(GBRegisterSTAT, HblankIRQ, 3);
52DECL_BIT(GBRegisterSTAT, VblankIRQ, 4);
53DECL_BIT(GBRegisterSTAT, OAMIRQ, 5);
54DECL_BIT(GBRegisterSTAT, LYCIRQ, 6);
55
56struct GBVideo {
57 struct GB* p;
58 struct GBVideoRenderer* renderer;
59
60 int ly;
61 GBRegisterSTAT stat;
62
63 int mode;
64
65 int32_t nextEvent;
66 int32_t eventDiff;
67
68 int32_t nextMode;
69
70 uint8_t* vram;
71
72 int32_t frameCounter;
73 int frameskip;
74 int frameskipCounter;
75};
76
77void GBVideoInit(struct GBVideo* video);
78void GBVideoReset(struct GBVideo* video);
79void GBVideoDeinit(struct GBVideo* video);
80void GBVideoAssociateRenderer(struct GBVideo* video, struct GBVideoRenderer* renderer);
81int32_t GBVideoProcessEvents(struct GBVideo* video, int32_t cycles);
82
83void GBVideoWriteLCDC(struct GBVideo* video, GBRegisterLCDC value);
84void GBVideoWriteSTAT(struct GBVideo* video, GBRegisterSTAT value);
85
86#endif