title Z80 ENKODER, KONZOLA SK5/2
subttl Dodano poenostavljeno klicanje V2.1
;.XLIST

.COMMENT @rem

Revizija 2: 	10.3.1991
- V switch stavku za CCIR9 ton ni bilo ukaza LD HL, CCIR9
- Label Z1NAP premaknjena za 2 ukaza navzgor
- Label ZASED je predelana tako, da gori PAGE ko je BUSY
- Dodatno testiranje BUSY v rutini KEY

Revizija 2.1 :  19.3.1993
- Omogoceno ponovno klicanje zadnje klicane stevilke samo s
  pritiskom na tipko *

- 20.3.1993 Tipka # ne podalj{uje ve~ poziva ampak ga lahko v 
  poljubnem trenutku prekine. Za podaljsevanje klica je predvidena
  tipka *


     ASSEMBLERSKI IZPIS KRMILNEGA PROGRAMA ZA Z80 ENKODER

brez tabele za sinusno digitaliziranje frekvenc po  ZVEI  in  CCIR 
normi.

Stati~ne spremenljivke v sistemu:
 (Indeksna registra sta razdeljena s sinteti'nimi ukazi  na  vi{ji 
in na ni`ji byte: XH in XL ter YH in YL). Registri  (8  bitni)  so 
nato razdeljeni `e na 4+4  bite  tako,  da  je  v  enem  8  bitnem 
registru mo`no shraniti po dve cifri, ki jih beremo na za~etku  za 
dolo~anje konfiguracije sistema (1.cifra 2.,  3.cifra  ter  MODE). 
Ker ne uporabljmo prekinitev je I (interrupt) register  uporabljen 
za MODE in pa kot "flag vektor" za podprograme. Ker v hardveru  ni 
uporabljen RAM za sklad (stack) v  sistemu  ni  mo`no  uporabljati 
ukaze, ki uporabljajo sklad (CALL, RST in prekinitve).


V indeksnih registrih se hranijo cifre za normalen in skupinski 
klic. R3, R4, R5 so cifre normalnega klica. R3', R4', R5' pa 
dodatne cifre za klicanje zaporedja (skupine) im povedo zgornjo
ali spodnjo stevilko v zaporedju klicev. Cifre so zbrisane ce
vsebujejo 0Fh.

                XH		  HL
	+--------+--------+--------+--------+    
	|   ??   |   ??   |   R3'  |   R3   |      IX
	+--------+--------+--------+--------+    

	        YH                YL
	+--------+--------+--------+--------+    
	|   R4   |   R5   |   R4'  |   R5'  |      IY
	+--------+--------+--------+--------+    
	15     12 11     8 7      4 3      0

Interrupt register hrani konfiguracijo (mode) sistema.
pomen bitov je slede~:

				 ____
  I0:   ZVEI/CCIR  norma
  I1:   70/100 ms  dol`ina tona
  I2:   2/3  fiksirane cifre
  I3:   0/G  ton
  I4:   telefon vektor vrnitve podprograma 1=KEYSCAN/KEYPA
  I5:   piskaj vektor   KEYRTN/TELRTN
  I6:   zastavica za skupinski klic  GRKLIC
  I7:   ni uporabljen



Pomen bitov OUT porta 0:               Pomen bitov OUT  porta 1:
    ____
D7: mode                               D7: mute
D6: /MSD display                       D6: on line pulzi
D5: /NSD display                       D5: T (PTT)
D4: /LSD display                       D4: P (Page)
D3: O3 bus display/kbd                 D3: D/A konverter msb
D2: O2 bus                             D2: D/A konverter
D1: O1 bus                             D1: D/A konverter
D0: O0 bus                             D0: D/A konverter lsb

Pomen bitov IN porta 0:
    ____
D5: BUSY == READY vhod iz oddajnika
D4: DIAL vhod iz telefona 
D3: }
D2: }}  vrste iz tipkovnice 
D1: }}
D0: }

@rem


.Z80
.SALL
 
P0	EQU	0FEH 	;port st. 0
P1	EQU	0FDH 	;port st. 1
X 	EQU 	0DDH 	;byte za sinteticne ukaze
Y	EQU	0FDH 	;byte za sinteticne ukaze
 

MOV 	MACRO  index, dest, data  ;sinteticni Z80 ukaz.
	DEFB index		  	
	LD dest,data
	ENDM
;primer MOV X A, L


MOVXL 	MACRO	data	;Sinteticni Z80 ukaz
	DEFB 	X
	LD L,data
	ENDM

MOVXH 	MACRO	data	;Sinteticni Z80 ukaz
	DEFB 	X
	LD H,data
	ENDM

MOVYL 	MACRO	data	;Sinteticni Z80 ukaz
	DEFB 	Y
	LD L,data
	ENDM
	
MOVYH 	MACRO	data	;Sinteticni Z80 ukaz
	DEFB 	Y
	LD H,data
	ENDM

SWAP	MACRO
	RLCA
	RLCA
	RLCA
	RLCA
	ENDM


OBRNIST	MACRO		;zamenja sifre s sen~nimi
	MOV X A,L	;swap R3 in R3'
	SWAP
	MOVXL A
	MOV Y A,L	;swap R4,R4' in R5,R5'
	MOV Y E,H
	MOV Y H,A
	MOV Y L,E
	ENDM

DISPL345 MACRO	;izpisovanje 2 ali 3 fix cifer
  LOCAL DISPLN
        LD A, 10111111B
	OUT (P0),A	;izpis MSD=0FFH
	OR 0F0H
	OUT (P0),A
	LD A,I
	BIT 2,A
	JR Z,DISPLN
	MOV X A,L
	AND 0FH
	OR 10110000B
	OUT (P0),A	;izpis MSD
	OR 0F0H
	OUT (P0),A
DISPLN:	MOV Y A,H
	SWAP
	AND 0FH
	OR 11010000B
	OUT (P0),A	;izpis NSD
	OR 0F0H
	OUT (P0),A
        MOV Y A,H
	AND 0FH
	OR 11100000B
	OUT (P0),A	;izpis LSD
	OR 0F0H
	OUT (P0),A
	ld a,0FFh
	out (P0), a
	ENDM
	
	
WAIT MACRO MS		;~aka doloceno {tevilo ms
	LD E,MS
	LD B,103
	IN A,(P0)
	DEFB 10H, 0FCH
        DEC E
	DEFB 20H, 0F7H
        ENDM


CRLF	MACRO
	DEFB 0Dh, 0Ah
	ENDM
CSEG
	ORG 0000H     	;ZACETEK GLAVNEGA PROGRAMA

        DI              ;onemogo~i prekinitve
        LD   A,10000111B; P1 inicializacija
        OUT  (P1),A
 
        LD   A,0F0H      ;Beri tipkovnico
        OUT  (P0),A
	nop
        IN   A,(P0)
        AND  0FH        ;~e je pritisnjena
        XOR  0FH        ;katerakoli tipka
        JP   Z,SYSINI   ;~e ne beri DIP st.
        JP   SYSKS      ;sko'i na rutino za
                        ;nastavitev DIP stikal

	REPT 20		;pomik nazaj zaradi lepsega izpisa pri type
	DEFB 8
	ENDM

	DEFM " Z80A CPU PAGER Encoder SYSTEM PROGRAM "
	CRLF
	DEFM " VERSION 2.10"
	CRLF
	DEFM " Release Date: June 1989 "
	CRLF
	DEFM " Revision 2: March 1993"
	CRLF
	CRLF
        DEFM "AUTHOR: Kos Leon, C.4.maja 11a "
        DEFM "61380 Cerknica, Slovenia, tel (061)791084. "
	CRLF
	DEFM "         leon.kos [at] uni-lj.si"
	CRLF
	CRLF
        DEFM "The program is provided 'as is'. "
	CRLF
        DEFM "The entire risk risk as to the quality "
        DEFM "and performance of the program is with"
	CRLF
        DEFM "you. Should the program prove defective, "
        DEFM "you assume " 
        DEFM "the etire cost of all necessary "
        DEFM "servicing, repair or correction!"
	CRLF
	REPT 50
	DEFB 1Ah
	ENDM
 
INIT: 	NOP  		;GLAVNI PROGRAM
	  		;se za~enja tukaj po 
			;testiranju nastavitev
        LD   A,0FH	;bri{i display
        OUT  (P0),A
        LD   A,0FFH
        OUT  (P0),A
	MOVYH 0FFh		;brisi R4,R5
	MOV X A,L		;brisi R3
	OR  0Fh
	MOVXL A
	MOVYL 0FFH		;brisi R4'R5'
	MOV X A,L		;brisi R3'
	OR 0F0H
	MOVXL A
	LD A,I	;brisi flag za skupinski klic
	RES 6,A 	
	LD I,A	








 ;.............................................
 
 ;1.ZANKA 
 
 DENA   EQU  10000111B ;ON Line high level
 DNIC   EQU  11000111B ;ON Line low level
 BUSY   EQU  00100000B ;Busy signal iz IN porta
 DIAL   EQU  00010000B ;Dial signal iz IN porta 
 PAGE	EQU  00010000B ;PAGE dioda maska

 ;GLAVNA ZANKA 
 Z1:
; 	MOVYH 0FFh	;brise cifre ki niso  
;	LD   A,I        ;fiksirane
;	BIT  2,A 
;	JR   Z,Z1NAP
; 	DEFB X 		; Izkljucimo brisanje
; 	LD B,L
;       DEFB X
;       LD   A,0FH
;       OR   B
;	MOVXL A


; Testiranje prisotnosti vezja za dodatno fiksiranje
; preostalih dveh cifer (4 in 5) v2.1
	ld a, 11111100b	;testiram prvi dve koloni
	out (P0), a
	nop
	in a, (P0)
	and 0Fh
	cp 0Fh
	jr z, Z1NAP	;~e vezje ni prisotno sko~i
	swap
	ld e,a
	ld a, 11111001b	;testiram naslednji dve koloni
	out (P0), a
	nop
	nop
	in a, (P0)
	and 0Fh
	or e
	mov y h,a
	ld a, i
	bit 2,a
	jr z, z13fix
	ld a, 10111111b	;berem 3. cifro na stikalih
	out (P0), a
	in a, (P0)
	and 0Fh
	ld e, a
	mov x a,l		
	and 0F0h
	or  e
	movxl a

z13fix:	displ345


Z1NAP:	LD A,10000111B
	OUT (P1),A 	;ugasni TX,PG in D/A
	IN   A,(P0)	;Rev 2
        AND  BUSY	
        JR   Z,ZASED
        LD   A,11110000B
        OUT  (P0),A
        IN   A,(P0)
        AND  1111B
        XOR  1111B
        JP   NZ,KEY ;Tipka pritisnjena
        IN   A,(P0)
        AND  DIAL
        JR   Z,TELEF ;Telefon dvignjen
        JR   Z1NAP
 
 ;.............................................
 
 ; ZASEDENO  generiranje tona zasedeno
 ; bere BUSY vhod dokler poziv ni mogo~
 ZASPA  EQU  11100 	;pauza med toni
 LTON1  EQU  255 	;{tevilo nihajev
 TON1   EQU  22 	;~as enega nivoja
 
 ZASED: LD   C,LTON1
 ZAS0:  LD   B,TON1
 ZAS1:  IN   A,(P0)
        AND  BUSY
        JP   NZ,Z1 		;sko~i iz zanke nazaj
        LD   A,DENA OR PAGE	;Rev 2
        OUT  (P1),A
        DJNZ ZAS1
        LD   B,TON1
 ZAS2:  IN   A,(P0)
        AND  BUSY
        JP   NZ,Z1 		;Sko~i iz zanke
        LD   A,DNIC OR PAGE	;Rev 2
        OUT  (P1),A
        DJNZ ZAS2
        DEC  C
        JR   NZ,ZAS0
        LD   BC,ZASPA
 ZAS3:  LD   A,DENA OR PAGE	;Rev 2
        OUT  (P1),A
        IN   A,(P0)
        AND  BUSY
        JP   NZ,Z1 		;sko~i iz zanke 
        DEC  BC
        LD   A,B
        OR   C
        JR   NZ,ZAS3
        JR   ZASED
 ;..............................................
 
 ;Generiranje  ON  LINE pulzov in testiranje
 TELOL  EQU  100 	;dol`ina tona za on line (1ms)
 TELTL  EQU  255	;{tevilo impulzov
 TEPA0  EQU  2000	;100ms pavza med toni
 TEPA1  EQU  18000	;0.9 sek pavza 
 TELEF:  

 ;HL {tevec za H(igh) Dial signal
 
 ONLIN: LD   C,3 	;TU-TU-TU
 ONLI:  LD   E,TELTL
 ONLI0: LD   B,TELOL 	;B {tevec ton
        LD   A,DNIC
        OUT  (P1),A
 ONLI1: DJNZ ONLI1
        LD   B,TELOL
        LD   A,DENA
        OUT  (P1),A
 ONLI2: DJNZ ONLI2
        IN   A,(P0)
        AND  BUSY     	;testiranje BUSY linije
        JR   Z,ZASED
        IN   A,(P0)
        AND  DIAL
        JR   NZ,TEHI
        LD   A,0F0H
        OUT  (P0),A
        IN   A,(P0)
        AND  0FH
        XOR  0FH
        JP   NZ,KEY
        DEC  E
        JR   NZ,ONLI0
        LD   DE,TEPA0
 ONLI3: IN   A,(P0)  ;generiranje pavze med
        AND  BUSY    ;toni
        JR   Z,ZASED ;kontrola dial in BUSY
        IN   A,(P0)  ;~kamo na H signal iz
        AND  DIAL    ;DIAL linije
        JR   NZ,TEHI ;sko'i 'e je visok signal
        LD   A,0F0H
        OUT  (P0),A
        IN   A,(P0)
        AND  0FH
        XOR  0FH
        JP   NZ,KEY
        DEC  DE
        LD   A,D
        OR   E
        JR   NZ,ONLI3
        DEC  C
        JR   NZ,ONLI
        LD   DE,TEPA1
 ONLI4: IN   A,(P0)
        AND  BUSY
        JP   Z,ZASED
        IN   A,(P0)
        AND  DIAL
        JR   NZ,TEHI
        LD   A,0F0H
        OUT  (P0),A
        IN   A,(P0)
        AND  0FH
        XOR  0FH
        JP   NZ,KEY
        DEC  DE
        LD   A,D
        OR   E
        JR   NZ,ONLI4
        JR   ONLIN
 ;..............................................
 ;Stetje impulzov iz telefona po prvem H impulzu
 MAXHI  EQU  4902; 100ms max Hi signal iz DIAL
 MINLO  EQU  MAXHI/256-2;10.4ms min LO level
 MAXLO  EQU  9802 ;200ms max low ~akanje
 MINHI  EQU  MAXLO/256-2 ;minimalno ~akanje
 
 
 TEHI:  LD   D,1 ; D `tevec impulzov digit:=1
 TEHI0: LD   HL,MAXHI
 TEHI1: IN   A,(P0)
        AND  DIAL
        JR   Z,TELO
        DEC  HL 
        LD   A,H
        OR   L
        JR   NZ,TEHI1
        JP   Z1
 TELO:  LD   A,H
        CP   MINLO    ;'e je L signal kraj`i
        JP   NC,ONLIN ;od 10 ms potem ignoriraj
        LD   BC,MAXLO ;LOW signal ker je to 
 TELO1: IN   A,(P0)   ;bounce
        AND  DIAL
        JR   NZ,TEHI2
        DEC  BC
        LD   A,B
        OR   C
        JR   NZ,TELO1
        JR   TEDVA    ;pojdi na drugo `tevilko
 TEHI2: LD   A,B      ;ignoriraj kraj`i impulz
        CP   MINHI    ;od 10 ms
        JR   NC,TEHI1
        INC  D        ;pove'aj digit
        JR   TEHI0    ;in `tej naprej
 TEDVA: LD   A,D      ;testiraj 'e je digit 
        CP   11       ;ve' kot 10 impulzov
        JR   C,TEDVA1 ;'e je ve' potem
        JP   OFFLIN   ;piskaj napako
 TEDVA1: CP   10       ;deset impulzov
        JR   NZ,TEDVA2;spremeni v
        LD   D,0      ; ni'lo
 TEDVA2: LD   A,I      ; kontrola 2 ali 3 cifre
        AND  100B
         JR   NZ,TEDVA3
;3 fiksirane cifre 
         LD   A,D     
        ;shranjevanje 1. cifre v 
        RLA  ;YHH 
        RLA   
        RLA  
        RLA   
        AND  11110000B 
        LD   B,A 
        DEFB Y 
        LD   H,A 
        AND  1111B
        OR   B 
        DEFB Y 
        LD   H,A 
        JR   TEDVA4 
TEDVA3: DEFB X
	LD A,L
        AND 0F0h
	OR D
	DEFB X 		;2 fiksirani cifri 
        LD   L,A	;shranjevanje  
TEDVA4: LD A,1111B 
;izpis 1.cifre 
        OUT  (P0),A 
        LD   A,0FFH 
        OUT  (P0),A
        LD   A,D 
        OR   11110000B 
        OUT  (P0),A 
        AND  11101111B 
        OUT (P0),A 
        OR   11110000B 
        OUT  (P0),A ;Cakanje na HI signaliz telefona max 20 sek 
TWAHI  	EQU  10;zgornjih 16 bitov WAIT zanke
 
        LD   HL,TWAHI
 TWA1:   LD   BC,0FFFFH
 TWA2:   IN   A,(P0)
        AND  DIAL
        JR   NZ,TEHID1
        IN   A,(P0)
        AND  BUSY
        JP   Z,ZASED
        DEC  BC
        LD   A,B
        OR   C
        JR   NZ,TWA2
        DEC  HL
        LD   A,H
        OR   L
        JR   NZ,TWA1
        JP   OFFLIN   ;'as 20 sek prekora'en
 ;..........................................
 ;~tetje impulzov z telefona po drugem H impulzu
 TEHID1: LD   D,1
 DEHI0:  LD   HL,MAXHI
 DEHI1:  IN   A,(P0)
        AND  DIAL
        JR   Z,DELO
        DEC  HL
        LD   A,H
        OR   L
        JR   NZ,DEHI1
        JP   Z1
 DELO:   LD   A,H
        CP   MINLO
        JR   NC,TWA2
        LD   BC,MAXLO
 DELO1:  IN   A,(P0)
        AND  DIAL
        JR   NZ,DEHI2
        DEC  BC
        LD   A,B
        OR   C
        JR   NZ,DELO1
        JR   DEDVA
 DEHI2:  LD   A,B
        CP   MINHI
        JR   NC,DEHI1
        INC  D
        JR   DEHI0
 DEDVA:  LD   A,D
        CP   11
        JR   C,DEDVA1
        JP   OFFLIN
 DEDVA1: CP   10
        JR   NZ,DEDVA2
        LD   D,0
 DEDVA2: LD   A,I
        AND  100B
        JR   Z,DEDVA3
        LD   A,D          ;dve fix cifri
        RLA           
        RLA              
        RLA  
        RLA  
        AND  11110000B
        LD   B,A
        DEFB Y
        LD   A,H
        AND  1111B
        OR   B
        DEFB Y
        LD   H,A
        JR   DEDVA4
 DEDVA3: DEFB Y
        LD   A,H
        AND  11110000B
        OR   D
        DEFB Y
        LD   H,A
 DEDVA4: LD   A,1111B
        OUT  (P0),A
        LD   A,0FFH
        OUT  (P0),A
        LD   A,I
        AND  100B
        JR   Z,DEDVA5
        DEFB X
        LD   A,L
	AND  0Fh			;XLL
        OR   11110000B
        OUT  (P0),A
        AND  11011111B
        OUT  (P0),A
        OR   11110000B
        OUT  (P0),A
        JR   DEDVA6
 DEDVA5: DEFB Y
        LD   A,H
        RRA  
        RRA  
        RRA  
        RRA  
        OR   11110000B
        OUT  (P0),A
        AND  11011111B
        OUT  (P0),A
        OR   11110000B
        OUT  (P0),A
 DEDVA6: LD   A,D
        OR   11110000B
        OUT  (P0),A
        AND  11101111B
        OUT  (P0),A
        OR   11110000B
        OUT  (P0),A
 ;..........................................
 ;Kontrola ali je potrebno pre~itati {e eno 
 ;cifro z telefona in ~itanje le te
        LD   A,I
        AND  100B
        JP   Z,TEPISK
        LD   HL,TWAHI ;~akanje na hi signal
DWA1:   LD   BC,0FFFFH ;Max 20 sek
DWA2:   IN   A,(P0)
        AND  DIAL
        JR   NZ,REHID1
        IN   A,(P0)
        AND  BUSY
        JP   Z,ZASED
        DEC  BC
        LD   A,B
        OR   C
        JR   NZ,DWA2
        DEC  HL
        LD   A,H
        OR   L
        JR   NZ,DWA1
        JP   OFFLIN
;.........................
;stetje impulzov z telefona po tretjem
REHID1: LD   D,1           ; H impulzu
REHI0:  LD   HL,MAXHI
REHI1:  IN   A,(P0)
        AND  DIAL
        JR   Z,RELO
        DEC  HL
        LD   A,H
        OR   L
        JR   NZ,REHI1
        JP   Z1
RELO:   LD   A,H
        CP   MINLO
        JR   NC,DWA2
        LD   BC,MAXLO
RELO1:  IN   A,(P0)
        AND  DIAL
        JR   NZ,REHI2
        DEC  BC
        LD   A,B
        OR   C
        JR   NZ,RELO1
        JR   REDVA
REHI2:  LD   A,B
        CP   MINHI
        JR   NC,REHI1
        INC  D
        JR   REHI0
REDVA:  LD   A,D
        CP   11
        JR   C,REDVA1
        JP   OFFLIN
REDVA1: CP   10
        JR   NZ,REDVA2
        LD   D,0
REDVA2: DEFB Y
;Shranjevanje 3. cifre in prikaz 
;kompletno vseh treh cifer
        LD   A,H
        AND  11110000B
        OR   D
        DEFB Y
        LD   H,A
;Izpis 2. cifre
        RRA  
        RRA  
        RRA  
        RRA  
        OR   11110000B
        OUT  (P0),A
        AND  11011111B
        OUT  (P0),A
        OR   11110000B
        OUT  (P0),A
 ;Izpis 3. cifre
        LD   A,D
        OR   11110000B
        OUT  (P0),A
        AND  11101111B
        OUT  (P0),A
        OR   11110000B
        OUT  (P0),A
 ;izpis 1.cifre
        DEFB X
        LD   A,L
	AND  0Fh			;XLL
        OR   11110000B
        OUT  (P0),A
        AND  10111111B
        OUT  (P0),A
        OR   11110000B
        OUT  (P0),A
 ;...........Konec izpisa vseh cifer..........
        JP   TEPISK
 ;Generiranje OFF-LINE signala za telefon
OFLPA  EQU  2000 ;pavza za OFFLINE piskanje
OFLTON EQU  15 ;dol`ina tonov cca 1ms
OFLTL  EQU  200 ;{tevilo nihajev cca 200ms
 
OFFLIN: LD   C,OFLTL
OFLIN0: LD   B,OFLTON
OFLIN1: LD   A,11110000B
        OUT  (P0),A
        IN   A,(P0)
        AND  1111B
        XOR  1111B
        JP   NZ,KEY
        IN   A,(P0)
        AND  DIAL
        JR   NZ,OFLCNT ;pojdi na `tetje
        IN   A,(P0)    ;H pulsa
        AND  BUSY
        JP   Z,ZASED
        LD   A,DENA
        OUT  (P1),A
        DJNZ OFLIN1
        LD   B,OFLTON
OFLIN2: LD   A,11110000B
        OUT  (P0),A
        IN   A,(P0)
        AND  1111B
        XOR  1111B
        JP   NZ,KEY
        IN   A,(P0)
        AND  DIAL
        JR   NZ,OFLCNT;pojdi za stetje H pulza
        IN   A,(P0)
        AND  BUSY
        JP   Z,ZASED
        LD   A,DNIC
        OUT  (P1),A
        DJNZ OFLIN2
        DEC  C
        JR   NZ,OFLIN0
;......Generiranje pavze......
        LD   HL,OFLPA
OFLIN3: LD   A,11110000B
        OUT  (P0),A
        IN   A,(P0)
        AND  1111B
        XOR  1111B
        JP   NZ,KEY
        IN   A,(P0)
        AND  DIAL
        JR   NZ,OFLCNT ;pojdi na stetje
        IN   A,(P0)    ;H pulsa
        AND  BUSY
        JP   Z,ZASED
        LD   A,DENA
        OUT  (P1),A
        DEC  HL
        LD   A,H
        OR   L
        JR   NZ,OFLIN3
        JR   OFFLIN
;{tetje dolzine H pulza
OFLHPU	 EQU  MAXHI/104*51 ;maksimalo 'akanje 
OFLCNT: LD   HL,OFLHPU    ;na low signal
OFLIN5: LD   A,11110000B
        OUT  (P0),A
        IN   A,(P0)
        AND  1111B
        XOR  1111B
        JP   NZ,KEY
        IN   A,(P0)
        AND  DIAL
        JP   Z,OFFLIN
        DEC  HL
        LD   A,H
        OR   L
        JR   NZ,OFLIN5
        JP   Z1
 ;...........................................
 ;KEY-SCAN rutina ko je pritisnjena tipka 
 ;in piskanje OFF LINE v telefon
 
 MAXNKE EQU  50 ;15sek max.'akanje na tipko
 KSHTON EQU  94 ;H pulz v telef. (0.5msek)
 KSLTON EQU  60 ;L pulz v telefon (0.5ms)
 
 KEY:
;v2.1 	DEFB Y		;C=stevec impulzov za piskanje v telefon
;       LD   H,0FFH	;B=DJNZ stevec (zacasen)
        LD   H,1	;D=Scan koda tipke
;       LD   A,I	;HL=stetje dolzine pavze
;       BIT  2,A
;       JR   Z,KEYZAC
;v2.1   DEFB X		;brisanje 3. in ze prej 4. in 5. cifre
;       LD   A,L
;	OR   0Fh
;	DEFB X
;	LD L,A
KEYZAC: LD   C,150 	;stevilo impulzov (150 ms)
KSPIS:  LD   B,KSHTON	;Low ton v telefon
        LD   A,DNIC
        OUT  (P1),A
	IN   A,(P0)	;Rev 2
	AND  BUSY   	;Rev 2
	JP   Z,ZASED	;Rev 2
KSWA1:  DJNZ KSWA1
        LD   A,DENA
        OUT  (P1),A
        LD   B,KSLTON	;High ton v telefon
KSWA2:  DJNZ KSWA2
        LD   A,I
        SET  4,A	;Vektor vrnitve iz KeyScan rutine
        LD   I,A
        JR   KEYSCA
KEYPI:  DEC  C		;to se vrne KeyScan ce je BIT(I4)==1
        JR   NZ,KSPIS
        LD   C,0FFH	;300 ms pavze v telefonu
        LD   A,I
        RES  4,A
        LD   I,A	;Vektor vrnitve iz KeyScan rutine
KEYPA0: JR   KEYSCA	;je resetiran za vracanje na KEYPA
KEYPA:  LD   B,226
KSWA3:  DJNZ KSWA3	;1.175 ms pavze
        DEC  C
        JR   NZ,KEYPA0
        JR   KEYZAC

;..Rutina za skaniranje in izpis tastature
KEYSCA:   
	LD   D,0FFH 		;Nobena tipka ni pritisnjena
	LD   A,11111110B  	;row 1,4,7,*
	OUT  (P0),A
	nop
	IN   A,(P0)
	BIT  0,A
	JR   NZ,KS1
	LD   D,1	; 1
KS1:	BIT  1,A
        JR   NZ,KS2     
	LD   D,4	; 4
KS2:	BIT  2,A
	JR   NZ,KS3
        LD   D,7	; 7
KS3:	BIT  3,A
	JR   NZ,KS4
	LD   D,11	; *

KS4:	LD   A,11111101B	;row 2,5,8,0
        OUT  (P0),A
	nop
        IN   A,(P0)
        BIT  0,A
        JR   NZ,KS5
        LD   D,2	; 2
KS5:    BIT  1,A
        JR   NZ,KS6
        LD   D,5        ; 5
KS6:    BIT  2,A
        JR   NZ,KS7
        LD   D,8        ; 8
KS7:    BIT  3,A
        JR   NZ,KS8
        LD   D,0        ; 0
KS8:	LD   A,11111011B	;row 3,6,9,#
        OUT  (P0),A
	nop
        IN   A,(P0)
        BIT  0,A
        JR   NZ,KS9
        LD   D,3        ; 3
KS9:    BIT  1,A
        JR   NZ,KS10
        LD   D,6        ; 6
KS10:	BIT  2,A
        JR   NZ,KS11
        LD   D,9        ; 9
KS11:	BIT  3,A
	JR NZ,KS12
	JP GRKLIC	; # obdelaj rutino za skupinski klic

KS12:	LD   A, 0FFh	; v2.1
	OUT  (P0), A	; v2.1
	LD   A,D
        CP   0FFH
        JR   NZ,KS13
        INC  HL
        LD   A,MAXNKE
        CP   H
        JP   Z,Z1
        JP   KSOUT
KS13:	XOR  A		; A=0
        OR   H       	; A<-H
        JR   NZ,KSDONE	; ali je H=0
	LD   A,100	; Ali je preteklo 100ms pavze
	CP   L		; vsak scan traja 1ms
	JP   NC,KSOUT
KSDONE: LD   A,10  ; ali je koda tipke ve~ja 
        CP   D     ; od 10
        JP   C,KSPTST
        LD   A,I 
        BIT  2,A
        JR   Z,KS3OUT
	DEFB X		;shrani R3'. cifro v
	LD A,L		;E register
	AND 0F0H	;le zacasno
	LD E,A
        DEFB Y     ;dve fix. cifri
        LD   A,H   ;rotiram tri cifre
        RRA  
        RRA  
        RRA  
        RRA
	AND 0Fh
	OR E
        DEFB X 		;shrani R3.in R3'. cifro
        LD   L,A

        OR   11110000B ;izpis 1. cifre
        OUT  (P0),A
        AND  10111111B
        OUT  (P0),A
        OR   11110000B
        OUT  (P0),A
        DEFB Y
        LD   A,H
        OR   11110000B ;izpis 2.cifre
        OUT  (P0),A
        AND  11011111B
        OUT  (P0),A
        OR   11110000B
        OUT  (P0),A
        RLA            ;rotiranje cifer
        RLA            ; 4 <= 5
        RLA            ; 5 <= D
        RLA  
        AND  11110000B
        OR   D
        DEFB Y
        LD   H,A
        OR   11110000B ; izpis 3.cifre
        OUT  (P0),A
        AND  11101111B
        OUT  (P0),A
        OR   11110000B
        OUT  (P0),A
        LD   HL,0
        JR   KSOUT
KS3OUT: DEFB Y        ; 3 fix. cifre
        LD   A,H      ; rotiram dve cifri
        OR   11110000B; izpis prve cifre
        OUT  (P0),A
        AND  11011111B
        OUT  (P0),A
        OR   11110000B
        OUT  (P0),A
        RLA  
        RLA           ; rotiranje cifer
        RLA           ; 4 <= 5
        RLA           ; 5 <= D
        AND  11110000B
        OR   D
        DEFB Y
        LD   H,A
        OR   11110000B
        OUT  (P0),A
        AND  11101111B
        OUT  (P0),A
        OR   11110000B
        OUT  (P0),A
        LD   A,0FFH
        OUT  (P0),A
        AND  10111111B
        OUT  (P0),A
        LD   A,0FFH
        OUT  (P0),A
        LD   HL,0
KSOUT:  LD   A,I
        BIT  4,A
        JP   Z,KEYPA
        JP   KEYPI
KSPTST: LD   A,I
        BIT  2,A
        JR   Z,KSPTS3
        DEFB X       ;dve fix. cifri
        LD   A,L     ;testiram ali je XL==#0F
        AND  1111B
        CP   0FH
        JR   Z,KSOUT ;ce je potem 
        JP   KEYPIS  ;pojdi na piskanje tonov

KSPTS3: DEFB Y       ;isto testiram tudi 'e 
        LD   A,H     ;so 3 fix. cifre
        AND  11110000B
        CP   0F0H     ;'e koda tipke cifra
        JR   Z,KSOUT ;skaniraj tipke naprej
        JP   KEYPIS
 ;............................................
TEPISK: LD   A,I    ;rutina za piskanje iz
        RES  5,A    ;telefona
        LD   I,A
        JP   KPROG
TELRTN: LD   A,00100111B
        OUT  (P1),A
        IN   A,(P0)
        AND  DIAL
        JP   Z,TELRTN
        LD   HL,MAXHI/100*113
TEHIPA: IN   A,(P0)		;Ali je po pomoti pritisnjena 
        AND  DIAL  		;`tevilka med razgovorom
        JR   Z,TELRTN 		;ce je potem je puls
        DEC  HL       		;kraj`i od 100 ms
        LD   A,H
        OR   L
        JR   NZ,TEHIPA
        LD   A,10000111B;zato ga ignoriraj
        OUT  (P1),A
        JP   Z1
;.........................
KPROG:  LD   A,10111000B
        OUT  (P1),A
        LD   HL,20192 ;210ms pavze pred 
KPWA:   DEC  HL       ;generiranjem sinusev
        LD   A,H
        OR   L
        JR   NZ,KPWA
 
RT      EQU  11      ;koda repeat tona
 
GRPISK:	EXX  	     ;ALTERNATE REGS
        DEFB X       ;rutina ki kontrolira 
        LD   A,H     ;ali je potrebno 
        RRA          ;vstaviti repeat tone
        RRA          ;med ciframi in jih po
        RRA          ;potrebi tudi vstavi
        RRA  
        AND  1111B
        LD   H,A ;H'
        DEFB X
        LD   A,H
        AND  1111B
        LD   L,A ;L'
        DEFB X
        LD   A,L
        AND  1111B
        LD   D,A ;D'
        DEFB Y
        LD   A,H
        RRA  
        RRA  
        RRA  
        RRA  
        AND  1111B
        LD   E,A
        DEFB Y ; E'
        LD   A,H
        AND  1111B
        LD   B,A
;KONTROLA ALI JE POTREBNO VSTAVITI R TON
        LD   A,H
        CP   L
        JR   NZ,ALT1
        LD   L,RT
ALT1:   LD   A,L
        CP   D
        JR   NZ,ALT2
        LD   D,RT
ALT2:   LD   A,D
        CP   E
        JR   NZ,ALT3
        LD   E,RT
ALT3:   LD   A,E
        CP   B
        JR   NZ,ALT4
        LD   B,RT
ALT4:   EXX  ;.....konec rutine za R ton


; Program za piskanje
        LD   A,1
        EX   AF,AF'   	;v A' je vektor vrnitve
        EXX           	;po generiranju 1.cifre
        LD   A,H
        EXX  
        JP   PISKAJ 	;generiraj prvo cifro
KPR1:   LD   A,2
        EX   AF,AF'	;vektor vrnitve za 2.cifro
        EXX        	;ko se vrne iz generiranja
        LD   A,L   	;druge cifre se program
        EXX        	;nadaljuje na labeli KPR2
        JP   PISKAJ
KPR2:   LD   A,3
        EX   AF,AF'
        EXX        	; 3. cifra
        LD   A,D
        EXX  
        JP   PISKAJ
KPR3:   LD   A,4
        EX   AF,AF'
        EXX        	; 4. cifra
        LD   A,E
        EXX  
        JP   PISKAJ
KPR4:   LD   A,5
        EX   AF,AF'
        EXX       	; 5. cifra
        LD   A,B
        EXX  
        JP   PISKAJ
KPR5:	LD A,I
 	BIT  6,A     ;Skoci ce je to skupinski
	JP NZ,GRPRTN	;klic
	LD   L,17  	;BIBIBIBI
BIP:    LD   H,4   	;piskanje v telefon
BI:     LD   C,27  	;5.29 sek ~akanje
BIP0:   LD   B,120
        LD   A,11110111B
        OUT  (P1),A
BIP1:   DJNZ BIP1
        LD   B,120
        LD   A,10110111B
        OUT  (P1),A
BIP2:   DJNZ BIP2
        DEC  C
        JR   NZ,BIP0
        LD   DE,2885
BIP3:   DEC  DE
        LD   A,D
        OR   E
        JR   NZ,BIP3

	LD   A,11111011B 	; testiranje kolone 1 4 7 #
        OUT (P0),A    		; V2.1
        IN   A,(P0) 
        AND  1000B 		 
        JR   NZ, BIP33		;~e je bila oddaja prekinjena
	JR   BIPEXIT		;sko~i na izhod

BIP33:  DEC  H
        JR   NZ,BI
        LD   DE,7692
BIP4:   DEC  DE
        LD   A,D
        OR   E
        JR   NZ,BIP4
        DEC  L
        JR   NZ,BIP   	;konec ~akanja po {estih
;.........        	 sekundah od za~etka poziva
BIPEXIT:LD   A,I
        BIT  5,A
        JP   Z,TELRTN
        JP   KEYRTN
 ;........................
 TAB    EQU  20;Za'etek tabele sinusov za razne
 ;frekvence v blokih po 256 bytov 20*256=5120
 PISKAJ: LD   B,A;Program za generiranje sinusov
        LD   A,I
        BIT  0,A
        JP   Z,PICCIR
        LD   A,B
        CP   0
        JR   NZ, PI1
        LD   A,I
        BIT  3,A
        JR   NZ,PI0
        LD   B,TAB+23
        LD   HL,ZVEIG
        JP   TOUT
PI0:    LD   B,TAB    ;v Breg zapi{emo MSB 
        LD   HL ,ZVEI0;tabele sinusov
        JP   TOUT     ;V HL pa za'etek tabele
PI1:    CP   1        ;za~etkov generiranja
        JR   NZ,PI2   ;sinusov
        LD   B,TAB+1
        LD   HL,ZVEI1
        JP   TOUT
PI2:    CP   2
        JR   NZ,PI3
        LD   B,TAB+2
        LD   HL,ZVEI2
        JP   TOUT
PI3:    CP   3
        JR   NZ,PI4
        LD   B,TAB+3
        LD   HL,ZVEI3
        JP   TOUT
PI4:    CP   4
        JR   NZ,PI5
        LD   B,TAB+4
        LD   HL,ZVEI4
        JP   TOUT
PI5:    CP   5
        JR   NZ,PI6
        LD   B,TAB+5
        LD   HL,ZVEI5
        JP   TOUT
PI6:    CP   6
        JR   NZ,PI7
        LD   B,TAB+6
        LD   HL,ZVEI6
        JP   TOUT
PI7:    CP   7
        JR   NZ,PI8
        LD   B,TAB+7
        LD   HL,ZVEI7
        JP   TOUT
PI8:    CP   8
        JR   NZ,PI9
        LD   B,TAB+8
        LD   HL,ZVEI8
        JP   TOUT
PI9:    CP   9
        JR   NZ,PI10
        LD   B,TAB+9
        LD   HL,ZVEI9
        JP   TOUT
PI10:   LD   B,TAB+10
        LD   HL,ZVEIR
        JP   TOUT
;...........................................
PICCIR: LD   A,B
        CP   0
        JR   NZ,PI11
        LD   A,I
        BIT  3,A
        JR   NZ,PI100
        LD   B,TAB+22
        LD   HL,CCIRG
        JP   TOUT
PI100:  LD   B,TAB+11
        LD   HL,CCIR0
        JP   TOUT
PI11:   CP   1
        JR   NZ,PI12
        LD   B,TAB+12
        LD   HL,CCIR1
        JP   TOUT
PI12:   CP   2
        JR   NZ,PI13
        LD   B,TAB+13
        LD   HL,CCIR2
        JP   TOUT
PI13:   CP   3
        JR   NZ,PI14
        LD   B,TAB+14
        LD   HL,CCIR3
        JP   TOUT
PI14:   CP   4
        JR   NZ,PI15
        LD   B,TAB+15
        LD   HL,CCIR4
        JP   TOUT
PI15:   CP   5
        JR   NZ,PI16
        LD   B,TAB+16
        LD   HL,CCIR5
        JP   TOUT
PI16:   CP   6
        JR   NZ,PI17
        LD   B,TAB+17
        LD   HL,CCIR6
        JP   TOUT
PI17:   CP   7
        JR   NZ,PI18
        LD   B,TAB+18
        LD   HL,CCIR7
        JP   TOUT
PI18:   CP   8
        JR   NZ,PI19
        LD   B,TAB+19
        LD   HL,CCIR8
        JP   TOUT
PI19:   CP   9
        JR   NZ,PI20
        LD   B,TAB+20
	LD   HL,CCIR9	; REVIZIJA 2
        JP   TOUT
PI20:   LD   B,TAB+21
        LD   HL,CCIRR
;........................
TOUT:   LD   D,28  	;100ms puls
;       JP   OUTST 	;Testiranje izpisa
        LD   A,I
        BIT  1,A    
        JR   Z,TOUT1
        LD   D,20 	;70ms puls
TOUT1:  LD   C,0	;zacni na nicti poziciji tabele
CONT0:  LD   E,200	;generiraj 200 bytov tabele
CONT1:  LD   A,(BC)
        OR   10110000B
        OUT  (P1),A      ;glavna zanka za
        INC  C           ;digitaliziraje
        DEC  E           ;sinusov
        JR   NZ,CONT1
        INC  HL
        LD   C,(HL)
        DEC  D
        JP   NZ,CONT0
;........................
OUTRTN: LD   A,10110111B
        OUT  (P1),A   ;gledamo kam kaze 
        EX   AF,AF'   ;vektor vrnitve programa
        CP   1        ;za piskanje in skocimo
        JP   Z,KPR1   ;tja kamor kaze A'
        CP   2
        JP   Z,KPR2
        CP   3
        JP   Z,KPR3
        CP   4
        JP   Z,KPR4
        JP   KPR5
 ;....Glavni program za oddajo iz tipkovnice
 KEYPIS: LD   A,I 
        LD   A,I
        SET  5,A             	;Vektor vrnitve
        LD   I,A
        IN   A,(P0)
        AND  BUSY
        JP   Z,ZASED
        JP   KPROG
 KEYRTN: LD   A,10100111B
        OUT  (P1),A 		;Brisi P
;-----------------------------------------------------------           
        LD   E,8 		;30*0.45 sek piskaj OFF LINE
        LD   A,1                ;SPREMEMBA NA 8*0.45 Sekunde
        EX   AF,AF' 		;vektor vrnitve
        JR   KEYOFP
KEYRT1: LD   D,5 		;5* utripni displaj
KEYPI0: LD   A,0FH 		;bri{i display
        OUT  (P0),A
        LD   A,0FFH
        OUT  (P0),A
        LD   E,1 		;0.45sek piskaj OFF LINE
        LD   A,2
        EX   AF,AF'
        JR   KEYOFP
KEYRT2: LD   A,I
        BIT  2,A
        JR   Z,KEYIZ3 		;ne izpisi prve cifre
        DEFB X
        LD   A,L
        AND  0FH
        OR   10110000B
        OUT  (P0),A
        OR   0F0H
        OUT  (P0),A
KEYIZ3: DEFB Y
        LD   A,H
        RRA  
        RRA  
        RRA  
        RRA  
        AND  0FH
        OR   11010000B
        OUT  (P0),A
        OR   0F0H
        OUT  (P0),A
        DEFB Y
        LD   A,H
        AND  0FH
        OR   11100000B 
        OUT  (P0),A 
        OR   0F0H 
        OUT  (P0),A 
        LD   E,1
        LD   A,3 
        EX   AF,AF' 
        JR   KEYOFP 		;0.45sek cakaj 
KEYRT3: DEC  D 			; ponovno utripni,
        JR   NZ,KEYPI0		; ~e je potrebno
KEYPI1: LD   A,11111110B 	; testiranje kolone 1 4 7 *
        OUT (P0),A    
        IN   A,(P0) 
        AND  1000B 
        JR   NZ,BOUTST 
        LD   E,1 
        LD   A,4 
        EX   AF,AF' 
        JR   KEYOFP 
KEYRT4: JR   KEYPI1
 
BOUTST: LD   HL,49000;Ignoriraj bounce T tipke 
BOUTS1: IN   A,(P0) 		;max 1 sek
        AND  1000B
        JR   Z,KEYPI1
        DEC  HL
        LD   A,H
        OR   L
        JR   NZ,BOUTS1
        LD   A,10000111B ;Brisi PTT
        OUT  (P1),A
        JP   Z1 ;in se vrni v glavno zanko
 ;............................................
 
 ;Podprogram za piskanje OFF LINE
 ;pri KEY programu
KEYOFP: LD   C,150 ;ms tona
KEOFFT: LD   B,95
        LD   A,11100111B
        OUT  (P1),A
KOFFW1: DJNZ KOFFW1 ;500ms H tona
        LD   B,95
        LD   A,10100111B
        OUT  (P1),A
KOFFW2: DJNZ KOFFW2 ;isto le L ton
	LD   A,11111011B 	; testiranje kolone 1 4 7 #
        OUT (P0),A    		; V2.1
        IN   A,(P0) 
        AND  1000B 		 
        JR   NZ, KOFFNP		;~e je bila oddaja prekinjena
	OBRNIST			;prej obrni {tevilke
	JP   Z1			;in sko~i

KOFFNP: DEC  C
        JR   NZ,KEOFFT
        LD   BC,28846 ;300ms pavze
KOFFW3: DEC  BC
        LD   A,B
        OR   C
        JR   NZ,KOFFW3
        DEC  E
        JR   NZ,KEYOFP
        EX   AF,AF'
        CP   1
        JP   Z,KEYRT1
        CP   2
        JP   Z,KEYRT2 ;Vektorji vra'anja
        CP   3        ;v zanke
        JR   Z,KEYRT3
        JR   KEYRT4
 ;............................................
 
 OUTST:  LD   H,0 ;ta rutina je le za testiranje 
        LD   A,0FH ;izpisa B reg pri piskanju 
        OUT  (P0),A ;glej linijo 9970
        LD   A,0FFH
        OUT  (P0),A
        LD   A,B
        CP   10
        JR   C,TSTIZP
 TSTZ1:  INC  H
        SUB  10
        CP   10
        JR   NC,TSTZ1
 TSTIZP: OR   11100000B
        OUT  (P0),A
        OR   0F0H
        OUT  (P0),A
        LD   A,H
        OR   11010000B
        OUT  (P0),A
        OR   0F0H
        OUT  (P0),A
        LD   DE,0FFFFH
 TSTZ2:  DEC  DE
        LD   (0),A
        IN A,(P0)
        IN A,(P0)
        IN A,(P0)
        LD   A,D
        OR   E
        JR   NZ,TSTZ2
        JP   OUTRTN
 ;Tabele za'etkov digitaliziranja sinusov
 
 ZVEI0:  DEFB 0,39,31,0,39    ;2400 Hz
        DEFB 31,0,39,8,0
        DEFB 39,8,0,39,8
        DEFB 47,39,8,47,16
        DEFB 8,47,16,8,47
        DEFB 16,8,47,16
 ZVEI1:  DEFB 0,43,34,25,16   ;1060 Hz
        DEFB 7,51,42,33,24
        DEFB 15,6,49,40,31
        DEFB 22,14,4,48,39
        DEFB 30,21,12,3,46
        DEFB 37,28,19,10
 ZVEI2:  DEFB 0,9,18,27,36    ;1160 Hz
        DEFB 45,7,16,25,34
        DEFB 43,4,13,22,31
        DEFB 40,49,11,20,29
        DEFB 38,47,8,17,26
        DEFB 35,44,53,15
 ZVEI3:  DEFB 0,26,51,33,15  ;1270 Hz
        DEFB 41,23,5,30,12
        DEFB 38,20,2,27,53
        DEFB 35,17,43,25,50
        DEFB 32,14,40,22,47
        DEFB 29,11,37,19
 ZVEI4:  DEFB 0,42,44,7,9    ;1400 Hz
        DEFB 11,53,16,18,20
        DEFB 22,25,27,29,31
        DEFB 33,36,38,40,3
        DEFB 5,47,49,51,14
        DEFB 16,18,21,23
 ZVEI5:  DEFB 0,19,2,21,40   ;1530 Hz
        DEFB 23,42,25,44,27
        DEFB 46,28,11,30,13
        DEFB 32,15,34,53,36
        DEFB 19,38,21,40,22
        DEFB 5,24,7,26
 ZVEI6:  DEFB 0,1,2,3,4      ;1670 Hz
        DEFB 5,6,41,42,9
        DEFB 44,45,46,47,48
        DEFB 49,50,51,52,53
        DEFB 21,22,23,24,25
        DEFB 26,27,28,29
 ZVEI7:  DEFB 0,49,37,25,13  ;1830 Hz
        DEFB 32,20,8,27,45
        DEFB 33,52,40,28,16
        DEFB 4,53,11,29,48
        DEFB 36,24,12,31,19
        DEFB 7,25,44,32
 ZVEI8:  DEFB 0,34,40,19,25  ;2000 Hz
        DEFB 31,37,16,22,28
        DEFB 34,13,19,25,31
        DEFB 10,16,22,28,34
        DEFB 13,19,53,4,10
        DEFB 44,50,1,34
 ZVEI9:  DEFB 0,24,48,21,45  ;2200 Hz
        DEFB 44,17,41,14,38
        DEFB 37,10,34,7,6
        DEFB 30,3,27,51,24
        DEFB 23,47,20,44,43
        DEFB 16,40,13,12
 ZVEIR:  DEFB 0,51,38,25,12  ;2600 Hz
        DEFB 20,50,37,24,11
        DEFB 19,49,36,23,10
        DEFB 18,48,35,22,9
        DEFB 17,47,34,42,29
        DEFB 38,3,33,41
 CCIR0:  DEFB 0,4,37,13,17   ;1981 Hz
        DEFB 50,26,2,35,39
        DEFB 15,48,52,0,33
        DEFB 37,13,46,50,26
        DEFB 31,35,11,44,48
        DEFB 24,29,33,9
 CCIR1:  DEFB 0,3,6,9,12     ;1124 Hz
        DEFB 15,18,21,24,26
        DEFB 29,32,35,38,41
        DEFB 44,47,50,53,6
        DEFB 9,12,15,18,21
        DEFB 24,27,30,33
 CCIR2:  DEFB 0,15,30,45,14  ;1197 Hz
        DEFB 29,44,12,27,42
        DEFB 11,26,31,9,24
        DEFB 39,8,23,38,53
        DEFB 21,36,5,20,35
        DEFB 50,19,34,2
 CCIR3:  DEFB 0,26,9,35,18   ;1275 Hz
        DEFB 1,27,10,36,19
        DEFB 2,28,11,38,20
        DEFB 3,30,12,39,21
        DEFB 4,31,13,40,22
        DEFB 49,32,14,41
 CCIR4:  DEFB 0,37,33,29,25  ;1358 Hz
        DEFB 21,17,14,10,6
        DEFB 2,39,35,31,27
        DEFB 23,19,15,52,8
        DEFB 4,0,37,33,29
        DEFB 25,21,17,13
 CCIR5:  DEFB 0,47,17,26,34  ;1446 Hz
        DEFB 43,13,21,30,0
        DEFB 47,17,26,34,43
        DEFB 13,21,30,0,47
        DEFB 17,26,34,43,13
        DEFB 21,30,0,47
 CCIR6:  DEFB 0,20,41,25,45  ;1540 Hz
        DEFB 29,13,34,18,2
        DEFB 22,43,27,11,31
        DEFB 15,36,20,4,24
        DEFB 45,29,13,33,17
        DEFB 38,22,6,27
 CCIR7:  DEFB 0,31,29,26,24  ;1640 Hz
        DEFB 21,52,16,13,44
        DEFB 8,39,3,0,31
        DEFB 29,26,23,21,52
        DEFB 16,13,44,8,39
        DEFB 3,0,31,29
 CCIR8:  DEFB 0,10,20,30,39  ;1747 Hz
        DEFB 49,27,37,47,25
        DEFB 3,13,23,1,42
        DEFB 52,30,40,50,28
        DEFB 6,16,26,4,45
        DEFB 23,33,43,53
 CCIR9:  DEFB 0,51,13,5,26   ;1860 Hz
        DEFB 18,39,1,52,14
        DEFB 35,27,48,10,31
        DEFB 23,44,6,27,19
        DEFB 40,2,53,15,7
        DEFB 28,20,41,3
 CCIRR:  DEFB 0,43,6,49,39   ;2110 Hz
        DEFB 29,19,9,52,42
        DEFB 32,48,38,28,18
        DEFB 8,51,41,31,47
        DEFB 37,27,17,7,50
        DEFB 40,30,20,36
 
 CCIRG:  DEFB 0,39,31,0,39   ;2400 Hz
        DEFB 31,0,39,8,0    ;G(roup) ton
        DEFB 39,8,0,39,8
        DEFB 47,39,8,47,16
        DEFB 8,47,16,8,47
        DEFB 16,8,47,16
 
 ZVEIG:  DEFB 0,22,44,7,9    ;2800 Hz
        DEFB 31,53,35,18,40
        DEFB 42,44,7,29,51
        DEFB 14,16,38,40,3
        DEFB 44,27,49,12,14
        DEFB 36,38,1,3
 
 
	
	
;SYSTEM INFORMATION rutina za branje 
;konfiguracije sistema in izpis le teh, 'e je

 SYSKS:  ;potrebno   ;keyscan rutina
        LD   A,11111110B  ;row 1,4,7,*
        OUT  (P0),A
        IN   A,(P0)
        BIT  0,A
        JR   NZ,SYSKS1
        LD   D,1         ;1
 SYSKS1: BIT  1,A
        JR   NZ,SYSKS2
        LD   D,4         ;4
 SYSKS2: BIT  2,A
        JR   NZ,SYSKS3
        LD   D,7         ;7
 SYSKS3: LD   A,11111101B ;row 2,5,8,0
        OUT  (P0),A
        IN   A,(P0)
        BIT  0,A
        JR   NZ,SYSKS4
        LD   D,2         ;2
 SYSKS4: BIT  1,A
        JR   NZ,SYSKS5
        LD   D,5         ;5
 SYSKS5: LD   A,11111011B ;row 3,6,9,#
        OUT  (P0),A
        IN   A,(P0)
        BIT  0,A
        JR   NZ,SYSKS6
        LD   D,3         ;3
 SYSKS6: BIT  1,A
        JR   NZ,SYSKS7
        LD   D,6         ;6
 SYSKS7: BIT  3,A
        JR   NZ,SYSKS8
        LD   D,12        ;# == 12
 SYSKS8: LD   A,0F0H
        OUT  (P0),A
        IN   A,(P0)
        AND  0FH
        XOR  0FH
        JR   NZ,SYSKS   ;tipka pritisnjena
 
        LD   BC,3000 ;100ms pavze
 SYSKWA: DEC  BC
        LD   A,0F0H
        OUT  (P0),A
        IN   A,(P0)
        AND  0FH
        XOR  0FH
        JR   NZ,SYSKS
        LD   A,B
        OR   C
        JR   NZ,SYSKWA
        LD   A,01111111B  ;beri MODE
        OUT  (P0),A
        IN   A,(P0)
        LD   I,A
        LD   A,11101111B
        OUT  (P0),A
        IN   A,(P0)
        RLA  
        RLA  
        RLA  
        RLA  
        AND  0F0H
        LD   B,A         ;za'asno sHrani 
        LD   A,11011111B ;prvo cifro
        OUT  (P0),A
        IN   A,(P0)
        AND  0FH          ;2. cifra
        OR   B
        DEFB X           ; == LD XH,a
        LD   H,A
        LD   A,10111111B
        OUT  (P0),A
        IN   A,(P0)
        DEFB X           ;3. cifra
        LD   L,A
        LD   A,D
        CP   1
        JR   NZ,SYSKT2
        DEFB X
        LD   A,H
        RRA  
        RRA  
        RRA  
        RRA  
        JR   SYSCIF
 SYSKT2: CP   2           ;v A je `tevilka
        JR   NZ,SYSKT3   ;katero je treba
        DEFB X           ;izpisat in pojdi
        LD   A,H         ;na SYSCIF
        JR   SYSCIF
 SYSKT3: CP   3
        JR   NZ,SYSKT4
        DEFB X
        LD   A,L
 SYSCIF: AND  0FH          ;izpis `tevilke, ki
        CP   10          ;je v A registru
        JR   NC,DVOMES   ;`tev. je dvomestna
        OR   11100000B
        OUT  (P0),A
        OR   0F0H
        OUT  (P0),A
        LD   A,10011111B ;bri`i MSD in NSD
        OUT  (P0),A
        LD   A,0FFH
        OUT  (P0),A
        JP   SYSKS
 DVOMES: SUB  10         ;izpis dvomestne
        OR   11100000B  ;`tevilke (10..15)
        OUT  (P0),A
        OR   0F0H
        OUT  (P0),A
        LD   A,11010001B
        OUT  (P0),A
        OR   0F0H
        OUT  (P0),A
        LD   A,10111111B
        OUT  (P0),A
        OR   0F0H
        OUT  (P0),A
        JP   SYSKS
 SYSKT4: CP   4
        JR   NZ,SYSKT5
        LD   A,I         ;"1" == ZVEI
        AND  1           ;"0" == CCIR
        JR   SYSCIF
 SYSKT5: CP   5
        JR   NZ,SYSKT6
        LD   A,11000000B
        OUT  (P0),A
        LD   A,0F0H       ;"100" == 100ms
        OUT  (P0),A
        LD   A,I
        BIT  1,A
        JR   NZ,SYS70
        LD   A,10110001B
        OUT  (P0),A
        OR   0F0H
        OUT  (P0),A
        JP   SYSKS
 SYS70:  LD   A,11010000B+7
        OUT  (P0),A
        OR   0F0H         ;"70" == 70 ms
        OUT  (P0),A
        LD   A,10111111B
        OUT  (P0),A
        OR   0F0H
        OUT  (P0),A
        JP   SYSKS
 SYSKT6: CP   6
        JR   NZ,SYSKT7
        LD   A,I
        BIT  2,A         ;"2"
        LD   A,2
        JR   NZ,SYSCIF
        LD   A,3         ;ali "3" fiksirane
        JR   SYSCIF      ;cifre
 SYSKT7: CP   7
        JR   NZ,SYSKT8
        LD   A,0FH
        OUT  (P0),A      ;"00" == G ton
        LD   A,0FFH       ;"0"  == 0 ton
        OUT  (P0),A
        LD   A,11100000B
        OUT  (P0),A
        LD   A,0F0H
        OUT  (P0),A
        LD   A,I
        BIT  3,A
        JP   NZ,SYSKS
        LD   A,11010000B
        OUT  (P0),A
        LD   A,0F0H
        OUT  (P0),A
        JP   SYSKS
 SYSKT8: CP   12        ;'e je bila pritisnjena
        JR   NZ,SYSKT9 ;tipka # potem za'ni od
        JP   0000H     ;za'etka
 SYSKT9: LD   A,R
        DAA  
        AND  0FH
        OR   11010000b
        OUT  (P0),A
        OR   0F0H
        OUT  (P0),A
        LD   A,10101111B
        OUT  (P0),A
        LD   A,0FFH
        OUT  (P0),A
 ;...........................................
 ;Citanje MODE in fiksiranih cifer
 SYSINI: LD   A,01111111B
        OUT  (P0),A
        IN   A,(P0)
        LD   I,A
 
        LD   A,11101111B
        OUT  (P0),A
        IN   A,(P0)
        AND  0FH
        LD   D,A
        RLA  
        RLA  
        RLA  
        RLA  
        AND  0F0H
        LD   B,A
        LD   A,11011111B
        OUT  (P0),A
        IN   A,(P0)
        AND  1111B
        LD   E,A
        OR   B
        DEFB X
        LD   H,A
 
        LD   A,10111111B
        OUT  (P0),A
        IN   A,(P0)
        AND  0FH
        LD   C,A
        DEFB X
        LD   L,A
 ;kontrola ali so fiksirane cifre v oobmo'ju
        LD   A,9   ; od 0 do 9
        CP   D
        JR   NC,SYSIN1
        LD   B,1   ;'e ne pojdi na izpis
        JR   SYSERR    ;napake
 SYSIN1: CP   E
        JR   NC,SYSIN2
        LD   B,2
        JR   SYSERR
 SYSIN2: CP   C
        JR   NC,SYSIN3
        LD   B,3
        JR   SYSERR
 SYSIN3: JP   INIT     ;vse fix. cifre so OK
 
 SYSERR: LD   A,B       ;utripajo' izpis `t.,
        OR   11100000B ;ki je napa'no 
        OUT  (P0),A    ;postavljena z DIP
        OR   0F0H       ;stikali
        OUT  (P0),A
        LD   A,10010000B
        OUT  (P0),A
        OR   0F0H
        OUT  (P0),A
        LD   DE,15000
 SYSERW: LD   A,0F0H
        OUT  (P0),A
        IN   A,(P0)
        AND  0FH
        XOR  0FH
        JP   NZ,SYSKS
        DEC  DE
        LD   A,D
        OR   E
        JR   NZ,SYSERW
        LD   A,0FH
        OUT  (P0),A
        LD   A,0FFH
        OUT  (P0),A
 
 
        LD   DE,18000
SYSEW2: DEC  DE
        LD   A,D
        OR   E
        JR   NZ,SYSEW2
        JR   SYSEW2
        JR   SYSERR



GRKLIC:	LD  A,I
	BIT 2,A
	JP Z,GRKLIC2
	MOV X A,L
	AND  0FH	;brisi zgornje bite
	CP   0FH	;ali je R3=0FH
	JP Z,KS12	;ce je potem se vrni
;	CP 0		;ali je R3=0
;	JP Z,KS12
GRKSWR:	MOV X A,L	;swap R3 in R3'
	SWAP
	MOVXL A
	MOV Y A,L	;swap R4,R4' in R5,R5'
	MOV Y E,H
	MOV Y H,A
	MOV Y L,E
	DISPL345
GRK3C:	LD E,10	;300ms count stevec za #tipko
GRK3C0:	LD B,247	;10ms je en obhod
GRK3C1:	LD A,11111110B	; 7T row 1,4,7,*
	OUT  (P0),A	;11T
	IN   A,(P0)	;11T
	nop
 	BIT 3,A		; 8T
        JR Z,GRK3TST	; 7T * je pritisnjena
	LD A,11111011B	; 7T row 3,6,9,#
        OUT  (P0),A	;11T
	nop
        IN   A,(P0)	;11T
	BIT 3,A		; 8T
	JR Z,GRK3C	; 7T # je pritisnjena
	DJNZ GRK3C1	;13T SUM=101T=40.4æS
	DEC E
	JR NZ,GRK3C0
	ld a, 0FFh	;v2.1
	out (P0), a
	JP KS12
GRK3TST:MOV X A,L
	AND 0FH
        CP 0FH		;ali je R3=0Fh
	JR Z,GRK3C1

;testiranje in vstavljanje enic v R3, R4, R5 ce
;je to potrebno

	MOV X A,L
	AND 0FH
	JR NZ,GRKIZ0
	MOV X A,L
	INC A		;povecamo R3 za 1
	MOVXL A
GRKIZ0:	MOV Y A,H
	AND 0F0H
	JR NZ,GRKIZ1
	MOV Y A,H
	OR 10H		;R4<-1
	MOVYH A
GRKIZ1:	MOV Y A,H
	AND 0FH
	JR NZ,GRKIZ2
	MOV Y A,H
        INC A
	MOVYH A		;povecamo R5 za 1

;Testiranje ali je potrebno vstaviti enice v
; R3', R4'in R5'
GRKIZ2:	MOV X A,L
	AND 0F0H
	JR NZ,GRKIZ3
	MOV X A,L
	OR 10H		; R3'<- 1
	MOVXL A
GRKIZ3:	MOV Y A,L
	AND 0FH
	JR NZ,GRKIZ4
	MOV Y A,L
	OR 1		;R5'<- 1
	MOVYL A
GRKIZ4:	MOV Y A,L
	AND 0F0H
	JR NZ,GRK3S1
	MOV Y A,L
	OR 10H		;R4'<- 1
	MOVYL A
;Rutina, ki po potrebi zamenja R345 in R3'4'5'
;tako, da je v R345 manjsa stevilka
GRK3S1:	MOV X A,L
	AND 0FH
	LD E,A		;R3 je v E
        MOV X A,L
	SWAP
	AND 0FH		;R3' je v A
	CP E		;F <- R3'-R3
	JP M,GRK3SW	;SWAP   ce je R3>R3'
        JR NZ,GRK3PS	;NOSWAP ce je R3ØR3'
	MOV Y A,H	;E <- R4
        SWAP
	AND 0FH
	LD E,A
	MOV Y A,L
	SWAP
	AND 0FH		;A <- R4'
	CP E
	JP M,GRK3SW	;SWAP ce je R4>R4'
	JR NZ,GRK3PS	;NOSWAP ce je R4=<R4'
	MOV Y A,H	;A <- R5
	AND 0FH
	LD E,A
	MOV Y A,L
	AND 0FH		;A <- R5'
	CP E		;F <- R5'-R5
	JP M,GRK3SW	;SWAP ce je R5>R5'
	JR GRK3PS	;NOSWAP ce je R5=<R5'

GRK3SW:	MOV X A,L	;swap R3 in R3'
	SWAP
	MOVXL A
	MOV Y A,L	;swap R4,R4' in R5,R5'
	MOV Y E,H
	MOV Y H,A
	MOV Y L,E
GRK3PS:	LD A,10111000B	;prizgi TX in PG
	OUT (P1),A
	LD A,I
	SET 6,A		;nastvi vektor za GRPISK
	LD I,A
	LD E,210	;cakaj 210ms
GRK3PS0:LD B,103	;zakasnitev 1ms
GRK3PS1:IN A,(P0)
        DJNZ GRK3PS1	;ena zanka je 9.6æs
	DEC E
	JR NZ,GRK3PS0


GRKPI:	

;rutina, ki avtomatsko vstavlja Group tone ce je
;to le mozno

	MOV X A,L
	AND 0FH
	LD H,A		;H:=R3
	MOV Y A,H
	SWAP
	AND 0FH
	LD L,A		;L:=R4
	MOV Y A,H
	AND 0FH
	LD D,A		;D:=R5
	LD A,L		;A:=R4
	CP 0		;R4=0
	JR NZ,GRKCR5
	INC H 		;R3:=R3+1
	LD L,1		;R4:=1
	LD D,1		;R5:=1
	JR GRKCR3
GRKCR5:	LD A,D		;A:=R5
	CP 0		;R5=0
	JR NZ,GRKC51
	INC L		;R4:=R4+1
	LD D,1		;R5:=1
	LD A,L
	CP 10
	JR NZ,GRKC41
	LD L,1
	INC H
	JR GRKCR3
GRKC51:	CP 1		;R5=1
	JR NZ,GRKMPI
GRKC41:	LD A,L		;A:=R4
	CP 1		;R4=1
	JR NZ,GRKCR4
GRKCR3:	MOV X A,L
	SWAP
	AND 0FH    	;A:=R3'
	CP H		;F:=R3'-R3
	JR Z,GRKC49	;R3'=R3
GRKC3P:	LD L,0		;R4:=0
	LD D,0		;R5:=0
	JR GRKMPI
GRKC49:	MOV Y A,L
	CP 99H		;(R4'=9)AND(R5'=9)=>F:=Z
	JR Z,GRKC3P

GRKCR4:	MOV X A,L
	SWAP
	AND 0FH		;A:=R3'
	CP H		;F:=R3'-R3
	JR NZ,GRKCI5
	MOV Y A,L
        SWAP
	AND 0FH		;A:=R4'
	CP L		;F:=R4'-R4
	JR NZ,GRKCI5
	MOV Y A,L
	AND 0FH		;A:=R5'
	CP 9
	JR Z,GRKCI5
	JR GRKMPI
GRKCI5:	LD D,0

GRKMPI:	;Rutina za klic programa za Piskanje
	MOV X A,L
	AND 0F0H
	OR H
	MOVXL A		;najprej shranimo R3
	LD A,L
	SWAP
	OR D
	MOVYH A		;nato se R4 in R5




	DISPL345
;piskaj ton ************************************
	JP GRPISK	;klisi rutino za
GRPRTN:			;piskanje 	
        
        
        LD E,98		;cakaj 100ms  e=100
GRK3WA0:LD B,103	;zakasnitev 1ms  b=103
GRK3WA1:IN A,(P0)
        DJNZ GRK3WA1	;ena zanka je 9.6æs
        DEC E
	JR NZ,GRK3WA0

	MOV Y E,H	;primerjaj med sabo R4,5
	MOV Y A,L	;in R4'R5'
	CP E		;ce niso enaki pojdi na
	JR NZ,GRKINC	;povecanje R345
	MOV X A,L	;primerjaj se R3
        LD E,A
	SWAP
	XOR E		;ce je R3=R3'->> A=0
	JP Z,LASTWAIT	;if A=0 pojdi v Z1
GRKINC:	;povecanje R345 za 1 in nadaljevanje
	;Grupnega klica
	MOV Y A,H	;A:=R5
	AND 0FH 	;ce je R5=0 potem ne
	JP Z,GR5TST	;povecaj ampak skoci

	MOV Y A,H
	AND 0F0H
	LD E,A		;zacasno shrani R4
	MOV Y A,H
        AND 0FH
	INC A		;povecaj R5
	CP 10		;ali je R5=10
	JR Z,GRKINC1	;if R5=10 JR
	OR E		;sicer shrani R4 in R5
	MOVYH A
	JP GRKPI	;in nadaljuj s piskanjem
GRKINC1:LD A,E		;poklici R4
	SWAP
	INC A		;povecaj R4
	CP 10		;ali je R4=10
	JR Z,GRKINC2	;if R4=10 JR
	SWAP
	OR 1		;R5=1
	MOVYH A		;shrani R4inR5
	JP GRKPI	;in nadljuj s piskanjem
GRKINC2:MOVYH 11H	;brisi R4inR5 R4=1,R5=1
	MOV X A,L	;povecaj se R3
	AND 0F0h
	LD E,A
	MOV X A,L
	AND 0FH
	INC A
	OR E
	MOVXL A
	JP GRKPI
GR5TST:	;Testiramo ali smo prisli do konca z
	;piskanjem G tona v R5
	MOV X A,L	;A:=R3'R3
	LD E,A		;E:=A
	SWAP		;A:=R3R3'
	XOR E		;primerjaj R3' in R3
	JP NZ,GRKPI	;JP ce nista enaka
	MOV Y A,H
	AND 0F0H
	LD E,A		;Eh:=R4
	MOV Y A,L
	AND 0F0H	;Ah:=R4'
	CP E		;F:=R4'-R4
	JR Z,LASTWAIT	;JP ce sta enaka
	LD A,E		;A:=R4
	CP 0
	JP NZ,GRKPI
	JR LASTWAIT


;Rutina Skupinskega klica za 3 fiksirane cifre.
;glej Labelo GRKLIC

GRKLIC2:
	MOV Y A,H
	AND 0F0H
	CP 0F0H		;ali je R4=0Fh
        JP Z,KS12
	MOV X A,L
	AND 0FH		;A:=R3
	LD E,A		;E:=R3
	SWAP
	OR E		;A:=R3R3
	MOVXL A
	JP GRKSWR


LASTWAIT: ;5 sek cakanje na zadnjega ki smo ga
	; poklicali s skupinskim klicem
GLWPI	MACRO FREQ, WTAB, WATIME
	LOCAL GLWC0,GLWC1	
	LD HL,FREQ
	LD D,WATIME
	LD B,WTAB+TAB
	LD   C,0	;zacni na nicti poziciji tabele
GLWC0:	LD   E,200	;generiraj 200 bytov tabele
GLWC1:  LD   A,(BC)
        OR   10110000B
        OUT  (P1),A      ;glavna zanka za
        INC  C           ;digitaliziraje
        DEC  E           ;sinusov
        JR   NZ,GLWC1
        INC  HL
        LD   C,(HL)
        DEC  D
        JP   NZ,GLWC0
	ENDM
        
	EXX
        LD B,5	;Cakaj 5 sekund
	EXX
GLW:
	WAIT 110
	WAIT 200               
        GLWPI CCIR4,15,20 ;1358Hz,70ms
	WAIT 200
	GLWPI CCIR4,15,20
	WAIT 250
	GLWPI CCIR1,12,28 ;1124Hz,100ms
	EXX
	DEC B
	EXX
	JP NZ,GLW
	LD A,I
	RES 6,A		;brisi vektor za
	LD I,A		;skupinski klic
	JP KEYRTN
















.COMMENT @

 IZPISI:DEFB X         ;program je rabljen le pri testiranju
 	LD A,L		;izpise vrednost registra B  na display
	SWAP
	AND 0FH
	LD B,A
	defb y
	ld a,l
	SWAP
	and 0fH
	LD B,A
	LD   H,0 ;ta rutina je le za testiranje 
        LD   A,0FH ;
        OUT  (P0),A ;glej linijo 9970 
        LD   A,0FFH
        OUT  (P0),A 
        LD   A,B 
        CP   10 
        JR   C,IZPIZP 
IZPISZ1:INC  H
        SUB  10 
        CP   10 
        JR   NC,IZPISZ1
IZPIZP: OR   11100000B 
	OUT (P0),A 
        OR   0F0H 
        OUT  (P0),A 
        LD   A,H 
        OR   11010000B 
        OUT (P0),A 
        OR   0F0H 
        OUT  (P0),A
        OUT (P1),A
        
	JR IZPISI
@

	DEFM "MACRO-80 RTL END"
 END