summaryrefslogtreecommitdiff
path: root/source/main.c
blob: ef2633e623dd3bb66c52b0e9b621493d8ba73324 (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
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
/*
   3ds81 - Nintendo 3DS ZX81 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 "zx81.h"
#include "tapes.h"
#include "config.h"
#include "snapshot.h"
#include "debug.h"

#ifndef DS81_VERSION
#define DS81_VERSION "DEV " __TIME__ "/" __DATE__
#endif

#ifndef TRUE
#define TRUE 1
#endif

#ifndef FALSE
#define FALSE 0
#endif

/* Macro for PRINT AT in the upper framebuffer
*/
#define AT(x,y) (x)*8,upper.height-1-(y)*8

/* ---------------------------------------- STATIC DATA
*/
static const char *main_menu[]=
	{
	    "Reset ZX81",
	    "Select Tape",
	    "Configure",
	    "Map Joypad to Keys",
	    "Save Memory Snapshot",
	    "Load Memory Snapshot",
	    "Save Joypad/Key State",
	    "Load Joypad/Key State",
            "Help",
            "Debug",
	    "Exit 3DS81",
	    "Cancel",
	    NULL
	};

typedef enum
{
    MenuReset,
    MenuSelectTape,
    MenuConfigure,
    MenuMapJoypad,
    MenuSaveSnapshot,
    MenuLoadSnapshot,
    MenuSaveMappings,
    MenuLoadMappings,
    MenuHelp,
    MenuDebug,
    MenuExit
} MenuOpt;

static const char *debug_menu[]=
	{
	    "Enable Debug Output",
	    "Disable Debug Output",
            "Disassembler",
            "Memory Viewer",
            "CPU Info",
            "Cancel",
	    NULL
	};

typedef enum
{
    DebugMenuEnableOutput,
    DebugMenuDisableOutput,
    DebugMenuDisassembler,
    DebugMenuMemoryView,
    DebugMenuCPUInfo,
} DebugMenuOpt;


/* ---------------------------------------- DISPLAY FUNCS
*/
static void Splash(void)
{
    static char scroller[]=
    {
    	"                   "
	"Welcome to 3DS81, a ZX81 emulator for the Ninetendo 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[]=
    {
    	"3DS81 \177 2021 Ian Cowburn",
	" ",
	"ZX81 ROM \177 1981",
	"Nine Tiles Networks LTD",
	" ",
	"PRESS A TO CONTINUE",
	" ",
	"https://noddybox.co.uk/",
	" ",
        "If you place .P tape files in",
        "the top directory or ZX81SNAP",
        "then you should be able to load",
        "GAME.P with the command",
        "LOAD \"GAME\"",
	NULL
    };

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

    ZX81EnableFileSystem(TRUE);
    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" DS81_VERSION "\001\n"
                         "20 PRINT \"\001THE ZX81 IS ACE\001\"\n"
                         "30 GOTO 20", 
                 0, 230, COL_WHITE, 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;
	}
    }
}


/* ---------------------------------------- HELP
*/
static void ShowHelp(void)
{
    struct
    {
        FB_Colour col;
        const char *text;
    }
    help[] =
    {
        {COL_RED, "KEYBOARD"},
        {COL_BLACK, "Use the touchscreen to press keys on the computer."},
        {COL_BLACK, "The ZX81 had a keyword entry system, so when the"},
        {COL_BLACK, "cursor is an inverse K then the keyword printed"},
        {COL_BLACK, "at the top of the key is displayed.  Therefore"},
        {COL_BLACK, "to load a tape press the J key, then the SHIFT"},
        {COL_BLACK, "key (which by default stays pressed until you"},
        {COL_BLACK, "click it again) and press the P key to enter a"},
        {COL_BLACK, "quote, press the SHIFT key to stop pressing it,"}, 
        {COL_BLACK, "type the name of the .P file and close it with"},
        {COL_BLACK, "another quote, then press ENTER"},
        {COL_TRANSPARENT, ""},
        {COL_RED, "LOADING TAPES"},
        {COL_BLACK, "There are two ways of loading tapes in the"},
        {COL_BLACK, "simulator.  The first is to put .P files in"},
        {COL_BLACK, "either the root directory of the SD card"},
        {COL_BLACK, "or a directory called ZX81SNAP.  Files from"},
        {COL_BLACK, "either place can be loaded by typing"},
        {COL_BLACK, "LOAD \"GAME\" to load GAME.P.  Alternatively"},
        {COL_BLACK, "you can type LOAD \"*\" to display a file"},
        {COL_BLACK, "selector that allows you to select the tape"},
        {COL_BLACK, "to load.  LOAD \"\" can be used to load an"},
        {COL_BLACK, "internal tape image once it has been selected."},
        {COL_TRANSPARENT, NULL}
    };

    int done = FALSE;
    u32 key;
    Framebuffer upper;
    Framebuffer lower;

    while(!done)
    {
        int f;

        FB_StartFrame(&upper, &lower);

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

        for(f = 0; help[f].text; f++)
        {
            FB_Print(&upper, help[f].text, 0, upper.height - f * 8,
                     help[f].col,COL_TRANSPARENT);
        }

        FB_Print(&lower, "Scroll around help by using the\n"
                         "control pad\n"
		         "Press A to finish.",
                         0, lower.height - 40, COL_YELLOW,COL_TRANSPARENT);

        FB_EndFrame();

        key = hidKeysDown();

        if (key & KEY_A)
        {
            done = TRUE;
        }
    }
}


/* ---------------------------------------- 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 ZX81 key\n"
                         "you want to use.\n\n"
		         "Press CONFIG 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;
		}
	    }
	}
    }
}


/* ---------------------------------------- DEBUG FUNCTIONS
*/
static void DebugDisassembler(Z80 *cpu)
{
    static Z80Word addr = 0;
    int done = FALSE;
    Z80Word curr_addr = 0;
    Framebuffer upper;
    Framebuffer lower;
    SoftKeyEvent ev;
    int f;

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

        SK_DisplayKeyboard();

        curr_addr = addr;

        FB_Clear(&upper, COL_WHITE);

        for(f=upper.height - 1; f > 0; f -= 8)
        {
            FB_printf(&upper, 0, f, COL_RED, COL_TRANSPARENT, 
                      "%4.4x:", (unsigned)curr_addr);
            FB_printf(&upper, 6*8, f, COL_BLACK, COL_TRANSPARENT,
                      "%s", Z80Disassemble(cpu, &curr_addr));
        }

        FB_EndFrame();

	while(SK_GetBareEvent(&ev))
	{
	    if (ev.pressed)
	    {
                switch(ev.key)
                {
                    case SK_PAD_UP:
                        addr--;
                        break;

                    case SK_PAD_DOWN:
                        addr++;
                        break;

                    case SK_PAD_R:
                        addr = curr_addr;
                        break;

                    case SK_PAD_L:
                        addr -= 20;
                        break;

                    case SK_PAD_START:
                        done = TRUE;
                        break;

                    case SK_PAD_B:
                    {
                        char addrstr[32] = {0};

                        if (GUI_Input("Enter address", addrstr, 31))
                        {
                            addr = (Z80Word)strtoul(addrstr, NULL, 0);
                        }

                        break;
                    }

                    default:
                        break;
                }
	    }
	}
    }
}

static void DebugMemory(Z80 *cpu)
{
    static Z80Word addr = 0;
    int done = FALSE;
    Z80Word curr_addr = 0;
    Framebuffer upper;
    Framebuffer lower;
    SoftKeyEvent ev;
    int f;
    int r;
    int rows;

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

        SK_DisplayKeyboard();

        curr_addr = addr;

        FB_Clear(&upper, COL_WHITE);

        rows = 0;

        for(f=upper.height - 1; f > 0; f -= 8)
        {
            FB_printf(&upper, 0, f, COL_RED, COL_TRANSPARENT, 
                      "%4.4x:", (unsigned)curr_addr);

            for(r=0; r < 8; r++)
            {
                char c = '.';
                unsigned b;

                b = ZX81ReadDisassem(cpu, curr_addr);

                FB_printf(&upper, 6*8 + r*24, f, COL_BLACK, COL_TRANSPARENT,
                          "%2.2x", b);

                if (b >= 32 && b < 128)
                {
                    c = b;
                }

                FB_printf(&upper, 31*8 + r*8, f, COL_BLUE, COL_TRANSPARENT,
                          "%c", c);

                curr_addr++;
            }

            rows++;
        }

        FB_EndFrame();

	while(SK_GetBareEvent(&ev))
	{
	    if (ev.pressed)
	    {
                switch(ev.key)
                {
                    case SK_PAD_UP:
                        addr--;
                        break;

                    case SK_PAD_DOWN:
                        addr++;
                        break;

                    case SK_PAD_R:
                        addr += rows * 8;
                        break;

                    case SK_PAD_L:
                        addr -= rows * 8;
                        break;

                    case SK_PAD_START:
                        done = TRUE;
                        break;

                    case SK_PAD_B:
                    {
                        char addrstr[32] = {0};

                        if (GUI_Input("Enter address", addrstr, 31))
                        {
                            addr = (Z80Word)strtoul(addrstr, NULL, 0);
                        }

                        break;
                    }

                    default:
                        break;
                }
	    }
	}
    }
}

static void DebugCPU(Z80 *cpu)
{
    static enum
    {
        AddrPC,
        AddrSP,
        AddrHL,
        AddrBC,
        AddrDE,
        AddrIX,
        AddrIY,
        NumAddr
    } addr_type = AddrPC;

    const char *addr_text[]=
    {
        "PC", "SP", "HL", "BC", "DE", "IX", "IY"
    };

    int done = FALSE;
    Framebuffer upper;
    Framebuffer lower;
    SoftKeyEvent ev;
    Z80Word addr;
    Z80Word base;
    int f;

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

        SK_DisplayKeyboard();

        FB_Clear(&upper, COL_WHITE);

        addr = cpu->PC;

        FB_printf(&upper, AT(0,0), COL_BLACK, COL_TRANSPARENT,
                  "PC: %4.4x %s", (unsigned)cpu->PC,
                                  Z80Disassemble(cpu, &addr));

        FB_printf(&upper, AT(0,2), COL_BLACK, COL_TRANSPARENT,
                  "AF: %4.4x", (unsigned)cpu->AF.w);

        FB_printf(&upper, AT(0,3), COL_BLACK, COL_TRANSPARENT,
                  "BC: %4.4x", (unsigned)cpu->BC.w);

        FB_printf(&upper, AT(0,4), COL_BLACK, COL_TRANSPARENT,
                  "DE: %4.4x", (unsigned)cpu->DE.w);

        FB_printf(&upper, AT(0,5), COL_BLACK, COL_TRANSPARENT,
                  "HL: %4.4x", (unsigned)cpu->HL.w);

        FB_printf(&upper, AT(0,6), COL_BLACK, COL_TRANSPARENT,
                  "IX: %4.4x", (unsigned)cpu->IX.w);

        FB_printf(&upper, AT(0,7), COL_BLACK, COL_TRANSPARENT,
                  "IY: %4.4x", (unsigned)cpu->IX.w);

        FB_printf(&upper, AT(9,2), COL_BLACK, COL_TRANSPARENT,
                  "AF': %4.4x", (unsigned)cpu->AF_);

        FB_printf(&upper, AT(9,3), COL_BLACK, COL_TRANSPARENT,
                  "BC': %4.4x", (unsigned)cpu->BC_);

        FB_printf(&upper, AT(9,4), COL_BLACK, COL_TRANSPARENT,
                  "DE': %4.4x", (unsigned)cpu->DE_);

        FB_printf(&upper, AT(9,5), COL_BLACK, COL_TRANSPARENT,
                  "HL': %4.4x", (unsigned)cpu->HL_);

        switch(addr_type)
        {
            case AddrPC:
                addr = cpu->PC;
                break;
            case AddrSP:
                addr = cpu->SP;
                break;
            case AddrHL:
                addr = cpu->HL.w;
                break;
            case AddrBC:
                addr = cpu->BC.w;
                break;
            case AddrDE:
                addr = cpu->DE.w;
                break;
            case AddrIX:
                addr = cpu->IX.w;
                break;
            case AddrIY:
                addr = cpu->IY.w;
                break;
            default:
                break;
        }

        base = addr;
        addr -= 5;

        for(f=0; f < 11; f++)
        {
            if (addr == base)
            {
                FB_printf(&upper, AT(0,9+f), COL_BLACK, COL_LIGHTGREY,
                            "%s %4.4x: %2.2x",
                                addr_text[addr_type],
                                (unsigned)addr,
                                (unsigned)ZX81ReadDisassem(cpu, addr));
            }
            else
            {
                FB_printf(&upper, AT(3,9+f), COL_BLACK, COL_TRANSPARENT,
                            "%4.4x: %2.2x",
                                (unsigned)addr,
                                (unsigned)ZX81ReadDisassem(cpu, addr));
            }

            addr++;
        }

        FB_printf(&upper, AT(0,21), COL_BLACK, COL_TRANSPARENT,
                    "IFF1: %2.2x IFF2: %2.2x IM: %2.2x I:%2.2x R:%2.2x",
                        (unsigned)cpu->IFF1,
                        (unsigned)cpu->IFF2,
                        (unsigned)cpu->IM,
                        (unsigned)cpu->I,
                        (unsigned)cpu->R);

        FB_printf(&upper, AT(0,23), COL_BLACK, COL_TRANSPARENT,
                    "Flags: SZ5H3PNC");

        FB_printf(&upper, AT(7,24), COL_BLACK, 
                    cpu->AF.b.lo & eZ80_Sign ? COL_GREEN : COL_RED, " ");

        FB_printf(&upper, AT(8,24), COL_BLACK, 
                    cpu->AF.b.lo & eZ80_Zero ? COL_GREEN : COL_RED, " ");

        FB_printf(&upper, AT(9,24), COL_BLACK, 
                    cpu->AF.b.lo & eZ80_Hidden5 ? COL_GREEN : COL_RED, " ");

        FB_printf(&upper, AT(10,24), COL_BLACK, 
                    cpu->AF.b.lo & eZ80_HalfCarry ? COL_GREEN : COL_RED, " ");

        FB_printf(&upper, AT(11,24), COL_BLACK, 
                    cpu->AF.b.lo & eZ80_Hidden3 ? COL_GREEN : COL_RED, " ");

        FB_printf(&upper, AT(12,24), COL_BLACK, 
                    cpu->AF.b.lo & eZ80_PV ? COL_GREEN : COL_RED, " ");

        FB_printf(&upper, AT(13,24), COL_BLACK, 
                    cpu->AF.b.lo & eZ80_Neg ? COL_GREEN : COL_RED, " ");

        FB_printf(&upper, AT(14,24), COL_BLACK, 
                    cpu->AF.b.lo & eZ80_Carry ? COL_GREEN : COL_RED, " ");

        FB_EndFrame();

        if (SK_KeyPressed(SK_PAD_X))
        {
            Z80SingleStep(cpu);
        }

	while(SK_GetBareEvent(&ev))
	{
	    if (ev.pressed)
	    {
                switch(ev.key)
                {
                    case SK_PAD_L:
                        if (addr_type == AddrPC)
                        {
                            addr_type = AddrIY;
                        }
                        else
                        {
                            addr_type--;
                        }
                        break;

                    case SK_PAD_R:
                        addr_type = (addr_type + 1) % NumAddr;
                        break;

                    case SK_PAD_START:
                        done = TRUE;
                        break;

                    case SK_PAD_B:
                        Z80SingleStep(cpu);
                        break;

                    default:
                        break;
                }
	    }
	}
    }
}

static void DebugMenu(Z80 *cpu)
{
    switch(GUI_Menu(debug_menu))
    {
        case DebugMenuEnableOutput:
        {
            char host[32] = {0};
            char port[32] = {0};

            debug_enabled = TRUE;

            GUI_Input("Enter host", host, 31);
            GUI_Input("Enter port", port, 31);

            if (!host[0])
            {
                strcpy(host, "192.168.0.77");
            }

            if (!port[0])
            {
                strcpy(port, "12345");
            }

            DEBUG_SetAddress(host, port);
            break;
        }

        case DebugMenuDisableOutput:
            debug_enabled = FALSE;
            break;

        case DebugMenuDisassembler:
            DebugDisassembler(cpu);
            break;

        case DebugMenuMemoryView:
            DebugMemory(cpu);
            break;

        case DebugMenuCPUInfo:
            DebugCPU(cpu);
            break;
    }
}

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

    gfxInit(GSP_RGB565_OES, GSP_RGB565_OES, false);

    FB_Init();

    z80 = Z80Init(ZX81ReadMem,
		  ZX81WriteMem,
		  ZX81ReadPort,
		  ZX81WritePort,
		  ZX81ReadDisassem);

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

    ZX81Init(z80);

    Splash();

    LoadConfig();
    ZX81Reconfigure();

    SK_DisplayKeyboard();

    SK_SetSticky(SK_SHIFT,DS81_Config[DS81_STICKY_SHIFT]);

    if (DS81_Config[DS81_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();

    	Z80Exec(z80);

        ZX81RenderDisplay(&upper, 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:
				ZX81Reset(z80);
				break;

			    case MenuSelectTape:
			    	SelectTape();
				break;

			    case MenuConfigure:
				GUI_Config();
				SK_SetSticky(SK_SHIFT,
					     DS81_Config[DS81_STICKY_SHIFT]);
				ZX81Reconfigure();
				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 MenuHelp:
                                ShowHelp();
                                break;

                            case MenuDebug:
                                DebugMenu(z80);
                                break;

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

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

    gfxExit();

    return 0;
}