;---------------------------------------
;Program kodujacy i dekodujacy
;oparty na algorytmie 'rot13'
;praca na konkurs informatyczny
;Uniwersytetu Gdanskiego.
;autor:          Krzysztof Dabrowski
;            L.O. im K.E.N. w Przasnyszu
;
;---------------------------------------

oknad    = $0400
statad   = $0700
okno     = oknad+$3c
pomoc    = $fa         ;word
lbufad   = $fc
sbufad   = $fe


         *= $1000

;---------------------------------------
;program glowny
;---------------------------------------

         jsr cls     ;skasowanie ekranu
         lda #$00
         sta ktoryeor
begin    lda #$03
         jsr putask
         lda ktoryeor
         bne ble2
         ldx #$08
hjk1     lda oknad+$36,x
         eor #$80
         sta oknad+$36,x
         dex
         bpl hjk1
         jmp ble
ble2     ldx #$0a
hjk2     lda oknad+$40,x
         eor #$80
         sta oknad+$40,x
         dex
         bpl hjk2
ble      jsr $ffe4
         beq ble
         cmp #$1d
         bne next1
         lda #$ff
plum     sta ktoryeor
         jmp begin
next1    cmp #$9d
         bne next2
         lda #$00
         jmp plum
next2    cmp #$0d
         bne ble


         lda #$01
         sta co
         jsr getname ;pobranie nazwy pz
         ldx #$0f
hops1    lda tbuf,x
         sta srcnam,x
         dex
         bpl hops1
         lda pos
         sta srcnl

         lda #$02
         sta co
         jsr getname ;pobranie nazwy pw
         ldx #$0f
hops2    lda tbuf,x
         sta destnam,x
         dex
         bpl hops2
         lda pos
         sta destnl
         lda #$00
         jsr putask

         jsr opensrc
         jsr opendest

         lda #$00
         sta lbufad
         lda #$20
         sta lbufad+1
         lda #$00
         sta sbufad
         lda #$30
         sta sbufad+1
         lda #$00
         sta last
         lda #$ff
         sta end

lab      jsr get

         ldx end
         beq oki
         ldx last
         bne closit

oki      jsr put
         jmp lab

closit   jsr stopwr
         lda #$08
         jsr $ffc3

         inc $d020
         jmp *-3

;---------------------------------------
;Procedura pobierajaca nazwe pliku
;---------------------------------------

getname  lda co
         jsr putask ;wydrukowanie okna
         ldx #$10
         jsr input
         lda tbuf
         cmp #$24
         beq pokkat
         rts
pokkat   jsr showdir
         jsr cls
         jmp getname

;---------------------------------------
;Procedura edycji
;---------------------------------------

input    stx len
         lda #$00
         sta pos
         lda #$00
         sta max
petla    ldy pos
         lda okno,y
         eor #$80
         sta okno,y
kjp3     jsr $ffe4
         beq kjp3
         ldy pos
         cmp #$14
         bne in1
         lda #$20
         sta tbuf,y
         sta okno,y
         jsr movl
         jmp petla
in1      cmp #$1d
         bne in2
         lda okno,y
         eor #$80
         sta okno,y
         jsr movr
         jmp petla
in2      cmp #$9d
         bne in3
         lda okno,y
         eor #$80
         sta okno,y
         jsr movl
         jmp petla
in3      cmp #$0d
         bne wstaw
         ldy pos
         beq kjp3
         lda okno,y
         eor #$80
         sta okno,y
         rts

wstaw    sta tbuf,y
         and #$3f
         sta okno,y
         jsr movr
         jmp petla
;---------------------------------------
movr     inc pos
         lda pos
         cmp len
         bne mov1
         dec pos
         lda #$ff
         sta max
mov1     rts
;---------------------------------------
movl     lda #$00
         sta max
         dec pos
         bpl movl1
         inc pos
movl1    rts

;---------------------------------------
;Procedura drukujaca status operacji d.
;---------------------------------------

status   jsr close
         lda #$08
         jsr $ffb4
         lda #$6f
         jsr $ff96
         ldx #$00
lopc     jsr $ffa5
         cmp #$0d
         beq lopb
         and #$3f
         sta statad,x
         inx
         cpx #$26
         bne lopc
lopb     jmp inicjacja

;---------------------------------------
;Kawalek universalny procedury open
;---------------------------------------

open1    jsr $ffbd
         ldy #$00
open2    lda #$08
         tax
         jmp $ffba

;---------------------------------------
;Procedura close.
;---------------------------------------

close    lda #$08
         jsr $ffc3
         jmp $ffcc

;---------------------------------------
;Procedura pokazujaca katalog
;---------------------------------------

showdir  lda #$01
         jsr $e536
         lda #$01
         ldx #<name
         ldy #>name
         jsr open1
         jsr $ffc0
         ldx #$08
         jsr $ffc6
         jsr $ffcf
         jsr $ffcf
dir2     jsr $ffcf
         jsr $ffcf
         lda $90
         bne enddir
         jsr $ffcf
         tax
         jsr $ffcf
         jsr $bdcd
         jsr $ab3b
dir1     jsr $ffcf
         jsr $ffd2
         bne dir1
         lda #$0d
         jsr $ffd2
         jmp dir2
enddir   ldx #$00
dir3     lda press,x
         beq dir4
         jsr $ffd2
         inx
         bne dir3
dir4     jsr close
         jsr $ffe4
         beq dir4
         cmp #$20
         bne dir4
         rts

press    .byte $0d,$0d
         .text "nacisnij spacje"
         .byte 0

;---------------------------------------
;Procedura inicjujaca stacje.
;---------------------------------------

inicjacja lda $ba
         jsr $ffb1
         lda #$6f
         jsr $ff93
         lda #$49
         jsr $ffa8
         jmp $ffae

;--------------------------------------

status1  jsr close
         lda #$08
         jsr $ffb4
         lda #$6f
         jsr $ff96
         ldx #$00
ropc     jsr $ffa5
         cmp #$0d
         beq ropb
         inx
         cpx #$26
         bne ropc
         jsr close
ropb     jmp inicjacja

;---------------------------------------
;procedura kasujaca ekran i inicjujaca
;wektory systemowe
;--------------------------

cls      sei       ;zablokowanie przerw.
         lda #$37  ;ustawienie konfigo-
         sta $01   ;racji pamieci.
         jsr $e5a0 ;wyczyszczenie ekranu
         lda #$01  ;i ustawienie kolorow
         jsr $e536
         lda #$06
         sta $d021
         sta $d021
         jsr $ff8a ;inicjacja wektorow
         jsr $ff84 ;systemowych.
         jsr $e3bf
         jsr $ffcc
         lda #$00  ;zablokowanie prze-
         sta $9d   ;laczania zestawu
         lda #$80  ;znakowitp.
         sta $028a
         sta $0291
         lda #$40
         sta $028b
         cli       ;odblokowanie przerw.
         rts

;---------------------------------------
;procedura drukujaca ramke z wybranym
;komunikatem.    A-numer komunikatu
;---------------------------------------

putask   asl a
         tay
         ldx #$77
kaja1    lda ramka,x
         sta oknad,x
         dex
         bpl kaja1
         lda komad,y
         sta pomoc
         lda komad+1,y
         sta pomoc+1
         ldy #$25
hop1     lda (pomoc),y
         sta oknad+$29,y
         dey
         bpl hop1
         rts

;---------------------------------------
;otwarcie pliku zrodlowego
;---------------------------------------

opensrc  lda #$00
         sta loadptr
         sta loadptr+1
         lda srcnl
         ldx #<srcnam
         ldy #>srcnam
         jsr $ffbd
         lda #$08
         ldx #$08
         ldy #$60
         jsr $ffba
         lda #$08
         jmp $ffc0

;---------------------------------------
;otwarcie pliku wynikowego
;---------------------------------------

opendest lda #$00
         sta saveptr
         sta saveptr+1
         lda destnl
         ldx #<destnam
         ldy #>destnam
         jsr $ffbd
         lda #$09
         ldx #$08
         ldy #$61
         jsr $ffba
         lda #$09
         jmp $ffc0

;---------------------------------------
;Procedura pobierajaca dane
;---------------------------------------

get      lda end
         beq get2
         lda last
         beq ok3
         rts

ok3      lda #$00
         sta end

         lda #$00
         sta lbufad
         lda #$20
         sta lbufad+1

         lda lbufad
         sta pomoc
         lda lbufad+1
         sta pomoc+1

         lda #$08
         jsr $ffb4
         lda #$60
         jsr $ff96

         lda #$00
         sta $90
dalej    jsr $ffa5

         inc $d020

         ldx $90
         bne error

         ldy #$00
         sta (pomoc),y
         inc pomoc
         bne lop1
         inc pomoc+1
lop1     lda pomoc+1
         cmp sbufad+1
         bne dalej
         lda pomoc
         cmp sbufad
         bne dalej

         lda #$00
         sta loadptr
         lda #$30
         sta loadptr+1
         jmp get2

error    tay
         txa
         and #$40
         bne eof
         sec
         rts
eof      tya
         ldy #$00
         sta (pomoc),y
         lda pomoc
         clc
         adc #$01
         sta loadptr
         lda pomoc+1
         adc #$00
         sta loadptr+1
         lda #$ff
         sta last

get2     lda end
         beq ok2
         sec
         rts

ok2      ldy #$00
         lda (lbufad),y
         inc $d020
         inc lbufad
         bne hop2
         inc lbufad+1
hop2     ldx lbufad+1
         cpx loadptr+1
         bne ok1
         ldx lbufad
         cpx loadptr
         bne ok1
         ldx #$ff
         stx end
ok1      rts

;---------------------------------------
;procedura wysylajaca dane
;---------------------------------------

put      ldy #$00
         sta (sbufad),y
         inc sbufad
         bne exit2
         inc sbufad+1
exit2    lda sbufad+1
         cmp #$40
         beq pchnbuf
         rts

pchnbuf  lda #$30
         sta sbufad+1
         lda #$08
         jsr $ffb1
         lda #$61
         jsr $ff93
dalii    ldy #$00
         lda (sbufad),y
         jsr $ffa8
         inc $d020

         inc sbufad
         bne dalii

         inc sbufad+1
lki1     lda sbufad+1
         cmp #$40
         bne dalii
         lda #$30
         sta sbufad+1
         lda #$09
         jmp $ffae

;---------------------------------------
;procedura oprozniajaca bufor i zamyka-
;jaca kanal transmisji.
;---------------------------------------

stopwr   lda #$00
         sta pomoc
         lda #$30
         sta pomoc+1
         lda #$08
         jsr $ffb1
         lda #$61
         jsr $ff93
         lda #$00
         sta $90
dalii2   ldy #$00
         lda (pomoc),y
         jsr $ffa8
         inc $d020
         lda $90
         beq okk
         sec
         rts

okk      inc pomoc
         bne lkj1
         inc pomoc+1
lkj1     lda pomoc+1
         cmp sbufad+1
         bne dalii2
         lda pomoc
         cmp sbufad
         bne dalii2
         lda #$09
         jsr $ffae
         lda #$09
         jmp $ffc3

;---------------------------------------

name     .text "$"
len      .byte 0
pos      .byte 0
max      .byte 0
loadptr  .byte 0,0
saveptr  .byte 0,0
co       .byte 0
last     .byte 0
end      .byte 0
ktoryeor
funkcja  .byte 0

;---------------------------------------

tbuf     .text "                "
srcnam   .text "                "
destnam  .text "                "
srcnl    .byte 0
destnl   .byte 0
;---------------------------------------
ramka    .byte $70
         .text "ccccccccccccccccccc"
         .text "ccccccccccccccccccc"
         .byte $6e,$42
         .text "                   "
         .text "                   "
         .byte $42,$6d
         .text "ccccccccccccccccccc"
         .text "ccccccccccccccccccc"
         .byte $7d
;---------------------------------------
k1       .text "           
,"
         .text "
..           "
k2       .text "    : "
         .text "                   "
k3       .text "     : "
         .text "                   "
k4       .text "    : "
         .text "/    "
;---------------------------------------
komad    .byte <k1,>k1,<k2,>k2,<k3,>k3
         .byte <k4,>k4
;---------------------------------------

