-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsx2gw.cpp
260 lines (230 loc) · 6.94 KB
/
msx2gw.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/**
* micro MSX2+ - Wrapper Library for C
* -----------------------------------------------------------------------------
* The MIT License (MIT)
*
* Copyright (c) 2023 Yoji Suzuki.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* -----------------------------------------------------------------------------
*/
#include <pthread.h>
#include <chrono>
#include <thread>
#include <map>
#include <string>
#include "msx2.hpp"
#include "sha1.hpp"
#include "msx2gw.h"
class Binary {
public:
void *data;
size_t size;
Binary(const void *newData, size_t newSize) {
this->data = nullptr;
this->size = 0;
this->setNewData(newData, newSize);
}
void replace(const void *newData, size_t newSize) {
this->setNewData(newData, newSize);
}
~Binary() {
free(this->data);
}
private:
void setNewData(const void *newData, size_t newSize) {
if (this->data) free(this->data);
this->data = malloc(newSize);
this->size = newSize;
memcpy(this->data, newData, newSize);
}
};
class Context {
public:
MSX2* msx2;
std::map<std::string, Binary *> bintray;
pthread_mutex_t mutex{};
Context(int colorCode) {
msx2 = new MSX2(colorCode);
pthread_mutex_init(&this->mutex, nullptr);
}
~Context() {
delete msx2;
for (std::pair<std::string, Binary *> bin: this->bintray) {
delete bin.second;
}
}
void addBinary(std::string &label, const void *data, size_t size) {
if (this->bintray.find(label) == this->bintray.end()) {
this->bintray[label] = new Binary(data, size);
} else {
this->bintray[label]->replace(data, size);
}
}
void lock() {
pthread_mutex_lock(&this->mutex);
}
void unlock() {
pthread_mutex_unlock(&this->mutex);
}
};
extern "C" void* msx2_createContext(int colorCode)
{
return new Context(colorCode);
}
extern "C" void msx2_releaseContext(void* context)
{
delete (Context*)context;
}
extern "C" void msx2_setupSecondaryExist(void* context,
bool page0,
bool page1,
bool page2,
bool page3)
{
Context* c = (Context*)context;
c->lock();
c->msx2->setupSecondaryExist(page0, page1, page2, page3);
c->unlock();
}
extern "C" void msx2_setupRAM(void* context, int pri, int sec)
{
Context* c = (Context*)context;
c->lock();
c->msx2->setupRAM(pri, sec);
c->unlock();
}
extern "C" void msx2_setup(void* context,
int pri,
int sec,
int idx,
const void* data,
size_t size,
const char* label)
{
Context* c = (Context*)context;
c->lock();
std::string labelStr = label;
c->addBinary(labelStr, data, size);
c->msx2->setup(pri, sec, idx, c->bintray[labelStr]->data, (int)size, label);
c->unlock();
}
extern "C" void msx2_loadFont(void* context, const void* font, size_t size)
{
Context* c = (Context*)context;
c->lock();
std::string label = "KNJFNT16";
c->addBinary(label, font, size);
c->msx2->loadFont(c->bintray[label]->data, size);
c->unlock();
}
extern "C" void msx2_setupSpecialKeyCode(void* context, int select, int start)
{
Context* c = (Context*)context;
c->lock();
c->msx2->setupKeyAssign(0, MSX2_JOY_S2, select);
c->msx2->setupKeyAssign(0, MSX2_JOY_S1, start);
c->unlock();
}
extern "C" void msx2_tick(void* context, int pad1, int pad2, int key)
{
Context* c = (Context*)context;
c->lock();
c->msx2->tick(pad1 & 0xFF, pad2 & 0xff, key & 0xFF);
c->unlock();
}
extern "C" unsigned short* msx2_getDisplay(void* context)
{
return ((Context*)context)->msx2->getDisplay();
}
extern "C" int msx2_getDisplayWidth(void* context)
{
return ((Context*)context)->msx2->getDisplayWidth();
}
extern "C" int msx2_getDisplayHeight(void* context)
{
return ((Context*)context)->msx2->getDisplayHeight();
}
extern "C" const void* msx2_getSound(void* context, size_t* size)
{
return ((Context*)context)->msx2->getSound(size);
}
extern "C" void msx2_loadRom(void* context, const void* rom, size_t size, int romType)
{
Context* c = (Context*)context;
c->lock();
std::string label = "CART";
c->addBinary(label, rom, size);
c->msx2->loadRom(c->bintray[label]->data, (int)size, romType);
c->unlock();
}
extern "C" void msx2_ejectRom(void* context)
{
Context* c = (Context*)context;
c->lock();
c->msx2->ejectRom();
c->unlock();
}
extern "C" void msx2_insertDisk(void* context,
int driveId,
const void* disk,
size_t size,
bool readOnly)
{
char base64[SHA1_BASE64_SIZE];
sha1("DiskImage:")
.add(disk, (int)size)
.finalize()
.print_base64(base64);
std::string label = base64;
Context* c = (Context*)context;
c->lock();
c->addBinary(label, disk, size);
c->msx2->insertDisk(driveId, c->bintray[label], size, readOnly);
c->unlock();
}
extern "C" void msx2_ejectDisk(void* context, int driveId)
{
Context* c = (Context*)context;
c->lock();
c->msx2->ejectDisk(driveId);
c->unlock();
}
extern "C" const void* msx2_quickSave(void* context, size_t* size)
{
Context* c = (Context*)context;
c->lock();
const void* result = c->msx2->quickSave(size);
c->unlock();
return result;
}
extern "C" void msx2_quickLoad(void* context, const void* save, size_t size)
{
Context* c = (Context*)context;
c->lock();
c->msx2->quickLoad(save, size);
c->unlock();
}
extern "C" void msx2_reset(void* context)
{
Context* c = (Context*)context;
c->lock();
c->msx2->reset();
c->unlock();
}