summaryrefslogtreecommitdiffstats
path: root/src/sequencer.c
blob: d7d1c7cae3c83e95e0ea31f1cb51fee3a4ddd4e2 (plain)
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
#define SPRITE_NOTE_NAMES_W 16
#define SPRITE_NOTE_NAMES_H 8

u32 sprite_note_names[] = {
    0x000000e0, 0x202020e0, 0x0000000e, 0x080e020e,
    0x00000098, 0xa8a8a898, 0x00000038, 0x203b0a39,
    0x00000060, 0xa0a0a060, 0x0000000e, 0x080e020e,
    0x000000b8, 0x889888b8, 0x00000038, 0x203b0a39,
    0x000000e0, 0x206020e0, 0x0000000e, 0x080e020e,
    0x000000e0, 0x20602020, 0x0000000e, 0x080e020e,
    0x000000b8, 0x8888a8b8, 0x00000038, 0x203b0a39,
    0x000000e0, 0x2020a0e0, 0x0000000e, 0x080e020e,
    0x000000b8, 0xa8a8b8a8, 0x00000038, 0x203b0a39,
    0x000000e0, 0xa0a0e0a0, 0x0000000e, 0x080e020e,
    0x000000b8, 0xa898a8b8, 0x00000038, 0x203b0a39,
    0x000000e0, 0xa060a0e0, 0x0000000e, 0x080e020e,
    0x000000e0, 0x202020e0, 0x0000000e, 0x080c080e,
    0x00000098, 0xa8a8a898, 0x00000038, 0x20332239,
    0x00000060, 0xa0a0a060, 0x0000000e, 0x080c080e,
    0x000000b8, 0x889888b8, 0x00000038, 0x20332239,
    0x000000e0, 0x206020e0, 0x0000000e, 0x080c080e,
    0x000000e0, 0x20602020, 0x0000000e, 0x080c080e,
    0x000000b8, 0x8888a8b8, 0x00000038, 0x20332239,
    0x000000e0, 0x2020a0e0, 0x0000000e, 0x080c080e,
    0x000000b8, 0xa8a8b8a8, 0x00000038, 0x20332239,
    0x000000e0, 0xa0a0e0a0, 0x0000000e, 0x080c080e,
    0x000000b8, 0xa898a8b8, 0x00000038, 0x20332239,
    0x000000e0, 0xa060a0e0, 0x0000000e, 0x080c080e,
    0x000000e0, 0x202020e0, 0x00000008, 0x0c0a0e08,
    0x00000098, 0xa8a8a898, 0x00000020, 0x302b3a21,
    0x00000060, 0xa0a0a060, 0x00000008, 0x0c0a0e08,
    0x000000b8, 0x889888b8, 0x00000020, 0x302b3a21,
    0x000000e0, 0x206020e0, 0x00000008, 0x0c0a0e08,
    0x000000e0, 0x20602020, 0x00000008, 0x0c0a0e08,
    0x000000b8, 0x8888a8b8, 0x00000020, 0x302b3a21,
    0x000000e0, 0x2020a0e0, 0x00000008, 0x0c0a0e08,
    0x000000b8, 0xa8a8b8a8, 0x00000020, 0x302b3a21,
    0x000000e0, 0xa0a0e0a0, 0x00000008, 0x0c0a0e08,
    0x000000b8, 0xa898a8b8, 0x00000020, 0x302b3a21,
    0x000000e0, 0xa060a0e0, 0x00000008, 0x0c0a0e08,
    0x000000e0, 0x202020e0, 0x0000000e, 0x020e080e,
    0x00000098, 0xa8a8a898, 0x00000038, 0x083b2239,
    0x00000060, 0xa0a0a060, 0x0000000e, 0x020e080e,
    0x000000b8, 0x889888b8, 0x00000038, 0x083b2239,
    0x000000e0, 0x206020e0, 0x0000000e, 0x020e080e,
    0x000000e0, 0x20602020, 0x0000000e, 0x020e080e,
    0x000000b8, 0x8888a8b8, 0x00000038, 0x083b2239,
    0x000000e0, 0x2020a0e0, 0x0000000e, 0x020e080e,
    0x000000b8, 0xa8a8b8a8, 0x00000038, 0x083b2239,
    0x000000e0, 0xa0a0e0a0, 0x0000000e, 0x020e080e,
    0x000000b8, 0xa898a8b8, 0x00000038, 0x083b2239,
    0x000000e0, 0xa060a0e0, 0x0000000e, 0x020e080e,
    0x000000e0, 0x202020e0, 0x0000000e, 0x020e0a0e,
    0x00000098, 0xa8a8a898, 0x00000038, 0x083b2a39,
    0x00000060, 0xa0a0a060, 0x0000000e, 0x020e0a0e,
    0x000000b8, 0x889888b8, 0x00000038, 0x083b2a39,
    0x000000e0, 0x206020e0, 0x0000000e, 0x020e0a0e,
    0x000000e0, 0x20602020, 0x0000000e, 0x020e0a0e,
    0x000000b8, 0x8888a8b8, 0x00000038, 0x083b2a39,
    0x000000e0, 0x2020a0e0, 0x0000000e, 0x020e0a0e,
    0x000000b8, 0xa8a8b8a8, 0x00000038, 0x083b2a39,
    0x000000e0, 0xa0a0e0a0, 0x0000000e, 0x020e0a0e,
    0x000000b8, 0xa898a8b8, 0x00000038, 0x083b2a39,
    0x000000e0, 0xa060a0e0, 0x0000000e, 0x020e0a0e,
    0x000000e0, 0x202020e0, 0x0000000e, 0x08040202,
    0x00000098, 0xa8a8a898, 0x00000038, 0x20130a09,
    0x00000060, 0xa0a0a060, 0x0000000e, 0x08040202,
    0x000000b8, 0x889888b8, 0x00000038, 0x20130a09,
    0x000000e0, 0x206020e0, 0x0000000e, 0x08040202,
    0x000000e0, 0x20602020, 0x0000000e, 0x08040202,
    0x000000b8, 0x8888a8b8, 0x00000038, 0x20130a09,
    0x000000e0, 0x2020a0e0, 0x0000000e, 0x08040202,
    0x000000b8, 0xa8a8b8a8, 0x00000038, 0x20130a09,
    0x000000e0, 0xa0a0e0a0, 0x0000000e, 0x08040202,
    0x000000b8, 0xa898a8b8, 0x00000038, 0x20130a09,
    0x000000e0, 0xa060a0e0, 0x0000000e, 0x08040202,
    0x000000e0, 0x202020e0, 0x0000000e, 0x0a0e0a0e,
};

u32 sprite_trigger_button[] = {
    0xfe020202, 0x02020202, 0xff808080, 0x80808080,
    0x02020202, 0x02020202, 0x80808080, 0x80808080,
    0x02020202, 0x020202fe, 0x80808080, 0x808080ff,
    0x00000000, 0x00000000, 0x00000000, 0x00000000,
};

u32 sprite_trigger_active_indicator[] = {
    0x00000000, 0x00f80000,
};

u32 sprite_trigger_selection[] = {
    0x0f010101, 0x00000000, 0x80000000, 0x00000000,
    0x07040404, 0x00000000, 0x00000000, 0x00000000,
    0x00000000, 0x00000000, 0x00000000, 0x00000000,
    0x00000000, 0x00000000, 0x00000000, 0x00000000,
    0x00000000, 0x00000000, 0x00000000, 0x00000000,
    0x00000000, 0x00000000, 0x00000000, 0x00000000,
    0x0101010f, 0x00000000, 0x00000080, 0x00000000,
    0x04040407, 0x00000000, 0x00000000, 0x00000000,
};

u32 sprite_env_label_volume[] = {
    0xa0a0a0a0, 0xc0000000, 0x262a2a2a, 0xee000000,
    0xaaeaaaaa, 0xae000000, 0x0e020602, 0x0e000000,
};

u32 sprite_env_label_time[] = {
    0x00000000, 0x00000000, 0xee444444, 0xe4000000,
    0xea2e6a2a, 0xea000000, 0x00000000, 0x00000000,
};

u32 sprite_env_label_direction[] = {
    0x00000000, 0x00000000, 0xcc949494, 0xcc000000,
    0x1d140cd4, 0xd5000000, 0x00000000, 0x00000000,
};

u32 sprite_env_label_env[] = {
    0x6ea2a6a2, 0xae000000, 0xea2a6a2a, 0xec000000,
    0x62a2a2a2, 0xee000000, 0xee2a6e22, 0xe2000000,
};

u32 sprite_env_volume[] = {
    0x80808080, 0x80808000, 0x0f0c0c0c, 0x0c6c6f00,
    0x1f999919, 0x19999f00, 0x00090d06, 0x030d0c00,
    0x80808080, 0x80808000, 0x0f01010f, 0x09696f00,
    0x1f83831f, 0x13939f00, 0x00090d06, 0x030d0c00,
    0x60606060, 0x60606000, 0x1f18181e, 0x18d8df00,
    0x3e30303c, 0x30303e00, 0x00131b0c, 0x061b1900,
    0xf8c0c0f8, 0x1818f800, 0x3e06063e, 0x26a6be00,
    0x7c0c0c7c, 0x4c4d7d00, 0x00263618, 0x0c363200,
    0xf8c0c0f8, 0x1818f800, 0x3e323232, 0x32b2be00,
    0x7c646464, 0x64657d00, 0x00263618, 0x0c363200,
    0xf8c0c0f0, 0xc0c0f800, 0x3e30303c, 0x30b0be00,
    0x7c606078, 0x60617d00, 0x00263618, 0x0c363200,
    0xc0e0d0c8, 0xf8c0c000, 0x3e323232, 0x32b2be00,
    0x7c646464, 0x64657d00, 0x00263618, 0x0c363200,
    0xc0e0d0c8, 0xf8c0c000, 0x3e06063e, 0x26a6be00,
    0x7c0c0c7c, 0x4c4d7d00, 0x00263618, 0x0c363200,
    0xf81818f8, 0xc0c0f800, 0x3e30303c, 0x30b0be00,
    0x7c606078, 0x60617d00, 0x00263618, 0x0c363200,
    0xf81818f8, 0x9898f800, 0x3e323232, 0x32b2be00,
    0x7c646464, 0x64657d00, 0x00263618, 0x0c363200,
    0xf81818f8, 0x9898f800, 0x3e06063e, 0x26a6be00,
    0x7c0c0c7c, 0x4c4d7d00, 0x00263618, 0x0c363200,
    0xf8c0c060, 0x30181800, 0x3e30303c, 0x30b0be00,
    0x7c606078, 0x60617d00, 0x00263618, 0x0c363200,
    0xf8c8c8f8, 0xc8c8f800, 0x3e323232, 0x32b2be00,
    0x7c646464, 0x64657d00, 0x00263618, 0x0c363200,
    0xf8c8c8f8, 0xc8c8f800, 0x3e06063e, 0x26a6be00,
    0x7c0c0c7c, 0x4c4d7d00, 0x00263618, 0x0c363200,
    0xf8c8c8f8, 0xc0c0f800, 0x3e30303c, 0x30b0be00,
    0x7c606078, 0x60617d00, 0x00263618, 0x0c363200,
    0xec2c2c2c, 0x2c2cec00, 0xfbcbcbcb, 0xcbcbfb00,
    0xf0909090, 0x9096f600, 0x0199d961, 0x31d9c900,
};

u32 sprite_env_time[] = {
    0x80808080, 0x80808000, 0x0f0c0c0c, 0x0c6c6f00,
    0x1f999919, 0x19999f00, 0x00090d06, 0x030d0c00,
    0x60606060, 0x60606000, 0x181c1a19, 0x1fd8d800,
    0x3e30303c, 0x30303e00, 0x00131b0c, 0x061b1900,
    0xf8c0c0f8, 0x1818f800, 0x3e32323e, 0x32b2be00,
    0x7c0c0c7c, 0x4c4d7d00, 0x00263618, 0x0c363200,
    0xc0e0d0c8, 0xf8c0c000, 0x3e30303e, 0x0686be00,
    0x7c64647c, 0x60617d00, 0x00263618, 0x0c363200,
    0xe06060e0, 0x0000e000, 0xfbc0c063, 0x331b1b00,
    0x30303030, 0x30363600, 0x00131b0c, 0x061b1900,
    0xe0000080, 0xc0606000, 0x1b1b1b19, 0x18d8d800,
    0x30383432, 0x3e303000, 0x00131b0c, 0x061b1900,
    0xf8c8c8f8, 0xc8c8f800, 0x3e06063e, 0x30b0be00,
    0x7c606030, 0x180d0d00, 0x00263618, 0x0c363200,
    0xec2c2c2c, 0x2c2cec00, 0xfbcbcbcb, 0xcbcbfb00,
    0xf0909090, 0x9096f600, 0x0199d961, 0x31d9c900,
};

u32 sprite_env_sweep_direction[] = {
    0x00000000, 0x00000000, 0xc8c8c8c8, 0xc8c8f800,
    0x3e32323e, 0x02020200, 0x00000000, 0x00000000,
    0xe0202020, 0x2020e000, 0x71cbcbcb, 0xcbcb7100,
    0xb2b2b2b2, 0xb2ba9400, 0x0d0f0e0c, 0x0c0c0c00,
};

typedef struct SeqTrigger {
    bool trigger;
    Note note;
    u8 env_volume;
    u8 env_time;
    u8 env_direction;
    // TODO: ...
} SeqTrigger;

static SeqTrigger sequence_synth[] = {
    {true  , NOTE_D_4       , 0, 0, 0},
    {true  , NOTE_F_4       , 1, 1, 0},
    {true  , NOTE_A_4       , 2, 2, 0},
    {true  , NOTE_C_5       , 3, 3, 0},

    {true  , NOTE_D_4       , 4, 4, 1},
    {false , NOTE_C_SHARP_4 , 5, 5, 1},
    {false , NOTE_D_4       , 6, 6, 1},
    {false , NOTE_D_4       , 7, 7, 1},

    {true  , NOTE_D_4       , 8, 0, 0},
    {true  , NOTE_F_4       , 9, 1, 0},
    {true  , NOTE_A_4       , 10, 2, 0},
    {true  , NOTE_C_5       , 11, 3, 0},

    {true  , NOTE_D_4       , 12, 4, 1},
    {false , NOTE_D_4       , 13, 5, 1},
    {true  , NOTE_A_4       , 14, 6, 1},
    {false , NOTE_A_5       , 15, 7, 1},
};

static int bpm = 120;
static int step_counter = 0;
static Note active_note;

void
irq_timer_0(void) {
    SeqTrigger *trig = &sequence_synth[step_counter];
    active_note = trig->note;
    if (trig->trigger) {
        SOUND_SQUARE1_CTRL = SOUND_SQUARE_ENV_VOL(trig->env_volume)
            | SOUND_SQUARE_ENV_TIME(trig->env_time)
            | SOUND_SQUARE_ENV_INC(trig->env_direction)
            | SOUND_SQUARE_DUTY(2);
        SOUND_SQUARE1_FREQ = SOUND_SQUARE_RESET | sound_rates[active_note];
    }
    step_counter = (step_counter + 1) % 16;
}

void
set_time(int bpm) {
    // The number of ticks of a 1024 cycle clock in a step based on the BPM can
    // be calculated as:
    //     X bpm -> 60000 / 4 / bpm = Y ms = Ye-3 s
    //     Y ms  -> Ye-3 / 59.99e-9 /1024 = Z ticks
    // We have to operate on integer values, so the numbers have been
    // precalculated to `n_ticks = 244181 / bmp`
    int n_ticks = -244181 / bpm;

    irs_set(IRQ_TIMER_0, irq_timer_0);
    TIMER_DATA_0 = n_ticks;
    TIMER_CTRL_0 = TIMER_CTRL_IRQ | TIMER_CTRL_ENABLE | TIMER_CTRL_FREQ_3;
}

//
// Sequencer sprite memory.
//
// Currently we load all sequencer sprite memory in the VRAM:
//
// - Note names: 73x2 tiles.
// - Sprite trigger button: 8 tiles.
// - Current step marker: 1 tile.
// - Selected trigger marker: 16 tile.
// - Env: Volume label: 4 tiles.
// - Env: Volume text: 16x4 tiles
// - Env: Time label: 4 tiles.
// - Env: Time text: 8x4 tiles
// - Env: Direction label: 4 tiles.
// - Env: Direction text: 2x4 tiles
//
// The order of OBJs correspond to:
//
// - 000-015 step note names.
// - 015-031 step trigger steps.
// - 032-032 current step marker.
// - 033-033 trigger selection indicator.
// - 034-034 envelope: volume label.
// - 035-035 envelope: volume indicator.
// - 036-036 envelope: time label.
// - 037-037 envelope: time indicator.
// - 038-038 envelope: direction label.
// - 039-039 envelope: direction indicator.
// - 040-040 envelope: envelope label.
//


// Positioning parameters.
#define SEQ_TRIG_POS_X 45
#define SEQ_TRIG_POS_Y 50
#define SEQ_TRIG_DIST 20
#define SEQ_ENV_POS_X 10
#define SEQ_ENV_POS_Y 10
#define SEQ_ENV_DIST  34

size_t obj_counter = 0;

typedef struct SeqSprite {
    u8 id;
    int x;
    int y;
    size_t base_tile;
    u16 obj_attr_0;
    u16 obj_attr_1;
    u16 obj_attr_2;
} SeqSprite;

int trig_selection_loc = 1;

SeqSprite seq_sprites[41] = {0};

void
init_sequencer_sprites(void) {
    // Sprite note names.
    size_t sprite_id = load_packed_sprite_data(&sprite_note_names, 2, 73);
    for (size_t i = 0; i < 16; ++i) {
        int x = SEQ_TRIG_POS_X + i * SEQ_TRIG_DIST;
        int y = SEQ_TRIG_POS_Y;
        int base_tile = sprites[sprite_id].tile_start;
        if (i >= 8) {
            y += 32;
            x -= 8 * SEQ_TRIG_DIST;
        }
        seq_sprites[i].x = x;
        seq_sprites[i].y = y;

        seq_sprites[i].id = obj_counter++;
        seq_sprites[i].base_tile = base_tile;
        seq_sprites[i].obj_attr_0 = OBJ_SHAPE_WIDE | OBJ_HIDDEN | OBJ_Y_COORD(y);
        seq_sprites[i].obj_attr_1 = OBJ_SIZE_SMALL | OBJ_X_COORD(x);
        seq_sprites[i].obj_attr_2 = base_tile;
    }

    // Trigger boxes.
    sprite_id = load_packed_sprite_data(&sprite_trigger_button, 8, 1);
    for (size_t i = 0; i < 16; ++i) {
        int x = SEQ_TRIG_POS_X + i * SEQ_TRIG_DIST;
        int y = SEQ_TRIG_POS_Y;
        int base_tile = sprites[sprite_id].tile_start;
        if (i >= 8) {
            y += 32;
            x -= 8 * SEQ_TRIG_DIST;
        }
        seq_sprites[i + 16].id = obj_counter++;
        seq_sprites[i + 16].base_tile = base_tile;
        seq_sprites[i + 16].obj_attr_0 = OBJ_SHAPE_TALL | OBJ_Y_COORD(y);
        seq_sprites[i + 16].obj_attr_1 = OBJ_SIZE_BIG | OBJ_X_COORD(x);
        seq_sprites[i + 16].obj_attr_2 = base_tile;
    }

    sprite_id = load_packed_sprite_data(&sprite_trigger_active_indicator, 1, 1);
    {
        int x = SEQ_TRIG_POS_X;
        int y = SEQ_TRIG_POS_Y + 15;
        int base_tile = sprites[sprite_id].tile_start;
        seq_sprites[32].id = obj_counter++;
        seq_sprites[32].base_tile = base_tile;
        seq_sprites[32].obj_attr_0 = OBJ_SHAPE_SQUARE | OBJ_Y_COORD(y);
        seq_sprites[32].obj_attr_1 = OBJ_SIZE_SMALL | OBJ_X_COORD(x);
        seq_sprites[32].obj_attr_2 = base_tile | OBJ_PAL_BANK(1);
    }

    sprite_id = load_packed_sprite_data(&sprite_trigger_selection, 16, 1);
    {
        int x = SEQ_TRIG_POS_X - 1 + trig_selection_loc * SEQ_TRIG_DIST;
        int y = SEQ_TRIG_POS_Y - 2;
        int base_tile = sprites[sprite_id].tile_start;
        if (trig_selection_loc >= 8) {
            y += 32;
            x -= 8 * SEQ_TRIG_DIST;
        }
        seq_sprites[33].id = obj_counter++;
        seq_sprites[33].base_tile = base_tile;
        seq_sprites[33].obj_attr_0 = OBJ_SHAPE_SQUARE | OBJ_Y_COORD(y);
        seq_sprites[33].obj_attr_1 = OBJ_SIZE_BIG | OBJ_X_COORD(x);
        seq_sprites[33].obj_attr_2 = base_tile | OBJ_PAL_BANK(2);
    }

    sprite_id = load_packed_sprite_data(&sprite_env_label_volume, 4, 1);
    {
        int x = SEQ_ENV_POS_X;
        int y = SEQ_ENV_POS_Y;
        int base_tile = sprites[sprite_id].tile_start;
        seq_sprites[34].id = obj_counter++;
        seq_sprites[34].base_tile = base_tile;
        seq_sprites[34].obj_attr_0 = OBJ_SHAPE_WIDE | OBJ_Y_COORD(y);
        seq_sprites[34].obj_attr_1 = OBJ_SIZE_MID | OBJ_X_COORD(x);
        seq_sprites[34].obj_attr_2 = base_tile | OBJ_PAL_BANK(2);
    }

    sprite_id = load_packed_sprite_data(&sprite_env_volume, 4, 16);
    {
        int x = SEQ_ENV_POS_X;
        int y = SEQ_ENV_POS_Y + 8;
        int base_tile = sprites[sprite_id].tile_start;
        seq_sprites[35].id = obj_counter++;
        seq_sprites[35].base_tile = base_tile;
        seq_sprites[35].obj_attr_0 = OBJ_SHAPE_WIDE | OBJ_Y_COORD(y);
        seq_sprites[35].obj_attr_1 = OBJ_SIZE_MID | OBJ_X_COORD(x);
        seq_sprites[35].obj_attr_2 = base_tile | OBJ_PAL_BANK(2);
    }

    sprite_id = load_packed_sprite_data(&sprite_env_label_time, 4, 1);
    {
        int x = SEQ_ENV_POS_X + SEQ_ENV_DIST;
        int y = SEQ_ENV_POS_Y;
        int base_tile = sprites[sprite_id].tile_start;
        seq_sprites[36].id = obj_counter++;
        seq_sprites[36].base_tile = base_tile;
        seq_sprites[36].obj_attr_0 = OBJ_SHAPE_WIDE | OBJ_Y_COORD(y);
        seq_sprites[36].obj_attr_1 = OBJ_SIZE_MID | OBJ_X_COORD(x);
        seq_sprites[36].obj_attr_2 = base_tile | OBJ_PAL_BANK(2);
    }

    sprite_id = load_packed_sprite_data(&sprite_env_time, 4, 8);
    {
        int x = SEQ_ENV_POS_X + SEQ_ENV_DIST;
        int y = SEQ_ENV_POS_Y + 8;
        int base_tile = sprites[sprite_id].tile_start;
        seq_sprites[37].id = obj_counter++;
        seq_sprites[37].base_tile = base_tile;
        seq_sprites[37].obj_attr_0 = OBJ_SHAPE_WIDE | OBJ_Y_COORD(y);
        seq_sprites[37].obj_attr_1 = OBJ_SIZE_MID | OBJ_X_COORD(x);
        seq_sprites[37].obj_attr_2 = base_tile | OBJ_PAL_BANK(2);
    }

    sprite_id = load_packed_sprite_data(&sprite_env_label_direction, 4, 1);
    {
        int x = SEQ_ENV_POS_X + SEQ_ENV_DIST * 2;
        int y = SEQ_ENV_POS_Y;
        int base_tile = sprites[sprite_id].tile_start;
        seq_sprites[38].id = obj_counter++;
        seq_sprites[38].base_tile = base_tile;
        seq_sprites[38].obj_attr_0 = OBJ_SHAPE_WIDE | OBJ_Y_COORD(y);
        seq_sprites[38].obj_attr_1 = OBJ_SIZE_MID | OBJ_X_COORD(x);
        seq_sprites[38].obj_attr_2 = base_tile | OBJ_PAL_BANK(2);
    }

    sprite_id = load_packed_sprite_data(&sprite_env_sweep_direction, 4, 2);
    {
        int x = SEQ_ENV_POS_X + SEQ_ENV_DIST * 2;
        int y = SEQ_ENV_POS_Y + 8;
        int base_tile = sprites[sprite_id].tile_start;
        seq_sprites[39].id = obj_counter++;
        seq_sprites[39].base_tile = base_tile;
        seq_sprites[39].obj_attr_0 = OBJ_SHAPE_WIDE | OBJ_Y_COORD(y);
        seq_sprites[39].obj_attr_1 = OBJ_SIZE_MID | OBJ_X_COORD(x);
        seq_sprites[39].obj_attr_2 = base_tile | OBJ_PAL_BANK(2);
    }

    sprite_id = load_packed_sprite_data(&sprite_env_label_env, 4, 1);
    {
        int x = SEQ_ENV_POS_X + SEQ_ENV_DIST;
        int y = SEQ_ENV_POS_Y - 8;
        int base_tile = sprites[sprite_id].tile_start;
        seq_sprites[40].id = obj_counter++;
        seq_sprites[40].base_tile = base_tile;
        seq_sprites[40].obj_attr_0 = OBJ_SHAPE_WIDE | OBJ_Y_COORD(y);
        seq_sprites[40].obj_attr_1 = OBJ_SIZE_MID | OBJ_X_COORD(x);
        seq_sprites[40].obj_attr_2 = base_tile | OBJ_PAL_BANK(2);
    }
}

void
update_sequencer_sprites(void) {
    // 000-015: Step note names.
    for (size_t i = 0; i < 16; ++i) {
        // Each note name is made of 2 8x8 tiles (16x8).
        size_t base_tile = sequence_synth[i].note * 2;

        // TODO: Show the note name if is within the duration, hide when trigger
        // is off or duration of previous note was cut short. If not triggered
        // but note name appears, needs to be on a different palette bank.
        if (!sequence_synth[i].trigger) {
            seq_sprites[i].obj_attr_0 |= OBJ_HIDDEN;
        } else {
            seq_sprites[i].obj_attr_0 &= ~OBJ_HIDDEN;
        }

        seq_sprites[i].obj_attr_2 = base_tile;
    }

    // 33: Sequence indicator.
    {
        int x = SEQ_TRIG_POS_X + step_counter * SEQ_TRIG_DIST + 3;
        int y = SEQ_TRIG_POS_Y + 15;
        if (step_counter >= 8) {
            y += 32;
            x -= 8 * SEQ_TRIG_DIST;
        }
        seq_sprites[32].obj_attr_0 = OBJ_SHAPE_SQUARE | OBJ_Y_COORD(y); // TODO: Mask and update instead.
        seq_sprites[32].obj_attr_1 = OBJ_SIZE_SMALL | OBJ_X_COORD(x);   // TODO: Mask and update instead.
    }

    // 34: Trigger selection.
    {
        int x = SEQ_TRIG_POS_X - 1 + trig_selection_loc * SEQ_TRIG_DIST;
        int y = SEQ_TRIG_POS_Y - 2;
        if (trig_selection_loc >= 8) {
            y += 32;
            x -= 8 * SEQ_TRIG_DIST;
        }
        // TODO: Mask and update instead.
        seq_sprites[33].obj_attr_0 = OBJ_SHAPE_SQUARE | OBJ_Y_COORD(y);
        seq_sprites[33].obj_attr_1 = OBJ_SIZE_BIG | OBJ_X_COORD(x);
    }

    // 36: Envelope initial volume
    {
        size_t tile_diff =  sequence_synth[trig_selection_loc].env_volume * 4;
        size_t base_tile = seq_sprites[35].base_tile;
        size_t tile = base_tile + tile_diff;
        seq_sprites[35].obj_attr_2 = tile | OBJ_PAL_BANK(2);
    }

    // 38: Envelope initial volume
    {
        size_t tile_diff =  sequence_synth[trig_selection_loc].env_time * 4;
        size_t base_tile = seq_sprites[37].base_tile;
        size_t tile = base_tile + tile_diff;
        seq_sprites[37].obj_attr_2 = tile | OBJ_PAL_BANK(2);
    }

    // 40: Envelope initial volume
    {
        size_t tile_diff =  sequence_synth[trig_selection_loc].env_direction * 4;
        size_t base_tile = seq_sprites[39].base_tile;
        size_t tile = base_tile + tile_diff;
        seq_sprites[39].obj_attr_2 = tile | OBJ_PAL_BANK(2);
    }
}

void
handle_sequencer_input(void) {
    if (key_pressed(KEY_LEFT)) {
        if (trig_selection_loc == 0) {
            trig_selection_loc = 15;
        } else {
            trig_selection_loc = CLAMP(trig_selection_loc - 1, 0, 15);
        }
    }
    if (key_pressed(KEY_RIGHT)) {
        if (trig_selection_loc == 15) {
            trig_selection_loc = 0;
        } else {
            trig_selection_loc = CLAMP(trig_selection_loc + 1, 0, 15);
        }
    }
    if (key_pressed(KEY_UP) || key_pressed(KEY_DOWN)) {
        trig_selection_loc = (trig_selection_loc + 8) % 16;
    }
    if (key_pressed(KEY_B)) {
        sequence_synth[trig_selection_loc].trigger ^= 1;
    }
    if (key_pressed(KEY_L)) {
        sequence_synth[trig_selection_loc].note = CLAMP(sequence_synth[trig_selection_loc].note - 1, NOTE_C_2, NOTE_C_8);
    }
    if (key_pressed(KEY_R)) {
        sequence_synth[trig_selection_loc].note = CLAMP(sequence_synth[trig_selection_loc].note + 1, NOTE_C_2, NOTE_C_8);
    }

    if (key_pressed(KEY_START)) {
        step_counter = 0;
        set_time(bpm);
    }
    if (key_pressed(KEY_SELECT)) {
        TIMER_CTRL_0 ^= TIMER_CTRL_ENABLE;
    }
}

void
render_sequencer_sprites(void) {
    size_t len = sizeof(seq_sprites) / sizeof(SeqSprite);
    for (size_t i = 0; i < len; ++i) {
        size_t id = seq_sprites[i].id;
        OBJ_ATTR_0(id) = seq_sprites[i].obj_attr_0;
        OBJ_ATTR_1(id) = seq_sprites[i].obj_attr_1;
        OBJ_ATTR_2(id) = seq_sprites[i].obj_attr_2;
    }
}