Advertisement
Runer112

Untitled

Jul 16th, 2011
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. ;Initialization. You only need to run this once.
  3.  push hl
  4.   ld hl,Uncrunch2BaseAddress
  5.   ld bc,Program2End-Uncrunch2
  6.   ld de,$8000
  7.   ldir
  8.  pop hl
  9.  
  10. Uncrunch:
  11. ; Entry: HL = Destination for unpacked data
  12. ;        DE = Source packed data pointer
  13. ;        B  = Source page
  14.  ld (OutPtr),hl
  15.  ex de,hl    ;HL=FILE
  16.  ld a,i
  17.  in a,(6)     ;does not affect flags.
  18.  call Uncrunch2
  19.  ret po
  20.  ei
  21.  ret
  22. Uncrunch2BaseAddress:
  23. .org $8000
  24. Uncrunch2:
  25.  push af
  26.   ld a,b
  27.   out (6),a   ;does not affect flags.
  28.   call Uncrunch2a
  29.  pop af
  30.  out (6),a
  31.  ret
  32. Uncrunch2a:
  33. ; Read the file header & setup variables
  34.   call _2IncHL
  35.   call _2IncHL  ;getting past the first two bytes to check for the header
  36.   call _2getAincHL
  37.   cp 'p'
  38.   jr nz,StopUncrunch2
  39.   call _2getAincHL
  40.   cp 'u'
  41.   jr z,Uncrunch2Cont
  42. StopUncrunch2:
  43.   ld hl,0
  44.   ret
  45. Uncrunch2Cont:
  46.   call _2IncHL
  47.   call _2IncHL  ;inc by 6
  48.  
  49.   call _2getAincHL
  50.   ld (escPu),a                                              ; 32 C4 DF
  51.   call _2IncHL
  52.   call _2IncHL
  53.   ld a,(hl)                                                 ; 7E
  54.   ld (EscBits),a                                            ; 32 C9 DF
  55.   ld a,8                                                    ; 3E 08    saved 1 byte
  56.   sub (hl)                                                  ; 96
  57.   ld (Esc8Bits),a                                           ; 32 CA DF
  58.   call _2IncHL
  59.   ld a,(hl)                                                 ; 7E
  60.   ld (MaxGamma),a                                           ; 32 CB DF
  61.   ld a,8                                                    ; 3E 08    saved 1 byte
  62.   sub (hl)                                                  ; 96
  63.   inc a                                                     ; 3C
  64.   ld (Max8Gamma),a                                          ; 32 CE DF
  65.   call _2IncHL
  66.   ld a,(hl)                                                 ; 7E
  67.   ld (Max1Gamma),a                                          ; 32 CC DF
  68.   add a,a                                                   ; 87
  69.   dec a                                                     ; 3D
  70.   ld (Max2Gamma),a                                          ; 32 CD DF
  71.   call _2IncHL
  72.   ld a,(hl)                                                 ; 7E
  73.   ld (ExtraBits),a                                          ; 32 CF DF
  74.   call _2IncHL
  75.   call _2IncHL
  76.   call _2IncHL
  77.   call _2getAincHL
  78.   ld b,a                                                    ; 47
  79.   ld de,tablePu                                             ; 11 D0 DF
  80. ; Copy the RLE table (maximum of 31 bytes) to RAM
  81.   inc b                                                     ; 04
  82.   srl b                                                     ; CB 38
  83.   jr nc,_2orleloop                                           ; 30 04
  84. _2rleloop:
  85.   call _2getAincHL
  86.   ld (de),a                                                 ; 12
  87.   inc de                                                    ; 13
  88. _2orleloop:
  89.   call _2getAincHL
  90.   ld (de),a                                                 ; 12
  91.   inc de                                                    ; 13
  92.   djnz _2rleloop                                             ; 10 <offset>     saved 1 byte
  93.   ld d,$80                                                  ; 16 80
  94.   jr _2main                                                  ; 18 2A
  95. _2newesc:
  96.   ld b,a                                                    ; 47
  97.   ld a,(escPu)                                              ; 3A C4 DF
  98.   ld (regy),a                                               ; 32 EF DF
  99.   ld a,(EscBits)                                            ; 3A C9 DF
  100.   ld e,a                                                    ; 5F
  101.   ld a,b                                                    ; 78
  102.   inc e                                                     ; 1C
  103.   call _2getchk                                              ; CD 3D 06
  104.   ld (escPu),a                                              ; 32 C4 DF
  105.   ld a,(regy)                                               ; 3A EF DF
  106.   ; Fall through and get the rest of the bits.
  107. _2noesc:
  108.   ld b,a                                                    ; 47
  109.   ld a,(Esc8Bits)                                           ; 3A CA DF
  110.   ld e,a                                                    ; 5F
  111.   ld a,b                                                    ; 78
  112.   inc e                                                     ; 1C
  113.   call _2getchk                                              ; CD 3D 06
  114. ; Write out the escaped/normal byte
  115.   ld bc,(OutPtr)                                            ; ED 4B C5 DF
  116.   ld (bc),a                                                 ; 02
  117.   inc bc                                                    ; 03
  118.   ld (OutPtr),bc                                            ; ED 43 C5 DF
  119.   ; Fall through and check the escape bits again
  120. _2main:
  121.   ld a,(EscBits)                                            ; 3A C9 DF
  122.   ld e,a                                                    ; 5F
  123.   xor a ; A = 0                                             ; AF
  124.   ld (regy),a                                               ; 32 EF DF
  125.   inc e                                                     ; 1C
  126.   call _2getchk   ; X=2 -> X=0                               ; CD 3D 06
  127.   ld b,a                                                    ; 47
  128.   ld a,(escPu)                                              ; 3A C4 DF
  129.   cp b                                                      ; B8
  130.   ld a,b                                                    ; 78
  131.   jr nz,_2noesc  ; Not the escape code -> get the rest of the byte ; 20 D8
  132.   ; Fall through to packed code
  133.   call _2getval    ; X=0 -> X=0                              ; CD 1B 06
  134.   ld (lzpos),a    ; xstore - save the length for a          ; 32 C7 DF
  135.                   ;   later time
  136.   srl a           ; cmp #1  ; LEN == 2 ? (A is never 0)     ; CB 3F
  137.   jr nz,_2lz77     ; LEN != 2 -> LZ77                        ; jp -> jr -> saved 1 byte
  138.   call _2get1bit   ; X=0 -> X=0                              ; CD 33 06
  139.   srl a           ; bit -> C, A = 0                         ; CB 3F
  140.   jr nc,_2lz77_22   ; A=0 -> LZPOS+1  LZ77, len=2             ; jp -> jr -> saved 1 byte
  141.     ; e..e01
  142.   call _2get1bit   ; X=0 -> X=0                              ; CD 33 06
  143.   srl a           ; bit -> C, A = 0                         ; CB 3F
  144.   jr nc,_2newesc   ; e..e010 New Escape                      ; jp -> jr -> saved 1 byte
  145.     ; e..e011               Short/Long RLE
  146.   ld a,(regy)     ; Y is 1 bigger than MSB loops            ; 3A EF DF
  147.   inc a                                                     ; 3C
  148.   ld (regy),a                                               ; 32 EF DF
  149.   call _2getval    ; Y is 1, get len,  X=0 -> X=0            ; CD 1B 06
  150.   ld (lzpos),a    ; xstore - Save length LSB                ; 32 C7 DF
  151.   ld c,a                                                    ; 4F
  152.   ld a,(Max1Gamma)                                          ; 3A CC DF
  153.   ld b,a                                                    ; 47
  154.   ld a,c                                                    ; 79
  155.   cp b            ; ** PARAMETER 63-64 -> C set,            ; B8
  156.                   ; 64-64 -> C clear..
  157.   jr c,_2chrcode   ; short RLE, get bytecode                 ; 38 12
  158.     ; Otherwise it's long RLE
  159. _2longrle:
  160.   ld b,a                                                    ; 47
  161.   ld a,(Max8Gamma)                                          ; 3A CE DF
  162.   ld e,a          ; ** PARAMETER  111111xxxxxx              ; 5F
  163.   ld a,b                                                    ; 78
  164.   call _2getbits   ; get 3/2/1 more bits to get a full byte, ; CD 34 06
  165.                   ;   X=2 -> X=0
  166.   ld (lzpos),a    ; xstore - Save length LSB                ; 32 C7 DF
  167.   call _2getval    ; length MSB, X=0 -> X=0                  ; CD 1B 06
  168.   ld (regy),a     ; Y is 1 bigger than MSB loops            ; 32 EF DF
  169. _2chrcode:
  170.   call _2getval    ; Byte Code,  X=0 -> X=0                  ; CD 1B 06
  171.   ld e,a                                                    ; 5F
  172.   add a,(tablePu-1) % 256                                   ; C6 xx ; saved 1 byte
  173.   ld c,a                                                    ; 4F
  174.   ld a,(tablePu-1) / 256                                    ; 3E xx
  175.   adc a,0                                                   ; CE 00
  176.   ld b,a                                                    ; 47
  177.   ld a,e                                                    ; 7B
  178.   cp 32           ; 31-32 -> C set, 32-32 -> C clear..      ; FE 20
  179.   ld a,(bc)                                                 ; 0A
  180.   jr c,_2less32    ; 1..31                                   ; 38 06
  181.     ; Not ranks 1..31, -> 11111 ° xxxxx (32..64), get byte..
  182.   ld a,e          ; get back the value (5 valid bits)       ; 7B
  183.   ld e,3                                                    ; 1E 03
  184.   call _2getbits   ; get 3 more bits to get a full byte,     ; CD 34 06
  185.                   ; X=3 -> X=0
  186. _2less32:
  187.   push hl                                                   ; E5
  188.     push af                                                 ; F5
  189.       ld a,(lzpos)                                          ; 3A C7 DF
  190.       ld e,a          ; xstore - get length LSB             ; 5F
  191.       ld b,e                                                ; 43
  192.       inc b           ; adjust for cpx#$ff;bne -> bne       ; 04
  193.       ld a,(regy)                                           ; 3A EF DF
  194.       ld c,a                                                ; 4F
  195.       ld hl,(OutPtr)                                        ; 2A C5 DF
  196.     pop af                                                  ; F1
  197. _2dorle:
  198.     ld (hl),a                                               ; 77
  199.     inc hl                                                  ; 23
  200.     djnz _2dorle                                             ; 10 nn  ; saved 1 byte
  201.     dec c                                                   ; 0D
  202.     jr nz,_2dorle    ; Y was 1 bigger than wanted originally ; 20 F8
  203.     ld (OutPtr),hl                                          ; 22 C5 DF
  204.   pop hl                                                    ; E1
  205.   jp _2main                                                  ; C3 2D 05
  206. _2lz77:
  207.   call _2getval    ; X=0 -> X=0                              ; CD 1B 06
  208.   ld b,a                                                    ; 47
  209.   ld a,(Max2Gamma)                                          ; 3A CD DF
  210.   cp b            ; end of file ?                           ; B8
  211.   ret z           ; yes, exit                               ; C8
  212.   ld a,(ExtraBits) ; ** PARAMETER (more bits to get)        ; 3A CF DF
  213.   ld e,a                                                    ; 5F
  214.   ld a,b                                                    ; 78
  215.   dec a           ; subtract 1  (1..126 -> 0..125)          ; 3D
  216.   inc e                                                     ; 1C
  217.   call _2getchk    ;f  ; clears Carry, X=0 -> X=0            ; CD 3D 06
  218. _2lz77_22:
  219.   ld (lzpos+1),a  ; offset MSB                              ; 32 C8 DF
  220.   ld e,8                                                    ; 1E 08
  221.   call _2getbits   ; clears Carry, X=8 -> X=0                ; CD 34 06
  222.   ; Note: Already eor:ed in the compressor..
  223.   ld b,a                                                    ; 47
  224.   ld a,(lzpos)                                              ; 3A C7 DF
  225.   ld e,a          ; xstore - LZLEN (read before it's        ; 5F
  226.                   ;   overwritten)
  227.   ld a,(OutPtr)                                             ; 3A C5 DF
  228.   add a,b         ; -offset -1 + curpos (C is clear)        ; 80
  229.   ld (lzpos),a                                              ; 32 C7 DF
  230.   ld a,(lzpos+1)                                            ; 3A C8 DF
  231.   ld b,a                                                    ; 47
  232.   ld a,(OutPtr+1)                                           ; 3A C6 DF
  233.   ccf                                                       ; 3F
  234.   sbc a,b                                                   ; 98
  235.   ld (lzpos+1),a  ; copy X+1 number of chars from LZPOS to  ; 32 C8 DF
  236.                   ; OUTPOS
  237.   inc e           ; adjust for cpx#$ff;bne -> bne           ; 1C
  238.   ; Write decompressed bytes out to RAM
  239.   ld b,e                                                    ; 43
  240.   push de                                                   ; D5
  241.   push hl                                                   ; E5
  242.     ld hl,(lzpos)                                           ; 2A C7 DF
  243.     ld de,(OutPtr)                                          ; ED 5B C5 DF
  244.     ld a,b                                                  ; 78
  245.     or a            ; Is it zero?                           ; B7
  246.     jr z,_2zero      ; yes                                   ; 28 19
  247.     inc b                                                   ; 04
  248.     srl b                                                   ; CB 38
  249.     jr nc,_2olzloop                                          ; 30 04
  250. _2lzloop:
  251.     ld a,(hl)                                               ; 7E
  252.     inc hl                                                  ; 23
  253.     ld (de),a                                               ; 12
  254.     inc de                                                  ; 13
  255. _2olzloop:
  256.     ld a,(hl)       ; Note: Must be copied forward          ; 7E
  257.     inc hl                                                  ; 23
  258.     ld (de),a                                               ; 12
  259.     inc de                                                  ; 13
  260.     djnz _2lzloop                                            ; 10 <offset>    saved 1 byte
  261.     ld (OutPtr),de                                          ; ED 53 C5 DF
  262.   pop hl                                                    ; E1
  263.   pop de                                                    ; D1
  264.   jp _2main                                                  ; C3 2D 05
  265.  
  266. _2zero:
  267.   ld b,128                                                  ; 06 80
  268.   jr _2lzloop                                                ; 18 E8
  269. ; getval : Gets a 'static huffman coded' value
  270. ; ** Scratches X, returns the value in A **
  271. _2getval: ; X must be 0 when called!
  272.   ld a,1                                                    ; 3E 01
  273.   ld e,a                                                    ; 5F
  274. _2loop0:
  275.   sla d                                                     ; CB 22
  276.   jr nz,_2loop1                                              ; 20 04
  277.   call _2getDincHL
  278.   rl d ; Shift in C=1 (last bit marker)                     ; CB 12
  279.        ; bitstr initial value = $80 == empty
  280. _2loop1:
  281.   jr nc,_2getchk ; got 0-bit                                 ; 30 15
  282.   inc e                                                     ; 1C
  283.   ld b,a  ; save a                                          ; 47
  284.   ld a,(MaxGamma)                                           ; 3A CB DF
  285.   cp e                                                      ; BB
  286.   ld a,b  ; restore a                                       ; 78
  287.   jr nz,_2loop0                                              ; 20 ED
  288.   jr _2getchk                                                ; 18 0A
  289. ; getbits: Gets X bits from the stream
  290. ; ** Scratches X, returns the value in A **
  291. _2get1bit:
  292.   inc e                                                     ; 1C
  293. _2getbits:
  294.   sla d                                                     ; CB 22
  295.   jr nz,_2loop3                                              ; 20 04
  296.   call _2getDincHL
  297.   rl d ; Shift in C=1 (last bit marker)                     ; CB 12
  298.        ; bitstr initial value = $80 == empty
  299. _2loop3:
  300.   rla                                                       ; 17
  301. _2getchk:
  302.   dec e                                                     ; 1D
  303.   jr nz,_2getbits                                            ; 20 F4
  304. ;  or a  ; clear carry flag                                  ; B7
  305.   ret                                                       ; C9
  306.  
  307. _2getDincHL:
  308.  ld d,(hl)
  309. _2incHL:
  310.  bit 7,h
  311.  inc hl
  312.  ret nz
  313.  bit 7,h
  314.  ret z
  315.  in a,(6)
  316.  inc a
  317.  out (6),a
  318.  ret
  319. _2getAincHL:
  320.  ld a,(hl)
  321.  jp _2incHL
  322.  
  323. Program2End:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement