1; GEOS KERNAL by Berkeley Softworks
2; reverse engineered by Maciej Witkowiak, Michael Steil
3;
4; Graphics library: GetScanLine syscall
5
6.include "const.inc"
7.include "geossym.inc"
8.include "geosmac.inc"
9.include "config.inc"
10.include "gkernal.inc"
11.include "c64.inc"
12.include "kernal.inc"
13
14.global _GetScanLine
15
16.segment "graph2n"
17
18.import _DMult
19.import g_col1
20
21.setcpu "65c02"
22
23; The GetScanLine API is no longer supported. On C64
24; GEOS, callers of this function could safely assume the
25; VIC-II bitmap layout and that the bitmap is actually
26; stored in CPU memory on the current bank. Neither of
27; this is the case on a system with a VERA. deskTop 2.0
28; for example would trash CPU memory if this returned
29; real offsets into video RAM. Therefore, to all existing
30; GEOS apps, we return a fake address that cannot cause
31; any harm.
32_GetScanLine:
33	LoadW r5, $ff00
34	LoadW r6, $ff00
35	rts
36
37.include "banks.inc"
38.import gjsrfar
39
40.export _DrawLine, _DrawPoint, _FrameRectangle, _ImprintRectangle, _InvertRectangle, _RecoverRectangle, _Rectangle, _TestPoint, _HorizontalLine, _InvertLine, _RecoverLine, _VerticalLine, _GRAPH_start_direct, _GRAPH_set_pixel
41
42.macro jsrfar addr
43	jsr gjsrfar
44	.word addr
45	.byte BANK_KERNAL
46.endmacro
47
48;---------------------------------------------------------------
49; DrawLine                                                $C130
50;
51; Pass:      signFlg  set to recover from back screen
52;                     reset for drawing
53;            carryFlg set for drawing in forground color
54;                     reset for background color
55;            r3       x pos of 1st point (0-319)
56;            r11L     y pos of 1st point (0-199)
57;            r4       x pos of 2nd point (0-319)
58;            r11H     y pos of 2nd point (0-199)
59; Return:    -
60; Destroyed: a, x, y, r4 - r8, r11
61;---------------------------------------------------------------
62_DrawLine:
63	php
64	plx
65
66	PushW r0
67	PushW r1
68	PushW r2
69	PushW r3
70	MoveW r3, r0
71	MoveB r11L, r1L
72	stz r1H
73	MoveB r11H, r3L
74	stz r3H
75	MoveW r4, r2
76
77	phx
78	plp
79	bmi @3 ; recover
80; draw
81	lda #0
82	rol
83	eor #1
84	php
85	sei
86	jsrfar GRAPH_set_colors
87	plp
88
89	lda #0
90	bra @2 ; N=0 -> draw
91@3:	sec ; N=1, C=1 -> recover
92@2:
93	php
94	sei
95	jsrfar GRAPH_draw_line
96	plp
97
98	PopW r3
99	PopW r2
100	PopW r1
101	PopW r0
102	rts
103
104
105;---------------------------------------------------------------
106; DrawPoint                                               $C133
107;
108; Pass:      r3       x pos of point (0-319)
109;            r11L     y pos of point (0-199)
110;            carryFlg color: 1: black; 0: white
111;            signFlg  0: draw color; 1: recover
112; Return:    -
113; Destroyed: a, x, y, r5 - r6
114;---------------------------------------------------------------
115_DrawPoint:
116	bmi @3 ; recover
117; draw
118	lda #0
119	rol
120	eor #1
121
122	pha
123	PushW r0
124	PushW r1
125	MoveW r3, r0
126	MoveB r11L, r1L
127	jsr _GRAPH_start_direct
128	PopW r1
129	PopW r0
130	pla
131
132	jmp _GRAPH_set_pixel
133
134; recover a point: use DrawLine
135@3:	PushW r4
136	PushB r11H
137	MoveW r3, r4
138	MoveB r11L, r11H
139	jsr _DrawLine
140	PopB r11H
141	PopW r4
142	rts
143
144;---------------------------------------------------------------
145; FrameRectangle                                          $C127
146;
147; Pass:      a   pattern byte
148;            r2L top (0-199)
149;            r2H bottom (0-199)
150;            r3  left (0-319)
151;            r4  right (0-319)
152; Return:    r2L, r3H unchanged
153; Destroyed: a, x, y, r5 - r9, r11
154;---------------------------------------------------------------
155_FrameRectangle:
156	jsr Convert8BitPattern
157
158	PushW r0
159	PushW r1
160	PushW r2
161	PushW r3
162
163	; r0: x
164	MoveW r3, r0
165	; r1: y
166	MoveB r2L, r1L
167	stz r1H
168	; r2: width
169	lda r4L
170	sec
171	sbc r3L
172	tax
173	lda r4H
174	sbc r3H
175	tay
176	; r3: height
177	lda r2H
178	sec
179	sbc r2L
180	inc
181	sta r3L
182	stz r3H
183	; store r2
184	inx
185	bne :+
186	iny
187:	stx r2L
188	sty r2H
189
190	php
191	sei
192	clc ; no fill
193	jsrfar GRAPH_draw_rect
194	plp
195
196	PopW r3
197	PopW r2
198	PopW r1
199	PopW r0
200
201	rts
202
203;---------------------------------------------------------------
204; ImprintRectangle                                        $C250
205;
206; Pass:      r2L top (0-199)
207;            r2H bottom (0-199)
208;            r3  left (0-319)
209;            r4  right (0-319)
210; Return:    r2L, r3H unchanged
211; Destroyed: a, x, y, r5 - r8, r11
212;---------------------------------------------------------------
213_ImprintRectangle:
214	; TODO
215	rts
216
217;---------------------------------------------------------------
218; InvertRectangle                                         $C12A
219;
220; Pass:      r2L top in scanlines (0-199)
221;            r2H bottom in scanlines (0-199)
222;            r3  left in pixels (0-319)
223;            r4  right in pixels (0-319)
224; Return:    r2L, r3H unchanged
225; Destroyed: a, x, y, r5 - r8
226;---------------------------------------------------------------
227_InvertRectangle:
228	MoveB r2L, r11L
229@1:	jsr _InvertLine
230	lda r11L
231	inc r11L
232	cmp r2H
233	bne @1
234	rts
235
236;---------------------------------------------------------------
237; RecoverRectangle                                        $C12D
238;
239; Pass:      r2L top (0-199)
240;            r2H bottom (0-199)
241;            r3  left (0-319)
242;            r4  right (0-319)
243; Return:    rectangle recovered from backscreen
244; Destroyed: a, x, y, r5 - r8, r11
245;---------------------------------------------------------------
246_RecoverRectangle:
247	; TODO
248	rts
249
250;---------------------------------------------------------------
251; Rectangle                                               $C124
252;
253; Pass:      r2L top (0-199)
254;            r2H bottom (0-199)
255;            r3  left (0-319)
256;            r4  right (0-319)
257; Return:    draws the rectangle
258; Destroyed: a, x, y, r5 - r8, r11
259;---------------------------------------------------------------
260_Rectangle:
261	lda g_col1
262	tax
263	php
264	sei
265	jsrfar GRAPH_set_colors
266	plp
267
268	PushW r0
269	PushW r1
270	PushW r2
271	PushW r3
272
273	; r0: x
274	MoveW r3, r0
275	; r1: y
276	MoveB r2L, r1L
277	stz r1H
278	; r2: width
279	lda r4L
280	sec
281	sbc r3L
282	tax
283	lda r4H
284	sbc r3H
285	tay
286	; r3: height
287	lda r2H
288	sec
289	sbc r2L
290	inc
291	sta r3L
292	stz r3H
293	; store r2
294	inx
295	bne :+
296	iny
297:	stx r2L
298	sty r2H
299
300	php
301	sei
302	sec ; fill
303	jsrfar GRAPH_draw_rect
304	plp
305
306	PopW r3
307	PopW r2
308	PopW r1
309	PopW r0
310	rts
311
312;---------------------------------------------------------------
313; TestPoint                                               $C13F
314;
315; Pass:      r3   x position of pixel (0-319)
316;            r11L y position of pixel (0-199)
317; Return:    carry set if bit is set
318; Destroyed: a, x, y, r5, r6
319;---------------------------------------------------------------
320_TestPoint:
321	PushW r0
322	PushW r1
323	MoveW r3, r0
324	MoveB r11L, r1L
325	stz r1H
326	php
327	sei
328	jsr gjsrfar
329	.word FB_get_pixel
330	.byte BANK_KERNAL
331	plp
332	tax
333	PopW r1
334	PopW r0
335	cpx #0  ; black
336	beq @1
337	cpx #16 ; also black
338	beq @1
339	clc
340	rts
341@1:	sec
342	rts
343
344;---------------------------------------------------------------
345; HorizontalLine                                          $C118
346;
347; Pass:      a    pattern byte
348;            r3   x in pixel of left end (0-319)
349;            r4   x in pixel of right end (0-319)
350;            r11L y position in scanlines (0-199)
351; Return:    r11L unchanged
352; Destroyed: a, x, y, r5 - r8, r11
353;---------------------------------------------------------------
354_HorizontalLine:
355	jsr Convert8BitPattern
356	PushW r0
357	PushW r1
358	PushW r2
359	PushW r3
360	MoveW r3, r0
361	MoveB r11L, r1L
362	stz r1H
363	MoveW r4, r2
364	MoveB r11L, r3L
365	stz r3H
366
367	php
368	sei
369	lda #0 ; N=0 -> draw
370	jsrfar GRAPH_draw_line
371	plp
372
373	PopW r3
374	PopW r2
375	PopW r1
376	PopW r0
377	rts
378
379;---------------------------------------------------------------
380; InvertLine                                              $C11B
381;
382; Pass:      r3   x pos of left endpoint (0-319)
383;            r4   x pos of right endpoint (0-319)
384;            r11L y pos (0-199)
385; Return:    r3-r4 unchanged
386; Destroyed: a, x, y, r5 - r8
387;---------------------------------------------------------------
388_InvertLine:
389	PushW r0
390	PushW r1
391	MoveW r3, r0
392	MoveB r11L, r1L
393	stz r1H
394	jsr _GRAPH_start_direct
395	MoveW r4, r0
396	SubW r3, r0
397	IncW r0
398
399	PushW r12
400	PushB r13L
401	LoadW r1, r12L  ; pointer to code
402	LoadB r12L, $49 ; EOR #
403	LoadB r12H, $01 ;      1
404	LoadB r13L, $60 ; RTS
405
406	php
407	sei
408	jsrfar FB_filter_pixels
409	plp
410
411	PopB r12L
412	PopW r13
413	PopW r1
414	PopW r0
415	rts
416
417;---------------------------------------------------------------
418; RecoverLine                                             $C11E
419;
420; Pass:      r3   x pos of left endpoint (0-319)
421;            r4   x pos of right endpoint (0-319)
422;            r11L y pos of line (0-199)
423; Return:    copies bits of line from background to
424;            foreground sceen
425; Destroyed: a, x, y, r5 - r8
426;---------------------------------------------------------------
427_RecoverLine:
428	; TODO
429	rts
430
431;---------------------------------------------------------------
432; VerticalLine                                            $C121
433;
434; Pass:      a pattern byte
435;            r3L top of line (0-199)
436;            r3H bottom of line (0-199)
437;            r4  x position of line (0-319)
438; Return:    draw the line
439; Destroyed: a, x, y, r4 - r8, r11
440;---------------------------------------------------------------
441_VerticalLine:
442	jsr Convert8BitPattern
443	PushW r0
444	PushW r1
445	PushW r2
446	PushW r3
447	MoveW r4, r0
448	MoveB r3L, r1L
449	stz r1H
450	MoveW r4, r2
451	MoveB r3H, r3L
452	stz r3H
453
454	php
455	sei
456	lda #0 ; N=0 -> draw
457	jsrfar GRAPH_draw_line
458	plp
459
460	PopW r3
461	PopW r2
462	PopW r1
463	PopW r0
464	rts
465
466_GRAPH_start_direct:
467	php
468	sei
469	jsrfar FB_cursor_position
470	plp
471	rts
472
473_GRAPH_set_pixel:
474	php
475	sei
476	jsrfar FB_set_pixel
477	plp
478	rts
479
480;---------------------------------------------------------------
481; Color compatibility logic
482;---------------------------------------------------------------
483
484; in compat mode, this converts 8 bit patterns into shades of gray
485Convert8BitPattern:
486	ldx #8
487	ldy #8
488@1:	lsr
489	bcc @2
490	dey
491@2:	dex
492	bne @1
493	cpy #8
494	beq @3
495	tya
496	asl
497	ora #16
498	bra @4
499@3:	lda #16+15
500@4:	php
501	sei
502	jsrfar GRAPH_set_colors
503	plp
504	rts
505