home *** CD-ROM | disk | FTP | other *** search
/ Commodore Disk User Volume 3 #7 / Commodore_Disk_User_Vol.3_7_1990_-.d64 / compression (.txt) < prev    next >
Commodore BASIC  |  2022-10-26  |  7KB  |  349 lines

  1. 10 ;**********************************
  2. 15 ;*       data compression         *
  3. 20 ;*--------------------------------*
  4. 25 ;*        n.higgins 1989          *
  5. 30 ;*--------------------------------*
  6. 35 ;* method: run length encoding    *
  7. 40 ;* using : marker(count,char)     *
  8. 45 ;**********************************
  9. 50 ;* first call the routines at:-   *
  10. 55 ;*                                *
  11. 60 ;*  compress - sys $c000          *
  12. 65 ;*  decomper - sys $c0b8          *
  13. 70 ;*                                *
  14. 75 ;* now enter the monitor and use  *
  15. 80 ;* the command (mc110 c170) to    *
  16. 85 ;* see the results.               *
  17. 90 ;**********************************
  18. 95 ;
  19. 100 ;
  20. 105 ;
  21. 110 ;
  22. 115 ;--------------------------------
  23. 120 ;pointer to the end of the data
  24. 125 ;to be compressed.
  25. 130 ;--------------------------------
  26. 135 ;
  27. 140 textend=text+16
  28. 145 ;
  29. 150 marker=$ef
  30. 155 datpnt=$fb
  31. 160 stopnt=$fd
  32. 165 ;
  33. 170 ;
  34. 175 *=49152
  35. 180 ;
  36. 185 ;
  37. 190 ;--------------------------------
  38. 195 ;set pointer to data to be
  39. 200 ;compressed (text)
  40. 205 ;set pointer to store the
  41. 210 ;compressed data (storage)
  42. 215 ;--------------------------------
  43. 220 ;
  44. 225 compress lda #<text
  45. 230 sta datpnt
  46. 235 lda #>text
  47. 240 sta datpnt+1
  48. 245 lda #<storage
  49. 250 sta stopnt
  50. 255 lda #>storage
  51. 260 sta stopnt+1
  52. 265 ;
  53. 270 ;
  54. 275 ;---------------------------------
  55. 280 ;set (endflag) to zero
  56. 285 ;set (count) to 1
  57. 290 ;get byte/is carry set?
  58. 295 ;yes:goto [e]
  59. 300 ;store byte
  60. 305 ;[a] get next byte/is carry set?
  61. 310 ;yes:goto [e]
  62. 315 ;is byte same as previous?
  63. 320 ;no :goto [c]
  64. 325 ;yes:increment (count)
  65. 330 ;is (count) =0
  66. 335 ;yes:output marker($ff,char)
  67. 340 ;set (count)=1/goto [a]
  68. 345 ;no :goto [a]
  69. 350 ;---------------------------------
  70. 355 ;
  71. 360 ldy #0
  72. 365 sty endflag
  73. 370 lda #1
  74. 375 sta count
  75. 380 jsr getbyte
  76. 385 bcs finish
  77. 390 sta character
  78. 395 ;
  79. 400 again jsr getbyte
  80. 405 bcs loop
  81. 410 cmp character
  82. 415 bne loop2
  83. 420 continue inc count
  84. 425 bne again
  85. 430 lda #marker
  86. 435 jsr putbyte
  87. 440 lda #$ff
  88. 445 jsr putbyte
  89. 450 lda character
  90. 455 jsr putbyte
  91. 460 jmp continue
  92. 465 ;
  93. 470 ;
  94. 475 ;--------------------------------
  95. 480 ;[c] store byte
  96. 485 ;get previous byte/is it a marker?
  97. 490 ;yes:goto [f]
  98. 495 ;no :is (count) <4
  99. 500 ;yes:goto [g]
  100. 505 ;no :goto [f]
  101. 510 ;--------------------------------
  102. 515 ;
  103. 520 loop2 sta store
  104. 525 loop lda character
  105. 530 cmp #marker
  106. 535 beq loop9
  107. 540 lda count
  108. 545 cmp #4
  109. 550 bcc loop5
  110. 555 ;
  111. 560 ;
  112. 565 ;--------------------------------
  113. 570 ;[f] output marker(count,char)
  114. 575 ;--------------------------------
  115. 580 ;
  116. 585 loop9 lda #marker
  117. 590 jsr putbyte
  118. 595 lda count
  119. 600 jsr putbyte
  120. 605 lda character
  121. 610 jsr putbyte
  122. 615 ;
  123. 620 ;
  124. 625 ;--------------------------------
  125. 630 ;[d] check flag/is it set?
  126. 635 ;yes:goto [e]
  127. 640 ;no :get stored byte
  128. 645 ;set (count) to 1/goto [a]
  129. 650 ;--------------------------------
  130. 655 ;
  131. 660 loop6 lda endflag
  132. 665 bmi finish
  133. 670 lda store
  134. 675 sta character
  135. 680 lda #1
  136. 685 sta count
  137. 690 jmp again
  138. 695 ;
  139. 700 ;
  140. 705 ;--------------------------------
  141. 710 ;[g] output char
  142. 715 ;decrement (count)
  143. 720 ;is (count)=0
  144. 725 ;yes:goto [d]
  145. 730 ;no :goto [g]
  146. 735 ;--------------------------------
  147. 740 ;
  148. 745 loop5 lda character
  149. 750 jsr putbyte
  150. 755 dec count
  151. 760 beq loop6
  152. 765 jmp loop5
  153. 770 ;
  154. 775 ;
  155. 780 ;--------------------------------
  156. 785 ;[e] when finished we store the
  157. 790 ;end address+1 of the compacted
  158. 795 ;data for the decompress routine.
  159. 800 ;--------------------------------
  160. 805 ;
  161. 810 finish lda stopnt
  162. 815 sta compend
  163. 820 lda stopnt+1
  164. 825 sta compend+1
  165. 830 rts
  166. 835 ;
  167. 840 ;
  168. 845 ;--------------------------------
  169. 850 ;check if reached the end of data
  170. 855 ;no :get next byte/clr carry/return
  171. 860 ;yes:set (endflag)/set carry/return
  172. 865 ;--------------------------------
  173. 870 ;
  174. 875 getbyte lda datpnt
  175. 880 cmp #<textend
  176. 885 bne loop12
  177. 890 lda datpnt+1
  178. 895 cmp #>textend
  179. 900 bne loop12
  180. 905 lda #$ff
  181. 910 sta endflag
  182. 915 sec
  183. 920 rts
  184. 925 ;
  185. 930 loop12 lda (datpnt),y
  186. 935 inc datpnt
  187. 940 bne loop14
  188. 945 inc datpnt+1
  189. 950 loop14 clc
  190. 955 rts
  191. 960 ;
  192. 965 ;
  193. 970 ;---------------------------------
  194. 975 ;output byte
  195. 980 ;---------------------------------
  196. 985 ;
  197. 990 putbyte sta (stopnt),y
  198. 995 inc stopnt
  199. 1000 bne loop15
  200. 1005 inc stopnt+1
  201. 1010 loop15 rts
  202. 1015 ;
  203. 1020 ;
  204. 1025 ;
  205. 1030 ;*********************************
  206. 1035 ;*     decompress routine        *
  207. 1040 ;*********************************
  208. 1045 ;* this must be called after the *
  209. 1050 ;* compression routine,it will   *
  210. 1055 ;* restore the compressed data   *
  211. 1060 ;* at 'storage' back to its      *
  212. 1065 ;* original values and place it  *
  213. 1070 ;* at 'newtext'.                 *
  214. 1075 ;*********************************
  215. 1080 ;* you can then compare both     *
  216. 1085 ;* 'text' & 'newtext' to see     *
  217. 1090 ;* that they match.              *
  218. 1095 ;*********************************
  219. 1100 ;
  220. 1105 ;
  221. 1110 ;-------------------------------
  222. 1115 ;set pointers to start of
  223. 1120 ;compressed data (storage) &
  224. 1125 ;decompressed data (newtext)
  225. 1130 ;-------------------------------
  226. 1135 ;
  227. 1140 decomper lda #<storage
  228. 1145 sta datpnt
  229. 1150 lda #>storage
  230. 1155 sta datpnt+1
  231. 1160 lda #<newtext
  232. 1165 sta stopnt
  233. 1170 lda #>newtext
  234. 1175 sta stopnt+1
  235. 1180 ;
  236. 1185 ;-------------------------------
  237. 1190 ;get compressed data
  238. 1195 ;is it a marker?
  239. 1200 ;no:output it/increment (newtext)
  240. 1205 ;increment pointer (storage)
  241. 1210 ;is it end of comp.data?
  242. 1215 ;no:go back get next byte
  243. 1220 ;yes:exit
  244. 1225 ;-------------------------------
  245. 1230 ;
  246. 1235 ldy #0
  247. 1240 reloop lda (datpnt),y
  248. 1245 cmp #marker
  249. 1250 beq loop40
  250. 1255 sta (stopnt),y
  251. 1260 jsr pnt1
  252. 1265 loop50 jsr pnt2
  253. 1270 lda datpnt
  254. 1275 cmp compend
  255. 1280 bne reloop
  256. 1285 lda datpnt+1
  257. 1290 cmp compend+1
  258. 1295 bne reloop
  259. 1300 rts
  260. 1305 ;
  261. 1310 ;-------------------------------
  262. 1315 ;if a marker is found then
  263. 1320 ;put (count) in x
  264. 1325 ;put (char) in a
  265. 1330 ;output (char) until x=0
  266. 1335 ;-------------------------------
  267. 1340 ;
  268. 1345 loop40 jsr pnt2
  269. 1350 lda (datpnt),y
  270. 1355 tax
  271. 1360 jsr pnt2
  272. 1365 lda (datpnt),y
  273. 1370 loop42 sta (stopnt),y
  274. 1375 jsr pnt1
  275. 1380 dex
  276. 1385 bne loop42
  277. 1390 beq loop50
  278. 1395 ;
  279. 1400 ;
  280. 1405 ;-----------------------------
  281. 1410 ;increment pointers
  282. 1415 ;-----------------------------
  283. 1420 ;
  284. 1425 pnt1 inc stopnt
  285. 1430 bne loop43
  286. 1435 inc stopnt+1
  287. 1440 loop43 rts
  288. 1445 ;
  289. 1450 pnt2 inc datpnt
  290. 1455 bne loop44
  291. 1460 inc datpnt+1
  292. 1465 loop44 rts
  293. 1470 ;
  294. 1475 ;
  295. 1480 ;
  296. 1485 ;
  297. 1490 ;
  298. 1495 compend byt 0,0
  299. 1500 store byt 0
  300. 1505 count byt 0
  301. 1510 character byt 0
  302. 1515 endflag byt 0
  303. 1520 ;
  304. 1525 ;--------------------------------
  305. 1530 ;these are the bytes to be
  306. 1535 ;compressed...you can use your
  307. 1540 ;own instead but if you extend
  308. 1545 ;its size then make sure you
  309. 1550 ;alter the other storage area's
  310. 1555 ;and pointers accordingly...
  311. 1560 ;--------------------------------
  312. 1565 ;
  313. 1570 text ;
  314. 1575 ;
  315. 1580 byt $01,$01,$01,$01
  316. 1585 byt $01,$01,$01,$01
  317. 1590 byt $01,$01,$01,$01
  318. 1595 byt $01,$01,$ef,$01
  319. 1600 ;
  320. 1605 ;--------------------------------
  321. 1610 ;the bytes above are compressed
  322. 1615 ;and stored here..............
  323. 1620 ;(3 bytes/for every 1 in text)
  324. 1625 ;--------------------------------
  325. 1630 storage ;
  326. 1635 ;
  327. 1640 byt 0,0,0,0,0,0,0,0
  328. 1645 byt 0,0,0,0,0,0,0,0
  329. 1650 byt 0,0,0,0,0,0,0,0
  330. 1655 byt 0,0,0,0,0,0,0,0
  331. 1660 byt 0,0,0,0,0,0,0,0
  332. 1665 byt 0,0,0,0,0,0,0,0
  333. 1670 ;
  334. 1675 ;--------------------------------
  335. 1680 ;the compressed data in 'storage'
  336. 1685 ;is decompressed & placed here...
  337. 1690 ;--------------------------------
  338. 1695 ;
  339. 1700 newtext ;
  340. 1705 ;
  341. 1710 byt 0,0,0,0,0,0,0,0
  342. 1715 byt 0,0,0,0,0,0,0,0
  343. 1720 byt 0,0,0,0,0,0,0,0
  344. 1725 byt 0,0,0,0,0,0,0,0
  345. 1730 byt 0,0,0,0,0,0,0,0
  346. 1735 byt 0,0,0,0,0,0,0,0
  347. 1740 ;
  348. 1745 ;
  349.