cmatrix.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #define word char
  2. #include "lib/math.c"
  3. #include "lib/stdlib.c"
  4. #include "lib/sys.c"
  5. #include "lib/gfx.c"
  6. #define SCREEN_WIDTH 40
  7. #define SCREEN_HEIGHT 25
  8. #define MAX_RAIN_LENGTH 15
  9. #define MIN_RAIN_LENGTH 5
  10. #define RAIN_DELAY_MS 32 // ~30fps
  11. #define VRAM_PALETTE_TABLE_ADDR 0xC00400
  12. #define VRAM_WINDOW_TILE_ADDR 0xC01420
  13. #define MEMSTATUS_ADDR 0x440000
  14. // Window tile vram. vidMem[Y][X] (bottom right is [24][39])
  15. char (*vidMem)[SCREEN_WIDTH] = (char (*)[SCREEN_WIDTH]) VRAM_WINDOW_TILE_ADDR;
  16. // Hidden vram layer for pixel status
  17. char (*memStatus)[SCREEN_WIDTH] = (char (*)[SCREEN_WIDTH]) MEMSTATUS_ADDR;
  18. // Chars: 33 to 126
  19. unsigned word rngLfsr = 0xACE1;
  20. word rngBit = 0;
  21. word rngRand()
  22. {
  23. rngBit = ((rngLfsr >> 0) ^ (rngLfsr >> 2) ^ (rngLfsr >> 3) ^ (rngLfsr >> 5) ) & 1;
  24. rngLfsr = (rngLfsr >> 1) | (rngBit << 15);
  25. return rngLfsr;
  26. }
  27. void initGraphics()
  28. {
  29. GFX_clearWindowtileTable();
  30. // set default green palette
  31. word* palette = (word*) VRAM_PALETTE_TABLE_ADDR;
  32. palette[0] = 28; // green -> 0b00011100
  33. word x, y;
  34. for (x = 0; x < SCREEN_WIDTH; x++)
  35. {
  36. for (y = 0; y < SCREEN_HEIGHT; y++)
  37. {
  38. vidMem[y][x] = 0;
  39. memStatus[y][x] = 0;
  40. }
  41. }
  42. }
  43. // Return a random character to print
  44. char getRandomChar()
  45. {
  46. rngRand(); rngRand();
  47. return MATH_modU(rngRand(), 93) + 33;
  48. }
  49. // Update all rain on screen
  50. void updateRain()
  51. {
  52. word x, y;
  53. // do last line
  54. for (x = 0; x < SCREEN_WIDTH; x++)
  55. {
  56. if (memStatus[SCREEN_HEIGHT-1][x] > 0)
  57. {
  58. memStatus[SCREEN_HEIGHT-1][x]--;
  59. if (memStatus[SCREEN_HEIGHT-1][x] == 0)
  60. {
  61. vidMem[SCREEN_HEIGHT-1][x] = 0; // clear the character on screen
  62. }
  63. }
  64. }
  65. // skip last line
  66. for (y = SCREEN_HEIGHT-2; y >= 0; y--)
  67. {
  68. for (x = 0; x < SCREEN_WIDTH; x++)
  69. {
  70. if (memStatus[y][x] > 0)
  71. {
  72. // create new char if at bottom of rain
  73. if (memStatus[y+1][x] == 0)
  74. {
  75. vidMem[y+1][x] = getRandomChar();
  76. }
  77. memStatus[y+1][x] = memStatus[y][x]; // propagate downwards
  78. memStatus[y][x]--; // decrease so it eventually ends
  79. if (memStatus[y][x] == 0)
  80. {
  81. vidMem[y][x] = 0; // clear the character on screen
  82. }
  83. }
  84. }
  85. }
  86. }
  87. // Generate a falling line at position x
  88. void genRainLine(word x)
  89. {
  90. // return if x is not free
  91. if (memStatus[0][x] != 0 || memStatus[1][x] != 0)
  92. {
  93. return;
  94. }
  95. word rainLen = MATH_modU(rngRand(), (MAX_RAIN_LENGTH + 1) - MIN_RAIN_LENGTH) + MIN_RAIN_LENGTH;
  96. memStatus[0][x] = rainLen;
  97. vidMem[0][x] = getRandomChar();
  98. }
  99. int main()
  100. {
  101. initGraphics();
  102. while (1)
  103. {
  104. if (hid_checkfifo())
  105. {
  106. word c = hid_fiforead();
  107. if (c == 27) // escape
  108. {
  109. GFX_clearWindowtileTable();
  110. return 'q';
  111. }
  112. }
  113. updateRain();
  114. rngRand(); rngRand(); rngRand();
  115. genRainLine(MATH_modU(rngRand(), SCREEN_WIDTH));
  116. rngRand(); rngRand(); rngRand();
  117. genRainLine(MATH_modU(rngRand(), SCREEN_WIDTH));
  118. delay(RAIN_DELAY_MS);
  119. }
  120. return 'q';
  121. }
  122. void interrupt()
  123. {
  124. // Handle all interrupts
  125. word i = get_int_id();
  126. switch(i)
  127. {
  128. case INTID_TIMER1:
  129. timer1Value = 1; // Notify ending of timer1
  130. break;
  131. }
  132. }