;This source is desined for A86 assembler

isport:					;tests if we run on PC or PF
	push 	di
	push 	es
	mov 	di,0fff0
	mov 	es,di			;es=0fff0
	mov 	di,0ee	
	cmp 	es:w ptr [es:di],04350	;test 0FFF0:00EE if there is 'PC'
					;if it is 'PC' -> we are on PF
	pop es
	pop di

 	jne 	exit			;jmp if it is not Portfolio


	call	init			;graphics on

	mov	cx,0			;starting value of x axis

main1:	
	mov	bh,0			;replace mode
	mov	bl,0			;auto refresh off	
	mov	dx,10			;y co-ordinate
	mov	si,offset image 	;pointer to image [cs:si]

	call	PutImage

	call	refresh			;whole screen refreshed 

	push	cx			;save cx
	mov	cx,03000		;wait
l1:	nop
	loop 	l1			;loop
	pop	cx

	call	clear_ram		;clears content of Video RAM

	cmp	cx,200
	jae	exit			;end?

	add	cx,5			;the step of x
	jmp	short main1		;again

exit:
	MOV	AX,03
	INT	010			;text mode on
	MOV 	ax,04c00
	INT	021			;terminate

image:
db 79,0,29,0	;this is the header
DB 000,000,000,000,000,000,000,000,000,000
DB 000,000,000,000,000,000,000,000,000,000
DB 000,000,000,000,000,000,000,000,000,000
DB 00E,000,000,007,0F0,07F,01C,00C,01F,000
DB 013,000,03C,002,00C,020,08C,018,060,080
DB 011,000,070,002,004,020,04A,028,040,080
DB 009,001,0E0,002,002,022,00A,028,080,040
DB 007,003,080,002,002,03E,00A,028,080,040
DB 001,0CE,000,002,002,022,009,048,080,040
DB 000,0F8,000,002,002,022,049,048,080,040
DB 000,070,000,002,004,020,048,088,040,080
DB 000,04E,000,002,004,020,088,088,061,080
DB 003,083,080,007,0F8,07F,09C,09C,01E,000
DB 00C,081,0C0,000,000,000,000,000,000,000
DB 010,080,070,000,000,000,000,000,000,000
DB 011,000,01C,000,000,000,000,000,000,000
DB 01E,000,000,000,000,000,000,000,000,000
DB 000,000,000,000,000,000,000,000,000,000
DB 000,003,087,000,070,0E0,000,080,001,000
DB 000,004,048,080,089,010,000,000,001,000
DB 000,004,048,0A2,009,010,058,0A2,071,000
DB 000,004,048,094,009,010,064,094,089,000
DB 000,003,088,094,031,010,044,094,089,000
DB 000,004,048,088,009,010,044,088,0F9,000
DB 000,004,048,094,009,010,044,094,081,000
DB 000,004,048,094,089,010,064,094,089,000
DB 000,003,087,022,070,0E0,058,0A2,071,000
DB 000,000,000,000,000,000,040,000,000,000
DB 000,000,000,000,000,000,040,000,000,000
DB 000,000,000,000,000,000,000,000,000,000


;=============== S U B R O U T I N E S =====================

;**************
;INIT - INTITIATES GRAPHIC MODE
;     - CLEARS THE SCREEN
;     - ERASES VIDEO RAM
;
;INPUT      none
;OUTPUT     none
;DESTROYS   flags
;**************

INIT:
  	PUSH 	AX
  	MOV 	AX,0A
  	INT 	010
  	POP 	AX
  	RET
;-end of routine ------------


;*******************************
;CLEAR_RAM - CLEARS THE CONTENT
;            OF ACTUAL VIDEO RAM
;but doesnot influence the LCD content
;
;INPUT		none
;OUTPUT		none
;DESTROYS	flags
;
;**************

CLEAR_RAM:
  PUSH AX
  PUSH CX
  PUSH DI
  PUSH ES
  CLD
  SUB  AX,AX
  MOV  DI,0
  MOV  ES,0B000
  MOV  CX,0800
REP STOSB
  POP  ES
  POP  DI
  POP  CX
  POP  AX
  RET
;--end of routine-------------

;******************************
;REFRESH - MAKES A FAST COPY OF VIDEO RAM
;	   INTO LCD DRIVER ON PORTFOLIO
;
;this routine was developed by Martin and Milan Hrdlicka
;
;INPUT  	none
;OUTPUT		none
;DESTRYOS	flags
;
;******************************

REFRESH:
 cld
 push ax
 push bx
 push cx
 push dx
 push si
 push di
 push ds
refresh_hop:
 mov  si,0
 mov  ax,0b000
 mov  ds,ax
 mov  di,64
refresh_2:
 mov  cx,30
 mov  bx,si
 mov  al,0ah
 mov  dx,8011h
 cli         
 out  dx,al  
 mov  al,bl
 mov  dx,8010h
 out  dx,al  
 sti         
 mov  al,0bh 
 mov  dx,8011h
 cli         
 out  dx,al  
 mov  dx,8010h
 mov  al,bh  
 and  al,7   
 out  dx,al  
 sti         
 mov  bx,offset zrcadleni
refresh_1:
 lodsb        
 xlat [cs:bx] 
 mov  ah,al
 inc  dx     
 mov  al,0ch 
 cli         
 out  dx,al  
 mov  al,ah
 mov  dx,8010h
 out  dx,al     
 sti            
 loop refresh_1 
 dec  di        
 jnz  refresh_2 

 pop  ds        
 pop  di
 pop  si
 pop  dx
 pop  cx
 pop  bx
 pop  ax
 ret            

zrcadleni:
db    0,128,64,192,32,160,96,224,16,144,80,208,48,176,112,240
db    8,136,72,200,40,168,104,232,24,152,88,216,56,184,120,248
db    4,132,68,196,36,164,100,228,20,148,84,212,52,180,116,244
db    12,140,76,204,44,172,108,236,28,156,92,220,60,188,124,252
db    2,130,66,194,34,162,98,226,18,146,82,210,50,178,114,242
db    10,138,74,202,42,170,106,234,26,154,90,218,58,186,122,250
db    6,134,70,198,38,166,102,230,22,150,86,214,54,182,118,246
db    14,142,78,206,46,174,110,238,30,158,94,222,62,190,126,254
db    1,129,65,193,33,161,97,225,17,145,81,209,49,177,113,241
db    9,137,73,201,41,169,105,233,25,153,89,217,57,185,121,249
db    5,133,69,197,37,165,101,229,21,149,85,213,53,181,117,245
db    13,141,77,205,45,173,109,237,29,157,93,221,61,189,125,253
db    3,131,67,195,35,163,99,227,19,147,83,211,51,179,115,243
db    11,139,75,203,43,171,107,235,27,155,91,219,59,187,123,251
db    7,135,71,199,39,167,103,231,23,151,87,215,55,183,119,247
db    15,143,79,207,47,175,111,239,31,159,95,223,63,191,127,255

;-----------------------------------------------------------------

;*****************************************************************************
;PutImage
;
;this routine was developed by Martin and Milan Hrdlicka
;writes image (bit map) into VIDEO RAM
;may refresh the image directly
;
;Image is a raw bit map:
;1. First 4 bytes are a header
;2. The rest is data
;
;INPUT
;		CS:SI	- pointer to image data
;		BH	- function (mixing the image with background)
;				- 0	replace
;				- 1	xor
;				- 2	or
;				- 3	and
;
;		BL	- direct refresh
;				- 0	no refresh (must be refreshed later)
;				- 1	with LCD refresh
;		CX	- x axis
;		DX	- y axis
;
;OUTPUT		none
;DESTROYS	flags
;***************************************************************************

       
PutImage:
		push    si
                push    cx
                push    dx
		mov 	[ref],bl
                call    putimagest
                pop     dx
                pop     cx
                pop     si
                ret


putimgbuf       db     96 dup (0)
Xvel            dw      0
Yvel            dw      0
Xmem            dw      0
Radek           dw      0
BituLev         db      0
BituPrav        db      0
UpravMem        db      0
ref		db	0	;1 - refresh permitted


ven:
		cmp b[ref],0
		if z ret

	        push    ax
                push    dx
                cli
                mov     dx,8011h
                xchg    ah,al
                out     dx,al
                mov     dx,8010h
                xchg    ah,al
                out     dx,al
                sti
                pop     dx
                pop     ax
                ret



putimg_nulb:    push    ax
                push    cx
                push    es
                mov     cx,ds
                mov     es,cx
                mov     al,0
                mov     cx,90
                mov     di,offset putimgbuf
                rep
                stosb
                pop     es
                pop     cx
                pop     ax
                ret


MovImgBuf:      push    cx
                push    es
                mov     cx,ds
                mov     es,cx
                mov     cx,word ptr ds:[offset Xmem]
                mov     di,offset putimgbuf
                rep
                movsb
                pop     es
                pop     cx
                ret


ShRImgBufJ:     push    cx
                push    ax
shrimgbufj_1:   mov     di,offset putimgbuf
                mov     cx,word ptr ds:[offset Xmem]
                inc     cx
                clc
shrimgbufj_2:   rcr     byte ptr ds:[di],1
                inc     di
                loop    shrimgbufj_2
                dec     ah
                jnz     shrimgbufj_1
                pop     ax
                pop     cx
                ret


ShRImgBufH:     push    cx
                push    si
                push    ax
                mov     cx,word ptr ds:[offset Xmem]
                inc     cx
                mov     si,offset putimgbuf
                add     si,cx
                mov     di,si
                mov     ah,0
                inc     cx
                add     di,ax   ; v AH je 0, v AL je hruby pocet
shrimgbufh_1:   mov     al,ds:[si]
                mov     ds:[di],al
                mov     ds:[si],ah
                dec     si
                dec     di
                loop    shrimgbufh_1
                inc     di
                pop     ax
                pop     si
                pop     cx
                ret


OutRadek:       mov     ax,di
                push    ax
                mov     ah,0ah
                call    ven
                pop     ax
                xchg    ah,al
                mov     ah,0bh
                call    ven
outradek_1:     mov     al,es:[di]
                inc     di
                shr     al,1
                rcl     ah,1
                shr     al,1
                rcl     ah,1
                shr     al,1
                rcl     ah,1
                shr     al,1
                rcl     ah,1
                shr     al,1
                rcl     ah,1
                shr     al,1
                rcl     ah,1
                shr     al,1
                rcl     ah,1
                shr     al,1
                rcl     ah,1  ; AL<-0, AH zrc AL
 
		push ax
		mov al,ah
		mov ah,0c
		call	ven
		pop ax

                loop    outradek_1
putimg_kon1:    ret


putimagest:     cld        ; Vstup: CX - x  DX - y  DS:SI - pointer
                cmp     cx,239
                jg      putimg_kon1  ; out of X
                cmp     dx,63
                jg      putimg_kon1  ; out of Y
                lodsw                ; do AX Xvel
                mov     word ptr ds:[offset Xvel],ax
                or      cx,cx
                jns     putimg_dal1  ; kdyz CX lezi v disp tak skoc
                add     ax,cx
                js      putimg_kon1  ; podout of X
putimg_dal1:    lodsw                ; do AX Yvel
                mov     word ptr ds:[offset Yvel],ax
                or      dx,dx
                jns     putimg_dal2  ; kdyz DX lezi v disp tak skoc
                add     ax,dx
                js      putimg_kon1

putimg_dal2:    mov     ax,ds:[offset Xvel]
                inc     ax
                mov     bl,8
                div     bl
                or      ah,ah
                jz      putimg_dal3
                inc     al
putimg_dal3:    mov     ah,0
                mov     word ptr ds:[offset Xmem],ax   ; ulozi delku jednoho radku
putimg_smc1:    or      dx,dx
                jns     putimg_dal4    ; DX>=0
                inc     dx
                dec     word ptr ds:[offset Yvel]
                add     si,ax
                jmp     short putimg_smc1
putimg_dal4:    mov     ax,dx
                add     ax,word ptr ds:[offset Yvel] ; do AX Ysour dol hrany
putimg_smc2:    cmp     ax,63
                jbe     putimg_dal5
                dec     ax
                dec     word ptr ds:[offset Yvel]
                jmp     short putimg_smc2
putimg_dal5:    add     cx,240
                mov     ax,cx
                mov     bl,8
                div     bl          ; AH jemne, AL hrube
                inc     word ptr ds:[offset Yvel]
                push    es
                mov     bp,0b000h
                mov     es,bp       ; do ES seg VideoRAM
putimg_smc3:    call    PutImg_NulB
                call    MovImgBuf
                or      ah,ah
                jz      putimg_dal6
                call    ShRImgBufJ
putimg_dal6:    call    ShRImgBufH  ; DS:DI adresa 1.byte v ImgBuf
                push    si
                push    ax
                push    cx
                push    dx
                mov     ds:[offset BituLev],ah
                mov     bl,ah
                mov     ax,word ptr ds:[offset Xvel]
                inc     ax
                add     al,bl
                adc     ah,0
                mov     bl,8
                div     bl
                or      ah,ah
                jnz     @putimg2
                dec     al
@putimg2:       mov     ds:[offset UpravMem],al
                or      ah,ah
                jz      @putimg1
                sub     bl,ah
                mov     ah,bl
@putimg1:       mov     ds:[offset BituPrav],ah
                mov     bp,di
                mov     ax,dx
                mov     bl,30
                mul     bl
                mov     di,ax                   ; v DI adresa DX-teho radku
                xor     cx,cx
                sub     bp,offset PutImgBuf+30
                js      Putimg_znv1    ; Zacatek nelezi ve Video
                add     di,bp          ; do DI adresu Video kam se bude strkat
                mov     ch,es:[di]
                jmp     short putimg_dal7
PutImg_ZNV1:    mov     ah,0           ; zacatek nelezi ve videoRAM
                mov     ds:[offset BituLev],ah
                mov     al,ds:[offset UpravMem]
                add     ax,bp
                mov     ds:[offset UpravMem],al
                xor     bp,bp
PutImg_dal7:    mov     ah,0
                mov     al,ds:[offset UpravMem]
                add     bp,ax
                cmp     bp,29
                ja      PutImg_knv1  ; konec nelezi ve video
                push    di
                add     di,ax
                mov     cl,es:[di]  ; do CL "posl" byte z video
                pop     di
                jmp     short putimg_dal8
PutImg_KNV1:    mov     ah,0
                mov     ds:[offset BituPrav],ah
                mov     al,ds:[offset UpravMem]
                sub     bp,29
                sub     ax,bp
                mov     bp,29
                mov     ds:[offset UpravMem],al
PutImg_dal8:    add     bp,30+offset putimgbuf  ; DS:BP ukazuje na pos rel byte
                cmp     bh,1
                jz      putimg_XOR
                cmp     bh,2
                jz      putimg_OR
                push    cx
                mov     dx,0ffffh
                mov     cl,ds:[offset BituLev]
                shr     dh,cl
                mov     cl,ds:[offset BituPrav]
                shl     dl,cl
                not     dx
                pop     cx
                cmp     bh,3
                jz      putimg_AND
                and     cl,dl      ; PutImg_MOV
                or      ds:[bp],cl   ; upravi pravou stranu IMG
                push    bp
                xor     ax,ax
                mov     al,ds:[offset UpravMem]
                sub     bp,ax
                and     ch,dh
                or      ds:[bp],ch  ; upravi levou stranu IMG
                mov     cx,ax
                inc     cx
                pop     si
                push    cx
                push    di
                add     di,cx
                dec     di
                std
                rep
                movsb
                cld
                pop     di
                pop     cx
                jmp     PutImg_dal9
PutImg_XOR:     mov     si,bp
                xor     cx,cx
                mov     cl,ds:[offset UpravMem]
                inc     cx
                push    cx
                push    di
                add     di,cx
                dec     di
Putimg_xor1:    mov     al,ds:[si]
                xor     es:[di],al
                dec     si
                dec     di
                loop    putimg_xor1
                pop     di
                pop     cx
                jmp     PutImg_dal9
PutImg_OR:      mov     si,bp
                xor     cx,cx
                mov     cl,ds:[offset UpravMem]
                inc     cx
                push    cx
                push    di
                add     di,cx
                dec     di
Putimg_or1:     mov     al,ds:[si]
                or      es:[di],al
                dec     si
                dec     di
                loop    putimg_or1
                pop     di
                pop     cx
                jmp     PutImg_dal9
Putimg_AND:     or      ds:[bp],dl
                push    bp
                xor     cx,cx
                mov     ch,ds:[offset UpravMem]
                sub     bp,cx
                or      ds:[bp],dh
                inc     cx
                pop     si
                push    cx
                push    di
                add     di,cx
                dec     di
Putimg_and1:    mov     al,ds:[si]
                and     es:[di],al
                dec     si
                dec     di
                loop    putimg_or1
                pop     di
                pop     cx
PutImg_dal9:    call    OutRadek
                pop     dx
                pop     cx
                pop     ax
                pop     si
                inc     dx
                dec     word ptr ds:[offset Yvel]
                jz      putimg_dal10
                jmp     putimg_smc3
putimg_dal10:   pop     es
                ret

;-------------- end of routine PutImage ---------------------------------


