                              1 	.include "global.s"
                    0001      1 	.NEAR_CALLS = 1         ; <near_calls> - tag so that sed can change this
                              2         
                              3 	;; Changed by astorgb.pl to 1
                    0000      4 	__RGBDS__	= 0
                              5 
                              6 	
                              7 	;;  Screen dimensions 
                    0013      8 	.MAXCURSPOSX	= 0x13	; In tiles
                    0011      9 	.MAXCURSPOSY	= 0x11
                             10 
                    0080     11 	.START		= 0x80
                    0040     12 	.SELECT		= 0x40
                    0020     13 	.B		= 0x20
                    0010     14 	.A		= 0x10
                    0008     15 	.DOWN		= 0x08
                    0004     16 	.UP		= 0x04
                    0002     17 	.LEFT		= 0x02
                    0001     18 	.RIGHT		= 0x01
                             19 
                    00A0     20 	.SCREENWIDTH	= 0xA0
                    0090     21 	.SCREENHEIGHT	= 0x90
                    0007     22 	.MINWNDPOSX	= 0x07
                    0000     23 	.MINWNDPOSY	= 0x00
                    00A6     24 	.MAXWNDPOSX	= 0xA6
                    008F     25 	.MAXWNDPOSY	= 0x8F
                             26 
                    0001     27 	.VBL_IFLAG	= 0x01
                    0002     28 	.LCD_IFLAG	= 0x02
                    0004     29 	.TIM_IFLAG	= 0x04
                    0008     30 	.SIO_IFLAG	= 0x08
                    0010     31 	.JOY_IFLAG	= 0x10
                             32  
                    0000     33 	.P1		= 0x00	; Joystick: 1.1.P15.P14.P13.P12.P11.P10
                    0001     34 	.SB		= 0x01	; Serial IO data buffer
                    0002     35 	.SC		= 0x02	; Serial IO control register
                    0004     36 	.DIV		= 0x04	; Divider register
                    0005     37 	.TIMA		= 0x05	; Timer counter
                    0006     38 	.TMA		= 0x06	; Timer modulo
                    0007     39 	.TAC		= 0x07	; Timer control
                    000F     40 	.IF		= 0x0F	; Interrupt flags: 0.0.0.JST.SIO.TIM.LCD.VBL
                    0010     41 	.NR10		= 0x10	; Sound register
                    0011     42 	.NR11		= 0x11	; Sound register
                    0012     43 	.NR12		= 0x12	; Sound register
                    0013     44 	.NR13		= 0x13	; Sound register
                    0014     45 	.NR14		= 0x14	; Sound register
                    0016     46 	.NR21		= 0x16	; Sound register
                    0017     47 	.NR22		= 0x17	; Sound register
                    0018     48 	.NR23		= 0x18	; Sound register
                    0019     49 	.NR24		= 0x19	; Sound register
                    001A     50 	.NR30		= 0x1A	; Sound register
                    001B     51 	.NR31		= 0x1B	; Sound register
                    001C     52 	.NR32		= 0x1C	; Sound register
                    001D     53 	.NR33		= 0x1D	; Sound register
                    001E     54 	.NR34		= 0x1E	; Sound register
                    0020     55 	.NR41		= 0x20	; Sound register
                    0021     56 	.NR42		= 0x21	; Sound register
                    0022     57 	.NR43		= 0x22	; Sound register
                    0023     58 	.NR44		= 0x23	; Sound register
                    0024     59 	.NR50		= 0x24	; Sound register
                    0025     60 	.NR51		= 0x25	; Sound register
                    0026     61 	.NR52		= 0x26	; Sound register
                    0040     62 	.LCDC		= 0x40	; LCD control
                    0041     63 	.STAT		= 0x41	; LCD status
                    0042     64 	.SCY		= 0x42	; Scroll Y
                    0043     65 	.SCX		= 0x43	; Scroll X
                    0044     66 	.LY		= 0x44	; LCDC Y-coordinate
                    0045     67 	.LYC		= 0x45	; LY compare
                    0046     68 	.DMA		= 0x46	; DMA transfer
                    0047     69 	.BGP		= 0x47	; BG palette data
                    0048     70 	.OBP0		= 0x48	; OBJ palette 0 data
                    0049     71 	.OBP1		= 0x49	; OBJ palette 1 data
                    004A     72 	.WY		= 0x4A	; Window Y coordinate
                    004B     73 	.WX		= 0x4B	; Window X coordinate
                    004D     74 	.KEY1		= 0x4D	; CPU speed
                    004F     75 	.VBK		= 0x4F	; VRAM bank
                    0051     76 	.HDMA1		= 0x51	; DMA control 1
                    0052     77 	.HDMA2		= 0x52	; DMA control 2
                    0053     78 	.HDMA3		= 0x53	; DMA control 3
                    0054     79 	.HDMA4		= 0x54	; DMA control 4
                    0055     80 	.HDMA5		= 0x55	; DMA control 5
                    0056     81 	.RP		= 0x56	; IR port
                    0068     82 	.BCPS		= 0x68	; BG color palette specification
                    0069     83 	.BCPD		= 0x69	; BG color palette data
                    006A     84 	.OCPS		= 0x6A	; OBJ color palette specification
                    006B     85 	.OCPD		= 0x6B	; OBJ color palette data
                    0070     86 	.SVBK		= 0x70	; WRAM bank
                    00FF     87 	.IE		= 0xFF	; Interrupt enable
                             88 
                    0001     89 	.G_MODE		= 0x01	; Graphic mode
                    0002     90 	.T_MODE		= 0x02	; Text mode (bit 2)
                    0002     91 	.T_MODE_OUT	= 0x02	; Text mode output only
                    0003     92 	.T_MODE_INOUT	= 0x03	; Text mode with input
                    0004     93 	.M_NO_SCROLL	= 0x04	; Disables scrolling of the screen in text mode
                    0008     94 	.M_NO_INTERP	= 0x08	; Disables special character interpretation
                             95 
                    2000     96 	.MBC1_ROM_PAGE	= 0x2000 ; Address to write to for MBC1 switching
                             97 	
                             98 	;; Status codes for IO
                    0000     99 	.IO_IDLE	= 0x00
                    0001    100 	.IO_SENDING	= 0x01
                    0002    101 	.IO_RECEIVING	= 0x02
                    0004    102 	.IO_ERROR	= 0x04
                            103 
                            104 	;; Type of IO data
                    0066    105 	.DT_IDLE	= 0x66
                    0055    106 	.DT_RECEIVING	= 0x55
                            107 
                            108 	;; Table of routines for modes
                    01E0    109 	.MODE_TABLE	= 0x01E0
                            110 
                            111 	;; C related
                            112 	;; Overheap of a banked call.  Used for parameters
                            113 	;;  = ret + real ret + bank
                            114 
                    0001    115 	.if .NEAR_CALLS
                    0002    116 	.BANKOV		= 2
                            117 
                            118 	.else
                            119 	.BANKOV		= 6
                            120 
                            121 	.endif
                            122 	
                            123 	.globl	banked_call
                            124 	.globl	banked_ret
                            125 	
                            126 	;; Global variables
                            127 	.globl	.mode
                            128 
                            129 	.globl	__cpu
                            130 
                            131 	.globl	__io_out
                            132 	.globl	__io_in
                            133 	.globl	__io_status
                            134 
                            135 	;; Global routines
                            136 	.globl	.set_mode
                            137 
                            138 	.globl	.reset
                            139 
                            140 	.globl	.display_off
                            141 
                            142 	.globl	.wait_vbl_done
                            143 
                            144 	;; Interrupt routines 
                            145 	.globl	.add_VBL
                            146 	.globl	.add_LCD
                            147 	.globl	.add_TIM
                            148 	.globl	.add_SIO
                            149 	.globl	.add_JOY
                            150 
                            151 	;; Symbols defined at link time
                            152 	.globl	.STACK
                            153 	.globl	.OAM
                            154 	.globl	.refresh_OAM
                            155 
                            156 	;; Initialization routine   
                            157 	.globl	.init
                            158 
                            159 	;; Main user routine	
                            160 	.globl	_main
                              2 
                              3 	;; BANKED:	checked
                              4 	.title "Sound sample player"
                              5 	.module Sample
                              6 
                              7 	.area	_BASE
                    FF30      8 	.AUD3WAVERAM = 0xff30
                              9 
   0000                      10 _play_sample::
   0000 C5                   11   push bc
   0001 F8 04                12   lda hl,4(sp)
   0003 2A                   13   ld a,(hl+)
   0004 56                   14   ld d,(hl)
   0005 5F                   15   ld e,a
                             16 
   0006 F8 06                17   lda hl,6(sp)
   0008 2A                   18   ld a,(hl+)
   0009 46                   19   ld b,(hl)
   000A 4F                   20   ld c,a
                             21 
   000B 62                   22   ld h,d
   000C 6B                   23   ld l,e
                             24 
   000D CDr12s00             25   call .play_sample
   0010 C1                   26   pop bc
   0011 C9                   27   ret
                             28 
                             29 ; Playback raw sound sample with length BC from HL at 8192Hz rate.
                             30 ; BC defines the length of the sample in samples/32 or bytes/16.
                             31 ; The format of the data is unsigned 4-bit samples,
                             32 ; 2 samples per byte, upper 4-bits played before lower 4 bits.
                             33 ;
                             34 ; Adaption for GBDK by Lars Malmborg.
                             35 ; Original code by Jeff Frohwein.
                             36 
   0012                      37 .play_sample::
   0012 3E 84                38   ld  a,#0x84
   0014 E0 26                39   ldh (.NR52),a       ;enable sound 3
                             40 
   0016 3E 00                41   ld a,#0
   0018 E0 1A                42   ldh (.NR30),a
   001A E0 25                43   ldh (.NR51),a
                             44 
   001C 3E 77                45   ld a,#0x77
   001E E0 24                46   ldh (.NR50),a       ;select speakers
   0020 3E FF                47   ld a,#0xff
   0022 E0 25                48   ldh (.NR51),a       ;enable sound 3
                             49 
   0024 3E 80                50   ld a,#0x80
   0026 E0 1B                51   ldh (.NR31),a       ;sound length
   0028 3E 20                52   ld a,#0x20
   002A E0 1C                53   ldh (.NR32),a       ;sound level high
                             54 
   002C 3E 00                55   ld a,#0x00
   002E E0 1D                56   ldh (.NR33),a       ;sound freq low
                             57 
   0030                      58 .samp2:
   0030 11 30 FF             59   ld de,#.AUD3WAVERAM ;12
   0033 C5                   60   push bc             ;16
   0034 06 10                61   ld b,#16            ;16
                             62 
   0036 AF                   63   xor a
   0037 E0 1A                64   ldh (.NR30),a
   0039                      65 .samp3:
   0039 2A                   66   ld a,(hl+)          ;8
   003A 12                   67   ld (de),a           ;8
   003B 13                   68   inc de              ;8
   003C 05                   69   dec b               ;4
   003D 20 FA                70   jr nz,.samp3        ;12
                             71 
   003F 3E 80                72   ld a,#0x80
   0041 E0 1A                73   ldh (.NR30),a
                             74 
   0043 3E 87                75   ld a,#0x87          ; (256hz)
   0045 E0 1E                76   ldh (.NR34),a
                             77 
   0047 01 2E 02             78   ld bc,#558          ;delay routine
   004A                      79 .samp4:
   004A 0B                   80   dec bc              ;8
   004B 78                   81   ld a,b              ;4
   004C B1                   82   or c                ;4
   004D 20 FB                83   jr nz,.samp4        ;12
                             84 
   004F 3E 00                85   ld a,#0             ;more delay
   0051 3E 00                86   ld a,#0
   0053 3E 00                87   ld a,#0
                             88 
   0055 C1                   89   pop bc              ;12
   0056 0B                   90   dec bc              ;8
   0057 78                   91   ld a,b              ;4
   0058 B1                   92   or c                ;4
   0059 20 D5                93   jr nz,.samp2        ;12
                             94 
   005B 3E BB                95   ld a,#0xbb
   005D E0 25                96   ldh (.NR51),a       ;disable sound 3
   005F C9                   97   ret
