home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / enterprs / c128 / text / examples.arc / CSXARC.A < prev    next >
Text File  |  1989-12-01  |  46KB  |  1,695 lines

  1. ;csxarc.main
  2. ;=================================
  3. ; Extract IBM archive
  4. ;=================================
  5. ;
  6. int01       =   $1701
  7. int04       =   $1704
  8. int05       =   $1705
  9. int08       =   $1708
  10. int0b       =   $170b
  11. int0c       =   $170c
  12. int0d       =   $170d
  13. int0e       =   $170e
  14. int15       =   $1715
  15. int16       =   $1716
  16. int17       =   $1717
  17. int21       =   $1721
  18. sw1         =   $1bfc
  19. sw2         =   $1bfd
  20. cdv         =   $1bf9
  21. cl          =   $1bf7
  22. maxm1       =   $0039
  23. status      =   $0090
  24. fnbank      =   $00c7
  25. pntr        =   $00ec
  26. fnlen       =   $00b7
  27. fnadr       =   $00bb
  28. color       =   $00f1
  29. local       =   $0024
  30. primm       =   $ff7d
  31. open        =   $ffc0
  32. close       =   $ffc3
  33. chkin       =   $ffc6
  34. hexa        =   $b8c2
  35. chkout      =   $ffc9
  36. stop        =   $ffe1
  37. clrchn      =   $ffcc
  38. setlfs      =   $ffba
  39. setnam      =   $ffbd
  40. chrin       =   $ffcf
  41. getin       =   $ffe4
  42. chrout      =   $ffd2
  43. ibuf        =   $0b00               ;input buffer
  44. ndx         =   $00d0
  45. poker       =   $0016
  46. check       =   $00fb               ;table entry to check
  47. stkptr      =   $00fd               ;lz stack pointer
  48. ;
  49.             *   = $0c00
  50. ;
  51. stack       *=*+256                     ;lz decompressor stack
  52. code        *=*+2                     ;input code
  53. oldcod      *=*+2                     ;previous code
  54. finchr      *=*+1
  55. incode      *=*+2                     ;2 bytes
  56. wtcl        *=*+2                     ;2 bytes ... when to change code length
  57. ncodes      *=*+2                     ;2 bytes ... number of codes in string table
  58. wtcl1       *=*+1                     ;copy of wtcl+1
  59. cdlen       *=*+1                     ;length of lzw code in bits
  60. omega       *=*+2                     ;temp ... current prefix
  61. kay         *=*+1                     ;temp ... current extension
  62. arcst       *=*+1                     ;eof flag
  63. count       *=*+1                     ;run length coding count
  64. prev        *=*+1                     ;rl char for output
  65. ltmp        *=*+1
  66. cmsk        *=*+1
  67. fnl         *=*+7
  68. ftyp        *=*+1
  69. arcla       *=*+1
  70. ;
  71. star        =   $1c01
  72.             .wor star
  73.             *   = star
  74.             &   = star
  75. ;
  76.             .wor there, 10
  77.             .byt $9e
  78.             .asc "(7183)", 0
  79. there       .wor 0
  80. ;
  81.             jsr primm
  82.             .asc 14, 13,"CSXARC for MS-DOS format archives "
  83.             .asc "(C)1987,88 - Ampere Metal",13
  84.             .asc "Compatible with SEA ARC version 5.20 or lower "
  85.             .asc "PKARC 3.5 or lower",13
  86.             .asc "Version 0.02",13, 0
  87. ;
  88. m0          lda #0
  89.             sta ibyt
  90.             lda sw1
  91.             bne ntex
  92.             jsr int01
  93.             sta sw2
  94.             lda #"x"
  95.             sta sw1
  96. ntex        cmp #"p"
  97.             bne m3
  98.             lda sw2                 ;default is /p
  99.             bne m3
  100.             lda #"p"
  101.             sta sw2
  102. m3          lda #%00001110
  103.             sta $ff00
  104.             rol a
  105.             sta $4000
  106.             jsr opnarc
  107.             bcc m2                  ;ok
  108.             jsr int0d
  109.             jmp int0e
  110. ;
  111. m2          jsr gethdr              ;get archive header
  112.             jsr res                 ;reset output buffer
  113.             lda #0
  114.             sta arcst
  115.             sta count
  116.             lda #<unc
  117.             sta ucr+1
  118.             lda #>unc
  119.             sta ucr+2
  120.             jsr rl0
  121.             lda method
  122.             cmp #4
  123.             bne m1
  124.             jsr usqtab
  125. m1          jsr getnxt
  126.             bmi m4
  127.             jsr bytout
  128.             jsr stop
  129.             bne m1
  130.             jsr mbfl                ;flush if e or x
  131.             jmp int0e
  132. ;
  133. m4          lda oldcrc
  134.             cmp newcrc
  135.             bne m5
  136.             lda oldcrc+1
  137.             cmp newcrc+1
  138.             bne m5
  139.             jsr primm
  140.             .asc "...ok.",13, 0
  141.             jmp jm2
  142. ;
  143. m5          jsr primm
  144.             .asc 14, "...CRC error!",13, 0
  145. jm2         jsr mbfl
  146.             lda #1
  147.             jsr close
  148.             jmp m2
  149. ;
  150. ;csxarc.common
  151. ;--------------------------------
  152. ; chrout for IBM archive extract
  153. ;--------------------------------
  154. ;
  155. bytout      bit yes                 ;do we want this file?
  156.             bmi byto                ;yep
  157.             rts
  158. byto        ldx sw1                 ;option
  159.             cpx #"p"                ;type?
  160.             bne bo1
  161.             jmp cvt
  162. ;
  163. bo1         cpx #"v"                ;verify
  164.             bne bo2
  165.             rts
  166. ;
  167. bo2         cpx #"e"                ;extract
  168.             bne bo3
  169.             beq bout
  170. ;
  171. bo3         cpx #"x"                ;extract
  172.             beq bout
  173.             jsr primm
  174.             .asc 13,"Bad option?", 0
  175.             jmp int0e
  176. ;
  177. ;-----------------
  178. ; buffered chrout
  179. ;-----------------
  180. ;
  181. bout        bit asctyp              ;conversion?
  182.             bpl bou                 ;no
  183.             jsr a22p                ;convert
  184.             bne bou                 ;not lf
  185.             rts                     ;ignore lf
  186. ;
  187. bou         ldx $ff00
  188.             sta $ff02
  189. bto         sta $4000
  190.             stx $ff00
  191.             inc bto+1
  192.             bne bto1
  193.             inc bto+2
  194. bto1        lda bto+2
  195.             cmp maxm1+1
  196.             bne btox
  197.             lda bto+1
  198.             cmp maxm1
  199.             beq flush
  200. btox        rts
  201. ;
  202. flush       lda #<$4000
  203.             sta fl+1
  204.             lda #>$4000
  205.             sta fl+2
  206.             ldx #1
  207.             jsr chkout
  208. flp         ldx $ff00
  209.             sta $ff02
  210. fl          lda $4000
  211.             stx $ff00
  212.             inc fl+1
  213.             bne bfl2
  214.             inc fl+2
  215. bfl2        jsr chrout
  216.             lda fl+1
  217.             cmp bto+1
  218.             bne flp
  219.             lda fl+2
  220.             cmp bto+2
  221.             bne flp
  222.             jsr clrchn
  223. res         lda #<$4000
  224.             sta bto+1
  225.             lda #>$4000
  226.             sta bto+2
  227.             rts
  228. ;
  229. ;-----------------------------------------
  230. ; Subroutine: flush output buffer (maybe)
  231. ;-----------------------------------------
  232. ;
  233. mbfl        lda sw1                 ;is it x or e?
  234.             cmp #"x"
  235.             beq dfl
  236.             cmp #"e"
  237.             beq dfl
  238. mbflx       rts                     ;no. print or verify..no flush required
  239. ;
  240. dfl         lda bto+2               ;buffer empty?
  241.             cmp #>$4000
  242.             bne ddfl
  243.             lda bto+1
  244.             cmp #<$4000
  245.             beq mbflx
  246. ddfl        jmp flush
  247. ;
  248. ;csxarc.io
  249. ;-------
  250. ; bitin
  251. ;-------
  252. ;
  253. bits        .byt 1, 2, 4, 8, 16, 32, 64, 128
  254. ibit        .byt 0
  255. ibyt        .byt 0
  256. bite        .byt 0
  257. ;
  258. bitin       dec ibit                ;offset into bit buffer
  259.             bpl bti1                ;need a new byte if zero
  260.             pha
  261.             jsr bytin
  262.             sta bite
  263.             lda #7
  264.             sta ibit
  265.             pla
  266. bti1        lsr bite                ;put bit in carry
  267.             rts
  268. ;
  269. ;-------
  270. ; bytin
  271. ;-------
  272. ;
  273. srcst       pla
  274. eos         pla
  275. ;
  276. bytin       sty biy+1
  277.             stx bix+1
  278.             ldy #0
  279.             sty srcst
  280.             ldy ibyt                ;offset into buffer
  281.             bne bi1                 ;full buffer. get char
  282.             ldx arcla               ;else refresh buffer
  283.             jsr chkin
  284. ibytlp      jsr chrin
  285.             sta ibuf,y
  286.             bit status
  287.             bvs eoff
  288.             bmi eoff
  289.             iny
  290.             bne ibytlp
  291. eoff        sty eos
  292.             jsr clrchn
  293. bi2         ldy #0
  294. bi1         lda ibuf,y
  295.             iny
  296.             sty ibyt
  297.             beq biy
  298.             cpy eos
  299.             bne biy
  300.             dec srcst
  301. biy         ldy #0
  302. bix         ldx #0
  303.             rts
  304. ;
  305. ;--------------------------
  306. ; get archive entry header
  307. ;--------------------------
  308. ;
  309. gethdr      jsr wait                ;wait for ARC header byte
  310.             sta header+1            ;save type
  311.             cmp #1                  ;old type store?
  312.             bne nos                 ;no
  313.             lda #25                 ;if so only 25 byte header
  314.             .byt $2c
  315. nos         lda #29
  316.             sta header
  317.             ldy #2                  ;get 30 bytes
  318. ghd0        jsr bytin
  319.             sta header,y
  320.             iny
  321.             cpy header
  322.             bne ghd0
  323.             lda method
  324.             cmp #1
  325.             bne ghd2
  326.             ldy #3
  327. cpyl        lda sqlen,y
  328.             sta len,y
  329.             dey
  330.             bpl cpyl
  331. ghd2        ldy #0
  332. ghd4        lda filenm,y
  333.             beq ghd3
  334.             jsr a22p
  335.             cmp #"A"
  336.             bcc a33p
  337.             cmp #"Z"+1
  338.             bcs a33p
  339.             and #$7f
  340. a33p        sta $1b22,y
  341.             jsr chrout
  342.             iny
  343.             bne ghd4
  344. ghd3        sty ghd33+1
  345.             jsr chkif               ;do we want this one?
  346.             lda #0                  ;assume no
  347.             bcs ghd33
  348.             lda #$ff
  349. ghd33       ldy #0
  350.             sta yes
  351.             lda #","
  352.             sta $1b22,y
  353.             iny
  354.             lda pattyp
  355.             sta $1b22,y
  356.             iny
  357.             lda #","
  358.             sta $1b22,y
  359.             iny
  360.             lda #"w"
  361.             sta $1b22,y
  362.             iny
  363.             iny
  364.             iny
  365.             tya
  366.             ldx #<$1b20
  367.             ldy #>$1b20
  368.             jsr setnam
  369.             jsr tab
  370.             lda #1
  371.             tay
  372.             jsr setlfs
  373.             lda #0
  374.             sta fnbank
  375.             lda #" "
  376.             jsr chrout
  377.             lda method
  378.             cmp #9
  379.             bne ghd9
  380.             lda #13
  381.             sta cdmax
  382.             lda #$20
  383.             sta cdmaxx
  384. ghd9        ldx date
  385.             ldy date+1
  386.             jsr int15
  387.             lda #" "
  388.             jsr chrout
  389.             lda #0
  390.             sta ibit
  391.             sta newcrc
  392.             sta newcrc+1
  393.             lda method
  394.             cmp #8
  395.             bne ghda
  396.             jsr bytin
  397.             sta cdmax
  398.             tay
  399.             sec
  400.             lda #0
  401. s0          rol a
  402.             bcs s0
  403.             dey
  404.             bpl s0
  405.             sta cdmaxx
  406.             lda cdmax
  407.             cmp #10
  408.             bcc ltt
  409.             clc
  410.             adc #6
  411. ltt         nop                     ;jsr ghexa
  412. ;jsr primm
  413. ;.asc "bit ", 0
  414.             lda cdmaxx
  415.             cmp #$40
  416.             bcc ghda
  417.             jsr primm
  418.             .asc 13,"String table too large",13, 0
  419. jd          jmp done
  420. ;
  421. ghexa       pha
  422.             jmp hex
  423. ;
  424. ghda        ldy cdmaxx
  425.             dey
  426.             sty cmxm1
  427.             lda method
  428.             jsr ptype
  429.             tay
  430.             lda mthflg,y
  431.             sta meth
  432.             lda sw1
  433.             cmp #"x"
  434.             bne ghdax
  435.             bit yes
  436.             bpl ghdax
  437.             lda sw2
  438.             bne usesw2
  439.             jsr $1701
  440. usesw2      sta $1b20
  441.             lda #":"
  442.             sta $1b21
  443.             jsr open
  444.             jsr int0c
  445.             cmp #20
  446.             bcc ghdax
  447.             jsr int0d
  448.             jmp jd
  449. ghdax       rts
  450. ;
  451. mthflg      .byt 0, 0, 0, 0, 0
  452.             .byt 0, 0
  453.             .byt %10000000, %11000000, %11000000
  454. ;
  455. meth        .byt 9
  456. ;
  457. ;-----------------
  458. ; chrout for type
  459. ;-----------------
  460. ;
  461. cvt         pha
  462.             lda sw2
  463.             cmp #"p"
  464.             beq a2p
  465.             cmp #"a"
  466.             beq a2p
  467.             cmp #"n"
  468.             beq none
  469.             cmp #"s"
  470.             beq screen
  471.             cmp #"h"
  472.             beq hex
  473. none        pla
  474.             jmp chrout
  475. ;
  476. screen      pla
  477.             sty poker
  478.             ldx color
  479.             jsr $c003
  480.             lda #29                 ; Cursor right
  481.             jsr $c00c
  482.             ldy poker
  483.             rts
  484. ;
  485. hex         lda $ff00
  486.             sta plpl+1
  487.             lda #0
  488.             sta $ff00
  489.             pla
  490.             jsr hexa
  491. plpl        lda #0
  492.             sta $ff00
  493.             rts
  494. ;
  495. p2a         pla
  496.             cmp #"a"                ;petscii to ascii
  497.             bcc p2ax
  498.             cmp #$5b
  499.             bcs p2a2
  500.             ora #$20
  501.             bne p2ax
  502. ;
  503. p2a2        cmp #$c1
  504.             bcc p2ax
  505.             cmp #$db
  506.             bcs p2ax
  507.             and #$7f
  508. p2ax        jmp chrout
  509. ;
  510. a2p         pla
  511.             jsr a22p
  512.             beq ap2x
  513.             jsr chrout
  514. ap2x        rts
  515. ;
  516. a22p        pha
  517.             lda char
  518.             sta oldchr
  519.             pla
  520.             sta char
  521.             cmp #"a"                ;ascii to petscii
  522.             bcc a2px
  523.             cmp #$5b
  524.             bcs a2p2
  525.             ora #$80
  526.             bne a2px
  527. ;
  528. a2p2        cmp #$61
  529.             bcc a2px
  530.             cmp #$7b
  531.             bcs a2px
  532.             and #$df
  533. a2px        cmp #10
  534.             bne a2pxx
  535.             lda #13
  536.             cmp oldchr
  537.             bne a2pxx
  538.             lda #10
  539.             cmp #10
  540. a2pxx       rts
  541. ;
  542. wait        jsr bytin               ;wait for $1a
  543.             cmp #$1a
  544.             beq gothdr              ;ok. maybe got one
  545.             bit srcst
  546.             bpl wait                ;until EOF
  547. done        jsr primm
  548.             .asc 13,"Done.", 0
  549.             jmp int0e
  550. ;
  551. gothdr      jsr bytin
  552.             cmp #0
  553.             beq done
  554.             bit srcst
  555.             bmi done
  556.             cmp #$1a
  557.             beq gothdr
  558.             cmp #10
  559.             bcs help
  560.             rts
  561. ;
  562. help        jsr primm
  563.             .asc 13,"I can't handle this next file",13, 0
  564.             jmp done
  565. ;
  566. opnarc      ldx #1
  567.             jsr int04
  568.             bcc pna0
  569.             jmp int0e
  570. ;
  571. pna0        jsr popt                ;display option
  572.             ldx #1                  ;setup %1 as a filename
  573.             ldy #2
  574.             jsr int21
  575.             jsr chkarc              ;check for .arc extension
  576.             ldy #0
  577. pna2        lda (fnadr),y
  578.             jsr chrout
  579.             iny
  580.             cpy fnlen
  581.             bne pna2
  582.             lda #13
  583.             jsr chrout
  584.             lda #2
  585.             sta arcla
  586.             ldy #2
  587.             jsr setlfs
  588.             jsr open
  589.             jmp int0b
  590. ;
  591. ;------------------------
  592. ; display storage method
  593. ;------------------------
  594. ;
  595. types       .asc "EOF     "
  596.             .asc "Stored  "
  597.             .asc "STored  "
  598.             .asc "Packed  "
  599.             .asc "Squeezed"
  600.             .asc "Crunched"
  601.             .asc "CRunched"
  602.             .asc "CRUnched"
  603.             .asc "CRUNched"
  604.             .asc "Squashed"
  605.             .asc "Unknown "
  606. ;
  607. ptype       pha
  608.             cmp #10
  609.             bcc pty
  610.             lda #10
  611. pty         asl a
  612.             asl a
  613.             asl a
  614.             tay
  615.             ldx #8
  616. pt          lda types,y
  617.             jsr chrout
  618.             iny
  619.             dex
  620.             bne pt
  621.             pla
  622.             rts
  623. ;
  624. ;---------
  625. ; tab(.a)
  626. ;---------
  627. ;
  628. tab         lda #" "
  629.             jsr chrout
  630.             lda pntr
  631.             cmp #21
  632.             bne tab
  633.             rts
  634. ;
  635. ;-------------------------------------
  636. ; subroutine: display selected option
  637. ;-------------------------------------
  638. ;
  639. popt        lda sw1
  640.             cmp #"e"
  641.             beq pext
  642.             cmp #"x"
  643.             beq pext
  644.             cmp #"v"
  645.             beq pver
  646.             cmp #"l"
  647.             beq plis
  648.             cmp #"p"
  649.             beq pext
  650.             jsr primm
  651.             .asc 13,"bad option",13, 0
  652.             jmp int0e
  653. ;
  654. pext        jsr primm
  655.             .asc 13,"extracting from", 0
  656.             jmp pfrom
  657. plis        jsr primm
  658.             .asc 13,"directory for", 0
  659.             jmp pfrom
  660. pver        jsr primm
  661.             .asc 13,"verifying", 0
  662. pfrom       jsr primm
  663.             .asc " archive: ", 0
  664.             rts
  665. ;
  666. ;-----------------------------------
  667. ; check filename for .arc extension
  668. ;-----------------------------------
  669. ;
  670. dotarc      .asc ".arc"
  671. ;
  672. chkarc      ldy fnlen
  673.             cpy #4
  674.             bcc adarc               ;can't be there if len<4
  675.             ldx #3
  676.             dey
  677. ckalp       lda (fnadr),y
  678.             cmp dotarc,x
  679.             bne adda
  680.             dey
  681.             dex
  682.             bpl ckalp
  683.             rts
  684. ;
  685. adda        ldy fnlen
  686. adarc       ldx #0
  687. adrc        lda dotarc,x
  688.             sta (fnadr),y
  689.             iny
  690.             inx
  691.             cpx #4
  692.             bne adrc
  693.             sty fnlen
  694.             rts
  695. ;
  696. ;--------------------------------------------------------
  697. ; subroutine: check directory entry for match with pattrn
  698. ;--------------------------------------------------------
  699. ;
  700. chknam      lda #<$1b22             ;address of PETscii filename
  701.             sta $fc
  702.             lda #>$1b22
  703.             sta $fc+1
  704.             ldy #0                  ;now get true filename length
  705. ckn0        lda ($fc),y
  706.             cmp #","                ;End of name?
  707.             beq ckn1
  708.             iny
  709.             cpy #13
  710.             bcc ckn0
  711. ckn1        sty namlen
  712.             ldy #0                  ;offset into name
  713.             ldx #0                  ;offset into pattern
  714.             cpx patlen              ;null pattern..match nothing
  715.             beq nmatch
  716. comnxt      lda pattrn,x
  717.             cmp #"?"
  718.             beq chrmat              ;found matching character
  719.             cmp #"*"                ;* is sliding match
  720.             beq slide
  721.             cmp ($fc),y
  722.             beq chrmat
  723. nmatch      clc                     ;no match
  724.             rts
  725. ;
  726. slide       inx                     ;is * last char of pattern?
  727.             cpx patlen              ;yes..a match
  728.             beq match
  729.             lda pattrn,x            ;check for *=type
  730. sl0         iny                     ;otherwise advance in name to next char of pattern
  731.             cpy namlen              ;didn't find it..no match
  732.             beq nmatch
  733.             cmp ($fc),y
  734.             bne sl0
  735. chrmat      inx                     ;chars match...advance in both pattern and name
  736.             cpx patlen              ;end of pattern?
  737.             beq eopat               ;yes..match if also end of name
  738.             iny                     ;end of name?
  739.             cpy namlen
  740.             bne comnxt              ;no..still more to check
  741.             lda pattrn,x            ;end of name, but not of pattern...no match unless =typ
  742.             cmp #"*"
  743.             beq match
  744.             bne nmatch
  745. ;
  746. eopat       iny                     ;end of pattern
  747.             cpy namlen              ;also end of name?
  748.             beq match               ;yes..match
  749.             bne comnxt              ;otherwise still more to check
  750. ;
  751. match       sec                     ;name matches
  752.             rts
  753. ;
  754. not         .byt 0
  755. wchnam      .byt 0                   ;offset into directory block
  756. namtyp      .byt 0                   ;file type d,s,p,u or r
  757. namlen      .byt 0                   ;length of file's name in ARC header
  758. ftypes      .asc "dspur"
  759. pattrn      .asc "(C)1987,88 - Ampere Metal",0
  760. patlen      .byt 0
  761. pattyp      .byt 0                   ;filetype if this is a match
  762. asctyp      .byt 0                   ;ASCII type if this is a match (0=no conv, bmi=ascii)
  763. parm        .byt 0
  764. yes         .byt 0
  765. ;
  766. ;-------------------------------------------------------
  767. ; Get pattern/type for selective extraction
  768. ;-------------------------------------------------------
  769. ;
  770. getpat      stx parm
  771.             jsr gtp3                ;default type is seq
  772.             jsr int04
  773.             bcc gtp0                ;ok, continue
  774.             rts                     ;else none there SEC
  775. ;
  776. gtp0        ldx #0                  ;Save it
  777. gtp1        cmp #"/"                ;type?
  778.             beq gtp2                ;yes
  779.             sta pattrn,x
  780.             inx
  781.             jsr int05
  782.             bcc gtp1
  783.             stx patlen
  784.             rts
  785. ;
  786. gtp2        jsr int05               ;get filetype
  787.             stx patlen
  788.             cmp #"p"                ;prg?
  789.             bne gtp5                ;No, maybe "a"
  790.             jsr int05
  791.             bcs gtp9
  792. gtp5        cmp #"a"
  793.             beq gtp8
  794.             lda #0                  ;no conversion
  795.             .byt $2c
  796. gtp8        lda #$ff
  797.             sta asctyp
  798.             jmp gtp9
  799. gtp3        lda #"s"                ;assume seq if not prg
  800. gtp4        sta pattyp
  801. gtp9        rts
  802. ;
  803. ;----------------------------------
  804. ; Check for name in parameter list
  805. ;----------------------------------
  806. ;
  807. chkif       ldx #2                  ;start with %2 and work up
  808. chif1       jsr int04
  809.             bcc chif0               ;ok its there
  810.             cpx #2                  ;Not if no parameters at all
  811.             beq chkify              ;Then always return true
  812.             sec
  813.             rts                     ;no match SEC
  814. ;
  815. chif0       jsr getpat              ;get 'pattrn', 'patlen', 'pattyp'
  816.             jsr chknam              ;matches name?
  817.             bcs chkify              ;yes. a match
  818.             inc parm
  819.             ldx parm
  820.             bne chif1               ;always
  821. ;
  822. chkify      clc
  823.             rts
  824. ;
  825. ;xibm.crc
  826. ;------------------------
  827. ; subroutine: Update CRC
  828. ;------------------------
  829. ;
  830. updcrc      pha                     ;save char
  831.             sty uc+1                ;save .y
  832.             eor newcrc
  833.             tay
  834.             lda crclo,y
  835.             eor newcrc+1
  836.             sta newcrc
  837.             lda crchi,y
  838.             sta newcrc+1
  839. uc          ldy #0
  840.             pla
  841.             rts
  842. ;
  843. newcrc      .wor 0
  844. ;
  845. crclo       .byt $00, $c1, $81, $40, $01, $c0, $80, $41
  846.             .byt $01, $c0, $80, $41, $00, $c1, $81, $40
  847.             .byt $01, $c0, $80, $41, $00, $c1, $81, $40
  848.             .byt $00, $c1, $81, $40, $01, $c0, $80, $41
  849.             .byt $01, $c0, $80, $41, $00, $c1, $81, $40
  850.             .byt $00, $c1, $81, $40, $01, $c0, $80, $41
  851.             .byt $00, $c1, $81, $40, $01, $c0, $80, $41
  852.             .byt $01, $c0, $80, $41, $00, $c1, $81, $40
  853.             .byt $01, $c0, $80, $41, $00, $c1, $81, $40
  854.             .byt $00, $c1, $81, $40, $01, $c0, $80, $41
  855.             .byt $00, $c1, $81, $40, $01, $c0, $80, $41
  856.             .byt $01, $c0, $80, $41, $00, $c1, $81, $40
  857.             .byt $00, $c1, $81, $40, $01, $c0, $80, $41
  858.             .byt $01, $c0, $80, $41, $00, $c1, $81, $40
  859.             .byt $01, $c0, $80, $41, $00, $c1, $81, $40
  860.             .byt $00, $c1, $81, $40, $01, $c0, $80, $41
  861.             .byt $01, $c0, $80, $41, $00, $c1, $81, $40
  862.             .byt $00, $c1, $81, $40, $01, $c0, $80, $41
  863.             .byt $00, $c1, $81, $40, $01, $c0, $80, $41
  864.             .byt $01, $c0, $80, $41, $00, $c1, $81, $40
  865.             .byt $00, $c1, $81, $40, $01, $c0, $80, $41
  866.             .byt $01, $c0, $80, $41, $00, $c1, $81, $40
  867.             .byt $01, $c0, $80, $41, $00, $c1, $81, $40
  868.             .byt $00, $c1, $81, $40, $01, $c0, $80, $41
  869.             .byt $00, $c1, $81, $40, $01, $c0, $80, $41
  870.             .byt $01, $c0, $80, $41, $00, $c1, $81, $40
  871.             .byt $01, $c0, $80, $41, $00, $c1, $81, $40
  872.             .byt $00, $c1, $81, $40, $01, $c0, $80, $41
  873.             .byt $01, $c0, $80, $41, $00, $c1, $81, $40
  874.             .byt $00, $c1, $81, $40, $01, $c0, $80, $41
  875.             .byt $00, $c1, $81, $40, $01, $c0, $80, $41
  876.             .byt $01, $c0, $80, $41, $00, $c1, $81, $40
  877. ;
  878. crchi       .byt $00, $c0, $c1, $01, $c3, $03, $02, $c2
  879.             .byt $c6, $06, $07, $c7, $05, $c5, $c4, $04
  880.             .byt $cc, $0c, $0d, $cd, $0f, $cf, $ce, $0e
  881.             .byt $0a, $ca, $cb, $0b, $c9, $09, $08, $c8
  882.             .byt $d8, $18, $19, $d9, $1b, $db, $da, $1a
  883.             .byt $1e, $de, $df, $1f, $dd, $1d, $1c, $dc
  884.             .byt $14, $d4, $d5, $15, $d7, $17, $16, $d6
  885.             .byt $d2, $12, $13, $d3, $11, $d1, $d0, $10
  886.             .byt $f0, $30, $31, $f1, $33, $f3, $f2, $32
  887.             .byt $36, $f6, $f7, $37, $f5, $35, $34, $f4
  888.             .byt $3c, $fc, $fd, $3d, $ff, $3f, $3e, $fe
  889.             .byt $fa, $3a, $3b, $fb, $39, $f9, $f8, $38
  890.             .byt $28, $e8, $e9, $29, $eb, $2b, $2a, $ea
  891.             .byt $ee, $2e, $2f, $ef, $2d, $ed, $ec, $2c
  892.             .byt $e4, $24, $25, $e5, $27, $e7, $e6, $26
  893.             .byt $22, $e2, $e3, $23, $e1, $21, $20, $e0
  894.             .byt $a0, $60, $61, $a1, $63, $a3, $a2, $62
  895.             .byt $66, $a6, $a7, $67, $a5, $65, $64, $a4
  896.             .byt $6c, $ac, $ad, $6d, $af, $6f, $6e, $ae
  897.             .byt $aa, $6a, $6b, $ab, $69, $a9, $a8, $68
  898.             .byt $78, $b8, $b9, $79, $bb, $7b, $7a, $ba
  899.             .byt $be, $7e, $7f, $bf, $7d, $bd, $bc, $7c
  900.             .byt $b4, $74, $75, $b5, $77, $b7, $b6, $76
  901.             .byt $72, $b2, $b3, $73, $b1, $71, $70, $b0
  902.             .byt $50, $90, $91, $51, $93, $53, $52, $92
  903.             .byt $96, $56, $57, $97, $55, $95, $94, $54
  904.             .byt $9c, $5c, $5d, $9d, $5f, $9f, $9e, $5e
  905.             .byt $5a, $9a, $9b, $5b, $99, $59, $58, $98
  906.             .byt $88, $48, $49, $89, $4b, $8b, $8a, $4a
  907.             .byt $4e, $8e, $8f, $4f, $8d, $4d, $4c, $8c
  908.             .byt $44, $84, $85, $45, $87, $47, $46, $86
  909.             .byt $82, $42, $43, $83, $41, $81, $80, $40
  910. ;
  911. ;csxarc.lzw
  912. ;-----------------------------
  913. ; Unsqueeze a byte subroutine
  914. ;-----------------------------
  915. ;
  916. getnxt      stx bast+1
  917.             sty basty+1
  918.             jsr dcln                ;check for end of file
  919.             bit arcst
  920.             bmi bast0               ;eof...don't input past end
  921. ;
  922. gxt         ldx count               ;on a run?
  923.             beq gnxt                ;no
  924.             jsr rl33                ;yes - get repeated character
  925.             jmp usq89
  926. ;
  927. gnxt        ldx method              ;what type of file?
  928.             cpx #4                  ;just get byte if stored or packed
  929.             bcc usq88               ;yes..get byte
  930.             beq huff                ;squeezed
  931. crnch       jsr ucr                 ;uncrunch a byte
  932.             ldx method
  933.             jmp usq80
  934. ;
  935. huff        jsr hufin
  936.             bcs eo
  937.             bcc rlo
  938. usq88       jsr bytin
  939. usq80       cpx #2                  ;was it stored?
  940.             beq usq89               ;yes then we've got a byte
  941.             cpx #5
  942.             beq usq89
  943.             cpx #9
  944.             beq usq89
  945. rlo         jsr rlout               ;otherwise it might need to be un-packed
  946. usq89       jsr updcrc              ;update crc
  947. bast0       clc
  948. bast        ldx #0
  949. basty       ldy #0
  950.             bit arcst
  951.             rts
  952. ;
  953. dcln        ldx len                 ;check for end of file
  954.             bne dl0
  955.             ldx len+1
  956.             bne dl1
  957.             ldx len+2
  958.             bne dl2
  959.             ldx len+3
  960.             bne dl3
  961. eo          lda #$ff                ;len is zero. flag eof
  962.             sta arcst
  963.             rts
  964. ;
  965. dl3         dec len+3
  966. dl2         dec len+2
  967. dl1         dec len+1
  968. dl0         dec len
  969. dl4         rts
  970. ;
  971. ;----------------------------------
  972. ; run-length byte output for arc/x
  973. ;----------------------------------
  974. ;
  975. rlout       jmp rl1                 ;changes
  976. ;
  977. rl1         cmp #$90                ;is it a control character?
  978.             beq contrl              ;yes-get count
  979.             sta prev
  980.             rts                     ;else send to output
  981. ;
  982. contrl      lda #<rl2               ;setup for count
  983.             sta rlout+1
  984.             lda #>rl2
  985.             sta rlout+2
  986. gnx         pla
  987.             pla
  988.             jmp gnxt
  989. ;
  990. rl2         sta count
  991.             dec count
  992.             cmp #0
  993.             bne rl9
  994.             sta count
  995.             jsr rl0
  996.             lda #$90
  997.             rts
  998. rl9         jsr rl0                 ;and setup for repeat
  999. rl33        dec count               ;send char count times
  1000.             bne rl44
  1001. rl0         lda #<rl1               ;reset rlout
  1002.             sta rlout+1
  1003.             lda #>rl1
  1004.             sta rlout+2
  1005. rl44        lda prev
  1006. rl45        rts
  1007. ;
  1008. ;-------------------------
  1009. ; lempel-zev decompressor
  1010. ;-------------------------
  1011. ;
  1012. ucr         jmp unc                 ;first time in
  1013. ;
  1014. unc         jsr lzwrst              ;reset string table
  1015. uncc        jsr resstk              ;reset stack
  1016.             jsr codein              ;get 'code'
  1017.             bit meth                ;old style?
  1018.             bvs newsty              ;no new
  1019.             sta check               ;else get extension for code
  1020.             lda code+1
  1021.             sta check+1
  1022.             jsr getexc
  1023.             sta kay
  1024.             lda code
  1025.             jmp tt
  1026. newsty      sta kay                 ;codein returns code in .a
  1027. tt          sta oldcod              ;first code is a byte
  1028.             lda kay
  1029.             sta finchr
  1030.             lda code+1              ;oldcod=code
  1031.             and cmxm1
  1032.             sta oldcod+1
  1033.             lda #<nxtcod
  1034.             sta ucr+1
  1035.             lda #>nxtcod
  1036.             sta ucr+2
  1037.             lda kay
  1038.             clc
  1039.             rts
  1040. ;
  1041. nxtcod      jsr codein              ;next code
  1042.             bit meth
  1043.             bvs newc
  1044.             lda code+1
  1045.             and #$0f
  1046.             sta incode+1
  1047.             ora #>used
  1048.             sta poker+1
  1049.             lda code
  1050.             sta incode
  1051.             sta poker
  1052.             jsr peek
  1053.             beq nxtsym
  1054.             bne nsm
  1055. newc        sec                     ;setup. test if code is defined (< ncodes)
  1056.             sta incode              ;incode=code
  1057.             sbc ncodes
  1058.             lda code+1
  1059.             sta incode+1
  1060.             sbc ncodes+1
  1061.             bcc nxtsym              ;carry clear. code was smaller.
  1062. nsm         lda finchr              ;undefined code - special case.
  1063.             sta kay
  1064.             jsr push
  1065.             lda oldcod
  1066.             sta code
  1067.             sta omega
  1068.             lda oldcod+1
  1069.             sta code+1
  1070.             sta omega+1
  1071.             bit meth
  1072.             bvs nnc
  1073.             jsr hash
  1074.             lda poker
  1075.             sta incode
  1076.             lda poker+1
  1077.             and #$0f
  1078.             jmp ncn
  1079. nnc         lda ncodes
  1080.             sta incode
  1081.             lda ncodes+1
  1082. ncn         sta incode+1
  1083. nxtsym      bit meth
  1084.             bvs nxtsy
  1085.             lda code
  1086.             sta poker
  1087.             lda code+1
  1088.             and #$0f
  1089.             ora #>pfxhi
  1090.             sta poker+1
  1091.             jsr peek
  1092.             cmp #$ff
  1093.             beq kaybyt
  1094.             bne nkay
  1095. nxtsy       lda code+1              ;is it just a byte?
  1096.             beq kaybyt              ;yes-end of string
  1097. nkay        lda code                ;else extension(code) to stack
  1098.             sta poker
  1099.             lda code+1
  1100.             ora #>ext
  1101.             sta poker+1
  1102.             jsr peek
  1103.             jsr push
  1104.             lda poker+1             ;and code=prefix(code)
  1105.             and cmxm1
  1106.             ora #>pfxlo
  1107.             sta poker+1
  1108.             jsr peek
  1109.             sta code
  1110.             lda poker+1
  1111.             and cmxm1
  1112.             ora #>pfxhi
  1113.             sta poker+1
  1114.             jsr peek
  1115.             sta code+1
  1116.             bit meth
  1117.             bvs cnc
  1118.             eor #$ff
  1119. cnc         cmp #0
  1120.             bne nxtsym              ;until just a byte
  1121. ;
  1122. kaybyt      lda #<eps
  1123.             sta ucr+1
  1124.             lda #>eps
  1125.             sta ucr+2
  1126.             bit meth
  1127.             bvs kbg
  1128.             lda code
  1129.             sta poker
  1130.             lda code+1
  1131.             and #$0f
  1132.             ora #>ext
  1133.             sta poker+1
  1134.             jsr peek
  1135.             jmp kbj
  1136. kbg         lda code                ;code is now only a single byte
  1137. kbj         sta kay
  1138.             sta finchr
  1139.             clc
  1140.             rts
  1141. ;
  1142. eps         jsr pull                ;get from top of stack
  1143.             bcs sie                 ;stack is empty
  1144.             rts
  1145. ;
  1146. sie         lda oldcod
  1147.             sta omega
  1148.             lda oldcod+1
  1149.             sta omega+1
  1150.             jsr lzadd               ;add omega,kay to table
  1151.             lda incode              ;oldcode=incode
  1152.             sta oldcod
  1153.             lda incode+1
  1154.             sta oldcod+1
  1155.             jmp nxtcod
  1156. ;
  1157. ;
  1158. ;--------------------------------------
  1159. ; subroutine. get code from input file
  1160. ;--------------------------------------
  1161. ;
  1162. oldcr       lda #0
  1163.             bne odd
  1164.             sta code+1
  1165.             inc oldcr+1
  1166.             jsr bytin
  1167.             sta code
  1168.             jsr bytin
  1169.             sta bytsav
  1170.             ldy #4
  1171. lpcr        asl a
  1172.             rol code
  1173.             rol code+1
  1174.             dey
  1175.             bne lpcr
  1176.             lda code
  1177.             rts
  1178. ;
  1179. odd         lda bytsav
  1180.             and #$0f
  1181.             sta code+1
  1182.             jsr bytin
  1183.             sta code
  1184.             dec oldcr+1
  1185.             rts
  1186. ;
  1187. codein      bit meth
  1188.             bvc oldcr
  1189.             lda #0
  1190.             sta code
  1191.             inc cdcnt               ;bump code counter
  1192.             ldy cdlen               ;bit length of code
  1193. ci0         jsr bitin               ;read in code bitwise
  1194.             ror code+1
  1195.             ror code
  1196.             dey
  1197.             bne ci0
  1198.             ldy #16
  1199. ci2         lsr code+1
  1200.             ror code
  1201.             dey
  1202.             cpy cdlen
  1203.             bne ci2
  1204. ci          lda code                ;test eof
  1205.             bne ci3                 ;not 256
  1206.             lda code+1
  1207.             cmp #>256
  1208.             bne ci3
  1209.             pla
  1210.             pla
  1211.             jsr flb
  1212.             jmp unc
  1213. ;
  1214. flb         lda cdcnt
  1215.             and #7                  ;number of codes in buffer
  1216.             tay
  1217.             beq gunc                ;none..no flush
  1218.             clc
  1219.             lda #0
  1220. bbl         adc cdlen               ;times code len=bits in buffer
  1221.             dey
  1222.             bne bbl
  1223.             pha                     ;save it
  1224.             lsr a                   ;/8=bytes
  1225.             lsr a
  1226.             lsr a
  1227.             tay                     ;save it
  1228.             pla                     ;check for remainder
  1229.             and #7
  1230.             beq skpbf               ;none
  1231.             iny
  1232. skpbf       jsr bytin
  1233.             iny
  1234.             cpy cdlen
  1235.             bne skpbf
  1236. gunc        ldy #0
  1237.             sty ibit
  1238.             sty cdcnt
  1239.             rts
  1240. ;
  1241. ci3         lda code                ;and bump code length
  1242.             rts
  1243. ;
  1244. hm2s        .byt 0, 10, 9, 7, 6, 4, 3, 1
  1245. ;
  1246. ;------------------------------------------
  1247. ; subroutine. push/pull char to/from stack
  1248. ;------------------------------------------
  1249. ;
  1250. push        ldy #0
  1251.             sta (stkptr),y          ;stkptr must be initialized
  1252.             inc stkptr
  1253.             bne push0
  1254.             inc stkptr+1
  1255. push0       rts
  1256. ;
  1257. pull        lda stkptr              ;check for empty stack
  1258.             cmp #<stack
  1259.             bne pull0
  1260.             lda stkptr+1
  1261.             cmp #>stack
  1262.             bne pull0               ;not empty
  1263.             sec                     ;empty
  1264.             rts
  1265. ;
  1266. resstk      lda #<stack             ;reset stack
  1267.             sta stkptr
  1268.             lda #>stack
  1269.             sta stkptr+1
  1270.             rts
  1271. ;
  1272. pull0       lda stkptr
  1273.             bne pull1
  1274.             dec stkptr+1
  1275. pull1       dec stkptr
  1276.             ldx #0
  1277.             lda (stkptr,x)
  1278.             clc
  1279.             rts
  1280. ;
  1281. ;-----------------------------------
  1282. ; lempel zev table reset subroutine
  1283. ;-----------------------------------
  1284. ;
  1285. lzwrst      bit meth
  1286.             bvs lzwr
  1287.             lda #12
  1288.             sta cdlen
  1289.             sta cdmax
  1290.             lda #$10
  1291.             sta cdmaxx
  1292.             sta cmxm1
  1293.             dec cmxm1
  1294.             lda #0
  1295.             sta oldcr+1
  1296.             sta ncodes
  1297.             sta ncodes+1
  1298.             inc ncodes+1
  1299.             jmp init
  1300. ;
  1301. lzwr        lda #<257               ;set number of codes to 257
  1302.             ldy #>257               ;(code 256 is reserved)
  1303.             sta ncodes
  1304.             sty ncodes+1
  1305.             ldy #>512               ;256 of length 9 then 512 of length 10 etc.
  1306.             sty wtcl
  1307.             lda #9                  ;code length=9
  1308.             sta cdlen
  1309.             lda #0                  ;code counter
  1310.             sta cdcnt
  1311.             rts                     ;done
  1312. ;
  1313. cdcnt       .byt 0
  1314. ;
  1315. ;-------------------------------------------
  1316. ; lempel-zev add string to table subroutine
  1317. ;-------------------------------------------
  1318. ;
  1319. lzadd       lda ncodes+1            ;don't add if table is full
  1320.             cmp cdmaxx
  1321.             bcc lza1                ;its ok-add it
  1322.             rts
  1323. ;
  1324. lza1        sta poker+1             ;prefix(ncodes)=omega
  1325.             lda ncodes
  1326.             sta poker
  1327.             bit meth
  1328.             bvs nohash
  1329.             jsr hash
  1330. nohash      ldy omega+1
  1331.             lda #>pfxhi
  1332.             jsr poke
  1333.             ldy omega
  1334.             lda #>pfxlo
  1335.             jsr poke
  1336.             ldy kay                 ;extension(ncodes)=kay
  1337.             lda #>ext
  1338.             jsr poke
  1339.             bit meth
  1340.             bvs lza3
  1341.             ldy #0                  ;flag this code as used
  1342.             lda #>used
  1343.             jsr poke
  1344. lza3        inc ncodes              ;and finally bump number of codes
  1345.             bne lza4
  1346.             inc ncodes+1
  1347. lza4        jmp bcl
  1348. ;
  1349. poke        sta pk1+1               ;store .y in table .a at offset in poker
  1350.             lda poker+1
  1351.             and cmxm1
  1352. pk1         ora #0
  1353.             sta poker+1
  1354.             tya
  1355.             ldy #0
  1356.             sta (poker),y
  1357.             rts
  1358. ;
  1359. ;-------------------------------
  1360. ; subroutines. get/put pointers
  1361. ;-------------------------------
  1362. ;
  1363. getexc      lda check+1             ;get extension(check)
  1364.             and cmxm1
  1365.             ora #>ext
  1366.             sta poker+1
  1367.             lda check
  1368.             sta poker
  1369. peek        ldy #0
  1370.             lda (poker),y
  1371.             rts
  1372. ;
  1373. bcl         pha
  1374.             lda cdlen               ;is code 12 bits?
  1375.             cmp cdmax
  1376.             bcs bclrt               ;if so don't adjust length
  1377.             lda wtcl
  1378.             and ncodes+1
  1379.             beq bclrt
  1380.             inc cdlen               ;counted to zero. bump code length
  1381.             asl wtcl                ;and do twice as many next time
  1382. bclrt       pla
  1383.             rts
  1384. ;
  1385. ; initialize tables
  1386. ;
  1387. init        jsr inn
  1388.             ldy #0
  1389.             lda #>pfxhi
  1390.             sta poker+1
  1391.             tya
  1392.             sta poker
  1393.             lda #$80
  1394. init1       sta (poker),y           ;set all 'used' flags to No
  1395.             iny
  1396.             bne init1
  1397.             inc poker+1
  1398.             ldx poker+1
  1399.             cpx #>ext
  1400.             bne init1
  1401.             lda #$ff
  1402.             sta omega
  1403.             sta omega+1
  1404.             ldx #0
  1405. init0       stx kay
  1406.             jsr hash
  1407.             ldy kay
  1408.             lda #>ext
  1409.             jsr poke
  1410.             ldy #$ff
  1411.             lda #>pfxlo
  1412.             jsr poke
  1413.             ldy #$ff
  1414.             lda #>pfxhi
  1415.             jsr poke
  1416.             ldy #0
  1417.             lda #>used
  1418.             jsr poke
  1419.             ldx kay
  1420.             inx
  1421.             bne init0
  1422.             rts
  1423. ;
  1424. inn         lda #<next
  1425.             sta poker
  1426.             lda #>next
  1427.             sta poker+1
  1428.             ldy #0
  1429.             tya
  1430.             ldx #16
  1431. nxt0        sta (poker),y
  1432.             iny
  1433.             bne nxt0
  1434.             inc poker+1
  1435.             dex
  1436.             bne nxt0
  1437.             rts
  1438. ;
  1439. ;xibm.usq
  1440. ;=====================
  1441. ; un-squeeze routines
  1442. ;=====================
  1443. ;
  1444. usqtab      jsr bytin               ;get node count
  1445.             sta ndc
  1446.             jsr bytin
  1447.             sta ndc+1
  1448.             cmp #1
  1449.             beq n256                ;256 nodes?
  1450.             bcc usqt0               ;less. get table
  1451. badsq       jsr primm
  1452.             .asc 13,"Error...invalid decode tree.", 0
  1453.             jmp done
  1454. ;
  1455. ndc         pla
  1456.             tay
  1457. nndc        pla
  1458.             rts
  1459. ;
  1460. n256        lda ndc
  1461.             bne badsq
  1462.             beq usqt0
  1463. ;
  1464. usqt1       lda ndc                 ;must be at least one node!
  1465.             ora ndc+1
  1466.             beq badsq
  1467. usqt0       ldy #0                  ;get tree
  1468.             ldx #0
  1469. usqt3       jsr bytin
  1470.             sta $4000,y             ;left low
  1471.             jsr bytin
  1472.             sta $4100,y             ;left high
  1473.             jsr bytin
  1474.             sta $4200,y             ;right low
  1475.             jsr bytin
  1476.             sta $4300,y             ;right high
  1477.             iny
  1478.             bne usqt2
  1479.             inx
  1480. usqt2       cpy ndc
  1481.             bne usqt3
  1482.             cpx ndc+1
  1483.             bne usqt3
  1484.             rts
  1485. ;
  1486. ;--------------------
  1487. ; input huffman code
  1488. ;--------------------
  1489. ;
  1490. hufin       ldy #0
  1491. bt          jsr bitin
  1492.             bcc left
  1493. right       lda $4300,y
  1494.             bmi gr
  1495.             lda $4200,y
  1496.             tay
  1497.             jmp bt
  1498. ;
  1499. gr          eor #$ff
  1500.             bne eosq
  1501.             lda $4200,y
  1502.             eor #$ff
  1503.             clc
  1504.             rts
  1505. ;
  1506. left        lda $4100,y
  1507.             bmi gl
  1508.             lda $4000,y
  1509.             tay
  1510.             jmp bt
  1511. ;
  1512. gl          eor #$ff
  1513.             bne eosq
  1514.             lda $4000,y
  1515.             eor #$ff
  1516.             clc
  1517.             rts
  1518. ;
  1519. eosq        sec
  1520.             rts
  1521. ;
  1522. ;xibm.hash
  1523. ;======================================
  1524. ; hash functions for old style crunch
  1525. ;=====================================
  1526. ;
  1527. oldh        rti                     ;flag old/new hash function in bit 7
  1528. ;
  1529. ; old hash = [(pfx+ext) OR $0800]^2  taking middle 12 bits
  1530. ; new hash = [(pfx+ext) * 15073]     taking lower  12 bits
  1531. ;
  1532. hash        lda method              ;5,6=old 7=new
  1533.             cmp #7
  1534.             ror oldh                ;bmi for new hash
  1535.             clc                     ;start with omega+kay
  1536.             lda omega
  1537.             adc kay
  1538.             sta n1
  1539.             lda omega+1
  1540.             adc #0
  1541.             bit oldh                ;or with $0800 if old hash
  1542.             bmi hash0
  1543.             ora #8
  1544.             sta n1+1
  1545.             sta n2+1                ;n1=n2 for old hash
  1546.             lda n1
  1547.             sta n2
  1548.             jmp mul                 ;do n1*n2
  1549. ;
  1550. hash0       sta n1+1                ;n2=15073 for new hash
  1551.             lda #<15073
  1552.             sta n2
  1553.             lda #>15073
  1554.             sta n2+1
  1555. mul         lda #0                  ;calculate n1*n2
  1556.             sta poker
  1557.             sta poker+1
  1558.             sta poker+2
  1559.             sta n1+2
  1560.             sta n2+2
  1561.             ldy #24
  1562. addlp       lsr n2+2
  1563.             ror n2+1
  1564.             ror n2
  1565.             bcc noadd
  1566.             clc
  1567.             lda n1
  1568.             adc poker
  1569.             sta poker
  1570.             lda n1+1
  1571.             adc poker+1
  1572.             sta poker+1
  1573.             lda n1+2
  1574.             adc poker+2
  1575.             sta poker+2
  1576. noadd       asl n1
  1577.             rol n1+1
  1578.             rol n1+2
  1579.             dey
  1580.             bne addlp
  1581.             bit oldh                ;take middle bits of result for old hash
  1582.             bmi agin                ;take lower 12 bits if new hash
  1583.             ldy #6
  1584. lpr         lsr poker+2
  1585.             ror poker+1
  1586.             ror poker
  1587.             dey
  1588.             bne lpr
  1589. agin        lda poker+1             ;now have hash value in poker
  1590.             and #$0f                ;save it and see if it's in use
  1591.             sta local+1
  1592.             pha
  1593.             lda poker
  1594.             sta local
  1595.             pla
  1596.             ora #>used
  1597.             sta poker+1
  1598.             ldy #0
  1599.             lda (poker),y
  1600.             bpl yoused              ;it is...
  1601.             rts                     ;its not used. return
  1602. ;
  1603. ;
  1604. ; hash resulted in a collision
  1605. ;
  1606. yoused      lda local+1             ;trace it back to its root
  1607.             and #$0f
  1608.             ora #>next
  1609.             sta local+1
  1610.             ldy #0
  1611.             lda (local),y
  1612.             beq root
  1613.             pha
  1614.             lda local+1
  1615.             and #$0f
  1616.             ora #>neext
  1617.             sta local+1
  1618.             lda (local),y
  1619.             sta local
  1620.             pla
  1621.             sta local+1
  1622.             jmp yoused
  1623. ;
  1624. root        clc
  1625.             lda local
  1626.             adc #101
  1627.             sta poker
  1628.             lda local+1
  1629.             adc #0
  1630.             and #$0f
  1631.             ora #>used
  1632.             sta poker+1
  1633. rt1         lda (poker),y
  1634.             bmi goth
  1635.             inc poker
  1636.             bne bmp
  1637.             inc poker+1
  1638. bmp         lda poker+1
  1639.             cmp #>ext
  1640.             bcc rt1
  1641.             lda #0
  1642.             sta poker
  1643.             lda #>used
  1644.             sta poker+1
  1645.             bne rt1
  1646. ;
  1647. goth        lda local+1
  1648.             and #$0f
  1649.             ora #>next
  1650.             sta local+1
  1651.             lda poker+1
  1652.             ldy #0
  1653.             sta (local),y
  1654.             lda local+1
  1655.             and #$0f
  1656.             ora #>neext
  1657.             sta local+1
  1658.             lda poker
  1659.             sta (local),y
  1660.             rts
  1661. ;
  1662. ;csxarc.dat
  1663. ;====================================
  1664. ; data tables for IBM un-ARC routine
  1665. ;====================================
  1666. ;
  1667. cdmax       *=*+1
  1668. cdmaxx      *=*+1
  1669. cmxm1       *=*+1
  1670. n1          *=*+3
  1671. n2          *=*+3
  1672. bytsav      *=*+1
  1673. char        *=*+1
  1674. oldchr      *=*+1
  1675. ;
  1676. header      *=*+1                     ;flag $1a=ok otherwise invalid
  1677. method      *=*+1                     ;compression method.
  1678. filenm      *=*+13                     ;filename. asciiz
  1679. sqlen       *=*+4                     ;squeezed file length
  1680. date        *=*+2                     ;date
  1681. time        *=*+2                     ;time
  1682. oldcrc      *=*+2                     ;stored crc
  1683. len         *=*+4                     ;unsqueezed file length
  1684. ;
  1685.             *   = $4000
  1686. ;
  1687. pfxlo       *=*+4096
  1688. next        *=*+4096
  1689. pfxhi       *=*+4096
  1690. used        *=*+4096
  1691. ext         *=*+4096
  1692. neext       *=*+4096
  1693.  
  1694.             .end
  1695.