Advertisement
Guest User

CM-5 with better symbol

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