include/mgba/core/interface.h (view raw)
1/* Copyright (c) 2013-2015 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 CORE_INTERFACE_H
7#define CORE_INTERFACE_H
8
9#include <mgba-util/common.h>
10
11CXX_GUARD_START
12
13#include <mgba-util/vector.h>
14
15struct mCore;
16struct mStateExtdataItem;
17
18#ifdef COLOR_16_BIT
19typedef uint16_t color_t;
20#define BYTES_PER_PIXEL 2
21#else
22typedef uint32_t color_t;
23#define BYTES_PER_PIXEL 4
24#endif
25
26#define M_R5(X) ((X) & 0x1F)
27#define M_G5(X) (((X) >> 5) & 0x1F)
28#define M_B5(X) (((X) >> 10) & 0x1F)
29
30#define M_R8(X) (((((X) << 3) & 0xF8) * 0x21) >> 5)
31#define M_G8(X) (((((X) >> 2) & 0xF8) * 0x21) >> 5)
32#define M_B8(X) (((((X) >> 7) & 0xF8) * 0x21) >> 5)
33
34#define M_RGB5_TO_BGR8(X) ((M_R5(X) << 3) | (M_G5(X) << 11) | (M_B5(X) << 19))
35#define M_RGB5_TO_RGB8(X) ((M_R5(X) << 19) | (M_G5(X) << 11) | (M_B5(X) << 3))
36#define M_RGB8_TO_BGR5(X) ((((X) & 0xF8) >> 3) | (((X) & 0xF800) >> 6) | (((X) & 0xF80000) >> 9))
37#define M_RGB8_TO_RGB5(X) ((((X) & 0xF8) << 7) | (((X) & 0xF800) >> 6) | (((X) & 0xF80000) >> 19))
38
39#ifndef COLOR_16_BIT
40#define M_COLOR_RED 0x000000FF
41#define M_COLOR_GREEN 0x0000FF00
42#define M_COLOR_BLUE 0x00FF0000
43#define M_COLOR_ALPHA 0xFF000000
44#define M_COLOR_WHITE 0x00FFFFFF
45#elif defined(COLOR_5_6_5)
46#define M_COLOR_RED 0x001F
47#define M_COLOR_GREEN 0x07E0
48#define M_COLOR_BLUE 0xF800
49#define M_COLOR_ALPHA 0x0000
50#define M_COLOR_WHITE 0xFFDF
51#else
52#define M_COLOR_RED 0x001F
53#define M_COLOR_GREEN 0x03E0
54#define M_COLOR_BLUE 0x7C00
55#define M_COLOR_ALPHA 0x1000
56#define M_COLOR_WHITE 0x7FFF
57#endif
58
59#ifndef PYCPARSE
60static inline color_t mColorFrom555(uint16_t value) {
61#ifdef COLOR_16_BIT
62#ifdef COLOR_5_6_5
63 color_t color = 0;
64 color |= (value & 0x001F) << 11;
65 color |= (value & 0x03E0) << 1;
66 color |= (value & 0x7C00) >> 10;
67#else
68 color_t color = value;
69#endif
70#else
71 color_t color = M_RGB5_TO_BGR8(value);
72 color |= (color >> 5) & 0x070707;
73#endif
74 return color;
75}
76
77ATTRIBUTE_UNUSED static unsigned mColorMix5Bit(int weightA, unsigned colorA, int weightB, unsigned colorB) {
78 unsigned c = 0;
79 unsigned a, b;
80#ifdef COLOR_16_BIT
81#ifdef COLOR_5_6_5
82 a = colorA & 0xF81F;
83 b = colorB & 0xF81F;
84 a |= (colorA & 0x7C0) << 16;
85 b |= (colorB & 0x7C0) << 16;
86 c = ((a * weightA + b * weightB) / 16);
87 if (c & 0x08000000) {
88 c = (c & ~0x0FC00000) | 0x07C00000;
89 }
90 if (c & 0x0020) {
91 c = (c & ~0x003F) | 0x001F;
92 }
93 if (c & 0x10000) {
94 c = (c & ~0x1F800) | 0xF800;
95 }
96 c = (c & 0xF81F) | ((c >> 16) & 0x07C0);
97#else
98 a = colorA & 0x7C1F;
99 b = colorB & 0x7C1F;
100 a |= (colorA & 0x3E0) << 16;
101 b |= (colorB & 0x3E0) << 16;
102 c = ((a * weightA + b * weightB) / 16);
103 if (c & 0x04000000) {
104 c = (c & ~0x07E00000) | 0x03E00000;
105 }
106 if (c & 0x0020) {
107 c = (c & ~0x003F) | 0x001F;
108 }
109 if (c & 0x8000) {
110 c = (c & ~0xF800) | 0x7C00;
111 }
112 c = (c & 0x7C1F) | ((c >> 16) & 0x03E0);
113#endif
114#else
115 a = colorA & 0xFF;
116 b = colorB & 0xFF;
117 c |= ((a * weightA + b * weightB) / 16) & 0x1FF;
118 if (c & 0x00000100) {
119 c = 0x000000FF;
120 }
121
122 a = colorA & 0xFF00;
123 b = colorB & 0xFF00;
124 c |= ((a * weightA + b * weightB) / 16) & 0x1FF00;
125 if (c & 0x00010000) {
126 c = (c & 0x000000FF) | 0x0000FF00;
127 }
128
129 a = colorA & 0xFF0000;
130 b = colorB & 0xFF0000;
131 c |= ((a * weightA + b * weightB) / 16) & 0x1FF0000;
132 if (c & 0x01000000) {
133 c = (c & 0x0000FFFF) | 0x00FF0000;
134 }
135#endif
136 return c;
137}
138#endif
139
140struct blip_t;
141
142enum mColorFormat {
143 mCOLOR_XBGR8 = 0x00001,
144 mCOLOR_XRGB8 = 0x00002,
145 mCOLOR_BGRX8 = 0x00004,
146 mCOLOR_RGBX8 = 0x00008,
147 mCOLOR_ABGR8 = 0x00010,
148 mCOLOR_ARGB8 = 0x00020,
149 mCOLOR_BGRA8 = 0x00040,
150 mCOLOR_RGBA8 = 0x00080,
151 mCOLOR_RGB5 = 0x00100,
152 mCOLOR_BGR5 = 0x00200,
153 mCOLOR_RGB565 = 0x00400,
154 mCOLOR_BGR565 = 0x00800,
155 mCOLOR_ARGB5 = 0x01000,
156 mCOLOR_ABGR5 = 0x02000,
157 mCOLOR_RGBA5 = 0x04000,
158 mCOLOR_BGRA5 = 0x08000,
159 mCOLOR_RGB8 = 0x10000,
160 mCOLOR_BGR8 = 0x20000,
161
162 mCOLOR_ANY = -1
163};
164
165enum mCoreFeature {
166 mCORE_FEATURE_OPENGL = 1,
167};
168
169struct mCoreCallbacks {
170 void* context;
171 void (*videoFrameStarted)(void* context);
172 void (*videoFrameEnded)(void* context);
173 void (*coreCrashed)(void* context);
174 void (*sleep)(void* context);
175 void (*shutdown)(void* context);
176 void (*keysRead)(void* context);
177 void (*savedataUpdated)(void* context);
178};
179
180DECLARE_VECTOR(mCoreCallbacksList, struct mCoreCallbacks);
181
182struct mAVStream {
183 void (*videoDimensionsChanged)(struct mAVStream*, unsigned width, unsigned height);
184 void (*postVideoFrame)(struct mAVStream*, const color_t* buffer, size_t stride);
185 void (*postAudioFrame)(struct mAVStream*, int16_t left, int16_t right);
186 void (*postAudioBuffer)(struct mAVStream*, struct blip_t* left, struct blip_t* right);
187};
188
189struct mKeyCallback {
190 uint16_t (*readKeys)(struct mKeyCallback*);
191};
192
193enum mPeripheral {
194 mPERIPH_ROTATION = 1,
195 mPERIPH_RUMBLE,
196 mPERIPH_IMAGE_SOURCE,
197 mPERIPH_CUSTOM = 0x1000
198};
199
200struct mRotationSource {
201 void (*sample)(struct mRotationSource*);
202
203 int32_t (*readTiltX)(struct mRotationSource*);
204 int32_t (*readTiltY)(struct mRotationSource*);
205
206 int32_t (*readGyroZ)(struct mRotationSource*);
207};
208
209struct mRTCSource {
210 void (*sample)(struct mRTCSource*);
211
212 time_t (*unixTime)(struct mRTCSource*);
213
214 void (*serialize)(struct mRTCSource*, struct mStateExtdataItem*);
215 bool (*deserialize)(struct mRTCSource*, const struct mStateExtdataItem*);
216};
217
218struct mImageSource {
219 void (*startRequestImage)(struct mImageSource*, unsigned w, unsigned h, int colorFormats);
220 void (*stopRequestImage)(struct mImageSource*);
221 void (*requestImage)(struct mImageSource*, const void** buffer, size_t* stride, enum mColorFormat* colorFormat);
222};
223
224enum mRTCGenericType {
225 RTC_NO_OVERRIDE,
226 RTC_FIXED,
227 RTC_FAKE_EPOCH,
228 RTC_CUSTOM_START = 0x1000
229};
230
231struct mRTCGenericSource {
232 struct mRTCSource d;
233 struct mCore* p;
234 enum mRTCGenericType override;
235 int64_t value;
236 struct mRTCSource* custom;
237};
238
239struct mRTCGenericState {
240 int32_t type;
241 int32_t padding;
242 int64_t value;
243};
244
245void mRTCGenericSourceInit(struct mRTCGenericSource* rtc, struct mCore* core);
246
247struct mRumble {
248 void (*setRumble)(struct mRumble*, int enable);
249};
250
251struct mCoreChannelInfo {
252 size_t id;
253 const char* internalName;
254 const char* visibleName;
255 const char* visibleType;
256};
257
258enum mCoreMemoryBlockFlags {
259 mCORE_MEMORY_READ = 0x01,
260 mCORE_MEMORY_WRITE = 0x02,
261 mCORE_MEMORY_RW = 0x03,
262 mCORE_MEMORY_WORM = 0x04,
263 mCORE_MEMORY_MAPPED = 0x10,
264 mCORE_MEMORY_VIRTUAL = 0x20,
265};
266
267struct mCoreMemoryBlock {
268 size_t id;
269 const char* internalName;
270 const char* shortName;
271 const char* longName;
272 uint32_t start;
273 uint32_t end;
274 uint32_t size;
275 uint32_t flags;
276 uint16_t maxSegment;
277 uint32_t segmentStart;
278};
279
280CXX_GUARD_END
281
282#endif