-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinput.c
More file actions
1028 lines (970 loc) · 28.1 KB
/
input.c
File metadata and controls
1028 lines (970 loc) · 28.1 KB
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
/*
* input.c - keyboard, joysticks and mouse emulation
*
* Copyright (C) 2001-2002 Piotr Fusik
* Copyright (C) 2001-2006 Atari800 development team (see DOC/CREDITS)
*
* This file is part of the Atari800 emulator project which emulates
* the Atari 400, 800, 800XL, 130XE, and 5200 8-bit computers.
*
* Atari800 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 2 of the License, or
* (at your option) any later version.
*
* Atari800 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 Atari800; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#include <string.h>
#include <stdlib.h> /* for exit() */
#include "antic.h"
#include "atari.h"
#include "cassette.h"
#include "cpu.h"
#include "gtia.h"
#include "input.h"
#include "akey.h"
#include "log.h"
#include "memory.h"
#include "pia.h"
#include "platform.h"
#include "pokey.h"
#include "util.h"
#ifndef CURSES_BASIC
#include "screen.h" /* for Screen_atari */
#endif
#ifdef __PLUS
#include "input_win.h"
#endif
#ifdef EVENT_RECORDING
#include <zlib.h>
#endif
#ifdef DREAMCAST
extern int Atari_POT(int);
#else
#define Atari_POT(x) 228
#endif
int INPUT_key_code = AKEY_NONE;
int INPUT_key_shift = 0;
int INPUT_key_consol = INPUT_CONSOL_NONE;
int INPUT_joy_autofire[4] = {INPUT_AUTOFIRE_OFF, INPUT_AUTOFIRE_OFF, INPUT_AUTOFIRE_OFF, INPUT_AUTOFIRE_OFF};
int INPUT_joy_block_opposite_directions = 1;
int INPUT_joy_multijoy = 0;
int INPUT_joy_5200_min = 6;
int INPUT_joy_5200_center = 114;
int INPUT_joy_5200_max = 220;
int INPUT_cx85 = 0;
int INPUT_mouse_mode = INPUT_MOUSE_OFF;
int INPUT_mouse_port = 0;
int INPUT_mouse_delta_x = 0;
int INPUT_mouse_delta_y = 0;
int INPUT_mouse_buttons = 0;
int INPUT_mouse_speed = 3;
int INPUT_mouse_pot_min = 1; /* min. value of POKEY's POT register */
int INPUT_mouse_pot_max = 228; /* max. value of POKEY's POT register */
/* There should be UI or options for light pen/gun offsets.
Below are best offsets for different programs:
AtariGraphics: H = 0..32, V = 0 (there's calibration in the program)
Bug Hunt: H = 44, V = 2
Barnyard Blaster: H = 40, V = 0
Operation Blood (light gun version): H = 40, V = 4
*/
int INPUT_mouse_pen_ofs_h = 42;
int INPUT_mouse_pen_ofs_v = 2;
int INPUT_mouse_joy_inertia = 10;
int INPUT_direct_mouse = 0;
#ifndef MOUSE_SHIFT
#define MOUSE_SHIFT 4
#endif
static int mouse_x = 0;
static int mouse_y = 0;
static int mouse_move_x = 0;
static int mouse_move_y = 0;
static int mouse_pen_show_pointer = 0;
static UBYTE mouse_amiga_codes[16] = {
0x00, 0x02, 0x0a, 0x08,
0x01, 0x03, 0x0b, 0x09,
0x05, 0x07, 0x0f, 0x0d,
0x04, 0x06, 0x0e, 0x0c
};
static UBYTE mouse_st_codes[16] = {
0x00, 0x02, 0x03, 0x01,
0x08, 0x0a, 0x0b, 0x09,
0x0c, 0x0e, 0x0f, 0x0d,
0x04, 0x06, 0x07, 0x05
};
static UBYTE STICK[4];
static UBYTE TRIG_input[4];
static int joy_multijoy_no = 0; /* number of selected joy */
static int cx85_port = 1;
static int max_scanline_counter;
static int scanline_counter;
#ifdef EVENT_RECORDING
static gzFile recordfp = NULL; /*output file for input recording*/
static gzFile playbackfp = NULL; /*input file for playback*/
static int recording = FALSE;
static int playingback = FALSE;
static void update_adler32_of_screen(void);
static unsigned int compute_adler32_of_screen(void);
static int recording_version;
#define GZBUFSIZE 256
static char gzbuf[GZBUFSIZE+1];
#define EVENT_RECORDING_VERSION 1
#endif
int INPUT_Initialise(int *argc, char *argv[])
{
int i;
int j;
for (i = j = 1; i < *argc; i++) {
int i_a = (i + 1 < *argc); /* is argument available? */
int a_m = FALSE; /* error, argument missing! */
if (strcmp(argv[i], "-mouse") == 0) {
if (i_a) {
char *mode = argv[++i];
if (strcmp(mode, "off") == 0)
INPUT_mouse_mode = INPUT_MOUSE_OFF;
else if (strcmp(mode, "pad") == 0)
INPUT_mouse_mode = INPUT_MOUSE_PAD;
else if (strcmp(mode, "touch") == 0)
INPUT_mouse_mode = INPUT_MOUSE_TOUCH;
else if (strcmp(mode, "koala") == 0)
INPUT_mouse_mode = INPUT_MOUSE_KOALA;
else if (strcmp(mode, "pen") == 0)
INPUT_mouse_mode = INPUT_MOUSE_PEN;
else if (strcmp(mode, "gun") == 0)
INPUT_mouse_mode = INPUT_MOUSE_GUN;
else if (strcmp(mode, "amiga") == 0)
INPUT_mouse_mode = INPUT_MOUSE_AMIGA;
else if (strcmp(mode, "st") == 0)
INPUT_mouse_mode = INPUT_MOUSE_ST;
else if (strcmp(mode, "trak") == 0)
INPUT_mouse_mode = INPUT_MOUSE_TRAK;
else if (strcmp(mode, "joy") == 0)
INPUT_mouse_mode = INPUT_MOUSE_JOY;
}
else a_m = TRUE;
}
else if (strcmp(argv[i], "-mouseport") == 0) {
if (i_a) {
INPUT_mouse_port = Util_sscandec(argv[++i]) - 1;
if (INPUT_mouse_port < 0 || INPUT_mouse_port > 3) {
Log_print("Invalid mouse port number - should be between 0 and 3");
return FALSE;
}
}
else a_m = TRUE;
}
else if (strcmp(argv[i], "-mousespeed") == 0) {
if (i_a) {
INPUT_mouse_speed = Util_sscandec(argv[++i]);
if (INPUT_mouse_speed < 1 || INPUT_mouse_speed > 9) {
Log_print("Invalid mouse speed - should be between 1 and 9");
return FALSE;
}
}
else a_m = TRUE;
}
else if (strcmp(argv[i], "-multijoy") == 0) {
INPUT_joy_multijoy = 1;
}
#ifdef EVENT_RECORDING
else if (strcmp(argv[i], "-record") == 0) {
if (i_a) {
char *recfilename = argv[++i];
if ((recordfp = gzopen(recfilename, "wb")) == NULL) {
Log_print("Cannot open record file");
return FALSE;
}
else {
recording = TRUE;
gzprintf(recordfp, "Atari800 event recording, version: %d\n", EVENT_RECORDING_VERSION);
}
}
else a_m = TRUE;
}
else if (strcmp(argv[i], "-playback") == 0) {
if (i_a) {
char *pbfilename = argv[++i];
if ((playbackfp = gzopen(pbfilename, "rb")) == NULL) {
Log_print("Cannot open playback file");
return FALSE;
}
else {
playingback = TRUE;
gzgets(playbackfp, gzbuf, GZBUFSIZE);
if (sscanf(gzbuf, "Atari800 event recording, version: %d\n", &recording_version) != 1) {
Log_print("Invalid playback file");
playingback = FALSE;
gzclose(playbackfp);
return FALSE;
}
else if (recording_version > EVENT_RECORDING_VERSION) {
Log_print("Newer version of playback file than this version of Atari800 can handle");
playingback = FALSE;
gzclose(playbackfp);
return FALSE;
}
}
}
else a_m = TRUE;
}
#endif /* EVENT_RECORDING */
else if (strcmp(argv[i], "-directmouse") == 0) {
INPUT_direct_mouse = 1;
}
else if (strcmp(argv[i], "-cx85") == 0) {
if (i_a) {
INPUT_cx85 = 1;
cx85_port = Util_sscandec(argv[++i]) - 1;
if (cx85_port < 0 || cx85_port > 3) {
Log_print("Invalid cx85 port - should be between 0 and 3");
return FALSE;
}
}
else a_m = TRUE;
}
else {
if (strcmp(argv[i], "-help") == 0) {
Log_print("\t-mouse off Do not use mouse");
Log_print("\t-mouse pad Emulate paddles");
Log_print("\t-mouse touch Emulate Atari Touch Tablet");
Log_print("\t-mouse koala Emulate Koala Pad");
Log_print("\t-mouse pen Emulate Light Pen");
Log_print("\t-mouse gun Emulate Light Gun");
Log_print("\t-mouse amiga Emulate Amiga mouse");
Log_print("\t-mouse st Emulate Atari ST mouse");
Log_print("\t-mouse trak Emulate Atari Trak-Ball");
Log_print("\t-mouse joy Emulate joystick using mouse");
Log_print("\t-mouseport <n> Set mouse port 1-4 (default 1)");
Log_print("\t-mousespeed <n> Set mouse speed 1-9 (default 3)");
Log_print("\t-directmouse Use absolute X/Y mouse coords");
Log_print("\t-cx85 <n> Emulate CX85 numeric keypad on port <n>");
Log_print("\t-multijoy Emulate MultiJoy4 interface");
Log_print("\t-record <file> Record input to <file>");
Log_print("\t-playback <file> Playback input from <file>");
}
argv[j++] = argv[i];
}
/* this is the end of the additional argument check */
if (a_m) {
Log_print("Missing argument for '%s'", argv[i]);
return FALSE;
}
}
if(INPUT_direct_mouse && !(
INPUT_mouse_mode == INPUT_MOUSE_PAD ||
INPUT_mouse_mode == INPUT_MOUSE_TOUCH ||
INPUT_mouse_mode == INPUT_MOUSE_KOALA)) {
Log_print("-directmouse only valid with -mouse pad|touch|koala");
return FALSE;
}
INPUT_CenterMousePointer();
*argc = j;
return TRUE;
}
/* For event recording */
void INPUT_Exit(void) {
#ifdef EVENT_RECORDING
if (recording) {
gzclose(recordfp);
recording = FALSE;
}
#endif
}
/* mouse_step is used in Amiga, ST, trak-ball and joystick modes.
It moves mouse_x and mouse_y in the direction given by
mouse_move_x and mouse_move_y.
Bresenham's algorithm is used:
if (abs(deltaX) >= abs(deltaY)) {
if (deltaX == 0)
return;
MoveHorizontally();
e -= abs(deltaY);
if (e < 0) {
e += abs(deltaX);
MoveVertically();
}
}
else {
MoveVertically();
e -= abs(deltaX);
if (e < 0) {
e += abs(deltaY);
MoveHorizontally();
}
}
Returned is stick code for the movement direction.
*/
static UBYTE mouse_step(void)
{
static int e = 0;
UBYTE r = INPUT_STICK_CENTRE;
int dx = mouse_move_x >= 0 ? mouse_move_x : -mouse_move_x;
int dy = mouse_move_y >= 0 ? mouse_move_y : -mouse_move_y;
if (dx >= dy) {
if (mouse_move_x == 0)
return r;
if (mouse_move_x < 0) {
r &= INPUT_STICK_LEFT;
mouse_x--;
mouse_move_x += 1 << MOUSE_SHIFT;
if (mouse_move_x > 0)
mouse_move_x = 0;
}
else {
r &= INPUT_STICK_RIGHT;
mouse_x++;
mouse_move_x -= 1 << MOUSE_SHIFT;
if (mouse_move_x < 0)
mouse_move_x = 0;
}
e -= dy;
if (e < 0) {
e += dx;
if (mouse_move_y < 0) {
r &= INPUT_STICK_FORWARD;
mouse_y--;
mouse_move_y += 1 << MOUSE_SHIFT;
if (mouse_move_y > 0)
mouse_move_y = 0;
}
else {
r &= INPUT_STICK_BACK;
mouse_y++;
mouse_move_y -= 1 << MOUSE_SHIFT;
if (mouse_move_y < 0)
mouse_move_y = 0;
}
}
}
else {
if (mouse_move_y < 0) {
r &= INPUT_STICK_FORWARD;
mouse_y--;
mouse_move_y += 1 << MOUSE_SHIFT;
if (mouse_move_y > 0)
mouse_move_y = 0;
}
else {
r &= INPUT_STICK_BACK;
mouse_y++;
mouse_move_y -= 1 << MOUSE_SHIFT;
if (mouse_move_y < 0)
mouse_move_y = 0;
}
e -= dx;
if (e < 0) {
e += dy;
if (mouse_move_x < 0) {
r &= INPUT_STICK_LEFT;
mouse_x--;
mouse_move_x += 1 << MOUSE_SHIFT;
if (mouse_move_x > 0)
mouse_move_x = 0;
}
else {
r &= INPUT_STICK_RIGHT;
mouse_x++;
mouse_move_x -= 1 << MOUSE_SHIFT;
if (mouse_move_x < 0)
mouse_move_x = 0;
}
}
}
return r;
}
void INPUT_Frame(void)
{
int i;
static int last_key_code = AKEY_NONE;
static int last_key_break = 0;
static UBYTE last_stick[4] = {INPUT_STICK_CENTRE, INPUT_STICK_CENTRE, INPUT_STICK_CENTRE, INPUT_STICK_CENTRE};
static int last_mouse_buttons = 0;
scanline_counter = 10000; /* do nothing in INPUT_Scanline() */
/* handle keyboard */
/* In Atari 5200 joystick there's a second fire button, which acts
like the Shift key in 800/XL/XE (bit 3 in SKSTAT) and generates IRQ
like the Break key (bit 7 in IRQST and IRQEN).
Note that in 5200 the joystick position and first fire button are
separate for each port, but the keypad and 2nd button are common.
That is, if you press a key in the emulator, it's like you really pressed
it in all the controllers simultaneously. Normally the port to read
keypad & 2nd button is selected with the CONSOL register in GTIA
(this is simply not emulated).
INPUT_key_code is used for keypad keys and INPUT_key_shift is used for 2nd button.
*/
#ifdef EVENT_RECORDING
if (playingback) {
gzgets(playbackfp, gzbuf, GZBUFSIZE);
sscanf(gzbuf, "%d %d %d ", &INPUT_key_code, &INPUT_key_shift, &INPUT_key_consol);
}
if (recording) {
gzprintf(recordfp, "%d %d %d \n", INPUT_key_code, INPUT_key_shift, INPUT_key_consol);
}
#endif
i = Atari800_machine_type == Atari800_MACHINE_5200 ? INPUT_key_shift : (INPUT_key_code == AKEY_BREAK);
if (i && !last_key_break) {
if (POKEY_IRQEN & 0x80) {
POKEY_IRQST &= ~0x80;
CPU_GenerateIRQ();
}
}
last_key_break = i;
POKEY_SKSTAT |= 0xc;
if (INPUT_key_shift)
POKEY_SKSTAT &= ~8;
if (INPUT_key_code < 0) {
if (CASSETTE_press_space) {
INPUT_key_code = AKEY_SPACE;
CASSETTE_press_space = 0;
}
else {
last_key_code = AKEY_NONE;
}
}
/* Following keys cannot be read with both shift and control pressed:
J K L ; + * Z X C V B F1 F2 F3 F4 HELP */
/* which are 0x00-0x07 and 0x10-0x17 */
/* This is caused by the keyboard itself, these keys generate 'ghost keys'
* when pressed with shift and control */
if (Atari800_machine_type != Atari800_MACHINE_5200 && (INPUT_key_code&~0x17) == AKEY_SHFTCTRL) {
INPUT_key_code = AKEY_NONE;
}
if (INPUT_key_code >= 0) {
/* The 5200 has only 4 of the 6 keyboard scan lines connected */
/* Pressing one 5200 key is like pressing 4 Atari 800 keys. */
/* The LSB (bit 0) and bit 5 are the two missing lines. */
/* When debounce is enabled, multiple keys pressed generate
* no results. */
/* When debounce is disabled, multiple keys pressed generate
* results only when in numerical sequence. */
/* Thus the LSB being one of the missing lines is important
* because that causes events to be generated. */
/* Two events are generated every 64 scan lines
* but this code only does one every frame. */
/* Bit 5 is different for each keypress because it is one
* of the missing lines. */
if (Atari800_machine_type == Atari800_MACHINE_5200) {
static int bit5_5200 = 0;
if (bit5_5200) {
INPUT_key_code &= ~0x20;
}
bit5_5200 = !bit5_5200;
/* 5200 2nd fire button generates CTRL as well */
if (INPUT_key_shift) {
INPUT_key_code |= AKEY_SHFTCTRL;
}
}
POKEY_SKSTAT &= ~4;
if ((INPUT_key_code ^ last_key_code) & ~AKEY_SHFTCTRL) {
/* ignore if only shift or control has changed its state */
last_key_code = INPUT_key_code;
POKEY_KBCODE = (UBYTE) INPUT_key_code;
if (POKEY_IRQEN & 0x40) {
if (POKEY_IRQST & 0x40) {
POKEY_IRQST &= ~0x40;
CPU_GenerateIRQ();
}
else {
/* keyboard over-run */
POKEY_SKSTAT &= ~0x40;
/* assert(CPU_IRQ != 0); */
}
}
}
}
/* handle joysticks */
#ifdef EVENT_RECORDING
if (playingback) {
gzgets(playbackfp, gzbuf, GZBUFSIZE);
sscanf(gzbuf,"%d ",&i);
} else {
#endif
i = PLATFORM_PORT(0);
#ifdef EVENT_RECORDING
}
if (recording) {
gzprintf(recordfp,"%d \n",i);
}
#endif
STICK[0] = i & 0x0f;
STICK[1] = (i >> 4) & 0x0f;
#ifdef EVENT_RECORDING
if (playingback) {
gzgets(playbackfp, gzbuf, GZBUFSIZE);
sscanf(gzbuf,"%d ",&i);
} else {
#endif
i = PLATFORM_PORT(1);
#ifdef EVENT_RECORDING
}
if (recording) {
gzprintf(recordfp,"%d \n",i);
}
#endif
STICK[2] = i & 0x0f;
STICK[3] = (i >> 4) & 0x0f;
for (i = 0; i < 4; i++) {
if (INPUT_joy_block_opposite_directions) {
if ((STICK[i] & 0x0c) == 0) { /* right and left simultaneously */
if (last_stick[i] & 0x04) /* if wasn't left before, move left */
STICK[i] |= 0x08;
else /* else move right */
STICK[i] |= 0x04;
}
else {
last_stick[i] &= 0x03;
last_stick[i] |= STICK[i] & 0x0c;
}
if ((STICK[i] & 0x03) == 0) { /* up and down simultaneously */
if (last_stick[i] & 0x01) /* if wasn't up before, move up */
STICK[i] |= 0x02;
else /* else move down */
STICK[i] |= 0x01;
}
else {
last_stick[i] &= 0x0c;
last_stick[i] |= STICK[i] & 0x03;
}
}
else
last_stick[i] = STICK[i];
/* Joystick Triggers */
#ifdef EVENT_RECORDING
if(playingback){
int trigtemp;
gzgets(playbackfp, gzbuf, GZBUFSIZE);
sscanf(gzbuf,"%d ",&trigtemp);
TRIG_input[i] = trigtemp;
} else {
#endif
TRIG_input[i] = PLATFORM_TRIG(i);
#ifdef EVENT_RECORDING
}
if(recording){
gzprintf(recordfp,"%d \n",TRIG_input[i]);
}
#endif
if ((INPUT_joy_autofire[i] == INPUT_AUTOFIRE_FIRE && !TRIG_input[i]) || (INPUT_joy_autofire[i] == INPUT_AUTOFIRE_CONT))
TRIG_input[i] = (Atari800_nframes & 2) ? 1 : 0;
}
/* handle analog joysticks in Atari 5200 */
if (Atari800_machine_type != Atari800_MACHINE_5200) {
if(!INPUT_direct_mouse)
for (i = 0; i < 8; i++)
POKEY_POT_input[i] = Atari_POT(i);
}
else {
for (i = 0; i < 4; i++) {
#ifdef DREAMCAST
/* first get analog js data */
POKEY_POT_input[2 * i] = Atari_POT(2 * i); /* x */
POKEY_POT_input[2 * i + 1] = Atari_POT(2 * i + 1); /* y */
if (POKEY_POT_input[2 * i] != INPUT_joy_5200_center
|| POKEY_POT_input[2 * i + 1] != INPUT_joy_5200_center)
continue;
/* if analog js is unused, alternatively try keypad */
#endif
if ((STICK[i] & (INPUT_STICK_CENTRE ^ INPUT_STICK_LEFT)) == 0)
POKEY_POT_input[2 * i] = INPUT_joy_5200_min;
else if ((STICK[i] & (INPUT_STICK_CENTRE ^ INPUT_STICK_RIGHT)) == 0)
POKEY_POT_input[2 * i] = INPUT_joy_5200_max;
else
POKEY_POT_input[2 * i] = INPUT_joy_5200_center;
if ((STICK[i] & (INPUT_STICK_CENTRE ^ INPUT_STICK_FORWARD)) == 0)
POKEY_POT_input[2 * i + 1] = INPUT_joy_5200_min;
else if ((STICK[i] & (INPUT_STICK_CENTRE ^ INPUT_STICK_BACK)) == 0)
POKEY_POT_input[2 * i + 1] = INPUT_joy_5200_max;
else
POKEY_POT_input[2 * i + 1] = INPUT_joy_5200_center;
}
}
/* handle mouse */
#ifdef __PLUS
if (g_Input.ulState & IS_CAPTURE_MOUSE)
#endif
switch (INPUT_mouse_mode) {
case INPUT_MOUSE_PAD:
case INPUT_MOUSE_TOUCH:
case INPUT_MOUSE_KOALA:
if(!INPUT_direct_mouse) {
if (INPUT_mouse_mode != INPUT_MOUSE_PAD || Atari800_machine_type == Atari800_MACHINE_5200)
mouse_x += INPUT_mouse_delta_x * INPUT_mouse_speed;
else
mouse_x -= INPUT_mouse_delta_x * INPUT_mouse_speed;
if (mouse_x < INPUT_mouse_pot_min << MOUSE_SHIFT)
mouse_x = INPUT_mouse_pot_min << MOUSE_SHIFT;
else if (mouse_x > INPUT_mouse_pot_max << MOUSE_SHIFT)
mouse_x = INPUT_mouse_pot_max << MOUSE_SHIFT;
if (INPUT_mouse_mode == INPUT_MOUSE_KOALA || Atari800_machine_type == Atari800_MACHINE_5200)
mouse_y += INPUT_mouse_delta_y * INPUT_mouse_speed;
else
mouse_y -= INPUT_mouse_delta_y * INPUT_mouse_speed;
if (mouse_y < INPUT_mouse_pot_min << MOUSE_SHIFT)
mouse_y = INPUT_mouse_pot_min << MOUSE_SHIFT;
else if (mouse_y > INPUT_mouse_pot_max << MOUSE_SHIFT)
mouse_y = INPUT_mouse_pot_max << MOUSE_SHIFT;
POKEY_POT_input[INPUT_mouse_port << 1] = mouse_x >> MOUSE_SHIFT;
POKEY_POT_input[(INPUT_mouse_port << 1) + 1] = mouse_y >> MOUSE_SHIFT;
}
if (Atari800_machine_type == Atari800_MACHINE_5200) {
if (INPUT_mouse_buttons & 1)
TRIG_input[INPUT_mouse_port] = 0;
if (INPUT_mouse_buttons & 2)
POKEY_SKSTAT &= ~8;
}
else
STICK[INPUT_mouse_port] &= ~(INPUT_mouse_buttons << 2);
break;
case INPUT_MOUSE_PEN:
case INPUT_MOUSE_GUN:
mouse_x += INPUT_mouse_delta_x * INPUT_mouse_speed;
if (mouse_x < 0)
mouse_x = 0;
else if (mouse_x > 167 << MOUSE_SHIFT)
mouse_x = 167 << MOUSE_SHIFT;
mouse_y += INPUT_mouse_delta_y * INPUT_mouse_speed;
if (mouse_y < 0)
mouse_y = 0;
else if (mouse_y > 119 << MOUSE_SHIFT)
mouse_y = 119 << MOUSE_SHIFT;
ANTIC_PENH_input = 44 + INPUT_mouse_pen_ofs_h + (mouse_x >> MOUSE_SHIFT);
ANTIC_PENV_input = 4 + INPUT_mouse_pen_ofs_v + (mouse_y >> MOUSE_SHIFT);
if ((INPUT_mouse_buttons & 1) == (INPUT_mouse_mode == INPUT_MOUSE_PEN))
STICK[INPUT_mouse_port] &= ~1;
if ((INPUT_mouse_buttons & 2) && !(last_mouse_buttons & 2))
mouse_pen_show_pointer = !mouse_pen_show_pointer;
break;
case INPUT_MOUSE_AMIGA:
case INPUT_MOUSE_ST:
case INPUT_MOUSE_TRAK:
mouse_move_x += (INPUT_mouse_delta_x * INPUT_mouse_speed) >> 1;
mouse_move_y += (INPUT_mouse_delta_y * INPUT_mouse_speed) >> 1;
/* i = max(abs(mouse_move_x), abs(mouse_move_y)); */
i = mouse_move_x >= 0 ? mouse_move_x : -mouse_move_x;
if (mouse_move_y > i)
i = mouse_move_y;
else if (-mouse_move_y > i)
i = -mouse_move_y;
{
UBYTE stick = INPUT_STICK_CENTRE;
if (i > 0) {
i += (1 << MOUSE_SHIFT) - 1;
i >>= MOUSE_SHIFT;
if (i > 50)
max_scanline_counter = scanline_counter = 5;
else
max_scanline_counter = scanline_counter = Atari800_tv_mode / i;
stick = mouse_step();
}
if (INPUT_mouse_mode == INPUT_MOUSE_TRAK) {
/* bit 3 toggles - vertical movement, bit 2 = 0 - up */
/* bit 1 toggles - horizontal movement, bit 0 = 0 - left */
STICK[INPUT_mouse_port] = ((mouse_y & 1) << 3) | ((stick & 1) << 2)
| ((mouse_x & 1) << 1) | ((stick & 4) >> 2);
}
else {
STICK[INPUT_mouse_port] = (INPUT_mouse_mode == INPUT_MOUSE_AMIGA ? mouse_amiga_codes : mouse_st_codes)
[(mouse_y & 3) * 4 + (mouse_x & 3)];
}
}
if (INPUT_mouse_buttons & 1)
TRIG_input[INPUT_mouse_port] = 0;
if (INPUT_mouse_buttons & 2)
POKEY_POT_input[INPUT_mouse_port << 1] = 1;
if (INPUT_mouse_buttons & 4)
POKEY_POT_input[(INPUT_mouse_port << 1) + 1] = 1;
break;
case INPUT_MOUSE_JOY:
if (Atari800_machine_type == Atari800_MACHINE_5200) {
/* 2 * INPUT_mouse_speed is ok for Super Breakout, for Ballblazer you need more */
int val = INPUT_joy_5200_center + ((INPUT_mouse_delta_x * 2 * INPUT_mouse_speed) >> MOUSE_SHIFT);
if (val < INPUT_joy_5200_min)
val = INPUT_joy_5200_min;
else if (val > INPUT_joy_5200_max)
val = INPUT_joy_5200_max;
POKEY_POT_input[INPUT_mouse_port << 1] = val;
val = INPUT_joy_5200_center + ((INPUT_mouse_delta_y * 2 * INPUT_mouse_speed) >> MOUSE_SHIFT);
if (val < INPUT_joy_5200_min)
val = INPUT_joy_5200_min;
else if (val > INPUT_joy_5200_max)
val = INPUT_joy_5200_max;
POKEY_POT_input[(INPUT_mouse_port << 1) + 1] = val;
if (INPUT_mouse_buttons & 2)
POKEY_SKSTAT &= ~8;
}
else {
mouse_move_x += INPUT_mouse_delta_x * INPUT_mouse_speed;
mouse_move_y += INPUT_mouse_delta_y * INPUT_mouse_speed;
if (mouse_move_x < -INPUT_mouse_joy_inertia << MOUSE_SHIFT ||
mouse_move_x > INPUT_mouse_joy_inertia << MOUSE_SHIFT ||
mouse_move_y < -INPUT_mouse_joy_inertia << MOUSE_SHIFT ||
mouse_move_y > INPUT_mouse_joy_inertia << MOUSE_SHIFT) {
mouse_move_x >>= 1;
mouse_move_y >>= 1;
}
STICK[INPUT_mouse_port] &= mouse_step();
}
if (INPUT_mouse_buttons & 1)
TRIG_input[INPUT_mouse_port] = 0;
break;
default:
break;
}
last_mouse_buttons = INPUT_mouse_buttons;
/* CX85 numeric keypad */
if (INPUT_key_code <= AKEY_CX85_1 && INPUT_key_code >= AKEY_CX85_YES) {
int val = 0;
switch (INPUT_key_code) {
case AKEY_CX85_1:
val = 0x19;
break;
case AKEY_CX85_2:
val = 0x1a;
break;
case AKEY_CX85_3:
val = 0x1b;
break;
case AKEY_CX85_4:
val = 0x11;
break;
case AKEY_CX85_5:
val = 0x12;
break;
case AKEY_CX85_6:
val = 0x13;
break;
case AKEY_CX85_7:
val = 0x15;
break;
case AKEY_CX85_8:
val = 0x16;
break;
case AKEY_CX85_9:
val = 0x17;
break;
case AKEY_CX85_0:
val = 0x1c;
break;
case AKEY_CX85_PERIOD:
val = 0x1d;
break;
case AKEY_CX85_MINUS:
val = 0x1f;
break;
case AKEY_CX85_PLUS_ENTER:
val = 0x1e;
break;
case AKEY_CX85_ESCAPE:
val = 0x0c;
break;
case AKEY_CX85_NO:
val = 0x14;
break;
case AKEY_CX85_DELETE:
val = 0x10;
break;
case AKEY_CX85_YES:
val = 0x18;
break;
}
if (val > 0) {
STICK[cx85_port] = (val&0x0f);
POKEY_POT_input[cx85_port*2+1] = ((val&0x10) ? 0 : 228);
TRIG_input[cx85_port] = 0;
}
}
if (INPUT_joy_multijoy && Atari800_machine_type != Atari800_MACHINE_5200) {
PIA_PORT_input[0] = 0xf0 | STICK[joy_multijoy_no];
PIA_PORT_input[1] = 0xff;
GTIA_TRIG[0] = TRIG_input[joy_multijoy_no];
GTIA_TRIG[2] = GTIA_TRIG[1] = 1;
GTIA_TRIG[3] = Atari800_machine_type == Atari800_MACHINE_XLXE ? MEMORY_cartA0BF_enabled : 1;
}
else {
GTIA_TRIG[0] = TRIG_input[0];
GTIA_TRIG[1] = TRIG_input[1];
if (Atari800_machine_type == Atari800_MACHINE_XLXE) {
GTIA_TRIG[2] = 1;
GTIA_TRIG[3] = MEMORY_cartA0BF_enabled;
}
else {
GTIA_TRIG[2] = TRIG_input[2];
GTIA_TRIG[3] = TRIG_input[3];
}
PIA_PORT_input[0] = (STICK[1] << 4) | STICK[0];
PIA_PORT_input[1] = (STICK[3] << 4) | STICK[2];
}
#ifdef EVENT_RECORDING
update_adler32_of_screen();
#endif
}
#ifdef EVENT_RECORDING
static void update_adler32_of_screen(void)
{
unsigned int adler32val = 0;
static unsigned int adler32_errors = 0;
static int first = TRUE;
if (first) { /* don't calculate the first frame */
first = FALSE;
adler32val = 0;
}
else if (recording || playingback){
adler32val = compute_adler32_of_screen();
}
if (recording) {
gzprintf(recordfp, "%08X \n", adler32val);
}
if (playingback) {
unsigned int pb_adler32val;
gzgets(playbackfp, gzbuf, GZBUFSIZE);
sscanf(gzbuf, "%08X ", &pb_adler32val);
if (pb_adler32val != adler32val){
Log_print("adler32 does not match");
adler32_errors++;
}
}
if (playingback && gzeof(playbackfp)) {
playingback = FALSE;
gzclose(playbackfp);
Atari800_Exit(FALSE);
exit(adler32_errors > 0 ? 1 : 0); /* return code indicates errors*/
}
}
/* Compute the adler32 value of the visible screen */
/* Note that the visible portion is 24..360 on the horizontal and */
/* 0..Screen_HEIGHT on the vertical */
static unsigned int compute_adler32_of_screen(void)
{
int y;
unsigned int adler = adler32(0L,Z_NULL,0);
for (y = 0; y < Screen_HEIGHT; y++) {
adler = adler32(adler, (unsigned char*)Screen_atari + 24 + Screen_WIDTH*y, 360 - 24);
}
return adler;
}
#endif /* EVENT_RECORDING */
int INPUT_Recording(void)
{
#ifdef EVENT_RECORDING
return recording;
#else
return 0;
#endif
}
int INPUT_Playingback(void)
{
#ifdef EVENT_RECORDING
return playingback;
#else
return 0;
#endif
}
void INPUT_RecordInt(int i)
{
#ifdef EVENT_RECORDING
if (recording) gzprintf(recordfp, "%d\n", i);
#endif
}
int INPUT_PlaybackInt(void)
{
#ifdef EVENT_RECORDING
int i;
if (playingback) {
gzgets(playbackfp, gzbuf, GZBUFSIZE);
sscanf(gzbuf, "%d", &i);
}
return i;
#else
return 0;
#endif
}
void INPUT_Scanline(void)
{
if (--scanline_counter == 0) {
UBYTE stick = mouse_step();
if (INPUT_mouse_mode == INPUT_MOUSE_TRAK) {
/* bit 3 toggles - vertical movement, bit 2 = 0 - up */
/* bit 1 toggles - horizontal movement, bit 0 = 0 - left */
STICK[INPUT_mouse_port] = ((mouse_y & 1) << 3) | ((stick & 1) << 2)
| ((mouse_x & 1) << 1) | ((stick & 4) >> 2);
}
else {
STICK[INPUT_mouse_port] = (INPUT_mouse_mode == INPUT_MOUSE_AMIGA ? mouse_amiga_codes : mouse_st_codes)
[(mouse_y & 3) * 4 + (mouse_x & 3)];
}
PIA_PORT_input[0] = (STICK[1] << 4) | STICK[0];
PIA_PORT_input[1] = (STICK[3] << 4) | STICK[2];
scanline_counter = max_scanline_counter;
}
}
void INPUT_SelectMultiJoy(int no)
{
no &= 3;
joy_multijoy_no = no;
if (INPUT_joy_multijoy && Atari800_machine_type != Atari800_MACHINE_5200) {
PIA_PORT_input[0] = 0xf0 | STICK[no];
GTIA_TRIG[0] = TRIG_input[no];
}
}
void INPUT_CenterMousePointer(void)
{
switch (INPUT_mouse_mode) {
case INPUT_MOUSE_PAD:
case INPUT_MOUSE_TOUCH:
case INPUT_MOUSE_KOALA:
mouse_x = 114 << MOUSE_SHIFT;
mouse_y = 114 << MOUSE_SHIFT;
break;
case INPUT_MOUSE_PEN:
case INPUT_MOUSE_GUN:
mouse_x = 84 << MOUSE_SHIFT;
mouse_y = 60 << MOUSE_SHIFT;
break;
case INPUT_MOUSE_AMIGA:
case INPUT_MOUSE_ST:
case INPUT_MOUSE_TRAK:
case INPUT_MOUSE_JOY:
mouse_move_x = 0;
mouse_move_y = 0;
break;
}
}
#ifndef CURSES_BASIC
#define PLOT(dx, dy) do {\
ptr[(dx) + Screen_WIDTH * (dy)] ^= 0x0f0f;\
ptr[(dx) + Screen_WIDTH * (dy) + Screen_WIDTH / 2] ^= 0x0f0f;\
} while (0)