;Based on wav2ay player so
; sample player needs 317 tstates per sample
; with  3.5 Mhz thats 317/3500000 second per sample , 11,041.009463722397476340694006309 = 11.041 khz

; but this player uses 319 tstate per sample!! , tiny bit slower!!

;AY has 3 channels.

;on 48k spec it would have a max of 3x10 kb, not much, but on 128k spec that would be 3x 16k at least and 3x 32k max
;bank 0+1 basic routines + control mc : up to adres 32767
;bank 2+3
;bank 4+5
;bank 6+7, by which bank 7 holding extra data (RAMdrive+screen) would be wasted

;this player uses 3 separate code blocks 
; LD a,(hl) = 0x7e  %01111110
; LD a,(bc) = 0x0a  %00001010
; LD a,(de) = 0x1a  %00011010

;every channel uses 2 adresses as pointers by IX

; its a 2 samples in 1 bytes format >> %xxxx + %yyyy = %1111.1111

argh  equ dodone-A_here

 	org 32768-2444

A_here    jp MainLoop
A_leng  equ z_the -A_here
	
SAMPLE3:  ; seketoot, made with wav2ay
; 1027 bytes now follow:
	DEFW $0321,$6146,$4045,$0000,$4333,$6634,$7787,$AB98,$0110,$0400,$4300,$7690,$1100,$C020,$BB16,$02EA
	DEFW $61CB,$BC24,$401C,$BC43,$910B,$2632,$4910,$CC2A,$49E2,$4890,$8683,$8945,$3B18,$5098,$5790,$E71C
	DEFW $1C55,$28CB,$CA55,$CC92,$50CB,$D193,$51CA,$A455,$D6AC,$ABDB,$7218,$5DAA,$D9AD,$AF32,$ABDA,$53CD
	DEFW $5AB1,$BACD,$B222,$36DF,$ACAE,$A28A,$2342,$BB92,$81F3,$ABAB,$CCE5,$51DC,$A21E,$49B0,$5552,$05CA
	DEFW $C964,$D15D,$F876,$124D,$8031,$2A03,$2414,$3389,$1094,$7F21,$4109,$6013,$CC4D,$2E60,$4BA2,$5041
	DEFW $23FE,$3813,$C020,$8004,$1109,$008C,$1010,$E8CB,$0559,$898D,$8538,$2105,$E369,$64B0,$9BD1,$9550
	DEFW $2035,$0962,$D8E3,$914E,$0C09,$9253,$1399,$AFA8,$A423,$54CA,$ABBC,$29DB,$343E,$6430,$CDD0,$42E6
	DEFW $3B19,$1691,$4A08,$9D54,$24A7,$5905,$CE12,$78BA,$BAE8,$93BD,$C965,$BDB1,$CA57,$ACCC,$DCB3,$9B5E
	DEFW $9CDA,$2153,$6BAD,$BA47,$6E6B,$BA4E,$BD34,$F256,$82FA,$27D1,$2977,$059D,$5B7B,$E504,$0C2A,$3008
	DEFW $C063,$86D1,$B62A,$484F,$4A44,$C48B,$2BA1,$6552,$6427,$2908,$341D,$C384,$5E3B,$0C6A,$CB38,$2203
	DEFW $28D9,$777C,$4089,$3026,$9B06,$0134,$11DB,$43D0,$A28A,$638E,$E9AF,$92C6,$8796,$3A22,$113C,$C24B
	DEFW $54B2,$E0AA,$D0DC,$8495,$DBE9,$D0E6,$34F5,$6BE5,$5221,$C372,$E81A,$AC2C,$B42C,$DB22,$E989,$AB6D
	DEFW $09EB,$47DE,$0BAB,$D1D3,$DD22,$BDAA,$5EB4,$2ABB,$A4CE,$307B,$D2C5,$ABAC,$34AC,$DE83,$D3CA,$9DA0
	DEFW $3BC6,$2F66,$2DCB,$7C8B,$F92D,$B8E7,$60AF,$994E,$9239,$3712,$60D8,$703C,$602A,$2102,$C351,$F710
	DEFW $2504,$6007,$F804,$C321,$6E00,$81A7,$9C46,$F603,$5978,$F2F0,$68C0,$0788,$69DB,$27BC,$1D84,$1380
	DEFW $67A4,$109D,$135A,$E282,$5394,$11B0,$0195,$118C,$841F,$B6CC,$6882,$ECF2,$43E7,$DCB6,$463A,$B088
	DEFW $BDE8,$8214,$1980,$18C9,$D429,$7EB8,$C367,$9319,$65A6,$D360,$D41B,$5563,$E05C,$155C,$8288,$E036
	DEFW $E142,$D231,$711A,$A63E,$EB04,$A503,$3805,$1BB3,$5842,$1D55,$4A67,$0031,$4127,$3AED,$4314,$108D
	DEFW $1849,$C64B,$B06E,$5B05,$A500,$4065,$600D,$F414,$CC3E,$D93C,$A198,$61DE,$23AC,$81D1,$886E,$84F4
	DEFW $0EDB,$B05A,$07E6,$C96C,$AB7E,$8E5B,$942A,$5DCD,$B400,$4705,$9489,$C332,$405C,$06FA,$2037,$2591
	DEFW $8C36,$F96E,$3602,$7804,$7069,$8CAC,$6C76,$ED7C,$5889,$C16E,$ED0D,$E12D,$8C1A,$4D23,$3684,$F1C0
	DEFW $3BD8,$618C,$70C7,$3742,$25B8,$5A68,$3056,$353C,$2F1C,$E99A,$60BE,$9C4E,$1407,$1367,$AFC0,$8386
	DEFW $4178,$A6FE,$BCA0,$5733,$97C3,$84E0,$870F,$DD62,$CA01,$E6F1,$C19C,$B0B0,$42DE,$9346,$AD1F,$97B2
	DEFW $EBE3,$6A30,$F390,$F12C,$B08A,$2875,$7C2A,$3AA0,$2E24,$D548,$04D1,$C1D5,$DB60,$A3B1,$9A28,$34A9
	DEFW $7442,$59AE,$17AC,$F7BE,$D86F,$2D53,$BD6B,$F9DA,$6829,$CCAB,$78B5,$1926,$7F45,$0B94,$7BDF,$71A5
	DEFW $F04D,$5449,$C8AE,$A0E8,$9747,$7A82,$304C,$94F6,$394F,$C3BE,$8752,$79CF,$3085,$9B41,$08F6,$A14D
	DEFW $21AB,$41FE,$4C85,$1D47,$07DF,$0676,$07BC,$D869,$EB01,$4BB9,$8A01,$8841,$1E01,$A23E,$6A4E,$0A99
	DEFW $49E8,$A839,$B110,$B1CC,$C1D0,$01D0,$4964,$6540,$AC35,$8E87,$1C42,$4194,$AD6D,$3ECA,$995D,$D35D
	DEFW $ADBA,$D325,$CDAA,$13AF,$AC93,$EC3E,$F9C0,$0C10,$49AF,$70DD,$40DC,$5D9E,$D01C,$EB54,$D027,$BA73
	DEFW $392F,$6052,$E3EF,$3B43,$15C3,$7304,$328A,$42D1,$5621,$C842,$950D,$9B0F,$9822,$4F04,$2054,$F775
	DEFW $0168,$FDFB,$ED0F,$A428,$18F0,$E270,$3072,$521F,$2703,$C36E,$8147,$FCC6,$944E,$870C,$AA15,$BC03
	DEFW $9911,$E008,$48C9,$F209,$1F68,$48E4,$44F7,$9729,$740F,$AE81,$CE6B,$85F8,$028F,$AB43,$00BC,$F463
	DEFW $0000
DEFB 2
SAMPLE3_END:


SAMPLE2:
; 521 bytes now follow:
	DEFW $2420,$4650,$2510,$8067,$4203,$1413,$4533,$8B58,$0034,$0023,$0000,$2625,$0200,$C0C6,$BB0A,$5118
	DEFW $A1CC,$CBBC,$4141,$4B05,$9D58,$3826,$173E,$3DCB,$4634,$6F11,$5124,$2826,$6AE4,$D24C,$4B42,$224C
	DEFW $574C,$790E,$0AC9,$E6F0,$391B,$123D,$5252,$C321,$1C3C,$0341,$4546,$4212,$60BC,$8732,$6B56,$4147
	DEFW $82B1,$843D,$A260,$40DD,$5D26,$B0AB,$6BAA,$CA99,$74F9,$DEDC,$2EDD,$AAED,$C5AB,$EA6C,$7A41,$8854
	DEFW $C815,$CB68,$D5AB,$91DD,$EEA5,$53CD,$AAB4,$CB1A,$AEAB,$94DC,$F675,$A6BA,$BF61,$B989,$BABC,$5DDC
	DEFW $35D2,$76EE,$E9A4,$AAA9,$2B9A,$16AB,$D13D,$1E46,$D4FC,$88BD,$ACCA,$DC1A,$56AD,$DEB1,$9BBB,$3299
	DEFW $EBBB,$DEEF,$7B8A,$66A9,$9AAB,$C9AF,$ACCB,$14B9,$BC1D,$994F,$57A9,$BAA7,$35B3,$CDEE,$AF71,$0BCC
	DEFW $899F,$CB98,$F75F,$DEED,$A945,$CDFD,$BCCB,$619A,$3615,$B5A7,$96ED,$D3BC,$6D85,$AFAB,$97F4,$9BE0
	DEFW $0254,$CC95,$CB45,$785E,$F4CD,$899A,$B978,$F5E5,$96D5,$5A16,$CB4A,$ACE6,$C4DC,$4D16,$6B01,$A1BC
	DEFW $5E78,$57D6,$FE9D,$6ADE,$49CD,$A9ED,$8DC0,$4BA3,$CD37,$60AA,$1D4D,$86C6,$EBDB,$5ADC,$C67A,$6A27
	DEFW $BACA,$F8D2,$5B27,$FB4E,$C31A,$5F66,$76B2,$2DE7,$EFD9,$BC32,$A9E5,$4F68,$765C,$C0D1,$F2F7,$F5E0
	DEFW $67FD,$CCA7,$DFE7,$82FF,$BB2B,$E76C,$7DBA,$6DE0,$4F4D,$141F,$CCFC,$E867,$8889,$B959,$EA0E,$5D9A
	DEFW $A96C,$DDA1,$9158,$10F5,$48E3,$2A18,$F3A5,$41E5,$06D1,$15CA,$F343,$7C48,$A31B,$8002,$0CB6,$0C00
	DEFW $0006,$4330,$449A,$E20D,$C781,$3C1E,$E40D,$9007,$B30B,$1B26,$5170,$3C82,$1EF6,$4146,$54BC,$DD58
	DEFW $4BD1,$78DA,$D097,$A661,$4FAB,$45D7,$C5CC,$D96B,$DE8C,$41B4,$FE86,$E88D,$E298,$2B46,$4E31,$1A70
	DEFW $94EF,$8FBC,$5038,$01A2,$40E7,$BCC3,$0749,$125D,$1877,$A436,$6C0F,$80AC,$42D8,$F5CB,$3F40,$7E5E
	DEFW $301C,$00B0,$4008,$000B
DEFB 16
SAMPLE2_END:


SAMPLE1:
; 321 bytes now follow:
	DEFW $0000,$5610,$2370,$0080,$0001,$2020,$4012,$8035,$1133,$0403,$6050,$6254,$2022,$C050,$BB04,$4008
	DEFW $48BC,$CCB1,$DA1A,$CBAA,$AB52,$2819,$54BA,$BA28,$5FC1,$9BCD,$DCB9,$ACFE,$DBA9,$9ABD,$DDCA,$A8CD
	DEFW $AC95,$FE88,$8ADE,$EDB8,$87AD,$4543,$1D6A,$EB76,$5A6E,$EEB7,$75B5,$DF6A,$B6DF,$AEFE,$DA56,$EFFE
	DEFW $926B,$CEFD,$C807,$8DD6,$4B70,$AD1A,$BEA4,$05ED,$FFD9,$605A,$EFED,$A109,$CF5A,$C7B5,$DA9D,$EA30
	DEFW $5BFC,$FC80,$09DF,$13A3,$9EE5,$B210,$507C,$2ADC,$FD80,$6B98,$063F,$FFB5,$01AE,$2ED8,$00F2,$7DA7
	DEFW $A720,$6CEA,$40D3,$FEEB,$504B,$EFFB,$602B,$9EFC,$701A,$D341,$820A,$2AFD,$602A,$B8E0,$8D12,$554B
	DEFW $395B,$AAEC,$32CD,$4B64,$408C,$DA98,$12AF,$FEC9,$ADCD,$7106,$CDCE,$B618,$49FD,$ED94,$6ADF,$EC3F
	DEFW $8BEE,$9C75,$D6DA,$C966,$BD9E,$7658,$CE7A,$A669,$DE7E,$857A,$F0DB,$CF9C,$2957,$CABE,$A758,$7A7E
	DEFW $EC96,$8BDE,$4986,$DFAC,$C977,$79BD,$CF1B,$988A,$9C4F,$889E,$F4E5,$B989,$CDFC,$A99A,$FDDC,$BD99
	DEFW $ACDB,$4B32,$DCBC,$31BA,$AB4E,$CB9C,$5D7B,$93C1,$EFA6,$70BB,$FB90,$3A89,$240E,$22E1,$0080,$0040
DEFB 1
SAMPLE1_END:

DEPACKER:
        ;; INSERT YOUR FAV DEPACKER HERE (EG: EXOZIP OR MEGALZ)..
        ;; INPUT HL=LOCATION OF PACKED DATA, DE=WHERE YOU WANT TO DUMP IT
        ;; OUTPUT: THIS WILL DEPACK YOUR DATA & DUMP IT WHERE TOLD...
        ;; USING EXOZIP HERE...

deexo:          ld      ixh,128

                ld      b,52
                ld      iy,exo_mapbasebits
                push    de
exo_initbits:   ld      a,b
                sub     4
                and     15
                jr      nz,exo_node1

                ld      de,1            ;DE=b2
exo_node1:      ld      c,16            ;%00010000
exo_get4bits:   call    exo_getbit
                rl      c
                jr      nc,exo_get4bits
                ld      (iy+0),c        ;bits(i)=b1

                push    hl
                inc     c
                ld      hl,0
                scf
exo_setbit:     adc     hl,hl
                dec     c
                jr      nz,exo_setbit
                ld      (iy+52),e
                ld      (iy+104),d      ;base(i)=b2
                add     hl,de
                ex      de,hl
                inc     iy

                pop     hl
                djnz    exo_initbits
                inc     c

exo_literalseq: pop     de
exo_literalcopy:ldir                    ;copy literal(s)
exo_mainloop:   ld      c,1
                call    exo_getbit      ;literal?
                jr      c,exo_literalcopy
                ld      c,255
exo_getindex:   inc     c
                call    exo_getbit
                jr      nc,exo_getindex
                ld      a,c             ;C=index
                cp      16
                ret     z
                jr      c,exo_continue
                push    de
                ld      d,16
                call    exo_getbits
                jr      exo_literalseq
exo_continue:   push    de
                call    exo_getpair
                push    bc
                pop     af
                ex      af,af'          ;lenght in AF'
                ld      de,512+48       ;1?
                dec     bc
                ld      a,b
                or      c
                jr      z,exo_goforit
                ld      de,1024+32
                dec     bc              ;2?
                ld      a,b
                or      c
                jr      z,exo_goforit
                ld      e,16
exo_goforit:    call    exo_getbits
                ld      a,e
                add     a,c
                ld      c,a
                call    exo_getpair     ;bc=offset
                pop     de              ;de=destination
                push    hl
                ld      h,d
                ld      l,e
                sbc     hl,bc           ;hl=origin
                ex      af,af'
                push    af
                pop     bc              ;bc=lenght
                ldir
                pop     hl              ;Keep HL, DE is updated
                jr      exo_mainloop    ;Next!

exo_getpair:    ld      iy,exo_mapbasebits
                ld      b,0
                add     iy,bc
                ld      d,(iy+0)
                call    exo_getbits
                ld      a,c
                add     a,(iy+52)
                ld      c,a
                ld      a,b
                adc     a,(iy+104)
                ld      b,a
                ret

exo_getbits:    ld      bc,0            ;get D bits in BC
exo_gettingbits:dec     d
                ret     m
                call    exo_getbit
                rl      c
                rl      b
                jr      exo_gettingbits

exo_getbit:     ld      a,ixh           ;get one bit
                add     a,a
                ld      ixh,a
                ret     nz
                ld      a,(hl)
                inc     hl
                rla
                ld      ixh,a
          ret

exo_mapbasebits:
          defs    156     ;tables for bits, baseL, baseH
exodone   defb    0       ; done already

;MAIN ENTRY for 3 channel 3 waves waveplayer
MainLoop:
          exx      ; alt_reg
          push hl  ; save system pointer
          push iy  ;
          push ix  

chkin     in a,(254)
          cp 255   ; issue 1 zx ?
          jr z,ok
          cp 191   ; issue 2 zx ?
          jr z,ok
          jr chkin  ; on NO keys pushed the prog will start

ok        ld (ktype1),a  ; store 'empty key input' in key check routine being an 255 or an 191 value !!!
          ld (ktype2),a
; dont now how long some one will press ENTER !!!???!!!???

          ld ix,lengts    ; 6 addresses to remember
          CALL ORGZSND    ;; ORGANIZE PACKED DATA FROM ADDRTABLE
                        ;; (ASSUMES 'WAV'  has been poked with Sample TO PLAY)

          CALL PLAYWAV   ;; Plays the WAV,  MUSIC MAESTRO or just say something weird

          pop ix
          pop iy
          pop hl
          exx
          RET             ;; THATS IT FOLKS, THATS YOUR MAIN ROUTINE DONE!
        
ORGZSND:
        ;; ORGANIZE SOUND SAMPLE DATA ACCORDING TO ADDRTABLE OFFSETS
        ;; INPUT Poke 'WAV' with SAMPLE number first
        ;; OUTPUTS HL=ADDRESS OF SAMPLE, DE=DESTINATION OF WHERE TO DUMP IT
        ;; THEN ACTUALLY DUMPS THE PACK INTO THE UNPACKED AREA (ADDRESS DE)

          ld hl,exodone ; but first check if de_exo was already executed.
          bit 0,(hl)
          ret nz

wipe      xor a          ;  4t
          ld hl,35000    ; 10t
          ld (hl),a      ;  7t
          ld de,35001    ; 10t
          ld bc,30534    ; 10t wipe full available memory
          ldir           ; 21t * 30534  +16 t = 641230 = about 9/50 second

ADDRTABLE:
src_1 equ $+1
          ld hl,SAMPLE3 
          ld de,35000
          inc de
          inc de       ;step over length in samples (twice byte length -oddness)
hl1       ld (ix+0),e
          ld (ix+1),d  ; begin adres
          dec de
          dec de
          push ix
          CALL invoke   ;; UNZIP THE SAMPLE (ASSUMES HL & DE)
          pop ix
hl1b      ld (ix+2),l
          ld (ix+3),h  ; end adres

src_2 equ $+1
          ld hl,SAMPLE2
          ld de,45000
          inc de
          inc de
hl2       ld (ix+4),e
          ld (ix+5),d
          dec de
          dec de
          push ix
          CALL invoke   ;; UNZIP THE SAMPLE (ASSUMES HL & DE)
          pop ix
hl2b      ld (ix+6),l
          ld (ix+7),h

src_3 equ $+1
          ld hl,SAMPLE1
          ld de,55000
          inc de
          inc de
hl3       ld (ix+8),e
          ld (ix+9),d
          dec de
          dec de
          push ix
          CALL invoke   ;; UNZIP THE SAMPLE (ASSUMES HL & DE)
          pop ix
hl3b      ld (ix+10),l
          ld (ix+11),h

          push ix
          pop hl
          push hl
          ld bc,12
          add hl,bc
          pop de
          ex de,hl
          ldir  ; copy 'lengts' to 'keeptese' as SPARE after mixing IX values up

          ld hl,exodone
          set 0,(hl)
sret      RET                     ; BUT FINE FOR ANY OTHER DEPACKED DATA


invoke    push de
          CALL DEPACKER   ;; UNZIP THE SAMPLE (ASSUMES HL & DE)
          ex de,hl   ; hl end adres
          pop de     ; start  data
          sbc hl,de
          ex de,hl   ; hl start, de length in bytes 
;swop_samples $Ba becomes $aB
;     ld hl,00  ; dummy adres
;     ld de,01  ; deummy length
swop_smpls:
          ld a,(hl)
          rlca
          rlca
          rlca
          rlca
          ld (hl),a
          inc hl
          dec de
          ld a,e
          or d
          jr nz,swop_smpls
          ret

;store in 'normal register set' norm_reg
fillIx    ld l,(ix+0)   ;19t lsb 
          ld h,(ix+1)   ;19t msb start adres channel A
          ld e,(ix+4)   ;19t
          ld d,(ix+5)   ;19t start adres channel B
          ld c,(ix+8)   ;19t
          ld b,(ix+9)   ;19t start adres channel C
          ret           ;10t  =124t

; 3 channel sample PLAYER LOOP (almost and still without recorder!!)
PLAYWAV   di            ; 4t
          call fillIx   ; alt_reg
          exx           ; 4t norm_reg   store DATA_POINTERS in alternate_reg'
;preset AY with a Formant voice chord, or any other full sized  AYregister group
; reg 7 should be/is usualy set to 63 !!!!

          call vox      ;
          inc a         ; make a=7
          ld d,a        ; start AY_reg
          ld e,63       ; value set mixer to %00.111.111  , 
          call xov1     ; set mixer channel in AY, C_reg still 0xfd

          xor A         ; clear A> CARRY=0 > NC
          ld hl,$ffbf   ;  H and L set as quick value's for B in OUT (C),x
          LD E,%00001111;  mask for lower digit

dodone    equ $

      org 32768

; sample storage can have 2 ways in 1 byte, sample 'a' is 1st and 'B' 2nd.
;s1) step 1 low  digit step 2 high digit  $Ba,Dc etc regular wav2ay format
;s2) step 1 high digit step 2 low  digit  $aB,cD etc 
; the adres raise is after 2 steps only and takes 3x6t time which needs to be compensated
; the 4 nops are compensation for the rca.
; if the adres raise is done in nop time, then option S2 is most fast/compact but needs a 'stubburn' format...
;a regulair wav2ay file WILL HAVE to be converted by switching both digits within every sample couple.

; this player is timed for S2) $aB $cD format

samplelp:               ; work_1
          ld c,$fd      ; 7 t    7t worktime for 2nd  set of routine

;playtime_1
; channel A
          exx           ; 4 alt_reg
          ld a,(hl)     ; 7  , routine differs 3x but only at 'ld a,(reg)'
          exx           ; 4 norm_reg
          rrca          ; 4 
          rrca          ; 4
          rrca          ; 4
          rrca          ; 4 high digit first %11110000 > %00001111

          and e         ; 4
          ld b,h        ; 4
          ld d,8        ; 7 
          out (c),d     ; 12  reg 8
          ld b,l        ; 4
          out (c),a     ; 12  =4+7+4 +4+4+4+4 +4+4+7 +12+4+12= 74 t 

; channel B
          exx           ; 4 alt_reg
          ld a,(de)     ; 7  a_reg is NOT exx'ed , de_reg is exx'ed
          exx           ; 4 norm_reg
          rrca          ; 4 
          rrca          ; 4
          rrca          ; 4
          rrca          ; 4

          and e         ; 4
          ld b,h        ; 4
          inc d         ; 4
          out (c),d     ; 12  reg 9
          ld b,l        ; 4
          out (c),a     ; 12  =4+7+4 +4+4+4+4 +4+4+4 +12+4+12= 71 t 
; channel C
          exx           ; 4 alt_Reg
          ld a,(bc)     ; 7
          exx           ; 4 norm_reg
          rrca          ; 4 
          rrca          ; 4
          rrca          ; 4
          rrca          ; 4

          and e         ; 4
          ld b,h        ; 4
          inc d         ; 4
          out (c),d     ; 12  reg 10
          ld b,l        ; 4
          out (c),a     ; 12 =71t
;first  3x 4bit sample output done
;74+71+71=216 tstate for 12 bits 317-216= 101 t left

;(ix+0) LSB, (ix+1) MSB
; the following order of checking (x+0) and then (ix+1), versus (ix+1) and then (ix+0) gives a big timing difference
;
          exx           ; 4t alt_reg
lnachk    ld a,(ix+2)   ;19t
          cp l          ; 4t
          jp nz,lnbchk  ;10t (33) compare HL with IX_02,03

          ld a,(ix+3)   ;|19t once in 256 time(or less) 
          cp h          ;| 4t
          jp nz,lnbchk  ;|10t33t extra

          ld l,(ix+0)   ;|]19t
          ld h,(ix+1)   ;|]19t once per sample length (can be just 10) t=33+33+38=104t against 33t

lnbchk    ld a,(ix+6)   ;19t
          cp e          ; 4t
          jp nz,lncchk  ;10t (33)

          ld a,(ix+7)   ;]19t
          cp d          ;] 4t
          jp nz,lncchk   ;]10t

          ld e,(ix+4)   ;]\19t
          ld d,(ix+5)   ;]\19t

lncchk    ld a,(ix+10)  ;19t
          cp c          ; 4t
          jp nz,lnend   ;10t (33)

          ld a,(ix+11)  ;:19t
          cp b          ;: 4t
          jp nz,lnend    ;:10t

          ld c,(ix+8)   ;:/19t
          ld b,(ix+9)   ;:/19t

lnend
;playtime_1 end  216+107= 323 = 6t more the 'standard' 317
;first sample set of 3x4 bits in 323t_state is done

; playtime_2 
; channel A
          ld a,(hl)         ; 7
          inc HL            ; 6 raise sample address
          exx               ; 4 norm_reg
          inc iy            ;10t  DELAY, is it useable for something??

          and e             ; 4
          ld b,h            ; 4
          ld d,8            ; 7 
          out (c),d         ; 12  reg 8
          ld b,l            ; 4
          out (c),a         ; 12 = 4+7+6+4+ 10+ 4+4+7+12+4+12= 74t

; channel B
          exx               ; 4 alt_reg
          ld a,(de)         ; 7
          inc DE            ; 6 raise sample address
          exx               ; 4 norm_reg
          inc iy            ;10t  DELAY, is it useable for something??

          and e             ; 4
          ld b,h            ; 4
          inc d             ; 4
          out (c),d         ; 12  reg 9
          ld b,l            ; 4
          out (c),a         ; 12 = = 4+7+6+4+ 10+ 4+4+4+12+4+12= 71t

; channel C
          exx               ; 4 alt_reg
          ld a,(bc)         ; 7
          inc BC            ; 6 raise sample address
          exx               ; 4 norm_reg
          inc iy            ;10t  DELAY, is it useable for something??

          and e             ; 4
          ld b,h            ; 4
          inc d             ; 4
          out (c),d         ; 12  reg 10
          ld b,l            ; 4
          out (c),a         ; 12 =71t
; playtime_2 end      74+71+71=216
;second  3x 4bit sample output done

          inc c           ; 4t from fd to fe
          ld b,254        ; 7t by rotating value all 8 keyboard lines can be red
rotb      equ $-1
          in    a,(c)     ;11t port $00fe read keyboard CONTENDED !!!! 11 or 24 tstate!!
          xor %10111111   ; 7t   %101111111>00000000=nokey
ktype1    equ $-1
          jr    nz,keyed    ; 7t  exit stops timing needs

          rrc b             ; 8t from #fe,254 to #7f,127
          in    a,(c)       ;11t port $00fe read keyboard CONTENDED !!!! 11 or 24 tstate!!
          xor %10111111     ; 7t   %101111111>00000000=nokey
ktype2    equ $-1
          jr    nz,keyed    ; 7t  exit stops timing needs
 
          ld b,(hl)  ; 7t delay
          ld b,(hl)  ; 7t delay
          ld b,(hl)  ; 7t delay

go_on     jp samplelp     ;   norm_reg 10 work=(7)+(4+7+11+7+7)+(8+11+7+7)+(7+7+7)+(10)= 107t
;                         ; 216+107=323  from 317

keyed     and    $1F      ; 7t filter ANY key touch with bin 11111
          bit 7,b         ; 8t
          exx             ; 4t alt_reg
          jp z,keyed2     ;10

ky1       cp 1            ; 7t
          jr z,ter        ; 7t  END OF MC

          ld bc,4         ;10t
          ld de,lengts    ;10t destiny
          ld hl,keeptese   ;10t source

ky2       cp 2            ; 7t effect key
          jr nz,ky3       ;12t  make a=b=c from a  source
          jp pphl          ;10t

ky3       cp 4            ; 7t effect key
          jr nz,ky4       ;12t
;          ld bc,4         ;10t
;          ld de,lengts    ;10t destiny
          ld hl,keeptese+4 ;10t make a=b=c from b  source
          jp pphl          ;10t

ky4       cp 8            ; 7t effect key
          jr nz,ky5       ;12t
;          ld bc,4         ;10t
;          ld de,lengts    ;10t destiny
          ld hl,keeptese+8 ;10t make a=b=c from c  source
          jp pphl          ;10t

ky5       cp 16           
          jr nz,nokeys    ; nothing yet
          ld bc,12         ;10t
;          ld de,lengts    ;10t destiny
;          ld hl,keeptese   ;10t source
          ldir             ; make start a =/= b =/= c
          jr nokeys

pphl:
          push hl ; source
          push bc ; amount
          ldir    ;4byte
          pop bc  ; re-amount
          pop hl  ; re-source
                  ; destiny is destiny+4
          push hl ; source
          push bc ; amount
          ldir    ;4byte
          pop bc  ; re-amount
          pop hl  ; re-source
                  ; destiny is destiny+4
          ldir    ;4byte
                 ;12 bytes moved

keyed2  ; nothing yet

nokeys    call fillIx   ; restart WAVE , but keep on playing
          exx         ; 4t norm_reg 
          jp go_on    ;10t

; set all AY channels to zero 'silence'
ter       ei              ; did i forget about it ??? weird
silence   ld a,13         ; end   AY_reg ??will it work??
          jr d255

vox       ld a,6          ; end reg PORT
d255	  ld d,255        ; start reg PORT-1
          ld e,0          ; VALUE to set

xov       inc d           ; so start with 255 for 0
xov1	  ld bc,$fffd
	  out (c),d       ;
	  ld b,$bf
	  out (c),e
          cp d            ;
	  jr nz,xov       ; if a <> d then next AY_register
          ret             ; return to basic or not

; used in IX to COMPARE end off loop, filled with dummy adres to check the overwrite.
lengts    defw 16384 ; start A
          defw 16385 ;  end A
          defw 16384 ; start B
          defw 16385 ;  end B
          defw 16384 ; start C
          defw 16385 ;  end C

keeptese  defw 00
          defw 00
          defw 00
          defw 00
          defw 00
          defw 00    

z_the     end ;

; check if bankswitch is needed
;chkbanks:
; some code here
; jr nz,notneeded aka ayz
;bankswitching here, after 16384 * 2 samples
;
;     ld      a,(0x5b5c)  ; 13t ??
;     and     %11111000   ;  7t mask ram_banks to zero !!
;     ld      b,ram       ;  7t 0 to 7 only
;     or      b           ;  4t set ram in a
;     ld      bc, 0x7ffd  ; 10t
;     LD      (0x5b5c),A  ; 13t
;     OUT     (C),A       ; 12t

