]> git.sur5r.net Git - cc65/blobdiff - samples/plasma.c
Replace hard returns with an "else", add an error for non-IDENT tokens, and test...
[cc65] / samples / plasma.c
index 0267b8efbd55477df9072b86659341dc448d5b22..ac17265f3791d01c858e9538ed7c5ac177fda3a7 100644 (file)
@@ -1,44 +1,57 @@
-/*****************************************************************************
- * plasma test program for cc65.                                             *
- *                                                                           *
- * (w)2001 by groepaz/hitmen                                                 *
- *                                                                           *
- * Cleanup and porting by Ullrich von Bassewitz.                            *
- *                                                                           *
- *****************************************************************************/
+/*****************************************************************************\
+** plasma test program for cc65.                                             **
+**                                                                           **
+** (w)2001 by groepaz/hitmen                                                 **
+**                                                                           **
+** Cleanup and porting by Ullrich von Bassewitz.                             **
+**                                                                           **
+\*****************************************************************************/
 
 
 
 #include <stdlib.h>
 #include <time.h>
 #include <conio.h>
+#include <cc65.h>
 
 
 
-#if defined(__C64__)
+#if defined(__C64__) || defined(__C128__)
 #  define SCREEN1               0xE000
 #  define SCREEN2               0xE400
 #  define CHARSET               0xE800
-#  define outb(addr,val)               (*(addr)) = (val)
+#  define outb(addr,val)        (*(addr)) = (val)
 #  define inb(addr)             (*(addr))
 #elif defined(__CBM510__)
 #  define SCREEN1               0xF000
 #  define SCREEN2               0xF400
 #  define CHARSET               0xE000
-#  define outb(addr,val)       pokebsys ((unsigned)(addr), val)
+#  define outb(addr,val)        pokebsys ((unsigned)(addr), val)
 #  define inb(addr)             peekbsys ((unsigned)(addr))
+#elif defined(__PLUS4__)
+#  define SCREEN1               0x6400
+#  define SCREEN2               0x6C00
+#  define CHARSET               0x7000
+#  define outb(addr,val)        (*(addr)) = (val)
+#  define inb(addr)             (*(addr))
 #endif
 
 
 
 /* Values for the VIC address register to switch between the two pages */
+#if defined(__PLUS4__)
+#define PAGE1                   ((SCREEN1 >> 8) & 0xF8)
+#define PAGE2                   ((SCREEN2 >> 8) & 0xF8)
+#define CHARADR                 ((CHARSET >> 8) & 0xFC)
+#else
 #define PAGE1                   ((SCREEN1 >> 6) & 0xF0) | ((CHARSET >> 10) & 0x0E)
 #define PAGE2                   ((SCREEN2 >> 6) & 0xF0) | ((CHARSET >> 10) & 0x0E)
+#endif
 
 
 
 /* Use static local variables for speed */
-#pragma staticlocals (1);
+#pragma static-locals (1);
 
 
 
@@ -79,26 +92,22 @@ static const unsigned char sinustable[0x100] = {
 
 
 
-static unsigned char *scrn;
-
-
-
-static void doplasma (void)
+static void doplasma (register unsigned char* scrn)
 {
     unsigned char xbuf[40];
     unsigned char ybuf[25];
-    unsigned char i,ii;
     unsigned char c1a,c1b;
     unsigned char c2a,c2b;
     unsigned char c1A,c1B;
     unsigned char c2A,c2B;
+    register unsigned char i, ii;
 
     c1a = c1A;
     c1b = c1B;
     for (ii = 0; ii < 25; ++ii) {
         ybuf[ii] = (sinustable[c1a] + sinustable[c1b]);
         c1a += 4;
-       c1b += 9;
+        c1b += 9;
     }
     c1A += 3;
     c1B -= 5;
@@ -107,15 +116,15 @@ static void doplasma (void)
     for (i = 0; i < 40; ++i) {
         xbuf[i] = (sinustable[c2a] + sinustable[c2b]);
         c2a += 3;
-       c2b += 7;
+        c2b += 7;
     }
     c2A += 2;
     c2B -= 3;
     for (ii = 0; ii < 25; ++ii) {
-       /* Unrolling the following loop will give a speed increase of
-        * nearly 100% (~24fps), but it will also increase the code 
-        * size a lot.
-        */
+        /* Unrolling the following loop will give a speed increase of
+        ** nearly 100% (~24fps), but it will also increase the code
+        ** size a lot.
+        */
         for (i = 0; i < 40; ++i, ++scrn) {
             *scrn = (xbuf[i] + ybuf[ii]);
         }
@@ -127,26 +136,26 @@ static void doplasma (void)
 static void makechar (void)
 {
     static const unsigned char bittab[8] = {
-       0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80
+        0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80
     };
     unsigned char i, ii, b, s;
     unsigned c;
 
     gotoxy (0, 1);
     for (c = 0; c < 0x100; ++c) {
-       s = sinustable[c];
+        s = sinustable[c];
         for (i = 0; i < 8; ++i){
             b = 0;
             for (ii = 0; ii < 8; ++ii) {
                 if ((rand() & 0xFF) > s) {
-                   b |= bittab[ii];
-               }
+                    b |= bittab[ii];
+                }
             }
             ((unsigned char*)CHARSET) [(c*8) + i] = b;
         }
-       if ((c & 0x07) == 0) {
-           cputc ('.');
-       }
+        if ((c & 0x07) == 0) {
+            cputc ('.');
+        }
     }
 }
 
@@ -166,6 +175,19 @@ int main (void)
     unsigned      fps10;
 
 
+#if defined(__C64__)
+    unsigned char block;
+#endif
+#if defined(__C128__)
+    unsigned char block;
+    unsigned char initflag;
+    unsigned char graphflag;
+#endif
+#if defined(__PLUS4__)
+    unsigned int i;
+    unsigned char v2;
+#endif
+
     clrscr ();
     cprintf ("Making charset, mompls");
     makechar();
@@ -176,38 +198,77 @@ int main (void)
     text       = textcolor (COLOR_BLACK);
     clrscr ();
 
-#if defined(__C64__)
+#if defined(__C64__) || defined(__C128__)
     /* Move the VIC 16K block */
-    outb (&CIA2.pra, 0x00);
+    block = inb (&CIA2.pra);
+    outb (&CIA2.pra, (block & 0xFC) | ((SCREEN1 >> 14) ^ 0x03));
+#endif
+#if defined(__C128__)
+    /* Save and change some flags, so that kernal/basic interrupt handler will
+    ** not interfere with our routine.
+    */
+    initflag = *(unsigned char*) 0xA04;
+    *(unsigned char*) 0xA04 &= 0xFE;
+    graphflag = *(unsigned char*) 0xD8;
+    *(unsigned char*) 0xD8 = 0xFF;
 #endif
 
     /* Remember the VIC address register */
+#if defined(__PLUS4__)
+    v = inb (&TED.char_addr);
+    v2 = inb (&TED.video_addr);
+#else
     v = inb (&VIC.addr);
+#endif
+
+#if defined(__PLUS4__)
+    for (i=0;i<1000;i++) {
+        ((unsigned char *) (SCREEN1-0x0400))[i] = 0;
+        ((unsigned char *) (SCREEN2-0x0400))[i] = 0;
+    }
+    outb (&TED.char_addr, CHARADR);
+#endif
 
     /* Run the demo until a key was hit */
     t = clock ();
     while (!kbhit()) {
-       /* Build page 1, then make it visible */
-        scrn = (unsigned char*)SCREEN1;
-        doplasma();
-       outb (&VIC.addr, PAGE1);
+        /* Build page 1, then make it visible */
+        doplasma ((unsigned char*)SCREEN1);
+#if defined(__PLUS4__)
+        outb (&TED.video_addr, PAGE1);
+#else
+        outb (&VIC.addr, PAGE1);
+#endif
 
-       /* Build page 2, then make it visible */
-        scrn = (unsigned char*)SCREEN2;
-        doplasma();
-       outb (&VIC.addr, PAGE2);
+        /* Build page 2, then make it visible */
+        doplasma ((unsigned char*)SCREEN2);
+#if defined(__PLUS4__)
+        outb (&TED.video_addr, PAGE2);
+#else
+        outb (&VIC.addr, PAGE2);
+#endif
 
-       /* Count frames */
+        /* Count frames */
         f += 2;
-    };
+    }
     t = clock() - t;
 
     /* Switch back the VIC screen */
+#if defined(__PLUS4__)
+    outb (&TED.video_addr, v2);
+    outb (&TED.char_addr, v);
+#else
     outb (&VIC.addr, v);
+#endif
 
-#if defined(__C64__)
+#if defined(__C64__) || defined(__C128__)
     /* Move back the VIC 16K block */
-    outb (&CIA2.pra, 0x03);
+    outb (&CIA2.pra, block);
+#endif
+#if defined(__C128__)
+    /* Restore the flags */
+    *(unsigned char*) 0xA04 = initflag;
+    *(unsigned char*) 0xD8  = graphflag;
 #endif
 
     /* Fetch the character from the keyboard buffer and discard it */
@@ -217,7 +278,7 @@ int main (void)
     bordercolor (border);
     bgcolor (background);
     textcolor (text);
-    clrscr();
+    clrscr ();
 
     /* Calculate stats */
     sec   = (t * 10) / CLK_TCK;
@@ -232,12 +293,11 @@ int main (void)
     gotoxy (0, 1); cprintf ("frames: %lu", f);
     gotoxy (0, 2); cprintf ("fps   : %lu.%u", fps, fps10);
 
-    /* Wait for a key, then end */
-    cputsxy (0, 4, "Press any key when done...");
-    (void) cgetc ();
+    if (doesclrscrafterexit ()) {
+        cputsxy (0, 4, "Press any key when done...");
+        (void) cgetc ();
+    }
 
     /* Done */
     return EXIT_SUCCESS;
 }
-
-