.include "testing.s"

.macro chr b,s
.repeat s
.byte b
.endrepeat
.endmacro

.pushseg
.segment "CHARS"
chr $FF,16 ; Full
chr $00,8
chr $FF,8 ; Gray
chr $55,16 ; Vertical stripes
chr $AA,16 ; Vertical stripes - reverse
.repeat 8
.byte $FF
.byte $00
.endrepeat ; Horizontal stripes
.repeat 8
.byte $00
.byte $FF
.endrepeat ; Horizontal stripes - reverse
.repeat 8
.byte $55
.byte $AA
.endrepeat ; Checkerboard
.repeat 8
.byte $AA
.byte $55
.endrepeat ; Checkerboard - reverse
.popseg

zp_res CUR
zp_res POS

reset:
        sei ; Ignore IRQs while starting up
        cld ; disabled decimal mode (iirc it doesn't work properly on NES anyway)
        ldx #$40
        stx $4017 ; Disable APU frame IRQ
        ldx #$ff
        txs ; Set stack pointer to 0x1ff
        inx ; Set x to zero
        stx $2000 ; Disable NMI (by writing zero)
        stx $4010 ; Disable DMC IRQs
        ldx #$0A
        stx $2001 ; Disable rendering

        bit $2002 ; Clear vblank flag by reading ppu status
VBLANKWAIT1:
        bit $2002
        bpl VBLANKWAIT1
VBLANKWAIT2:
        bit $2002
        bpl VBLANKWAIT2
        lda #$1
        sta CUR
        lda #$0
        sta POS
        lda #$80
        sta PPUCTRL
loop:
        jmp loop

nmi:
        lda #$20
        sta PPUADDR ; MSB
        lda POS
        adc #$1
        sta POS
        sta PPUADDR
        bne update
        clc
        lda CUR
        adc #$1
        sta CUR
        cmp #$7
        bne update
        lda #$0
        sta CUR
update:
        lda CUR
        sta PPUDATA

        lda #$0
        sta PPUSCROLL
        sta PPUSCROLL

        lda #$80
        sta PPUCTRL
        
        rti

irq:
        stp
