-
Notifications
You must be signed in to change notification settings - Fork 1
/
vgmduino.ino
91 lines (79 loc) · 1.71 KB
/
vgmduino.ino
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
// https://github.com/zenmai/portableMDXPlayer/blob/master/YM2151.cpp
#include "YM2151.h"
#include <avr/pgmspace.h>
#include "music/vgmsample.h"
// https://en.wikipedia.org/wiki/VGM_(file_format)
unsigned int vgmpos = 0xb0;
bool vgmend = false;
unsigned long startTime;
unsigned long duration;
uint8_t getByte() {
uint8_t ret = pgm_read_byte_near(vgmdata + vgmpos);
vgmpos++;
return ret;
}
unsigned int read16() {
return getByte() + (getByte() << 8);
}
void pause(long samples){
duration = ((1000.0 / (44100.0 / (float)samples)) * 1000);
startTime = micros();
}
void vgmplay() {
if((micros() - startTime) <= duration) {
return;
}
byte command = getByte();
uint8_t reg;
uint8_t dat;
switch (command) {
case 0x54:
// YM2151
reg = getByte();
dat = getByte();
YM2151.write(reg, dat);
break;
case 0x61:
pause(read16());
break;
case 0x62:
pause(735);
break;
case 0x63:
pause(882);
break;
case 0x66:
vgmend = true;
break;
case 0x70:
case 0x71:
case 0x72:
case 0x73:
case 0x74:
case 0x75:
case 0x76:
case 0x77:
case 0x78:
case 0x79:
case 0x7A:
case 0x7B:
case 0x7C:
case 0x7D:
case 0x7E:
case 0x7F:
pause((command & 0x0f) + 1);
break;
default:
break;
}
}
void setup() {
YM2151.begin();
delay(400);
}
void loop() {
while(!vgmend) {
vgmplay();
}
asm volatile("nop\n\t nop\n\t nop\n\t nop\n\t");
}