                              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 	.globl	.copy_vram
                              4 
                              5 	;; BANKED:	checked
                              6 	.area	_BASE
                              7 
   0000                       8 _set_bkg_data::
   0000                       9 _set_win_data::
   0000 F0 40                10 	LDH	A,(.LCDC)
   0002 CB 67                11 	BIT	4,A
   0004 C2r4Ds00             12 	JP	NZ,_set_sprite_data
                             13 
   0007 C5                   14 	PUSH	BC
                             15 
   0008 F8 07                16 	LDA	HL,7(SP)	; Skip return address and registers
   000A 46                   17 	LD	B,(HL)		; BC = data
   000B 2B                   18 	DEC	HL
   000C 4E                   19 	LD	C,(HL)
   000D 2B                   20 	DEC	HL
   000E 5E                   21 	LD	E,(HL)		; E = nb_tiles
   000F 2B                   22 	DEC	HL
   0010 6E                   23 	LD	L,(HL)		; L = first_tile
   0011 E5                   24 	PUSH	HL
                             25 
   0012 AF                   26 	XOR	A
   0013 B3                   27 	OR	E		; Is nb_tiles == 0?
   0014 20 05                28 	JR	NZ,1$
   0016 11 00 10             29 	LD	DE,#0x1000	; DE = nb_tiles = 256
   0019 18 09                30 	JR	2$
   001B                      31 1$:
   001B 26 00                32 	LD	H,#0x00		; HL = nb_tiles
   001D 6B                   33 	LD	L,E
   001E 29                   34 	ADD	HL,HL		; HL *= 16
   001F 29                   35 	ADD	HL,HL
   0020 29                   36 	ADD	HL,HL
   0021 29                   37 	ADD	HL,HL
   0022 54                   38 	LD	D,H		; DE = nb_tiles
   0023 5D                   39 	LD	E,L
   0024                      40 2$:
   0024 E1                   41 	POP	HL		; HL = first_tile
   0025 7D                   42 	LD	A,L
   0026 07                   43 	RLCA			; Sign extend (patterns have signed numbers)
   0027 9F                   44 	SBC	A
   0028 67                   45 	LD	H,A
   0029 29                   46 	ADD	HL,HL		; HL *= 16
   002A 29                   47 	ADD	HL,HL
   002B 29                   48 	ADD	HL,HL
   002C 29                   49 	ADD	HL,HL
                             50 
   002D C5                   51 	PUSH	BC
   002E 01 00 90             52 	LD	BC,#0x9000
   0031 09                   53 	ADD	HL,BC
   0032 C1                   54 	POP	BC
                             55 
   0033                      56 3$:				; Special version of '.copy_vram'
   0033 CB 5C                57 	BIT	3,H		; Bigger than 0x9800
   0035 28 06                58 	JR	Z,4$
   0037 CB 64                59 	BIT	4,H
   0039 28 02                60 	JR	Z,4$
   003B CB A4                61 	RES	4,H		; Switch to 0x8800
   003D                      62 4$:
   003D F0 41                63 	LDH	A,(.STAT)
   003F E6 02                64 	AND	#0x02
   0041 20 FA                65 	JR	NZ,4$
                             66 
   0043 0A                   67 	LD	A,(BC)
   0044 22                   68 	LD	(HL+),A
   0045 03                   69 	INC	BC
   0046 1B                   70 	DEC	DE
   0047 7A                   71 	LD	A,D
   0048 B3                   72 	OR	E
   0049 20 E8                73 	JR	NZ,3$
                             74 
   004B C1                   75 	POP	BC
   004C C9                   76 	RET
                             77 
   004D                      78 _set_sprite_data::
   004D C5                   79 	PUSH	BC
                             80 
   004E F8 07                81 	LDA	HL,7(SP)	; Skip return address and registers
   0050 46                   82 	LD	B,(HL)		; BC = data
   0051 2B                   83 	DEC	HL
   0052 4E                   84 	LD	C,(HL)
   0053 2B                   85 	DEC	HL
   0054 5E                   86 	LD	E,(HL)		; E = nb_tiles
   0055 2B                   87 	DEC	HL
   0056 6E                   88 	LD	L,(HL)		; L = first_tile
   0057 E5                   89 	PUSH	HL
                             90 
   0058 AF                   91 	XOR	A
   0059 B3                   92 	OR	E		; Is nb_tiles == 0?
   005A 20 05                93 	JR	NZ,1$
   005C 11 00 10             94 	LD	DE,#0x1000	; DE = nb_tiles = 256
   005F 18 09                95 	JR	2$
   0061                      96 1$:
   0061 26 00                97 	LD	H,#0x00		; HL = nb_tiles
   0063 6B                   98 	LD	L,E
   0064 29                   99 	ADD	HL,HL		; HL *= 16
   0065 29                  100 	ADD	HL,HL
   0066 29                  101 	ADD	HL,HL
   0067 29                  102 	ADD	HL,HL
   0068 54                  103 	LD	D,H		; DE = nb_tiles
   0069 5D                  104 	LD	E,L
   006A                     105 2$:
   006A E1                  106 	POP	HL		; HL = first_tile
   006B 26 00               107 	LD	H,#0x00
   006D 29                  108 	ADD	HL,HL		; HL *= 16
   006E 29                  109 	ADD	HL,HL
   006F 29                  110 	ADD	HL,HL
   0070 29                  111 	ADD	HL,HL
                            112 
   0071 C5                  113 	PUSH	BC
   0072 01 00 80            114 	LD	BC,#0x8000
   0075 09                  115 	ADD	HL,BC
   0076 C1                  116 	POP	BC
                            117 
   0077 CDr00s00            118 	CALL	.copy_vram
                            119 
   007A C1                  120 	POP	BC
   007B C9                  121 	RET
