8 #include "jdb_module.h"
9 #include "static_init.h"
13 #include "kernel_console.h"
16 static int lines, score, current_pos, mode, slice,
17 *current_tile, *next_tile, grid[264], next[48];
19 static int tiles[] = { 9, 9, -13, -12, 1, 4,
20 10, 10, -11, -12, -1, 4,
22 3, 3, -13, -12, -1, 2,
26 23, 21, -12, 11, 13, 3,
31 13, 11, -12, -1, 1, 6,
32 2, 12, -12, -1, 12, 6,
33 15, 4, -12, 12, 13, 5,
34 16, 14, -11, -1, 1, 5,
35 4, 15, -13, -12, 12, 5,
36 18, 5, -11, -12, 12, 5,
37 19, 17, -13, 1, -1, 5,
38 5, 18, -12, 12, 11, 5,
41 21, 23, -13, -11, 12, 3,
42 22, 7, -11, -1, 13, 3 };
44 static const char *modes[] = { "", "Fiasco Mode", "Lars Mode" };
45 static long unsigned int randseed;
49 static unsigned slice_to_timeout(unsigned slice)
52 return slice < 2 ? 2 : slice;
55 static void show_grid()
61 for (i = j = 0; i < 264; i++)
64 printf ("\033[m\033[1;4%d;30m \033[40m", grid[i]);
70 for (; j <= i && j < 48; j++)
72 printf ("\033[m\033[1;4%d;30m \033[40m", next[j]);
74 putstr ("\033[m\033[30m \033[m");
80 printf ("\033[mLines: %d Score: %d %s\033[K", lines, score, modes[mode]);
85 static unsigned slice_to_timeout(unsigned slice)
90 static void show_grid()
96 for (i = j = 0; i < 264; i++)
99 printf ("\033[1;3%dm\333\333", grid[i]);
105 for (; j <= i && j < 48; j++)
107 printf ("\033[1;3%dm\333\333", next[j]);
115 printf ("Lines: %d Score: %d %s\033[K", lines, score, modes[mode]);
120 static int getchar_timeout()
123 static unsigned to = slice;
125 to = slice_to_timeout(to);
128 if ((c = Kconsole::console()->getchar (false)) != -1)
131 if (Config::getchar_does_hlt && Config::getchar_does_hlt_works_ok)
134 Cpu::cpus.cpu(0).busy_wait_ns(1000000ULL);
142 static void add_score(int value)
144 if ((score + value) / 1000 != score / 1000)
153 static long unsigned int myrand()
155 randseed = (randseed * 13561+14000) % 150001;
159 static int *new_tile()
161 return tiles + myrand() % (7 + mode) * 6;
164 static int try_move (int pos, int *tile)
166 if (grid[pos] + grid[pos+tile[2]] + grid[pos+tile[3]] + grid[pos+tile[4]])
175 static void set_grid (int color)
178 grid[current_pos+current_tile[2]] =
179 grid[current_pos+current_tile[3]] =
180 grid[current_pos+current_tile[4]] = color;
183 static void set_next (int color)
186 next[17+next_tile[2]] =
187 next[17+next_tile[3]] =
188 next[17+next_tile[4]] = color;
191 void show_tile (void)
193 set_grid(current_tile[5]);
194 set_next(next_tile[5]);
205 * This module makes fun.
211 Jdb_tetris_m() FIASCO_INIT;
214 static Jdb_tetris_m jdb_tetris_m INIT_PRIORITY(JDB_MODULE_INIT_PRIO);
217 Jdb_tetris_m::Jdb_tetris_m()
222 Jdb_module::Action_code
223 Jdb_tetris_m::action( int, void *&, char const *&, int & )
227 lines = score = c = 0;
229 randseed= Cpu::rdtsc() & 0xffffffff;
231 puts ("\nDisabling output of serial console -- quit Tetris with 'q'!");
232 Kconsole::console()->change_state(Console::UART, 0, ~Console::OUTENABLED, 0);
234 printf ("\033[H\033[J");
236 // Setup grid borders
237 for (i = 0; i < 264; i++)
238 grid[i] = (i % 12 == 0 || i % 12 == 11 || i > 251) ? 7 : 0;
240 try_move (17, new_tile());
241 next_tile = new_tile();
247 if (!try_move (current_pos + 12, current_tile))
249 set_grid(current_tile[5]);
251 for (i = k = 0; i < 252; i += 12)
252 for (j = i; grid[++j];)
254 while (j > i) grid[j--] = 0; show_grid();
255 while (--j) grid[j + 12] = grid[j]; show_grid();
260 add_score (k ? (1 << k) * 10 : 1);
262 if (!try_move (17, next_tile))
265 next_tile = new_tile();
270 else if (c == KEY_CURSOR_LEFT)
271 try_move (current_pos - 1, current_tile);
274 else if (c == KEY_CURSOR_RIGHT)
275 try_move (current_pos + 1, current_tile);
279 while (try_move (current_pos + 12, current_tile))
286 else if (c == KEY_CURSOR_DOWN)
287 try_move (current_pos, tiles + 6 ** current_tile);
290 else if (c == KEY_CURSOR_UP)
291 try_move (current_pos, tiles + 6 ** (current_tile+1));
294 else if (c == 'q' || c == KEY_ESC)
303 c = getchar_timeout();
306 Kconsole::console()->change_state(Console::UART, 0, ~0U, Console::OUTENABLED);
314 Jdb_tetris_m::num_cmds() const
320 Jdb_module::Cmd const *
321 Jdb_tetris_m::cmds() const
326 "X\tPlay Tetris (cursor keys = left/right/rotate;\n"
327 "\t[space] = drop; q = quit)", 0 },