
	
	.include "\SrcALL\V1_Header.asm"		;Cartridge/Program header - platform specific
	.include "\SrcAll\BasicMacros.asm"		;Basic macros for ASM tasks
MyProg:
	SEI						;Stop interrupts
	clc
	xce	;16 bit mode (65816 Native)
	
	jsr ScreenInit			;Init the graphics screen
	jsr Cls

	Mode16bit

	.ifndef BuildFNX
		lda #$1FF			;Alternate stack pointer
		tas
	.endif
	
	;jmp NewRegPushes
	;jmp PushesExchangeTransfers
	;jmp TransferSpecial
	
	;jmp PushEffective
	
	;jmp TSBTRB
	;jmp Branches
	jmp LongCallsJumps
	;jmp StrangeCommands
	
	

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	

NewRegPushes:	

	lda #$1234
	pha
	plb					;Change the data Bank to $34
	
	lda #$5678
	tcd					;Change the Direct Page to $5678
			
	phd					;Push the Direct Page Register (16 bit)
		phb					;Push the Databank Register (8 bit)
			phk					;Push the Program Bank Register (8 bit)
			
				jsr ShowStack	;Test Stack Dump
			
				phk
			pla					;No way to Pull the Data bank register
		plb					;Pull the databank register (8 bit)
	pld					;Pull the Direct Page Register (16 bit)
	
	jsr Monitor
	
	jmp infloop
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	

PushesExchangeTransfers:	
	
	lda #$BBAA
	ldx #$1111
	ldy #$2222
	
	phx					;Push X onto the stack
		phy				;Push Y onto the stack
			jsr Monitor
			pha
				jsr NewLine
				jsr ShowStack	;Test Stack Dump
				jsr NewLine
			pla
			txy				;Transfer X to Y
			xba				;Swap B and A
			jsr Monitor
			
		plx				;Pull X off the stack
	ply					;Pull Y off the stack
	xba					;Swap B and A
	jsr Monitor	
	
	tyx					;Transfer Y to X
	jsr Monitor
	
		
	jsr NewLine
	jsr NewLine
	
	
	SEP  #%00110000		;8 bit XY+Accumulator 
	.ACCU 8				;Accumulator 8 bit
	.INDEX 8			;XY 8 bit
	
	;The pushes of X,Y will now be 8 bit
	
	ldx #$11
	ldy #$22
	
	phx						;Push X onto the stack
		phy					;Push Y onto the stack
			jsr Monitor
			pha
				jsr NewLine
				jsr ShowStack	;Test Stack Dump
				jsr NewLine
			pla
			txy				;Transfer X to Y
			
			jsr Monitor
		plx					;Pull X off the stack
	ply						;Pull Y off the stack
	jsr Monitor
		
		
		
	jmp infloop
	Mode16bit
	
ShowStack:
	jsr MemDump			;Dump an address to screen
	.ifdef BuildFNX
		.dl $00D6F0     ;Address to show (24 bit)
	.else
		.dl $0001F8     ;Address to show (24 bit)
	.endif
	.db $1	         	;Lines to show	
	rts
ShowStack2:
	jsr MemDump			;Dump an address to screen
	.ifdef BuildFNX
		.dl $00D6E8     ;Address to show (24 bit)
	.else
		.dl $0001F0     ;Address to show (24 bit)
	.endif
	.db $2	         	;Lines to show	
	rts


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	

TransferSpecial:
	jsr ShowStack	;Test Stack Dump
	jsr NewLine
	jsr Monitor
	
;D is always 16 bit, even in 8 bit CPU mode	
	
	lda #$FFFF
	pha
		phd 				;Push D (Direct page register)
			lda #$0200		;Lets set Direct Page to $000200	
			tcd				;Transfer 16 bit Accumulator -> Direct page D
			jsr Monitor
	
			lda #$FFFF
			pha
				jsr ShowStack	;Test Stack Dump
			pla
		pld					;Pull D (Direct page register)
		tdc					;Transsfer 16 bit DirectPage D -> Accumulator
		
		jsr Monitor
		
		jsr NewLine
		jsr NewLine
		
		jsr Monitor

	;S is always 16 bit, even in 8 bit CPU mode	
		
		lda #$0500			;Lets set the stack to $500
		tcs					;Transfer 16 bit Accumulator -> Stack S
			jsr Monitor
			lda #$6666		;Test item
			pha
				jsr Monitor
		tsc					;Stack S -> Transfer 16 bit Accumulator
		jsr Monitor

	
	jmp infloop	
		
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
	
PushEffective:		;These pushes are always 16 bit

	jsr ShowStack2
	
	lda #$FFFF			;Marker
	pha
	
	pea $7777			;Push Absolute 
	
	lda #IndirectA
	sta $10				;Store address in zeropage
	
	pei ($10)			;Push Indirect
	
	jsr NewLine
	
	per $1000			;Push PC+$1000
	per PushEffective	;Push relative label (relocatable)
	jsr Monitor
	
	lda #$FFFF			;Marker
	pha
	jsr NewLine	
	jsr ShowStack2
	
	jmp infloop	
	
IndirectA:				;Test label for indireection
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
	
TSBTRB:	
	lda #$1234				;NZ result
	;lda #$0000				;Z result
	sta $10
	
	jsr MemDump				;Dump an address to screen
		.dl $00010     		;Address to show (24 bit)
		.db $1	         	;Lines to show
	jsr NewLine	
		
	lda #$F000
	tsb $10					;Set flags according to AND
							;And  set bit 1s to one in dest
	jsr ShowZeroFlag
	
	jsr MemDump				;Dump an address to screen
		.dl $00010     		;Address to show (24 bit)
		.db $1	         	;Lines to show
	jsr NewLine	
		
	lda #$0FF0
	trb $10					;Set flags according to AND
							;And  set bit 1s to zero in dest
	jsr ShowZeroFlag
	
	jsr MemDump				;Dump an address to screen
		.dl $00010     		;Address to show (24 bit)
		.db $1	         	;Lines to show
	jsr NewLine	
		
	jmp infloop	
	
ShowZeroFlag:
	php
	pha
		Mode8bit
		beq ShowZeroFlagZ
		lda #'N'
		jsr PrintChar
ShowZeroFlagZ:
		lda #'Z'
		jsr PrintChar
		lda #' '
		jsr PrintChar
		
		jsr NewLine
	
		Mode16bit
	pla
	plp
	jsr Monitor
	rts
	
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	

Branches:
	lda #$1234
	bra TestBra		;Branch Always to 8 bit offset (-128 to +127)
		lda #$7777		;This never happens
TestBra:
	jsr Monitor

	brl TestBrl		;Branch always to 16 bit offset (-32768 to +32767) 
	;bra TestBrl		;This won't work
		.ds 256,0		;256 zero bytes - BRA cannot jump over this 
		lda #$FEDC		;This never happens
TestBrl:	
	jsr Monitor
	
	jmp infloop
	
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
			
LongCallsJumps:
	lda #7				;Copy 8 Bytes (7+1)
	ldx #LongJumpTest	;Source 
	ldy #$0100			;Destination
	mvn $00,$03			;Copy to $030100 (MoVe Next)

	lda #7				;Copy 8 Bytes (7+1)
	ldx #LongCallTest	;Source 
	ldy #$0200			;Destination
	mvn $00,$03			;Copy to $030200 (MoVe Next)

	jsr Monitor			;Show the result
	
	jml $030100			;Jump to 24 bit address
	
LongJumpReturn:
	jsr Monitor			;Show the result
	
	jsl $030200			;Call subroutine at 24 bit address
	
	jsr Monitor			;Show the result
	
	jmp infloop

LongJumpTest:
	lda #$FFCC
	jml LongJumpReturn	;Long call

LongCallTest:
	lda #$FFDD
	rtl					;Long return

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
	
StrangeCommands:	

	STP				;Stop CPU
	
	WDM 			;Reserved for future use
	
	COP				;Coprocessor enable
	
	WAI				;Wait for interrupt
	
	jsr Monitor
	
	jmp infloop
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
	
infloop:
	jmp infloop

	
	
	.include "\SrcAll\Monitor.asm"			;Debugging tools
	.include "\SrcAll\BasicFunctions.asm"	;Basic commands for ASM tasks
	
BitmapFont:									;Chibiakumas bitmap font
	.incbin "\ResALL\Font96.FNT"		
	

	.include "\SrcALL\V1_Functions.asm"	;Basic text to screen functions
	.include "\SrcAll\V1_BitmapMemory.asm"	;Bitmap functions for Bitmap screen systems
	.include "\SrcAll\V1_VdpMemory.asm"		;VRAM functions for Tilemap Systems
	.include "\SrcALL\V1_Palette.asm"		;Palette functions
	.include "\SrcALL\V1_Footer.asm"		;Footer for systems that need it
	
	