This repository has been archived by the owner on Mar 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imf.c
171 lines (145 loc) · 3.76 KB
/
imf.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
#include "imf.h"
#include <assert.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
// See: https://moddingwiki.shikadi.net/wiki/IMF_Format
uint8_t imf_guess_type(
const void* data,
size_t size
)
{
const uint8_t* ptr = (const uint8_t *)data;
uint16_t word;
uint16_t i = 42;
uint32_t sum1 = 0;
uint32_t sum2 = 0;
assert(data);
if (size < 2) {
return 0;
}
word = (ptr[0] | ((uint16_t)ptr[1] << 8));
ptr += 2;
if (!word || (word & 3)) {
return 0;
}
while ((size >= 4) && i--)
{
word = (ptr[0] | ((uint16_t)ptr[1] << 8));
ptr += 2;
sum1 += word;
word = (ptr[0] | ((uint16_t)ptr[1] << 8));
ptr += 2;
sum2 += word;
}
return (uint8_t)(sum1 > sum2);
}
void imf_init(
struct imf_status* status,
uint32_t imf_rate,
uint32_t opl_rate
)
{
uint32_t division;
assert(status);
assert(opl_rate);
assert(imf_rate);
division = (opl_rate / imf_rate); // TODO: improve resolution via fixed point 24.8
assert(division);
status->events = NULL;
status->imf_rate = imf_rate;
status->opl_rate = opl_rate;
status->division = division;
status->divider = 0;
status->length = 0;
status->index = 0;
status->delay = 0;
status->type = 0;
status->address_hi = 0;
}
void imf_load(
struct imf_status* status,
const void* data,
size_t size,
uint8_t type
)
{
uint8_t* ptr = (uint8_t*)data;
uint32_t length_by_header;
uint32_t length_by_size;
assert(status);
status->events = NULL;
status->length = 0;
status->type = 0;
imf_restart(status);
if (type) {
length_by_header = (ptr[0] | ((uint16_t)ptr[1] << 8));
length_by_header /= sizeof(struct imf_event);
status->length = length_by_header;
status->events = (const struct imf_event*)(const void*)&ptr[2];
length_by_size = (uint32_t)(size - 2);
length_by_size /= sizeof(struct imf_event);
if (status->length > length_by_size) {
status->length = length_by_size;
}
}
else {
length_by_size = (uint32_t)size;
length_by_size /= sizeof(struct imf_event);
status->length = length_by_size;
status->events = (const struct imf_event*)(const void*)&ptr[0];
}
}
void imf_restart(
struct imf_status* status
)
{
assert(status);
status->divider = 0;
status->index = 0;
status->delay = 0;
status->address_hi = 0;
}
const struct imf_cmd imf_opl_tick(
struct imf_status* status
)
{
const struct imf_event* event = NULL;
struct imf_cmd cmd = { 0, 0, 1 };
uint16_t delay;
assert(status);
assert(status->events);
if (status->divider) {
status->divider--;
}
if (!status->divider) {
status->divider = status->division;
if (status->delay) {
status->delay--;
}
if (!status->delay) {
if (status->index < status->length) {
event = &status->events[status->index++];
delay = (((uint16_t)event->delay_hi << 8) | event->delay_lo);
status->delay = delay;
cmd.delaying = (uint8_t)(delay > 0);
// Override virtual register 0x05 to extend the address range for OPL3
if (event->address_lo == 0x05) {
status->address_hi = (event->value & 0x01);
}
else {
cmd.address = ((uint16_t)(status->address_hi << 8) | event->address_lo);
cmd.value = event->value;
}
}
else {
cmd.delaying = 2; // EOF
}
}
}
return cmd;
}
#ifdef __cplusplus
} // extern "C"
#endif