.macro color
        lda v2001
        and #%11111110
        sta v2001
        sta $2001
.endmacro

.macro mono
        lda v2001
        ora #%00000001
        sta v2001
        sta $2001
.endmacro

;---------------------------------------------------------
;---------------------------------------------------------
;       Load 16-bit pointer (for indirect addressing)
;       (macro: pointer target,zeropage)
;---------------------------------------------------------
.macro  pointer   addr,addr2
                lda #<addr
                sta addr2
                lda #>addr
                sta addr2+1
.endmacro
;---------------------------------------------------------
;---------------------------------------------------------
;       16-bit compare
;       (macro: cmp16 addr1,addr2,branch_label (if not equal)
;---------------------------------------------------------
.macro  cmp16 addr,addr2,branch
                lda addr
                cmp addr2
                bne branch
                lda addr+1
                cmp addr2+1
                bne branch
.endmacro
;---------------------------------------------------------
;---------------------------------------------------------
;       16-bit increment (macro: inc16 address)
;---------------------------------------------------------
.macro  inc16   addr
.local label
		inc     addr
		bne     label
		inc     addr+1
	label:
.endmacro
;---------------------------------------------------------
;---------------------------------------------------------
;       16-bit decrement (macro: dec16 address)
;       (this is slower than an increment)
;---------------------------------------------------------
.macro  dec16   addr
                lda addr
                bne :+
                dec addr+1
        :       dec addr
.endmacro
;---------------------------------------------------------
;---------------------------------------------------------
;       Enable BG and OBJ (macro: screen_on)
;---------------------------------------------------------
.macro  screen_on
                lda v2001
                ora #%00011000
                sta v2001
                sta $2001
.endmacro
;---------------------------------------------------------
;---------------------------------------------------------
;       Disable BG and OBJ (macro: screen_off)
;---------------------------------------------------------
.macro  screen_off
                lda v2001
                and #%11100111
                sta v2001
                sta $2001
.endmacro
;---------------------------------------------------------
;---------------------------------------------------------
;       Wait until vblank NMI has finished (macro: vblank)
;       NMIs must be enabled, or this will lock up
;---------------------------------------------------------
.macro  vblank
        :
                lda vb_hit
                beq :-
                lda #0
                sta vb_hit
.endmacro
;---------------------------------------------------------
;---------------------------------------------------------
;       Enable NMI
;---------------------------------------------------------
.macro  nmi_enable
                lda v2000
                ora #$80
                sta v2000
                sta $2000
.endmacro
;---------------------------------------------------------
;---------------------------------------------------------
;       Disable NMI
;---------------------------------------------------------
.macro  nmi_disable
                lda v2000
                and #$7F
                sta v2000
                sta $2000
.endmacro
;---------------------------------------------------------
;---------------------------------------------------------
;       Enable Clipping for BG and OBJs (8 pixels of the left border)
;---------------------------------------------------------
.macro  clipping_enable
                lda v2001
                and #%11111001
                sta v2001
                sta $2001
.endmacro
;---------------------------------------------------------
;---------------------------------------------------------
;       Disable Clipping for BG and OBJs (8 pixels of the left border)
;---------------------------------------------------------
.macro  clipping_disable
                lda v2001
                ora #%00000110
                sta v2001
                sta $2001
.endmacro
;---------------------------------------------------------
;---------------------------------------------------------
;       Set Horizontal VRAM writing (auto-inc by 1)
;---------------------------------------------------------
.macro  vram_h
                lda v2000
                and #%11111011
                sta v2000
                sta $2000
.endmacro
;---------------------------------------------------------
;---------------------------------------------------------
;       Set Vertical VRAM writing (auto-inc by 32)
;---------------------------------------------------------
.macro  vram_v
                lda v2000
                ora #%00000100
                sta v2000
                sta $2000
.endmacro
;---------------------------------------------------------
;---------------------------------------------------------
;       Check Controller
;       (macro: controller button,branch,(optional)branch
;---------------------------------------------------------
.macro  controller button,not_pressed,button_held
        .ifblank button_held
                lda joy1
                and #button
                beq not_pressed
        .endif
        .ifnblank button_held
                lda joy1
                and #button
                beq not_pressed
                and joy1old
                bne button_held
        .endif
.endmacro
;---------------------------------------------------------
;---------------------------------------------------------
;       Initialize NES (and clear memory)
;---------------------------------------------------------
.macro  nes_init
                sei
                cld
                ldx #0
                stx $2000
                stx $2001
                stx $4015
         
        @wait:
                lda $2002
                bpl @wait
        @wait2:
                lda $2002
                bpl @wait2
        
                dex
                txs
                 
                inx
                txa
        
        clear_ram:
                sta $00, x
                sta $200, x
                sta $300, x
                sta $400, x
                sta $500, x
                sta $600, x
                sta $700, x
                inx
                bne clear_ram
.endmacro
;---------------------------------------------------------
;---------------------------------------------------------
;       Setup a sprite (object)
;---------------------------------------------------------
.macro  sprite number,tile,Ycoord,Xcoord,attribute

                lda #Ycoord
                sta sprites+number*4
                lda #tile
                sta sprites+1+number*4
        .ifnblank attribute
                lda #attribute
                sta sprites+2+number*4
        .endif
                lda #Xcoord
                sta sprites+3+number*4
.endmacro
;---------------------------------------------------------
