.include "testing.s"
.include "minimal_ppu.s"

reset:
	sei
	cld
	ldx #$FF
	txs

	lda #$01
	sta SNDCHN
	lda #$BF ; Duty 2, LC halted, Constant volume, volume = F
	sta PULSE_CH1_DLCV
	lda #$7F ; 
	sta PULSE_CH1_SWEEP
	lda #$6F
	sta PULSE_CH1_TLOW
	sta TIMER_LOW
	lda #$00
	sta PULSE_CH1_LCTH
; PULSE_CH1_DLCV = $4000
; PULSE_CH1_SWEEP = $4001
; PULSE_CH1_TLOW = $4002
; PULSE_CH1_LCTH = $4003

	jmp init_ppu_and_wait

; zp_res 
zp_res TIMER_LOW
update_audio:
	; adc TIMER_LOW
	; sta PULSE_CH1_TLOW
	; sta TIMER_LOW

	rts

zp_res PRESSED_A
zp_res PRESSED_B

nmi:
	pha
	txa
	pha
	tya
	pha
	lda PPUSTATUS
	lda #%00000110
	sta PPUMASK ; Force blank

	lda #$01
	sta JOY1
	lda #$00
	sta JOY1

	lda JOY1 ; A
	and #$01
	tax
	eor PRESSED_A
	beq done
	stx PRESSED_A
	txa
	and #$01
	beq done

	lda #$08
	jsr update_audio

done:
	bit JOY1 ; B
	and #$01
	tax
	eor PRESSED_B
	beq done_b
	stx PRESSED_B
	txa
	and #$01
	beq done_b

	lda #$F7
	jsr update_audio

done_b:
	bit JOY1 ; Select
	bit JOY1 ; Start
	bit JOY1 ; Up
	bit JOY1 ; Down
	bit JOY1 ; Left
	bit JOY1 ; Right

	lda #$00
	sta PPUSCROLL
	sta PPUSCROLL
	
	lda #%00001110
	sta PPUMASK ; Enable rendering
	pla
	tay
	pla
	tax
	pla
	rti

irq:
	rti
