-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathnesemu.hpp
429 lines (382 loc) · 11.7 KB
/
nesemu.hpp
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#pragma once
//=====================================================================//
/*! @file
@brief NES Emulator ハンドラー
@author 平松邦仁 ([email protected])
@copyright Copyright (C) 2018, 2019 Kunihito Hiramatsu @n
Released under the MIT license @n
https://github.com/hirakuni45/RX/blob/master/LICENSE
*/
//=====================================================================//
#include "common/string_utils.hpp"
#include "emu/log.h"
#include "emu/nes/nes.h"
#include "emu/nes/nesinput.h"
#include "emu/nes/nesstate.h"
#include "emu/nes/nes_pal.h"
#include "emu/cpu/dis6502.hpp"
#include "chip/FAMIPAD.hpp"
extern "C" {
uint8_t get_fami_pad();
}
namespace emu {
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief NESエミュレーション・クラス
@param[in] RENDER レンダリングクラス
@param[in] AUDIO_SAMPLE_RATE オーディオ・サンプル・レート
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
template <class RENDER, uint32_t AUDIO_SAMPLE_RATE>
class nesemu {
static constexpr int nes_width_ = 256;
static constexpr int nes_height_ = 240;
static constexpr int sample_rate_ = AUDIO_SAMPLE_RATE;
static constexpr int sample_bits_ = 16;
static constexpr int audio_len_ = (sample_rate_ / 60) + 1;
RENDER& render_;
uint16_t audio_buf_[audio_len_];
bool nesrom_;
cpu::dis6502 disa_;
uint16_t mon_val_[3];
nesinput_t inp_[2];
public:
//-----------------------------------------------------------------//
/*!
@brief コンストラクタ
*/
//-----------------------------------------------------------------//
nesemu(RENDER& render) noexcept : render_(render), audio_buf_{ 0 }, nesrom_(false),
disa_(nes6502_getbyte, nes6502_putbyte),
mon_val_{ 0 }
{ }
//-----------------------------------------------------------------//
/*!
@brief 開始
@param[in] オーディオをサポートしない場合「false」
@return 成功なら「true」
*/
//-----------------------------------------------------------------//
bool start(bool audio_ena = true)
{
log_init();
nes_create(sample_rate_, sample_bits_);
inp_[0].type = INP_JOYPAD0;
inp_[0].data = 0;
input_register(&inp_[0]);
inp_[1].type = INP_JOYPAD1;
inp_[1].data = 0;
input_register(&inp_[1]);
return true;
}
//-----------------------------------------------------------------//
/*!
@brief エミュレーター・ファイルを開く
@param[in] filename ファイル名
@return 成功なら「true」
*/
//-----------------------------------------------------------------//
bool open(const char* filename)
{
if(nes_insert_cart(filename) == 0) {
nesrom_ = true;
}
return nesrom_;
}
//-----------------------------------------------------------------//
/*!
@brief カートリッジが有効か?
@return 有効なら「true」
*/
//-----------------------------------------------------------------//
bool probe() const { return nesrom_; }
//-----------------------------------------------------------------//
/*!
@brief 一時停止の取得
@return 一時停止
*/
//-----------------------------------------------------------------//
bool get_pause() const noexcept {
const nes_t* t = nes_getcontext();
return t->pause;
}
//-----------------------------------------------------------------//
/*!
@brief 一時停止
*/
//-----------------------------------------------------------------//
void enable_pause(bool ena = true) noexcept {
if(!nesrom_) return;
nes_pause(ena);
}
//-----------------------------------------------------------------//
/*!
@brief 情報取得
@return 情報テキスト
*/
//-----------------------------------------------------------------//
const char* get_info() noexcept {
nes_t* t = nes_getcontext();
if(t == nullptr) return nullptr;
if(t->rominfo == nullptr) return nullptr;
return rom_getinfo(t->rominfo);
}
//-----------------------------------------------------------------//
/*!
@brief エミュレーターを終了
*/
//-----------------------------------------------------------------//
void close() noexcept
{
if(nesrom_) {
nes_eject_cart();
nesrom_ = false;
}
}
//-----------------------------------------------------------------//
/*!
@brief サービス
*/
//-----------------------------------------------------------------//
void service() noexcept
{
auto nes = nes_getcontext();
bitmap_t* v = nes->vidbuf;
const rgb_t* lut = get_palette();
if(v == nullptr || lut == nullptr) {
return;
}
{
inp_[0].data = 0; // Player 1
inp_[1].data = 0; // Player 2
uint8_t pad = get_fami_pad();
if(chip::on(pad, chip::FAMIPAD_ST::A)) {
inp_[0].data |= INP_PAD_A;
}
if(chip::on(pad, chip::FAMIPAD_ST::B)) {
inp_[0].data |= INP_PAD_B;
}
if(chip::on(pad, chip::FAMIPAD_ST::SELECT)) {
inp_[0].data |= INP_PAD_SELECT;
}
if(chip::on(pad, chip::FAMIPAD_ST::START)) {
inp_[0].data |= INP_PAD_START;
}
if(chip::on(pad, chip::FAMIPAD_ST::UP)) {
inp_[0].data |= INP_PAD_UP;
}
if(chip::on(pad, chip::FAMIPAD_ST::DOWN)) {
inp_[0].data |= INP_PAD_DOWN;
}
if(chip::on(pad, chip::FAMIPAD_ST::LEFT)) {
inp_[0].data |= INP_PAD_LEFT;
}
if(chip::on(pad, chip::FAMIPAD_ST::RIGHT)) {
inp_[0].data |= INP_PAD_RIGHT;
}
}
#if 0
uint16_t luttmp[256];
for(uint32_t i = 0; i < 64; ++i) {
uint16_t r = lut[i].r; // R
uint16_t g = lut[i].g; // G
uint16_t b = lut[i].b; // B
// R(5), G(6), B(5)
luttmp[i] = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | (b >> 3);
luttmp[i+128+64] = luttmp[i+128] = luttmp[i+64] = luttmp[i];
}
uint16_t* dst = static_cast<uint16_t*>(org);
dst += ((ys - (nes_height_ - 16)) / 2) * xs;
const uint8_t* src = v->data;
src += v->pitch * 16;
for(int h = 0; h < (nes_height_ - 16); ++h) {
uint16_t* tmp = dst;
tmp += (xs - nes_width_) / 2;
for(int w = 0; w < nes_width_ / 16; ++w) {
*tmp++ = luttmp[*src++]; *tmp++ = luttmp[*src++];
*tmp++ = luttmp[*src++]; *tmp++ = luttmp[*src++];
*tmp++ = luttmp[*src++]; *tmp++ = luttmp[*src++];
*tmp++ = luttmp[*src++]; *tmp++ = luttmp[*src++];
*tmp++ = luttmp[*src++]; *tmp++ = luttmp[*src++];
*tmp++ = luttmp[*src++]; *tmp++ = luttmp[*src++];
*tmp++ = luttmp[*src++]; *tmp++ = luttmp[*src++];
*tmp++ = luttmp[*src++]; *tmp++ = luttmp[*src++];
}
src += v->pitch - nes_width_;
dst += xs;
}
#else
uint32_t* clut = render_.get_clut();
for(uint32_t i = 0; i < 64; ++i) {
clut[i] = (lut[i].r << 16) | (lut[i].g << 8) | (lut[i].b);
clut[i+128+64] = clut[i+128] = clut[i+64] = clut[i];
}
render_.set_clut(0, 256);
int16_t ox = (RENDER::glc_type::width - nes_width_) / 2;
int16_t oy = (RENDER::glc_type::height - nes_height_) / 2;
render_.draw_indexed8(vtx::spos(ox, oy), v->data, vtx::spos(nes_width_, nes_height_), v->pitch);
#endif
if(nesrom_) {
apu_process(audio_buf_, audio_len_);
nes_emulate(1);
}
}
//-----------------------------------------------------------------//
/*!
@brief オーディオ・バッファの長さを取得
@return オーディオ・バッファの長さ
*/
//-----------------------------------------------------------------//
uint32_t get_audio_len() const noexcept { return audio_len_; }
//-----------------------------------------------------------------//
/*!
@brief オーディオ・バッファを取得
@return オーディオ・バッファのポインター
*/
//-----------------------------------------------------------------//
const uint16_t* get_audio_buf() const noexcept { return audio_buf_; }
//-----------------------------------------------------------------//
/*!
@brief ステートのセーブ
@param[in] no スロット番号(0~9)
@return 成功なら「true」
*/
//-----------------------------------------------------------------//
bool save_state(int no) noexcept {
if(!nesrom_) return false;
state_setslot(no);
return state_save() == 0;
}
//-----------------------------------------------------------------//
/*!
@brief ステートのロード
@param[in] no スロット番号(0~9)
@return 成功なら「true」
*/
//-----------------------------------------------------------------//
bool load_state(int no) noexcept {
if(!nesrom_) return false;
state_setslot(no);
return state_load() == 0;
}
//-----------------------------------------------------------------//
/*!
@brief ハード・リセット
*/
//-----------------------------------------------------------------//
void reset() noexcept {
nes_reset(HARD_RESET);
}
//-----------------------------------------------------------------//
/*!
@brief モニター機能
@param[in] cmd コマンド
*/
//-----------------------------------------------------------------//
void monitor(const char* cmd) noexcept {
char ch;
uint16_t hexadec = 0;
bool write = false;
bool area = false;
bool search = false;
while(1) {
ch = *cmd++;
if(ch >= 'a' && ch <= 'z') ch -= 0x20;
if(ch >= '0' && ch <= '9') {
mon_val_[0] <<= 4;
mon_val_[0] |= static_cast<uint16_t>(ch - '0');
++hexadec;
} else if(ch >= 'A' && ch <= 'F') {
mon_val_[0] <<= 4;
mon_val_[0] |= static_cast<uint16_t>(ch - 'A' + 10);
++hexadec;
} else if(ch == 'L') { ///< disasm List
if(hexadec > 0) {
mon_val_[2] = mon_val_[0];
}
for(int i = 0; i < 20; ++i) {
char tmp[64];
disa_.disasm(mon_val_[2], tmp, sizeof(tmp));
utils::str::to_caps(tmp, tmp, sizeof(tmp));
utils::format("%s\n") % tmp;
mon_val_[2] = disa_.get_pc();
}
hexadec = 0;
write = false;
area = false;
} else if(ch == 'S') { ///< Search
mon_val_[2] = mon_val_[0];
search = true;
hexadec = 0;
} else if(ch == '.') {
mon_val_[1] = mon_val_[0];
write = false;
area = true;
hexadec = 0;
} else if(ch == ':') {
mon_val_[1] = mon_val_[0];
write = true;
area = false;
hexadec = 0;
} else if(ch == ' ' || ch == 0) {
if(write) {
if(hexadec > 0) {
nes6502_putbyte(mon_val_[1], mon_val_[0]);
mon_val_[1]++;
}
} else if(ch == 0) {
uint32_t org = 0;
uint32_t end = 0;
if(search) { // search
org = mon_val_[1];
end = mon_val_[2];
while(org <= end) {
auto rd = nes6502_getbyte(org);
if(rd == (mon_val_[0] & 0xff)) {
utils::format("%04X- %02X\n") % org % static_cast<uint16_t>(rd);
}
++org;
}
} else { // for dump
if(area) {
org = mon_val_[1];
if(hexadec > 0) {
end = mon_val_[0] + 1;
if(org > end) end = org;
} else {
end = org + 16;
}
area = false;
} else {
if(hexadec > 0) {
org = mon_val_[1] = mon_val_[0];
end = org + 1;
} else {
org = end = mon_val_[1];
}
}
while(org < end) {
char tmp[96];
auto l = end - org;
if(l == 0) l = 1;
else if(l > 16) l = 16;
org = disa_.dump(org, l, tmp, sizeof(tmp));
utils::format("%s\n") % tmp;
}
mon_val_[1] = org;
}
}
mon_val_[0] = 0;
hexadec = 0;
} else {
utils::format("%c?\n") % ch;
hexadec = 0;
write = false;
area = false;
search = false;
}
if(ch == 0) break;
}
}
};
}