git.y1.nz

gbdk-2020

GameBoy Development Kit
download: https://git.y1.nz/archives/gbdk.tar.gz
README | Files | Log | Refs | LICENSE

gbdk-lib/examples/gb/wav_sample/src/sample_player.c

      1 #include "sample_player.h"
      2 
      3 UINT8 play_bank = 1;
      4 const UINT8 * play_sample = 0;
      5 UINT16 play_length = 0;
      6 
      7 void play_isr(void) __nonbanked __naked {
      8     __asm
      9         ld hl, #_play_length    ; something left to play?
     10         ld a, (hl+)
     11         or (hl)
     12         ret z
     13 
     14         ld hl, #_play_sample
     15         ld a, (hl+)
     16         ld h, (hl)
     17         ld l, a                 ; HL = current position inside the sample
     18 
     19                                 ; load new waveform
     20         ld a, (#__current_bank) ; save bank and switch
     21         ld e, a
     22         ld a, (#_play_bank)
     23         ld (_rROMB0), a
     24 
     25         ldh a, (_NR51_REG)
     26         ld c, a
     27         and #0b10111011
     28         ldh (_NR51_REG), a
     29 
     30         xor a
     31         ldh (_NR30_REG), a       
     32 
     33         .irp ofs,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
     34             ld a, (hl+)
     35             ldh (__AUD3WAVERAM+ofs), a
     36         .endm
     37 
     38         ld a, #0x80             
     39         ldh (_NR30_REG), a
     40         ld a, #0xFE             ; length of wave
     41         ldh (_NR31_REG), a
     42         ld a, #0x20             ; volume
     43         ldh (_NR32_REG), a
     44         xor a                   ; low freq bits are zero
     45         ldh (_NR33_REG), a
     46         ld a, #0xC7             ; start; no loop; high freq bits are 111
     47         ldh (_NR34_REG), a       
     48 
     49         ld a, c
     50         ldh (_NR51_REG), a
     51 
     52         ld a, e                 ; restore bank
     53         ld (_rROMB0), a
     54 
     55         ld a, l                 ; save current position
     56         ld (#_play_sample), a
     57         ld a, h
     58         ld (#_play_sample+1), a
     59 
     60         ld hl, #_play_length    ; decrement length variable
     61         ld a, (hl)
     62         sub #1
     63         ld (hl+), a
     64         ld a, (hl)
     65         sbc #0
     66         ld (hl), a
     67         ret
     68     __endasm;
     69 }
     70 
     71 void set_sample(UINT8 bank, const UINT8 * sample, UINT16 length) __critical {
     72     play_bank = bank, play_sample = sample, play_length = length >> 4;
     73 }

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.