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.

sample.c 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * sample.c
  3. * Duality
  4. *
  5. * Copyright (C) 2025 Thomas Buck <thomas@xythobuz.de>
  6. *
  7. * Based on examples from gbdk-2020:
  8. * https://github.com/gbdk-2020/gbdk-2020/tree/develop/gbdk-lib/examples/gb/wav_sample
  9. *
  10. * And the docs for the DMG APU:
  11. * https://gbdev.io/pandocs/Audio_Registers.html
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation, either version 3 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * See <http://www.gnu.org/licenses/>.
  24. */
  25. #include "sfx_shoot.h"
  26. #include "sample.h"
  27. static volatile uint8_t play_bank = 1;
  28. static volatile const uint8_t *play_sample = 0;
  29. static volatile uint16_t play_length = 0;
  30. void sample_play_shoot(void) NONBANKED {
  31. CRITICAL {
  32. play_bank = BANK(sfx_shoot);
  33. play_sample = sfx_shoot;
  34. play_length = sfx_shoot_SIZE >> 4;
  35. }
  36. }
  37. void sample_isr(void) NONBANKED NAKED {
  38. __asm
  39. ld hl, #_play_length ; something left to play?
  40. ld a, (hl+)
  41. or (hl)
  42. ret z
  43. ld hl, #_play_sample
  44. ld a, (hl+)
  45. ld h, (hl)
  46. ld l, a ; HL = current position inside the sample
  47. ; load new waveform
  48. ld a, (#__current_bank) ; save bank and switch
  49. ld e, a
  50. ld a, (#_play_bank)
  51. ld (_rROMB0), a
  52. ldh a, (_NR51_REG)
  53. ld c, a
  54. and #0b10111011
  55. ldh (_NR51_REG), a
  56. xor a
  57. ldh (_NR30_REG), a
  58. .irp ofs,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
  59. ld a, (hl+)
  60. ldh (__AUD3WAVERAM+ofs), a
  61. .endm
  62. ld a, #0x80
  63. ldh (_NR30_REG), a
  64. ld a, #0xFE ; length of wave
  65. ldh (_NR31_REG), a
  66. ld a, #0x20 ; volume
  67. ldh (_NR32_REG), a
  68. xor a ; low freq bits are zero
  69. ldh (_NR33_REG), a
  70. ld a, #0xC7 ; start; no loop; high freq bits are 111
  71. ldh (_NR34_REG), a
  72. ld a, c
  73. ldh (_NR51_REG), a
  74. ld a, e ; restore bank
  75. ld (_rROMB0), a
  76. ld a, l ; save current position
  77. ld (#_play_sample), a
  78. ld a, h
  79. ld (#_play_sample+1), a
  80. ld hl, #_play_length ; decrement length variable
  81. ld a, (hl)
  82. sub #1
  83. ld (hl+), a
  84. ld a, (hl)
  85. sbc #0
  86. ld (hl), a
  87. ret
  88. __endasm;
  89. }