blob: e4a08c9e0fdb23a1d151934fdd0c8d26820b0d4b [file] [log] [blame]
adamdunkelsbc70b4d2003-04-24 17:10:32 +00001/*
2 * Copyright (c) 2002, Adam Dunkels.
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 * 2. Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer in the documentation and/or other materials provided
13 * with the distribution.
14 * 3. All advertising materials mentioning features or use of this
15 * software must display the following acknowledgement:
16 * This product includes software developed by Adam Dunkels.
17 * 4. The name of the author may not be used to endorse or promote
18 * products derived from this software without specific prior
19 * written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * This file is part of the Contiki desktop environment
34 *
adamdunkels7f782822003-08-09 23:28:49 +000035 * $Id: ssfire.c,v 1.3 2003/08/09 23:28:49 adamdunkels Exp $
adamdunkelsbc70b4d2003-04-24 17:10:32 +000036 *
37 */
38
39#include <stdlib.h>
40
41#include "ctk.h"
42#include "ctk-draw.h"
adamdunkels7f782822003-08-09 23:28:49 +000043#include "ctk-mouse.h"
adamdunkelsbc70b4d2003-04-24 17:10:32 +000044#include "dispatcher.h"
45#include "loader.h"
46
adamdunkelsbc70b4d2003-04-24 17:10:32 +000047
48static DISPATCHER_SIGHANDLER(ssfire_sighandler, s, data);
49static void ssfire_idle(void);
50static struct dispatcher_proc p =
51 {DISPATCHER_PROC("Fire screensaver", ssfire_idle,
52 ssfire_sighandler,
53 NULL)};
54static ek_id_t id;
55
56
57static unsigned char flames[8*17];
58
59static unsigned char *flameptr, *colorptr1, *colorptr2;
60static unsigned char x, y;
61
62
63static const unsigned char flamecolors[16] =
64 {COLOR_BLACK, COLOR_BLACK, COLOR_BLACK,
65 COLOR_RED, COLOR_LIGHTRED, COLOR_YELLOW, COLOR_WHITE,
66 COLOR_WHITE, COLOR_WHITE, COLOR_WHITE, COLOR_WHITE,
67 COLOR_WHITE, COLOR_WHITE, COLOR_WHITE, COLOR_WHITE,
68 COLOR_WHITE};
69
70
adamdunkels7f782822003-08-09 23:28:49 +000071static void fire_init(void);
adamdunkelsbc70b4d2003-04-24 17:10:32 +000072
73/*-----------------------------------------------------------------------------------*/
74LOADER_INIT_FUNC(ssfire_init)
75{
76 if(id == EK_ID_NONE) {
77 id = dispatcher_start(&p);
adamdunkelsbc70b4d2003-04-24 17:10:32 +000078 dispatcher_listen(ctk_signal_screensaver_stop);
adamdunkels7f782822003-08-09 23:28:49 +000079 ctk_mode_set(CTK_MODE_SCREENSAVER);
80 ctk_mouse_hide();
81 fire_init();
adamdunkelsbc70b4d2003-04-24 17:10:32 +000082 }
83}
84/*-----------------------------------------------------------------------------------*/
85static void
86fire_quit(void)
87{
88 dispatcher_exit(&p);
89 id = EK_ID_NONE;
90 LOADER_UNLOAD();
91}
92/*-----------------------------------------------------------------------------------*/
93static void
94fire_init(void)
95{
96 unsigned char *ptr, *cptr;
97
98 SID.v3.freq = 0xffff;
99 SID.v3.ctrl = 0x80;
100 SID.amp = 0;
101
102 VIC.ctrl1 = 0x1b; /* $D011 */
103 VIC.addr = 0x17; /* $D018 */
104 VIC.ctrl2 = 0xc8; /* $D016 */
105 VIC.bordercolor = 0x00; /* $D020 */
106 VIC.bgcolor0 = 0x00; /* $D021 */
107 CIA2.pra = 0x03; /* $DD00 */
108
109 /* Fill screen with inverted spaces. */
110 cptr = COLOR_RAM;
111 for(ptr = (unsigned char *)0x0400;
112 ptr != (unsigned char *)0x07e8;
113 ++ptr) {
114 *ptr = 0xa0;
115 *cptr++ = 0x00;
116 }
117}
118/*-----------------------------------------------------------------------------------*/
119static
120DISPATCHER_SIGHANDLER(ssfire_sighandler, s, data)
121{
122 DISPATCHER_SIGHANDLER_ARGS(s, data);
123
adamdunkels7f782822003-08-09 23:28:49 +0000124 if(s == ctk_signal_screensaver_stop ||
125 s == dispatcher_signal_quit) {
adamdunkelsbc70b4d2003-04-24 17:10:32 +0000126 fire_quit();
127 ctk_draw_init();
adamdunkelseb4d92a2003-07-31 23:34:04 +0000128 ctk_desktop_redraw(NULL);
adamdunkelsbc70b4d2003-04-24 17:10:32 +0000129 }
130}
131/*-----------------------------------------------------------------------------------*/
132void
133ssfire_idle(void)
134{
adamdunkels7f782822003-08-09 23:28:49 +0000135 if(ctk_mode_get() == CTK_MODE_SCREENSAVER) {
adamdunkelsbc70b4d2003-04-24 17:10:32 +0000136
adamdunkels7f782822003-08-09 23:28:49 +0000137 /* Calculate new flames. */
138 asm("ldx #0");
139 asm("loop:");
140 asm("lda _flames+7,x");
141 asm("clc");
142 asm("adc _flames+8,x");
143 asm("adc _flames+9,x");
144 asm("adc _flames+16,x");
145 asm("lsr");
146 asm("lsr");
147 asm("sta _flames,x");
148 asm("inx");
149 asm("cpx #(8*15)");
150 asm("bne loop");
adamdunkelsbc70b4d2003-04-24 17:10:32 +0000151
adamdunkels7f782822003-08-09 23:28:49 +0000152 /* Fill last line with pseudo-random data from noise generator on
153 voice 3. */
154 asm("ldx #$05");
155 asm("loop2:");
156 asm("ldy #$20");
157 asm("delay:");
158 asm("dey");
159 asm("bne delay");
160 asm("lda $d41b");
161 asm("and #$0f");
162 asm("sta _flames+8*15+1,x");
163 asm("dex");
164 asm("bpl loop2");
adamdunkelsbc70b4d2003-04-24 17:10:32 +0000165
adamdunkels7f782822003-08-09 23:28:49 +0000166 /* Display flames on screen. */
167 flameptr = flames;
168 colorptr1 = COLOR_RAM + 40*10;
169 colorptr2 = colorptr1 + 0x20;
170 for(y = 0; y < 15; ++y) {
171 for(x = 0; x < 8; ++x) {
172 *colorptr1 = *colorptr2 = flamecolors[*flameptr++];
173 ++colorptr1;
174 ++colorptr2;
175 }
176 colorptr1 += 0x20;
177 colorptr2 += 0x20;
adamdunkelsbc70b4d2003-04-24 17:10:32 +0000178 }
adamdunkelsbc70b4d2003-04-24 17:10:32 +0000179
180 }
181}
182/*-----------------------------------------------------------------------------------*/
183