adamdunkels | bc70b4d | 2003-04-24 17:10:32 +0000 | [diff] [blame] | 1 | /* |
| 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 | * |
| 35 | * $Id: ssfire.c,v 1.1 2003/04/24 17:10:34 adamdunkels Exp $ |
| 36 | * |
| 37 | */ |
| 38 | |
| 39 | #include <stdlib.h> |
| 40 | |
| 41 | #include "ctk.h" |
| 42 | #include "ctk-draw.h" |
| 43 | #include "dispatcher.h" |
| 44 | #include "loader.h" |
| 45 | |
| 46 | static unsigned char save; |
| 47 | |
| 48 | static DISPATCHER_SIGHANDLER(ssfire_sighandler, s, data); |
| 49 | static void ssfire_idle(void); |
| 50 | static struct dispatcher_proc p = |
| 51 | {DISPATCHER_PROC("Fire screensaver", ssfire_idle, |
| 52 | ssfire_sighandler, |
| 53 | NULL)}; |
| 54 | static ek_id_t id; |
| 55 | |
| 56 | |
| 57 | static unsigned char flames[8*17]; |
| 58 | |
| 59 | static unsigned char *flameptr, *colorptr1, *colorptr2; |
| 60 | static unsigned char x, y; |
| 61 | |
| 62 | |
| 63 | static 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 | |
| 71 | |
| 72 | /*-----------------------------------------------------------------------------------*/ |
| 73 | LOADER_INIT_FUNC(ssfire_init) |
| 74 | { |
| 75 | if(id == EK_ID_NONE) { |
| 76 | id = dispatcher_start(&p); |
| 77 | dispatcher_listen(ctk_signal_screensaver_start); |
| 78 | dispatcher_listen(ctk_signal_screensaver_stop); |
| 79 | dispatcher_listen(ctk_signal_screensaver_uninstall); |
| 80 | } |
| 81 | } |
| 82 | /*-----------------------------------------------------------------------------------*/ |
| 83 | static void |
| 84 | fire_quit(void) |
| 85 | { |
| 86 | dispatcher_exit(&p); |
| 87 | id = EK_ID_NONE; |
| 88 | LOADER_UNLOAD(); |
| 89 | } |
| 90 | /*-----------------------------------------------------------------------------------*/ |
| 91 | static void |
| 92 | fire_init(void) |
| 93 | { |
| 94 | unsigned char *ptr, *cptr; |
| 95 | |
| 96 | SID.v3.freq = 0xffff; |
| 97 | SID.v3.ctrl = 0x80; |
| 98 | SID.amp = 0; |
| 99 | |
| 100 | VIC.ctrl1 = 0x1b; /* $D011 */ |
| 101 | VIC.addr = 0x17; /* $D018 */ |
| 102 | VIC.ctrl2 = 0xc8; /* $D016 */ |
| 103 | VIC.bordercolor = 0x00; /* $D020 */ |
| 104 | VIC.bgcolor0 = 0x00; /* $D021 */ |
| 105 | CIA2.pra = 0x03; /* $DD00 */ |
| 106 | |
| 107 | /* Fill screen with inverted spaces. */ |
| 108 | cptr = COLOR_RAM; |
| 109 | for(ptr = (unsigned char *)0x0400; |
| 110 | ptr != (unsigned char *)0x07e8; |
| 111 | ++ptr) { |
| 112 | *ptr = 0xa0; |
| 113 | *cptr++ = 0x00; |
| 114 | } |
| 115 | } |
| 116 | /*-----------------------------------------------------------------------------------*/ |
| 117 | static |
| 118 | DISPATCHER_SIGHANDLER(ssfire_sighandler, s, data) |
| 119 | { |
| 120 | DISPATCHER_SIGHANDLER_ARGS(s, data); |
| 121 | |
| 122 | if(s == ctk_signal_screensaver_start) { |
| 123 | fire_init(); |
| 124 | save = 1; |
| 125 | } else if(s == ctk_signal_screensaver_stop) { |
| 126 | save = 0; |
| 127 | ctk_draw_init(); |
| 128 | ctk_redraw(); |
| 129 | } else if(s == ctk_signal_screensaver_uninstall) { |
| 130 | fire_quit(); |
| 131 | ctk_draw_init(); |
| 132 | ctk_redraw(); |
| 133 | } |
| 134 | } |
| 135 | /*-----------------------------------------------------------------------------------*/ |
| 136 | void |
| 137 | ssfire_idle(void) |
| 138 | { |
| 139 | if(save) { |
| 140 | |
| 141 | /* Calculate new flames. */ |
| 142 | asm("ldx #0"); |
| 143 | asm("loop:"); |
| 144 | asm("lda _flames+7,x"); |
| 145 | asm("clc"); |
| 146 | asm("adc _flames+8,x"); |
| 147 | asm("adc _flames+9,x"); |
| 148 | asm("adc _flames+16,x"); |
| 149 | asm("lsr"); |
| 150 | asm("lsr"); |
| 151 | asm("sta _flames,x"); |
| 152 | asm("inx"); |
| 153 | asm("cpx #(8*15)"); |
| 154 | asm("bne loop"); |
| 155 | |
| 156 | /* Fill last line with pseudo-random data from noise generator on |
| 157 | voice 3. */ |
| 158 | asm("ldx #$05"); |
| 159 | asm("loop2:"); |
| 160 | asm("ldy #$20"); |
| 161 | asm("delay:"); |
| 162 | asm("dey"); |
| 163 | asm("bne delay"); |
| 164 | asm("lda $d41b"); |
| 165 | asm("and #$0f"); |
| 166 | asm("sta _flames+8*15+1,x"); |
| 167 | asm("dex"); |
| 168 | asm("bpl loop2"); |
| 169 | |
| 170 | /* Display flames on screen. */ |
| 171 | flameptr = flames; |
| 172 | colorptr1 = COLOR_RAM + 40*10; |
| 173 | colorptr2 = colorptr1 + 0x20; |
| 174 | for(y = 0; y < 15; ++y) { |
| 175 | for(x = 0; x < 8; ++x) { |
| 176 | *colorptr1 = *colorptr2 = flamecolors[*flameptr++]; |
| 177 | ++colorptr1; |
| 178 | ++colorptr2; |
| 179 | } |
| 180 | colorptr1 += 0x20; |
| 181 | colorptr2 += 0x20; |
| 182 | } |
| 183 | |
| 184 | } |
| 185 | } |
| 186 | /*-----------------------------------------------------------------------------------*/ |
| 187 | |
| 188 | /*-----------------------------------------------------------------------------------*/ |