forked from randyrossi/bmc64
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernel.cpp
1708 lines (1507 loc) · 52.1 KB
/
kernel.cpp
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
//
// kernel.cpp
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "kernel.h"
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <circle/gpiopin.h>
CKernel *static_kernel = NULL;
#define MAX_KEY_CODES 128
#define TICKS_PER_SECOND 1000000L
// A global to control whether our special VICE CIA port changes
// should take effect. Only set when gpio_outputs_enabled is allowed.
int raspi_userport_enabled;
// Usb key states
static bool key_states[MAX_KEY_CODES];
static unsigned char mod_states;
static bool uiLeftShift = false;
static bool uiRightShift = false;
static int vol_percent_to_vchiq(int percent) {
int range = VCHIQ_SOUND_VOLUME_MAX-(-2720);
return range * ((float)percent)/100.0 + (-2720);
}
// Real keyboard matrix states
static bool kbdMatrixStates[8][8];
// These for translating row/col scans into equivalent keycodes.
#if defined(RASPI_PLUS4) | defined(RASPI_PLUS4EMU)
static long kbdMatrixKeyCodes[8][8] = {
{KEYCODE_Backspace, KEYCODE_3, KEYCODE_5, KEYCODE_7, KEYCODE_9, KEYCODE_Left, KEYCODE_Up, KEYCODE_1},
{KEYCODE_Return, KEYCODE_w, KEYCODE_r, KEYCODE_y, KEYCODE_i, KEYCODE_p, KEYCODE_Dash, KEYCODE_BackQuote},
{KEYCODE_BackSlash, KEYCODE_a, KEYCODE_d, KEYCODE_g, KEYCODE_j, KEYCODE_l, KEYCODE_SingleQuote, KEYCODE_Tab},
{KEYCODE_F7, KEYCODE_4, KEYCODE_6, KEYCODE_8, KEYCODE_0, KEYCODE_Right, KEYCODE_Down, KEYCODE_2},
{KEYCODE_F1, KEYCODE_z, KEYCODE_c, KEYCODE_b, KEYCODE_m, KEYCODE_Period, KEYCODE_RightShift, KEYCODE_Space},
{KEYCODE_F3, KEYCODE_s, KEYCODE_f, KEYCODE_h, KEYCODE_k, KEYCODE_SemiColon, KEYCODE_RightBracket, KEYCODE_LeftControl},
{KEYCODE_F5, KEYCODE_e, KEYCODE_t, KEYCODE_u, KEYCODE_o, KEYCODE_LeftBracket, KEYCODE_Equals, KEYCODE_q},
{KEYCODE_Insert, KEYCODE_LeftShift, KEYCODE_x, KEYCODE_v, KEYCODE_n, KEYCODE_Comma, KEYCODE_Slash, KEYCODE_Escape},
};
#else
static long kbdMatrixKeyCodes[8][8] = {
{KEYCODE_Backspace, KEYCODE_3, KEYCODE_5, KEYCODE_7, KEYCODE_9, KEYCODE_Dash, KEYCODE_Insert, KEYCODE_1},
{KEYCODE_Return, KEYCODE_w, KEYCODE_r, KEYCODE_y, KEYCODE_i, KEYCODE_p, KEYCODE_RightBracket, KEYCODE_BackQuote},
{KEYCODE_Right, KEYCODE_a, KEYCODE_d, KEYCODE_g, KEYCODE_j, KEYCODE_l, KEYCODE_SingleQuote, KEYCODE_Tab},
{KEYCODE_F7, KEYCODE_4, KEYCODE_6, KEYCODE_8, KEYCODE_0, KEYCODE_Equals, KEYCODE_Home, KEYCODE_2},
{KEYCODE_F1, KEYCODE_z, KEYCODE_c, KEYCODE_b, KEYCODE_m, KEYCODE_Period, KEYCODE_RightShift, KEYCODE_Space},
{KEYCODE_F3, KEYCODE_s, KEYCODE_f, KEYCODE_h, KEYCODE_k, KEYCODE_SemiColon, KEYCODE_BackSlash, KEYCODE_LeftControl},
{KEYCODE_F5, KEYCODE_e, KEYCODE_t, KEYCODE_u, KEYCODE_o, KEYCODE_LeftBracket, KEYCODE_Delete, KEYCODE_q},
{KEYCODE_Down, KEYCODE_LeftShift, KEYCODE_x, KEYCODE_v, KEYCODE_n, KEYCODE_Comma, KEYCODE_Slash, KEYCODE_Escape},
};
#endif
static int kbdRestoreState;
extern "C" {
int circle_get_machine_timing() {
return static_kernel->circle_get_machine_timing();
}
void circle_sleep(long delay) {
// Timer guaranteed to be ready before vice can call this.
return static_kernel->circle_sleep(delay);
}
unsigned long circle_get_ticks() {
// Timer guaranteed to be ready before vice can call this.
return static_kernel->circle_get_ticks();
}
int circle_sound_bufferspace() {
// Sound init will happen before this so this is okay
return static_kernel->circle_sound_bufferspace();
}
int circle_sound_init(const char *param, int *speed, int *fragsize, int *fragnr,
int *channels) {
// VCHIQ is guaranteed to have been constructed but not necessarily
// initialized so we defer its initialization until this method is
// called by vice.
return static_kernel->circle_sound_init(param, speed, fragsize, fragnr,
channels);
}
int circle_sound_write(int16_t *pbuf, size_t nr) {
// Sound init will happen before this so this is okay
return static_kernel->circle_sound_write(pbuf, nr);
}
void circle_sound_close(void) {
// Sound init will happen before this so this is okay
static_kernel->circle_sound_close();
}
int circle_sound_suspend(void) {
// Sound init will happen before this so this is okay
return static_kernel->circle_sound_suspend();
}
int circle_sound_resume(void) {
// Sound init will happen before this so this is okay
return static_kernel->circle_sound_resume();
}
void circle_yield(void) {
// Scheduler guaranteed to be ready before vice calls this.
static_kernel->circle_yield();
}
void circle_check_gpio() {
// GPIO pins guaranteed to be setup before vice calls this.
static_kernel->circle_check_gpio();
}
void circle_reset_gpio(int gpio_config) {
// Ensure GPIO pins are in correct configuration for current mode.
static_kernel->circle_reset_gpio(gpio_config);
}
void circle_lock_acquire() {
// Always ok
static_kernel->circle_lock_acquire();
}
void circle_lock_release() {
// Always ok
static_kernel->circle_lock_release();
}
void circle_boot_complete() {
// Always ok
static_kernel->circle_boot_complete();
}
int circle_cycles_per_sec() {
// Always ok
return static_kernel->circle_cycles_per_second();
}
int circle_alloc_fbl(int layer, int pixelmode, uint8_t **pixels,
int width, int height, int *pitch) {
return static_kernel->circle_alloc_fbl(layer, pixelmode, pixels, width, height, pitch);
}
int circle_realloc_fbl(int layer, int shader) {
return static_kernel->circle_realloc_fbl(layer, shader);
}
void circle_free_fbl(int layer) {
static_kernel->circle_free_fbl(layer);
}
void circle_clear_fbl(int layer) {
static_kernel->circle_clear_fbl(layer);
}
void circle_show_fbl(int layer) {
static_kernel->circle_show_fbl(layer);
}
void circle_hide_fbl(int layer) {
static_kernel->circle_hide_fbl(layer);
}
void circle_frames_ready_fbl(int layer1, int layer2, int sync) {
static_kernel->circle_frames_ready_fbl(layer1, layer2, sync);
}
void circle_set_palette_fbl(int layer, uint8_t index, uint16_t rgb565) {
static_kernel->circle_set_palette_fbl(layer, index, rgb565);
}
void circle_set_palette32_fbl(int layer, uint8_t index, uint32_t argb) {
static_kernel->circle_set_palette32_fbl(layer, index, argb);
}
void circle_update_palette_fbl(int layer) {
static_kernel->circle_update_palette_fbl(layer);
}
void circle_set_stretch_fbl(int layer, double hstretch, double vstretch, int hintstr, int vintstr, int use_hintstr, int use_vintstr) {
static_kernel->circle_set_stretch_fbl(layer, hstretch, vstretch, hintstr, vintstr, use_hintstr, use_vintstr);
}
void circle_set_center_offset(int layer, int cx, int cy) {
static_kernel->circle_set_center_offset(layer, cx, cy);
}
void circle_set_src_rect_fbl(int layer, int x, int y, int w, int h) {
static_kernel->circle_set_src_rect_fbl(layer, x,y,w,h);
}
void circle_set_valign_fbl(int layer, int align, int padding) {
static_kernel->circle_set_valign_fbl(layer, align, padding);
}
void circle_set_halign_fbl(int layer, int align, int padding) {
static_kernel->circle_set_halign_fbl(layer, align, padding);
}
void circle_set_padding_fbl(int layer, double lpad, double rpad, double tpad, double bpad) {
static_kernel->circle_set_padding_fbl(layer, lpad, rpad, tpad, bpad);
}
void circle_set_zlayer_fbl(int layer, int zlayer) {
static_kernel->circle_set_zlayer_fbl(layer, zlayer);
}
int circle_get_zlayer_fbl(int layer) {
return static_kernel->circle_get_zlayer_fbl(layer);
}
void circle_find_usb(int (*usb)[3]) {
return static_kernel->circle_find_usb(usb);
}
int circle_mount_usb(int usb) {
return static_kernel->circle_mount_usb(usb);
}
int circle_unmount_usb(int usb) {
return static_kernel->circle_unmount_usb(usb);
}
void circle_set_volume(int value) {
static_kernel->circle_set_volume(value);
}
int circle_get_model() {
return static_kernel->circle_get_model();
}
unsigned circle_get_arm_clock() {
return static_kernel->circle_get_arm_clock();
}
int circle_gpio_enabled() {
return static_kernel->circle_gpio_enabled();
}
int circle_gpio_outputs_enabled() {
return static_kernel->circle_gpio_outputs_enabled();
}
void circle_kernel_core_init_complete(int core) {
static_kernel->circle_kernel_core_init_complete(core);
}
void circle_get_fbl_dimensions(int layer, int *display_w, int *display_h,
int *fb_w, int *fb_h,
int *src_w, int *src_h,
int *dst_w, int *dst_h) {
static_kernel->circle_get_fbl_dimensions(layer, display_w, display_h,
fb_w, fb_h,
src_w, src_h, dst_w, dst_h);
}
void circle_get_scaling_params(int display,
int *fbw, int *fbh,
int *sx, int *sy) {
static_kernel->circle_get_scaling_params(display, fbw, fbh, sx, sy);
}
void circle_set_interpolation(int enable) {
static_kernel->circle_set_interpolation(enable);
}
void circle_set_use_shader(int enable) {
static_kernel->circle_set_use_shader(enable);
}
void circle_set_shader_params(int curvature,
float curvature_x,
float curvature_y,
int mask,
float mask_brightness,
int gamma,
int fake_gamma,
int scanlines,
int multisample,
float scanline_weight,
float scanline_gap_brightness,
float bloom_factor,
float input_gamma,
float output_gamma,
int sharper,
int bilinear_interpolation) {
static_kernel->circle_set_shader_params(curvature,
curvature_x,
curvature_y,
mask,
mask_brightness,
gamma,
fake_gamma,
scanlines,
multisample,
scanline_weight,
scanline_gap_brightness,
bloom_factor,
input_gamma,
output_gamma,
sharper,
bilinear_interpolation);
}
};
namespace {
long func_to_keycode(int btn_func) {
switch (btn_func) {
case BTN_ASSIGN_UP:
return KEYCODE_Up;
case BTN_ASSIGN_DOWN:
return KEYCODE_Down;
case BTN_ASSIGN_LEFT:
return KEYCODE_Left;
case BTN_ASSIGN_RIGHT:
return KEYCODE_Right;
case BTN_ASSIGN_FIRE:
return KEYCODE_Return;
default:
return 0;
}
}
}
CKernel::CKernel(void)
: ViceStdioApp("vice"), mViceSound(nullptr),
mNumJoy(emu_get_num_joysticks()),
mVolume(100), mNumCoresComplete(0),
mNeedSoundInit(false), mNumSoundChannels(1) {
static_kernel = this;
mod_states = 0;
memset(key_states, 0, MAX_KEY_CODES * sizeof(bool));
// Only used for pins that are used as buttons. See viceapp.h.
for (int i = 0; i < NUM_GPIO_PINS; i++) {
gpio_debounce_state[i] = BTN_UP;
gpio_prev_state[i] = HIGH;
}
kbdRestoreState = HIGH;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
kbdMatrixStates[i][j] = HIGH;
}
}
fbl[FB_LAYER_VIC].SetLayer(0);
fbl[FB_LAYER_VIC].SetTransparency(false);
fbl[FB_LAYER_VDC].SetLayer(1);
fbl[FB_LAYER_VDC].SetTransparency(false);
fbl[FB_LAYER_STATUS].SetLayer(2);
fbl[FB_LAYER_STATUS].SetTransparency(true);
fbl[FB_LAYER_UI].SetLayer(3);
fbl[FB_LAYER_UI].SetTransparency(true);
if (circle_gpio_outputs_enabled()) {
raspi_userport_enabled = 1;
}
}
bool CKernel::Initialize(void) {
if (!ViceStdioApp::Initialize()) {
return false;
}
return true;
}
static void exec_button_func(int button_func, int is_press, int is_ui) {
// KEEP THIS IN SYNC WITH kbd.c
switch (button_func) {
case BTN_ASSIGN_MENU:
if (is_press) {
emu_key_pressed(KEYCODE_F12);
} else {
emu_key_released(KEYCODE_F12);
}
break;
case BTN_ASSIGN_WARP:
case BTN_ASSIGN_SWAP_PORTS:
case BTN_ASSIGN_STATUS_TOGGLE:
case BTN_ASSIGN_TAPE_MENU:
case BTN_ASSIGN_CART_MENU:
case BTN_ASSIGN_CART_FREEZE:
case BTN_ASSIGN_RESET_MENU:
case BTN_ASSIGN_RESET_HARD:
case BTN_ASSIGN_RESET_SOFT:
case BTN_ASSIGN_ACTIVE_DISPLAY:
case BTN_ASSIGN_PIP_LOCATION:
case BTN_ASSIGN_PIP_SWAP:
case BTN_ASSIGN_40_80_COLUMN:
case BTN_ASSIGN_VKBD_TOGGLE:
case BTN_ASSIGN_FLUSH_DISK:
if (is_press) {
emu_quick_func_interrupt(button_func);
}
break;
case BTN_ASSIGN_CUSTOM_KEY_1:
case BTN_ASSIGN_CUSTOM_KEY_2:
case BTN_ASSIGN_CUSTOM_KEY_3:
case BTN_ASSIGN_CUSTOM_KEY_4:
case BTN_ASSIGN_CUSTOM_KEY_5:
case BTN_ASSIGN_CUSTOM_KEY_6:
if (is_press) {
emu_key_pressed(
emu_get_key_binding(button_func - BTN_ASSIGN_CUSTOM_KEY_1));
} else {
emu_key_released(
emu_get_key_binding(button_func - BTN_ASSIGN_CUSTOM_KEY_1));
}
break;
case BTN_ASSIGN_RUN_STOP_BACK:
if (is_ui) {
emu_ui_key_interrupt(KEYCODE_Escape, is_press);
} else {
if (is_press) {
emu_key_pressed(KEYCODE_Escape);
} else {
emu_key_released(KEYCODE_Escape);
}
}
break;
// Only do direction/fire button assignments for UI, joy is handled
// in circle_add_usb_values seperately.
case BTN_ASSIGN_UP:
case BTN_ASSIGN_DOWN:
case BTN_ASSIGN_LEFT:
case BTN_ASSIGN_RIGHT:
case BTN_ASSIGN_FIRE:
if (is_ui) {
emu_ui_key_interrupt(func_to_keycode(button_func), is_press);
}
break;
default:
break;
}
}
// KEEP THIS IN SYNC WITH kbd.c
static void handle_button_function(bool is_ui, int device, unsigned buttons) {
int button_num = 0;
int button_func;
int is_press;
while (emu_button_function(device, button_num, buttons,
&button_func, &is_press) >= 0) {
exec_button_func(button_func, is_press, is_ui);
button_num++;
}
}
#if 0 // COUNT INVOCATIONS PER SECOND
static unsigned long entry_delay = 5 * TICKS_PER_SECOND;
static unsigned long entry_start = 0;
static long invoked;
#endif
// Interrupt handler. Make this quick.
void CKernel::GamePadStatusHandler(unsigned nDeviceIndex,
const TGamePadState *pState) {
#if 0 // COUNT INVOCATIONS PER SECOND
invoked++;
if (static_kernel->circle_get_ticks() - entry_start >= entry_delay) {
printf ("%ld\n", invoked / 5);
invoked = 0;
entry_start = static_kernel->circle_get_ticks();
}
#endif
static int dpad_to_joy[8] = {0x01, 0x09, 0x08, 0x0a, 0x02, 0x06, 0x04, 0x05};
static unsigned int prev_buttons[MAX_USB_DEVICES] = {0, 0, 0, 0};
static int prev_dpad[MAX_USB_DEVICES] = {8, 8, 8, 8};
static int prev_axes_dirs[MAX_USB_DEVICES][4] = {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};
static int prev_xaxes_values[MAX_USB_DEVICES] = {0,0,0,0};
static int prev_yaxes_values[MAX_USB_DEVICES] = {0,0,0,0};
if (nDeviceIndex >= MAX_USB_DEVICES)
return;
if (emu_wants_raw_usb()) {
// Send the raw usb data and we're done.
int axes[16];
for (int i = 0; i < pState->naxes; i++) {
axes[i] = pState->axes[i].value;
}
emu_set_raw_usb(nDeviceIndex, pState->buttons, pState->hats, axes);
return;
}
int ui_activated = emu_is_ui_activated();
unsigned b = pState->buttons;
// usb_pref is the value of the usb pref menu item
int usb_pref;
int axis_x;
int axis_y;
float thresh_x;
float thresh_y;
emu_get_usb_pref(nDeviceIndex, &usb_pref, &axis_x, &axis_y, &thresh_x,
&thresh_y);
int max_index = axis_x;
if (axis_y > max_index)
max_index = axis_y;
if ((usb_pref == USB_PREF_HAT || usb_pref == USB_PREF_HAT_AND_PADDLES) &&
pState->nhats > 0) {
int dpad = pState->hats[0];
bool has_changed =
(prev_buttons[nDeviceIndex] != b) || (prev_dpad[nDeviceIndex] != dpad);
if (usb_pref == USB_PREF_HAT_AND_PADDLES) {
int xval = pState->axes[axis_x].value;
int yval = pState->axes[axis_y].value;
has_changed |=
prev_xaxes_values[nDeviceIndex] != xval ||
prev_yaxes_values[nDeviceIndex] != yval;
prev_xaxes_values[nDeviceIndex] = xval;
prev_yaxes_values[nDeviceIndex] = yval;
}
if (has_changed) {
int old_dpad = prev_dpad[nDeviceIndex];
prev_buttons[nDeviceIndex] = b;
prev_dpad[nDeviceIndex] = dpad;
// If the UI is activated, route to the menu.
if (ui_activated) {
if (dpad == 0 && old_dpad != 0) {
emu_ui_key_interrupt(KEYCODE_Up, 1);
} else if (dpad != 0 && old_dpad == 0) {
emu_ui_key_interrupt(KEYCODE_Up, 0);
}
if (dpad == 4 && old_dpad != 4) {
emu_ui_key_interrupt(KEYCODE_Down, 1);
} else if (dpad != 4 && old_dpad == 4) {
emu_ui_key_interrupt(KEYCODE_Down, 0);
}
if (dpad == 6 && old_dpad != 6) {
emu_ui_key_interrupt(KEYCODE_Left, 1);
} else if (dpad != 6 && old_dpad == 6) {
emu_ui_key_interrupt(KEYCODE_Left, 0);
}
if (dpad == 2 && old_dpad != 2) {
emu_ui_key_interrupt(KEYCODE_Right, 1);
} else if (dpad != 2 && old_dpad == 2) {
emu_ui_key_interrupt(KEYCODE_Right, 0);
}
handle_button_function(true, nDeviceIndex, b);
return;
}
handle_button_function(false, nDeviceIndex, b);
int value = 0;
if (dpad < 8)
value |= dpad_to_joy[dpad];
value |= emu_add_button_values(nDeviceIndex, b);
// Handle axes as paddles here. This will potentially overwrite
// 2nd/3rd button configs from the call above if they were
// assigned. The UI does not prevent the user from assigning
// potx/poty as buttons and specifying axes as paddles at the same
// time.
if (usb_pref == USB_PREF_HAT_AND_PADDLES && pState->naxes > max_index) {
int minx = pState->axes[axis_x].minimum;
int maxx = pState->axes[axis_x].maximum;
int miny = pState->axes[axis_y].minimum;
int maxy = pState->axes[axis_y].maximum;
int distx = maxx - minx;
int disty = maxy - miny;
double scalex = distx / 255.0d;
double scaley = disty / 255.0d;
unsigned char valuex = (pState->axes[axis_x].value - minx) / scalex;
unsigned char valuey = (pState->axes[axis_y].value - miny) / scaley;
value &= ~ 0x1fffe0; // null out potx and poty
value |= (valuex << 5);
value |= (valuey << 13);
}
emu_set_joy_usb_interrupt(nDeviceIndex, value);
}
} else if (usb_pref == USB_PREF_ANALOG && pState->naxes > max_index) {
// TODO: Do this just once at init
int minx = pState->axes[axis_x].minimum;
int maxx = pState->axes[axis_x].maximum;
int miny = pState->axes[axis_y].minimum;
int maxy = pState->axes[axis_y].maximum;
int tx = (maxx - minx) / 2 * thresh_x;
int mx = (maxx + minx) / 2;
int ty = (maxy - miny) / 2 * thresh_y;
int my = (maxy + miny) / 2;
int a_left = pState->axes[axis_x].value < mx - tx;
int a_right = pState->axes[axis_x].value > mx + tx;
int a_up = pState->axes[axis_y].value < my - ty;
int a_down = pState->axes[axis_y].value > my + ty;
bool has_changed = (prev_buttons[nDeviceIndex] != b) ||
(prev_axes_dirs[nDeviceIndex][0] != a_up) ||
(prev_axes_dirs[nDeviceIndex][1] != a_down) ||
(prev_axes_dirs[nDeviceIndex][2] != a_left) ||
(prev_axes_dirs[nDeviceIndex][3] != a_right);
if (has_changed) {
int prev_a_up = prev_axes_dirs[nDeviceIndex][0];
int prev_a_down = prev_axes_dirs[nDeviceIndex][1];
int prev_a_left = prev_axes_dirs[nDeviceIndex][2];
int prev_a_right = prev_axes_dirs[nDeviceIndex][3];
prev_axes_dirs[nDeviceIndex][0] = a_up;
prev_axes_dirs[nDeviceIndex][1] = a_down;
prev_axes_dirs[nDeviceIndex][2] = a_left;
prev_axes_dirs[nDeviceIndex][3] = a_right;
prev_buttons[nDeviceIndex] = b;
// If the UI is activated, route to the menu.
if (ui_activated) {
if (a_up && !prev_a_up) {
emu_ui_key_interrupt(KEYCODE_Up, 1);
} else if (!a_up && prev_a_up) {
emu_ui_key_interrupt(KEYCODE_Up, 0);
}
if (a_down && !prev_a_down) {
emu_ui_key_interrupt(KEYCODE_Down, 1);
} else if (!a_down && prev_a_down) {
emu_ui_key_interrupt(KEYCODE_Down, 0);
}
if (a_left && !prev_a_left) {
emu_ui_key_interrupt(KEYCODE_Left, 1);
} else if (!a_left && prev_a_left) {
emu_ui_key_interrupt(KEYCODE_Left, 0);
}
if (a_right && !prev_a_right) {
emu_ui_key_interrupt(KEYCODE_Right, 1);
} else if (!a_right && prev_a_right) {
emu_ui_key_interrupt(KEYCODE_Right, 0);
}
handle_button_function(true, nDeviceIndex, b);
return;
}
handle_button_function(false, nDeviceIndex, b);
int value = 0;
if (a_left)
value |= 0x4;
if (a_right)
value |= 0x8;
if (a_up)
value |= 0x1;
if (a_down)
value |= 0x2;
value |= emu_add_button_values(nDeviceIndex, b);
emu_set_joy_usb_interrupt(nDeviceIndex, value);
}
}
}
void CKernel::SetupUSBKeyboard() {
CUSBKeyboardDevice *pKeyboard =
(CUSBKeyboardDevice *)mDeviceNameService.GetDevice("ukbd1", FALSE);
if (pKeyboard) {
pKeyboard->RegisterKeyStatusHandlerRaw(KeyStatusHandlerRaw);
}
}
void CKernel::SetupUSBMouse() {
CMouseDevice *pMouse =
(CMouseDevice *)mDeviceNameService.GetDevice("mouse1", FALSE);
if (pMouse) {
pMouse->RegisterStatusHandler(MouseStatusHandler);
}
}
void CKernel::SetupUSBGamepads() {
unsigned num_pads = 0;
int num_buttons[MAX_USB_DEVICES] = {0, 0, 0, 0};
int num_axes[MAX_USB_DEVICES] = {0, 0, 0, 0};
int num_hats[MAX_USB_DEVICES] = {0, 0, 0, 0};
while (num_pads < MAX_USB_DEVICES) {
CString DeviceName;
DeviceName.Format("upad%u", num_pads + 1);
CUSBGamePadDevice *game_pad =
(CUSBGamePadDevice *)mDeviceNameService.GetDevice(DeviceName, FALSE);
if (game_pad == 0) {
break;
}
const TGamePadState *pState = game_pad->GetInitialState();
assert(pState != 0);
num_axes[num_pads] = pState->naxes;
num_hats[num_pads] = pState->nhats;
num_buttons[num_pads] = pState->nbuttons;
game_pad->RegisterStatusHandler(GamePadStatusHandler);
num_pads++;
}
// Tell the emulator what we found
emu_set_gamepad_info(num_pads, num_buttons, num_axes, num_hats);
}
ViceApp::TShutdownMode CKernel::Run(void) {
SetupUSBKeyboard();
SetupUSBMouse();
SetupUSBGamepads();
emu_set_demo_mode(mViceOptions.DemoEnabled());
#ifndef ARM_ALLOW_MULTI_CORE
mEmulatorCore->LaunchEmulator(mTimingOption);
#else
// This core will do nothing but service interrupts from
// usb or gpio.
printf("Core 0 idle\n");
asm("dsb\n\t"
"1: wfi\n\t"
"b 1b\n\t");
#endif
return ShutdownHalt;
}
void CKernel::ScanKeyboard() {
int ui_activated = emu_is_ui_activated();
int restore = gpioPins[GPIO_KBD_RESTORE_INDEX]->Read();
// For restore, there is no public API that triggers it so we will
// pass the keycode that will. NOTE: On the plus/4, this key sym
// will be the CLR key according to the keymap.
if (restore == LOW && kbdRestoreState == HIGH) {
emu_key_pressed(restore_key_sym);
} else if (restore == HIGH && kbdRestoreState == LOW) {
emu_key_released(restore_key_sym);
}
kbdRestoreState = restore;
// Keyboard scan
for (int kbdPA = 0; kbdPA < 8; kbdPA++) {
gpioPins[kbdPA]->SetMode(GPIOModeOutput);
gpioPins[kbdPA]->Write(LOW);
circle_sleep(10);
for (int kbdPB = 0; kbdPB < 8; kbdPB++) {
// Read PB line
int val = gpioPins[kbdPB + 8]->Read();
// My PA/PB to keycode matrix is transposed and I'm too lazy to fix
// it. Just swap PB and PA here for the keycode lookup.
long keycode = kbdMatrixKeyCodes[kbdPB][kbdPA];
if (ui_activated) {
if (val == LOW && kbdMatrixStates[kbdPA][kbdPB] == HIGH) {
if (keycode == KEYCODE_LeftShift) {
uiLeftShift = true;
} else if (keycode == KEYCODE_RightShift) {
uiRightShift = true;
}
if (keycode == KEYCODE_Right && (uiLeftShift || uiRightShift)) {
emu_key_pressed(KEYCODE_Left);
} else if (keycode == KEYCODE_Down && (uiLeftShift || uiRightShift)) {
emu_key_pressed(KEYCODE_Up);
} else {
emu_key_pressed(keycode);
}
} else if (val == HIGH && kbdMatrixStates[kbdPA][kbdPB] == LOW) {
if (keycode == KEYCODE_LeftShift) {
uiLeftShift = false;
} else if (keycode == KEYCODE_RightShift) {
uiRightShift = false;
}
if (keycode == KEYCODE_Right && (uiLeftShift || uiRightShift)) {
emu_key_released(KEYCODE_Left);
} else if (keycode == KEYCODE_Down && (uiLeftShift || uiRightShift)) {
emu_key_released(KEYCODE_Up);
} else {
emu_key_released(keycode);
}
}
} else {
// TODO: Need to watch out for key combos here. Hook into
// the handle functions directly in kbd.c so we can invoke the
// same hotkey funcs.
if (val == LOW && kbdMatrixStates[kbdPA][kbdPB] == HIGH) {
emu_key_pressed(keycode);
} else if (val == HIGH && kbdMatrixStates[kbdPA][kbdPB] == LOW) {
emu_key_released(keycode);
}
}
kbdMatrixStates[kbdPA][kbdPB] = val;
}
gpioPins[kbdPA]->SetMode(GPIOModeInputPullUp);
}
}
// Read joystick state.
// If gpioConfig is 0, the NavButtons+Joys config is used where pins can
// be grounded.
// If gpioConfig is 1, the Keyboard+Joys PCB config is used (where
// selector is used to drive pins low instead of GND).
// If gpioConfig is 2, the Waveshare HAT layout is used.
void CKernel::ReadJoystick(int device, int gpioConfig) {
// For remembering button states for UI only
static int js_prev_0[5] = {HIGH, HIGH, HIGH, HIGH, HIGH};
static int js_prev_1[5] = {HIGH, HIGH, HIGH, HIGH, HIGH};
int *js_prev;
CGPIOPin **js_pins = NULL;
CGPIOPin *js_selector = NULL;
int port = 0;
int devd = 0;
int ui_activated = emu_is_ui_activated();
// If ui is activated, don't bail if port assignment can't be done
// since the event will always go to the ui. We want the joystick to
// function in the ui even if the control port is not assigned to be
// gpio.
if (device == 0) {
if (joydevs[0].device == JOYDEV_GPIO_0) {
port = joydevs[0].port;
devd = JOYDEV_GPIO_0;
} else if (joydevs[1].device == JOYDEV_GPIO_0) {
port = joydevs[1].port;
devd = JOYDEV_GPIO_0;
} else if (!ui_activated) {
return;
}
js_prev = js_prev_0;
switch (gpioConfig) {
case GPIO_CONFIG_NAV_JOY:
js_pins = config_0_joystickPins1;
break;
case GPIO_CONFIG_KYB_JOY:
js_selector = gpioPins[GPIO_JS1_SELECT_INDEX];
js_pins = config_1_joystickPins1;
break;
case GPIO_CONFIG_WAVESHARE:
js_pins = config_2_joystickPins;
break;
case GPIO_CONFIG_USERPORT:
js_pins = config_3_joystickPins1;
break;
default:
assert(false);
}
} else {
if (joydevs[0].device == JOYDEV_GPIO_1) {
port = joydevs[0].port;
devd = JOYDEV_GPIO_1;
} else if (joydevs[1].device == JOYDEV_GPIO_1) {
port = joydevs[1].port;
devd = JOYDEV_GPIO_1;
} else if (!ui_activated) {
return;
}
js_prev = js_prev_1;
switch (gpioConfig) {
case GPIO_CONFIG_NAV_JOY:
js_pins = config_0_joystickPins2;
break;
case GPIO_CONFIG_KYB_JOY:
js_selector = gpioPins[GPIO_JS2_SELECT_INDEX];
js_pins = config_1_joystickPins2;
break;
case GPIO_CONFIG_USERPORT:
js_pins = config_3_joystickPins2;
break;
default:
assert(false);
}
}
if (gpioConfig == 1) {
// Drive the select pin low. Don't leave this routine
// before setting it as input-pullup again.
js_selector->SetMode(GPIOModeOutput);
js_selector->Write(LOW);
circle_sleep(10);
}
int js_up = js_pins[JOY_UP]->Read();
int js_down = js_pins[JOY_DOWN]->Read();
int js_left = js_pins[JOY_LEFT]->Read();
int js_right = js_pins[JOY_RIGHT]->Read();
int js_fire = js_pins[JOY_FIRE]->Read();
int js_potx = gpioConfig == 2 ? js_pins[JOY_POTX]->Read() : HIGH;
int js_poty = gpioConfig == 2 ? js_pins[JOY_POTY]->Read() : HIGH;
if (ui_activated) {
if (js_up == LOW && js_prev[JOY_UP] != LOW) {
emu_ui_key_interrupt(KEYCODE_Up, 1);
} else if (js_up != LOW && js_prev[JOY_UP] == LOW) {
emu_ui_key_interrupt(KEYCODE_Up, 0);
}
if (js_down == LOW && js_prev[JOY_DOWN] != LOW) {
emu_ui_key_interrupt(KEYCODE_Down, 1);
} else if (js_down != LOW && js_prev[JOY_DOWN] == LOW) {
emu_ui_key_interrupt(KEYCODE_Down, 0);
}
if (js_left == LOW && js_prev[JOY_LEFT] != LOW) {
emu_ui_key_interrupt(KEYCODE_Left, 1);
} else if (js_left != LOW && js_prev[JOY_LEFT] == LOW) {
emu_ui_key_interrupt(KEYCODE_Left, 0);
}
if (js_right == LOW && js_prev[JOY_RIGHT] != LOW) {
emu_ui_key_interrupt(KEYCODE_Right, 1);
} else if (js_right != LOW && js_prev[JOY_RIGHT] == LOW) {
emu_ui_key_interrupt(KEYCODE_Right, 0);
}
if (js_fire == LOW && js_prev[JOY_FIRE] != LOW) {
emu_ui_key_interrupt(KEYCODE_Return, 1);
} else if (js_fire != LOW && js_prev[JOY_FIRE] == LOW) {
emu_ui_key_interrupt(KEYCODE_Return, 0);
}
js_prev[JOY_UP] = js_up;
js_prev[JOY_DOWN] = js_down;
js_prev[JOY_LEFT] = js_left;
js_prev[JOY_RIGHT] = js_right;
js_prev[JOY_FIRE] = js_fire;
// not necessary to remember pot values as they are not used for ui
} else {
emu_joy_interrupt_abs(port, devd,
js_up == LOW,
js_down == LOW,
js_left == LOW,
js_right == LOW,
js_fire == LOW,
js_potx == LOW,
js_poty == LOW);
}
if (gpioConfig == 1) {
js_selector->SetMode(GPIOModeInputPullUp);
}
}
void CKernel::ReadCustomGPIO() {
int i;
unsigned int bank;
unsigned int func;
int value;
int js_up_1 = HIGH;
int js_down_1 = HIGH;
int js_left_1 = HIGH;
int js_right_1 = HIGH;
int js_fire_1 = HIGH;
int js_potx_1 = HIGH;
int js_poty_1 = HIGH;
int js_up_2 = HIGH;
int js_down_2 = HIGH;
int js_left_2 = HIGH;
int js_right_2 = HIGH;
int js_fire_2 = HIGH;
int js_potx_2 = HIGH;
int js_poty_2 = HIGH;
int ui_activated = emu_is_ui_activated();
int port_is_gpio_joy[2] = {0,0};
for (i = 0 ; i < NUM_GPIO_PINS; i++) {
bank = gpio_bindings[i] >> 8;
func = gpio_bindings[i] & 0xFF;