Advertisement
Guest User

Superfast Kos

a guest
Aug 8th, 2015
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; ||||||||||||||| S U B R O U T I N E |||||||||||||||||||||||||||||||||||||||
  2. ; ---------------------------------------------------------------------------
  3. ; KOSINSKI DECOMPRESSION PROCEDURE
  4. ; (sometimes called KOZINSKI decompression)
  5. ;
  6. ; ARGUMENTS:
  7. ; a0 = source address
  8. ; a1 = destination address
  9. ;
  10. ; For format explanation see http://info.sonicretro.org/Kosinski_compression
  11. ; New faster version by written by vladikcomper, with additional improvements by
  12. ; MarkeyJester and Flamewing
  13. ; ---------------------------------------------------------------------------
  14. _Kos_UseLUT := 1
  15. _Kos_LoopUnroll := 3
  16. _Kos_ExtremeUnrolling := 1
  17.  
  18. _Kos_RunBitStream macro
  19.     dbra    d2,.skip
  20.     moveq   #7,d2                   ; Set repeat count to 8.
  21.     move.b  d1,d0                   ; Use the remaining 8 bits.
  22.     swap    d3                      ; Have all 16 bits been used up?
  23.     bpl.s   .skip                   ; Branch if not.
  24.     move.b  (a0)+,d0                ; Get desc field low-byte.
  25.     move.b  (a0)+,d1                ; Get desc field hi-byte.
  26.     if _Kos_UseLUT==1
  27.     move.b  (a4,d0.w),d0            ; Invert bit order...
  28.     move.b  (a4,d1.w),d1            ; ... for both bytes.
  29.     endif
  30. .skip
  31.     endm
  32.  
  33. _Kos_ReadBit macro
  34.     if _Kos_UseLUT==1
  35.     add.b   d0,d0                   ; Get a bit from the bitstream.
  36.     else
  37.     lsr.b   #1,d0                   ; Get a bit from the bitstream.
  38.     endif
  39.     endm
  40. ; ===========================================================================
  41. ; KozDec_193A:
  42. KosDec:
  43.     moveq   #(1<<_Kos_LoopUnroll)-1,d7
  44.     if _Kos_UseLUT==1
  45.     moveq   #0,d0
  46.     moveq   #0,d1
  47.     lea KosDec_ByteMap(pc),a4       ; Load LUT pointer.
  48.     endif
  49.     move.b  (a0)+,d0                ; Get desc field low-byte.
  50.     move.b  (a0)+,d1                ; Get desc field hi-byte.
  51.     if _Kos_UseLUT==1
  52.     move.b  (a4,d0.w),d0            ; Invert bit order...
  53.     move.b  (a4,d1.w),d1            ; ... for both bytes.
  54.     endif
  55.     moveq   #7,d2                   ; Set repeat count to 8.
  56.     moveq   #-1,d3                  ; d3 will be desc field switcher.
  57.     clr.w   d3                      ; Low word is zero.
  58.     bra.s   .FetchNewCode
  59. ; ---------------------------------------------------------------------------
  60. .FetchCodeLoop:
  61.     ; Code 1 (Uncompressed byte).
  62.     _Kos_RunBitStream
  63.     move.b  (a0)+,(a1)+
  64.  
  65. .FetchNewCode:
  66.     _Kos_ReadBit
  67.     bcs.s   .FetchCodeLoop          ; If code = 1, branch.
  68.  
  69.     ; Codes 00 and 01.
  70.     moveq   #-1,d5
  71.     lea (a1),a5
  72.     _Kos_RunBitStream
  73.     if _Kos_ExtremeUnrolling==1
  74.     _Kos_ReadBit
  75.     bcs.w   .Code_01
  76.  
  77.     ; Code 00 (Dictionary ref. short).
  78.     _Kos_RunBitStream
  79.     _Kos_ReadBit
  80.     bcs.s   .Copy45
  81.     _Kos_RunBitStream
  82.     _Kos_ReadBit
  83.     bcs.s   .Copy3
  84.     _Kos_RunBitStream
  85.     move.b  (a0)+,d5                ; d5 = displacement.
  86.     adda.w  d5,a5
  87.     move.b  (a5)+,(a1)+
  88.     move.b  (a5)+,(a1)+
  89.     bra.s   .FetchNewCode
  90. ; ---------------------------------------------------------------------------
  91. .Copy3:
  92.     _Kos_RunBitStream
  93.     move.b  (a0)+,d5                ; d5 = displacement.
  94.     adda.w  d5,a5
  95.     move.b  (a5)+,(a1)+
  96.     move.b  (a5)+,(a1)+
  97.     move.b  (a5)+,(a1)+
  98.     bra.w   .FetchNewCode
  99. ; ---------------------------------------------------------------------------
  100. .Copy45:
  101.     _Kos_RunBitStream
  102.     _Kos_ReadBit
  103.     bcs.s   .Copy5
  104.     _Kos_RunBitStream
  105.     move.b  (a0)+,d5                ; d5 = displacement.
  106.     adda.w  d5,a5
  107.     move.b  (a5)+,(a1)+
  108.     move.b  (a5)+,(a1)+
  109.     move.b  (a5)+,(a1)+
  110.     move.b  (a5)+,(a1)+
  111.     bra.w   .FetchNewCode
  112. ; ---------------------------------------------------------------------------
  113. .Copy5:
  114.     _Kos_RunBitStream
  115.     move.b  (a0)+,d5                ; d5 = displacement.
  116.     adda.w  d5,a5
  117.     move.b  (a5)+,(a1)+
  118.     move.b  (a5)+,(a1)+
  119.     move.b  (a5)+,(a1)+
  120.     move.b  (a5)+,(a1)+
  121.     move.b  (a5)+,(a1)+
  122.     bra.w   .FetchNewCode
  123. ; ---------------------------------------------------------------------------
  124.     else
  125.     moveq   #0,d4                   ; d4 will contain copy count.
  126.     _Kos_ReadBit
  127.     bcs.s   .Code_01
  128.  
  129.     ; Code 00 (Dictionary ref. short).
  130.     _Kos_RunBitStream
  131.     _Kos_ReadBit
  132.     addx.w  d4,d4
  133.     _Kos_RunBitStream
  134.     _Kos_ReadBit
  135.     addx.w  d4,d4
  136.     _Kos_RunBitStream
  137.     move.b  (a0)+,d5                ; d5 = displacement.
  138.  
  139. .StreamCopy:
  140.     adda.w  d5,a5
  141.     move.b  (a5)+,(a1)+             ; Do 1 extra copy (to compensate +1 to copy counter).
  142.  
  143. .copy:
  144.     move.b  (a5)+,(a1)+
  145.     dbra    d4,.copy
  146.     bra.w   .FetchNewCode
  147.     endif
  148. ; ---------------------------------------------------------------------------
  149. .Code_01:
  150.     moveq   #0,d4                   ; d4 will contain copy count.
  151.     ; Code 01 (Dictionary ref. long / special).
  152.     _Kos_RunBitStream
  153.     move.b  (a0)+,d6                ; d6 = %LLLLLLLL.
  154.     move.b  (a0)+,d4                ; d4 = %HHHHHCCC.
  155.     move.b  d4,d5                   ; d5 = %11111111 HHHHHCCC.
  156.     lsl.w   #5,d5                   ; d5 = %111HHHHH CCC00000.
  157.     move.b  d6,d5                   ; d5 = %111HHHHH LLLLLLLL.
  158.     if _Kos_LoopUnroll==3
  159.     and.w   d7,d4                   ; d4 = %00000CCC.
  160.     else
  161.     andi.w  #7,d4
  162.     endif
  163.     bne.s   .StreamCopy             ; if CCC=0, branch.
  164.  
  165.     ; special mode (extended counter)
  166.     move.b  (a0)+,d4                ; Read cnt
  167.     beq.s   .Quit                   ; If cnt=0, quit decompression.
  168.     subq.b  #1,d4
  169.     beq.w   .FetchNewCode           ; If cnt=1, fetch a new code.
  170.  
  171.     adda.w  d5,a5
  172.     move.b  (a5)+,(a1)+             ; Do 1 extra copy (to compensate +1 to copy counter).
  173.     move.w  d4,d6
  174.     not.w   d6
  175.     and.w   d7,d6
  176.     add.w   d6,d6
  177.     lsr.w   #_Kos_LoopUnroll,d4
  178.     jmp .largecopy(pc,d6.w)
  179. ; ---------------------------------------------------------------------------
  180. .largecopy:
  181.     rept (1<<_Kos_LoopUnroll)
  182.     move.b  (a5)+,(a1)+
  183.     endm
  184.     dbra    d4,.largecopy
  185.     bra.w   .FetchNewCode
  186. ; ---------------------------------------------------------------------------
  187.     if _Kos_ExtremeUnrolling==1
  188. .StreamCopy:
  189.     adda.w  d5,a5
  190.     move.b  (a5)+,(a1)+             ; Do 1 extra copy (to compensate +1 to copy counter).
  191.     if _Kos_LoopUnroll==3
  192.     eor.w   d7,d4
  193.     else
  194.     eori.w  #7,d4
  195.     endif
  196.     add.w   d4,d4
  197.     jmp .mediumcopy(pc,d4.w)
  198. ; ---------------------------------------------------------------------------
  199. .mediumcopy:
  200.     rept 8
  201.     move.b  (a5)+,(a1)+
  202.     endm
  203.     bra.w   .FetchNewCode
  204.     endif
  205. ; ---------------------------------------------------------------------------
  206. .Quit:
  207.     rts                             ; End of function KosDec.
  208. ; ===========================================================================
  209.     if _Kos_UseLUT==1
  210. KosDec_ByteMap:
  211.     dc.b    $00,$80,$40,$C0,$20,$A0,$60,$E0,$10,$90,$50,$D0,$30,$B0,$70,$F0
  212.     dc.b    $08,$88,$48,$C8,$28,$A8,$68,$E8,$18,$98,$58,$D8,$38,$B8,$78,$F8
  213.     dc.b    $04,$84,$44,$C4,$24,$A4,$64,$E4,$14,$94,$54,$D4,$34,$B4,$74,$F4
  214.     dc.b    $0C,$8C,$4C,$CC,$2C,$AC,$6C,$EC,$1C,$9C,$5C,$DC,$3C,$BC,$7C,$FC
  215.     dc.b    $02,$82,$42,$C2,$22,$A2,$62,$E2,$12,$92,$52,$D2,$32,$B2,$72,$F2
  216.     dc.b    $0A,$8A,$4A,$CA,$2A,$AA,$6A,$EA,$1A,$9A,$5A,$DA,$3A,$BA,$7A,$FA
  217.     dc.b    $06,$86,$46,$C6,$26,$A6,$66,$E6,$16,$96,$56,$D6,$36,$B6,$76,$F6
  218.     dc.b    $0E,$8E,$4E,$CE,$2E,$AE,$6E,$EE,$1E,$9E,$5E,$DE,$3E,$BE,$7E,$FE
  219.     dc.b    $01,$81,$41,$C1,$21,$A1,$61,$E1,$11,$91,$51,$D1,$31,$B1,$71,$F1
  220.     dc.b    $09,$89,$49,$C9,$29,$A9,$69,$E9,$19,$99,$59,$D9,$39,$B9,$79,$F9
  221.     dc.b    $05,$85,$45,$C5,$25,$A5,$65,$E5,$15,$95,$55,$D5,$35,$B5,$75,$F5
  222.     dc.b    $0D,$8D,$4D,$CD,$2D,$AD,$6D,$ED,$1D,$9D,$5D,$DD,$3D,$BD,$7D,$FD
  223.     dc.b    $03,$83,$43,$C3,$23,$A3,$63,$E3,$13,$93,$53,$D3,$33,$B3,$73,$F3
  224.     dc.b    $0B,$8B,$4B,$CB,$2B,$AB,$6B,$EB,$1B,$9B,$5B,$DB,$3B,$BB,$7B,$FB
  225.     dc.b    $07,$87,$47,$C7,$27,$A7,$67,$E7,$17,$97,$57,$D7,$37,$B7,$77,$F7
  226.     dc.b    $0F,$8F,$4F,$CF,$2F,$AF,$6F,$EF,$1F,$9F,$5F,$DF,$3F,$BF,$7F,$FF
  227.     endif
  228. ; ===========================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement