Advertisement
Guest User

An Optimized Kosinski decompressor for 68k (AS version)

a guest
Nov 5th, 2013
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; ===========================================================================
  2. ; ---------------------------------------------------------------------------
  3. ; Kosinski decompression routine
  4. ;
  5. ; Created by vladikcomper
  6. ; Special thanks to flamewing and MarkeyJester
  7. ; Modified for AS by MainMemory
  8. ; ---------------------------------------------------------------------------
  9.  
  10. _Kos_RunBitStream macro
  11.     dbf d2,+
  12.     moveq   #7,d2
  13.     move.b  d1,d0
  14.     swap    d3
  15.     bpl.s   +
  16.     move.b  (a0)+,d0            ; get desc. bitfield
  17.     move.b  (a0)+,d1            ;
  18.     move.b  (a4,d0.w),d0            ; reload converted desc. bitfield from a LUT
  19.     move.b  (a4,d1.w),d1            ;
  20. +
  21.     endm
  22. ; ---------------------------------------------------------------------------
  23.  
  24. KosDec:
  25.     moveq   #7,d7
  26.     moveq   #0,d0
  27.     moveq   #0,d1
  28.     lea KosDec_ByteMap(pc),a4
  29.     move.b  (a0)+,d0            ; get desc field low-byte
  30.     move.b  (a0)+,d1            ; get desc field hi-byte
  31.     move.b  (a4,d0.w),d0            ; reload converted desc. bitfield from a LUT
  32.     move.b  (a4,d1.w),d1            ;
  33.     moveq   #7,d2               ; set repeat count to 8
  34.     moveq   #-1,d3              ; d3 will be desc field switcher
  35.     clr.w   d3              ;
  36.     bra.s   KosDec_FetchNewCode
  37.  
  38. KosDec_FetchCodeLoop:
  39.     ; code 1 (Uncompressed byte)
  40.     _Kos_RunBitStream
  41.     move.b  (a0)+,(a1)+
  42.  
  43. KosDec_FetchNewCode:
  44.     add.b   d0,d0               ; get a bit from the bitstream
  45.     bcs.s   KosDec_FetchCodeLoop        ; if code = 0, branch
  46.  
  47.     ; codes 00 and 01
  48.     _Kos_RunBitStream
  49.     moveq   #0,d4               ; d4 will contain copy count
  50.     add.b   d0,d0               ; get a bit from the bitstream
  51.     bcs.s   KosDec_Code_01
  52.  
  53.     ; code 00 (Dictionary ref. short)
  54.     _Kos_RunBitStream
  55.     add.b   d0,d0               ; get a bit from the bitstream
  56.     addx.w  d4,d4
  57.     _Kos_RunBitStream
  58.     add.b   d0,d0               ; get a bit from the bitstream
  59.     addx.w  d4,d4
  60.     _Kos_RunBitStream
  61.     moveq   #-1,d5
  62.     move.b  (a0)+,d5            ; d5 = displacement
  63.  
  64. KosDec_StreamCopy:
  65.     lea (a1,d5),a3
  66.     move.b  (a3)+,(a1)+         ; do 1 extra copy (to compensate for +1 to copy counter)
  67.  
  68. KosDec_copy:
  69.     move.b  (a3)+,(a1)+
  70.     dbf d4,KosDec_copy
  71.     bra.w   KosDec_FetchNewCode
  72. ; ---------------------------------------------------------------------------
  73. KosDec_Code_01:
  74.     ; code 01 (Dictionary ref. long / special)
  75.     _Kos_RunBitStream
  76.     move.b  (a0)+,d6            ; d6 = %LLLLLLLL
  77.     move.b  (a0)+,d4            ; d4 = %HHHHHCCC
  78.     moveq   #-1,d5
  79.     move.b  d4,d5               ; d5 = %11111111 HHHHHCCC
  80.     lsl.w   #5,d5               ; d5 = %111HHHHH CCC00000
  81.     move.b  d6,d5               ; d5 = %111HHHHH LLLLLLLL
  82.     and.w   d7,d4               ; d4 = %00000CCC
  83.     bne.s   KosDec_StreamCopy       ; if CCC=0, branch
  84.  
  85.     ; special mode (extended counter)
  86.     move.b  (a0)+,d4            ; read cnt
  87.     beq.s   KosDec_Quit         ; if cnt=0, quit decompression
  88.     subq.b  #1,d4
  89.     beq.w   KosDec_FetchNewCode     ; if cnt=1, fetch a new code
  90.  
  91.     lea (a1,d5),a3
  92.     move.b  (a3)+,(a1)+         ; do 1 extra copy (to compensate for +1 to copy counter)
  93.     move.w  d4,d6
  94.     not.w   d6
  95.     and.w   d7,d6
  96.     add.w   d6,d6
  97.     lsr.w   #3,d4
  98.     jmp KosDec_largecopy(pc,d6.w)
  99.  
  100. KosDec_largecopy:
  101.     rept 8
  102.     move.b  (a3)+,(a1)+
  103.     endm
  104.     dbf d4,KosDec_largecopy
  105.     bra.w   KosDec_FetchNewCode
  106.  
  107. KosDec_Quit:
  108.     rts
  109.  
  110. ; ---------------------------------------------------------------------------
  111. ; A look-up table to invert bits order in desc. field bytes
  112. ; ---------------------------------------------------------------------------
  113.  
  114. KosDec_ByteMap:
  115.     dc.b    $00,$80,$40,$C0,$20,$A0,$60,$E0,$10,$90,$50,$D0,$30,$B0,$70,$F0
  116.     dc.b    $08,$88,$48,$C8,$28,$A8,$68,$E8,$18,$98,$58,$D8,$38,$B8,$78,$F8
  117.     dc.b    $04,$84,$44,$C4,$24,$A4,$64,$E4,$14,$94,$54,$D4,$34,$B4,$74,$F4
  118.     dc.b    $0C,$8C,$4C,$CC,$2C,$AC,$6C,$EC,$1C,$9C,$5C,$DC,$3C,$BC,$7C,$FC
  119.     dc.b    $02,$82,$42,$C2,$22,$A2,$62,$E2,$12,$92,$52,$D2,$32,$B2,$72,$F2
  120.     dc.b    $0A,$8A,$4A,$CA,$2A,$AA,$6A,$EA,$1A,$9A,$5A,$DA,$3A,$BA,$7A,$FA
  121.     dc.b    $06,$86,$46,$C6,$26,$A6,$66,$E6,$16,$96,$56,$D6,$36,$B6,$76,$F6
  122.     dc.b    $0E,$8E,$4E,$CE,$2E,$AE,$6E,$EE,$1E,$9E,$5E,$DE,$3E,$BE,$7E,$FE
  123.     dc.b    $01,$81,$41,$C1,$21,$A1,$61,$E1,$11,$91,$51,$D1,$31,$B1,$71,$F1
  124.     dc.b    $09,$89,$49,$C9,$29,$A9,$69,$E9,$19,$99,$59,$D9,$39,$B9,$79,$F9
  125.     dc.b    $05,$85,$45,$C5,$25,$A5,$65,$E5,$15,$95,$55,$D5,$35,$B5,$75,$F5
  126.     dc.b    $0D,$8D,$4D,$CD,$2D,$AD,$6D,$ED,$1D,$9D,$5D,$DD,$3D,$BD,$7D,$FD
  127.     dc.b    $03,$83,$43,$C3,$23,$A3,$63,$E3,$13,$93,$53,$D3,$33,$B3,$73,$F3
  128.     dc.b    $0B,$8B,$4B,$CB,$2B,$AB,$6B,$EB,$1B,$9B,$5B,$DB,$3B,$BB,$7B,$FB
  129.     dc.b    $07,$87,$47,$C7,$27,$A7,$67,$E7,$17,$97,$57,$D7,$37,$B7,$77,$F7
  130.     dc.b    $0F,$8F,$4F,$CF,$2F,$AF,$6F,$EF,$1F,$9F,$5F,$DF,$3F,$BF,$7F,$FF
  131.  
  132. ; ===========================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement