Debian Codegraph @

src/hardware/iohandler.cpp

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 * Copyright (C) 2002-2010 The DOSBox Team
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 /* $Id: iohandler.cpp,v 1.30 2009-05-27 09:15:41 qbix79 Exp $ */
20
21 #include <string.h>
22 #include "dosbox.h"
23 #include "inout.h"
24 #include "setup.h"
25 #include "cpu.h"
26 #include "../src/cpu/lazyflags.h"
27 #include "callback.h"
28
29 //#define ENABLE_PORTLOG
30
31 IO_WriteHandler * io_writehandlers[3][IO_MAX];
32 IO_ReadHandler * io_readhandlers[3][IO_MAX];
33
34 static Bitu IO_ReadBlocked(Bitu /*port*/,Bitu /*iolen*/) {
35 return ~0;
36 }
37
38 static void IO_WriteBlocked(Bitu /*port*/,Bitu /*val*/,Bitu /*iolen*/) {
39 }
40
41 static Bitu IO_ReadDefault(Bitu port,Bitu iolen) {
42 switch (iolen) {
43 case 1:
44 LOG(LOG_IO,LOG_WARN)("Read from port %04X",port);
45 io_readhandlers[0][port]=IO_ReadBlocked;
46 return 0xff;
47 case 2:
48 return
49 (io_readhandlers[0][port+0](port+0,1) << 0) |
50 (io_readhandlers[0][port+1](port+1,1) << 8);
51 case 4:
52 return
53 (io_readhandlers[1][port+0](port+0,2) << 0) |
54 (io_readhandlers[1][port+2](port+2,2) << 16);
55 }
56 return 0;
57 }
58
59 void IO_WriteDefault(Bitu port,Bitu val,Bitu iolen) {
60 switch (iolen) {
61 case 1:
62 LOG(LOG_IO,LOG_WARN)("Writing %02X to port %04X",val,port);
63 io_writehandlers[0][port]=IO_WriteBlocked;
64 break;
65 case 2:
66 io_writehandlers[0][port+0](port+0,(val >> 0) & 0xff,1);
67 io_writehandlers[0][port+1](port+1,(val >> 8) & 0xff,1);
68 break;
69 case 4:
70 io_writehandlers[1][port+0](port+0,(val >> 0 ) & 0xffff,2);
71 io_writehandlers[1][port+2](port+2,(val >> 16) & 0xffff,2);
72 break;
73 }
74 }
75
76 void IO_RegisterReadHandler(Bitu port,IO_ReadHandler * handler,Bitu mask,Bitu range) {
77 while (range--) {
78 if (mask&IO_MB) io_readhandlers[0][port]=handler;
79 if (mask&IO_MW) io_readhandlers[1][port]=handler;
80 if (mask&IO_MD) io_readhandlers[2][port]=handler;
81 port++;
82 }
83 }
84
85 void IO_RegisterWriteHandler(Bitu port,IO_WriteHandler * handler,Bitu mask,Bitu range) {
86 while (range--) {
87 if (mask&IO_MB) io_writehandlers[0][port]=handler;
88 if (mask&IO_MW) io_writehandlers[1][port]=handler;
89 if (mask&IO_MD) io_writehandlers[2][port]=handler;
90 port++;
91 }
92 }
93
94 void IO_FreeReadHandler(Bitu port,Bitu mask,Bitu range) {
95 while (range--) {
96 if (mask&IO_MB) io_readhandlers[0][port]=IO_ReadDefault;
97 if (mask&IO_MW) io_readhandlers[1][port]=IO_ReadDefault;
98 if (mask&IO_MD) io_readhandlers[2][port]=IO_ReadDefault;
99 port++;
100 }
101 }
102
103 void IO_FreeWriteHandler(Bitu port,Bitu mask,Bitu range) {
104 while (range--) {
105 if (mask&IO_MB) io_writehandlers[0][port]=IO_WriteDefault;
106 if (mask&IO_MW) io_writehandlers[1][port]=IO_WriteDefault;
107 if (mask&IO_MD) io_writehandlers[2][port]=IO_WriteDefault;
108 port++;
109 }
110 }
111
112 void IO_ReadHandleObject::Install(Bitu port,IO_ReadHandler * handler,Bitu mask,Bitu range) {
113 if(!installed) {
114 installed=true;
115 m_port=port;
116 m_mask=mask;
117 m_range=range;
118 IO_RegisterReadHandler(port,handler,mask,range);
119 } else E_Exit("IO_readHandler allready installed port %x",port);
120 }
121
122 IO_ReadHandleObject::~IO_ReadHandleObject(){
123 if(!installed) return;
124 IO_FreeReadHandler(m_port,m_mask,m_range);
125 }
126
127 void IO_WriteHandleObject::Install(Bitu port,IO_WriteHandler * handler,Bitu mask,Bitu range) {
128 if(!installed) {
129 installed=true;
130 m_port=port;
131 m_mask=mask;
132 m_range=range;
133 IO_RegisterWriteHandler(port,handler,mask,range);
134 } else E_Exit("IO_writeHandler allready installed port %x",port);
135 }
136
137 IO_WriteHandleObject::~IO_WriteHandleObject(){
138 if(!installed) return;
139 IO_FreeWriteHandler(m_port,m_mask,m_range);
140 //LOG_MSG("FreeWritehandler called with port %X",m_port);
141 }
142
143 struct IOF_Entry {
144 Bitu cs;
145 Bitu eip;
146 };
147
148 #define IOF_QUEUESIZE 16
149 static struct {
150 Bitu used;
151 IOF_Entry entries[IOF_QUEUESIZE];
152 } iof_queue;
153
154 static Bits IOFaultCore(void) {
155 CPU_CycleLeft+=CPU_Cycles;
156 CPU_Cycles=1;
157 Bits ret=CPU_Core_Full_Run();
158 CPU_CycleLeft+=CPU_Cycles;
159 if (ret<0) E_Exit("Got a dosbox close machine in IO-fault core?");
160 if (ret)
161 return ret;
162 if (!iof_queue.used) E_Exit("IO-faul Core without IO-faul");
163 IOF_Entry * entry=&iof_queue.entries[iof_queue.used-1];
164 if (entry->cs == SegValue(cs) && entry->eip==reg_eip)
165 return -1;
166 return 0;
167 }
168
169
170 /* Some code to make io operations take some virtual time. Helps certain
171 * games with their timing of certain operations
172 */
173
174
175 #define IODELAY_READ_MICROS 1.0
176 #define IODELAY_WRITE_MICROS 0.75
177
178 inline void IO_USEC_read_delay_old() {
179 if(CPU_CycleMax > static_cast<Bit32s>((IODELAY_READ_MICROS*1000.0))) {
180 // this could be calculated whenever CPU_CycleMax changes
181 Bits delaycyc = static_cast<Bits>((CPU_CycleMax/1000)*IODELAY_READ_MICROS);
182 if(CPU_Cycles > delaycyc) CPU_Cycles -= delaycyc;
183 else CPU_Cycles = 0;
184 }
185 }
186
187 inline void IO_USEC_write_delay_old() {
188 if(CPU_CycleMax > static_cast<Bit32s>((IODELAY_WRITE_MICROS*1000.0))) {
189 // this could be calculated whenever CPU_CycleMax changes
190 Bits delaycyc = static_cast<Bits>((CPU_CycleMax/1000)*IODELAY_WRITE_MICROS);
191 if(CPU_Cycles > delaycyc) CPU_Cycles -= delaycyc;
192 else CPU_Cycles = 0;
193 }
194 }
195
196
197 #define IODELAY_READ_MICROSk (Bit32u)(1024/1.0)
198 #define IODELAY_WRITE_MICROSk (Bit32u)(1024/0.75)
199
200 inline void IO_USEC_read_delay() {
201 Bits delaycyc = CPU_CycleMax/IODELAY_READ_MICROSk;
202 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
203 CPU_Cycles -= delaycyc;
204 CPU_IODelayRemoved += delaycyc;
205 }
206
207 inline void IO_USEC_write_delay() {
208 Bits delaycyc = CPU_CycleMax/IODELAY_WRITE_MICROSk;
209 if(GCC_UNLIKELY(CPU_Cycles < 3*delaycyc)) delaycyc=0;
210 CPU_Cycles -= delaycyc;
211 CPU_IODelayRemoved += delaycyc;
212 }
213
214 #ifdef ENABLE_PORTLOG
215 static Bit8u crtc_index = 0;
216 const char* const len_type[] = {" 8","16","32"};
217 void log_io(Bitu width, bool write, Bitu port, Bitu val) {
218 switch(width) {
219 case 0:
220 val&=0xff;
221 break;
222 case 1:
223 val&=0xffff;
224 break;
225 }
226 if (write) {
227 // skip the video cursor position spam
228 if (port==0x3d4) {
229 if (width==0) crtc_index = (Bit8u)val;
230 else if(width==1) crtc_index = (Bit8u)(val>>8);
231 }
232 if (crtc_index==0xe || crtc_index==0xf) {
233 if((width==0 && (port==0x3d4 || port==0x3d5))||(width==1 && port==0x3d4))
234 return;
235 }
236
237 switch(port) {
238 //case 0x020: // interrupt command
239 //case 0x040: // timer 0
240 //case 0x042: // timer 2
241 //case 0x043: // timer control
242 //case 0x061: // speaker control
243 case 0x3c8: // VGA palette
244 case 0x3c9: // VGA palette
245 // case 0x3d4: // VGA crtc
246 // case 0x3d5: // VGA crtc
247 // case 0x3c4: // VGA seq
248 // case 0x3c5: // VGA seq
249 break;
250 default:
251 LOG_MSG("iow%s % 4x % 4x, cs:ip %04x:%04x", len_type[width],
252 port, val, SegValue(cs),reg_eip);
253 break;
254 }
255 } else {
256 switch(port) {
257 //case 0x021: // interrupt status
258 //case 0x040: // timer 0
259 //case 0x042: // timer 2
260 //case 0x061: // speaker control
261 case 0x201: // joystick status
262 case 0x3c9: // VGA palette
263 // case 0x3d4: // VGA crtc index
264 // case 0x3d5: // VGA crtc
265 case 0x3da: // display status - a real spammer
266 // don't log for the above cases
267 break;
268 default:
269 LOG_MSG("ior%s % 4x % 4x,\t\tcs:ip %04x:%04x", len_type[width],
270 port, val, SegValue(cs),reg_eip);
271 break;
272 }
273 }
274 }
275 #else
276 #define log_io(W, X, Y, Z)
277 #endif
278
279
280 void IO_WriteB(Bitu port,Bitu val) {
281 log_io(0, true, port, val);
282 if (GCC_UNLIKELY(GETFLAG(VM) && (CPU_IO_Exception(port,1)))) {
283 LazyFlags old_lflags;
284 memcpy(&old_lflags,&lflags,sizeof(LazyFlags));
285 CPU_Decoder * old_cpudecoder;
286 old_cpudecoder=cpudecoder;
287 cpudecoder=&IOFaultCore;
288 IOF_Entry * entry=&iof_queue.entries[iof_queue.used++];
289 entry->cs=SegValue(cs);
290 entry->eip=reg_eip;
291 CPU_Push16(SegValue(cs));
292 CPU_Push16(reg_ip);
293 Bit8u old_al = reg_al;
294 Bit16u old_dx = reg_dx;
295 reg_al = val;
296 reg_dx = port;
297 RealPt icb = CALLBACK_RealPointer(call_priv_io);
298 SegSet16(cs,RealSeg(icb));
299 reg_eip = RealOff(icb)+0x08;
300 CPU_Exception(cpu.exception.which,cpu.exception.error);
301
302 DOSBOX_RunMachine();
303 iof_queue.used--;
304
305 reg_al = old_al;
306 reg_dx = old_dx;
307 memcpy(&lflags,&old_lflags,sizeof(LazyFlags));
308 cpudecoder=old_cpudecoder;
309 }
310 else {
311 IO_USEC_write_delay();
312 io_writehandlers[0][port](port,val,1);
313 }
314 }
315
316 void IO_WriteW(Bitu port,Bitu val) {
317 log_io(1, true, port, val);
318 if (GCC_UNLIKELY(GETFLAG(VM) && (CPU_IO_Exception(port,2)))) {
319 LazyFlags old_lflags;
320 memcpy(&old_lflags,&lflags,sizeof(LazyFlags));
321 CPU_Decoder * old_cpudecoder;
322 old_cpudecoder=cpudecoder;
323 cpudecoder=&IOFaultCore;
324 IOF_Entry * entry=&iof_queue.entries[iof_queue.used++];
325 entry->cs=SegValue(cs);
326 entry->eip=reg_eip;
327 CPU_Push16(SegValue(cs));
328 CPU_Push16(reg_ip);
329 Bit16u old_ax = reg_ax;
330 Bit16u old_dx = reg_dx;
331 reg_al = val;
332 reg_dx = port;
333 RealPt icb = CALLBACK_RealPointer(call_priv_io);
334 SegSet16(cs,RealSeg(icb));
335 reg_eip = RealOff(icb)+0x0a;
336 CPU_Exception(cpu.exception.which,cpu.exception.error);
337
338 DOSBOX_RunMachine();
339 iof_queue.used--;
340
341 reg_ax = old_ax;
342 reg_dx = old_dx;
343 memcpy(&lflags,&old_lflags,sizeof(LazyFlags));
344 cpudecoder=old_cpudecoder;
345 }
346 else {
347 IO_USEC_write_delay();
348 io_writehandlers[1][port](port,val,2);
349 }
350 }
351
352 void IO_WriteD(Bitu port,Bitu val) {
353 log_io(2, true, port, val);
354 if (GCC_UNLIKELY(GETFLAG(VM) && (CPU_IO_Exception(port,4)))) {
355 LazyFlags old_lflags;
356 memcpy(&old_lflags,&lflags,sizeof(LazyFlags));
357 CPU_Decoder * old_cpudecoder;
358 old_cpudecoder=cpudecoder;
359 cpudecoder=&IOFaultCore;
360 IOF_Entry * entry=&iof_queue.entries[iof_queue.used++];
361 entry->cs=SegValue(cs);
362 entry->eip=reg_eip;
363 CPU_Push16(SegValue(cs));
364 CPU_Push16(reg_ip);
365 Bit32u old_eax = reg_eax;
366 Bit16u old_dx = reg_dx;
367 reg_al = val;
368 reg_dx = port;
369 RealPt icb = CALLBACK_RealPointer(call_priv_io);
370 SegSet16(cs,RealSeg(icb));
371 reg_eip = RealOff(icb)+0x0c;
372 CPU_Exception(cpu.exception.which,cpu.exception.error);
373
374 DOSBOX_RunMachine();
375 iof_queue.used--;
376
377 reg_eax = old_eax;
378 reg_dx = old_dx;
379 memcpy(&lflags,&old_lflags,sizeof(LazyFlags));
380 cpudecoder=old_cpudecoder;
381 }
382 else io_writehandlers[2][port](port,val,4);
383 }
384
385 Bitu IO_ReadB(Bitu port) {
386 Bitu retval;
387 if (GCC_UNLIKELY(GETFLAG(VM) && (CPU_IO_Exception(port,1)))) {
388 LazyFlags old_lflags;
389 memcpy(&old_lflags,&lflags,sizeof(LazyFlags));
390 CPU_Decoder * old_cpudecoder;
391 old_cpudecoder=cpudecoder;
392 cpudecoder=&IOFaultCore;
393 IOF_Entry * entry=&iof_queue.entries[iof_queue.used++];
394 entry->cs=SegValue(cs);
395 entry->eip=reg_eip;
396 CPU_Push16(SegValue(cs));
397 CPU_Push16(reg_ip);
398 Bit16u old_dx = reg_dx;
399 reg_dx = port;
400 RealPt icb = CALLBACK_RealPointer(call_priv_io);
401 SegSet16(cs,RealSeg(icb));
402 reg_eip = RealOff(icb)+0x00;
403 CPU_Exception(cpu.exception.which,cpu.exception.error);
404
405 DOSBOX_RunMachine();
406 iof_queue.used--;
407
408 retval = reg_al;
409 reg_dx = old_dx;
410 memcpy(&lflags,&old_lflags,sizeof(LazyFlags));
411 cpudecoder=old_cpudecoder;
412 return retval;
413 }
414 else {
415 IO_USEC_read_delay();
416 retval = io_readhandlers[0][port](port,1);
417 }
418 log_io(0, false, port, retval);
419 return retval;
420 }
421
422 Bitu IO_ReadW(Bitu port) {
423 Bitu retval;
424 if (GCC_UNLIKELY(GETFLAG(VM) && (CPU_IO_Exception(port,2)))) {
425 LazyFlags old_lflags;
426 memcpy(&old_lflags,&lflags,sizeof(LazyFlags));
427 CPU_Decoder * old_cpudecoder;
428 old_cpudecoder=cpudecoder;
429 cpudecoder=&IOFaultCore;
430 IOF_Entry * entry=&iof_queue.entries[iof_queue.used++];
431 entry->cs=SegValue(cs);
432 entry->eip=reg_eip;
433 CPU_Push16(SegValue(cs));
434 CPU_Push16(reg_ip);
435 Bit16u old_dx = reg_dx;
436 reg_dx = port;
437 RealPt icb = CALLBACK_RealPointer(call_priv_io);
438 SegSet16(cs,RealSeg(icb));
439 reg_eip = RealOff(icb)+0x02;
440 CPU_Exception(cpu.exception.which,cpu.exception.error);
441
442 DOSBOX_RunMachine();
443 iof_queue.used--;
444
445 retval = reg_ax;
446 reg_dx = old_dx;
447 memcpy(&lflags,&old_lflags,sizeof(LazyFlags));
448 cpudecoder=old_cpudecoder;
449 }
450 else {
451 IO_USEC_read_delay();
452 retval = io_readhandlers[1][port](port,2);
453 }
454 log_io(1, false, port, retval);
455 return retval;
456 }
457
458 Bitu IO_ReadD(Bitu port) {
459 Bitu retval;
460 if (GCC_UNLIKELY(GETFLAG(VM) && (CPU_IO_Exception(port,4)))) {
461 LazyFlags old_lflags;
462 memcpy(&old_lflags,&lflags,sizeof(LazyFlags));
463 CPU_Decoder * old_cpudecoder;
464 old_cpudecoder=cpudecoder;
465 cpudecoder=&IOFaultCore;
466 IOF_Entry * entry=&iof_queue.entries[iof_queue.used++];
467 entry->cs=SegValue(cs);
468 entry->eip=reg_eip;
469 CPU_Push16(SegValue(cs));
470 CPU_Push16(reg_ip);
471 Bit16u old_dx = reg_dx;
472 reg_dx = port;
473 RealPt icb = CALLBACK_RealPointer(call_priv_io);
474 SegSet16(cs,RealSeg(icb));
475 reg_eip = RealOff(icb)+0x04;
476 CPU_Exception(cpu.exception.which,cpu.exception.error);
477
478 DOSBOX_RunMachine();
479 iof_queue.used--;
480
481 retval = reg_eax;
482 reg_dx = old_dx;
483 memcpy(&lflags,&old_lflags,sizeof(LazyFlags));
484 cpudecoder=old_cpudecoder;
485 } else {
486 retval = io_readhandlers[2][port](port,4);
487 }
488 log_io(2, false, port, retval);
489 return retval;
490 }
491
492 class IO :public Module_base {
493 public:
494 IO(Section* configuration):Module_base(configuration){
495 iof_queue.used=0;
496 IO_FreeReadHandler(0,IO_MA,IO_MAX);
497 IO_FreeWriteHandler(0,IO_MA,IO_MAX);
498 }
499 ~IO()
500 {
501 //Same as the constructor ?
502 }
503 };
504
505 static IO* test;
506
507 void IO_Destroy(Section*) {
508 delete test;
509 }
510
511 void IO_Init(Section * sect) {
512 test = new IO(sect);
513 sect->AddDestroyFunction(&IO_Destroy);
514 }