-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathesfmout.cpp
More file actions
364 lines (318 loc) · 9.99 KB
/
Copy pathesfmout.cpp
File metadata and controls
364 lines (318 loc) · 9.99 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
#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
#include "dpmi.h"
#include "lowlevel.h" // for TSS exploit stuff
//#define VSYNC_HACK
uint32_t esfm_base;
static uint32_t esfm_volume_mixer;
static uint32_t esfm_volume = 15;
static bool esfm_was_native = false;
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() {
if (esfm_was_native) {
// back to native mode
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};
} else {
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;
// first try to read compatibility register
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 & ~1) == 0x80) { // mask out bit 0
// it's a ESFM!
esfm_was_native = true;
rtn = true;
// return to OPL3-compatible mode
outp(oplbase, 0);
// no need for further detection
return rtn;
}
// 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);
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 (mainprops.disable_volume_ctrl) return;
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 (mainprops.disable_volume_ctrl) return 15;
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
}
// ----------------------
// evil TSS hack to gain access to ESFM ports under Win9x WDM drivers :meatjob:
static uint8_t iopm_saved[2]; // saved IO permission map for 16 consecutive ports
struct tss_info_t {
uint8_t* base;
uint32_t limit; // limit+1
uint32_t ioperm_ofs; // offset of I/O permission map
uint32_t ioperm_size;
};
static int get_tss(tss_info_t * tss) {
if (tss == NULL) return 1;
descriptorTableAddr gdtPtr;
sgdt(&gdtPtr);
_dpmi_descriptor* gdt = (_dpmi_descriptor*)gdtPtr.base;
_dpmi_descriptor* cdt = gdt; // current descriptor table
uint32_t cdt_limit = gdtPtr.limit;
uint32_t tssSelector = str();
if (tssSelector != 0) {
// found it
if (tssSelector & 4) {
// TSS located in LDT
uint32_t ld = sldt(); _dpmi_descriptor* ldt = (_dpmi_descriptor*)(FP_OFF(gdt) + (ld & ~7));
void* ldtAddr = (void*)((ldt->base_low) | (ldt->base_mid << 16) | (ldt->base_high << 24));
cdt = (_dpmi_descriptor*)ldtAddr;
cdt_limit = ((ldt->limit_low) | (ldt->limit_high << 16)); // TODO: limit granularity check?
}
// check if TSS is within limits (technically this should never happen as CPU will fault if invalid TSS is loaded)
if (tssSelector > cdt_limit) return 1;
// find TSS in current descriptor table
_dpmi_descriptor *tssDescriptor = cdt + (tssSelector >> 3);
tss->base = (uint8_t*)((tssDescriptor->base_low) | (tssDescriptor->base_mid << 16) | (tssDescriptor->base_high << 24));
tss->limit = ((tssDescriptor->limit_low) | (tssDescriptor->limit_high << 16)); // TODO: limit granularity check?
tss->ioperm_ofs = *(uint16_t*)(tss->base + 0x66);
if (tss->ioperm_ofs > tss->limit) return 1; // ioperm map is invalid
tss->ioperm_size = (tss->limit+1) - tss->ioperm_ofs;
return 0;
} else return 1;
}
int flip_iopm(uint32_t base, uint32_t num_ports, bool unlock) {
// first check if we're on ring0 already, then we don't need to mess with this stuff at all
if ((CS() & 3) == 0) return 0;
// if num_ports higher than 16, fail (not enough room in static storage for saved IOPM)
if (num_ports > 16) return 1;
// get TSS and check if there's enough room for registers to unlock
tss_info_t tssinfo;
if ((get_tss(&tssinfo)) || (tssinfo.base == NULL)) return 1; // oops no TSS
// adjust base port and ports num
base >>= 3; num_ports >>= 3;
if ((base + num_ports - 1) >= tssinfo.ioperm_size) return 1; // not covered by TSS, can't patch
if (unlock) {
for (int i = 0; i < num_ports; i++) {
iopm_saved[i] = tssinfo.base[tssinfo.ioperm_ofs + base + i];
tssinfo.base[tssinfo.ioperm_ofs + base + i] = 0; // all ports unlocked!
}
} else {
for (int i = 0; i < num_ports; i++) {
tssinfo.base[tssinfo.ioperm_ofs + base + i] = iopm_saved[i];
}
}
// reload TSS by triggering ring0 switch
_asm {
mov eax, 0x0400
int 0x31 // get DPMI version, discard results
}
return 0;
}
// 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
// unlock ports in IOPM if requested
if (mainprops.tss_unlock) flip_iopm(iobase, 16, true);
// activate ESFM
if (mainprops.activate_mixer) 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;
}