Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / dev / misc / syscons / dragon / dragon_saver.c
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: src/sys/modules/syscons/dragon/dragon_saver.c,v 1.1.2.1 2003/05/11 01:17:02 murray Exp $
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 gcls(void)
126 {
127 #ifdef PC98
128         outb(0x7c, 0x80);       /* GRCG on & TDW mode */
129         outb(0x7e, 0);                  /* tile B */
130         outb(0x7e, 0);                  /* tile R */
131         outb(0x7e, 0);                  /* tile G */
132         outb(0x7e, 0);                  /* tile I */
133
134         fillw(0, vid, 0x8000);
135
136         outb(0x7c, 0);  /* GRCG off */
137 #else
138         bzero(vid, SCRW*SCRH);
139 #endif
140 }
141
142 static void
143 dragon_update(video_adapter_t *adp)
144 {
145         static int      i, p, q;
146         static int      order, mul, out;
147         static int      org_x, org_y;
148         static int      dx, dy;
149         static unsigned char    fold[1 << (ORDER - 3)];
150 #define GET_FOLD(x)     (fold[(x) >> 3]  &  (1 << ((x) & 7)))
151 #define SET_FOLD(x)     (fold[(x) >> 3] |=  (1 << ((x) & 7)))
152 #define CLR_FOLD(x)     (fold[(x) >> 3] &= ~(1 << ((x) & 7)))
153         int     tmp;
154
155         if (curve > CURVE) {
156                 gcls();
157
158                 /* set palette of each curves */
159                 for (tmp = 0; tmp < 3*CURVE; ++tmp) {
160                         dragon_pal[3+tmp] = (u_char)random(); 
161                 }
162                 load_palette(adp, dragon_pal);
163
164                 mul = ((random() & 7) + 1) * (SCRW / 320);
165                 org_x = random() % SCRW; org_y = random() % SCRH;
166
167                 curve = 0;
168                 order = ORDER;
169         }
170
171         if (order >= ORDER) {
172                 ++curve;
173
174                 cur_x = org_x; cur_y = org_y;
175
176                 switch (curve) {
177                 case 1:
178                         dx = 0; dy = mul;
179                         break;
180                 case 2:
181                         dx = mul; dy = 0;
182                         break;
183                 case 3:
184                         dx = 0; dy = -mul;
185                         break;
186                 }
187                 (void)gdraw(dx, dy, curve); out = 0;
188
189                 order = 0;
190                 q = p = 0; i = q + 1;
191         }
192
193         if (i > q) {
194                 SET_FOLD(p); q = p * 2;
195
196                 ++order;
197                 i = p; p = q + 1;
198         }
199
200         if (GET_FOLD(q-i) != 0) {
201                 CLR_FOLD(i);
202                 tmp = dx; dx =  dy; dy = -tmp;  /* turn right */
203         }
204         else {
205                 SET_FOLD(i);
206                 tmp = dx; dx = -dy; dy =  tmp;  /* turn left */
207         }
208         if (gdraw(dx, dy, curve)) {
209                 out = 0;
210         }
211         else {
212                 if (++out > OUT) {
213                         order = ORDER;  /* force to terminate this curve */
214                 }
215         }
216         ++i;
217 }
218
219 static int
220 dragon_saver(video_adapter_t *adp, int blank)
221 {
222         int pl;
223
224         if (blank) {
225                 /* switch to graphics mode */
226                 if (blanked <= 0) {
227                         pl = splhigh();
228                         set_video_mode(adp, VIDEO_MODE);
229                         vid = (u_char *)adp->va_window;
230                         curve = CURVE + 1;
231                         ++blanked;
232                         splx(pl);
233                 }
234
235                 /* update display */
236                 dragon_update(adp);
237         }
238         else {
239                 blanked = 0;
240         }
241         return 0;
242 }
243
244 static int
245 dragon_init(video_adapter_t *adp)
246 {
247         video_info_t info;
248
249         /* check that the console is capable of running in 320x200x256 */
250         if (get_mode_info(adp, VIDEO_MODE, &info)) {
251                 log(LOG_NOTICE,
252                     "%s: the console does not support " VIDEO_MODE_NAME "\n",
253                     SAVER_NAME);
254                 return ENODEV;
255         }
256
257         blanked = 0;
258         return 0;
259 }
260
261 static int
262 dragon_term(video_adapter_t *adp)
263 {
264         return 0;
265 }
266
267 static scrn_saver_t dragon_module = {
268         SAVER_NAME,
269         dragon_init,
270         dragon_term,
271         dragon_saver,
272         NULL,
273 };
274
275 SAVER_MODULE(dragon_saver, dragon_module);