		DEVICE 	ZXSPECTRUM128
Start_addr = 0x4200

use_emulator = 1

		include "../../include/estex_h.asm"
		include "../../include/head_short2.inc"
		include "../../include/macro.s"

Counter = 0xfe5b


LoaderStart:
main:		di
		ld sp,stack_buff
		jp begin
;-------------------------------------------------------------------------------

init0:		SYS ESTEX_getvmode		;получить текущий режим экрана
		ld (vmode.mode),a		;сохранить режим
		ex af,af
		ld a,b
		ld (vmode.screen),a		;и страницу (экран) режима
		ex af,af
		ld b,_VMODE._T80		;текстовый режим 80 сим.в строке
		cp b				;текущий режим совпадает?
;		jr z,.next_init0				;да
		ret z
		ld a,b
		ld b,1				;иначе
		ld c,ESTEX_setvmode		;установить его
		rst 0x10
		ret


WaitMsCallibration:
		di
		ld a,WaitMS_IntVector
		ld i,a
		im 2
		ei
		halt
		call WaitMS_LockCounter
		di
		im 1
		ld de,(WaitMS_LockHL)
		ld hl,20
		call WaitMS_DIV_DE_HL
		ld l,e
		ld h,d
		call WaitMS_NOT_HL
		ld (walcx0+1),hl
		ret


;HL=-HL
WaitMS_NOT_HL:	ld a,l
		cpl
		ld l,a
		ld a,h
		cpl
		ld h,a
		inc hl
		ret

;DE = DE/HL
WaitMS_DIV_DE_HL:
		push hl
		call WaitMS_NOT_HL
		ld c,l
		ld b,h
		ld hl,0
		ld a,e
		add a,a
		rl d
		call wmdi08
		call wmdi08
		ld e,a
		pop bc
		srl b
		rr c
		or a
		sbc hl,bc
		ret c
		inc de
		ret

wmdi08:		call wmdi04
wmdi04:		call wmdi02
wmdi02:		call wmdi01
wmdi01:		adc hl,hl
		add hl,bc
		jr c,$+4
		sbc hl,bc
		rla
		rl d
		ret


;One MS Delay
WaitMS_LockCounter:
		push hl
walcx0:		ld hl,Counter
walcx1:		inc hl          ;6      = 26
		ld a,l          ;4
		or h            ;4
		jr nz,walcx1    ;12.7
		pop hl
		ret


;HL = Milliseconds Number
DelayMS:
DEMSCx1:	ld a,l
		or h
		ret z
		push hl
		call WaitMS_LockCounter
		pop hl
		dec hl
		jr DEMSCx1


SysPrint:	ld a,(hl)
		inc hl
		or a
		ret z
		push hl
		call SysPrintChar
		pop hl
		jr SysPrint

SysPrintChar:	push bc
		ld c,0x5b               ; print char
		rst 0x10
		pop bc
		ret


; IN:HL=VALUE
SysPrintDEC:	LD C,255
		LD DE,10000
		CALL SYPDCx0
		LD DE,1000
		CALL SYPDCx0
		LD DE,100
		CALL SYPDCx0
		LD DE,10
		CALL SYPDCx0
		LD A,L
		JR SYPDCx2
SYPDCx0:	XOR A
		DEC A
SYPDCx1:	INC A
		SBC HL,DE
		JR NC,SYPDCx1
		ADD HL,DE
		INC C
		JR NZ,SYPDCx2
		DEC C
		OR A
		RET Z
SYPDCx2:	INC C
		ADD A,48
		PUSH HL
		push DE
		push BC
		CALL SysPrintChar
		POP BC
		pop DE
		pop HL
		RET

SysPrint0D0A:	LD A,10
		LD C,05BH               ; Print Char
		RST 16
		LD A,13
		LD C,05BH               ; Print Char
		RST 16
		RET
;-------------------------------------------------------------------------------
;errors handles
quit:		ld b,0
.err:		ld c,0x41
		rst 0x10
		jr $

;-------------------------------------------------------------------------------


begin:		call init0

		ld hl,SomeText
		call SysPrint

;		CALL WaitMsCallibration

;		ld hl,(walcx0+1)
;		call SysPrintDEC
		
		ld bc,(10000/250)*256
lomx1:		push bc
		ld a,c
		and 3
		jr nz,lomx4
		ld a,b
		rra
		rrca
		dec a
		ld l,a
		ld h,0
		call SysPrintDEC
		ld a,' '
		call SysPrintChar
lomx4:		ld hl,250
		call SysPrintDEC
		ld hl,TextMS
		call SysPrint
		pop hl
		push hl
		ld h,0
		ld a,l
		and 3
		ld l,a
		call SysPrintDEC
		ld a,'.'
		call SysPrintChar
		ld hl,250
		call DelayMS
		pop bc
		inc c
		push bc
		ld a,c
		and 3
		ld a,' '
		ld hl,SysPrintChar
		jr nz,lomx2
		ld hl,SysPrint0D0A
lomx2:		ld (lomx3+1),hl
lomx3:		call SysPrintChar
		pop bc
		djnz lomx1


		ld a,(vmode.mode)
		cp _VMODE._T80
		jr z,.exit

		ld a,(vmode.screen)
		ld b,a
		ld a,(vmode.mode)
		SYS ESTEX_setvmode
.exit		ld bc,0x0041
		rst 0x10
		jr $
		
;Interrupt For Callibration Delay
		align 256
WaitMS_IntVector = $/256-1
		org $-1
		dw WaitMS_IntCallibration

WaitMS_LockHL	dw 0
WaitMS_IntCallibration:
		ld (WaitMS_LockHL),hl
		ld hl,65535
		ei
		ret


;===============================================================================
vmode:
.mode:		db 0
.screen:	db 0

		align 256
str_buff:	ds 256

stack_buff = $+255
stack_buff_size = 256

;===============================================================================

SomeText:	db "Test For Wait Milliseconds ...",cr,lf,0
TextMS:		db "ms.",0

;===============================================================================


Loader_End:

		display "Loader start: ", LoaderStart
		display "Loader end: ", Loader_End
		display "Loader size: ", Loader_End-LoaderStart
		savebin "delayms.exe",EXEHeader,$-EXEHeader