SCODE.ASM 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. ;
  2. ; Command & Conquer Red Alert(tm)
  3. ; Copyright 2025 Electronic Arts Inc.
  4. ;
  5. ; This program is free software: you can redistribute it and/or modify
  6. ; it under the terms of the GNU General Public License as published by
  7. ; the Free Software Foundation, either version 3 of the License, or
  8. ; (at your option) any later version.
  9. ;
  10. ; This program is distributed in the hope that it will be useful,
  11. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ; GNU General Public License for more details.
  14. ;
  15. ; You should have received a copy of the GNU General Public License
  16. ; along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. ;
  18. IDEAL
  19. IDEAL_MODE EQU 1
  20. VERSION T300 ; Code written for V3.0 TASM version.
  21. IDEAL
  22. P386N ; target machine.
  23. SMART ; Enable optimizations.
  24. WARN ; Full warnings.
  25. LOCALS ??
  26. MODEL LARGE @filename
  27. ; INCLUDE "d:\library\wwlib.i"
  28. CODE_2BIT EQU 0
  29. CODE_4BIT EQU 1
  30. CODE_RAW EQU 2
  31. CODE_SILENCE EQU 3
  32. MAGICNUMBER EQU 00000DEAFh
  33. MAGICNUMBER2 EQU 0BABEBABEh
  34. ; VERSION T300 ; Code written for V3.0 TASM version.
  35. ; P386N ; target machine.
  36. ; MODEL LARGE @filename
  37. ; WARN ; Full warnings.
  38. ; LOCALS ??
  39. CODESEG
  40. _2bitdecode DB -2, -1, 0, 1
  41. ;_2bitdecode DB -2, -1, 1, 2
  42. _4bitdecode DB -9,-8,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,8
  43. ;_4bitdecode DB -16,-13,-10,-8,-6,-4,-2,-1,1,2,4,6,8,10,13,16
  44. GLOBAL C Decompress_Frame:FAR
  45. PROC C Decompress_Frame FAR USES bx cx edx ds si es di bp
  46. ARG source:DWORD
  47. ARG dest:DWORD
  48. ARG count:WORD
  49. LOCAL previous:BYTE
  50. LOCAL incount:WORD
  51. ; Initialize counter for number of bytes read from source.
  52. mov [incount],0
  53. ; Verify parameters for legality.
  54. cmp [source],0
  55. je ??fini
  56. cmp [dest],0
  57. je ??fini
  58. cmp [count],0
  59. je ??fini
  60. ; Fetch parameter values into working registers.
  61. lds si,[source] ; Pointer to source data.
  62. les di,[dest] ; Pointer to destination data.
  63. mov cx,[count] ; Number of bytes to fill dest buffer.
  64. mov dl,080h ; Previous sample (starting value).
  65. ??mainloop:
  66. ; Check to see if the destination is full. Exit if so.
  67. cmp cx,0
  68. jle ??fini
  69. ; Fetch code byte from input stream.
  70. xor ah,ah
  71. mov al,[ds:si]
  72. inc [incount]
  73. inc si
  74. shl ax,2 ; AH contains code.
  75. shr al,2 ; AL contains sub-code data.
  76. ; Check to see if a raw sequence follows.
  77. cmp ah,CODE_RAW
  78. jne short ??try4bit
  79. ; The code contains either a 5 bit delta or a count of raw samples
  80. ; to dump out.
  81. test al,00100000b
  82. je short ??justraw
  83. ; The lower 5 bits are actually a signed delta. Sign extend the
  84. ; delta and add it to the stream.
  85. shl al,3
  86. sar al,3
  87. add dl,al
  88. mov [es:di],dl
  89. dec cx
  90. inc di
  91. jmp ??mainloop
  92. ; The lower 5 bits hold a count of the number of raw samples that
  93. ; follow this code. Dump these samples to the output buffer.
  94. ??justraw:
  95. mov bx,cx
  96. xor ah,ah
  97. inc al
  98. mov cx,ax
  99. shr cx,1
  100. rep movsw
  101. adc cx,cx
  102. rep movsb
  103. mov cx,bx
  104. add [incount],ax
  105. sub cx,ax
  106. dec di
  107. mov dl,[es:di] ; Set "previous" value.
  108. inc di
  109. jmp ??mainloop
  110. ; Check to see if this is a 4 bit delta code sequence.
  111. ??try4bit:
  112. inc al ; Following codes use AL+1
  113. cmp ah,CODE_4BIT
  114. jne short ??try2bit
  115. ; A sequence of 4bit deltas follow. AL equals the number of nibble
  116. ; packed delta bytes to process.
  117. ??bit4loop:
  118. ; Fetch nibble packed delta codes.
  119. mov ah,[ds:si]
  120. mov bl,ah
  121. inc [incount]
  122. inc si
  123. ; Add first delta to 'previous' sample already in DL.
  124. and bx,00001111b
  125. add dl,[cs:_4bitdecode+bx] ; Add in delta
  126. pushf
  127. cmp [cs:_4bitdecode+bx],0
  128. jl short ??neg1
  129. popf
  130. jnc short ??ok1
  131. mov dl,0FFh
  132. jmp short ??ok1
  133. ??neg1:
  134. popf
  135. jc short ??ok1
  136. xor dl,dl
  137. ??ok1:
  138. mov dh,dl ; DH now holds new 'previous' sample.
  139. mov bl,ah
  140. shr bl,4
  141. add dh,[cs:_4bitdecode+bx] ; Add in delta
  142. pushf
  143. cmp [cs:_4bitdecode+bx],0
  144. jl short ??neg2
  145. popf
  146. jnc short ??ok2
  147. mov dh,0FFh
  148. jmp short ??ok2
  149. ??neg2:
  150. popf
  151. jc short ??ok2
  152. xor dh,dh
  153. ??ok2:
  154. ; Output the two sample bytes.
  155. mov [es:di],dx
  156. sub cx,2
  157. add di,2
  158. ; Put the correct 'previous' sample in DL where it belongs.
  159. mov dl,dh
  160. ; If there are more deltas to process then loop back.
  161. dec al
  162. jnz short ??bit4loop
  163. jmp ??mainloop
  164. ; Check to see if 2 bit deltas need to be processed.
  165. ??try2bit:
  166. cmp ah,CODE_2BIT
  167. jne ??zerodelta
  168. ; A sequence of 2bit deltas follow. AL equals the number of
  169. ; packed delta bytes to process.
  170. ??bit2loop:
  171. ; Fetch packed delta codes.
  172. mov ah,[ds:si]
  173. inc [incount]
  174. inc si
  175. ; Add first delta to 'previous' sample already in DL.
  176. mov bl,ah
  177. and bx,000011b
  178. add dl,[cs:_2bitdecode+bx] ; Add in delta
  179. pushf
  180. cmp [cs:_2bitdecode+bx],0
  181. jl short ??neg3
  182. popf
  183. jnc short ??ok3
  184. mov dl,0FFh
  185. jmp short ??ok3
  186. ??neg3:
  187. popf
  188. jc short ??ok3
  189. xor dl,dl
  190. ??ok3:
  191. mov dh,dl
  192. ror edx,8
  193. mov bl,ah
  194. shr bx,2
  195. and bl,00000011b
  196. add dl,[cs:_2bitdecode+bx] ; Add in delta
  197. pushf
  198. cmp [cs:_2bitdecode+bx],0
  199. jl short ??neg4
  200. popf
  201. jnc short ??ok4
  202. mov dl,0FFh
  203. jmp short ??ok4
  204. ??neg4:
  205. popf
  206. jc short ??ok4
  207. xor dl,dl
  208. ??ok4:
  209. mov dh,dl
  210. ror edx,8
  211. mov bl,ah
  212. shr bx,4
  213. and bl,00000011b
  214. add dl,[cs:_2bitdecode+bx] ; Add in delta
  215. pushf
  216. cmp [cs:_2bitdecode+bx],0
  217. jl short ??neg5
  218. popf
  219. jnc short ??ok5
  220. mov dl,0FFh
  221. jmp short ??ok5
  222. ??neg5:
  223. popf
  224. jc short ??ok5
  225. xor dl,dl
  226. ??ok5:
  227. mov dh,dl
  228. ror edx,8
  229. mov bl,ah
  230. shr bx,6
  231. and bl,00000011b
  232. add dl,[cs:_2bitdecode+bx] ; Add in delta
  233. pushf
  234. cmp [cs:_2bitdecode+bx],0
  235. jl short ??neg6
  236. popf
  237. jnc short ??ok6
  238. mov dl,0FFh
  239. jmp short ??ok6
  240. ??neg6:
  241. popf
  242. jc short ??ok6
  243. xor dl,dl
  244. ??ok6:
  245. ;mov dh,dl
  246. ror edx,8
  247. ; Output the two sample bytes.
  248. mov [es:di],edx
  249. sub cx,4
  250. add di,4
  251. ; Put the correct 'previous' sample in DL where it belongs.
  252. rol edx,8
  253. ; If there are more deltas to process then loop back.
  254. dec al
  255. jnz ??bit2loop
  256. jmp ??mainloop
  257. ; There is a run of zero deltas. Zero deltas merely duplicate
  258. ; the 'previous' sample the requested number of times.
  259. ??zerodelta:
  260. xor bh,bh
  261. mov bl,al
  262. mov al,dl
  263. sub cx,bx
  264. xchg cx,bx
  265. rep stosb
  266. mov cx,bx
  267. jmp ??mainloop
  268. ; Final cleanup and exit.
  269. ??fini:
  270. mov ax,[incount]
  271. ret
  272. ENDP Decompress_Frame
  273. END