	.title Sean Riddle's Williams Emulation Test Suite
	;www.seanriddle.com
	;assembled with as6809

	;2/8/2012 SKR created 
	;2/12/2012 V0.1 posted 	- 6809 instruction tests, 6 blitter speed tests
	;2/12/2012 V0.2 posted 	- added 5 more blitter speed tests from MAME log file
	;2/14/2012 V0.3 posted 	- normalized number of blitter loops to make each test about 1 minute
	;						- moved variables in memory

	;game hardware
	screen_end = 0x9800				;screen memory from $0000-$97FF

	color_registers = 0xC000

	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
	blitter_mask = 0xCA01
	blitter_source = 0xCA02
	blitter_dest = 0xCA04
	blitter_w_h = 0xCA06

	vidctrs = 0xcb00
	watchdog = 0xCBFF
	watchdog_data = 0x39

	stack = 0xbf00					;stack pointer

	EXTREG =   0xa000				;for 6809 test suite
	
	curpos = 0xb000					;cursor position for PSTRNG 2 bytes
	
	tmp_blitter_source = 0xb002		;2 bytes
	tmp_blitter_dest = 0xb004		;2 bytes
	tmp_blitter_w_h = 0xb006		;2 bytes
	tmp_blitter_mask = 0xb008		;1 byte
	tmp_start_blitter = 0xb009		;1 byte
	tmp_blitter_loops = 0xb00a		;1 byte
	
	failed_6809=0xb00a				;1 byte flag indicating 6809 test suite failed
		
	.area	_CODEE000				;this area will link at $E000
	
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		#0x00
	jsr		set_LED
	
	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		#0x01					;enable ROM
	sta		rom_enable_scr_ctrl

	ldb		#0x01
	jsr		set_LED

	jsr		clear_screen
	jsr		load_palette_bluebkg

	ldx		#0x1c88
	stx		curpos
	LDX		#TITLEM1
	JSR		PSTRNG

	ldb		#watchdog_data
	jsr		delay_ffff
	jsr		delay_ffff
	jsr		delay_ffff
	jsr		clear_screen
	
	jsr		test6809	

	ldb		failed_6809
	bne		nosuccess

	ldb		#0x03
	jsr		set_LED
	
	jsr		load_palette_greenbkg

	ldx	#0x2c88
	stx	curpos
	LDX   #PASSM1
	JSR   PSTRNG
	
	ldb		#watchdog_data
	jsr		delay_ffff
	jsr		delay_ffff
	jsr		delay_ffff

nosuccess:	
	jsr		fadeout

	ldb		#watchdog_data
	jsr		delay_ffff

;blitter timing tests
	ldy		#blit_paras_start
blitloop:
	pshs	y
	jsr		load_palette_allwhite
	puls	y

	ldx		,y++
	stx		tmp_blitter_source
	lda		,y+
	sta		tmp_blitter_mask
	ldx		,y++
	stx		tmp_blitter_dest
	ldx		,y++
	stx		tmp_blitter_w_h
	lda		,y+
	sta		tmp_start_blitter	
	lda		,y+	
	sta		tmp_blitter_loops	;this many loops of 65535...
	ldx		,y++				;...plus this many more loops

	ldb		#watchdog_data
1$:
	stb		watchdog
	pshs	x					;loops remaining
	
	ldx		tmp_blitter_source
	stx		blitter_source
	lda		tmp_blitter_mask
	sta		blitter_mask
	ldx		tmp_blitter_dest
	stx		blitter_dest
	ldx		tmp_blitter_w_h
	stx		blitter_w_h
	lda		tmp_start_blitter
	sta		start_blitter	
	
	puls	x
	leax	0xffff,x
	bne		1$
	
	lda		tmp_blitter_loops
	beq		blitter_pass_done
	
	suba	#0x01
	sta		tmp_blitter_loops
	
	ldx		#0xffff
	
	bra		1$

blitter_pass_done:
	pshs	y
	jsr		load_palette_allgreen

	ldb		#watchdog_data
	jsr		delay_ffff

	puls	y
	cmpy	#blit_paras_end	
	blt		blitloop
	
spin:
	ldb		#watchdog_data
	stb		watchdog
	bra		spin


;blitter test parameters
blit_paras_start:

			;source addr,	mask, 	dest addr, 	w/ht,		mode,	loops
	.byte	0xf0, 0x00, 	0xff,	0x3c,0x99,	0x24,0x24,	0x02,	0x00,0xd0,0x68
			;ROM-RAM, lin src, scr dst - 60.0s actual, 60.0s in MAME0145 64-bit
			
	.byte	0x00, 0x00, 	0xff,	0x3c,0x99,	0x24,0x24,	0x06,	0x00,0x6d,0x2a
			;RAM-RAM, RAM, lin src, scr dst - 60.0s actual, 33.1s in MAME0145 64-bit
			
	.byte	0x00, 0x00, 	0xff,	0x3c,0x99,	0x24,0x24,	0x07,	0x00,0x6d,0x2a
			;RAM-RAM, RAM, scr src, scr dst - 60.0s actual, 39.3s in MAME0145 64-bit
			
	.byte	0xd0, 0x00, 	0xff,	0x3c,0x99,	0x24,0x24,	0x02,	0x00,0xd0,0x68
			;Sin SRAM-RAM, lin src, scr dst - 60.0s actual, 60.0s in MAME0145 64-bit
			
	.byte	0xd0, 0x00, 	0xff,	0x3c,0x99,	0x24,0x24,	0x03,	0x00,0xd0,0x68
			;Sin SRAM-RAM, scr src, scr dst - 60.0s actual, 59.9s in MAME0145 64-bit
			
	.byte	0xf0, 0x00, 	0x11,	0x3c,0x99,	0x24,0x24,	0x02,	0x00,0xd0,0x68
			;ROM-RAM, lin src, scr dst - 60.0s actual, 60.0s in MAME0145 64-bit

;the following masks, widths/heights and modes are taken from a MAME log for a Robotron game
;so they should be a representative sample

			;source addr,	mask, 	dest addr, 	w/ht,		mode,	loops
	.byte	0xf0, 0x00, 	0x99,	0x3c,0x99,	0x07,0x02,	0x1a,	0x07,0x7f,0xf8
			;ROM-RAM, solid FG, lin src, scr dst - 52.3s actual, 52.7s MAME0145 64-bit
	
	.byte	0xf0, 0x00, 	0x00,	0x3c,0x99,	0x01,0x09,	0x0a,	0x05,0xb6,0xd5
			;ROM-RAM, FG, lin src, scr dst - 57.6s actual, 58.0s in MAME0145 64-bit
	
	.byte	0xf0, 0x00, 	0x00,	0x3c,0x99,	0x0c,0x0b,	0x2a,	0x04,0x23,0x4b
			;ROM-RAM, Shift FG, lin src, scr dst - 56.9s actual, 59.1s MAME0145 64-bit
														
	.byte	0x00, 0x00, 	0x00,	0x3c,0x99,	0x05,0x09,	0x56,	0x07,0x51,0x24
			;RAM-RAM, Even Solid RAM, lin src, scr dst - 54.7s actual, 48.9s MAME0145 64-bit
														
	.byte	0x00, 0x00, 	0x46,	0x3c,0x99,	0x05,0x08,	0x46,	0x07,0x51,0x24
			;RAM-RAM, Even RAM, lin src, scr dst - 53.1s actual, 48.7s in MAME0145 64-bit
	
blit_paras_end:


	;subroutines

;delays - must ldb #watchdog_data before calling
delay_ffff:
	ldx		#0xffff
	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


draw_gfx:							;blit block onto screen - x holds starting address
	stx		blitter_source
	lda		#0xff					;replace the current contents
	sta		blitter_mask
	ldx		#0x3c99					;destination x=120, y=153
	stx		blitter_dest
	ldx		#0x2424					;width and height XOR 0x04 - 32 x 32 bytes = 64 x 32 pixels
	stx		blitter_w_h
	lda		#0x02					;start blit linear to screen layout
	sta		start_blitter
	rts

	;Bit 0 =linear/screen source
	;Bit 1 =linear/screen dest
	;Bit 2 sync for blits from RAM, slowing 
	;Bit 3 tells the blitter only to blit the foreground
	;Bit 4 is 'solid' mode.
	;Bit 5 shifts the image one pixel to the right
	;Bit 6 says even only, bit 7 says odd only
	

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_greenbkg:
	ldy		#greenbkg
	bra		load_palette

load_palette_allgreen:
	ldy		#allgreen
	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
$1:
	lda		,x+
	beq		$2
	pshs 	x
	jsr		pchar
	puls	x
	bra		$1
$2:
	rts


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

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


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
	

	.area	_CODEEC00				;this area will link at EC00
	
;  MC6809 CPU Emulation Validation
;
; Tested on an SGS Thomson EF6809P Processor
;
; W. Schwotzer                     20.07.2003
;

	EOT =   0x00		;end of text

test6809:
	ldb		#0x00
	stb		failed_6809

; Test Addressing modes
	ldb		#watchdog_data
	stb		watchdog
       JSR  TAD1
	ldb		#watchdog_data
	stb		watchdog
       JSR  TAD2
	ldb		#watchdog_data
	stb		watchdog
       JSR  TAD3
	ldb		#watchdog_data
	stb		watchdog
       JSR  TAD4
	ldb		#watchdog_data
	stb		watchdog
       JSR  TAD5
	ldb		#watchdog_data
	stb		watchdog
       JSR  TAD6
	ldb		#watchdog_data
	stb		watchdog
       JSR  TAD7
	ldb		#watchdog_data
	stb		watchdog
       JSR  TAD8
	ldb		#watchdog_data
	stb		watchdog
       JSR  TAD9
	ldb		#watchdog_data
	stb		watchdog
       JSR  TADA
	ldb		#watchdog_data
	stb		watchdog
       JSR  TADB
	ldb		#watchdog_data
	stb		watchdog
       JSR  TADI1
	ldb		#watchdog_data
	stb		watchdog
       JSR  TADI3
	ldb		#watchdog_data
	stb		watchdog
       JSR  TADI5
	ldb		#watchdog_data
	stb		watchdog
       JSR  TADI6
	ldb		#watchdog_data
	stb		watchdog
       JSR  TADI7
	ldb		#watchdog_data
	stb		watchdog
       JSR  TADI8
	ldb		#watchdog_data
	stb		watchdog
       JSR  TADI9
	ldb		#watchdog_data
	stb		watchdog
       JSR  TADIA
	ldb		#watchdog_data
	stb		watchdog
       JSR  TADIB
	ldb		#watchdog_data
	stb		watchdog
       JSR  TADIC

; Test Instructions
	ldb		#watchdog_data
	stb		watchdog
       JSR   TNEG
	ldb		#watchdog_data
	stb		watchdog
       JSR   TCOM
	ldb		#watchdog_data
	stb		watchdog
       JSR   TDEC
	ldb		#watchdog_data
	stb		watchdog
       JSR   TINC
	ldb		#watchdog_data
	stb		watchdog
       JSR   TCLR
	ldb		#watchdog_data
	stb		watchdog
       JSR   TADD
	ldb		#watchdog_data
	stb		watchdog
       JSR   TADDD
	ldb		#watchdog_data
	stb		watchdog
       JSR   TADC
	ldb		#watchdog_data
	stb		watchdog
       JSR   TMUL
	ldb		#watchdog_data
	stb		watchdog
       JSR   TSEX
	ldb		#watchdog_data
	stb		watchdog
       JSR   TSUB
	ldb		#watchdog_data
	stb		watchdog
       JSR   TSUBD
	ldb		#watchdog_data
	stb		watchdog
       JSR   TSBC
	ldb		#watchdog_data
	stb		watchdog
       JSR   TDAA
	ldb		#watchdog_data
	stb		watchdog
       JSR   TCMP
	ldb		#watchdog_data
	stb		watchdog
       JSR   TCMPD
	ldb		#watchdog_data
	stb		watchdog
       JSR   TTST
	ldb		#watchdog_data
	stb		watchdog
       JSR   TBIT
	ldb		#watchdog_data
	stb		watchdog
       JSR   TLSR
	ldb		#watchdog_data
	stb		watchdog
       JSR   TLSL
	ldb		#watchdog_data
	stb		watchdog
       JSR   TASR
	ldb		#watchdog_data
	stb		watchdog
       JSR   TROL
	ldb		#watchdog_data
	stb		watchdog
       JSR   TROR
	ldb		#watchdog_data
	stb		watchdog
       JSR   TLD
	ldb		#watchdog_data
	stb		watchdog
       JSR   TST
	ldb		#watchdog_data
	stb		watchdog
       JSR   TLDD
	ldb		#watchdog_data
	stb		watchdog
       JSR   TSTD
	ldb		#watchdog_data
	stb		watchdog
       JSR   TLEA
	ldb		#watchdog_data
	stb		watchdog
       JSR   TTFR
	ldb		#watchdog_data
	stb		watchdog
       JSR   TEXG
	ldb		#watchdog_data
	stb		watchdog
       
       rts
;end of 6809 test suite


;**************************************************
; Test MUL
;**************************************************
TMUL:   LDU   #LMUL
       LDX   #TBMUL
TMUL1:  LDD   ,X++
       MUL
       TFR   CC,DP
       CMPD  ,X++
       LBNE  OUTERR
       TFR   DP,B
       ANDB  #0x05   ; Only check Bits ZC
       CMPB  ,X+
       LBNE  OUTERR
       CMPX  #LMUL
       LBNE  TMUL1
       RTS

TBMUL:  .FDB   0x0000,0x0000
       .FCB   0x04
       .FDB   0x8080,0x4000
       .FCB   0x00
       .FDB   0x01FF,0x00FF
       .FCB   0x01
       .FDB   0xFF01,0x00FF
       .FCB   0x01
       .FDB   0x7F81,0x3FFF
       .FCB   0x01
       .FDB   0x817F,0x3FFF
       .FCB   0x01
       .FDB   0xC040,0x3000
       .FCB   0x00
       .FDB   0x40C0,0x3000
       .FCB   0x00
       .FDB   0xFFFF,0xFE01
       .FCB   0x00
       .FDB   0x7F7F,0x3F01
       .FCB   0x00
       .FDB   0x0101,0x0001
       .FCB   0x00
LMUL:   .FCC   'MUL'
		.FCB	EOT

;**************************************************
; Test SEX
;**************************************************
TSEX:   LDU   #LSEX
       LDX   #TBSEX
TSEX1:  LDB   ,X+
       SEX
       TFR   CC,DP
       CMPD  ,X++
       LBNE  OUTERR
       TFR   DP,B
       ANDB  #0x0C   ; Only check Bits NZ
       CMPB  ,X+
       LBNE  OUTERR
       CMPX  #LSEX
       LBNE  TSEX1
       RTS


TBSEX:  .FCB   0x00
       .FDB   0x0000
       .FCB   0x04
       .FCB   0x01
       .FDB   0x0001
       .FCB   0x00
       .FCB   0x7F
       .FDB   0x007F
       .FCB   0x00
       .FCB   0x80
       .FDB   0xFF80
       .FCB   0x08
       .FCB   0xFF
       .FDB   0xFFFF
       .FCB   0x08
LSEX:  .FCC   'SEX'
		.FCB	EOT


;**************************************************
; Test TST
;**************************************************
TTST:   LDU   #LTST
       LDX   #TBTST
TTST1:  LDA   ,X+
       TSTA
       TFR   CC,B
       ANDB  #0x0E   ; Only check Bits NZV
       CMPB  ,X+
       LBNE  OUTERR
       CMPX  #LTST
       LBNE  TTST1
       RTS

TBTST:  .FCB   0x00,0x04
       .FCB   0x80,0x08
       .FCB   0x01,0x00
       .FCB   0xFF,0x08
       .FCB   0x7F,0x00
       .FCB   0x81,0x08
       .FCB   0xC0,0x08
       .FCB   0x40,0x00
LTST:   .FCC   'TST'
		.FCB	EOT


;**************************************************
; Test LD
;**************************************************
TLD:    LDU   #LLD
       LDX   #TBTST
TLD1:   LDA   ,X+
       TFR   CC,B
       ANDB  #0x0E   ; Only check Bits NZV
       CMPB  ,X+
       LBNE  OUTERR
       CMPX  #LTST
       LBNE  TLD1
       RTS
LLD:    .FCC   'LD'
		.FCB	EOT


;**************************************************
; Test ST
;**************************************************
TST:    LDU   #LST
       LDX   #TBTST
TST1:   LDA   ,X+
       STA   EXTREG
       TFR   CC,B
       ANDB  #0x0E   ; Only check Bits NZV
       CMPB  ,X+
       LBNE  OUTERR
       CMPX  #LTST
       LBNE  TST1
       RTS
LST:    .FCC   'ST'
		.FCB	EOT


;**************************************************
; Test LDD
;**************************************************
TLDD:   LDU   #LLDD
       LDX   #TBLDD
TLDD1:  LDD   ,X++
       TFR   CC,B
       ANDB  #0x0E   ; Only check Bits NZV
       CMPB  ,X+
       LBNE  OUTERR
       CMPX  #LLDD
       LBNE  TLDD1
       RTS
TBLDD:  .FDB   0x0000
       .FCB   0x04
       .FDB   0x8000
       .FCB   0x08
       .FDB   0x0001
       .FCB   0x00
       .FDB   0xFFFF
       .FCB   0x08
       .FDB   0x7FFF
       .FCB   0x00
       .FDB   0x8001
       .FCB   0x08
       .FDB   0xC000
       .FCB   0x08
       .FDB   0x4000
       .FCB   0x00
LLDD:   .FCC   'LDD'
		.FCB	EOT


;**************************************************
; Test STD
;**************************************************
TSTD:   LDU   #LSTD
       LDX   #TBLDD
TSTD1:  LDD   ,X++
       STD   EXTREG
       TFR   CC,B
       ANDB  #0x0E   ; Only check Bits NZV
       CMPB  ,X+
       LBNE  OUTERR
       CMPX  #LLDD
       LBNE  TSTD1
       RTS
LSTD:   .FCC   'STD'
		.FCB	EOT


;**************************************************
; Test LEA
;**************************************************
TLEA:   LDU   #LLEA
       LDX   #TBLEA
TLEA1:  LDY   ,X++
       LEAY  ,Y
       TFR   CC,B
       ANDB  #0x04   ; Only check Bit Z
       CMPB  ,X+
       LBNE  OUTERR
       CMPX  #LLEA
       LBNE  TLEA1
       RTS

TBLEA:  .FDB   0x0000
       .FCB   0x04
       .FDB   0x0001
       .FCB   0x00
       .FDB   0xFFFF
       .FCB   0x00
       .FDB   0x7FFF
       .FCB   0x00
       .FDB   0x8000
       .FCB   0x00
       .FDB   0x8001
       .FCB   0x00
LLEA:   .FCC   'LEA'
		.FCB	EOT


;**************************************************
; Test BIT
;**************************************************
TBIT:   LDU   #LBIT
       LDX   #TBBIT
TBIT1:  LDA   ,X+
       BITA  ,X+
       TFR   CC,B
       ANDB  #0x0E   ; Only check Bits NZV
       CMPB  ,X+
       LBNE  OUTERR
       CMPX  #LBIT
       LBNE  TBIT1
       RTS

TBBIT:  .FCB   0x00,0x00,0x04
       .FCB   0xAA,0x55,0x04
       .FCB   0xAA,0xAA,0x08
       .FCB   0x55,0x55,0x00
       .FCB   0xFF,0xFF,0x08
       .FCB   0xFF,0x80,0x08
       .FCB   0x81,0x80,0x08
       .FCB   0xFF,0x7F,0x00
       .FCB   0xFF,0x01,0x00
       .FCB   0xF0,0x0F,0x04
LBIT:   .FCC   'BIT'
		.FCB	EOT



;**************************************************
; Test DAA
;**************************************************
TDAA:  LDU   #LDAA
       LDX   #TBDAA
TDAA1:  LDA   ,X+
       TFR   CC,B
       ANDB  ,X+
       ORB   ,X+
       TFR   B,CC
       DAA
       TFR   CC,B
       CMPA  ,X+
       LBNE  OUTERR
       ANDB  #0x0D   ; Only check Bits NZC
       CMPB  ,X+
       LBNE  OUTERR
       CMPX  #LDAA
       LBNE  TDAA1
       RTS

TBDAA:
; Carry: 0, Halfcarry: 0
       .byte   0x00,0xDE,0x00,0x00,0x04
       .byte   0x01,0xDE,0x00,0x01,0x00
       .byte   0x09,0xDE,0x00,0x09,0x00
       .byte   0x0A,0xDE,0x00,0x10,0x00
       .byte   0x0F,0xDE,0x00,0x15,0x00
       .byte   0x10,0xDE,0x00,0x10,0x00
       .byte   0x4A,0xDE,0x00,0x50,0x00
       .byte   0x79,0xDE,0x00,0x79,0x00
       .byte   0x7A,0xDE,0x00,0x80,0x08
       .byte   0x7F,0xDE,0x00,0x85,0x08
       .byte   0x81,0xDE,0x00,0x81,0x08
       .byte   0x99,0xDE,0x00,0x99,0x08
       .byte   0xA0,0xDE,0x00,0x00,0x05
       .byte   0xBF,0xDE,0x00,0x25,0x01
       .byte   0xF0,0xDE,0x00,0x50,0x01
       .byte   0xFF,0xDE,0x00,0x65,0x01
; Carry: 1, Halfcarry: 0
       .byte   0x00,0xDF,0x01,0x60,0x01
       .byte   0x01,0xDF,0x01,0x61,0x01
       .byte   0x09,0xDF,0x01,0x69,0x01
       .byte   0x0A,0xDF,0x01,0x70,0x01
       .byte   0x0F,0xDF,0x01,0x75,0x01
       .byte   0x10,0xDF,0x01,0x70,0x01
       .byte   0x4A,0xDF,0x01,0xB0,0x09
       .byte   0x79,0xDF,0x01,0xD9,0x09
       .byte   0x7A,0xDF,0x01,0xE0,0x09
       .byte   0x7F,0xDF,0x01,0xe5,0x09
       .byte   0x81,0xDF,0x01,0xE1,0x09
       .byte   0x99,0xDF,0x01,0xF9,0x09
       .byte   0xA0,0xDF,0x01,0x00,0x05
       .byte   0xBF,0xDF,0x01,0x25,0x01
       .byte   0xF0,0xDF,0x01,0x50,0x01
       .byte   0xFF,0xDF,0x01,0x65,0x01
; Carry: 0, Halfcarry: 1
       .byte   0x00,0xFE,0x20,0x06,0x00
       .byte   0x01,0xFE,0x20,0x07,0x00
       .byte   0x09,0xFE,0x20,0x0F,0x00
       .byte   0x0A,0xFE,0x20,0x10,0x00
       .byte   0x0F,0xFE,0x20,0x15,0x00
       .byte   0x10,0xFE,0x20,0x16,0x00
       .byte   0x4A,0xFE,0x20,0x50,0x00
       .byte   0x79,0xFE,0x20,0x7F,0x00
       .byte   0x7A,0xFE,0x20,0x80,0x08
       .byte   0x7F,0xFE,0x20,0x85,0x08
       .byte   0x81,0xFE,0x20,0x87,0x08
       .byte   0x99,0xFE,0x20,0x9F,0x08
       .byte   0xA0,0xFE,0x20,0x06,0x01
       .byte   0xBF,0xFE,0x20,0x25,0x01
       .byte   0xF0,0xFE,0x20,0x56,0x01
       .byte   0xFF,0xFE,0x20,0x65,0x01
; Carry: 1, Halfcarry: 1
       .byte   0x00,0xFF,0x21,0x66,0x01
       .byte   0x01,0xFF,0x21,0x67,0x01
       .byte   0x09,0xFF,0x21,0x6F,0x01
       .byte   0x0A,0xFF,0x21,0x70,0x01
       .byte   0x0F,0xFF,0x21,0x75,0x01
       .byte   0x10,0xFF,0x21,0x76,0x01
       .byte   0x4A,0xFF,0x21,0xB0,0x09
       .byte   0x79,0xFF,0x21,0xDF,0x09
       .byte   0x7A,0xFF,0x21,0xE0,0x09
       .byte   0x7F,0xFF,0x21,0xE5,0x09
       .byte   0x81,0xFF,0x21,0xE7,0x09
       .byte   0x99,0xFF,0x21,0xFF,0x09
       .byte   0xA0,0xFF,0x21,0x06,0x01
       .byte   0xBF,0xFF,0x21,0x25,0x01
       .byte   0xF0,0xFF,0x21,0x56,0x01
       .byte   0xFF,0xFF,0x21,0x65,0x01
LDAA:  .FCC   'DAA'
		.byte  EOT

;*************************************************
; Test NEG
;*************************************************
TNEG:   LDU   #LNEG
       LDX   #TBNEG
TNEG1:  LDA   ,X+
       NEGA
       TFR   CC,B
       CMPA  ,X+
       LBNE  OUTERR
       ANDB  #0x0F   ; Only check Bits NZVC
       CMPB  ,X+
       LBNE  OUTERR
       CMPX  #LNEG
       LBNE  TNEG1
       RTS

TBNEG:  .FCB   0x00,0x00,0x04
       .FCB   0x80,0x80,0x0B
       .FCB   0x01,0xFF,0x09
       .FCB   0xFF,0x01,0x01
       .FCB   0x7F,0x81,0x09
       .FCB   0x81,0x7F,0x01
       .FCB   0xC0,0x40,0x01
       .FCB   0x40,0xC0,0x09
LNEG:   .FCC   'NEG'
       .FCB   EOT

;**************************************************
; Test COM
;**************************************************
TCOM:   LDU   #LCOM
       LDX   #TBCOM
TCOM1:  LDA   ,X+
       COMA
       TFR   CC,B
       CMPA  ,X+
       LBNE  OUTERR
       ANDB  #0x0F   ; Only check Bits NZVC
       CMPB  ,X+
       LBNE  OUTERR
       CMPX  #LCOM
       BNE   TCOM1
       RTS


TBCOM:  .FCB   0x00,0xFF,0x09
       .FCB   0xFF,0x00,0x05
       .FCB   0xF0,0x0F,0x01
       .FCB   0x0F,0xF0,0x09
       .FCB   0x55,0xAA,0x09
       .FCB   0xAA,0x55,0x01
       .FCB   0x01,0xFE,0x09
       .FCB   0xFE,0x01,0x01
       .FCB   0x80,0x7F,0x01
       .FCB   0x7F,0x80,0x09
LCOM:   .FCC   'COM'
       .FCB   EOT

;**************************************************
; Test LSR
;**************************************************
TLSR:   LDU   #LLSR
       LDX   #TBLSR
TLSR1:  LDA   ,X+
       LSRA
       TFR   CC,B
       CMPA  ,X+
       LBNE  OUTERR
       ANDB  #0x0F   ; Only check Bits NZVC
       CMPB  ,X+
       LBNE  OUTERR
       CMPX  #LLSR
       BNE   TLSR1
       RTS


TBLSR:  .FCB   0x00,0x00,0x04
       .FCB   0xFF,0x7F,0x01
       .FCB   0x7F,0x3F,0x01
       .FCB   0x3F,0x1F,0x01
       .FCB   0x1F,0x0F,0x01
       .FCB   0x0F,0x07,0x01
       .FCB   0x07,0x03,0x01
       .FCB   0x03,0x01,0x01
       .FCB   0x01,0x00,0x05
       .FCB   0x55,0x2A,0x01
       .FCB   0xAA,0x55,0x00
       .FCB   0x80,0x40,0x00
       .FCB   0x10,0x08,0x00
       .FCB   0xC0,0x60,0x00
       .FCB   0xE0,0x70,0x00
       .FCB   0xF0,0x78,0x00
       .FCB   0xF8,0x7C,0x00
       .FCB   0xFC,0x7E,0x00
       .FCB   0xFE,0x7F,0x00
LLSR:   .FCC   'LSR'
       .FCB   EOT

;**************************************************
; Test LSL
;**************************************************
TLSL:   LDU   #LLSL
       LDX   #TBLSL
TLSL1:  LDA   ,X+
       LSLA
       TFR   CC,B
       CMPA  ,X+
       LBNE  OUTERR
       ANDB  #0x0F   ; Only check Bits NZVC
       CMPB  ,X+
;       LBNE  OUTERR		;!!! was commented in orig code
       CMPX  #LLSL
       BNE   TLSL1
       RTS


TBLSL:  .FCB   0x00,0x00,0x04
       .FCB   0xFF,0xFE,0x01
       .FCB   0x7F,0xFE,0x01
       .FCB   0x3F,0x7E,0x01
       .FCB   0x1F,0x3E,0x00
       .FCB   0x0F,0x1E,0x00
       .FCB   0x07,0x0E,0x00
       .FCB   0x03,0x06,0x00
       .FCB   0x01,0x02,0x00
       .FCB   0x55,0xAA,0x00
       .FCB   0xAA,0x54,0x00
       .FCB   0x80,0x00,0x01
       .FCB   0x10,0x20,0x01
       .FCB   0xC0,0x80,0x01
       .FCB   0xE0,0xC0,0x01
       .FCB   0xF0,0xE0,0x01
       .FCB   0xF8,0xF0,0x01
       .FCB   0xFC,0xF8,0x01
       .FCB   0xFE,0xFC,0x01
LLSL:   .FCC   'LSL'
       .FCB   EOT

;**************************************************
; Test ASR
;**************************************************
TASR:   LDU   #LASR
       LDX   #TBASR
TASR1:  LDA   ,X+
       ASRA
       TFR   CC,B
       CMPA  ,X+
       LBNE  OUTERR
       ANDB  #0x0D   ; Only check Bits NZC
       CMPB  ,X+
       LBNE  OUTERR
       CMPX  #LASR
       BNE   TASR1
       RTS


TBASR:  .FCB   0x00,0x00,0x04
       .FCB   0xFF,0xFF,0x09
       .FCB   0x7F,0x3F,0x01
       .FCB   0x3F,0x1F,0x01
       .FCB   0x1F,0x0F,0x01
       .FCB   0x0F,0x07,0x01
       .FCB   0x07,0x03,0x01
       .FCB   0x03,0x01,0x01
       .FCB   0x01,0x00,0x05
       .FCB   0x55,0x2A,0x01
       .FCB   0xAA,0xD5,0x08
       .FCB   0x80,0xC0,0x08
       .FCB   0x10,0x08,0x00
       .FCB   0xC0,0xE0,0x08
       .FCB   0xE0,0xF0,0x08
       .FCB   0xF0,0xF8,0x08
       .FCB   0xF8,0xFC,0x08
       .FCB   0xFC,0xFE,0x08
       .FCB   0xFE,0xFF,0x08
LASR:   .FCC   'ASR'
       .FCB   EOT

;**************************************************
; Test ROR
;**************************************************
TROR:   LDU   #LROR
       LDX   #TBROR
TROR1:  LDA   ,X+
       TFR   CC,B
       ANDB  ,X+
       ORB   ,X+
       TFR   B,CC
       RORA
       TFR   CC,B
       CMPA  ,X+
       LBNE  OUTERR
       ANDB  #0x0D   ; Only check Bits NZC
       CMPB  ,X+
       LBNE  OUTERR
       CMPX  #LROR
       BNE   TROR1
       RTS

TBROR:  .FCB   0x00,0xFE,0x00,0x00,0x04
       .FCB   0x01,0xFE,0x00,0x00,0x05
       .FCB   0x0F,0xFE,0x00,0x07,0x01
       .FCB   0x10,0xFE,0x00,0x08,0x00
       .FCB   0x7F,0xFE,0x00,0x3F,0x01
       .FCB   0x80,0xFE,0x00,0x40,0x00
       .FCB   0xFE,0xFE,0x00,0x7F,0x00
       .FCB   0xFF,0xFE,0x00,0x7F,0x01
       .FCB   0x00,0xFF,0x01,0x80,0x08
       .FCB   0x01,0xFF,0x01,0x80,0x09
       .FCB   0x0F,0xFF,0x01,0x87,0x09
       .FCB   0x10,0xFF,0x01,0x88,0x08
       .FCB   0x7F,0xFF,0x01,0xBF,0x09
       .FCB   0x80,0xFF,0x01,0xC0,0x08
       .FCB   0xFE,0xFF,0x01,0xFF,0x08
       .FCB   0xFF,0xFF,0x01,0xFF,0x09
LROR:   .FCC   'ROR'
       .FCB   EOT

;**************************************************
; Test ROL
;**************************************************
TROL:   LDU   #LROL
       LDX   #TBROL
TROL1:  LDA   ,X+
       TFR   CC,B
       ANDB  ,X+
       ORB   ,X+
       TFR   B,CC
       ROLA
       TFR   CC,B
       CMPA  ,X+
       LBNE  OUTERR
       ANDB  #0x0D   ; Only check Bits NZC
       CMPB  ,X+
;       LBNE  OUTERR		;!!! was commented in orig code
       CMPX  #LROL
       BNE   TROL1
       RTS

TBROL:  .FCB   0x00,0xFE,0x00,0x00,0x04
       .FCB   0x01,0xFE,0x00,0x02,0x00
       .FCB   0x0F,0xFE,0x00,0x1E,0x00
       .FCB   0x08,0xFE,0x00,0x10,0x00
       .FCB   0x7F,0xFE,0x00,0xFE,0x08
       .FCB   0x80,0xFE,0x00,0x00,0x05
       .FCB   0xFE,0xFE,0x00,0xFC,0x09
       .FCB   0xFF,0xFE,0x00,0xFE,0x09
       .FCB   0x00,0xFF,0x01,0x01,0x00
       .FCB   0x01,0xFF,0x01,0x03,0x00
       .FCB   0x0F,0xFF,0x01,0x1F,0x00
       .FCB   0x08,0xFF,0x01,0x11,0x00
       .FCB   0x7F,0xFF,0x01,0xFF,0x08
       .FCB   0x80,0xFF,0x01,0x01,0x01
       .FCB   0xFE,0xFF,0x01,0xFD,0x09
       .FCB   0xFF,0xFF,0x01,0xFF,0x09
LROL:   .FCC   'ROL'
       .FCB   EOT

;**************************************************
; Test DEC
;**************************************************
TDEC:   LDU   #LDEC
       LDX   #TBDEC
TDEC1:  LDA   ,X+
       DECA
       TFR   CC,B
       CMPA  ,X+
       LBNE  OUTERR
       ANDB  #0x0E   ; Only check Bits NZV
       CMPB  ,X+
       LBNE  OUTERR
       CMPX  #LDEC
       BNE   TDEC1
       RTS

TBDEC:  .FCB   0x01,0x00,0x04
       .FCB   0x00,0xFF,0x08
       .FCB   0xFF,0xFE,0x08
       .FCB   0x80,0x7F,0x02
       .FCB   0x7F,0x7E,0x00
       .FCB   0x10,0x0F,0x00
       .FCB   0x0F,0x0E,0x00
LDEC:   .FCC   'DEC'
       .FCB   EOT

;**************************************************
; Test INC
;**************************************************
TINC:   LDU   #LINC
       LDX   #TBINC
TINC1:  LDA   ,X+
       INCA
       TFR   CC,B
       CMPA  ,X+
       LBNE  OUTERR
       ANDB  #0x0E   ; Only check Bits NZV
       CMPB  ,X+
       LBNE  OUTERR
       CMPX  #LINC
       BNE   TINC1
       RTS

TBINC:  .FCB   0x00,0x01,0x00
       .FCB   0xFE,0xFF,0x08
       .FCB   0xFF,0x00,0x04
       .FCB   0x7F,0x80,0x0A
       .FCB   0x80,0x81,0x08
       .FCB   0x0F,0x10,0x00
       .FCB   0x10,0x11,0x00
LINC:   .FCC   'INC'
       .FCB   EOT


;**************************************************
; Test CLR
;**************************************************
TCLR:   LDU   #LCLR
       LDX   #TBCLR
TCLR1:  LDA   ,X+
       CLRA
       TFR   CC,B
       CMPA  ,X+
       LBNE  OUTERR
       ANDB  #0x0F   ; Only check Bits NZVC
       CMPB  ,X+
;       LBNE  OUTERR		;!!! was commented in orig code
       CMPX  #LCLR
       BNE   TCLR1
       RTS

TBCLR:  .FCB   0x01,0x00,0x04
       .FCB   0x00,0x00,0x04
       .FCB   0xFF,0x00,0x04
       .FCB   0x80,0x00,0x04
       .FCB   0x7F,0x00,0x04
       .FCB   0x10,0x00,0x04
       .FCB   0x0F,0x00,0x04
LCLR:   .FCC   'CLR'
       .FCB   EOT


;**************************************************
; Test ADDD
;**************************************************
TADDD:  LDU   #LADDD
       LDX   #TBADDD
TADDD1: LDD   ,X++
       ADDD  ,X++
       TFR   CC,DP
       CMPD  ,X++
       LBNE  OUTERR
       TFR   DP,A
       ANDA  #0x0F   ; Only check Bits NZVC
       CMPA  ,X+
       LBNE  OUTERR
       CMPX  #LADDD
       BNE   TADDD1
       RTS

TBADDD: .FDB   0x0000,0x0000,0x0000
       .FCB   0x04
       .FDB   0x0000,0x0001,0x0001
       .FCB   0x00
       .FDB   0x4000,0x4000,0x8000
       .FCB   0x0A
       .FDB   0x0000,0x8000,0x8000
       .FCB   0x08
       .FDB   0x8000,0x8000,0x0000
       .FCB   0x07
       .FDB   0x4000,0x8000,0xC000
       .FCB   0x08
       .FDB   0x7FFF,0x7FFF,0xFFFE
       .FCB   0x0A
       .FDB   0x7FFF,0x8000,0xFFFF
       .FCB   0x08
       .FDB   0x7FFF,0x0001,0x8000
       .FCB   0x0A
       .FDB   0x8000,0x0001,0x8001
       .FCB   0x08
       .FDB   0x4000,0xC000,0x0000
       .FCB   0x05
       .FDB   0x4000,0xC001,0x0001
       .FCB   0x01
       .FDB   0xFFFF,0xFFFF,0xFFFE
       .FCB   0x09
       .FDB   0x8001,0x8000,0x0001
       .FCB   0x03
LADDD:  .FCC   'ADDD'
       .FCB   EOT


;**************************************************
; Test ADD
;**************************************************
TADD:   LDU   #LADD
       LDX   #TBADD
TADD1:  LDB   ,X+
       ADDB  ,X+
       TFR   CC,A
       CMPB  ,X+
       LBNE  OUTERR
       ANDA  #0x2F   ; Only check Bits HNZVC
       CMPA  ,X+
       LBNE  OUTERR
       CMPX  #LADD
       BNE   TADD1
       RTS

TBADD:  .FCB   0x00,0x00,0x00,0x04
       .FCB   0x00,0x01,0x01,0x00
       .FCB   0x40,0x40,0x80,0x0A
       .FCB   0x00,0x80,0x80,0x08
       .FCB   0x80,0x80,0x00,0x07
       .FCB   0x40,0x80,0xC0,0x08
       .FCB   0x7F,0x7F,0xFE,0x2A
       .FCB   0x7F,0x80,0xFF,0x08
       .FCB   0x80,0x7F,0xFF,0x08
       .FCB   0x7F,0x01,0x80,0x2A
       .FCB   0x80,0x01,0x81,0x08
       .FCB   0x40,0xC0,0x00,0x05
       .FCB   0x40,0xC1,0x01,0x01
       .FCB   0xFF,0xFF,0xFE,0x29
       .FCB   0x81,0x80,0x01,0x03
LADD:   .FCC   'ADD'
       .FCB   EOT


;**************************************************
; Test ADC
;**************************************************
TADC:   LDU   #LADC
       LDX   #TBADC
TADC1:  LDA   ,X+
       TFR   CC,B
       ANDB  ,X+
       ORB   ,X+
       TFR   B,CC
       ADCA  ,X+
       TFR   CC,B
       CMPA  ,X+
       LBNE  OUTERR
       ANDB  #0x2F   ; Only check Bits HNZVC
       CMPB  ,X+
       LBNE  OUTERR
       CMPX  #LADC
       BNE   TADC1
       RTS

TBADC:  .FCB   0x00,0xFE,0x00,0x00,0x00,0x04
       .FCB   0x00,0xFE,0x00,0x01,0x01,0x00
       .FCB   0x40,0xFE,0x00,0x40,0x80,0x0A
       .FCB   0x00,0xFE,0x00,0x80,0x80,0x08
       .FCB   0x80,0xFE,0x00,0x80,0x00,0x07
       .FCB   0x40,0xFE,0x00,0x80,0xC0,0x08
       .FCB   0x7F,0xFE,0x00,0x7F,0xFE,0x2A
       .FCB   0x7F,0xFE,0x00,0x80,0xFF,0x08
       .FCB   0x80,0xFE,0x00,0x7F,0xFF,0x08
       .FCB   0x7F,0xFE,0x00,0x01,0x80,0x2A
       .FCB   0x80,0xFE,0x00,0x01,0x81,0x08
       .FCB   0x40,0xFE,0x00,0xC0,0x00,0x05
       .FCB   0x40,0xFE,0x00,0xC1,0x01,0x01
       .FCB   0xFF,0xFE,0x00,0xFF,0xFE,0x29
       .FCB   0x81,0xFE,0x00,0x80,0x01,0x03
       .FCB   0x00,0xFF,0x01,0x00,0x01,0x00
       .FCB   0x00,0xFF,0x01,0x01,0x02,0x00
       .FCB   0x40,0xFF,0x01,0x40,0x81,0x0A
       .FCB   0x00,0xFF,0x01,0x80,0x81,0x08
       .FCB   0x80,0xFF,0x01,0x80,0x01,0x03
       .FCB   0x40,0xFF,0x01,0x80,0xC1,0x08
       .FCB   0x7F,0xFF,0x01,0x7F,0xFF,0x2A
       .FCB   0x7F,0xFF,0x01,0x80,0x00,0x25
       .FCB   0x80,0xFF,0x01,0x7F,0x00,0x25
       .FCB   0x7F,0xFF,0x01,0x01,0x81,0x2A
       .FCB   0x80,0xFF,0x01,0x01,0x82,0x08
       .FCB   0x40,0xFF,0x01,0xC0,0x01,0x01
       .FCB   0x40,0xFF,0x01,0xC1,0x02,0x01
       .FCB   0xFF,0xFF,0x01,0xFF,0xFF,0x29
       .FCB   0x81,0xFF,0x01,0x80,0x02,0x03
LADC:   .FCC   'ADC'
       .FCB   EOT


;**************************************************
; Test CMP
;**************************************************
TCMP:   LDU   #LCMP
       LDX   #TBSUB
TCMP1:  LDB   ,X+
       CMPB  ,X+
       TFR   CC,A
       LEAX  1,X    ; Skip result Byte
       ANDA  #0x0F   ; Only check Bits NZVC
       CMPA  ,X+
       LBNE  OUTERR
       CMPX  #LSUB
       BNE   TCMP1
       RTS

LCMP:   .FCC   'CMP'
       .FCB   EOT

;**************************************************
; Test SUB
;**************************************************
TSUB:   LDU   #LSUB
       LDX   #TBSUB
TSUB1:  LDB   ,X+
       SUBB  ,X+
       TFR   CC,A
       CMPB  ,X+
       LBNE  OUTERR
       ANDA  #0x0F   ; Only check Bits NZVC
       CMPA  ,X+
       LBNE  OUTERR
       CMPX  #LSUB
       BNE   TSUB1
       RTS

TBSUB:  .FCB   0x00,0x00,0x00,0x04
       .FCB   0x00,0x01,0xFF,0x09
       .FCB   0x00,0x7F,0x81,0x09
       .FCB   0x00,0x80,0x80,0x0B
       .FCB   0x00,0xFF,0x01,0x01
       .FCB   0x01,0x00,0x01,0x00
       .FCB   0x01,0x7F,0x82,0x09
       .FCB   0x40,0x41,0xFF,0x09
       .FCB   0x40,0x80,0xC0,0x0B
       .FCB   0x40,0xC0,0x80,0x0B
       .FCB   0x40,0xC1,0x7F,0x01
       .FCB   0x7F,0x00,0x7F,0x00
       .FCB   0x7F,0x01,0x7E,0x00
       .FCB   0x7F,0x7E,0x01,0x00
       .FCB   0x7F,0x7F,0x00,0x04
       .FCB   0x7F,0x80,0xFF,0x0B
       .FCB   0x7F,0xFF,0x80,0x0B
       .FCB   0x80,0x01,0x7F,0x02
       .FCB   0x80,0x40,0x40,0x02
       .FCB   0x80,0x7F,0x01,0x02
       .FCB   0x80,0x80,0x00,0x04
       .FCB   0x80,0x81,0xFF,0x09
       .FCB   0x81,0x80,0x01,0x00
       .FCB   0xFF,0xFF,0x00,0x04
       .FCB   0xFF,0xFE,0x01,0x00
       .FCB   0xFF,0x00,0xFF,0x08
LSUB:   .FCC   'SUB'
       .FCB   EOT


;**************************************************
; Test SBC
;**************************************************
TSBC:   LDU   #LSBC
       LDX   #TBSBC
TSBC1:  LDA   ,X+
       TFR   CC,B
       ANDB  ,X+
       ORB   ,X+
       TFR   B,CC
       SBCA  ,X+
       TFR   CC,B
       CMPA  ,X+
       LBNE  OUTERR
       ANDB  #0x0F   ; Only check Bits NZVC
       CMPB  ,X+
       LBNE  OUTERR
       CMPX  #LSBC
       BNE   TSBC1
       RTS

TBSBC:  .FCB   0x00,0xFE,0x00,0x00,0x00,0x04
       .FCB   0x00,0xFE,0x00,0x01,0xFF,0x09
       .FCB   0x00,0xFE,0x00,0x7F,0x81,0x09
       .FCB   0x00,0xFE,0x00,0x80,0x80,0x0B
       .FCB   0x00,0xFE,0x00,0xFF,0x01,0x01
       .FCB   0x01,0xFE,0x00,0x00,0x01,0x00
       .FCB   0x01,0xFE,0x00,0x7F,0x82,0x09
       .FCB   0x40,0xFE,0x00,0x41,0xFF,0x09
       .FCB   0x40,0xFE,0x00,0x80,0xC0,0x0B
       .FCB   0x40,0xFE,0x00,0xC0,0x80,0x0B
       .FCB   0x40,0xFE,0x00,0xC1,0x7F,0x01
       .FCB   0x7F,0xFE,0x00,0x00,0x7F,0x00
       .FCB   0x7F,0xFE,0x00,0x01,0x7E,0x00
       .FCB   0x7F,0xFE,0x00,0x7E,0x01,0x00
       .FCB   0x7F,0xFE,0x00,0x7F,0x00,0x04
       .FCB   0x7F,0xFE,0x00,0x80,0xFF,0x0B
       .FCB   0x7F,0xFE,0x00,0xFF,0x80,0x0B
       .FCB   0x80,0xFE,0x00,0x01,0x7F,0x02
       .FCB   0x80,0xFE,0x00,0x40,0x40,0x02
       .FCB   0x80,0xFE,0x00,0x7F,0x01,0x02
       .FCB   0x80,0xFE,0x00,0x80,0x00,0x04
       .FCB   0x80,0xFE,0x00,0x81,0xFF,0x09
       .FCB   0x81,0xFE,0x00,0x80,0x01,0x00
       .FCB   0xFF,0xFE,0x00,0xFF,0x00,0x04
       .FCB   0xFF,0xFE,0x00,0xFE,0x01,0x00
       .FCB   0xFF,0xFE,0x00,0x00,0xFF,0x08
       .FCB   0x00,0xFF,0x01,0x00,0xFF,0x09
       .FCB   0x00,0xFF,0x01,0x01,0xFE,0x09
       .FCB   0x00,0xFF,0x01,0x7F,0x80,0x09
       .FCB   0x00,0xFF,0x01,0x80,0x7F,0x01
       .FCB   0x00,0xFF,0x01,0xFF,0x00,0x05
       .FCB   0x01,0xFF,0x01,0x00,0x00,0x04
       .FCB   0x01,0xFF,0x01,0x7F,0x81,0x09
       .FCB   0x40,0xFF,0x01,0x41,0xFE,0x09
       .FCB   0x40,0xFF,0x01,0x80,0xBF,0x0B
       .FCB   0x40,0xFF,0x01,0xC0,0x7F,0x01
       .FCB   0x40,0xFF,0x01,0xC1,0x7E,0x01
       .FCB   0x7F,0xFF,0x01,0x00,0x7E,0x00
       .FCB   0x7F,0xFF,0x01,0x01,0x7D,0x00
       .FCB   0x7F,0xFF,0x01,0x7E,0x00,0x04
       .FCB   0x7F,0xFF,0x01,0x7F,0xFF,0x09
       .FCB   0x7F,0xFF,0x01,0x80,0xFE,0x0B
       .FCB   0x7F,0xFF,0x01,0xFF,0x7F,0x01
       .FCB   0x80,0xFF,0x01,0x01,0x7E,0x02
       .FCB   0x80,0xFF,0x01,0x40,0x3F,0x02
       .FCB   0x80,0xFF,0x01,0x7F,0x00,0x06
       .FCB   0x80,0xFF,0x01,0x80,0xFF,0x09
       .FCB   0x80,0xFF,0x01,0x81,0xFE,0x09
       .FCB   0x81,0xFF,0x01,0x80,0x00,0x04
       .FCB   0xFF,0xFF,0x01,0xFF,0xFF,0x09
       .FCB   0xFF,0xFF,0x01,0xFE,0x00,0x04
       .FCB   0xFF,0xFF,0x01,0x00,0xFE,0x08
LSBC:   .FCC   'SBC'
       .FCB   EOT


;**************************************************
; Test SUBD
;**************************************************
TSUBD:  LDU   #LSUBD
       LDX   #TBSUBD
TSUBD1: LDD   ,X++
       SUBD  ,X++
       TFR   CC,DP
       CMPD  ,X++
       LBNE  OUTERR
       TFR   DP,A
       ANDA  #0x0F   ; Only check Bits NZVC
       CMPA  ,X+
       LBNE  OUTERR
       CMPX  #LSUBD
       BNE   TSUBD1
       RTS

TBSUBD: .FDB   0x0000,0x0000,0x0000
       .FCB   0x04
       .FDB   0x0000,0x0001,0xFFFF
       .FCB   0x09
       .FDB   0x0000,0x7FFF,0x8001
       .FCB   0x09
       .FDB   0x0000,0x8000,0x8000
       .FCB   0x0B
       .FDB   0x0000,0xFFFF,0x0001
       .FCB   0x01
       .FDB   0x0001,0x0000,0x0001
       .FCB   0x00
       .FDB   0x0001,0x7FFF,0x8002
       .FCB   0x09
       .FDB   0x4000,0x4001,0xFFFF
       .FCB   0x09
       .FDB   0x4000,0x8000,0xC000
       .FCB   0x0B
       .FDB   0x4000,0xC000,0x8000
       .FCB   0x0B
       .FDB   0x4000,0xC001,0x7FFF
       .FCB   0x01
       .FDB   0x7FFF,0x0000,0x7FFF
       .FCB   0x00
       .FDB   0x7FFF,0x0001,0x7FFE
       .FCB   0x00
       .FDB   0x7FFF,0x7FFE,0x0001
       .FCB   0x00
       .FDB   0x7FFF,0x7FFF,0x0000
       .FCB   0x04
       .FDB   0x7FFF,0x8000,0xFFFF
       .FCB   0x0B
       .FDB   0x7FFF,0xFFFF,0x8000
       .FCB   0x0B
       .FDB   0x8000,0x0001,0x7FFF
       .FCB   0x02
       .FDB   0x8000,0x4000,0x4000
       .FCB   0x02
       .FDB   0x8000,0x7FFF,0x0001
       .FCB   0x02
       .FDB   0x8000,0x8000,0x0000
       .FCB   0x04
       .FDB   0x8000,0x8001,0xFFFF
       .FCB   0x09
       .FDB   0x8001,0x8000,0x0001
       .FCB   0x00
       .FDB   0xFFFF,0xFFFF,0x0000
       .FCB   0x04
       .FDB   0xFFFF,0xFFFE,0x0001
       .FCB   0x00
       .FDB   0xFFFF,0x0000,0xFFFF
       .FCB   0x08
LSUBD:  .FCC   'SUBD'
       .FCB   EOT


;**************************************************
; Test CMPD
;**************************************************
TCMPD:  LDU   #LCMPD
       LDX   #TBSUBD
TCMPD1: LDD   ,X++
       SUBD  ,X++
       TFR   CC,DP
       LEAX  2,X    ; Skip result Word
       TFR   DP,A
       ANDA  #0x0F   ; Only check Bits NZVC
       CMPA  ,X+
       LBNE  OUTERR
       CMPX  #LSUBD
       BNE   TCMPD1
       RTS

LCMPD:  .FCC   'CMPD'
       .FCB   EOT


;**************************************************
; Test TFR
;**************************************************
TTFR:   LDX   #TBTFR
TTFR1:  PSHS  X
       LDY   ,X
       JSR   ,Y
       PULS  X
       BNE   FTFR
       LEAX  2,X
       CMPX  #LTFR
       BNE   TTFR1
       RTS
FTFR:   LDU   #LTFR
       LBRA  OUTERR

CLRREG: CLRA
       CLRB
       LDX   #0
       LDY   #0
       LDU   #0
       TFR   A,DP
       TFR   A,CC
       RTS
TBTFR1: BSR   CLRREG
       LDD   #0x5678
       TFR   D,X
       CMPX  #0x5678  
       RTS
TBTFR2: BSR   CLRREG
       LDX   #0x1234
       TFR   X,Y
       CMPY  #0x1234  
       RTS
TBTFR3: BSR   CLRREG
       LDY   #0x5500
       TFR   Y,U
       CMPU  #0x5500  
       RTS
TBTFR4: BSR   CLRREG
       LDU   #0xFEFF
       TFR   U,D
       CMPD  #0xFEFF  
       RTS
TBTFR5: BSR   CLRREG
       LDA   #0x55
       TFR   A,B
       CMPB  #0x55  
       RTS
TBTFR6: BSR   CLRREG
       LDB   #0xAA
       TFR   B,DP
       PSHS  DP
       PULS  A
       CMPA  #0xAA
       RTS
TBTFR7: BSR   CLRREG
       LDA   #0x03
       PSHS  A
       CLRA
       PULS  DP
       TFR   DP,CC
       PSHS  CC
       PULS  A
       CMPA  #0x03
       RTS
TBTFR8: BSR   CLRREG
       LDA   #0x01
       PSHS  A
       PULS  CC
       TFR   CC,A
       CMPA  #0x01
       RTS

TBTFR:  .FDB   TBTFR1
       .FDB   TBTFR2
       .FDB   TBTFR3
       .FDB   TBTFR4
       .FDB   TBTFR5
       .FDB   TBTFR6
       .FDB   TBTFR7
       .FDB   TBTFR8
LTFR:   .FCC   'TFR'
       .FCB   EOT


;**************************************************
; Test EXG
;**************************************************
TEXG:   LDX   #TBEXG
TEXG1:  PSHS  X
       LDY   ,X
       JSR   ,Y
       PULS  X
       BNE   FEXG
       LEAX  2,X
       CMPX  #LEXG
       BNE   TEXG1
       RTS
FEXG:   LDU   #LEXG
       LBRA  OUTERR

TBEXG1: LBSR  CLRREG
       LDD   #0x5678
       LDX   #0xA55A
       EXG   D,X
       CMPX  #0x5678
       BNE   TBEXGE
       CMPD  #0xA55A  
TBEXGE: RTS
TBEXG2: LBSR  CLRREG
       LDX   #0x1234
       LDY   #0x0815
       EXG   X,Y
       CMPY  #0x1234
       BNE   TBEXGE
       CMPX  #0x0815
       RTS
TBEXG3: LBSR  CLRREG
       LDY   #0x5500
       LDU   #0x4711
       EXG   Y,U
       CMPU  #0x5500 
       BNE   TBEXGE 
       CMPY  #0x4711  
       RTS
TBEXG4: LBSR  CLRREG
       LDU   #0xFEFF
       LDD   #0x9999
       EXG   U,D
       CMPD  #0xFEFF
       BNE   TBEXGE
       CMPU  #0x9999  
       RTS
TBEXG5: LBSR  CLRREG
       LDA   #0x55
       LDB   #0xAA
       EXG   A,B
       CMPB  #0x55  
       BNE   TBEXGE
       CMPA  #0xAA
       RTS
TBEXG6: LBSR  CLRREG
       LDB   #0xAA
       LDA   #0x44
       PSHS  A
       CLRA
       PULS  DP
       EXG   B,DP
       PSHS  DP
       PULS  A
       CMPA  #0xAA
       BNE   TBEXGE
       CMPB  #0x44
       RTS
TBEXG7: LBSR  CLRREG
       LDA   #0x03
       PSHS  A
       CLRA
       PULS  DP
       LDA   #0x30
       PSHS  A
       CLRA
       PULS  CC
       EXG   DP,CC
       PSHS  CC
       PULS  A
       PSHS  DP
       PULS  B
       CMPA  #0x03
       LBNE  TBEXGE
       CMPB  #0x30
       RTS
TBEXG8: LBSR  CLRREG
       LDA   #0x11
       LDB   #0x99
       PSHS  B
       CLRB
       PULS  CC
       EXG   CC,A
       PSHS  CC
       PULS  B
       CMPA  #0x99
       LBNE  TBEXGE
       CMPB  #0x11
       RTS

TBEXG:  .FDB   TBEXG1
       .FDB   TBEXG2
       .FDB   TBEXG3
       .FDB   TBEXG4
       .FDB   TBEXG5
       .FDB   TBEXG6
       .FDB   TBEXG7
       .FDB   TBEXG8
LEXG:   .FCC   'EXG'

;**************************************************
; Test Index Addressing modes
;**************************************************
CLRMEM: PSHS  X
       LDX   #EXTREG-128
       CLRA
CLRM1:  CLR   ,X+
       INCA
       BNE   CLRM1
       PULS  X,PC

TAD1:   LDU   #LAD1
       LDX   #EXTREG
; LDA ,X
       BSR   CLRMEM
       LDA   #0x55
       STA   EXTREG
       LDA   ,X
       CMPA  #0x55
       LBNE  OUTERR
; LDA 1,X
       BSR   CLRMEM
       LDA   #0x12
       STA   EXTREG+1
       LDA   1,X
       CMPA  #0x12
       LBNE  OUTERR
; LDA 127,X
       BSR   CLRMEM
       LDA   #0x44
       STA   EXTREG+127
       LDA   127,X
       CMPA  #0x44
       LBNE  OUTERR
; LDA -1,X
       BSR   CLRMEM
       LDA   #0x34
       STA   EXTREG-1
       LDA   -1,X
       CMPA  #0x34
       LBNE  OUTERR
; LDA -128,X
       BSR   CLRMEM
       LDA   #0xAA
       STA   EXTREG-128
       LDA   -128,X
       CMPA  #0xAA
       LBNE  OUTERR
       RTS

; LDA ,X+
TAD2:   LDX   #EXTREG
       LDU   #LAD2
       BSR   CLRMEM
       LDD   #0xA55A
       STD   EXTREG
       LDA   ,X+
       CMPA  #0xA5
       LBNE  OUTERR
       LDA   ,X+
       CMPA  #0x5A
       LBNE  OUTERR
       RTS

; LDD ,X++
TAD3:   LDX   #EXTREG
       LDU   #LAD3
       LBSR  CLRMEM
       LDD   #0xA55A
       STD   EXTREG
       LDD   #0x789A
       STD   EXTREG+2
       LDD   ,X++
       CMPD  #0xA55A
       LBNE  OUTERR
       LDD   ,X++
       CMPD  #0x789A
       LBNE  OUTERR
       RTS

; LDA ,-X
TAD4:   LDX   #EXTREG
       LDU   #LAD4
       LBSR  CLRMEM
       LDD   #0xA55A
       STD   EXTREG-2
       LDA   ,-X
       CMPA  #0x5A
       LBNE  OUTERR
       LDA   ,-X
       CMPA  #0xA5
       LBNE  OUTERR
       RTS

; LDD ,--X
TAD5:   LDX   #EXTREG
       LDU   #LAD5
       LBSR  CLRMEM
       LDD   #0xA55A
       STD   EXTREG-2
       LDD   #0x789A
       STD   EXTREG-4
       LDD   ,--X
       CMPD  #0xA55A
       LBNE  OUTERR
       LDD   ,--X
       CMPD  #0x789A
       LBNE  OUTERR
       RTS

; LDA A,X
TAD6:   LDU   #LAD6
       LDX   #EXTREG
       LBSR  CLRMEM
       LDA   #0x12
       STA   EXTREG+1
       LDA   #1
       LDA   A,X
       CMPA  #0x12
       LBNE  OUTERR
       LBSR  CLRMEM
       LDA   #0x44
       STA   EXTREG+127
       LDA   #127
       LDA   A,X
       CMPA  #0x44
       LBNE  OUTERR
       RTS

; LDA B,Y
TAD7:   LDU   #LAD7
       LDY   #EXTREG
       LBSR  CLRMEM
       LDA   #0x34
       STA   EXTREG-1
       LDB   #-1
       LDA   B,Y
       CMPA  #0x34
       LBNE  OUTERR
       LBSR  CLRMEM
       LDA   #0xAA
       STA   EXTREG-128
       LDB   #-128
       LDA   B,Y
       CMPA  #0xAA
       LBNE  OUTERR
       RTS

; LDD D,X
TAD8:   LDU   #LAD8
       LDX   #0
       LBSR  CLRMEM
       LDD   #0x1289
       STD   EXTREG
       LDD   #EXTREG
       LDD   D,X
       CMPD  #0x1289
       LBNE  OUTERR
       LDX   #EXTREG
       LBSR  CLRMEM
       LDD   #0x2299
       STD   EXTREG
       LDD   #EXTREG-#EXTREG
       LDD   D,X
       CMPD  #0x2299
       LBNE  OUTERR
       RTS

; LDD n16,X
TAD9:   LDU   #LAD9
       LDX   #0
       LBSR  CLRMEM
       LDD   #0x1289
       STD   EXTREG
       LDD   EXTREG,X
       CMPD  #0x1289
       LBNE  OUTERR
       LDX   #EXTREG
       LBSR  CLRMEM
       LDD   #0x2299
       STD   EXTREG
       LDD   EXTREG-#EXTREG,X
       CMPD  #0x2299
       LBNE  OUTERR
       RTS

; LDA n8,PC
REG81:  .FCB   0x13
TADA:   LDU   #LADA
       LBSR  CLRMEM
;       LDA   #0x13
;       STA   REG81			;??? REG81 in ROM
       LDA   <REG81,PCR
       CMPA  #0x13
       LBNE  OUTERR
       LBSR  CLRMEM
;       LDA   #0x23
;       STA   REG82			;??? REG82 in ROM
       LDA   <REG82,PCR
       CMPA  #0x23
       LBNE  OUTERR
       RTS
REG82:  .FCB   0x23

REG161: .FDB   0x9876

; LDA n16,PC
TADB:   LDU   #LADB
       LBSR  CLRMEM
;       LDD   #0x9876
;       STD   REG161			;??? REG161 in ROM
       LDD   >REG161,PCR
       CMPD  #0x9876
       LBNE  OUTERR
       LBSR  CLRMEM
;       LDD   #0x5432
;       STD   REG162			;??? REG162 in ROM
       LDD   >REG162,PCR
       CMPD  #0x5432
       LBNE  OUTERR
       RTS

TADI1:  LDU   #LADI1
       LDX   #EXTREG
; LEAY [,X]
       LBSR  CLRMEM
       LDD   #0x5522
       STD   EXTREG
       LEAY  [,X]
       CMPY  #0x5522
       LBNE  OUTERR
; LEAY [1,X]
       LBSR  CLRMEM
       LDD   #0x1234
       STD   EXTREG+1
       LEAY  [1,X]
       CMPY  #0x1234
       LBNE  OUTERR
; LEAY [127,X]
       LBSR  CLRMEM
       LDD   #0x4444
       STD   EXTREG+127
       LEAY  [127,X]
       CMPY  #0x4444
       LBNE  OUTERR
; LEAY [-1,X]
       LBSR  CLRMEM
       LDD   #0x3456
       STD   EXTREG-1
       LEAY  [-1,X]
       CMPY  #0x3456
       LBNE  OUTERR
; LEAY [-128,X]
       LBSR  CLRMEM
       LDD   #0xAAAA
       STD   EXTREG-128
       LEAY  [-128,X]
       CMPY  #0xAAAA
       LBNE  OUTERR
       RTS

; LEAY [,X++]
TADI3:  LDX   #EXTREG
       LDU   #LADI3
       LBSR  CLRMEM
       LDD   #0xA55A
       STD   EXTREG
       LDD   #0x5AA5
       STD   EXTREG+2
       LEAY  [,X++]
       CMPY  #0xA55A
       LBNE  OUTERR
       LEAY  [,X++]
       CMPY  #0x5AA5
       LBNE  OUTERR
       RTS

; LEAY [,--X]
TADI5:  LDX   #EXTREG
       LDU   #LADI5
       LBSR  CLRMEM
       LDD   #0xA55A
       STD   EXTREG-2
       LDD   #0x789A
       STD   EXTREG-4
       LEAY  [,--X]
       CMPY  #0xA55A
       LBNE  OUTERR
       LEAY  [,--X]
       CMPY  #0x789A
       LBNE  OUTERR
       RTS

; LEAY [A,X]
TADI6:  LDU   #LADI6
       LDX   #EXTREG
       LBSR  CLRMEM
       LDD   #0x1234
       STD   EXTREG+1
       LDA   #1
       LEAY  [A,X]
       CMPY  #0x1234
       LBNE  OUTERR
       LBSR  CLRMEM
       LDD   #0x4444
       STD   EXTREG+127
       LDA   #127
       LEAY  [A,X]
       CMPY  #0x4444
       LBNE  OUTERR
       RTS

; LEAY [B,X]
TADI7:  LDU   #LADI7
       LDX   #EXTREG
       LBSR  CLRMEM
       LDD   #0x3456
       STD   EXTREG-1
       LDB   #-1
       LEAY  [B,X]
       CMPY  #0x3456
       LBNE  OUTERR
       LBSR  CLRMEM
       LDD   #0xAAAA
       STD   EXTREG-128
       LDB   #-128
       LEAY  [B,X]
       CMPY  #0xAAAA
       LBNE  OUTERR
       RTS

; LEAY [D,X]
TADI8:  LDU   #LADI8
       LDX   #0
       LBSR  CLRMEM
       LDD   #0x1289
       STD   EXTREG
       LDD   #EXTREG
       LEAY  [D,X]
       CMPY  #0x1289
       LBNE  OUTERR
       LDX   #EXTREG
       LBSR  CLRMEM
       LDD   #0x2299
       STD   EXTREG
       LDD   #EXTREG-#EXTREG
       LEAY  [D,X]
       CMPY  #0x2299
       LBNE  OUTERR
       RTS

; LEAY [n16,X]
TADI9:  LDU   #LADI9
       LDX   #0
       LBSR  CLRMEM
       LDD   #0x1289
       STD   EXTREG
       LEAY  [EXTREG,X]
       CMPY  #0x1289
       LBNE  OUTERR
       LDX   #EXTREG
       LBSR  CLRMEM
       LDD   #0x2299
       STD   EXTREG
       LEAY  [EXTREG-EXTREG,X]
       CMPY  #0x2299
       LBNE  OUTERR
       RTS

; LEAY [n8,PCR]
REGI81: .FDB   0x1357
TADIA:  LDU   #LADIA
       LBSR  CLRMEM
;       LDD   #0x1357
;       STD   REGI81		;??? REGI81 in ROM
       LEAY  [REGI81,PCR]
       CMPY  #0x1357
       LBNE  OUTERR
       LBSR  CLRMEM
;       LDD   #0x2345
;       STD   REGI82
       LEAY  [REGI82,PCR]	;??? REGI82 in ROM
       CMPY  #0x2345
       LBNE  OUTERR
       RTS
REGI82: .FDB   0x2345

; LEAY [n16,PC]
TADIB:  LDU   #LADIB
       LBSR  CLRMEM
;       LDD   #0x9876
;       STD   REG161			;??? REG161 in ROM
       LEAY  [REG161,PCR]
       CMPY  #0x9876
       LBNE  OUTERR
       LBSR  CLRMEM
;       LDD   #0x5432
;       STD   REG162			; ??? REG162 in ROM
       LEAY  [REG162,PCR]
       CMPY  #0x5432
       LBNE  OUTERR
       RTS

; LEAY [addr]
TADIC:  LDU   #LADIC
       LBSR  CLRMEM
;       LDD   #0x9876
;       STD   REG161			;??? REG161 in ROM
       LEAY  [#REG161]
       CMPY  #0x9876
       LBNE  OUTERR
       LBSR  CLRMEM
;       LDD   #0x5432
;       STD   REG162			;??? REG162 in ROM
       LEAY  [#REG162]
       CMPY  #0x5432
       LBNE  OUTERR
       RTS

REG162: .FDB   0x5432

LAD1:   .FCC   'LDA N8,X'
        .byte EOT
LAD2:   .FCC   'LDA ,X+'
        .byte EOT
LAD3:   .FCC   'LDD ,X++'
        .byte EOT
LAD4:   .FCC   'LDA ,-X'
        .byte EOT
LAD5:   .FCC   'LDD ,--X'
        .byte EOT
LAD6:   .FCC   'LDA A,X'
        .byte EOT
LAD7:   .FCC   'LDA B,Y'
        .byte EOT
LAD8:   .FCC   'LDD D,X'
        .byte EOT
LAD9:   .FCC   'LDD N16,X'
        .byte EOT
LADA:   .FCC   'LDA N8,PC'
        .byte EOT
LADB:   .FCC   'LDD N16,PC'
        .byte EOT
LADI1:  .FCC   'LEAY [n8,X]'
        .byte EOT
LADI3:  .FCC   'LEAY [,X++]'
        .byte EOT
LADI5:  .FCC   'LEAY [,--X]'
        .byte EOT
LADI6:  .FCC   'LEAY [A,X]'
        .byte EOT
LADI7:  .FCC   'LEAY [B,Y]'
        .byte EOT
LADI8:  .FCC   'LEAY [D,X]'
        .byte EOT
LADI9:  .FCC   'LEAY [N16,X]'
        .byte EOT
LADIA:  .FCC   'LEAY [N8,PCR]'
        .byte EOT
LADIB:  .FCC   'LEAY [N16,PCR]'
        .byte EOT
LADIC:  .FCC   'LEAY [ADDR]'
        .byte EOT


;**************************************************
; Print error message for a failed Test
; Parameters:
; U: Pointer to Mnemonic
;**************************************************
OUTERR:
		ldx	#0x3c88
		stx	curpos
		LDX   #ERRM1
		JSR   PSTRNG

		ldx	#0x3c99
		stx	curpos
		TFR   U,X
		JSR   PSTRNG

		ldb		#0x01
		stb		failed_6809

		ldb		#0x02		;did not pass the 6809 test suite
		jsr		set_LED

		;set color to red and loop
		jsr		load_palette_redbkg
	
;1$:
;		ldb		#watchdog_data
;		stb		watchdog
;		bra		1$

		ldb		#watchdog_data
		jsr		delay_ffff
		jsr		delay_ffff
		jsr		delay_ffff
		jsr		clear_screen
		rts

	
ERRM1:  .FCC   'FAILED TEST: '
        .byte	EOT
PASSM1: .FCC   'PASSED 6809 TEST SUITE'
        .byte	EOT
TITLEM1:.FCC   'WILLIAMS EMULATION TEST SUITE V0.3'
        .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
