

	;Unrem this if building with vasm
	include "\SrcALL\VasmBuildCompat.asm"

;For Cartridge	
	org &4000				;Base Cart Address
	db "AB"					;Fixed Header
	dw ProgramStart 		;Pointer to start of program
	db 00,00,00,00,00,00	;Unused

	
	;;;Effectively Code starts at address &400A

VdpIn_Status equ &99

VdpOut_Control equ &99
VdpOut_Indirect equ &9B

Vdp_SendByteData equ &9B	

;Common	
ProgramStart:			;Program Code Starts Here
	
	
	ld c,VdpOut_Control
	ld b,VDPScreenInitData_End-VDPScreenInitData
	ld hl,VDPScreenInitData
	otir
		
	; ld hl,48
	; ld (Tile_MyHMMC_NX),hl
	; ld (Tile_MyHMMC_NY),hl
	
	; ld (Tile_MyHMMC_DX),hl
	; ld (Tile_MyHMMC_DY),hl
	
	ld hl,sprite
	ld a,(hl)		;First byte of sprite data
	inc hl
	push hl
		ld ix,50	;Xpos
		ld iy,50	;Ypos
		ld hl,48	;Width
		ld de,48	;Height
		call VDP_HMMC_Generated_FromStack
	pop hl
	
	ld bc,SpriteEnd-Sprite

NextByte:
	ld a,(hl)
	out (Vdp_SendByteData),a
	dec bc
	inc hl
	ld a,b
	or c
	jr nz,NextByte
	
	
	ld ix,50	;XSource
	ld iy,50	;YSource
	ld hl,48	;Width
	ld de,48	;Height
	exx
		ld hl,100	;Xdest
		ld de,100	;YDest
		
	exx
	call VDP_HMMM_FromStack		;Fast Copy Vram->Vram
	
	ld ix,50	;XSource
	ld iy,50	;YSource
	ld hl,48	;Width
	ld de,48	;Height
	exx
		ld hl,150	;Xdest
		ld de,100	;YDest
		
	exx
	call VDP_HMMM_FromStack		;Fast Copy Vram->Vram
	
	
	DI					;Can't RET on Cartridge
	Halt 	

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


	
VDPScreenInitData:	
	db %00000110,128+0
	db %01100000,128+1
	db 31,128+2
	db 239,128+5
	db %11110000,128+7
	db 128,128+10
	db %00001010,128+8
	db %00000000,128+9	 
VDPScreenInitData_End:	
	


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


	
VDP_HMMM_FromStack:		;Fast copy from VRAM to VRAM

		ld bc,&00D0	;Command Byte (don't change)
		push bc
		push bc		;unused
		push de		;Height
		push hl		;Width
		exx
			push de	;DestY
			push hl	;DestX
		exx
		push iy		;SourceY
		push ix		;SourceX
		
		ld hl,0
		add hl,sp
		push hl
			call VDP_FirmwareSafeWait	;Wait for VDP to be ready
			call VDP_HMMM
		pop hl
		
		ld bc,16
		add hl,bc
		ld sp,hl
	
		ret
	

; T_Tile_MyHMMM:					;Tile Copy command Vars 	
; T_Tile_MyHMMM_SX:	defw &0000 		;SY 32,33	;Destination Xpos
; T_Tile_MyHMMM_SY:	defw &0200 		;SY 34,35	;Tiles start 512 pixels down
; T_Tile_MyHMMM_DX:	defw &0000 		;DX 36,37	;Destination X
; T_Tile_MyHMMM_DY:	defw &0000 		;DY 38,39	;Destination Y
; T_Tile_MyHMMM_NX:	defw &0008 		;NX 40,41 	;Width=8px
; T_Tile_MyHMMM_NY:	defw &0008 		;NY 42,43	;Height=8px
				; defb 0     		;Color 44 	;unused
; T_Tile_MyHMMM_MV:	defb 0     		;Move 45	;Unused
				; defb %11010000 	;Command 46	;HMMM command - Don't mess with this 
; T_TemplateCommands_End				
				
VDP_HMMM:				;Fast copy from VRAM to VRAM

						;Set the autoinc for more data
	ld a,32				;AutoInc From 32
	call SetIndirect
	
	defb &ED,&A3,&ED,&A3,&ED,&A3,&ED,&A3,&ED,&A3,&ED,&A3,&ED,&A3,&ED,&A3,&ED,&A3,&ED,&A3,&ED,&A3,&ED,&A3,&ED,&A3,&ED,&A3,&ED,&A3	;outi x 15
	ret
	
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
	

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
		
VDP_HMMC_Generated_FromStack:
		ld bc,&00F0	;Command Byte (don't change)
		push bc
		ld c,a		;First Pixel
		push bc
		push de		;Height
		push hl		;Width
		push iy		;Ypos
		push ix		;Xpos
		
		ld hl,0
		add hl,sp
		push hl
			call VDP_HMMC_Generated
		pop hl
		
		ld bc,12
		add hl,bc
		ld sp,hl
	
		ret
		
;VDP_MyHMMC:								:HMMC Command looks like this
;VDP_MyHMMC_DX:	defw &0000 ;DX 36,37
;VDP_MyHMMC_DY:	defw &0000 ;DY 38,39
;VDP_MyHMMC_NX:	defw &0032 ;NX 40,41
;VDP_MyHMMC_NY:	defw &0032 ;NY 42,43
;VDP_MyHMMCByte:	defb 255   ;Color 44
;				defb 0     ;Move 45
;				defb %11110000 ;Command 46	

VDP_HMMC_Generated:		;Fill ram from calculated values (first in A)

	;Set the autoinc for more data
	ld a,36				;AutoInc From 36
	call SetIndirect
	defb &ED,&A3,&ED,&A3,&ED,&A3,&ED,&A3,&ED,&A3,&ED,&A3,&ED,&A3,&ED,&A3,&ED,&A3,&ED,&A3,&ED,&A3	; 11 outis
	
	ld a,128+44			;128 = NO Increment ;R#44    Color byte
SetIndirect:
	out (VdpOut_Control),a		
	ld a,128+17			;R#17  [AII] [ 0 ] [R5 ] [R4 ] [R3 ] [R2 ] [R1 ] [R0 ] 		Indirect Register 128=no inc
	out (VdpOut_Control),a
	ld c,VdpOut_Indirect	
	ret


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
	;Wait for VDP to be ready
VDP_FirmwareSafeWait:
	di
	call VDP_Wait						;Wait for the VDP to be available
	call VDP_GetStatusFirwareDefault	;Reset selected Status register to 0 for the firmware
	ret

VDP_Wait: 			;Get The status register - Disable interrupts, as they require status register 0 to be selected!

	call VDP_GetStatusRegister
VDP_DoWait:
	in a,(VdpIn_Status)	;S#2  [TR ] [VR ] [HR ] [BD ] [ 1 ] [ 1 ] [EO ] [CE ] - Status register 2
	rra
	ret nc
	jr VDP_DoWait

Sprite:
	incbin "\ResALL\Sprites\RawMSX.RAW"
SpriteEnd:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	;Set Status Reg

VDP_GetStatusFirwareDefault:
	xor a
	jr VDP_GetStatusRegisterB
VDP_GetStatusRegister:		;Get The status register - Disable interrupts!
	ld a,2					;S#2  [TR ] [VR ] [HR ] [BD ] [ 1 ] [ 1 ] [EO ] [CE ] - Status register 2
VDP_GetStatusRegisterB:
	out (VdpOut_Control),a
	ld a,128+15				;R#15  [ 0 ] [ 0 ] [ 0 ] [ 0 ] [S3 ] [S2 ] [S1 ] [S0 ] - Set Stat Reg to read
	out (VdpOut_Control),a
	ret
	
	org &C000		;Pad to 32k