// preruseni SKD
// by fallvonder 2007
// fallvonder.net

//nezapomente prilozit znaky.inc

.include "m169def.inc"

.cseg
.org 0x0000
	jmp start


.org 0x000A
	jmp int_T2
.org 0x0100
.dseg
	sekundy: .byte 1
.cseg
str: .include "znaky.inc"
start: 
	//zasobnik init
	ldi r16, 0x00
	out SPL, r16
	ldi r16, 0x04
	out SPH, r16

	cli ; globální zakázání prerušení
	ldi r16, 0b00001000
	sts ASSR, r16 
	// vyber hodiny od krystalového oscilátoru 32768 Hz
	ldi r16, 0b00000001
	sts TIMSK2, r16 ; povolení prerušení od casovace 2
	ldi r16, 0b00000101
	sts TCCR2A, r16 ; spuštení cítace s delicím pomerem 128
	clr r16 ; zákaz prerušení od
	sts PCMSK0, r16 ; joysticku
	out EIMSK, r16
	sei ; globální povolení prerušení
	ldi r16, 0x00
	sts sekundy, r16

	call init_disp



//… další instrukce …
//jmp hl_cyklus
akcni_cast:
	clr r17
	lds r16, sekundy
	call show_dec
jmp akcni_cast



end: rjmp end

int_T2: //… obsluha prerušení …
	push r16
	in r16, SREG
	push r16
	lds r16, sekundy
	inc r16
	sts sekundy, r16
	pop r16
	out SREG, r16
	pop r16

reti

//___________show char________________
show_char:
clr r25
      ldi r30, low(str<<1)
      ldi r31, high(str<<1)
      subi r16, 0x2a
      clz
      lsl r16
      //brcs, brcc adc - kontrola na prenosy shift left
      adc r31,r25
      lsl r16
      adc r31,r25

      add r30, r16
      adc r31,r25
      cpi r17, 2
      BRLO zasebou
      call show_charPoz
      jmp konec

      zasebou:
      lpm
      mov r18, r0
      adiw r30, 1
      call mov_lccdrX0

      lpm
      mov r18, r0
      adiw r30, 1
      call mov_lccdrX5

      lpm
      mov r18, r0
      adiw r30, 1
      call mov_lccdrX10

      lpm
      mov r18,r0
      call mov_lccdrX15
      konec:

ret

show_charPoz:
      ldi r22, 4
      ldi r23, 0
      ldi r24, 5
      //brcs, brcc adc - kontrola na prenosy shift left
	  //r22 pocet cyklu, r23 zvysujici se ukazatel na displayreg, r24 konstanta
      opakuj:
      lpm 
      mov r18, r0
      adiw r30, 1
      call charPoz_insert
      add r23, r24
      dec r22
      brne opakuj

      clr r17

ret
//r23 5 10..., r21maska, pocita adresu, r17 pozice
charPoz_insert:
      clc
      mov r19, r17
      lsr r19
      ldi r21, 0xF0
      brcc next
              lsl r18
              lsl r18
              lsl r18
              lsl r18
              ldi r21, 0x0F

      next:
      ldi r26, low(LCDDR0)
      add r26, r23
      add r26, r19
      SBCI r26, 1

      ldi r27, high(LCDDR0)
      ld r20, X
      and r20, r21
      or r20, r18
      st X,r20
ret


mov_univ:
       ldi r17, 4
       cykl2:
               lsr r18
      ror r19
      ror r20
      ror r21
       dec r17
       brne cykl2

ret
mov_lccdrX0:
      lds r21, LCDDR0
      lds r20, LCDDR1
      lds r19, LCDDR2
      call mov_univ
      sts LCDDR0, r21
      sts LCDDR1, r20
      sts LCDDR2, r19
ret
mov_lccdrX5:
      lds r21, LCDDR5
      lds r20, LCDDR6
      lds r19, LCDDR7
      call mov_univ
      sts LCDDR5, r21
      sts LCDDR6, r20
      sts LCDDR7, r19
ret
mov_lccdrX10:
      lds r21, LCDDR10
      lds r20, LCDDR11
      lds r19, LCDDR12
      call mov_univ
      sts LCDDR10, r21
      sts LCDDR11, r20
      sts LCDDR12, r19
ret
mov_lccdrX15:
      lds r21, LCDDR15
      lds r20, LCDDR16
      lds r19, LCDDR17
      call mov_univ
      sts LCDDR15, r21
      sts LCDDR16, r20
      sts LCDDR17, r19
ret


init_disp:
      ldi r16, 0xB7
      sts LCDCRB, r16
      ldi r16, 0x10
      sts LCDFRR, r16
      ldi r16, 0x0F
      sts LCDCCR, r16
      ldi r16, 0x80
      sts LCDCRA, r16
ret

cleansweep:
clr r16
ldi r26, low(LCDDR0)
ldi r27, high(LCDDR0)
ldi r17, 18
 cykl:
st X+, r16
dec r17
brne cykl

ret

show_dec:
       ldi r18, 0x64
       call deleni
       push r16
       mov r16, r17

       ldi r18, 0x0a
       call deleni
       push r16
       mov r16,r17

       ori r16, 0b00110000
       ldi r17, 0x07
       call show_char

       pop r16
		ori r16, 0b00110000
       ldi r17, 0x06
       call show_char

       pop r16
       ori r16, 0b00110000
       ldi r17, 0x05
       call show_char
ret
deleni:
               //LDI r16, 0xaf //delenec
       //LDI r18, 10 //delitel
		CLR r17 //register pro zbytek
      	ldi r19, 8 //pocet posunu/scitani 8 bitove cislo
       	lsl r16 //rotace left > carry

       del_cyklu:
 rol r17
 cp r17,r18 //porovnani
 brlo neodcitej //pokud je r17 mensi nez r18 preskocit
 sub r17, r18
       neodcitej:
 rol r16
 dec r19 //dekrem cyklu
 brne del_cyklu //dokud plati

 com r16 //invertuj vysledek
ret

