Guest User

Untitled

a guest
Jun 17th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.19 KB | None | 0 0
  1. ; HL = InPtr
  2. ; D = bitstr
  3. ; E = X
  4. ; BC = temps
  5. ; ****** Unpack pucrunch data ******
  6. ; Entry: HL = Source packed data
  7. ; DE = Destination for unpacked data
  8. Uncrunch:
  9. ld (OutPtr),de
  10. ; Read the file header & setup variables
  11. ld bc,6 ; 01 06 00
  12. add hl,bc ; 09
  13. ld a,(hl) ; 7E
  14. ld (escPu),a ; 32 C4 DF
  15. inc hl ; 23
  16. inc hl ; 23
  17. inc hl ; 23
  18. ld a,(hl) ; 7E
  19. ld (EscBits),a ; 32 C9 DF
  20. ld a,8 ; 3E 08 saved 1 byte
  21. sub (hl) ; 96
  22. ld (Esc8Bits),a ; 32 CA DF
  23. inc hl ; 23
  24. ld a,(hl) ; 7E
  25. ld (MaxGamma),a ; 32 CB DF
  26. ld a,8 ; 3E 08 saved 1 byte
  27. sub (hl) ; 96
  28. inc a ; 3C
  29. ld (Max8Gamma),a ; 32 CE DF
  30. inc hl ; 23
  31. ld a,(hl) ; 7E
  32. ld (Max1Gamma),a ; 32 CC DF
  33. add a,a ; 87
  34. dec a ; 3D
  35. ld (Max2Gamma),a ; 32 CD DF
  36. inc hl ; 23
  37. ld a,(hl) ; 7E
  38. ld (ExtraBits),a ; 32 CF DF
  39. inc hl ; 23
  40. inc hl ; 23
  41. inc hl ; 23
  42. ld a,(hl) ; 7E
  43. inc hl ; 23
  44. ld b,a ; 47
  45. ld de,tablePu ; 11 D0 DF
  46. ; Copy the RLE table (maximum of 31 bytes) to RAM
  47. inc b ; 04
  48. srl b ; CB 38
  49. jr nc,_orleloop ; 30 04
  50. _rleloop:
  51. ld a,(hl) ; 7E
  52. inc hl ; 23
  53. ld (de),a ; 12
  54. inc de ; 13
  55. _orleloop:
  56. ld a,(hl) ; 7E
  57. inc hl ; 23
  58. ld (de),a ; 12
  59. inc de ; 13
  60. djnz _rleloop ; 10 <offset> saved 1 byte
  61. ld d,$80 ; 16 80
  62. jr _main ; 18 2A
  63. _newesc:
  64. ld b,a ; 47
  65. ld a,(escPu) ; 3A C4 DF
  66. ld (regy),a ; 32 EF DF
  67. ld a,(EscBits) ; 3A C9 DF
  68. ld e,a ; 5F
  69. ld a,b ; 78
  70. inc e ; 1C
  71. call _getchk ; CD 3D 06
  72. ld (escPu),a ; 32 C4 DF
  73. ld a,(regy) ; 3A EF DF
  74. ; Fall through and get the rest of the bits.
  75. _noesc:
  76. ld b,a ; 47
  77. ld a,(Esc8Bits) ; 3A CA DF
  78. ld e,a ; 5F
  79. ld a,b ; 78
  80. inc e ; 1C
  81. call _getchk ; CD 3D 06
  82. ; Write out the escaped/normal byte
  83. ld bc,(OutPtr) ; ED 4B C5 DF
  84. ld (bc),a ; 02
  85. inc bc ; 03
  86. ld (OutPtr),bc ; ED 43 C5 DF
  87. ; Fall through and check the escape bits again
  88. _main:
  89. ld a,(EscBits) ; 3A C9 DF
  90. ld e,a ; 5F
  91. xor a ; A = 0 ; AF
  92. ld (regy),a ; 32 EF DF
  93. inc e ; 1C
  94. call _getchk ; X=2 -> X=0 ; CD 3D 06
  95. ld b,a ; 47
  96. ld a,(escPu) ; 3A C4 DF
  97. cp b ; B8
  98. ld a,b ; 78
  99. jr nz,_noesc ; Not the escape code -> get the rest of the byte ; 20 D8
  100. ; Fall through to packed code
  101. call _getval ; X=0 -> X=0 ; CD 1B 06
  102. ld (lzpos),a ; xstore - save the length for a ; 32 C7 DF
  103. ; later time
  104. srl a ; cmp #1 ; LEN == 2 ? (A is never 0) ; CB 3F
  105. jr nz,_lz77 ; LEN != 2 -> LZ77 ; jp -> jr -> saved 1 byte
  106. call _get1bit ; X=0 -> X=0 ; CD 33 06
  107. srl a ; bit -> C, A = 0 ; CB 3F
  108. jr nc,_lz77_2 ; A=0 -> LZPOS+1 LZ77, len=2 ; jp -> jr -> saved 1 byte
  109. ; e..e01
  110. call _get1bit ; X=0 -> X=0 ; CD 33 06
  111. srl a ; bit -> C, A = 0 ; CB 3F
  112. jr nc,_newesc ; e..e010 New Escape ; jp -> jr -> saved 1 byte
  113. ; e..e011 Short/Long RLE
  114. ld a,(regy) ; Y is 1 bigger than MSB loops ; 3A EF DF
  115. inc a ; 3C
  116. ld (regy),a ; 32 EF DF
  117. call _getval ; Y is 1, get len, X=0 -> X=0 ; CD 1B 06
  118. ld (lzpos),a ; xstore - Save length LSB ; 32 C7 DF
  119. ld c,a ; 4F
  120. ld a,(Max1Gamma) ; 3A CC DF
  121. ld b,a ; 47
  122. ld a,c ; 79
  123. cp b ; ** PARAMETER 63-64 -> C set, ; B8
  124. ; 64-64 -> C clear..
  125. jr c,_chrcode ; short RLE, get bytecode ; 38 12
  126. ; Otherwise it's long RLE
  127. _longrle:
  128. ld b,a ; 47
  129. ld a,(Max8Gamma) ; 3A CE DF
  130. ld e,a ; ** PARAMETER 111111xxxxxx ; 5F
  131. ld a,b ; 78
  132. call _getbits ; get 3/2/1 more bits to get a full byte, ; CD 34 06
  133. ; X=2 -> X=0
  134. ld (lzpos),a ; xstore - Save length LSB ; 32 C7 DF
  135. call _getval ; length MSB, X=0 -> X=0 ; CD 1B 06
  136. ld (regy),a ; Y is 1 bigger than MSB loops ; 32 EF DF
  137. _chrcode:
  138. call _getval ; Byte Code, X=0 -> X=0 ; CD 1B 06
  139. ld e,a ; 5F
  140. add a,(tablePu-1) % 256 ; C6 xx ; saved 1 byte
  141. ld c,a ; 4F
  142. ld a,(tablePu-1) / 256 ; 3E xx
  143. adc a,0 ; CE 00
  144. ld b,a ; 47
  145. ld a,e ; 7B
  146. cp 32 ; 31-32 -> C set, 32-32 -> C clear.. ; FE 20
  147. ld a,(bc) ; 0A
  148. jr c,_less32 ; 1..31 ; 38 06
  149. ; Not ranks 1..31, -> 11111 ° xxxxx (32..64), get byte..
  150. ld a,e ; get back the value (5 valid bits) ; 7B
  151. ld e,3 ; 1E 03
  152. call _getbits ; get 3 more bits to get a full byte, ; CD 34 06
  153. ; X=3 -> X=0
  154. _less32:
  155. push hl ; E5
  156. push af ; F5
  157. ld a,(lzpos) ; 3A C7 DF
  158. ld e,a ; xstore - get length LSB ; 5F
  159. ld b,e ; 43
  160. inc b ; adjust for cpx#$ff;bne -> bne ; 04
  161. ld a,(regy) ; 3A EF DF
  162. ld c,a ; 4F
  163. ld hl,(OutPtr) ; 2A C5 DF
  164. pop af ; F1
  165. _dorle:
  166. ld (hl),a ; 77
  167. inc hl ; 23
  168. djnz _dorle ; 10 nn ; saved 1 byte
  169. dec c ; 0D
  170. jr nz,_dorle ; Y was 1 bigger than wanted originally ; 20 F8
  171. ld (OutPtr),hl ; 22 C5 DF
  172. pop hl ; E1
  173. jp _main ; C3 2D 05
  174. _lz77:
  175. call _getval ; X=0 -> X=0 ; CD 1B 06
  176. ld b,a ; 47
  177. ld a,(Max2Gamma) ; 3A CD DF
  178. cp b ; end of file ? ; B8
  179. ret z ; yes, exit ; C8
  180. ld a,(ExtraBits) ; ** PARAMETER (more bits to get) ; 3A CF DF
  181. ld e,a ; 5F
  182. ld a,b ; 78
  183. dec a ; subtract 1 (1..126 -> 0..125) ; 3D
  184. inc e ; 1C
  185. call _getchk ;f ; clears Carry, X=0 -> X=0 ; CD 3D 06
  186. _lz77_2:
  187. ld (lzpos+1),a ; offset MSB ; 32 C8 DF
  188. ld e,8 ; 1E 08
  189. call _getbits ; clears Carry, X=8 -> X=0 ; CD 34 06
  190. ; Note: Already eor:ed in the compressor..
  191. ld b,a ; 47
  192. ld a,(lzpos) ; 3A C7 DF
  193. ld e,a ; xstore - LZLEN (read before it's ; 5F
  194. ; overwritten)
  195. ld a,(OutPtr) ; 3A C5 DF
  196. add a,b ; -offset -1 + curpos (C is clear) ; 80
  197. ld (lzpos),a ; 32 C7 DF
  198. ld a,(lzpos+1) ; 3A C8 DF
  199. ld b,a ; 47
  200. ld a,(OutPtr+1) ; 3A C6 DF
  201. ccf ; 3F
  202. sbc a,b ; 98
  203. ld (lzpos+1),a ; copy X+1 number of chars from LZPOS to ; 32 C8 DF
  204. ; OUTPOS
  205. inc e ; adjust for cpx#$ff;bne -> bne ; 1C
  206. ; Write decompressed bytes out to RAM
  207. ld b,e ; 43
  208. push de ; D5
  209. push hl ; E5
  210. ld hl,(lzpos) ; 2A C7 DF
  211. ld de,(OutPtr) ; ED 5B C5 DF
  212. ld a,b ; 78
  213. or a ; Is it zero? ; B7
  214. jr z,_zero ; yes ; 28 19
  215. inc b ; 04
  216. srl b ; CB 38
  217. jr nc,_olzloop ; 30 04
  218. _lzloop:
  219. ld a,(hl) ; 7E
  220. inc hl ; 23
  221. ld (de),a ; 12
  222. inc de ; 13
  223. _olzloop:
  224. ld a,(hl) ; Note: Must be copied forward ; 7E
  225. inc hl ; 23
  226. ld (de),a ; 12
  227. inc de ; 13
  228. djnz _lzloop ; 10 <offset> saved 1 byte
  229. ld (OutPtr),de ; ED 53 C5 DF
  230. pop hl ; E1
  231. pop de ; D1
  232. jp _main ; C3 2D 05
  233. _zero:
  234. ld b,128 ; 06 80
  235. jr _lzloop ; 18 E8
  236. ; getval : Gets a 'static huffman coded' value
  237. ; ** Scratches X, returns the value in A **
  238. _getval: ; X must be 0 when called!
  239. ld a,1 ; 3E 01
  240. ld e,a ; 5F
  241. _loop0:
  242. sla d ; CB 22
  243. jr nz,_loop1 ; 20 04
  244. ld d,(hl) ; 56
  245. inc hl ; 23
  246. rl d ; Shift in C=1 (last bit marker) ; CB 12
  247. ; bitstr initial value = $80 == empty
  248. _loop1:
  249. jr nc,_getchk ; got 0-bit ; 30 15
  250. inc e ; 1C
  251. ld b,a ; save a ; 47
  252. ld a,(MaxGamma) ; 3A CB DF
  253. cp e ; BB
  254. ld a,b ; restore a ; 78
  255. jr nz,_loop0 ; 20 ED
  256. jr _getchk ; 18 0A
  257. ; getbits: Gets X bits from the stream
  258. ; ** Scratches X, returns the value in A **
  259. _get1bit:
  260. inc e ; 1C
  261. _getbits:
  262. sla d ; CB 22
  263. jr nz,_loop3 ; 20 04
  264. ld d,(hl) ; 56
  265. inc hl ; 23
  266. rl d ; Shift in C=1 (last bit marker) ; CB 12
  267. ; bitstr initial value = $80 == empty
  268. _loop3:
  269. rla ; 17
  270. _getchk:
  271. dec e ; 1D
  272. jr nz,_getbits ; 20 F4
  273. ; or a ; clear carry flag ; B7
  274. ret ; C9
Add Comment
Please, Sign In to add comment