Advertisement
Guest User

CM-5

a guest
Jul 28th, 2020
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.52 KB | None | 0 0
  1. /* written by iskunk (Daniel Richard G.)
  2.    http://www.housedillon.com/?p=1272 (check the Wayback Machine)
  3.    emulates CM-5's 'random and pleasing' LED panel mode (rev. 3)
  4.    FILE IS IN THE PUBLIC DOMAIN */
  5.  
  6. #include <stdio.h>
  7. #include <string.h> /* defines memset */
  8. #include <stdint.h> /* defines fixed-size 2-byte integer uint16_t */
  9. #include <unistd.h> /* defines usleep function */
  10.  
  11. #define SLEEP 222222 /* was 250000 */
  12. #define NUM_ROWS 64  /* unique rows - was 32 */
  13. #define NUM_ROWS_DISPLAYED 64 /* displayed rows - was 106 on the CM-5 */
  14. #define CHAR '▒' /* ■ ▀ ▄ █ ▓ ▒ ░ - they all print out the same char */
  15.  
  16. int main(void)
  17. {
  18.    /* uint16_t is used as integer sizes can vary between machines */
  19.    uint16_t gen, i, gen_bit, row_bit, rows[NUM_ROWS], m, pos;
  20.    char output[17];
  21.    memset(rows, 0, sizeof(rows)); /* zeroes out the rows array */
  22.    fputs("\033[0;31m", stdout);   /* set forground color to red */
  23.    gen = 1; /* non-random seed for LFSR - other numbers also work */
  24.    for (;;) /* main loop - each iteration fills out the entire grid */
  25.    {
  26.       fputs("\033[2J\033[1;1H", stdout); /* ANSI to clear terminal */
  27.       for (i = 0; i < NUM_ROWS; i++) /* run for num unique rows */
  28.       {
  29.          /* see: wikipedia.org/wiki/Linear_feedback_shift_register
  30.             a way to generate pseudo-random nums from a non-random seed
  31.             operation is deterministic, has a finite number of states,
  32.             will eventually repeat itself, can have a long cycle
  33.             primitive polynomial: x^16 + x^12 + x^3 + x^1 + 1 */
  34.          /* C bitwise operators used - all have L to R associativity:
  35.              & = AND     | = OR     ^ = XOR
  36.             >> = right-shift     << = left-shift     */
  37.          gen_bit = ((gen >> 0)^(gen >> 4)^(gen >> 13)^(gen >> 15)) & 1;
  38.          row_bit = ((gen >> 0) & (gen >> 1)) & 1;
  39.          gen = (gen_bit << 15) | (gen >> 1);
  40.          /* the 4 defines the row block height */
  41.          if (i & 4)
  42.             rows[i] = (row_bit << 15) | (rows[i] >> 1);
  43.          else
  44.             rows[i] = (rows[i] << 1) | row_bit;
  45.       }
  46.       /* run 64 times - number of displayed rows */
  47.       for (i = 0; i < NUM_ROWS_DISPLAYED; i++)
  48.       {
  49.          /* why is m being setup this way? it's equal to 32768 */
  50.          for (m = 1 << 15, pos = 0; m != 0; m >>= 1, pos++)
  51.             output[pos] = ((rows[i & 31]) & m) ? CHAR : ' ';
  52.          output[16] = '\0';
  53.          puts(output);
  54.       }
  55.       usleep(SLEEP); /* pause between each loop iteration */
  56.    }
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement