;
         ORG  #8000
this     EQU  #1012
comp
         ENT  $
         DI   
         LD   hl,this
         LD   de,this+1
         LD   bc,#100
         LD   (hl),0
         LDIR 
         LD   hl,main
         LD   de,this
         LD   bc,mainl
         LDIR                           ; Set Up Main Section
make1
         CALL check1
         PUSH de
check1
         LD   a,1
         LD   (type),a
         LD   hl,t1omp
         LD   bc,t1ompl
         LDIR 
         PUSH de
         POP  ix
         RET  
main                                    ; Main Start Code - Set Up Colours.
         LD   ix,#4000
         LD   a,(ix+0)
         CALL #bc0e
         LD   b,(ix+1)
         LD   c,b
         CALL #bc38
         INC  ix
         INC  ix
         LD   a,15
main1
         PUSH af
         LD   b,(ix+0)
         LD   c,b
         CALL #bc32
         POP  af
         INC  ix
         DEC  a
         JP   p,this+#17
main2                                   ; This Is Where The Decomp Code Goes
mainl    EQU  $-main
type     DEFB 0                         ; Compression Type
;
t1omp
         XOR  a
         LD   l,a
         LD   h,l
         LD   e,h
         LD   d,e
         LD   ix,smallpic
t1omp1
         LD   a,(ix+0)
         OR   a
         RET  z
         BIT  7,a
         JR   nz,t1r
         LD   b,a
         INC  ix
t1omp3
         LD   a,(ix+0)
         INC  ix
         CALL t1omp2
         RET  nc
         DJNZ t1omp3
         JR   t1omp1
t1r
         RES  7,a
         LD   b,a
         INC  ix
t1r1
         LD   a,(ix+0)
         CALL t1omp2
         RET  nc
         DJNZ t1r1
         INC  ix
         JR   t1omp1
t1omp2   EQU  #5a+this
         PUSH bc
         PUSH de
         PUSH hl
         PUSH af
         LD   h,0
         LD   a,l
         AND  7
         ADD  a,a
         ADD  a,a
         ADD  a,a
         SRL  l
         SRL  l
         SRL  l
         LD   c,a
         LD   a,l
         ADD  a,a
         ADD  a,a
         ADD  a,l
         ADD  a,a
         LD   l,a
         ADD  hl,hl
         ADD  hl,hl
         ADD  hl,hl
         LD   a,c
         ADD  a,h
         OR   #c0
         LD   h,a
         LD   a,e
         ADD  a,l
         LD   l,a
         ADC  a,h
         SUB  l
         OR   #c0
         LD   h,a
         POP  af
         LD   (hl),a
         POP  hl
         POP  de
         POP  bc
         INC  l
         LD   a,l
         SUB  81
         RET  nz
         LD   l,0
         INC  e
         SCF  
         RET  
t1ompl   EQU  $-t1omp
;
compact
         LD   hl,#c000
         LD   (getb3),hl
         LD   hl,getbyte1
         DEC  a
         JR   z,comp1
         LD   hl,getbyte2
         DEC  a
         JR   z,comp1
         LD   hl,getbyte3
comp1
         LD   (gettype),hl
         LD   ix,count
         LD   bc,0
         LD   de,smallpic
         CALL start
         XOR  a
         LD   (de),a
         INC  de
         EX   de,hl
         LD   de,this-#12
         SBC  hl,de                     ; HL=Length Of Data
         RET  
smallpic EQU  #100
start
         LD   hl,smallpic+#4000
         OR   a
         SBC  hl,de
         RET  c
         LD   hl,diffs
         CALL getbyte
         RET  nc
         LD   (ix+1),a
         LD   (hl),a
         INC  hl
         LD   (ix+0),1
         CALL getbyte
         CALL nc,outsame
         RET  nc
         INC  (ix+0)
         LD   (hl),a
         INC  hl
         CP   (ix+1)
         LD   (ix+1),a
         JR   z,same
diffrent
         CALL getbyte
         CALL nc,outdiff
         RET  nc
         CP   (ix+1)
         CALL z,backdiff
         CALL z,outdiff
         JR   z,start
         CALL adddiff
         JR   nc,start
         JR   diffrent
same
         CALL getbyte
         CALL nc,outsame
         RET  nc
         CP   (ix+1)
         CALL nz,backsame
         CALL nz,outsame
         JR   nz,start
         CALL addsame
         JR   nc,start
         JR   same
getbyte
         JP   getbyte1                  ; Alter Address Depending On RTN.
gettype  EQU  $-2
getbyte1                                ; Horizontal Compression !
         LD   a,c
         CP   200
         RET  z
         PUSH hl
         PUSH bc
         AND  7
         ADD  a,a
         ADD  a,a
         ADD  a,a
         ADD  a,#c0
         LD   h,a
         LD   l,b
         PUSH hl
         LD   b,0
         SRL  c
         SRL  c
         SRL  c
         PUSH bc
         POP  hl
         ADD  hl,hl
         ADD  hl,hl
         ADD  hl,bc
         ADD  hl,hl
         ADD  hl,hl
         ADD  hl,hl
         ADD  hl,hl
         POP  bc
         ADD  hl,bc
         POP  bc
         INC  b
         LD   a,b
         CP   80
         JR   nz,gb2
         LD   b,0
         INC  c
gb2
         LD   a,(hl)
         POP  hl
         SCF  
         RET  
;
getbyte2                                ; Vertical Compression
         LD   a,b
         CP   80
         RET  z
         PUSH hl
         PUSH bc
         AND  7
         ADD  a,a
         ADD  a,a
         ADD  a,a
         ADD  a,#c0
         LD   h,a
         LD   l,b
         PUSH hl
         LD   b,0
         SRL  c
         SRL  c
         SRL  c
         PUSH bc
         POP  hl
         ADD  hl,hl
         ADD  hl,hl
         ADD  hl,bc
         ADD  hl,hl
         ADD  hl,hl
         ADD  hl,hl
         ADD  hl,hl
         POP  bc
         ADD  hl,bc
         POP  bc
         INC  c
         LD   a,c
         SUB  200
         JR   nz,gb2a
         LD   c,a
         INC  b
gb2a
         LD   a,(hl)
         POP  hl
         SCF  
         RET  
;
getbyte3                                ; Standard Compression
         PUSH hl
         LD   hl,#c000
getb3    EQU  $-2
         LD   b,(hl)
         INC  hl
         POP  hl
         LD   a,h
         OR   a
         RET  z
         LD   a,b
         SCF  
         RET  
adddiff
         LD   (ix+1),a
         LD   (hl),a
         INC  hl
         INC  (ix+0)
         LD   a,(ix+0)
         CP   127
         RET  c
outdiff
         LD   a,(ix+0)
         LD   (de),a
         INC  de
         LD   hl,diffs
         PUSH bc
         LD   c,a
         LD   b,0
         LDIR 
         POP  bc
         RET  
addsame
         INC  (ix+0)
         LD   a,(ix+0)
         CP   127
         RET  c
outsame
         LD   a,(ix+0)
         SET  7,a
         LD   (de),a
         INC  de
         LD   a,(ix+1)
         LD   (de),a
         INC  de
         RET  
backdiff
         PUSH af
         DEC  (ix+0)
         POP  af
         CALL backsame
backsame
         PUSH af
         LD   a,b
         SUB  1
         JR   nc,bs1
         DEC  c
         LD   a,79
bs1
         LD   b,a
         POP  af
         RET  
count    DEFW 0
diffs    DEFS 127
