1 #include "libretro.h"
2 #include "libretro_core_options.h"
3 #include <stdint.h>
4 #include <string.h>
5 #include <math.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <sstream>
9 #include <fstream>
10
11 #include "../source/core/api/NstApiMachine.hpp"
12 #include "../source/core/api/NstApiEmulator.hpp"
13 #include "../source/core/api/NstApiVideo.hpp"
14 #include "../source/core/api/NstApiCheats.hpp"
15 #include "../source/core/api/NstApiSound.hpp"
16 #include "../source/core/api/NstApiInput.hpp"
17 #include "../source/core/api/NstApiCartridge.hpp"
18 #include "../source/core/api/NstApiUser.hpp"
19 #include "../source/core/api/NstApiFds.hpp"
20
21 #include "../source/core/NstMachine.hpp"
22
23 #include "nstdatabase.hpp"
24
25 #define NST_VERSION "1.50-WIP"
26
27 #define MIN(a,b) ((a)<(b)?(a):(b))
28 #define MAX(a,b) ((a)>(b)?(a):(b))
29 #define NES_NTSC_PAR ((Api::Video::Output::WIDTH - (overscan_h ? 16 : 0)) * (8.0 / 7.0)) / (Api::Video::Output::HEIGHT - (overscan_v ? 16 : 0))
30 #define NES_PAL_PAR ((Api::Video::Output::WIDTH - (overscan_h ? 16 : 0)) * (2950000.0 / 2128137.0)) / (Api::Video::Output::HEIGHT - (overscan_v ? 16 : 0))
31 #define NES_4_3_DAR (4.0 / 3.0);
32 #define SAMPLERATE 48000
33
34 using namespace Nes;
35
36 static retro_log_printf_t log_cb;
37 static retro_video_refresh_t video_cb;
38 static retro_audio_sample_t audio_cb;
39 static retro_audio_sample_batch_t audio_batch_cb;
40 static retro_environment_t environ_cb;
41 static retro_input_poll_t input_poll_cb;
42 static retro_input_state_t input_state_cb;
43
44 #ifdef _3DS
45 extern "C" void* linearMemAlign(size_t size, size_t alignment);
46 extern "C" void linearFree(void* mem);
47 #endif
48 static uint32_t* video_buffer = NULL;
49
50 static int16_t audio_buffer[(SAMPLERATE / 50)];
51 static int16_t audio_stereo_buffer[2 * (SAMPLERATE / 50)];
52 static Api::Emulator emulator;
53 static Api::Machine *machine;
54 static Api::Fds *fds;
55 static char g_basename[256];
56 static char g_rom_dir[256];
57 static char *g_save_dir;
58 static char samp_dir[256];
59 static unsigned blargg_ntsc;
60 static bool fds_auto_insert;
61 static bool overscan_v;
62 static bool overscan_h;
63 static unsigned aspect_ratio_mode;
64 static unsigned tpulse;
65 static bool libretro_supports_bitmasks = false;
66 static bool show_advanced_av_settings = true;
67
68 int16_t video_width = Api::Video::Output::WIDTH;
69 size_t pitch;
70
71 static Api::Video::Output *video;
72 static Api::Sound::Output *audio;
73 static Api::Input::Controllers *input;
74 static unsigned input_type[4];
75 static Api::Machine::FavoredSystem favsystem;
76
77 static void *sram;
78 static unsigned long sram_size;
79 static bool is_pal;
80 static byte custpal[64*3];
81 static char slash;
82
83 static const byte cxa2025as_palette[64][3] =
84 {
85 {0x58,0x58,0x58}, {0x00,0x23,0x8C}, {0x00,0x13,0x9B}, {0x2D,0x05,0x85},
86 {0x5D,0x00,0x52}, {0x7A,0x00,0x17}, {0x7A,0x08,0x00}, {0x5F,0x18,0x00},
87 {0x35,0x2A,0x00}, {0x09,0x39,0x00}, {0x00,0x3F,0x00}, {0x00,0x3C,0x22},
88 {0x00,0x32,0x5D}, {0x00,0x00,0x00}, {0x00,0x00,0x00}, {0x00,0x00,0x00},
89 {0xA1,0xA1,0xA1}, {0x00,0x53,0xEE}, {0x15,0x3C,0xFE}, {0x60,0x28,0xE4},
90 {0xA9,0x1D,0x98}, {0xD4,0x1E,0x41}, {0xD2,0x2C,0x00}, {0xAA,0x44,0x00},
91 {0x6C,0x5E,0x00}, {0x2D,0x73,0x00}, {0x00,0x7D,0x06}, {0x00,0x78,0x52},
92 {0x00,0x69,0xA9}, {0x00,0x00,0x00}, {0x00,0x00,0x00}, {0x00,0x00,0x00},
93 {0xFF,0xFF,0xFF}, {0x1F,0xA5,0xFE}, {0x5E,0x89,0xFE}, {0xB5,0x72,0xFE},
94 {0xFE,0x65,0xF6}, {0xFE,0x67,0x90}, {0xFE,0x77,0x3C}, {0xFE,0x93,0x08},
95 {0xC4,0xB2,0x00}, {0x79,0xCA,0x10}, {0x3A,0xD5,0x4A}, {0x11,0xD1,0xA4},
96 {0x06,0xBF,0xFE}, {0x42,0x42,0x42}, {0x00,0x00,0x00}, {0x00,0x00,0x00},
97 {0xFF,0xFF,0xFF}, {0xA0,0xD9,0xFE}, {0xBD,0xCC,0xFE}, {0xE1,0xC2,0xFE},
98 {0xFE,0xBC,0xFB}, {0xFE,0xBD,0xD0}, {0xFE,0xC5,0xA9}, {0xFE,0xD1,0x8E},
99 {0xE9,0xDE,0x86}, {0xC7,0xE9,0x92}, {0xA8,0xEE,0xB0}, {0x95,0xEC,0xD9},
100 {0x91,0xE4,0xFE}, {0xAC,0xAC,0xAC}, {0x00,0x00,0x00}, {0x00,0x00,0x00}
101 };
102
103 static const byte pal_palette[64][3] =
104 {
105 {0x80,0x80,0x80}, {0x00,0x00,0xBA}, {0x37,0x00,0xBF}, {0x84,0x00,0xA6},
106 {0xBB,0x00,0x6A}, {0xB7,0x00,0x1E}, {0xB3,0x00,0x00}, {0x91,0x26,0x00},
107 {0x7B,0x2B,0x00}, {0x00,0x3E,0x00}, {0x00,0x48,0x0D}, {0x00,0x3C,0x22},
108 {0x00,0x2F,0x66}, {0x00,0x00,0x00}, {0x05,0x05,0x05}, {0x05,0x05,0x05},
109 {0xC8,0xC8,0xC8}, {0x00,0x59,0xFF}, {0x44,0x3C,0xFF}, {0xB7,0x33,0xCC},
110 {0xFE,0x33,0xAA}, {0xFE,0x37,0x5E}, {0xFE,0x37,0x1A}, {0xD5,0x4B,0x00},
111 {0xC4,0x62,0x00}, {0x3C,0x7B,0x00}, {0x1D,0x84,0x15}, {0x00,0x95,0x66},
112 {0x00,0x84,0xC4}, {0x11,0x11,0x11}, {0x09,0x09,0x09}, {0x09,0x09,0x09},
113 {0xFE,0xFE,0xFE}, {0x00,0x95,0xFF}, {0x6F,0x84,0xFF}, {0xD5,0x6F,0xFF},
114 {0xFE,0x77,0xCC}, {0xFE,0x6F,0x99}, {0xFE,0x7B,0x59}, {0xFE,0x91,0x5F},
115 {0xFE,0xA2,0x33}, {0xA6,0xBF,0x00}, {0x51,0xD9,0x6A}, {0x4D,0xD5,0xAE},
116 {0x00,0xD9,0xFF}, {0x66,0x66,0x66}, {0x0D,0x0D,0x0D}, {0x0D,0x0D,0x0D},
117 {0xFE,0xFE,0xFE}, {0x84,0xBF,0xFF}, {0xBB,0xBB,0xFF}, {0xD0,0xBB,0xFF},
118 {0xFE,0xBF,0xEA}, {0xFE,0xBF,0xCC}, {0xFE,0xC4,0xB7}, {0xFE,0xCC,0xAE},
119 {0xFE,0xD9,0xA2}, {0xCC,0xE1,0x99}, {0xAE,0xEE,0xB7}, {0xAA,0xF8,0xEE},
120 {0xB3,0xEE,0xFF}, {0xDD,0xDD,0xDD}, {0x11,0x11,0x11}, {0x11,0x11,0x11}
121 };
122
123 static const byte composite_direct_fbx_palette[64][3] =
124 {
125 {0x65,0x65,0x65}, {0x00,0x12,0x7D}, {0x18,0x00,0x8E}, {0x36,0x00,0x82},
126 {0x56,0x00,0x5D}, {0x5A,0x00,0x18}, {0x4F,0x05,0x00}, {0x38,0x19,0x00},
127 {0x1D,0x31,0x00}, {0x00,0x3D,0x00}, {0x00,0x41,0x00}, {0x00,0x3B,0x17},
128 {0x00,0x2E,0x55}, {0x00,0x00,0x00}, {0x00,0x00,0x00}, {0x00,0x00,0x00},
129 {0xAF,0xAF,0xAF}, {0x19,0x4E,0xC8}, {0x47,0x2F,0xE3}, {0x6B,0x1F,0xD7},
130 {0x93,0x1B,0xAE}, {0x9E,0x1A,0x5E}, {0x99,0x32,0x00}, {0x7B,0x4B,0x00},
131 {0x5B,0x67,0x00}, {0x26,0x7A,0x00}, {0x00,0x82,0x00}, {0x00,0x7A,0x3E},
132 {0x00,0x6E,0x8A}, {0x00,0x00,0x00}, {0x00,0x00,0x00}, {0x00,0x00,0x00},
133 {0xFF,0xFF,0xFF}, {0x64,0xA9,0xFF}, {0x8E,0x89,0xFF}, {0xB6,0x76,0xFF},
134 {0xE0,0x6F,0xFF}, {0xEF,0x6C,0xC4}, {0xF0,0x80,0x6A}, {0xD8,0x98,0x2C},
135 {0xB9,0xB4,0x0A}, {0x83,0xCB,0x0C}, {0x5B,0xD6,0x3F}, {0x4A,0xD1,0x7E},
136 {0x4D,0xC7,0xCB}, {0x4C,0x4C,0x4C}, {0x00,0x00,0x00}, {0x00,0x00,0x00},
137 {0xFF,0xFF,0xFF}, {0xC7,0xE5,0xFF}, {0xD9,0xD9,0xFF}, {0xE9,0xD1,0xFF},
138 {0xF9,0xCE,0xFF}, {0xFF,0xCC,0xF1}, {0xFF,0xD4,0xCB}, {0xF8,0xDF,0xB1},
139 {0xED,0xEA,0xA4}, {0xD6,0xF4,0xA4}, {0xC5,0xF8,0xB8}, {0xBE,0xF6,0xD3},
140 {0xBF,0xF1,0xF1}, {0xB9,0xB9,0xB9}, {0x00,0x00,0x00}, {0x00,0x00,0x00}
141 };
142
143 static const byte pvm_style_d93_fbx_palette[64][3] =
144 {
145 {0x69,0x6B,0x63}, {0x00,0x17,0x74}, {0x1E,0x00,0x87}, {0x34,0x00,0x73},
146 {0x56,0x00,0x57}, {0x5E,0x00,0x13}, {0x53,0x1A,0x00}, {0x3B,0x24,0x00},
147 {0x24,0x30,0x00}, {0x06,0x3A,0x00}, {0x00,0x3F,0x00}, {0x00,0x3B,0x1E},
148 {0x00,0x33,0x4E}, {0x00,0x00,0x00}, {0x00,0x00,0x00}, {0x00,0x00,0x00},
149 {0xB9,0xBB,0xB3}, {0x14,0x53,0xB9}, {0x4D,0x2C,0xDA}, {0x67,0x1E,0xDE},
150 {0x98,0x18,0x9C}, {0x9D,0x23,0x44}, {0xA0,0x3E,0x00}, {0x8D,0x55,0x00},
151 {0x65,0x6D,0x00}, {0x2C,0x79,0x00}, {0x00,0x81,0x00}, {0x00,0x7D,0x42},
152 {0x00,0x78,0x8A}, {0x00,0x00,0x00}, {0x00,0x00,0x00}, {0x00,0x00,0x00},
153 {0xFF,0xFF,0xFF}, {0x69,0xA8,0xFF}, {0x96,0x91,0xFF}, {0xB2,0x8A,0xFA},
154 {0xEA,0x7D,0xFA}, {0xF3,0x7B,0xC7}, {0xF2,0x8E,0x59}, {0xE6,0xAD,0x27},
155 {0xD7,0xC8,0x05}, {0x90,0xDF,0x07}, {0x64,0xE5,0x3C}, {0x45,0xE2,0x7D},
156 {0x48,0xD5,0xD9}, {0x4E,0x50,0x48}, {0x00,0x00,0x00}, {0x00,0x00,0x00},
157 {0xFF,0xFF,0xFF}, {0xD2,0xEA,0xFF}, {0xE2,0xE2,0xFF}, {0xE9,0xD8,0xFF},
158 {0xF5,0xD2,0xFF}, {0xF8,0xD9,0xEA}, {0xFA,0xDE,0xB9}, {0xF9,0xE8,0x9B},
159 {0xF3,0xF2,0x8C}, {0xD3,0xFA,0x91}, {0xB8,0xFC,0xA8}, {0xAE,0xFA,0xCA},
160 {0xCA,0xF3,0xF3}, {0xBE,0xC0,0xB8}, {0x00,0x00,0x00}, {0x00,0x00,0x00}
161 };
162
163 static const byte ntsc_hardware_fbx_palette[64][3] =
164 {
165 {0x6A,0x6D,0x6A}, {0x00,0x13,0x80}, {0x1E,0x00,0x8A}, {0x39,0x00,0x7A},
166 {0x55,0x00,0x56}, {0x5A,0x00,0x18}, {0x4F,0x10,0x00}, {0x38,0x21,0x00},
167 {0x21,0x33,0x00}, {0x00,0x3D,0x00}, {0x00,0x40,0x00}, {0x00,0x39,0x24},
168 {0x00,0x2E,0x55}, {0x00,0x00,0x00}, {0x00,0x00,0x00}, {0x00,0x00,0x00},
169 {0xB9,0xBC,0xB9}, {0x18,0x50,0xC7}, {0x4B,0x30,0xE3}, {0x73,0x22,0xD6},
170 {0x95,0x1F,0xA9}, {0x9D,0x28,0x5C}, {0x96,0x3C,0x00}, {0x7A,0x51,0x00},
171 {0x5B,0x67,0x00}, {0x22,0x77,0x00}, {0x02,0x7E,0x02}, {0x00,0x76,0x45},
172 {0x00,0x6E,0x8A}, {0x00,0x00,0x00}, {0x00,0x00,0x00}, {0x00,0x00,0x00},
173 {0xFF,0xFF,0xFF}, {0x68,0xA6,0xFF}, {0x92,0x99,0xFF}, {0xB0,0x85,0xFF},
174 {0xD9,0x75,0xFD}, {0xE3,0x77,0xB9}, {0xE5,0x8D,0x68}, {0xCF,0xA2,0x2C},
175 {0xB3,0xAF,0x0C}, {0x7B,0xC2,0x11}, {0x55,0xCA,0x47}, {0x46,0xCB,0x81},
176 {0x47,0xC1,0xC5}, {0x4A,0x4D,0x4A}, {0x00,0x00,0x00}, {0x00,0x00,0x00},
177 {0xFF,0xFF,0xFF}, {0xCC,0xEA,0xFF}, {0xDD,0xDE,0xFF}, {0xEC,0xDA,0xFF},
178 {0xF8,0xD7,0xFE}, {0xFC,0xD6,0xF5}, {0xFD,0xDB,0xCF}, {0xF9,0xE7,0xB5},
179 {0xF1,0xF0,0xAA}, {0xDA,0xFA,0xA9}, {0xC9,0xFF,0xBC}, {0xC3,0xFB,0xD7},
180 {0xC4,0xF6,0xF6}, {0xBE,0xC1,0xBE}, {0x00,0x00,0x00}, {0x00,0x00,0x00}
181 };
182
183 static const byte nes_classic_fbx_fs_palette[64][3] =
184 {
185 {0x60,0x61,0x5F}, {0x00,0x00,0x83}, {0x1D,0x01,0x95}, {0x34,0x08,0x75},
186 {0x51,0x05,0x5E}, {0x56,0x00,0x0F}, {0x4C,0x07,0x00}, {0x37,0x23,0x08},
187 {0x20,0x3A,0x0B}, {0x0F,0x4B,0x0E}, {0x19,0x4C,0x16}, {0x02,0x42,0x1E},
188 {0x02,0x31,0x54}, {0x00,0x00,0x00}, {0x00,0x00,0x00}, {0x00,0x00,0x00},
189 {0xA9,0xAA,0xA8}, {0x10,0x4B,0xBF}, {0x47,0x12,0xD8}, {0x63,0x00,0xCA},
190 {0x88,0x00,0xA9}, {0x93,0x0B,0x46}, {0x8A,0x2D,0x04}, {0x6F,0x52,0x06},
191 {0x5C,0x71,0x14}, {0x1B,0x8D,0x12}, {0x19,0x95,0x09}, {0x17,0x84,0x48},
192 {0x20,0x6B,0x8E}, {0x00,0x00,0x00}, {0x00,0x00,0x00}, {0x00,0x00,0x00},
193 {0xFB,0xFB,0xFB}, {0x66,0x99,0xF8}, {0x89,0x74,0xF9}, {0xAB,0x58,0xF8},
194 {0xD5,0x57,0xEF}, {0xDE,0x5F,0xA9}, {0xDC,0x7F,0x59}, {0xC7,0xA2,0x24},
195 {0xA7,0xBE,0x03}, {0x75,0xD7,0x03}, {0x60,0xE3,0x4F}, {0x3C,0xD6,0x8D},
196 {0x56,0xC9,0xCC}, {0x41,0x42,0x40}, {0x00,0x00,0x00}, {0x00,0x00,0x00},
197 {0xFB,0xFB,0xFB}, {0xBE,0xD4,0xFA}, {0xC9,0xC7,0xF9}, {0xD7,0xBE,0xFA},
198 {0xE8,0xB8,0xF9}, {0xF5,0xBA,0xE5}, {0xF3,0xCA,0xC2}, {0xDF,0xCD,0xA7},
199 {0xD9,0xE0,0x9C}, {0xC9,0xEB,0x9E}, {0xC0,0xED,0xB8}, {0xB5,0xF4,0xC7},
200 {0xB9,0xEA,0xE9}, {0xAB,0xAB,0xAB}, {0x00,0x00,0x00}, {0x00,0x00,0x00}
201 };
202
203 int crossx = 0;
204 int crossy = 0;
205
206 #define CROSSHAIR_SIZE 3
207
draw_crosshair(int x,int y)208 void draw_crosshair(int x, int y)
209 {
210 uint32_t w = 0xFFFFFFFF;
211 uint32_t b = 0x00000000;
212 int current_width = 256;
213
214 if (blargg_ntsc){
215 x *= 2.36;
216 current_width = 602;
217 }
218
219 for (int i = MAX(-CROSSHAIR_SIZE, -x); i <= MIN(CROSSHAIR_SIZE, current_width - x); i++) {
220 video_buffer[current_width * y + x + i] = i % 2 == 0 ? w : b;
221 }
222
223 for (int i = MAX(-CROSSHAIR_SIZE, -y); i <= MIN(CROSSHAIR_SIZE, 239 - y); i++) {
224 video_buffer[current_width * (y + i) + x] = i % 2 == 0 ? w : b;
225 }
226 }
227
load_wav(const char * sampgame,Api::User::File & file)228 static void load_wav(const char* sampgame, Api::User::File& file)
229 {
230 char samp_path[292];
231 int length = 0;
232 int blockalign = 0;
233 int numchannels = 0;
234 int bitspersample = 0;
235 char fmt[4] = { 0x66, 0x6d, 0x74, 0x20};
236 char subchunk2id[4] = { 0x64, 0x61, 0x74, 0x61};
237 char *wavfile;
238 char *dataptr;
239
240 sprintf(samp_path, "%s%c%s%c%02d.wav", samp_dir, slash, sampgame, slash, file.GetId());
241
242 std::ifstream samp_file(samp_path, std::ifstream::in|std::ifstream::binary);
243
244 if (samp_file) {
245 samp_file.seekg(0, samp_file.end);
246 length = samp_file.tellg();
247 samp_file.seekg(0, samp_file.beg);
248 wavfile = (char*)malloc(length * sizeof(char));
249 samp_file.read(wavfile, length);
250
251 // Check to see if it has a valid header
252 if (memcmp(&wavfile[0x00], "RIFF", 4) != 0) { return; }
253 if (memcmp(&wavfile[0x08], "WAVE", 4) != 0) { return; }
254 if (memcmp(&wavfile[0x0c], &fmt, 4) != 0) { return; }
255 if (memcmp(&wavfile[0x24], &subchunk2id, 4) != 0) { return; }
256
257 // Load the sample into the emulator
258 dataptr = &wavfile[0x2c];
259 blockalign = wavfile[0x21] << 8 | wavfile[0x20];
260 numchannels = wavfile[0x17] << 8 | wavfile[0x16];
261 bitspersample = wavfile[0x23] << 8 | wavfile[0x22];
262 file.SetSampleContent(dataptr, (length - 44) / blockalign, 0, bitspersample, 44100);
263 free(wavfile);
264 }
265 }
266
file_io_callback(void *,Api::User::File & file)267 static void NST_CALLBACK file_io_callback(void*, Api::User::File &file)
268 {
269 const void *addr;
270 unsigned long addr_size;
271
272 #ifdef _WIN32
273 slash = '\\';
274 #else
275 slash = '/';
276 #endif
277
278 switch (file.GetAction())
279 {
280 case Api::User::File::LOAD_SAMPLE_MOERO_PRO_YAKYUU:
281 load_wav("moepro", file); break;
282 case Api::User::File::LOAD_SAMPLE_MOERO_PRO_YAKYUU_88:
283 load_wav("moepro88", file); break;
284 case Api::User::File::LOAD_SAMPLE_MOERO_PRO_TENNIS:
285 load_wav("mptennis", file); break;
286 case Api::User::File::LOAD_SAMPLE_TERAO_NO_DOSUKOI_OOZUMOU:
287 load_wav("terao", file); break;
288 case Api::User::File::LOAD_SAMPLE_AEROBICS_STUDIO:
289 load_wav("ftaerobi", file); break;
290
291 case Api::User::File::LOAD_BATTERY:
292 case Api::User::File::LOAD_EEPROM:
293 case Api::User::File::LOAD_TAPE:
294 case Api::User::File::LOAD_TURBOFILE:
295 file.GetRawStorage(sram, sram_size);
296 break;
297
298 case Api::User::File::SAVE_BATTERY:
299 case Api::User::File::SAVE_EEPROM:
300 case Api::User::File::SAVE_TAPE:
301 case Api::User::File::SAVE_TURBOFILE:
302 file.GetContent(addr, addr_size);
303 if (addr != sram || sram_size != addr_size)
304 if (log_cb)
305 log_cb(RETRO_LOG_INFO, "[Nestopia]: SRAM changed place in RAM!\n");
306 break;
307 case Api::User::File::LOAD_FDS:
308 {
309 char base[256];
310 sprintf(base, "%s%c%s.sav", g_save_dir, slash, g_basename);
311 if (log_cb)
312 log_cb(RETRO_LOG_INFO, "Want to load FDS sav from: %s\n", base);
313 std::ifstream in_tmp(base,std::ifstream::in|std::ifstream::binary);
314
315 if (!in_tmp.is_open())
316 return;
317
318 file.SetPatchContent(in_tmp);
319 }
320 break;
321 case Api::User::File::SAVE_FDS:
322 {
323 char base[256];
324 sprintf(base, "%s%c%s.sav", g_save_dir, slash, g_basename);
325 if (log_cb)
326 log_cb(RETRO_LOG_INFO, "Want to save FDS sav to: %s\n", base);
327 std::ofstream out_tmp(base,std::ifstream::out|std::ifstream::binary);
328
329 if (out_tmp.is_open())
330 file.GetPatchContent(Api::User::File::PATCH_UPS, out_tmp);
331 }
332 break;
333 default:
334 break;
335 }
336 }
337
check_system_specs(void)338 static void check_system_specs(void)
339 {
340 unsigned level = 6;
341 environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
342 }
343
retro_init(void)344 void retro_init(void)
345 {
346 struct retro_log_callback log;
347
348 if (environ_cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &log))
349 log_cb = log.log;
350 else
351 log_cb = NULL;
352
353 if (environ_cb(RETRO_ENVIRONMENT_GET_INPUT_BITMASKS, NULL))
354 libretro_supports_bitmasks = true;
355
356 check_system_specs();
357 }
358
retro_deinit(void)359 void retro_deinit(void)
360 {
361 libretro_supports_bitmasks = false;
362 }
363
retro_api_version(void)364 unsigned retro_api_version(void)
365 {
366 return RETRO_API_VERSION;
367 }
368
retro_set_controller_port_device(unsigned port,unsigned device)369 void retro_set_controller_port_device(unsigned port, unsigned device)
370 {
371 if (port >= 4)
372 return;
373
374 input_type[port] = device;
375 }
376
retro_get_system_info(struct retro_system_info * info)377 void retro_get_system_info(struct retro_system_info *info)
378 {
379 memset(info, 0, sizeof(*info));
380 info->library_name = "Nestopia";
381 #ifdef GIT_VERSION
382 info->library_version = NST_VERSION GIT_VERSION;
383 #else
384 info->library_version = NST_VERSION;
385 #endif
386 info->need_fullpath = false;
387 info->valid_extensions = "nes|fds|unf|unif";
388 }
389
get_aspect_ratio(void)390 double get_aspect_ratio(void)
391 {
392 double aspect_ratio = is_pal ? NES_PAL_PAR : NES_NTSC_PAR;
393
394 if (aspect_ratio_mode == 1)
395 aspect_ratio = NES_NTSC_PAR;
396 else if (aspect_ratio_mode == 2)
397 aspect_ratio = NES_PAL_PAR;
398 else if (aspect_ratio_mode == 3)
399 aspect_ratio = NES_4_3_DAR;
400
401 return aspect_ratio;
402 }
403
retro_get_system_av_info(struct retro_system_av_info * info)404 void retro_get_system_av_info(struct retro_system_av_info *info)
405 {
406 const retro_system_timing timing = { is_pal ? 50.0 : 60.0, SAMPLERATE };
407 info->timing = timing;
408
409 // It's better if the size is based on NTSC_WIDTH if the filter is on
410 const retro_game_geometry geom = {
411 Api::Video::Output::WIDTH - (overscan_h ? 16 : 0),
412 Api::Video::Output::HEIGHT - (overscan_v ? 16 : 0),
413 Api::Video::Output::NTSC_WIDTH,
414 Api::Video::Output::HEIGHT,
415 get_aspect_ratio(),
416 };
417 info->geometry = geom;
418 }
419
420
retro_set_environment(retro_environment_t cb)421 void retro_set_environment(retro_environment_t cb)
422 {
423 environ_cb = cb;
424 libretro_set_core_options(environ_cb);
425 }
426
retro_set_audio_sample(retro_audio_sample_t cb)427 void retro_set_audio_sample(retro_audio_sample_t cb)
428 {
429 audio_cb = cb;
430 }
431
retro_set_audio_sample_batch(retro_audio_sample_batch_t cb)432 void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb)
433 {
434 audio_batch_cb = cb;
435 }
436
retro_set_input_poll(retro_input_poll_t cb)437 void retro_set_input_poll(retro_input_poll_t cb)
438 {
439 input_poll_cb = cb;
440 }
441
retro_set_input_state(retro_input_state_t cb)442 void retro_set_input_state(retro_input_state_t cb)
443 {
444 input_state_cb = cb;
445 }
446
retro_set_video_refresh(retro_video_refresh_t cb)447 void retro_set_video_refresh(retro_video_refresh_t cb)
448 {
449 video_cb = cb;
450 }
451
retro_reset(void)452 void retro_reset(void)
453 {
454 machine->Reset(false);
455
456 if (machine->Is(Nes::Api::Machine::DISK))
457 {
458 fds->EjectDisk();
459 if (fds_auto_insert)
460 fds->InsertDisk(0, 0);
461 }
462 }
463
464 typedef struct
465 {
466 unsigned retro;
467 unsigned nes;
468 } keymap;
469
470 static enum {
471 ZAPPER_DEVICE_LIGHTGUN,
472 ZAPPER_DEVICE_MOUSE,
473 ZAPPER_DEVICE_POINTER
474 } zapper_device;
475
476 static keymap bindmap_default[] = {
477 { RETRO_DEVICE_ID_JOYPAD_A, Core::Input::Controllers::Pad::A },
478 { RETRO_DEVICE_ID_JOYPAD_B, Core::Input::Controllers::Pad::B },
479 { RETRO_DEVICE_ID_JOYPAD_X, Core::Input::Controllers::Pad::A },
480 { RETRO_DEVICE_ID_JOYPAD_Y, Core::Input::Controllers::Pad::B },
481 { RETRO_DEVICE_ID_JOYPAD_SELECT, Core::Input::Controllers::Pad::SELECT },
482 { RETRO_DEVICE_ID_JOYPAD_START, Core::Input::Controllers::Pad::START },
483 { RETRO_DEVICE_ID_JOYPAD_UP, Core::Input::Controllers::Pad::UP },
484 { RETRO_DEVICE_ID_JOYPAD_DOWN, Core::Input::Controllers::Pad::DOWN },
485 { RETRO_DEVICE_ID_JOYPAD_LEFT, Core::Input::Controllers::Pad::LEFT },
486 { RETRO_DEVICE_ID_JOYPAD_RIGHT, Core::Input::Controllers::Pad::RIGHT },
487 };
488
489 static keymap bindmap_shifted[] = {
490 { RETRO_DEVICE_ID_JOYPAD_B, Core::Input::Controllers::Pad::A },
491 { RETRO_DEVICE_ID_JOYPAD_Y, Core::Input::Controllers::Pad::B },
492 { RETRO_DEVICE_ID_JOYPAD_A, Core::Input::Controllers::Pad::A },
493 { RETRO_DEVICE_ID_JOYPAD_X, Core::Input::Controllers::Pad::B },
494 { RETRO_DEVICE_ID_JOYPAD_SELECT, Core::Input::Controllers::Pad::SELECT },
495 { RETRO_DEVICE_ID_JOYPAD_START, Core::Input::Controllers::Pad::START },
496 { RETRO_DEVICE_ID_JOYPAD_UP, Core::Input::Controllers::Pad::UP },
497 { RETRO_DEVICE_ID_JOYPAD_DOWN, Core::Input::Controllers::Pad::DOWN },
498 { RETRO_DEVICE_ID_JOYPAD_LEFT, Core::Input::Controllers::Pad::LEFT },
499 { RETRO_DEVICE_ID_JOYPAD_RIGHT, Core::Input::Controllers::Pad::RIGHT },
500 };
501
502 static keymap *bindmap = bindmap_default;
503
update_input(bool supports_bitmasks)504 static void update_input(bool supports_bitmasks)
505 {
506 input_poll_cb();
507 input->pad[0].buttons = 0;
508 input->pad[1].buttons = 0;
509 input->pad[2].buttons = 0;
510 input->pad[3].buttons = 0;
511 input->pad[1].mic = 0;
512 input->zapper.fire = 0;
513 input->vsSystem.insertCoin = 0;
514
515 if (Api::Input(emulator).GetConnectedController(1) == 5)
516 {
517 static int zapx = overscan_h ? 8 : 0;
518 static int zapy = overscan_v ? 8 : 0;
519 int min_x = overscan_h ? 8 : 0;
520 int max_x = overscan_h ? 247 : 255;
521 int min_y = overscan_v ? 8 : 0;
522 int max_y = overscan_v ? 231 : 239;
523
524 switch (zapper_device)
525 {
526 case ZAPPER_DEVICE_LIGHTGUN:
527 if (!input_state_cb(1, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_IS_OFFSCREEN))
528 {
529 zapx = input_state_cb(1, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_SCREEN_X);
530 zapy = input_state_cb(1, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_SCREEN_Y);
531
532 zapx = (zapx + 0x7FFF) * max_x / (0x7FFF * 2);
533 zapy = (zapy + 0x7FFF) * max_y / (0x7FFF * 2);
534 }
535 else
536 {
537 zapx = min_x;
538 zapy = min_y;
539 }
540
541 if (input_state_cb(1, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_TRIGGER)) {
542 input->zapper.x = zapx;
543 input->zapper.y = zapy;
544 input->zapper.fire = 1;
545 }
546
547 if (input_state_cb(1, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_RELOAD)) {
548 input->zapper.x = ~1U;
549 input->zapper.fire = 1;
550 }
551 break;
552 case ZAPPER_DEVICE_MOUSE:
553 zapx += input_state_cb(1, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_X);
554 zapy += input_state_cb(1, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_Y);
555
556 if (zapx < min_x)
557 zapx = min_x;
558 else if (zapx > max_x)
559 zapx = max_x;
560
561 if (zapy < min_y)
562 zapy = min_y;
563 else if (zapy > max_y)
564 zapy = max_y;
565
566 if (input_state_cb(1, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_LEFT))
567 {
568 input->zapper.x = zapx;
569 input->zapper.y = zapy;
570 input->zapper.fire = 1;
571 }
572 break;
573 case ZAPPER_DEVICE_POINTER:
574 zapx = input_state_cb(1, RETRO_DEVICE_POINTER, 0, RETRO_DEVICE_ID_POINTER_X);
575 zapy = input_state_cb(1, RETRO_DEVICE_POINTER, 0, RETRO_DEVICE_ID_POINTER_Y);
576
577 zapx = (zapx + 0x7FFF) * max_x / (0x7FFF * 2);
578 zapy = (zapy + 0x7FFF) * max_y / (0x7FFF * 2);
579
580 if (input_state_cb(1, RETRO_DEVICE_POINTER, 0, RETRO_DEVICE_ID_POINTER_PRESSED))
581 {
582 input->zapper.x = zapx;
583 input->zapper.y = zapy;
584 input->zapper.fire = 1;
585 }
586 break;
587 default:
588 break;
589 }
590
591 if (zapx > max_x) { crossx = max_x; }
592 else if (zapx < min_x) { crossx = min_x; }
593 else {crossx = zapx; }
594
595 if (zapy > max_y) { crossy = max_y; }
596 else if (zapy < min_y) { crossy = min_y; }
597 else {crossy = zapy; }
598 }
599
600 static unsigned tstate = 2;
601 bool pressed_l3 = false;
602 bool pressed_l2 = false;
603 bool pressed_r2 = false;
604 bool pressed_l = false;
605 bool pressed_r = false;
606
607 if (supports_bitmasks)
608 {
609 int16_t ret[4];
610 /* Player 0 needs some extra checks */
611 ret[0] = input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_MASK);
612 ret[1] = input_state_cb(1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_MASK);
613 ret[2] = input_state_cb(2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_MASK);
614 ret[3] = input_state_cb(3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_MASK);
615 pressed_l3 = ret[0] & (1 << RETRO_DEVICE_ID_JOYPAD_L3);
616 pressed_l2 = ret[0] & (1 << RETRO_DEVICE_ID_JOYPAD_L2);
617 pressed_r2 = ret[0] & (1 << RETRO_DEVICE_ID_JOYPAD_R2);
618 pressed_l = ret[0] & (1 << RETRO_DEVICE_ID_JOYPAD_L );
619 pressed_r = ret[0] & (1 << RETRO_DEVICE_ID_JOYPAD_R );
620
621 for (unsigned p = 0; p < 4; p++)
622 {
623 for (unsigned bind = 0; bind < sizeof(bindmap_default) / sizeof(bindmap[0]); bind++)
624 input->pad[p].buttons |= (ret[p] & (1 << bindmap[bind].retro)) ? bindmap[bind].nes : 0;
625 if (ret[p] & (1 << bindmap[2].retro))
626 tstate ? input->pad[p].buttons &= ~Core::Input::Controllers::Pad::A : input->pad[p].buttons |= Core::Input::Controllers::Pad::A;
627 if (ret[p] & (1 << bindmap[3].retro))
628 tstate ? input->pad[p].buttons &= ~Core::Input::Controllers::Pad::B : input->pad[p].buttons |= Core::Input::Controllers::Pad::B;
629 }
630 }
631 else
632 {
633 pressed_l3 = input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3);
634 pressed_l2 = input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2);
635 pressed_r2 = input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2);
636 pressed_l = input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L);
637 pressed_r = input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R);
638
639 for (unsigned p = 0; p < 4; p++)
640 {
641 for (unsigned bind = 0; bind < sizeof(bindmap_default) / sizeof(bindmap[0]); bind++)
642 input->pad[p].buttons |= input_state_cb(p, RETRO_DEVICE_JOYPAD, 0, bindmap[bind].retro) ? bindmap[bind].nes : 0;
643 if (input_state_cb(p, RETRO_DEVICE_JOYPAD, 0, bindmap[2].retro))
644 tstate ? input->pad[p].buttons &= ~Core::Input::Controllers::Pad::A : input->pad[p].buttons |= Core::Input::Controllers::Pad::A;
645 if (input_state_cb(p, RETRO_DEVICE_JOYPAD, 0, bindmap[3].retro))
646 tstate ? input->pad[p].buttons &= ~Core::Input::Controllers::Pad::B : input->pad[p].buttons |= Core::Input::Controllers::Pad::B;
647 }
648 }
649
650 if (tstate) tstate--; else tstate = tpulse;
651
652 if (pressed_l3)
653 input->pad[1].mic |= 0x04;
654
655 if (pressed_l2)
656 input->vsSystem.insertCoin |= Core::Input::Controllers::VsSystem::COIN_1;
657
658 if (pressed_r2)
659 input->vsSystem.insertCoin |= Core::Input::Controllers::VsSystem::COIN_2;
660
661 if (machine->Is(Nes::Api::Machine::DISK))
662 {
663 bool curL = pressed_l;
664 static bool prevL = false;
665
666 if (curL && !prevL)
667 {
668 if (!fds->IsAnyDiskInserted())
669 fds->InsertDisk(0, 0);
670 else if (fds->CanChangeDiskSide())
671 fds->ChangeSide();
672 }
673 prevL = curL;
674
675 bool curR = pressed_r;
676 static bool prevR = false;
677
678 if (curR && !prevR && (fds->GetNumDisks() > 1))
679 {
680 int currdisk = fds->GetCurrentDisk();
681 fds->EjectDisk();
682 fds->InsertDisk(!currdisk, 0);
683 }
684 prevR = curR;
685 }
686 }
687
check_variables(void)688 static void check_variables(void)
689 {
690 static bool last_ntsc_val_same;
691 struct retro_variable var = {0};
692 struct retro_system_av_info av_info;
693
694 Api::Sound sound(emulator);
695 Api::Video video(emulator);
696 Api::Video::RenderState renderState;
697 Api::Machine machine(emulator);
698 Api::Video::RenderState::Filter filter;
699
700 var.key = "nestopia_zapper_device";
701 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
702 {
703 if (strcmp(var.value, "lightgun") == 0)
704 zapper_device = ZAPPER_DEVICE_LIGHTGUN;
705 if (strcmp(var.value, "mouse") == 0)
706 zapper_device = ZAPPER_DEVICE_MOUSE;
707 else if (strcmp(var.value, "pointer") == 0)
708 zapper_device = ZAPPER_DEVICE_POINTER;
709 }
710
711 var.key = "nestopia_button_shift";
712
713 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
714 {
715 if (strcmp(var.value, "disabled") == 0)
716 bindmap = bindmap_default;
717 else if (strcmp(var.value, "enabled") == 0)
718 bindmap = bindmap_shifted;
719 }
720
721 var.key = "nestopia_favored_system";
722 is_pal = false;
723
724 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
725 {
726 if (strcmp(var.value, "auto") == 0)
727 {
728 machine.SetMode(machine.GetDesiredMode());
729 if (machine.GetMode() == Api::Machine::PAL)
730 {
731 is_pal = true;
732 favsystem = Api::Machine::FAVORED_NES_PAL;
733 machine.SetMode(Api::Machine::PAL);
734 }
735 else
736 {
737 favsystem = Api::Machine::FAVORED_NES_NTSC;
738 machine.SetMode(Api::Machine::NTSC);
739 }
740 }
741 else if (strcmp(var.value, "ntsc") == 0)
742 {
743 favsystem = Api::Machine::FAVORED_NES_NTSC;
744 machine.SetMode(Api::Machine::NTSC);
745 }
746 else if (strcmp(var.value, "pal") == 0)
747 {
748 favsystem = Api::Machine::FAVORED_NES_PAL;
749 machine.SetMode(Api::Machine::PAL);
750 is_pal = true;
751 }
752 else if (strcmp(var.value, "famicom") == 0)
753 {
754 favsystem = Api::Machine::FAVORED_FAMICOM;
755 machine.SetMode(Api::Machine::NTSC);
756 }
757 else if (strcmp(var.value, "dendy") == 0)
758 {
759 favsystem = Api::Machine::FAVORED_DENDY;
760 machine.SetMode(Api::Machine::PAL);
761 is_pal = true;
762 }
763 else
764 {
765 favsystem = Api::Machine::FAVORED_NES_NTSC;
766 machine.SetMode(Api::Machine::NTSC);
767 }
768 }
769 if (audio) delete audio;
770 audio = new Api::Sound::Output(audio_buffer, is_pal ? SAMPLERATE / 50 : SAMPLERATE / 60);
771
772 var.key = "nestopia_genie_distortion";
773
774 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
775 {
776 if (strcmp(var.value, "disabled") == 0)
777 sound.SetGenie(0);
778 else if (strcmp(var.value, "enabled") == 0)
779 sound.SetGenie(1);
780 }
781
782 var.key = "nestopia_ram_power_state";
783
784 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
785 {
786 if (strcmp(var.value, "0x00") == 0)
787 machine.SetRamPowerState(0);
788 else if (strcmp(var.value, "0xFF") == 0)
789 machine.SetRamPowerState(1);
790 else if (strcmp(var.value, "random") == 0)
791 machine.SetRamPowerState(2);
792 }
793
794 var.key = "nestopia_nospritelimit";
795
796 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
797 {
798 if (strcmp(var.value, "disabled") == 0)
799 video.EnableUnlimSprites(false);
800 else if (strcmp(var.value, "enabled") == 0)
801 video.EnableUnlimSprites(true);
802 }
803
804 var.key = "nestopia_overclock";
805
806 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
807 {
808 if (strcmp(var.value, "1x") == 0)
809 video.EnableOverclocking(false);
810 else if (strcmp(var.value, "2x") == 0)
811 video.EnableOverclocking(true);
812 }
813
814 var.key = "nestopia_fds_auto_insert";
815
816 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
817 fds_auto_insert = (strcmp(var.value, "enabled") == 0);
818
819 var.key = "nestopia_blargg_ntsc_filter";
820
821 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
822 {
823 if (strcmp(var.value, "disabled") == 0)
824 blargg_ntsc = 0;
825 else if (strcmp(var.value, "composite") == 0)
826 blargg_ntsc = 2;
827 else if (strcmp(var.value, "svideo") == 0)
828 blargg_ntsc = 3;
829 else if (strcmp(var.value, "rgb") == 0)
830 blargg_ntsc = 4;
831 else if (strcmp(var.value, "monochrome") == 0)
832 blargg_ntsc = 5;
833 }
834
835 switch(blargg_ntsc)
836 {
837 case 0:
838 filter = Api::Video::RenderState::FILTER_NONE;
839 video_width = Api::Video::Output::WIDTH;
840 video.SetSaturation(Api::Video::DEFAULT_SATURATION);
841 break;
842 case 2:
843 filter = Api::Video::RenderState::FILTER_NTSC;
844 video.SetSharpness(Api::Video::DEFAULT_SHARPNESS_COMP);
845 video.SetColorResolution(Api::Video::DEFAULT_COLOR_RESOLUTION_COMP);
846 video.SetColorBleed(Api::Video::DEFAULT_COLOR_BLEED_COMP);
847 video.SetColorArtifacts(Api::Video::DEFAULT_COLOR_ARTIFACTS_COMP);
848 video.SetColorFringing(Api::Video::DEFAULT_COLOR_FRINGING_COMP);
849 video.SetSaturation(Api::Video::DEFAULT_SATURATION_COMP);
850 video_width = Api::Video::Output::NTSC_WIDTH;
851 break;
852 case 3:
853 filter = Api::Video::RenderState::FILTER_NTSC;
854 video.SetSharpness(Api::Video::DEFAULT_SHARPNESS_SVIDEO);
855 video.SetColorResolution(Api::Video::DEFAULT_COLOR_RESOLUTION_SVIDEO);
856 video.SetColorBleed(Api::Video::DEFAULT_COLOR_BLEED_SVIDEO);
857 video.SetColorArtifacts(Api::Video::DEFAULT_COLOR_ARTIFACTS_SVIDEO);
858 video.SetColorFringing(Api::Video::DEFAULT_COLOR_FRINGING_SVIDEO);
859 video.SetSaturation(Api::Video::DEFAULT_SATURATION_SVIDEO);
860 video_width = Api::Video::Output::NTSC_WIDTH;
861 break;
862 case 4:
863 filter = Api::Video::RenderState::FILTER_NTSC;
864 video.SetSharpness(Api::Video::DEFAULT_SHARPNESS_RGB);
865 video.SetColorResolution(Api::Video::DEFAULT_COLOR_RESOLUTION_RGB);
866 video.SetColorBleed(Api::Video::DEFAULT_COLOR_BLEED_RGB);
867 video.SetColorArtifacts(Api::Video::DEFAULT_COLOR_ARTIFACTS_RGB);
868 video.SetColorFringing(Api::Video::DEFAULT_COLOR_FRINGING_RGB);
869 video.SetSaturation(Api::Video::DEFAULT_SATURATION_RGB);
870 video_width = Api::Video::Output::NTSC_WIDTH;
871 break;
872 case 5:
873 filter = Api::Video::RenderState::FILTER_NTSC;
874 video.SetSharpness(Api::Video::DEFAULT_SHARPNESS_MONO);
875 video.SetColorResolution(Api::Video::DEFAULT_COLOR_RESOLUTION_MONO);
876 video.SetColorBleed(Api::Video::DEFAULT_COLOR_BLEED_MONO);
877 video.SetColorArtifacts(Api::Video::DEFAULT_COLOR_ARTIFACTS_MONO);
878 video.SetColorFringing(Api::Video::DEFAULT_COLOR_FRINGING_MONO);
879 video.SetSaturation(Api::Video::DEFAULT_SATURATION_MONO);
880 video_width = Api::Video::Output::NTSC_WIDTH;
881 break;
882 }
883
884 var.key = "nestopia_palette";
885
886 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
887 {
888 if (strcmp(var.value, "consumer") == 0) {
889 video.GetPalette().SetMode(Api::Video::Palette::MODE_YUV);
890 video.SetDecoder(Api::Video::DECODER_CONSUMER);
891 }
892 else if (strcmp(var.value, "canonical") == 0) {
893 video.GetPalette().SetMode(Api::Video::Palette::MODE_YUV);
894 video.SetDecoder(Api::Video::DECODER_CANONICAL);
895 }
896 else if (strcmp(var.value, "alternative") == 0) {
897 video.GetPalette().SetMode(Api::Video::Palette::MODE_YUV);
898 video.SetDecoder(Api::Video::DECODER_ALTERNATIVE);
899 }
900 else if (strcmp(var.value, "rgb") == 0) {
901 video.GetPalette().SetMode(Api::Video::Palette::MODE_RGB);
902 }
903 else if (strcmp(var.value, "cxa2025as") == 0) {
904 video.GetPalette().SetMode(Api::Video::Palette::MODE_CUSTOM);
905 video.GetPalette().SetCustom(cxa2025as_palette, Api::Video::Palette::STD_PALETTE);
906 }
907 else if (strcmp(var.value, "pal") == 0) {
908 video.GetPalette().SetMode(Api::Video::Palette::MODE_CUSTOM);
909 video.GetPalette().SetCustom(pal_palette, Api::Video::Palette::STD_PALETTE);
910 }
911 else if (strcmp(var.value, "composite-direct-fbx") == 0) {
912 video.GetPalette().SetMode(Api::Video::Palette::MODE_CUSTOM);
913 video.GetPalette().SetCustom(composite_direct_fbx_palette, Api::Video::Palette::STD_PALETTE);
914 }
915 else if (strcmp(var.value, "pvm-style-d93-fbx") == 0) {
916 video.GetPalette().SetMode(Api::Video::Palette::MODE_CUSTOM);
917 video.GetPalette().SetCustom(pvm_style_d93_fbx_palette, Api::Video::Palette::STD_PALETTE);
918 }
919 else if (strcmp(var.value, "ntsc-hardware-fbx") == 0) {
920 video.GetPalette().SetMode(Api::Video::Palette::MODE_CUSTOM);
921 video.GetPalette().SetCustom(ntsc_hardware_fbx_palette, Api::Video::Palette::STD_PALETTE);
922 }
923 else if (strcmp(var.value, "nes-classic-fbx-fs") == 0) {
924 video.GetPalette().SetMode(Api::Video::Palette::MODE_CUSTOM);
925 video.GetPalette().SetCustom(nes_classic_fbx_fs_palette, Api::Video::Palette::STD_PALETTE);
926 }
927 else if (strcmp(var.value, "raw") == 0) {
928 /* outputs raw chroma/level/emphasis in the R/G/B channels
929 * that can be decoded by the frontend (using shaders for example)
930 * the following formulas can be used to extract the
931 * values back from a normalized R/G/B triplet
932 * chroma = floor((R * 15.0) + 0.5)
933 * level = floor((G * 3.0) + 0.5)
934 * emphasis = floor((B * 7.0) + 0.5) */
935 unsigned char raw_palette[512][3];
936 int i;
937 for (i = 0; i < 512; i++)
938 {
939 raw_palette[i][0] = (((i >> 0) & 0xF) * 255) / 15;
940 raw_palette[i][1] = (((i >> 4) & 0x3) * 255) / 3;
941 raw_palette[i][2] = (((i >> 6) & 0x7) * 255) / 7;
942 }
943 video.GetPalette().SetMode(Api::Video::Palette::MODE_CUSTOM);
944 video.GetPalette().SetCustom(raw_palette, Api::Video::Palette::EXT_PALETTE);
945 }
946 else if (strcmp(var.value, "custom") == 0) {
947 video.GetPalette().SetMode(Api::Video::Palette::MODE_CUSTOM);
948 video.GetPalette().SetCustom((const byte(*)[3])custpal, Api::Video::Palette::STD_PALETTE);
949 }
950 }
951
952 var.key = "nestopia_overscan_v";
953
954 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
955 overscan_v = (strcmp(var.value, "enabled") == 0);
956
957 var.key = "nestopia_overscan_h";
958
959 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
960 overscan_h = (strcmp(var.value, "enabled") == 0);
961
962 var.key = "nestopia_aspect";
963
964 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
965 {
966 if (!strcmp(var.value, "ntsc"))
967 aspect_ratio_mode = 1;
968 else if (!strcmp(var.value, "pal"))
969 aspect_ratio_mode = 2;
970 else if (!strcmp(var.value, "4:3"))
971 aspect_ratio_mode = 3;
972 else
973 aspect_ratio_mode = 0;
974 }
975
976 var.key = "nestopia_select_adapter";
977 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
978 {
979 if (!strcmp(var.value, "auto")) {
980 Api::Input(emulator).AutoSelectController(2);
981 Api::Input(emulator).AutoSelectController(3);
982 Api::Input(emulator).AutoSelectAdapter();
983 }
984 else if (!strcmp(var.value, "ntsc")) {
985 Api::Input(emulator).ConnectController(2, Api::Input::PAD3);
986 Api::Input(emulator).ConnectController(3, Api::Input::PAD4);
987 Api::Input(emulator).ConnectAdapter(Api::Input::ADAPTER_NES);
988 }
989 else if (!strcmp(var.value, "famicom")) {
990 Api::Input(emulator).ConnectController(2, Api::Input::PAD3);
991 Api::Input(emulator).ConnectController(3, Api::Input::PAD4);
992 Api::Input(emulator).ConnectAdapter(Api::Input::ADAPTER_FAMICOM);
993 }
994 }
995
996 var.key = "nestopia_turbo_pulse";
997
998 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
999 tpulse = atoi(var.value);
1000
1001 pitch = video_width * 4;
1002
1003 renderState.filter = filter;
1004 renderState.width = video_width;
1005 renderState.height = Api::Video::Output::HEIGHT;
1006 renderState.bits.count = 32;
1007 renderState.bits.mask.r = 0x00ff0000;
1008 renderState.bits.mask.g = 0x0000ff00;
1009 renderState.bits.mask.b = 0x000000ff;
1010 if (NES_FAILED(video.SetRenderState( renderState )) && log_cb)
1011 log_cb(RETRO_LOG_WARN, "Nestopia core rejected render state\n");;
1012
1013 retro_get_system_av_info(&av_info);
1014 environ_cb(RETRO_ENVIRONMENT_SET_GEOMETRY, &av_info);
1015
1016 var.key = "nestopia_audio_vol_sq1";
1017
1018 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1019 {
1020 sound.SetVolume(Api::Sound::CHANNEL_SQUARE1, atoi(var.value));
1021 }
1022
1023 var.key = "nestopia_audio_vol_sq2";
1024
1025 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1026 {
1027 sound.SetVolume(Api::Sound::CHANNEL_SQUARE2, atoi(var.value));
1028 }
1029
1030 var.key = "nestopia_audio_vol_tri";
1031
1032 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1033 {
1034 sound.SetVolume(Api::Sound::CHANNEL_TRIANGLE, atoi(var.value));
1035 }
1036
1037 var.key = "nestopia_audio_vol_noise";
1038
1039 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1040 {
1041 sound.SetVolume(Api::Sound::CHANNEL_NOISE, atoi(var.value));
1042 }
1043
1044 var.key = "nestopia_audio_vol_dpcm";
1045
1046 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1047 {
1048 sound.SetVolume(Api::Sound::CHANNEL_DPCM, atoi(var.value));
1049 }
1050
1051 var.key = "nestopia_audio_vol_fds";
1052
1053 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1054 {
1055 sound.SetVolume(Api::Sound::CHANNEL_FDS, atoi(var.value));
1056 }
1057
1058 var.key = "nestopia_audio_vol_mmc5";
1059
1060 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1061 {
1062 sound.SetVolume(Api::Sound::CHANNEL_MMC5, atoi(var.value));
1063 }
1064
1065 var.key = "nestopia_audio_vol_vrc6";
1066
1067 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1068 {
1069 sound.SetVolume(Api::Sound::CHANNEL_VRC6, atoi(var.value));
1070 }
1071
1072 var.key = "nestopia_audio_vol_vrc7";
1073
1074 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1075 {
1076 sound.SetVolume(Api::Sound::CHANNEL_VRC7, atoi(var.value));
1077 }
1078
1079 var.key = "nestopia_audio_vol_n163";
1080
1081 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1082 {
1083 sound.SetVolume(Api::Sound::CHANNEL_N163, atoi(var.value));
1084 }
1085
1086 var.key = "nestopia_audio_vol_s5b";
1087
1088 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1089 {
1090 sound.SetVolume(Api::Sound::CHANNEL_S5B, atoi(var.value));
1091 }
1092
1093
1094 var.key = "nestopia_show_advanced_av_settings";
1095
1096 var.value = NULL;
1097 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1098 {
1099 bool show_advanced_av_settings_prev = show_advanced_av_settings;
1100
1101 show_advanced_av_settings = true;
1102 if (strcmp(var.value, "disabled") == 0)
1103 show_advanced_av_settings = false;
1104
1105 if (show_advanced_av_settings != show_advanced_av_settings_prev)
1106 {
1107 size_t i;
1108 struct retro_core_option_display option_display;
1109 char av_keys[11][40] = {
1110 "nestopia_audio_vol_sq1",
1111 "nestopia_audio_vol_sq2",
1112 "nestopia_audio_vol_tri",
1113 "nestopia_audio_vol_noise",
1114 "nestopia_audio_vol_dpcm",
1115 "nestopia_audio_vol_fds",
1116 "nestopia_audio_vol_mmc5",
1117 "nestopia_audio_vol_vrc6",
1118 "nestopia_audio_vol_vrc7",
1119 "nestopia_audio_vol_n163",
1120 "nestopia_audio_vol_s5b"
1121 };
1122
1123 option_display.visible = show_advanced_av_settings;
1124
1125 for (i = 0; i < 11; i++)
1126 {
1127 option_display.key = av_keys[i];
1128 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
1129 }
1130 }
1131 }
1132
1133 }
1134
retro_run(void)1135 void retro_run(void)
1136 {
1137 update_input(libretro_supports_bitmasks);
1138 emulator.Execute(video, audio, input);
1139
1140 if (Api::Input(emulator).GetConnectedController(1) == 5)
1141 draw_crosshair(crossx, crossy);
1142
1143 unsigned frames = is_pal ? SAMPLERATE / 50 : SAMPLERATE / 60;
1144 for (unsigned i = 0; i < frames; i++)
1145 audio_stereo_buffer[(i << 1) + 0] = audio_stereo_buffer[(i << 1) + 1] = audio_buffer[i];
1146 audio_batch_cb(audio_stereo_buffer, frames);
1147
1148 bool updated = false;
1149 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
1150 {
1151 check_variables();
1152 delete video;
1153 video = 0;
1154 video = new Api::Video::Output(video_buffer, video_width * sizeof(uint32_t));
1155 }
1156
1157 // Absolute mess of inline if statements...
1158 int dif = blargg_ntsc ? 18 : 8;
1159
1160 video_cb(video_buffer + (overscan_v ? ((overscan_h ? dif : 0) + (blargg_ntsc ? Api::Video::Output::NTSC_WIDTH : Api::Video::Output::WIDTH) * 8) : (overscan_h ? dif : 0) + 0),
1161 video_width - (overscan_h ? 2 * dif : 0),
1162 Api::Video::Output::HEIGHT - (overscan_v ? 16 : 0),
1163 pitch);
1164 }
1165
extract_basename(char * buf,const char * path,size_t size)1166 static void extract_basename(char *buf, const char *path, size_t size)
1167 {
1168 const char *base = strrchr(path, '/');
1169 if (!base)
1170 base = strrchr(path, '\\');
1171 if (!base)
1172 base = path;
1173
1174 if (*base == '\\' || *base == '/')
1175 base++;
1176
1177 strncpy(buf, base, size - 1);
1178 buf[size - 1] = '\0';
1179
1180 char *ext = strrchr(buf, '.');
1181 if (ext)
1182 *ext = '\0';
1183 }
1184
extract_directory(char * buf,const char * path,size_t size)1185 static void extract_directory(char *buf, const char *path, size_t size)
1186 {
1187 strncpy(buf, path, size - 1);
1188 buf[size - 1] = '\0';
1189
1190 char *base = strrchr(buf, '/');
1191 if (!base)
1192 base = strrchr(buf, '\\');
1193
1194 if (base)
1195 *base = '\0';
1196 else
1197 buf[0] = '\0';
1198 }
1199
1200
retro_load_game(const struct retro_game_info * info)1201 bool retro_load_game(const struct retro_game_info *info)
1202 {
1203 const char *dir;
1204 char slash;
1205 char db_path[256];
1206 char palette_path[256];
1207
1208 #if defined(_WIN32)
1209 slash = '\\';
1210 #else
1211 slash = '/';
1212 #endif
1213
1214 struct retro_input_descriptor desc[] = {
1215 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
1216 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
1217 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
1218 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
1219 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" },
1220 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "A" },
1221 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Turbo A" },
1222 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Turbo B" },
1223 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "(FDS) Disk Side Change" },
1224 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "(FDS) Eject Disk" },
1225 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "(VSSystem) Coin 1" },
1226 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "(VSSystem) Coin 2" },
1227 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "(Famicom) Microphone" },
1228 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
1229 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
1230
1231 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
1232 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
1233 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
1234 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
1235 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" },
1236 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "A" },
1237 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Turbo A" },
1238 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Turbo B" },
1239 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "(FDS) Disk Side Change" },
1240 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "(FDS) Eject Disk" },
1241 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
1242 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
1243
1244 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
1245 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
1246 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
1247 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
1248 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" },
1249 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "A" },
1250 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Turbo A" },
1251 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Turbo B" },
1252 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "(FDS) Disk Side Change" },
1253 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "(FDS) Eject Disk" },
1254 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
1255 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
1256
1257 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
1258 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
1259 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
1260 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
1261 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" },
1262 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "A" },
1263 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Turbo A" },
1264 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Turbo B" },
1265 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "(FDS) Disk Side Change" },
1266 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "(FDS) Eject Disk" },
1267 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
1268 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
1269
1270 { 0 },
1271 };
1272
1273 #ifdef _3DS
1274 video_buffer = (uint32_t*)linearMemAlign(Api::Video::Output::NTSC_WIDTH * Api::Video::Output::HEIGHT * sizeof(uint32_t), 0x80);
1275 #else
1276 video_buffer = (uint32_t*)malloc(Api::Video::Output::NTSC_WIDTH * Api::Video::Output::HEIGHT * sizeof(uint32_t));
1277 #endif
1278
1279 machine = new Api::Machine(emulator);
1280 input = new Api::Input::Controllers;
1281 Api::User::fileIoCallback.Set(file_io_callback, 0);
1282
1283 environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, desc);
1284
1285 if (!environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) || !dir)
1286 return false;
1287
1288 sprintf(samp_dir, "%s%cnestopia%csamples", dir, slash, slash);
1289
1290 sprintf(palette_path, "%s%ccustom.pal", dir, slash);
1291
1292 if (log_cb)
1293 log_cb(RETRO_LOG_INFO, "Custom palette path: %s\n", palette_path);
1294
1295 std::ifstream *custompalette = new std::ifstream(palette_path, std::ifstream::in|std::ifstream::binary);
1296
1297 if (custompalette->is_open())
1298 {
1299 custompalette->read((char*)custpal, sizeof(custpal));
1300 if (log_cb)
1301 log_cb(RETRO_LOG_INFO, "custom.pal loaded from system directory.\n");
1302 }
1303 else
1304 {
1305 memcpy(custpal, cxa2025as_palette, sizeof(custpal));
1306 if (log_cb)
1307 log_cb(RETRO_LOG_INFO, "custom.pal not found in system directory.\n");
1308 }
1309 delete custompalette;
1310
1311 Api::Cartridge::Database database(emulator);
1312
1313 size_t db_size = sizeof(nst_db_xml)/sizeof(unsigned char);
1314 std::string db_buf((const char*)nst_db_xml, db_size);
1315 std::istringstream *db_file = new std::istringstream(db_buf);
1316
1317 database.Load(*db_file);
1318 database.Enable(true);
1319
1320 if (info->path != NULL)
1321 {
1322 extract_basename(g_basename, info->path, sizeof(g_basename));
1323 extract_directory(g_rom_dir, info->path, sizeof(g_rom_dir));
1324 }
1325
1326 enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_XRGB8888;
1327 if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt))
1328 {
1329 if (log_cb)
1330 log_cb(RETRO_LOG_ERROR, "XRGB8888 is not supported.\n");
1331 return false;
1332 }
1333
1334 std::stringstream ss(std::string(reinterpret_cast<const char*>(info->data),
1335 reinterpret_cast<const char*>(info->data) + info->size));
1336
1337 if (info->path && (strstr(info->path, ".fds") || strstr(info->path, ".FDS")))
1338 {
1339 fds = new Api::Fds(emulator);
1340
1341 if (fds)
1342 {
1343 char fds_bios_path[256];
1344 /* search for BIOS in system directory */
1345 bool found = false;
1346
1347 sprintf(fds_bios_path, "%s%cdisksys.rom", dir, slash);
1348 if (log_cb)
1349 log_cb(RETRO_LOG_INFO, "FDS BIOS path: %s\n", fds_bios_path);
1350
1351 std::ifstream *fds_bios_file = new std::ifstream(fds_bios_path, std::ifstream::in|std::ifstream::binary);
1352
1353 if (fds_bios_file->is_open())
1354 fds->SetBIOS(fds_bios_file);
1355 else
1356 {
1357 delete fds_bios_file;
1358 return false;
1359 }
1360 }
1361 else
1362 return false;
1363 }
1364
1365 if (!environ_cb(RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY, &g_save_dir))
1366 {
1367 if (log_cb)
1368 log_cb(RETRO_LOG_ERROR, "Could not find save directory.\n");
1369 }
1370
1371 is_pal = false;
1372 check_variables();
1373
1374 if (machine->Load(ss, favsystem))
1375 return false;
1376
1377 Api::Video ivideo(emulator);
1378 ivideo.SetSharpness(Api::Video::DEFAULT_SHARPNESS_RGB);
1379 ivideo.SetColorResolution(Api::Video::DEFAULT_COLOR_RESOLUTION_RGB);
1380 ivideo.SetColorBleed(Api::Video::DEFAULT_COLOR_BLEED_RGB);
1381 ivideo.SetColorArtifacts(Api::Video::DEFAULT_COLOR_ARTIFACTS_RGB);
1382 ivideo.SetColorFringing(Api::Video::DEFAULT_COLOR_FRINGING_RGB);
1383
1384 Api::Video::RenderState state;
1385 state.filter = Api::Video::RenderState::FILTER_NONE;
1386 state.width = 256;
1387 state.height = 240;
1388 state.bits.count = 32;
1389 state.bits.mask.r = 0x00ff0000;
1390 state.bits.mask.g = 0x0000ff00;
1391 state.bits.mask.b = 0x000000ff;
1392 ivideo.SetRenderState(state);
1393
1394 Api::Sound isound(emulator);
1395 isound.SetSampleBits(16);
1396 isound.SetSampleRate(SAMPLERATE);
1397 isound.SetSpeaker(Api::Sound::SPEAKER_MONO);
1398
1399 Api::Input(emulator).AutoSelectController(0);
1400 Api::Input(emulator).AutoSelectController(1);
1401
1402 machine->Power(true);
1403
1404 check_variables();
1405
1406 if (fds_auto_insert && machine->Is(Nes::Api::Machine::DISK))
1407 fds->InsertDisk(0, 0);
1408
1409 video = new Api::Video::Output(video_buffer, video_width * sizeof(uint32_t));
1410
1411 if (log_cb)
1412 log_cb(RETRO_LOG_INFO, "[Nestopia]: Machine is %s.\n", is_pal ? "PAL" : "NTSC");
1413
1414 return true;
1415 }
1416
retro_unload_game(void)1417 void retro_unload_game(void)
1418 {
1419 if (machine)
1420 {
1421 machine->Unload();
1422
1423 if (machine->Is(Nes::Api::Machine::DISK))
1424 {
1425 if (fds)
1426 delete fds;
1427 fds = 0;
1428 }
1429
1430 delete machine;
1431 }
1432
1433 if (video)
1434 delete video;
1435 if (audio)
1436 delete audio;
1437 if (input)
1438 delete input;
1439
1440 machine = 0;
1441 video = 0;
1442 audio = 0;
1443 input = 0;
1444
1445 sram = 0;
1446 sram_size = 0;
1447
1448 #ifdef _3DS
1449 linearFree(video_buffer);
1450 #else
1451 free(video_buffer);
1452 #endif
1453 video_buffer = NULL;
1454 }
1455
retro_get_region(void)1456 unsigned retro_get_region(void)
1457 {
1458 return is_pal ? RETRO_REGION_PAL : RETRO_REGION_NTSC;
1459 }
1460
retro_load_game_special(unsigned,const struct retro_game_info *,size_t)1461 bool retro_load_game_special(unsigned, const struct retro_game_info *, size_t)
1462 {
1463 return false;
1464 }
1465
retro_serialize_size(void)1466 size_t retro_serialize_size(void)
1467 {
1468 std::stringstream ss;
1469 if (machine->SaveState(ss, Api::Machine::NO_COMPRESSION))
1470 return 0;
1471 return ss.str().size();
1472 }
1473
retro_serialize(void * data,size_t size)1474 bool retro_serialize(void *data, size_t size)
1475 {
1476 std::stringstream ss;
1477 if (machine->SaveState(ss, Api::Machine::NO_COMPRESSION))
1478 return false;
1479
1480 std::string state = ss.str();
1481 if (state.size() > size)
1482 return false;
1483
1484 std::copy(state.begin(), state.end(), reinterpret_cast<char*>(data));
1485 return true;
1486 }
1487
retro_unserialize(const void * data,size_t size)1488 bool retro_unserialize(const void *data, size_t size)
1489 {
1490 std::stringstream ss(std::string(reinterpret_cast<const char*>(data),
1491 reinterpret_cast<const char*>(data) + size));
1492 return !machine->LoadState(ss);
1493 }
1494
retro_get_memory_data(unsigned id)1495 void *retro_get_memory_data(unsigned id)
1496 {
1497 Core::Machine& machineGet = emulator;
1498 switch(id)
1499 {
1500 case RETRO_MEMORY_SAVE_RAM:
1501 return sram;
1502
1503 case RETRO_MEMORY_SYSTEM_RAM:
1504 return (void*)&machineGet.cpu.GetRam()[0];
1505
1506 }
1507
1508 return NULL;
1509 }
1510
retro_get_memory_size(unsigned id)1511 size_t retro_get_memory_size(unsigned id)
1512 {
1513 Core::Machine& machineGet = emulator;
1514 switch(id)
1515 {
1516 case RETRO_MEMORY_SAVE_RAM:
1517 return sram_size;
1518
1519 case RETRO_MEMORY_SYSTEM_RAM:
1520 return machineGet.cpu.RAM_SIZE;
1521 }
1522
1523 return 0;
1524 }
1525
retro_cheat_reset(void)1526 void retro_cheat_reset(void)
1527 {
1528 Nes::Api::Cheats cheater(emulator);
1529 cheater.ClearCodes();
1530 }
1531
retro_cheat_set(unsigned index,bool enabled,const char * code)1532 void retro_cheat_set(unsigned index, bool enabled, const char *code)
1533 {
1534 Nes::Api::Cheats cheater(emulator);
1535 Nes::Api::Cheats::Code ggCode;
1536 char codeCopy[256];
1537 char *part;
1538
1539 if (code == NULL) return;
1540 strcpy(codeCopy,code);
1541 part = strtok(codeCopy,"+,;._ ");
1542
1543 while (part)
1544 {
1545 if ((strlen(part) == 7) && (part[4]==':'))
1546 {
1547 part[4]='\0';
1548 ggCode.address=strtoul(part,NULL,16);
1549 ggCode.value=strtoul(part+5,NULL,16);
1550 cheater.SetCode(ggCode);
1551 }
1552 else if ((strlen(part)==10) && (part[4]=='?') && (part[7]==':'))
1553 {
1554 part[4]='\0';
1555 part[7]='\0';
1556 ggCode.address=strtoul(part,NULL,16);
1557 ggCode.compare=strtoul(part+5,NULL,16);
1558 ggCode.useCompare=true;
1559 ggCode.value=strtoul(part+8,NULL,16);
1560 cheater.SetCode(ggCode);
1561 }
1562 else if (Nes::Api::Cheats::GameGenieDecode(part, ggCode) == RESULT_OK)
1563 cheater.SetCode(ggCode);
1564 else if (Nes::Api::Cheats::ProActionRockyDecode(part, ggCode) == RESULT_OK)
1565 cheater.SetCode(ggCode);
1566 part = strtok(NULL,"+,;._ ");
1567 }
1568 }
1569