xref: /freebsd/sys/dev/syscons/dragon/dragon_saver.c (revision 39beb93c3f8bdbf72a61fda42300b5ebed7390c8)
1 /*-
2  * Copyright (c) 2000 Chiharu Shibata
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #include	<sys/param.h>
32 #include	<sys/systm.h>
33 #include	<sys/kernel.h>
34 #include	<sys/module.h>
35 #include	<sys/syslog.h>
36 #include	<sys/consio.h>
37 #include	<sys/fbio.h>
38 
39 #include	<sys/random.h>
40 
41 #include	<dev/fb/fbreg.h>
42 #include	<dev/fb/splashreg.h>
43 #include	<dev/syscons/syscons.h>
44 
45 #define SAVER_NAME	 "dragon_saver"
46 
47 static u_char	*vid;
48 static int	blanked;
49 
50 #ifdef PC98
51 #define	VIDEO_MODE	M_PC98_EGC640x400
52 #define	VIDEO_MODE_NAME	"M_PC98_EGC640x400"
53 #define	SCRW	640
54 #define	SCRH	400
55 #else
56 #define	VIDEO_MODE	M_VGA_CG320
57 #define	VIDEO_MODE_NAME	"M_VGA_CG320"
58 #define	SCRW	320
59 #define	SCRH	200
60 #endif
61 #define	ORDER	13
62 #define	CURVE	3
63 #define	OUT	100
64 
65 static int	cur_x, cur_y;
66 static int	curve;
67 static u_char	dragon_pal[3*256];	/* zero-filled by the compiler */
68 
69 static __inline int
70 gpset(int x, int y, int val)
71 {
72 	if (x < 0 || y < 0 || SCRW <= x || SCRH <= y) {
73 		return 0;
74 	}
75 #ifdef PC98
76 	vid[(x + y * SCRW) >> 3] = (0x80 >> (x & 7));	/* write new dot */
77 #else
78 	vid[x + y * SCRW] = val;
79 #endif
80 	return 1;
81 }
82 
83 static int
84 gdraw(int dx, int dy, int val)
85 {
86 	int	i;
87 	int	set = 0;
88 
89 #ifdef PC98
90 	outb(0x7c, 0xcc);	/* GRCG on & RMW mode(disable planeI,G) */
91 	outb(0x7e, (val & 1) ? 0xff: 0);	/* tile B */
92 	outb(0x7e, (val & 2) ? 0xff: 0);	/* tile R */
93 #endif
94 	if (dx != 0) {
95 		i = cur_x;
96 		cur_x += dx;
97 		if (dx < 0) {
98 			i += dx;
99 			dx = -dx;
100 		}
101 		/* horizontal line */
102 		for (; dx >= 0; --dx, ++i) {
103 			set |= gpset(i, cur_y, val);
104 		}
105 	}
106 	else {	/* dy != 0 */
107 		i = cur_y;
108 		cur_y += dy;
109 		if (dy < 0) {
110 			i += dy;
111 			dy = -dy;
112 		}
113 		/* vertical line */
114 		for (; dy >= 0; --dy, ++i) {
115 			set |= gpset(cur_x, i, val);
116 		}
117 	}
118 #ifdef PC98
119 	outb(0x7c, 0);		/* GRCG off */
120 #endif
121 	return set;
122 }
123 
124 static void
125 dragon_update(video_adapter_t *adp)
126 {
127 	static int	i, p, q;
128 	static int	order, mul, out;
129 	static int	org_x, org_y;
130 	static int	dx, dy;
131 	static unsigned char	fold[1 << (ORDER - 3)];
132 #define	GET_FOLD(x)	(fold[(x) >> 3]  &  (1 << ((x) & 7)))
133 #define	SET_FOLD(x)	(fold[(x) >> 3] |=  (1 << ((x) & 7)))
134 #define	CLR_FOLD(x)	(fold[(x) >> 3] &= ~(1 << ((x) & 7)))
135 	int	tmp;
136 
137 	if (curve > CURVE) {
138 		vidd_clear(adp);
139 
140 		/* set palette of each curves */
141 		for (tmp = 0; tmp < 3*CURVE; ++tmp) {
142 			dragon_pal[3+tmp] = (u_char)random();
143 		}
144 		vidd_load_palette(adp, dragon_pal);
145 
146 		mul = ((random() & 7) + 1) * (SCRW / 320);
147 		org_x = random() % SCRW; org_y = random() % SCRH;
148 
149 		curve = 0;
150 		order = ORDER;
151 	}
152 
153 	if (order >= ORDER) {
154 		++curve;
155 
156 		cur_x = org_x; cur_y = org_y;
157 
158 		switch (curve) {
159 		case 1:
160 			dx = 0; dy = mul;
161 			break;
162 		case 2:
163 			dx = mul; dy = 0;
164 			break;
165 		case 3:
166 			dx = 0; dy = -mul;
167 			break;
168 		}
169 		(void)gdraw(dx, dy, curve); out = 0;
170 
171 		order = 0;
172 		q = p = 0; i = q + 1;
173 	}
174 
175 	if (i > q) {
176 		SET_FOLD(p); q = p * 2;
177 
178 		++order;
179 		i = p; p = q + 1;
180 	}
181 
182 	if (GET_FOLD(q-i) != 0) {
183 		CLR_FOLD(i);
184 		tmp = dx; dx =  dy; dy = -tmp;	/* turn right */
185 	}
186 	else {
187 		SET_FOLD(i);
188 		tmp = dx; dx = -dy; dy =  tmp;	/* turn left */
189 	}
190 	if (gdraw(dx, dy, curve)) {
191 		out = 0;
192 	}
193 	else {
194 		if (++out > OUT) {
195 			order = ORDER;	/* force to terminate this curve */
196 		}
197 	}
198 	++i;
199 }
200 
201 static int
202 dragon_saver(video_adapter_t *adp, int blank)
203 {
204 	int pl;
205 
206 	if (blank) {
207 		/* switch to graphics mode */
208 		if (blanked <= 0) {
209 			pl = splhigh();
210 			vidd_set_mode(adp, VIDEO_MODE);
211 			vid = (u_char *)adp->va_window;
212 			curve = CURVE + 1;
213 			++blanked;
214 			splx(pl);
215 		}
216 
217 		/* update display */
218 		dragon_update(adp);
219 	}
220 	else {
221 		blanked = 0;
222 	}
223 	return 0;
224 }
225 
226 static int
227 dragon_init(video_adapter_t *adp)
228 {
229 	video_info_t info;
230 
231 	/* check that the console is capable of running in 320x200x256 */
232 	if (vidd_get_info(adp, VIDEO_MODE, &info)) {
233 		log(LOG_NOTICE,
234 		    "%s: the console does not support " VIDEO_MODE_NAME "\n",
235 		    SAVER_NAME);
236 		return ENODEV;
237 	}
238 
239 	blanked = 0;
240 	return 0;
241 }
242 
243 static int
244 dragon_term(video_adapter_t *adp)
245 {
246 	return 0;
247 }
248 
249 static scrn_saver_t dragon_module = {
250 	SAVER_NAME,
251 	dragon_init,
252 	dragon_term,
253 	dragon_saver,
254 	NULL,
255 };
256 
257 SAVER_MODULE(dragon_saver, dragon_module);
258