home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / enterprs / c128 / util / sfxsrc.arc / RECONST.A < prev    next >
Encoding:
Text File  |  1990-02-12  |  9.9 KB  |  426 lines

  1. ; reconst.a
  2. ;──────────────────────────────────────────────────────────────────────────────
  3. ; LHARChive self extraction module for C64/C128 - Copyright 1990 - Chris Smeets
  4. ;──────────────────────────────────────────────────────────────────────────────
  5. ; Reconstruct the frequency table when a frequency count overflows. This is
  6. ; a rare occurrence. Its called only when a given character appears more than
  7. ; 32768 times in a file.
  8. ;
  9. ; Since the size of a self extract module is limited, its not inconcievable
  10. ; that this code would never get called.
  11.  
  12.  
  13.             INSTXT "sfx.i"
  14.  
  15.             EXT freq
  16.             EXT son
  17.             EXT prnt
  18.  
  19. ;───────────────────────────────────────────────────────────
  20. ; void reconst()
  21. ; {
  22. ;     static  int i, j, k;
  23. ;     static  unsigned f, l;
  24. ;     j = 0;
  25. ;     for (i = 0; i < T; i++) {
  26. ;         if (son[i] >= T) {
  27. ;             freq[j] = (freq[i] + 1) / 2;
  28. ;             son[j] = son[i];
  29. ;             j++;
  30. ;         }
  31. ;     }
  32. ;     for (i = 0, j = N_CHAR; j < T; i += 2, j++) {
  33. ;         k = i + 1;
  34. ;         f = freq[j] = freq[i] + freq[k];
  35. ;         for (k=j-1; f<freq[k]; k--)
  36. ;             ;
  37. ;         k++;
  38. ;         l = (j-k)*2;
  39. ;         memmove(&freq[k+1], &freq[k], l);
  40. ;         freq[k] = f;
  41. ;         memmove(&son[k+1], &son[k], l);
  42. ;         son[k] = i;
  43. ;     }
  44. ;     for (i = 0; i < T; i++) {
  45. ;         if ((k = son[i]) >= T)
  46. ;             prnt[k] = i;
  47. ;         else
  48. ;             prnt[k] = prnt[k + 1] = i;
  49. ;     }
  50. ; }
  51. ;──────────────────────────────────────────────────────────
  52.  
  53.             PUBLIC Reconst
  54.  
  55. I           EQU T0
  56. J           EQU I+2
  57. K           EQU I+4
  58. EF          EQU I+6
  59. L           EQU I+8
  60. SON_I       EQU I+10
  61. P1          EQU I+12
  62. P2          EQU I+14
  63.  
  64. Reconst     ldy #1
  65.             ldx #0                  ; j=0
  66.             stx J
  67.             stx J+1
  68.             stx I                   ; for(i=0;
  69.             stx I+1
  70.  
  71. for1        lda I                   ;         i<T;
  72.             cmp #<Tstar2
  73.             lda I+1
  74.             sbc #>Tstar2
  75.             bcc if1
  76.             jmp endfor1
  77.  
  78. if1         clc                     ; if(son[i] >= T)
  79.             lda #<son
  80.             adc I
  81.             sta SON_I
  82.             lda #>son
  83.             adc I+1
  84.             sta SON_I+1
  85.  
  86.             lda (SON_I,x)
  87.             cmp #<T
  88.             lda (SON_I),y
  89.             sbc #>T
  90.             bcc endif1              ; Its not
  91.  
  92.             clc                     ;     freq[j] = (freq[i]+1)/2;
  93.             lda #<freq
  94.             adc I
  95.             sta P1                  ; P1 = &freq[i]
  96.             lda #>freq
  97.             adc I+1
  98.             sta P1+1
  99.             clc
  100.             lda #<freq
  101.             adc J
  102.             sta P2                  ; P2 = &freq[j]
  103.             lda #>freq
  104.             adc J+1
  105.             sta P2+1
  106.  
  107.             lda (P1,x)
  108.             sta EF
  109.             lda (P1),y
  110.             sta EF+1
  111.             inc EF
  112.             bne f1b
  113.             inc EF+1
  114. f1b         lsr EF+1
  115.             ror EF
  116.  
  117.             lda EF
  118.             sta (P2,x)
  119.             lda EF+1
  120.             sta (P2),y
  121.  
  122.             clc                     ; son[j] = son[i];
  123.             lda #<son
  124.             adc J
  125.             sta P1
  126.             lda #>son
  127.             adc J+1
  128.             sta P1+1
  129.             lda (SON_I,x)
  130.             sta (P1,x)
  131.             lda (SON_I),y
  132.             sta (P1),y
  133.  
  134.             clc                     ; j++
  135.             lda J
  136.             adc #2
  137.             sta J
  138.             bcc endif1
  139.             inc J+1
  140.  
  141. endif1      clc                     ; i++
  142.             lda I
  143.             adc #2
  144.             sta I
  145.             bcc f1c
  146.             inc I+1
  147. f1c         jmp for1
  148.  
  149. endfor1     stx I                   ; for (i=0,j=N_CHAR;
  150.             stx I+1
  151.             lda #<N_CHAR
  152.             sta J
  153.             lda #>N_CHAR
  154.             sta J+1
  155.             asl J
  156.             rol J+1
  157.  
  158. for2        lda J                   ;    j<T;
  159.             cmp #<Tstar2
  160.             lda J+1
  161.             sbc #>Tstar2
  162.             bcc f2a
  163.             jmp endfor2
  164.  
  165. f2a         clc                     ; P1 = &freq[i]
  166.             lda #<freq
  167.             adc I
  168.             sta P1
  169.             lda #>freq
  170.             adc I+1
  171.             sta P1+1
  172.             clc                     ; P2 = &freq[j]
  173.             lda J
  174.             adc #<freq
  175.             sta P2
  176.             lda J+1
  177.             adc #>freq
  178.             sta P2+1
  179.  
  180.             ldy #2                  ; f = freq[j] = freq[i]+freq[i+1];
  181.             lda (P1),y
  182.             adc (P1,x)
  183.             sta EF
  184.             sta (P2,x)
  185.             ldy #3
  186.             lda (P1),y
  187.             ldy #1
  188.             adc (P1),y
  189.             sta EF+1
  190.             sta (P2),y
  191.  
  192.             sec                     ; for(k=j-1;
  193.             lda J
  194.             sbc #2
  195.             sta K
  196.             lda J+1
  197.             sbc #0
  198.             sta K+1
  199.             clc
  200.             lda K                   ; Make K an address for this loop
  201.             adc #<freq
  202.             sta K
  203.             lda K+1
  204.             adc #>freq
  205.             sta K+1
  206.  
  207. for3        lda EF                   ;    f<freq[K];
  208.             cmp (K,x)
  209.             lda EF+1
  210.             sbc (K),y
  211.             bcc f3a
  212.             jmp endfor3
  213.  
  214. f3a         sec                     ;                 k--)
  215.             lda K
  216.             sbc #2
  217.             sta K
  218.             lda K+1
  219.             sbc #0
  220.             sta K+1
  221.             jmp for3
  222.  
  223. endfor3     clc                     ; k++
  224.             lda K
  225.             adc #2
  226.             sta K
  227.             bcc f2b
  228.             inc K+1
  229. f2b         sec                     ; Convert K back into an offset
  230.             lda K
  231.             sbc #<freq
  232.             sta K
  233.             lda K+1
  234.             sbc #>freq
  235.             sta K+1
  236.             sec                     ; l=(j-k)*2 (# of bytes to move)
  237.             lda J
  238.             sbc K
  239.             sta L
  240.             lda J+1
  241.             sbc K+1
  242.             sta L+1
  243.  
  244.             ldx #<freq             ; memmove(&freq[k+1], &freq[k], l)
  245.             ldy #>freq
  246.             jsr moveit
  247.             lda EF                   ; freq[k] = f;
  248.             sta (P1,x)
  249.             lda EF+1
  250.             sta (P1),y
  251.             ldx #<son              ; memmove(&son[k+1], &son[k], l)
  252.             ldy #>son
  253.             jsr moveit
  254.             lda I+1                 ; son[k] = i;
  255.             lsr a
  256.             sta (P1),y
  257.             lda I
  258.             ror a
  259.             sta (P1,x)
  260.  
  261.             clc                     ; i+=2; j++
  262.             lda I
  263.             adc #4
  264.             sta I
  265.             bcc f2c
  266.             inc I+1
  267. f2c         clc
  268.             lda J
  269.             adc #2
  270.             sta J
  271.             bcc f2d
  272.             inc J+1
  273. f2d         jmp for2
  274.  
  275. endfor2     stx I                   ; for(i=0;
  276.             stx I+1
  277.  
  278. for4        lda I                   ;         i<T;
  279.             cmp #<Tstar2
  280.             lda I+1
  281.             sbc #>Tstar2
  282.             bcc f4a
  283.             jmp endfor4
  284.  
  285. f4a         clc                     ; k=son[i]
  286.             lda #<son
  287.             adc I
  288.             sta P1
  289.             lda #>son
  290.             adc I+1
  291.             sta P1+1
  292.             lda (P1,x)
  293.             asl a
  294.             sta K
  295.             lda (P1),y
  296.             rol a
  297.             sta K+1
  298.  
  299.             clc                     ; prnt[k] = i
  300.             lda K
  301.             adc #<prnt
  302.             sta P1
  303.             lda K+1
  304.             adc #>prnt
  305.             sta P1+1
  306.             lda I+1
  307.             lsr a
  308.             sta (P1),y
  309.             lda I
  310.             ror a
  311.             sta (P1,x)
  312.  
  313.             lda K                   ; if( k >= T )
  314.             cmp #<Tstar2
  315.             lda K+1
  316.             sbc #>Tstar2
  317.             bcs f4endif
  318.  
  319.             lda I+1                 ; prnt[k+1] = i
  320.             ldy #3
  321.             lsr a
  322.             sta (P1),y
  323.             dey
  324.             lda I
  325.             ror a
  326.             sta (P1),y
  327.             dey
  328. f4endif     clc                     ; i++
  329.             lda I
  330.             adc #2
  331.             sta I
  332.             bcc jfor4
  333.             inc I+1
  334. jfor4       jmp for4
  335.  
  336. endfor4     rts
  337.  
  338. ;───────────────────────────────
  339. ; Local subroutine. Call memcpy
  340. ;───────────────────────────────
  341.  
  342. DST         equ T9
  343. SRC         equ T8
  344. LEN         equ T10
  345.  
  346. moveit      clc                     ; dest=src=P1 = &array[k]
  347.             txa
  348.             adc K
  349.             sta DST
  350.             sta SRC
  351.             sta P1
  352.             tya
  353.             adc K+1
  354.             sta DST+1
  355.             sta SRC+1
  356.             sta P1+1
  357.             clc                     ; dest=src+2
  358.             lda DST
  359.             adc #2
  360.             sta DST
  361.             lda DST+1
  362.             adc #0
  363.             sta DST+1
  364.             lda L
  365.             sta LEN
  366.             lda L+1
  367.             sta LEN+1
  368.             jsr memcpy
  369.             ldx #0
  370.             ldy #1
  371.             rts
  372.  
  373.  
  374. memcpy      ldy #0
  375.             lda SRC
  376.             cmp DST
  377.             lda SRC+1
  378.             sbc DST+1
  379.             bcs forw                ;if src >= dst copy from beginning
  380.  
  381.             clc
  382.             lda SRC
  383.             adc LEN
  384.             sta SRC
  385.             lda SRC+1
  386.             adc LEN+1
  387.             sta SRC+1
  388.             clc
  389.             lda DST
  390.             adc LEN
  391.             sta DST
  392.             lda DST+1
  393.             adc LEN+1
  394.             sta DST+1
  395.             ldx LEN
  396.             beq rvrs3
  397. rvrs1       tya
  398.             bne rvrs2
  399.             dec SRC+1
  400.             dec DST+1
  401. rvrs2       dey
  402.             lda (SRC),Y
  403.             sta (DST),Y
  404.             dex
  405.             bne rvrs1
  406. rvrs3       dec LEN+1
  407.             bpl rvrs1
  408.             rts
  409.  
  410. forw        ldx LEN
  411.             beq forw3
  412. forw1       lda (SRC),Y
  413.             sta (DST),Y
  414.             iny
  415.             bne forw2
  416.             inc SRC+1
  417.             inc DST+1
  418. forw2       dex
  419.             bne forw1
  420. forw3       dec LEN+1
  421.             bpl forw1
  422.             rts
  423.  
  424.             END
  425.  
  426.