summaryrefslogtreecommitdiff
path: root/source/main.c
blob: b5f9c72cfba2553bc305f5bfdde2b8cc307c6b21 (plain)
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
/*
   3dsspec - Nintendo 3DS Spectrum emulator.

   Copyright (C) 2021  Ian Cowburn <ianc@noddybox.co.uk>
   
   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version 3
   of the License, or (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, see <http://www.gnu.org/licenses/>
  
   $Id: main.c 77 2010-11-23 08:10:25Z ianc $
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <3ds.h>

#include "framebuffer.h"
#include "gui.h"
#include "keyboard.h"
#include "z80.h"
#include "spec.h"
#include "config.h"
#include "snapshot.h"
#include "snap.h"

#define DSSPEC_VERSION "0.1"

#ifndef TRUE
#define TRUE 1
#endif

#ifndef FALSE
#define FALSE 0
#endif

#define DEBUG(x) \
    do                                          \
    {                                           \
        printf(x);                              \
        gfxFlushBuffers();                      \
        gfxSwapBuffers();                       \
    } while(0);                                 \
    do                                          \
    {                                           \
        hidScanInput();                         \
    } while(!(hidKeysDown() & KEY_TOUCH))       \

/* ---------------------------------------- STATIC DATA
*/
static const char *main_menu[]=
	{
	    "Reset Spectrum",
            "Load TAP File",
	    "Configure",
	    "Map Joypad to Keys",
	    "Save Memory Snapshot",
	    "Load Memory Snapshot",
	    "Save Joypad/Key State",
	    "Load Joypad/Key State",
	    "Exit 3DSSPEC",
	    "Cancel",
	    NULL
	};

typedef enum
{
    MenuReset,
    MenuLoadTAP,
    MenuConfigure,
    MenuMapJoypad,
    MenuSaveSnapshot,
    MenuLoadSnapshot,
    MenuSaveMappings,
    MenuLoadMappings,
    MenuExit
} MenuOpt;


/* ---------------------------------------- DISPLAY FUNCS
*/
static void Splash(void)
{
    static char scroller[]=
    {
    	"                   "
	"Welcome to 3DSSPEC, a Sinclair Spectrum 48K emulator for the Nintendo "
        "3DS.  You can safely ignore this message.  I was just bored for half "
        "an hour.  And no retro game is complete without a side-scroller...  "
	"Thanks to Slay Radio, Ladytron, the Genki Rockets, the High "
	"Voltage SID Collection, The Prodigy, Paradise Lost and "
        "Retro Gamer for coding fuel."
    };

    static const char *text[]=
    {
    	"3DSSPEC \177 2021 Ian Cowburn",
	" ",
	"Spectrum ROM \177 1982 Amstrad",
	" ",
	"PRESS A TO CONTINUE",
	" ",
	"https://deathstation9000.org.uk/",
	" ",
	NULL
    };

    int f;
    int y;
    int scr_x = 0;
    int scr_y = 240;
    Framebuffer upper;
    Framebuffer lower;

    SNAP_Enable(TRUE);

    while(!(hidKeysDown() & KEY_A))
    {
        FB_StartFrame(&upper, &lower);

        FB_Clear(&upper, COL_BLACK);
        FB_Clear(&lower, COL_BLACK);

        FB_Blit(&upper, IMG_SPLASH, 0, scr_y);

        FB_Print(&upper, "10 REM VERSION \001" DSSPEC_VERSION "\001\n"
                         "20 PRINT \"\001THE SPECTRUM IS ACE\001\"\n"
                         "30 GOTO 20", 
                 0, 230, COL_BLACK, COL_TRANSPARENT);

        FB_printf(&lower, scr_x, 8, COL_WHITE, COL_TRANSPARENT,
                  "%-42.42s",scroller);

        y = lower.height - 20;

        for(f=0;text[f];f++)
        {
            FB_Centre(&lower, text[f], y, COL_WHITE, COL_TRANSPARENT);
            y -= 8;
        }

	FB_EndFrame();

        if (scr_y > 0)
        {
            scr_y--;
        }

	if (--scr_x == -8)
	{
	    size_t l = sizeof scroller;
	    char c;

	    scr_x = 0;

	    c = scroller[0];
	    memmove(scroller, scroller+1, l-2);
	    scroller[l-2] = c;
	}
    }
}


/* ---------------------------------------- JOYPAD MAPPING
*/
static void MapJoypad(void)
{
    SoftKeyEvent ev;
    SoftKey pad = NUM_SOFT_KEYS;
    int done = FALSE;
    Framebuffer upper;
    Framebuffer lower;

    while(!done)
    {
        FB_StartFrame(&upper, &lower);

        SK_DisplayKeyboard();

        FB_Clear(&upper, COL_WHITE);

        FB_Print(&upper, "Press the joypad button you want\n"
                         "to define and then the Spectrum key\n"
                         "you want to use for that button.\n\n"
		         "Press MENU to finish.",
                         0, upper.height - 40, COL_BLACK,COL_TRANSPARENT);

        if (pad != NUM_SOFT_KEYS)
        {
            FB_printf(&upper, 0, 80, COL_BLACK, COL_TRANSPARENT,
                        "defining\n  \001%s\001",SK_KeyName(pad));
        }

        FB_EndFrame();

	while(SK_GetBareEvent(&ev))
	{
	    if (ev.pressed)
	    {
	    	if (ev.key==SK_ABOUT || ev.key==SK_CONFIG)
		{
		    done = true;
		}
	    }
	    else
	    {
		if (ev.key>=SK_PAD_UP && ev.key<=SK_PAD_SELECT)
		{
		    pad = ev.key;
		}

		if (ev.key<=SK_SPACE && pad!=NUM_SOFT_KEYS)
		{
		    SK_DefinePad(pad,ev.key);
		    pad = NUM_SOFT_KEYS;
		}
	    }
	}
    }
}


/* ---------------------------------------- MAIN
*/
int main(int argc, char *argv[])
{
    Z80 *z80;
    int quit = FALSE;
    float mix[12] = {1.0, 1.0};

    gfxInit(GSP_RGB565_OES, GSP_RGB565_OES, FALSE);
    ndspInit();

    ndspSetOutputMode(NDSP_OUTPUT_MONO);
    ndspChnSetInterp(0, NDSP_INTERP_NONE);
    ndspChnSetRate(0, SAMPLE_RATE);
    ndspChnSetFormat(0, NDSP_FORMAT_MONO_PCM8);
    ndspChnSetMix(0, mix);

    FB_Init();
    GUI_Init();

    z80 = Z80Init(SPECReadPort, SPECWritePort);

    if (!z80)
    {
	GUI_Alert(TRUE,"Failed to initialise\nthe Z80 CPU emulation!");
    }

    SPECInit(z80);

    Splash();

    LoadConfig();
    SPECReconfigure();

    SK_DisplayKeyboard();

    SK_SetSticky(SK_SHIFT,DSSPEC_Config[DSSPEC_STICKY_SHIFT]);
    SK_SetSticky(SK_SYMBOL,DSSPEC_Config[DSSPEC_STICKY_SHIFT]);

    if (DSSPEC_Config[DSSPEC_LOAD_DEFAULT_SNAPSHOT])
    {
    	SNAP_Load(z80, "AUTO", SNAP_TYPE_FULL);
    }

    while(!quit && aptMainLoop())
    {
	SoftKeyEvent ev;
        Framebuffer upper;
        Framebuffer lower;

        FB_StartFrame(&upper, &lower);

        SK_DisplayKeyboard();

        SPECStartFrame(&upper);

    	Z80Exec(z80);

        FB_EndFrame();

	while(SK_GetEvent(&ev))
	{
	    switch(ev.key)
	    {
	    	case SK_ABOUT:
	    	case SK_CONFIG:
		    if (ev.pressed)
		    {
			switch(GUI_Menu(main_menu))
			{
			    case MenuReset:
				SPECReset(z80);
				break;

			    case MenuLoadTAP:
                            {
                                char file[FILENAME_MAX];

                                if (GUI_FileSelect(last_dir, file, ".TAP"))
                                {
                                    TAPCloseTape();
                                    TAPOpenTape(file);
                                }
				break;
                            }

			    case MenuConfigure:
				GUI_Config();
				SK_SetSticky
                                    (SK_SHIFT,
                                     DSSPEC_Config[DSSPEC_STICKY_SHIFT]);
				SPECReconfigure();
				break;

			    case MenuMapJoypad:
				MapJoypad();
			    	break;

			    case MenuSaveSnapshot:
			    	SNAP_Save(z80, SNAP_TYPE_FULL);
			    	break;

			    case MenuLoadSnapshot:
			    	SNAP_Load(z80, NULL, SNAP_TYPE_FULL);
			    	break;

			    case MenuSaveMappings:
			    	SNAP_Save(z80, SNAP_TYPE_KEYBOARD);
			    	break;

			    case MenuLoadMappings:
			    	SNAP_Load(z80, NULL, SNAP_TYPE_KEYBOARD);
			    	break;

                            case MenuExit:
                                quit = TRUE;
                                break;
			}
		    }
		    break;

	    	default:
		    SPECHandleKey(ev.key,ev.pressed);
		    break;
	    }
	}
    }

    ndspExit();
    gfxExit();

    return 0;
}