Advertisement
Reiko_Fujimoto

S3K Kos_Decomp Fix

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