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 
11 CXX_GUARD_START
12 
13 #include <mgba-util/vector.h>
14 
15 struct mCore;
16 struct mStateExtdataItem;
17 
18 #ifdef COLOR_16_BIT
19 typedef uint16_t color_t;
20 #define BYTES_PER_PIXEL 2
21 #else
22 typedef 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 PYCPARSE
mColorFrom555(uint16_t value)40 static inline color_t mColorFrom555(uint16_t value) {
41 #ifdef COLOR_16_BIT
42 #ifdef COLOR_5_6_5
43 	color_t color = 0;
44 	color |= (value & 0x001F) << 11;
45 	color |= (value & 0x03E0) << 1;
46 	color |= (value & 0x7C00) >> 10;
47 #else
48 	color_t color = value;
49 #endif
50 #else
51 	color_t color = M_RGB5_TO_BGR8(value);
52 	color |= (color >> 5) & 0x070707;
53 #endif
54 	return color;
55 }
56 #endif
57 
58 struct blip_t;
59 
60 enum mColorFormat {
61 	mCOLOR_XBGR8  = 0x00001,
62 	mCOLOR_XRGB8  = 0x00002,
63 	mCOLOR_BGRX8  = 0x00004,
64 	mCOLOR_RGBX8  = 0x00008,
65 	mCOLOR_ABGR8  = 0x00010,
66 	mCOLOR_ARGB8  = 0x00020,
67 	mCOLOR_BGRA8  = 0x00040,
68 	mCOLOR_RGBA8  = 0x00080,
69 	mCOLOR_RGB5   = 0x00100,
70 	mCOLOR_BGR5   = 0x00200,
71 	mCOLOR_RGB565 = 0x00400,
72 	mCOLOR_BGR565 = 0x00800,
73 	mCOLOR_ARGB5  = 0x01000,
74 	mCOLOR_ABGR5  = 0x02000,
75 	mCOLOR_RGBA5  = 0x04000,
76 	mCOLOR_BGRA5  = 0x08000,
77 	mCOLOR_RGB8   = 0x10000,
78 	mCOLOR_BGR8   = 0x20000,
79 
80 	mCOLOR_ANY    = -1
81 };
82 
83 enum mCoreFeature {
84 	mCORE_FEATURE_OPENGL = 1,
85 };
86 
87 struct mCoreCallbacks {
88 	void* context;
89 	void (*videoFrameStarted)(void* context);
90 	void (*videoFrameEnded)(void* context);
91 	void (*coreCrashed)(void* context);
92 	void (*sleep)(void* context);
93 	void (*keysRead)(void* context);
94 };
95 
96 DECLARE_VECTOR(mCoreCallbacksList, struct mCoreCallbacks);
97 
98 struct mAVStream {
99 	void (*videoDimensionsChanged)(struct mAVStream*, unsigned width, unsigned height);
100 	void (*postVideoFrame)(struct mAVStream*, const color_t* buffer, size_t stride);
101 	void (*postAudioFrame)(struct mAVStream*, int16_t left, int16_t right);
102 	void (*postAudioBuffer)(struct mAVStream*, struct blip_t* left, struct blip_t* right);
103 };
104 
105 struct mKeyCallback {
106 	uint16_t (*readKeys)(struct mKeyCallback*);
107 };
108 
109 enum mPeripheral {
110 	mPERIPH_ROTATION = 1,
111 	mPERIPH_RUMBLE,
112 	mPERIPH_IMAGE_SOURCE,
113 	mPERIPH_CUSTOM = 0x1000
114 };
115 
116 struct mRotationSource {
117 	void (*sample)(struct mRotationSource*);
118 
119 	int32_t (*readTiltX)(struct mRotationSource*);
120 	int32_t (*readTiltY)(struct mRotationSource*);
121 
122 	int32_t (*readGyroZ)(struct mRotationSource*);
123 };
124 
125 struct mRTCSource {
126 	void (*sample)(struct mRTCSource*);
127 
128 	time_t (*unixTime)(struct mRTCSource*);
129 
130 	void (*serialize)(struct mRTCSource*, struct mStateExtdataItem*);
131 	bool (*deserialize)(struct mRTCSource*, const struct mStateExtdataItem*);
132 };
133 
134 struct mImageSource {
135 	void (*startRequestImage)(struct mImageSource*, unsigned w, unsigned h, int colorFormats);
136 	void (*stopRequestImage)(struct mImageSource*);
137 	void (*requestImage)(struct mImageSource*, const void** buffer, size_t* stride, enum mColorFormat* colorFormat);
138 };
139 
140 enum mRTCGenericType {
141 	RTC_NO_OVERRIDE,
142 	RTC_FIXED,
143 	RTC_FAKE_EPOCH,
144 	RTC_CUSTOM_START = 0x1000
145 };
146 
147 struct mRTCGenericSource {
148 	struct mRTCSource d;
149 	struct mCore* p;
150 	enum mRTCGenericType override;
151 	int64_t value;
152 	struct mRTCSource* custom;
153 };
154 
155 struct mRTCGenericState {
156 	int32_t type;
157 	int32_t padding;
158 	int64_t value;
159 };
160 
161 void mRTCGenericSourceInit(struct mRTCGenericSource* rtc, struct mCore* core);
162 
163 struct mRumble {
164 	void (*setRumble)(struct mRumble*, int enable);
165 };
166 
167 struct mCoreChannelInfo {
168 	size_t id;
169 	const char* internalName;
170 	const char* visibleName;
171 	const char* visibleType;
172 };
173 
174 enum mCoreMemoryBlockFlags {
175 	mCORE_MEMORY_READ = 0x01,
176 	mCORE_MEMORY_WRITE = 0x02,
177 	mCORE_MEMORY_RW = 0x03,
178 	mCORE_MEMORY_MAPPED = 0x10,
179 	mCORE_MEMORY_VIRTUAL = 0x20,
180 };
181 
182 struct mCoreMemoryBlock {
183 	size_t id;
184 	const char* internalName;
185 	const char* shortName;
186 	const char* longName;
187 	uint32_t start;
188 	uint32_t end;
189 	uint32_t size;
190 	uint32_t flags;
191 	uint16_t maxSegment;
192 	uint32_t segmentStart;
193 };
194 
195 CXX_GUARD_END
196 
197 #endif
198