Subversion Repositories Kolibri OS

Rev

Rev 1102 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;;================================================================================================;;
  2. ;;//// z80.asm //// (c) Nable, 2007-2008 /////////////////////////////////////////////////////////;;
  3. ;;================================================================================================;;
  4. ;;                                                                                                ;;
  5. ;; This file is part of Common development libraries (Libs-Dev).                                  ;;
  6. ;;                                                                                                ;;
  7. ;; Libs-Dev is free software: you can redistribute it and/or modify it under the terms of the GNU ;;
  8. ;; Lesser General Public License as published by the Free Software Foundation, either version 2.1 ;;
  9. ;; of the License, or (at your option) any later version.                                         ;;
  10. ;;                                                                                                ;;
  11. ;; Libs-Dev is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without  ;;
  12. ;; even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU  ;;
  13. ;; Lesser General Public License for more details.                                                ;;
  14. ;;                                                                                                ;;
  15. ;; You should have received a copy of the GNU Lesser General Public License along with Libs-Dev.  ;;
  16. ;; If not, see <http://www.gnu.org/licenses/>.                                                    ;;
  17. ;;                                                                                                ;;
  18. ;;================================================================================================;;
  19. ;;                                                                                                ;;
  20. ;; References:                                                                                    ;;
  21. ;;   1.                                                                                           ;;
  22. ;;                                                                                                ;;
  23. ;;================================================================================================;;
  24.  
  25. include 'z80.inc'
  26.  
  27. ;;================================================================================================;;
  28. proc img.is.z80 _data, _length ;//////////////////////////////////////////////////////////////////;;
  29. ;;------------------------------------------------------------------------------------------------;;
  30. ;? Determine if raw data could be decoded (is in z80 screen format)                               ;;
  31. ;;------------------------------------------------------------------------------------------------;;
  32. ;> _data = raw data as read from file/stream                                                      ;;
  33. ;> _length = data length                                                                          ;;
  34. ;;------------------------------------------------------------------------------------------------;;
  35. ;< eax = false / true                                                                             ;;
  36. ;;================================================================================================;;
  37.         xor eax,eax
  38.         cmp [_length],6929
  39.         setz al
  40.         je @f
  41.         cmp [_length],6912
  42.         setz al
  43. @@:    
  44.         ret
  45. endp
  46.  
  47. ;;================================================================================================;;
  48. proc img.decode.z80 _data, _length, _options ;////////////////////////////////////////////////////;;
  49. ;;------------------------------------------------------------------------------------------------;;
  50. ;? Decode data into image if it contains correctly formed raw data in z80 screen format           ;;
  51. ;;------------------------------------------------------------------------------------------------;;
  52. ;> _data = raw data as read from file/stream                                                      ;;
  53. ;> _length = data length                                                                          ;;
  54. ;;------------------------------------------------------------------------------------------------;;
  55. ;< eax = 0 (error) or pointer to image                                                            ;;
  56. ;;================================================================================================;;
  57. ;---------------------------------------------------------------------------------------------------
  58. ;During the decoding:
  59. ;bl - PixelLeft (this means how much pixels left to put in current string)
  60. ;bh - CurrentString
  61. ;High half of ebx - use DualStos (two frames per one pixel_write)
  62. ;cl - PixelColorIndexInPalette
  63. ;ch - BackgroundColorIndexInPalette
  64. ;High half of ecx - blinking flag
  65. ;edx - address of current attribute byte
  66. ;---------------------------------------------------------------------------------------------------
  67. locals
  68.   frame1           dd ?
  69.   OffsetIn2ndFrame dd ?
  70. endl
  71.         xor     eax,eax
  72.         pushad
  73.         cld                                             ;paranoia
  74.         stdcall img.create,256,192,Image.bpp8i
  75.         test eax,eax
  76.         jz      img.decode.z80.locret                   ;test if allocation failed
  77.         mov     [frame1],eax
  78.         mov     esi,z80._._16color_palette
  79.         mov     ecx,16
  80.         mov     edi,[eax+Image.Palette]
  81.         rep movsd                               ;write palette for the first frame
  82.         mov     esi,[_data]
  83.         cmp     [_length],6929
  84.     jne  @f
  85.     add  esi,17 ;in case of 6929 byte files we just skip the info in the begininning.
  86. @@:
  87. ;---------------------------------------------------------------------------------------------------
  88. ;At first we'll determine if there are any blinking pixels
  89. ;if no - we'll produce statical single image
  90.     mov  ecx,768
  91.     lea  edx,[esi+6912-768];edx points to attribute area
  92.     xor  ebx,ebx          ;begin from <0,0> (for further decoding)
  93. @@:
  94.     test byte[edx+ecx-1],z80.BlinkFlag ;such addressing is a good optimisation
  95.                                         ;(as I hope), edx is unchanged
  96.     jnz  .decode_z80_with_blinking
  97.     loop @b
  98. .decode_z80_without_blinking:
  99.     jmp  .decode_z80_main_stage
  100.  
  101. .decode_z80_with_blinking:
  102.     or   ebx,0xFFFF0000         ;use DualStos
  103.         mov     ecx,eax                         ;eax still points to the first frame
  104.         stdcall img.create,256,192,Image.bpp8i
  105.         test eax,eax
  106.         jz      img.decode.z80.failed
  107.         mov     [eax+Image.Previous],ecx
  108.         mov     [ecx+Image.Next],eax
  109.         mov     esi,z80._._16color_palette
  110.         mov     ecx,16
  111.         mov     edi,[eax+Image.Palette]
  112.         rep movsd                               ;write palette for the second frame
  113.         mov eax,[eax+Image.Data]
  114.         mov     [OffsetIn2ndFrame],eax
  115. ;-------------------------------------------------------------------------------
  116. .decode_z80_main_stage:
  117. ;2nd stage - convert z80 screen to 8bpp image with palette
  118. .decode_z80_main_stage_main_loop:
  119.     test bl,7
  120.     jnz  .decode_z80_main_stage_put_now
  121.  
  122. ._z80_update_attributes:
  123.     movsx ecx,byte[edx]   ;note that BlinkFlag is the highest bit in attribute
  124.                           ;byte, so ecx's highest bit is set automatically
  125.     shl  ecx,5
  126.     shr  cl,5
  127.     and  ch,0xF
  128.     test ch,1000b
  129.     jz   @f
  130.     or   ecx,1000b        ;it has the same size with 'or cl,1000b' but could be faster
  131. @@:
  132.     inc  edx
  133.  
  134.     lodsb
  135.     mov  ah,al
  136.  
  137. .decode_z80_main_stage_put_now:
  138.     shl  ah,1
  139. ;-------------------------------------------------------------------------------
  140. ._z80_put_pixel:
  141. ;In: CF - put pixel with color CL, !CF - pixel with color CH
  142. ;High parts of ebx and ecx - as described above
  143.     mov  al,cl            ;'mov' doesn't affect flags
  144.     jc   @f
  145.     mov  al,ch
  146. @@:
  147.     stosb                 ;'stosb' doesn't affect flags
  148.  
  149.     test ebx,ebx
  150.     jns  @f
  151.     test ecx,ecx
  152.     jns  .1
  153.     mov  al,ch
  154. .1:
  155.         xchg [OffsetIn2ndFrame],edi
  156.         stosb
  157.         xchg [OffsetIn2ndFrame],edi
  158. @@:
  159.     inc  bl               ;next pixel
  160.     jz   .decode_z80_main_stage_row_finished
  161.     jmp  .decode_z80_main_stage_main_loop
  162. ;-------------------------------------------------------------------------------
  163. .decode_z80_main_stage_row_finished:
  164.     cmp  bh,191           ;is image finished?
  165.     jb   .decode_z80_main_stage_image_not_finished ;no.
  166. .decode_z80_finish:
  167.     jmp  .locret          ;now really finished
  168. ;-------------------------------------------------------------------------------
  169. ;or not finished yet. Branch according to a row number (see documentation)
  170. .decode_z80_main_stage_next_third:
  171.     sub  bh,7
  172.     sub  edi,256*(8-1)
  173.     jmp  .decode_z80_main_stage_main_loop
  174.  
  175. .decode_z80_main_stage_image_not_finished:
  176. ;next row
  177.     add  bh,8             ;refer to documentation
  178.     add  edi,256*(8-1)
  179.  
  180. ;if finished row is 63 or 127 then we process next third of the image
  181.     cmp  bh,63+8
  182.     je   .decode_z80_main_stage_next_third
  183.     cmp  bh,127+8
  184.     je   .decode_z80_main_stage_next_third
  185.  
  186.     cmp  bh,56+8          ;if finished row in [56;63) or [120;127) or [184;191)
  187.     jb   .decode_z80_main_stage_main_loop
  188.     cmp  bh,63+8
  189.     jb   .4
  190.     cmp  bh,120+8
  191.     jb   .decode_z80_main_stage_main_loop
  192.     cmp  bh,127+8
  193.     jb   .4
  194.     cmp  bh,184+8
  195.     jb   .decode_z80_main_stage_main_loop
  196. ;note that if we are here then bh is < 191 (see label .2) but >= 184+8
  197. .4:
  198. ;and if we here then bh is in [56;63) or [120;127) or [184;191)
  199.     sub  bh,(8+56-1)
  200.     sub  edi,256*(8+56-1)
  201.     sub  edx,z80.AttrString*8
  202.     jmp  .decode_z80_main_stage_main_loop
  203. img.decode.z80.locret:
  204.         popad
  205.         ret
  206. img.decode.z80.failed:
  207.         stdcall img.destroy,[frame1]
  208.         jmp    img.decode.z80.locret
  209. endp
  210.  
  211. ;;================================================================================================;;
  212. proc img.encode.z80 _img, _p_length, _options ;///////////////////////////////////////////////////;;
  213. ;;------------------------------------------------------------------------------------------------;;
  214. ;? Encode image into raw data in z80 screen format                                                ;;
  215. ;;------------------------------------------------------------------------------------------------;;
  216. ;> _img = pointer to image                                                                        ;;
  217. ;;------------------------------------------------------------------------------------------------;;
  218. ;< eax = 0 (error) or pointer to encoded data                                                     ;;
  219. ;< _p_length = encoded data length                                                                ;;
  220. ;;================================================================================================;;
  221.         xor     eax, eax
  222.         ret
  223. endp
  224.  
  225.  
  226. ;;================================================================================================;;
  227. ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
  228. ;;================================================================================================;;
  229. ;! Below are private procs you should never call directly from your code                          ;;
  230. ;;================================================================================================;;
  231. ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
  232. ;;================================================================================================;;
  233.  
  234. ;;================================================================================================;;
  235. ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
  236. ;;================================================================================================;;
  237. ;! Below is private data you should never use directly from your code                             ;;
  238. ;;================================================================================================;;
  239. ;;////////////////////////////////////////////////////////////////////////////////////////////////;;
  240. ;;================================================================================================;;
  241. z80._._16color_palette:
  242. dd      0                       ; black
  243. dd      0x000000b0      ; blue
  244. dd      0x00b00000      ; red
  245. dd      0x00b000b0      ; magenta
  246. dd      0x0000b000      ; green
  247. dd      0x0000b0b0      ; cyan
  248. dd      0x00b0b000      ; yellow
  249. dd      0x00b0b0b0      ; gray
  250. dd      0                       ; black
  251. dd      0x000000ff      ; light blue
  252. dd      0x00ff0000      ; light red
  253. dd      0x00ff00ff      ; light magenta
  254. dd      0x0000ff00      ; light green
  255. dd      0x0000ffff      ; light cyan
  256. dd      0x00ffff00      ; light yellow
  257. dd      0x00ffffff      ; white
  258.