-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmain.c
More file actions
293 lines (259 loc) · 7.81 KB
/
main.c
File metadata and controls
293 lines (259 loc) · 7.81 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
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <assert.h>
#include <string.h>
#include "USBDDOS/DPMI/dpmi.h"
#include "USBDDOS/usb.h"
#include "USBDDOS/CLASS/msc.h"
#include "USBDDOS/CLASS/hid.h"
#include "USBDDOS/dbgutil.h"
#define USBDDOS_VERSION 0x0200 //BCD
#ifndef USBDDOS_BUILD
#define USBDDOS_BUILD "Local build"
#endif
#if defined(__BC__) || defined(__WC__)
#define ENABLE_RETROWAVE 0 //code exceed 64K
#else
#define ENABLE_RETROWAVE 1
#endif
#if ENABLE_RETROWAVE
#include "RetroWav/retrowav.h"
#include "RetroWav/Platform/dos_cdc.h"
#include "RetroWav/Board/opl3.h"
#include "emm.h"
#include "hdpmipt.h"
#define OPL_PRIMARY 0
#define OPL_SECONDARY 1
RetroWaveContext MAIN_RWContext = {0};
static uint32_t MAIN_OPLTimerCtrlReg[2]; //if start 1 and 2 seperately we will miss one, so use 2 cache
static uint8_t MAIN_OPLIndexReg[2];
//primary index read
#define OPL_TIMER_REG_INDEX 4
#define OPL_TIMER1_MASK 0xC0
#define OPL_TIMER2_MASK 0xA0
#define OPL_TIMER1_START 0x01
#define OPL_TIMER2_START 0x02
#define OPL_TIMER1_TIMEOUT OPL_TIMER1_MASK
#define OPL_TIMER2_TIMEOUT OPL_TIMER2_MASK
//secondary index read (Adlib Gold). reference: AIL2.0 source code, dosbox
#define ADLG_IOBUSY 0x40UL
#define ADLG_VOLL_REG_INDEX 9
#define ADLG_VOLR_REG_INDEX 10
//data
#define KEY_ON 0x10 //channel on bit
static uint32_t ADLG_CtrlEnable = 0; //seems not working for Miles Sound, don't use it
static uint32_t ADLG_Volume[2] = {0x08,0x08};
static uint32_t MAIN_OPL_Primary_Index(uint32_t port, uint32_t reg, uint32_t out)
{
unused(port);
if(out)
MAIN_OPLIndexReg[OPL_PRIMARY] = (uint8_t)reg;
else
{ //in status reg
reg = reg & ~0xFFUL;
if ((MAIN_OPLTimerCtrlReg[0] & (OPL_TIMER1_MASK|OPL_TIMER1_START)) == OPL_TIMER1_START)
reg |= OPL_TIMER1_TIMEOUT;
if ((MAIN_OPLTimerCtrlReg[1] & (OPL_TIMER2_MASK|OPL_TIMER2_START)) == OPL_TIMER2_START)
reg |= OPL_TIMER2_TIMEOUT;
inp((uint16_t)port); //instant delay
}
return reg;
}
static uint32_t MAIN_OPL_Primary_Data(uint32_t port, uint32_t val, uint32_t out)
{
unused(port);
if(out)
{
if(MAIN_OPLIndexReg[OPL_PRIMARY] == OPL_TIMER_REG_INDEX)
{
if(val&(OPL_TIMER1_START|OPL_TIMER1_MASK))
MAIN_OPLTimerCtrlReg[0] = val;
if(val&(OPL_TIMER2_START|OPL_TIMER2_MASK))
MAIN_OPLTimerCtrlReg[1] = val;
}
retrowave_opl3_emit_port0(&MAIN_RWContext, MAIN_OPLIndexReg[OPL_PRIMARY], (uint8_t)val);
return val;
}
return MAIN_OPL_Primary_Index(port, val, out);
}
static uint32_t MAIN_OPL_Secondary_Index(uint32_t port, uint32_t reg, uint32_t out)
{
unused(port);
if(out)
{
if(reg == 0xFF)
ADLG_CtrlEnable = TRUE;
else if(reg == 0xFE)
ADLG_CtrlEnable = FALSE;
MAIN_OPLIndexReg[OPL_SECONDARY] = (uint8_t)reg;
return reg;
}
return ADLG_CtrlEnable ? ~ADLG_IOBUSY : MAIN_OPL_Primary_Index(port, reg, out);
}
static uint32_t MAIN_OPL_Secondary_Data(uint32_t port, uint32_t val, uint32_t out)
{
unused(port);
if(out)
{
if(/*ADLG_CtrlEnable && */(MAIN_OPLIndexReg[OPL_SECONDARY] == ADLG_VOLL_REG_INDEX || MAIN_OPLIndexReg[OPL_SECONDARY] == ADLG_VOLR_REG_INDEX))
ADLG_Volume[MAIN_OPLIndexReg[OPL_SECONDARY]-ADLG_VOLL_REG_INDEX] = val;
retrowave_opl3_emit_port1(&MAIN_RWContext, MAIN_OPLIndexReg[OPL_SECONDARY], (uint8_t)val);
return val;
}
//in
//if(ADLG_CtrlEnable) //adlib gold
{
if(MAIN_OPLIndexReg[OPL_SECONDARY] == ADLG_VOLL_REG_INDEX || MAIN_OPLIndexReg[OPL_SECONDARY] == ADLG_VOLR_REG_INDEX)
return ADLG_Volume[MAIN_OPLIndexReg[OPL_SECONDARY]-ADLG_VOLL_REG_INDEX];
}
return MAIN_OPL_Primary_Index(port, val, out);
}
static EMM_IODT MAIN_IODT[4] =
{
0x388, &MAIN_OPL_Primary_Index,
0x389, &MAIN_OPL_Primary_Data,
0x38A, &MAIN_OPL_Secondary_Index,
0x38B, &MAIN_OPL_Secondary_Data,
};
static EMM_IOPT MAIN_IOPT;
#if defined(__DJ2__)
static EMM_IOPT MAIN_HDPMI_IOPT;
BOOL MAIN_HDPMI_PortTrapped;
#endif
#endif
struct
{
const char* option;
const char* desc;
BOOL enable;
}MAIN_Options[] =
{
"/?", "Show help.", FALSE,
#if ENABLE_RETROWAVE
"/RW", "Enable RetroWave OPL3 supprt. EMM386 v4.46+ / HDPMI32i needed.", FALSE,
#endif
"/disk", "Enable USB-disk support", FALSE,
"/hid", "Enable USB keyboard & mouse support", FALSE,
#if DEBUG
"/test", "debug tests", FALSE,
#endif
NULL, NULL, 0,
};
enum EOption
{
OPT_Help,
#if ENABLE_RETROWAVE
OPT_RetroWave,
#endif
OPT_Disk,
OPT_HID,
#if DEBUG
OPT_TEST,
#endif
OPT_COUNT,
};
int main(int argc, char* argv[])
{
if(argc == 1 || (argc == 2 && stricmp(argv[1],"/?") == 0))
{
printf("USBDDOS: USB driver for DOS. https://github.com/crazii/USBDDOS\n");
printf("Version: %d.%02d, Build: %s. Usage:\n", USBDDOS_VERSION>>8, USBDDOS_VERSION&0xFF, USBDDOS_BUILD);
int i = 0;
while(MAIN_Options[i].option)
{
printf(" %-8s: %s\n", MAIN_Options[i].option, MAIN_Options[i].desc);
++i;
}
return 0;
}
else
{
for(int i = 1; i < argc; ++i)
{
for(int j = 0; j < OPT_COUNT; ++j)
{
if(stricmp(argv[i], MAIN_Options[j].option) == 0)
{
MAIN_Options[j].enable = TRUE;
break;
}
}
}
}
#if DEBUG && 0
if(MAIN_Options[OPT_TEST].enable)
{
extern void DPMI_InitFlat();
extern void USB_MSC_Test();
//DPMI_InitFlat();
//USB_MSC_Test();
return 0;
}
#endif
DPMI_Init();
#if ENABLE_RETROWAVE
if(MAIN_Options[OPT_RetroWave].enable)
{
unsigned short EMMVer = EMM_GetVersion();
_LOG("EMM386 version: %d.%02d\n", (EMMVer&0xFF), (EMMVer>>8));
if((EMMVer&0xFF) < 4 || (EMMVer&0xFF) == 4 && (EMMVer&0xFF00) < 46)
{
printf("EMM386 not installed or version not supported: %d.%02d\n", (EMMVer&0xFF), (EMMVer>>8));
return -1;
}
}
#endif
USB_Init();
#if ENABLE_RETROWAVE
if(MAIN_Options[OPT_RetroWave].enable)
{
if(retrowave_init_dos_cdc(&MAIN_RWContext) != 0)
return -1;
retrowave_io_init(&MAIN_RWContext);
retrowave_opl3_reset(&MAIN_RWContext);
if(!EMM_Install_IOPortTrap(0x388, 0x38B, MAIN_IODT, sizeof(MAIN_IODT)/sizeof(EMM_IODT), &MAIN_IOPT))
{
puts("IO trap installation failed.\n");
return 1;
}
#if defined(__DJ2__)
if(!(MAIN_HDPMI_PortTrapped=HDPMIPT_Install_IOPortTrap(0x388, 0x38B, MAIN_IODT, sizeof(MAIN_IODT)/sizeof(EMM_IODT), &MAIN_HDPMI_IOPT)))
puts("Protected mode IO trap installation failed, make sure an HDPMI that supporting port trap is used.\n");
#endif
}
BOOL TSR = MAIN_Options[OPT_RetroWave].enable;
#else
BOOL TSR = FALSE;
#endif
TSR = (MAIN_Options[OPT_Disk].enable && USB_MSC_DOS_Install()) || TSR; //note: TSR must be put in the back
TSR = (MAIN_Options[OPT_HID].enable && USB_HID_DOS_Install()) || TSR;
#if DEBUG && 0
TSR = MAIN_Options[OPT_TEST].enable || TSR;
#endif
if(TSR)
{
if(!DPMI_TSR())
puts("TSR Installation failed.\n");
}
else
puts("No USB device found, exit.\n");
//TSR failure
#if ENABLE_RETROWAVE
if(MAIN_Options[OPT_RetroWave].enable)
{
BOOL uninstalled;
uninstalled = EMM_Uninstall_IOPortTrap(&MAIN_IOPT);
assert(uninstalled);
#if defined(__DJ2__)
if(MAIN_HDPMI_PortTrapped)
{
uninstalled = HDPMIPT_Uninstall_IOPortTrap(&MAIN_IOPT);
assert(uninstalled);
}
#endif
unused(uninstalled);
}
#endif
return DPMI_Exit(0);
}