#if not(ATARI_LYNX)
#warn atari lynx module should be only used on atari lynx
#endif

import joy

// TODO: mark them correctly as volatile:

word TMPADRL			@$fc00
word TILTACUM		@$fc02
word HOFF			@$fc04
word VOFF			@$fc06
word VIDBASE		@$fc08
word COLLBASE		@$fc0a
word VIDADRL		@$fc0c
word COLLADRL		@$fc0e
word SCBNEXT		@$fc10
word SPRDLINE		@$fc12 
word HPOSSTRT		@$fc14
word VPOSSTRT		@$fc16
word SPRHSIZ		@$fc18
word SPRVSIZ		@$fc1a
word STRETCH		@$fc1c
word TILT			@$fc1e
word SPRDOFF		@$fc20
word SPRVPOS		@$fc22
word COLLOFF		@$fc24
word VSIZACUM		@$fc26
word HSIZOFF		@$fc28
word VSIZOFF		@$fc2a
word SCBADR			@$fc2c
word PROCADR		@$fc2e
byte SPRCTRL0		@$fc80
byte SPRCTRL1		@$fc81
byte SPRCOLL		@$fc82
byte SPRINT 		@$fc83 

volatile byte SUZYBUSEN  @$fc90
volatile byte SPRGO      @$fc91
volatile byte SPRSYS     @$fc92
	
volatile byte JOYSTICK 		@$fcb0
volatile byte SWITCHES 		@$fcb1

word DISPADR		@$fd94
byte SUZYDONEACK 	@$fd90
byte CPUSLEEP 		@$fd91
byte DISPCTRL		@$fd92
byte PBKUP			@$fd93

byte INTSET			@$fd81	

volatile byte TIMER0_reload	@$fd00
volatile byte TIMER0_ctrl	@$fd01
volatile byte TIMER0_count	@$fd02
volatile byte TIMER0_ctrl2	@$fd03

volatile byte TIMER1_reload	@$fd04
volatile byte TIMER1_ctrl	@$fd05
volatile byte TIMER1_count	@$fd06
volatile byte TIMER1_ctrl2	@$fd07

//	video vertical position
volatile byte TIMER2_reload	@$fd08
volatile byte TIMER2_ctrl	@$fd09
volatile byte TIMER2_count	@$fd0a
volatile byte TIMER2_ctrl2	@$fd0b

volatile byte RASTER			@$fd0a

volatile byte TIMER3_reload	@$fd0c
volatile byte TIMER3_ctrl	@$fd0d
volatile byte TIMER3_count	@$fd0e
volatile byte TIMER3_ctrl2	@$fd0f

volatile byte TIMER4_reload	@$fd10
volatile byte TIMER4_ctrl	@$fd11
volatile byte TIMER4_count	@$fd12
volatile byte TIMER4_ctrl2	@$fd13

volatile byte TIMER5_reload	@$fd14
volatile byte TIMER5_ctrl	@$fd15
volatile byte TIMER5_count	@$fd16
volatile byte TIMER5_ctrl2	@$fd17

volatile byte TIMER6_reload	@$fd18
volatile byte TIMER6_ctrl	@$fd19
volatile byte TIMER6_count	@$fd1a
volatile byte TIMER6_ctrl2	@$fd1b

volatile byte TIMER7_reload	@$fd1c
volatile byte TIMER7_ctrl	@$fd1d
volatile byte TIMER7_count	@$fd1e
volatile byte TIMER7_ctrl2	@$fd1f

const byte DISPLAY_COLOR=%1000
const byte DISPLAY_4bpp=%0100
const byte DISPLAY_FLIPPED=%0010
const byte DISPLAY_ENABLE=%0001

//	suzy sprite type
const byte TYPE_BACKGROUND	=00
const byte TYPE_BACKNONCOLL	=01
const byte TYPE_BSHADOW		=02
const byte TYPE_BOUNDARY	=03
const byte TYPE_NORMAL		=04
const byte TYPE_NONCOLL		=05
const byte TYPE_XOR			=06
const byte TYPE_SHADOW		=07

//	suzy drawing flags
const byte LITERAL			=$80
const byte PACKED			=$00
const byte ALGO3			=$40
const byte RENONE			=$00
const byte REHV				=$10
const byte REHVS			=$20
const byte REHVST			=$30
const byte REUSEPAL			=$08
const byte SKIP				=$04
const byte DRAWUP			=$02
const byte DRAWLEFT			=$01

const byte BPP_1			= %00000000
const byte BPP_2			= %01000000
const byte BPP_3			= %10000000
const byte BPP_4			= %11000000
const byte HFLIP			= $20 
const byte VFLIP			= $10 

array(byte) palette_ram_green[16] @$FDA0
array(byte) palette_ram_blue_red[16] @$FDb0

//joystick

const byte BTN_A=%00000001 
const byte BTN_B=%00000010 
const byte BTN_R=%00010000 
const byte BTN_L=%00100000 
const byte BTN_D=%01000000 
const byte BTN_U=%10000000

alias input_a = input_btn
byte input_b

void lynx_reset_joy() {
	input_dx = 0
	input_dy = 0
	input_btn = 0
    input_b = 0
}
alias reset_joy = lynx_reset_joy!

inline void read_joy() {
	reset_joy()  
	if JOYSTICK & BTN_U != 0 { input_dy += 1 }
	if JOYSTICK & BTN_D != 0 { input_dy -= 1 }
	if JOYSTICK & BTN_R != 0 { input_dx += 1 }
	if JOYSTICK & BTN_L != 0 { input_dx -= 1 }

	if JOYSTICK & BTN_A != 0 { input_a = 1 }
	if JOYSTICK & BTN_B != 0 { input_b = 1 }
}

void lynx_init() {
	//	vid memory to the furthest point from code 
	//	right at the top of ram 
	DISPADR=$dc00
	VIDBASE=$dc00
	COLLBASE=$bc00 
	//	reset Horizontal and Vertical offset regs
	HOFF = 0
	VOFF = 0
	//	suzy and display 
	SPRSYS = 0
	SPRINT = $f3 
	DISPCTRL = DISPLAY_COLOR | DISPLAY_4bpp | DISPLAY_ENABLE
	SUZYBUSEN = 1
	//	set 60hz display 
	TIMER0_reload=$9e
	TIMER0_ctrl=$18 
	TIMER2_reload=$68 
	TIMER2_ctrl=$1f 
//	TIMER2_ctrl = %11011000
	PBKUP=$29
}

//	wait for suzy to finish rendering 
void lynx_wait_suzy() {
	asm {
waitsuzy:
	stz CPUSLEEP
	lda SPRSYS
	lsr
	bcs waitsuzy
	}
}
