]> git.sur5r.net Git - cc65/blob - samples/plasma.c
Fixed _textcolor definition.
[cc65] / samples / plasma.c
1 /*****************************************************************************\
2 ** plasma test program for cc65.                                             **
3 **                                                                           **
4 ** (w)2001 by groepaz/hitmen                                                 **
5 **                                                                           **
6 ** Cleanup and porting by Ullrich von Bassewitz.                             **
7 **                                                                           **
8 \*****************************************************************************/
9
10
11
12 #include <stdlib.h>
13 #include <time.h>
14 #include <conio.h>
15 #include <cc65.h>
16
17
18
19 #if defined(__C64__) || defined(__C128__)
20 #  define SCREEN1               0xE000
21 #  define SCREEN2               0xE400
22 #  define CHARSET               0xE800
23 #  define outb(addr,val)        (*(addr)) = (val)
24 #  define inb(addr)             (*(addr))
25 #elif defined(__CBM510__)
26 #  define SCREEN1               0xF000
27 #  define SCREEN2               0xF400
28 #  define CHARSET               0xE000
29 #  define outb(addr,val)        pokebsys ((unsigned)(addr), val)
30 #  define inb(addr)             peekbsys ((unsigned)(addr))
31 #elif defined(__PLUS4__)
32 #  define SCREEN1               0x6400
33 #  define SCREEN2               0x6C00
34 #  define CHARSET               0x7000
35 #  define outb(addr,val)        (*(addr)) = (val)
36 #  define inb(addr)             (*(addr))
37 #endif
38
39
40
41 /* Values for the VIC address register to switch between the two pages */
42 #if defined(__PLUS4__)
43 #define PAGE1                   ((SCREEN1 >> 8) & 0xF8)
44 #define PAGE2                   ((SCREEN2 >> 8) & 0xF8)
45 #define CHARADR                 ((CHARSET >> 8) & 0xFC)
46 #else
47 #define PAGE1                   ((SCREEN1 >> 6) & 0xF0) | ((CHARSET >> 10) & 0x0E)
48 #define PAGE2                   ((SCREEN2 >> 6) & 0xF0) | ((CHARSET >> 10) & 0x0E)
49 #endif
50
51
52
53 /* Use static local variables for speed */
54 #pragma static-locals (1);
55
56
57
58 static const unsigned char sinustable[0x100] = {
59     0x80, 0x7d, 0x7a, 0x77, 0x74, 0x70, 0x6d, 0x6a,
60     0x67, 0x64, 0x61, 0x5e, 0x5b, 0x58, 0x55, 0x52,
61     0x4f, 0x4d, 0x4a, 0x47, 0x44, 0x41, 0x3f, 0x3c,
62     0x39, 0x37, 0x34, 0x32, 0x2f, 0x2d, 0x2b, 0x28,
63     0x26, 0x24, 0x22, 0x20, 0x1e, 0x1c, 0x1a, 0x18,
64     0x16, 0x15, 0x13, 0x11, 0x10, 0x0f, 0x0d, 0x0c,
65     0x0b, 0x0a, 0x08, 0x07, 0x06, 0x06, 0x05, 0x04,
66     0x03, 0x03, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01,
67     0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x03,
68     0x03, 0x04, 0x05, 0x06, 0x06, 0x07, 0x08, 0x0a,
69     0x0b, 0x0c, 0x0d, 0x0f, 0x10, 0x11, 0x13, 0x15,
70     0x16, 0x18, 0x1a, 0x1c, 0x1e, 0x20, 0x22, 0x24,
71     0x26, 0x28, 0x2b, 0x2d, 0x2f, 0x32, 0x34, 0x37,
72     0x39, 0x3c, 0x3f, 0x41, 0x44, 0x47, 0x4a, 0x4d,
73     0x4f, 0x52, 0x55, 0x58, 0x5b, 0x5e, 0x61, 0x64,
74     0x67, 0x6a, 0x6d, 0x70, 0x74, 0x77, 0x7a, 0x7d,
75     0x80, 0x83, 0x86, 0x89, 0x8c, 0x90, 0x93, 0x96,
76     0x99, 0x9c, 0x9f, 0xa2, 0xa5, 0xa8, 0xab, 0xae,
77     0xb1, 0xb3, 0xb6, 0xb9, 0xbc, 0xbf, 0xc1, 0xc4,
78     0xc7, 0xc9, 0xcc, 0xce, 0xd1, 0xd3, 0xd5, 0xd8,
79     0xda, 0xdc, 0xde, 0xe0, 0xe2, 0xe4, 0xe6, 0xe8,
80     0xea, 0xeb, 0xed, 0xef, 0xf0, 0xf1, 0xf3, 0xf4,
81     0xf5, 0xf6, 0xf8, 0xf9, 0xfa, 0xfa, 0xfb, 0xfc,
82     0xfd, 0xfd, 0xfe, 0xfe, 0xfe, 0xff, 0xff, 0xff,
83     0xff, 0xff, 0xff, 0xff, 0xfe, 0xfe, 0xfe, 0xfd,
84     0xfd, 0xfc, 0xfb, 0xfa, 0xfa, 0xf9, 0xf8, 0xf6,
85     0xf5, 0xf4, 0xf3, 0xf1, 0xf0, 0xef, 0xed, 0xeb,
86     0xea, 0xe8, 0xe6, 0xe4, 0xe2, 0xe0, 0xde, 0xdc,
87     0xda, 0xd8, 0xd5, 0xd3, 0xd1, 0xce, 0xcc, 0xc9,
88     0xc7, 0xc4, 0xc1, 0xbf, 0xbc, 0xb9, 0xb6, 0xb3,
89     0xb1, 0xae, 0xab, 0xa8, 0xa5, 0xa2, 0x9f, 0x9c,
90     0x99, 0x96, 0x93, 0x90, 0x8c, 0x89, 0x86, 0x83
91 };
92
93
94
95 static void doplasma (register unsigned char* scrn)
96 {
97     unsigned char xbuf[40];
98     unsigned char ybuf[25];
99     unsigned char c1a,c1b;
100     unsigned char c2a,c2b;
101     unsigned char c1A,c1B;
102     unsigned char c2A,c2B;
103     register unsigned char i, ii;
104
105     c1a = c1A;
106     c1b = c1B;
107     for (ii = 0; ii < 25; ++ii) {
108         ybuf[ii] = (sinustable[c1a] + sinustable[c1b]);
109         c1a += 4;
110         c1b += 9;
111     }
112     c1A += 3;
113     c1B -= 5;
114     c2a = c2A;
115     c2b = c2B;
116     for (i = 0; i < 40; ++i) {
117         xbuf[i] = (sinustable[c2a] + sinustable[c2b]);
118         c2a += 3;
119         c2b += 7;
120     }
121     c2A += 2;
122     c2B -= 3;
123     for (ii = 0; ii < 25; ++ii) {
124         /* Unrolling the following loop will give a speed increase of
125         ** nearly 100% (~24fps), but it will also increase the code
126         ** size a lot.
127         */
128         for (i = 0; i < 40; ++i, ++scrn) {
129             *scrn = (xbuf[i] + ybuf[ii]);
130         }
131     }
132 }
133
134
135
136 static void makechar (void)
137 {
138     static const unsigned char bittab[8] = {
139         0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80
140     };
141     unsigned char i, ii, b, s;
142     unsigned c;
143
144     gotoxy (0, 1);
145     for (c = 0; c < 0x100; ++c) {
146         s = sinustable[c];
147         for (i = 0; i < 8; ++i){
148             b = 0;
149             for (ii = 0; ii < 8; ++ii) {
150                 if ((rand() & 0xFF) > s) {
151                     b |= bittab[ii];
152                 }
153             }
154             ((unsigned char*)CHARSET) [(c*8) + i] = b;
155         }
156         if ((c & 0x07) == 0) {
157             cputc ('.');
158         }
159     }
160 }
161
162
163
164 int main (void)
165 {
166     unsigned char border;
167     unsigned char background;
168     unsigned char text;
169     unsigned char v;
170     clock_t       t;
171     unsigned long f = 0;
172     unsigned long sec;
173     unsigned      sec10;
174     unsigned long fps;
175     unsigned      fps10;
176
177
178 #if defined(__C64__)
179     unsigned char block;
180 #endif
181 #if defined(__C128__)
182     unsigned char block;
183     unsigned char initflag;
184     unsigned char graphflag;
185 #endif
186 #if defined(__PLUS4__)
187     unsigned int i;
188     unsigned char v2;
189 #endif
190
191     clrscr ();
192     cprintf ("Making charset, mompls");
193     makechar();
194
195     /* Set the border and background colors */
196     border     = bordercolor (COLOR_BLUE);
197     background = bgcolor (COLOR_BLUE);
198     text       = textcolor (COLOR_BLACK);
199     clrscr ();
200
201 #if defined(__C64__) || defined(__C128__)
202     /* Move the VIC 16K block */
203     block = inb (&CIA2.pra);
204     outb (&CIA2.pra, (block & 0xFC) | ((SCREEN1 >> 14) ^ 0x03));
205 #endif
206 #if defined(__C128__)
207     /* Save and change some flags, so that kernal/basic interrupt handler will
208     ** not interfere with our routine.
209     */
210     initflag = *(unsigned char*) 0xA04;
211     *(unsigned char*) 0xA04 &= 0xFE;
212     graphflag = *(unsigned char*) 0xD8;
213     *(unsigned char*) 0xD8 = 0xFF;
214 #endif
215
216     /* Remember the VIC address register */
217 #if defined(__PLUS4__)
218     v = inb (&TED.char_addr);
219     v2 = inb (&TED.video_addr);
220 #else
221     v = inb (&VIC.addr);
222 #endif
223
224 #if defined(__PLUS4__)
225     for (i=0;i<1000;i++) {
226         ((unsigned char *) (SCREEN1-0x0400))[i] = 0;
227         ((unsigned char *) (SCREEN2-0x0400))[i] = 0;
228     }
229     outb (&TED.char_addr, CHARADR);
230 #endif
231
232     /* Run the demo until a key was hit */
233     t = clock ();
234     while (!kbhit()) {
235         /* Build page 1, then make it visible */
236         doplasma ((unsigned char*)SCREEN1);
237 #if defined(__PLUS4__)
238         outb (&TED.video_addr, PAGE1);
239 #else
240         outb (&VIC.addr, PAGE1);
241 #endif
242
243         /* Build page 2, then make it visible */
244         doplasma ((unsigned char*)SCREEN2);
245 #if defined(__PLUS4__)
246         outb (&TED.video_addr, PAGE2);
247 #else
248         outb (&VIC.addr, PAGE2);
249 #endif
250
251         /* Count frames */
252         f += 2;
253     }
254     t = clock() - t;
255
256     /* Switch back the VIC screen */
257 #if defined(__PLUS4__)
258     outb (&TED.video_addr, v2);
259     outb (&TED.char_addr, v);
260 #else
261     outb (&VIC.addr, v);
262 #endif
263
264 #if defined(__C64__) || defined(__C128__)
265     /* Move back the VIC 16K block */
266     outb (&CIA2.pra, block);
267 #endif
268 #if defined(__C128__)
269     /* Restore the flags */
270     *(unsigned char*) 0xA04 = initflag;
271     *(unsigned char*) 0xD8  = graphflag;
272 #endif
273
274     /* Fetch the character from the keyboard buffer and discard it */
275     (void) cgetc();
276
277     /* Reset screen colors */
278     bordercolor (border);
279     bgcolor (background);
280     textcolor (text);
281     clrscr ();
282
283     /* Calculate stats */
284     sec   = (t * 10) / CLK_TCK;
285     sec10 = sec % 10;
286     sec  /= 10;
287     fps   = (f * (CLK_TCK * 10)) / t;
288     fps10 = fps % 10;
289     fps  /= 10;
290
291     /* Output stats */
292     gotoxy (0, 0); cprintf ("time  : %lu.%us", sec, sec10);
293     gotoxy (0, 1); cprintf ("frames: %lu", f);
294     gotoxy (0, 2); cprintf ("fps   : %lu.%u", fps, fps10);
295
296     if (doesclrscrafterexit ()) {
297         cputsxy (0, 4, "Press any key when done...");
298         (void) cgetc ();
299     }
300
301     /* Done */
302     return EXIT_SUCCESS;
303 }