;
;  KOMO 3705 von SCI-KUHN1082-AUDA1.bin
;
; IO-Adr.   1x U 855 (PIO), 1x U857 (CTC)
; 078H:   PIOx-C  INIT mit 0FFH=MODE 3 Bit-EA
; 079H:   PIOx-C                            
; 07AH:   PIOy-C  INIT mit 0FFH=
; 07BH:   PIOy-C  INIT mit 00FH=
;
; CTC
; 0ECH: INIT mit 0A0H=IR-Vektor; 
; 0ECH: 0A7H=IR, Zeitgeber, VT=16, ZK folgt, reset; ZK=0FFH  (in SP005:)
;      spÃ¤ter: 0A7H und ZK 0C8H  (ISRC0:)
;    in I2C35: 0ACH dann EI und RETI
;
; 0EDH: 087H=IR, Zeitgeber, VT=16, ZK folgt, reset; ZK=04DH (in0093H ISRC1)
; 0EEH: 087H=IR, Zeitgeber, VT=16, ZK folgt, reset; ZK=09AH
; 0EFH: 0A5H=IR, Zeitgeber, VT=256, ZK folgt; ZK=000H
;
; RAM-Zellen 
; 0800H  A
; 0801H + 0802H  HL, BC
; 0804H  A
; 0804H  A  in Schleife LD (0804H),07H
; 0805H  A
; 0807H (HL)
; 0807H  A
; 0808H  A
; 0809H  A
; 080AH  A
;
; LD SP,0810H
;
; EPROM-ZELLEN
; 03EDH=C8H
;
;
        .ORG 0000H
        JR SP001
;
;
;  Funktion?
        .ORG 0002H
LXX1:   .DB 04BH        ;LD C,E
        .DB 055H        ;LD D,L
        .DB 048H        ;LD C,B
        .DB 04EH        ;LD C,(HL)
        .DB 031H        ;LD SP,3830H
        .DB 030H
        .DB 038H
        .DB 032H        ;LD (4649H),A
;
        .ORG 000AH
        .DB 049H
        .DB 046H
        .DB 052H        ;LD D,D
;
;
; Anfangs-INIT  von 0000H
        .ORG 000DH
SP001:  IM 2
        XOR A
        LD HL,080FH
        LD B,0CH
SP002:  LD (HL),A       ; RAM-INIT
        DEC HL
        DJNZ SP002
        LD A,0FFH       ; HL=0803H
        LD (HL),A
        DEC HL          ; HL=0802H
;
;
; Schleife
;       LD B,0FFH       ; war original
        LD B,001H       ; nur für TEST
SP003:  DJNZ SP003      ; Zeitschleife
        LD B,03H
SP004:  LD (HL),A
        DEC HL
        DJNZ SP004
        LD D,00H        ; HL=0800H
        LD A,07H
        LD (0804H),A
        LD E,00H
        LD SP,0810H
        LD A,03H
        LD I,A
;
;
;
; INIT PIO ?
        LD C,7AH        ; PIO A?
        LD A,0FFH
        OUT (C),A
        LD A,78H        ; PIO B?
        OUT (C),A
        LD C,7BH        ; PIO B?
        LD A,0FH
        OUT (C),A       ; PIO B BYTE-Eingabe
; INIT CTC
        LD C,0ECH       ; CTC-K0?
        LD A,0A0H       ; CTC IR-Vektor?  03A0H ist programm
        OUT (C),A
        LD C,0EEH       ; CTC-K2?
        LD A,87H
        OUT (C),A       ; K2 IR, Zeitgeber, VT=16, ZK folgt
        LD A,9AH
        OUT (C),A       ; ZK
        LD C,0EFH       ; CTC-K3?
        LD A,0A5H
        OUT (C),A       ; K2 IR, Zeitgeber, VT=256, ZK folgt
        XOR A
        OUT (C),A
        EI
        NOP
        JP SP005        ; Sprung 0360H mit Ausgabe und dann JR SP009 (Schleife)
;
;
; 
;  Funktion?
        .ORG 065H
        RST 38H
;
;
; ======================================  
        .ORG 066H
NMI:    NOP
        RETN            ; ED 45
;
;
;   Schleife
        .ORG 0069H
SP009:  NOP
        DI              ; bitte nicht stÃ¶ren
        LD A,(0809H)
        AND 07H
        JR NZ,SP006     ; hier keine Aktion da JR SP009
        LD A,(0803H)
        CP 51H
        JP Z,SP007
        CP 65H
        CALL Z,SP008
SP006:  NOP
SP053:  EI              ; kommt von SP052
        JR SP009        ; Schleife
;
;
; ===============================================
;  ISR CTC-K0
        .ORG 0083H
ISRC0:  EX AF,AF'       ; 
        EXX             ; 
        LD A,0A7H       ; INIT CTC-K0
        OUT (0ECH),A
        LD A,(03EDH)    ; .DB 0C8H
        OUT (0ECH),A
        EX AF,AF'
        EXX
        EI
        RETI
;
;
; =================================================
;  ISR CTC-K1
        .ORG 0093H
ISRC1:  LD A,D
        INC A
        AND 07H
        LD B,A
        LD A,D
        AND 08H
        ADD A,B
        LD D,A
        CALL I1C10
        LD C,0EDH       ; CTC-K2 INIT
        LD A,87H
        OUT (C),A
        LD A,4DH
        OUT (C),A
        EI
        RETI
;
;
; Teil ISR CTC-K1 CALL
        .ORG 00ADH
I1C10:  XOR A
        OUT (79H),A         ; PIO-?
        LD C,78H            ; PIO-?
        LD A,D
        AND 07H
        ADD A,80H
        OUT (C),A
        LD A,D
        RRCA
        AND 03H
        LD HL,0800H
        ADD A,L
        LD L,A
        LD B,(HL)
        LD A,D
        RRCA
        JR C,I1C11      ; 4x RRC B, dann JR I1C20
;
I1C20:  LD A,0FH
        AND B
        LD B,A
        INC B
        LD H,B
        LD A,D
        AND 07H
        LD B,A
        INC B
        LD A,01H
        RRA
I1C12:  RLA
        DJNZ I1C12
        LD B,H
        LD L,A
        LD A,(080AH)
        AND L
        JR NZ,I1C13
        LD A,00H
        JR I1C14
;
;
; Teil ISR CTC-K1
I1C13:  LD A,D
        AND 07H
        SUB 06H
        JR NC,I1C15
        LD A,(0809H)
        AND 07H
        CP 04H
        JR NZ,I1C15
        LD A,B
        SUB 10H
        JR NZ,I1C16
        LD HL,03ECH         ;LD HL,03ECH, (HL)=40H
        JR I1C17
;
;
; Teil ISR CTC-K1       
I1C15:  LD A,(0808H)
        AND L
        LD HL,03DBH
        JR NZ,I1C18
I1C16:  LD HL,03CBH
I1C18:  INC HL
        DJNZ I1C18
I1C17:  LD A,(0809H)        ;von I1C13 mit LD HL,03ECH, (HL)=40H
        AND 07H
        SUB 04H
        JP Z,I1C19
I1C21:  LD A,(0807H)
        AND A
;
;
; 
        LD A,(HL)           
        JR Z,I1C14
        LD A,(0804H)
        AND 08H
        LD A,(HL)
        JR Z,I1C14
        LD A,D
        AND 07H
        LD B,A
        LD A,(0804H)
        AND 07H
        SUB B
        LD A,(HL)
        JR NZ,I1C14
        LD A,80H
        ADD A,(HL)
I1C14:  LD C,A
        IN A,(78H)
        LD A,C
        OUT (79H),A
        RET                 ; kein RETI, da CALL in ISRC1:
;
;
; Teil ISR CTC-K1
I1C11:  RRC B
        RRC B
        RRC B
        RRC B
        JR I1C20
;
;
; Teil ISR CTC-K1
I1C19:  LD A,(0804H)        ;vorher LD HL,03ECH, (HL)=40H
        AND 80H
        JP Z,I1C21
        LD A,D
        AND 07H
        LD B,A
        LD A,(0804H)
        AND 70H
        RRCA
        RRCA
        RRCA
        RRCA
        SUB B
        JP NZ,I1C21
        LD A,80H
        ADD A,(HL)
        JP I1C14
;
;
; =================================
; ISR CTC-K2
        .ORG 0166H
ISRC2:  LD A,03H
        OUT (0EDH),A        ; CTC-K2 reset
        BIT 3,D
        JR NZ,I2C22
        IN A,(78H)
        LD B,A
        XOR A
        SLA B
        SLA B
        SLA B
        ADC A,00H
        SLA B
        ADC A,00H
        SLA B
        ADC A,00H
        CP 01H
        JR Z,I2C23
        EI
        RETI
;
;
; Teil ISR CTC-K2
I2C23:  LD E,10H
        IN A,(78H)
        AND 38H
        LD HL,0805H
        LD (HL),A
        LD A,D
        AND 07H
        ADD A,(HL)
        LD (HL),A
        SET 3,D
        EI
        RETI
;
;
; Teil ISR CTC-K2
I2C25:  IN A,(78H)
        AND 38H
        JR Z,I2C24    ; EI und RETI
        RES 3,D
I2C24:  EI
        RETI
;
;
; Teil ISR CTC-K2
I2C22:  DEC E
        LD A,08H
        CP E
        JR C,I2C25
        RES 3,D
        LD E,10H
        LD HL,0805H
        JP I2C26
;
;
; Teil ISR CTC-K2
I2C36:  LD A,(HL)
        CP 0AH
        JR Z,I2C27
I2C40:  LD HL,0805H
        LD A,(HL)
        CP 16H
        JP Z,I2C28
        CP 09H
        JR Z,I2C29
        CP 08H
        JR Z,I2C30
I2C42:  LD IX,03A6H
        XOR A
        LD B,A
        LD HL,0805H
        LD C,(HL)
        ADD IX,BC
        DEC HL
        LD A,(HL)
        AND 07H
        SRL A
        INC A
        LD B,A
        JR C,I2C31
        LD HL,07FFH
I2C32:  INC HL
        DJNZ I2C32
        LD A,(HL)
        AND 0F0H
        ADD A,(IX+00H)
        LD (HL),A
        JR I2C29
;
;
; Teil ISR CTC-K2
I2C31:  LD HL,07FFH
I2C33:  INC HL
        DJNZ I2C33
        LD A,(HL)
        AND 0FH
        LD B,A
        LD A,(IX+00H)
        RLCA
        RLCA
        RLCA
        RLCA
        ADD A,B
        LD (HL),A
I2C29:  LD A,(0804H)
        AND 07H
        DEC A
I2C34:  AND 07H
        LD B,A
        LD A,(0804H)
        AND 0F8H
        ADD A,B
        LD (0804H),A
I2C35:  LD A,0ACH           ; INIT-Vektor  CTC-K0 ==> 03ACH
        OUT (0ECH),A
        EI
        RETI
;
;
; Teil ISR CTC-K2
I2C30:  LD A,(0804H)
        AND 07H
        INC A
        JR I2C34
;
;
; Teil ISR CTC-K2
I2C27:  LD BC,(0801H)
        INC BC
        LD (0801H),BC
        JR I2C35
;
;
; Teil ISR CTC-K2
; in I2C40:  LD HL,0805H; LD A,(HL); CP 16H; JP Z,I2C28
; in I2C47:  LD HL,0805H; LD A,(HL); ... ; CP 16H; JP Z,I2C28   
I2C28:  OUT (0DFH),A        ; CTC-K3
        JR I2C35
;
;
; Teil ISR CTC-K2
I2C26:  LD A,(0809H)
        AND 07H
        JP Z,I2C36
        LD C,A
        LD A,(0807H)
        AND A
        JP Z,I2C37
        LD A,(0804H)
        AND 07H
        LD B,A
        INC B
        LD A,01H
        RRA
I2C38:  RLA
        DJNZ I2C38
        LD HL,0807H
        AND (HL)
        JP Z,I2C35
I2C47:  LD HL,0805H
        DEC C
        JP Z,I2C36
        LD A,(HL)
        CP 0AH
        JR Z,I2C39
        DEC C
        JP Z,I2C40
        CP 16H
        JP Z,I2C28
        CP 11H
        JR Z,I2C41
        DEC C
        JR Z,I2C35
        CP 12H
        JP NC,I2C42
        CP 09H
        JP Z,I2C29
        CP 08H
        JR Z,I2C30
        DEC C
        JR Z,I2C43
        JR I2C35
;
;
; Teil ISR CTC-K2
I2C39:  LD B,40H
        JR I2C44
;
;
; Teil ISR CTC-K2
I2C41:  LD B,80H
I2C44:  LD HL,0809H
        LD A,(HL)
        AND 3FH
        ADD A,B
        LD (HL),A
        JP I2C35
;
;
; Teil ISR CTC-K2
I2C43:  LD A,(HL)
        CP 0DH
        JR NZ,I2C45
        JP I2C42
;
;
; Teil ISR CTC-K2
I2C45:  CP 0CH
        JR NZ,I2C46
        JP I2C42
;
;
; Teil ISR CTC-K2
I2C46:  CP 0BH
        JP NZ,I2C35
        LD HL,0804H
        LD A,(HL)
        AND 07H
        ADD A,09H
        RLCA
        RLCA
        RLCA
        RLCA
        LD B,A
        LD A,(HL)
        AND 0FH
        ADD A,B
        LD (HL),A
        JP I2C35
;
;
; Teil ISR CTC-K2
I2C37:  LD A,C
        CP 03H
        JP NZ,I2C35
        JP I2C47
;
;
; ==================================
; ISR   Funktion???
        .ORG 02CCH      
ISRX:   LD A,03H            ; Sprungtabelle auf 03AAH
        OUT (0EDH),A        ; CTC-K2 reset
        IN A,(78H)
        AND 38H
        JR Z,ISX48
        LD E,10H            ; ?
        EI
        RETI
;
;
; Teil ISR ISRX
ISX48:  LD A,E
        AND E
        JR Z,ISX49
        DEC E
        EI
        RETI
;
;
; Teil ISR ISRX
ISX49:  RES 3,D
        LD A,0A4H
        OUT (0ECH),A
        EI
        RETI
;
;
; ==================================
;  ISR CTC-K3
ISRC3:  LD HL,0806H
        INC (HL)
        LD A,14H
        CP (HL)
        JR C,I3C50
        LD A,0AH
        CP (HL)
        JR NC,I3C51
        DEC HL
        DEC HL
        SET 3,(HL)
I3C51:  EI
        RETI
;
;
; Teil ISR CTC-K3
I3C50:  LD (HL),00H
        DEC HL
        DEC HL
        RES 3,(HL)
        EI
        RETI
;
;
;  vorher: LD A,(0803H);  CP 51H    ;  JP Z,SP007 von 007CH
SP007:  XOR A
        OUT (79H),A
        OUT (78H),A
;
SP052:  LD A,(0800H)
        LD HL,(0801H)
        LD (HL),A
        LD A,(0803H)
        CP 51H
        JR Z,SP052      ; Schleife
        JP SP053        ; dann JR SP009 (Schleife) nach 0080H
;
;
; vorher: LD A,(0803H);  CP 65H    ;  CALL Z,SP008
SP008:  LD HL,(0801H)
        LD BC,0400H
        ADD HL,BC
        LD A,(HL)
        LD HL,0800H
        LD (HL),A
        RET
;
;
;
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
;
;
;  Sprung von 0062H
        .ORG 0360H
SP005:  LD A,0A7H
        OUT (0ECH),A    ; CTC-K0: IR,Zeitgeber, VT=16, ZK folgt, reset
        LD A,0FFH
        OUT (0ECH),A
        JP SP009        ; Schleife mit Abfrage (0803H) und (0809H)
;
;
;
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
;
;
; ====================================
; ISR-Tabelle von CTC
; CTC-Tabelle beim Start
        .ORG 03A0H
IS-C0:  .DW 0083H           ; ISR CTC-K0
        .ORG 03A2H
IS-C1:  .DW 0166H           ; ISR CTC-K1
;
; CTC-Tabelle durch ISR                       ++++++++
        .ORG 03A4H
IS-C2:  .DW 0093H           ; ISR CTC-K2 oder ISR-CTC0
        .ORG 03A6H
IS-C3:  .DW 02ECH           ; ISR CTC-K3 oder ISR-CTC1
;
;
; 
        .ORG 03A8H
        .DW 0083H           ;            oder ISR-CTC2
;
        .ORG 03AAH
IS-RX:  .DW 02CCH           ;       ISRX  als ISR-CTC3
;
;
; neuer Vektor für CTC
        .ORG 03ACH
        .DW 0093H           ;                              neue ISR CTC-K0
;
        .ORG 03AEH
        .DW 02ECH           ;                              neue ISR CTC-K1
;
        .ORG 03B0H
        .DB 0FH
        .DB 0DH
        .DB 0EH
        .DB 0FH
;
        .ORG 03B4H
        .DB 0AH         ;LD A,(BC)
        .DB 00H         ;NOP
        .DB 0BH         ;DEC BC
        .DB 0CH         ;INC C
        .DB 07H         ;RLCA
        .DB 08H         ;EX AF,AF'
        .DB 09H         ;ADD HL,BC
        .DB 04H         ;INC B
        NOP
        NOP
        NOP
        NOP
        NOP
        NOP
        NOP
        NOP
        NOP
        NOP
;
;
        .ORG 03C6H
        .DB 05H         ;DEC B
        .DB 06H         ;LD B,01H
        .DB 01H
        .DB 02H         ;LD (BC),A
        .DB 03H         ;INC BC
;
;
; Tabelle
        .ORG 03CBH
        .DB 00H         ;NOP
        .DB 3FH         ;CCF
        .DB 06H         ;LD B,5BH
        .DB 5BH
;
        .ORG 03CFH
        .DB 4FH         ;LD C,A
        .DB 66H         ;LD H,(HL)
        .DB 6DH         ;LD L,L
        .DB 7DH         ;LD A,L
        .DB 07H         ;RLCA
        .DB 7FH         ;LD A,A
        .DB 6FH         ;LD L,A
        .DB 77H         ;LD (HL),A
        .DB 7CH         ;LD A,H
;
        .ORG 03D8H
        .DB 39H         ;ADD HL,SP
        .DB 5EH         ;LD E,(HL)
        .DB 79H         ;LD A,C
        .DB 71H         ;LD (HL),C
        .DB 5BH         ;LD E,E
        .DB 6DH         ;LD L,L
        .DB 07H         ;RLCA
        .DB 3EH         ;LD A,37H
        .DB 37H
;
        .ORG 03E1H
        .DB 73H         ;LD (HL),E
        LD D,B
        LD A,L
        LD B,76H
        LD (HL),A
        LD A,H
        ADD HL,SP
        LD E,(HL)
        LD A,C
        LD (HL),C
;
;
;
        .ORG 03ECH
        .DB 40H         ; 
;
        .ORG 03EDH
        .DB 0C8H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H
        RST 38H


