-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtitle.c
More file actions
437 lines (364 loc) · 14.8 KB
/
title.c
File metadata and controls
437 lines (364 loc) · 14.8 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#include "memory.h"
#include "sprites.h"
#include "sprites_title.h"
#include "sound.h"
#include "tools.h"
#include "emt.h"
#include "digger_sprites_title.h"
#include "digger_music_title.h"
#include "digger_full_font.h"
#define COIN_Y_OFFSET 3 // Смещение спрайта монетки в ячейке по оси Y
void sound_tmr(uint16_t period, uint8_t durance, uint16_t count)
{
asm volatile (
"movb %[durance], @%[REG_TVE_LIMIT]\n\t"
"mov %[TIMER_MODE], @%[REG_TVE_CSR]\n"
"mov %[period], r1\n\t"
"mov $0100, r2\n\t"
"clr r0\n\t"
".l1_%=:\n\t"
"mov r1, r3\n\t"
"mov r0, @$-062\n"
// "bit $0100, r0\n\t"
// "beq .l2_%=\n\t"
// "sub %[count], r4\n\t"
// "br .l3_%=\n\t"
// ".l2_%=:\n\t"
// "add %[count], r4\n\t"
".l3_%=:\n\t"
"sob r3, .l3_%=\n\t"
"xor r2, r0\n\t"
"movb @%[REG_TVE_CSR], r3\n\t"
"tstb r3\n\t"
"bge .l1_%=\n\t"
:
: [period]"g"(period), [durance]"g"(durance), [count]"g"(count), [REG_TVE_LIMIT]"i"(REG_TVE_LIMIT), [REG_TVE_CSR]"i"(REG_TVE_CSR),
[TIMER_MODE]"i"((1 << TVE_CSR_MON) | (1 << TVE_CSR_RUN) | (1 << TVE_CSR_D16) /*| (1 << TVE_CSR_D4)*/)
: "r0", "r1", "r2", "r3", "cc", "memory"
);
}
/**
* @brief Вывод 16-битного десятичного числа
*
* @param number - число для вывода
* @param x_graph - координата X по которой будет осуществлён вывод числа
* @param y_graph - координата Y по которой будет осуществлён вывод числа
*/
void print_dec(uint16_t number, uint16_t x_graph, uint16_t y_graph)
{
constexpr char zero = '0';
char buf[5] = { zero, zero, zero, zero, zero }; // Буфер для 5 десятичных знаков
char *ptr = &buf[sizeof(buf)];
uint_to_str(number, &ptr);
for (uint8_t i = 0; i < sizeof(buf); ++i)
{
uint16_t index = buf[i] - zero;
sp_put(x_graph, y_graph, sizeof(ch_digits[0][0]), sizeof(ch_digits[0]) / sizeof(ch_digits[0][0]), (uint8_t *)ch_digits[index], nullptr); // Вывести спрайт цифры
x_graph += sizeof(ch_digits[0][0]);
}
}
/**
* @brief Вывод строки
*
* @param str - строка для вывода
* @param x_graph - координата X по которой будет осуществлён вывод числа
* @param y_graph - координата Y по которой будет осуществлён вывод числа
*/
void print_str(const char *str, uint16_t x_graph, uint16_t y_graph)
{
while (*str)
{
char c = *str++;
if (c != ' ')
{
uint16_t index = c - 'A';
sp_put(x_graph, y_graph, sizeof(ch_alpha[0][0]), sizeof(ch_alpha[0]) / sizeof(ch_alpha[0][0]), (uint8_t *)ch_alpha[index], nullptr); // Вывести спрайт буквы
}
x_graph += sizeof(ch_alpha[0][0]);
}
}
constexpr uint16_t char_width = sizeof(ch_alpha[0][0]);
constexpr uint16_t str_height = sizeof(ch_alpha[0]) / char_width;
constexpr uint16_t y_space = 8;
constexpr uint16_t windmill_height = 42;
constexpr uint16_t table_height = SCREEN_PIX_HEIGHT - (str_height + y_space) - windmill_height;
const char digger_str[] = "D I G G E R";
constexpr uint16_t digger_str_x_pos = (SCREEN_BYTE_WIDTH - char_width * sizeof(digger_str) + char_width) / 2;
const char one_str[] = "ONE";
constexpr uint16_t one_x_pos = (SCREEN_BYTE_WIDTH + SCREEN_BYTE_WIDTH / 2 - char_width * sizeof(one_str) + char_width) / 2;
const char player_str[] = "PLAYER";
constexpr uint16_t player_x_pos = (SCREEN_BYTE_WIDTH + SCREEN_BYTE_WIDTH / 2 - char_width * sizeof(player_str) + char_width) / 2;
uint16_t one_player_y;
uint16_t note_index = 0;
/**
* @brief Инициализация игры
*/
void init_demo()
{
sp_paint_brick_long(0, 0, SCREEN_BYTE_WIDTH, SCREEN_PIX_HEIGHT, 0); // Очистка экрана
uint16_t y_pos = 0;
// Строка "D I G G E R"
print_str(digger_str, digger_str_x_pos, y_pos);
y_pos += str_height + y_space;
// Верхняя линия рамки
sp_paint_brick_long(0, y_pos, SCREEN_BYTE_WIDTH, 2, 0b01010101);
y_pos += 2;
one_player_y = y_pos + y_space;
// Строка "ONE"
print_str(one_str, one_x_pos, one_player_y);
one_player_y += str_height + y_space;
// Строка "PLAYER"
print_str(player_str, player_x_pos, one_player_y);
one_player_y += str_height + y_space * 2;
// Вертикальные линии рамки и разделитель
sp_paint_brick_long(0, y_pos, 1, table_height, 0b00000001);
sp_paint_brick_long(SCREEN_BYTE_WIDTH - 1, y_pos, 1, table_height, 0b01000000);
sp_paint_brick_long(SCREEN_BYTE_WIDTH / 2, y_pos, 1, table_height, 0b01000000);
sp_paint_brick_long(SCREEN_BYTE_WIDTH / 2 + 1, y_pos, 1, table_height, 0b00000001);
y_pos += table_height;
// Нижняя линия рамки
sp_paint_brick_long(0, y_pos, SCREEN_BYTE_WIDTH, 2, 0b01010101);
}
constexpr uint16_t note_duration = 10;
constexpr uint16_t inter_note_delay = 1;
constexpr uint16_t inter_music_delay = 512;
uint16_t demo_time = 0;
uint16_t nobbin_x = 0, nobbin_y = 0;
uint16_t hobbin_x = 0, hobbin_y = 0;
uint16_t digger_x = 0, digger_y = 0;
uint16_t bag_x = 0, bag_y = 0;
uint16_t emerald_x = 0, emerald_y = 0;
uint16_t cherry_x = 0, cherry_y = 0;
bool hobbin_mirror, digger_mirror;
uint8_t image_phase; ///< Фаза анимации при выводе спрайта
int8_t image_phase_inc = 1; ///< Направление изменения фазы анимации при выводе спрайта (+1 или -1)
uint16_t *cur_music_ptr = nullptr;
uint16_t note_duration_count = note_duration;
uint16_t silence_count = 0;
/**
* @brief Обработка общего состояния игры (переход на новый уровень, Game Over и т.д.)
*/
void process_demo_state()
{
constexpr uint16_t image_width = sizeof(image_nobbin[0][0]);
constexpr uint16_t image_height = sizeof(image_nobbin[0]) / sizeof(image_nobbin[0][0]); // Высота спрайта Ноббина
constexpr uint16_t y_space = 8;
constexpr uint16_t move_start_pos = SCREEN_BYTE_WIDTH - 6;
constexpr uint16_t start_time = 0;
constexpr uint16_t start_delay = 128;
constexpr uint16_t move_durance = 184; // 176
constexpr uint16_t end_to_print = 16;
constexpr uint16_t print_to_next = 16;
// Тайминги отображения Ноббина в демо
constexpr uint16_t nobbin_start_time = start_time + start_delay;
constexpr uint16_t nobbin_begin_time = nobbin_start_time + 1;
constexpr uint16_t nobbin_end_time = nobbin_begin_time + move_durance;
constexpr uint16_t nobbin_print_time = nobbin_end_time + end_to_print;
// Тайминги отображения Хоббина в демо
constexpr uint16_t hobbin_start_time = nobbin_print_time + print_to_next;
constexpr uint16_t hobbin_begin_time = hobbin_start_time + 1;
constexpr uint16_t hobbin_end_time = hobbin_begin_time + move_durance;
constexpr uint16_t hobbin_mirror_time = hobbin_end_time + 1;
constexpr uint16_t hobbin_print_time = hobbin_mirror_time + end_to_print;
// Тайминги отображения Диггера в демо
constexpr uint16_t digger_start_time = hobbin_print_time + print_to_next;
constexpr uint16_t digger_begin_time = digger_start_time + 1;
constexpr uint16_t digger_end_time = digger_begin_time + move_durance;
constexpr uint16_t digger_mirror_time = digger_end_time + 1;
constexpr uint16_t digger_print_time = digger_mirror_time + end_to_print;
// Тайминги отображения мешка в демо
constexpr uint16_t bag_display_time = digger_print_time + move_durance;
constexpr uint16_t bag_print_time = bag_display_time + end_to_print;
// Тайминги отображения монеты в демо
constexpr uint16_t emerald_display_time = bag_print_time + move_durance;
constexpr uint16_t emerald_print_time = emerald_display_time + end_to_print;
// Тайминги отображения вишенки в демо
constexpr uint16_t cherry_display_time = emerald_print_time + move_durance;
constexpr uint16_t cherry_print_time = cherry_display_time + end_to_print;
// Время до повтора демо
constexpr uint16_t demo_restart_time = cherry_print_time + 256;
volatile uint16_t *t_limit = (volatile uint16_t *)REG_TVE_LIMIT;
volatile union TVE_CSR *tve_csr = (volatile union TVE_CSR *)REG_TVE_CSR;
uint16_t duration = popcorn_durations[note_index];
*t_limit = 48 - duration << 1;
tve_csr->reg = (1 << TVE_CSR_MON) | (1 << TVE_CSR_RUN) | (1 << TVE_CSR_D4);
switch (demo_time)
{
case start_time:
{
// Очистка области Demo
nobbin_x = hobbin_x = digger_x = 0;
constexpr uint16_t demo_height = table_height - (str_height + y_space * 2) * 2;
sp_paint_brick_long(SCREEN_BYTE_WIDTH / 2 + 2, one_player_y, SCREEN_BYTE_WIDTH / 2 - 3, demo_height, 0);
break;
}
case nobbin_start_time:
{
nobbin_x = move_start_pos;
nobbin_y = one_player_y;
break;
}
case nobbin_begin_time ... nobbin_end_time:
{
if (!(demo_time & 7)) nobbin_x--;
break;
}
case nobbin_print_time:
{
const char nobbin_str[] = "NOBBIN";
print_str(nobbin_str, nobbin_x + image_width * 2 - 1, nobbin_y);
break;
}
case hobbin_start_time:
{
hobbin_mirror = true;
hobbin_x = move_start_pos;
hobbin_y = nobbin_y + image_height + y_space;
break;
}
case hobbin_begin_time ... hobbin_end_time:
{
if (!(demo_time & 7)) hobbin_x--;
break;
}
case hobbin_mirror_time:
{
hobbin_mirror = false;
break;
}
case hobbin_print_time:
{
const char hobbin_str[] = "HOBBIN";
print_str(hobbin_str, hobbin_x + image_width * 2 - 1, hobbin_y);
break;
}
case digger_start_time:
{
digger_mirror = true;
digger_x = move_start_pos;
digger_y = hobbin_y + image_height + y_space;
break;
}
case digger_begin_time ... digger_end_time:
{
if (!(demo_time & 7)) digger_x--;
break;
}
case digger_mirror_time:
{
digger_mirror = false;
break;
}
case digger_print_time:
{
const char digger_str[] = "DIGGER";
print_str(digger_str, digger_x + image_width * 2 - 1, digger_y);
break;
}
case bag_display_time:
{
bag_x = digger_x;
bag_y = digger_y + image_height + y_space;
sp_4_15_put(bag_x, bag_y, (uint8_t *)image_bag);
break;
}
case bag_print_time:
{
const char digger_str[] = "GOLD";
print_str(digger_str, bag_x + image_width * 2 - 1, bag_y);
break;
}
case emerald_display_time:
{
emerald_x = bag_x;
emerald_y = bag_y + image_height + y_space;
sp_put(emerald_x, emerald_y + COIN_Y_OFFSET, sizeof(image_coin[0]), sizeof(image_coin) / sizeof(image_coin[0]), (uint8_t *)image_coin, nullptr);
break;
}
case emerald_print_time:
{
const char emerald_str[] = "EMERALD";
print_str(emerald_str, emerald_x + image_width * 2 - 1, emerald_y);
break;
}
case cherry_display_time:
{
cherry_x = emerald_x;
cherry_y = emerald_y + image_height + y_space;
sp_4_15_put(cherry_x, cherry_y, (uint8_t *)image_cherry);
break;
}
case cherry_print_time:
{
const char bonus_str[] = "BONUS";
print_str(bonus_str, cherry_x + image_width * 2 - 1, cherry_y);
break;
}
}
if (nobbin_x)
{
sp_paint_brick_long(nobbin_x + image_width, nobbin_y, 1, image_height, 0);
sp_4_15_put(nobbin_x, nobbin_y, (uint8_t *)image_nobbin[image_phase]);
}
if (hobbin_x)
{
sp_paint_brick_long(hobbin_x + image_width, hobbin_y, 1, image_height, 0);
if (hobbin_mirror) sp_4_15_put(hobbin_x, hobbin_y, (uint8_t *)image_hobbin_left[image_phase]);
else sp_4_15_put(hobbin_x, hobbin_y, (uint8_t *)image_hobbin_right[image_phase]);
}
if (digger_x)
{
sp_paint_brick_long(digger_x + image_width, digger_y, 1, image_height, 0);
if (digger_mirror) sp_4_15_h_mirror_put(digger_x, digger_y, (uint8_t *)image_digger_right[image_phase]);
else sp_4_15_put(digger_x, digger_y, (uint8_t *)image_digger_right[image_phase]);
}
if (!(demo_time & 7))
{
// Увеличить/уменьшить фазу на единицу
image_phase += image_phase_inc;
// Переключить направление изменения фазы, если фаза дошла до предельного значения
if (!image_phase || image_phase >= 2) image_phase_inc = -image_phase_inc;
}
// Увеличивать время Demo по кругу
if (++demo_time > demo_restart_time)
{
demo_time = 0;
}
while ((tve_csr->reg & (1 << TVE_CSR_FL)) == 0); // Ожидать срабатывания таймера.
if (silence_count > 0)
{
silence_count--;
return;
}
if (--note_duration_count == 0)
{
note_duration_count = note_duration;
note_index++;
silence_count = inter_note_delay;
return;
}
uint8_t period = popcorn_periods[note_index];
if (!period)
{
note_index = 0;
silence_count = inter_music_delay;
return;
}
sound_tmr(period, duration << 3, note_duration_count);
}
extern void start();
/**
* @brief Основная программа
*/
void main()
{
// EMT_14();
typedef void (*vector)();
*((volatile vector *)VEC_STOP) = start; // Установить вектор клавиши "СТОП" на _start
// EMT_16(0233);
// EMT_16(0236);
set_PSW(1 << PSW_I); // Замаскировать прерывания IRQ
((union KEY_STATE *)REG_KEY_STATE)->bits.INT_MASK = 1; // Отключить прерывание от клавиатуры
init_demo(); // Начальная инициализация игры
for (;;) process_demo_state(); // Основной бесконечный цикл демо
}