-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathesfmout.cpp
More file actions
245 lines (220 loc) · 5.8 KB
/
esfmout.cpp
File metadata and controls
245 lines (220 loc) · 5.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
#include <i86.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <dos.h>
#include "esfmout.h"
#include "tinypci.h"
#include "main.h"
#include <flexptc.h> // for vsync hack
//#define VSYNC_HACK
uint32_t esfm_base;
static uint32_t esfm_volume_mixer;
static uint32_t esfm_volume = 15;
void esfm_delay(int timeout) {
do { _asm {in al, 0xE1} } while (--timeout);
}
void esfm_set_baseport(uint32_t base) {
esfm_base = base;
}
#if 1
#if 0
void esfm_out(int reg, int data) {
_asm {
mov edx, [esfm_base]
mov eax, [reg]
mov ecx, [data]
add edx, 2
out dx, al
in al, 0xE1
in al, 0xE1
mov al, ah
inc edx
out dx, al
in al, 0xE1
in al, 0xE1
mov al, cl
sub edx, 2
out dx, al
mov edx, 0x3DA
in al, dx
and al, 8
or [ptc_force_vblank], al
}
}
#endif
void esfm_out_fast(int reg, int data) {
_asm {
mov edx, [esfm_base]
mov eax, [reg]
mov ecx, [data]
add edx, 2
out dx, al
in al, 0xE1
mov al, ah
inc edx
out dx, al
in al, 0xE1
mov al, cl
sub edx, 2
out dx, al
mov edx, 0x3DA
in al, dx
and al, 8
or [ptc_force_vblank], al
}
}
#else
void esfm_out(int reg, int data) {
outp(esfm_base + 2, (reg & 0xFF));
_asm {in al, 0xE1};
_asm {in al, 0xE1};
outp(esfm_base + 3, (reg >> 8));
_asm {in al, 0xE1};
_asm {in al, 0xE1};
outp(esfm_base + 1, data);
#ifdef VSYNC_HACK
if (inp(0x3da) & 8) ptc_force_vblank = true;
#else
_asm {in al, 0xE1};
_asm {in al, 0xE1};
#endif
}
#endif
void esfm_out_slow(int reg, int data) {
outp(esfm_base + 2, (reg & 0xFF));
esfm_delay(4);
outp(esfm_base + 3, (reg >> 8));
esfm_delay(4);
outp(esfm_base + 1, data);
esfm_delay(4);
}
void esfm_enable() {
outp(esfm_base + 2, 0x05);
_asm {in al, 0xE1};
_asm {in al, 0xE1};
outp(esfm_base + 3, 0x81);
_asm {in al, 0xE1};
_asm {in al, 0xE1};
}
void esfm_disable() {
outp(esfm_base + 0, 0);
_asm {in al, 0xE1};
_asm {in al, 0xE1};
// restore mixer volume
esfm_set_volume(esfm_volume_mixer);
}
void esfm_reset() {
// TODO: confirm
for (int i = 0; i < 0x7FF; i++) {
esfm_out_slow(i, 0);
}
}
bool esfm_try_detect(uint32_t oplbase) {
bool rtn = false;
// enable OPL3 mode
outp(oplbase + 2, 0x05);
esfm_delay(128);
outp(oplbase + 3, 0x01);
esfm_delay(1024);
// try to enable ESFM native mode
outp(oplbase + 2, 0x05);
esfm_delay(128);
outp(oplbase + 3, 0x80);
esfm_delay(1024);
// try to read compatibility register now
outp(oplbase + 2, 0x05);
esfm_delay(128);
outp(oplbase + 3, 0x05); // select ESFM reg 0x505 (or OPL3 bank 1 reg 0x05)
esfm_delay(1024);
int ii = inp(oplbase + 1);
if (ii == 0x80) {
// it's a ESFM!
rtn = true;
// return to OPL3-compatible mode
outp(oplbase, 0);
}
return rtn;
}
void esfm_set_volume(uint32_t volume) {
if (esfm_base != 0x388) {
volume &= 15;
outp(esfm_base + 0x04, 54); esfm_delay(1);
outp(esfm_base + 0x05, (volume << 4) | (volume)); esfm_delay(1);
}
esfm_volume = volume;
}
uint32_t esfm_get_volume() {
if (esfm_base != 0x388) {
outp(esfm_base + 0x04, 54); esfm_delay(1);
esfm_volume = inp(esfm_base + 0x05) & 0x0F; esfm_delay(1);
}
return esfm_volume;
}
static void esfm_activate(uint32_t iobase) {
// activate ESFM
// https://github.com/pachuco/ESSPlayMid/blob/master/src/esfmmidi.c#L81
outp(iobase + 0x04, 72); esfm_delay(1); // serial mode control
outp(iobase + 0x05, 0); esfm_delay(1);
outp(iobase + 0x04, 127); esfm_delay(1); // music digital record
outp(iobase + 0x05, 0); esfm_delay(1);
outp(iobase + 0x04, 107); esfm_delay(1); // music DAC record volume
outp(iobase + 0x05, 0); esfm_delay(1);
outp(iobase + 0x07, 66); esfm_delay(1); // power management register
}
// searches for ESFM, returns base address
uint32_t esfm_detect() {
uint32_t iobase = 0;
char envstr[128];
// query BLASTER variable
char* blasterEnv = getenv("BLASTER");
if (blasterEnv != NULL) {
// copy variable to temporary buffer
strncpy(envstr, blasterEnv, sizeof(envstr));
// tokenize
char* p = strtok(envstr, " ");
while (p != NULL) {
switch (toupper(*p)) {
case 'A': iobase = strtol(p + 1, NULL, 16); break;
default: break;
}
p = strtok(NULL, " ");
}
} else {
// scan for ESS Solo-1
pciDeviceList pcilist;
if (tinypci::enumerateByDeviceId(&pcilist, 1, 0x125d, 0x1969) != 0) {
// extract BAR2
iobase = tinypci::configReadDword(pcilist.address, 0x14) & ~0xF;
} else {
// try default 0x220 :) (danger zone!)
iobase = 0x220;
}
}
#ifdef DEBUG
printf("iobase = %X\n", iobase);
#endif
// activate ESFM
esfm_activate(iobase);
// try detect here
if (esfm_try_detect(iobase)) {
// found!
esfm_base = iobase;
// unmute if muted
// TODO: detect dosbox-x somehow (it's too quiet now!)
esfm_volume_mixer = esfm_get_volume();
esfm_set_volume(15);
return iobase;
}
// if failed - try default Adlib base 0x388
if (esfm_try_detect(0x388)) {
esfm_base = 0x388;
esfm_volume = 15;
return 0x388;
}
// else return 0
return 0;
}