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
|
/*
* TilEm II
*
* Copyright (c) 2011-2012 Benjamin Moody
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <gtk/gtk.h>
#include <glib/gstdio.h>
#include <ticalcs.h>
#include <tilem.h>
#include "gui.h"
#include "emucore.h"
#include "msgbox.h"
#include "filedlg.h"
#define MILLISEC_PER_FRAME 30
#define MICROSEC_PER_FRAME (MILLISEC_PER_FRAME * 1000)
#define GRAY_WINDOW_SIZE 4
#define GRAY_SAMPLE_INT 200
/* Lock emulator. Notify the core loop that we wish to do so - note
that if the core is running at full speed, it keeps the mutex
locked almost all the time, so we need to explicitly ask it to
yield. (This may not be necessary with all mutex implementations,
but definitely is necessary with some.) */
void tilem_calc_emulator_lock(TilemCalcEmulator *emu)
{
g_atomic_int_inc(&emu->calc_lock_waiting);
g_mutex_lock(emu->calc_mutex);
}
/* Unlock emulator and (if no other threads are waiting to lock it)
wake up the core thread. This also serves to resume emulation if
the calculator has been powered off. */
void tilem_calc_emulator_unlock(TilemCalcEmulator *emu)
{
if (g_atomic_int_dec_and_test(&emu->calc_lock_waiting))
g_cond_signal(emu->calc_wakeup_cond);
g_mutex_unlock(emu->calc_mutex);
}
static gboolean refresh_lcd(gpointer data)
{
TilemCalcEmulator* emu = data;
if (emu->ewin)
tilem_emulator_window_refresh_lcd(emu->ewin);
return FALSE;
}
static void tmr_screen_update(TilemCalc *calc, void *data)
{
TilemCalcEmulator *emu = data;
g_mutex_lock(emu->lcd_mutex);
if (emu->glcd)
tilem_gray_lcd_get_frame(emu->glcd, emu->lcd_buffer);
else
tilem_lcd_get_frame(calc, emu->lcd_buffer);
if (emu->anim) {
if (emu->anim_grayscale) {
tilem_animation_append_frame(emu->anim,
emu->lcd_buffer,
MILLISEC_PER_FRAME);
}
else {
tilem_lcd_get_frame(calc, emu->tmp_lcd_buffer);
tilem_animation_append_frame(emu->anim,
emu->tmp_lcd_buffer,
MILLISEC_PER_FRAME);
}
}
if (!emu->lcd_update_pending) {
emu->lcd_update_pending = TRUE;
g_idle_add_full(G_PRIORITY_DEFAULT, &refresh_lcd, emu, NULL);
}
g_mutex_unlock(emu->lcd_mutex);
}
static void cancel_animation(TilemCalcEmulator *emu)
{
if (emu->anim)
g_object_unref(emu->anim);
emu->anim = NULL;
}
static GtkWidget *get_toplevel(TilemCalcEmulator *emu)
{
if (emu->ewin)
return emu->ewin->window;
else
return NULL;
}
static void link_update_nop()
{
}
TilemCalcEmulator *tilem_calc_emulator_new()
{
TilemCalcEmulator *emu = g_new0(TilemCalcEmulator, 1);
CalcUpdate *update;
emu->calc_mutex = g_mutex_new();
emu->calc_wakeup_cond = g_cond_new();
emu->lcd_mutex = g_mutex_new();
tilem_config_get("emulation",
"grayscale/b=1", &emu->grayscale,
"limit_speed/b=1", &emu->limit_speed,
NULL);
emu->task_queue = g_queue_new();
emu->task_finished_cond = g_cond_new();
emu->timer = g_timer_new();
emu->pbar_mutex = g_mutex_new();
update = g_new0(CalcUpdate, 1);
update->start = &link_update_nop;
update->stop = &link_update_nop;
update->refresh = &link_update_nop;
update->pbar = &link_update_nop;
update->label = &link_update_nop;
emu->link_update = update;
return emu;
}
void tilem_calc_emulator_free(TilemCalcEmulator *emu)
{
g_return_if_fail(emu != NULL);
tilem_calc_emulator_cancel_tasks(emu);
tilem_calc_emulator_lock(emu);
cancel_animation(emu);
emu->exiting = TRUE;
tilem_calc_emulator_unlock(emu);
if (emu->z80_thread)
g_thread_join(emu->z80_thread);
g_free(emu->key_queue);
g_free(emu->rom_file_name);
g_free(emu->state_file_name);
g_mutex_free(emu->calc_mutex);
g_mutex_free(emu->lcd_mutex);
g_cond_free(emu->calc_wakeup_cond);
g_cond_free(emu->task_finished_cond);
g_queue_free(emu->task_queue);
g_timer_destroy(emu->timer);
g_mutex_free(emu->pbar_mutex);
g_free(emu->link_update);
if (emu->lcd_buffer)
tilem_lcd_buffer_free(emu->lcd_buffer);
if (emu->tmp_lcd_buffer)
tilem_lcd_buffer_free(emu->tmp_lcd_buffer);
if (emu->glcd)
tilem_gray_lcd_free(emu->glcd);
if (emu->calc)
tilem_calc_free(emu->calc);
g_free(emu);
}
static char *get_sav_name(const char *romname)
{
char *dname, *bname, *sname, *suff;
dname = g_path_get_dirname(romname);
bname = g_path_get_basename(romname);
if ((suff = strrchr(bname, '.')))
*suff = 0;
sname = g_strconcat(dname, G_DIR_SEPARATOR_S, bname, ".sav", NULL);
g_free(dname);
g_free(bname);
return sname;
}
gboolean tilem_calc_emulator_load_state(TilemCalcEmulator *emu,
const char *romfname,
const char *statefname,
int model, GError **err)
{
const char *modelname;
FILE *romfile, *savfile;
char *rname = NULL, *sname = NULL;
TilemCalc *calc;
char *dname;
int errnum;
g_return_val_if_fail(emu != NULL, FALSE);
g_return_val_if_fail(err == NULL || *err == NULL, FALSE);
tilem_calc_emulator_cancel_tasks(emu);
if (romfname)
rname = g_strdup(romfname);
if (!sname && statefname)
sname = g_strdup(statefname);
/* Choose ROM file */
if (!rname && model) {
modelname = model_to_name(model);
g_return_val_if_fail(modelname != NULL, FALSE);
if (sname) g_free(sname);
tilem_config_get(modelname,
"rom_file/f", &rname,
"state_file/f", &sname,
NULL);
}
if (!rname) {
g_set_error(err, TILEM_EMULATOR_ERROR,
TILEM_EMULATOR_ERROR_NO_ROM,
"No ROM file specified");
g_free(rname);
g_free(sname);
return FALSE;
}
/* Open ROM file */
romfile = g_fopen(rname, "rb");
if (!romfile) {
errnum = errno;
dname = g_filename_display_basename(rname);
g_set_error(err, G_FILE_ERROR,
g_file_error_from_errno(errnum),
"Unable to open %s for reading: %s",
dname, g_strerror(errnum));
g_free(dname);
g_free(rname);
g_free(sname);
return FALSE;
}
/* Open state file */
if (!sname)
sname = get_sav_name(rname);
savfile = g_fopen(sname, "rb");
if (!savfile && errno != ENOENT) {
errnum = errno;
dname = g_filename_display_basename(sname);
g_set_error(err, G_FILE_ERROR,
g_file_error_from_errno(errnum),
"Unable to open %s for reading: %s",
dname, g_strerror(errnum));
g_free(dname);
g_free(rname);
g_free(sname);
fclose(romfile);
return FALSE;
}
/* Determine model from state file, if possible */
if (!model && savfile)
model = tilem_get_sav_type(savfile);
/* Otherwise, guess from ROM file; ask user if ambiguous */
if (!model) {
model = tilem_guess_rom_type(romfile);
if (model) {
model = choose_rom_popup(get_toplevel(emu),
rname, model);
}
else {
dname = g_filename_display_basename(rname);
g_set_error(err, TILEM_EMULATOR_ERROR,
TILEM_EMULATOR_ERROR_INVALID_ROM,
"The file %s is not a recognized"
" calculator ROM file.",
dname);
g_free(dname);
}
}
if (!model) {
fclose(romfile);
if (savfile) fclose(savfile);
g_free(rname);
g_free(sname);
return FALSE;
}
/* Create new calc, and load state */
calc = tilem_calc_new(model);
if (tilem_calc_load_state(calc, romfile, savfile)) {
g_set_error(err, TILEM_EMULATOR_ERROR,
TILEM_EMULATOR_ERROR_INVALID_STATE,
"The specified ROM or state file is invalid.");
fclose(romfile);
if (savfile) fclose(savfile);
g_free(rname);
g_free(sname);
return FALSE;
}
if (!savfile) {
/* save model as default for the future */
savfile = g_fopen(sname, "wb");
if (savfile)
fprintf(savfile, "MODEL = %s\n", calc->hw.name);
}
fclose(romfile);
if (savfile) fclose(savfile);
/* Switch to new calc */
tilem_calc_emulator_lock(emu);
cancel_animation(emu);
if (emu->glcd)
tilem_gray_lcd_free(emu->glcd);
if (emu->calc)
tilem_calc_free(emu->calc);
emu->calc = calc;
emu->lcd_buffer = tilem_lcd_buffer_new();
emu->tmp_lcd_buffer = tilem_lcd_buffer_new();
if (emu->grayscale)
emu->glcd = tilem_gray_lcd_new(calc, GRAY_WINDOW_SIZE,
GRAY_SAMPLE_INT);
else
emu->glcd = NULL;
tilem_z80_add_timer(calc, MICROSEC_PER_FRAME,
MICROSEC_PER_FRAME, 1,
&tmr_screen_update, emu);
tilem_calc_emulator_unlock(emu);
if (emu->rom_file_name)
g_free(emu->rom_file_name);
emu->rom_file_name = rname;
if (emu->state_file_name)
g_free(emu->state_file_name);
emu->state_file_name = sname;
tilem_keybindings_init(emu, calc->hw.name);
if (emu->ewin)
tilem_emulator_window_calc_changed(emu->ewin);
if (emu->dbg)
tilem_debugger_calc_changed(emu->dbg);
if (emu->rcvdlg)
tilem_receive_dialog_free(emu->rcvdlg);
emu->rcvdlg = NULL;
return TRUE;
}
gboolean tilem_calc_emulator_revert_state(TilemCalcEmulator *emu, GError **err)
{
FILE *romfile, *savfile;
char *dname;
int errnum = 0;
gboolean status = TRUE;
g_return_val_if_fail(emu != NULL, FALSE);
g_return_val_if_fail(emu->calc != NULL, FALSE);
g_return_val_if_fail(emu->rom_file_name != NULL, FALSE);
g_return_val_if_fail(emu->state_file_name != NULL, FALSE);
g_return_val_if_fail(err == NULL || *err == NULL, FALSE);
/* Open ROM file */
if (emu->calc->hw.flags & TILEM_CALC_HAS_FLASH) {
romfile = g_fopen(emu->rom_file_name, "rb");
if (!romfile) {
errnum = errno;
dname = g_filename_display_basename(emu->rom_file_name);
g_set_error(err, G_FILE_ERROR,
g_file_error_from_errno(errnum),
"Unable to open %s for reading: %s",
dname, g_strerror(errnum));
g_free(dname);
return FALSE;
}
}
else {
romfile = NULL;
}
/* Open state file */
savfile = g_fopen(emu->state_file_name, "rb");
if (!savfile) {
errnum = errno;
dname = g_filename_display_basename(emu->state_file_name);
g_set_error(err, G_FILE_ERROR,
g_file_error_from_errno(errnum),
"Unable to open %s for reading: %s",
dname, g_strerror(errnum));
g_free(dname);
if (romfile) fclose(romfile);
return FALSE;
}
/* Read state */
tilem_calc_emulator_lock(emu);
if (tilem_calc_load_state(emu->calc, romfile, savfile)) {
g_set_error(err, TILEM_EMULATOR_ERROR,
TILEM_EMULATOR_ERROR_INVALID_STATE,
"The specified ROM or state file is invalid.");
status = FALSE;
}
tilem_calc_emulator_unlock(emu);
if (emu->dbg)
tilem_debugger_refresh(emu->dbg, TRUE);
if (romfile) fclose(romfile);
fclose(savfile);
return status;
}
gboolean tilem_calc_emulator_save_state(TilemCalcEmulator *emu, GError **err)
{
FILE *romfile, *savfile;
char *dname;
int errnum = 0;
g_return_val_if_fail(emu != NULL, FALSE);
g_return_val_if_fail(emu->calc != NULL, FALSE);
g_return_val_if_fail(emu->rom_file_name != NULL, FALSE);
g_return_val_if_fail(emu->state_file_name != NULL, FALSE);
g_return_val_if_fail(err == NULL || *err == NULL, FALSE);
/* Open ROM file */
if (emu->calc->hw.flags & TILEM_CALC_HAS_FLASH) {
romfile = g_fopen(emu->rom_file_name, "r+b");
if (!romfile) {
errnum = errno;
dname = g_filename_display_basename(emu->rom_file_name);
g_set_error(err, G_FILE_ERROR,
g_file_error_from_errno(errnum),
"Unable to open %s for writing: %s",
dname, g_strerror(errnum));
g_free(dname);
return FALSE;
}
}
else {
romfile = NULL;
}
/* Open state file */
savfile = g_fopen(emu->state_file_name, "wb");
if (!savfile) {
errnum = errno;
dname = g_filename_display_basename(emu->state_file_name);
g_set_error(err, G_FILE_ERROR,
g_file_error_from_errno(errnum),
"Unable to open %s for writing: %s",
dname, g_strerror(errnum));
g_free(dname);
if (romfile) fclose(romfile);
return FALSE;
}
/* Write state */
tilem_calc_emulator_lock(emu);
if (romfile && tilem_calc_save_state(emu->calc, romfile, NULL))
errnum = errno;
if (romfile && fclose(romfile))
errnum = errno;
if (errnum) {
dname = g_filename_display_basename(emu->rom_file_name);
g_set_error(err, G_FILE_ERROR,
g_file_error_from_errno(errnum),
"Error writing %s: %s",
dname, g_strerror(errnum));
g_free(dname);
fclose(savfile);
tilem_calc_emulator_unlock(emu);
return FALSE;
}
if (tilem_calc_save_state(emu->calc, NULL, savfile))
errnum = errno;
if (fclose(savfile))
errnum = errno;
tilem_calc_emulator_unlock(emu);
if (errnum) {
dname = g_filename_display_basename(emu->state_file_name);
g_set_error(err, G_FILE_ERROR,
g_file_error_from_errno(errnum),
"Error writing %s: %s",
dname, g_strerror(errnum));
g_free(dname);
return FALSE;
}
return TRUE;
}
void tilem_calc_emulator_reset(TilemCalcEmulator *emu)
{
g_return_if_fail(emu != NULL);
g_return_if_fail(emu->calc != NULL);
tilem_calc_emulator_lock(emu);
tilem_calc_reset(emu->calc);
tilem_calc_emulator_unlock(emu);
if (emu->dbg)
tilem_debugger_refresh(emu->dbg, TRUE);
}
void tilem_calc_emulator_pause(TilemCalcEmulator *emu)
{
g_return_if_fail(emu != NULL);
tilem_calc_emulator_lock(emu);
emu->paused = TRUE;
tilem_calc_emulator_unlock(emu);
}
void tilem_calc_emulator_run(TilemCalcEmulator *emu)
{
g_return_if_fail(emu != NULL);
g_return_if_fail(emu->calc != NULL);
tilem_calc_emulator_lock(emu);
emu->paused = FALSE;
tilem_calc_emulator_unlock(emu);
if (!emu->z80_thread)
emu->z80_thread = g_thread_create(&tilem_em_main, emu, TRUE, NULL);
}
void tilem_calc_emulator_set_limit_speed(TilemCalcEmulator *emu,
gboolean limit)
{
emu->limit_speed = limit;
}
void tilem_calc_emulator_set_grayscale(TilemCalcEmulator *emu,
gboolean grayscale)
{
emu->grayscale = grayscale;
if (grayscale && emu->calc && !emu->glcd) {
tilem_calc_emulator_lock(emu);
emu->glcd = tilem_gray_lcd_new(emu->calc, GRAY_WINDOW_SIZE,
GRAY_SAMPLE_INT);
tilem_calc_emulator_unlock(emu);
}
else if (!grayscale && emu->glcd) {
tilem_calc_emulator_lock(emu);
tilem_gray_lcd_free(emu->glcd);
emu->glcd = NULL;
tilem_calc_emulator_unlock(emu);
}
}
/* If currently recording a macro, record a keypress */
static void record_key(TilemCalcEmulator* emu, int code)
{
char* codechar;
int type = 0;
if (emu->isMacroRecording) {
codechar = g_strdup_printf("%04d", code);
tilem_macro_add_action(emu->macro, type, codechar);
g_free(codechar);
}
}
void tilem_calc_emulator_press_key(TilemCalcEmulator *emu, int key)
{
g_return_if_fail(emu != NULL);
if (key == 0)
return;
tilem_calc_emulator_lock(emu);
tilem_keypad_press_key(emu->calc, key);
tilem_calc_emulator_unlock(emu);
record_key(emu, key);
if (emu->dbg && emu->dbg->keypad_dialog)
tilem_keypad_dialog_refresh(emu->dbg->keypad_dialog);
}
void tilem_calc_emulator_release_key(TilemCalcEmulator *emu, int key)
{
g_return_if_fail(emu != NULL);
if (key == 0)
return;
tilem_calc_emulator_lock(emu);
tilem_keypad_release_key(emu->calc, key);
tilem_calc_emulator_unlock(emu);
if (emu->dbg && emu->dbg->keypad_dialog)
tilem_keypad_dialog_refresh(emu->dbg->keypad_dialog);
}
static gboolean refresh_kpd(gpointer data)
{
TilemCalcEmulator *emu = data;
if (emu->dbg && emu->dbg->keypad_dialog)
tilem_keypad_dialog_refresh(emu->dbg->keypad_dialog);
return FALSE;
}
/* Timer callback for key sequences */
static void tmr_key_queue(TilemCalc* calc, void* data)
{
TilemCalcEmulator *emu = data;
int nextkey;
if (emu->key_queue_pressed) {
if (emu->key_queue_len > 0 || !emu->key_queue_hold) {
tilem_keypad_release_key(calc, emu->key_queue_cur);
emu->key_queue_pressed = 0;
emu->key_queue_cur = 0;
tilem_z80_set_timer(calc, emu->key_queue_timer,
50000, 0, 1);
}
else {
tilem_z80_remove_timer(calc, emu->key_queue_timer);
emu->key_queue_timer = 0;
}
}
else {
if (emu->key_queue_len > 0) {
nextkey = emu->key_queue[--emu->key_queue_len];
tilem_keypad_press_key(calc, nextkey);
emu->key_queue_pressed = 1;
emu->key_queue_cur = nextkey;
tilem_z80_set_timer(calc, emu->key_queue_timer,
20000, 0, 1);
}
else {
tilem_z80_remove_timer(calc, emu->key_queue_timer);
emu->key_queue_timer = 0;
}
}
g_idle_add(&refresh_kpd, emu);
}
static void queue_keys(TilemCalcEmulator *emu, const byte *keys, int nkeys)
{
byte *q;
int i;
q = g_new(byte, emu->key_queue_len + nkeys);
for (i = 0; i < nkeys; i++) {
q[nkeys - i - 1] = keys[i];
record_key(emu, keys[i]);
}
if (emu->key_queue_len)
memcpy(q + nkeys, emu->key_queue, emu->key_queue_len);
g_free(emu->key_queue);
emu->key_queue = q;
emu->key_queue_len += nkeys;
emu->key_queue_hold = 1;
if (!emu->key_queue_timer) {
emu->key_queue_timer
= tilem_z80_add_timer(emu->calc, 1, 0, 1,
&tmr_key_queue, emu);
}
}
void tilem_calc_emulator_queue_keys(TilemCalcEmulator *emu,
const byte *keys, int nkeys)
{
g_return_if_fail(emu != NULL);
g_return_if_fail(keys != NULL);
g_return_if_fail(nkeys > 0);
tilem_calc_emulator_lock(emu);
queue_keys(emu, keys, nkeys);
tilem_calc_emulator_unlock(emu);
}
void tilem_calc_emulator_release_queued_key(TilemCalcEmulator *emu)
{
g_return_if_fail(emu != NULL);
tilem_calc_emulator_lock(emu);
if (emu->key_queue_timer) {
emu->key_queue_hold = 0;
}
else if (emu->key_queue_pressed) {
tilem_keypad_release_key(emu->calc, emu->key_queue_cur);
emu->key_queue_pressed = 0;
emu->key_queue_cur = 0;
}
tilem_calc_emulator_unlock(emu);
if (emu->dbg && emu->dbg->keypad_dialog)
tilem_keypad_dialog_refresh(emu->dbg->keypad_dialog);
}
gboolean tilem_calc_emulator_press_or_queue(TilemCalcEmulator *emu,
int key)
{
byte b;
gboolean status;
g_return_val_if_fail(emu != NULL, FALSE);
tilem_calc_emulator_lock(emu);
if (emu->key_queue_timer) {
b = key;
queue_keys(emu, &b, 1);
status = TRUE;
}
else {
tilem_keypad_press_key(emu->calc, key);
status = FALSE;
}
tilem_calc_emulator_unlock(emu);
if (emu->dbg && emu->dbg->keypad_dialog)
tilem_keypad_dialog_refresh(emu->dbg->keypad_dialog);
return status;
}
TilemAnimation * tilem_calc_emulator_get_screenshot(TilemCalcEmulator *emu,
gboolean grayscale)
{
TilemAnimation *anim;
g_return_val_if_fail(emu != NULL, NULL);
g_return_val_if_fail(emu->calc != NULL, NULL);
anim = tilem_animation_new(emu->calc->hw.lcdwidth,
emu->calc->hw.lcdheight);
tilem_calc_emulator_lock(emu);
if (grayscale && emu->glcd)
tilem_gray_lcd_get_frame(emu->glcd, emu->tmp_lcd_buffer);
else
tilem_lcd_get_frame(emu->calc, emu->tmp_lcd_buffer);
tilem_animation_append_frame(anim, emu->tmp_lcd_buffer, 1);
tilem_calc_emulator_unlock(emu);
return anim;
}
void tilem_calc_emulator_begin_animation(TilemCalcEmulator *emu,
gboolean grayscale)
{
g_return_if_fail(emu != NULL);
g_return_if_fail(emu->calc != NULL);
tilem_calc_emulator_lock(emu);
cancel_animation(emu);
emu->anim = tilem_animation_new(emu->calc->hw.lcdwidth,
emu->calc->hw.lcdheight);
emu->anim_grayscale = grayscale;
tilem_calc_emulator_unlock(emu);
}
TilemAnimation * tilem_calc_emulator_end_animation(TilemCalcEmulator *emu)
{
TilemAnimation *anim;
g_return_val_if_fail(emu != NULL, NULL);
g_return_val_if_fail(emu->anim != NULL, NULL);
tilem_calc_emulator_lock(emu);
anim = emu->anim;
emu->anim = NULL;
tilem_calc_emulator_unlock(emu);
return anim;
}
/* Prompt for a ROM file to open */
int tilem_calc_emulator_prompt_open_rom(TilemCalcEmulator *emu)
{
char *dir, *filename;
GError *err = NULL;
const char *modelname;
if (emu->rom_file_name)
dir = g_path_get_dirname(emu->rom_file_name);
else
dir = g_get_current_dir();
filename = prompt_open_file("Open Calculator", GTK_WINDOW(get_toplevel(emu)),
dir, "ROM files", "*.rom;*.clc;*.bin",
"All files", "*", NULL);
g_free(dir);
if (!filename)
return 0;
if (tilem_calc_emulator_load_state(emu, filename, NULL,
0, &err)) {
modelname = emu->calc->hw.name;
tilem_config_set(modelname,
"rom_file/f", emu->rom_file_name,
"state_file/f", emu->state_file_name,
NULL);
tilem_config_set("recent", "last_model/s", modelname, NULL);
}
g_free(filename);
if (err) {
messagebox01(GTK_WINDOW(get_toplevel(emu)), GTK_MESSAGE_ERROR,
"Unable to load calculator state",
"%s", err->message);
g_error_free(err);
return -1;
}
else {
return 1;
}
}
/* Run slowly to play macro (used instead run_with_key() function) */
void run_with_key_slowly(TilemCalc* calc, int key)
{
tilem_z80_run_time(calc, 5000000, NULL); /* Wait */
tilem_keypad_press_key(calc, key); /* Press */
tilem_z80_run_time(calc, 10000, NULL); /* Wait (don't forget to wait) */
tilem_keypad_release_key(calc, key); /* Release */
tilem_z80_run_time(calc, 50, NULL); /* Wait */
}
|