
.include "testing.s"
; patterns_bin "pat.bin"
.macro chr b,s
.repeat s
.byte b
.endrepeat
.endmacro
.pushseg
.segment "CHARS"
T_EMPTY = 0
.repeat 2 ; Empty 0
.byte %00000000
.byte %00000000
.byte %00000000
.byte %00000000
.byte %00000000
.byte %00000000
.byte %00000000
.byte %00000000
.endrepeat

T_TOP_LEFT = 1
.repeat 2 ; Top Left 1
.byte %11111111
.byte %11111111
.byte %11000000
.byte %11000000
.byte %11000000
.byte %11000000
.byte %11000000
.byte %11000000
.endrepeat
T_TOP_RIGHT = 2
.repeat 2 ; Top Right 2
.byte %11111111
.byte %11111111
.byte %00000011
.byte %00000011
.byte %00000011
.byte %00000011
.byte %00000011
.byte %00000011
.endrepeat
T_BOTTOM_RIGHT = 3
.repeat 2 ; Bottom Right 3
.byte %00000011
.byte %00000011
.byte %00000011
.byte %00000011
.byte %00000011
.byte %00000011
.byte %11111111
.byte %11111111
.endrepeat
T_BOTTOM_LEFT = 4
.repeat 2 ; Bottom Left 4
.byte %11000000
.byte %11000000
.byte %11000000
.byte %11000000
.byte %11000000
.byte %11000000
.byte %11111111
.byte %11111111
.endrepeat
T_FORWARD = 5
.repeat 2 ; FW 5
.byte %00000011
.byte %00000111
.byte %00001110
.byte %00011100
.byte %00111000
.byte %01110000
.byte %11100000
.byte %11000000
.endrepeat
T_BACKWARD = 6
.repeat 2 ; BW 6
.byte %11000000
.byte %11100000
.byte %01110000
.byte %00111000
.byte %00011100
.byte %00001110
.byte %00000111
.byte %00000011
.endrepeat

T_PILL_L = 7
.repeat 2 ; Pill L 7
.byte %00111111
.byte %01111111
.byte %11100000
.byte %11000000
.byte %11000000
.byte %11100000
.byte %01111111
.byte %00111111
.endrepeat
T_PILL_R = 8
.repeat 2 ; Pill R 8
.byte %11111100
.byte %11111110
.byte %00000111
.byte %00000011
.byte %00000011
.byte %00000111
.byte %11111110
.byte %11111100
.endrepeat

T_LINE = 9
.repeat 2 ; Line
.byte %00000000
.byte %11111111
.byte %11111111
.byte %00000000
.byte %00000000
.byte %00000000
.byte %00000000
.byte %00000000
.endrepeat

.popseg

zp_res Y_SCROLL
zp_res X_SCROLL
zp_res CTRL_BYTE

.macro load_ppu_addr addr
	lda #.hibyte(addr)
	sta PPUADDR
	lda #.lobyte(addr)
	sta PPUADDR ; Load $2000
.endmacro

reset:
	sei
	cld
	ldx #$FF
	txs

	; Init PPU
	bit PPUSTATUS
vwait1:
	bit PPUSTATUS
	bpl vwait1
vwait2:
	bit PPUSTATUS
	bpl vwait2

	lda #$00
	sta PPUCTRL ; NMI off, PPU slave, Small sprites, 0 addresses

	; Fill NT0
	load_ppu_addr $2000
	ldx #$00
	ldy #$04
	lda #$00
fill_loop:
	sta PPUDATA
	dex
	bne fill_loop
	dey
	bne fill_loop
	; Set specific tiles
	load_ppu_addr $2184
	lda #T_TOP_LEFT
	sta PPUDATA
	lda #T_TOP_RIGHT
	sta PPUDATA
	load_ppu_addr $21A4
	lda #T_BACKWARD
	sta PPUDATA
	lda #T_FORWARD
	sta PPUDATA

	load_ppu_addr $21C2
	lda #T_TOP_LEFT
	sta PPUDATA
	lda #T_BACKWARD
	sta PPUDATA
	load_ppu_addr $21E2
	lda #T_BOTTOM_LEFT
	sta PPUDATA
	lda #T_FORWARD
	sta PPUDATA

	load_ppu_addr $21C6
	lda #T_FORWARD
	sta PPUDATA
	lda #T_TOP_RIGHT
	sta PPUDATA
	load_ppu_addr $21E6
	lda #T_BACKWARD
	sta PPUDATA
	lda #T_BOTTOM_RIGHT
	sta PPUDATA

	load_ppu_addr $2204
	lda #T_FORWARD
	sta PPUDATA
	lda #T_BACKWARD
	sta PPUDATA
	load_ppu_addr $2224
	lda #T_BOTTOM_LEFT
	sta PPUDATA
	lda #T_BOTTOM_RIGHT
	sta PPUDATA

	load_ppu_addr $21EA
	lda #T_PILL_L
	sta PPUDATA
	lda #T_PILL_R
	sta PPUDATA
	lda #$00
	sta PPUDATA
	sta PPUDATA

	lda #T_PILL_L
	sta PPUDATA
	lda #T_PILL_R
	sta PPUDATA

	load_ppu_addr $21D2
	lda #T_FORWARD
	sta PPUDATA
	lda #T_BACKWARD
	sta PPUDATA
	load_ppu_addr $21F2
	lda #T_TOP_LEFT
	sta PPUDATA
	lda #T_TOP_RIGHT
	sta PPUDATA

	load_ppu_addr $21D6
	lda #T_TOP_LEFT
	sta PPUDATA
	lda #T_PILL_R
	sta PPUDATA
	load_ppu_addr $21F6
	lda #T_TOP_LEFT
	sta PPUDATA
	lda #T_PILL_R
	sta PPUDATA

	load_ppu_addr $3F00
	lda #$0f
	sta PPUDATA
	lda #$20
	sta PPUDATA
	sta PPUDATA
	sta PPUDATA
	lda #$0f
	sta PPUDATA
	lda #$09
	sta PPUDATA
	sta PPUDATA
	sta PPUDATA
	lda #$0f
	sta PPUDATA
	lda #$1A
	sta PPUDATA
	sta PPUDATA
	sta PPUDATA
	lda #$0f
	sta PPUDATA
	lda #$21
	sta PPUDATA
	sta PPUDATA
	sta PPUDATA

	load_ppu_addr $2000
	lda #T_TOP_LEFT
	ldx #$1F
line_loop:
	sta PPUDATA
	dex
	bne line_loop

	load_ppu_addr $23C0
	ldx #$7
line_color_loop:
	lda #%00000000
	sta PPUDATA
	lda #%00001111
	sta PPUDATA
	dex
	bne line_color_loop


	lda #$00
	sta PPUSCROLL
	sta PPUSCROLL
	lda #%00001110
	sta PPUMASK 
	lda #$80
	sta PPUCTRL ; NMI on, PPU slave, Small sprites, 0 addresses
	sta CTRL_BYTE
loop:
	jmp loop

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

	lda #$01
	adc X_SCROLL
	sta X_SCROLL
	sta PPUSCROLL
	bcc y_scroll
	lda #$01
	eor CTRL_BYTE
	sta CTRL_BYTE
	sta PPUCTRL
y_scroll:
	clc
	lda #$00
	adc Y_SCROLL
	sta Y_SCROLL
	sta PPUSCROLL

	lda CTRL_BYTE
	sta PPUCTRL
	lda #%00001110
	sta PPUMASK ; Enable rendering
	rti
exit:
	stp

irq:
	stp
