This repository has been archived by the owner on Sep 4, 2019. It is now read-only.
/
iohandler.cpp
514 lines (460 loc) · 13.5 KB
/
iohandler.cpp
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
* Copyright (C) 2002-2010 The DOSBox Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/* $Id: iohandler.cpp,v 1.30 2009-05-27 09:15:41 qbix79 Exp $ */
#include <string.h>
#include "dosbox.h"
#include "inout.h"
#include "setup.h"
#include "cpu.h"
#include "../src/cpu/lazyflags.h"
#include "callback.h"
//#define ENABLE_PORTLOG
IO_WriteHandler * io_writehandlers[3][IO_MAX];
IO_ReadHandler * io_readhandlers[3][IO_MAX];
static Bitu IO_ReadBlocked(Bitu /*port*/,Bitu /*iolen*/) {
return ~0;
}
static void IO_WriteBlocked(Bitu /*port*/,Bitu /*val*/,Bitu /*iolen*/) {
}
static Bitu IO_ReadDefault(Bitu port,Bitu iolen) {
switch (iolen) {
case 1:
LOG(LOG_IO,LOG_WARN)("Read from port %04X",port);
io_readhandlers[0][port]=IO_ReadBlocked;
return 0xff;
case 2:
return
(io_readhandlers[0][port+0](port+0,1) << 0) |
(io_readhandlers[0][port+1](port+1,1) << 8);
case 4:
return
(io_readhandlers[1][port+0](port+0,2) << 0) |
(io_readhandlers[1][port+2](port+2,2) << 16);
}
return 0;
}
void IO_WriteDefault(Bitu port,Bitu val,Bitu iolen) {
switch (iolen) {
case 1:
LOG(LOG_IO,LOG_WARN)("Writing %02X to port %04X",val,port);
io_writehandlers[0][port]=IO_WriteBlocked;
break;
case 2:
io_writehandlers[0][port+0](port+0,(val >> 0) & 0xff,1);
io_writehandlers[0][port+1](port+1,(val >> 8) & 0xff,1);
break;
case 4:
io_writehandlers[1][port+0](port+0,(val >> 0 ) & 0xffff,2);
io_writehandlers[1][port+2](port+2,(val >> 16) & 0xffff,2);
break;
}
}
void IO_RegisterReadHandler(Bitu port,IO_ReadHandler * handler,Bitu mask,Bitu range) {
while (range--) {
if (mask&IO_MB) io_readhandlers[0][port]=handler;
if (mask&IO_MW) io_readhandlers[1][port]=handler;
if (mask&IO_MD) io_readhandlers[2][port]=handler;
port++;
}
}
void IO_RegisterWriteHandler(Bitu port,IO_WriteHandler * handler,Bitu mask,Bitu range) {
while (range--) {
if (mask&IO_MB) io_writehandlers[0][port]=handler;
if (mask&IO_MW) io_writehandlers[1][port]=handler;
if (mask&IO_MD) io_writehandlers[2][port]=handler;
port++;
}
}
void IO_FreeReadHandler(Bitu port,Bitu mask,Bitu range) {
while (range--) {
if (mask&IO_MB) io_readhandlers[0][port]=IO_ReadDefault;
if (mask&IO_MW) io_readhandlers[1][port]=IO_ReadDefault;
if (mask&IO_MD) io_readhandlers[2][port]=IO_ReadDefault;
port++;
}
}
void IO_FreeWriteHandler(Bitu port,Bitu mask,Bitu range) {
while (range--) {
if (mask&IO_MB) io_writehandlers[0][port]=IO_WriteDefault;
if (mask&IO_MW) io_writehandlers[1][port]=IO_WriteDefault;
if (mask&IO_MD) io_writehandlers[2][port]=IO_WriteDefault;
port++;
}
}
void IO_ReadHandleObject::Install(Bitu port,IO_ReadHandler * handler,Bitu mask,Bitu range) {
if(!installed) {
installed=true;
m_port=port;
m_mask=mask;
m_range=range;
IO_RegisterReadHandler(port,handler,mask,range);
} else E_Exit("IO_readHandler allready installed port %x",port);
}
IO_ReadHandleObject::~IO_ReadHandleObject(){
if(!installed) return;
IO_FreeReadHandler(m_port,m_mask,m_range);
}
void IO_WriteHandleObject::Install(Bitu port,IO_WriteHandler * handler,Bitu mask,Bitu range) {
if(!installed) {
installed=true;
m_port=port;
m_mask=mask;
m_range=range;
IO_RegisterWriteHandler(port,handler,mask,range);
} else E_Exit("IO_writeHandler allready installed port %x",port);
}
IO_WriteHandleObject::~IO_WriteHandleObject(){
if(!installed) return;
IO_FreeWriteHandler(m_port,m_mask,m_range);
//LOG_MSG("FreeWritehandler called with port %X",m_port);
}
struct IOF_Entry {
Bitu cs;
Bitu eip;
};
#define IOF_QUEUESIZE 16
static struct {
Bitu used;
IOF_Entry entries[IOF_QUEUESIZE];
} iof_queue;
static Bits IOFaultCore(void) {
CPU_CycleLeft+=CPU_Cycles;
CPU_Cycles=1;
Bits ret=CPU_Core_Full_Run();
CPU_CycleLeft+=CPU_Cycles;
if (ret<0) E_Exit("Got a dosbox close machine in IO-fault core?");
if (ret)
return ret;
if (!iof_queue.used) E_Exit("IO-faul Core without IO-faul");
IOF_Entry * entry=&iof_queue.entries[iof_queue.used-1];
if (entry->cs == SegValue(cs) && entry->eip==reg_eip)
return -1;
return 0;
}
/* Some code to make io operations take some virtual time. Helps certain
* games with their timing of certain operations
*/
#define IODELAY_READ_MICROS 1.0
#define IODELAY_WRITE_MICROS 0.75
inline void IO_USEC_read_delay_old() {
if(CPU_CycleMax > static_cast<Bit32s>((IODELAY_READ_MICROS*1000.0))) {
// this could be calculated whenever CPU_CycleMax changes
Bits delaycyc = static_cast<Bits>((CPU_CycleMax/1000)*IODELAY_READ_MICROS);
if(CPU_Cycles > delaycyc) CPU_Cycles -= delaycyc;
else CPU_Cycles = 0;
}
}
inline void IO_USEC_write_delay_old() {
if(CPU_CycleMax > static_cast<Bit32s>((IODELAY_WRITE_MICROS*1000.0))) {
// this could be calculated whenever CPU_CycleMax changes
Bits delaycyc = static_cast<Bits>((CPU_CycleMax/1000)*IODELAY_WRITE_MICROS);
if(CPU_Cycles > delaycyc) CPU_Cycles -= delaycyc;
else CPU_Cycles = 0;
}
}
#define IODELAY_READ_MICROSk (Bit32u)(1024/1.0)
#define IODELAY_WRITE_MICROSk (Bit32u)(1024/0.75)
inline void IO_USEC_read_delay() {
Bits delaycyc = CPU_CycleMax/IODELAY_READ_MICROSk;
if(GCC_UNLIKELY(CPU_Cycles < 3*delaycyc)) delaycyc = 0; //Else port acces will set cycles to 0. which might trigger problem with games which read 16 bit values
CPU_Cycles -= delaycyc;
CPU_IODelayRemoved += delaycyc;
}
inline void IO_USEC_write_delay() {
Bits delaycyc = CPU_CycleMax/IODELAY_WRITE_MICROSk;
if(GCC_UNLIKELY(CPU_Cycles < 3*delaycyc)) delaycyc=0;
CPU_Cycles -= delaycyc;
CPU_IODelayRemoved += delaycyc;
}
#ifdef ENABLE_PORTLOG
static Bit8u crtc_index = 0;
const char* const len_type[] = {" 8","16","32"};
void log_io(Bitu width, bool write, Bitu port, Bitu val) {
switch(width) {
case 0:
val&=0xff;
break;
case 1:
val&=0xffff;
break;
}
if (write) {
// skip the video cursor position spam
if (port==0x3d4) {
if (width==0) crtc_index = (Bit8u)val;
else if(width==1) crtc_index = (Bit8u)(val>>8);
}
if (crtc_index==0xe || crtc_index==0xf) {
if((width==0 && (port==0x3d4 || port==0x3d5))||(width==1 && port==0x3d4))
return;
}
switch(port) {
//case 0x020: // interrupt command
//case 0x040: // timer 0
//case 0x042: // timer 2
//case 0x043: // timer control
//case 0x061: // speaker control
case 0x3c8: // VGA palette
case 0x3c9: // VGA palette
// case 0x3d4: // VGA crtc
// case 0x3d5: // VGA crtc
// case 0x3c4: // VGA seq
// case 0x3c5: // VGA seq
break;
default:
LOG_MSG("iow%s % 4x % 4x, cs:ip %04x:%04x", len_type[width],
port, val, SegValue(cs),reg_eip);
break;
}
} else {
switch(port) {
//case 0x021: // interrupt status
//case 0x040: // timer 0
//case 0x042: // timer 2
//case 0x061: // speaker control
case 0x201: // joystick status
case 0x3c9: // VGA palette
// case 0x3d4: // VGA crtc index
// case 0x3d5: // VGA crtc
case 0x3da: // display status - a real spammer
// don't log for the above cases
break;
default:
LOG_MSG("ior%s % 4x % 4x,\t\tcs:ip %04x:%04x", len_type[width],
port, val, SegValue(cs),reg_eip);
break;
}
}
}
#else
#define log_io(W, X, Y, Z)
#endif
void IO_WriteB(Bitu port,Bitu val) {
log_io(0, true, port, val);
if (GCC_UNLIKELY(GETFLAG(VM) && (CPU_IO_Exception(port,1)))) {
LazyFlags old_lflags;
memcpy(&old_lflags,&lflags,sizeof(LazyFlags));
CPU_Decoder * old_cpudecoder;
old_cpudecoder=cpudecoder;
cpudecoder=&IOFaultCore;
IOF_Entry * entry=&iof_queue.entries[iof_queue.used++];
entry->cs=SegValue(cs);
entry->eip=reg_eip;
CPU_Push16(SegValue(cs));
CPU_Push16(reg_ip);
Bit8u old_al = reg_al;
Bit16u old_dx = reg_dx;
reg_al = val;
reg_dx = port;
RealPt icb = CALLBACK_RealPointer(call_priv_io);
SegSet16(cs,RealSeg(icb));
reg_eip = RealOff(icb)+0x08;
CPU_Exception(cpu.exception.which,cpu.exception.error);
DOSBOX_RunMachine();
iof_queue.used--;
reg_al = old_al;
reg_dx = old_dx;
memcpy(&lflags,&old_lflags,sizeof(LazyFlags));
cpudecoder=old_cpudecoder;
}
else {
IO_USEC_write_delay();
io_writehandlers[0][port](port,val,1);
}
}
void IO_WriteW(Bitu port,Bitu val) {
log_io(1, true, port, val);
if (GCC_UNLIKELY(GETFLAG(VM) && (CPU_IO_Exception(port,2)))) {
LazyFlags old_lflags;
memcpy(&old_lflags,&lflags,sizeof(LazyFlags));
CPU_Decoder * old_cpudecoder;
old_cpudecoder=cpudecoder;
cpudecoder=&IOFaultCore;
IOF_Entry * entry=&iof_queue.entries[iof_queue.used++];
entry->cs=SegValue(cs);
entry->eip=reg_eip;
CPU_Push16(SegValue(cs));
CPU_Push16(reg_ip);
Bit16u old_ax = reg_ax;
Bit16u old_dx = reg_dx;
reg_al = val;
reg_dx = port;
RealPt icb = CALLBACK_RealPointer(call_priv_io);
SegSet16(cs,RealSeg(icb));
reg_eip = RealOff(icb)+0x0a;
CPU_Exception(cpu.exception.which,cpu.exception.error);
DOSBOX_RunMachine();
iof_queue.used--;
reg_ax = old_ax;
reg_dx = old_dx;
memcpy(&lflags,&old_lflags,sizeof(LazyFlags));
cpudecoder=old_cpudecoder;
}
else {
IO_USEC_write_delay();
io_writehandlers[1][port](port,val,2);
}
}
void IO_WriteD(Bitu port,Bitu val) {
log_io(2, true, port, val);
if (GCC_UNLIKELY(GETFLAG(VM) && (CPU_IO_Exception(port,4)))) {
LazyFlags old_lflags;
memcpy(&old_lflags,&lflags,sizeof(LazyFlags));
CPU_Decoder * old_cpudecoder;
old_cpudecoder=cpudecoder;
cpudecoder=&IOFaultCore;
IOF_Entry * entry=&iof_queue.entries[iof_queue.used++];
entry->cs=SegValue(cs);
entry->eip=reg_eip;
CPU_Push16(SegValue(cs));
CPU_Push16(reg_ip);
Bit32u old_eax = reg_eax;
Bit16u old_dx = reg_dx;
reg_al = val;
reg_dx = port;
RealPt icb = CALLBACK_RealPointer(call_priv_io);
SegSet16(cs,RealSeg(icb));
reg_eip = RealOff(icb)+0x0c;
CPU_Exception(cpu.exception.which,cpu.exception.error);
DOSBOX_RunMachine();
iof_queue.used--;
reg_eax = old_eax;
reg_dx = old_dx;
memcpy(&lflags,&old_lflags,sizeof(LazyFlags));
cpudecoder=old_cpudecoder;
}
else io_writehandlers[2][port](port,val,4);
}
Bitu IO_ReadB(Bitu port) {
Bitu retval;
if (GCC_UNLIKELY(GETFLAG(VM) && (CPU_IO_Exception(port,1)))) {
LazyFlags old_lflags;
memcpy(&old_lflags,&lflags,sizeof(LazyFlags));
CPU_Decoder * old_cpudecoder;
old_cpudecoder=cpudecoder;
cpudecoder=&IOFaultCore;
IOF_Entry * entry=&iof_queue.entries[iof_queue.used++];
entry->cs=SegValue(cs);
entry->eip=reg_eip;
CPU_Push16(SegValue(cs));
CPU_Push16(reg_ip);
Bit16u old_dx = reg_dx;
reg_dx = port;
RealPt icb = CALLBACK_RealPointer(call_priv_io);
SegSet16(cs,RealSeg(icb));
reg_eip = RealOff(icb)+0x00;
CPU_Exception(cpu.exception.which,cpu.exception.error);
DOSBOX_RunMachine();
iof_queue.used--;
retval = reg_al;
reg_dx = old_dx;
memcpy(&lflags,&old_lflags,sizeof(LazyFlags));
cpudecoder=old_cpudecoder;
return retval;
}
else {
IO_USEC_read_delay();
retval = io_readhandlers[0][port](port,1);
}
log_io(0, false, port, retval);
return retval;
}
Bitu IO_ReadW(Bitu port) {
Bitu retval;
if (GCC_UNLIKELY(GETFLAG(VM) && (CPU_IO_Exception(port,2)))) {
LazyFlags old_lflags;
memcpy(&old_lflags,&lflags,sizeof(LazyFlags));
CPU_Decoder * old_cpudecoder;
old_cpudecoder=cpudecoder;
cpudecoder=&IOFaultCore;
IOF_Entry * entry=&iof_queue.entries[iof_queue.used++];
entry->cs=SegValue(cs);
entry->eip=reg_eip;
CPU_Push16(SegValue(cs));
CPU_Push16(reg_ip);
Bit16u old_dx = reg_dx;
reg_dx = port;
RealPt icb = CALLBACK_RealPointer(call_priv_io);
SegSet16(cs,RealSeg(icb));
reg_eip = RealOff(icb)+0x02;
CPU_Exception(cpu.exception.which,cpu.exception.error);
DOSBOX_RunMachine();
iof_queue.used--;
retval = reg_ax;
reg_dx = old_dx;
memcpy(&lflags,&old_lflags,sizeof(LazyFlags));
cpudecoder=old_cpudecoder;
}
else {
IO_USEC_read_delay();
retval = io_readhandlers[1][port](port,2);
}
log_io(1, false, port, retval);
return retval;
}
Bitu IO_ReadD(Bitu port) {
Bitu retval;
if (GCC_UNLIKELY(GETFLAG(VM) && (CPU_IO_Exception(port,4)))) {
LazyFlags old_lflags;
memcpy(&old_lflags,&lflags,sizeof(LazyFlags));
CPU_Decoder * old_cpudecoder;
old_cpudecoder=cpudecoder;
cpudecoder=&IOFaultCore;
IOF_Entry * entry=&iof_queue.entries[iof_queue.used++];
entry->cs=SegValue(cs);
entry->eip=reg_eip;
CPU_Push16(SegValue(cs));
CPU_Push16(reg_ip);
Bit16u old_dx = reg_dx;
reg_dx = port;
RealPt icb = CALLBACK_RealPointer(call_priv_io);
SegSet16(cs,RealSeg(icb));
reg_eip = RealOff(icb)+0x04;
CPU_Exception(cpu.exception.which,cpu.exception.error);
DOSBOX_RunMachine();
iof_queue.used--;
retval = reg_eax;
reg_dx = old_dx;
memcpy(&lflags,&old_lflags,sizeof(LazyFlags));
cpudecoder=old_cpudecoder;
} else {
retval = io_readhandlers[2][port](port,4);
}
log_io(2, false, port, retval);
return retval;
}
class IO :public Module_base {
public:
IO(Section* configuration):Module_base(configuration){
iof_queue.used=0;
IO_FreeReadHandler(0,IO_MA,IO_MAX);
IO_FreeWriteHandler(0,IO_MA,IO_MAX);
}
~IO()
{
//Same as the constructor ?
}
};
static IO* test;
void IO_Destroy(Section*) {
delete test;
}
void IO_Init(Section * sect) {
test = new IO(sect);
sect->AddDestroyFunction(&IO_Destroy);
}