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