GameBoy (Color) port of the GTA San Andreas arcade game Duality
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

timer.c 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * timer.c
  3. * Duality
  4. *
  5. * Copyright (C) 2025 Thomas Buck <thomas@xythobuz.de>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * See <http://www.gnu.org/licenses/>.
  18. */
  19. #include "sample.h"
  20. #include "sound.h"
  21. #include "timer.h"
  22. static uint16_t count = 0;
  23. static void timer_isr(void) NONBANKED {
  24. if ((count & 0x03) == 0) {
  25. sample_isr();
  26. }
  27. snd_play();
  28. count++;
  29. }
  30. void timer_init(void) BANKED {
  31. CRITICAL {
  32. count = 0;
  33. add_TIM(timer_isr);
  34. if (_cpu == CGB_TYPE) {
  35. TMA_REG = 0x100 - 131; // 131.072kHz / 131 = ~1000Hz
  36. } else {
  37. TMA_REG = 0x100 - 65; // 65.536kHz / 65 = ~1008Hz
  38. }
  39. TAC_REG = TACF_65KHZ | TACF_START;
  40. set_interrupts(TIM_IFLAG | VBL_IFLAG);
  41. }
  42. }
  43. uint16_t timer_get(void) BANKED {
  44. uint16_t r;
  45. CRITICAL {
  46. r = count;
  47. }
  48. return r;
  49. }