|
| 1 | +// Copyright 2023 LLVM-MOS Project |
| 2 | +// Licensed under the Apache License, Version 2.0 with LLVM Exceptions. |
| 3 | +// See https://github.com/llvm-mos/llvm-mos-sdk/blob/main/LICENSE for license |
| 4 | +// information. |
| 5 | + |
| 6 | +/* |
| 7 | + * Simplistic character-mode plasma effect |
| 8 | + * |
| 9 | + * Inspired by cc65/samples/cbm |
| 10 | + * - 2001 by groepaz |
| 11 | + * - Cleanup and porting by Ullrich von Bassewitz |
| 12 | + * - 2023 Mega65/LLVM-MOS C++ adaptation by Mikael Lund aka Wombat |
| 13 | + */ |
| 14 | + |
| 15 | +#include <array> |
| 16 | +#include <cstdint> |
| 17 | +#include <mega65.h> |
| 18 | + |
| 19 | +/* |
| 20 | + * Simple pseudo-random number generator |
| 21 | + * https://en.wikipedia.org/wiki/Xorshift |
| 22 | + */ |
| 23 | +class RandomXORS { |
| 24 | +private: |
| 25 | + uint32_t state = 7; |
| 26 | + |
| 27 | +public: |
| 28 | + inline uint8_t rand8() { return static_cast<uint8_t>(rand32() & 0xff); } |
| 29 | + inline uint32_t rand32() { |
| 30 | + state ^= state << 13; |
| 31 | + state ^= state >> 17; |
| 32 | + state ^= state << 5; |
| 33 | + return state; |
| 34 | + } |
| 35 | +}; |
| 36 | + |
| 37 | +/* |
| 38 | + * Sets MEGA65 speed to 3.5 Mhz |
| 39 | + */ |
| 40 | +void speed_mode3() { |
| 41 | + VICIV.ctrlb |= VIC3_FAST_MASK; |
| 42 | + VICIV.ctrlc &= ~VIC4_VFAST_MASK; |
| 43 | +} |
| 44 | + |
| 45 | +/* |
| 46 | + * Cyclic sine lookup table |
| 47 | + */ |
| 48 | +constexpr uint8_t sine_table[UINT8_MAX + 1] = { |
| 49 | + 0x80, 0x7d, 0x7a, 0x77, 0x74, 0x70, 0x6d, 0x6a, 0x67, 0x64, 0x61, 0x5e, |
| 50 | + 0x5b, 0x58, 0x55, 0x52, 0x4f, 0x4d, 0x4a, 0x47, 0x44, 0x41, 0x3f, 0x3c, |
| 51 | + 0x39, 0x37, 0x34, 0x32, 0x2f, 0x2d, 0x2b, 0x28, 0x26, 0x24, 0x22, 0x20, |
| 52 | + 0x1e, 0x1c, 0x1a, 0x18, 0x16, 0x15, 0x13, 0x11, 0x10, 0x0f, 0x0d, 0x0c, |
| 53 | + 0x0b, 0x0a, 0x08, 0x07, 0x06, 0x06, 0x05, 0x04, 0x03, 0x03, 0x02, 0x02, |
| 54 | + 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x03, |
| 55 | + 0x03, 0x04, 0x05, 0x06, 0x06, 0x07, 0x08, 0x0a, 0x0b, 0x0c, 0x0d, 0x0f, |
| 56 | + 0x10, 0x11, 0x13, 0x15, 0x16, 0x18, 0x1a, 0x1c, 0x1e, 0x20, 0x22, 0x24, |
| 57 | + 0x26, 0x28, 0x2b, 0x2d, 0x2f, 0x32, 0x34, 0x37, 0x39, 0x3c, 0x3f, 0x41, |
| 58 | + 0x44, 0x47, 0x4a, 0x4d, 0x4f, 0x52, 0x55, 0x58, 0x5b, 0x5e, 0x61, 0x64, |
| 59 | + 0x67, 0x6a, 0x6d, 0x70, 0x74, 0x77, 0x7a, 0x7d, 0x80, 0x83, 0x86, 0x89, |
| 60 | + 0x8c, 0x90, 0x93, 0x96, 0x99, 0x9c, 0x9f, 0xa2, 0xa5, 0xa8, 0xab, 0xae, |
| 61 | + 0xb1, 0xb3, 0xb6, 0xb9, 0xbc, 0xbf, 0xc1, 0xc4, 0xc7, 0xc9, 0xcc, 0xce, |
| 62 | + 0xd1, 0xd3, 0xd5, 0xd8, 0xda, 0xdc, 0xde, 0xe0, 0xe2, 0xe4, 0xe6, 0xe8, |
| 63 | + 0xea, 0xeb, 0xed, 0xef, 0xf0, 0xf1, 0xf3, 0xf4, 0xf5, 0xf6, 0xf8, 0xf9, |
| 64 | + 0xfa, 0xfa, 0xfb, 0xfc, 0xfd, 0xfd, 0xfe, 0xfe, 0xfe, 0xff, 0xff, 0xff, |
| 65 | + 0xff, 0xff, 0xff, 0xff, 0xfe, 0xfe, 0xfe, 0xfd, 0xfd, 0xfc, 0xfb, 0xfa, |
| 66 | + 0xfa, 0xf9, 0xf8, 0xf6, 0xf5, 0xf4, 0xf3, 0xf1, 0xf0, 0xef, 0xed, 0xeb, |
| 67 | + 0xea, 0xe8, 0xe6, 0xe4, 0xe2, 0xe0, 0xde, 0xdc, 0xda, 0xd8, 0xd5, 0xd3, |
| 68 | + 0xd1, 0xce, 0xcc, 0xc9, 0xc7, 0xc4, 0xc1, 0xbf, 0xbc, 0xb9, 0xb6, 0xb3, |
| 69 | + 0xb1, 0xae, 0xab, 0xa8, 0xa5, 0xa2, 0x9f, 0x9c, 0x99, 0x96, 0x93, 0x90, |
| 70 | + 0x8c, 0x89, 0x86, 0x83}; |
| 71 | + |
| 72 | +/* |
| 73 | + * Generate charset with 8 * 256 characters at given address |
| 74 | + */ |
| 75 | +void make_charset(uint16_t charset_address, RandomXORS &rng) { |
| 76 | + /* |
| 77 | + * Lambda function to generate a single 8x8 pixels pattern |
| 78 | + */ |
| 79 | + auto make_char = [&](const uint8_t sine) { |
| 80 | + uint8_t pattern = 0; |
| 81 | + constexpr uint8_t bits[8] = {1, 2, 4, 8, 16, 32, 64, 128}; |
| 82 | + for (const auto bit : bits) { |
| 83 | + if (rng.rand8() > sine) { |
| 84 | + pattern |= bit; |
| 85 | + } |
| 86 | + } |
| 87 | + return pattern; |
| 88 | + }; |
| 89 | + |
| 90 | + auto charset = reinterpret_cast<volatile uint8_t *>(charset_address); |
| 91 | + for (const auto sine : sine_table) { |
| 92 | + for (int _i = 0; _i < 8; ++_i) { |
| 93 | + *(charset++) = make_char(sine); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +/* |
| 99 | + * Plasma class |
| 100 | + */ |
| 101 | +template <size_t COLS, size_t ROWS> class Plasma { |
| 102 | +private: |
| 103 | + std::array<uint8_t, ROWS> ydata; |
| 104 | + std::array<uint8_t, COLS> xdata; |
| 105 | + uint8_t x_cnt1 = 0; |
| 106 | + uint8_t x_cnt2 = 0; |
| 107 | + uint8_t y_cnt1 = 0; |
| 108 | + uint8_t y_cnt2 = 0; |
| 109 | + |
| 110 | +public: |
| 111 | + /* |
| 112 | + * Generate and activate charset at given address |
| 113 | + */ |
| 114 | + Plasma(const uint16_t charset_address, RandomXORS &rng) { |
| 115 | + make_charset(charset_address, rng); |
| 116 | + VICIV.charptr = charset_address; |
| 117 | + } |
| 118 | + |
| 119 | + /* |
| 120 | + * Draw frame |
| 121 | + */ |
| 122 | + inline void update() { |
| 123 | + auto i = y_cnt1; |
| 124 | + auto j = y_cnt2; |
| 125 | + for (auto &y : ydata) { |
| 126 | + y = sine_table[i] + sine_table[j]; |
| 127 | + i += 4; |
| 128 | + j += 9; |
| 129 | + } |
| 130 | + i = x_cnt1; |
| 131 | + j = x_cnt2; |
| 132 | + for (auto &x : xdata) { |
| 133 | + x = sine_table[i] + sine_table[j]; |
| 134 | + i += 3; |
| 135 | + j += 7; |
| 136 | + } |
| 137 | + x_cnt1 += 2; |
| 138 | + x_cnt2 -= 3; |
| 139 | + y_cnt1 += 3; |
| 140 | + y_cnt2 -= 5; |
| 141 | + |
| 142 | + write_to_screen(); |
| 143 | + } |
| 144 | + |
| 145 | + /* |
| 146 | + * Write summed buffers to screen memory |
| 147 | + */ |
| 148 | + inline void write_to_screen() const { |
| 149 | + auto screen_ptr = reinterpret_cast<volatile uint8_t *>(&DEFAULT_SCREEN); |
| 150 | + for (const auto y : ydata) { |
| 151 | +#pragma unroll |
| 152 | + for (const auto x : xdata) { |
| 153 | + *(screen_ptr++) = y + x; |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | +}; |
| 158 | + |
| 159 | +int main() { |
| 160 | + constexpr size_t COLS = 80; |
| 161 | + constexpr size_t ROWS = 25; |
| 162 | + constexpr uint16_t CHARSET_ADDRESS = 0x3000; |
| 163 | + RandomXORS rng; |
| 164 | + Plasma<COLS, ROWS> plasma(CHARSET_ADDRESS, rng); |
| 165 | + speed_mode3(); |
| 166 | + while (true) { |
| 167 | + plasma.update(); |
| 168 | + } |
| 169 | +} |
0 commit comments