-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.c
2181 lines (1992 loc) · 49.7 KB
/
main.c
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
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2022, 2023 S. V. Nickolas.
* Copyright 2023 Marcin Wołoszczuk.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following condition: The
* above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
/*
* Note: Some parts of this program have other copyrights. See the individual
* files for specific copyright information. SDL2 has different but
* similar license terms.
*/
#define VERSION "0.26e"
/* C99 includes */
#include <errno.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
/*
* MS-DOS (DJGPP):
* Use BIOS to access hardware, redirected through DPMI
* XXX: we need to completely disable sigint, sigquit etc. in DJGPP
*
* Otherwise:
* Use SDL for hardware abstraction
*/
#ifdef __MSDOS__
#include <dpmi.h>
#include <pc.h>
#include <sys/nearptr.h>
#define diag_printf(...)
#else
/* SDL2 include */
#include <SDL.h>
/* XXX: and for OSX we use...? */
#if (!defined(_WIN32))&&(!defined(__APPLE__))
#include <gtk/gtk.h>
#endif
#define diag_printf printf
#endif
#ifdef _WIN32
#include <windows.h>
#endif
/* Chipset includes */
#include "tms9918.h"
#include "tms_util.h"
#include "emu2149.h"
#include "z80.h"
/* FDC include */
#include "disk.h"
/* Cable modem include */
#include "modem.h"
/* Alterable filenames */
#include "paths.h"
/*
* Forward declarations.
*/
static void reinit_cpu(void);
void fatal_diag(int, char *);
/* Extern declaration */
void cpustatus (z80 *cpu);
int trace;
/*
* Speed control.
*
* Not exact, but it helps keep the speed more or less even based on how long
* it takes to run one scanline's worth of code.
*
* XXX: no support for this on MS-DOS.
*/
#ifdef __MSDOS__
#else
#define FIRE_TICK 63492
unsigned long long next_fire;
struct timespec timespec;
#endif
#ifdef _WIN32
LARGE_INTEGER currenttime,throttlerate;
long long wantedtime, looptimedesired;
#endif
/*
* The NABU has 64K RAM.
*
* Early models have 4K ROM, there is also a version with 8K ROM.
*/
static uint8_t RAM[65536], ROM[8192];
int romsize;
/*
* Internal state for emulated chips.
*
* This means both chips with external cores and internal cores.
*/
z80 cpu;
VrEmuTms9918 *vdp;
PSG *psg;
int ctrlreg;
int gotmodem;
unsigned dog_speed;
/* Next cycle for scanline loop. */
unsigned long next;
/* Set to nonzero to tell the emulator to exit. */
int death_flag;
/* To kick the dog */
unsigned next_watchdog;
/* keyboard/joystick? */
int keyjoy;
uint8_t joybyte;
#define JOY_THRESH 2048 /* distance from center to "trip"; 0..32767 */
int dojoy;
void add_gamecontroller(int joystick_index);
FILE *lpt;
uint8_t lpt_data;
#ifdef __MSDOS__
/*
* display is an offscreen buffer which is blitted to the screen every frame.
* vgamem is a pointer to 0x000A0000 absolute, translated through the DJGPP
* extender.
*/
uint8_t *display, *vgamem;
int ttyup;
#else
/*
* SDL2 structure pointers.
*
* display is a dynamically allocated offscreen buffer used for rendering the
* VDP data, or the generated NTSC noise.
*/
SDL_Window *screen;
SDL_Renderer *renderer;
SDL_Texture *texture;
SDL_AudioDeviceID audio_device;
SDL_AudioSpec audio_spec;
SDL_GameController *pad;
SDL_Joystick *joystick;
uint32_t *display;
#endif
int psg_calc_flag = 0;
/*
* Emulation of the NABU memory map.
*
* Unlike 6502 and 68000 systems, but like x86, the Z80 has two separate
* memory maps - one for RAM and ROM, and one for I/O devices. This makes it
* much easier to interface a full 64K of RAM to a Z80, where double-banking
* is absolutely necessary to do that on a 6502 (witness the double Dxxx on an
* Apple ][).
*
* Emulation of basic memory reads and writes is mindlessly simple; if the ROM
* is banked in, it overrides reads from RAM - otherwise operate on RAM. The
* entire memory map is otherwise filled with RAM.
*
* What the following functions do should be self-evident. The void pointers
* are unused but required by the CPU core.
*/
uint8_t mem_read(void *blob, uint16_t addr)
{
if ((!(ctrlreg & 0x01)) && (addr < romsize))
return ROM[addr];
return RAM[addr];
}
void mem_write(void *blob, uint16_t addr, uint8_t val)
{
RAM[addr] = val;
}
/*
* Emulation of port I/O is a lot more complicated, and handled by the
* following two functions.
*
* As with the above, the CPU core passes an unnecessary (but potentially
* useful on other architectures) pointer to the CPU struct; it is not used.
*/
/*
* 00 - control register (write)
* 40 - AY-8910 data port
* 41 - AY-8910 latch (?)
* The PSG ports are BACKWARD from other systems! Or at least from the
* MSX and the Arcade Board.
* 80 - cable modem
* 90 - keyboard (mostly ASCII)
* 91 - keyboard strobe (also written to, not sure what for yet)
* This should call an interrupt but not sure how interrupts are supposed
* to work because I'm a 65C02 person, not a Z80 person.
* A0 - TMS9918 read/write data
* A1 - TMS9918 write control register
* B0 - parallel port data
*
* Control reg:
* 01 - ROM disable
* 02 - enable video
* 04 - parallel port strobe
* 08 - green (check) LED
* 10 - red (alert) LED
* 20 - yellow (pause) LED
*
* The keyboard sends 0x95 when powering up.
* Every so often (~3.7 sec.) it should send 0x94 to kick the dog.
*
* https://vintagecomputer.ca/files/Nabu/
* Nabu_Computer_Technical_Manual_by_MJP-compressed.pdf
*/
/* priority encoder */
void int_prio_enc(int EI, int I0, int I1, int I2, int I3, int I4, int I5, int I6, int I7,
int *GS, int *Q0, int *Q1, int *Q2, int *EO)
{
/* most often and default values, ease the typing later on ;) */
*GS = 0;
*EO = 1;
*Q0 = *Q1 = *Q2 = 0;
if (EI == 1)
{
*GS = 1;
*Q0 = 1;
*Q1 = 1;
*Q2 = 1;
}
else if (I0 & I1 & I2 & I3 & I4 & I5 & I6 & I7)
{
*GS = *Q0 = *Q1 = *Q2 = 1;
*EO = 0;
}
else if (!I7)
{ /* nop */
}
else if (!I6)
{
*Q0 = 1;
}
else if (!I5)
{
*Q1 = 1;
}
else if (!I4)
{
*Q0 = *Q1 = 1;
}
else if (!I3)
{
*Q2 = 1;
}
else if (!I2)
{
*Q0 = *Q2 = 1;
}
else if (!I1)
{
*Q1 = *Q2 = 1;
}
else
{
*Q0 = *Q1 = *Q2 = 1;
}
}
/* basically a macro to accept an interrupt vector instead of single bits */
void int_prio_enc_alt(int EI, int interrupts, int *GS, int *Q0,
int *Q1, int *Q2, int *EO)
{
int_prio_enc(EI, interrupts & 0x01, (interrupts & 0x02) >> 1,
(interrupts & 0x04) >> 2, (interrupts & 0x08) >> 3,
(interrupts & 0x10) >> 4, (interrupts & 0x20) >> 5,
(interrupts & 0x40) >> 6, (interrupts & 0x80) >> 7,
GS, Q0, Q1, Q2, EO);
}
/* fed into PSG's PORTB via PSG_writeReg,
since NABU never writes to PORTB, this should be safe */
uint8_t psg_portb = 0;
/* keep the current PSG's PORTA in this scope */
uint8_t psg_porta = 0;
/* interrupt variables */
uint8_t hccarint = 0;
uint8_t hccatint = 0;
uint8_t keybdint = 0;
uint8_t vdpint = 0;
uint8_t interrupts = 0;
uint8_t prev_int_line = 0;
void update_interrupts()
{
if (hccarint)
{
interrupts |= 0x80;
}
else
{
interrupts &= ~0x80;
}
if (hccatint)
{
interrupts |= 0x40;
}
else
{
interrupts &= ~0x40;
}
if (keybdint)
{
interrupts |= 0x20;
}
else
{
interrupts &= ~0x20;
}
if (vdpint)
{
interrupts |= 0x10;
}
else
{
interrupts &= ~0x10;
}
int int_prio = ~(interrupts & psg_porta);
int GS, Q0, Q1, Q2, EO;
int_prio_enc_alt(0, int_prio, &GS, &Q0, &Q1, &Q2, &EO);
psg_portb &= 0xf0;
psg_portb |= EO | (Q0 << 1) | (Q1 << 2) | (Q2 << 3);
PSG_writeReg(psg, 15, psg_portb);
/*
A0 - D7
A1 - D2
A2 - D8
*/
z80_gen_int(&cpu, !GS, psg_portb & 0x0e);
}
char keyboard_buffer[256];
uint8_t keyboard_buffer_write_ptr = 0;
uint8_t keyboard_buffer_read_ptr = 0;
void keyboard_buffer_put(uint8_t code)
{
keyboard_buffer[keyboard_buffer_write_ptr++] = code;
}
int keyboard_buffer_empty()
{
if (keyboard_buffer_read_ptr == keyboard_buffer_write_ptr)
{
return 1;
}
return 0;
}
uint8_t keyboard_buffer_get()
{
if (keyboard_buffer_read_ptr != keyboard_buffer_write_ptr)
{
return keyboard_buffer[keyboard_buffer_read_ptr++];
}
return 255;
}
/* used for latching PSG's register address */
uint8_t psg_reg_address = 0x00;
uint8_t port_read(z80 *mycpu, uint8_t port)
{
uint8_t t, b;
if ((port&0xF0)==0xC0) return disksys_read(port);
switch (port)
{
case 0x40: /* read register from PSG */
t = PSG_readReg(psg, psg_reg_address);
return t;
case 0x41:
fatal_diag(-1, "IO read from 0x41, this shouldn't happen, exiting!");
return 0;
case 0x80:
if (gotmodem)
{
t = modem_read(&b);
if (t)
{
hccarint = 0;
update_interrupts();
return b;
}
}
return 0;
case 0x90: /* Not sure if this is the right action */
t = keyboard_buffer_get();
keybdint = 0;
update_interrupts();
if (t == 255)
return 0;
else
return t;
case 0x91: /* Not sure if this is the right action */
return keyboard_buffer_empty() ? 0x00 : 0xff;
case 0xA0:
return vrEmuTms9918ReadData(vdp);
case 0xA1: /* Not sure if this is the right action */
b = vrEmuTms9918ReadStatus(vdp);
vdpint = 0;
update_interrupts();
return b;
default:
#ifdef PORT_DEBUG
printf("WARNING: unknown port read (0x%02X)\n", port);
#endif
return 0;
}
}
void port_write(z80 *mycpu, uint8_t port, uint8_t val)
{
uint8_t psg_reg7;
if ((port&0xF0)==0xC0) disksys_write(port, val);
switch (port)
{
case 0x00:
if ((val&0x04)&&(!(ctrlreg&0x04))&&(lpt)) fputc(lpt_data, lpt);
ctrlreg = val;
return;
case 0x40: /* write data to PSG */
psg_reg7 = PSG_readReg(psg, 7);
if (psg_reg_address == 0x0E)
{
if (!(psg_reg7 & 0x40))
{
diag_printf("Writing to PORTA when it's set to input, DENIED!\r\n");
diag_printf("psg_reg7 = %02X\r\n", psg_reg7);
}
if (val & 0x10)
{
}
if (psg_porta != val)
{
psg_porta = val;
update_interrupts();
}
}
if (psg_reg_address == 0x0F)
{
if (!(psg_reg7 & 0x80))
{
diag_printf("Writing to PORTB when it's set to input, DENIED!\r\n");
diag_printf("psg_reg7 = %02X\r\n", psg_reg7);
}
}
PSG_writeReg(psg, psg_reg_address, val);
return;
case 0x41: /* write address to PSG */
if (val > 0x1f)
{
fatal_diag(-1, "PSG reg address > 0x1f when writing, exiting!");
}
psg_reg_address = val;
return;
case 0x80:
if (gotmodem)
{
modem_write(val);
}
return;
case 0xA0:
vrEmuTms9918WriteData(vdp, val);
return;
case 0xA1:
vrEmuTms9918WriteAddr(vdp, val);
return;
case 0xB0:
if (lpt) lpt_data=val;
return;
#ifdef DEBUG
case 0xBF: /* debug port */
trace=val;
return;
#endif
#ifdef PORT_DEBUG
default:
printf("WARNING: unknown port write (0x%02X): 0x%02X\n", port, val);
#endif
}
}
#ifdef __MSDOS__
/*
* This is a cruder version of the SDL code found below. Many comments from
* that version will still be relevant here.
*
* XXX: We need a better way to do this to detect make and break from the
* relevant keys. Like, say, grabbing INT9. (But I'm not familiar with
* that kind of stuff in 386 mode... -uso.)
*/
void keyboard_poll(void)
{
__dpmi_regs regs;
uint16_t k;
/* Key in the buffer? If not, do nothing */
regs.h.ah=0x01;
__dpmi_int(0x16, ®s);
if (regs.x.flags&0x40) return; /* Z */
regs.h.ah=0x00;
__dpmi_int(0x16, ®s);
k=regs.x.ax;
if (keyjoy&&((k&0xFF)==0x20))
{
}
if (k==0x0E08) /* BkSp */
{
keyboard_buffer_put(0x7F);
return;
}
/* Plain, ordinary, ASCII */
if ((k&0xFF)&&(!(k&0x80)))
{
keyboard_buffer_put(k&0x7F);
return;
}
switch (k>>8)
{
case 0x48: /* up */
if (keyjoy)
{
}
else
{
keyboard_buffer_put(0xE2);
keyboard_buffer_put(0xF2);
}
break;
case 0x50: /* down */
if (keyjoy)
{
}
else
{
keyboard_buffer_put(0xE3);
keyboard_buffer_put(0xF3);
}
break;
case 0x4B: /* left */
if (keyjoy)
{
}
else
{
keyboard_buffer_put(0xE1);
keyboard_buffer_put(0xF1);
}
break;
case 0x4D: /* right */
if (keyjoy)
{
}
else
{
keyboard_buffer_put(0xE0);
keyboard_buffer_put(0xF0);
}
break;
case 0x49: /* PgUp */
keyboard_buffer_put(0xE5);
keyboard_buffer_put(0xF5);
break;
case 0x51: /* PgDn */
keyboard_buffer_put(0xE4);
keyboard_buffer_put(0xF4);
break;
case 0x52: /* Ins */
keyboard_buffer_put(0xE7);
keyboard_buffer_put(0xF7);
break;
case 0x53: /* Del */
keyboard_buffer_put(0xE6);
keyboard_buffer_put(0xF6);
break;
case 0x4F: /* End */
keyboard_buffer_put(0xEA);
keyboard_buffer_put(0xFA);
break;
/* Special Keys */
case 0x3D: /* F3 */
#if 0 /* DJGPP doesn't have this */
clock_gettime(CLOCK_REALTIME, ×pec);
next_fire = timespec.tv_nsec + FIRE_TICK;
#endif
reinit_cpu();
break;
case 0x40: /* F6 */
keyjoy=!keyjoy;
break;
}
if (k==0x4400) death_flag=1;
}
#else
void add_gamecontroller(int joystick_index)
{
if (joystick != NULL)
return;
printf("Controller %d added: %s\n", joystick_index, SDL_JoystickNameForIndex(joystick_index));
pad=SDL_GameControllerOpen(joystick_index);
joystick=SDL_GameControllerGetJoystick(pad);
SDL_JoystickEventState(SDL_ENABLE);
SDL_GameControllerEventState(SDL_ENABLE);
}
void remove_gamecontroller(int joystick_index, bool last)
{
printf("Controller %d removed\n", joystick_index);
SDL_JoystickClose(joystick);
SDL_GameControllerClose(pad);
joystick = NULL;
if (last)
{
/* .... No more rainbows for us to chase .... */
/* .... No more time to playyyyyyyyyyyyy .... */
SDL_GameControllerEventState(SDL_DISABLE);
SDL_JoystickEventState(SDL_DISABLE);
}
}
void send_joybyte() {
keyboard_buffer_put(0x80);
keyboard_buffer_put(joybyte|0xA0);
}
void keyboard_poll(void)
{
SDL_Event event;
/* eat up all events */
while (SDL_PollEvent(&event))
{
/* These are irrelevant if the keyboard is emulating the joystick */
if (!keyjoy)
{
/* Don't care what stick or what button. Nabu only has one. */
switch (event.type)
{
/* All new controller fancy: */
case SDL_CONTROLLERDEVICEADDED:
add_gamecontroller(event.jdevice.which);
break;
case SDL_CONTROLLERDEVICEREMOVED:
remove_gamecontroller(event.jdevice.which, SDL_NumJoysticks()==0);
break;
case SDL_JOYBUTTONDOWN:
joybyte|=0x10;
send_joybyte();
break;
case SDL_JOYBUTTONUP:
joybyte&=0xEF;
send_joybyte();
break;
case SDL_JOYHATMOTION:
joybyte&=0xF0;
switch (event.jhat.value)
{
case SDL_HAT_LEFTUP:
joybyte|=0x09;
break;
case SDL_HAT_UP:
joybyte|=0x08;
break;
case SDL_HAT_RIGHTUP:
joybyte|=0x0C;
break;
case SDL_HAT_LEFT:
joybyte|=0x01;
break;
case SDL_HAT_CENTERED:
/* Already done */
break;
case SDL_HAT_RIGHT:
joybyte|=0x04;
break;
case SDL_HAT_LEFTDOWN:
joybyte|=0x03;
break;
case SDL_HAT_DOWN:
joybyte|=0x02;
break;
case SDL_HAT_RIGHTDOWN:
joybyte|=0x06;
break;
}
send_joybyte();
break;
case SDL_JOYAXISMOTION:
joybyte&=0xF0;
switch (event.caxis.axis)
{
case 0: /* X */
if (event.jaxis.value<-JOY_THRESH)
joybyte|=0x01;
else if (event.jaxis.value>JOY_THRESH)
joybyte|=0x04;
break;
case 1: /* Y */
if (event.jaxis.value<-JOY_THRESH)
joybyte|=0x08;
else if (event.jaxis.value>JOY_THRESH)
joybyte|=0x02;
break;
}
send_joybyte();
break;
/* END new controller fancy */
}
}
switch (event.type)
{
/*
* "Break key" codes for arrows.
*/
case SDL_KEYUP:
switch (event.key.keysym.sym)
{
case SDLK_LALT: /* Alt for Sym */
case SDLK_RALT:
keyboard_buffer_put(0xF8);
break;
case ' ':
if (keyjoy)
{
joybyte &= 0xEF;
keyboard_buffer_put(0x80);
keyboard_buffer_put(joybyte|0xA0);
}
break;
case SDLK_UP:
if (keyjoy)
{
joybyte &= 0xF7;
keyboard_buffer_put(0x80);
keyboard_buffer_put(joybyte|0xA0);
}
else
keyboard_buffer_put(0xF2);
break;
case SDLK_DOWN:
if (keyjoy)
{
joybyte &= 0xFD;
keyboard_buffer_put(0x80);
keyboard_buffer_put(joybyte|0xA0);
}
else
keyboard_buffer_put(0xF3);
break;
case SDLK_LEFT:
if (keyjoy)
{
joybyte &= 0xFE;
keyboard_buffer_put(0x80);
keyboard_buffer_put(joybyte|0xA0);
}
else
keyboard_buffer_put(0xF1);
break;
case SDLK_RIGHT:
if (keyjoy)
{
joybyte &= 0xFB;
keyboard_buffer_put(0x80);
keyboard_buffer_put(joybyte|0xA0);
}
else
keyboard_buffer_put(0xF0);
break;
case SDLK_PAGEUP: /* « */
keyboard_buffer_put(0xF5);
break;
case SDLK_PAGEDOWN: /* » */
keyboard_buffer_put(0xF4);
break;
case SDLK_INSERT: /* YES */
keyboard_buffer_put(0xF7);
break;
case SDLK_DELETE: /* NO */
keyboard_buffer_put(0xF6);
break;
case SDLK_PAUSE:
keyboard_buffer_put(0xF9);
break;
case SDLK_END:
keyboard_buffer_put(0xFA);
break;
}
break;
case SDL_KEYDOWN:
/*
* "Make key" codes for arrows.
*/
switch (event.key.keysym.sym)
{
case SDLK_LALT: /* Alt for Sym */
case SDLK_RALT:
keyboard_buffer_put(0xE8);
break;
case ' ':
if (keyjoy)
{
joybyte |= 0x10;
keyboard_buffer_put(0x80);
keyboard_buffer_put(joybyte|0xA0);
}
break;
case SDLK_UP:
if (keyjoy)
{
joybyte |= 0x08;
keyboard_buffer_put(0x80);
keyboard_buffer_put(joybyte|0xA0);
}
else
keyboard_buffer_put(0xE2);
break;
case SDLK_DOWN:
if (keyjoy)
{
joybyte |= 0x02;
keyboard_buffer_put(0x80);
keyboard_buffer_put(joybyte|0xA0);
}
else
keyboard_buffer_put(0xE3);
break;
case SDLK_LEFT:
if (keyjoy)
{
joybyte |= 0x01;
keyboard_buffer_put(0x80);
keyboard_buffer_put(joybyte|0xA0);
}
else
keyboard_buffer_put(0xE1);
break;
case SDLK_RIGHT:
if (keyjoy)
{
joybyte |= 0x04;
keyboard_buffer_put(0x80);
keyboard_buffer_put(joybyte|0xA0);
}
else
keyboard_buffer_put(0xE0);
break;
case SDLK_PAGEUP: /* « */
keyboard_buffer_put(0xE5);
break;
case SDLK_PAGEDOWN: /* » */
keyboard_buffer_put(0xE4);
break;
case SDLK_INSERT: /* YES */
keyboard_buffer_put(0xE7);
break;
case SDLK_DELETE: /* NO */
keyboard_buffer_put(0xE6);
break;
case SDLK_PAUSE:
keyboard_buffer_put(0xE9);
break;
case SDLK_END:
keyboard_buffer_put(0xEA);
break;
case SDLK_BACKSPACE:
keyboard_buffer_put(0x7F);
break;
}
if ((event.key.keysym.sym < 128) &&
(event.key.keysym.sym != SDLK_BACKSPACE)) /* urk */
{
static char *shiftnums = ")!@#$%^&*(";
SDL_Keymod m;
int k;
/*
* Shift/Ctrl translation. Not the most efficient method.
*
* The NABU keyboard looks like a standard modern ASCII layout (not the
* strict ASCII layout used by some older computers). Caps Lock is off
* by default.
*
* The NABU keyboard doesn't actually have a |\ key, but we'll just
* forget that for now.
*/
k = event.key.keysym.sym;
if ((k==' ')&&keyjoy) break; /* we already handled this. */
m = SDL_GetModState();
if (m & KMOD_CTRL)
{
if (k == '[')
k = 0x1B;
if (k == '\\')
k = 0x1C;
if (k == ']')
k = 0x1D;
if (k == '-')
k = 0x1F;
}
if (m & KMOD_SHIFT)
{
if (k == '`')
k = '~';
if (k == '-')
k = '_';
if (k == '=')
k = '+';
if (k == '[')
k = '{';
if (k == ']')
k = '}';
if (k == '\\')
k = '|';
if (k == ';')
k = ':';
if (k == '\'')
k = '"';
if (k == ',')
k = '<';
if (k == '.')
k = '>';
if (k == '/')
k = '?';
}
if ((k >= 'a') && (k <= 'z'))
{
if (m & KMOD_CAPS)