]> git.sur5r.net Git - cc65/blob - samples/plasma.c
Fixed an error
[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 void doplasma (register unsigned char* scrn)
83 {
84     unsigned char xbuf[40];
85     unsigned char ybuf[25];
86     unsigned char c1a,c1b;
87     unsigned char c2a,c2b;
88     unsigned char c1A,c1B;
89     unsigned char c2A,c2B;
90     register unsigned char i, ii;
91
92     c1a = c1A;
93     c1b = c1B;
94     for (ii = 0; ii < 25; ++ii) {
95         ybuf[ii] = (sinustable[c1a] + sinustable[c1b]);
96         c1a += 4;
97         c1b += 9;
98     }
99     c1A += 3;
100     c1B -= 5;
101     c2a = c2A;
102     c2b = c2B;
103     for (i = 0; i < 40; ++i) {
104         xbuf[i] = (sinustable[c2a] + sinustable[c2b]);
105         c2a += 3;
106         c2b += 7;
107     }
108     c2A += 2;
109     c2B -= 3;
110     for (ii = 0; ii < 25; ++ii) {
111         /* Unrolling the following loop will give a speed increase of
112          * nearly 100% (~24fps), but it will also increase the code
113          * size a lot.
114          */
115         for (i = 0; i < 40; ++i, ++scrn) {
116             *scrn = (xbuf[i] + ybuf[ii]);
117         }
118     }
119 }
120
121
122
123 static void makechar (void)
124 {
125     static const unsigned char bittab[8] = {
126         0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80
127     };
128     unsigned char i, ii, b, s;
129     unsigned c;
130
131     gotoxy (0, 1);
132     for (c = 0; c < 0x100; ++c) {
133         s = sinustable[c];
134         for (i = 0; i < 8; ++i){
135             b = 0;
136             for (ii = 0; ii < 8; ++ii) {
137                 if ((rand() & 0xFF) > s) {
138                     b |= bittab[ii];
139                 }
140             }
141             ((unsigned char*)CHARSET) [(c*8) + i] = b;
142         }
143         if ((c & 0x07) == 0) {
144             cputc ('.');
145         }
146     }
147 }
148
149
150
151 int main (void)
152 {
153     unsigned char border;
154     unsigned char background;
155     unsigned char text;
156     unsigned char v;
157     clock_t       t;
158     unsigned long f = 0;
159     unsigned long sec;
160     unsigned      sec10;
161     unsigned long fps;
162     unsigned      fps10;
163
164
165 #if defined(__C64__)
166     unsigned char block;
167 #endif
168 #if defined(__C128__)
169     unsigned char block;
170     unsigned char initflag;
171     unsigned char graphflag;
172 #endif
173
174     clrscr ();
175     cprintf ("Making charset, mompls");
176     makechar();
177
178     /* Set the border and background colors */
179     border     = bordercolor (COLOR_BLUE);
180     background = bgcolor (COLOR_BLUE);
181     text       = textcolor (COLOR_BLACK);
182     clrscr ();
183
184 #if defined(__C64__) || defined(__C128__)
185     /* Move the VIC 16K block */
186     block = inb (&CIA2.pra);
187     outb (&CIA2.pra, (block & 0xFC) | ((SCREEN1 >> 14) ^ 0x03));
188 #endif
189 #if defined(__C128__)
190     /* Save and change some flags, so that kernal/basic interupt handler will
191      * not interfere with our routine.
192      */
193     initflag = *(unsigned char*) 0xA04;
194     *(unsigned char*) 0xA04 &= 0xFE;
195     graphflag = *(unsigned char*) 0xD8;
196     *(unsigned char*) 0xD8 = 0xFF;
197 #endif
198
199     /* Remember the VIC address register */
200     v = inb (&VIC.addr);
201
202     /* Run the demo until a key was hit */
203     t = clock ();
204     while (!kbhit()) {
205         /* Build page 1, then make it visible */
206         doplasma ((unsigned char*)SCREEN1);
207         outb (&VIC.addr, PAGE1);
208
209         /* Build page 2, then make it visible */
210         doplasma ((unsigned char*)SCREEN2);
211         outb (&VIC.addr, PAGE2);
212
213         /* Count frames */
214         f += 2;
215     }
216     t = clock() - t;
217
218     /* Switch back the VIC screen */
219     outb (&VIC.addr, v);
220
221 #if defined(__C64__) || defined(__C128__)
222     /* Move back the VIC 16K block */
223     outb (&CIA2.pra, block);
224 #endif
225 #if defined(__C128__)
226     /* Restore the flags */
227     *(unsigned char*) 0xA04 = initflag;
228     *(unsigned char*) 0xD8  = graphflag;
229 #endif
230
231     /* Fetch the character from the keyboard buffer and discard it */
232     (void) cgetc();
233
234     /* Reset screen colors */
235     bordercolor (border);
236     bgcolor (background);
237     textcolor (text);
238     clrscr ();
239
240     /* Calculate stats */
241     sec   = (t * 10) / CLK_TCK;
242     sec10 = sec % 10;
243     sec  /= 10;
244     fps   = (f * (CLK_TCK * 10)) / t;
245     fps10 = fps % 10;
246     fps  /= 10;
247
248     /* Output stats */
249     gotoxy (0, 0); cprintf ("time  : %lu.%us", sec, sec10);
250     gotoxy (0, 1); cprintf ("frames: %lu", f);
251     gotoxy (0, 2); cprintf ("fps   : %lu.%u", fps, fps10);
252
253     /* Wait for a key, then end */
254     cputsxy (0, 4, "Press any key when done...");
255     (void) cgetc ();
256
257     /* Done */
258     return EXIT_SUCCESS;
259 }
260
261