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 GB_BASE_MAP = 0x1800,
29 GB_SIZE_MAP = 0x0400
30};
31
32struct GBVideoRenderer {
33 void (*init)(struct GBVideoRenderer* renderer);
34 void (*reset)(struct GBVideoRenderer* renderer);
35 void (*deinit)(struct GBVideoRenderer* renderer);
36
37 uint8_t (*writeVideoRegister)(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value);
38 void (*writeVRAM)(struct GBVideoRenderer* renderer, uint16_t address);
39 void (*drawScanline)(struct GBVideoRenderer* renderer, int y);
40 void (*finishFrame)(struct GBVideoRenderer* renderer);
41
42 void (*getPixels)(struct GBVideoRenderer* renderer, unsigned* stride, const void** pixels);
43 void (*putPixels)(struct GBVideoRenderer* renderer, unsigned stride, void* pixels);
44
45 uint8_t* vram;
46};
47
48DECL_BITFIELD(GBRegisterLCDC, uint8_t);
49DECL_BIT(GBRegisterLCDC, BgEnable, 0);
50DECL_BIT(GBRegisterLCDC, ObjEnable, 1);
51DECL_BIT(GBRegisterLCDC, ObjSize, 2);
52DECL_BIT(GBRegisterLCDC, TileMap, 3);
53DECL_BIT(GBRegisterLCDC, TileData, 4);
54DECL_BIT(GBRegisterLCDC, Window, 5);
55DECL_BIT(GBRegisterLCDC, WindowTileMap, 6);
56DECL_BIT(GBRegisterLCDC, Enable, 7);
57
58DECL_BITFIELD(GBRegisterSTAT, uint8_t);
59DECL_BITS(GBRegisterSTAT, Mode, 0, 2);
60DECL_BIT(GBRegisterSTAT, LYC, 2);
61DECL_BIT(GBRegisterSTAT, HblankIRQ, 3);
62DECL_BIT(GBRegisterSTAT, VblankIRQ, 4);
63DECL_BIT(GBRegisterSTAT, OAMIRQ, 5);
64DECL_BIT(GBRegisterSTAT, LYCIRQ, 6);
65
66struct GBVideo {
67 struct GB* p;
68 struct GBVideoRenderer* renderer;
69
70 int ly;
71 GBRegisterSTAT stat;
72
73 int mode;
74
75 int32_t nextEvent;
76 int32_t eventDiff;
77
78 int32_t nextMode;
79
80 uint8_t* vram;
81 uint8_t* vramBank;
82
83 int32_t frameCounter;
84 int frameskip;
85 int frameskipCounter;
86};
87
88void GBVideoInit(struct GBVideo* video);
89void GBVideoReset(struct GBVideo* video);
90void GBVideoDeinit(struct GBVideo* video);
91void GBVideoAssociateRenderer(struct GBVideo* video, struct GBVideoRenderer* renderer);
92int32_t GBVideoProcessEvents(struct GBVideo* video, int32_t cycles);
93
94void GBVideoWriteLCDC(struct GBVideo* video, GBRegisterLCDC value);
95void GBVideoWriteSTAT(struct GBVideo* video, GBRegisterSTAT value);
96
97#endif