.286
.model tiny

.code

        startupcode
start:
	
        mov     ah,1                        ; VYCISTENI BUFFERU
        int     16h
	jz	kb_empty
        mov     ah,0                        ; VYCISTENI BUFFERU
        int     16h
kb_empty:
;-----------------realok. pameti
	mov	ah,4ah			;realokace pameti
	mov	bx,8192
	int	21h
	jnc short realok_ok

	mov	ah,09
        mov     dx,offset err_text		;chybove hlaseni
        int     21h
	jmp	end_pr

realok_ok:	
;--------otevreni sourobu ----------------------------------
	xor	bp,bp
	xor	cx,cx
	xor	di,di
	dec	bp
	mov	cl,cs:[80h]		;pocet znaku parametru
	cmp	cl,0
	jz	file_error
	mov	al,' '
first_char:
	inc	bp
	cmp	cs:[81h+bp],al		;prekoceni mezer na zacatek parametru
	je	first_char
	
;	inc	di			;cilovy retezec se musi posunou na zacatku musi byt '"'	
file_string:
	mov	ah,cs:[81h+bp]		;kopirovani znaku do pameti
	mov	[param + di],ah
	inc	bp
	inc	di
	cmp	bp,cx			;test jestli uz se nacetl cely parametr
	je	try_open

	cmp	cs:[81h+bp],al		;dokud nenarazi na dalsi mezeru
	je	try_open
	jmp	short	file_string

try_open:
;	mov	[param],'"'
;	mov	[param + di ],'"'
	mov	[param + di],0	; mame vygenerovany retezec pro otevreni souboru
	mov	dx,offset param
	
	mov	ah,03dh
	xor	al,al			;zpusob otevreni
	int	21h
	jc	file_error
	
	mov	[file_handle],ax	;ulozeni handle souboru
	jmp	short	file_ok

file_error:
	mov	ah,09
        mov     dx,offset file_err_text		;zobrazi retezec
        int     21h
	jmp	end_pr

file_ok:
;----------------------uspesne otevren soubor
;------------------------detekce mysi
	xor	ax,ax
	int	33h		;inicializace mysi
	cmp	ax,0
	jne	mouse_ok

	mov	ah,09
        mov     dx,offset mouse_err_text		;zobrazi chybovy retezec
        int     21h
	jmp	end_pr
mouse_ok:


;---------------zacatek inicializace gr rezimu
;	mov	ax,0a000h		;nastaveni es na zacatek video pameti
;	mov	es,ax

        mov        ah,0fh                        ;nacteni stavajiciho modu
        int        10h

	mov	[old_gr_mod],al		;zaloha grafickeho modu

	mov	ax,13h			;inicializace grafickeho rezimu	13h (320*200 256c)
	int	10h
	


;--------------------------nacteni palety ze souboru a obrzku do do shadow pameti
	mov	ah,03fh			;zahozeni prvnich 18 ti bytu souboru
	mov	bx,[file_handle]
	mov	cx,18
	mov	dx,offset param		; param uz nepotrebuji muzu si premazat
	int	21h
	cmp	cx,ax
	jne	file_error		;pokud se nenacet steny pocet bytu jako sem chtel

	mov	bp,ds
	mov	es,bp
	add	bp,4096			;posunuti na realokovane misto v pameti
	mov	ds,bp
	mov	cx,768			;nacteni palety ze souboru
	xor	dx,dx
	mov	ah,03fh
	int	21h
;---------- poslani palety z pameti do graf adapteru.
	xor	ax,ax			;nastaveni indexu prni barvy co budem menit tj. 0		
	mov	dx,03c8h
	out	dx,al
	
	inc	dx			; dalsi port
	
	mov	cx,256
palete_loop:
	mov	bx,cx			;prevedni z cx klesajici od 256 na rosouci od 1
	sub	bx,0101h
	not	bx
	
	mov	ax,bx
	shl	bx,1
	add	bx,ax

	mov	al,ds:[bx+2]		;generovani palety
	shr	al,2
	out	dx,al
	mov	al,ds:[bx+1]
	shr	al,2
	out	dx,al
	mov	al,ds:[bx]
	shr	al,2
	out	dx,al
	
	loop	palete_loop
;-------------------------------------------
;------------------- nastaveni rozsahu mysi
	xor	ax,ax
	mov	al,R	; vypocet pro okraje kde se muze pohybovat lupa
	shl	ax,1			;vydeleni dvouma
					;X
	mov	cx,0			;minimum
	mov	dx,320
	sub	dx,ax			;maximum
	mov	ax,07h
	int	33h
					;Y
	mov	cx,0			;minimum
	mov	dx,150
	sub	dx,ax			;maximum
	mov	ax,08h
	int	33h
	
;-----------nacteni obrazku do stinovy palety
	mov	cx,0fa00h			;nacteni palety ze souboru
	mov	bx,cs:[file_handle]	;handle souboru (ds tak cs = ds)
	xor	dx,dx
	mov	ah,03fh
	int	21h
; ---------- nyni je obrzek ve stinove pameti
	push	0a000h
	pop	es		; nasteveni es na video pamet
	
	xor	si,si	
	xor	di,di
		
	mov	cx,0fa00h
	rep 	movsb		;zkopirovani svech bytu ze stinove do video

;--------------------genrovani pole AM[]

	push	cs
	pop	es
        mov bx,1-R      ;MAP build
	mov si,R*R
	mov di,offset map
	mov bp,-2*R*(R-1)
	
nok:	mov cx,1-R
nol:	mov ax,cx
	imul cx
	push ax
	mov ax,bx
	imul bx
	pop dx
	add ax,dx
	cmp ax,si
	jc nor
	mov ax,bx
	push cx
	jmp short nos
nor:	mov dx,	10922		;(0FFFFh*(M-N)/M/N)
	mul dx
	mov ax,dx
	add ax,R*R/M
	push ax
	imul cx
	idiv si
	mov dx,ax
	pop ax
	push dx	
	imul bx
	idiv si
nos:	mov ah,2*R-1
	imul ah
	pop dx
	add ax,dx
	push ax
	sub ax,bp
	stosw
	pop bp
	inc cx
	cmp cl,R
	jl nol
	inc bx
	cmp bl,R
	jl nok

;--------------------------hlavni vykl. cte mys a podle potreby vola genrovani lupy

MainLoop:			;hlavni cyklus pro zobrazovani lupy

	mov	ax,03h
	int	33h		;CX vodorovna a DX svisla

	call	lupa_proc		;volani generujici lupu

;-----------------------
	mov	ah,01h			;cekani na stisk klavesy
	int	16h
	jz	MainLoop

        mov         ah,0                        ; VYCISTENI BUFFERU po stisku klavesy
        int         16h


;-------------------------------------------------
	push	cs
	pop	ds		; ds na puvodni hodnotu 

	mov	al,[old_gr_mod]			;inicializace puvodniho grafickeho rezimu
	xor	ah,ah
	int	10h

	mov	ah,3eh				;zavreni souboru
	mov	bx,[file_handle]
	int	21h
end_pr:
	mov	ah,09
        mov     dx,offset end_text		;zobrazi retezec
        int     21h


; konec programu -------------------------------
        mov         ax,4c00h
        int         21h

;----------------PROC---------------------------
PROC	lupa_proc	;cx	vodorovna	dx svisla

	mov	bp,cs
	add	bp,4096			;posunuti na realokovane misto v pameti
	mov	ds,bp


	mov	si,cx			;posun na zacatek ctverce kde se bude realizovat lupa
	mov	di,dx
	
	mov	dx,di			;
	xchg	dh,dl			;nasobeni 256
	shl	di,6			;nasobeni 64
	add	di,dx			; v di je 320*y
	add	di,si			;pricteni x ovy osy -> v di je souradnice prvniho bodu
				;kopirovani ctverce do zalozniho pole zalohaBI


	mov	cs:[begin_lupa],di
	xchg	si,di
	mov	di,offset zalohaBI
	push	cs
	pop	es
	mov	ax,(2*R-1)
copy_loop:
	mov	cx,(2*R-1)
	rep	movsb
	add	si,(320 - 2*r + 1)
	dec	ax
	jnz	copy_loop	;zkopirovani ctverce konec
	
;--------------------genrovani lupy

	mov	di,offset map
	mov	si,offset zalohaBI

	mov	bp,cs:[begin_lupa]	
	mov	ax,(2*R-1)
lupa_loop:
	mov 	cx,(2*R-1)
lupa_loop1:
	add	si,cs:[di]
	mov	bl,cs:[si]
	mov	ds:[bp],bl
	inc	di
	inc	di
	inc	bp
	loop	lupa_loop1

	add	bp,320 - (2*R-1)
	dec	ax
	jnz	lupa_loop	;vygenerovani lupy (v stin. pam je lupa v zalohaBI je puvodni ctverec)

;------------- cekani na synchronizacni paprsek
	mov	dx,3DAh
WaitVR1:
	in      al,dx
	test    al,8
	jne     WaitVR1
waitVR2:
	in      al,dx
	test    al,8
	je      WaitVR2
;--------------------------------

;----------vykresleni ze stin. pam
	mov	bp,cs
	add	bp,4096			;posunuti na realokovane misto v pameti
	mov	ds,bp

	push	0a000h
	pop	es		; nasteveni es na video pamet
	xor	si,si	
	xor	di,di
	mov	cx,0fa00h
	rep 	movsb		;zkopirovani svech bytu ze stinove do video
;------------------obnoveni puvodniho obrazu pod lupou
	push	ds
	pop	es
	push	cs
	pop	ds

	mov	si,offset zalohaBI
	mov	di,cs:[begin_lupa]
	mov	ax,(2*R-1)
copy_loop1:
	mov	cx,(2*R-1)
	rep	movsb
	add	di,(320 - 2*R +1)
	dec	ax
	jnz	copy_loop1	;zkopirovani ctverce konec	

	ret
ENDP
;----------konstanty
R	equ 31
N	equ 2
M	equ 3

;------------- DATA
citac		dw	0
old_gr_mod    db	?
file_handle	dw	?
palete_RGB	db	?,?,?
param		db	128 dup(?)
err_text      db 10,'Memory realocation error',10,13,'$'
file_err_text	db	10,'File error opening /reading',10,13,'$'
mouse_err_text	db	10,'Mouse error detection',10,13,'$'
end_text      db 10,'Thanks for using, Bye',10,13,'$'
map		dw	3721 dup(?)
zalohaBI	db	3721 dup(?)
begin_lupa	dw	?
end