comparison io.c @ 1111:2eb54e24914e

Mostly working changes to allow support for multiple emulated system types in main blastem program
author Michael Pavone <pavone@retrodev.com>
date Mon, 19 Dec 2016 13:28:18 -0800
parents 22e87b739ad6
children fe8c79f82c22
comparison
equal deleted inserted replaced
1110:d1eed3b1121c 1111:2eb54e24914e
105 uint8_t bind_type; 105 uint8_t bind_type;
106 } mousebinding; 106 } mousebinding;
107 107
108 #define DEFAULT_JOYBUTTON_ALLOC 12 108 #define DEFAULT_JOYBUTTON_ALLOC 12
109 109
110 static keybinding * bindings[0x10000]; 110 static sega_io *current_io;
111 static keybinding *bindings[0x10000];
111 static joystick joysticks[MAX_JOYSTICKS]; 112 static joystick joysticks[MAX_JOYSTICKS];
112 static mousebinding mice[MAX_MICE]; 113 static mousebinding mice[MAX_MICE];
113 static io_port *keyboard_port; 114 static io_port *keyboard_port;
114 const uint8_t dpadbits[] = {RENDER_DPAD_UP, RENDER_DPAD_DOWN, RENDER_DPAD_LEFT, RENDER_DPAD_RIGHT}; 115 const uint8_t dpadbits[] = {RENDER_DPAD_UP, RENDER_DPAD_DOWN, RENDER_DPAD_LEFT, RENDER_DPAD_RIGHT};
115 116
292 handle_binding_down(binding); 293 handle_binding_down(binding);
293 } 294 }
294 295
295 void handle_mousedown(int mouse, int button) 296 void handle_mousedown(int mouse, int button)
296 { 297 {
297 if (genesis->mouse_mode == MOUSE_CAPTURE && !genesis->mouse_captured) { 298 if (current_io->mouse_mode == MOUSE_CAPTURE && !current_io->mouse_captured) {
298 genesis->mouse_captured = 1; 299 current_io->mouse_captured = 1;
299 render_relative_mouse(1); 300 render_relative_mouse(1);
300 return; 301 return;
301 } 302 }
302 if (mouse >= MAX_MICE || button > MAX_MOUSE_BUTTONS || button <= 0) { 303 if (mouse >= MAX_MICE || button > MAX_MOUSE_BUTTONS || button <= 0) {
303 return; 304 return;
346 break; 347 break;
347 case BIND_UI: 348 case BIND_UI:
348 switch (binding->subtype_a) 349 switch (binding->subtype_a)
349 { 350 {
350 case UI_DEBUG_MODE_INC: 351 case UI_DEBUG_MODE_INC:
351 ui_debug_mode++; 352 current_system->inc_debug_mode(current_system);
352 if (ui_debug_mode == 7) {
353 ui_debug_mode = 0;
354 }
355 genesis->vdp->debug = ui_debug_mode;
356 break; 353 break;
357 case UI_DEBUG_PAL_INC: 354 case UI_DEBUG_PAL_INC:
358 ui_debug_pal++; 355 current_system->inc_debug_pal(current_system);
359 if (ui_debug_pal == 4) {
360 ui_debug_pal = 0;
361 }
362 genesis->vdp->debug_pal = ui_debug_pal;
363 break; 356 break;
364 case UI_ENTER_DEBUGGER: 357 case UI_ENTER_DEBUGGER:
365 break_on_sync = 1; 358 current_system->enter_debugger = 1;
366 break; 359 break;
367 case UI_SAVE_STATE: 360 case UI_SAVE_STATE:
368 genesis->save_state = QUICK_SAVE_SLOT+1; 361 current_system->save_state = QUICK_SAVE_SLOT+1;
369 break; 362 break;
370 case UI_NEXT_SPEED: 363 case UI_NEXT_SPEED:
371 current_speed++; 364 current_speed++;
372 if (current_speed >= num_speeds) { 365 if (current_speed >= num_speeds) {
373 current_speed = 0; 366 current_speed = 0;
374 } 367 }
375 printf("Setting speed to %d: %d\n", current_speed, speeds[current_speed]); 368 printf("Setting speed to %d: %d\n", current_speed, speeds[current_speed]);
376 set_speed_percent(genesis, speeds[current_speed]); 369 current_system->set_speed_percent(current_system, speeds[current_speed]);
377 break; 370 break;
378 case UI_PREV_SPEED: 371 case UI_PREV_SPEED:
379 current_speed--; 372 current_speed--;
380 if (current_speed < 0) { 373 if (current_speed < 0) {
381 current_speed = num_speeds - 1; 374 current_speed = num_speeds - 1;
382 } 375 }
383 printf("Setting speed to %d: %d\n", current_speed, speeds[current_speed]); 376 printf("Setting speed to %d: %d\n", current_speed, speeds[current_speed]);
384 set_speed_percent(genesis, speeds[current_speed]); 377 current_system->set_speed_percent(current_system, speeds[current_speed]);
385 break; 378 break;
386 case UI_SET_SPEED: 379 case UI_SET_SPEED:
387 if (binding->value < num_speeds) { 380 if (binding->value < num_speeds) {
388 current_speed = binding->value; 381 current_speed = binding->value;
389 printf("Setting speed to %d: %d\n", current_speed, speeds[current_speed]); 382 printf("Setting speed to %d: %d\n", current_speed, speeds[current_speed]);
390 set_speed_percent(genesis, speeds[current_speed]); 383 current_system->set_speed_percent(current_system, speeds[current_speed]);
391 } else { 384 } else {
392 printf("Setting speed to %d\n", speeds[current_speed]); 385 printf("Setting speed to %d\n", speeds[current_speed]);
393 set_speed_percent(genesis, binding->value); 386 current_system->set_speed_percent(current_system, speeds[current_speed]);
394 } 387 }
395 break; 388 break;
396 case UI_RELEASE_MOUSE: 389 case UI_RELEASE_MOUSE:
397 if (genesis->mouse_captured) { 390 if (current_io->mouse_captured) {
398 genesis->mouse_captured = 0; 391 current_io->mouse_captured = 0;
399 render_relative_mouse(0); 392 render_relative_mouse(0);
400 } 393 }
401 break; 394 break;
402 case UI_EXIT: 395 case UI_EXIT:
403 genesis->m68k->should_return = 1; 396 current_system->request_exit(current_system);
397 break;
404 } 398 }
405 break; 399 break;
406 } 400 }
407 } 401 }
408 402
458 { 452 {
459 if (mouse >= MAX_MICE || !mice[mouse].motion_port) { 453 if (mouse >= MAX_MICE || !mice[mouse].motion_port) {
460 return; 454 return;
461 } 455 }
462 //TODO: relative mode 456 //TODO: relative mode
463 switch(genesis->mouse_mode) 457 switch(current_io->mouse_mode)
464 { 458 {
465 case MOUSE_ABSOLUTE: { 459 case MOUSE_ABSOLUTE: {
466 float scale_x = 640.0 / ((float)render_width()); 460 float scale_x = 640.0 / ((float)render_width());
467 float scale_y = 480.0 / ((float)render_height()); 461 float scale_y = 480.0 / ((float)render_height());
468 float scale = scale_x > scale_y ? scale_y : scale_x; 462 float scale = scale_x > scale_y ? scale_y : scale_x;
474 mice[mouse].motion_port->device.mouse.cur_x += deltax; 468 mice[mouse].motion_port->device.mouse.cur_x += deltax;
475 mice[mouse].motion_port->device.mouse.cur_y += deltay; 469 mice[mouse].motion_port->device.mouse.cur_y += deltay;
476 break; 470 break;
477 } 471 }
478 case MOUSE_CAPTURE: { 472 case MOUSE_CAPTURE: {
479 if (genesis->mouse_captured) { 473 if (current_io->mouse_captured) {
480 mice[mouse].motion_port->device.mouse.cur_x += deltax; 474 mice[mouse].motion_port->device.mouse.cur_x += deltax;
481 mice[mouse].motion_port->device.mouse.cur_y += deltay; 475 mice[mouse].motion_port->device.mouse.cur_y += deltay;
482 } 476 }
483 } 477 }
484 } 478 }
719 unlink(sockfile_name); 713 unlink(sockfile_name);
720 } 714 }
721 715
722 void setup_io_devices(tern_node * config, rom_info *rom, genesis_context *gen) 716 void setup_io_devices(tern_node * config, rom_info *rom, genesis_context *gen)
723 { 717 {
724 io_port * ports = gen->ports; 718 current_io = &gen->io;
719 io_port * ports = current_io->ports;
725 tern_node *io_nodes = tern_get_node(tern_find_path(config, "io\0devices\0")); 720 tern_node *io_nodes = tern_get_node(tern_find_path(config, "io\0devices\0"));
726 char * io_1 = rom->port1_override ? rom->port1_override : tern_find_ptr(io_nodes, "1"); 721 char * io_1 = rom->port1_override ? rom->port1_override : tern_find_ptr(io_nodes, "1");
727 char * io_2 = rom->port2_override ? rom->port2_override : tern_find_ptr(io_nodes, "2"); 722 char * io_2 = rom->port2_override ? rom->port2_override : tern_find_ptr(io_nodes, "2");
728 char * io_ext = rom->ext_override ? rom->ext_override : tern_find_ptr(io_nodes, "ext"); 723 char * io_ext = rom->ext_override ? rom->ext_override : tern_find_ptr(io_nodes, "ext");
729 724
730 process_device(io_1, ports); 725 process_device(io_1, ports);
731 process_device(io_2, ports+1); 726 process_device(io_2, ports+1);
732 process_device(io_ext, ports+2); 727 process_device(io_ext, ports+2);
733 728
734 if (render_fullscreen()) { 729 if (render_fullscreen()) {
735 gen->mouse_mode = MOUSE_RELATIVE; 730 current_io->mouse_mode = MOUSE_RELATIVE;
736 render_relative_mouse(1); 731 render_relative_mouse(1);
737 } else { 732 } else {
738 if (rom->mouse_mode && !strcmp(rom->mouse_mode, "absolute")) { 733 if (rom->mouse_mode && !strcmp(rom->mouse_mode, "absolute")) {
739 gen->mouse_mode = MOUSE_ABSOLUTE; 734 current_io->mouse_mode = MOUSE_ABSOLUTE;
740 } else { 735 } else {
741 gen->mouse_mode = MOUSE_CAPTURE; 736 current_io->mouse_mode = MOUSE_CAPTURE;
742 } 737 }
743 } 738 }
744 739
745 for (int i = 0; i < 3; i++) 740 for (int i = 0; i < 3; i++)
746 { 741 {
924 pmb_state state = {padbuttons, mousebuttons, mouseidx}; 919 pmb_state state = {padbuttons, mousebuttons, mouseidx};
925 tern_foreach(buttons, process_mouse_button, &state); 920 tern_foreach(buttons, process_mouse_button, &state);
926 } 921 }
927 } 922 }
928 923
929 void set_keybindings(io_port *ports) 924 void set_keybindings(sega_io *io)
930 { 925 {
926 static uint8_t already_done;
927 if (already_done) {
928 map_all_bindings(io);
929 return;
930 }
931 already_done = 1;
932 io_port *ports = io->ports;
931 tern_node * special = tern_insert_int(NULL, "up", RENDERKEY_UP); 933 tern_node * special = tern_insert_int(NULL, "up", RENDERKEY_UP);
932 special = tern_insert_int(special, "down", RENDERKEY_DOWN); 934 special = tern_insert_int(special, "down", RENDERKEY_DOWN);
933 special = tern_insert_int(special, "left", RENDERKEY_LEFT); 935 special = tern_insert_int(special, "left", RENDERKEY_LEFT);
934 special = tern_insert_int(special, "right", RENDERKEY_RIGHT); 936 special = tern_insert_int(special, "right", RENDERKEY_RIGHT);
935 special = tern_insert_int(special, "enter", '\r'); 937 special = tern_insert_int(special, "enter", '\r');
1065 if (!speeds[i]) { 1067 if (!speeds[i]) {
1066 warning("Speed index %d was not set to a valid percentage!", i); 1068 warning("Speed index %d was not set to a valid percentage!", i);
1067 speeds[i] = 100; 1069 speeds[i] = 100;
1068 } 1070 }
1069 } 1071 }
1070 map_all_bindings(ports); 1072 map_all_bindings(io);
1071 } 1073 }
1072 1074
1073 void map_all_bindings(io_port *ports) 1075 void map_all_bindings(sega_io *io)
1074 { 1076 {
1077 current_io = io;
1078 io_port *ports = io->ports;
1079
1075 for (int bucket = 0; bucket < 0x10000; bucket++) 1080 for (int bucket = 0; bucket < 0x10000; bucket++)
1076 { 1081 {
1077 if (bindings[bucket]) 1082 if (bindings[bucket])
1078 { 1083 {
1079 map_bindings(ports, bindings[bucket], 0x8000); 1084 map_bindings(ports, bindings[bucket], 0x8000);
1115 break; 1120 break;
1116 } 1121 }
1117 } 1122 }
1118 //not really related to the intention of this function, but the best place to do this currently 1123 //not really related to the intention of this function, but the best place to do this currently
1119 if (speeds[0] != 100) { 1124 if (speeds[0] != 100) {
1120 set_speed_percent(genesis, speeds[0]); 1125 current_system->set_speed_percent(current_system, speeds[0]);
1121 } 1126 }
1122 } 1127 }
1123 1128
1124 #define TH 0x40 1129 #define TH 0x40
1125 #define TR 0x20 1130 #define TR 0x20
1131 port->device.mouse.tr_counter++; 1136 port->device.mouse.tr_counter++;
1132 port->device.mouse.ready_cycle = CYCLE_NEVER; 1137 port->device.mouse.ready_cycle = CYCLE_NEVER;
1133 if (port->device.mouse.tr_counter == 3) { 1138 if (port->device.mouse.tr_counter == 3) {
1134 port->device.mouse.latched_x = port->device.mouse.cur_x; 1139 port->device.mouse.latched_x = port->device.mouse.cur_x;
1135 port->device.mouse.latched_y = port->device.mouse.cur_y; 1140 port->device.mouse.latched_y = port->device.mouse.cur_y;
1136 if (genesis->mouse_mode == MOUSE_ABSOLUTE) { 1141 if (current_io->mouse_mode == MOUSE_ABSOLUTE) {
1137 //avoid overflow in absolute mode 1142 //avoid overflow in absolute mode
1138 int deltax = port->device.mouse.latched_x - port->device.mouse.last_read_x; 1143 int deltax = port->device.mouse.latched_x - port->device.mouse.last_read_x;
1139 if (abs(deltax) > 255) { 1144 if (abs(deltax) > 255) {
1140 port->device.mouse.latched_x = port->device.mouse.last_read_x + (deltax > 0 ? 255 : -255); 1145 port->device.mouse.latched_x = port->device.mouse.last_read_x + (deltax > 0 ? 255 : -255);
1141 } 1146 }