	.title Sean Riddle's Williams vidctrs test
	;www.seanriddle.com
	;assembled with as6809
		;del willyvidctrs.bin
		;del willyvidctrs.rel
		;del willyvidctrs.s19
		;as6809 -l -o -s willyvidctrs.asm
		;aslink -s -m -u -b _CODEF000=0xF000 -b _VECTORS=0xfff0 willyvidctrs.rel
		;srec_cat willyvidctrs.s19 -o willyvidctrs.bin -binary

	;3/13/2012 SKR created 

	;game hardware
	screen_end = 0x9800				;screen memory from $0000-$97FF
	ram_end = 0xc000				;RAM from $0000-$BFFF

	color_registers = 0xC000
	COLOR_BLUE = 0xc0
	COLOR_WHITE = 0xff

	widget_pia_dataa = 0xC804		;control panel
	widget_pia_ctrla = 0xC805
	widget_pia_datab = 0xC806
	widget_pia_ctrlb = 0xC807

	rom_pia_dataa = 0xC80C			;coin door and sound board
	rom_pia_ctrla = 0xC80D
	rom_pia_datab = 0xC80E
	rom_pia_ctrlb = 0xC80F

	rom_enable_scr_ctrl = 0xC900	;when D0=1, reads come from ROM, not RAM

	start_blitter = 0xCA00			;special chip registers
	blitter_mask = 0xCA01
	blitter_source = 0xCA02
	blitter_dest = 0xCA04
	blitter_w_h = 0xCA06

	vidctrs = 0xcb00				;beam position
	
	watchdog = 0xCBFF
	watchdog_data = 0x39

	stack = 0xbf00					;stack pointer

	EXTREG =   0xa000				;for 6809 test suite

	EOT =   0x00		;end of text
	
	curpos = 0xb000					;cursor position for PSTRNG 2 bytes
	tmp = 0xb002					;1 byte
	numb = 0xbffe					;2 bytes
	
	PARPORT = 0xc900				;used for ROM dump
		
	.area	_CODEF000				;this area will link at $D000
	
reset:
	orcc	#0xff
	lds		#stack					;set up stack pointer
	ldb		#watchdog_data
	stb		watchdog

	clr		rom_pia_ctrla			;setup ROM PIA
	clr		rom_pia_dataa
	lda		#0x3c
	sta		rom_pia_ctrla
	clr		rom_pia_ctrlb
	lda		#0xff
	sta		rom_pia_datab
	lda		#0x35
	sta		rom_pia_ctrlb
	lda		#0x3f
	sta		rom_pia_datab

	ldb		#0x09
	jsr		set_LED
	jsr		delay_2000
	ldb		#0x08
	jsr		set_LED
	jsr		delay_2000
	ldb		#0x07
	jsr		set_LED
	jsr		delay_2000
	ldb		#0x06
	jsr		set_LED
	jsr		delay_2000
	ldb		#0x05
	jsr		set_LED
	jsr		delay_2000
	ldb		#0x04
	jsr		set_LED
	jsr		delay_2000
	ldb		#0x03
	jsr		set_LED
	jsr		delay_2000
	ldb		#0x02
	jsr		set_LED
	jsr		delay_2000
	ldb		#0x01
	jsr		set_LED
	jsr		delay_2000
	ldb		#0x00
	jsr		set_LED
	jsr		delay_2000
	
	clr		widget_pia_ctrla		;setup widget PIA
	clr		widget_pia_ctrlb
	clr		widget_pia_dataa
	clr		widget_pia_datab
	lda		#0x34
	sta		widget_pia_ctrla
	lda		#0x3c					;select B controls for joystick
	sta		widget_pia_ctrlb

	lda		#0x00					;enable RAM
	sta		rom_enable_scr_ctrl

	ldb		#0x01
	jsr		set_LED

	jsr		clear_screen
	jsr		load_palette_graybkg

	ldx		#0x1c88
	stx		curpos
	LDX		#TITLEM1
	JSR		PSTRNG

	ldb		#watchdog_data
	jsr		delay_ffff
	jsr		delay_ffff
	jsr		delay_ffff
	jsr		clear_screen	
	jsr		load_palette_greenbkg

	ldb		#watchdog_data
	stb		watchdog

	ldx		#0x0000
1$:
	lda		vidctrs					;sync to video vert blank
	anda		#0xfc
	bne		1$

2$:
	stb		watchdog
	lda		vidctrs
	sta		,x+
	cmpx		#0x8000
	bne		2$

	ldu		#0x8000			;length
	stu		numb
	ldu		#0x0000			;start
	jmp		dumpram	

	jmp		spin	




	;subroutines

dumpram:							;bit-bangs RAM buffer from U to numb - uses A, B, X, U
LEDON:
	ldb		#0x08
	tfr		b,a
	lsla
	lsla
	lsla
	lsla
	lsla
	lsla
	sta		rom_pia_datab			;set bits A & B
	tfr		b,a
	anda		#0x08
	ora		#0x34					;select Cx2 as output
	sta		rom_pia_ctrla			;set bit D
	tfr		b,a
	lsla
	anda		#0x08
	ora		#0x34
	sta		rom_pia_ctrlb			;set bit C

	lda		#0x02					;enable RAM, trigger ROM dump w/screen flip
	sta		rom_enable_scr_ctrl

parbyteout:
	ldb		#watchdog_data
	stb		watchdog
	
	lda		,u
	anda		#0xf0
	sta		PARPORT

	ldx		#0x0020
$delay1:
	leax 		-1,x
	bne		$delay1

	lda		,u+
	rola
	rola
	rola
	rola
	anda		#0xf0
	sta		PARPORT

	ldx		#0x0020
$delay2:
	leax 		-1,x
	bne		$delay2
		
	cmpu		numb
	bne		parbyteout

	lda		#0x00					;enable RAM, turn off trigger
	sta		rom_enable_scr_ctrl

spin:
	ldb		#watchdog_data
	stb		watchdog
	bra		spin



;delays - must ldb #watchdog_data before calling
delay_ffff:
	ldx		#0xffff
	bra		delay_x
delay_2000:
	ldx		#0x2000
	bra		delay_x
delay_600:
	ldx		#0x0600
	bra		delay_x
delay_300:
	ldx		#0x0300
delay_x:
	stb		watchdog
	leax	0xffff,x
	bne		delay_x
	rts
	

clear_screen:						;write 0x00 to 0x0000-0x97ff
	lda		#0x00
fill_screen:
	ldx		#0x0000
1$:
	sta		,x+
	stb		watchdog
	cmpx	#screen_end
	bne		1$
	rts


load_palette_allblack:
	ldy		#allblack
load_palette:						;load palette from y
	ldx		#color_registers
2$:
	lda		vidctrs
	anda	#0xfc
	bne		2$
1$:
	lda		,y+
	sta		,x+
	cmpx	#color_registers+0x10
	bne		1$
	rts
			
load_mypalette:
	ldy		#mypalette
	bra		load_palette

load_dimpalette:
	ldy		#dimpalette
	bra		load_palette

load_palette2:
	ldy		#palette2
	bra		load_palette

load_palette_redbkg:
	ldy		#redbkg
	bra		load_palette

load_palette_bluebkg:
	ldy		#bluebkg
	bra		load_palette

load_palette_purplebkg:
	ldy		#purplebkg
	bra		load_palette

load_palette_allpurple:
	ldy		#allpurple
	bra		load_palette

load_palette_greenbkg:
	ldy		#greenbkg
	bra		load_palette

load_palette_graybkg:
	ldy		#graybkg
	bra		load_palette

load_palette_allgreen:
	ldy		#allgreen
	bra		load_palette

load_palette_allblue:
	ldy		#allblue
	bra		load_palette

load_palette_allwhite:
	ldy		#allwhite
	bra		load_palette


;colors are BB GGG RRR
fadein:								;mask with 00, 09, 52, 5b, a4, ad, f6 and ff
	ldb		#0x00
2$:
	ldy		#mypalette
	ldx		#color_registers
6$:
	lda		vidctrs					;sync to video vert blank
	anda	#0xfc
	bne		6$
1$:
	tfr		b,a
	anda	,y+
	sta		,x+
	cmpx	#color_registers+0x10
	bne		1$
	tfr		b,a
	ldb		#watchdog_data
	jsr		delay_300
	tfr		a,b
	cmpb	#0xff
	beq		5$
	cmpb	#0x09
	beq		3$
	cmpb	#0x5b
	beq		3$
	cmpb	#0xad
	bne		4$
3$:
	addb	#0x40
4$:
	addb	#0x09
	bra 	2$
5$:
	ldb		#watchdog_data
	rts

	
fadeout:							;mask with ff, f6, ad, a4, 5b, 52, 09 and 00
	ldb		#0xff
2$:
	ldy		#mypalette
	ldx		#color_registers
6$:
	lda		vidctrs					;sync to video vert blank
	anda	#0xfc
	bne		6$
1$:
	tfr		b,a
	anda	,y+
	sta		,x+
	cmpx	#color_registers+0x10
	bne		1$
	tfr		b,a
	ldb		#watchdog_data
	jsr		delay_300
	tfr		a,b
	cmpb	#0x00
	beq		5$
	cmpb	#0xf6
	beq		3$
	cmpb	#0xa4
	beq		3$
	cmpb	#0x52
	bne		4$
3$:
	subb	#0x40
4$:
	subb	#0x09
	bra 	2$
5$:
	ldb		#watchdog_data
	rts


set_LED:
	andb	#0x0f
	tfr		b,a
	lsla
	lsla
	lsla
	lsla
	lsla
	lsla
	sta		rom_pia_datab			;set bits A & B
	tfr		b,a
	anda	#0x08
	ora		#0x34					;select Cx2 as output
	sta		rom_pia_ctrla			;set bit D
	tfr		b,a
	lsla
	anda	#0x08
	ora		#0x34
	sta		rom_pia_ctrlb			;set bit C
	ldb		#watchdog_data
	rts


PSTRNG:			;print string in X terminated with null
	lda		,x+
	beq		$pexit
	pshs 	x
	jsr		pchar
	puls	x
	bra		PSTRNG
$pexit:		rts


PHEX:			;print value in A in hex
	sta		tmp
	rora		
	rora		
	rora		
	rora		
	anda	#0x0f
	cmpa	#0x0a
	blt		$1a
	adda	#0x07
$1a:
	adda	#0x30
	jsr		pchar
	
	lda		tmp
	anda	#0x0f
	cmpa	#0x0a
	blt		$2a
	adda	#0x07
$2a:
	adda	#0x30
	jmp		pchar


pchar:			;a=ascii char to print, from space to underscore
	cmpa	#0x20
	blt		badchar
	cmpa	#0x60
	blt		goodchar

badchar:
	ldx		#badfont
	bra		blitchar
	
goodchar:
	ldx		#font
	suba	#0x20					;space is first char
	ldb		#0x0a
	mul								;index into font table; 2 bytes wide, 5 bytes tall = 10 bytes per char
	leax	d,x
	
blitchar:
	stx		blitter_source
	lda		#0xff					;replace the current contents
	sta		blitter_mask
	ldx		curpos
	stx		blitter_dest
	ldx		#0x0601					;width and height XOR 0x04 - 2 x 5 bytes = 4 x 5 pixels, 3x5 active w/spaces
	stx		blitter_w_h
	lda		#0x1a					;start blit solid, foreground, linear to screen layout
	sta		start_blitter
	ldx 	curpos
	leax	0x0300,x				;move cursor right 1 char
	stx		curpos
	rts


	;data
	
	;color palettes

mypalette:							;normal palette
	.byte 0x00,0xff,0x07,0x05,0x38,0xc7,0xc0,0x80,0xa4,0xe8,0x14,0x90,0x3f,0x51,0x0a,0x67
	;		k	w	R	 dk.r   G  purp blue dkbl gray skbl brn  slt   yel dkslt dkbrn salmon

dimpalette:							;darker colors
	.byte 0x00,0xa4,0x04,0x03,0x20,0x84,0x80,0x40,0x52,0x98,0x0a,0x48,0x24,0x08,0x01,0x14

palette2:							;other colors
	.byte 0x00,0x02,0x10,0x40,0x04,0x20,0x80,0xff,0xf6,0xa4,0x52,0x3f,0xc7,0xf8,0xa7,0xbc

allred:
	.byte 0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07
	
redbkg:
	.byte 0x07,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff

bluebkg:
	.byte 0xc0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff

greenbkg:
	.byte 0x38,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff

purplebkg:
	.byte 0xc7,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff

graybkg:
	.byte 0xa4,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff

allgreen:
	.byte 0x38,0x38,0x38,0x38,0x38,0x38,0x38,0x38,0x38,0x38,0x38,0x38,0x38,0x38,0x38,0x38

allblack:
	.byte 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00

allwhite:
	.byte 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff

allblue:
	.byte 0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0

allpurple:
	.byte 0xc7,0xc7,0xc7,0xc7,0xc7,0xc7,0xc7,0xc7,0xc7,0xc7,0xc7,0xc7,0xc7,0xc7,0xc7,0xc7


font:
	.byte 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00	;space
	.byte 0x01,0x00,0x01,0x00,0x01,0x00,0x00,0x00,0x01,0x00	;!
	.byte 0x10,0x10,0x10,0x10,0x00,0x00,0x00,0x00,0x00,0x00	;"
	.byte 0x10,0x10,0x11,0x10,0x10,0x10,0x11,0x10,0x10,0x10	;#
	.byte 0x11,0x10,0x11,0x00,0x11,0x10,0x01,0x10,0x11,0x10	;$
	.byte 0x10,0x00,0x00,0x10,0x01,0x00,0x10,0x00,0x00,0x10	;%
	.byte 0x01,0x10,0x10,0x00,0x01,0x00,0x10,0x10,0x01,0x00	;&
	.byte 0x01,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00	;'
	.byte 0x00,0x10,0x01,0x00,0x01,0x00,0x01,0x00,0x00,0x10	;(
	.byte 0x10,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x10,0x00	;)
	.byte 0x01,0x00,0x10,0x10,0x01,0x00,0x10,0x10,0x00,0x00	;*
	.byte 0x00,0x00,0x01,0x00,0x11,0x10,0x01,0x00,0x00,0x00	;+
	.byte 0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x10,0x00	;,
	.byte 0x00,0x00,0x00,0x00,0x11,0x10,0x00,0x00,0x00,0x00	;-
	.byte 0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x01,0x00	;.
	.byte 0x00,0x10,0x00,0x10,0x01,0x00,0x10,0x00,0x10,0x00	;/

	.byte 0x11,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x11,0x10	;0
	.byte 0x01,0x00,0x11,0x00,0x01,0x00,0x01,0x00,0x11,0x10
	.byte 0x11,0x10,0x00,0x10,0x11,0x10,0x10,0x00,0x11,0x10
	.byte 0x11,0x10,0x00,0x10,0x11,0x10,0x00,0x10,0x11,0x10 
	.byte 0x10,0x10,0x10,0x10,0x11,0x10,0x00,0x10,0x00,0x10
	.byte 0x11,0x10,0x10,0x00,0x11,0x10,0x00,0x10,0x11,0x10	;5
	.byte 0x11,0x10,0x10,0x00,0x11,0x10,0x10,0x10,0x11,0x10
	.byte 0x11,0x10,0x00,0x10,0x01,0x00,0x01,0x00,0x01,0x00 
	.byte 0x11,0x10,0x10,0x10,0x11,0x10,0x10,0x10,0x11,0x10
	.byte 0x11,0x10,0x10,0x10,0x11,0x10,0x00,0x10,0x00,0x10	;9
	
	.byte 0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00	;:
	.byte 0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x10,0x00	;;
	.byte 0x00,0x10,0x01,0x00,0x10,0x00,0x01,0x00,0x00,0x10	;<
	.byte 0x00,0x00,0x11,0x10,0x00,0x00,0x11,0x10,0x00,0x00	;=
	.byte 0x10,0x00,0x01,0x00,0x00,0x10,0x01,0x00,0x10,0x00	;>
	.byte 0x01,0x00,0x10,0x10,0x00,0x10,0x01,0x00,0x01,0x00	;?
	.byte 0x01,0x00,0x10,0x10,0x11,0x00,0x10,0x00,0x01,0x10	;@
	
	.byte 0x11,0x10,0x10,0x10,0x11,0x10,0x10,0x10,0x10,0x10 ;A
	.byte 0x11,0x10,0x10,0x10,0x11,0x00,0x10,0x10,0x11,0x10
	.byte 0x11,0x10,0x10,0x00,0x10,0x00,0x10,0x00,0x11,0x10
	.byte 0x11,0x00,0x10,0x10,0x10,0x10,0x10,0x10,0x11,0x00
	.byte 0x11,0x10,0x10,0x00,0x11,0x00,0x10,0x00,0x11,0x10 
	.byte 0x11,0x10,0x10,0x00,0x11,0x00,0x10,0x00,0x10,0x00 
	.byte 0x11,0x10,0x10,0x00,0x10,0x10,0x10,0x10,0x11,0x10
	.byte 0x10,0x10,0x10,0x10,0x11,0x10,0x10,0x10,0x10,0x10
	.byte 0x11,0x10,0x01,0x00,0x01,0x00,0x01,0x00,0x11,0x10
	.byte 0x00,0x10,0x00,0x10,0x00,0x10,0x10,0x10,0x11,0x10
	.byte 0x10,0x10,0x10,0x10,0x11,0x00,0x10,0x10,0x10,0x10
	.byte 0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x11,0x10	;L
	.byte 0x11,0x10,0x11,0x10,0x11,0x10,0x10,0x10,0x10,0x10	;my skinny M
	.byte 0x11,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10
	.byte 0x11,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x11,0x10	;O same as 0
	.byte 0x11,0x10,0x10,0x10,0x11,0x10,0x10,0x00,0x10,0x00
	.byte 0x11,0x10,0x10,0x10,0x10,0x10,0x01,0x00,0x00,0x10
	.byte 0x11,0x10,0x10,0x10,0x11,0x00,0x10,0x10,0x10,0x10
	.byte 0x11,0x10,0x10,0x00,0x11,0x10,0x00,0x10,0x11,0x10	;S same as 5
	.byte 0x11,0x10,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00
	.byte 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x11,0x10
	.byte 0x10,0x10,0x10,0x10,0x10,0x10,0x01,0x00,0x01,0x00 
	.byte 0x10,0x10,0x10,0x10,0x11,0x10,0x11,0x10,0x11,0x10 ;my skinny W	 
	.byte 0x10,0x10,0x10,0x10,0x01,0x00,0x10,0x10,0x10,0x10
	.byte 0x10,0x10,0x10,0x10,0x11,0x10,0x01,0x00,0x01,0x00
	.byte 0x11,0x10,0x00,0x10,0x01,0x00,0x10,0x00,0x11,0x10	;Z

	.byte 0x11,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x11,0x00	;[
	.byte 0x10,0x00,0x10,0x00,0x01,0x00,0x00,0x10,0x00,0x10	;\
	.byte 0x01,0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x01,0x10	;]
	.byte 0x01,0x00,0x10,0x10,0x00,0x00,0x00,0x00,0x00,0x00	;^
	.byte 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x11,0x10	;_
	
badfont:
	.byte 0x11,0x10,0x00,0x00,0x11,0x10,0x00,0x00,0x11,0x10	;lines
	
blitdata:
	.byte 00,01,02,03,04,05,06,07,08,09,10,11,12,13,14,15
	.byte 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
	.byte 32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47
	.byte 48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63
	.byte 64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79
	.byte 80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95
	.byte 96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111
	.byte 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127
	.byte 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143
	.byte 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159
	.byte 160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175
	.byte 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191
	.byte 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207
	.byte 208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223
	.byte 224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239
	.byte 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255
	
	


TITLEM1:.FCC   'WILLIAMS VIDCTRS TEST V0.2'
        .byte	EOT

;unused space from end of code to FFEF

	.area	_VECTORS				;CPU vectors at FFF0

	.word	#reset,#reset,#reset,#reset,#reset,#reset,#reset,#reset	;point them all to start of code

	.end
