-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
226 lines (188 loc) · 4.97 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <stdbool.h>
#include <avr/io.h>
#include <stdlib.h>
#include <avr/pgmspace.h>
#include <uzebox.h>
#include <stdio.h>
/* EXAMPLES
* 0: just for miscellaneous debugging code
* 1: show ASCII with 12 different palettes
* 2: show color values
* 3: scramble VRAM
* 4: fake terminal output (TODO, NYI)
*/
#define EXAMPLE 0
ColorCombo m96_palette[16] = {
#if EXAMPLE >= 0 && EXAMPLE < 4
{ 0xc0, 0x15 }, // blue on orange
{ 0x02, 0x10 }, // red on green
{ 0x83, 0x36 }, // violet on yellow
{ 0xa8, 0x02 }, // cyan on red
{ 0xe3, 0x42 },
{ 0x2f, 0x23 },
{ 0x55, 0x49 },
{ 0x61, 0xd3 },
{ 0xc2, 0xd7 },
{ 0xa7, 0xfd },
{ 0x39, 0x05 },
{ 0xff, 0x00 },
#elif EXAMPLE == 4
// an imitation of ANSI color codes on a VGA display
{ 0x00, 0x52 }, // low-intensity
{ 0x05, 0x00 },
{ 0x28, 0x00 },
{ 0x15, 0x00 },
{ 0x80, 0x00 },
{ 0x85, 0x00 },
{ 0xa8, 0x00 },
{ 0xad, 0x00 },
{ 0x52, 0x00 }, // high-intensity
{ 0x57, 0x00 },
{ 0x7a, 0x00 },
{ 0x7f, 0x00 },
{ 0xd2, 0x00 },
{ 0xd7, 0x00 },
{ 0xfa, 0x00 },
{ 0xff, 0x00 },
#endif
};
typedef struct {
u16 prev; // Previous buttons that were held
u16 down; // Buttons that are held right now
u16 prsd; // Buttons that were pressed this frame
u16 rlsd; // Buttons that were released this frame
} ButtonData;
ButtonData _btn = {0, 0, 0, 0};
void button_update() {
_btn.down = ReadJoypad(0);
_btn.prsd = _btn.down & (_btn.down ^ _btn.prev);
_btn.rlsd = _btn.prev & (_btn.down ^ _btn.prev);
_btn.prev = _btn.down;
}
#if EXAMPLE > 0 && EXAMPLE < 4
static void palette_randomize() {
for (u8 i = 0; i < 16; i++) {
m96_palette[i].fg = GetPrngNumber(0);
m96_palette[i].bg = GetPrngNumber(0);
}
}
#endif
#if EXAMPLE == 2
static void palette_display() {
static u8 X = 3, Y = 4;
Print(X, Y-1, PSTR(" N: FG BG"));
for (u8 i = 0; i < 16; i++) {
PrintByte(X+1, Y+i, i, false);
PrintChar(X+2, Y+i, ':');
PrintHexByte(X+4, Y+i, m96_palette[i].fg);
PrintHexByte(X+7, Y+i, m96_palette[i].bg);
for (u8 j = 0; j < 9; j++)
SetTileColor(X+j, Y+i, i);
}
}
#elif EXAMPLE == 3
static void scramble_screen() {
for (u8 row = 0; row < SCREEN_TILES_V; row++) {
for (u8 col = 0; col < SCREEN_TILES_H; col++) {
SetTile(col, row, GetPrngNumber(0) % 96);
SetTileColor(col, row, GetPrngNumber(0) % 16);
}
}
}
#endif
int main() {
GetPrngNumber(GetTrueRandomSeed());
ClearVram();
#if EXAMPLE == 0
SetTile(0, 0, '0'-32);
SetTile(SCREEN_TILES_H-1, 0, '1'-32);
SetTile(SCREEN_TILES_H-1, SCREEN_TILES_V-1, '2'-32);
SetTile(0, SCREEN_TILES_V-1, '3'-32);
PrintColor(32, 2, PSTR("hurg nurg wurg"), 1);
u8 x = 10;
u8 line = 4;
PrintRamColor(x, line++, "foo bar baz", 1);
PrintBinaryByteColor(x, line++, 0x7f, 1);
PrintHexByteColor(x, line++, 0x7f, 1);
PrintHexIntColor(x, line++, 0x03f2, 1);
PrintHexLongColor(x, line++, 0xdeadbeef, 1);
PrintHexLongColor(x, line++, 0x0000beef, 1);
PrintLongColor(x, line++, 0xdeadbeef, 1);
PrintLongColor(x, line++, 0x00adbeef, 1);
PrintByteColor(x, line++, 0x7f, false, 1);
PrintByteColor(x, line++, 0x7f, true, 1);
PrintIntColor(x, line++, 5234, false, 1);
PrintIntColor(x, line++, 65234, false, 1);
PrintIntColor(x, line++, 5234, true, 1);
PrintIntColor(x, line++, 65234, true, 1);
PrintCharColor(x, line++, 'A', 1);
while (true)
WaitVsync(1);
#elif EXAMPLE == 1
for (u8 section = 0; section < 12; section++) {
for (u8 row = 0; row < 8; row++) {
for (u8 col = 0; col < 12; col++) {
u8 rowoff = 8 *(section % 4);
u8 coloff = 12*(section / 4);
if (row+rowoff > SCREEN_TILES_V-1)
continue;
SetTileBoth(col+coloff, row+rowoff, col + 12*row, section);
}
}
}
while (true) {
WaitVsync(1);
button_update();
if (_btn.prsd)
palette_randomize();
}
#elif EXAMPLE == 2
palette_display();
while (true) {
WaitVsync(1);
button_update();
if (_btn.prsd) {
palette_randomize();
palette_display();
}
}
#elif EXAMPLE == 3
palette_randomize();
scramble_screen();
while (true) {
WaitVsync(1);
button_update();
if (_btn.prsd) {
palette_randomize();
scramble_screen();
}
}
#elif EXAMPLE == 4
Print(8, 0, PSTR("ANSI VGA simulation"));
Print(0, 2, PSTR("normal"));
Print(18, 2, PSTR("bright"));
for (u8 i = 0; i < SCREEN_TILES_H; i++)
for (u8 j = 0; j < SCREEN_TILES_V; j++)
SetTileColor(i, j, 7);
u8 x = 0;
u8 line = 3;
char buf[18];
for (u8 color = 0; color < 16; color++) {
snprintf_P(buf, sizeof(buf), PSTR("%2d: color %2d: %02X"),
color + (color > 7 ? 82 : 30), color,
m96_palette[color].fg);
PrintRam(x, line, (unsigned char*)buf);
for (u8 i = 0; i < sizeof(buf); i++)
SetTileColor(x+i, line, color);
line++;
if (color == 7) {
x = 18;
line = 3;
}
}
while (true)
WaitVsync(1);
#else
#error Unknown example
#endif
}