Advertisement
iskunk

Attempt at CM-5 "random and pleasing" pattern (rev. 3)

Mar 30th, 2016
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.45 KB | None | 0 0
  1. /* http://www.housedillon.com/?p=1272
  2.  * Written by iskunk (Daniel Richard G.)
  3.  * THIS FILE IS IN THE PUBLIC DOMAIN
  4.  *
  5.  * Program to emulate the CM-5's "random and pleasing" LED panel mode
  6.  * (revision 3)
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <stdint.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13.  
  14. #define NUM_ROWS 32     /* unique rows */
  15. #define NUM_ROWS_DISPLAYED 106  /* rows in front panel display */
  16.  
  17. static uint16_t fn(uint16_t x)
  18. {
  19.     /* https://en.wikipedia.org/wiki/Linear_feedback_shift_register
  20.      * Primitive polynomial: x^16 + x^12 + x^3 + x^1 + 1
  21.      */
  22.     return ((x >> 0) ^ (x >> 4) ^ (x >> 13) ^ (x >> 15)) & 1;
  23. }
  24.  
  25. static void print_row(uint16_t x)
  26. {
  27.     uint16_t m;
  28.     int pos;
  29.     char v[17];
  30.  
  31.     for (m = 1 << 15, pos = 0; m != 0; m >>= 1, pos++)
  32.         v[pos] = (x & m) ? 'O' : '-';
  33.  
  34.     v[16] = '\0';
  35.  
  36.     puts(v);
  37. }
  38.  
  39. int main(void)
  40. {
  41.     uint16_t gen = 1;
  42.     uint16_t rows[NUM_ROWS];
  43.     int i;
  44.  
  45.     memset(rows, 0, sizeof(rows));
  46.  
  47.     for (;;)
  48.     {
  49.         /* ANSI sequence to clear terminal */
  50.         fputs("\033[2J\033[1;1H", stdout);
  51.  
  52.         for (i = 0; i < NUM_ROWS; i++)
  53.         {
  54.             uint16_t gen_bit = fn(gen);
  55.             uint16_t row_bit = ((gen >> 0) & (gen >> 1)) & 1;
  56.  
  57.             gen = (gen_bit << 15) | (gen >> 1);
  58.  
  59.             if (i & 4)
  60.                 rows[i] = (row_bit << 15) | (rows[i] >> 1);
  61.             else
  62.                 rows[i] = (rows[i] << 1) | row_bit;
  63.         }
  64.  
  65.         for (i = 0; i < NUM_ROWS_DISPLAYED; i++)
  66.             print_row(rows[i & 31]);
  67.  
  68.         fflush(stdout);
  69.         usleep(250000);
  70.     }
  71.  
  72.     return 0;
  73. }
  74.  
  75. /* EOF */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement