]> 4ch.mooo.com Git - 16.git/blob - 16/xlib/xline.asm
added xlib to the project and i gotta convert the damn makefile -.-
[16.git] / 16 / xlib / xline.asm
1 ;-----------------------------------------------------------------------\r
2 ; MODULE XLINE\r
3 ;\r
4 ; Line drawing functions.\r
5 ;\r
6 ; Compile with Tasm.\r
7 ; C callable.\r
8 ;\r
9 ;\r
10 ; ****** XLIB - Mode X graphics library                ****************\r
11 ; ******                                               ****************\r
12 ; ****** Written By Themie Gouthas                     ****************\r
13 ;\r
14 ; egg@dstos3.dsto.gov.au\r
15 ; teg@bart.dsto.gov.au\r
16 ;-----------------------------------------------------------------------\r
17 include xlib.inc\r
18 include xline.inc\r
19 \r
20         .code\r
21 \r
22 \r
23 ModeXAddr       macro\r
24         mov     cl,bl\r
25         push    dx\r
26         mov     dx,[_ScrnLogicalByteWidth]\r
27         mul     dx\r
28         pop     dx\r
29         shr     bx,2\r
30         add     bx,ax\r
31         add     bx,[PgOffs]\r
32         and     cl,3\r
33         endm\r
34 \r
35 \r
36 ;-----------------------------------------------------------------------\r
37 ; _x_line\r
38 ;\r
39 ; Line drawing function for all MODE X 256 Color resolutions\r
40 ; Based on code from "PC and PS/2 Video Systems" by Richard Wilton.\r
41 ;\r
42 ; Compile with Tasm.\r
43 ; C callable.\r
44 ;\r
45 \r
46 _x_line         proc\r
47 ARG     x1:word,y1:word,x2:word,y2:word,Color:word,PgOffs:word\r
48 LOCAL   vertincr:word,incr1:word,incr2:word,routine:word=LocalStk\r
49         push    bp              ; Set up stack frame\r
50         mov     bp,sp\r
51         sub     sp,LocalStk\r
52         push    si\r
53         push    di\r
54 \r
55         mov     ax,0a000h\r
56         mov     es,ax\r
57 \r
58         mov     dx,SC_INDEX     ; setup for plane mask access\r
59 \r
60 ; check for vertical line\r
61 \r
62         mov     si,[_ScrnLogicalByteWidth]\r
63         mov     cx,[x2]\r
64         sub     cx,[x1]\r
65         jz      VertLine\r
66 \r
67 ; force x1 < x2\r
68 \r
69         jns     L01\r
70 \r
71         neg     cx\r
72 \r
73         mov     bx,[x2]\r
74         xchg    bx,[x1]\r
75         mov     [x2],bx\r
76 \r
77         mov     bx,[y2]\r
78         xchg    bx,[y1]\r
79         mov     [y2],bx\r
80 \r
81 ; calc dy = abs(y2 - y1)\r
82 \r
83 L01:\r
84         mov     bx,[y2]\r
85         sub     bx,[y1]\r
86         jnz     short skip\r
87         jmp     HorizLine\r
88 skip:           jns     L03\r
89 \r
90         neg     bx\r
91         neg     si\r
92 \r
93 ; select appropriate routine for slope of line\r
94 \r
95 L03:\r
96         mov     [vertincr],si\r
97         mov     [routine],offset LoSlopeLine\r
98         cmp     bx,cx\r
99         jle     L04\r
100         mov     [routine],offset HiSlopeLine\r
101         xchg    bx,cx\r
102 \r
103 ; calc initial decision variable and increments\r
104 \r
105 L04:\r
106         shl     bx,1\r
107         mov     [incr1],bx\r
108         sub     bx,cx\r
109         mov     si,bx\r
110         sub     bx,cx\r
111         mov     [incr2],bx\r
112 \r
113 ; calc first pixel address\r
114 \r
115         push    cx\r
116         mov     ax,[y1]\r
117         mov     bx,[x1]\r
118         ModeXAddr\r
119         mov     di,bx\r
120         mov     al,1\r
121         shl     al,cl\r
122         mov     ah,al           ; duplicate nybble\r
123         shl     al,4\r
124         add     ah,al\r
125         mov     bl,ah\r
126         pop     cx\r
127         inc     cx\r
128         jmp     [routine]\r
129 \r
130 ; routine for verticle lines\r
131 \r
132 VertLine:\r
133         mov     ax,[y1]\r
134         mov     bx,[y2]\r
135         mov     cx,bx\r
136         sub     cx,ax\r
137         jge     L31\r
138         neg     cx\r
139         mov     ax,bx\r
140 \r
141 L31:\r
142         inc     cx\r
143         mov     bx,[x1]\r
144         push    cx\r
145         ModeXAddr\r
146 \r
147         mov     ah,1\r
148         shl     ah,cl\r
149         mov     al,MAP_MASK\r
150         out     dx,ax\r
151         pop     cx\r
152         mov     ax, word ptr [Color]\r
153 \r
154 ; draw the line\r
155 \r
156 L32:\r
157         mov     es:[bx],al\r
158         add     bx,si\r
159         loop    L32\r
160         jmp     Lexit\r
161 \r
162 ; routine for horizontal line\r
163 \r
164 HorizLine:\r
165         push    ds\r
166 \r
167         mov     ax,[y1]\r
168         mov     bx,[x1]\r
169         ModeXAddr\r
170 \r
171         mov     di,bx     ; set dl = first byte mask\r
172         mov     dl,00fh\r
173         shl     dl,cl\r
174 \r
175         mov     cx,[x2] ; set dh = last byte mask\r
176         and     cl,3\r
177         mov     dh,00eh\r
178         shl     dh,cl\r
179         not     dh\r
180 \r
181 ; determine byte offset of first and last pixel in line\r
182 \r
183         mov     ax,[x2]\r
184         mov     bx,[x1]\r
185 \r
186         shr     ax,2     ; set ax = last byte column\r
187         shr     bx,2    ; set bx = first byte column\r
188         mov     cx,ax    ; cx = ax - bx\r
189         sub     cx,bx\r
190 \r
191         mov     ax,dx    ; mov end byte masks to ax\r
192         mov     dx,SC_INDEX ; setup dx for VGA outs\r
193         mov     bx, [Color]\r
194 \r
195 ; set pixels in leftmost byte of line\r
196 \r
197         or      cx,cx      ; is start and end pt in same byte\r
198         jnz     L42        ; no !\r
199         and     ah,al      ; combine start and end masks\r
200         jmp     short L44\r
201 \r
202 L42:            push    ax\r
203         mov     ah,al\r
204         mov     al,MAP_MASK\r
205         out     dx,ax\r
206         mov     al,bl\r
207         stosb\r
208         dec     cx\r
209 \r
210 ; draw remainder of the line\r
211 \r
212 L43:\r
213         mov     ah,0Fh\r
214         mov     al,MAP_MASK\r
215         out     dx,ax\r
216         mov     al,bl\r
217         rep     stosb\r
218         pop     ax\r
219 \r
220 ; set pixels in rightmost byte of line\r
221 \r
222 L44:\r
223         mov     al,MAP_MASK\r
224         out     dx, ax\r
225         mov     byte ptr es:[di],bl\r
226         pop     ds\r
227         jmp     short Lexit\r
228 \r
229 \r
230 ; routine for dy >= dx (slope <= 1)\r
231 \r
232 LoSlopeLine:\r
233         mov     al,MAP_MASK\r
234         mov     bh,byte ptr [Color]\r
235 L10:\r
236         mov     ah,bl\r
237 \r
238 L11:\r
239         or      ah,bl\r
240         rol     bl,1\r
241         jc      L14\r
242 \r
243 ; bit mask not shifted out\r
244 \r
245         or      si,si\r
246         jns     L12\r
247         add     si,[incr1]\r
248         loop    L11\r
249 \r
250         out     dx,ax\r
251         mov     es:[di],bh\r
252         jmp     short Lexit\r
253 \r
254 L12:\r
255         add     si,[incr2]\r
256         out     dx,ax\r
257         mov     es:[di],bh\r
258         add     di,[vertincr]\r
259         loop    L10\r
260         jmp     short Lexit\r
261 \r
262 ; bit mask shifted out\r
263 \r
264 L14:            out     dx,ax\r
265         mov     es:[di],bh\r
266         inc     di\r
267         or      si,si\r
268         jns     L15\r
269         add     si,[incr1]\r
270         loop    L10\r
271         jmp     short Lexit\r
272 \r
273 L15:\r
274         add     si,[incr2]\r
275         add     di,[vertincr]\r
276         loop    L10\r
277         jmp     short Lexit\r
278 \r
279 ; routine for dy > dx (slope > 1)\r
280 \r
281 HiSlopeLine:\r
282         mov     bx,[vertincr]\r
283         mov     al,MAP_MASK\r
284 L21:            out     dx,ax\r
285         push    ax\r
286         mov     ax,[Color]\r
287         mov     es:[di],al\r
288         pop     ax\r
289         add     di,bx\r
290 \r
291 L22:\r
292         or      si,si\r
293         jns     L23\r
294 \r
295         add     si,[incr1]\r
296         loop    L21\r
297         jmp     short Lexit\r
298 \r
299 L23:\r
300         add     si,[incr2]\r
301         rol     ah,1\r
302         adc     di,0\r
303 lx21:   loop    L21\r
304 \r
305 ; return to caller\r
306 \r
307 Lexit:\r
308         pop     di\r
309         pop     si\r
310         mov     sp,bp\r
311         pop     bp\r
312         ret\r
313 \r
314 _x_line endp\r
315 \r
316 end\r
317 \1a