; programed by NAIRAM (Marian Nagy, om1ei)
; 23.02.2010, Bratislava, Slovak republic
; my page: www.nairam.sk
;
        org $8000       ;best address again

        di              ;nerusit
;vytvori pole adries vram aby sa v nich dalo chodit hore-dole
        ld hl,$4000-$20 ;posun o $20 aby data boli od $4000
        ld de,adrvram
        ld b,192
genadr   call dole
        ld a,l
        ld (de),a
        inc de
        ld a,h
        ld (de),a
        inc de
        djnz genadr

;nastavi farby
        ld a,%00000100  ;zelena
        ld ($5800),a
        ld hl,$5800
        ld de,$5801
        ld bc,768-1
        ldir

;vytvori deliacu ciaru
        ld hl,$400f     ;stred
        ld b,191/2      ;robi sa po dvoch
        ld c,%00000001
        ld d,%10000000
ciara   ld (hl),c
        inc l
        ld (hl),d
        dec l
        call dole
        call dole
        djnz ciara


;vynuluje citac
          ld hl,$5c79
          ld (hl),b     ;istota je istota ;-)



          
;pozicia 0 az 191 chytacov
pocit   ld a,255

smer    inc a        ;posunie poziciu
        ld (pocit+1),a
        ld b,a
        
test    cp 191       ;posledny riadok?
        jr nz,skok
test2   ld a,62         ;vymeni 191-32 za 32
        ex af,af'
        ld a,(test+1)
        ld (test2+1),a
test3        inc a
        ld (pocit+1),a
        ex af,af'
        ld (test+1),a
        ld hl,smer
        call otoc       ;vymeni INC za DEC na adrese HL
        
        ld (test3),a    ;v reg.A je INC alebo DEC
        ld a,b

;spodny riadok
skok    call adresu     ;zisti vram adresu
        call vzor       ;kreslenie

;vrchny riadok
         ld a,b
         sub 62         ;vyska odraziska
         call adresu    ;zisti vram adresu
         call vzor      ;kreslenie

         
;zmazanie predchadzajucej lopticky
clear    ld de,$401e    ;spodna adresa
         ld hl,mazacka  ;adresy mazania vram 8x word
         ld b,8
clearl   ld e,(hl)
         inc l          ;POZOR nesmie byt na prelome FF - 00
         ld d,(hl)
         inc l
         ld a,(de)
         cpl
         ld (de),a
         djnz clearl


;lopticka
lopticka ld a,(pocit+1) ;xx berie z chytacov
stred    sub 32        ;stred lopticky default 205 or 32
         nop            ;NOP alebo ADD a,a

gfx      ld c,255       ;vyplnaci bajt
         ld b,8         ;vyska
         ld ix,mazacka  ;adresy kde sa kreslilo
loop     call adresu

         push af
yy       ld a,30        ;1 az 30 stlpec
         add a,e        ;to pripocita k adrese DE
         ld e,a         
         ex de,hl       ;s HL sa lepsie robi
         ld a,(hl)
         cpl            ;inverzia
         ld (hl),a
         ld (ix+0),l    ;ulozi adresy kde sa kreslilo
         ld (ix+1),h    ;kvoli mazaniu
         inc ix
         inc ix
         pop af

         inc a
         djnz loop

;posnu lopticky vlavo / vpravo
         ld a,(yy+1)
lopt     dec a          ;DEC=smer vlavo  INC=vpravo
         ld (yy+1),a
         
yytemp   cp 1
         jr nz,noaudio
yy2temp  ld a,30
         ex af,af'
         ld a,(yytemp+1)   ;vymena 1 za 30 a opacne
         ld (yy2temp+1),a
         ex af,af'
         ld (yytemp+1),a

         ld hl,lopt
         call otoc       ;vymeni INC za DEC na adrese HL

;AY audio
        call ay

noaudio
;spomalenie
        ei
        halt
citac   ld a,($5c79)    ;citac
        bit 1,a         ;za par sekund zmeni sklon lopticky
        jr z,citacnic
        ld hl,stred+1
        ld (hl),205     ;hodnota 205 (32)
        inc l
        ld (hl),$87     ;add a,a (0)
        
citacnic  di
        jp pocit

        
;BUSY-ho AY rutina pre NAIRAM-a
ay      ld      hl,gong1        ;; Gong 1 zaznie po stlaceni 1
outall  ld      e,$00           ;; Odoslanie dat do AY, HL=adresa dat
        ld      b,$0e
outlop	ld	d,(hl)
        push    bc                  ;; Zapis udaja do AY: E=register D=data
        ld      bc,$fffd
        out     (c),e               ;; reg
        ld      b,$bf
        out     (c),d               ;; data
        pop     bc
	inc	e
	inc	hl
        djnz    outlop

ay2     ld hl,gong2
        push hl
        ld hl,(ay+1)
        ld (ay2+1),hl
        pop hl
        ld (ay+1),hl
        
        ret

gong1   defw      200,0,800       ;; Frekvencia A = 200, Frekvencia C = 800
        defb      0
        defb      $3A             ;; Hraj ton v kanali A,C
        defb      16,0,16         ;; Hlasitosti A,C podla obalky
        defw      $1000           ;; Pomer pre obalku
        defb      0               ;; Kod pre jednorazove doznievanie
gong2   defw      0,150,600       ;; Frekvencia A = 150, Frekvencia C = 600
        defb      0
        defb      $39             ;; Hraj ton v kanalu B,C
        defb      0,16,16         ;; Hlasitosti B,C podla obalky
        defw      $1000           ;; Pomer pre obalku
        defb      0               ;; Kod pre jednorazove doznievanie

        
;vymeni INC za DEC a opacne na adrese HL
otoc    ld a,(hl)
        rra             ;0.bit -> Carry
        ccf             ;negovanie Carry
        rla             ;Carry -> 0.bit
        ld (hl),a     
        ret

        
;vstup A=0az191
;vystup DE=adresa vram
adresu     ld d,0
        ld e,a          ;podla A vypocita HL pointer
        ld hl,adrvram   ;ukazujuci na pole vram adries
        add hl,de       
        add hl,de       ;2x lebo adr je 16.bit
        ld e,(hl)
        inc hl
        ld d,(hl)       ;nacita vram adr
        ret


;klasicka rutina vypoctu vram adresy DOLE
dole    inc h
        ld a,h
        and $07
        ret nz
        ld a,$20
        add a,l
        ld l,a
        ret c
        ld a,$f8
        add a,h
        ld h,a
        ret


;vykresluje chytace DE=vram
vzor    ld a,(de)
        cpl
        ld (de),a
        ld a,e
        add a,$1f       ;koniec riadku
        ld e,a
        ld a,(de)
        cpl
        ld (de),a
        ret


;adresy posledneho kreslenia pre mazanie 8x word
;POZOR nesmie byt na prelome adries xxFF - xx00
mazacka defw $0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
        
;tu budu adresy vram $4000 az $57E0
adrvram defw $0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
        defw $0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
        defw $0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
        defw $0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
        defw $0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
        defw $0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
        defw $0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
        defw $0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
        defw $0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
        defw $0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
        defw $0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
        defw $0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
        defw $0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
        defw $0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
        defw $0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
        defw $0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
        defw $0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
        defw $0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
        defw $0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
        defw $0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
        defw $0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
        defw $0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
        defw $0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
        defw $0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
        
;uz sa mi nechce kodit tak je koniec
koniec  defb $00  ;sem sa naozaj nic neuklada







        
