Subversion Repositories Kolibri OS

Rev

Rev 1079 | Rev 2733 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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