tamalib.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * TamaLIB - A hardware agnostic Tamagotchi P1 emulation library
  3. *
  4. * Copyright (C) 2021 Jean-Christophe Rona <jc@rona.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. #include "tamalib.h"
  21. #include "hw.h"
  22. #include "cpu.h"
  23. #include "hal.h"
  24. #define DEFAULT_FRAMERATE 30 // fps
  25. static exec_mode_t exec_mode = EXEC_MODE_RUN;
  26. static u32_t step_depth = 0;
  27. static timestamp_t screen_ts = 0;
  28. static u32_t ts_freq;
  29. static u8_t g_framerate = DEFAULT_FRAMERATE;
  30. hal_t* g_hal;
  31. bool_t tamalib_init(const u12_t* program, breakpoint_t* breakpoints, u32_t freq) {
  32. bool_t res = 0;
  33. res |= cpu_init(program, breakpoints, freq);
  34. res |= hw_init();
  35. ts_freq = freq;
  36. return res;
  37. }
  38. void tamalib_release(void) {
  39. hw_release();
  40. cpu_release();
  41. }
  42. void tamalib_set_framerate(u8_t framerate) {
  43. g_framerate = framerate;
  44. }
  45. u8_t tamalib_get_framerate(void) {
  46. return g_framerate;
  47. }
  48. void tamalib_register_hal(hal_t* hal) {
  49. g_hal = hal;
  50. }
  51. void tamalib_set_exec_mode(exec_mode_t mode) {
  52. exec_mode = mode;
  53. step_depth = cpu_get_depth();
  54. cpu_sync_ref_timestamp();
  55. }
  56. void tamalib_step(void) {
  57. if(exec_mode == EXEC_MODE_PAUSE) {
  58. return;
  59. }
  60. if(cpu_step()) {
  61. exec_mode = EXEC_MODE_PAUSE;
  62. step_depth = cpu_get_depth();
  63. } else {
  64. switch(exec_mode) {
  65. case EXEC_MODE_PAUSE:
  66. case EXEC_MODE_RUN:
  67. break;
  68. case EXEC_MODE_STEP:
  69. exec_mode = EXEC_MODE_PAUSE;
  70. break;
  71. case EXEC_MODE_NEXT:
  72. if(cpu_get_depth() <= step_depth) {
  73. exec_mode = EXEC_MODE_PAUSE;
  74. step_depth = cpu_get_depth();
  75. }
  76. break;
  77. case EXEC_MODE_TO_CALL:
  78. if(cpu_get_depth() > step_depth) {
  79. exec_mode = EXEC_MODE_PAUSE;
  80. step_depth = cpu_get_depth();
  81. }
  82. break;
  83. case EXEC_MODE_TO_RET:
  84. if(cpu_get_depth() < step_depth) {
  85. exec_mode = EXEC_MODE_PAUSE;
  86. step_depth = cpu_get_depth();
  87. }
  88. break;
  89. }
  90. }
  91. }
  92. void tamalib_mainloop(void) {
  93. timestamp_t ts;
  94. while(!g_hal->handler()) {
  95. tamalib_step();
  96. /* Update the screen @ g_framerate fps */
  97. ts = g_hal->get_timestamp();
  98. if(ts - screen_ts >= ts_freq / g_framerate) {
  99. screen_ts = ts;
  100. g_hal->update_screen();
  101. }
  102. }
  103. }