Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. ; ----------------------------------------------------------------------------------------
  3. ; The decruncher jsr:s to the getCrunchedByte address when it wants to
  4. ; read a crunched byte. This subroutine has to preserve x and y register
  5. ; and must not modify the state of the carry flag.
  6. ; ----------------------------------------------------------------------------------------
  7. getCrunchedByte:
  8. {
  9.             lda DecrunchDataLo
  10.             bne skipHi
  11.             dec DecrunchDataHi
  12. skipHi:
  13.             dec DecrunchDataLo
  14. .export DecrunchDataLo = * + 1
  15. .export DecrunchDataHi = * + 2
  16.             lda $1234
  17.  
  18.             rts
  19. }
  20.  
  21.  
  22. ;#define LITERAL_SEQUENCES_NOT_USED
  23.  
  24.  
  25. ; ----------------------------------------------------------------------------------------
  26. ; this 156 byte table area may be relocated. It may also be clobbered
  27. ; by other data between decrunches.
  28. ; ----------------------------------------------------------------------------------------
  29. DecrunchTable:
  30.             .buf 156       
  31.  
  32. DecrunchTableBase = DecrunchTable
  33. DecrunchTableLo = DecrunchTable + 52
  34. DecrunchTableHi = DecrunchTable + 104
  35.  
  36.  
  37.  
  38. ; ----------------------------------------------------------------------------------------
  39. .export exoDecrunch:
  40. {
  41.             ; -------------------------------------------------------------------
  42.             ; init zeropage, x and y regs. (12 bytes)
  43.             ;
  44.             ldy #0
  45.             ldx #3
  46.             {
  47.                 jsr getCrunchedByte
  48.                 sta ExoBitBuf - 1,x
  49.                 dex
  50.                 bne _cont
  51.             }
  52.  
  53.             ; -------------------------------------------------------------------
  54.             ; calculate tables (50 bytes)
  55.             ; x and y must be #0 when entering
  56.             ;
  57. nextone:
  58.             inx
  59.             tya
  60.             and #$0f
  61.             beq shortcut
  62.        
  63.             txa                         ; this clears reg a
  64.             lsr                         ; and sets the carry flag
  65.             ldx DecrunchTableBase-1,y
  66.             {
  67.                 rol
  68.                 rol ExoBitsHi
  69.                 dex
  70.                 bpl _cont               ; c = 0 after this (rol ExoBitsHi)
  71.             }
  72.        
  73.             adc DecrunchTableLo-1,y
  74.             tax
  75.        
  76.             lda ExoBitsHi
  77.             adc DecrunchTableHi-1,y
  78. shortcut:
  79.             sta DecrunchTableHi,y
  80.             txa
  81.             sta DecrunchTableLo,y
  82.        
  83.             ldx #4
  84.             jsr get_bits        ; clears x-reg.
  85.             sta DecrunchTableBase,y
  86.             iny
  87.             cpy #52
  88.             bne nextone
  89.             ldy #0
  90.             beq begin
  91.            
  92.             ; -------------------------------------------------------------------
  93.             ; get bits (29 bytes)
  94.             ;
  95.             ; args:
  96.             ;   x = number of bits to get
  97.             ; returns:
  98.             ;   a = #bits_lo
  99.             ;   x = #0
  100.             ;   c = 0
  101.             ;   z = 1
  102.             ;   ExoBitsHi = #bits_hi
  103.             ; notes:
  104.             ;   y is untouched
  105.             ; -------------------------------------------------------------------
  106. get_bits:
  107.             lda #$00
  108.             sta ExoBitsHi
  109.             cpx #$01
  110.             bcc bits_done
  111. bits_next:
  112.             lsr ExoBitBuf
  113.             bne ok
  114.             pha
  115. literal_get_byte:
  116.             jsr getCrunchedByte
  117.             bcc literal_byte_gotten
  118.             ror
  119.             sta ExoBitBuf
  120.             pla
  121. ok:
  122.             rol
  123.             rol ExoBitsHi
  124.             dex
  125.             bne bits_next
  126. bits_done:
  127.             rts
  128.  
  129.             ; -------------------------------------------------------------------
  130.             ; main copy loop (18(16) bytes)
  131.             ;
  132. copy_next_hi:
  133.             dex
  134.             dec ExoDestHi
  135.             dec ExoSrcHi
  136. copy_next:
  137.             dey
  138. #ifndef LITERAL_SEQUENCES_NOT_USED
  139.             bcc literal_get_byte
  140. #endif
  141.             lda (ExoSrcLo),y
  142. literal_byte_gotten:
  143.             sta (ExoDestLo),y
  144. copy_start:
  145.             tya
  146.             bne copy_next
  147. begin:
  148.             txa
  149.             bne copy_next_hi
  150.  
  151.             ; -------------------------------------------------------------------
  152.             ; decruncher entry point, needs calculated tables (21(13) bytes)
  153.             ; x and y must be #0 when entering
  154.             ;
  155. #ifndef LITERAL_SEQUENCES_NOT_USED
  156.             inx
  157.             jsr get_bits
  158.             tay
  159.             bne literal_start1
  160. #else
  161.             dey
  162. #endif
  163. begin2:
  164.             inx
  165.             jsr bits_next
  166.             lsr
  167.             iny
  168.             bcc begin2
  169. #ifdef LITERAL_SEQUENCES_NOT_USED
  170.             beq literal_start
  171. #endif
  172.             cpy #$11
  173. #ifndef LITERAL_SEQUENCES_NOT_USED
  174.             bcc sequence_start
  175.             beq bits_done
  176.            
  177.             ; -------------------------------------------------------------------
  178.             ; literal sequence handling (13(2) bytes)
  179.             ;
  180.             ldx #$10
  181.             jsr get_bits
  182. literal_start1:
  183.             sta <ExoLenLo
  184.             ldx <ExoBitsHi
  185.             ldy #0
  186.             bcc literal_start
  187. sequence_start:
  188. #else
  189.             bcs bits_done
  190. #endif
  191.             ; -------------------------------------------------------------------
  192.             ; calulate length of sequence (zp_len) (11 bytes)
  193.             ;
  194.             ldx DecrunchTableBase - 1,y
  195.             jsr get_bits
  196.             adc DecrunchTableLo - 1,y   ; we have now calculated ExoLenLo
  197.             sta ExoLenLo
  198.            
  199.             ; -------------------------------------------------------------------
  200.             ; now do the hibyte of the sequence length calculation (6 bytes)
  201.             lda ExoBitsHi
  202.             adc DecrunchTableHi - 1,y   ; c = 0 after this.
  203.             pha
  204.  
  205.             ; -------------------------------------------------------------------
  206.             ; here we decide what offset table to use (20 bytes)
  207.             ; x is 0 here
  208.             ;
  209.             bne nots123
  210.             ldy ExoLenLo
  211.             cpy #$04
  212.             bcc size123
  213. nots123:
  214.             ldy #$03
  215. size123:
  216.             ldx TableBit - 1,y
  217.             jsr get_bits
  218.             adc TableOffset - 1,y   ; c = 0 after this.
  219.             tay         ; 1 <= y <= 52 here
  220.            
  221.             ; -------------------------------------------------------------------
  222.             ; Here we do the dest_lo -= len_lo subtraction to prepare zp_dest
  223.             ; but we do it backwards:   a - b == (b - a - 1) ^ ~0 (C-syntax)
  224.             ; (16(16) bytes)
  225.            
  226.             lda ExoLenLo
  227. literal_start:          ; literal enters here with y = 0, c = 1
  228.             sbc ExoDestLo
  229.             bcc noborrow
  230.             dec ExoDestHi
  231. noborrow:
  232.             eor #$ff
  233.             sta ExoDestLo
  234.             cpy #$01        ; y < 1 then literal
  235. #ifndef LITERAL_SEQUENCES_NOT_USED
  236.             bcc pre_copy
  237. #else
  238.             bcc literal_get_byte
  239. #endif
  240.  
  241.             ; -------------------------------------------------------------------
  242.             ; calulate absolute offset (zp_src) (27 bytes)
  243.             ;
  244.             ldx DecrunchTableBase,y
  245.             jsr get_bits;
  246.             adc DecrunchTableLo,y
  247.             bcc skipcarry
  248.             inc ExoBitsHi
  249.             clc
  250. skipcarry:
  251.             adc ExoDestLo
  252.             sta ExoSrcLo
  253.             lda ExoBitsHi
  254.             adc DecrunchTableHi,y
  255.             adc ExoDestHi
  256.             sta ExoSrcHi
  257.            
  258.             ; -------------------------------------------------------------------
  259.             ; prepare for copy loop (8(6) bytes)
  260.             ;
  261.             pla
  262.             tax
  263. #ifndef LITERAL_SEQUENCES_NOT_USED
  264.             sec
  265. pre_copy:
  266.             ldy <ExoLenLo
  267.             jmp copy_start
  268. #else
  269.             ldy <ExoLenLo
  270.             bcc copy_start
  271. #endif
  272.             ; -------------------------------------------------------------------
  273.             ; two small static tables (6(6) bytes)
  274.             ;
  275. TableBit:
  276.             .byte 2, 4, 4
  277. TableOffset:
  278.             .byte 48, 32, 16
  279.  
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement