 
		DEVICE ZXSPECTRUM128
;supporting 4 servers
;server0= sarah.speccy.cz- official
;server1= sarah.speccy.cz- private (can be made hidden with POKE)
;server2= zxart.ee
;server3= zxdb.remysharp.com


;ver09
;DONE simplification!! OSL 09 exists in two versions from now. For BSDOS and ESXDOS. This change made due to change in MB core since 25.1.2025.

;ver08
;DONE zobrazeni TXT a POK
;PRINTHL vraci v HL nasledujici tiskovou pozici, v DE posledni adresu znaku, v A posledni znak
;PRINT HL nyni umi tisko po cele obrazovce, umi prechod mezi tretinami
;DONE vypnuti 20MHz po downloadu textu nebo pok+ udelat spravny lddr pro navrat jmena do inpbufferu
;DONE udelat chybi zobrazeni "OK" u "Logged in:"
;DONE udelat cekovani statusu i u EL, ktery nemusi mit zadny RTC generator

;ver07xx (not release version)
;fixed: after quit, previou search will be carried out upon OSL restart
;fixed: search- sometimes search got stuck
;new:roll item
;fixed: red items- they do not appear any more
;fixed: added close server upon OSL quit
;added: BSDOS version can load snaps (.Z80 and .SNA) via Hood's nmi menu

;ver07- a pak to může jít mezi lidi
;TODO	DONE při not found automaticky najet na další server v pořadí
;TODO	DONE POKE pro skrytí privátního serveru
;TODO	DONE error handling: here I need to evaluate, ho wto handle error. If it happens in the middle of download, or elsewhere.
;TODO	DONE pokud status 5, tak cekat a nechat puvodni server
;TODO	DONE pri chybe, napr 37 udelat AT+CIPCLOSE, aby se server zase rozbehl
;TODO	DONE aby buffer od #5BXX nepřelézal do sys proměnných od #5C00!!! (uz neukladame pri look_on_fly dolu)
;TODE	DONE-vyresil jsem pomoci CIPCLOSE=5 natvrdo. Pri startu zavrit vice kanalu (CIPMUX dat na nulu) a udelat cipclose. Nedjrive tak, ze 1) otestuju cipstatus 2) pokud je <>3 pokracuj. 3)zjisti CIPMUX 4) pokud je 1 pak udelej CIPCLOSE=5, pokud je 0, pak staci jen AT+CIPCLOSE (necham to d dalsi verze).

;DONE	overit, page up/down, pri stisku H i pri hledani
;DONE	kdyz not found na vsech serverech, musi jet abort (ok, klavesa B)
;DONE	jakz,takz. spousteni SCL i TRD v esxdosu



;^18836^Manic Bugs^ManicBugs.tap^5583^1^1987^4^
;^18838^Manic Miner^ManicMiner.tzx^33205^1^1983^4^
;^18839^Manic Miner^ManicMiner(different).tzx^33738^1^1983^4^
;^18840^Manic Miner^ManicMiner.tap^33168^1^1983^4^
;^18841^Manic Miner^ManicMiner(MastertronicAddedDimension).tzx^33276^1^1983^4^
;^18842^Manic Miner^ManicMiner(SoftwareProjectsLtd).tzx^33299^1^1983^4^
;^18843^Manic Miner^ManicMiner(SoftwareProjectsLtd).tap^33218^1^1983^4^
;^18844^Manic Miner^ManicMiner(Ventamatic).tzx^33998^1^1984^4^
;^18864^Manic Miner 2^ManicMiner2.tzx^33780^1^1985^4^
;^18865^Manic Miner 2^ManicMiner2.tap^33640^1^1985^4^ 



;delka polozky: na konci i zacatku vcetne indikacniho bajtu. Tzn napr. 33, 33 bajtu obsahuje 1 bajt info+32 bajtu obsah polozky
;v pameti ulozit takto:
;0,44,0,188360Manic Bugs0ManicBugs.tap05583 1 1987,0,0,44.
;jak to ma vypadat na obrazovce:
/*

Manic Bugs     ManicBugs                       tap   5583 1 1987
Manic Miner    ManicMiner                      tzx  33205 1 1983
Manic Miner    ManicMiner(different)           tzx  33738 1 1983
Manic Miner    ManicMiner                      tap  33168 1 1983
Manic Miner    ManicMiner(MastertronicAddedD...tzx  33276 1 1983 
Manic Miner    ManicMiner(SoftwareProjectsLtd) tzx  33299 1 1983
Manic Miner    ManicMiner(SoftwareProjectsLtd) tap  33218 1 1983
Manic Miner    ManicMiner(Ventamatic)          tzx  33998 1 1984
Manic Miner 2  ManicMiner2                     tzx  33780 1 1985
Manic Miner 2  ManicMiner2                     tap  33640 1 1985

*/
;dir name	15
;file name	32
;extension	4
;length		7
;number		2
;year		4
;total		64

;02 makame na zpracovani nalezenych her
;TODO	DONE prepsat 16 bit odecitani
;TODO	DONE delka snizena o ocasek, opravit
;TODO	DONE ukonceni relace, vycisteni bufferu v pripade abortu
;TODO	DONE tisk polozek
;TODO	DONE pohyb po polozkach
;TODO	DONE v polozce stazeni delky souboru, netreba, vezmu to z hlavicky HTTP
;TODO	DONE v polozce vyber jmena souboru
;TODO	DONE sejv polozky (vezmi original nazev)
;TODO	DONE page up/page down
;TODO	DONE po nothing found to udelat jinak. Bud skocit na nove vyhledavani, nebo, kdyz se neco naslo, tak nechat ovladat sipkami
;TODO	DONE v hledanem retezci nahradit mezery hvezdickami
;TODO	DONE po downloadu (kavesa D) obnovit page, kurzor a pozici

;TODO	po stazeni souboru a jeho spusteni vypsat na obrazovku Transfer OK
;TODO	sejv polozky (vloz svuj nazev) (ss+D) (do dalsi verze, pripadne vubec)
;TODO	pokud se nazev souboru nevejde, udelat scroll (do dalsi verze)

;cipstatus=3 to chceme
;closed=4, zajima nas jen 3
;verze 1.1 je stejná jako 1.0, jen vkladame novy bios 1.9 a budou celkem 4 verze:
;1) MB03+ BSDOS (MBBSD)		... pro wifi modul v MB03+
;2) MB03+ ESXDOS (MBESX)	... pro wifi modul v MB03+
;3) EL+ BSDOS (ELBSD)		... pro wifi modul v el
;4) EL+ ESXDOS (ELESX)		... pro wifi modul v el
;bugfix: speed managemenet call earlier (caused endless loop if wifi was off)

;jako 0.99, ale ošetření stavu rutin?
;koncept, kdy se vracim na misto odkus jsem vysel relizovan jen pri vyskoku z adresare na 1 urovni. Ostatni navraty (jako pri downloadu, uploadu, apod.) udelat v pristi verzi, pripadne
;99-8-pridavame vyskok do nadadresare na stejnou polozku, jako byla pri vnoreni do podadresare
;err34- se take vraci, pokud po dr end ok, kdyz cteme zda byl transfer uplny. Proste na sluzbe askfor replyinpassive mode jsem vzdy moc brzo a ona nestaci nacist obsah
;na sarah, pismeno games-wos/c je problem, prepisuje kod!!! presneji receno, pokud je transfer incomplete- vyreseno, musi se udelat vzdy cipclose kanalu 1
;chyba 34- v odpovedi na LIST casto jsme tam moc dopredu, musime si pockat az modul vrati vsechny picoviny typu IPD a teprve pote nam vrati hledany retezec CIPRECVDATA:. Zkusit zvetsit timeout ze 4 na vic
;err34-opravit check module answer, cte uz moc dopredu a pri LISTu nacita adresar!!!
;upravujeme check_module_answer
;chyba 35= nejsme prepnuti v ciprecmodu 0!!!!, na ciprecvmode=0 je odpoved OK, tu musime mit abychom jeli dal
;na cIPRECVDATA=0,128 odpovi #D#AOK#D#A pokud nejsou zadna data k dispozici
;do send ftp status dopsat detekci pripojeni ke kanalu 1?? zatim asi ne.
;oprava INPUTU při deletu to padá
;DONE zobrazeni screenu
;DONE cost only 8 bytes!!, pocet adresaru 16 bitove
;DONE toto bohuzel nejde. Server neposila zadne ukonceni adresare. Za posledni polozkou je #D#A jako z akazdou jinou. Ukonceni cteni adresare, pokud prijdou 2xDA za sebou (problem, pokud je adresar prazdny, tak i prichozi adredarova data jsou nulova/zadna!!)

;send_ftp_command
;DONE...adding TYPE command (A for directory), I for file transfer.
;DONE...adding un/mark all (SS/CS+M)
;in process adding Esxdos disk operation

;v0.99
;cleaning up the code
;adding: variable ftp port, eg. for mobile ftp servers (eg. 192.168.0.200:31456)

;v0.98
;multiple download

;v0.97
;DONE...	delete marked

;v0.96		adding:
;DONE...	file mark	
;DONE...	file search
;in097		delete marked
;DONE...	opravit-neumi hledat jediny znak (viz rutina 12, vysvetleno)

;v0.95		see ASM in 0.95
;v0.94
;opraveno	sif.speccy.cz. sinclair pismeno W, spatne pocita zarazku
;opraveno	pg_updown, a nacitani zkracenych polozek a NLST sif.specc.cz, wos/sinclair/games/s
;opraveno	pri LIST abbr se naktere polozky adresare spatne zobrazovaly, nyni ok
;v0.93		u stazeni adresare kontrolujeme odezvu modulu po prijeti dat (tzn. OK, IPD atd.)
;v0.92		hodne vyladeno chyb v nove napsanem kodu

;v0.91
todo:
;DONE...	proverit: podle me by pri abortu melo být 1x POP, download file i directory retrieve callujeme!!!
;DONE...	opravit: page up/down, kdyz dojedu na konec stranky, kurzor ma zmatek
;DONE...	opravit: nacitani zkracenych polozek!!! nacte blbosti mezi polozkami (resil jsem na Schlimeischi 2022)
;opravit: chyby 26 pri downloadu obcas
;DONE...	opravit: sif.speccy.cz, wos/sinclair/games/s... tuhne.......
;DONE...  	opravit pri NLST se spatne uzavre posledni polozka
;DONE...	proverit. testovat: operace, kdyz stojim na polozce ".." v adresari
;DONE...	pridat: dopsat do obrazovky nové klavesy
;DONE... 	hromadne oznacovani souboru a jejich download (zatim bez adresaru). Pridano na pul, ale se spolecnou pameti pro stahovani dat a adresar nelze dost dobre realizovat
;DONE...	pridat: abort pri stazeni adresare (pokracuju normalne, pokud mam v pameti aspon jednu polozku)
;DONE...	pridat: roll item >64 chars
;DONE...	pridat: moznost zadavat prikazy rucne
;DONE...	udelat Reread adresare
;DONE...	(viz0.96)pridat: pres klavesu Ext: hledat dle zadani klaves (pr. zmacknu "s" a najde to prvni polozku zacinajici na "s")
;pridat: nacitat nejdriv adresare a ve druhem cteni pak soubory




;DONE... make dir/ delete dir
;DONE... delete file
;DONE... rename file
;DONE... adding file and dir/ items counting
;DONE... get_file_name- to return also file length
;opraveno: po ABORTu transferu mazou klavesy nahoru dolu obrazovku a neco je asi se zasobnikem, proverit: opravil jsem. 


;v0.9
;speeding up directory read
;further keys:
;N=	toggle LIST/NLST
;SS+N=	toggle full/abbr
;BREAK	go to updir
;"7"	create dir
;delete	delete dir/file/item
;"6"	rename
;
;CS+D
;C	enter command

;OK... added STOU pri uploadu
;OK... oprava key down- print page nulujeA= zadny nechteny stisk klavesy



;v0.8
;adding dir browsing support. Buffer for dir will be from basic_end (#6000) till start_address (#C7FF)= #6800=26624 bytes for first version. In next versions I will start using MB03+ SRAM pages for better comfort.
;odpovida take kodem 150 a 226
;stahnout data jen velikosgti 2048 a vzdy jen na zacatku


;v0.7 release

;v0.6
;done...ABORT pri username a password znamena, vyskok do key_loop? Mel by, protoze pri blbem pass, nebo jak ho neznam, tak to chce porad heslo nebo usera, nejde z toho vyskocit. (change_host, ftp_upload_file, set_filename, set_start, set_user, send_ftp_command))
;done...zobrazit heslo jako XXXXX, kdyz bude uzivatel chtit
;done...zablokovat vsechny pokusy o posilani prikazu serveru, pokud nejsme pripojeni (size, logoff, upload, 2x download)
;done...kazdou minut poslat NOOP cist primo z RTC?

;nemuzu upload dostat na vic nez 3,5Mhz, proc?????? viz rutina pro ftp_upload. I po vyzkouseni send_block se jednak rychlost nezvedla, ale prenos byl nespolehlivy


;done...u loginu a auto loginu zjistit na zacatku, zda jsme pripojeni ke stejne URL, pokud se lisi, pak pokracovat, pokud jsou stejne, pak key_loop
;done...misto c. chyby vypsat hlaseni, ze nenasel url
;done...zprovoznit na rychlosti 16, a taky 7, 14 a 20 MHz!!! (pouze download, upload proste nejede na >3,5Mhz)
;done...pri opusteni programu prepnout na rychlost 7?
;done...pri uploadu, downloadu, stazeni adresare vykotlit rychlost


;dodelat volbu:
;done...F
;done...T
;done...A
;done...R
;done...P
;done...ss+L
;done...ss+D
;done...vypsat řádky 17 a 18

;done...turnoff printing of sending AT commands to module
;done...save application with its settings
;done...when entering empty input line, the program crashes
;at new URL login, ask message if current connection should be turned down
;done...known bugs: pri chybe 425 Can't open data connection pri downloadu zustane program v CIPRECVMODE=1;do downloadu doplnit pri chybach vypnuti passive modu


;done...ftp messages scroll up (in v 0.5. were down)
;done...ACTFORCE2_KM_48_128.TAP se spatne stahuje
;done....pri abortu se nevypisuje 226 ABOR command sucessful
;done...asci okpri uploadu. error 12!!!
;done...pri uploadu a abortu to vypisuje, ze prikaz abort nebyl uspesny, chyba 16



;todo:
;done...cykleni pri loginu odstraneno
;done...*v tiskove rutine premenit kody #D,#A na odradkovani
;done...*tisknout jen hlaseni FTP (nikoliv cely obsah RXbufferu)
;*udelat do dalsi verze-v uploadu- zkusit vynechat cekani na odpovedi,treba se upload dat zrychli


;otestovat ABORT pri uploadu i downloadu
;kod 226 po downloadu i uploadu??? otazka jako to otestovat, když vždy to dopadlo dobře....

/*pokud selže login.....
(000005)14.9.2022 3:16:50 - (not logged in) (192.168.0.110)> USER hoodjkjk
(000005)14.9.2022 3:16:50 - (not logged in) (192.168.0.110)> 331 Password required for hoodjkjk
(000005)14.9.2022 3:16:59 - (not logged in) (192.168.0.110)> PASS *******
(000005)14.9.2022 3:16:59 - (not logged in) (192.168.0.110)> 530 Login or password incorrect!
(000005)14.9.2022 3:17:00 - (not logged in) (192.168.0.110)> NOOP 
(000005)14.9.2022 3:17:00 - (not logged in) (192.168.0.110)> 530 Please log in with USER and PASS first.
(000005)14.9.2022 3:17:28 - (not logged in) (192.168.0.110)> 421 Login time exceeded. Closing control connection.
(000005)14.9.2022 3:17:28 - (not logged in) (192.168.0.110)> disconnected.*/

;v0.5.
;opravujeme chybu 0.4 v downloadu, kdy se nekdy neplni buffer do plna.

;v0.4.
;precizujeme download, stale pouzivame passivni mod prenosu (AT+CIPRECVMODE=1), ale necekame, az bude alespon 2048 bajtu,
;ale bereme cokoliv. Pouze, pokud je bajtu 0, cekame, az se nejake do bufferu modulu nactou. Snad se cely proces ;downloadu zrychli.
;super, funguje to. Rychlosti downloadu:
;pri 7= 	6,8 kb/sec	1x
;pri 12=	14,1 kb/sec	10x
;pri 16=	15,2 kb/sec	30x



;v0.3
;new concept of download: using passive transfer mode
;the principle: AT+CIPRECVMODE=1 enables to wait for incoming data and ask for them whenever we like.
;response to AT commands works normally
;8:04 12.08.2022 we leave this one. Problem: this ver is waiting until at least 2048 bytes is in module memory. But if ;this condition should be met, then it lasts forever. 16kb transfer lasts 18 secs!!!!
;ver0.4. will use passive receibe mode too, but will fetch any data immediatelly. Hopefully, this will speed up whole ;download.

;v0.2- working on download
;closed for now: continuous flow of data is quite hard to handle
;going to ver0.3: I will realise passive transfer mode during download


;v0.1-closed
;otestovat doma:
;done	udelat moznost ABORT v prubehu uploadu
;done, ale vyprecizovat!!!!!!	pred AT+CIPMUX se podivat, jestli uz neni vykonano, jinak haze error 3
;pokud je pripojeno k ftp, pak na dotaz AT+CIPSTATUS vraci: "STATUS:3<cr>+CIPSTATUS:0,"TCP","192.168.0.113",21,port, atd" a soucasne na dotaz AT+CIPMUX=1 vraci "link is builded"..>tzn jsme jiz pripojeni k FTP serveru
;tzn. ukol= prepracovat rutinu get_status, aby zjistila, zda jsem pripojen k ftp (musí být status 3, url adresa a port 21). Pokud ano, pak vypsat URL serveru a zkusit NOOP. Pokud ne, pak jet pres CIPMUX=1 klasicky.
;statusy 2,4 a 5 znamenaji zadne připojení k ftp. Pouze status 3= pripojeno.
;done	proc se pri erroru vraci error handler na zacatek (na adresu #C800)???- u/opraveno, error handler se vraci na adresu error_handler_loop
;done	rychlost 12 opravit, aby jel pri ni i HTTP downloader- problem nalezen. FTP nastavuje CIPMUX=1, kdezto downloader ma CIPMUX=0. Proto haze error. Pri vyskoku z ftp nelze nastavit CIPMUX na 0, protoze jsme pripojeni k serveru. Cili, toto si musi osetrit kazda aplikace.


;done	quit- do BASICu, prace se zasobnikem
;done	implementovat prikaz ABOR, pokud upload, download selze, abychom se mohli vratit (server vraci:
	;(000019)18.7.2022 5:34:35 - hood (192.168.0.113)> ABOR
	;(000019)18.7.2022 5:34:35 - hood (192.168.0.113)> 426 Connection closed; transfer aborted.
	;(000019)18.7.2022 5:34:35 - hood (192.168.0.113)> 226 ABOR command successful
;done	prepsani souboru pri uploadu- prikaz APPE (reseni: pred uploadem hodit dotaz SIZE a pokud server vrati: 550 File not found, pak
	;muzeme uploadovat. Pokud vrati 213, soubor existuje. 150 conneection accepted, 
	;uspech uploadu:
	;(000010)16.7.2022 5:37:54 - hood (192.168.0.110)> 150 Connection accepted
	;(000010)16.7.2022 5:38:07 - hood (192.168.0.110)> 226 Transfer OK


	;(000021)18.7.2022 5:44:41 - hood (192.168.0.113)> 227 Entering Passive Mode (192,168,0,103,213,139)
	;(000021)18.7.2022 5:44:44 - hood (192.168.0.113)> APPE 16kb
	;(000021)18.7.2022 5:44:44 - hood (192.168.0.113)> 150 Connection accepted, restarting at offset 16384
;done	do uploadu dát kontrolu odpovědí FTPka
;done	pridat volbu URL!!!

;info- test rychlosti uploadu: prakticky totozneho casu bylo dosazeno pri zakladni rychlosti CPU 3,5MHz a 4x nasobne rychlosti 14MHz. Server je nastaven na max. Takze to brzdi modul. Mohli bychom zkusit jiny mod prenosu pomoci AT prikazu, ale az nakonec, az bude i download pripadne prace s adresarem.



;ZX FTP client for MB03+
;Hood

;- ver 0.1	- login to ftp server (login+ pasword), done
;		- find out file size, done
;		- quit ftp connection, done
;		- download file, in process
;		- upload file, done

;	output "zxdb_downloader.bin"

;set C=		SCF
;set NC=	AND A, if Z needs to be preserved, then SCF:CCF



;FTP commands of interest:
;done	USER		username
;done	PASS		password
;done	PASV		enter passive mode
;done	QUIT		quit ftp connection
;done	STOR		upload file		;max transfer block allowed by ESP modele= 2048 bytes, top speed is 2,53 kb/sec
;done	RETR		download file
;done	SIZE		return file size
;done	ABOR		abort command		;in case we got stuck, ABOR will return us
;done	APPE		append file		;if uploaded file exist, you may choose to overwrite it or append
;done	NOOP					;check if ftp connection is alive (ver 0.7.)
;done	STOU					;new from 0.8 up
;done	LIST
;done	NLST
;done	PWD
;done	CWD
;done	CDUP
;done	DELE
;done	MKD
;done	RMD
;done	RNFR+RNTO

;the following need passive mode:
;	RETR		download file
;	STOR		upload file
;	STOU		like STOR, but a new file is set
;	LIST		download directory list
;	NLST argument	list of directory "argument" and file names (without attributes)

;the following do not need passive mode:
;	MKD argument	make new directory called "argument"
;	RMD argument	remove directory called "argument"
;	RNFR+RNTO	rename file
;	SIZE		find out file length
;	QUIT		quits ftp connection
;	PWD		returns current directory name
;	CWD argument	go to directory "argument"
;	CDUP		go to up directory
;	DELE argument	delete file "argument"

;overview:
;LOW LEVEL- SENDING
;1) send_string:	mainly to send direct AT commands
;2) send_data:		to send any type of data
;3) send_string_busy	same as send tring but check also for busy
;LOW LEVEL- RECEIVING
;6) receive_data_to_RXbuf
;7) receive_data_timeout receive data with timeout.IN=BC=max.length of data to receive, HL=sata start, OUT Z= ok, received
;LOW LEVEL- PARSING
;10) look_for_string:	seeks certain string in memory
;11) look_on_fly:	seeks certain string in the incoming flow of bytes
;12) search_by_Busy	seeks string in memory, according to Busy (in BSDOS)
;13) search_until_zero

;OTHERS
;15) count_com_len	counts command length
;16) ConvRStr16		converts ASCII to HL (16 bit)
;17) HL2ASCII		converts HL to ASCII (16 bit)
;18) wait routines	wait certain amount of time (user definable)
;19) print_title	printle initial title
;20) save_dir_position	saves dir position so that you can return to where you have been
;22) ConvrStr32		converts ASCII to HLDE (32 bit)
;28) HL2ASCII32		converts HL to ASCII (32 bit)
;29) send_at_check_ok	sends AT command and checks for answer OK
;40) set_print_speed	sets speed on MB UART and print it
;48) convASCII2hex	convert eg. "14" to #14
;49) cls_4000		clear screen
;50) cls_9		clear 9 lines
;;;;51) prn_ap_name_sig	print AP name and signal strength, not used!!!!!

;53) set_def_wifi_speed	sets both speeds to default wifi speed
;54) clear_edit_zone	clears 2 bottom lines
;55) akey		waits for key press
;56) wifi_wait/unwait	signals, that we wait for wifi
;57) set_ud_speed	sets user defined speed (0-23 from IX+2), mainly for SM2.0


;FTP RELATED
;52) get_status_ftp	gets status, if connected to ftp server or not
;58) send_abort		sends ABORt command
;58) send_ftp_command	sends any ftp command placed in HL
;59) cipsend_request	sends data to server, command eg. "AT+CIPSEND=0,6"
;60) check_module_answercheck if module answers string in HL (used for AT+CIPSEND (HL=char_zob), AT+CIPSTART="TCP"...HL="CONNECT"
;61) receive_ftp_answer
;65) enter_pasive_mode
;73) print_ftp_status	prints status of URL and login
;74) send_noop		send "noop" command
;75) print_url		prints selected url
;76) print_transfer_status print Transfer OK, or error, or aborted
;77) passive_receive_offturn off passive recieve mode
;78) print_ftp_message	prints ftp message on the screen
;79) connect_to_ftp_server
;80) test_ftp_connectionfor login and auto login purposes


;62) clears RX and TX buffers, or only one of them
;63) error_handler	universal routine to deal with errors
;64) cipstart_connect	sends command like: "AT+CIPSTART=1,"TCP","h1,h2,h3,h4",port"
;65) connect_to_server	connect to server+ test status
;66) input_line		inputs line for user to enter anything
;68) cipsend_lod	sends purely AT+CIPSEND=1,data length
;69) display_progress	prints length of file each time file is loaded/saved
;70) look_for_ipd	looks for IPDs in the incoming bytes
;81) set_3_5mhz
;82) set_20mhz
;83) send_ftp_command_in_pasive_mode	used in download file and retrieve dir list
;84) data_available_in_module			;find out, if module has available data,
;85) ask_for_data_in_passive_mode		;send "AT+CIPRECVDATA=1,2048" in passive receive mode
;86) ask_for_reply_in_passive_mode		;send "AT+CIPRECVDATA=1,128" in pasive receive mode
;87) passive_receive_on	;trun on passive receive mode
;88) print_working_directory
;89) get_item_name	;get item name under cursor
;90) print_dir_files_items; prints number of files and dirs
;91) search_marked_items;go through directory and search for marked items
;92) un_mark_all	;mark or unmark all items
;93) send_type_command
;94) print_whole_screen	;prints screen at the start or after .screen files displayed

;	DEFINE border_colour	;0=no colours,1=show border colours, yellow for disk operations,
				;violet= communication with module
/*podoba screenu
0	nadpis
1	nadpis
2	Speed: max   Logged in: ok  Status: 3
3	Host: hood.speccy.cz123456789012345678901234567890123456789012	
4	\
5	> navratove zpravy z ftp serveru
6	/
7
8
9
10
11
12
13
14
15
16
17	file:0123456789                         start: 49152
18	host: 192.168.0.103        user: hood     pass: test
19	F-file T-start A-YES H-host R-usr ss+/P-pass U-upl S-size Q-quit 
20	W-wifi OFF ss+/L-auto/login O-logoff V-save app ss+/D-dwnld/file 
22	input txt/action (např. Downloading file)......     Progress: xy
23	input					  error= ..press a key..	
*/

;text address on the screen
txt_adr_cwd		equ #4000
txt_adr_header		equ #4003
txt_adr_header_line	equ #4103
txt_adr_speed_login	equ #4200
txt_adr_host		equ #4300
txt_adr_ftp_reports	equ #4600
txt_adr_mezera		equ #4700
txt_adr_dir_display	equ #4800
txt_adr_module_send	equ #4800	;exist in code but disabled
txt_adr_dir_info	equ #5000
txt_adr_file		equ #5100
txt_adr_hostvar		equ #5200
txt_adr_menu1		equ #5300
txt_adr_menu2		equ #5400
txt_adr_meziradek	equ #5500
txt_adr_edit_zone1	equ #5600
txt_adr_progress	equ txt_adr_edit_zone1+40
txt_adr_edit_zone2	equ #5700



	
main_port	equ #203B
wifi_port	equ #213b
basic_end	equ #6000
terminal_start	equ 30000
start_address	equ #C800
stack		equ start_address
;AP_list_start	equ terminal_start+terminal_len	;start of AP list in the memory goes right after terminal
upload_length	equ #800	;serves for upload routine (#800 is max, #400 work also)
download_length	equ #800
line_length	equ #40		;length of line (for standart ZX=#40, for hires= different)
mark_cursor	equ 58		;marked item with cursor
mark_no_cursor	equ 2		;marked item without cursor
unmark_cursor	equ 56		;not marked item with cursor
unmark_no_curs	equ 7		;not marked item without cursor
print_buffer	equ #5B00

;bios		equ	#FD00
bios		equ	bios_start
gver_inst	equ	bios+0
wifi_on		equ	bios+4
wifi_off	equ	bios+6
set_baud	equ	bios+8
get_baud	equ	bios+10
clear_rece_buf	equ	bios+12
rece_byte	equ	bios+16
send_byte	equ	bios+18
rece_block	equ	bios+20
send_block	equ	bios+22

number_of_servers	equ	4	;change it with every new server (servers are 0-3)
port_select	equ	#703B

;	DEFINE	bs_dos
;	UNDEFINE bs_dos
;bs_dos		equ	1	;1=compile for BS DOS, 0= compile for Esxdos<--- defined in upper .ASM file


;Error reports
	;1=sending string error (cipsend_request routine)			...OK
	;2=error sending CWMODE AT commad					...OK
	;3=error sending CIPMUX AT command					...OK
	;7=error sending CIPCLOSE AT command (ftp upload)			...OK


	;8= module did not return SEND OK after data block sending (ftp_upl_send)..OK
	;9= error in entering passive mode (enter_passive_mode)			...OK
	;14=file not exist							...OK
	;15=disk error								...OK
	;25= module not connected to AP, the user has to set his module externally.OK

;ftp	;4= ftp answer, requested ftp code not found				...OK
	;old,not valid;4= check_module_answer routine did not found the answer from DE	...OK
	;5= uploaded data not sent correctly (ftp_upl_send)			...OK
	;6= cipsend_lod routine error. Module not ready to send data for upload	...OK
	;10= error in connecting to ftp server (ftp_login)(probably wrong URL)	...OK
	;11= error in receiving ftp answer to AT+CIPSEND "<" (receive_ftp_answer)..OK
	;12= "+IPD," string not found in the incoming bytes (look_for_ipd)	...OK
	;13= FTP server did not confirm answer 225 on download attempt (ftp_download_file, or retrieve directory...OK
	;16= ABOR command not successful					...OK
	;17= upload: connection not accepted (code 150 not received)		...OK
	;18= upload: transfer not ok (code 226 not accepted)			...OK
	;19= initial check. Connected to server, not good answer to NOOP cmnd	...OK
	;20= ABOR command initiated by the user					...OK
	;21= download: files does not exist					...OK
	;22= file does not exist on ftp server					...OK
	;23= download: block of data receive error				...OK
	;24= server did not respond with "226 Transfer OK"			...OK
	;26= no data available in passive receive mode (routine 84)		...OK
	;27= command for create dir not accepted (create dir)

	;28=speed management: AT command not accepted						... present in the code but not used
	;29=speed management: all speeds tested, timeout, fix it in terminal-add it in the next ver... present in the code but not used
	;30=speed management: failed, probably Wifi module wrongly set,fix in terminal-add it in the next ver... present in the code but not used
	;31=user defined speed_ AT+UART_CUR command not sent			...???
	;32=user defined routine_ speed in UART not set				...???
	;33=user defined speed_ you try to set nonsence speed (ie. > 23)	...???
	;34= dir retrieve error
	;35= (look_for_ipd) 2nd error
	;36= error in receive_ftp_answer
	;37= send_ftp_command_in_passive_mode
	;38= error sending TYPE command
	;39= error in sending AT+CIPRECVMODE=0(1), (passive_receive_switch)	...OK
	;40= error in sending command in ftp_quit_connectio			...OK
	;41= error in sending command in enter_passive_mode			...OK
	;42= error in sending command in send_noop				...OK
	;43= channel to sclosed sucessfully					...OK


		IFDEF flash

		org	start_address-init_len-init_ldir_len

start_flash	di
		ld	hl,#F					;the only hard address!!!
		ld	de,start_address-init_len
		ld	bc,total_len+init_len
		ldir
		jp	start_address-init_len
init_ldir_len	equ	$-start_flash


start_init
/*
			xor a
			ld bc,#703B	;ZXi_port0				//ZXi portselect
			out (c),a
			ld bc,#713B	;ZXi_port1
			ld a,255					//HW - FF - Firmware
			out (c),a
			xor a	
			out (#53),a
			out (#27),a
			out (#67),a
			out (#07),a
			inc a
			out (#47),a
			
			xor a
//			out (23),a
			ld bc,#703B	;ZXi_port0				//ZXi portselect
			out (c),a
			ld bc,#713B	;ZXi_port1
			ld a,3					//HW - 3 - DIVMMC/DIVIDE
			out (c),a

			ld bc,#703B	;ZXi_port0				//ZXi portselect 
			ld a,2
			out (c),a
			ld bc,#713B	;ZXi_port1
			ld a,2					//IDE - 2 PVL ports- DIVMMC/DIVIDE
			out (c),a

			ld bc,#703B	;ZXi_port0				//ZXi portselect 
			ld a,1					//Audio
			out (c),a
			ld bc,#713B	;ZXi_port1
			ld a,%11101111  		//dissable GS
			out (c),a

			ld a,128
div_erase2	out (#E3),a
			ld hl,8192
			ld de,8193
			ld bc,8191
			ld (hl),l
			ldir
			inc a
			cp 128+5
			jr nz,div_erase2			//delete first 32KB od DivIde RAM
			xor a						//is enought to erase bank0 or
			out (#E3),a					//to force cold start zero $2dXX in page 0
;;			rst 0
			jp #66
*/

;old bad
			xor a
			ld bc,#703B	;ZXi_port0				//ZXi portselect
			out (c),a
			inc b
			ld a,#FF					//HW - FF - Firmware
			out (c),a
			xor a	
			out (#53),a
			out (#27),a
			out (#67),a
			out (#07),a
			inc a
			out (#47),a

			xor a
			ld bc,#703B	;ZXi_port0				//ZXi portselect
			out (c),a
			ld bc,#713B	;ZXi_port1
			ld a,2					//HW - 2 - DIVMMC
			out (c),a
			ld bc,#703B	;ZXi_port0				//ZXi portselect 
			ld a,2
			out (c),a
			ld bc,#713B	;ZXi_port1
			xor a					//IDE - 0 dissable
			out (c),a			
			out (#E3),a			//to force cold start zero $2dXX in page 0
;			jp #66		
;			jp start_ldir


init_len	equ	$-start_init	

		ENDIF

;;;	org 49152
		org	start_address		

start_ldir	di

;		ld	a,"H"			;ENABLE private server;this POKE is for en/disabling private server- 1= disable, "H"= enable
		ld	a,1			;DISABLE private server
		ld	(connectserver1),a
		ld	a,0
		ld	(server_flag),a		;POKE starting server (0-3 only)

		ld	sp,basic_end
		ld	hl,error_handler_loop	;HL= address, where error handler jumps after an error occurs
		push	hl

/*
code_		ld	hl,bios_start		;redundant. This ldir rewrites itself.
		ld	de,bios
		ld	bc,bios_len
		ldir
*/
		call	bios

		ld	ix,flag1

		ld	a,e			;find out which wifi is running and write it to the headings
		or	a
		ld	a,"M"
		set	2,(ix+0)		;set flag, we are on MB wifi
		jr	nz,s_ldir

		res	2,(ix+0)		;res flag, we are on el wifi
		ld	a,"E"
s_ldir		ld	(wifi_type),a

		xor	a
		out	(254),a


		ld	bc,#783B		;read out CPU speed and save it later
		ld	a,#30
		out	(c),a
		inc	b
		in	a,(c)			;speed is 0-3
		ld	(return_speed+1),a
		ld	a,0			;speed will be 3,5 MHz
		out	(c),a
;		ld 	a,1			;activate conevnient 7 Mhz, but not at the moment, we need test
;		out	(c),a			;at normal speed

	
;		bit	2,(ix+0)		;test which wifi is running. MB or el?... not needed, the below should work by itself


		and	a			;test if Wifi is on/off
		call	wifi_on
		set	3,(ix+0)
		jr	nz,st1			;Z=off, NZ=on
		res	3,(ix+0)
;		ld	(ix+2),#FF		;#FF=initial speed will be set to max, SM2.0


;flag1 (IX+0),bit 7=used in "HL2ASCII"
;bit 0=used in "look_for_string"
;bit 1=login to server,0=automatic, 1=manual
;USED bit 2= runs mb wifi?,0= no, 1=yes
;USED bit 3=wifi on exit, 0=off,1=on
;USED bit 4=print info in speed management? 0=no, 1=yes
;bit 5=ftp login status, 0= not connected to host, 1= connected to host
;USED bit 6=speed correctly set (module and UART), 0= no, 1= yes
;bit 7=AP list, 0=not complete, 1= complete
;(IX+1)= counter of AP in memory
;flag2 (IX+3).bit 0=print send command,0=NO, 1=YES
;USED bit 1= delete TAP after extracting, 0=NO, 1=YES
;USED bit 2= we download TXT or POK, 0=NO, 1=YES
;USED bit 3= show PRESS A KEY after TAP download, 0=NO, 1=YES
;USED bit 4=search or download, 0= search, 1= download
;not used, oldbit 5 not in use other method than "/v2" does not work; =search method without, or with "/v2", 0=without, 1=with
;USED bit 6= download file or screen?, 0=file, 1=screen
;USED bit 7= we go up or down (right/left arrow), 0=up, 1=down
						

st1
main		bit	0,(ix+3)
		ld	hl,send_string+1
		ld	a,18			;jr ss5= do not print what you send to module
		jr	z,printyesno1		;Z=do not print
		xor	a			;jr ss2= print it
printyesno1	ld	(hl),a


;		call	cls_4000
		set	4,(ix+0)		;set flag to print info in speed management
		call	print_whole_screen

		call	speed_management
;		call	print_whole_screen+3	;bez CLS (aby se nesmazalo info o Speed)

;		call	print_title
;
;		ld	de,menu1
;		ld	hl,txt_adr_menu1
;		call	PRINTHL			;print menu line 1
;
;		call	delete_tap_prn		;print if TAP should be deleted or not

;	IF bs_dos=0
	IFNDEF bs_dos
		call	GetSetDrive
	ENDIF


		ld	hl,start_address	;0.22,count buffer length in bytes. Flexible.
		ld	de,basic_end		;do not have to care about buffer length anymore
		or	a
		sbc	hl,de
		ld	a,h
		and	%11111100		;to obtain *2048
		ld	h,a
		ld	l,0			;HL=maximum length of disk buffer in bytes
		ld	(disk_buf_len),hl	;DE=disk buffer start, "end"

		xor	a
		ld	(item_checksum),hl	;signal dir start from begining

		ld	hl,#80
		ld	(rd_timeout+1),hl	;set receive byte to very low time

		ld	hl,at_cwmode
		call	send_string		;set module to station mode (CWMODE=1)
		ld	hl,ok
		call	look_on_fly
;		jr	z,error_handler_loop	;kontroly v inicializacni casti vyhod pryc
;		ld	c,2
;		jp	error_handler

		ld	hl,at_cipclose5		;we have to clear possible previous connections
		call	send_string
;		ld	hl,ok
;		call	look_on_fly		;was too much waiting

		ld	hl,at_cipmux		;and set single connection (+checks if commands carried out successfuly)
		call	send_string

error_handler_loop				;this is starting point, where routine jumps after error occurs
		ld	sp,basic_end-2
		set	4,(ix+0)		;print info in speed management= YES
		ld	(ix+6),8		;timeout for module check


/*
kl5		call	get_status_ftp		;find out if already connected to ftp server
		jr	nz,kl10
;		jr	z,key_loop		;Z=connected+ logged in
		bit	0,(ix+1)		;dir already read in?
		jp	z,directory_retrieve
		jr	key_loop

kl10		cp	"5"
		jr	nz,kl3			;NZ=status "2" or "4" or "3"
;		jr	nz,kl4
		dec	(ix+6)			;status= "5" module not connected to AP, try it out 20x
		jr	nz,kl5
		ld	c,25			;error 25 module not connected to AP, the user has to set his module externally
		jp	error_handler

kl3		cp	"3"
;		jr	z,kl_connected		;Z=status 3 (connected to server but not logged in)
		jp	z,al_user_pass		;make auto login with USER and PASS

						;NZ=status "2" or "4"- continue to CIPMUX
kl4		ld	hl,at_cipmux		;!!!!!!!!we re not connected to ftp server
		call	send_string
		ld	hl,ok
		call	look_on_fly
;		jr	z,key_loop
		ld	c,3
		jp	nz,error_handler

kl_alogin	bit	1,(ix+0)		;carry out autologin? ;bit 1=login to server,0=automatic, 1=manual
		jp	z,auto_login
;		jp	ftp_login

*/

/*
;		ld	hl,ok
;		push	hl
;		call	look_on_fly		;osetrit odpovedi!!!!!
*/

;		pop	hl
;		call	look_on_fly


;		call	connect_to_zxdb_server
		call	get_status_ftp		;to print server on the screen

		ld	a,(INPBUFF)		;dali jsme to primo do search_file, nutno vyzkouset
		or	a
;		jp	nz,pts3			;pokud je uz neco v INPBUFF, skoc na search file
;		call	nz,pts3			;pts3 kopiruje nazev z print bufferu do input bufferu
		call	nz,pts5			;pts5=input buffer, then jump to search file
	
;;		call	connect_to_server	;toto tu (asi) byt nemusi, je to soucasti search_file
;;		jp	nz,error_handler

		bit	0,(ix+1)
		call	z,search_file		;if no dir read, start program with search option, for easy of use

key_loop	call	KEYSCAN
;		cp	"u"
;		call	z,ftp_upload_file
;		cp	"o"
;		call	z,ftp_quit_connection
;		cp	"l"
kl2;		call	z,ftp_login
		cp	"s"
		call	z,search_file
;		cp	"D"
;		call	z,ftp_download_file_new
;		cp	#5C
;;		cp	"\"			;SS+D
;		call	z,ftp_download_file_var
		cp	"w"
		call	z,set_onoff
		cp	"q"
		jp	z,quit_program
		cp	"h"
		call	z,change_server
;		call	z,change_host
;		cp	"v"
;		call	z,save_app
;		cp	"a"
;		call	z,set_alogin
;		cp	"f"
;		call	z,set_filename
;		cp	"t"
;		call	z,set_start
		cp	"e"
;;		call	z,set_user
		call	z,delete_tap
;		cp	"p"
;		call	z,set_pass
;		cp	"="			;SS+L
;		call	z,auto_login
;		cp	#22			;SS+P
;		call	z,set_hide_pass
;		cp	"n"
;		call	z,set_list_nlst
;		cp	","			;SS+n
;		call	z,set_full_abbr
;		cp	"r"
;		call	z,directory_retrieve

		bit	0,(ix+1)		;dir already read?
		jr	z,kl11
;;;vratit		jr	z,kl11			;Z=not read, skip some keys

		cp	#B			;up. Move through directory
		call	z,key_up
		cp	#A			;down
		call	z,key_down
		cp	#9
		call	z,pg_up			;page up
		cp	#8
		call	z,pg_down		;page down
		cp	#D
		call	z,download_file_and_run
;		call	z,enter_directory
		cp	"d"
		call	z,download_file
;		cp	1			;BREAK to go to updir directly
;		call	z,go_updir
;		cp	"7"
;		call	z,create_dir
;		cp	#C			;DELETE key
;		call	z,delete_item_file
;		cp	"6"
;		call	z,rename_item_file
		cp	"x"
;		jp	z,roll_item		;no CALL, we return with JP from roll_item
		call	z,roll_item
;		cp	#20			;SPACE key
;		call	z,mark_item
;		cp	"M"			;CS+M=mark all
;		call	z,mark_all
;		cp	"."
;		call	z,unmark_all		;SS+M=unmark all
;		cp	"i"
;		jp	z,find_items
;		call	z,find_items

kl11;		cp	"c"
;		call	z,enter_command
		bit	5,(ix+0)
		jr	z,kl6			;Z=not logged in, do not send NOOP command

;časovač pro element, ktery nema RTC. Ale je jednotyn i pro MB03+. To u zted tedy RTC nepouziva,
noop_timer	ld	hl,#4000		;send NOOP command every 1 minute, read it from MBC RTC chip
		dec	hl			;read RTC only every 65535th cycle, means roughly 50 secs
		ld	(noop_timer+1),hl	;#4000= 13 secs cca (#8000= 25 secs. etc.)
		ld	a,h			;#2000= 6 secs
		or	l
		jr	nz,kl6
		ld	h,#40
		ld	(noop_timer+1),hl	;dej zase #4000 do pocitadla
		call	get_status_ftp		;volej za #2000 cyklu
;*/

/*****staré funkční přes RTC MB03+, ale kod je delsi a jen pro MB03+, schovat, ale nepouzivat
;valid for MB03+ only
;		ld a,1
		xor a
		ld bc,#703B			;ZXi_port0
		out (c),a
		ld bc,#773B			;ZXi_port7
		in a,(c)
		and %11110000
;		push af			
;		and #0F
;		or 48

		cp	(ix+24)			//1min
		jr	z,kl6
		ld	(ix+24),a
		call	get_status_ftp
;		call	send_noop
;		ld	hl,0
;		ld	(noop_timer+1),hl
obnovit nazpět********/


kl6		and	a			;check module, if it brings any data. Yes? Print them for the moment.
		call	rece_byte
		jr	key_loop
;		jr	z,key_loop
;		jp	z,key_loop


/*
kl8		ld	hl,RX_buf		;data avialable, accept them
		ld	bc,buffers_len/2	;ZXDB-here comes CLOSED, not really needed
		push	hl
		call	receive_data_timeout
		pop	hl			;omit IPDs....etc
		jr	kl9
*/
;not needed for ZXDB
/*
		ld	de,dvojt		;if code 421 returned check connection
		call	look_for_string
		jr	nz,kl9
;		jp	nz,key_loop

		push	de
		ld	h,d
		ld	l,e
		call	print_ftp_message

		pop	de
		call	ConvRStr16
		ld	de,421
		or	a
		sbc	hl,de
		jp	z,error_handler_loop	;code is 421, check connection
*/
/*
kl9		xor	a			;A=0 to return to key_loop
		jp	key_loop
*/






;speed management 2.0. starts here. Enables to set user_defined speed. Works with every MBFW.
;the principle:
;are you aligned?
;NO: try to align, if not success->error. If sucess, you have AKT (=current alligned speed is ;known). Is user defined speed definied? (IX+2=255 for no user defined speed) yes, END. NO. set ;speed according to ix+2 (max up to MBFW), end
;
;YES:
;is user defined speed set? (ix+2=255 or other?) NO=check if FW is 7/12? YES, align at speed
;ix+2, end. FW is higher then 7/12, then check if AKT=IX+2? YES, end, NO, align at ix+2, END.
;ix+2 was not 255, is FW 7/12? YES, then set FWmax, end. NO the is AKT=MBFW max? YES, then END
;NO, then align at FW max speed, end
;output: ix+7= 0-23 if speed set, or 255 if not set

;SM20 uses:
;bit 4,(ix+0)	PRINT or not to PRINT
;(ix+8)		contains maximum speed by detected MBFW (7, 12 or 23)
;(ix+6)		timeout value
;bit 6,(ix+0)	state of speed alignment between uart& module, 0 not aligned
;bit 5,(ix+0)	state of module connection to AP, 0 not connected to AP

;internal subroutines: switch_speed, print_bauds, find_out_speed
;external subroutines: send_at:check_ok, set_ud_speed, set_print_speed, print_speed

speed_management
		ld	(ix+7),#FF		;reset currently set speed status
		res	6,(ix+0)		;set signal UART and module speed not aligned

;;		ld	hl,#4300
;;		ld	de,wifi_on_off
;;		call	PRINTHL

		scf				;power on wifi
		call	wifi_on


		ld	hl,txt_adr_speed_login	;print "Speed" text
		ld	de,speed
		call	PRINTHL

		ld	bc,#783B		;set CPU to 3,5 MHz
		ld	a,#30
		out	(c),a
		inc	b
		xor	a
		out	(c),a

		push	ix
		call	gver_inst
		pop	ix			;A= max MBFW speed= 7 or 12 or 23
		ld	(ix+8),a		;save max possible speed for given FW

;		ld	a,e
;		or	a
;		jr	nz,sm1
;						;NZ=MB wifi, Z=el wifi
;		res	2,(ix+0)
;		jr	sm2
;
;sm1		set	2,(ix+0)		;test which wifi is running, mb, or el?

sm2		ld	(ix+6),1		;timeout for quick AT check, 1 should be ok.
		call	send_at_check_ok
		jr	z,speed_aligned		;Z= speed aligned (UART<-> module)

						;speed not aligned
		call	switch_speed		;try to find out aligned speed
		jr	z,align_success
;		pop	hl
		ld	bc,29			;solve in next versions
;		jr	nz,speed_uart_err

speed_uart_err	call	print_bauds		;print speed in bauds even if speed no communicate

		ld	hl,txt_adr_edit_zone1
		ld	de,module_no_aswer
		call	PRINTHL
;		ld	bc,30
	
speed_m_error	res	5,(ix+0)		;set signal wifi not connected to AP
;		jp	error	
sme1		call	akey
		call	undraw_cursor		;return from AP selection
		call	wind_cls
		call	clear_edit_zone
		ret
;zde konci vetev, kdy na pocatku SM2.0 nebylo nalezeno slazeni rychlosti

;zde jsme ze zacatku rychlost nasli a zkoumame, zda je to ta, kterou chceme, pokud ne, tak ji nastav
speed_aligned	call	find_out_speed		;output: D= currently aligned speed
align_success	call	set_ud_speed

;ve speed_we_want vyhodnotit, zda je odezva na AT prikaz. Pokud ne, neznamená to, že
;rychlosti nebyly slazeny, ale ze nektere z rychlosti 0-23 nekomunikuji s modulem.
speed_we_want	ld	(ix+6),#A
		call	send_at_check_ok
		jr	nz,speed_uart_err

wifi_cont					;we are at speed we want. Final steps... print UART speed+ Baud speed
		set	6,(ix+0)		;signal, that uart and module speed is aligned

;		call	get_baud
		call	find_out_speed
		ld	a,d			;A=aligned speed we ended with
		ld	(ix+7),a		;save it
		call	print_speed
		call	print_bauds

		ld	hl,ate0
		call	send_string
		ld	hl,ok
		jp	look_on_fly
;		call	look_on_fly
		;test for answer should be here?
;		ret		

;SM2.0 subroutines
find_out_speed					;subroutine is able to find out current speed for all MBFW, output in D
		call	get_baud		;we are at aligned speed, is the one we want?
		jr	nc,fos1			;C=speed reading not supported by given MBFW
		call	switch_speed		;in case of MBFW 7, 12-> find out currently aligned speed
		ld	a,d
fos1		ld	d,a			;D=curently aligned speed
		ret

						;tests speeds <7-0>, then <max-8>
;switch_speed	ld	a,7			;start with speeds 7-0 (quicker finds the speed)
;		call	sws2
;		ret	z			;Z= speed <0,7> found

switch_speed	ld	a,(ix+8)		;try out every speed if you find connection UART+Wifi
						;start with max speed, end with 0
sws2		push	af
		call	set_print_speed

		call	clear_rece_buf		;very important to clear receive buffer!!!

		pop	af			;we need to inrease timeout for speeds <3
		push	af
		cp	3
		ld	(ix+6),1		;set timeout for 1 for speeds >3
		jr	nc,sws1
		ld	(ix+6),#A		;set tieout for #A for speeds <2

sws1		call	send_at_check_ok
		pop	de
		ret	z			;Z=answer found, D= speed set in HW
		ld	a,d
		dec	a
		cp	255			;test for 255, so that also speed 0 is tested
		jr	nz,sws2
		dec	a			;NZ=not successfull
		ret

print_bauds	ret
/*
print_bauds	call	get_baud		;print speed in Bauds...solved in bios 1.8
		push	de			;E=number of zeros. If MBFW=7 or 12, zeros speed in Bd is printed
		ld	de,block_asci
		ld	a,"="
		ld	(de),a
		push	de			;DE=start of speed to print
		inc	de
		ld	a,32
		ld	(de),a
		inc	de
		ld	h,b
		ld	l,c
		call	HL2ASCII
		pop	hl			;HL=start of speed to print
		pop	bc			;C=number of zeros
		ld	b,c
		ld	a,"0"
prbloop		ld	(de),a
		inc	de
		djnz	prbloop
		xor	a
 		ld	(de),a
		ld	d,h
		ld	e,l
		ld	hl,#4400+9+8
		call	PRINTHL
		ld	de,Bd
		call	PRINT

		ld	de,mbfw			;print max UART speed supported by found MB FW
		call	PRINT
		ld	l,(ix+8)
		ld	h,0
		ld	de,block_asci
		push	de
		call	HL2ASCII
		ld	a,h
		ld	(de),a
		pop	de
		call	PRINT
		ld	de,spaces+27
		jp	PRINT
*/
;end of speed management 2.0 here


		



;save application with its settings

/*not used, not needed
save_app	
;	IF bs_dos=0
	IFNDEF bs_dos
		call	tapeout_open
       ld      hl,prikaz	;BASIC save according to Busy
        ld      (#5C5D),hl
	ld	iy,#5C3A
	ld	(iy+#7C),#6F	;disable tape message+ key waiting
	ld	(iy+#7E),#14
	ei
        xor     a
        call      #60B

	ENDIF

;Pokial chces autostart iny ako riadok 10, tak namiesto 10 napis potrebne cislo. Pokial nechces autostart vobec, tak cely ten jeden riadok kde je 10 jednoducho zmaz.
;Jaj, este som zabudol. Namiesto toho DS #10 mozes samozrejme dat akykolvek svoj kod. Tam ide len o to, aby nasledujuce kody CA,B0,.... boli vzdialene presne 16 bajtov od posledneho #0D za menom. Vyzaduje to tak romka.
		xor	a
		ld	de,17
		ld	ix,save_app_head
		ld	hl,total_len+8
		ld	(save_app_len),hl
		push	hl
		call	#4C2
		pop	de		;DE=total_len+8
		ld	a,#FF
		ld	ix,start_address
		call	#4C2
;	IF bs_dos=0
	IFNDEF bs_dos
		call	tapeout_close
	ENDIF
sa1		jp	fdfm3		;set iX to flag1 and RET

;	IF bs_dos=0
	IFNDEF bs_dos
prikaz  db      '"Meno"',#0D
        ds      #10
        db      #CA,#B0,'"10"'  ;; LINE VAL "10"
        db      #0D
	ENDIF
*/

/*old working, but long
save_app	ld	hl,zxftpcl
		ld	de,exp_name
		ld	bc,10
		ldir
		ld	hl,0
		ld	(blen+2),hl
		ld	hl,total_len+8		;???? have to add 8 bytes to reach the same length as original, why?
		ld	(blen),hl
		ld	(flen),hl
		ld	hl,start_address
		ld	(sfh+1),hl
		ld	(sif1+2),hl
		call	save_file_head
		call	save_into_file
		ld	hl,basic_end
		ld	(sif1+2),hl
		jp	save_close
*/


;tisk textu, umi jen page up/down
print_txt_main	ld	hl,print_buffer		;#5B00
		ld	(page_pointer),hl
		ld	de,basic_end		;#6000	;init; text zacina na #6000
		ld	(DECKO+1),de		;ZDROJ
;		ld	hl,page_pointer

print_txt	ld	(hl),e
		inc	hl
		ld	(hl),d

pr1		call	cls_4000
		call	tisk_stranky		;pokud A=0, slo o posledni stranku

		or	a
		ex	af,af'			;uloz. Z=posledni stranka

prn_txt_loop	ld	a,1
		out	(254),a
		call	KEYSCAN
		ld	hl,(page_pointer)
		ld	de,(DECKO+1)
		cp	#9
		jr	z,txt_pg_down
		cp	#8
		jr	z,txt_pg_up
		or	a
		jr	z,prn_txt_loop
		res	2,(ix+3)		;reset txt view flag
		ret				;any other key=return

txt_pg_up	push	hl
		ld	c,(hl)
		inc	hl
		ld	b,(hl)
		or	a
		ld	hl,#6000
		sbc	hl,bc
		pop	hl
		jr	z,prn_txt_loop

		dec	hl
		ld	d,(hl)
		dec	hl
;		push	hl
		ld	e,(hl)

		ld	(page_pointer),hl

;		dec	hl

;		dec	hl			;decrease pointer
;		dec	hl
		ld	(DECKO+1),de
		jr	pr1
;		jr	print_txt

txt_pg_down	ex	af,af'
		jr	z,prn_txt_loop		;Z=last page, no more possibility to page down

		inc	hl
		inc	hl
		ld	(page_pointer),hl
;		ld	de,(DECKO+1)
;		ld	(hl),e
;		inc	hl
;		ld	(hl),d
		jr	print_txt



page_pointer	dw	#5555			;default je #5B00


;pokusne, pridavame tisk stranky, jen pro ladeni emulu
tisk_stranky;	ld	hl,basic_end		;#60000	;init
;		ld	(DECKO+1),hl		;ZDROJ
		ld	hl,txt_adr_cwd		;#4000
		ld	(HLKO+1),hl		;POS

ts2		ld	a,(HLKO+2)
		cp	#58
		ret	nc			;end of page, end

		ld	hl,(DECKO+1)
		ld	a,(hl)
		inc	hl
		ld	b,(hl)

		or	a			;0=end of text
		ret	z

		add	a,b			;HL=ZDROJ
		cp	#17			;Z=jde o #AD, nebo #DA
		jr	z,yes_cr_lf

		sub	b

		cp	#D			;test first for #D
		jr	z,yes_cr_lf1

		cp	#A			;test first for #A
		jr	z,yes_cr_lf3

		ld	a,b			;test second for #D
		cp	#D
		jr	z,yes_cr_lf2	

		cp	#A			;test second for #A
		jr	z,yes_cr_lf2

no_cr_lf	xor	a
		ld	(HLKO+1),a
		call	PRINTDE			;ZDROJ i POS bere z rutiny pro print
		jr	ts2

yes_cr_lf2	inc	hl
yes_cr_lf1	ld	(DECKO+1),hl
		jr	ts2

yes_cr_lf	inc	hl
yes_cr_lf3	ld	(DECKO+1),hl		;#DA, or #AD
		ld	hl,HLKO+2
		inc	(hl)
		jr	ts2




;general and cumpolsory layers (code parts) of working with WIFI
;***************************************************************
;wifi bios- low level routines communicating with HW (eg. ESP-01)
;connection layer (eg. speed management)- sets communication speed, finds out if wifi is connected to AP
;wifi module communication layer, aka AT commands handling (send& receive bytes/ string, evaluate answer from wifi module, AT commands set, etc.)


;optional layers
;***************
;keyboard routine
;print and input routines
;file system layer
;special layer (FTP handling, HTTP requests and answers, etc.)
;incoming data organisation layer (file list, file load& save, etc...)


;minimal HTTP request
;GET /?s=manic HTTP/1.1 or GET /v2/?s=manic&p=0 HTTP/1.1 receive list of results
;Host: zxdb.remysharp.com

;GET /get/18840 HTTP/1.1 receive file number 18840 (76907, 76908, 25483)
;Host: zxdb.remysharp.com
pg_down		call	print_searched_string
		set	7,(ix+3)	;indicate we go down
		ld	a,(ix+4)
		or	a
		jr	nz,pg_down1
;		ret	z		;Z=we are at zero page, already
		ld	(ix+4),a	;start from page 0
					;if we are at zero page, move server number
		call	close_server
;		ld	hl,at_cipclose		;we have to clear possible previous connections
;		call	send_string
;		ld	hl,closed
;		call	look_on_fly

pg_down2	;call	move_server_number_down	;ensure we want to avoid private server
		call	move_server_number
		call	fs1
		ld	a,(hl)
		cp	1
		jr	nz,sf4
		jr	pg_down2

;		ld	hl,server_flag
;		dec	(hl)
;		jp	p,sf4
;		ld	a,number_of_servers-1
;		ld	(hl),a
;		jr	sf4


pg_down1	dec	(ix+4)
;		jr	pg_up1
		jr	sf4

pg_up		inc	(ix+4)
		res	7,(ix+3)		;indicate we go up
pg_up1		call	print_searched_string
		jr	sf4		;only increase pg number, and jump to search

print_searched_string
		ld	hl,txt_adr_edit_zone2	;print what is in input line
		push	hl
		ld	de,spaces
		call	PRINTHL
		pop	hl
		ld	de,INPBUFF
		jp	PRINTHL

;if method "/v2" not sucessful, try search without "/v2" (eg. games Chemi Chaos, F.I.R.E.)
search_file	ld	hl,#40E0
		ld	b,8*11
		call	cls_9_lines
		call	undraw_cursor

		ld	de,spaces
;		push	de
		ld	hl,txt_adr_menu1+41
		call	PRINTHL

		ld	de,find_txt
		ld	hl,txt_adr_edit_zone1
		call	PRINTHL
;		pop	de
;		call	PRINT

sf7		call	input_line
		jr	nz,sf8
		res	0,(ix+1)

		xor	a
		ld	(INPBUFF),a	;added erasing of buffer, was an error in 0.7 when changing servers

		jp	clear_edit_zone	;Z=abort pressed
sf8		ld	a,c
		ld	(df3+1),a	;length of string
		ld	(ix+4),0	;reset page_counter to 0
;		set	5,(ix+3)	;we start with method "/v2"

;		ld	hl,INPBUFF	;if spaces in searched string, replace them with "*"
		ld	b,c
sf9		ld	a,(hl)
		cp	#20
		jr	nz,sf10
		ld	(hl),"*"
sf10		inc	hl
		djnz	sf9

;here we continue if we need to do page up/down
;and after download, renew dir
sf4		;call	get_status_ftp
;		cp	"3"
;		jr	z,sf3
;		call	connect_to_zxdb_server
;		jr	sf4

		call	connect_to_server
		jp	nz,error_handler

sf3		res	4,(ix+3)		;indicate we do search

		exx
		call	find_server
					;HL=server string, BC=server string length
;		ld	hl,http_search0
;		ld	bc,2			;for zxdb.remysharp.com
;		ld	a,(server_flag)
;		or	a
;		jr	z,sf5
;		ld	hl,http_search1
;		ld	c,10			;for hood.speccy.cz
sf5		exx
;		push	hl		;HL=http argument
;		push	bc		;BC=length of http argument

		call	df1

		call	evaluate_http_answer_no_passive
		or	a
		jr	z,sf2		;all ok
		dec	a
		ld	de,http_400err+4
		jp	z,nf1
		ld	de,error_answ
		jp	nf1

;we start to accept and bend incoming data
;delka polozky: na konci i zacatku vcetne indikacniho bajtu. Tzn napr. 33, 33 bajtu obsahuje 1 bajt info+32 bajtu obsah polozky
;v pameti ulozit takto:
;0,44,0,18836,0,Manic Bugs0ManicBugs0tap05583 1 1987,0,0,44.
;sf2		ld	bc,(disk_buf_len)	;data itself are prepared now fetch them
;		ld	hl,basic_end
;		jp	receive_data_timeout	;raw searched data in memory #6000

;init part
sf2		ld	de,(blen)		;DE=total lenght of data to come, if blen=1 for zxdb or 0 for hood.speccy.cz, then nothing found
		push	de
		ld	a,(server_flag)
;		or	a
		cp	3			;Z=ZXDB.REMYSHARP.COM
		jr	nz,sf11
		dec	de			;id 1 (ZXDB) or 0 (hood.speccy.cz)= nothing found
sf11		ld	a,d
		or	e
		pop	de
;		jr	z,nothing_found

		jr	nz,sf6			;implementation of second search method without "/v2"

;		dec	de
;		or	e
;		jr	nz,sf6			;NZ=was found		

;		jp	nothing_found
;		bit	5,(ix+3)
;		jp	z,nothing_found		;method without "/v2" did not find anything, Show message.
;		res	5,(ix+3)		;set without "/v2" and repeat serach
;		ld	hl,http_search+3
;		ld	bc,3			;method without "/v2" returns bad items, that cannot be loaded
;		jr	sf5		

nothing_found	call	wifi_unwait
		call	KEYSCAN
		cp	"b"
		jr	nz,nf111
		xor	a
		ld	(INPBUFF),a
		jp	error_handler_loop

;		jp	z,error_handler_loop	;connect to current server and invoke search prompt
;		jp	z,nf111
;		jp	z,fdfm10		;Z=end searching, in case nothing is found ZXDB keeps searching in endless loop
nf111		ld	hl,txt_adr_edit_zone2	;print what is in input line
		push	hl
		ld	de,spaces
		call	PRINTHL
		ld	de,not_found
		pop	hl
		call	PRINTHL

		call	move_server_number	;if nothing found,change server number and retry
		ld	(ix+4),0		;reset page counter

		call	close_server

;		ld	hl,at_cipclose		;we have to clear possible previous connections
;		call	send_string
;		ld	hl,closed
;		call	look_on_fly

;		call	check_module_answer
;		jp	c,sfcerr2		;tohle tu dělalo bordel!!!
		jr	sf4			;start again	

;;;		ld	hl,page_counter		;if nothing found, decrease page number
;;;		dec	(hl)			;if page counter= zero, do not decrease it
;;;		ret	p			;P= continue in search


;;		jp	m,nf3			;M=was zero page and hothing found=new search
;		jp	p,nf2			;P=page counter>0=continue this search
;		inc	(hl)
						;here we were at zero already and nothing was found
;		dec	(ix+4)			;if nothing found, decrease page number
;		jp	p,nf2			;if page counter= zero, do not decrease it
;		inc	(ix+4)			

;nf2;		ld	de,not_found
;		ld	a,(page_counter)
;		or	a
;		ld	hl,txt_adr_edit_zone2
;		jr	z,nf1
;		jp	PRINTHL

nf1		ld	hl,txt_adr_edit_zone2	;used by another routine
		call	PRINTHL
nf3		res	0,(ix+1)		;indicate nothing found
		call	akey
		jp	sf7			;go to input again



sf6		;inc	de			;DE=len of content
		
		push	de			;print menu related to dir
		ld	de,menu_dir
		ld	hl,txt_adr_menu1+42
		call	PRINTHL
		pop	de
		
;		call	undraw_cursor		;problem. If there is no items but data in the memory.

		set	0,(ix+1)		;indicate, items found
		ld	iy,0
		exx
		push	iy
		pop	bc
		exx
		ld	hl,basic_end		;init done

;DE=data counter, HL=address counter,IY=item len counter, B'=item counter
;loop starts here
au_loop		xor	a
		ld	(hl),a
		inc	hl
		ld	(hl),a
		inc	hl
		ld	(hl),a
		inc	iy
		scf
		call	rece_byte
		jp	z,print_items		;na vyzkouseni. kdyz nebude sedet delka prilohy, nic uz neprijimej.	
		dec	de			;get rid of very first ^
		
		ld	b,3
;;		ld	a,2			;not needed, see below accept_until_arrowup
;;		ld	(aua+1),a		;for good counting of 2 byte chars
au2		call	accept_until_arrowup	;accept "18836^Manic Bugs^ManicBugs.tap^"
		djnz	au2

		push	hl			;replace "." by zero

		ld	b,4
au8		dec	hl
		ld	a,(hl)
		cp	"."
		jr	z,au9
		djnz	au8
au9		ld	(hl),0

;		dec	hl		;old method, failed if extension was eg. "*.AY"
;		dec	hl
;		dec	hl
;		dec	hl
;		ld	a,(hl)
;		cp	"."
;		jr	nz,au6
;		ld	(hl),0

au6		pop	hl
		ld	a,(server_flag)
		cp	2
		ld	b,1
		jr	c,au3			;servery zxart a remysharp dokonči s dlouhou koncovkou

au7		ld	b,3
au3		scf				;accept rest of item
		call	rece_byte		;5583 1 1987,0
		inc	hl
		dec	de
		ld	(hl),a
		cp	"^"
		inc	iy
		jr	nz,au3
;		ld	a, " "			;insert space
		xor	a			;insert zero, for better syntax check
au4		ld	(hl),a
		djnz	au3

au1		inc	hl
;		inc	iy
		push	iy			;finish item,IY=item length
		pop	bc			;DE=data counter,HL=current addr
		ld	(hl),c
		inc	hl
		ld	(hl),b
		push	hl
		or	a
		sbc	hl,bc
		dec	hl
		dec	hl
		ld	(hl),b
		dec	hl
		ld	(hl),c
		pop	hl
		inc	hl

au5		scf				;omit end of length
		call	rece_byte
		dec	de
		cp	#A
		jr	nz,au5

		exx
		inc	b
;		inc	bc			;increase item counter
		exx				
;		jr	z,auend
;		ret	z
		ld	a,d
		or	e
		jr	z,print_items		;Z=all items finished in memory
		ld	iy,0
		jr	au_loop
;auend		halt


;error in ZXDB, if ASCCI chars are >128, they come in 2, and they count as one byte.
;Eg. try AMAZ, HTTP returns length 365, in fact the length is 366
accept_until_arrowup
		scf
		call	rece_byte
		inc	hl
		dec	de
		ld	(hl),a
		cp	"^"
		inc	iy
		jr	nz,accept_until_arrowup
;;		jr	z,aua1			;not needed, we try to solve it in the start of au_loop
;;
;;		bit	7,a
;;		jr	z,accept_until_arrowup
;;aua		ld	a,#55			;come two bytes, that needs to be count as one
;;		dec	a			;this is beacause of ZXDB. If ASCII>127, content-length returns bad value
;;		ld	(aua+1),a		;in ZXDB.REMYSHARP.COM, try search "Pedro" to test it
;;		jr	nz,accept_until_arrowup
;;		inc	de
;;		ld	a,2
;;		ld	(aua+1),a
;;		jr	accept_until_arrowup
;;;;		jr	nz,accept_until_arrowup
;;		dec	iy
aua1		xor	a
		ld	(hl),a
		ret

;prints all searched items on the screen
print_items	call	wifi_unwait
		ld	(hl),e			;before print, mark 2x0 at the very end
		inc	hl
		ld	(hl),e

		ld	hl,basic_end		;must be here to prevent red items
		ld	(cur_item_addr),hl

		call	undraw_cursor

		ld	hl,txt_adr_mezera	;print page number
		ld	de,page_txt
		call	PRINTHL

		ld	de,spaces+30
		call	PRINT

		ld	de,block_asci
		ld	l,(ix+4)
		ld	h,0
		push	de
		call	HL2ASCII
		pop	de
		ld	hl,txt_adr_mezera+5
		call	PRINTHL

		ld	hl,txt_adr_dir_display
		ld	b,8*10
		call	cls_9_lines
		exx				;B=number of items to print
		ld	hl,txt_adr_dir_display
		ld	(cur_lin_prn_adr),hl
prn_page	push	bc
		call	print_item
		pop	bc
		ld	hl,cur_lin_prn_adr+1
		inc	(hl)
		djnz	prn_page
;		ret

;what is needed for moving through items?
;cur_item_addr=		address of item pointer (IX+25)
;cursor_w_stadr=	start address of cursor (IX+11), default=#5900
;cursor_w_curad=	current address of cursor (IX+13)
;wind_curs_item=	current number of cursor position (IX+21)(1-10)
;AP_a_page=		maximum number of lines in cursor window (IX+16), default=10
;item_pg_start=		starting address of item pointer (IX+20)
;last item must end with 00

;initialise pointers+ print cursor in its initial state
;		call	undraw_cursor			;too late, causes red item, sometimes
		ld	hl,basic_end
;		ld	d,h
;		ld	e,l
		ld	a,1
		ld	bc,(cursor_w_stadr)
		ld	(cur_item_addr),hl
		ld	(item_pg_start),hl
		ld	(wind_curs_item),a
		ld	hl,(cursor_w_stadr)
		ld	(cursor_w_curad),hl

		call	print_searched_string		;to remove "_" at the line end to signal we have finished inputting

		jp	draw_cursor

;input file number and download it.
;!!!! downlad file and screen now mingle in the routines just below, see bit 6,(ix+3)!!!!
download_screen	set	6,(ix+3)		;indicate, we are about to download screen
		jr	df51

download_file	set	3,(ix+3)		;show "press a key" after download file
		res	6,(ix+3)		;indicate, we download file
df51		set	4,(ix+3)		;indicate we do download and not search

df5;		call	get_status_ftp
;		cp	"3"
;		jr	z,df2
;		call	connect_to_zxdb_server
;		jr	df5
;		jr	download_file

		call	connect_to_server
		jp	nz,error_handler

df2		call	passive_receive_on	;for file download, we must turn on passive receive mode before accepting data


		ld	a,1
		out	(254),a

		ld	hl,exp_name		;prepare name for save
       		push	hl			;HL=exp_name
		ld	de,exp_name+1
	IFNDEF bs_dos
		ld	bc,12
	ELSE
		ld	bc,9
	ENDIF
		ld	(hl),32
		ldir				;clear file name

		ld	hl,INPBUFF-1		;backup file name, davame do print bufferu odzadu
		ld	a,(df3+1)		;length of string in INPBUFF
		ld	de,print_buffer+#100
;		ld	de,#4800
		ld	b,e
		ld	c,a
		add	hl,bc
		ld	(pts4+1),a
;		ld	c,a
		lddr

		inc	de
		ld	(pts6+1),de		;uloz pozici pro zpetny ldir
		

/*		ld	hl,INPBUFF		;backup file name
		ld	de,print_buffer
;		ld	de,#4800
		ld	a,(df3+1)
		ld	(pts4+1),a
		ld	b,e
		ld	c,a
		ldir
*/

		call	get_item_name
		ld	a,c			;HL'=start of extension
		ld	(df3+1),a		;length of number
		ld	a,b			;A=name length

		push	hl			;HL=file name
		ex	de,hl			;now, send file number to INPBUFF
		ld	de,INPBUFF
		ld	b,0
		ldir				;file number in INPBUFF


	IFNDEF bs_dos
;		halt		;jen test, proc nejdou mezery, pro esxdos, pak smazat

		pop	hl			;HL=file name
		pop	de			;DE=exp_name
		push	de
		ld	c,a
		cp	9
		jr	c,fdf_esx1
		ld	c,8
fdf_esx1	ldir				;name in its place, now search through
						;and replace dots, spaces...etc

		pop	hl			;HL=exp_name
		ld	b,8
fdf_esx3	ld	a,(hl)
		cp	#20
		jr	z,fdf_esx7
		cp	"."
		jr	nz,fdf_esx2
fdf_esx7	ld	a,"_"			;replace spaces with "_"
		ld	(hl),a
fdf_esx2	inc	hl
		djnz	fdf_esx3

;		ld	(fopen_adr+1),hl

		ld	(hl),"."		;put "." before extension
		inc	hl

		ld	c,3
		ex	de,hl
		exx
		push	hl
		exx
		pop	hl
		ldir				;extension on its place
		xor	a
		ld	(de),a	


	ELSE
		ld	c,a
		pop	hl			;HL=file name
		pop	de			;DE=exp_name
		cp	11			;limit name to 10 chars max
		jr	c,fdow1
		ld	c,10
fdow1		ldir				;name of file prepared
	ENDIF


		ld	a,2
		out	(254),a

		exx
		call	find_server
;		ld	hl,http_get1
;		ld	bc,15			;HOOD.SPECCY.CZ
;		ld	a,(server_flag)
;		or	a
;		jr	nz,df10
;		ld	hl,http_get0
;		ld	c,4			;ZXDB.REMYSHARP.COM
df10		exx
		call	df1			;send http request to the server
						;request sent, check if data available
		call	data_available_in_module_file
		ld	c,26
		jp	z,error_handler
						;data available, check for 200 OK and length of content
;		ld	bc,150			;accept 150 bytes, ok for ZXDB, but for HOOD.SPECCY.CZ, you have to put 300
		ld	bc,300
		push	bc
		push	bc
		call	ask_for_data_in_passive_mode

		pop	bc
		ld	hl,basic_end
		push	hl
		ld	de,#FFFF
		call	rece_block
		ld	c,23
		jp	nc,error_handler

	
		ld	de,http_200ok
		pop	hl
		ld	b,h
		ld	c,l
		call	look_for_string
		jp	nz,sfcerr1	;NZ=string not found

		ld	de,http_length
		call	look_for_string	;looking for eg. "Content-Length:" entry
		jp	nz,sfcerr1

;		ld	bc,10		;now, receive length of bytes the server is going to send
;		ld	hl,RX_buf
;		push	hl
;		call	receive_data_timeout
;		pop	bc
		ld	b,d
		ld	c,e
		push	de
		call	ConvRStr32
		ld	(blen),de	;DEHL= cislo
		ld	(blen+2),hl	;blen= length of data server will send
		ld	(rest_len),de	;these two are just for screen load. File load routine fills in rest_len itself
		ld	(rest_len+2),hl

		bit	2,(ix+3)
		jr	nz,df7		;NZ=TXT or POK, delku uz neresime, tu jsme overili pred zapocetim downloadu
		bit	6,(ix+3)
		jr	z,df7

		ld	a,h		;evaluate length of scr
		or	l
		jr	z,df8
		halt			;scr >6912, stop here
df8		ex	de,hl
		ld	bc,6912
		or	a
		sbc	hl,bc
		jr	z,df7
		add	hl,bc
		ld	b,#18
		sbc	hl,bc
		jr	z,df7
		halt

df7		pop	hl
		ld	de,eoleol
		call	look_for_string

		pop	hl		;HL=150 or 300

;		push	de		;DE=start of file
		ld	a,h
		or	d
		ld	h,a
;		ld	h,d

		or	a
		sbc	hl,de
		push	hl			;HL=portion already downloaded in the buffer
		pop	bc
;		pop	hl		;HL=start of file
		ex	de,hl

		bit	2,(ix+3)	;we download POK or TXT?
		jr	nz,df19
;		ld	de,#6000	;TXT or POK starts at #6000
;		jr	df6

		bit	6,(ix+3)
		ld	de,#4000	;screen will be loaded to #4000
		jr	nz,df6

df19		ld	de,basic_end	;"basic_end" for file download
;		ld	a,c
;		sub	e
;		ld	c,a
;		ex	de,hl
;		ld	e,b		;BC=portion of file already downloaded to buffer, need to be substracted from total length
df6		push	bc
		ldir			;file on start of buffer, DE=address to continue download

;		push	de

		ld	hl,(var_start)
		ld	(sfh+1),hl		;start address from variable to the saved file
;		pop	hl			;start adr in buffer (part of file already placed on "basic_end"
;		ld	hl,basic_end		;set start address of download buffer
		ld	(fdf6+1),de
;		ld	(fdf6+1),hl
;		ld	(sif1+2),hl		;chceme uložit od začátku

		call	set_20mhz		;set CPU to 20MHz

		bit	6,(ix+3)
		jr	nz,df9

;pro download souboru udelat:
;založ jméno, máš i délku
;CD85, zapni 20MHz a pasivní mod
;vypis texty, pak fdf_main až po buff_ad_float

		ld	de,loading_file		;length of file in blen already prepared, let's save file header
		ld	hl,txt_adr_edit_zone1
		call	PRINTHL
		ld	de,press_b
		call	PRINT
		ld	hl,txt_adr_progress
		ld	de,progress
		call	PRINTHL



		push	ix
		call	save_file_head		;open file for save
		pop	ix			;NC=file is lower than buffer

;		call	passive_receive_on	;nelze, tady uz tecou data

df9		pop	bc
		ld	(fdf7+1),bc		;fdf7=buffer
		ld	hl,(rest_len)
		ld	de,(rest_len+2)
		ld	(rest_len_first),hl
		ld	(rest_len_first+2),de
		or	a
		sbc	hl,bc
		ld	(rest_len),hl
		jr	nc,fdfm5
		dec	de
		ld	(rest_len+2),de

fdfm5		ld	hl,rest_len_first
		ld	(dp1+1),hl		;for correct file length in first display

fdf_main	ld	ix,flag1
		bit	6,(ix+3)
		jr	nz,fdfm8		;NZ=we load screen
		call	display_progress
fdfm8		ld	hl,(rest_len)		;main loop taking buffer by buffer
		ld	de,(rest_len+2)
		or	a
		sbc	hl,de			;is rest_len=0?
		jr	z,fdfm2			;Z=no more download
		call	fdf_loop
		ld	hl,rest_len
		ld	(dp1+1),hl
		jr	fdf_main

fdfm2		bit	2,(ix+3)
		ret	nz		;download of TXT or POK finished. Return and display it.
		bit	6,(ix+3)
		jr	z,fdfm9		;NZ=screen fully loaded

		res	4,(ix+0)
		call	akey1
		set	4,(ix+0)
		
fdfm11		call	print_whole_screen	;CLS+ prepare screen

		jr	fdfm10

fdfm9		call	save_close		;successful end, file downloaded completely

fdfm10		xor	a
fdfm1		ld	hl,transfer_flag
		ld	(hl),a

;fdfm1		call	save_close
		call	clear_rece_buf
fdfm3		ld	ix,flag1
;tn2		call	prn_mark_items	;print zero marked items
tn2		call	set_3_5mhz		
		call	passive_receive_off	;turn off passive transfer mode
fdfm4		jp	print_transfer_status	;end of download


rest_len_first	dw	0,0			;here is original true file length

fdf_loop	call	KEYSCAN
		cp	"b"			;B=ABBORT process
		jr	nz,fdf6

fdf_loop_err	call	delete_file		;in case of abbort, delete the file from MB disk.
		ld	ix,flag1
fdfm7;		call	passive_receive_off

		call	close_server

;		ld	hl,at_cipclose				;zkousime--- funguje to, super, navrat k serveru je ok
;		call	send_string
;		ld	hl,closed
;		call	look_on_fly

		call	clear_rece_buf		;tady by se měla vysypat příchozí data
		ld	a,1			;set transfer flag 1=aborted
		pop	hl			;to get of rid of ret addr
		jr	fdfm1
/*
		ld	hl,at_cipclose1		;close data channel
		call	send_string
		ld	hl,RX_buf
		ld	bc,#80
		push	hl
		push	bc
		call	receive_data_timeout
		pop	bc
		pop	hl
		ld	de,dvojt
		call	look_for_string
		ex	de,hl
		call	print_ftp_message
		ld	c,20
		jp	ful_abort		
*/

fdf6		ld	hl,basic_end		;loop, that downloads and saves 1x buffer, or less
		ld	(buff_add_float),hl
		ld	hl,(disk_buf_len)
		or	a
fdf7		ld	bc,0
		sbc	hl,bc
		ld	(portion_len),hl	;buffer counter
		
fdf3		ld	hl,(rest_len)
		ld	de,(rest_len+2)
		or	a
		sbc	hl,de
		jr	z,fdf9
;		jp	z,save_into_file	;Z=file completed

fdf8		call	data_available_in_module_file
		ld	c,26
		jr	nz,fdf5

;		halt				;testovaci, zastavi se pri vyskytu chyby 26

		call	display_error
		jr	fdf_loop_err

fdf9		bit	6,(ix+3)
		ret	nz
		jp	save_into_file

fdf5		ld	(rest_chunk),hl		;HL=length of data ZX can download
		ld	b,h
		ld	c,l
		ld	hl,(portion_len)	;file not complete, yet
		or	a
		sbc	hl,bc
		jr	c,fdf4

		call	fdf_sub1		;enough space in buffer, download chunk

		ld	bc,(rest_chunk)
		push	bc
		ld	hl,(rest_len)
		ld	de,(rest_len+2)
		ld	a,sub32			;rest_len=rest_len-rest_chunk
		call	bsdos
		ld	(rest_len+2),de
		ld	(rest_len),hl

		pop	bc			;BC=rest_chunk
		ld	hl,(portion_len)
		or	a
		sbc	hl,bc
		ld	(portion_len),hl
		jr	fdf3


fdf4		add	hl,bc			;not enough space in buffer, download only data for rest of buffer
		ld	(rest_chunk),hl
		ld	b,h
		ld	c,l

		call	fdf_sub1
		ld	bc,(rest_chunk)
		ld	hl,(rest_len)
		ld	de,(rest_len+2)
		ld	a,sub32			;rest_len=rest_len-rest_chunk
		call	bsdos
		ld	(rest_len+2),de
		ld	(rest_len),hl

		ld	hl,basic_end
		ld	(fdf6+1),hl		;second or more buffer filling starts on "basic_end" always
		ld	h,0
		ld	(fdf7+1),hl

		jp	save_into_file		;buffer full

;input: BC=length of data for download
fdf_sub1	call	ask_for_data_in_passive_mode
		ld	bc,(rest_chunk)
		ld	hl,(buff_add_float)	;HL=buffer start
		ld	de,#FFFF
		call	rece_block		;receive bytes
		ld	c,23
		jr	c,fdf_sub11

		halt				;testovaci, zastavi se pri vyskytu chyby 23
						;za stazenym blokem muze OK, nebo +IPD,2832
		call	display_error
		jp	fdf_loop_err
;		jp	nc,error_handler
fdf_sub11	ld	(buff_add_float),hl

;fdf_sub12	and	a			;vyporadej se s OK nebo +IPD,
;		call	rece_byte
;		jr	z,fdf_sub12

;;;;;		jp	clear_rece_buf		;po datech posle "eolOKeol", tohle fungovalo, ale zkusime jinak:
		ld	hl,ok
		jp	look_for_string


;clearni_RXko	call	clear_RX_buf		;tato rutina zpomalovala download souboru z 60 na 33 kb/sec
;		ld	bc,#80
;		ld	hl,RX_buf
;		jp	receive_data_timeout	;get rid of any incoming OKs, or
						;incoming IPDs



;		ei
;		ld	b,50
;ffff		halt				;wait 20 ms
;		djnz	ffff
;		di
;;		ret



;promenne pro download. Muzeme pak umistit jinam:
rest_chunk	dw	#5555			;in bytes, 0=budeme stahovat cele IPD (chunk), jinak stahujeme jen velikost v rest_chunk
buff_add_float	dw	#5555			;in bytes, moving address throughout the buffer, where download should start
;portion_len=rest_buf_len	již máme v disk operations


;		call	input_line	;zde musí být řetězec, který buď hledáme, nebo stahujeme
df11		ld	hl,INPBUFF	;ldirne retezec z INPBUFF (bud hledane jmeno, nebo cislo souboru)
df3		ld	c,#55		;c=length of string
;		ld	b,0
;;;		pop	de
		ldir
		ret



;GET /	_sarah_ftp			/?s=jmeno&p=0	HTTP/1.1
;GET /	_sarah_ftp_private		/?s=jmeno&p=0	HTTP/1.1
;GET /					?s=jmeno&p=0	HTTP/1.1
;GET /	v2				/?s=jmeno&p=0	HTTP/1.1

;GET /	_sarah_ftp			/get/cislo	HTTP/1.1
;GET /	_sarah_ftp_private		/get/cislo	HTTP/1.1
;GET /					get/cislo	HTTP/1.1
;GET /					get/cislo	HTTP/1.1


;df1 sends HTTP request from speccy to server
;OUT=A=0=NC=all ok, if A<>0 an C, then error
df1		ld	hl,http_GET
		ld	de,TX_buf
		ld	bc,5
		ldir			;"GET /"

					;now split it in 2 was. 1) search, 2)download
					;now very clever piece of code comes... do not change a single line until df16!!!
df12		push	de
		exx			;BC=length of http argument
		pop	de		;HL=http argument
		push	de		;remember ldir position
		ld	a,(hl)
		or	a
		jr	z,df17		;the only case we do put server, both in search and download
		ldir			;prensel jsi obe sary, ted rozhodnout podle search nebo download co dal
		inc	c		;set NZ

df17		push	af		;save flag Z=?s=, NZ=/?s=
		bit	4,(ix+3)
		jr	nz,df18		;NZ=we do download

		pop	af
		pop	hl		;ldir position do not need to be remembered any longer
		ld	hl,http_search
		ld	c,4
		jr	nz,df14	
		inc	hl
		dec	c
df14		ldir			;/?s= on its place

		call	df11		;put cislo or searched name

		ld	hl,http_page
		ld	bc,3
		ldir
					;add page number
		ld	l,(ix+4)	;prepare page number
		ld	h,b
		call	HL2ASCII
;		xor	a
;		ld	(de),a

		jr	df4		;prepare rest of line (HTTP/1.1)

;		ld	hl,http_get
;		ld	c,5
;		ldir


df18		pop	af		;we do download
		pop	hl
		cp	"_"
		jr	z,df15

		ex	de,hl		;continue with get right after string start

df15		ld	hl,http_get
		ld	c,5
		jr	z,df16
		inc	hl
		dec	c
df16		ldir			;/get/ or get/ is on its place

		call	df11		;cislo souboru

df4		ld	hl,http_http11	;toto uz je spolecne pro search i download
		ld	bc,17		;" HTTP/1.1..Host:"
		ldir
		push	de
		call	fs1		;dopln nazev serveru do druheho radku Host: nazev serveru
		pop	de
df13		ldir			;complete http request prepared in TX_buf

		ld	hl,eoleol
		ld	c,4
		ldir
		xor	a
		ld	(de),a		;all in TX buf

;59)sends any data in ftp, eg. "AT+CIPSEND=0,6"+ "USER xy"
;data have to be prepared in TX_buf, must end with 0
cipsend_request	call	wifi_wait
		ld	(ix+6),10	;timeout
cipsr3		ld	hl,at_cipsend	;send "AT+CIPSEND="
		call	send_string
		jp	nz,sfcerr1

		ld	hl,TX_buf	;HL=TX_buf
		call	count_com_len
					;BC=length of request
		ld	h,b
		ld	l,c
		ld	de,RX_buf
		push	de
		call	HL2ASCII
		xor	a
		ld	(de),a

		pop	hl		;RX_buf=length of command in ASCII, eg. "20"
		call	send_string	;send length of command
		jp	nz,sfcerr1

		ld	hl,eol
		call	send_string	;command completely sent
		jp	nz,sfcerr1

		ld	hl,char_zob	;Wifi module returned ">" to show readiness to accept?
		call	check_module_answer
		jr	nc,cipsr2		;NC=ll ok
		dec	(ix+6)
		jr	nz,cipsr3
;		jr	c,cipsend_request	;C=either busy or string not returned, repeat, can cycle here in endless loop!!!!!
;		jr	nc,cipsr2		;NC=all ok
;		cp	1
;		jr	z,cipsend_request	;Z=busy detected, resend the command
		ld	c,4			;A must be 2, wifi module did not returned the character
		jp	error_handler

cipsr2		ld	hl,TX_buf	;send data to ftp server from TX_buf address
		call	send_string
		jr	nz,sfcerr1
;		ret	z		;Z=sending ok, ošetřit
					;check for "Received" and "SEND OK"
		ld	hl,recv
		call	look_on_fly
		jr	z,cipsr1
		halt
cipsr1		ld	hl,sendok
		call	look_on_fly
		push	af
;		call	clear_rece_buf	;clear rest of message (+IPDxy), necistit, prichazim o dulezite info
		pop	af
		ret	z
		halt

evaluate_http_answer_no_passive
;OUT=A=0=no error, A=1 Bad request, A=2=other err
;		ld	hl,http_200ok
;		call	look_on_fly
;		jr	nz,sfcerr1	;NZ=string not found
;we need to read precisely server response, mainly, because of wrong user input

		ld	bc,50
		call	receive_data_to_RXbuf_bc
		push	bc
		push	hl
					;look for response 400 Bad Request
		ld	de,http_400err
		call	look_for_string
		pop	hl
		pop	bc
		jr	z,sfcerr3	;was error 400 Bad Request

		ld	de,http_200ok
		call	look_for_string
		jr	nz,sfcerr2	;was other message than 200 OK

		ld	hl,http_length
		call	look_on_fly	;looking for eg. "Content-Length:" entry
		jr	nz,sfcerr2

/*stary funkcni kod, ale nejde s nim nacitat ze zxart.ee, kde hned za Content-Length je 2x#D,#A
		ld	bc,10		;now, receive length of bytes the server is going to send
;		ld	bc,3		;dočasně kvůli serveru zxart.ee. Nutno vyřešit buď na zxart.ee, nebo
		ld	hl,RX_buf	;lepší rutinou. (zxart.ee vrací hned za content_len 2xeol!!)
		push	hl
		call	receive_data_timeout
		pop	bc
		call	ConvRStr32
		ld	(blen),de	;DEHL= cislo
		ld	(blen+2),hl	;blen= length of data server will send
		

dalsi		ld	hl,eoleol	;look for double #DA, now come data
		call	look_on_fly
		xor	a		;A=0=signal all ok
		ret
*/

;...a novy kod, ktery to umi
		ld	hl,RX_buf
		push	hl

loop_eha1	scf
		call	rece_byte
		cp	#D
		jr	z,loop_eha1
		cp	#A
		jr	z,cont_eha1
		ld	(hl),a
		inc	hl
		jr	loop_eha1

cont_eha1	pop	bc			;v RX_buf je číslo ASCII
		call	ConvRStr32
		ld	(blen),de
		ld	(blen+2),hl

loop1_eha1	scf
		call	rece_byte
		cp	#A
		jr	z,loop1_eha3
;		ret	z			;Z=all ok
		cp	#D
		jr	z,loop1_eha1		;Z= přijmi druhé #D,#A a konec HTTP hlavičky

;		xor	a
;		ret

loop1_eha2	ld	hl,eoleol
		call	look_on_fly
loop1_eha3	xor	a		;set NC, A=0=df1 successful
		ret


;		jp	sf2		;toto se musi lepe vyresit

sfcerr1		scf
sfcerr3		ld	a,1
		ret	
sfcerr2		scf
		ld	a,2
		ret

;sfcerr3
;sfcerr2
cr_err

close_server	bit	5,(ix+0)
		ret	z			;do not attempt to close server, if module not logged on to it. Otherwise it takes time
		ld	hl,at_cipclose		;we have to clear possible previous connections
		call	send_string
		ld	hl,closed
		jp	look_on_fly

move_server_number_down
;		ld	hl,server_flag
;		ld	a,(hl)
		dec	a
		jp	p,msnd1
		ld	a,number_of_servers-1
msnd1		ld	(hl),a
		ret

move_server_number
		ld	hl,server_flag
		ld	a,(hl)
		bit	7,(ix+3)		;Z=up, NZ=down
		jr	nz,move_server_number_down
		inc	a
		cp	number_of_servers
		jr	c,chs_ok
nuluj		xor	a
chs_ok		ld	(hl),a
		ret

ftp_upload_file
;change_host
change_server;	ld	hl,server_flag
;		ld	a,(hl)
;		xor	1
;		ld	(hl),a

;loopovaci smycka stridajici servery bude tato:
		res	7,(ix+3)		;indicate we go up
		call	move_server_number

		call	close_server

;		ld	hl,at_cipclose		;we have to clear possible previous connections
;		call	send_string
;		ld	hl,closed
;		call	look_on_fly

;;		call	check_module_answer	;dělá to neplechu, pryč zkusmo
;;		jp	c,sfcerr2
;		jr	z,chs0
;		halt				;stop if not found


chs0;		call	connect_to_zxdb_server
;		call	get_status_ftp
;		cp	"3"
;		jp	z,search_file
;		ret	z
;		jr	chs0

		call	connect_to_server
		jp	nz,error_handler
		ld	a,(INPBUFF)
		or	a
		jp	z,search_file		;new search
		cp	#20
		jp	z,search_file
		ld	(ix+4),0		;reset page counter
		jp	pts5			;pts5=existing search


;connects to zxdb server with "AT+CIPSTART="TCP","ZXDB.REMYSHARP.COM",80"
;OUT=NC=all ok, else error
connect_to_zxdb_server
		call	fs1
		ld	a,(hl)
		cp	1
		jr	nz,ctzs			;NZ=ok, server enabled

		call	move_server_number
		jr	connect_to_zxdb_server

/*
		ld	a,(server_flag)
		cp	2
		ld	hl,connectserver01
		jr	c,ctzs
		ld	hl,connectserver2
		cp	3
		jr	z,ctzs
		ld	hl,connectserver3
*/


ctzs		push	hl
		ld	hl,connect_txt0
		call	send_string
		ld	c,3
		pop	hl
		jp	nz,error_handler
		call	send_string
		ld	hl,connect_txt1
		call	send_string
		ld	hl,CONNECT
		jp	check_module_answer
;		call	check_module_answer
;		ld	de,host_hood
;		ld	a,(server_flag)
;		or	a
;		ld	de,host_zxdb
;		ld	hl,

connect_txt0	db	"AT+CIPSTART=",#22,"TCP",#22,",",#22,0
connect_txt1	db	#22,",","80",#D,#A,0

connectserver0	db	"HOOD.SPECCY.CZ",0		;valid for server0
connectserver1	db	"HOOD.SPECCY.CZ",0		;valid for server1
connectserver2	db	"NEXT.ZXART.EE",0		;valid for server2
connectserver3	db	"ZXDB.REMYSHARP.COM",0		;valid for server3

;out=HL start of string, BC=string len, A= server number (currently 0-3)
fs1		ld	hl,connectserver0
		jr	fs2
find_server	ld	hl,http_server0
fs2		xor	a
fs3		push	af
;		ld	a,(hl)				;because of disabled server, that begins with 0
;		or	a
;		jr	nz,fs4
;		inc	hl
fs4		push	hl
		call	search_until_zero
		ld	c,b
		ld	b,a				;BC=length of string
		pop	de
		pop	af
		ex	de,hl
		exx
		ld	hl,server_flag
		cp	(hl)
		exx
		ret	z				;HL=start of string
		ex	de,hl
		inc	a				;HL=next string
		cp	number_of_servers		;4
		ret	z
		jr	fs3

;http answers
http_200ok	db	"200 OK",0
http_400err	db	"400 Bad Request",0
http_length	db	"-Length: ",0

;http servers related
http_server0	db	"_sarah_ftp",0			;server0=_sarah_ftp, offical
http_server1	db	"_sarah_ftp_private",0		;server1=_sarah_ftp_private
http_server2	db	0				;server2=zxart.ee
http_server3	db	"v2",0				;server3=zxdb.remysharp.com

;http_get0	db	"_sarah_ftp"			;server0=_sarah_ftp, offical
;http_get1	db	"_sarah_ftp_private"		;server1=_sarah_ftp_private
;http_get2	db	0				;server2=zxart.ee
;http_get3	db	0				;server3=zxdb.remysharp.com

http_get	db	"/get/"
http_search	db	"/?s="
http_page	db	"&p="
http_GET	db	"GET /"
http_http11	db	" HTTP/1.1",#D,#A,"Host: "
;host_zxdb	db	"zxdb.remysharp.com",0
;host_hood	db	"hood.speccy.cz",0
eoleol		db	#D,#A
		db	#D,#A,0


/*
ful_abort;	push	bc			;in case of error, not only print code, but also send ABOR command to return
		res	4,(ix+3)
		ld	de,426
		call	send_abort
;		ld	c,16			;426 Connection closed
;		jp	c,error_handler		;neni osetreno, ale melo by, zde muze byt i kod 226, pak se to tvari,ze
;		jr	nc,fua1			;prikaz ABOR nebyl uspesne prijat
;		pop	bc



		ld	(ix+23),1		;set flag to print "Transfer aborted"	
		call	print_transfer_status
;;;		call	directory_retrieve
;		call	set_3_5mhz
		pop	hl			;new!!! since we call routines, pop should be here
		jp	kl8			;receive ftp answer (226 ABOR command successful), and print it

;subroutine, which sends to ftp server whole buffer blocks by blocks. Max length of block that ESP can send is #800:((
ftp_send_loop	ld	hl,basic_end
		ld	(ftp_upl_start),hl
		ld	hl,(portion_len)	;send whole buffer, part by part
		push	hl


fsl1;		ld	bc,#400			;!!!!!IMPORTANT, we can put #800 here, and will send twice as much data in one go!!
;		ld	bc,#800
		ld	bc,upload_length		;#800 is here, #400 works too but is slower
		pop	hl
		or	a
		sbc	hl,bc
		jr	z,ftp_upl_send		;file is round of #800 (#400)
		jr	c,ftp_send_lastc
		push	hl
		call	ftp_upl_send
		call	KEYSCAN			;user can ABBORT upload with "B" key ("A" already reserved for APPEND selection)
		cp	"b"
		ld	c,20
		jr	z,ful_abort
		jr	fsl1

ftp_send_lastc	add	hl,bc
		ld	b,h
		ld	c,l

ftp_upl_send	ld	(ftp_upl_len),bc
		push	bc
		call	cipsend_lod		;send AT+CIPSEND=1,length_of_data
		ld	c,6
		jr	c,ful_abort
;		jp	c,error_handler
		pop	bc
		push	bc
		ld	hl,(ftp_upl_start)	;IN=BC=lenght of data (#400 bytes or lower)
		push	hl

;		ei				;should be there according to manual, but works without the 20 ms pause
;		halt
;		halt
;		nop
;		di				;wait 20 ms (one interrupt) as AT+CIPSEND command requires in the manual

;		ld	de,0
;		call	send_block

		call	send_data

		pop	hl			;HL=ftp_upl_start
		pop	bc			;BC=length of data
		add	hl,bc			;increase next start position by data length
		ld	(ftp_upl_start),hl

		ld	c,5
		jr	nz,ful_abort
;		jp	nz,error_handler


		ld	(ix+6),#30		;48 attempts to check if data were sent ok
fus1		call	clear_RX_buf
		ld	hl,RX_buf		;find out, if data were sent ok
		push	hl
		ld	bc,#20
		push	bc
		call	receive_data_timeout
		pop	bc
		pop	hl

		ld	de,sendok
		call	look_for_string
		jr	z,fus2
		dec	(ix+6)
		jr	nz,fus1
		ld	c,8			;if error 8, we could start to reupload the packet
		jp	ful_abort
;		jp	error_handler



fus2		ret				;upload of packets works without 20 ms pause
;		ei				;should be there according to manual, but works without the 20 ms pause
;		halt
;		di				;wait 20 ms (one interrupt) as AT+CIPSEND command requires in the manual
;		ret
ftp_upl_len	dw	#5555			
ftp_upl_start	dw	#5555


;log off ftp
ftp_quit_connection
		bit	5,(ix+0)
		ret	z			;if no connection, return

		ld	de,ftp_quit+3
		ld	hl,txt_adr_edit_zone1
		call	PRINTHL

		ld	hl,ftp_quit
		xor	a
		ld	de,221
		call	send_ftp_command
		ld	c,40
		jp	c,error_handler
		res	0,(ix+1)		;indicate, that no dir is retrieved
		call	cls_9
		call	cls_dir_files
		call	undraw_cursor
		call	print_title
		jp	get_status_ftp
;		jp	error_handler1		;back to error_handler_loop
*/
		
/*
ftp_download_file_var
		call	send_type_command_i	;switch to TYPE I (binary)
		call	ftp_download_file_var1
		jp	tn2
ftp_download_file_new
		call	send_type_command_i	;switch to TYPE I (binary)
		call	ftp_download_file_new1
		jp	tn2
;priprava na hromadný download souboru
ftp_download_file_dir
		bit	5,(ix+0)
		ret	z			;if no connection, return

		call	send_type_command_i	;switch to TYPE I (binary)
		ld	hl,(mark_counter)
		ld	a,h
		or	l
		jr	nz,ldir_names
;		jr	z,fdfd1			;Z=download only file under cursor
		call	fdfd1
;end of download
;;tn2		call	prn_mark_items	;print zero marked items
;;		call	set_3_5mhz		
;;		call	passive_receive_off	;turn off passive transfer mode
;;fdfm4		jp	print_transfer_status	;end of download
;		call	undraw_cursor		;dir retrieve not needed
;;zxdb		jp	directory_retrieve	;after download or upload, dir list corrupted!!!

;		jp	directory_retrieve


;projdi adresar a oznacene soubory mrdni od #4800 (delka bufferu= #800 bajtu)
ldir_names	ld	hl,basic_end+fi_len-2	;delete multiple files
		ld	(cur_item_addr),hl
 
		ld	de,txt_adr_dir_display	;hl=start of buffer for names
		push	de			;DE=start addr to put new name

ln_loop		call	search_marked_items
		jr	z,ln1			;end of dir, go no..


		push	de
		scf
		call	get_item_name
		pop	de
		cp	3
		jr	c,ln2
		pop	hl			;get rid off call
		ret
;		ret	nc			;NC= item either LIST+full (not suported at the moment), or ".."
						;HL= marked item, BC= its length
ln2		ex	de,hl
		push	hl			;DE= marked item
		add	hl,bc			;HL= start addr of new name
		ld	a,h
		pop	hl			;HL=start addr of new name
		cp	high txt_adr_dir_info	;test for buffer end
		ld	a,0
		ex	de,hl
		jr	z,ln1			;DE= start addr of new name, HL=marked item

		ldir				;name on its place
		ld	(de),a			;0 as name end mark
		inc	de

		push	de			;DE= start addr for next name
		ld	bc,cur_item_addr
		call	item_add
		pop	de
		jr	nz,ln_loop		;Z=was last item
;		jr	ln_loop

ln1		ld	(de),a			;place 0 to signal buffer end (buffer ends with 2x 0)
;		ret
		

;take names and download them
take_names	;ld	hl,txt_adr_dir_display
		pop	hl		;HL=txt_adr_dir_display= addr of first name
tn_loop		ld	a,(hl)
		or	a
		jp	z,tn2

;		ld	(ix+23),a	;initialise transfer flag

;		jp	z,directory_retrieve
;		ret	z		;no more names

		xor	a		;we look for 0
		ld	de,#FFFF	;DE=#FFFF, counter
		push	hl		;HL=name start for download

tn1		cpi
		inc	de
		jr	nz,tn1		;look for 0

		ld	b,d
		ld	c,e
		pop	de		;DE=name start for download, BC=name length
		push	hl		;HL=next name start
		ex	de,hl
		call	fdfd2		;HL=name start for download, download file

;		halt			;jen pro testy

		call	passive_receive_off	;necessary to accept data correctly

		ld	hl,(mark_counter)
		dec	hl
		ld	(mark_counter),hl
		call	prn_mark_items
		pop	hl
		jr	tn_loop


;download file from ftp server
;ftp_download_file_dir
;		bit	5,(ix+0)
;		ret	z			;if no connection, return

fdfd1		scf
		call	get_item_name
		cp	3
		ret	nc			;NC= item either LIST+full (not suported at the moment), or ".."
;		ret	z			;Z= item=LIST+full- not supported at the moment

fdfd2		exx
		call	clear_inp_buf		;HL=start of file name,BC=name length
		exx

		ld	de,INPBUFF
		ldir
		jr	fdfv1


ftp_download_file_var1				;download file from variable
		bit	5,(ix+0)
		ret	z			;if no connection, return
	
		call	clear_inp_buf
		ld	hl,var_name
		ld	de,INPBUFF
		push	hl
		call	count_com_len
		pop	hl
		ldir				;ldir variable name into file header

fdfv1		ld	a,1
		ld	hl,ftp_size
		ld	de,213
		call	send_ftp_command	;send command SIZE+ file name take from vars
		jr	nc,fdf_cont

;		halt				;jen test

		ld	de,226
		or	a
		sbc	hl,de
;		jr	c,fdf_err		;C=file not found
		jr	z,fdf_cont
		jp	ffs1_fnf


ftp_download_file_new1				;download file from input
		bit	5,(ix+0)
		ret	z			;if no connection, return

		ld	de,enter_fil_nam
		ld	hl,txt_adr_edit_zone1
		call	PRINTHL

		ld	hl,ftp_size		;find out file size
		ld	a,2			;A=2=command+new input
		ld	de,213
		call	send_ftp_command
fdf_err		jp	c,ffs1_fnf		;C=file not found

fdf_cont	;call	save_dir_position	;realise in next version
		ld	hl,exp_name
		push	hl			;HL=exp_name
		ld	de,exp_name+1
		ld	bc,9
		ld	(hl),32
		ldir				;clear file name
*/

/*
;	IF bs_dos=0
	IFNDEF bs_dos
;		halt		;jen test, proc nejdou mezery, pro esxdos, pak smazat
		pop	hl			;HL=exp name (only to get rid off previous push)
		ld	hl,INPBUFF		;prepare name 8.3 for Esxdos
		push	hl
		ld	bc,254
		ld	a,"."
fdf_esx1	cpi
		jp	po,fdf_esx2		;dot not found
		jr	nz,fdf_esx1
		dec	hl
		ex	de,hl			;dot starts on address HL
		pop	hl			;HL=IPNBUFF, DE=dot
		push	de			;DE=dot
		ld	de,exp_name

		ld	b,8
fdf_esx3	ld	a,(hl)
		cp	#20
		jr	z,fdf_esx7
		cp	"."
		jr	nz,fdf_esx6
fdf_esx7	ld	a,"_"			;replace spaces with "_"
fdf_esx6	cp	"."			;esxdos won't accept: space, dot
		jr	z,fdf_esx4		;accepts: ',+,#,!,dollar,&,%
		ld	(de),a
		inc	hl
		inc	de
		djnz	fdf_esx3

fdf_esx4	ld	bc,4
		ld	h,d
		ld	l,e
		jr	fdf_esx5		;HL=end of file name

fdf_esx2	ld	b,8			;dot not found, find end of name or 8x max
		pop	hl			;HL=INPBUFF
		push	hl
		push	hl
fdf_esx21	ld	a,(hl)
		inc	hl
		cp	#20
		jr	z,fdf_esx24
/*
		jr	z,fdf_esx22
fdf_esx23	djnz	fdf_esx21
		jr	fdf_esx24

fdf_esx22	ld	(hl),"_"
		inc	hl			;eliminate spaces also with files without extension
		ld	a,(hl)
		cp	#20
		jr	nz,fdf_esx23
*/		
fdf_esx24	dec	hl	
		pop	de
		or	a
		sbc	hl,de
		ld	b,c			;B=0
		ld	c,l

;		ld	bc,8
		ld	de,exp_name
		push	de
		ex	de,hl			;now find out last position for use in esx disk operations
		add	hl,bc			;HL= end of file name
		pop	de
fdf_esx5	ld	(fopen_adr+1),hl
		pop	hl			;HL=INPBUFF
		ldir
		xor	a
		ld	(de),a	

	ELSE
		call	input_l_count
		pop	de			;DE=exp_name
		ld	a,c
		cp	11			;limit name to 10 chars max
		jr	c,fd1
		ld	c,10
fd1		ldir

	ENDIF
*/

/*
		ld	bc,RX_buf+4		;length of file
		call	ConvRStr32
		ld	(blen),de		;32 bit length in its place
		ld	(blen+2),hl

		call	set_20mhz		;set CPU to 20MHz

;;;		call	enter_passive_mode	;pokud nebude fungovat, pak dát zpět na začátek

		ld	de,loading_file
		ld	hl,txt_adr_edit_zone1
		call	PRINTHL
		ld	de,press_b
		call	PRINT
		ld	hl,txt_adr_progress
		ld	de,progress
		call	PRINTHL

		ld	hl,(var_start)
		ld	(sfh+1),hl		;start address from variable to the saved file
		ld	hl,basic_end		;set start address of download buffer
		ld	(sif1+2),hl


		push	ix
		call	save_file_head		;open file for save
		pop	ix			;NC=file is lower than buffer

		call	passive_receive_on

		ld	hl,ftp_retr
		scf
		call	send_ftp_command_in_passive_mode
*/
						;answer ok, now retrieve file
/* pouzito vyse, s upravou pro http
fdf_main	ld	ix,flag1
		call	display_progress
		ld	hl,(rest_len)		;main loop taking buffer by buffer
		ld	de,(rest_len+2)
		or	a
		sbc	hl,de			;is rest_len=0?
		jr	z,fdfm2			;Z=no more download
		call	fdf_loop
		jr	fdf_main


fdfm2		ld	a,(transfer_flag)
		or	a
		jr	z,fdfm1			;Z=transfer OK, skip code 226 check


		call	ask_for_reply_in_passive_mode
		ld	de,226
		or	a
		sbc	hl,de
		ld	hl,transfer_flag
		ld	(hl),0
		jr	z,fdfm1

		ld	(hl),2
		call	delete_file
		jr	fdfm3

fdfm1		call	save_close		;successful end, file downloaded completely
		call	clear_rece_buf
fdfm3		ld	ix,flag1
		ret

;dali jsme vyse, aby se mohl download opakovat
;		call	passive_receive_off	;turn off passive transfer mode
;		call	set_3_5mhz
;fdfm4		call	print_transfer_status	;end of download
;;		call	undraw_cursor		;dir retrieve not needed
;		jp	directory_retrieve	;after download or upload, dir list corrupted!!!


fdf_loop	call	KEYSCAN
		cp	"b"			;B=ABBORT process
		jr	nz,fdf6
fdf_loop_err	call	delete_file		;in case of abbort, delete the file from MB disk.
		ld	ix,flag1
;		ld	c,20
fdfm7		call	passive_receive_off
		ld	hl,at_cipclose1		;close data channel
		call	send_string
		ld	hl,RX_buf
		ld	bc,#80
		push	hl
		push	bc
		call	receive_data_timeout
		pop	bc
		pop	hl
		ld	de,dvojt
		call	look_for_string
		ex	de,hl
		call	print_ftp_message
		ld	c,20
		jp	ful_abort		

fdf6		ld	hl,basic_end		;loop, that downloads and saves 1x buffer, or less
		ld	(buff_add_float),hl
		ld	hl,(disk_buf_len)
		ld	(portion_len),hl	;buffer counter
,		
fdf3		ld	hl,(rest_len)
		ld	de,(rest_len+2)
		or	a
		sbc	hl,de
		jp	z,save_into_file	;Z=file completed


		call	data_available_in_module_file
		ld	c,26
		jr	nz,fdf5

;		halt				;testovaci, zastavi se pri vyskytu chyby 26

		call	display_error
		jr	fdf_loop_err


fdf5		ld	(rest_chunk),hl		;HL=length of data ZX can download
		ld	b,h
		ld	c,l
		ld	hl,(portion_len)	;file not complete, yet
		or	a
		sbc	hl,bc
		jr	c,fdf4

		call	fdf_sub1		;enough space in buffer, download chunk

		ld	bc,(rest_chunk)
		push	bc
		ld	hl,(rest_len)
		ld	de,(rest_len+2)
		ld	a,sub32			;rest_len=rest_len-rest_chunk
		call	bsdos
		ld	(rest_len+2),de
		ld	(rest_len),hl

		pop	bc			;BC=rest_chunk
		ld	hl,(portion_len)
		or	a
		sbc	hl,bc
		ld	(portion_len),hl
		jr	fdf3


fdf4		add	hl,bc			;not enough space in buffer, download only data for rest of buffer
		ld	(rest_chunk),hl
		ld	b,h
		ld	c,l

		call	fdf_sub1
		ld	bc,(rest_chunk)
		ld	hl,(rest_len)
		ld	de,(rest_len+2)
		ld	a,sub32			;rest_len=rest_len-rest_chunk
		call	bsdos
		ld	(rest_len+2),de
		ld	(rest_len),hl

		jp	save_into_file		;buffer full

;input: BC=length of data for download
fdf_sub1	call	ask_for_data_in_passive_mode
		ld	bc,(rest_chunk)
		ld	hl,(buff_add_float)	;HL=buffer start
		ld	de,#FFFF
		call	rece_block		;receive bytes
		ld	c,23
		jr	c,fdf_sub11

		halt				;testovaci, zastavi se pri vyskytu chyby 23

		call	display_error
		jp	fdf_loop_err
;		jp	nc,error_handler
fdf_sub11	ld	(buff_add_float),hl

		jp	clear_rece_buf

;clearni_RXko	call	clear_RX_buf		;tato rutina zpomalovala download souboru z 60 na 33 kb/sec
;		ld	bc,#80
;		ld	hl,RX_buf
;		jp	receive_data_timeout	;get rid of any incoming OKs, or
						;incoming IPDs



;		ei
;		ld	b,50
;ffff		halt				;wait 20 ms
;		djnz	ffff
;		di
;;		ret



;promenne pro download. Muzeme pak umistit jinam:
rest_chunk	dw	#5555			;in bytes, 0=budeme stahovat cele IPD (chunk), jinak stahujeme jen velikost v rest_chunk
buff_add_float	dw	#5555			;in bytes, moving address throughout the buffer, where download should start
;portion_len=rest_buf_len	již máme v disk operations
*/

/*
;enter any command to ftp server
enter_command	ld	hl,txt_adr_edit_zone1
		ld	de,enter_comm
		call	PRINTHL

		call	input_line
		ret	z		;input aborted
		ld	b,0
		ld	de,TX_buf
		ldir
		ex	de,hl
		ld	(hl),#D
		inc	hl
		ld	(hl),#A
		inc	hl
		ld	(hl),0


		push	de		;any value, normally we enter ftp_send_command with return code in DE, but must be here to correctly return from cipsend_request
		call	clear_edit_zone
		jp	cipsend_request	;here the return code can be any

/*		
;rename item/file
rename_item_file
		scf
		call	get_item_name
		cp	3
		ret	nc			;NC= item either LIST+full (not suported at the moment), or ".."
;		ret	z			;Z= item=LIST+full- not supported at the moment


		exx
		call	clear_inp_buf
		exx

		ld	de,INPBUFF
		ldir
		ld	hl,ftp_rnfr

		ld	a,1
		ld	de,350
		call	send_ftp_command
		jp	c,error_handler

		ld	hl,txt_adr_edit_zone1
		ld	de,enter_name
		call	PRINTHL

		ld	a,2
		ld	de,250
		ld	hl,ftp_rnto
		call	send_ftp_command
		jp	c,error_handler
		jp	directory_retrieve
		


;find out file size
ftp_file_size	bit	5,(ix+0)
		ret	z			;if no connection, return


		ld	de,enter_fil_nam
		ld	hl,txt_adr_edit_zone1
		call	PRINTHL

		ld	a,2
		ld	de,213
		ld	hl,ftp_size
		call	send_ftp_command
ffs1_fnf	ld	c,22
		jr	nc,ffc1			;NC=OK, file found
		ld	de,226			;bylo ;;;koko
		or	a			;bylo ;;;koko
		sbc	hl,de			;bylo ;;;koko
		jr	z,ffc1			;bylo ;;;koko
ffs1_fnf1	call	display_error
		xor	a			;A=0, for return to key_loop
		ret
;		ret	nc			
;		jp	error_handler

ffc1		ld	hl,exp_name
		push	hl
		ld	de,exp_name+1
		ld	bc,9
		ld	(hl),32
		ldir				;clear file name

		call	input_l_count
		pop	de
;		ld	b,0
		ldir
		xor	a			;A=0, for return to key_loop
		ret

;		jp	send_command_and_string
;		jp	enter_usr_psw		;print it only


;delete item/file
delete_item_file
		ld	de,yesno
		ld	hl,txt_adr_edit_zone1
		call	PRINTHL

dif1		call	KEYSCAN
		cp	"y"
		jr	z,dif2
		cp	"n"
		jp	z,clear_edit_zone
		jr	dif1

dif2		call	clear_edit_zone
		ld	hl,(mark_counter)
		ld	a,h
		or	l
		jr	nz,dif3			;Z=delet file under cursor
		call	dif_sub
		call	c,akey			;for user to read ftp error return code
		jr	cd1			;jp directory_retrieve

dif3		ld	hl,basic_end+fi_len-2	;delete multiple files
		ld	(cur_item_addr),hl
dif_loop	call	search_marked_items
		jp	z,tn2			;prn_mark_items, directory retrieve
;		jr	z,cd1			;Z=last item, jp directory_retrieve

		call	dif_sub
		jr	nc,dif5
		call	akey			;for user to read ftp error return code
		jr	dif4

dif5		ld	hl,(mark_counter)
		dec	hl			;decrease counter
		ld	(mark_counter),hl

		ld	hl,(cur_item_addr)
		inc	hl
		inc	hl
		res	0,(hl)			;unmark item

dif4		ld	bc,cur_item_addr
		call	item_add

		call	prn_mark_items
		jr	dif_loop	
*/

;puvodni
;OUT=NC,item deleted, C=item not deleted
dif_sub		scf
		call	get_item_name
		cp	3
		ret	nc			;NC= item either LIST+full (not suported at the moment), or ".."
;		ret	z			;Z= item=LIST+full- not supported at the moment

		ex	af,af'

		exx
		call	clear_inp_buf
		exx

		ld	de,INPBUFF
		ldir
		
		ex	af,af'
;		ld	c,a			;A= item status (0,1,2,3)
;		push	bc			;0=dir, 1=file, 2=dir or file
		or	a			;Z=directory
remove1		ld	hl,ftp_rmd
		jr	z,remove
;		cp	1
		ld	hl,ftp_dele
		
remove		ex	af,af'
		ld	de,250			;remove. With item 2 try delete file first...
		ld	a,1
		call	send_ftp_command
;		pop	bc			;C=item status (0,1,2,3)
		ret	nc			;NC=item deleted

;		or	a
;		ld	de,550			;550 Directory not empty
;		sbc	hl,de			;special case. Non empty dir have to be deleted file after file.
;		jr	nz,remove2

;		jr	nc,cd1			;NC=all ok
;		jr	c,fdf_err
;		ld	a,c
;		push	bc
remove2		ex	af,af'
		cp	2
;		ld	hl,ftp_rmd		;...if file delete not sucessful, delete dir.
;		push	bc			;because of "pop bc" in "remove" routine
		ld	a,3			;A can be any number from 2, it signals routine end
		jr	z,remove1
		scf				;C=item not deleted
		ret


;enter new directory
create_dir	ld	de,enter_dir
		ld	hl,txt_adr_edit_zone1
		call	PRINTHL

		ld	a,2
		ld	de,257
		ld	hl,ftp_mkd
		call	send_ftp_command
		ld	c,27
		jp	c,ffs1_fnf1		;C=command not successful

cd1;		call	undraw_cursor
		jp	directory_retrieve
*/
ffs1_fnf

/*
;automatic login to FTP server. If connection exists, it will ask if should be kept or proceed to new one.
auto_login	call	get_status_ftp		;find out if already connected to ftp server
		jr	nz,al1			;NZ= proceed to login

		call	test_ftp_connection
		ret	z			;Z=already connected

al1		call	connect_to_ftp_server	;connect to new server

al_user_pass	call	clear_inp_buf
		ld	hl,var_user
		push	hl
		call	count_com_len
		ld	de,INPBUFF
		pop	hl
		push	de			;DE=INPBUFF
		ldir

		ld	a,1			;enter username
		ld	de,331
		ld	hl,ftp_user
		call	send_ftp_command
		jp	c,error_handler

		ld	hl,var_pasw
		push	hl
		call	count_com_len
		pop	hl
		pop	de			;DE=INPBUF
		ldir

		ld	a,1			;enter password
		ld	de,230
		ld	hl,ftp_pass
		call	send_ftp_command
		jr	c,fl_usr_psw		;C=login not sucessful, another automatic try makes no sense, try manual login
;		jp	error_handler1		;error handler without printing error= test connection
		jr	fl3			;continue with directory retrieve
*/




/*
;login to ftp server. 1. make TCP connection at port 21, 2. input user+ password
;manual login
ftp_login	call	get_status_ftp		;find out if already connected to ftp server
		jr	nz,fl5			;NZ= proceed to login

		call	test_ftp_connection
		ret	z			;Z=already connected

;		ld	a,(ix+22)		;ix+22= status "2" to "5"
;		cp	"3"
;		jr	z,fl5
;		ld	hl,#4000+(32*4)		;if new connection, clear ftp message zone
;		ld	b,8*3
;		call	cls_9_lines

fl5		call	connect_to_ftp_server	;connect to new server

fl_usr_psw	res	4,(ix+3)		;ID=0
		ld	de,enter_usr		;enter& send username
		ld	hl,txt_adr_edit_zone1
		call	PRINTHL

		ld	de,331			;331=password required for user
		ld	hl,ftp_user
		ld	a,2
		call	send_ftp_command	;zde by mělo být vyhodnocení chyby!!!!
		jp	c,error_handler		;if error, than c=1, or 11 or 25

fl4		ld	de,psw			;enter& send password
		ld	hl,txt_adr_edit_zone1
		call	PRINTHL

		ld	de,230			;230= logged on
		ld	hl,ftp_pass
		ld	a,2
		call	send_ftp_command
		jr	c,fl_usr_psw		;bad password, enter it again
;		jr	c,fl4			;if error, than c=1, or 11 or 25, if wrong password, retry it (530= login incorrect)
;		jr	nc,fl3			;NC=login successful

fl3		call	clear_edit_zone
;		jp	error_handler1		;error handler without printing error= test connection
		call	get_status_ftp
;		jp	nz,kl10
		jr	nz,fl_usr_psw		;not connected, retry


directory_retrieve				;retrieve directory list
		call	cls_9			;clear screen from previous directory
		call	cls_dir_files
		call	undraw_cursor
		call	wifi_wait

		call	send_type_command_a	;switch to TYPE A (ASCII)
		call	enter_passive_mode
		call	passive_receive_on
		call	set_20mhz		;pridame, jakmile to bude odladeno

		ld	hl,flag_dir
		bit	4,(hl)
		jr	z,dr2		;Z=LIST= do not change bit 2

		set	3,(hl)		;NZ=NLST=change bit 2 to 1=full
		ld	hl,ftp_nlst	
					;set retrieve (abbreviated/full)
dr1_1		xor	a		;"call"= full list
		ld	b,a
		ld	c,a
		set	1,(ix+1)	;set flag as full
		jr	dr1


dr2		res	3,(hl)		;LIST set, set current command type to LIST also
		bit	2,(hl)		;user wish abbreviated(0) or full(1)?
		res	1,(hl)		;set flag as abbreviated 
		ld	hl,ftp_list
		jr	nz,dr1_1	;NZ=LIST+full


		ld	a,#C3		;"jp"= abbreviated
		ld	bc,dr5_skip
dr1		ld	de,dr_switch
		ld	(de),a
		inc	de
		ld	a,c
		ld	(de),a
		inc	de
		ld	a,b
		ld	(de),a

		and 	a
		call	send_ftp_command_in_passive_mode
					;answer ok, now you may retrieve directory data
		or	a		;if return code= 226= transfer ok, mark it in transfer_flag
		ld	hl,226
		sbc	hl,de
		ld	hl,transfer_flag
		ld	a,3		;3=incomplete
		jr	nz,dr12
		xor	a
dr12		ld	(hl),a

		ld	iy,0		;reset file/item counter. if LIST, then iy= file counter, ix+32=dir counter
					;if NLST, then only iy= item counter
;		ld	(ix+32),0	;reset also dir counter
		ld	hl,dir_counter
		xor	a
		ld	(hl),a
		inc	hl
		ld	(hl),a		
		res	5,(ix+1)	;set flag for files, the routine is able to change it later, if needed		


		ld	hl,first_item
		ld	de,basic_end
		ld	bc,9
		ldir
		ld	(dr10+1),de
;		push	de

		call	dr_get_data

dr10		ld	hl,#5555	;pop	hl
		dec	hl
		dec	hl		;HL=floating address for dir saving into buffer, first item
		ld	(item_addr),hl
	


dr_item_loop	ld	bc,0			;BC=item length counter
		ld	(hl),c			;put 2x 0 at the item start, if next item will not be completed, 2x0= end of list

		inc	hl			;1x inc_pos
		ld	a,h
		cp	high start_address
		jp	z,dr_end_err
		ld	(hl),c			;put 2x 0 at the item start, if next item will not be completed, 2x0= end of list

		inc	hl			;2x inc_pos
		ld	a,h
		cp	high start_address
		jp	z,dr_end_err
		ld	(hl),c

		inc	hl			;3x inc_pos
;		ld	(hl),c			;1 st item byte (flags), needs to be zero at initial phase
		inc	bc			;1x inc item length counter
		ld	a,h
		cp	high start_address
		jp	z,dr_end_err


dr_switch	jp	dr5_skip		;"jp dr5_skip" or 4xNOP

dr4		scf
		call	rece_byte		;neposkozuje zadny registr
		jr	z,dr4			;timeout!!!

		cp	#D
;		jr	nz,dr7			;better way, to add also test for #A
;		jr	nz,dr5_all		;potential place for bug, if here ends flow of data
		jp	nz,dr5_all

;		call	dr_dec_chunk

		dec	de			;1x dec rest_chunk
		ld	a,d			;;(ale musi!!) toto tu nemusi vubec byt, byte #D neni nikdy posledni
		or	e
		call	z,dr_get_data
;		jr	z,dr1			;Z=no more bytes available in module, ask for another

		scf
		call	rece_byte		;timeout, get rid off byte #A

		dec	de	;2x dec rest_chunk;!!!!DEC DE, but test it lower, see 0.94
;;		ld	a,d			;testing data was too early, item not closed
;;		or	e
;;		call	z,dr_get_data

		inc	hl			;4x dr_inc_pos
		ld	a,h
		cp	high start_address
		jr	z,open_item_invalid

		inc	hl			;5x dr_inc_pos
		ld	a,h
		cp	high start_address
		jr	z,open_item_invalid

			;item completed, close it with length bytes at its start and end

		push	hl			;next position address start
		push	de			;DE=rest_chunk
		dec	hl
;		ld	bc,(item_count_len)	;set item length at the item start and end
		ld	(hl),b
		dec	hl
		ld	(hl),c
		ld	de,item_addr
		ld	a,(de)
		ld	l,a
		inc	de
		ld	a,(de)
		ld	h,a

		ld	(hl),c
		inc	hl
		ld	(hl),b
		pop	de			;DE=rest_chunk		
					;test if 2 bytes after current item available
		pop	hl		;HL=new item start address

;		inc	hl			;dr_inc_pos
;		ld	a,h
;		cp	high start_address
;		jr	z,open_item_invalid_1

		inc	hl			;dr_inc_pos
		ld	a,h
		cp	high start_address
		jr	z,open_item_invalid_1

		xor	a			;fill 0s as a dir end signal/
		ld	(hl),a
		dec	hl
		ld	(hl),a


		ld	(item_addr),hl		;HL=position of new item in the buffer

		bit	4,(ix+1)		;muzu pouzit BC a IX, IY
		jr	nz,dr8			;Z=LIST, NZ=NLST
		bit	5,(ix+1)		;current item file or dir?
		jr	z,dr8			;Z=current item= file
;		inc	(ix+32)
		ld	bc,(dir_counter)
		inc	bc
		ld	(dir_counter),bc
		jr	dr9
dr8		inc	iy			;increase file/item counter

/*
		push	hl			;zjistujeme problem s nacitanim metadat +IPD
		push	de			;for testing purposes, can halt reading on any item
		push	iy
		pop	hl
		or	a
		ld	de,519
		sbc	hl,de
		pop	de
		pop	hl
		jr	nz,dr9
		halt				;halt on item number 635
*/

dr9
;;		dec	hl
;		dec	hl			;to go back to the start of the next item

		ld	a,d			;0.94, testing data was too early, item not closed
		or	e			;tady teprve testujeme bajt #D!!
		call	z,dr_get_data


;		dec	de			;dec rest_chunk, byte #A
;		ld	a,d			;only after item has been saved, test
;		or	e			;data availability
;		call	z,dr_get_data		;item is now fully closed, see previous lines

		jp	dr_item_loop

open_item_invalid_1
		pop	hl
open_item_invalid
		jp	dr_end_err
;;		xor	a
;;		ld	(item_addr),a		;limit the last valid item
;;
;;dir_continues	call	data_available_in_module
;;		jp	z,dr_end_ok		;Z=directory completed
;;		jr	dr5_all

;we leave unwanted bytes from the item
;eg. "-rw-r--r--    1 529      529          5460 Oct 21 19:57 PsychoSoldier.txt"
dr5_skip	scf
		call	rece_byte
		jr	z,dr5_skip
;						;pokud načteš #D, jde o konec adresáře, zkusme čtení ukončit a bude to super rychlé a spolehlivé
;		cp	#D
;		jp	z,dr_end_ok		;zkusíme, co to udělá


		cp	"d"
		res	5,(ix+1)		;set flag to indicate item is a file
		jr	nz,dr5_1		;NZ= není to adresář, jde o soubor
		sub	#20			;make "d"->"D"
		set	5,(ix+1)		;set flag to indicate item was a dir
		jr	dr5_2


dr5_1		ld	a,#20
dr5_2		inc	bc
		ld	(dr5_6+1),bc		;BC=item counter
		ld	(hl),a
		dec	de
		ld	a,d
		or	e
		call	z,dr_get_data
		inc	hl
		ld	a,h
		cp	high start_address
		jr	z,open_item_invalid

;new concept of skipping first item part- the attributes
;skip, until you meet byte #20
dr5_7		scf
		call	rece_byte
		ex	af,af'
		dec	de
		ld	a,d
		or	e
		call	z,dr_get_data
		ex	af,af'
		cp	#20
		jr	nz,dr5_7



		ld	b,3			;left: "    1 529      529          5460 Oct 21 19:57 PsychoSoldier.txt"
dr5_3		scf
		call	rece_byte
		ex	af,af'
		dec	de			;2x dec rest_chunk, prior to reading byte!!!! (other dec rest_chunk are after byte reading!!)
		ld	a,d
		or	e			;leave out all attributes
		call	z,dr_get_data

		ex	af,af
		cp	#20
		jr	z,dr5_3
dr5_4		scf
		call	rece_byte
		ex	af,af'
		dec	de			;2x dec rest_chunk, prior to reading byte!!!! (other dec rest_chunk are after byte reading!!)
		ld	a,d
		or	e			;leave out all attributes
		call	z,dr_get_data

		ex	af,af
		cp	#20
		jr	nz,dr5_4
		djnz	dr5_3
						;now we should have: "          5460 Oct 21 19:57 PsychoSoldier.txt"
		ld	b,4			;remove 4x space
dr5_5		scf
		call	rece_byte
		ex	af,af'
		dec	de			;2x dec rest_chunk, prior to reading byte!!!! (other dec rest_chunk are after byte reading!!)
		ld	a,d
		or	e			;leave out all attributes
		call	z,dr_get_data
		djnz	dr5_5			;finished: this is waiting:"      5460 Oct 21 19:57 PsychoSoldier.txt"

;		ld	a,#CD		;"call"= full list
;		ld	bc,rece_byte
;		push	hl
;		ld	hl,dr_switch
;		ld	(hl),a
;		inc	hl
;		ld	(hl),c
;		inc	hl
;		ld	(hl),b
;		pop	hl			;HL= floating address
dr5_6		ld	bc,#5555		;BC= item counter
		jp	dr4			;no need start from dr3
;		jr	dr3			;accept rest of item, all bytes, now


dr5_all		ld	(hl),a			;save one byte of dir to buffer and continue
;		call	dr_item_len_count
		inc	bc			;2x inc item_len_counter
		inc	hl			;6x dr_inc_pos
		ld	a,h
		cp	high start_address
		jr	z,dr_end_err
;		jr	nc,dr_end_err
		dec	de			;3x dec rest_chunk
		ld	a,d
		or	e
		call	z,dr_get_data
;		call	dr_dec_chunk
;		jr	c,dr3			;????
;		jp	dr3
		jp	dr4



dr_end_ok	;ld	(ix+23),0		;0=transfer ok, was assigned earlier
		ld	a,(ix+23)		;if transfer was ok, do not test 226 reply
		or	a
		jr	z,dr_end_ok1
		call	ask_for_reply_in_passive_mode	;look for code 226
		ld	de,226
		or	a
		sbc	hl,de
		ld	a,2
		jr	nz,dr_end_ok1		;2=code 226 not obtained, seems like error
		xor	a
dr_end_ok1	ld	hl,transfer_flag
		ld	(hl),a
		jr	dr_end

dr_end_err	ld	(ix+23),3		;3=transfer of dir incomplete (not enough memory)
		ld	hl,(item_addr)		;cancel item being uploaded
		xor	a
		ld	(hl),a
		inc	hl
		ld	(hl),a

dr_end		call	set_3_5mhz
		ld	hl,at_cipclose1		;close data channel-not needed, will be closed automatically
		call	send_string		;but if Transfer incomplete, the channel have to be closed
;		ld	hl,closed		;if already closed, the answer will be "UNLINK   ERROR"
;		call	check_module_answer
;		ld	c,43
;		jp	c,error_handler
		call	clear_rece_buf

		call	passive_receive_off
		push	iy
		pop	hl
		ld	(item_file_count),hl	;save number of items/files

;		call	set_3_5mhz		;seem to make problems, put it later
;		ld	hl,at_cipclose1		;close data channel. Needed, if channel not closed with directory retrieve completion
;		call	send_string
;		ld	hl,closed
;		call	check_module_answer
;		ld	c,43			;anser will be either CLOSED, or UNLINK ERROR (in case chanel had been closed)
;		jp	c,error_handler

		jr	dr6			;continue to directory display


						;nová rutina, musíme ji callovat
dr_get_data	ld	(drgd2+1),bc
		exx

		call	KEYSCAN
		cp	01			;test BREAK key
		jr	nz,drdg6
;		jr	nz,drdg3
		ld	hl,basic_end
		ld	d,0
		ld	a,fi_len
		dec	a
		dec	a
		ld	e,a
		add	hl,de
		ld	a,(hl)
		jr	z,drg1			;Z=no items read yet
;		set	1,(ix+1)		;directory retrieve user aborted, but some items were read
		ld	(ix+23),1		;transfer aborted
		pop	hl			;to get rid off previous call
		jr	dr_end

drg1		res	0,(ix+1)		;set flag dir has not been read		
		jp	fdfm7			;same routines as file download abort


drdg6		call	data_available_in_module_dir
		jr	z,drgd1
		ld	b,h
		ld	c,l
		call	ask_for_data_in_passive_mode
		exx
		ld	de,(rest_chunk)
drgd2		ld	bc,#5555		;nutnost zachovat BC
		ret
drgd1		pop	hl			;do not return, jump directly to end
		jp	z,dr_end_ok



dr6		call	print_working_directory
		call	print_dir_files_items	;prints number of items/dirs/files
		ld	de,menu_dir
		ld	hl,txt_adr_dir_info+22
		call	PRINTHL
;		ld	hl,ftp_path		;toto asi netřeba se piplat s pointerem
;		ld	(ftp_path_pointr),hl	;after dir reading, set path to its very start
		set	0,(ix+1)		;indicate that dir has been retrieved
;		ld	a,3
;		cp	(ix+23)
		ld	a,(ix+23)
		cp	3
		jr	z,dr11
		cp	1
		jr	nz,dir_move		;NZ=print status only in case of out of memory
		
dr11		call	print_transfer_status
*/

;moving through items in directory, OUT=NZ=move was ok, Z=start or end of dir reached
;item= byte 0,1=length of the item, 2=info byte, iteme data, last 2 bytes=length of the item

;up/down, move through dir, initialisation	
dir_move	ld	hl,0
		ld	(mark_counter),hl	;marked items
		bit	6,(ix+1)
		jr	nz,dir_move1		;NZ=we go updir, set directory from 1st pos
		
		ld	de,(item_checksum)
		ld	a,d
		or	e
		jr	z,dir_move1


		ld	hl,(cur_item_adr_tmp)	;check the checksum
;		ld	de,(item_checksum)
		ld	a,d
		or	e
		jr	z,dir_move1
		ld	a,(hl)
		cp	e
		jr	nz,dir_move1		;first byte not match, display from start
		dec	hl
		dec	hl
		ld	a,(hl)
		cp	d
		jr	nz,dir_move1		;second byte not match, display from start
		
		ld	hl,0
		ld	(item_checksum),hl	;signal, next reading go from start

		ld	de,(cur_item_adr_tmp)	;DE=cur_item_adr_tmp
		ld	hl,(item_pg_start_tmp)
		ld	a,(wind_curs_item_tmp)	;now, load parameters from previous updir
		ld	bc,(cursor_w_curad_tmp)
		jr	dir_move2
						;BC=cursor_w_curad
						;DE=cur_item_adr
						;HL=item_pg_start
						;A=wind_curs_item
dir_move1	ld	hl,basic_end
dir_move3	ld	d,h			;here jumps find function with HL
		ld	e,l
		ld	a,1
		ld	bc,(cursor_w_stadr)

;dir_move1	ld	hl,basic_end		;start of the buffer= first item
dir_move2	ld	(cur_item_addr),de	;start dir from item in HL
		ld	(item_pg_start),hl
;		call	prn_mark_items
;		ld	a,1
		ld	(wind_curs_item),a
;		ld	hl,(cursor_w_stadr)	;starting position of window
		ld	(cursor_w_curad),bc	;and make it current add
		call	wifi_unwait
dir_move4	call	print_page		;print without settings parameters
		call	prn_mark_items
		jp	draw_cursor
;;	call	print_page	;;;vyhodit pak	
;;	call	draw_cursor
;;	jp	key_loop	;;;vyhodit pak	

item_addr	dw	#5555			;starting address of dir item
;item_count_len	dw	#5555			;temporary length of dir item being downloaded

/*
go_updir1					;pak vymazat
go_updir;	call	check_updir
		res	6,(ix+1)		;signal, you go updir
		bit	0,(ix+1)
		jp	nz,go_updir1
;;;		jp	directory_retrieve	;if dir not read, read it new with BREAK key
*/

;check_updir	ld	hl,(item_checksum)
;		ld	a,h
;		or	l
;		ld	hl,flag_dir
;		res	6,(hl)
;		ret	nz
;		set	6,(hl)
;		ret

/*
download_screen;	ld	(blen),de		;length of screen= counter (6144 or 6912)
;		ld	de,INPBUFF
;		ldir

		set	4,(ix+3)			;set HTTP request to download file

		ex	de,hl				;file number into input buffer
		ld	de,INPBUFF
		ld	b,0
		ld	a,c			;HL'=start of extension
    		ld	(df3+1),a
		ldir

		ld	hl,6912
		ld	(blen),hl			;here should be possible enter either 6912 or 6144

		ld	hl,#4000
;		ld	(buff_add_float),hl
		ld	(df6+1),hl

		exx
		call	find_server
;		ld	hl,http_get
;		ld	bc,15			;HOOD.SPECCY.CZ
;		ld	a,(server_flag)
;		or	a
;		jr	nz,ds3
;		ld	c,4			;ZXDB.REMYSHARP.COM
ds3		exx
		call	df1			;send http request to the server

		call	set_20mhz
		call	passive_receive_on


ds1		call	data_available_in_module_file
		jr	nz,ds2
		ld	c,44
		call	display_error
download_screen_end
		call	set_3_5mhz
		res	4,(ix+0)		
		call	akey1
		set	4,(ix+0)
		call	passive_receive_off
		jp	sf4			;reread current page

;		call	print_whole_screen
;		call	print_dir_files_items
;		ld	de,menu_dir
;		ld	hl,txt_adr_dir_info+22
;		call	PRINTHL
;		call	print_ftp_status
;		call	get_status_ftp
;		jp	dir_move4		;start dir from where you ended

ds2		ld	b,h
		ld	c,l
		call	fdf_sub1
		ld	hl,(blen)
		ld	bc,(rest_chunk)
		or	a
		sbc	hl,bc
		jr	z,download_screen_end
		ld	(blen),hl
		jr	ds1
*/


prepare_txt	exx				;DE=file size in ACII
		call	ConvRStr16
		or	a			;HL=file size
;		ld	(df11_txt_len+1),hl	;place length of file to download
		ld	de,start_address-basic_end-1
		sbc	hl,de
		ret	nc			;NC=.TXT or .POK> buffer, do not continue

		set	2,(ix+3)
;		call	df51			;download file to buffer
		call	download_screen		;... we use the same way for download screen, with small differencies
		xor	a
		ld	hl,(buff_add_float)
		ld	(hl),a			;mark end of text with zero
		call	set_3_5mhz
		call	print_txt_main		;text downloaded, now display it...
		jp	fdfm11			;return to main prog, same as after screen download


download_file_and_run
		bit	0,(ix+1)
		ret	z			;dir not read, return
		call	get_item_name

		cp	4			;is it SCR extension?
		jp	z,download_screen

		cp	7
		jr	z,prepare_txt
		cp	8
		jr	z,prepare_txt

dfar		push	af
		res	3,(ix+3)		;do not show "press a key" after file download
		call	download_file+4
		pop	af

	IFDEF	bs_dos
		cp	1
		jr	z,sna_load_bsdos
		cp	2
		jr	nz,dfar1

sna_load_bsdos	;call	download_file+4		;dwonload snap
		ld	a,#63			;now jump into bsdos nmi menu and run snap
		out	(23),a

		or	a
		ld	de,#B536		;text "65" (last digit inverted)
		ld	hl,(#26D2)		;navesti VER v nmi menu
		sbc	hl,de
		jr	nz,slb			;NZ=nmi menu 1.64 not found

		ld	hl,(fnum)
		ld	(#5aa0),hl		;file number
		ld	sp,#57e2
;		jp	#d13			;load snap and JP back to nmi menu
		jp	#11DD			;start snap autoload, v kodu nmi navesti SA6


slb		ld	hl,txt_adr_edit_zone2
		ld	de,nmi_not_found_txt
		call	PRINTHL
		ld	a,#40
		out	(23),a			
		jp	pts			;wait a key and restart
dfar1
	ENDIF


	IFNDEF	bs_dos

;		cp	3
;		jp	nc,pts			;CLS, print info, waitkey, and restart
;		ret	nc			;3 or 4 (TZX or others=RET)
		or	a
		jr	z,esx_load_tap

		cp	5
		jr	z,esx_load_trdscl
		cp	6
		jr	nz,esx_load_snap		

esx_load_trdscl	ld	ix,vdisk_txt		;eject current one
		push	ix
		rst	8
		db	M_EXECCMD

;		ld	hl,vdisk_txt		;load TRD/SCL
		pop	hl
		ld	bc,7
		ld	de,exp_name-8
		push	de
		ldir
		ld	a,32
		ld	(de),a
		inc	de
		jr	els1

esx_load_snap	ld	hl,snapload_txt		;snap Z80 or SNA
		ld	bc,9
		ld	de,exp_name-9
		push	de
		ldir

els1		pop	ix
		rst	8
		db	M_EXECCMD		;load snap, ZXDB downloader ends here	
;		halt				;po odzkouseni odstranit

		im	1			;jump to TRDOS prompt
		ei
		ld	iy,23610
		ld	sp,65000
		jp	15616			;skok do TRDOsu (snapu je jedno, co zde je)

esx_load_tap	ld	b,0
		ld	ix,exp_name
		ld	a,(DefaultDrive)
		rst	8
		db	M_TAPEIN

		xor	a
		rst	8
		db	M_AUTOLOAD
		halt				;odstranit po odzkouseni


M_TAPEIN	equ	#8B
M_EXECCMD	equ	#8F
M_AUTOLOAD	equ	#90
snapload_txt	db	"snapload "
vdisk_txt	db	"vdisk 0",0
	ENDIF

	IFDEF	bs_dos
		or	a			
		jp	nz,pts			;NZ=CLS, waitkey, and restart
;		ret	nz			;NZ=file is not TAP, do not continue

		ld	hl,tapmaster_start
		ld	de,#4000
		push	de
		ld	bc,tapmaster_len
		ldir
		ret				;JP to TAPmaster
tapmaster_start	DISP	#4000
tapmaster
		include	"tapmaster.asm"		;here hand over to TAPmaster completely
tapmaster_len	equ	$-tapmaster
;		DISP	#4000			;nefunguje, pokracuje na #4222:((((( musim to vymyslet jinak
;		ENT

		org	tapmaster_start+tapmaster_len		;pro poslední verzi sjasmu ok. Pro verzi 1.19.0 musí zůstat řádek zachován
	ENDIF
/*
enter_directory	;and	a
;		call	get_item_name
;		ld	a,e
;		or	a
;		jr	nz,ed5			;NZ=file length not 6144 nor 6912
;		ld	a,h
;		cp	#18
;		jr	nz,ed5			;NZ=file length not 6144 nor 6912
;		cp	#1B
;		jr	z,download_screen
;

		call	clear_inp_buf

		scf
		call	get_item_name
		ld	de,INPBUFF
		ldir

		or	a
		jr	z,ed1			;Z=item is a directory
		cp	4
		jr	z,go_updir1		;Z=item is ".."		
						;if A=2, item is NLST mode and could be file or dir



		ld	a,1
		ld	hl,ftp_size
		ld	de,213
		call	send_ftp_command	;ošetřit chybu!!! pro případ špatné odpovědi, nebo NLST
;		jp	c,chyba
		ld	de,226
		or	a
		sbc	hl,de
;		jp	nz,chyba

		ld	bc,RX_buf+4
		call	ConvRStr32
		ld	a,l
		or	h
		jr	nz,ed1


		ld	a,e
		or	a
		jr	nz,ed1			;NZ=file length not 6144 nor 6912
		ld	a,d
		cp	#18
ed5		jp	z,download_screen	;NZ=file length not 6144 nor 6912
		cp	#1B
		jr	z,ed5


;ed5		ex	af,af'
;		or	a			;Z=item is a directory
;		jr	z,ed1
;;		cp	2
;;		jr	z,ed1			;Z=NLST, item could be a directory, but this is not sure
;		cp	4
;		jr	z,go_updir1		;Z=item is ".."
;		ret	nz			;Z=item is ".."

;;ed1		exx				;HL=dir start, BC=dir length
;;		call	clear_inp_buf
;;		exx

ed1		scf
		call	get_item_name
		ld	a,(hl)
		cp	"."
		jr	nz,ed2			;not ".."item to direct updir
		inc	hl
		cp	"."
		dec	hl
		jr	nz,ed2

go_updir1	res	6,(ix+1)		;signal, we go updir
;		call	check_updir
		xor	a			;we want to go updir
		ld	hl,ftp_cdup
		ld	de,200
		jr	ed3

ed2	;	ld	de,INPBUFF
	;	ldir

		call	save_dir_position	
;		set	6,(ix+6)

ed6		ld	a,1
		ld	hl,ftp_cwd
		ld	de,250
ed3		call	send_ftp_command
		jr	nc,ed4
;		push	bc			;preserve carry
		ld	de,200
		or	a
		sbc	hl,de
;		pop	bc
		jr	z,ed4			;Z=also valid code
		add	hl,de
		ld	de,250
		or	a
		sbc	hl,de
		jr	z,ed4
;		push	bc			;NZ=error
;		pop	af
		ret
;		ret	c			;or jp c,error_handler
ed4		call	undraw_cursor
		jp	directory_retrieve

*/



key_down	call	undraw_cursor
		ld	bc,cur_item_addr
		call	item_add
		jr	nz,kd4
		jp	draw_cursor
;		ret	z			;Z=end of dir, nothing to display

kd4		ld	hl,wind_curs_item
		ld	a,(hl)
		cp	(ix+16)
		jr	nc,kd1			;C=cursor at the end of the window

		inc	(hl)			;inc cursor position
;		call	undraw_cursor
		ld	hl,(cursor_w_curad)
		ld	de,32
		add	hl,de
		ld	(cursor_w_curad),hl
kd2		jp	draw_cursor
		
kd1		ld	bc,item_pg_start
		call	item_add
kd3		call	print_page
		jr	kd2

key_up		call	undraw_cursor
		ld	bc,cur_item_addr
		call	item_sub
		jr	z,kd2
;		ret	z			;Z=start of dir, nothing to display

		ld	hl,wind_curs_item
		ld	a,(hl)
		cp	1
;		or	a
		jr	z,ku1			;z=cursor at the start of the window

		dec	(hl)			;dec cursor position
;		call	undraw_cursor
		ld	hl,(cursor_w_curad)
		ld	de,32
		or	a
		sbc	hl,de
		ld	(cursor_w_curad),hl
		jr	kd2
;		jp	draw_cursor


ku1		ld	bc,item_pg_start
		call	item_sub
		jr	kd3
;		jp	print_page


item_add;	ld	hl,(cur_item_addr)	;move pointer one item add,IN=HL=(item addr to add)
		push	bc
		ld	a,(bc)
		ld	l,a
		inc	bc
		ld	a,(bc)
		ld	h,a
		
		ld	a,(hl)
		inc	hl
		ld	d,(hl)
		or	d
		pop	bc
		ret	z			;Z=we are at the end of directory
		ld	e,a
		inc	hl
;		inc	hl
		add	hl,de
		inc	hl
		inc	hl

		push	hl
		ld	a,(hl)
		inc	hl
		ld	l,(hl)
		or	l
		pop	hl
		ret	z			;Z=we are at the end of directory		

iu1		ld	d,h
		ld	e,l
		ld	h,b
		ld	l,c
		;ld	(cur_item_addr),hl
		ld	(hl),e
		inc	hl
		ld	(hl),d
		inc	b		;set NZ
		ret

item_sub;	ld	hl,(cur_item_addr)
		push	bc
		ld	a,(bc)
		ld	l,a
		inc	bc
		ld	a,(bc)
		ld	h,a

		or	a
		ld	de,basic_end
		sbc	hl,de
		add	hl,de
		pop	bc
		ret	z		;Z=we are at dir start
;		jr	c,pgd2		;C=we are at the starting item already, set NZ
		dec	hl
		ld	d,(hl)
		dec	hl
		ld	e,(hl)
		or	a
		sbc	hl,de
;		dec	hl
		dec	hl
		dec	hl
		jr	iu1

;0.92a*******************************
;;pg_up;		ld	hl,item_add
;		ld	(infsi3+1),hl	;modify the routine to add
;		ld	(mc3+1),hl

		scf
		call	count_from_start_item
		jr	nz,pgu1		;NZ=current page not full

pgu2		ld	e,a
		ld	a,(ix+21)
		cp	(ix+16)		;is cursor on the last item on page?
		ld	a,e
;		call	cfsi1		;return page starting item
		jr	nz,move_cursor	;NZ=not

pgu3		and	a
		call	count_from_start_item
;		push	af
		call	print_page
		dec	b		;with this solution we are dependant on print_page output!!!
		inc	b
		ld	a,(ix+16)
		jr	z,pgu4
		sub	b		
		inc	a		;A= number of items on the following page
;		pop	af
pgu4		ld	b,a
		jr	nz,move_cursor
		jr	mc5		;move item pointer
;		ret

pgu1		cp	(ix+16)		;is the cursor on the last item?
		ret	z
					;continue to move cursor
move_cursor				;move cursor to line A (1-8)
;IN=A= position (1-8)
		push	af		;not needed, routine does not use A anymore
		call	undraw_cursor
		pop	af
		ld	(ix+21),a	;current cursor line
		ld	b,a
		ld	hl,(cursor_w_stadr)
		dec	a
		ld	de,32
mc1		or	a
		jr	z,mc2
		add	hl,de
		dec	a
		jr	mc1
mc2		ld	(cursor_w_curad),hl



mc5		ld	hl,(item_pg_start)
		ld	(cur_item_addr),hl
		dec	b

mc4		push	bc		;B=number of lines to move item pointer
		ld	bc,cur_item_addr
mc3		call	item_add
		pop	bc
		djnz	mc4
		jp	draw_cursor
		

;count rest of items forward
;if page< 8, do not change starting item
;IN=C=return original item_pg_start, NC= do not return original item_pg_start
;OUT=NZ<8, Z>=8, A= number of available items
count_from_start_item
		rr	b		;preserve carry
		push	bc
		ld	hl,item_pg_start
		ld	e,(hl)
		inc	hl
		ld	d,(hl)		;DE=starting item
		exx			;save HL and DE

infsi_sub	ld	b,(ix+16)	;B=items a page. OUT=Z=A=8 found, NZ=found <8=A=number of found pages
infsi1		push	bc
		ld	bc,item_pg_start
infsi3		call	item_add
		pop	bc
		jr	z,infsi2
		djnz	infsi1
infsi2		inc	b
		dec	b
		ld	a,(ix+16)
		jr	z,infsi4
		sub	b
		inc	a		;A= number of items on the following page
infsi4		pop	bc		;Z=8 items found, NZ=<8 items found
		rl	b		;return carry
		ret	nc
;		ret	z		;Z=8 items found, NZ=<8 items found
;		sub	b
;		inc	a		;A= number of items on the following page
					;items<8 do not change page start		
cfsi1		exx
		ld	(hl),d
		dec	hl
		ld	(hl),e		;return original starting item
		ret			;A=items/page

		
	





;;pg_down;	ld	hl,item_sub
;		ld	(infsi3+1),hl	;modify the routine to add
;		ld	(mc3+1),hl

		ld	a,1
		cp	(ix+21)	;current cursor line
		jr	z,pgd1

		call	pgd_sub1		;move cursor
		jr	pgd3

pgd1		call	pgd_sub2		;Z=cursor on first position already
		jp	z,dir_move1
		call	print_page
		call	pgd2_4
pgd3		jp	draw_cursor



		
pgd_sub1	ld	(ix+21),a
		push	af
		call	undraw_cursor
		pop	af
		ld	hl,(cursor_w_stadr)	;move cursor
		dec	a
		ld	de,32
pgd2_1		or	a
		jr	z,pgd2_3
		add	hl,de
		dec	a
		jr	pgd2_1
pgd2_3		ld	(cursor_w_curad),hl
pgd2_4		ld	hl,(item_pg_start)
		ld	(cur_item_addr),hl
		ret


pgd_sub2	ld	b,(ix+16)	;items a page
pgd1_1		push	bc
		ld	bc,item_pg_start
		call	item_sub
		pop	bc
		ret	z
;		jp	z,dir_move	;Z=we are on dir start
		djnz	pgd1_1
		inc	b		;indicate NZ
		ret



print_item	ld	hl,(cur_item_addr)	;print one item
		ld	e,(hl)			;HL=addr in item
		inc	hl
		ld	d,(hl)
		inc	de
		inc	de
		inc	de
		add	hl,de
		ld	(cur_item_addr),hl	;next item start addr (to print)
		sbc	hl,de
		inc	hl
		inc	hl
		call	search_until_zero	;out=HL=next address in item
		ex	de,hl
		ld	hl,(cur_lin_prn_adr)	;DE="Manic Bugs"
		push	hl			;HL=adr1 for print on screen
		push	de			;DE=adr in item

		ld	b,13			;new in 0.8. Print dir name with ".." at the end if name>15
pi5		ld	a,(de)
		or	a
		jr	z,pi6
		push	de
		push	bc		
		call	CHAR
		pop	bc
		pop	de
		inc	de
		djnz	pi5			;ok13x printed
		call	print_dots

;		call	PRINTHL			;print dir name "Manic Bugs"

pi6		ld	bc,15
		pop	de
		pop	hl
		add	hl,bc			;HL=adr2 for print screen
		ex	de,hl
		call	search_until_zero
;		ex	de,hl
		push	hl
		push	de
		call	search_until_zero	;find out if name>32
		pop	hl			;HL=address
		pop	de			;DE=string
		push	hl
		push	de		
;		ld	a,b
;		ld	c,name_len
;		cp	c
;		jr	c,pi2			;<=32

		ld	b,name_len-1
pi7		ld	a,(de)
		or	a
		jr	z,pi3
		push	de
		push	bc		
		call	CHAR
		pop	bc
		pop	de
		inc	de
		djnz	pi7			;ok30x printed
		call	print_dots


/*		ld	b,0
		add	hl,bc

		ld	(pi4+1),hl		;last pos in name
;		inc	hl
		ld	a,(hl)
		ld	(hl),0
		dec	hl
		ld	b,(hl)
		ld	(hl),"."
		dec	hl
		ld	c,(hl)			;now A,BC stores bytes, that needs to be returned
		ld	(hl),"."
		pop	hl
		pop	de
		push	de
		push	hl		

		push	af
		push	bc
		call	PRINTHL			;print shortened name
		pop	bc
		pop	af
;		pop	hl
pi4		ld	hl,#5555
;		inc	hl
		ld	(hl),a
		dec	hl
		ld	(hl),b
		dec	hl
		ld	(hl),c
		jr	pi3
*/

pi2;		push	hl
;		push	de
;;		ex	de,hl
;;		call	PRINTHL			;print "ManicBugs"


pi3		pop	de
		pop	hl
		ld	bc,32
		add	hl,bc
		push	hl
		ex	de,hl
		call	search_until_zero
		push	hl
		ex	de,hl
		call	PRINTHL			;print "tap"
		pop	de
		pop	hl	
;		ld	bc,4			;tady se musí udělat zarovnání zprava
		ld	bc,10
		add	hl,bc			;delka souboru
		ex	de,hl
		push	de			;DE=pravy okraj
		call	search_until_zero
		push	hl			;HL=udaj delky souboru k tisku
		call	search_until_zero	;B=file length len
		pop	de
		pop	hl
		push	hl
		push	de
pi1;		dec	b
		dec	hl
		djnz	pi1

;		ex	de,hl
		call	PRINTHL			;print "5583"
 		pop	de
		pop	hl
		ld	a,(server_flag)
;		ld	a,(ix+5)
		cp	2
		ret	c
;		cp	1
;		ret	z			;Z=HOOD.SPECCY.CZ
;;		bit	0,(ix+5)		;server_flag
;;		ret	nz			;NZ=HOOD-SPECCC-CZ

;		ld	bc,3
;		add	hl,bc
	inc	hl
		push	hl
		ex	de,hl
		call	search_until_zero
		push	hl
		ex	de,hl
		call	PRINTHL			;print "1"
		pop	de
		pop	hl
		ld	bc,2
		add	hl,bc
		ex	de,hl
		call	search_until_zero
		ex	de,hl
		call	PRINTHL			;print "1987"
		ret
		
name_len	equ	32
;		ret

print_dots	ld	a,(de)
		or	a
		ret	z			;Z=name len is still visible (len 14 in case of dir, or 31 in case of name)
		ld	a,"."
;		push	af
;		call	CHAR
;		pop	af
		jp	CHAR

	
;********************************
/*old ftp	
print_item	ld	hl,(cur_item_addr)	;print one item
		ld	e,(hl)
		inc	hl
		ld	d,(hl)
		dec	de
		inc	hl
		inc	hl
		push	hl			;HL=start of item for print
		ld	hl,line_length
		or	a
		sbc	hl,de
		add	hl,de			;HL=line length
		inc	de
		jr	nc,pi_print_it		;NC=line<64, print it

		ex	de,hl			;DE=line length
		pop	hl			;C=line>64, print it with ".." at the end, ;HL=start of item for print
		ld	b,h
		ld	c,l		
		add	hl,de			;move to pos 64
		ld	a,(hl)
		ld	(hl),0
		push	af
		ld	a,"."
		dec	hl
		ld	e,(hl)
		ld	(hl),a
		dec	hl
		ld	d,(hl)
		ld	(hl),a
		push	de
		push	hl
		ld	hl,(cur_lin_prn_adr)
		ld	d,b
		ld	e,c		
		call	PRINTHL
		pop	hl
		pop	de
		pop	af
		ld	(hl),d
		inc	hl
		ld	(hl),e
		inc	hl
		ld	(hl),a
;		ret
*/
print_item_attrs	
		ld	hl,(cur_item_addr)
		ld	c,(hl)
		inc	hl
		ld	b,(hl)
		inc	hl
		ld	a,unmark_no_curs	;A=7=not marked
		bit	0,(hl)
		jr	z,pia1
		ld	a,mark_no_cursor	;A=2=marked
pia1		ld	hl,(cursor_w_curad)
		ld	b,32
pia2		ld	(hl),a
		inc	hl
		djnz	pia2
;		ld	(cursor_w_curad),hl
		ret

pi_print_it	pop	hl			;HL=start of item for print
		add	hl,de
		dec	hl
		ld	a,(hl)
		push	af			;save one length byte
		ld	(hl),0			;0 for print end
		push	hl
		or	a
		inc	hl
		sbc	hl,de
		ex	de,hl
		ld	hl,(cur_lin_prn_adr)
		call	PRINTHL
		pop	hl
		pop	af
		ld	(hl),a			;return one length byte
;		ret
		jr	print_item_attrs

print_page	;ld	hl,#4800
;		ld	b,8*8
;		call	cls_9_lines
		call	cls_9
		ld	hl,txt_adr_dir_display
		ld	(cur_lin_prn_adr),hl
		ld	b,(ix+16)		;prints whole page. Start=current item that a pointer is set to (cur_item_addr)
		ld	hl,(cur_item_addr)
		push	hl
		ld	hl,(cursor_w_curad)
		push	hl
		ld	hl,(cursor_w_stadr)
		ld	(cursor_w_curad),hl
		ld	hl,(item_pg_start)
		ld	(cur_item_addr),hl	;make item_pg_start current item

pp1		push	bc
		call	print_item		;length=number of lines (AP_a_page)
		ld	hl,(cursor_w_curad)
		ld	de,32
		add	hl,de
		ld	(cursor_w_curad),hl
		ld	hl,cur_lin_prn_adr+1
		inc	(hl)
;		call	move_lin_adr_u
		ld	bc,cur_item_addr
		call	item_add
		pop	bc
		jr	z,pp2			;Z=end of directory
		djnz	pp1

pp2		pop	hl			;return cur_item_addr
		ld	(cursor_w_curad),hl
		pop	hl
		ld	(cur_item_addr),hl
		xor	a			;set A=0 to eliminate key press
		ret




;roll item>47 chars
;BC <line_length, item_length>, eg.<64,200>

line_len	equ	46
nam_len		equ	32
;print position is 0-46
roll_item					;find out length to display (ie. directory+ file name without extension)
		ld	hl,(cur_item_addr)
		inc	hl
		inc	hl
		inc	hl
		call	search_until_zero	;HL=start of item to print
		ld	d,h
		ld	e,l
		call	search_until_zero	;directory name
		ld	c,b
		ld	a,c			;is the name longer than space?
		cp	14			;C=len of dir
		ex	af,af'			;save result
		call	search_until_zero	;filename
		ld	a,b			;B=length of file name without extension to print
		add	a,c
		ld	c,a
		ld	b,0			;BC=len of dir+name to print
		cp	nam_len
		jr	nc,ri2			;C=line redable as such, no need to roll it

		ex	af,af'
		ret	c			;C=line readable as such, no need to roll it

ri2		ld	a,c
		inc	a
		ld	l,line_len
		cp	l			;name len to display must be line_len or ietm len<line_len
		jr	c,ri1
		ld	a,l

ri1		ld	(pir2+1),a
		ld	(roll_item_inc+1),bc

		ld	a,high txt_adr_mezera	;=txt_adr_dir_display-1
		add	a,(ix+21)
		ld	(cur_lin_prn_adr+1),a

		push	de
		call	item_roll_clear
		pop	de			;DE=start of file name to print

		ld	bc,line_len
		call	print_item_roll
		call	roll_item_inc		;initial press of key x

roll_item_key	exx
		call	KEYSCAN
		exx
		cp	"z"
		call	z,roll_item_dec
		cp	"x"
		call	z,roll_item_inc
		cp	0
		jr	z,roll_item_key

		push	af			;A=code key

		call	item_roll_clear

		call	print_item		;print the one item
		ld	bc,cur_item_addr
		call	item_sub
		pop	af
		ld	hl,key_loop+3
		ex	(sp),hl
		ret			



roll_item_inc	ld	hl,#5555		;upper limiter
		or	a
		sbc	hl,bc
		ld	a,b			;signal A=0 to avoid artificial key press
		ret	c
		inc	de
		inc	bc
		jr	print_item_roll

roll_item_dec	or	a
		ld	hl,line_len		;HL=length of item, lower limiter
		sbc	hl,bc
		ld	a,h			;signal A=0 to avoid artificial key press
		ret	z
		dec	de
		dec	bc

;IN=DE=start of the name
print_item_roll	push	bc			;save BC
pir2		ld	b,#55			;item len
;		ld	b,line_len
;pir2		ld	hl,#5555		;item line address
		ld	hl,(cur_lin_prn_adr)

		push	de			;save item pointer

pir1		push	de
		push	bc
		ld	a,(de)
		call	CHAR
		pop	bc
		pop	de
		inc	de
		djnz	pir1
		pop	de			;renew item pointer
		
		pop	bc			;renew BC
		xor	a			;avoid key press
		ret		

item_roll_clear	ld	hl,(cur_lin_prn_adr)
		ld	de,spaces
		call	PRINTHL
		ld	de,spaces+17
		jp	PRINT


;marking item=bit 0 in item attr byte
mark_item	ld	hl,(cur_item_addr)
		ld	c,(hl)
		inc	hl
		ld	b,(hl)
		inc	hl
		ld	a,1
		xor	(hl)
		ld	(hl),a			;bit properly set

		bit	0,(hl)
		ld	hl,(mark_counter)
		jr	z,mi1
		inc	hl
		jr	mi2
mi1		dec	hl			;decrease counter
mi2		ld	(mark_counter),hl
		call	prn_mark_items
		call	print_item_attrs	;print only attributes
		jp	key_down



;find item/s
find_items	ld	de,find_txt
		ld	hl,txt_adr_edit_zone1
		call	PRINTHL

		call	input_line
		jp	z,clear_edit_zone	;Z=abort pressed
		ld	b,0			;BC=length of string, HL= start
;		ld	(ix+15),b		;mark counter
;		ld	(txtadd),hl		;zde dodelat case insensitive
		ld	(txtlen+1),bc		
				
		ld	hl,0
		ld	(mark_counter),hl

		ld	hl,(cur_item_addr)
		push	hl			;preserve current item pointer
		ld	hl,basic_end+fi_len-2
		ld	(cur_item_addr),hl	;set start of dir

		scf
		call	un_mark_all		;umark all previously marked items
find1		scf
		call	get_item_name
		ex	de,hl			;DE=start of current item			
;		cp	3			;if these 2 lines left out, will work with LIST full too
;		jr	z,find2
						;DE=item name addr
		call	search_by_Busy
		jr	nz,find2		;NZ=item does not match
		ld	hl,(mark_counter)
		inc	hl
		ld	(mark_counter),hl
		dec	hl
		ld	a,h
		or	l			;preserve 1st matching item
		jr	nz,find4
		pop	hl			;we do not want that old one
		ld	hl,(cur_item_addr)
		push	hl			;HL=1st matching item

;		inc	(ix+15)
find4		ld	hl,(cur_item_addr)
		inc	hl
		inc	hl
		set	0,(hl)			;mark the item as matching
		call	prn_mark_items

find2		ld	bc,cur_item_addr
		call	item_add		;move to next item
		jr	nz,find1		;Z=no more items

		call	clear_edit_zone
		ld	hl,(mark_counter)
		ld	a,h
		or	l
		pop	hl			;HL=matching or starting item
		ld	(cur_item_addr),hl
;		ret	z			;no match found, do nothing
;		pop	bc		;get rid off call
		jp	dir_move3

;mark or unmark all items
mark_all	and	a
		jr	unma1
unmark_all	scf
unma1		call	un_mark_all
		call	prn_mark_items
		jp	kd3		;print_page, draw_cursor

undraw_cursor	ld	hl,(cur_item_addr)
		ld	c,(hl)
		inc	hl
		ld	b,(hl)
		inc	hl
		ld	a,mark_no_cursor
		bit	0,(hl)
		jr	nz,dc_1
		ld	a,unmark_no_curs
		jr	dc_1

draw_cursor	ld	hl,(cur_item_addr)
		ld	c,(hl)
		inc	hl
		ld	b,(hl)
		inc	hl
		ld	a,mark_cursor
		bit	0,(hl)
		jr	nz,dc_1

		ld	a,unmark_cursor
dc_1		ld	hl,(cursor_w_curad)
		ld	b,#20
dc_2		ld	(hl),a
		inc	hl
		djnz	dc_2
;		ld	a,(ix+15)		;???? why ix+15 not in use, ix+21 is in use instead
		xor	a			;A=0 to avoid artifical key press
		ret


wind_cls	ld	hl,#4860		;clears window with AP list
		ld	a,(ix+16)
		add	a,a
		add	a,a
		add	a,a
		ld	b,a
		jp	cls_9_lines



block_asci	ds	10		;blocks converted to ASCII, (used by progress display)
		db	0



;	ld	hl,adr
;	ld	a,2
;	xor	(hl)
;	ld	(hl),a	;spravne nastavovani bitu pres xor, nahradit do nize uvedenych rutin


;change delte TAP flag+ print the change
delete_tap	ld	hl,flag2
		ld	a,(hl)
		xor	%00000010
		ld	(hl),a

delete_tap_prn	bit	1,(ix+3)
		ld	de,yes
		jr	nz,dtp1
		ld	de,no
dtp1		ld	hl,txt_adr_menu1+38
		jp	PRINTHL
		

set_onoff	bit	2,(ix+0)	;find out which wifi is it, MB or el? (el wifi cannot be turned off)
		ret	z
		ld	a,(ix+0)	;set if Wifi will be ON/OFF upon program quit
		bit	3,a
		set	3,(ix+0)
		jr	z,prn_w_onoff
		res	3,(ix+0)
prn_w_onoff	bit	3,(ix+0)	;print WIFI ON/OFF status
		ld	de,off
		jr	z,set_w_onoff_p
		ld	de,on
set_w_onoff_p	ld	hl,txt_adr_menu2+7
		jp	PRINTHL

set_alogin	bit	1,(ix+0)	;set auto login ON/OFF
		set	1,(ix+0)
		jr	z,prn_alogin
		res	1,(ix+0)
prn_alogin	bit	1,(ix+0)	;print auto login status
		ld	de,yes
		jr	z,prn_alogin_p
		ld	de,no
prn_alogin_p	ld	hl,txt_adr_menu1+17
		jp	PRINTHL


set_filename	ld	de,enter_fil_nam	;set new default file name
		ld	hl,txt_adr_edit_zone1
		call	PRINTHL

		ld	de,var_name
		push	de
		call	input_line
		pop	de
;		ret	z			;Z=abort pressed during input
		jp	z,clear_ini_keyl	;Z=abort pressed during input, carry out clear_edit_line+jp to key_loop
		ldir
		ex	de,hl
		ld	(hl),0
		jr	prn_file_start

set_start	ld	de,enter_start_add	;set new default file name
		ld	hl,txt_adr_edit_zone1
		call	PRINTHL
		call	input_line
;		ret	z			;Z=abort pressed during input
		jp	z,clear_ini_keyl	;Z=abort pressed during input, carry out clear_edit_line+jp to key_loop
		ex	de,hl
		call	ConvRStr16
		ld	(var_start),hl

prn_file_start	ld	hl,#5000+(32*1)	;print line 17 with filename and start address
		ld	b,8*1
		call	cls_9_lines
		ld	de,file
		ld	hl,txt_adr_file
		call	PRINTHL
		ld	de,var_name
		call	PRINT
		ld	de,start
		call	PRINT
		ld	hl,(var_start)
		ld	de,block_asci
		push	de
		call	HL2ASCII
		pop	de
pfs1		call	PRINT
		jp	clear_edit_zone
/*
set_user	ld	de,enter_usr
		ld	hl,txt_adr_edit_zone1
		call	PRINTHL

		ld	de,var_user
su1		push	de
		call	input_line
		pop	de
;		ret	z			;Z=abort pressed during input
		jp	z,clear_ini_keyl	;Z=abort pressed during input, carry out clear_edit_line+jp to key_loop
		ldir
		ex	de,hl
		ld	(hl),0
		jr	prn_host_usr_psw

set_pass	ld	de,psw
		ld	hl,txt_adr_edit_zone1
		call	PRINTHL
		ld	de,var_pasw
		jr	su1

set_hide_pass	ld	hl,flag1		;bit 2=hide ftp password,0= no, 1=yes
		bit	2,(hl)
		set	2,(hl)
		jr	z,prn_host_usr_psw
		res	2,(hl)
*/

/*
set_pass
set_hide_pass	
prn_host_usr_psw				;print line 18 with host, username, password
		ld	hl,#5000+(32*2)
		ld	b,8*1
		call	cls_9_lines
		ld	de,host
		ld	hl,txt_adr_hostvar
		call	PRINTHL
		ld	de,host_url
		call	PRINT
		ld	de,user
		call	PRINT
		ld	de,var_user
		call	PRINT
		ld	de,pass
		call	PRINT
		ld	de,var_pasw

		bit	2,(ix+0)
		jr	z,pfs1
		ex	de,hl			;NZ=hide password with XXXXXX
		call	count_com_len

		ld	b,c			;B=length of password
		ld	hl,(HLKO+1)
phup		push	bc
		ld	a,"X"
		call	CHAR
		pop	bc
		djnz	phup
		jr	pfs1+3			;jp clear_edit_zone


set_list_nlst	ld	hl,flag_dir
		bit	4,(hl)
		jr	z,sln1		;Z=LIST->NLST
		res	4,(hl)		;NLST->LIST
		jr	prn_lst_nlst_abbr_ful
sln1		set	4,(hl)

set_full_abbr	ld	hl,flag_dir
		bit	4,(hl)
		jr	nz,sfa1		;NZ=NLST
		bit	2,(hl)
		jr	z,sfa1
sfa2		res	2,(hl)		;set to abbr
		jr	prn_lst_nlst_abbr_ful
sfa1		set	2,(hl)		;set to full

prn_lst_nlst_abbr_ful
		bit	4,(ix+1)
		ld	de,ftp_list
		jr	z,plnaf1	;Z=LIST
		ld	de,ftp_nlst
plnaf1		ld	hl,txt_adr_meziradek+55
		call	PRINTHL
		bit	2,(ix+1)
		ld	de,abbr
		jr	z,plnaf2
		ld	de,full
plnaf2		jp	PRINT
*/

prn_mark_items	ld	hl,(mark_counter)
		ld	de,block_asci
		push	de
		call	HL2ASCII
		ld	a,h
		ld	(de),a
		pop	de
		ld	hl,txt_adr_meziradek+50
		call	PRINTHL
		ld	de,spaces+30
		jp	PRINT

quit_program

;allok		ld	bc,0		;0= all ok (for return to BASIC)
;		push	bc

quit		call	close_server
		call	set_def_wifi_speed	;set speed 7 for easier work with uart terminal
		
		bit	3,(ix+0)	;set Wifi on/off on exit
;		push	af
		call	z,wifi_off	;zjednodušení
;		pop	af
;		scf
;		call	nz,wifi_on

		res	0,(ix+1)	;set dir as not read, so that it will be read once you quit Sarah to Basic and return with RAND. USR 51200

		ld	bc,#783B	;return CPU speed
		ld	a,#30
		out	(c),a
		inc	b
return_speed	ld	a,#55		;speed is 0-3
		out	(c),a

;Busy's solution how to return to Basic
		ld	iy,23610
		ld	hl,(#5CB2)	;ramtop
		ld	(hl),#3E
		dec	hl
		ld	sp,hl
		ld	hl,#1303
		push	hl
		ei
		jp	#1B76






;****************************************************************
;****************************************************************
;****************************************************************


;PARSING
;look for a string in received bytes string, for receive string evaluation purposes
;IN=HL start of the memory area, DE=start of the string we look for, must end with 0, BC=max.len of searched memory, OUT=Z=found, HL=start address of found string in memory, DE=byte coming after found string, NZ=string not found
;10)
look_for_string	ld (lfs5+1),sp
		set 0,(ix+0)

lfs3		ld a,(de)
		cp (hl)
		jr z,lfs_match

		bit 0,(ix+0)		;NZ=no match, was already matching?
		inc hl			;new
		jr nz,lfs2
		dec hl			;new
		pop de			;Z=was matching, now not anymore, renew looked for string
		pop de			;DE=start of searched string
		inc bc
lfs2		set 0,(ix+0)
;		inc hl			;new


lfs1		dec bc
		ld a,b
		or c
		jr nz,lfs3
		inc c			;BC max length reached, set NZ & return
		jr lfs5	

		



lfs_match	bit 0,(ix+0)
		jr z,lfs_alrea_match
		res 0,(ix+0)		;Z=match found, push address
		push de
		push hl

lfs_alrea_match	inc hl
		inc de
		ld a,(de)
		or a
		jr nz,lfs1		;continue macthing if string continues

		ld d,h
		ld e,l			;DE=byte coming after found string
		pop hl			;HL=start address of found string in memory

lfs4		bit 0,(ix+0)
lfs5		ld sp,#5555
		ret			;Z/NZ






;IN=HL=start of looked for string, must end with 0
;OUT=Z=string found, NZ=string not found, RX_buf=all accepted bytes
;11)
look_on_fly	exx
		ld	bc,0
;;		ld	de,print_buffer+#40	;nechat to tam a ulozi do bufferu soucasne
		exx
		ld	e,#FF		;e= flag, Z=found,NZ=not found
		ld	b,e
		ld	c,e		;BC=timeout
lof3		push	hl
lof1		dec	bc		;bugfix 0.3
lof5		scf
		call	rece_byte
		jr	nz,lof4

		exx			;Z= no byte to receive. ESP can just breathe out and then continue
		dec	bc
		ld	a,c
		or	b
		exx
		jr	z,lofend	;Z=really, no byte to collect
		jr	lof5

lof4;;		exx			;nechat to tam a ulozi do bufferu soucasne
;;		ld	(de),a
;;		inc	de
;;		exx
		cp	(hl)
		ld	e,0
		jr	z,lof2		

		dec	e		;NZ,char does not match, bring another byte
		pop	hl
		ld	a,b		;max of 65535 unsucessfull tries
		or	c
		jr	z,lofend1
		jr	lof3		

lof2		inc	hl
		ld	a,(hl)
		or	a
		jr	nz,lof1		;still not end, continue search
		
		
lofend		pop	hl
lofend1		inc	e
		dec	e
		ret			;Z/NZ

;12)
search_by_Busy			;IN=DE= starting item address, BC=its length
;OUT=Z=found, NZ=not found
		push	bc
		exx
		pop	bc	;BC'=delka polozky
		exx
ms3		
txtadd		ld	hl,INPBUFF	;HL=Adresa vzoru
;txtadd		ld   hl,#5555 ; Adresa vzoru zadaneho uzivatelom
txtlen		ld   bc,#5555 ; Dlzka polozky
;		ld   a,b	;muzeme vynechat, retezec nebude nikdy prazdny
;		or   c
;		ret	z
;		jr   z,nasiel
;		push de
		call cpi ; Porovnanie dvoch podretazcov
;		pop  de
		ret	po	;Z=shoda. !!!!! if "ret po" deleted, then will find also 1 char inside string
				;if "ret po" left in code, than 1 char will be found only at start, very handy.
		ret	z	;Z=shoda

		exx		;nasla se shoda v pismenku, jsme na konci?
		ld	a,c
		or	b		;BC'=delka polozky
		jr	z,nenasel	;konec polozky
		dec	bc
		exx
		jr	ms3
		
cpi		ld   a,(de)
		call	convert
		push	af
		ld	a,(hl)
		call	convert
		ld	(hl),a
		pop	af
		cpi		;CP (HL): INC HL: DEC BC: LD A,B: OR C (PO/PE)
		inc  de ; Posun na dalsi znak mena suboru
		ret	nz	;NZ=bajty se nerovnaji
		ret	po	;PO=BC=0=delka vzoru
		jr	cpi

nenasel		inc	a	;set NZ
		ret

convert		cp	"A"
		ret	c
		cp	"Z"+1
		ret	nc
		or	#20
		ret

;13) in=HL, out=next byte after 0, B=length of string
search_until_zero
		ld	b,0
suzero		ld	a,(hl)
		or	a
		inc	hl
		ret	z
		inc	b
		jr	suzero


;RECEIVING
;IN=BC=max.length of data to receive, HL=start of received data, OUT=Z= desired length of data received
;;;;not valid->>>>>, NZ= timeout= data not transfered completely


;6) OUT=HL=RXbuf, BC=#80
receive_data_to_RXbuf
		ld	bc,#80
receive_data_to_RXbuf_bc
		ld	hl,RX_buf
		ld	(hl),0		
rdtRXbuf	push	hl
		push	bc
		call	receive_data_timeout
		pop	bc
		pop	hl
		ld	a,(hl)
		or	a
		ret	nz
		jr	rdtRXbuf

;7)
receive_data_timeout
;DE timeout constant tested in emul (roughly):
;DE=#FFFF	5 sec
;DE=#4000	1,2 sec
;DE=#2000	< 1 sec
;DE=#1000	< 1 sec (6,2x slower than #0290)
;DE=#0290	lightning fast


;		ld de,#290		;timeout, if #80, only app. first #500 bytes carried correctly
rd_timeout	ld de,#290
;		ld de,#1000		;must be enough
receive_data1	ld a,b			;bios1.2, timeout have to increase to #200
		or c
		ret z			;Z=length reached, return
		scf
		call rece_byte
		jr nz,rd1
		dec de			;Z=no byte to receive
		ld a,e			;DE=timeout, in case there is receive interuption
		or d
;		jr nz,receive_data1
;		inc e			;only to set flag to NZ!!
;		ret
		ret z
		jr receive_data1	;do not decrease BC, as no data came in

rd1		ld (hl),a
		inc hl
		dec bc
		jr receive_data1



;SENDING
;IN=HL string (ends with 0), OUT=Z=string has been sent, NZ=not sent, timeout
;1)
send_string	jr ss6			;send_string+1:0 for print info (jr ss6), 18 for not print info (jr ss5)

ss6		push hl			;print it first
		ld hl,txt_adr_module_send
		push	hl
		ld	de,spaces
		call 	PRINTHL
		pop	hl
		pop	de
		push	de		
		call	PRINTHL
		pop hl			;HL=string to be sent

ss5		ld de,0			;maximum timeout

ss2		ld a,(hl)
		or a
		ret z			;Z=end of string

ss1		scf
		call send_byte
		jr nc,ss3
		ld b,a
		ld a,3
		out (254),a		;magenta border= TX line is busy.....
		dec de
		ld a,d
		or e
		jr nz,ss4
		inc e
		ret
ss4		ld a,b			;maybe some timeout could be added here
		jr ss1

ss3		inc hl
		xor a
		out (254),a
		jr ss2


	IFUSED send_data

;IN=HL start of data, BC=data len, OUT=Z=data has been sent
;2)
send_data	ld a,b
		or c
		ret z			;Z=len of data in BC sent
		ld de,0			;maximum timeout

sd1		dec de
		ld a,d
		or e
		jr nz,sd2
		inc e		;set NZ, data not sent
		ret

sd2		ld a,(hl)
		scf
		call send_byte
		jr c,sd1
		inc hl
		dec bc
		jr send_data

	ENDIF


;3) check if busy occuerd, NZ=no busy, Z=still busy, in HL=string to be sent
send_string_busy
		push	hl
		call	send_string
/*					nefunguje
ssb		and	a
		call	rece_byte
		jr	z,ssb
*/

		IFDEF	mb03		

;		ld	b,1
lupen1		ld	hl,#200		;pri spatnem signalu #3000 je ok
lupen		dec	hl		;pri dobrem staci #40
		ld	a,l
		or	h
		jr	nz,lupen
;		djnz	lupen1

		ENDIF

	
		call	receive_data_to_RXbuf
		ld	de,busy
		call	look_for_string
		pop	hl
		ret	nz
		halt
		jr	send_string_busy	;keep sending the command until not busy


;OTHERS
;15)
;IN=HL=command, out=BC=len of command, must end with 0
count_com_len	ld	bc,0		;counts command length, 16 bit length
cc1		ld	a,(hl)
		or	a
		ret	z
		inc	hl
		inc	bc
		jr	cc1




	
;16)
ConvRStr16
;===============================================================
;http://z80-heaven.wikidot.com/math#toc32
;Input: 
;     DE points to the base 10 number string in RAM. 
;Outputs: 
;     HL is the 16-bit value of the number 
;     DE points to the byte after the number 
;     BC is HL/10 
;     z flag reset (nz)
;     c flag reset (nc)
;Destroys: 
;     A (actually, add 30h and you get the ending token) 
;Size:  23 bytes 
;Speed: 104n+42+11c
;       n is the number of digits 
;       c is at most n-2 
;       at most 595 cycles for any 16-bit decimal value 
;===============================================================

     ld hl,0          ;  10 : 210000 
ConvLoop:             ; 
     ld a,(de)        ;   7 : 1A 
     sub 30h          ;   7 : D630 
     cp 10            ;   7 : FE0A 
     ret nc           ;5|11 : D0 
     inc de           ;   6 : 13 
                      ; 
     ld b,h           ;   4 : 44 
     ld c,l           ;   4 : 4D 
     add hl,hl        ;  11 : 29 
     add hl,hl        ;  11 : 29 
     add hl,bc        ;  11 : 09 
     add hl,hl        ;  11 : 29 
                      ; 
     add a,l          ;   4 : 85 
     ld l,a           ;   4 : 6F 
     jr nc,ConvLoop   ;12|23: 30EE 
     inc h            ; --- : 24 			
     jr ConvLoop      ; --- : 18EB



;17)
;IN=HL=number, OUT=DE=address with the string
HL2ASCII		res 7,(IX+0)		;Leading zero suppression
HL2ASCII_00000		ld bc,10000
			call string_dl
			ld bc,1000
			call string_dl
HL2ASCII_000		ld bc,100
			call string_dl
			ld bc,10
			call string_dl
			ld a,l
			add a,'0'
			LD (DE),A
			INC DE
			xor	a
			ld	(de),a
			RET
			
string_dl:		ld a,'0'-1

string_lp2:		inc a
			or a
			sbc hl,bc
			jr nc,string_lp2
			add hl,bc
			CP '0'
			JR Z,string_store

			SET 7,(IX+0)		;Had something other than zero

string_store:		BIT 7,(IX+0)		;Test if still leading zeros
			RET Z						
			LD (DE),A		;If not store it	
			INC DE
			RET	






	IFUSED wait_long
;18)
wait_long	ld	b,3		;wait long time, DE delay fixed (3x 65536 times)
wait_long_def	call	wait_short	;wait long time, DE delay suer defined (Bx 65536 times), IN=B=delay
		djnz	wait_long_def
		ret

wait_short	ld	de,0		;wait short time, DE delay fixed (65536 times) (tady by mela stacit polovicni hodnota tedy #8000)!!!!
wait_short_def	dec	de		;wait short time, DE delay user defined,IN=DE=delay
		ld	a,d
		or	e
		jr	nz,wait_short_def
		ret

	ENDIF

;19)
print_title	ld	hl,txt_adr_cwd
		ld	b,8*2
		call	cls_9_lines

		ld	de,title
		ld	hl,txt_adr_header
		call	PRINTHL
		ld	hl,txt_adr_header_line
		ld	b,title_len
underl		ld	a,34			;""""
		push	bc
		call	CHAR
		pop	bc
		djnz	underl
		ret

;20) save dir position, so that you can renew the dir from where you have been
save_dir_position
;		res	6,(ix+1)
		set	6,(ix+1)		;signal, we enter dir
		ld	hl,(cur_item_addr)	;now, remember position in dir, to return to it
		ld	(cur_item_adr_tmp),hl	;when going updir
		or	a
		ld	de,basic_end
		sbc	hl,de
		ld	(item_checksum),hl
		ret	z
;		jr	z,ed6			;Z=we stand on the very first item
		add	hl,de			;NZ=remember position
		ld	e,(hl)
		dec	hl
		dec	hl
		ld	d,(hl)			;DE=checksum
		ld	hl,item_checksum
		ld	(hl),e
		inc	hl
		ld	(hl),d			;checksum on its place
		ld	hl,(cursor_w_curad)	;now read and save other values
		ld	(cursor_w_curad_tmp),hl
		ld	a,(wind_curs_item)
		ld	(wind_curs_item_tmp),a
		ld	hl,(item_pg_start)
		ld	(item_pg_start_tmp),hl
		ret



;22) by Busy
ConvRStr32

;; Vstup: BC = zaciatok postupnosti cislic ukoncena znakom inym ako cislica
;; Vystup: DEHL = 32 bitova hodnota

	xor	a
	ld	h,a
	ld	l,a
	exx
	ld	h,a
	ld	l,a
	ld	b,a
	ld	c,a
	exx
d32lop	ld	a,(bc)
	sub	'0'
	cp	10
	jr	c,d32ano
	ex	de,hl
	exx
	push	hl
	exx
	pop	hl
	ret

d32ano	ld	d,h
	ld	e,l
	add	hl,hl
	exx
	ld	d,h
	ld	e,l
	adc	hl,hl
	exx
	add	hl,hl
	exx
	adc	hl,hl
	exx
	add	hl,de
	exx
	adc	hl,de
	exx
	add	hl,hl
	exx
	adc	hl,hl
	exx
	ld	e,a
	ld	d,#00
	add	hl,de
	exx
	adc	hl,bc
	exx
	inc	bc
	jr	d32lop

;; Koniec rutinky



;28) (Busy's routine)
HL2ASCII32
;Prevod bin->dec bez znamienka
; in: bin
;out: dec


bindec	ld	hl,zzdec            ;; Vynulovanie buffera dec
	ld	bc,lldec*#0100
cls1	ld	(hl),c
	inc	hl
	djnz	cls1
	ld	c,8*llbin           ;; Opakuj pre kazdy bit bin:
bndc0	call	binx2               ;; vyrotovanie bitu z bin
	ld	hl,eedec
	ld	b,lldec             ;; a zarotovanie toho bitu do dec
bndc2	dec	hl
	ld	a,(hl)
	adc	a,a
	cp	#0a
	jr	c,bndc3
	sub	#0a
bndc3	ld	(hl),a
	ccf
	djnz	bndc2
	dec	c
	jr	nz,bndc0

	ld	hl,zzdec
	ld	b,10		;; change it to ASCII
prev_ascii32
	ld	a,(hl)
	or	a
	jr	nz,asc32n
	inc	hl
	djnz	prev_ascii32

	inc	b		;osetreni 0
	dec	hl		;udelej misto pro zobrazeni "O"

asc32n	ld	d,h
	ld	e,l		;; DE=ASCII number start

asc32_n	ld	a,#30
	or	(hl)
	ld	(hl),a
	inc 	hl
	djnz	asc32_n
	ret

binx2	ld	hl,zzbin            ;; Nasobenie bin dvomi
	ld	b,llbin
	xor	a
bnxx2	rl	(hl)
	inc	hl
	djnz	bnxx2
	ret

zzbin	db	#ff,#ff,#ff,#ff		;vstup v bajtech
eebin
llbin	=	eebin-zzbin

zzdec	db	0,0,0,0,0		;vystup v ASCII
	db	6,5,5,3,5
eedec
lldec	=	eedec-zzdec
	db	0			;zarážka pro tisk

;29)input: timeout in (ix+6), output, Z="OK" found, NZ=answer "OK" not found
send_at_check_ok
		call	wifi_wait
		ld	hl,at
		call	send_string
		ld	bc,28
		jp	nz,error_handler
;		jp	nz,error

;		ld	de,#100			;when it is away, it speeds up the search significantly
;		call	wait_short_def

		ld	hl,RX_buf
		ld	bc,20
		push	hl
		push	bc
		ld	de,RX_buf+1		;clear RX_buf before reading bytes to it
		ld	(hl),0
		ldir
		pop	bc
		pop	hl
		push	hl
		push	bc
		call	receive_data_timeout	;much quicker than look_on_fly
		pop	bc
		pop	hl
		call	wifi_unwait
		ld	de,ok
;		jp	look_for_string
		call	look_for_string
		ret	z
		dec	(ix+6)
		jr	nz,send_at_check_ok
		inc	c			;NZ=OK not found
		ret


;40) OUT CY 0=OK, 1= speed not supported, set and print UART speed
set_print_speed	scf
		push	af
		call	set_baud
		pop	de
		ld	a,d		;A=speed 0-23 we set
print_speed	push	af
ps2		ld	de,TX_buf
		push	de
		ld	h,0
		ld	l,a
		call	HL2ASCII
		ld	a,32
		ld	(de),a
		inc	de
		ld	(de),a
		xor	a
		inc	de
		ld	(de),a
		pop	de
		ld	hl,(HLKO+1)
		push	hl
		ld	hl,txt_adr_speed_login+7
;		ld	(HLKO+1),hl
		call	PRINTHL
		pop	hl			;return print position
		ld	(HLKO+1),hl
		pop	af			;CY/NC
		ret



;48)
convASCII2hex
;IN=HL ASCII number address (2 digits) eg. hl,14
;OUT=A=#14
		ld	a,(hl)
		and	%00001111
		sla	a
		sla	a
		sla	a
		sla	a
		ld	e,a
		inc	hl
		ld	a,(hl)
		and	%00001111
		or	e
		ret

;49) clear whole screen
cls_4000	ld	hl,16384
		push	hl
		ld	de,16385
		ld	bc,6144
		ld	(hl),0
		ldir
		ld	bc,767
		ld	(hl),7
		ldir
		pop	hl
		ret

;50) clear 9 lines, if called from "cls_9_hl", 9 lines starting from HL will be cleared
;if called from "cls_9_lines", the input must be HL=starting address, and B= number of micro lines to clear
;cls_9		ld	hl,#4000+(32*3)
;cls_9_hl	ld	b,8*9
cls_9		ld	hl,txt_adr_dir_display		;#4800
		ld	b,8*8
cls_9_lines	push	hl
		xor	a
		ld	e,#20
cls_91		ld	(hl),a
		inc	hl
		dec	e
		jr	nz,cls_91
		pop	hl
		call	DOWNHL
		djnz	cls_9_lines
		xor	a		;A=0 to return to key_loop
		ret

;54) clears edit zone (2 bottom lines)
clear_edit_zone	ld	b,2*8
		ld	hl,#50C0
		jr	cls_9_lines



	IFUSED prn_ap_name_sig
		
;51) INPUT:de=print position
prn_ap_name_sig	ld	hl,at_cwjap_cur?	;find out current AP
prn_ap_name_sig_def
		push	de			;DE=print position
		call	send_string
;		ld	bc,1			;error to be solved
;		jp	nz,error

		ld	hl,RX_buf		;receive the answer to RX_buf
		ld	bc,100
		push	hl
		push	bc

		ld	a,(ix+2)
		cp	3
		jr	nc,prap1
		ld	de,#D00			;experimental tomeout, it works!!
		call	receive_data1		;for speeds <3 we need bigger receive timeout
		jr	prap6


prap1		call	receive_data_timeout

prap6		pop	bc			;BC=100
		pop	hl			;HL=RX_buf


		push	hl			;HL=RX_buf
		push	bc			;BC=100

		ld	de,ok			;new concept, look for "OK", if not then "No AP"
		call	look_for_string
		jr	z,ap_answ_found


prap2		pop	bc			;BC=100, OK not found
		pop	de			;RX_buf
prap5		pop	hl			;print position


		push	hl			;print position
		ld	de,spaces
		call	PRINTHL

		ld	de,no_ap
		pop	hl			;HL=print position
		res	5,(ix+0)		;probably not needed
		jp	PRINTHL			;No AP


						;check type of answer	
ap_answ_found	pop	bc			;BC=100
		pop	hl			;HL=RX_buf
		push	hl
		push	bc
		ld	de,no_ap
		call	look_for_string
		jr	z,prap2			;"No AP" found, end
		pop	bc			;BC=100
		pop	hl			;HL=RX_buf

		ld	de,dvojt
		call	look_for_string
		jr	nz,prap5		;":" not found, probably nonsence returned
						;due to eg. speed module will not communicate
		ex	de,hl			;AP name found
		ld	de,uvoz
		inc	hl
		push	hl
		call	look_for_string
		ld	(hl),0

		pop	hl			;HL=start of AP name
		pop	bc			;BC=print position (push pop=)

		inc	de			;move behind "."
		ex	de,hl
		push	hl
		ld	h,b
		ld	l,c
		call	PRINTHL


		pop	hl			;HL next positin in receive string
		ld	bc,100

		ld	de,carka
		push	de
		call	look_for_string

		ex	de,hl
		pop	de
		push	de
		call	look_for_string

		pop	de
		push	hl			;HL=signal intensity
		inc	hl			;DE=hledame carku
		ld	c,4
		call	look_for_string
		jr	z,prap3
		pop	hl			;start of signal intensity
		push	hl

prap4		ld	a,(hl)
		cp	#D
		jr	z,prap3
		inc	hl
		jr	prap4


prap3		ld	(hl),0

		pop	de			;print signal inensity
		push	de
		call	PRINT
		pop	de
		inc	de
		inc	de
		call	ConvRStr16
		ld	a,l
		ld	de,excellent
		cp	50			;print text level of signal intensity
		jr	c,prn_sig_txt
		ld	de,good
		cp	60
		jr	c,prn_sig_txt
		ld	de,fair
		cp	70
		jr	c,prn_sig_txt
		ld	de,poor
		
prn_sig_txt	call	PRINT
		ld	de,spaces+25
		jp	PRINT


	ENDIF


;52) gets status (if wifi is connected to FTP or not).
;OUT=A=status "2"-"5"
;OUT sets status 5,ix+0, 0 not logged in, 1 logged in
;OUT=Z=A=3 host ok, login ok
;NZ=5= host x, login x, module not even connected to AP
;NZ=4= TCP or UDP disconnected
;NZ=3= TCP or UDP connected, but not ftp (only internal state of routine), routine will change it to status 4, NZ)
;OUT="current_host" either X, if not connected to ftp server, or host name if connected
;OUT=Z=HL=code returned by NOOP, NZ=HL=0
get_status_ftp	call	clear_rece_buf

		ld	de,STATUS
		ld	hl,txt_adr_speed_login+27
		call	PRINTHL

		ld	hl,cipstatus		;find out, if module connected to wifi
		call	send_string
;		ld	bc,1
;		jp	nz,speed_m_error	;spatne, zasobnik!!!

		ld	hl,RX_buf		;receive the answer about STATUS to RX_buf
		ld	bc,#80
		push	hl
		push	bc

/*
		ld	a,(ix+2)
		cp	3
		jr	nc,gs1
		ld	de,#500			;experimental timeout, it works!!
		call	receive_data1		;for speeds <3 we need bigger receive timeout
		jr	gs2		
*/

		ld	de,#390			;own wait constant
		call	receive_data1
;gs1		call	receive_data_timeout

gs2		pop	bc		;BC=#80
		pop	hl		;HL=RX_buf

		push	hl
		push	bc
		ld	de,ok		;look for OK in the answer
		call	look_for_string
		jr	z,gs5


		pop	hl		;OK not found
		pop	bc
gs4		ld	de,unknown	;STATUS not found
		ld	hl,txt_adr_speed_login+35
		jp	PRINTHL
;		ld	de,spaces+8
;		jp	PRINT


gs5		pop	bc		;BC=20
		pop	hl		;HL=RX_buf
		ld	de,STATUS
		call	look_for_string
		jr	nz,gs4

		ld	a,(de)		;A=status number ("2"-"5")
		push	de
		ld	(ix+22),a	;status_byte
		ld	hl,block_asci+1	;status= 2,4 or 5- use the same behaviour
		ld	(hl),0
		dec	hl
		ld	(hl),a
		ex	de,hl
		push	af
		ld	hl,txt_adr_speed_login+35
		call	PRINTHL		;print status
		pop	af

		pop	de
		cp	"3"
		set	5,(ix+0)
		jr	z,gs7

gs3		res	5,(ix+0)
		ld	hl,0
		push	hl		;HL=0 if status<> "3"
		push	af		;push NZ state
		ld	hl,#2058	;"X"+#20
;		ld 	hl,"X"		;"X"+0
		xor	a
		ld	(current_host),hl
		ld	(current_host+2),a
gs10		ld	(current_login),hl
gs9		call	print_ftp_status
		pop	af
		ld	a,(ix+22)	;A=status
		pop	hl		;if Z then contains ftp code returned after noop, if NZ, HL=0
		ret			

gs7		ex	de,hl		;A="3",we are connected with TCP or UDP protocol
		ld	b,3
gs6		push	bc
		ld	c,#60
		ld	de,uvoz
		call	look_for_string
		ex	de,hl
		pop	bc		;zde by měla být kontrola chyby
		djnz	gs6
		push	hl		;HL=start of URL
		ld	de,uvoz
		ld	c,#40
		call	look_for_string	;zde by měla být kontrola chyby
		ld	(hl),0
		pop	hl		;HL=start of URL
		or	a
		ex	de,hl
		sbc	hl,de
		ld	b,h
		ld	c,l
		add	hl,de
		push	hl		;HL=čárka, now we look for port number 21
		ex	de,hl
		ld	de,current_host
		ldir
		xor	a
		ld	(de),a
		push	af		;jen aby SP sedelo
;		ld	(current_login+2),a
		ld	hl,#4B4F	;if status="3" and we are connected to the server, print OK after "Logged in:", #4B4F= "OK"
		jr	gs10
;		jr	gs9		;print result and ret
;		call	PRINT		;print ftp server name


					;new in 0.99. ftp port can be even different from 21, eg. mobile phone ftp servers, eg: 192.168.0.200:31547

;		pop	hl

/*
		inc 	hl		;therefore, we try not to check the port
		ld	a,(hl)
		cp	"2"
		jr	nz,gs8		;NZ=port 21 not found!!! we should unconnect and reconnect on port 21!!!
		inc	hl
		ld	a,(hl)
		cp	"1"		;if port is not 21, then status is 3, but we are not connected to ftp port 21, therefore NZ
		jr	nz,gs8
*/

/*not needed for zxdb			;ok, we are successfuly connected to ftp server, now find out if we are logged in
		call	send_noop
		push	hl		;HL=code returned by NOOP
		ld	hl,#2058	;"X"+#20
;		ld 	hl,"X"
		inc	h
		dec	h		;set NZ
		push	af
		jr	c,gs10		;C=we are not logged in
		pop	af
		pop	hl
		ld	hl,ok_
		ld	de,current_login
		ld	bc,3
		ldir
		push	bc		;BC=0
		xor	a		;set Z
		push	af
		jr	gs9
*/

/*
gs8		ld	hl,at_cipclose	;status 3 but not connected to FTP (probably from previous connection), so close connection
		call	send_string
		ld	hl,ok		;add error check!!!
		call	look_on_fly
		jp	nz,cr_err
		ld	a,"3"
		inc	a		;set NZ
		ld	(ix+22),a	;set status 4
;		xor	a
;		inc	a		;set NZ
		jp	gs3
*/

;53) sets speed 155200 Bd on module and UART
set_def_wifi_speed

		ld	a,7
		jr	set_ud_speed_a


;55) a key
akey		call	wifi_unwait

akey1		call	inkey
		or	a
		jr	nz,akey1	;release previous press

akey2		ld	de,akey_txt
		ld	hl,txt_adr_edit_zone2+#30
		push	hl
		call	PRINTHL

		call	KEYSCAN
		pop	hl
		push	af
		ld	de,spaces+16
		call	PRINTHL
		pop	af
		or	a
		jr	z,akey2
		ret

;56) wifi_wait/unwait	signals, that we wait for wifi
wifi_wait	push	af
		ld	a,15
		jr	wifi_unwait1
wifi_unwait	push	af
		ld	a,7
wifi_unwait1	ld	(23295),a
		pop	af
		ret

;57) align speed on UART and module, speed taken from IX+2. If IX+2=255, align at max MBFW speed. Or speed taken from A,
; then call set_user_defined_speed_a
;mostly for usage in SM2.0
;INPUT=D=currently aligned speed. If D=255 continue with speed align, if 0-23 compare with
;speed you want to align, if same, do not continue.
set_ud_speed
		ld	a,(ix+2)
		inc	a
		jr	z,set_mspeed_mbfw
		dec	a
		jr	set_ud_speed_a
set_mspeed_mbfw	ld	a,(ix+8)		;A=max speed allowed for given MBFW
		cp	23
		jr	nz,set_ud_speed		;if 23, change it to 16 (max speed for module)
		ld	a,16

set_ud_speed_a	cp	24			;check if speed is within 0-23
		ld	bc,33
		jr	nc,suds5		;NC=speed >23, error
						
		ld	b,a			;A=speed to which we want align UART and module
		ld	c,b			;C=speed to which we want align UART and module
		cp	d			;D=currently aligned speed
		ret	z			;Z=speed we want is already aligned
		
		ld	hl,speed_table		;find the speed eg. "16= 3456000"
		or	a
		jr	z,suds2
sudsa1		ld	a,(hl)
		or	a
		inc	hl
		jr	nz,sudsa1
		djnz	sudsa1
suds2		ld	a,(hl)			;HL= Baud speed (eg. "3456000")
		cp	" "
		inc	hl
		jr	nz,suds2
				

		push	bc			;C= UART speed we want

		ld	de,TX_buf		;now prepare AT comand to TX_buf
		push	de
		push	hl			;HL=start of eg "3456000"
		ld	hl,uart
		ld	bc,12
		ldir

		pop	hl			;HL=start of eg "3456000"		
		push	hl
suds3		ld	a,(hl)
		or	a
		inc	bc
		inc	hl
		jr	nz,suds3
		dec	bc
		pop	hl
		ldir

		ld	hl,_8100
		ld	bc,10
		ldir		
		ex	de,hl
		ld	(hl),0
		
		pop	hl
		call	send_string	;set user defined speed on the module via AT+UART+CUR

		ld	hl,ok		;for speeds<3 seems to work
		call	look_on_fly	;correct answer OK means, we aligned speeds sucesfully

		pop	de
		ld	bc,31		;OK not received in answer
		jr	nz,suds1
		

		ld	a,e			;A= UART speed we want
		call	set_print_speed		;set the desired speed in UART
		ret	nc			;NC=set sucessfully

		ld	bc,32

suds1		push	bc
		ld	a,e
		call	set_print_speed
		call	print_bauds
		pop	bc

suds5		pop	hl
		jp	error_handler
;		jp	error


;58) sends abort command (in order to abort existing command and get unstuck)
;send_ftp_command is the main routine that sends command+ string if needed and returns ftp answer code
;IN=A=0=command only (eg. "ABORT"). A=1=comamnd+prepared string from INPUT (eg. "STOR filename"), A=2=command+new INPUT
;OUT=A=0=NC=sent ok, awaited ftp code returned correctly
;A=1=C=c=1=error sending string
;A=2=C=c=11=error in module answer (expected answer ">" not returned)
;A=3=C=c=4=sent ok, but return code other than expected
/*
send_abort	xor	a
		ld	hl,ftp_abor
*/
send_ftp_command
		push	de		;DE=ftp code we expect
		push	hl
		push	af
		ld	bc,0
sfc4		ld	a,(hl)
		cp	#20		;count length of command, some servers won't accept spaces
		inc	bc
		inc	hl
		jr	nz,sfc4
		ld	de,TX_buf	;universal routine, send any command in HL
;		ld	bc,5
		pop	af
		pop	hl
		ldir

		cp	1
		jr	c,sfc1		;C=command only
		jr	z,sfc2		;Z=command+ prepared text

		push	de		;NC=command+ new input parameter (string)
		call	input_line
		jr	nz,sfc3
		jp	z,clear_ini_keyl	;Z=abort pressed during input, carry out clear_edit_line+jp to key_loop
;		jp	initsp_keyloop	;Z=input aborted, init SP+ jp key_loop

sfc3		pop	de
		ldir
		jr	sfc1

sfc2		call	input_l_count
		ldir

sfc1		ld	hl,eol
		ld	bc,3
		ldir

		call	clear_edit_zone
;rutina volně navazuje na cipsend_request hned dole


;60)
;funkcni, hledame starou verzi
;IN=HL=string we look for in answer
;OUT=NC=A=0=all ok, C=error, A=1=busy=resend the command, A=2=string not found
check_module_answer				;new concept
		push	hl			;string we look for
		call	clear_RX_buf
chma11		ld	bc,TX_buf-RX_buf
		ld	hl,RX_buf
		ld	(hl),b			;zero first byte in RX_buf
		push	hl			;HL=RX_buf
		push	bc			;BC=length to seek
		call	receive_data_timeout
		pop	bc			;BC=length to seek
		pop	hl			;HL=RX_buf

		ld	a,(hl)
		or	a
		jr	z,chma11		;endless loop if module does not return anything!!!!

		push	hl
		push	bc
		ld	de,busy
		call	look_for_string
		pop	bc
		pop	hl
		jp	z,sfcerr1		;scf:ld a,1:ret=busy detected

		pop	de			;DE=string we look for

		call	look_for_string
		jp	nz,sfcerr2		;scf:ld a,2:ret= string not found

		call	clear_rece_buf		;clear the incoming "OK"
		jp	loop1_eha3		;=xor a:ret= set C=0, A=0





;61) !!!"receives_ftp_answer" is a part of send_ftp_command routine. Programs enters it with 2 byte ftp code on the stack!!!
;"receive_ftp_answer_isolated" can be used extra. IN=DE=ftpcode we expect

;OUT=HL=received code number, (not valid anymore and not neededDE= start address of message to print), NC=code found, C=code not found, DE= second code or 0 if none
;OUT=error code 11=error in receiving answer "OK"
;OUT=prints ftp report on the screen (temporarily, in future I want to have 3 lines for ftp report)
;!!!!!toto se dá použít na tisk chyb z ftp, pokud se vracíme s NC, pak v HL máme kod, ktery ftp vraci, muzeme je tisknout, zatim neimplementovano.
receive_ftp_answer_isolated
		push	de			;DE=code we expect
receive_ftp_answer				;new concept
;		ld	(ix+6),2		;was 0	
;		push	hl			;HL=code number we want
		call	clear_RX_buf
rfa3		ld	hl,ok			;look for "OK"
		call	look_on_fly		;lasts 4 seconds. 1x attempt is enough
		jr	z,rfa2
;		dec	(ix+6)
;		jr	nz,rfa3
		ld	c,36
		jp	error_handler
;		jr	nz,sfcerr2
;		jr	nz,rfa_err

;		halt				;testing purposes

rfa2		call	look_for_ipd
		jr	nc,rfa1
		jp	error_handler
rfa1		ld	b,h
		ld	c,l
		ld	hl,RX_buf
		push	hl
		call	receive_data_timeout	;receive ftp message to RX_buf
		ld	(hl),0
		pop	de			;RX_buf starts with code number
		push	de			;DE=start of message to print
		call	ConvRStr16
		pop	de			;DE=start address of message to print
		push	hl			;HL=code number we received
		ld	h,d			;na zasobniku mame: code we received, code we want
		ld	l,e			;HL=start address of message to print

		call	print_ftp_message

		pop	hl			;HL=code number we received
		pop	bc			;BC=code we expect
		or	a
		sbc	hl,bc
		add	hl,bc			;HL=code we obtained
;		jp	nz,sfcerr3
		push	af		;preserve Z flag

		push	hl		;HL=code we obtained
		ld	hl,RX_buf
		ld	bc,#80
		ld	de,eol
		call	look_for_string		;ošetřit? ukončení řádku je vždycky!!
		push	de			;DE=start of message 2
		ld	a,(de)
		or	a
		jr	z,rfa4			;Z=second message does not exist

		call	ConvRStr16
		pop	de			;DE=start of message 2
		push	hl		;HL=2nd code number
		ld	h,d
		ld	l,e
		call	print_ftp_message

		pop	de		;DE=2nd code number
		pop	hl		;HL=1st code number
		pop	af
		jp	nz,sfcerr3
		jr	rfa5		

rfa4		pop	de			;DE=indifferent
		ld	de,0			;DE=0=signal no 2nd code came in
		pop	hl			;HL=1st code we obtained
		pop	af
		jp	nz,sfcerr3

rfa5		xor	a			;set A=0 and NC=all ok
		ret
		

;rfa_err		ld	c,11
;		jp	display_error
		

;62) clears RX and TX buffers, or only one of them
clear_buffers	ld	bc,buffers_len-1
cb2		ld	hl,RX_buf
		ld	de,RX_buf+1
cb1		ld	(hl),0
		ldir
		ret
clear_RX_buf	ld	bc,buffers_len/2-1
		jr	cb2			
clear_TX_buf	ld	hl,TX_buf
		ld	de,TX_buf+1
		ld	bc,buffers_len/2-1
		jr	cb1

;error handling: (Busy's solution)
;main routine 	ld 	sp,stack
;		...
;		call	subrout1
;		...
;		call	subrout2
;		etc.
;
;subrout1	...
;		jp	error_handler
;		...	
;		and	a	;NC=all ok
;		ret
;
;error_handler	ld 	sp,stack-2
;		call	display_error
;		scf	;C= was error
;		ret
;63) prints error message, IN=register C=error number, OUT=print output, then returns to the main program
;may be called at any time from any place
;if "error_handler" called, it jumps to the highest level, if "display_error" is called, it returns,
;to from where it has been called
;3 types:
;display_error=		only displays error number from C input an returns with carry
;error_handler=		displays error and jumps to error_handler_loop
;error_handler1=	only jumps to error handler loop

error_handler	push	bc
		call	passive_receive_off	;hope it does not affect other routines
		call	wifi_unwait
		pop	bc			;C=error code
		call	display_error
		call	set_3_5mhz
		call	close_server
;error_handler1	call	wifi_unwait
;		ld	a,c
;		cp	4
;		jp	z,server_connection_error

		bit	6,(ix+3)
		call	z,delete_file
		ld	ix,flag1
		
		ld 	sp,basic_end-2	;Busy's universal solution. Jump back to "error_handler_loop"
		ret

display_error	set	4,(ix+0)	;PRINT again. Return back to from where you were called.
		ld	h,0
		ld	l,c		;C=error code
		ld	de,block_asci
		push	de
		call	HL2ASCII
		ex	de,hl
		ld	(hl),0
;		call	clear_edit_zone
		ld	hl,txt_adr_edit_zone2
		ld	de,error_txt
		call	PRINTHL
		pop	de
		call	PRINT
		call	akey
		call	clear_edit_zone
		scf
		ret

/*
;error_handler	ld 	sp,stack-2	;Busy's universal solution. Jump back to "key_scan"
error_handler	ld 	sp,stack-2	;Busy's universal solution. Jump back to "error_handler_loop"
		dec	sp
		dec	sp
;		call	display_error
;		scf	;C= was error
;		ret

display_error	set	4,(ix+0)	;PRINT again. Return back to from where you were called.
		ld	h,0
		ld	l,c		;C=error code
		ld	de,block_asci
		push	de
		call	HL2ASCII
		ex	de,hl
		ld	(hl),0
;		call	clear_edit_zone
		ld	hl,txt_adr_edit_zone2
		ld	de,error_txt
		call	PRINTHL
		pop	de
		call	PRINT
		call	akey
		call	clear_edit_zone
		scf
		ret
*/


;64)
;connect to server, main text must be prepared in TX_buf, sends commands eg. "AT+CIPSTART=1,"192.168.112",21"
;OUT=NC=all ok, else error
cipstart_connect
		ld	hl,cipstart		;make connection to server
		call	send_string		;send "AT+CIPSTART="
		ld	c,3
		jp	nz,error_handler	;connect to server
cs_con_buf	ld	hl,TX_buf		;send it
		call	send_string
		ld	c,3
		jp	nz,error_handler

		ld	hl,CONNECT		;new concept
;		ld	de,CONNECT		;check for answer
		jp	check_module_answer



;65) connect_to_server	connect to server+ test status, OUT=Z=connected, NZ=not connected, error 44= connection not successful
connect_to_server
		ld	(ix+6),#A		;10x retry
		call	wifi_wait
cts		call	get_status_ftp
		cp	"5"
		jr	z,connect_to_server	;if wifi not connected to internet, keep trying
		cp	"3"
		jr	nz,cts1
		jp	wifi_unwait
;		ret	z			;Z=all ok
cts1		call	connect_to_zxdb_server
		dec	(ix+6)
		jr	nz,cts
		call	move_server_number	;if this server fail, try another one
		ld	c,45			;error-1
		inc	c			;set NZ flag as error
		ret					
		

/*
;65)
;sends PASV command to ftp server+ sends connection command "AT+CIPSTART=1,"TCP","h1,h2,h3,h4",port"
;OUT=C=success
enter_passive_mode
		ld	hl,ftp_pasv
		xor	a			;A=0=command only
		ld	de,227
		call	send_ftp_command
		ld	c,41
		jp	c,error_handler

;		ld	de,TX_buf
;		ld	bc,4
;		ldir
;		ld	hl,eol
;		ld	bc,3
;		ldir
;		call	cipsend_request

		ld	hl,TX_buf	;now, enter passive mode with
					; "AT+CIPSTART=1,"TCP","h1,h2,h3,h4",port"
		ld	(hl),"1"
		inc	hl
		ld	(hl),","
		inc	hl
		ex	de,hl
		ld	hl,tcp
		ld	bc,7
		ldir
		push	de		;DE=where to continue with LDIR to TX buffer

		ld	de,zavorka_leva
		ld	hl,RX_buf
		ld	bc,75
		call	look_for_string
		jr	nz,epm4			;NZ=error, string not found
;		jp	nz,error_handler
		
		ld	b,4			;you found start of (h1,h2,h3,h4,p1,p2), now get the numbers
epm1		push	bc
		push	de			;DE points to source IP address
		ld	de,carka
		call	look_for_string
		jr	nz,epm4
		pop	de
		or	a
		sbc	hl,de			;L=length of IP address part
		ex	de,hl			;HL=source
		pop	bc			;cykly
		ld	c,e			;C=délka části adresy	
		pop	de			;TX buffer

epm2		ld	a,(hl)			;nasypej část IP adresy do TX bufferu a zakonči tečkou
		ld	(de),a
		inc	de
		inc	hl
		dec	c
		jr	nz,epm2
		ld	a,"."
		ld	(de),a
		inc	de
		push	de			;next TX buffer position
		inc	hl
		ld	d,h
		ld	e,l			
		djnz	epm1

		pop	de
		dec	de
		ld	a,34
		ld	(de),a			;IP address is complete in TX buffer
		inc	de
		ld	a,","
		ld	(de),a
		inc	de
		push	de			;DE=next TX buffer position
		
						;HL=port address start (p1,p2)
		ex	de,hl
		call	ConvRStr16
		push	hl			;L=higher port number
		inc	de
		call	ConvRStr16
		pop	de			;L=lower port number
		ld	h,e			;HL=port number
		ld	de,block_asci
		push	de
		push	de
		call	HL2ASCII
		pop	hl
		ex	de,hl
		or	a
		sbc	hl,de
		push	hl
		pop	bc
		pop	hl			;TX pos in buffer
		pop	de
		ldir				;port number in the place
		ld	hl,eol
		ld	bc,3
		ldir

		call	clear_RX_buf
		ld	(ix+6),5
epm3		call	cipstart_connect	;connect to ftp server and evaluate answer
		ret	nc		;NC=all ok, we are connected to server
		dec	(ix+6)
		jr	nz,epm3
epm4		ld	c,9
		jp	error_handler
*/
;66)
;inputs character. OUT=C=length of input string, HL= start of string=INPBUFF
;OUT=NZ=data inputed, Z=abort pressed, A=4
input_line	push	ix
		ld	ixh,64
		call	INPUT
		pop	ix

		cp	4
;		ld	c,a
		ret	z
;		jp	z,error_handler

input_l_count	ld	hl,INPBUFF+254		;username
		ld	bc,255		

il1		ld	a,(hl)
		cp	#20		;count from behind, correct way
		jr	z,il3		;C=length of username
		or	a
		jr	nz,il2
il3		dec	c
		dec	hl
		jr	il1
il2		inc	hl
		ld	(hl),e		;put 0 to input text end, to print it easier
		ld	hl,INPBUFF
		dec	c
		inc	c
		jr	z,input_line	;empty line inserted, try again
		ret

clear_inp_buf	ld   hl,INPBUFF
	        ld   b,255
cib		ld   (hl),32
        	inc  hl
	        djnz cib
		ret


/*
;68) sends purely AT+CIPSEND=1,data length
cipsend_lod	ld	hl,at_cipsend	;send "AT+CIPSEND="
		call	send_string
		jp	nz,cr_err

		ld	a,"1"
		ld	hl,TX_buf
		push	hl
		ld	(hl),a
		inc	hl
		ld	(hl),","
		inc	hl
		ld	(hl),0
		pop	hl
		call	send_string	;"1,"
		jp	nz,cr_err

;		ld	hl,(portion_len)
		ld	hl,(ftp_upl_len)
		ld	de,block_asci
		push	de
		call	HL2ASCII
		xor	a
		ld	(de),a
		pop	hl
		call	send_string	;number of bytes to be sent
		jp	nz,cr_err
		ld	hl,eol
		call	send_string
		jp	nz,cr_err

		ld	hl,char_zob	;new concept
;		ld	de,char_zob	;Wifi module returned ">" to show readiness to accept?
		jp	check_module_answer
;		call	check_module_answer	;show error, if any
;		jp	c,ret2main_scr	;was error
;		ret
*/
;69) display_progress	prints length of file each time file is loaded/saved, length taken from rest_len
display_progress
		ld	hl,txt_adr_progress+10
		push	hl
		ld	de,spaces+20
		call	PRINTHL

dp1		ld	hl,rest_len
		ld	de,zzbin
		ld	bc,4
		ldir
		call	HL2ASCII32
		ld	de,zzdec-1
dp0		inc	de
		ld	a,(de)
		or	a
		jr	z,dp0

		pop	hl
		jp	PRINTHL

;70) look_for_ipd	looks for IPDs in the incoming bytes
;OUT=A=connection number (0-3),HL= length of data (eg. +IPD,0,25:)
;!!!! in active mode returns eg: +IPD,0,25:331 Password required....
;!!!! in passive mode returnseg: +IPD,0,11. 11 bytes data then have to be fetched with AT+CIPRECVDATA=0,11.!!!

look_for_ipd	ld	(ix+6),3		;(was 0), 3 attempts for each search
		call	clear_RX_buf
lfi7		ld	hl,ipd			;"+IPD,"
		call	look_on_fly
		jr	z,lfi3
;		dec	(ix+6)
;		jr	nz,lfi7
lfi_err		ld	c,12
lfi_err1	scf				;set C to indicate error
		ret
;		jp	error_handler

lfi3		scf
		call	rece_byte
		jr	nz,lfi4
		dec	(ix+6)
		jr	z,lfi_err
		jr	lfi3

lfi4		sub	#30
		push	af			;A=channel number (0-3)

lfi2		scf
		call	rece_byte
		jr	nz,lfi5
		dec	(ix+6)
		jr	z,lfi_err
		jr	lfi2

lfi5		ld	de,RX_buf		;now read length of data
		push	de
look_f_ipd_loop	scf
		call	rece_byte
		jr	nz,lfi6
		dec	(ix+6)
;		jr	z,lfi_err
		jr	nz,look_f_ipd_loop
		pop	af
		pop	de			;get SP correct
		ld	c,35
		jr	lfi_err1

lfi6		cp	":"
		jr	z,lfi1
		ld	(de),a
		inc	de
		jr	look_f_ipd_loop

lfi1		pop	de			;HL=RX_buf
		call	ConvRStr16		;now parse lenght of data
		pop	af			;A=channel number, HL=lenght of data
		scf
		ccf				;set NC for all ok
		ret				;next reading of bytes brings data



;73)prints status of URL and login
print_ftp_status
		ld	de,logged_in
		ld	hl,txt_adr_speed_login+12
		call	PRINTHL
		ld	de,current_login
		call	PRINT
		ld	hl,txt_adr_host
		ld	de,host
		call	PRINTHL
		ld	de,spaces
		push	de
		call	PRINT
		ld	de,current_host
		ld	hl,txt_adr_host+6
		call	PRINTHL

		pop	de		;DE=spaces
		ld	hl,txt_adr_host+23
		push	hl
		call	PRINTHL

		call	fs1
		ex	de,hl
/*
		ld	a,(server_flag)
		ld	de,connectserver01
		cp	2
		jr	c,pfs0
		ld	de,connectserver3
		cp	3
		jr	c,pfs0
		ld	de,connectserver2
;		ld	de,host_zxdb
;		or	a
;		jr	z,pfs0
;		ld	de,host_hood
*/
pfs0;		ld	hl,txt_adr_host+23
		pop	hl		;txt_adr_host+23
;		push	af
		ex	af,af'
		ld	a,(de)
		cp	1
		ret	z		;server 1, but private and hidden

		call	PRINTHL
;		pop	af
		ex	af,af'
		cp	1
		ret	nz
		ld	de,http_server1+10
		jp	PRINT

		

/*
;74) send "noop" command
;OUT=NC="noop" successfuly accepted, all ok
send_noop	xor	a
		ld	hl,ftp_noop		;send NOOP command
		ld	de,200			;ftp code we accept
		call	send_ftp_command
		ret
		ret	nc			;should be here
		ld	c,42
		jp	error_handler
;		jp	send_ftp_command
;		ret
;		jp	nc,key_loop		;!!!!!!!!NC=expeted ftp code returned, OK, we are connected and logged in
*/

/*replaced by prn_host_user_pas
;75) prints selected URL/host
print_url	ld	de,host_url
		ld	hl,txt_adr_hostvar
		call	PRINTHL
		ld	de,spaces
		call	PRINT
pu		jp	clear_edit_zone
*/

;76) OUT= print Transfer OK, or error, or aborted, or incomplete
print_transfer_status
;		ld	hl,#4800		;return name to INPBUFF
		call	wifi_unwait
;		ld	hl,print_buffer
;		ld	de,INPBUFF
;		ld	b,0
;pts4		ld	c,#55
;		ld	a,c
;		ld	(df3+1),a
;		ldir
;		xor	a
;		ld	(de),a
	
		call	clear_edit_zone

		bit	6,(ix+3)
		jr	nz,pts3			;NZ=if screen downloaded, print page

		ld	hl,txt_adr_edit_zone1
		ld	de,transfer
		call	PRINTHL
		ld	a,(ix+23)
		cp	1
		jr	z,pts1		;Z=transfer aborted
;		ret	z
		push	af
		ld	hl,txt_adr_edit_zone1+9
		push	hl	
		ld	de,spaces
		call	PRINTHL
		pop	hl
		pop	af		;flags preserved from previous CP
		ld	de,error_answ
		jr	nc,pts2
		ld	de,ok_
		bit	3,(ix+3)
		ret	z		;Z=we download & run
;		jr	z,pts3			;Z=do not print transfer status
		jr	pts
pts2		cp	3
		jr	nz,pts
		ld	de,incomplete
pts		call	PRINTHL
pts1		call	akey
pts3;		call	clear_edit_zone

pts6		ld	hl,#5555
		ld	de,INPBUFF
		ld	b,0
pts4		ld	c,#55
		ld	a,c
		ld	(df3+1),a
		ldir

/*
;		ld	hl,print_buffer
		ld	hl,print_buffer+#100
		ld	de,INPBUFF
		ld	b,0
pts4		ld	c,#55
		ld	a,c
		ld	(df3+1),a
;		ldir
		lddr
*/
		xor	a
		ld	(de),a

pts5		ld	de,find_txt
		ld	hl,txt_adr_edit_zone1
		call	PRINTHL
		ld	de,spaces
		call	PRINT
		call	print_searched_string	

		jp	sf4


;78)prints ftp message on the screen
;IN=HL=start address of message to print
print_ftp_message
		push	hl			;HL=start address of message to print
		ld	hl,#40A0		;print& scroll, we use 3 lines for ftp messages, 2nd line
		ld	de,#4080		;adresy jsou natvrdo:(( 3rd line
		ld	c,#10

rfa_prn1	ld	b,#20			
		push	hl
		push	de
rfa_prn		ld	a,(hl)			;move 2 lines down
		ld	(de),a
		inc	hl
		inc	de
		djnz	rfa_prn
		pop	de
		pop	hl
		inc	h
		inc	d
		dec	c
		jr	z,rfa_prn2
		ld	a,c
		cp	8
		jr	nz,rfa_prn1
		ld	hl,#40C0
		ld	de,#40A0
		jr	rfa_prn1

rfa_prn2	ld	hl,txt_adr_ftp_reports
		push	hl
		ld	de,spaces
		push	de
		call	PRINTHL
		pop	de
		push	de
		call	PRINT				;erase whole line by 2x32 spaces
		pop	de
		push	de
		ld	h,high txt_adr_ftp_reports+1;erase also 4th line
		ld	l,0
		call	PRINTHL
		pop	de
		call	PRINT
		pop	hl				;HL=print address

;		ld	hl,txt_adr_ftp_reports
		pop	de			;DE=start address of message to print
		jp	PRINTHL			;print ftp report on 1st line


;79) connect to FTP server
connect_to_ftp_server
		call	wifi_wait
		ld	de,TX_buf		;prepare connection command to RX_buf
		ld	a,"0"			;like eg. "TCP","test.ilnx.cz",21"
		ld	(de),a
		inc	de
		ld	a,","
		ld	(de),a
		inc	de
		ld	hl,tcp
		ld	bc,7
		ldir
		ld	hl,host_url
;		push	hl

ctfs4		ld	a,(hl)			;ldir until ":", if any
		or	a
		inc	hl
		jr	z,ctfs3			;Z=URL name complete
		cp	":"
		jr	z,ctfs3
		ld	(de),a
		inc	de
;		inc	hl
		jr	ctfs4
		

;		call	count_com_len
;		pop	hl
;		push	hl
;		ldir				;eg. "test.ilnx.cz"

ctfs3		ld	c,a			;URL name on its place in TX_buf, C=0 or ":"
		ld	a,34			;uvozovky
		ld	(de),a
		inc	de
		ld	a,44			;carka
		ld	(de),a
		inc	de
;		pop	hl			;HL=host_url
;		push	de			;DE=current TX buf addr

		ld	a,c
		or	a
		jr	nz,ctfs1


;		ld	c,46			;BC=46, URL len
;		ld	de,dvojt		;find out port number from host_url
;		call	look_for_string
;		ex	de,hl
;		pop	de			;HL=start of port number, DE=current TX buf addr
;		jr	z,ctfs1			;NZ= no port found after ":", so, port will be 21

fl32		ld	hl,ftp_port

;		ld	hl,ftp_port
;		ld	bc,2
;		ldir

ctfs1		ld	a,(hl)
		or	a
		jr	z,ctfs2
		cp	"0"			;if not numbers, use port 21
		jr	c,fl32
		cp	"9"+1
		jr	nc,fl32
fl31		ld	(de),a
		inc	hl
		inc	de
		jr	ctfs1

ctfs2		ld	hl,eol
		ld	bc,3
		ldir				;connection command completed in TX_buf

		call	clear_RX_buf
fl1		call	cipstart_connect	;connect to ftp server and evaluate answer
		jr	nc,fl2			;NC=all ok, we are connected to server
;error 10 ok, but more precise error code comes from check_module_answer (c=1 or 11)
;		jp	error_handler		;we must end, if not connecttion not successful

server_connection_error
		ld	de,ftp_con_err
		ld	hl,txt_adr_edit_zone1
		call	PRINTHL
		call	akey
clear_ini_keyl	call	clear_edit_zone
;		jp	error_handler1		;ends in a loop, if connection not sucessuful
initsp_keyloop	ld	sp,basic_end-2
		jp	key_loop

fl2	;;	call	get_status_ftp

		ld	h,high txt_adr_ftp_reports-2
		ld	l,0
		ld	(HLKO+1),hl
		ld	hl,RX_buf
		ld	bc,buffers_len
		push	hl
		push	bc
		call	receive_data_timeout

fl21		pop	bc			;BC=length to be serached
		pop	hl			;HL=start adress to be searched
		ld	de,dvojt
		call	look_for_string
		call	nz,wifi_unwait
		ret	nz
;		jr	nz,fl_usr_psw
		push	de			;DE=byte after found string
		push	bc
		call	PRINT
		ld	hl,HLKO+1
		ld	(hl),0
		inc	hl
		inc	(hl)			;increase line number
		jr	fl21

/*
;80) for login and auto login purposes, OUT=Z=old connection same as new, NZ=proceed to connection
test_ftp_connection
		ld	bc,#80
		ld	hl,current_host
		ld	de,host_url
		call	look_for_string
		ret	z			;current url is the same as new one

		ld	de,already_connect
		ld	hl,txt_adr_edit_zone1
		call	PRINTHL
		ld	de,current_host
		call	PRINT
		ld	de,do_you_want_rec
		ld	hl,txt_adr_edit_zone2
		call	PRINTHL
		ld	de,host_url
		call	PRINT
		ld	de,yesno
		call	PRINT
tfc1		call	KEYSCAN
		cp	"y"
		jr	z,logoff_current	;proceed to new connection
		cp	"n"
		jr	z,tfc2
;		jr	z,key_loop
		cp	"b"
		jr	nz,tfc1
tfc2		
;		jp	z,ret2main_scr		;clear edit zone and jp to key:loop
;		jr	tfc1

ret2main_scr	call	passive_receive_off	;hope, it does not affect other routines
		jp	clear_edit_zone	;clear 2 bottom lines
;		jp	key_loop

logoff_current				;end current session
		call	ftp_quit_connection	;send QUIT command
;		ld	hl,at_cipclose5
		call	send_string
		ld	hl,ok
		call	look_on_fly
		jp	nz,cr_err		;all closed, now proceed to new connection
		call	clear_edit_zone
		inc	b			;set NZ
		ret
*/
;81)
set_3_5mhz	xor	a			;0=3,5Mhz, 1=7Mhz, 2=14Mhz, 3=20Mhz
		jr	s20
;82)
set_20mhz	ld	a,3
s20		ld	bc,#783B
		ld	l,#30
		out	(c),l
		inc	b
		out	(c),a
;		nop			;test pro LMN
		ret			;nový core by tuto chybu měl řešit
		halt			;test pro LMN
		halt
		halt
;		ret			;pozor, 2xRET= bezpecny navrat z rutiny. Obcas se RET preskoci a jede se dal!!!! Az to LMN vyresi, tak vymazat ten jeden RET

;83) IN=HL=command, C=including parameter, NC= command only
send_ftp_command_in_passive_mode
		ld	de,TX_buf
		ld	bc,5
		ldir

		jr	nc,sfcpm1

		push de
		call	input_l_count
		pop	de
		ldir
	
sfcpm1		ld	hl,eol
		ld	c,3
		ldir

;;;koko
		call	clear_rece_buf

		ld	hl,at_cipsend
		call	send_string
		jp	nz,cr_err

		bit	4,(ix+3)	;send session ID
		ld	a,"0"
		jr	z,cr11
		inc	a		;A="1"
cr11		ld	hl,TX_buf-3
		push	hl
		ld	(hl),a
		inc	hl
		ld	(hl),","
		inc	hl
		ld	(hl),0
		pop	hl
		call	send_string	;eg. "0,"
		jp	nz,cr_err

		ld	hl,TX_buf
		call	count_com_len	;count length of data in TX_buf
		ld	h,b
		ld	l,c

		ld	de,RX_buf
		push	de
		call	HL2ASCII
		xor	a
		ld	(de),a

		pop	hl		;RX_buf=length of command in ASCII, eg. "20"
		call	send_string	;send length of command
		jp	nz,cr_err

		ld	hl,eol
		call	send_string	;command completely sent
		jp	nz,cr_err

		ld	hl,char_zob	;new concept
;		ld	de,char_zob	;Wifi module returned ">" to show readiness to accept?
		call	check_module_answer
		jr	c,sfcip_err
;		jp	c,ret2main_scr	;was error

		ld	hl,TX_buf	;send data to ftp server from TX_buf address
		call	send_string
		jp	nz,cr_err

;		ld	(ix+6),3	;was 0
sfcip3		ld	hl,sendok
		call	look_on_fly
		jr	z,sfcip4
;		dec	(ix+6)
;		jr	nz,sfcip3
sfcip_err	ld	c,37
		jp	error_handler
;		jp	nz,cr_err

;koko/*toto myslím netřeba, vrací serii IPDcek, test na UNLINK ERROR!!!!
sfcip4		ld	hl,RX_buf
		ld	bc,#80
		push	hl
		push	bc
		call	receive_data_timeout
;tady teprve je v RXbufferu udaj s IPD, a pod IPD 0 uz byva 226 Transfer ok, pri malych souborech, toto treba osetrit
;ale opravdu se musíme ujistit, že jsou tady IPDčka, jinak program předběhneme, a vrátí chybu 34
		pop	bc
		pop	hl
		ld	de,ipd+2	;omit #D,#A
		call	look_for_string
		jr	nz,sfcip4	;endles loop!!!! avoid!!!

;koko*/

;;;koko sfcip4		;;;kokocall	clear_rece_buf

/*		ld	hl,at_ciprecvdata	;send AT command to receive desired length of data
		call	send_string
		jp	nz,cr_err

		ld	a,"0"
		ld	hl,TX_buf
		push	hl
		ld	(hl),a
		inc	hl
		ld	(hl),","
		inc	hl
		ld	(hl),"1"
		inc	hl
		ld	(hl),"2"
		inc	hl
		ld	(hl),"8"
		inc	hl
		ld	(hl),#D
		inc	hl
		ld	(hl),#A
		inc	hl
		ld	(hl),0
		pop	hl		;HL=TX_buf

		call	send_string	;eg. "0,"
		jp	nz,cr_err

;cr12		ld	hl,ok
;		call	look_on_fly
;		jr	nz,cr12		;dat pryc, najde ok, ktere je az za zpravami z ftpka

		ld	bc,#80
		ld	hl,RX_buf
		push	hl
		ld	de,#FFFF
		call	rece_block

		pop	hl		;HL=RX_buf
		ld	de,dvojt
		call	look_for_string
		push	de
		
		call	ConvRStr16
*/
		call	ask_for_reply_in_passive_mode

		ld	bc,150		;HL=message nr 1, DE=message nr.2 (0 if no message nr 2)
		or	a
		sbc	hl,bc
		jr	z,sfcip1

		add	hl,bc
		ld	bc,125
		or	a
		sbc	hl,bc
		ld	c,13
		jp	nz,error_handler	;neither code 150, nor 125 returned		

sfcip1		ld	a,d
		or	e
		jr	z,sfcip2		;Z=message nr 2 not exist

		ld	hl,226
		or	a
		sbc	hl,de
		jr	nz,sfcip2
		ld	(ix+23),0
		ret

sfcip2		ld	(ix+23),2	;transfer error (but code 226 can come later with longer files)
		ret

/*		push	de


		ld	de,150		;Connection accepted
		or	a
		sbc	hl,de
		ld	c,13
		jp	nz,error_handler
		

		pop	hl		;check, if 226 message comes (it comes with short files)
		push	hl
		call	print_ftp_message

		pop	hl
		ld	de,eol
		ld	bc,#80
		call	look_for_string
		push	de

		call	ConvRStr16
		ld	de,226
		or	a
		sbc	hl,de
		ld	hl,transfer_flag
		pop	de
		ld	(hl),2		;transfer error (but code 226 can come later with longer files)
		ret	nz
;		jr	nz,fdf_main
		ld	(hl),0		;code found, set transfer status as OK
		ld	h,d
		ld	l,e
		jp	print_ftp_message
*/

;84)
;find out, if module has available data, OUT=NZ=data available, HL=always valid length of data (either #800 or less), DE=always max data length (#800 given by download_length)
;OUT=Z=HL=0=data not available
data_available_in_module
;		ld	(ix+6),#5		;5 attempts, increased to #20, seem to help, 0.97- #20 increased to #80
						;was #80, <5= file download returns error 26, the less the quicker directopry retrieve.
data_available_in_module_dir
		ld	a,#20			;for dir 5 repetitions should be enough
		jr	dam6
data_available_in_module_file		
;		ld	a,#80			;for sucessful file download we need more than for dir, theoretically can be
		ld	a,#B0			;bigger value helps during download problems and delays
dam6		ld	(ix+6),a		;instant repetiotion, bacuse download is not called if file is not completed
;		call	clear_rece_buf		;eliminate "Recv 55 bytes" eg.
;		call	clearni_RXko		;get rid off IPDs, if any

						;now send AT command						
dam1		ld	hl,at_ciprecvlen	;now, send AT command to start receiving data
		call	send_string_busy	
;		call	send_string		;data available? (pridat timeout!!)
;		jp	nz,cr_err

;dam7		call	receive_data_to_RXbuf	;look if system is busy
;		ld	de,busy
;		call	look_for_string
;		jr	z,dam7			;still busy

;dam2		call	receive_data_to_RXbuf
;		push	hl
;		ld	de,dvojt
		ld	hl,RX_buf
		ld	bc,#80
		ld	de,at_len
		call	look_for_string
		jr	nz,dam1
;		jr	nz,dam2			;previous icoming IPDs in RX:buf. But we need
						;answer to AT+CIPRECVLEN?
;		jr	nz,dam7

;		ex	de,hl
;		pop	hl
;		ld	de,carka
;		call	look_for_string
;		jp	nz,cr_err
		call	ConvRStr16
		ld	a,h
		or	l
		jr	z,dam3			;Z=no data available, check again
;		ret	nz
		ld	de,download_length
		ex	de,hl
		or	a
		sbc	hl,de			;HL=#800, DE=available length
		jr	nc,dam4
		add	hl,de			;limit len to #800
dam5		xor	a			;HL=#800
		inc	a			;set NZ, data available
		ret
		
dam4		ld	h,d			;data<download_length (#800)
		ld	l,e
		jr	dam5
					
dam3		dec	(ix+6)			;HL=max length of data available in module buffer
		jr	nz,dam1
		ret

;85) ask_for_data_in_passive_mode		;send "AT+CIPRECVDATA=1,2048" in passive receive mode
;IN=BC=length of data we want to receive
ask_for_data_in_passive_mode
		push	bc			;BC=rest_chunk
		ld	(rest_chunk),bc

;		call	clear_rece_buf
;		call	clearni_RXko		;vycisti pripadne prichozi IPDs

;		ld	hl,block_asci
;		ld	(hl),"1"
;		inc	hl
;		ld	(hl),","
;		inc	hl
		pop	hl
;		ld	de,block_asci+2
		ld	de,block_asci
		call	HL2ASCII
						;prepare text "1,"+ rest_chunk in block_asci
		ex	de,hl
		ld	(hl),#D
		inc	hl
		ld	(hl),#A
		inc	hl
		ld	(hl),0

		ld	hl,at_ciprecvdata	;send "AT+CIPRECVDATA=1,2048" command to receive desired length of data
		call	send_string		;prikaz AT+CIPRECVDATA vraci na zacatku: "+CIPRECVDATA,xy:" a OK prijde az po datech!!!
		jp	nz,cr_err

		ld	hl,block_asci
		call	send_string
		jp	nz,cr_err


;test, nehledame za letu, ale nacitame do bufferu, toto tu je, abych si mohl precist obsag bufferu pri chybe 37
loopik2		ld	b,#80
		xor	a
		ld	hl,print_buffer+#40
loopik		ld	(hl),a
		inc	hl	
		djnz	loopik
/*
loopik1		ld	bc,15				;presne 15 bajtu: #D,#A,"+CIPRECVDATA,"
		call	receive_data_to_RXbuf_bc	;nedelame look on fly, ale nacteme poctive do RXbufferu
		ld	de,ciprecvdata
		call	look_for_string
		jr	z,afdipm3
		halt
		jr	loopik2
;		ld	c,37
;		jp	error_handler


		ld	b,100			;zpomalime
znovu		ld	hl,0
loopik		dec	hl
		ld	a,h
		or	l
		jr	nz,loopik
		djnz	znovu
*/

		ld	hl,ciprecvdata		;tento kod je ok pro ELBSD? ale nejede s MBBSD!!! musime ho rozebrat
		call	look_on_fly
		jr	z,afdipm3
;		jr	nz,loopik1
		ld	c,37
		jp	error_handler


afdipm3		ld	hl,block_asci
		push	hl
afdipm1		scf
		call	rece_byte
		cp	":"
		jr	z,afdipm2		;Z=prepared to read pure data
		ld	(hl),a
		inc	hl
		jr	afdipm1


		


/*
afdipm5		ld	hl,carka
		call	look_on_fly		;vyhodnoceni chyby!!!
		jr	z,afdipm3
		ld	c,37
		jp	error_handler

afdipm3		ld	hl,block_asci
		push	hl
afdipm1		scf
		call	rece_byte
		cp	"I"
		jr	z,afdipm4
		cp	":"
		jr	z,afdipm2		;Z=prepared to read pure data
		ld	(hl),a
		inc	hl
		jr	afdipm1
*/
		
afdipm2		pop	de
		ld	(hl),0
		call	ConvRStr16
		ld	(rest_chunk),hl
		ret
/*
afdipm4		scf
		call	rece_byte
		cp	#A
		jr	nz,afdipm4
		jr	afdipm5		

;		ld	hl,dvojt
;		jp	look_on_fly
*/
	
;86) ask_for_reply_in_passive_mode		;send "AT+CIPRECVDATA=1,128" in pasive receive mode
ask_for_reply_in_passive_mode
;OUT=HL=code 1, DE=code 2 or 0 if code 2 not present
		ld	(ix+6),4	;4 attempts

;;;koko		
		call	clear_rece_buf

afripm4		ld	hl,at_ciprecvdata	;send AT command to receive desired length of data
		call	send_string
		jp	nz,cr_err

		ld	a,"0"
		ld	hl,TX_buf
		push	hl
		ld	(hl),a
		inc	hl
		ld	(hl),","
		inc	hl
		ld	(hl),"1"
		inc	hl
		ld	(hl),"2"
		inc	hl
		ld	(hl),"8"
		inc	hl
		ld	(hl),#D
		inc	hl
		ld	(hl),#A
		inc	hl
		ld	(hl),0
		pop	hl		;HL=TX_buf

		call	send_string	;eg. "0,"
		jp	nz,cr_err

afripm2;;;koko		call	clear_RX_buf	;the answer is directly eg: #D,#A,"CIPRECVDATA,42:150 Connection accepted..."
/* rece_block seems to fast with 20 MHz	;delete this call after program rins good
		ld	bc,#80
		push	bc
		ld	hl,RX_buf
		push	hl
		ld	de,#FFFF
		call	rece_block

		pop	hl		;HL=RX_buf
		pop	bc
		push	bc
		ld	de,data
		call	look_for_string
		jr	z,afripm1	;Z=answer found		
		pop	bc
*/
		ld	bc,#80
		push	bc
		ld	hl,RX_buf
		push	hl
		call	receive_data_timeout
		pop	hl
		pop	bc
		ld	de,data
		call	look_for_string
		jr	z,afripm1

		dec	(ix+6)
		jr	nz,afripm4
;		jr	nz,afripm2
afripm3		ld	c,34
		jp	error_handler

afripm1		ld	de,dvojt
		ld	bc,#80
		call	look_for_string	;test na výsledek hledání chybí!!!!!
		jr	nz,afripm3	;NZ=not found
		push	de
		call	ConvRStr16
		pop	de		;DE=start of message 1
		push	hl		;HL=code number 1
		push	de		;DE= start of message 1
		ld	h,d
		ld	l,e
		call	print_ftp_message	;print first message

		pop	hl		;HL=start of the asnwer
		ld	de,eol
		ld	bc,#80
		call	look_for_string
		jr	nz,afripm3
		push	de		;DE=start of message 2
		ld	a,(de)
;		or	a
		cp	#D
		jr	z,afr1		;Z=second message not present
		call	ConvRStr16
		pop	de		;start of message 2
		push	hl		;HL=code number 2
		ld	h,d
		ld	l,e
		call	print_ftp_message
		pop	de		;DE=code number 2
		pop	hl		;HL=code number 1
		ret
afr1		pop	de
		xor	a
		ld	d,a
		ld	e,a		;DE=0, message 2 not present
		pop	hl		;HL=code number 1	
		ret

;77) turn off passive recieve mode
passive_receive_off
		ld	a,"0"		;recvmode=0
		jr	passive_receive_switch

;87) turn on passive receive mode
passive_receive_on
		ld	a,"1"		;recvmode=1
passive_receive_switch
		ld	(recvmode),a
		ld	hl,TX_buf
		push	hl
		ld	(hl),a
		inc	hl
		ld	(hl),0

		ld	hl,at_ciprecvmode	;turn on passive transfer mode
		call	send_string
		jp	nz,cr_err

		pop	hl
		call	send_string	;eg. "0,"
		jp	nz,cr_err
		ld	hl,eol
		call	send_string	;command completely sent
		jp	nz,cr_err
		ld	hl,ok
		call	check_module_answer
		ret	nc

;		ld	hl,#6000
;		ld	bc,26000
;		call	receive_data_timeout
;		halt


		ld	c,39
		jp	error_handler
;		jp	c,ret2main_scr	;we cannot check "OK" since we are already in passive mode, maybe needs solving
;		ret			;no need to solve, module responses are the same not depending on receive mode

/*
;88) print working directory+ copy it to memory
print_working_directory
;;;koko		
		call	clear_rece_buf
		xor	a		;;;koko
;		and	a
		ld	hl,ftp_pwd
		ld	de,257

;		ld	a,(hl)		;jen test na chybu 34, na nic neprisel
;		cp	"P"
;		jr	z,pwd2
;		halt

pwd2		xor	a		;;;koko		
		call	send_ftp_command
		jr	nc,pwd1		;all ok
					;test BC if 226
		push	hl
		ld	hl,257
		or	a
		sbc	hl,de
		pop	hl
		jr	z,pwd1
		ld	bc,226
		or	a
		sbc	hl,bc
		jr	z,pwd1
;		push	af		;preserve carry
;		ld	de,226		;check not needed anymore, code 226 checked at the dir retrieve end
;		or	a
;		sbc	hl,de
;		pop	bc
;		jr	z,pwd1
;		push	bc
;		pop	af		;carry is back
		ld	c,21
		jp	error_handler

pwd1		ld	hl,txt_adr_cwd
		push	hl		;HL=print address
		ld	b,8*2
		call	cls_9_lines
		ld	hl,RX_buf
		ld	de,uvoz
		ld	bc,#FF
		call	look_for_string
		push	de		;DE=start of path
		ex	de,hl
		ld	de,uvoz
		call	look_for_string
		ld	(hl),0		;end of path

;		pop	de		;toto asi netřeba, netřeba nahrávat cestu do paměti
;		or	a
;		sbc	hl,de
;		ld	b,h
;		ld	c,l		;BC=length of path
;		push	de		;DE=start of path
;		ld	hl,ftp_path
;		ldir

		pop	de		;DE= start of path
		pop	hl		;HL= print address
		jp	PRINTHL		;print ftp path on the upper screen lines
*/

;89)
get_item_name
;IN=nothing
;OUT=HL=file name, HL'=extension, DE'=file size in ASCII, B=name length, DE=file number, C= file number length
;A=0=tap, A=1=z80, A=2=sna, A=3=tzx, A=4=scr, A=5=trd, A=6=scl, A=7=txt, A=8=pok, A=9=others
		ld	hl,(cur_item_addr)
		inc	hl
		inc	hl
		inc	hl
		push	hl		;HL=file number
		call	search_until_zero
		ld	c,b		;C=file number length
		call	search_until_zero
		push	hl
		call	search_until_zero
		push	hl		;HL=extension
		push	bc		;B=file name length
		call	search_until_zero
		pop	bc		
		push	hl		;HL=file size
		exx
		pop	de		;DE'= start of file size
		pop	hl		;HL'= start of extension
		push	hl
		exx

		pop	de		;DE= start of extension
		push	bc

;novy zpusob- case insensitive
;		ex	de,hl
		ld	hl,tap_txt	;start of extension list
		ld	bc,#0900	;9x extension
gin_again	push	bc
;		ld	de,start_of_extension
		push	de		;DE=start of extension
		call	compare
		pop	de		;DE=start of extension
		pop	bc		;Z=found, end; NZ=not found, again
		jr	z,gin_found
		inc	c
		djnz	gin_again
gin_found	ld	a,c		;A=0-7

/*old, neumi rozlisit mala velka pismena
		ld	b,5		;we test 5x
		xor	a
		ld	de,tap_txt
gin1		push	af
		push	hl
		push	bc
		ld	bc,3
		call	look_for_string
		inc	de
		inc	de
		inc	de
		inc	de
		pop	bc
		pop	hl
		jr	z,gin2		;string found
		pop	af
		inc	a
		djnz	gin1
		jr	gin3

gin2		pop	af		;A=0-4
*/

gin3		pop	bc		;B=file name length, C=file number length
		pop	hl		;HL=file name addr
		pop	de		;DE=file number
		ret



compare		ld	b,3		;compare one extension
comp_cont	ld	a,(de)
		cp	(hl)
		jr	z,comp_ok
		xor	32
		cp	(hl)
		jr	z,comp_ok
comp_nok	inc	hl
		djnz	comp_nok
		inc	b		;indicate NZ
		ret
		
comp_ok		inc	hl
		inc	de
		djnz	comp_cont
		ret	

/*
		ld	b,5		;prepni velka<->mala pismena
		ld	hl,tap_txt
tu1		push	bc

		ld	b,3
tu		ld	a,(hl)
		xor	32
		ld	(hl),a
		inc	hl
		djnz	tu
		inc	hl

		pop	bc
		djnz	tu1
*/

/*
;89) get_item_name	;get file name under cursor
;IN=C=bring full info, NC= bring length in DE only (OUT= then A has no meaning)
;OUT=HL=name start, BC= name length, DE= file length
;OUT=A=0= item is a directory+ HL, BC
;OUT=A=1=item is a file+ HL, BC
;OUT=A=2=item type cannot be determined (because of NLST command)+ HL, BC
;OUT=A=3= item not parsed (for the moment) bacause it is LIST in full, HL, BC irrelevant
;OUT=A=4= starting item ".."
;bits 1 (items 0=abbreviated, 1= full) and 3 (current command type 0=LIST, 1=NLST) from flag_dir are of interest here
get_item_name	ex	af,af'		;backup C/NC
		ld	hl,(cur_item_addr)
		ld	c,(hl)
		inc	hl
		ld	b,(hl)		;BC=item length

		dec	hl
		or	a
		ld	de,basic_end
		sbc	hl,de
		add	hl,de
		inc	hl
		inc	hl
;		inc	hl
		ld	a,4
		jr	z,gfn10
					;Z=first item ".." for going updir, BC=length of item
					;HL=basic_end
;		dec	hl
;		dec	hl
		bit	3,(ix+1)
		ld	a,2
		jr	nz,gfn6		;NZ=NLST command
		inc	a		;A=3
		bit	1,(ix+1)
		jr	z,gfn5		;Z=LIST+abbreviated=ok
		ret			;LIST+full, not supported at the moment

gfn6		;dec	a		;A=2=NLST command
		dec	bc
		inc	hl
;		inc	hl
		ret		


gfn5	;	inc	hl		;LIST+abbreviated
		ld	d,h
		ld	e,l
		add	hl,bc		;DE= item offset
		ld	a,(hl)
		push	af		;A= initial value
		push	hl		;HL= ending address
		ld	(hl),0
		ld	h,d
		ld	l,e		;HL= item offset
		inc	hl

		ld	a,(hl)
		cp	"D"
		push	af		;Z=directory, NZ=file



		ld	e,#20
gfn8		inc	hl
		ld	a,(hl)
		cp	e
;		inc	hl		;new
		jr	z,gfn8

		push	hl		;HL=pointer in item
		ex	de,hl
		call	ConvRStr16
		ld	(gfn10+1),hl	;HL=file length
		ex	de,hl		;DE=file length
		ex	af,af'
		pop	hl		;HL=pointer in the item
		jr	c,gfn9
		pop	hl		;A= initial value
		pop	hl		;HL= ending address
		pop	af		;Z=directory, NZ=file
		ret
		

gfn9		ld	b,4
		ld	a,#20
gfn1		cp	(hl)
		inc	hl
		jr	nz,gfn1

gfn2		cp	(hl)
		inc	hl
		jr	z,gfn2
		djnz	gfn1		
/*old
gfn9		ld	b,4		;continue looking for name itself
		ld	e,#20
gfn1		inc	hl
		ld	a,(hl)
		cp	e
		jr	z,gfn1

gfn2		inc	hl
		ld	a,(hl)
		cp	e
		jr	nz,gfn2
		djnz	gfn1
*/
;		inc	hl
		dec	hl
		push	hl		;HL points to name start, now get file length
		ld	bc,0		;BC=file length counter
gfn3		ld	a,(hl)
		or	a
		jr	z,gfn4
		inc	bc
		inc	hl
		jr	gfn3

gfn4		pop	hl		;HL=start of the name
		pop	af		;Z=directory, NZ=file
		ex	af,af'
		pop	de		;DE= ending address
		pop	af		;A= initial value
		ld	(de),a
		ex	af,af'		;Z=directory, NZ=file
		ld	a,0
gfn10		ld	de,#5555	;DE=file length
		ret	z		;A=0=item is a directory
		inc	a		;A=1=item is a file
		ret			;BC=file name length
*/

;90)
print_dir_files_items			;after dir having been read, it prints number of items or dirs and files
		call	cls_dir_files
		ld	hl,txt_adr_dir_info	;little space for better clarity
		ld	de,block_asci
		push	de
		ld	de,items
		bit	3,(ix+1)
		jr	nz,pdfi1	;NZ=NLST

		ld	de,dirs
		call	PRINTHL		;print "Dirs: "
		pop	de		;DE=block_asci
		push	de
		ld	hl,(dir_counter)
;		ld	h,0
;		ld	l,(ix+32)	;number of dirs
		call	HL2ASCII
		xor	a
		ld	(de),a
		pop	de
		push	de
		call	PRINT		;print number of directories
		ld	de,files
		call	PRINT		;print "Files: "
		jr	pdfi2

pdfi1		call	PRINTHL
pdfi2		pop	de		;DE=block_asci
		push	de
		ld	hl,(item_file_count)
		call	HL2ASCII
		xor	a
		ld	(de),a
		pop	de
		jp	PRINT		;Items: has been printed

cls_dir_files	ld	de,spaces
		ld	hl,txt_adr_dir_info
		push	de
		call	PRINTHL
		pop	de
		jp	PRINT

;91)
search_marked_items
;RET if only marked item found
;IN=cur_item_addr, OUT=NZ=cur_item_addr, Z=already last file
		ld	hl,(cur_item_addr)
smi1		ld	c,(hl)
		inc	hl
		ld	b,(hl)		;BC=item length
		inc	hl
		ld	a,c
		or	b
		ret	z		;Z=last file

		bit	0,(hl)
		ret	nz		;NZ=cur_item_addr

		inc	hl
		inc	hl
		add	hl,bc
		ld	(cur_item_addr),hl
		jr	smi1



;92)
un_mark_all
;unmark or mark all items, IN=C=unmark, NC=mark
		ld	hl,0
		ld	(mark_counter),hl

		ex	af,af'		;preserve carry
		ld	hl,cur_item_addr
		ld	c,(hl)
		inc	hl
		ld	b,(hl)
		push	bc		;preserve original item pointer
		ld	bc,basic_end+fi_len-2
		ld	(hl),b
		dec	hl
		ld	(hl),c		;start of dir

ua1		ld	hl,(cur_item_addr)
		inc	hl
		inc	hl
		res	0,(hl)
		ex	af,af'
		jr	c,ua2
		set	0,(hl)		;mark
		exx
		ld	hl,(mark_counter)
		inc	hl
		ld	(mark_counter),hl
		exx

ua2		ex	af,af'
		ld	bc,cur_item_addr
		call	item_add
		jr	nz,ua1
		pop	hl		;renew original item pointer
		ld	(cur_item_addr),hl
		ret

/*
;93)
;IN=A="A" for ASCCI (directory) or "I" for binary (files)
send_type_command_a
		ld	a,"A"
		jr	stc1
send_type_command_i
		ld	a,"I"
stc1		call	clear_inp_buf
		ld	(INPBUFF),a
		ld	a,1
		ld	de,200			;200 Switching to mode ASCII
		ld	hl,ftp_type
		call	send_ftp_command
		ret	nc
		ld	c,38
		jp	error_handler		
*/

;94)
print_whole_screen
		call	cls_4000
		call	print_title

		ld	de,menu1
		ld	hl,txt_adr_menu1
		call	PRINTHL			;print menu line 1
		jp	delete_tap_prn		;print if TAP should be deleted or 

/*
print_whole_screen
		call	cls_4000

		ld	de,menu1
		ld	hl,txt_adr_menu1
		call	PRINTHL			;print menu line 1
		ld	de,menu2
		ld	hl,txt_adr_menu2
		call	PRINTHL			;print menu line 2

		call	prn_w_onoff		;print if WIFI should be ON/OFF on quit
		call	prn_alogin		;print autologin status
		call	prn_lst_nlst_abbr_ful	;print line 16 with LIST/NLST/full/abbr status
		call	prn_file_start		;print line 17 with filename and start address
		call	prn_host_usr_psw	;print line 18 with host, user, password

pws2		bit	0,(ix+1)		;directory or header?
		jp	nz,print_working_directory
pws1		jp	print_title
*/

;variables
;ftp_path	ds 0,#FE			;place for ftp path, asi netřeba
var_name	db "SARAH10BSD",0		;variable file name for download-user can change
		ds 56				;reserve
var_start	dw #C800			;variable start address-user can change
host_url	db "sarah.speccy.cz",0		;variable host-user can change 
		ds 30				;reserve for url
var_user	db "sarah",0			;variable user-user can change
		ds 31				;reserve
var_pasw	db "zxftpc",0			;variable password-user can change
		ds 31				;reserve

current_host	ds 64				;current host we are logged in
current_login	ds 2				;contains either "X " or "OK", both followed by 0
		db 0

;temporary positions and addresses with directory
cursor_w_curad_tmp	dw #5555		;cursor address on the screen (attrs)
wind_curs_item_tmp	db #55			;cursor position on the screen
cur_item_adr_tmp	dw #5555		;item address in memory
item_pg_start_tmp	dw #5555		;address of the starting item on the screen
item_checksum		dw #5555		;if checksum does not match current 2 bytes in memory, position the cursor on the very first item

;commands
at		db "AT",#D,#A,0
ate0		db "ATE0",#D,#A,0
at_cipmux	db "AT+CIPMUX=0",#D,#A,0
;at_cipmux	db "AT+CIPMUX=1",#D,#A,0
cipstatus	db "AT+CIPSTATUS",#D,#A,0
cipstart	db "AT+CIPSTART=",0
uart		db "AT+UART_CUR="
_8100		db ",8,1,0,0",#D,#A
at_cwmode	db "AT+CWMODE=1",#D,#A,0	
at_cipsntpcfg?	db "AT+CIPSNTPCFG?",#D,#A,0
at_cipsntpcfg	db "AT+CIPSNTPCFG=1,",0
at_rst		db "AT+RST",#D,#A,0
at_cwjap_cur?	db "AT+CWJAP_CUR?",#D,#A,0
at_cwjap_def?	db "AT+CWJAP_DEF?",#D,#A,0
at_autocon	db "AT+CWAUTOCONN?",#D,#A,0
at_uart_def	db "AT+UART_DEF?",#D,#A,0
at_cwlapopt	db "AT+CWLAPOPT=1,6",#D,#A,0
at_cwlap	db "AT+CWLAP",#D,#A,0
at_cwjap_def	db "AT+CWJAP_DEF=",#22,0
at_cwqap	db "AT+CWQAP",#D,#A,0
at_cipsend	db "AT+CIPSEND=",0
at_cipclose1	db "AT+CIPCLOSE=1",#D,#A,0
at_cipclose5	db "AT+CIPCLOSE=5",#D,#A,0
at_cipclose	db "AT+CIPCLOSE",#D,#A,0
at_ciprecvmode	db "AT+CIPRECVMODE=",0
at_ciprecvdata	db "AT+CIPRECVDATA=",0
at_ciprecvlen	db "AT+CIPRECVLEN?",#D,#A,0
at_len		db "LEN:",0

/*
;ftp commands
ftp_user	db "USER "
ftp_pass	db "PASS "
ftp_size	db "SIZE "
ftp_pasv	db "PASV "
ftp_retr	db "RETR "
ftp_quit	db "QUIT "	;uses no argument
ftp_stor	db "STOR "
ftp_stou	db "STOU "
ftp_appe	db "APPE "
ftp_abor	db "ABOR "	;uses no argument
ftp_noop	db "NOOP "	;uses no argument
ftp_list	db "LIST ",0	;uses no argument
ftp_nlst	db "NLST ",0
ftp_pwd		db "PWD  "
ftp_cwd		db "CWD  "
ftp_cdup	db "CDUP "
ftp_mkd		db "MKD  "
ftp_rmd		db "RMD  "
ftp_dele	db "DELE "
ftp_rnfr	db "RNFR "
ftp_rnto	db "RNTO "
ftp_type	db "TYPE "	;we use "A" or "I"
*/

;answers
data		db "DATA",0
STATUS		db "STATUS:",0
CONNECT		db "CONNECT",0
sendok		db "SEND "
ok		db "OK"
eol		db #D,#A,0
dvojt		db ":",0
carka		db ",",0
uvoz		db 34,0
zavorka		db ")",0
zavorka_leva	db "(",0
char_zob	db "OK",13,10,"> ",0
ok_		db "OK",0
no_ap		db "No AP",0
error_answ	db "ERROR",0
ipd		db #D,#A,"+IPD,",0
;code_226	db "226",0
ciprecvdata	db "+CIPRECVDATA,",0
closed		db "CLOSED",#D,#A,0
busy		db "busy",0
recv		db "Recv",0
tap_txt		db "tap"
z80_txt		db "z80"
sna_txt		db "sna"
tzx_txt		db "tzx"
scr_txt		db "scr"
trd_txt		db "trd"
scl_txt		db "scl"
txt_txt		db "txt"
pok_txt		db "pok"

;texts
;dir_txt	db "Dir ",0
;not_empty_txt	db "not empty. Erase?",0
items		db "Items:",0
dirs		db "Dirs:",0
files		db "  Files:",0
full		db "full",0
abbr		db "abbr",0
first_item	db 3,0,0,"..",3,0,0,0
fi_len		equ $-first_item
ftp_port	db "21",0
tcp		db 34,"TCP",34,#2C,34,0	;délka 7 bajtů
speed		db "Speed:",0
module_no_aswer	db "..speed aligned, but no answer from module..",0
spaces		db "                                ",0
not_found	db "Not found",0
page_txt	db "Page: ",0

;menu_dir	db "I-Fn Spc ZX R-Rr 6-Rn 7-MD Del Br-UD ss+/N",0
menu1		db "S-search Q-quit H-server E-delete TAP",0
menu_dir	db "D-dwnld Ent-run arrowsX-roll",0
;menu1		db "S-size Q-quit F-file T-start A-YES H-host R-user P-pass U-upld",0
;menu2		db "W-wifi OFF ss+/L-auto/login O-logoff V-save app ss+/D-dwnld/file",0
menu2		;db "W-wifi OFF Cs/ss+/L-login O-logoff V-savApp Cs/ss+/D-dwnld C-com",0
file		db "File: ",0
start		db "  Start: ",0
user		db "  User: ",0
pass		db "  Pass: ",0
enter_start_add	db "Enter start address: ",0
ftp_con_err	db "Error in connecting to FTP server",0
;login_to_ftp	db "L- login to FTP server",0
;file_size	db "S- file size",0
;download_file	db "D- download file",0
;ftp_quit_txt	db "T- quit FTP",0
;ftp_upl_txt	db "U- upload file",0
;url_txt	db "R- URL:",0
;saveapp_txt	db "A- save app",0
enter_comm	db "Enter command:",0
enter_name	db "Enter new name:",0
;enter_dir	db "Enter dir name:",0
;enter_url	db "Enter host:",0
;enter_usr	db "Enter username:",0
;psw		db "Enter password:",0
enter_fil_nam	db "Enter file name:",0
find_txt	db "Search:",0	
;find_txt	db "Find:",0
loading_file	db "Downloading file... ",0
;uploading_file	db "Uploading file... ",0
press_b		db "Press B to cancel",0
progress	db "Progress: ",0
file_exist_txt	db "File exists. Overwrite/Append/New/aBbort?",0
;title		db "ZXDB downloader 0.7beta, Hood, 10/2024-02/2025"
title		db "Online Speccy Library 0.9beta, Hood, 10/2024-02/2026 "
wifi_type	db #55	;here will be either "M" for MB wifi, or "E" for el wifi, final result will be eg. MBSD, or MESX or EESX.

/*	IFDEF mb03
;	IF mb03=1
		db "MB"
	ELSE
;	IF el=1
		db "EL"
	ENDIF
*/
	IFDEF bs_dos
;	IF bs_dos=1
		db "BSD"
	ELSE
		db "ESX"
	ENDIF

		db 0

title_len	equ $-title-1
error_txt	db "error=",0

nmi_not_found_txt db "nmi menu not found",0
on		db "ON ",0
off		db "OFF",0
logged_in	db "Logged in: ",0
host		db "Host: ",0
transfer	db "Transfer aborted",0
incomplete	db "incomplete",0
/*not needed, not used
save_app_head	db 3			;17 bytes header for application save
zxftpcl		db "SAR11SAV  "
;zxftpcl		db "ZXFtp_cl"		;8chars for esxdos name
;ver		db "09"
save_app_len	dw #5555
save_app_start	dw start_address,0
*/

akey_txt	db " ..press a key..",0
yesno		db " Yes/No?",0
yes		db "yes",0
no		db "no ",0
already_connect	db "Already connected to ",0
do_you_want_rec	db "Reconnect to ",0

/*
press_reset	db "Q- quit",0
wifi_on_exit	db "W- wifi on quit: ",0
set_ap		db "A- connect to AP",0
set_speed_txt	db "V- set speed",0
disconn_AP_txt	db "D- disconnect from AP",0
reset_wifi_txt	db "R- reset WiFi",0
press_run_term	db "T- run terminal",0
disconnect	db "DISCONNECT",0
wifi_reset	db "Resetting Wifi...",0
ntp_timeout	db "NTP server timeout. Try again.",0
gmt_shift	db "GMT shift: ",0
eoleol		db #D,#A
		db #D,#A,0
time_zone_act	db "CFG:1",0
answ_cwjap	db "+CWJAP",0
ready		db "ready",0
ent_psw		db "Enter password for ",0
setting_speed	db "setting speed...",0
con_2_AP	db "connecting to AP...",0
current_ap	db "current AP: ",0
default_ap	db "Default AP: ",0
auto_ap		db "Auto AP: ",0
def_wifi_speed	db "Default wifi speed: ",0
excellent	db ",Best",0
good		db ",Good",0
fair		db ",Fair",0
poor		db ",Poor",0
Bd		db " Bd. ",0
cwjap_err_1	db "..connection timeout",0
cwjap_err_2	db "..wrong password",0
cwjap_err_3	db "..cannot find the target AP",0
cwjap_err_4	db "..connection failed",0
connect_success db "New connection succesful.",0
control_keys	db "Use arrows, ss+ENT for current AP, EDIT to cancel.",0
;cipstatus_2	db "= connected to AP, IP is obtained",0
;cipstatus_3	db "connected to server. Logged on: ",0
;cipstatus_3	db "= created TCP/UDP transmission",0
;cipstatus_4	db "= TCP/UDP transmission diconnected",0
;cipstatus_5	db "= does NOT connect to AP",0
mbfw		db "MBFW= ",0
*/
speed_table	db "0= 2400",0
		db "1= 4800",0
		db "2= 9600",0
		db "3= 19200",0
		db "4= 31250",0
		db "5= 38400",0
		db "6= 57600",0
		db "7= 115200",0  ;(default at reset)
		db "8= 230400",0
		db "9= 460800",0
		db "10= 576000",0
		db "11= 921600",0
		db "12= 1152000",0
		db "13= 1500000",0
		db "14= 2000000",0
		db "15= 2304000",0
		db "16= 3456000",0
;		db "17= 4608000",0
;		db "18= 5760000",0
;		db "19= 6912000",0
;		db "20= 8064000",0
;		db "21= 9216000",0
;		db "22= 10368000",0
;		db "23= 11520000",0
;contr_k_speed	db "Use arrows, ENT select, EDIT to cancel.",0
sel_spd_ask_txt	db "Speed not supported by module, set it anyway?",0
unknown		db " unknown..",0

;flags=IX+0
flag1		db 8		;see start of the code, 2=SSL connection,0=TCP
flag_dir	db 0		;IX+1, flag related to dir
				;bit 0=dir already read?, 0=no, 1=yes
				;bit 1=current items in memory, 0=abbreviated, 1=full
				;bit 2=user wished items in memory, 0=abbreviated, 1=full
				;bit 3= current command type in memory, 0=LIST, 1=NLST
				;bit 4= user wished command type, 0=LIST, 1=NLST
				;bit 5= temporary flag, item being read in is file or dir, 0=file, 1=dir
				;bit 6= direction we go. 1= enter dir, 0= going updir
ud_speed	db 16		;IX+2,we want speed 16 for now (it is max speed)
flag2		db 2		;IX+3, print send command,0=NO, 1=YES;amount 2=delete TAP after extracting
page_counter	db 0		;IX+4, counts pages 0-255 (eg. "&p=4")
;server_flag	db 1		;IX+5, bit0=0=ZXDB.REMYSHARP.COM, bit 0=1=HOOD.SPECCY.CZ
server_flag	db 0		;IX+5, 0-255= podporuje 256 serverů. 0=ZXDB.REMYSHARP.COM, 1=SARAH.SPECCY.CZ
		db #55		;IX+6, timeout for initi wifi speed management (used on more places)
		db #55		;IX+7, uart speed currently set in HW (0-23 speed set, 255 speed not set)
		db #55		;IX+8, maximum speed for bios (counted from FW)
recvmode	db 0		;IX+9, 0=active, 1=passive
		db #55		;IX+10, free for use
;dir_counter	db #55		;IX+10
	
cursor_w_stadr	dw #5900	;start address of cursor window for AP list
cursor_w_curad	dw #5900	;current address of cursor window
;cursor_w_curad	dw #5555	;current address of cursor window
		db #55		;IX+15, free for use, was counter of marked files (we need 16 bit counter)
AP_a_page	db 10		;;;;;IX+16= maximum number of lines in cursor window
wind_adr	dw #4B00	;not in use, was: start of print address in the window
item_pg_start	dw #5555	;;;;;start address of first dir item on displayed page
wind_curs_item	db #55		;IX+21;;;;current start cursor position of the window (0-AP_a_page)

status_byte	db #55		;IX+22= current status of ftp (TCP) connection, contains "2"-"5"
transfer_flag	db #55		;IX+23=0=transfer OK, 1=transfer aborted, 2= transfer error (used for upload& download for now), 3=incomplete (in case there is no memory for directory)
minutes		db #55		;IX+24, minutes read out from RTC chip

cur_item_addr	dw #5555	;;;;;ix+25, current address/pointer to dir item
cur_lin_prn_adr	dw #5555	;;;;;ix+27, current addres for line/item print
;ftp_path_pointr	dw #5555	;ix+28, points to current dir in the ftp dir path, asi netřeba
item_file_count	dw #5555	;ix+29, item (for NLST)/ file (for LIST) counter (IY will count files/ items)
mark_counter	dw #5555	;ix+30, counts marked files
dir_counter	dw #5555	;IX+32

;end of flags

;core		db #55,#55,#55	

RX_buf		ds #80
TX_buf		ds #80
buffers_len	equ $-RX_buf


	include "keyboard_and_print.asm"

;	IF bs_dos=1
	IFDEF bs_dos
	include "disk_operations_bsdos.asm"
	ELSE
	include "disk_operations_esxdos.asm"
	ENDIF

bios_start
	INCBIN	"WifiBios20_MB_EL.cod"
/*
	IFDEF mb03
;	IF  mb03=1
	incbin	"WifiBios19_MB.cod"
	ELSE
	INCBIN	"WifiBios19_EL.cod"
	ENDIF
;	incbin	"WifiBios18.cod"
*/
bios_len	equ $-bios_start

;total_len	equ $-code_
total_len	equ $-start_ldir











