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