;***** R-TYPE 128K globals and constants *****

;rendering mode 0/1

r_mode		EQU 0

immort_duration	EQU 50	;immortality duration approx 2sec


;heap

heap		EQU #C000

font_size	EQU #400
font		EQU heap-font_size

color_table_options_normal_size	EQU	#100
color_table_options_normal	EQU font-color_table_options_normal_size

color_table_options_red_size	EQU	#100
color_table_options_red	EQU color_table_options_normal-color_table_options_red_size

color_table_title_bold_size	EQU	#100
color_table_title_bold	EQU color_table_options_normal

color_table_game_text_size EQU #100
color_table_game_text EQU color_table_options_red-color_table_game_text_size

;512 bytes for global vars

global_vars_size	EQU #200
global_vars			EQU color_table_game_text-global_vars_size

keys				EQU global_vars+ 0		;keymap +10			;size 6


;original globals from game
read "globals_org.asm"

;added gauge GFX in globals
GAUGE_FULL	EQU color_table_game_text-16
GAUGE_8		EQU	GAUGE_FULL-16
GAUGE_7		EQU GAUGE_8-16
GAUGE_6		EQU GAUGE_7-16
GAUGE_5		EQU GAUGE_6-16
GAUGE_4		EQU GAUGE_5-16
GAUGE_3		EQU GAUGE_4-16
GAUGE_2		EQU GAUGE_3-16
GAUGE_1		EQU GAUGE_2-16
GAUGE_0		EQU GAUGE_1-16
GAUGE_Right	EQU GAUGE_0-16
GAUGE_Left	EQU GAUGE_Right-16

;jump table for renderer
rend_table_size		EQU #200
rend_table			EQU global_vars-rend_table_size
rend_table_low		EQU rend_table/256
;actually we have #80 free bytes at rend_table + #80 and rend_table+#180

;pixel reverse table
rev_table_size		EQU #100
rev_table			EQU rend_table-rev_table_size
rev_table_high		EQU rev_table/256

;seq buffer
SEQBUFF				EQU rev_table-256
SEQBUFF_high		EQU SEQBUFF/256

;aligned arrays (be sure they are aligned !) get 2 blocks
BULLMTABLE			EQU SEQBUFF-256			;size 48 total = 48
ALIENFIRE			EQU BULLMTABLE+48		;size 84 total = 132
PILLBUFF			EQU ALIENFIRE+84		;size 8 total = 140 DEFS 8,0
PLASBUFF			EQU PILLBUFF+8			;size 3 total = 143 DEFS 3,0
CIRCBUFF1			EQU PLASBUFF+3			;size 4 total = 147 DEFS 4,0
CIRCBUFF2			EQU CIRCBUFF1+4			;size 4 total = 151 DEFS 4,0
EXPLOBUFF			EQU CIRCBUFF2+4			;size 60 total = 211
BULLETS_Table		EQU	EXPLOBUFF+60		;size 32 total = 243
keymap				EQU BULLETS_Table+32	;size 10 total = 253 global_vars +0		;size 10



LaserMove_Table		EQU	BULLMTABLE-256	;size 16 total = 16
LaserMoveTable_Low	EQU	LaserMove_Table AND 255
LaserMoveTable_High	EQU	LaserMove_Table / 256

STARS_Sprite		EQU LaserMove_Table+16	;stars sprite , 16 bytes , total 32
;BG effects share the same buffer !
;stars buffer 2*8*4 = 64
STARS_BufferA		EQU STARS_Sprite+32
STARS_BufferB		EQU STARS_BufferA+32
;BG elements 16*3 = 48 (max 64 bytes)
BG_Buffer			EQU STARS_BufferA
;save buffer for BG , 64 bytes
save_BG_Buffer		EQU BG_Buffer+64

;************************************
;*** ! PAGE FULL - DO NOT MODIFY ! ***
;************************************
;laser buffer is 10*3*5 = 160
LaserBufferSize		EQU 160
LaserBuffer			EQU	LaserMove_Table-256
LaserBuffer_High	EQU LaserBuffer / 256
;circular laser buffer is 4*8 = 32
CircularBuffer		EQU	LaserBuffer+LaserBufferSize
CircularBuffer_Low	EQU CircularBuffer AND 255
CircularBuffer_High	EQU CircularBuffer / 256
;missile 2=6 = 12 , total 204
MISS1BUFF			EQU CircularBuffer+32 ;DEFS 6,0
MISS2BUFF			EQU MISS1BUFF+6 ;DEFS 6,0
;wave cannon , 4*4 bytes = 16 , total 220
WaveBufferB1		EQU MISS2BUFF+6
WaveBufferB2		EQU	WaveBufferB1+4
WaveBufferR1		EQU	WaveBufferB2+4
WaveBufferR2		EQU	WaveBufferR1+4
;fire animation buffer 10 bytes , total 230
Fire_lopow_sprite	EQU WaveBufferR2+4 ;5*2 = 10 bytes



;TOTAL SIZE = 256

;low address for the heap
heap_low	EQU LaserBuffer


;sound player interface
SND_Run					EQU #4010
SND_Player				EQU SND_Run+3
SND_Stop				EQU SND_Player+3
SND_Skip				EQU	SND_Stop+3
SND_Volume				EQU SND_Skip+3
SFX_Start				EQU SND_Volume+3
SFX_Stop				EQU SFX_Start+3
SFX_StopAll				EQU SFX_Stop+3
SFX_Play				EQU SFX_StopAll+3
SFX_SetChanMode			EQU SFX_Play+3
SFX_SetForcedChannel	EQU SFX_SetChanMode+3
SND_Pause				EQU SFX_SetForcedChannel+3
SND_Unpause				EQU SND_Pause+3

;MASTER that does not collide
NeutralMaster			EQU 64

;scr address for hud objects
HUD_POS					EQU #C540

LIFE_POS				EQU HUD_POS
BEAM_POS				EQU LIFE_POS+16
GAUGE_POS				EQU BEAM_POS+8

SCORE_POS				EQU LIFE_POS+#46 ;GAUGE_POS+28
HI_POS					EQU SCORE_POS+#10

;CRTC

macro SetCrtcReg reg,val
	ld C,reg
	out (C),C
	inc B
	ld A,val
	out (C),A
	dec B
mend

;***** macros *****

;change bank already in A
macro change_bank
	rst 8
mend

;change bank in A
macro change_bankA bank
	ld A,bank
	rst 8
mend

;change mode in A
macro change_mode mode
	ld A,mode
	rst 16
mend

;change bank located in memory
macro change_bankM bank
	rst 24
	db bank
mend




;indirect call subroutine if not null
macro call_indirect func
	ld HL,(func)
	call MAIN_Indirect
mend
	


;Force enemy slot
macro FORCE_Slot	slot,tile,pos,id,x,y
	db	tile,pos,99,3,slot
	db	tile,pos,id,x,y
mend

;Set BG color
macro	LVL_BGcolor	tile,pos,color
	db tile,pos,99,12,color
mend

;Set FL color
macro	LVL_FLcolor	tile,pos,color
	db tile,pos,99,16,color
mend

;level play music
macro	LVL_Music	tile,pos,music
	db tile,pos,99,11,music
mend

macro	LVL_SetSpec1	tile,pos,value
	db tile,pos,99,9,value
mend

macro	LVL_SetSpec2	tile,pos,value
	db tile,pos,99,10,value
mend

macro	LVL_Do	tile,pos,count
	db tile,pos,99,7,count
mend

macro	LVL_Loop	tile,pos
	db tile,pos,99,8,0
mend

macro	SCROLL_Stop	tile,pos
	db	tile,pos,99,1,0
mend

macro	CHECK_POINT	tile,pos
	db tile,pos,99,13,0
mend

macro	START_EFFECT tile,pos,effect
	db tile,pos,99,14,effect
mend

macro	STOP_EFFECT	tile,pos
	db tile,pos,99,15,0
mend

;set master for collisions
macro	SetMaster mastr
	ld A,mastr
	ld (MASTER),A
mend

macro SetNullMaster
	xor A
	ld (MASTER),A
mend

macro SaveMaster
	ld A,(MASTER)
	push AF
mend

macro RestoreMaster
	pop AF
	ld (MASTER),A
mend

;set callback for collisions
macro SetCallback callbck
	ld HL,callbck
	call REND_SprSetCallBack
mend

;set INT table
macro SetIntTable	tbl
	ld HL,tbl
	ld (int_table),HL
mend

;*** breakpoint ***
macro break
	db #ED,#FF
mend

;*** charset ***

macro	charset_on
	charset ' ','z',0
mend

macro	charset_off
	charset
mend

;*** array attribute ***

ATTR_Line_Base	EQU #8000
ATTR_Addr		EQU ATTR_Line_Base+#A80
ATTR_Addr_high	EQU ATTR_Addr/256
ATTR_Addr_low	EQU ATTR_Addr AND 255

;renderering

;****************
;** erase char **
;****************
;use axelay optimisation

macro erase_char

	xor A
	
	;line 0
	ld (DE),A
	inc E
	ld (DE),A
	set 3,D
	
	;line 1
	ld (DE),A
	dec E
	ld (DE),A
	set 4,D
	
	;line 3
	ld (DE),A
	inc E
	ld (DE),A
	res 3,D
	
	;line 2
	ld (DE),A
	dec E
	ld (DE),A
	set 5,D

	;line 6
	ld (DE),A
	inc E
	ld (DE),A
	set 3,D
	
	;line 7
	ld (DE),A
	dec E
	ld (DE),A
	res 4,D
	
	;line 5
	ld (DE),A
	inc E
	ld (DE),A
	res 3,D
	
	;line 4
	ld (DE),A
	dec E
	ld (DE),A
	
	;extra reset DE
	;ld A,%11000111
	;and D
	;ld D,A
	;only reset bit 5
	res 5,D
mend	

;***********************
;** draw X char reversed **
;***********************

macro draw_line_revX_pair
	ld C,(HL)
	ld A,(BC)
	ld (DE),A
	inc L
	dec E
	ld C,(HL)
	ld A,(BC)
	ld (DE),A
	inc L
mend

macro draw_line_revX_unpair
	ld C,(HL)
	ld A,(BC)
	ld (DE),A
	inc L
	inc E
	ld C,(HL)
	ld A,(BC)
	ld (DE),A
	inc L
mend

macro draw_line_revX_unpair_last
	ld C,(HL)
	ld A,(BC)
	ld (DE),A
	inc L
	inc E
	ld C,(HL)
	ld A,(BC)
	ld (DE),A
	dec E
mend

macro draw_char_revX
	draw_line_revX_pair
	set 3,D
	draw_line_revX_unpair
	set 4,D
	draw_line_revX_pair
	res 3,D
	draw_line_revX_unpair
	set 5,D
	draw_line_revX_pair
	set 3,D
	draw_line_revX_unpair
	res 4,D
	draw_line_revX_pair
	res 3,D
	draw_line_revX_unpair_last
;extra reset DE
	;ld A,%11000111
	;and D
	;ld D,A
	res 5,D
mend

macro draw_char_revXY
	draw_line_revX_pair
	res 3,D
	draw_line_revX_unpair
	ld A,-8
	add D
	ld D,A
	
	draw_line_revX_pair
	res 3,D
	draw_line_revX_unpair
	ld A,-8
	add D
	ld D,A
	
	draw_line_revX_pair
	res 3,D
	draw_line_revX_unpair
	ld A,-8
	add D
	ld D,A
	
	draw_line_revX_pair
	res 3,D
	draw_line_revX_unpair_last
mend

macro draw_char_revXY_opt

	draw_line_revX_pair					;starting a line 7
	res 3,D				
	draw_line_revX_unpair				;line 6
	res 4,D					
	draw_line_revX_pair					;line 4
	set 3,D
	draw_line_revX_unpair				;line 5
	res 5,D	
	draw_line_revX_pair					;line 1
	res 3,D
	draw_line_revX_unpair				;line 0
	set 4,D
	draw_line_revX_pair					;line 2
	set 3,D
	draw_line_revX_unpair_last			;line 3
	
	ld A,%11000111					;return to line 0
	and D
	ld D,A

mend


;**************
;** draw char **
;**************

macro draw_line_pair
	ld A,(HL)
	ld (DE),A
	inc L
	inc E
	ld A,(HL)
	ld (DE),A
	inc L
mend

macro draw_line_pair_last
	ld A,(HL)
	ld (DE),A
	inc L
	inc E
	ld A,(HL)
	ld (DE),A
mend

macro draw_line_unpair
	ld A,(HL)
	ld (DE),A
	inc L
	dec E
	ld A,(HL)
	ld (DE),A
	inc L
mend

macro draw_line_unpair_last
	ld A,(HL)
	ld (DE),A
	inc L
	dec E
	ld A,(HL)
	ld (DE),A
mend

macro draw_char
	draw_line_pair
	set 3,D
	draw_line_unpair
	ld A,8
	add D
	ld D,A
	draw_line_pair
	set 3,D
	draw_line_unpair
	ld A,8
	add D
	ld D,A
	draw_line_pair
	set 3,D
	draw_line_unpair
	ld A,8
	add D
	ld D,A
	draw_line_pair
	set 3,D
	draw_line_unpair_last
;extra reset DE
	ld A,%11000111
	and D
	ld D,A
mend

macro draw_char_opt
	draw_line_pair			;=0
	set 3,D  				;+1
	draw_line_unpair		;=1
	set 4,D					;+2  	
	draw_line_pair			;=3
	res 3,D					;-1	
	draw_line_unpair		;=2
	set 5,D					;+4
	draw_line_pair			;=6
	set 3,D					;+1
	draw_line_unpair		;=7
	res 4,D					;-2
	draw_line_pair			;=5
	res 3,D					;-1
	draw_line_unpair_last	;=4
;extra reset DE
	;ld A,%11000111
	;and D
	;ld D,A
	res 5,D
mend

;**************************
;** draw char Y reversed **
;**************************

macro draw_char_Yrev
	draw_line_pair
	res 3,D
	draw_line_unpair
	ld A,-8
	add D
	ld D,A
	
	draw_line_pair
	res 3,D
	draw_line_unpair
	ld A,-8
	add D
	ld D,A
	
	draw_line_pair
	res 3,D
	draw_line_unpair
	ld A,-8
	add D
	ld D,A
	
	draw_line_pair
	res 3,D
	draw_line_unpair_last

mend

macro draw_char_Yrev_opt

	draw_line_pair					;starting a line 7
	res 3,D				
	draw_line_unpair				;line 6
	res 4,D					
	draw_line_pair					;line 4
	set 3,D
	draw_line_unpair				;line 5
	res 5,D	
	draw_line_pair					;line 1
	res 3,D
	draw_line_unpair				;line 0
	set 4,D
	draw_line_pair					;line 2
	set 3,D
	draw_line_unpair_last			;line 3
	
	ld A,%11000111					;return to line 0
	and D
	ld D,A

mend

;***************
;** draw STAR **
;***************
;axelay optimisation
macro draw_star

	ld A,(HL)	;line 0
	ld (DE),A
	inc L
	inc E
	ld A,(HL)
	ld (DE),A	
	set 3,D
	
	xor A
	
	;line 1
	ld (DE),A
	dec E
	ld (DE),A
	set 4,D
	
	;line 3
	ld (DE),A
	inc E
	ld (DE),A
	res 3,D
	
	;line 2
	ld (DE),A
	dec E
	ld (DE),A
	set 5,D

	;line 6
	ld (DE),A
	inc E
	ld (DE),A
	set 3,D
	
	;line 7
	ld (DE),A
	dec E
	ld (DE),A
	res 4,D
	
	;line 5
	ld (DE),A
	inc E
	ld (DE),A
	res 3,D
	
	;line 4
	ld (DE),A
	dec E
	ld (DE),A
	
;extra reset DE
	;ld A,%11000111
	;and D
	;ld D,A	
	res 5,D

mend