; Hlavicka
; write_text_from_DB - komplexni program na vykreslovani textu na LCD
; load_char_address - nastavi Z-pointer podle toho, jake cislo znaku (ASCII
  ; kod) je v aktualnim Z-pointeru

; write_char_from_flash - jednoduchy program, ktery vykresli jeden znak na LCD.
  ; Vhodne pro pripady, kdy potrebujeme vykreslit jen jeden znak. V techto
  ; pripadech je vyhodne pouzit tento maly a rychly program namisto
  ; write_text_from_DB.

 ; Znaky se nahravaji z flash pameti. Zmensi se vyuziti SRAM, i program
  ; samotny, ale snizi se o neco rychlost, protoze LPM trva 3 cykly a ST,
  ; pripadne LDS pouze 2


  ; A
A_big_address:	; rekneme si, ze na teto adrese
  ; bude zacinat znak A
;.DB	6	; Zapiseme info o delce pismene
;.DB	0b01111100	; prvni segment z pismene a jeho binarni
  ; vyjadreni
;.DB	0b00010010	; druhy segment z pismene...
;.DB	0b00010001	; treti atd....
;.DB	0b00010010
;.DB	0b01111100
  ; nemusime psat na kazdy radek. Jednodussi je to zapsat "za sebe". Navic
  ; pokud bychom meli u kazdeho ".DB" lichy pocet prvku, pak se prida
  ; "zero byte", ktery by jednak komplikoval samotne cteni (museli bychom vzdy
  ; preskocit o dva adresova mista, abychom se dostali na dalsi znak) a jednak
  ; by se velice nehospodarne zaplnovala pamet flash.
  ; Takze jeste jednou a poradne
.DB 6,0b01111100,0b00010010,0b00010001,0b00010010,0b01111100
  ; Struktura je stale stejna: info o delce pismene a pak jeho binarni
  ; vyjadreni.
  ; Pokud by byl pocet prvku v .DB lichy, pak se prida na konec zero byte
  ; (kompilator napise varovani). Proto u nekterych nasledujicich pismen,
  ; cisel, nebo znaku bude na konci pridana "0". aby prekladac nehlasil
  ; varovani

  ; B
B_big_address:
.DB	5,0b01111111,0b01001001,0b01001001,0b00110110,0
  ; Zrovna v tomto pripade je pocet prvku v .DB lichy. Automaticky by se
  ; pridal "zero byte" a kompilator by nahlasil varovani: Protoze ale nechci,
  ; aby se varovani zobrazovalo, tak sem si "zero" byte pridal sam. Mohl bych
  ; tam vlastne ulozit jakoukoli hodnotu, ale proc si neco vymyslet a delat
  ; kod mene prehledny

  ; C
C_big_address:
.DB	4,0b00111110,0b01000001,0b01000001

  ; D
D_big_address:
.DB	5,0b01111111,0b01000001,0b01000001,0b00111110,0

  ; E
E_big_address:
.DB	5,0b01111111,0b01001001,0b01001001,0b01001001,0

  ; F
F_big_address:
.DB	5,0b01111111,0b00001001,0b00001001,0b00000001,0

  ; G
G_big_address:
.DB	6,0b00111110,0b01000001,0b01000001,0b01001001,0b00111000

  ; H
H_big_address:
.DB	5,0b01111111,0b00001000,0b00001000,0b01111111,0

  ; I
I_big_address:
.DB	4,0b01000001,0b01111111,0b01000001

  ; J
J_big_address:
.DB	5,0b00100000,0b01000000,0b01000000,0b00111111,0

  ; K
K_big_address:
.DB	6,0b01111111,0b00001000,0b00010100,0b00100010,0b01000001

  ; L
L_big_address:
.DB	5,0b01111111,0b01000000,0b01000000,0b01000000,0

  ; M
M_big_address:
.DB	6,0b01111111,0b00000010,0b00000100,0b00000010,0b01111111

  ; N
N_big_address:
.DB	6,0b01111111,0b00000010,0b00001100,0b00110000,0b01111111

  ; O
O_big_address:
.DB	6,0b00111110,0b01000001,0b01000001,0b01000001,0b00111110

  ; P
P_big_address:
.DB	5,0b01111111,0b00001001,0b00001001,0b00000110,0

  ; Q
Q_big_address:
.DB	6,0b00111110,0b01000001,0b01010001,0b00100001,0b01011110

  ; R
R_big_address:
.DB	5,0b01111111,0b00011001,0b00101001,0b01000110,0

  ; S
S_big_address:
.DB	5,0b00100110,0b01001001,0b01001001,0b00110010,0

  ; T
T_big_address:
.DB	6,0b00000001,0b00000001,0b01111111,0b00000001,0b00000001

  ; U
U_big_address:
.DB	6,0b00111111,0b01000000,0b01000000,0b01000000,0b00111111

  ; V
V_big_address:
.DB	6,0b00000111,0b00111000,0b01000000,0b00111000,0b00000111

  ; W
W_big_address:
.DB	8,0b00001111,0b00110000,0b01000000,0b00110000,0b01000000,0b00110000,0b00001111

  ; X
X_big_address:
.DB	6,0b01100011,0b00010100,0b00001000,0b00010100,0b01100011

  ; Y
Y_big_address:
.DB	6,0b00000011,0b00000100,0b01111000,0b00000100,0b00000011

  ; Z
Z_big_address:
.DB	6,0b01100001,0b01010001,0b01001001,0b01000101,0b01000011

  ; a
a_small_address:
.DB	5,0b00100000,0b01010100,0b01010100,0b01111000,0

  ; b
b_small_address:
.DB	5,0b00111111,0b01001000,0b01001000,0b00110000,0

  ; c
c_small_address:
.DB	4,0b00110000,0b01001000,0b01001000

  ; d
d_small_address:
.DB	5,0b00110000,0b01001000,0b01001000,0b00111111,0

  ; e
e_small_address:
.DB	5,0b00111000,0b01010100,0b01010100,0b01011000,0

  ; f
f_small_address:
.DB	5,0b00001000,0b01111100,0b00001010,0b00000010,0

  ; g
g_small_address:
.DB	5,0b01001000,0b01010100,0b01010100,0b00111100,0

  ; h
h_small_address:
.DB	5,0b01111111,0b00010000,0b00001000,0b01110000,0

  ; i
i_small_address:
.DB	2,0b01111010

  ; j
j_small_address:
.DB	4,0b01000000,0b01000000,0b00111010

  ; k
k_small_address:
.DB	5,0b01111111,0b00010000,0b00101000,0b01000100,0

  ; l
l_small_address:
.DB	4,0b01000001,0b01111111,0b01000000

  ; m
m_small_address:
.DB	8,0b01111000,0b00010000,0b00001000,0b01110000,0b00001000,0b00001000,0b01110000

  ; n
n_small_address:
.DB	5,0b01111000,0b00010000,0b00001000,0b01110000,0

  ; o
o_small_address:
.DB	5,0b00110000,0b01001000,0b01001000,0b00110000,0

  ; p
p_small_address:
.DB	5,0b01111100,0b00010100,0b00010100,0b00001000,0

  ; q
q_small_address:
.DB	5,0b00001000,0b00010100,0b00010100,0b01111100,0

  ; r
r_small_address:
.DB	5,0b01111100,0b00001000,0b00000100,0b00001000,0

  ; s
s_small_address:
.DB	4,0b01011000,0b01010100,0b00110100

  ; t
t_small_address:
.DB	4,0b00000010,0b00111111,0b01000010

  ; u
u_small_address:
.DB	5,0b00111000,0b01000000,0b00100000,0b01111000,0

  ; v
v_small_address:
.DB	4,0b00111100,0b01000000,0b00111100

  ; w
w_small_address:
.DB	6,0b00111100,0b01000000,0b00111100,0b01000000,0b00111100

  ; x
x_small_address:
.DB	4,0b01101100,0b00010000,0b01101100

  ; y
y_small_address:
.DB	4,0b01011100,0b01010000,0b00111100

  ; z
z_small_address:
.DB	4,0b01100100,0b01010100,0b01001100

; *-----------------------------------------*
; |                  CISLA                  |
; *-----------------------------------------*

  ; 0
num_0_address:
.DB	5,0b00111110,0b01000001,0b01000001,0b00111110,0

  ; 1
num_1_address:
.DB	3,0b00000010,0b01111111,0

  ; 2
num_2_address:
.DB	5,0b01100010,0b01010001,0b01001001,0b01000110,0

  ; 3
num_3_address:
.DB	5,0b00100010,0b01000001,0b01001001,0b00110110,0

  ; 4
num_4_address:
.DB	5,0b00011111,0b00010000,0b01111000,0b00010000,0

  ; 5
num_5_address:
.DB	5,0b01000111,0b01001001,0b01001001,0b00110001,0

  ; 6
num_6_address:
.DB	5,0b00111100,0b01001010,0b01001001,0b00110000,0

  ; 7
num_7_address:
.DB	5,0b00000011,0b00000001,0b01110001,0b00001111,0

  ; 8
num_8_address:
.DB	5,0b00110110,0b01001001,0b01001001,0b00110110,0

  ; 9
num_9_address:
.DB	5,0b00000110,0b01001001,0b01001001,0b00111110,0

; *-----------------------------------------*
; |                  ZNAKY                  |
; *-----------------------------------------*

  ; space
space_address:
.DB	4,0,0,0

  ; mi
mi_address:
.DB	5,0b01111100,0b00100000,0b00100000,0b00011100,0

  ; point
point_address:
.DB	2,0b01000000

  ; ?
quest_adress:
.DB	5,0b00000010,0b01010001,0b00001001,0b00000110,0

  ; '
quotes_address:
.DB	2,0b00000011

  ; "
quotes_double_address:
.DB	4,0b00000011,0b00000000,0b00000011

  ; sipka nahoru
arrow_up_symbol_address:
.DB	7,0b00000000,0b00000100,0b00000010,0b01111111,0b00000010,0b00000100,0

  ; sipka dolu
arrow_down_symbol_address:
.DB	7,0b00000000,0b00010000,0b00100000,0b01111111,0b00100000,0b00010000,0

  ; sipka doleva
arrow_left_symbol_address:
.DB	8,0b00000000,0b00001000,0b00011100,0b00101010,0b00001000,0b00001000,0b00001000

  ; sipka doprava
arrow_right_symbol_address:
.DB	8,0b00000000,0b00001000,0b00001000,0b00001000,0b00101010,0b00011100,0b00001000

  ; & symbol
and_symbol_address:
.DB	6,0b00110110,0b01001001,0b01010101,0b00100010,0b01010000

  ; vzestupna hrana
rising_edge_symbol_address:
.DB	6,0b01000000,0b01000000,0b01111111,0b00000001,0b00000001


  ; sestupna hrana
falling_edge_symbol_address:
.DB	6,0b00000001,0b00000001,0b01111111,0b01000000,0b01000000

  ; znak |
pipe_symbol_address:
.DB	2,0b01111111

  ; nahoru a dolu (nevim jak jinak bych to napsal)
up_and_down_symbol_address:
.DB	4,0b00110110,0b01100011,0b00110110

  ; Dvojtecka ":"
colon_address:
.DB	2,0b00010100

  ; Carka ","
line_address:
.DB	2,0b11000000

  ; Vykricnik "!"
ex_mark_address:
.DB	2,0b01011111

  ; "("
open_parenthesis_address:
.DB	3,0b00111110,0b01000001,0

  ; ")"
close_parenthesis_address:
.DB	3,0b01000001,0b00111110,0

  ; "-"
dash_address:
.DB	4,0b00001000,0b00001000,0b00001000,0,0


  ; Znak pro ukonceni textu (tx)
  ; I kdyz definovat ho je irelevantni, protoze se vlastne z principu nemuze
  ; zobrazit (definuje ukonceni podprogramu "write_text_from_DB"). Ale pro
  ; debug se muze hodit
etx_address:
.DB	8,0b00000010,0b00111111,0b01000010,0b00000000,0b00010100,0b00001000,0b00010100

  ; Znak pro odradkovani - opet se nebude zobrazovat
lf_address:
.DB	6,0b00010000,0b00111000,0b01111100,0b00010000,0b00011111


  ; "A" - stridava (AC) vazba na vstupu
ac_coupling_is_enables_address:
.DB	6,0b01111111,0b01000011,0b01110101,0b01000011,0b01111111

  ; Takove to "B", ktere naznacuje, ze buffer je zaneprazdnen
buffer_is_busy_address:
.DB	6,0b01111111,0b01000001,0b01010101,0b01101011,0b01111111

  ; Znak "C", ktery rika, ze neni zapla zadna synchronizace
continuous_trigger_is_enabled_address:
.DB	6,0b01111111,0b01100011,0b01011101,0b01011101,0b01111111

  ; "D" - stejnosmerna (DC) vazba na vstupu
dc_coupling_is_enabled_address:
.DB	6,0b01111111,0b01000001,0b01011101,0b01100011,0b01111111

  ; "E" - externi trigger
external_trigger_is_enabled_address:
.DB	6,0b01111111,0b01000001,0b01010101,0b01011101,0b01111111

  ; "I" - interni trigger
internal_trigger_is_enabled_address:
.DB	6,0b01111111,0b01011101,0b01000001,0b01011101,0b01111111

  ; Takove to "P", ktere rika, ze vzorkovani je periodicke
sampling_is_periodic_address:
.DB	6,0b01111111,0b01000001,0b01110101,0b01111011,0b01111111

  ; "S" - Single mod pro trigger
trigger_single_mode_is_enabled_address:
.DB	5,0b01111111,0b01011011,0b01010101,0b01101101,0b01111111

  ; 2x zoom
two_special_symbol_address:
.DB	6,0b01111111,0b01001101,0b01010101,0b01011011,0b01111111

  ; 5x zoom
five_special_symbol_address:
.DB	6,0b01111111,0b01011001,0b01010101,0b01101101,0b01111111









; *-----------------------------------------*
; |      micro cisla, pismena, znaky        |
; *-----------------------------------------*

; *-----------------------------------------*
; |              CISLA - micro              |
; *-----------------------------------------*
num_0_u_address:
.DB	4,0b00111000,0b01000100,0b00111000

num_1_u_address:
.DB	4,0b00001000,0b01111100,0	; 3

num_2_u_address:
.DB	4,0b01100100,0b01011000,0	; 3

num_3_u_address:
.DB	4,0b01010100,0b01111100,0	; 3

num_4_u_address:
.DB	4,0b00011100,0b01110000,0	; 3

num_5_u_address:
.DB	4,0b01001100,0b00110100,0	; 3

num_6_u_address:
.DB	4,0b00111000,0b01010100,0b00100000

num_7_u_address:
.DB	4,0b00000100,0b01100100,0b00011100

num_8_u_address:
.DB	4,0b00101000,0b01010100,0b00101000

num_9_u_address:
.DB	4,0b00001000,0b01010100,0b00111000

; *-----------------------------------------*
; |             Pismena - micro             |
; *-----------------------------------------*
; Jen nektere, nejsou potreba vsechny
G_big_u_address:
.DB	4,0b01111000,0b01001000,0b01101000

H_big_u_address:
.DB	4,0b01111000,0b00100000,0b01111000

M_big_u_address:
.DB	4,0b01111000,0b00010000,0b01111000

V_big_u_address:
.DB	4,0b00111000,0b01000000,0b00111000

f_small_u_address:
.DB	4,0b00100000,0b01110000,0b00101000

k_small_u_address:
.DB	4,0b01111000,0b00100000,0b01010000

m_small_u_address:
.DB	6,0b01110000,0b00100000,0b01000000,0b00100000,0b01100000

n_small_u_address:
.DB	4,0b01100000,0b00100000,0b01000000

s_small_u_address:
.DB	3,0b01010000,0b00101000,0

z_small_u_address:
.DB	4,0b00010000,0b01110000,0b01000000


; *-----------------------------------------*
; |              Znaky - micro              |
; *-----------------------------------------*
; Jen nektere, nejsou potreba vsechny
mi_u_address:
.DB	4,0b01110000,0b00100000,0b00110000

; *-----------------------------------------*
; |                                         |
; *-----------------------------------------*
; MASCII - modified ASCII
  ; NEMENIT!!! Pokud je zapotrebi, pak zmenit i v "load_char_address" !!!

  ; Oddelovaci symbol - znaci, ze po tomto znaku se muze zalomit radek. Je
  ; potreba doplnit o prislusny kod v "load_char_address" !



.EQU etx = 3	; End of Text - konec textu
.EQU lf = 10	; Line feed - odradkovani

.EQU sp = 32	; mezera	; Oddelovaci symbol
.EQU em = 33	; vykricnik
.EQU dq = 34	; " - dvojte uvozovky

.EQU op = 40	; "("
.EQU cp = 41	; ")"

.EQU li = 44	; carka ","
.EQU mns = 45	; pomlcka/minus
.EQU po = 46	; tecka "."

.EQU n0 = 48	; cislo "0" bude v tomto kodu pod poradovym cislem 48, jako
  ; v klasickem ASCII. Cisla maji prefix "n"
.EQU n1 = 49
.EQU n2 = 50
.EQU n3 = 51
.EQU n4 = 52
.EQU n5 = 53
.EQU n6 = 54
.EQU n7 = 55
.EQU n8 = 56
.EQU n9 = 57

.EQU co = 58	; ":" - dvojtecka

.EQU qu = 63	; otaznik
 ; Velka pismena maji prefix "B" - big
.EQU BA = 65
.EQU BB = 66
.EQU BC = 67
.EQU BD = 68
.EQU BE = 69
.EQU BF = 70
.EQU BG = 71
.EQU BH = 72
.EQU BI = 73
.EQU BJ = 74
.EQU BK = 75
.EQU BL = 76
.EQU BM = 77
.EQU BN = 78
.EQU BO = 79
.EQU BP = 80
.EQU BQ = 81
.EQU BR = 82
.EQU BS = 83
.EQU BT = 84
.EQU BU = 85
.EQU BV = 86
.EQU BW = 87
.EQU BX = 88
.EQU BY = 89
.EQU BZ = 90


  ; Mala pismena maji prefix "n" (pokud by se prefix nepouzil, pak narazime
  ; na problem pri prekladu - male "e" a "x" uz jsou definovany drive.
  ; Pokud bychom pouzili prefix "s", pak zase narazime na problem se zkratkou
  ; "se"... takze jsem zvolil prefix "n" jako "normal"
.EQU na = 97
.EQU nb = 98
.EQU nc = 99
.EQU nd = 100
.EQU ne = 101
.EQU nf = 102
.EQU ng = 103
.EQU nh = 104
.EQU ni = 105
.EQU nj = 106
.EQU nk = 107
.EQU nl = 108
.EQU nm = 109
.EQU nn = 110
.EQU no = 111
.EQU np = 112
.EQU nq = 113
.EQU nr = 114
.EQU ns = 115
.EQU nt = 116
.EQU nu = 117
.EQU nv = 118
.EQU nw = 119
.EQU nx = 120
.EQU ny = 121
.EQU nz = 122

.EQU pipe = 124	; |

.EQU uad = 129	; "up and down" symbol
.EQU au = 130	; arrow up - sipka nahoru
.EQU ad = 131	; arrow down - sipka dolu
.EQU al = 132	; arrow left - sipka vlevo
.EQU ar = 133	; arrow right - sipka vpravo
.EQU rie = 134	; rising edge - nabezna hrana
.EQU fae = 135	; falling edge - sestupna hrana
.EQU bib = 136	; buffer is busy - buffer je zaneprazdnen "B"
.EQU sip = 137	; sampling is periodic - vzorkovani je periodicke
.EQU tss = 138	; two special symbol - "2" - zoom 2x - Osc. mode
.EQU fss = 139	; five special symbol - "5" - zoom 5x - Osc. mode
.EQU ite = 140	; internal trigger enabled - aktivni interni tgigger
.EQU ete = 141	; external trigger enabled - aktivni externi trigger
.EQU cte = 142	; continual trigger enabled - aktivni neustale vzorkovani
.EQU acc = 143	; AC coupling - symbol pro AC vazbu
.EQU dcc = 144	; DC coupling - symbol pro DC vazbu
.EQU tsm = 145	; trigger single mode - trigger se spusti pouze jednou a pak se
  ; cely prubeh zastavi

.EQU mi = 181	; mikro (u)


.EQU miu = 192	; mikro (u) - micro

.EQU BAu = 193	; A - micro
.EQU BBu = 194	; B - micro
.EQU BCu = 195	; C - micro
.EQU BDu = 196	; D - micro
.EQU BEu = 197	; E - micro
.EQU BFu = 198	; F - micro
.EQU BGu = 199	; G - micro
.EQU BHu = 200	; H - micro
.EQU BIu = 201	; I - micro
.EQU BJu = 202	; J - micro
.EQU BKu = 203	; K - micro
.EQU BLu = 204	; L - micro
.EQU BMu = 205	; M - micro
.EQU BNu = 206	; N - micro
.EQU BOu = 207	; O - micro
.EQU BPu = 208	; P - micro
.EQU BQu = 209	; Q - micro
.EQU BRu = 210	; R - micro
.EQU BSu = 211	; S - micro
.EQU BTu = 212	; T - micro
.EQU BUu = 213	; U - micro
.EQU BVu = 214	; V - micro
.EQU BWu = 215	; W - micro
.EQU BXu = 216	; X - micro
.EQU BYu = 217	; Y - micro
.EQU BZu = 218	; Z - micro

.EQU nau = 219	; a - micro
.EQU nbu = 220	; b - micro
.EQU ncu = 221	; c - micro
.EQU ndu = 222	; d - micro
.EQU neu = 223	; e - micro
.EQU nfu = 224	; f - micro
.EQU ngu = 225	; g - micro
.EQU nhu = 226	; h - micro
.EQU niu = 227	; i - micro
.EQU nju = 228	; j - micro
.EQU nku = 229	; k - micro
.EQU nlu = 230	; l - micro
.EQU nmu = 231	; m - micro
.EQU nnu = 232	; n - micro
.EQU nou = 233	; o - micro
.EQU npu = 234	; p - micro
.EQU nqu = 235	; q - micro
.EQU nru = 236	; r - micro
.EQU nsu = 237	; s - micro
.EQU ntu = 238	; t - micro
.EQU nuu = 239	; u - micro
.EQU nvu = 240	; v - micro
.EQU nwu = 241	; w - micro
.EQU nxu = 242	; x - micro
.EQU nyu = 243	; y - micro
.EQU nzu = 244	; z - micro

.EQU nu0 = 245	; 0 - micro
.EQU nu1 = 246	; 1 - micro
.EQU nu2 = 247	; 2 - micro
.EQU nu3 = 248	; 3 - micro
.EQU nu4 = 249	; 4 - micro
.EQU nu5 = 250	; 5 - micro
.EQU nu6 = 251	; 6 - micro
.EQU nu7 = 252	; 7 - micro
.EQU nu8 = 253	; 8 - micro
.EQU nu9 = 254	; 9 - micro






write_text_from_DB:
.DEF separate_symbol = R15	; sem se bude ukladat informace o tom, jestli
  ; byl nacten jeden z oddelovacich znaku (mezera, carka, dvojtecka atd..)
.DEF word_counter = R14

  ; Vypise pismena, ulozena v tabulce/databazi DB. Jedine co musime udelat,
  ; je, ze do Z-pointeru nahrajeme adresu, na ktere se dana tabulka nachazi
  ; O zbytek se postara program. Posledni zank MUSI byt "etx"!!!

  ; data - Xova souradnice
  ; T-bit - barva symbolu

  ; write_text_from_DB_line_SRAM - cislo radku pro LCD - nepovinne, ale pokud
  ; se znaky jiz nebudou vejit na jeden radek, pak se automaticky posune na
  ; dalsi a pouzije tuto promennou. Tato promenna je pro LCD displej!!! (0~7)
  ; Po ukonceni programu nas informuje o tom, jaky radek je aktivni

  ; wr_txt_from_DB_start_line_SRAM - cislo radku - opet nepovinne. Pokud mame
  ; tak dlouhy text, ze se nevejde na jednu obrazovku, tak timto parametrem
  ; rikame, at program zacne s vykreslovanim dat na LCD az od urciteho radku
  ; daneho textu. Tato hodnota se po ukonceni vykreslovani automaticky nastavi
  ; na nulu, aby pri opetovnem zavolani podprogramu, byl text vypsan inhned.
  ; Navic je to logicke, protoze ne vzdy bude chtit "uzivatel" zobrazovat
  ; text od prostredku a ne vzdy zobrazovany text je prez vice nez jednu
  ; obrazovku. Tato promenna se pri ukonceni nastavi na 0 -> zobrazovat
  ; od pocatku

  ; wr_txt_from_DB_actual_line_SRAM - interni pocitadlo radku ("preteceni"
  ; radku). Program si jej nastavuje sam

  ; wr_txt_from_DB_show_X_lines_SRAM - udava, kolik radku se ma vykreslit
  ; (po tom, co vykreslovani zacne). Toto je nepovinna promenna a defaulne se
  ; pri ukonceni tohoto podprogramu nastavi na 7 -> cely displej

  ; Auto_break_line_SRAM - zapnuti/vypnuti zalamovani radku. Bohuzel tento
  ; parametr je povinny. 0 - vypnuti zalamovani, jinak - zalamovat
  ; Tato promenna se po ukonceni programu nemeni, takze je plne na uzivateli
  ; jak s timto parametrem nalozi.

	CLR tmp
	STS wr_txt_from_DB_actual_line_SRAM,tmp	; nastavime na 0

  ; Kontrola "write_text_from_DB_line_SRAM"
	LDS tmp,write_text_from_DB_line_SRAM	; informuje o aktualnim radku
	CPI tmp,8
	BRLO write_text_from_DB_line_OK	; pokud je hodnota mensi jak
  ; 8, pak je vse v poradku. Pokud ne, pak se musi nastavit promenna na 0.
  ; Tzn. prvni radek. (Lepsi si prepsat dalsi radek, nez poslat spatny prikaz
  ; na LCD. Navic pokud potrebujeme cyklicky zapisovat na LCD, tak nemusime
  ; prakticky nic resit)
	CLR tmp	; pokud bylo write_text_from_DB_line_SRAM vetsi jak 8, pak se
  ; natsavi na 0
	STS write_text_from_DB_line_SRAM,tmp

write_text_from_DB_line_OK:
	CLR separate_symbol	; nastavime, ze zatim nebyl nacten zadny
  ; oddelivaci znak -> je to casem potreba pro "check_for_word_size"

	MOV XL,ZL
	MOV XH,ZH	; zaloha informace o Z registru - Z se bude menit
  ; Nyni je v X pointeru ulozena informace o tom, jaky je zrovna aktualni
  ; znak v databazi, kterou chceme vypsat


  ; Kontrola, zda aktualni symbol neni LF (zalomit radek)
	LPM tmp,Z	; "Natahneme" si cislo znaku
	CPI tmp,lf	; porovname s "lf"
	BRNE write_text_from_DB_line_no_lf	; pokud neni, neni potreba nic
  ; resit
	PUSH data	; Kvuli zasobniku. Pokud skocime na nize definovany
  ; kod, tak tam pocitame, ze jsme tento prikaz jiz provedli, takze je tam
  ; instrukce POP. A to by byl problem, protoze by program vlastne nikdy
  ; neskoncil (ve stack by byla ulozena "spatna" adresa a vlastne pri volani
  ; "RET" bychom se dostali na "write_text_from_DB" -> FAIL
	RJMP write_text_from_DB_draw_routine_break_line_now	; V opacnem
  ; pripade se zalomi radek

write_text_from_DB_line_no_lf:
  ; Barva pisma se bude zjistovat pri vykreslovani (sice bude kod o nejaky
  ; ten cyklus pomalejsi, ale usetrime tim dost flash pameti a programatorovy
  ; nervy

	RCALL load_char_address	; nacteme do Z-pointeru soucasne pismeno
  ; (nyni se bude odkazovat na jednotlive bity daneho pismena)

write_text_from_DB_draw_routine:
  ; Stejne pro vsechny znaky. V Z-registru mame ulozenou adresu znaku. Zbyva
  ; tedy jen vykreslit

	LPM CNT,Z+	; stahneme si informaci o tom, jak je dlouhy symbol
  ; Je nutne mit na pameti, ze delka symbolu je i s mezerou!

  ; Test na to, zda chceme aby se automaticky zalamovaly radky, nebo ne
	LDS tmp,Auto_break_line_SRAM
	CPI tmp,0	; porovnani s nulou
	BRNE write_text_from_DB_draw_routine_break_lines	; pokud neni
  ; nula, pak chceme zalamovat radky. V opacnem pripade se musi
  ; "separate_symbol" nastavit na 0 (vyhodnocuje se behem vykreslovacich
  ; algoritmu)
	CLR separate_symbol	; smaze data o oddelovacim symbolu
	RJMP write_text_from_DB_draw_routine_not_separ


write_text_from_DB_draw_routine_break_lines:
	MOV tmp,separate_symbol
	CPI tmp,0	; kontrola, zda nahodou neni aktualni symbol
  ; nahodou oddelovaci. Pokud ano, pak se musi provest kontrola, zda
  ; nasledujici slovo se vejde na aktualni radek LCD
	BREQ write_text_from_DB_draw_routine_not_separ

	RCALL check_for_word_size	; zkontroluje, zda se dane slovo
  ; vejde na dany radek. Pokud ne, pak se automaticky pokracuje na dalsim
  ; radku

write_text_from_DB_draw_routine_not_separ:
	PUSH data	; zaloha Xove souradnice - nelze pred spustenim

  ; Kontrola, zda se muze bez problemu zapisovat na levou polovinu LCD
  ; (Zda se jeste symbol vejde na levou polovinu)
	LDI tmp,65
	SUB tmp,CNT	; 65-(delka pismena)
  ; Je tam 65, protoze delka pismena je o jedno vetsi (ulozena navic
  ; informace o delce znaku)

	CP data,tmp	; porovname Xovou souradnici a "tmp" (maximalni Xova
  ; souradnice. Pokud nepresahneme tuto hodnotu, pak muzeme bez problemu
  ; zapisovat na levou polovinu LCD).
	BRLO write_text_from_DB_draw_routine_left
	CPI data,64	; pokud se znak nevejde cely na levou polovinu, pak
  ; zkontrolujeme, zda by alespon nebylo mozne bez komplikaci zapisovat na
  ; pravou polovinu LCD
	BRLO write_text_from_DB_L_pre
	RJMP write_text_from_DB_draw_routine_right
write_text_from_DB_L_pre:
  ; a pokud se dostaneme sem, pak to bude vetsi orisek...

	CALL select_X_both	; Xova souradnice je stale ulozena v registru
  ; "data" a nebyla zmenena

  ; Musime si spocitat, kolik musime udelat kroku na leve strane LCD a kolik
  ; kroku na prave strane LCD
	LDI CNT2,64
	SUB CNT2,data	; 64-(xova souradnice) - zjisime pocet kroku na leve
  ; strane LCD - pocet kroku je ulozen nyni v CNT2

  ; ted se vypocita pocet kroku na prave polovine LCD
	MOV CNT3,CNT	; CNT - delka znaku
	SUB CNT3,CNT2	; (delka znaku)-(pocet kroku na leve strane LCD)
  ; Vysledek je ulozen v CNT3 - vlastne udava pocet kroku na prave strane LCD


  ; Ted si zkontrolujeme, zda vubec chceme dany znak vykreslit na LCD
	LDS tmp,wr_txt_from_DB_actual_line_SRAM
	LDS data,wr_txt_from_DB_start_line_SRAM
	CP tmp,data	; porovname actual_line a start_line
	BRSH write_text_from_DB_L	; Pokud bude cislo aktualniho radku
  ; vetsi, nebo rovno start_line, pak se text zobrazi
  ; V opacnem pripade se dostaneme sem:


  ; nasledujici 4 radky jsou zakomentovany, protoze se ukazalo, ze nejsou
  ; potreba
;	ADC ZL,CNT	; jednoduse se posuneme se Z pointrem o delku pismene
;	BRCC write_text_from_DB_L_no_C	; kontrola, zda nedoslo k preteceni ->
  ; -> musime pak inkrementovat i ZH. Pokud C=0 -> skok na navesti
;	INC ZH	; v opacnem pripadne inkrementace ZH
;write_text_from_DB_L_no_C:

  ; Kontrola, zda neni pozadovano skocit na dalsi radek (nasledujici slovo se
  ; uz na aktualni radek nevejde)
	MOV tmp,separate_symbol	; se "separate_symbol" nelze pracovat primo
	CPI tmp,0	; porovnani s nulou (neni vyzadovany dalsi radek)
	BREQ write_text_from_DB_L_no_C_OK	; pokud je 0 -> OK

  ; Jinak je vyzadovan dalsi radek
	CLR separate_symbol
	RJMP write_text_from_DB_draw_routine_break_line_now

write_text_from_DB_L_no_C_OK:
	RJMP write_text_from_DB_end	; pokud se nevykreslovalo na levou
  ; polovinu, pak se logicky nebude vykreslovat ani na pravou polovinu


write_text_from_DB_L:
	LPM data,Z+	; nacte se dalsich 8px znaku
	BRTC write_text_from_DB_L_color	; kontrola T-bitu
  ; pokud nebude nastaven, pak se nasledujici operace neprovede
	COM data	; pokud byl nastaven T-bit -> invertuje se hodnota
  ; -> zmena barvy pismena
write_text_from_DB_L_color:
	CALL write_left	; zapise na LCD
	DEC CNT2	; a snizime pocet zbyvajicich kroku na leve strane LCD
	BRNE write_text_from_DB_L	; pokud zbyva vice jak 0 kroku,
  ; pak se provede prikaz znovu


  ; Nasleduje zapis na pravou stranu
	CLR data
	CALL select_X_both	; nastavime Xovou souradnici na prave strane
  ; na pocatek
write_text_from_DB_R:
	DEC CNT3
	BREQ write_text_from_DB_R_end

	LPM data,Z+
	BRTC write_text_from_DB_R_color	; zase T-bit - barva
	COM data	; 1 za 0 a 0 za 1
write_text_from_DB_R_color:
	CALL write_right
	RJMP write_text_from_DB_R

write_text_from_DB_R_end:
  ; Opet kontrola, zda neni pozadovano skocit na dalsi radek (nasl. slovo se
  ; uz na aktualni radek nevejde)
	MOV tmp,separate_symbol
	CPI tmp,0
	BREQ write_text_from_DB_R_end_OK

  ; Jinak je vyzadovan dalsi radek
	CLR separate_symbol
	RJMP write_text_from_DB_draw_routine_break_line_now

write_text_from_DB_R_end_OK:
  ; A nyni vykreslit 1px mezeru
	CLR data
	BRTC write_text_from_DB_R_end_color
	COM data
write_text_from_DB_R_end_color:
	CALL write_right
	RJMP write_text_from_DB_end	; a skok




write_text_from_DB_draw_routine_left:
	CALL select_X_both	; nastaveni Xove souradnice
	MOV CNT2,CNT	; do "CNT2" jsme si ulozili informaci o delce znaku
	DEC CNT2	; musime odecist 1, protoze delka znaku je vlastne o 1
  ; mensi, nez udava prvni cislo. To totiz udava delku i s touto jednobajtovou
  ; informaci. Vyse jsme se ruznymy vypocty tomuto vyhli

  ; Ted si zkontrolujeme, zda vubec chceme dany znak vykreslit na LCD
	LDS tmp,wr_txt_from_DB_actual_line_SRAM
	LDS data,wr_txt_from_DB_start_line_SRAM
	CP tmp,data
	BRSH write_text_from_DB_draw_routine_left_next


  ; nasledujici 4 radky jsou zakomentovany, protoze se ukazalo, ze nejsou
  ; potreba
;	ADC ZL,CNT
;	BRCC write_text_from_DB_draw_routine_left_next_no_C
;	INC ZH
;write_text_from_DB_draw_routine_left_next_no_C:

  ; Kontrola, zda neni pozadovano skocit na dalsi radek
	MOV tmp,separate_symbol
	CPI tmp,0
	BREQ write_text_from_DB_draw_routine_left_next_no_C_OK

	CLR separate_symbol
	RJMP write_text_from_DB_draw_routine_break_line_now

write_text_from_DB_draw_routine_left_next_no_C_OK:
	RJMP write_text_from_DB_end



write_text_from_DB_draw_routine_left_next:
	LPM data,Z+
	BRTC write_text_from_DB_draw_routine_left_next_color
	COM data
write_text_from_DB_draw_routine_left_next_color:
	RCALL write_left
	DEC CNT2
	BRNE write_text_from_DB_draw_routine_left_next	; dokud neni
  ; CNT2 = 0


  ; Kontrola, zda neni pozadovano skocit na dalsi radek
	MOV tmp,separate_symbol
	CPI tmp,0
	BREQ write_text_from_DB_draw_routine_left_next_color_OK

	CLR separate_symbol
	RJMP write_text_from_DB_draw_routine_break_line_now

write_text_from_DB_draw_routine_left_next_color_OK:
  ; 1px mezera
	CLR data
	BRTC write_text_from_DB_draw_routine_left_next_space_color
	COM data
write_text_from_DB_draw_routine_left_next_space_color:
	RCALL write_left
	RJMP write_text_from_DB_end





write_text_from_DB_draw_routine_right:
  ; Kontrola, zda se symbol vejde na radek, nebo ne
	MOV tmp,data	; do "tmp" si ulozime pocatek Xove souradnice
	ADD tmp,CNT	; tmp=(xova souradnice)+(delka znaku)
  ; Nyni mame v "tmp" hodnotu maximalni xove souradnice (vcetne mezery,
  ; protoze v CNT je delka znaku i s mezerou)
	CPI tmp,129
	BRSH write_text_from_DB_draw_routine_right_test	; pokud se nevejde
	RJMP write_text_from_DB_draw_routine_right_OK	; pokud se sem
  ; znak vejde, neni zadny problem -> pokracuje se v klidu dal

write_text_from_DB_draw_routine_right_test:
  ; Pokud je zde sance, ze by se sem vesel znak, ale bez pridane mezery, tak
  ; to ma stale smysl
	CPI tmp,129	; Je to magie. Opravdu me ted nenapada jediny rozumny
  ; duvod, proc to funguje zrovna takhle. Ale co funguje, to neopravuj!
	BRNE write_text_from_DB_draw_routine_right_next_line
  ; pokud, ale neni sance, ze by se znak vykreslil, tak nasleduje skok

	SUBI data,64	; velice podobne vykreslovani na prave polovine LCD
  ; s tim rozdilem, ze se na zaver nevykresli mezera a bude se pokracovat tim,
  ; ze se zvysi cislo radku a nacte se dalsi znak
	CALL select_X_both
	MOV CNT2,CNT
	DEC CNT2

  ; Ted si zkontrolujeme, zda vubec chceme dany znak vykreslit na LCD
	LDS tmp,wr_txt_from_DB_actual_line_SRAM
	LDS data,wr_txt_from_DB_start_line_SRAM
	CP tmp,data
	BRSH write_text_from_DB_draw_routine_right_without_space_next


  ; nasledujici 4 radky jsou zakomentovany, protoze se ukazalo, ze nejsou
  ; potreba
;	ADC ZL,CNT
;	BRCC write_text_from_DB_draw_routine_right_without_space_next_no_C
;	INC ZH
;write_text_from_DB_draw_routine_right_without_space_next_no_C:

  ; Kontrola, zda neni pozadovano skocit na dalsi radek
	MOV tmp,separate_symbol
	CPI tmp,0
	BREQ write_text_from_DB_draw_routine_right_without_space_next_no_C_OK

	CLR separate_symbol
	RJMP write_text_from_DB_draw_routine_break_line_now

write_text_from_DB_draw_routine_right_without_space_next_no_C_OK:
	RJMP write_text_from_DB_end

  ; Tady se kontrola "separate_symbol" nemusi provadet
write_text_from_DB_draw_routine_right_without_space_next:
	LPM data,Z+
	BRTC write_text_from_DB_draw_routine_right_without_space_next_color
	COM data
write_text_from_DB_draw_routine_right_without_space_next_color:
	RCALL write_right
	DEC CNT2
	BRNE write_text_from_DB_draw_routine_right_without_space_next

  ; nastaveni dalsiho radku. Velice podobne kodu nize
	LDS tmp,wr_txt_from_DB_actual_line_SRAM
	INC tmp
	STS wr_txt_from_DB_actual_line_SRAM,tmp

	DEC tmp
	LDS data,wr_txt_from_DB_start_line_SRAM
	CP tmp,data
	BRLO write_text_from_DB_draw_routine_right_without_space_next_end


	INC tmp
	SUB tmp,data
	LDS data,wr_txt_from_DB_show_X_lines_SRAM
	CP tmp,data
	BRNE write_text_from_DB_draw_routine_right_without_space_next_cont

	POP data
	RJMP write_text_from_DB_end_RET

write_text_from_DB_draw_routine_right_without_space_next_cont:
	LDS tmp,write_text_from_DB_line_SRAM
	INC tmp
	SBRC tmp,3	; test na 3. bit (zda bychom se nedostali na radek c. 8)
	CLR tmp
	STS write_text_from_DB_line_SRAM,tmp	; nyni se zapisi data zpet
  ; do SRAM

	MOV data,tmp
	CALL select_line_both	; nastaveni noveho radku
write_text_from_DB_draw_routine_right_without_space_next_end:
	POP data
	CLR data	; nastaveni Xove souradnice na "0"
	CLR CNT	; aby pri souctu vznikla nula
  ; symbol uz je vykreslen, takze to je rozdilne od nize uvedeneho kodu.
	RJMP write_text_from_DB_end_1 	; vynechame operaci POP, uz jsme tuto
  ; rutinu museli provest


write_text_from_DB_draw_routine_right_next_line:
  ; Pokud se sem znak nevejde, pak se musi vykreslit na dalsi radek

	LDS tmp,wr_txt_from_DB_actual_line_SRAM
	INC tmp
	STS wr_txt_from_DB_actual_line_SRAM,tmp

  ; A zase kontrola, zda se vubec neco zacalo vykreslovat na LCD. Protoze
  ; pokud ne, pak nema smysl posouvat se v cislovani radku (to by uzivatele
  ; pouze matlo)
	DEC tmp	; zjistime si hodnotu puvodniho actual_line
	LDS data,wr_txt_from_DB_start_line_SRAM
	CP tmp,data	; porovnani
	BRLO write_text_from_DB_draw_routine_right_next_line_end
  ; Pokud je cislo aktualniho radku mensi jak start_line -> nema smysl inc ->
  ; -> preskoci se nasledujici procedura

  ; Kontrola, zda se ma jeste vykreslovat (wr_txt_from_DB_show_X_lines_SRAM)
	INC tmp	; abychom dostali actual_line+1
	SUB tmp,data	; tmp = actual_line+1 - start_line ; rozdil udava pocet
  ; vykreslenych radku
	LDS data,wr_txt_from_DB_show_X_lines_SRAM
	CP tmp,data	; porovnani
	BRNE write_text_from_DB_draw_routine_right_next_line_cont
  ; Pokud se dosahne pozadovaneho poctu radku -> konec
	POP data	; kvuli Stacku. Jinak by pri RET nastal problemek...
	RJMP write_text_from_DB_end_RET

write_text_from_DB_draw_routine_right_next_line_cont:
	LDS tmp,write_text_from_DB_line_SRAM
	INC tmp	; cislo radku se zvysi o 1
  ; Kontrola, zda neni vetsi jak 7, to znamena v binarni soustave, zda neni
  ; nastavene cislo 0b00001000 -> 3. LSB bit (pocitano od nuly) nesmi byt
  ; v jednicce. Pokud tomu tak bude, pak se musi nastavt do nuly (tim padem se
  ; nastavi cislo radku na nula - jak genialni). Zde je ale jednodussi smazat
  ; cely obsah registru "write_text_from_DB_line_SRAM" (staci jedina instrukce)
	SBRC tmp,3	; test na 3. LSB bit. Preskoci se nasledujici
  ; instrukce, pokud bude bit v nule (neni potreba nic menit)
	CLR tmp	; pokud bylo v write_text_from_DB_line_SRAM cislo 8
	STS write_text_from_DB_line_SRAM,tmp

	MOV data,tmp
	RCALL select_line_both	; nastaveni radku
write_text_from_DB_draw_routine_right_next_line_end:
	POP data
	CLR data	; nastavi se "data" na nula. Pak se bude nastavovat
  ; xova souradnice, takze je vhodne, aby byla nastavena Xova souradnice na 0
	PUSH data
	RJMP write_text_from_DB_draw_routine_left	; a vykresli
  ; se na dalsi radek, samozrejme ze na levou polovinu LCD




 
write_text_from_DB_draw_routine_break_line_now:
  ; Zalomi se radek -> to znamena, ze symbol je uz vykreslen (pripadne neni,
  ; pokud to nebylo zapotrebi)

	LDS tmp,wr_txt_from_DB_actual_line_SRAM
	INC tmp
	STS wr_txt_from_DB_actual_line_SRAM,tmp	; Zvysime cislo aktualniho
  ; radku

  ; A zase kontrola, zda se vubec neco zacalo vykreslovat na LCD. Protoze
  ; pokud ne, pak nema smysl posouvat se v cislovani radku (to by uzivatele
  ; pouze matlo)
	DEC tmp	; zjistime si hodnotu puvodniho actual_line
	LDS data,wr_txt_from_DB_start_line_SRAM
	CP tmp,data	; porovnani
	BRLO write_text_from_DB_draw_routine_break_line_now_end
  ; Pokud je cislo aktualniho radku mensi jak start_line -> nema smysl inc ->
  ; -> preskoci se nasledujici procedura

  ; Kontrola, zda se ma jeste vykreslovat (wr_txt_from_DB_show_X_lines_SRAM)
	INC tmp	; abychom dostali actual_line+1
	SUB tmp,data	; tmp = actual_line+1 - start_line ; rozdil udava pocet
  ; vykreslenych radku
	LDS data,wr_txt_from_DB_show_X_lines_SRAM
	CP tmp,data	; porovnani
	BRNE write_text_from_DB_draw_routine_break_line_now_cont
  ; Pokud se ma jeste vykreslovat -> skok na *_cont

  ; Pokud se dosahne pozadovaneho poctu radku -> konec
	POP data	; kvuli Stacku. Jinak by pri RET nastal problemek...
	RJMP write_text_from_DB_end_RET

write_text_from_DB_draw_routine_break_line_now_cont:
	LDS tmp,write_text_from_DB_line_SRAM
	INC tmp	; cislo radku se zvysi o 1
  ; Kontrola, zda neni vetsi jak 7, to znamena v binarni soustave, zda neni
  ; nastavene cislo 0b00001000 -> 3. LSB bit (pocitano od nuly) nesmi byt
  ; v jednicce. Pokud tomu tak bude, pak se musi nastavt do nuly (tim padem se
  ; nastavi cislo radku na nula - jak genialni). Zde je ale jednodussi smazat
  ; cely obsah registru "write_text_from_DB_line_SRAM" (staci jedina instrukce)
	SBRC tmp,3	; test na 3. LSB bit. Preskoci se nasledujici
  ; instrukce, pokud bude bit v nule (neni potreba nic menit)
	CLR tmp	; pokud bylo v write_text_from_DB_line_SRAM cislo 8
	STS write_text_from_DB_line_SRAM,tmp

	MOV data,tmp
	RCALL select_line_both	; nastaveni radku

  ; Nasledujici radek se cely smaze, jen pokud je nastaveno, ze se ma
  ; vykreslovat vice jak 8 radku (Pak se pismena na LCD prepisuji)
	LDS tmp,wr_txt_from_DB_show_X_lines_SRAM	; do "tmp" si ulozime
  ; informaci, rikajici kolik radku se ma zobrazit
	CPI tmp,9	; porovname s cislem 9
	BRLO write_text_from_DB_draw_routine_break_line_now_end	; pokud je
  ; nastavena hodnota mensi jak 9, pak nemusime nic resit

  ; V opacnem pripade se smaze aktualni radek, aby mohl byt korektne prepsan
  ; novou informaci. Samotny proces mazani zabere celkem dost cyklu, takze se
  ; vyplati zkontrolovat, zda pocet vykreslenych radku presahl 7
	LDI tmp,8
	LDS data,wr_txt_from_DB_start_line_SRAM
	ADD data,tmp	; data = wr_txt_from_DB_start_line_SRAM+8
	LDS tmp,wr_txt_from_DB_actual_line_SRAM
	CP tmp,data	; porovname actual_line a start_line+8
	BRLO write_text_from_DB_draw_routine_break_line_now_end	; Pokud je
  ; actual_line mensi jak start_line+8, tak to znamena, ze zatim jeste nedoslo
  ; k prepisovani puvodniho textu -> Zbytecne provadet mazani radku -> mazani
  ; se preskoci

	CLR data
	CALL X_line	; Smaze aktualni radek
	
write_text_from_DB_draw_routine_break_line_now_end:
	CLR CNT	; nastavime delku znaku na 0 -> je to kvuli tomu, ze kdyz se
  ; provede odradkovani, tak nasleduje nastaveni data (xove souradnice) a pak
  ; se porvede vypocitani nove pocatecni xove souradnice -> musime zajistit,
  ; aby byl soucet nula -> pocatek radku

	POP data
	CLR data	; nastavi se "data" na nula. Pak se bude nastavovat
  ; xova souradnice, takze je vhodne, aby byla nastavena Xova souradnice na 0
;	PUSH data
	RJMP write_text_from_DB_end_1


write_text_from_DB_draw_routine_right_OK:
  ; Dale je to obdoba kodu pro levou polovinu
	SUBI data,64	; radic "zna" jen 64 Xovych adres -> proto musime
  ; odecist 64. Xovou souradnici mame jeste pro jistotu zalohovanou prez
  ; prikaz PUSH
	RCALL select_X_both
	MOV CNT2,CNT
	DEC CNT2

  ; Ted si zkontrolujeme, zda vubec chceme dany znak vykreslit na LCD
	LDS tmp,wr_txt_from_DB_actual_line_SRAM
	LDS data,wr_txt_from_DB_start_line_SRAM
	CP tmp,data
	BRSH write_text_from_DB_draw_routine_right_next	; zapsat data


  ; nasledujici 4 radky jsou zakomentovany, protoze se ukazalo, ze nejsou
  ; potreba
;	ADC ZL,CNT	; nebudeme probihat cely algoritmus pro vykreslovani
  ; bez vykreslovani, ale jenom se posuneme na dalsi znak
;	BRCC write_text_from_DB_draw_routine_right_no_C
;	INC ZH
;write_text_from_DB_draw_routine_right_no_C:

  ; Kontrola, zda neni pozadovano skocit na dalsi radek
	MOV tmp,separate_symbol
	CPI tmp,0
	BREQ write_text_from_DB_draw_routine_right_no_C_OK

	CLR separate_symbol
	RJMP write_text_from_DB_draw_routine_break_line_now

write_text_from_DB_draw_routine_right_no_C_OK:
	RJMP write_text_from_DB_end


write_text_from_DB_draw_routine_right_next:
	LPM data,Z+
	BRTC write_text_from_DB_draw_routine_right_next_color
	COM data
write_text_from_DB_draw_routine_right_next_color:
	RCALL write_right
	DEC CNT2
	BRNE write_text_from_DB_draw_routine_right_next

  ; Kontrola, zda neni pozadovano skocit na dalsi radek
	MOV tmp,separate_symbol
	CPI tmp,0
	BREQ write_text_from_DB_draw_routine_right_next_color_OK

	CLR separate_symbol
	RJMP write_text_from_DB_draw_routine_break_line_now

write_text_from_DB_draw_routine_right_next_color_OK:
  ; 1px mezera
	CLR data
	BRTC write_text_from_DB_draw_routine_right_next_space_color
	COM data
write_text_from_DB_draw_routine_right_next_space_color:
	RCALL write_right
	RJMP write_text_from_DB_end


write_text_from_DB_end:
	POP data	; obnova dat v registru
write_text_from_DB_end_1:
	ADD data,CNT	; (Xova souradnice)=(xova souradnice)+(delka pismene)
	MOV ZH,XH	; obnova Z-pointeru
	MOV ZL,XL
	ADIW ZH:ZL,1	; Posun o 1. Stejne jako u zakomentovaneho kodu, ale
  ; rychlejsi
;	LPM tmp,Z+	; nacte se dalsi znak
	LPM tmp,Z

	CPI tmp,etx	; porovnani s hodnotou "etx" - konec textu
	BREQ write_text_from_DB_end_RET	; pokud bude v nule, skok

;rcall wait_100ms ; pro debug - aby bylo videt, jak se pismo vykresluje
  ; casem se to muze pridat jako feature
	RJMP write_text_from_DB_line_OK	; pokud ne, pokracuje
  ; se znovu od "write_text_from_DB_line_OK", protoze promenna
  ; "write_text_from_DB_line_SRAM" je automaticky kontrolovana uz pri zmene
  ; jeji hodnoty. A navic bychom si predefinovali 

write_text_from_DB_end_RET:
	CLR tmp
	STS wr_txt_from_DB_start_line_SRAM,tmp	; a nastavime start_line na 0

	LDI tmp,8
	STS wr_txt_from_DB_show_X_lines_SRAM,tmp	; nastavime aby se
  ; priste vykreslil text na cely displej (pokud tuto promennou uzivatel
  ; neprepise)

	RET

; *-----------------------------------------*
; |                                         |
; *-----------------------------------------*
check_for_word_size:
  ; takze ted mame v Z-pointeru oddelovaci znak. Ten by se mel vejit
  ; na aktualni radek (pro naseldujici slovo se s tim bude pocitat)
	CLR separate_symbol	; smazeme info o tom, ze symbol je oddelovaci

	MOV word_counter,data	; do "word_counter" se bude ukladat vypoctena
  ; Xova souradnice
  ; Ted si tam ulozime aktualni xovou souradnici
	ADD word_counter,CNT	; pricteme velikost aktualniho oddelovaciho
  ; znaku. Takze ted mame spocitano, od jake souradnice se ma vypisovat dalsi
  ; slovo

  ; Pokud se nasledujici slovo nebude vejit na aktualni radek, pak po
  ; vykresleni tohoto oddelovaciho znaku zapiseme do promenne
  ; "separate_symbol" kod $FF, ktery rekne vykreslovacimu algoritmu, ze ma bez
  ; otaleni (tj. pocitani, zda se nasledujici symbol vejde na aktualni radek)
  ; pokracovat na dalsi radek. Ted uz jen vypocitat, zda se na aktualni radek
  ; nasledujici slove vejde, nebo ne

  ; Musime nacist dalsi symbol. To znamena, ze pohlneme s pointerem Z, takze
  ; je potreba si provest zalohu.
	PUSH ZH
	PUSH ZL

  ; Nacteme si adresu aktualniho symbolu
	MOV ZH,XH
	MOV ZL,XL
  ; A posuneme se o jeden symbol dal
	ADIW ZH:ZL,1

  ; Provedeme zkopirovani Z pointeru do Y
	MOV YH,ZH
	MOV YL,ZL

check_for_word_size_length:
	LPM tmp,Z	; nacteme si cislo znaku (MASCII kod)
	CPI tmp,etx	; pokud bude aktualni znak "etx" -> ukoncovaci znak ->
  ; -> ukoncit algoritmus a vypocitat, zda se slovo vejde na aktualni radek,
  ; nebo ne.
	BREQ check_for_word_size_next_line_test	; pokud bude nula -> vypocet
  ; zda se znak vejde na aktualni radek, nebo ne

	RCALL load_char_address	; Do Z nahraje adresu konkretniho znaku
	MOV tmp,separate_symbol	; zjistime si, jestli nejde o oddelivaci znak
	CPI tmp,0
	BRNE check_for_word_size_next_line_test	; pokud ano, skok
  ; Pokud ne, pokracuje se v pocitani
	LPM tmp,Z	; nacteme si prvni informaci ve znaku - delka znaku
	ADD word_counter,tmp	; a pricteme ji k pocitane Xove souradnici

  ; ted je potreba z ukazatele na znak, ukazat na dalsi symbol. Od toho mame Y
	ADIW YH:YL,1	; posuneme se o dalsi znak
	MOV ZH,YH	; A zmeny se zapisi do Z
	MOV ZL,YL
	RJMP check_for_word_size_length

check_for_word_size_next_line_test:
  ; Provede se potrebne nastaveni

  ; Takze ted vime, ze posledni znak je oddelovaci. Musime ho ale pripocitat
  ; k vypocitane Xove souradnici
	LPM tmp,Z
	ADD tmp,word_counter	; vysledek se ulozi do "tmp" -> ted vyhodne


  ; Obnova Z-pointeru. Musime v poradi FIFO
	POP ZL
	POP ZH

	CPI tmp,130	; a porovname s maximalnim rozlisenim LCD
  ; (127 - max rozliseni - SH kdyz je stejne, nebo rovno +1 , +2 za kazdym
  ; znakem se vypise 1px mezera -> tu lze v krajnim pripade vynechat)
	BRSH check_for_word_size_next_line	; pokud je vetsi, nebo rovno
  ; -> je potreba pokracovat na dalsi radek

  ; V opacnem pripade se jen smaze promenna "separate_symbol"
	CLR separate_symbol
	RET

check_for_word_size_next_line:
	LDI tmp,$AF	; timto kodem rekneme, ze po vykresleni
  ; aktualniho znaku je potreba pokracovat na dalsi radek (resp. staci, kdyz
  ; "separate_symbol" bude nenulova hodnota, ale $AF se dobre hleda
	MOV separate_symbol,tmp	; bohuzel zapsat hodnotu do R15 nelze primo
	RET

; *-----------------------------------------*
; |                                         |
; *-----------------------------------------*



load_char_address:
  ; Musime zjistit, jaky znak se ma vykreslit. Takze musime budto projet
  ; vsechny moznosti, jednu po druhe, nebo si zjistime, kde zhruba mame hledat
  ; a pak to budeme zjistovat podrobneji a podrobneji. Nejprve zacneme u stovek
  ; Vlastne mame 3 moznosti - 0~99 ; 100~199 a 200~255
	LPM tmp,Z	; nahrajeme si do "tmp" znak z flash pameti (tzn.
  ; nahrajeme si do registru znak z definovane tabulky/databaze)

	CPI tmp,100	; porovnani s cislem 100
	BRLO write_text_from_DB_0_to_99	; pokud je mensi jak
  ; 100, pak se provede skok na write....0_to_99

	RJMP write_text_from_DB_100_to_255	; v opacnem pripade
  ; se bude testovat na jine rozmezi

write_text_from_DB_0_to_99:
  ; Pokracuje v patrani desitkach (rozmezi je 0~99). V ASCII
  ; tabulce jsou pismena abecedy (a tudiz nejpouzivanejsi znaky) v rozmezi
  ; 65~90, proto se vyplati kontrola od "konce"
	CPI tmp,90
	BRLO write_text_from_DB_0_to_89
  ; Pokud se dostaneme sem, pak jsme v rozmezi 90~99
	RJMP write_text_from_DB_90_to_99

write_text_from_DB_0_to_89:
	CPI tmp,80
	BRLO write_text_from_DB_0_to_79
	RJMP write_text_from_DB_80_to_89	; 80~89

write_text_from_DB_0_to_79:
	CPI tmp,70
	BRLO write_text_from_DB_0_to_69
	RJMP write_text_from_DB_70_to_79	; 70~79

write_text_from_DB_0_to_69:
	CPI tmp,60
	BRLO write_text_from_DB_0_to_59
	RJMP write_text_from_DB_60_to_69	; 60~69

write_text_from_DB_0_to_59:
	CPI tmp,50
	BRLO write_text_from_DB_0_to_49
	RJMP write_text_from_DB_50_to_59	; 50~59

write_text_from_DB_0_to_49:
	CPI tmp,40
	BRLO write_text_from_DB_0_to_39
	RJMP write_text_from_DB_40_to_49	; 40~49

write_text_from_DB_0_to_39:
	CPI tmp,30
	BRLO write_text_from_DB_0_to_29
	RJMP write_text_from_DB_30_to_39	; 30~39

write_text_from_DB_0_to_29:
	CPI tmp,20
	BRLO write_text_from_DB_0_to_19
	RJMP write_text_from_DB_20_to_29	; 20~29

write_text_from_DB_0_to_19:
	CPI tmp,10
	BRLO write_text_from_DB_0_to_09
	RJMP write_text_from_DB_10_to_19	; 10~19


write_text_from_DB_0_to_09:
  ; No, jina moznost v desitkach uz neni :)
	CPI tmp,3	; etx - konec textu 
	BRNE write_text_from_DB_4
  ; jeho adresa je vlastne bezvyznamna. Pokud se totiz zjisti, ze NASLEDUJICI
  ; znak je prave tento, tak nastane ukonceni podprogramu pro vykreslovani
  ; z flash pameti. Ale pro debug se muze hodit
	LDI ZH,HIGH(2*etx_address)
	LDI ZL,LOW(2*etx_address)
	RET
write_text_from_DB_4:

write_text_from_DB_10_to_19:
	CPI tmp,10	; LF - odradkovani
	BRNE write_text_from_DB_11
  ; Opet tak trochu postrada smysl. Nebude se totiz zobrazovat, ale pokud se
  ; zjisti, ze je potreba "vykreslit" tento znak, pak se namisto toho provede
  ; odradkovani. Opet: pro debug se muze hodit
	LDI ZH,HIGH(2*lf_address)
	LDI ZL,LOW(2*lf_address)
	RET
write_text_from_DB_11:

write_text_from_DB_20_to_29:

write_text_from_DB_30_to_39:

	CPI tmp,32
	BRNE write_text_from_DB_33
	LDI ZH,HIGH(2*space_address)
	LDI ZL,LOW(2*space_address)
	MOV separate_symbol,tmp	; a rekneme, ze tento symbol je oddelovaci pro
  ; jednotliva slova. To nam umoznuje automaticky skocit na dalsi radek, za
  ; predpokladu, ze se naseldujici slovo nevejde na aktualni radek
	RET
write_text_from_DB_33:
	CPI tmp,33
	BRNE write_text_from_DB_34
	LDI ZH,HIGH(2*ex_mark_address)
	LDI ZL,LOW(2*ex_mark_address)
	RET
write_text_from_DB_34:
	CPI tmp,34
	BRNE write_text_from_DB_35
	LDI ZH,HIGH(2*quotes_double_address)
	LDI ZL,LOW(2*quotes_double_address)
	RET
write_text_from_DB_35:

write_text_from_DB_40_to_49:
	CPI tmp,40
	BRNE write_text_from_DB_41
	LDI ZH,HIGH(2*open_parenthesis_address)
	LDI ZL,LOW(2*open_parenthesis_address)
	RET

write_text_from_DB_41:
	CPI tmp,41
	BRNE write_text_from_DB_42
	LDI ZH,HIGH(2*close_parenthesis_address)
	LDI ZL,LOW(2*close_parenthesis_address)
	RET

write_text_from_DB_42:


write_text_from_DB_44:
	CPI tmp,44
	BRNE write_text_from_DB_45
	LDI ZH,HIGH(2*line_address)
	LDI ZL,LOW(2*line_address)
	RET
write_text_from_DB_45:
	CPI tmp,45
	BRNE write_text_from_DB_46
	LDI ZH,HIGH(2*dash_address)
	LDI ZL,LOW(2*dash_address)
	RET
write_text_from_DB_46:
	CPI tmp,46
	BRNE write_text_from_DB_47
	LDI ZH,HIGH(2*point_address)
	LDI ZL,LOW(2*point_address)
	RET
write_text_from_DB_47:

write_text_from_DB_48:
	CPI tmp,48	; porovnani s cislem 48
	BRNE write_text_from_DB_49	; pokud nesedi, skok
  ; pokud sedi, pokracuje se zde
	LDI ZH,HIGH(2*num_0_address)
	LDI ZL,LOW(2*num_0_address)
	RET
write_text_from_DB_49:
  ; CPI tmp,49	; Nema smysl, protoze toto cislo uz nepatri do rozsahu
  ; 40~49. Zkratka uz neni jina moznost
  ; BRNE write_text_from_DB_50
	LDI ZH,HIGH(2*num_1_address)
	LDI ZL,LOW(2*num_1_address)
	RET


write_text_from_DB_50_to_59:
write_text_from_DB_50:
	CPI tmp,50
	BRNE write_text_from_DB_51
	LDI ZH,HIGH(2*num_2_address)
	LDI ZL,LOW(2*num_2_address)
	RET
write_text_from_DB_51:
	CPI tmp,51
	BRNE write_text_from_DB_52
	LDI ZH,HIGH(2*num_3_address)
	LDI ZL,LOW(2*num_3_address)
	RET
write_text_from_DB_52:
	CPI tmp,52
	BRNE write_text_from_DB_53
	LDI ZH,HIGH(2*num_4_address)
	LDI ZL,LOW(2*num_4_address)
	RET
write_text_from_DB_53:
	CPI tmp,53
	BRNE write_text_from_DB_54
	LDI ZH,HIGH(2*num_5_address)
	LDI ZL,LOW(2*num_5_address)
	RET
write_text_from_DB_54:
	CPI tmp,54
	BRNE write_text_from_DB_55
	LDI ZH,HIGH(2*num_6_address)
	LDI ZL,LOW(2*num_6_address)
	RET
write_text_from_DB_55:
	CPI tmp,55
	BRNE write_text_from_DB_56
	LDI ZH,HIGH(2*num_7_address)
	LDI ZL,LOW(2*num_7_address)
	RET
write_text_from_DB_56:
	CPI tmp,56
	BRNE write_text_from_DB_57
	LDI ZH,HIGH(2*num_8_address)
	LDI ZL,LOW(2*num_8_address)
	RET
write_text_from_DB_57:
	CPI tmp,57
	BRNE write_text_from_DB_58
	LDI ZH,HIGH(2*num_9_address)
	LDI ZL,LOW(2*num_9_address)
	RET
write_text_from_DB_58:
	CPI tmp,58
	BRNE write_text_from_DB_59
	LDI ZH,HIGH(2*colon_address)
	LDI ZL,LOW(2*colon_address)
	RET
write_text_from_DB_59:

write_text_from_DB_60_to_69:
	CPI tmp,63
	BRNE write_text_from_DB_64
	LDI ZH,HIGH(2*quest_adress)
	LDI ZL,LOW(2*quest_adress)
	RET
write_text_from_DB_64:

	CPI tmp,65
	BRNE write_text_from_DB_66
	LDI ZH,HIGH(2*A_big_address)
	LDI ZL,LOW(2*A_big_address)
	RET
write_text_from_DB_66:
	CPI tmp,66
	BRNE write_text_from_DB_67
	LDI ZH,HIGH(2*B_big_address)
	LDI ZL,LOW(2*B_big_address)
	RET
write_text_from_DB_67:
	CPI tmp,67
	BRNE write_text_from_DB_68
	LDI ZH,HIGH(2*C_big_address)
	LDI ZL,LOW(2*C_big_address)
	RET
write_text_from_DB_68:
	CPI tmp,68
	BRNE write_text_from_DB_69
	LDI ZH,HIGH(2*D_big_address)
	LDI ZL,LOW(2*D_big_address)
	RET
write_text_from_DB_69:
;	CPI tmp,69
;	BRNE write_text_from_DB_70	; nepripada v uvahu jina moznost
	LDI ZH,HIGH(2*E_big_address)
	LDI ZL,LOW(2*E_big_address)
	RET

write_text_from_DB_70_to_79:
	CPI tmp,70
	BRNE write_text_from_DB_71
	LDI ZH,HIGH(2*F_big_address)
	LDI ZL,LOW(2*F_big_address)
	RET
write_text_from_DB_71:
	CPI tmp,71
	BRNE write_text_from_DB_72
	LDI ZH,HIGH(2*G_big_address)
	LDI ZL,LOW(2*G_big_address)
	RET
write_text_from_DB_72:
	CPI tmp,72
	BRNE write_text_from_DB_73
	LDI ZH,HIGH(2*H_big_address)
	LDI ZL,LOW(2*H_big_address)
	RET
write_text_from_DB_73:
	CPI tmp,73
	BRNE write_text_from_DB_74
	LDI ZH,HIGH(2*I_big_address)
	LDI ZL,LOW(2*I_big_address)
	RET
write_text_from_DB_74:
	CPI tmp,74
	BRNE write_text_from_DB_75
	LDI ZH,HIGH(2*J_big_address)
	LDI ZL,LOW(2*J_big_address)
	RET
write_text_from_DB_75:
	CPI tmp,75
	BRNE write_text_from_DB_76
	LDI ZH,HIGH(2*K_big_address)
	LDI ZL,LOW(2*K_big_address)
	RET
write_text_from_DB_76:
	CPI tmp,76
	BRNE write_text_from_DB_77
	LDI ZH,HIGH(2*L_big_address)
	LDI ZL,LOW(2*L_big_address)
	RET
write_text_from_DB_77:
	CPI tmp,77
	BRNE write_text_from_DB_78
	LDI ZH,HIGH(2*M_big_address)
	LDI ZL,LOW(2*M_big_address)
	RET
write_text_from_DB_78:
	CPI tmp,78
	BRNE write_text_from_DB_79
	LDI ZH,HIGH(2*N_big_address)
	LDI ZL,LOW(2*N_big_address)
	RET
write_text_from_DB_79:
;	CPI tmp,79
;	BRNE write_text_from_DB_72
	LDI ZH,HIGH(2*O_big_address)
	LDI ZL,LOW(2*O_big_address)
	RET


write_text_from_DB_80_to_89:
	CPI tmp,80
	BRNE write_text_from_DB_81
	LDI ZH,HIGH(2*P_big_address)
	LDI ZL,LOW(2*P_big_address)
	RET
write_text_from_DB_81:
	CPI tmp,81
	BRNE write_text_from_DB_82
	LDI ZH,HIGH(2*Q_big_address)
	LDI ZL,LOW(2*Q_big_address)
	RET
write_text_from_DB_82:
	CPI tmp,82
	BRNE write_text_from_DB_83
	LDI ZH,HIGH(2*R_big_address)
	LDI ZL,LOW(2*R_big_address)
	RET
write_text_from_DB_83:
	CPI tmp,83
	BRNE write_text_from_DB_84
	LDI ZH,HIGH(2*S_big_address)
	LDI ZL,LOW(2*S_big_address)
	RET
write_text_from_DB_84:
	CPI tmp,84
	BRNE write_text_from_DB_85
	LDI ZH,HIGH(2*T_big_address)
	LDI ZL,LOW(2*T_big_address)
	RET
write_text_from_DB_85:
	CPI tmp,85
	BRNE write_text_from_DB_86
	LDI ZH,HIGH(2*U_big_address)
	LDI ZL,LOW(2*U_big_address)
	RET
write_text_from_DB_86:
	CPI tmp,86
	BRNE write_text_from_DB_87
	LDI ZH,HIGH(2*V_big_address)
	LDI ZL,LOW(2*V_big_address)
	RET
write_text_from_DB_87:
	CPI tmp,87
	BRNE write_text_from_DB_88
	LDI ZH,HIGH(2*W_big_address)
	LDI ZL,LOW(2*W_big_address)
	RET
write_text_from_DB_88:
	CPI tmp,88
	BRNE write_text_from_DB_89
	LDI ZH,HIGH(2*X_big_address)
	LDI ZL,LOW(2*X_big_address)
	RET
write_text_from_DB_89:
;	CPI tmp,89
;	BRNE write_text_from_DB_90
	LDI ZH,HIGH(2*Y_big_address)
	LDI ZL,LOW(2*Y_big_address)
	RET

write_text_from_DB_90_to_99:
	CPI tmp,90
	BRNE write_text_from_DB_91
	LDI ZH,HIGH(2*Z_big_address)
	LDI ZL,LOW(2*Z_big_address)
	RET
write_text_from_DB_91:

write_text_from_DB_97:
	CPI tmp,97
	BRNE write_text_from_DB_98
	LDI ZH,HIGH(2*a_small_address)
	LDI ZL,LOW(2*a_small_address)
	RET
write_text_from_DB_98:
	CPI tmp,98
	BRNE write_text_from_DB_99
	LDI ZH,HIGH(2*b_small_address)
	LDI ZL,LOW(2*b_small_address)
	RET
write_text_from_DB_99:
;	CPI tmp,99
;	BRNE write_text_from_DB_100
	LDI ZH,HIGH(2*c_small_address)
	LDI ZL,LOW(2*c_small_address)
	RET


write_text_from_DB_100_to_255:
	CPI tmp,250
	BRLO write_text_from_DB_100_to_249
	RJMP write_text_from_DB_250_to_255
write_text_from_DB_100_to_249:
	CPI tmp,240
	BRLO write_text_from_DB_100_to_239
	RJMP write_text_from_DB_240_to_249
write_text_from_DB_100_to_239:
	CPI tmp,230
	BRLO write_text_from_DB_100_to_229
	RJMP write_text_from_DB_230_to_239
write_text_from_DB_100_to_229:
	CPI tmp,220
	BRLO write_text_from_DB_100_to_219
	RJMP write_text_from_DB_220_to_229
write_text_from_DB_100_to_219:
	CPI tmp,210
	BRLO write_text_from_DB_100_to_209
	RJMP write_text_from_DB_210_to_219
write_text_from_DB_100_to_209:
	CPI tmp,200
	BRLO write_text_from_DB_100_to_199
	RJMP write_text_from_DB_200_to_209
write_text_from_DB_100_to_199:
	CPI tmp,190
	BRLO write_text_from_DB_100_to_189
	RJMP write_text_from_DB_190_to_199
write_text_from_DB_100_to_189:
	CPI tmp,180
	BRLO write_text_from_DB_100_to_179
	RJMP write_text_from_DB_180_to_189
write_text_from_DB_100_to_179:
	CPI tmp,170
	BRLO write_text_from_DB_100_to_169
	RJMP write_text_from_DB_170_to_179
write_text_from_DB_100_to_169:
	CPI tmp,160
	BRLO write_text_from_DB_100_to_159
	RJMP write_text_from_DB_160_to_169
write_text_from_DB_100_to_159:
	CPI tmp,150
	BRLO write_text_from_DB_100_to_149
	RJMP write_text_from_DB_150_to_159
write_text_from_DB_100_to_149:
	CPI tmp,140
	BRLO write_text_from_DB_100_to_139
	RJMP write_text_from_DB_140_to_149
write_text_from_DB_100_to_139:
	CPI tmp,130
	BRLO write_text_from_DB_100_to_129
	RJMP write_text_from_DB_130_to_139
write_text_from_DB_100_to_129:
	CPI tmp,120
	BRLO write_text_from_DB_100_to_119
	RJMP write_text_from_DB_120_to_129
write_text_from_DB_100_to_119:
	CPI tmp,110
	BRLO write_text_from_DB_100_to_109
	RJMP write_text_from_DB_110_to_119
write_text_from_DB_100_to_109:
  ; Uz jina moznost neni....
;	CPI tmp,100
;	BRLO write_text_from_DB_100_to_100
;	RJMP write_text_from_DB_100_to_109
	CPI tmp,100
	BRNE write_text_from_DB_101
	LDI ZH,HIGH(2*d_small_address)
	LDI ZL,LOW(2*d_small_address)
	RET
write_text_from_DB_101:
	CPI tmp,101
	BRNE write_text_from_DB_102
	LDI ZH,HIGH(2*e_small_address)
	LDI ZL,LOW(2*e_small_address)
	RET
write_text_from_DB_102:
	CPI tmp,102
	BRNE write_text_from_DB_103
	LDI ZH,HIGH(2*f_small_address)
	LDI ZL,LOW(2*f_small_address)
	RET
write_text_from_DB_103:
	CPI tmp,103
	BRNE write_text_from_DB_104
	LDI ZH,HIGH(2*g_small_address)
	LDI ZL,LOW(2*g_small_address)
	RET
write_text_from_DB_104:
	CPI tmp,104
	BRNE write_text_from_DB_105
	LDI ZH,HIGH(2*h_small_address)
	LDI ZL,LOW(2*h_small_address)
	RET
write_text_from_DB_105:
	CPI tmp,105
	BRNE write_text_from_DB_106
	LDI ZH,HIGH(2*i_small_address)
	LDI ZL,LOW(2*i_small_address)
	RET
write_text_from_DB_106:
	CPI tmp,106
	BRNE write_text_from_DB_107
	LDI ZH,HIGH(2*j_small_address)
	LDI ZL,LOW(2*j_small_address)
	RET
write_text_from_DB_107:
	CPI tmp,107
	BRNE write_text_from_DB_108
	LDI ZH,HIGH(2*k_small_address)
	LDI ZL,LOW(2*k_small_address)
	RET
write_text_from_DB_108:
	CPI tmp,108
	BRNE write_text_from_DB_109
	LDI ZH,HIGH(2*l_small_address)
	LDI ZL,LOW(2*l_small_address)
	RET
write_text_from_DB_109:
;	CPI tmp,109
;	BRNE write_text_from_DB_110
	LDI ZH,HIGH(2*m_small_address)
	LDI ZL,LOW(2*m_small_address)
	RET


write_text_from_DB_110_to_119:
	CPI tmp,110
	BRNE write_text_from_DB_111
	LDI ZH,HIGH(2*n_small_address)
	LDI ZL,LOW(2*n_small_address)
	RET
write_text_from_DB_111:
	CPI tmp,111
	BRNE write_text_from_DB_112
	LDI ZH,HIGH(2*o_small_address)
	LDI ZL,LOW(2*o_small_address)
	RET
write_text_from_DB_112:
	CPI tmp,112
	BRNE write_text_from_DB_113
	LDI ZH,HIGH(2*p_small_address)
	LDI ZL,LOW(2*p_small_address)
	RET
write_text_from_DB_113:
	CPI tmp,113
	BRNE write_text_from_DB_114
	LDI ZH,HIGH(2*q_small_address)
	LDI ZL,LOW(2*q_small_address)
	RET
write_text_from_DB_114:
	CPI tmp,114
	BRNE write_text_from_DB_115
	LDI ZH,HIGH(2*r_small_address)
	LDI ZL,LOW(2*r_small_address)
	RET
write_text_from_DB_115:
	CPI tmp,115
	BRNE write_text_from_DB_116
	LDI ZH,HIGH(2*s_small_address)
	LDI ZL,LOW(2*s_small_address)
	RET
write_text_from_DB_116:
	CPI tmp,116
	BRNE write_text_from_DB_117
	LDI ZH,HIGH(2*t_small_address)
	LDI ZL,LOW(2*t_small_address)
	RET
write_text_from_DB_117:
	CPI tmp,117
	BRNE write_text_from_DB_118
	LDI ZH,HIGH(2*u_small_address)
	LDI ZL,LOW(2*u_small_address)
	RET
write_text_from_DB_118:
	CPI tmp,118
	BRNE write_text_from_DB_119
	LDI ZH,HIGH(2*v_small_address)
	LDI ZL,LOW(2*v_small_address)
	RET
write_text_from_DB_119:
;	CPI tmp,120
;	BRNE write_text_from_DB_120
	LDI ZH,HIGH(2*w_small_address)
	LDI ZL,LOW(2*w_small_address)
	RET


write_text_from_DB_120_to_129:
	CPI tmp,120
	BRNE write_text_from_DB_121
	LDI ZH,HIGH(2*x_small_address)
	LDI ZL,LOW(2*x_small_address)
	RET
write_text_from_DB_121:
	CPI tmp,121
	BRNE write_text_from_DB_122
	LDI ZH,HIGH(2*y_small_address)
	LDI ZL,LOW(2*y_small_address)
	RET
write_text_from_DB_122:
	CPI tmp,122
	BRNE write_text_from_DB_123
	LDI ZH,HIGH(2*z_small_address)
	LDI ZL,LOW(2*z_small_address)
	RET
write_text_from_DB_123:

write_text_from_DB_124:
	CPI tmp,124
	BRNE write_text_from_DB_125
	LDI ZH,HIGH(2*pipe_symbol_address)
	LDI ZL,LOW(2*pipe_symbol_address)
	RET
write_text_from_DB_125:

;	CPI tmp,129
;	BRNE write_text_from_DB_130
	LDI ZH,HIGH(2*up_and_down_symbol_address)
	LDI ZL,LOW(2*up_and_down_symbol_address)
	RET


write_text_from_DB_130_to_139:
	CPI tmp,130
	BRNE write_text_from_DB_131
	LDI ZH,HIGH(2*arrow_up_symbol_address)
	LDI ZL,LOW(2*arrow_up_symbol_address)
	RET
write_text_from_DB_131:
	CPI tmp,131
	BRNE write_text_from_DB_132
	LDI ZH,HIGH(2*arrow_down_symbol_address)
	LDI ZL,LOW(2*arrow_down_symbol_address)
	RET
write_text_from_DB_132:
	CPI tmp,132
	BRNE write_text_from_DB_133
	LDI ZH,HIGH(2*arrow_left_symbol_address)
	LDI ZL,LOW(2*arrow_left_symbol_address)
	RET
write_text_from_DB_133:
	CPI tmp,133
	BRNE write_text_from_DB_134
	LDI ZH,HIGH(2*arrow_right_symbol_address)
	LDI ZL,LOW(2*arrow_right_symbol_address)
	RET
write_text_from_DB_134:
	CPI tmp,134
	BRNE write_text_from_DB_135
	LDI ZH,HIGH(2*rising_edge_symbol_address)
	LDI ZL,LOW(2*rising_edge_symbol_address)
	RET
write_text_from_DB_135:
	CPI tmp,135
	BRNE write_text_from_DB_136
	LDI ZH,HIGH(2*falling_edge_symbol_address)
	LDI ZL,LOW(2*falling_edge_symbol_address)
	RET
write_text_from_DB_136:
	CPI tmp,136
	BRNE write_text_from_DB_137
	LDI ZH,HIGH(2*buffer_is_busy_address)
	LDI ZL,LOW(2*buffer_is_busy_address)
	RET
write_text_from_DB_137:
	CPI tmp,137
	BRNE write_text_from_DB_138
	LDI ZH,HIGH(2*sampling_is_periodic_address)
	LDI ZL,LOW(2*sampling_is_periodic_address)
	RET
write_text_from_DB_138:
	CPI tmp,138
	BRNE write_text_from_DB_139
	LDI ZH,HIGH(2*two_special_symbol_address)
	LDI ZL,LOW(2*two_special_symbol_address)
	RET
write_text_from_DB_139:
;	CPI tmp,139
;	BRNE write_text_from_DB_140
	LDI ZH,HIGH(2*five_special_symbol_address)
	LDI ZL,LOW(2*five_special_symbol_address)
	RET


write_text_from_DB_140_to_149:
;write_text_from_DB_140:
	CPI tmp,140
	BRNE write_text_from_DB_141
	LDI ZH,HIGH(2*internal_trigger_is_enabled_address)
	LDI ZL,LOW(2*internal_trigger_is_enabled_address)
	RET
write_text_from_DB_141:
	CPI tmp,141
	BRNE write_text_from_DB_142
	LDI ZH,HIGH(2*external_trigger_is_enabled_address)
	LDI ZL,LOW(2*external_trigger_is_enabled_address)
	RET
write_text_from_DB_142:
	CPI tmp,142
	BRNE write_text_from_DB_143
	LDI ZH,HIGH(2*continuous_trigger_is_enabled_address)
	LDI ZL,LOW(2*continuous_trigger_is_enabled_address)
	RET
write_text_from_DB_143:
	CPI tmp,143
	BRNE write_text_from_DB_144
	LDI ZH,HIGH(2*ac_coupling_is_enables_address)
	LDI ZL,LOW(2*ac_coupling_is_enables_address)
	RET
write_text_from_DB_144:
	CPI tmp,144
	BRNE write_text_from_DB_145
	LDI ZH,HIGH(2*dc_coupling_is_enabled_address)
	LDI ZL,LOW(2*dc_coupling_is_enabled_address)
	RET
write_text_from_DB_145:
	CPI tmp,145
	BRNE write_text_from_DB_146
	LDI ZH,HIGH(2*trigger_single_mode_is_enabled_address)
	LDI ZL,LOW(2*trigger_single_mode_is_enabled_address)
	RET

write_text_from_DB_146:

write_text_from_DB_150_to_159:

write_text_from_DB_160_to_169:

write_text_from_DB_170_to_179:

write_text_from_DB_180_to_189:

	CPI tmp,181
	BRNE write_text_from_DB_182
	LDI ZH,HIGH(2*mi_address)
	LDI ZL,LOW(2*mi_address)
	RET
write_text_from_DB_182:


write_text_from_DB_190_to_199:

write_text_from_DB_192:	; mi (u) - micro
	CPI tmp,192
	BRNE write_text_from_DB_193
	LDI ZH,HIGH(2*mi_u_address)
	LDI ZL,LOW(2*mi_u_address)
	RET

; *-----------------------------------------*
; |          MICRO - cisla, pismena         |
; *-----------------------------------------*
write_text_from_DB_193:	; A - micro
write_text_from_DB_194:	; B - micro
write_text_from_DB_195:	; C - micro
write_text_from_DB_196:	; D - micro
write_text_from_DB_197:	; E - micro
write_text_from_DB_198:	; F - micro
write_text_from_DB_199:	; G - micro
;	CPI tmp,199
;	BRNE write_text_from_DB_200
	LDI ZH,HIGH(2*G_big_u_address)
	LDI ZL,LOW(2*G_big_u_address)
	RET

write_text_from_DB_200_to_209:
write_text_from_DB_200:	; H - micro
	CPI tmp,200
	BRNE write_text_from_DB_201
	LDI ZH,HIGH(2*H_big_u_address)
	LDI ZL,LOW(2*H_big_u_address)
	RET
write_text_from_DB_201:	; I - micro
write_text_from_DB_202:	; J - micro
write_text_from_DB_203:	; K - micro
write_text_from_DB_204:	; L - micro
write_text_from_DB_205:	; M - micro
	CPI tmp,205
	BRNE write_text_from_DB_206
	LDI ZH,HIGH(2*M_big_u_address)
	LDI ZL,LOW(2*M_big_u_address)
	RET
write_text_from_DB_206:	; N - micro
write_text_from_DB_207:	; O - micro
write_text_from_DB_208:	; P - micro
write_text_from_DB_209:	; Q - micro

write_text_from_DB_210_to_219:
write_text_from_DB_210:	; R - micro
write_text_from_DB_211:	; S - micro
write_text_from_DB_212:	; T - micro
write_text_from_DB_213:	; U - micro
write_text_from_DB_214:	; V - micro
	CPI tmp,214
	BRNE write_text_from_DB_215
	LDI ZH,HIGH(2*V_big_u_address)
	LDI ZL,LOW(2*V_big_u_address)
	RET
write_text_from_DB_215:	; W - micro
write_text_from_DB_216:	; X - micro
write_text_from_DB_217:	; Y - micro
write_text_from_DB_218:	; Z - micro
write_text_from_DB_219:	; a - micro

write_text_from_DB_220_to_229:
write_text_from_DB_220:	; b - micro
write_text_from_DB_221:	; c - micro
write_text_from_DB_222:	; d - micro
write_text_from_DB_223:	; e - micro
write_text_from_DB_224:	; f - micro
	CPI tmp,224
	BRNE write_text_from_DB_225
	LDI ZH,HIGH(2*f_small_u_address)
	LDI ZL,LOW(2*f_small_u_address)
	RET
write_text_from_DB_225:	; g - micro
write_text_from_DB_226:	; h - micro
write_text_from_DB_227:	; i - micro
write_text_from_DB_228:	; j - micro
write_text_from_DB_229:	; k - micro
;	CPI tmp,229
;	BRNE write_text_from_DB_230
	LDI ZH,HIGH(2*k_small_u_address)
	LDI ZL,LOW(2*k_small_u_address)
	RET

write_text_from_DB_230_to_239:
write_text_from_DB_230:	; l - micro
write_text_from_DB_231:	; m - micro
	CPI tmp,231
	BRNE write_text_from_DB_232
	LDI ZH,HIGH(2*m_small_u_address)
	LDI ZL,LOW(2*m_small_u_address)
	RET
write_text_from_DB_232:	; n - micro
	CPI tmp,232
	BRNE write_text_from_DB_233
	LDI ZH,HIGH(2*n_small_u_address)
	LDI ZL,LOW(2*n_small_u_address)
	RET
write_text_from_DB_233:	; o - micro
write_text_from_DB_234:	; p - micro
write_text_from_DB_235:	; q - micro
write_text_from_DB_236:	; r - micro
write_text_from_DB_237:	; s - micro
	CPI tmp,237
	BRNE write_text_from_DB_238
	LDI ZH,HIGH(2*s_small_u_address)
	LDI ZL,LOW(2*s_small_u_address)
	RET
write_text_from_DB_238:	; t - micro
write_text_from_DB_239:	; u - micro

write_text_from_DB_240_to_249:
write_text_from_DB_240:	; v - micro
write_text_from_DB_241:	; w - micro
write_text_from_DB_242:	; x - micro
write_text_from_DB_243:	; y - micro
write_text_from_DB_244: ; z - micro
	CPI tmp,244
	BRNE write_text_from_DB_245
	LDI ZH,HIGH(2*z_small_u_address)
	LDI ZL,LOW(2*z_small_u_address)
	RET
write_text_from_DB_245:	; 0 - micro
	CPI tmp,245
	BRNE write_text_from_DB_246
	LDI ZH,HIGH(2*num_0_u_address)
	LDI ZL,LOW(2*num_0_u_address)
	RET
write_text_from_DB_246:	; 1 - micro
	CPI tmp,246
	BRNE write_text_from_DB_247
	LDI ZH,HIGH(2*num_1_u_address)
	LDI ZL,LOW(2*num_1_u_address)
	RET
write_text_from_DB_247:	; 2 - micro
	CPI tmp,247
	BRNE write_text_from_DB_248
	LDI ZH,HIGH(2*num_2_u_address)
	LDI ZL,LOW(2*num_2_u_address)
	RET
write_text_from_DB_248:	; 3 - micro
	CPI tmp,248
	BRNE write_text_from_DB_249
	LDI ZH,HIGH(2*num_3_u_address)
	LDI ZL,LOW(2*num_3_u_address)
	RET
write_text_from_DB_249:	; 4 - micro
;	CPI tmp,249
;	BRNE write_text_from_DB_250
	LDI ZH,HIGH(2*num_4_u_address)
	LDI ZL,LOW(2*num_4_u_address)
	RET

write_text_from_DB_250_to_255:
write_text_from_DB_250:	; 5 - micro
	CPI tmp,250
	BRNE write_text_from_DB_251
	LDI ZH,HIGH(2*num_5_u_address)
	LDI ZL,LOW(2*num_5_u_address)
	RET
write_text_from_DB_251:	; 6 - micro
	CPI tmp,251
	BRNE write_text_from_DB_252
	LDI ZH,HIGH(2*num_6_u_address)
	LDI ZL,LOW(2*num_6_u_address)
	RET
write_text_from_DB_252:	; 7 - micro
	CPI tmp,252
	BRNE write_text_from_DB_253
	LDI ZH,HIGH(2*num_7_u_address)
	LDI ZL,LOW(2*num_7_u_address)
	RET
write_text_from_DB_253:	; 8 - micro
	CPI tmp,253
	BRNE write_text_from_DB_254
	LDI ZH,HIGH(2*num_8_u_address)
	LDI ZL,LOW(2*num_8_u_address)
	RET
write_text_from_DB_254:	; 9 - micro
	CPI tmp,254
	BRNE write_text_from_DB_255
	LDI ZH,HIGH(2*num_9_u_address)
	LDI ZL,LOW(2*num_9_u_address)
	RET
write_text_from_DB_255:

load_char_address_err:
  ; ERROR - sem se nesmime dostat
	SBI control_port_LCD,LED
	CALL wait_1000ms
	CBI control_port_LCD,LED
	CALL wait_1000ms
	SBI control_port_LCD,LED
	RCALL wait_for_any_key
	RET


; *-----------------------------------------*
; |                                         |
; *-----------------------------------------*
write_char_from_flash:
  ; Jednoduchoucky program, ktery pouze "tupe" vykresli znak na LCD. Nestara se
  ; o zalamovani radku, neresi situaci, ze se symbol nevejde na dany radek.
  ; Na druhou stranu je o dost rychlejsi nez write_text_from_DB

  ; CNT - delka znaku
  ; data - xova souradnice

	RCALL load_char_address	; Nacteme do Z-pointeru chteny znak

	LPM CNT,Z+	; Nacteme si delku znaku

  ; Ted se musi zjistit, na jakou polovinu LCD se ma zapisovat. Pokud bude Xova
  ; souradnice vetsi, rovna jak 64, pak se jisto-jiste bude zapisovat na levou
  ; polovinu LCD. V opacnem pripade se musi zjistit, zda se znak "bez problemu"
  ; vejde na levou polovinu (Tzn. neni cast znaku na jedne polovine a druha
  ; cast na dalsi polovine LCD)
	CPI data,64	; Takze nejdriv test na tu pravou polovinu
	BRSH w_c_f_f_write_right	; Pokud je Xova souradnice vetsi/rovna
  ; jak 64 -> zapisuje se na pravou polovinu LCD

  ; Pokud tomu tak neni, tak se musi zjistit, jestli se symbol vejde na levou
  ; polovinu cely
  ; To se zjisti nasledovne: data+CNT<64 (Jinymi slovy: Xova souradnice + delka
  ; znaku musi byt mensi jak 64 - to je maximum, kam dokaze radic adresovat)
	ADD data,CNT	; data = data+CNT
	CPI data,65	; 65 protoze, pokud bude X 59 a delka znaku 5, tak
  ; vyjde 64. Ale pokud si to rozkreslite, tak zjistite, ze se to na levou
  ; polovinu LCD vejde. Ach ta cislicovka a pocitani od nuly....
	BRLO w_c_f_f_write_left	; Pokud bude vysledek mensi jak 65 -> OK

	RJMP w_c_f_f_write_left_and_right	; A pokud se bude muset
  ; zapisovat na levou a pravou stranu, pak to bude vetsi sranda
; *-----------------------------------------*
w_c_f_f_write_left:
	SUB data,CNT	; data = data - CNT (Tim mame vypoctenou
  ; puvodni/zadanou xovou souradnici)
	RCALL select_X_left	; Nastavi se Xova souradnice
	ADD data,CNT	; vypocet konecne Xove souradnice

	DEC CNT	; Pocet znaku -1
	PUSH data	; Zaloha Xove souradnice
w_c_f_f_write_left_cycle:
	LPM data,Z+	; Nacte se binarni hodnota znaku
	RCALL write_left	; Vykresli se
	DEC CNT	; Pocet znaku -1
	BRNE w_c_f_f_write_left_cycle	; Opakuj, dokud CNT=0

	CLR data
	RCALL write_left	; 1 px mezera

	POP data	; Obnova Xove souradnice
	RET	; Navrat
; *-----------------------------------------*
w_c_f_f_write_right:
	SUBI data,64	; Xova souradnice -64 -> radici muzeme predavat hodnotu
  ; 0~63 -> proto odecteme 64
	RCALL select_X_right	; Nastavi se Xova souradnice
  ; Nyni se vypocita konecna Xova souradnice (data = data+64+CNT)
  ; +64 - abychom dostali puvodni hodnotu ; +CNT - pocet pixelu pismena
  ; s mezerou
	LDI tmp,64	; tmp=64
	ADD data,tmp	; data = data+tmp
	ADD data,CNT	; data = data+CNT
  ; Takze nyni je v data ulozena vysledna Xova souradnice pro dalsi pouziti
  ; Samo, ze nesmi byt vetsi jak 127 -> nutna kontrola
	CPI data,128
	BRLO w_c_f_f_write_right_cycle_pre	; Pokud bude mensi jak 128, pak
  ; neni co resit

	CLR data	; V opacnem pripade se Xova souradnice nastavi na 0

w_c_f_f_write_right_cycle_pre:
	PUSH data	; Zaloha Xove souradnice
	DEC CNT	; Pocet znaku - 1 (mezera se vykresli "automaticky")
w_c_f_f_write_right_cycle:	; Vykresli jeden symbol
	LPM data,Z+	; Nacte se binarni hodnota znaku
	RCALL write_right	; Vykresleni
	DEC CNT	; Snizeni pocitadla o 1
	BRNE w_c_f_f_write_right_cycle	; A dokud nebude 0 -> skok

	POP data	; obnova Xove souradnice
 	CPI data,0	; Porovnani s nulou
 	BREQ w_c_f_f_write_right_end	; Pokud byla nula, pak se
  ; nebude vykreslovat mezera (vykreslila by se na pocatek souradnic -> FAIL)
 
 	PUSH data	; Zaloha data

 	CLR data
 	RCALL write_right	; 1. px mezera
 
 	POP data	; obnova data
w_c_f_f_write_right_end:
	RET
; *-----------------------------------------*
w_c_f_f_write_left_and_right:
	SUB data,CNT	; vypocet zadane Xove souradnice
	RCALL select_X_left	; Nastaveni Xove souradnice

	DEC CNT	; Pocet kroku -1

w_c_f_f_write_left_and_right_cycle_1:
	PUSH data	; Zaloha Xove souradnice
	LPM data,Z+
	RCALL write_left

	POP data	; Obnova Xove souradnice
	DEC CNT	; Pocet kroku -1
	INC data	; Xova souradnice+1
	CPI data,64
	BRNE w_c_f_f_write_left_and_right_cycle_1	; A dokud se nedojde
  ; na "konec" leve poloviny LCD, tak se bude proces opakovat


  ; Takze, pokud se dostaneme sem, pak se bude jeste zapisovat na pravou
  ; polovinu LCD
	PUSH data	; Zaloha Xove souradnice

	CLR data
	RCALL select_X_right	; Nastaveni Xove souradnice na pravem radici
  ; na pocatek

	POP data	; obnova Xove souradnice

	CPI CNT,0	; Porovnani poctu kroku s nulou
	BREQ w_c_f_f_write_left_and_right_space	; Pokud uz nezbyva vykreslit
  ; z daneho symbolu nic, pak se vykresli jen mezera a program skonci


  ; Jinak se proste pokracuje na prave polovine
w_c_f_f_write_left_and_right_cycle_2:
	PUSH data
	LPM data,Z+
	RCALL write_right

	POP data
	INC data
	DEC CNT
	BRNE w_c_f_f_write_left_and_right_cycle_2	; A dokud nejsme
  ; na konci, tak se bude cyklus opakovat


w_c_f_f_write_left_and_right_space:
	MOV CNT,data	; zaloha "data" do "CNT"
	CLR data
	RCALL write_right	; vykresli 1px mezeru
	MOV data,CNT	; obnova "data" - xove souradnice

	RET










