.smc
.list
.dasm
.detect on
.echo
.org $08000
.asctable
" ".."~" = 64
.end

start:	clc		; set all cpu registers to zero
	xce
	rep #$ff
	lda #$1fff
	tcs
	lda #$000
	ldx #$000
	ldy #$000
	tcd
	pha
	plb
	plp
-	sta $000,x	; clear first 8k of mem
	inx
	inx
	cpx #$2000
	bne -
	jsr init	;setup regs
	jsr initmem	;clear memory
	jsr initcol	;clear colors
	jsr initvid	;clear vram
	jsr initoam	;clear oam

;evrything should be cleared now (i'm not bothering with the sound!)
	sep #$30	;setup colors
	lda #$0
	ldy #$10
	sta $2121
--	ldx #$0
-	lda colors,x
	sta $2122
	inx
	cpx #$20
	bne -
	dey
	bne --	
	jsr col64	;do 64 cols

	lda #$7		;set to mode 7! =)
	sta $2105
	lda #$c0
	sta $211a

;setup vram
	lda #$0		;setup name table
	sta $2116
	sta $2117
	rep #$10
	ldx #$000	;do mode7 graphics
-	lda mode7,x
	sta $2119
	inx
	cpx #$440
	bne -

	lda #$0		;do mode7 name table	
	sta $2115
	ldx #$000	;clear to $10
	stx $2116
	lda #$10
-	sta $2118
	inx
	cpx #$4000
	bne -
	ldx #$000	;do \
-	stx $2116
	lda #$8
	sta $2118
	rep #$20
	txa
	clc
	adc #$081
	tax
	sep #$20
	cpx #$4000
	bmi -
	ldx #$07f	;do /
-	stx $2116
	lda #$9
	sta $2118
	rep #$20
	txa
	clc
	adc #$07f
	tax
	sep #$20
	cpx #$4000
	bmi -
	ldx #$000	;do top line
	stx $2116
	lda #$4
-	sta $2118
	inx
	cpx #$080
	bne -
	ldx #$3f80	;do bottom line
	stx $2116
	ldx #$000
	lda #$5
-	sta $2118
	inx
	cpx #$80
	bne -
	ldx #$1ebf	;do arrow
	stx $2116
	lda #$a
	sta $2118
	inc
	sta $2118
	lda #$3		;do left border + top bot
	sta $2115
	ldx #$000
	stx $2116
	lda #$0
	sta $2118
	lda #$6
-	sta $2118
	inx
	cpx #$7e
	bne -
	lda #$2
	sta $2118
	ldx #$07f	;do right border + top bot
	stx $2116
	ldx #$000
	lda #$1
	sta $2118
	lda #$7
-	sta $2118
	inx
	cpx #$7e
	bne -
	lda #$3
	sta $2118

	lda #$80
	sta $2115
	rep #$10	;do ascii in ram
	ldx #$000
	ldy #$000
-	lda ascii+7,y
	sta $7f0000,x
	sta $7f0001,x
	sta $7f0010,x
	sta $7f0011,x
	inx
	inx
	iny
	tya
	and #$7
	bne -
	rep #$20
	txa
	clc
	adc #$10
	tax
	sep #$20
	cpy #$300
	bne -
	lda #$0		;move ascii to vram
	sta $2116
	lda #$42
	sta $2117
	rep #$30
	ldx #$000
-	lda $7f0000,x
	sta $2118
	inx
	inx
	cpx #$C00
	bne -

	rep #$30	;move cursor to vram
	ldx #$4020
	stx $2116
	ldx #$000
-	lda cusrt,x
	sta $2118
	inx
	inx
	cpx #$40
	bne -
	ldx #$4120
	stx $2116
	ldx #$000
-	lda cusrb,x
	sta $2118
	inx
	inx
	cpx #$40
	bne -

	sep #$30

	lda #$2		;set sprite address and size
	sta $2101

	rep #$10	;set static sprites
	ldx #$000
	ldy #$010	;y points to buffer
back	lda stat,x
	beq eot
	sta $0
	inx
	lda stat,x
	sta $1
	inx
-	lda stat,x
	php
	inx
	plp
	bmi back
	sta $202,y
	lda $0
	sta $200,y
	lda $1
	sta $201,y
	lda #$30
	sta $203,y
	iny
	iny
	iny
	iny
	lda $0
	clc
	adc #$8
	sta $0
	bra -	
eot:
	ldx #$000	;do dyn16
	ldy #$000
-	lda dyn16+1,x
	sta $301,y
	sta $305,y
	sta $309,y
	sta $30d,y
	lda #$30
	sta $303,y
	sta $307,y
	sta $30b,y
	sta $30f,y
	lda dyn16,x
	sta $30c,y
	clc
	adc #$8
	sta $308,y
	adc #$8
	sta $304,y
	adc #$8
	sta $300,y
	rep #$20
	tya
	clc
	adc #$010
	tay
	sep #$20
	inx
	inx
	cpy #$80
	bne -

	ldx #$000	;do dyn8
	ldy #$000
-	lda dyn8,x
	sta $380,y
	lda dyn8+1,x
	sta $381,y
	lda #$30
	sta $383,y
	inx
	inx
	iny
	iny
	iny
	iny
	cpx #$8
	bne -

	lda #$2		;do cursor in obj buffer
	sta $392
	lda #$30
	sta $393
	lda #$2
	sta $419

	lda #$0		;init cursor
	sta $30
	sta $31

	ldx #$000	; init vars
-	lda initv,x
	sta $0,x
	inx
	cpx #$014
	bne -

	ldx #$000	;init chars
	ldy #$000
-	tya
	sta $280,x
	clc
	adc #$8
	tay
	lda #$98
	sta $281,x
	lda #$30
	sta $283,x
	inx
	inx
	inx
	inx
	cpx #$80
	bne -

	ldx #$000	;init scroll vals
	stx $80
	lda #$0
	sta $82

	lda #$1f	;do last video stuff
	sta $212c
	lda #$f
	sta $2100
	lda #$81
	sta $4200
;kernal

kernal:	lda $24		;see if interupt has occoured yet
	beq kernal
	stz $24

	jsr input
	jsr scroll

	ldx #$000	;make 10-13 valid
-	lda $10,x
	and #$3
	sta $10,x
	inx
	cpx #$004
	bne -

	lda $30		;made x and y valid
	and #$7
	sta $30
	lda $31
	and #$7
	sta $31
	ldx #$000	;move 0-1f to buffer
	ldy #$000
-	lda $0,x
	pha
	and #$f
	cmp #$a
	bcc aa
	clc
	adc #$7
aa:	clc
	adc #$30
	sta $302,y
	pla
	lsr
	lsr
	lsr
	lsr
	cmp #$a
	bcc bb
	clc
	adc #$7
bb:	clc
	adc #$30
	sta $306,y
	iny
	iny
	iny
	iny
	iny
	iny
	iny
	iny
	inx
	cpx #$010
	bne -
	ldx #$000	;move 10-13 to buffer
	ldy #$000
-	lda $10,x
	clc
	adc #$30
	sta $382,y
	inx
	iny
	iny
	iny
	iny
	cpx #$4
	bne -

	lda #$0
	xba
	lda $31		;do cursor to buffer
	tax
	and #$4
	bne var4
	lda cursy,x
	sta $391
	lda $30
	tax
	lda cursx,x
	sta $390
	bra hhh
var4:	lda #$76
	sta $390
	lda $31
	and #$3
	asl
	asl
	asl
	adc #$66
	sta $391
hhh:

brl kernal

scrollt: .db "Hi there! Welcome to my first snes demo! =) Controls: up, right, left, and down move the cursor. "
	.db "Y and B decrease and increase the number in the cursor, respectively. "
	.db "X and A decrease and increase continuously. "
	.db "Select selects whether to directly use the Scroll registers (SX) or indirectly (CX). "
	.db "This is an interface to "
	.db "experiment with the mode 7 registers.  I made this demo so I could learn about "
	.db "mode 7 since I haven't found any docs about how it works. :P~~~ "
	.db "Let me know what you think about this demo!  Send comments and suggestions to cainjs@iac.net.... "
	.db "My greets: Hello to my mom, my sister, my freind "
	.db "Joel, and Eric the 'raccoon' _^..^_.....     Thanks to Yoshi, Qwertie, the "
	.db "zSnes team, and the author of the x816 assembler. This could not have been done "
	.db "without you! Also thanks to [trac] from #snesdev for clarifying some interrupt stuff.   "
	.db "This font was ripped from the rom of the PC itself. "
	.db "All other graphics were typed in byte by byte since I can't find any good graphics "
	.db "development tools for the SNES! :P   This is a sucky demo "
	.db "compared to some; It has no sound or music!  (I know I'm pointing out the obvious =) ).   "
	.db "Stuff about me..... My name is Josh and I'm from Southwest Ohio. I am 23 years old. I like opera, "
	.db "math, programing, computers, and science.  "
	.db "I've been programing since I was 10 years old. "
	.db "I got an Apple IIgs when I was 12.  I got into ml programing when I was 16. "
	.db "For a long time I programed in hex codes! I didn't have an assembler. :( A friend "
	.db "later told me how to access a mini-assembler on the Apple rom.. :)     "  
	.db "This demo was finished 12- 1-1999 and updated some 6-10-2000..... Let me know what you think! "
	.db "Bye bye! --text restarts--     ",$0

table:	.dw $0001,$0010,$0100,$1000

cursx:	.db $46,$4e,$56,$5e,$be,$c6,$ce,$d6

cursy:	.db $3e,$46,$4e,$56

dyn16:	.db $48,$40,$48,$48,$48,$50,$48,$58
	.db $c0,$40,$c0,$48,$c0,$50,$c0,$58

dyn8:	.db $78,$68,$78,$70,$78,$78,$78,$80

initv:	.dw $0000,$0000,$0000,$0000
	.dw $0100,$0000,$0000,$0100
	.db $0,$1,$2,$3

stat:	.db $28,$40,"SX:",$ff
	.db $28,$48,"SY:",$ff
	.db $28,$50,"OX:",$ff
	.db $28,$58,"OY:",$ff
	.db $a8,$40,"0:",$ff
	.db $a8,$48,"1:",$ff
	.db $a8,$50,"2:",$ff
	.db $a8,$58,"3:",$ff
	.db $60,$68,"A:",$ff
	.db $60,$70,"B:",$ff
	.db $60,$78,"C:",$ff
	.db $60,$80,"D:",$ff
	.db $00

colors:	.dw $0000,$5400,$04a0,$56a0
	.dw $0015,$5415,$0155,$56b5
	.dw $294a,$7d4a,$2bea,$7fea
	.dw $295f,$7d5f,$2bff,$7fff

cusrt:	.db $f0,$f0,$80,$80,$80,$80,$80,$80,$00,$00,$00,$00,$00,$00,$00,$00
	.db $00,$f0,$00,$80,$00,$80,$00,$80,$00,$00,$00,$00,$00,$00,$00,$00
	.db $f0,$f0,$10,$10,$10,$10,$10,$10,$00,$00,$00,$00,$00,$00,$00,$00
	.db $00,$f0,$00,$10,$00,$10,$00,$10,$00,$00,$00,$00,$00,$00,$00,$00
cusrb:	.db $80,$80,$80,$80,$80,$80,$f0,$f0,$00,$00,$00,$00,$00,$00,$00,$00
	.db $00,$80,$00,$80,$00,$80,$00,$f0,$00,$00,$00,$00,$00,$00,$00,$00
	.db $10,$10,$10,$10,$10,$10,$f0,$f0,$00,$00,$00,$00,$00,$00,$00,$00
	.db $00,$10,$00,$10,$00,$10,$00,$f0,$00,$00,$00,$00,$00,$00,$00,$00

mode7:	.db $28,$28,$28,$28,$28,$28,$28,$28	;lu corner
	.db $28,$28,$00,$00,$00,$00,$00,$00
	.db $28,$00,$28,$00,$00,$00,$00,$00
	.db $28,$00,$00,$28,$00,$00,$00,$00
	.db $28,$00,$00,$00,$28,$00,$00,$00
	.db $28,$00,$00,$00,$00,$28,$00,$00
	.db $28,$00,$00,$00,$00,$00,$28,$00
	.db $28,$00,$00,$00,$00,$00,$00,$28

	.db $3f,$3f,$3f,$3f,$3f,$3f,$3f,$3f	;ru corner
	.db $00,$00,$00,$00,$00,$00,$3f,$3f
	.db $00,$00,$00,$00,$00,$3f,$00,$3f
	.db $00,$00,$00,$00,$3f,$00,$00,$3f
	.db $00,$00,$00,$3f,$00,$00,$00,$3f
	.db $00,$00,$3f,$00,$00,$00,$00,$3f
	.db $00,$3f,$00,$00,$00,$00,$00,$3f
	.db $3f,$00,$00,$00,$00,$00,$00,$3f

	.db $3f,$00,$00,$00,$00,$00,$00,$3f	;ld corner
	.db $3f,$00,$00,$00,$00,$00,$3f,$00
	.db $3f,$00,$00,$00,$00,$3f,$00,$00
	.db $3f,$00,$00,$00,$3f,$00,$00,$00
	.db $3f,$00,$00,$3f,$00,$00,$00,$00
	.db $3f,$00,$3f,$00,$00,$00,$00,$00
	.db $3f,$3f,$00,$00,$00,$00,$00,$00
	.db $3f,$3f,$3f,$3f,$3f,$3f,$3f,$3f

	.db $3f,$00,$00,$00,$00,$00,$00,$3f	;rd corner
	.db $00,$3f,$00,$00,$00,$00,$00,$3f
	.db $00,$00,$3f,$00,$00,$00,$00,$3f
	.db $00,$00,$00,$3f,$00,$00,$00,$3f
	.db $00,$00,$00,$00,$3f,$00,$00,$3f
	.db $00,$00,$00,$00,$00,$3f,$00,$3f
	.db $00,$00,$00,$00,$00,$00,$3f,$3f
	.db $3f,$3f,$3f,$3f,$3f,$3f,$3f,$3f

	.db $3f,$3f,$3f,$3f,$3f,$3f,$3f,$3f	;u line
	.db $00,$00,$00,$00,$00,$00,$00,$00
	.db $00,$00,$00,$00,$00,$00,$00,$00
	.db $00,$00,$00,$00,$00,$00,$00,$00
	.db $00,$00,$00,$00,$00,$00,$00,$00
	.db $00,$00,$00,$00,$00,$00,$00,$00
	.db $00,$00,$00,$00,$00,$00,$00,$00
	.db $00,$00,$00,$00,$00,$00,$00,$00

	.db $00,$00,$00,$00,$00,$00,$00,$00	;d line
	.db $00,$00,$00,$00,$00,$00,$00,$00
	.db $00,$00,$00,$00,$00,$00,$00,$00
	.db $00,$00,$00,$00,$00,$00,$00,$00
	.db $00,$00,$00,$00,$00,$00,$00,$00
	.db $00,$00,$00,$00,$00,$00,$00,$00
	.db $00,$00,$00,$00,$00,$00,$00,$00
	.db $3f,$3f,$3f,$3f,$3f,$3f,$3f,$3f

	.db $3f,$00,$00,$00,$00,$00,$00,$00	;l line
	.db $3f,$00,$00,$00,$00,$00,$00,$00
	.db $3f,$00,$00,$00,$00,$00,$00,$00
	.db $3f,$00,$00,$00,$00,$00,$00,$00
	.db $3f,$00,$00,$00,$00,$00,$00,$00
	.db $3f,$00,$00,$00,$00,$00,$00,$00
	.db $3f,$00,$00,$00,$00,$00,$00,$00
	.db $3f,$00,$00,$00,$00,$00,$00,$00

	.db $00,$00,$00,$00,$00,$00,$00,$3f	;r line
	.db $00,$00,$00,$00,$00,$00,$00,$3f
	.db $00,$00,$00,$00,$00,$00,$00,$3f
	.db $00,$00,$00,$00,$00,$00,$00,$3f
	.db $00,$00,$00,$00,$00,$00,$00,$3f
	.db $00,$00,$00,$00,$00,$00,$00,$3f
	.db $00,$00,$00,$00,$00,$00,$00,$3f
	.db $00,$00,$00,$00,$00,$00,$00,$3f

	.db $3f,$00,$00,$00,$00,$00,$00,$00	;\
	.db $00,$3f,$00,$00,$00,$00,$00,$00
	.db $00,$00,$3f,$30,$30,$00,$00,$00
	.db $00,$00,$30,$30,$30,$30,$00,$00
	.db $00,$00,$30,$30,$30,$30,$00,$00
	.db $00,$00,$00,$30,$30,$3f,$00,$00
	.db $00,$00,$00,$00,$00,$00,$3f,$00
	.db $00,$00,$00,$00,$00,$00,$00,$3f

	.db $00,$00,$00,$00,$00,$00,$00,$ff	;/
	.db $00,$00,$00,$00,$00,$00,$ff,$00
	.db $00,$00,$00,$0c,$0c,$ff,$00,$00
	.db $00,$00,$0c,$0c,$0c,$0c,$00,$00
	.db $00,$00,$0c,$0c,$0c,$0c,$00,$00
	.db $00,$00,$ff,$0c,$0c,$00,$00,$00
	.db $00,$ff,$00,$00,$00,$00,$00,$00
	.db $ff,$00,$00,$00,$00,$00,$00,$00

	.db $00,$00,$00,$00,$00,$00,$00,$3f	;arrow left
	.db $00,$00,$00,$00,$00,$00,$3f,$3f
	.db $00,$00,$00,$00,$00,$3f,$00,$3f
	.db $00,$00,$00,$00,$00,$00,$00,$3f
	.db $00,$00,$00,$00,$00,$00,$00,$3f
	.db $00,$00,$00,$00,$00,$00,$00,$3f
	.db $00,$00,$00,$00,$00,$00,$00,$3f
	.db $00,$00,$00,$00,$00,$00,$00,$3f

	.db $3f,$00,$00,$00,$00,$00,$00,$00	;arrow right
	.db $3f,$3f,$00,$00,$00,$00,$00,$00
	.db $3f,$00,$3f,$00,$00,$00,$00,$00
	.db $3f,$00,$00,$00,$00,$00,$00,$00
	.db $3f,$00,$00,$00,$00,$00,$00,$00
	.db $3f,$00,$00,$00,$00,$00,$00,$00
	.db $3f,$00,$00,$00,$00,$00,$00,$00
	.db $3f,$00,$00,$00,$00,$00,$00,$00

	.db $00,$00,$01,$01,$02,$02,$03,$03	;00ggbb
	.db $00,$00,$01,$01,$02,$02,$03,$03
	.db $04,$04,$05,$05,$06,$06,$07,$07
	.db $04,$04,$05,$05,$06,$06,$07,$07
	.db $08,$08,$09,$09,$0a,$0a,$0b,$0b
	.db $08,$08,$09,$09,$0a,$0a,$0b,$0b
	.db $0c,$0c,$0d,$0d,$0e,$0e,$0f,$0f
	.db $0c,$0c,$0d,$0d,$0e,$0e,$0f,$0f

	.db $10,$10,$11,$11,$12,$12,$13,$13	;01ggbb
	.db $10,$10,$11,$11,$12,$12,$13,$13
	.db $14,$14,$15,$15,$16,$16,$17,$17
	.db $14,$14,$15,$15,$16,$16,$17,$17
	.db $18,$18,$19,$19,$1a,$1a,$1b,$1b
	.db $18,$18,$19,$19,$1a,$1a,$1b,$1b
	.db $1c,$1c,$1d,$1d,$1e,$1e,$1f,$1f
	.db $1c,$1c,$1d,$1d,$1e,$1e,$1f,$1f

	.db $20,$20,$21,$21,$22,$22,$23,$23	;10ggbb
	.db $20,$20,$21,$21,$22,$22,$23,$23
	.db $24,$24,$25,$25,$26,$26,$27,$27
	.db $24,$24,$25,$25,$26,$26,$27,$27
	.db $28,$28,$29,$29,$2a,$2a,$2b,$2b
	.db $28,$28,$29,$29,$2a,$2a,$2b,$2b
	.db $2c,$2c,$2d,$2d,$2e,$2e,$2f,$2f
	.db $2c,$2c,$2d,$2d,$2e,$2e,$2f,$2f

	.db $30,$30,$31,$31,$32,$32,$33,$33	;11ggbb
	.db $30,$30,$31,$31,$32,$32,$33,$33
	.db $34,$34,$35,$35,$36,$36,$37,$37
	.db $34,$34,$35,$35,$36,$36,$37,$37
	.db $38,$38,$39,$39,$3a,$3a,$3b,$3b
	.db $38,$38,$39,$39,$3a,$3a,$3b,$3b
	.db $3c,$3c,$3d,$3d,$3e,$3e,$3f,$3f
	.db $3c,$3c,$3d,$3d,$3e,$3e,$3f,$3f

	.db $00,$00,$00,$06,$06,$00,$00,$00	;arrow
	.db $00,$00,$06,$06,$06,$06,$00,$00
	.db $00,$06,$00,$06,$06,$00,$06,$00
	.db $00,$00,$00,$06,$06,$00,$00,$00
	.db $00,$00,$00,$06,$06,$00,$00,$00
	.db $00,$00,$00,$06,$06,$00,$00,$00
	.db $00,$00,$00,$06,$06,$00,$00,$00
	.db $00,$00,$00,$00,$00,$00,$00,$00

ascii:	.incbin text.bin

do40:	php
	rep #$20	;make $40/42 valid
	pha
	lda $4
	sec
	sbc $0
	sta $40
	lda $6
	sec
	sbc $2
	sta $42
	pla
	sep #$20
	plp
	rts

scroll:	ldx #$000
	lda #$0
	xba
-	lda $280,x
	sec
	sbc #$2
	sta $280,x
	inx
	inx
	inx
	inx
	cpx #$80
	bne -
	and #$7
	bne skip
	ldx $80
	lda $82
	asl
	asl
	tay
	lda scrollt,x
	beq rest
	inx
	stx $80
	sta $282,y
	lda $82
	inc
	and #$1f
	sta $82
skip:	rts

rest:	ldx #$000
	stx $80
	lda $82
	inc
	and #$1f
	sta $82
	rts

input:	lda $21		;test for right
	and #$1
	beq nor
	and $23
	bne nor
	inc $30
nor:	lda $21		;test for left
	and #$2
	beq nol
	and $23
	bne nol
	dec $30
nol:	lda $21		;test for down
	and #$4
	beq nod
	and $23
	bne nod
	inc $31
nod:	lda $21		;test for up
	and #$8
	beq nou
	and $23
	bne nou
	dec $31
nou:	lda $21		;test for y (-)
	and #$40
	beq noy
	and $23
	bne noy
	lda $31		;test for 0 or 4
	and #$4
	beq do16a
	lda $31
	and #$3
	tax
	dec $10,x
	bra noy
do16a:	lda $30
	and #$4
	sta $ff
	lda $31
	and #$3
	ora $ff
	asl
	tax
	lda $30
	and #$3
	eor #$3
	asl
	tay
	rep #$20
	lda $0,x
	sec
	sbc table,y
	sta $0,x
	sep #$20
noy:	lda $21		;test for b (+)
	and #$80
	beq nob
	and $23
	bne nob
	lda $31		;test for 0 or 4
	and #$4
	beq do16b
	lda $31
	and #$3
	tax
	inc $10,x
	bra nob
do16b:	lda $30
	and #$4
	sta $ff
	lda $31
	and #$3
	ora $ff
	asl
	tax
	lda $30
	and #$3
	eor #$3
	asl
	tay
	rep #$20
	lda $0,x
	clc
	adc table,y
	sta $0,x
	sep #$20
nob:	lda $20		;test for x (-cont)
	and #$40
	beq nox
	lda $31		;test for 0 or 4
	and #$4
	beq do16c
	lda $31
	and #$3
	tax
	dec $10,x
	bra nox
do16c:	lda $30
	and #$4
	sta $ff
	lda $31
	and #$3
	ora $ff
	asl
	tax
	lda $30
	and #$3
	eor #$3
	asl
	tay
	rep #$20
	lda $0,x
	sec
	sbc table,y
	sta $0,x
	sep #$20
nox:	lda $20		;test for a (+cont)
	and #$80
	beq noa
	lda $31		;test for 0 or 4
	and #$4
	beq do16d
	lda $31
	and #$3
	tax
	inc $10,x
	bra noa
do16d:	lda $30
	and #$4
	sta $ff
	lda $31
	and #$3
	ora $ff
	asl
	tax
	lda $30
	and #$3
	eor #$3
	asl
	tay
	rep #$20
	lda $0,x
	clc
	adc table,y
	sta $0,x
	sep #$20
noa:	jsr do40	;make $0 and $40 valid b4 select
	lda $21		;test for select
	and #$20
	beq nos
	and $23
	bne nos
	lda $44
	eor #$40
	sta $44
	lda $212
	eor #$10
	sta $212
	sta $21e
	rep #$20
	lda $0
	pha
	lda $2
	pha
	lda $40
	sta $0
	lda $42
	sta $2
	pla
	sta $42
	pla
	sta $40
	sep #$20	
nos:
;BYSTudlr
rts

col64:	php
	rep #$30
	pha
	phx
	phy
	sep #$20
	lda #$0		;this sets up the first 64 cols according to rrggbb
	sta $2121
	rep #$20
	ldy #$000
--	tya
	and #$003
	sta $0
	tya
	lsr
	lsr
	and #$003
	sta $2
	tya
	lsr
	lsr
	lsr
	lsr
	and #$003
	sta $4
	ldx #$004
-	lda 0,x
	asl
	asl
	ora 0,x
	asl
	asl
	ora 0,x
	lsr
	sta 0,x
	dex
	dex
	bpl -
	lda $0		;its 5 bits now- now do for 15 bits =)
	asl
	asl
	asl
	asl
	asl
	ora $2
	asl
	asl
	asl
	asl
	asl
	ora $4
	sep #$20
	sta $2122
	xba
	sta $2122
	rep #$20
	iny
	cpy #$040
	bne --
	ply
	plx
	pla
	plp
	rts

;the nmi is used mainly to update the video during the vblank
; all the rest is caculated after the interupt completes in the kernal
nmi:	rep #$30
	phy
	phx
	pha
	sep #$20

	ldx #$000	;Move buffer to oam
	stx $2102
	lda #$0		;DMA control
	sta $4300
	lda #$4		;DMA destination
	sta $4301
	ldx #$200	;DMA source
	stx $4302
	lda #$0
	sta $4304
	ldx #$220	;DMA count
	stx $4305
	lda #$0
	sta $4307
	lda #$1		;DMA start
	sta $420b	

	ldx $44		;do mode 0 or 40
	lda $0,x	;update 0-7 vars to display
	sta $210d
	lda $1,x
	sta $210d
	lda $2,x
	sta $210e
	lda $3,x
	sta $210e
	lda $4
	sta $211f
	lda $5
	sta $211f
	lda $6
	sta $2120
	lda $7
	sta $2120
	ldy #$000	;do a-d
-	lda $10,y
	asl
	tax
	lda $8,x
	sta $211b,y
	lda $9,x
	sta $211b,y
	iny
	cpy #$004
	bne -
	ldx $20		;get joystick
	stx $22
-	lda $4212
	and #$01
	bne -
	ldx $4218
	stx $20
	lda #$80	;send interupt code to kernal
	sta $24
	rep #$30
	pla
	plx
	ply	
	rti		

;------------------------------ init subroutines ---------------------------------

; most of the below init subroutine wan't my own work, :\ sorry to whoever's code it was that i ripped! :)
init:	php                             ; Save registers used.
	sep #$20
	pha                             ; Save processor status.
	lda     #$8f                    ; Screen off, full brightness.
	sta     $2100                   ; Store to screen register.
	stz     $2101                   ; sprite register (size + address in VRAM)
	stz     $2102                   ; sprite registers (address of sprite memory [OAM])
	stz     $2103                   ; "
	stz     $2105                   ; set graphics Mode 0
	stz     $2106                   ; no planes, no mosiac
	stz     $2107                   ; plane 0 map VRAM location ($0000 vram)
	stz     $2108                   ; plane 1 map VRAM location
	stz     $2109                   ; plane 2 "
	stz     $210a                   ; plane 3 "
	stz     $210b                   ; plane 0+1 tile data location
	stz     $210c                   ; plane 0+2 "
	stz     $210d                   ; plane 0 scroll x (first 8 bits)
	stz     $210d                   ; plane 0 scroll x (last 3 bits) write to reg twice
	stz     $210e                   ; plane 0 scroll y "
	stz     $210e                   ; plane 0 scroll y "
	stz     $210f                   ; plane 1 scroll x (first 8 bits)
	stz     $210f                   ; plane 1 scroll x (last 3 bits) write to reg twice
	stz     $2110                   ; plane 1 scroll y "
	stz     $2110                   ; plane 1 scroll y "
	stz     $2111                   ; plane 2 scroll x (first 8 bits)
	stz     $2111                   ; plane 2 scroll x (last 3 bits) write to reg twice
	stz     $2112                   ; plane 2 scroll y "
	stz     $2112                   ; plane 2 scroll y "
	stz     $2113                   ; plane 3 scroll x (first 8 bits)
	stz     $2113                   ; plane 3 scroll x (last 3 bits) write to reg twice
	stz     $2114                   ; plane 3 scroll y "
	stz     $2114                   ; plane 3 scroll y "
	lda     #$80                    ; increase VRAM after writes to $2118.19
	sta     $2115                   ; store to VRAM increment register
	stz     $2116                   ; VRAM address low
	stz     $2117                   ; VRAM address hi
	stz     $211a                   ; init mode 7 setting reg
	stz     $211b                   ; mode 7 matrix parameter A register (low)
	stz     $211b                   ; Mode 7 matrix parameter A register (low)
	lda     #$01
	sta     $211b                   ; Mode 7 matrix parameter A register (high)
	stz     $211c                   ; Mode 7 matrix parameter B register (low)
	stz     $211c                   ; Mode 7 matrix parameter B register (high)
	stz     $211d                   ; Mode 7 matrix parameter C register (low)
	stz     $211d                   ; Mode 7 matrix parameter C register (high)
	stz     $211e                   ; Mode 7 matrix parameter D register (low)
	lda     #$01
	sta     $211e                   ; Mode 7 matrix parameter D register (high)
	stz     $211f                   ; Mode 7 center position X register (low)
	stz     $211f                   ; Mode 7 center position X register (high)
	stz     $2120                   ; Mode 7 center position Y register (low)
	stz     $2120                   ; Mode 7 center position Y register (high)
	stz     $2121                   ; Color number register ($0-ff)
	stz     $2123                   ; BG1 & BG2 Window mask setting register
	stz     $2124                   ; BG3 & BG4 Window mask setting register
	stz     $2125                   ; OBJ & Color Window mask setting register
	stz     $2126                   ; Window 1 left position register
	stz     $2127                   ; Window 2 left position register
	stz     $2128                   ; Window 3 left position register
	stz     $2129                   ; Window 4 left position register
	stz     $212a                   ; BG1, BG2, BG3, BG4 Window Logic register
	stz     $212b                   ; OBJ, Color Window Logic Register (or,and,xor,xnor)
	stz     $212c                   ; Main Screen designation (planes, sprites enable)
	stz     $212d                   ; Sub Screen designation
	stz     $212e                   ; Window mask for Main Screen
	stz     $212f                   ; Window mask for Sub Screen
	lda     #$30
	sta     $2130                   ; Color addition & screen addition init setting
	stz     $2131                   ; Add/Sub sub designation for screen, sprite, color
	lda     #$e0
	sta     $2132                   ; color data for addition/subtraction
	stz     $2133                   ; Screen setting (interlace x,y/enable SFX data)
	stz     $4200                   ; Enable V-blank, interrupt, Joypad register
	lda     #$ff
	stz     $4201                   ; Programmable I/O port
	stz     $4202                   ; Multiplicand A
	stz     $4203                   ; Multiplier B
	stz     $4204                   ; Multiplier C
	stz     $4205                   ; Multiplicand C
	stz     $4206                   ; Divisor B
	stz     $4207                   ; Horizontal Count Timer
	stz     $4208                   ; Horizontal Count Timer MSB (most significant bit)
	stz     $4209                   ; Vertical Count Timer
	stz     $420a                   ; Vertical Count Timer MSB
	stz     $420b                   ; General DMA enable (bits 0-7)
	stz     $420c                   ; Horizontal DMA (HDMA) enable (bits 0-7)
	stz     $420d                   ; Access cycle designation (slow/fast rom)
	pla                             ; Restore processor status.
	plp                             ; Restore all registers.
	rts

;now, back to my own work! =)
initmem: php
	rep #$30
	pha
	phx
	lda #$000
	ldx #$2000
-	sta $7E0000,x	;clear last 56k of 1st ram bank
	inx
	inx
	bne -
-	sta $7F0000,x	;clear last 64k of mem
	inx
	inx
	bne -
	plx
	pla
	plp
	rts

initcol: php
	rep #$30
	pha
	phx
	sep #$20
	lda #$0
	sta $2121
	ldx #$200
-	sta $2122
	dex
	bne -
	plx
	rep #$20
	pla
	plp
	rts

initvid: php
	rep #$30
	pha
	phx
	lda #$000
	ldx #$000
	sta $2116
	sta $2117
-	sta $2118
	inx
	inx
	bne -
	plx
	pla
	plp
	rts

initoam: php
	rep #$10
	pha
	phx
	lda #$0
	sta $2102
	lda #$0
	sta $2103
	ldx #$220
	lda #$0
-	sta $2104
	dex
	bne -
	plx
	pla
	plp
	rts

.cartridge
   title           =       "Mode 7 interactive demo"
   mode            =       $20
   type            =       $00
   romsize         =       $00
   ramsize         =       $00
   country         =       $00
   maker           =       $00
   version         =       $00
.end

.pad $ffea
	.dw nmi

.pad $fffc
	.dw start,$000
