Debian Codegraph @

src/cpu/callback.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: callback.cpp,v 1.42 2009-08-23 17:24:54 c2woody Exp $ */
20
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "dosbox.h"
25 #include "callback.h"
26 #include "mem.h"
27 #include "cpu.h"
28
29 /* CallBack are located at 0xF000:0x1000 (see CB_SEG and CB_SOFFSET in callback.h)
30 And they are 16 bytes each and you can define them to behave in certain ways like a
31 far return or and IRET
32 */
33
34 CallBack_Handler CallBack_Handlers[CB_MAX];
35 char* CallBack_Description[CB_MAX];
36
37 static Bitu call_stop,call_idle,call_default,call_default2;
38 Bitu call_priv_io;
39
40 static Bitu illegal_handler(void) {
41 E_Exit("Illegal CallBack Called");
42 return 1;
43 }
44
45 Bitu CALLBACK_Allocate(void) {
46 for (Bitu i=1;(i<CB_MAX);i++) {
47 if (CallBack_Handlers[i]==&illegal_handler) {
48 CallBack_Handlers[i]=0;
49 return i;
50 }
51 }
52 E_Exit("CALLBACK:Can't allocate handler.");
53 return 0;
54 }
55
56 void CALLBACK_DeAllocate(Bitu in) {
57 CallBack_Handlers[in]=&illegal_handler;
58 }
59
60
61 void CALLBACK_Idle(void) {
62 /* this makes the cpu execute instructions to handle irq's and then come back */
63 Bitu oldIF=GETFLAG(IF);
64 SETFLAGBIT(IF,true);
65 Bit16u oldcs=SegValue(cs);
66 Bit32u oldeip=reg_eip;
67 SegSet16(cs,CB_SEG);
68 reg_eip=CB_SOFFSET+call_idle*CB_SIZE;
69 DOSBOX_RunMachine();
70 reg_eip=oldeip;
71 SegSet16(cs,oldcs);
72 SETFLAGBIT(IF,oldIF);
73 if (!CPU_CycleAutoAdjust && CPU_Cycles>0)
74 CPU_Cycles=0;
75 }
76
77 static Bitu default_handler(void) {
78 LOG(LOG_CPU,LOG_ERROR)("Illegal Unhandled Interrupt Called %X",lastint);
79 return CBRET_NONE;
80 }
81
82 static Bitu stop_handler(void) {
83 return CBRET_STOP;
84 }
85
86
87
88 void CALLBACK_RunRealFar(Bit16u seg,Bit16u off) {
89 reg_sp-=4;
90 mem_writew(SegPhys(ss)+reg_sp,RealOff(CALLBACK_RealPointer(call_stop)));
91 mem_writew(SegPhys(ss)+reg_sp+2,RealSeg(CALLBACK_RealPointer(call_stop)));
92 Bit32u oldeip=reg_eip;
93 Bit16u oldcs=SegValue(cs);
94 reg_eip=off;
95 SegSet16(cs,seg);
96 DOSBOX_RunMachine();
97 reg_eip=oldeip;
98 SegSet16(cs,oldcs);
99 }
100
101 void CALLBACK_RunRealInt(Bit8u intnum) {
102 Bit32u oldeip=reg_eip;
103 Bit16u oldcs=SegValue(cs);
104 reg_eip=CB_SOFFSET+(CB_MAX*CB_SIZE)+(intnum*6);
105 SegSet16(cs,CB_SEG);
106 DOSBOX_RunMachine();
107 reg_eip=oldeip;
108 SegSet16(cs,oldcs);
109 }
110
111 void CALLBACK_SZF(bool val) {
112 Bit16u tempf = mem_readw(SegPhys(ss)+reg_sp+4);
113 if (val) tempf |= FLAG_ZF;
114 else tempf &= ~FLAG_ZF;
115 mem_writew(SegPhys(ss)+reg_sp+4,tempf);
116 }
117
118 void CALLBACK_SCF(bool val) {
119 Bit16u tempf = mem_readw(SegPhys(ss)+reg_sp+4);
120 if (val) tempf |= FLAG_CF;
121 else tempf &= ~FLAG_CF;
122 mem_writew(SegPhys(ss)+reg_sp+4,tempf);
123 }
124
125 void CALLBACK_SIF(bool val) {
126 Bit16u tempf = mem_readw(SegPhys(ss)+reg_sp+4);
127 if (val) tempf |= FLAG_IF;
128 else tempf &= ~FLAG_IF;
129 mem_writew(SegPhys(ss)+reg_sp+4,tempf);
130 }
131
132 void CALLBACK_SetDescription(Bitu nr, const char* descr) {
133 if (descr) {
134 CallBack_Description[nr] = new char[strlen(descr)+1];
135 strcpy(CallBack_Description[nr],descr);
136 } else
137 CallBack_Description[nr] = 0;
138 }
139
140 const char* CALLBACK_GetDescription(Bitu nr) {
141 if (nr>=CB_MAX) return 0;
142 return CallBack_Description[nr];
143 }
144
145 Bitu CALLBACK_SetupExtra(Bitu callback, Bitu type, PhysPt physAddress, bool use_cb=true) {
146 if (callback>=CB_MAX)
147 return 0;
148 switch (type) {
149 case CB_RETN:
150 if (use_cb) {
151 phys_writeb(physAddress+0x00,(Bit8u)0xFE); //GRP 4
152 phys_writeb(physAddress+0x01,(Bit8u)0x38); //Extra Callback instruction
153 phys_writew(physAddress+0x02,(Bit16u)callback); //The immediate word
154 physAddress+=4;
155 }
156 phys_writeb(physAddress+0x00,(Bit8u)0xC3); //A RETN Instruction
157 return (use_cb?5:1);
158 case CB_RETF:
159 if (use_cb) {
160 phys_writeb(physAddress+0x00,(Bit8u)0xFE); //GRP 4
161 phys_writeb(physAddress+0x01,(Bit8u)0x38); //Extra Callback instruction
162 phys_writew(physAddress+0x02,(Bit16u)callback); //The immediate word
163 physAddress+=4;
164 }
165 phys_writeb(physAddress+0x00,(Bit8u)0xCB); //A RETF Instruction
166 return (use_cb?5:1);
167 case CB_RETF8:
168 if (use_cb) {
169 phys_writeb(physAddress+0x00,(Bit8u)0xFE); //GRP 4
170 phys_writeb(physAddress+0x01,(Bit8u)0x38); //Extra Callback instruction
171 phys_writew(physAddress+0x02,(Bit16u)callback); //The immediate word
172 physAddress+=4;
173 }
174 phys_writeb(physAddress+0x00,(Bit8u)0xCA); //A RETF 8 Instruction
175 phys_writew(physAddress+0x01,(Bit16u)0x0008);
176 return (use_cb?7:3);
177 case CB_IRET:
178 if (use_cb) {
179 phys_writeb(physAddress+0x00,(Bit8u)0xFE); //GRP 4
180 phys_writeb(physAddress+0x01,(Bit8u)0x38); //Extra Callback instruction
181 phys_writew(physAddress+0x02,(Bit16u)callback); //The immediate word
182 physAddress+=4;
183 }
184 phys_writeb(physAddress+0x00,(Bit8u)0xCF); //An IRET Instruction
185 return (use_cb?5:1);
186 case CB_IRETD:
187 if (use_cb) {
188 phys_writeb(physAddress+0x00,(Bit8u)0xFE); //GRP 4
189 phys_writeb(physAddress+0x01,(Bit8u)0x38); //Extra Callback instruction
190 phys_writew(physAddress+0x02,(Bit16u)callback); //The immediate word
191 physAddress+=4;
192 }
193 phys_writeb(physAddress+0x00,(Bit8u)0x66); //An IRETD Instruction
194 phys_writeb(physAddress+0x01,(Bit8u)0xCF);
195 return (use_cb?6:2);
196 case CB_IRET_STI:
197 phys_writeb(physAddress+0x00,(Bit8u)0xFB); //STI
198 if (use_cb) {
199 phys_writeb(physAddress+0x01,(Bit8u)0xFE); //GRP 4
200 phys_writeb(physAddress+0x02,(Bit8u)0x38); //Extra Callback instruction
201 phys_writew(physAddress+0x03,(Bit16u)callback); //The immediate word
202 physAddress+=4;
203 }
204 phys_writeb(physAddress+0x01,(Bit8u)0xCF); //An IRET Instruction
205 return (use_cb?6:2);
206 case CB_IRET_EOI_PIC1:
207 if (use_cb) {
208 phys_writeb(physAddress+0x00,(Bit8u)0xFE); //GRP 4
209 phys_writeb(physAddress+0x01,(Bit8u)0x38); //Extra Callback instruction
210 phys_writew(physAddress+0x02,(Bit16u)callback); //The immediate word
211 physAddress+=4;
212 }
213 phys_writeb(physAddress+0x00,(Bit8u)0x50); // push ax
214 phys_writeb(physAddress+0x01,(Bit8u)0xb0); // mov al, 0x20
215 phys_writeb(physAddress+0x02,(Bit8u)0x20);
216 phys_writeb(physAddress+0x03,(Bit8u)0xe6); // out 0x20, al
217 phys_writeb(physAddress+0x04,(Bit8u)0x20);
218 phys_writeb(physAddress+0x05,(Bit8u)0x58); // pop ax
219 phys_writeb(physAddress+0x06,(Bit8u)0xcf); //An IRET Instruction
220 return (use_cb?0x0b:0x07);
221 case CB_IRQ0: // timer int8
222 if (use_cb) {
223 phys_writeb(physAddress+0x00,(Bit8u)0xFE); //GRP 4
224 phys_writeb(physAddress+0x01,(Bit8u)0x38); //Extra Callback instruction
225 phys_writew(physAddress+0x02,(Bit16u)callback); //The immediate word
226 physAddress+=4;
227 }
228 phys_writeb(physAddress+0x00,(Bit8u)0x50); // push ax
229 phys_writeb(physAddress+0x01,(Bit8u)0x52); // push dx
230 phys_writeb(physAddress+0x02,(Bit8u)0x1e); // push ds
231 phys_writew(physAddress+0x03,(Bit16u)0x1ccd); // int 1c
232 phys_writeb(physAddress+0x05,(Bit8u)0xfa); // cli
233 phys_writeb(physAddress+0x06,(Bit8u)0x1f); // pop ds
234 phys_writeb(physAddress+0x07,(Bit8u)0x5a); // pop dx
235 phys_writew(physAddress+0x08,(Bit16u)0x20b0); // mov al, 0x20
236 phys_writew(physAddress+0x0a,(Bit16u)0x20e6); // out 0x20, al
237 phys_writeb(physAddress+0x0c,(Bit8u)0x58); // pop ax
238 phys_writeb(physAddress+0x0d,(Bit8u)0xcf); //An IRET Instruction
239 return (use_cb?0x12:0x0e);
240 case CB_IRQ1: // keyboard int9
241 phys_writeb(physAddress+0x00,(Bit8u)0x50); // push ax
242 phys_writew(physAddress+0x01,(Bit16u)0x60e4); // in al, 0x60
243 phys_writew(physAddress+0x03,(Bit16u)0x4fb4); // mov ah, 0x4f
244 phys_writeb(physAddress+0x05,(Bit8u)0xf9); // stc
245 phys_writew(physAddress+0x06,(Bit16u)0x15cd); // int 15
246 if (use_cb) {
247 phys_writew(physAddress+0x08,(Bit16u)0x0473); // jc skip
248 phys_writeb(physAddress+0x0a,(Bit8u)0xFE); //GRP 4
249 phys_writeb(physAddress+0x0b,(Bit8u)0x38); //Extra Callback instruction
250 phys_writew(physAddress+0x0c,(Bit16u)callback); //The immediate word
251 // jump here to (skip):
252 physAddress+=6;
253 }
254 phys_writeb(physAddress+0x08,(Bit8u)0xfa); // cli
255 phys_writew(physAddress+0x09,(Bit16u)0x20b0); // mov al, 0x20
256 phys_writew(physAddress+0x0b,(Bit16u)0x20e6); // out 0x20, al
257 phys_writeb(physAddress+0x0d,(Bit8u)0x58); // pop ax
258 phys_writeb(physAddress+0x0e,(Bit8u)0xcf); //An IRET Instruction
259 return (use_cb?0x15:0x0f);
260 case CB_IRQ9: // pic cascade interrupt
261 if (use_cb) {
262 phys_writeb(physAddress+0x00,(Bit8u)0xFE); //GRP 4
263 phys_writeb(physAddress+0x01,(Bit8u)0x38); //Extra Callback instruction
264 phys_writew(physAddress+0x02,(Bit16u)callback); //The immediate word
265 physAddress+=4;
266 }
267 phys_writeb(physAddress+0x00,(Bit8u)0x50); // push ax
268 phys_writew(physAddress+0x01,(Bit16u)0x61b0); // mov al, 0x61
269 phys_writew(physAddress+0x03,(Bit16u)0xa0e6); // out 0xa0, al
270 phys_writew(physAddress+0x05,(Bit16u)0x0acd); // int a
271 phys_writeb(physAddress+0x07,(Bit8u)0xfa); // cli
272 phys_writeb(physAddress+0x08,(Bit8u)0x58); // pop ax
273 phys_writeb(physAddress+0x09,(Bit8u)0xcf); //An IRET Instruction
274 return (use_cb?0x0e:0x0a);
275 case CB_IRQ12: // ps2 mouse int74
276 if (!use_cb) E_Exit("int74 callback must implement a callback handler!");
277 phys_writeb(physAddress+0x00,(Bit8u)0x1e); // push ds
278 phys_writeb(physAddress+0x01,(Bit8u)0x06); // push es
279 phys_writew(physAddress+0x02,(Bit16u)0x6066); // pushad
280 phys_writeb(physAddress+0x04,(Bit8u)0xfc); // cld
281 phys_writeb(physAddress+0x05,(Bit8u)0xfb); // sti
282 phys_writeb(physAddress+0x06,(Bit8u)0xFE); //GRP 4
283 phys_writeb(physAddress+0x07,(Bit8u)0x38); //Extra Callback instruction
284 phys_writew(physAddress+0x08,(Bit16u)callback); //The immediate word
285 return 0x0a;
286 case CB_IRQ12_RET: // ps2 mouse int74 return
287 if (use_cb) {
288 phys_writeb(physAddress+0x00,(Bit8u)0xFE); //GRP 4
289 phys_writeb(physAddress+0x01,(Bit8u)0x38); //Extra Callback instruction
290 phys_writew(physAddress+0x02,(Bit16u)callback); //The immediate word
291 physAddress+=4;
292 }
293 phys_writeb(physAddress+0x00,(Bit8u)0xfa); // cli
294 phys_writew(physAddress+0x01,(Bit16u)0x20b0); // mov al, 0x20
295 phys_writew(physAddress+0x03,(Bit16u)0xa0e6); // out 0xa0, al
296 phys_writew(physAddress+0x05,(Bit16u)0x20e6); // out 0x20, al
297 phys_writew(physAddress+0x07,(Bit16u)0x6166); // popad
298 phys_writeb(physAddress+0x09,(Bit8u)0x07); // pop es
299 phys_writeb(physAddress+0x0a,(Bit8u)0x1f); // pop ds
300 phys_writeb(physAddress+0x0b,(Bit8u)0xcf); //An IRET Instruction
301 return (use_cb?0x10:0x0c);
302 case CB_IRQ6_PCJR: // pcjr keyboard interrupt
303 phys_writeb(physAddress+0x00,(Bit8u)0x50); // push ax
304 phys_writew(physAddress+0x01,(Bit16u)0x60e4); // in al, 0x60
305 phys_writew(physAddress+0x03,(Bit16u)0xe03c); // cmp al, 0xe0
306 if (use_cb) {
307 phys_writew(physAddress+0x05,(Bit16u)0x0674); // je skip
308 phys_writeb(physAddress+0x07,(Bit8u)0xFE); //GRP 4
309 phys_writeb(physAddress+0x08,(Bit8u)0x38); //Extra Callback instruction
310 phys_writew(physAddress+0x09,(Bit16u)callback); //The immediate word
311 physAddress+=4;
312 } else {
313 phys_writew(physAddress+0x05,(Bit16u)0x0274); // je skip
314 }
315 phys_writew(physAddress+0x07,(Bit16u)0x09cd); // int 9
316 // jump here to (skip):
317 phys_writeb(physAddress+0x09,(Bit8u)0xfa); // cli
318 phys_writew(physAddress+0x0a,(Bit16u)0x20b0); // mov al, 0x20
319 phys_writew(physAddress+0x0c,(Bit16u)0x20e6); // out 0x20, al
320 phys_writeb(physAddress+0x0e,(Bit8u)0x58); // pop ax
321 phys_writeb(physAddress+0x0f,(Bit8u)0xcf); //An IRET Instruction
322 return (use_cb?0x14:0x10);
323 case CB_MOUSE:
324 phys_writew(physAddress+0x00,(Bit16u)0x07eb); // jmp i33hd
325 physAddress+=9;
326 // jump here to (i33hd):
327 if (use_cb) {
328 phys_writeb(physAddress+0x00,(Bit8u)0xFE); //GRP 4
329 phys_writeb(physAddress+0x01,(Bit8u)0x38); //Extra Callback instruction
330 phys_writew(physAddress+0x02,(Bit16u)callback); //The immediate word
331 physAddress+=4;
332 }
333 phys_writeb(physAddress+0x00,(Bit8u)0xCF); //An IRET Instruction
334 return (use_cb?0x0e:0x0a);
335 case CB_INT16:
336 phys_writeb(physAddress+0x00,(Bit8u)0xFB); //STI
337 if (use_cb) {
338 phys_writeb(physAddress+0x01,(Bit8u)0xFE); //GRP 4
339 phys_writeb(physAddress+0x02,(Bit8u)0x38); //Extra Callback instruction
340 phys_writew(physAddress+0x03,(Bit16u)callback); //The immediate word
341 physAddress+=4;
342 }
343 phys_writeb(physAddress+0x01,(Bit8u)0xCF); //An IRET Instruction
344 for (Bitu i=0;i<=0x0b;i++) phys_writeb(physAddress+0x02+i,0x90);
345 phys_writew(physAddress+0x0e,(Bit16u)0xedeb); //jmp callback
346 return (use_cb?0x10:0x0c);
347 case CB_INT29: // fast console output
348 if (use_cb) {
349 phys_writeb(physAddress+0x00,(Bit8u)0xFE); //GRP 4
350 phys_writeb(physAddress+0x01,(Bit8u)0x38); //Extra Callback instruction
351 phys_writew(physAddress+0x02,(Bit16u)callback); //The immediate word
352 physAddress+=4;
353 }
354 phys_writeb(physAddress+0x00,(Bit8u)0x50); // push ax
355 phys_writew(physAddress+0x01,(Bit16u)0x0eb4); // mov ah, 0x0e
356 phys_writew(physAddress+0x03,(Bit16u)0x10cd); // int 10
357 phys_writeb(physAddress+0x05,(Bit8u)0x58); // pop ax
358 phys_writeb(physAddress+0x06,(Bit8u)0xcf); //An IRET Instruction
359 return (use_cb?0x0b:0x07);
360 case CB_HOOKABLE:
361 phys_writeb(physAddress+0x00,(Bit8u)0xEB); //jump near
362 phys_writeb(physAddress+0x01,(Bit8u)0x03); //offset
363 phys_writeb(physAddress+0x02,(Bit8u)0x90); //NOP
364 phys_writeb(physAddress+0x03,(Bit8u)0x90); //NOP
365 phys_writeb(physAddress+0x04,(Bit8u)0x90); //NOP
366 if (use_cb) {
367 phys_writeb(physAddress+0x05,(Bit8u)0xFE); //GRP 4
368 phys_writeb(physAddress+0x06,(Bit8u)0x38); //Extra Callback instruction
369 phys_writew(physAddress+0x07,(Bit16u)callback); //The immediate word
370 physAddress+=4;
371 }
372 phys_writeb(physAddress+0x05,(Bit8u)0xCB); //A RETF Instruction
373 return (use_cb?0x0a:0x06);
374 case CB_TDE_IRET: // TandyDAC end transfer
375 if (use_cb) {
376 phys_writeb(physAddress+0x00,(Bit8u)0xFE); //GRP 4
377 phys_writeb(physAddress+0x01,(Bit8u)0x38); //Extra Callback instruction
378 phys_writew(physAddress+0x02,(Bit16u)callback); //The immediate word
379 physAddress+=4;
380 }
381 phys_writeb(physAddress+0x00,(Bit8u)0x50); // push ax
382 phys_writeb(physAddress+0x01,(Bit8u)0xb8); // mov ax, 0x91fb
383 phys_writew(physAddress+0x02,(Bit16u)0x91fb);
384 phys_writew(physAddress+0x04,(Bit16u)0x15cd); // int 15
385 phys_writeb(physAddress+0x06,(Bit8u)0xfa); // cli
386 phys_writew(physAddress+0x07,(Bit16u)0x20b0); // mov al, 0x20
387 phys_writew(physAddress+0x09,(Bit16u)0x20e6); // out 0x20, al
388 phys_writeb(physAddress+0x0b,(Bit8u)0x58); // pop ax
389 phys_writeb(physAddress+0x0c,(Bit8u)0xcf); //An IRET Instruction
390 return (use_cb?0x11:0x0d);
391 /* case CB_IPXESR: // IPX ESR
392 if (!use_cb) E_Exit("ipx esr must implement a callback handler!");
393 phys_writeb(physAddress+0x00,(Bit8u)0x1e); // push ds
394 phys_writeb(physAddress+0x01,(Bit8u)0x06); // push es
395 phys_writew(physAddress+0x02,(Bit16u)0xa00f); // push fs
396 phys_writew(physAddress+0x04,(Bit16u)0xa80f); // push gs
397 phys_writeb(physAddress+0x06,(Bit8u)0x60); // pusha
398 phys_writeb(physAddress+0x07,(Bit8u)0xFE); //GRP 4
399 phys_writeb(physAddress+0x08,(Bit8u)0x38); //Extra Callback instruction
400 phys_writew(physAddress+0x09,(Bit16u)callback); //The immediate word
401 phys_writeb(physAddress+0x0b,(Bit8u)0xCB); //A RETF Instruction
402 return 0x0c;
403 case CB_IPXESR_RET: // IPX ESR return
404 if (use_cb) E_Exit("ipx esr return must not implement a callback handler!");
405 phys_writeb(physAddress+0x00,(Bit8u)0xfa); // cli
406 phys_writew(physAddress+0x01,(Bit16u)0x20b0); // mov al, 0x20
407 phys_writew(physAddress+0x03,(Bit16u)0xa0e6); // out 0xa0, al
408 phys_writew(physAddress+0x05,(Bit16u)0x20e6); // out 0x20, al
409 phys_writeb(physAddress+0x07,(Bit8u)0x61); // popa
410 phys_writew(physAddress+0x08,(Bit16u)0xA90F); // pop gs
411 phys_writew(physAddress+0x0a,(Bit16u)0xA10F); // pop fs
412 phys_writeb(physAddress+0x0c,(Bit8u)0x07); // pop es
413 phys_writeb(physAddress+0x0d,(Bit8u)0x1f); // pop ds
414 phys_writeb(physAddress+0x0e,(Bit8u)0xcf); //An IRET Instruction
415 return 0x0f; */
416 case CB_INT21:
417 phys_writeb(physAddress+0x00,(Bit8u)0xFB); //STI
418 if (use_cb) {
419 phys_writeb(physAddress+0x01,(Bit8u)0xFE); //GRP 4
420 phys_writeb(physAddress+0x02,(Bit8u)0x38); //Extra Callback instruction
421 phys_writew(physAddress+0x03,(Bit16u)callback); //The immediate word
422 physAddress+=4;
423 }
424 phys_writeb(physAddress+0x01,(Bit8u)0xCF); //An IRET Instruction
425 phys_writeb(physAddress+0x02,(Bit8u)0xCB); //A RETF Instruction
426 phys_writeb(physAddress+0x03,(Bit8u)0x51); // push cx
427 phys_writeb(physAddress+0x04,(Bit8u)0xB9); // mov cx,
428 phys_writew(physAddress+0x05,(Bit16u)0x0140); // 0x140
429 phys_writew(physAddress+0x07,(Bit16u)0xFEE2); // loop $-2
430 phys_writeb(physAddress+0x09,(Bit8u)0x59); // pop cx
431 phys_writeb(physAddress+0x0A,(Bit8u)0xCF); //An IRET Instruction
432 return (use_cb?15:11);
433
434 default:
435 E_Exit("CALLBACK:Setup:Illegal type %d",type);
436 }
437 return 0;
438 }
439
440 bool CALLBACK_Setup(Bitu callback,CallBack_Handler handler,Bitu type,const char* descr) {
441 if (callback>=CB_MAX) return false;
442 CALLBACK_SetupExtra(callback,type,CALLBACK_PhysPointer(callback)+0,(handler!=NULL));
443 CallBack_Handlers[callback]=handler;
444 CALLBACK_SetDescription(callback,descr);
445 return true;
446 }
447
448 Bitu CALLBACK_Setup(Bitu callback,CallBack_Handler handler,Bitu type,PhysPt addr,const char* descr) {
449 if (callback>=CB_MAX) return 0;
450 Bitu csize=CALLBACK_SetupExtra(callback,type,addr,(handler!=NULL));
451 if (csize>0) {
452 CallBack_Handlers[callback]=handler;
453 CALLBACK_SetDescription(callback,descr);
454 }
455 return csize;
456 }
457
458 void CALLBACK_RemoveSetup(Bitu callback) {
459 for (Bitu i = 0;i < 16;i++) {
460 phys_writeb(CALLBACK_PhysPointer(callback)+i ,(Bit8u) 0x00);
461 }
462 }
463
464 CALLBACK_HandlerObject::~CALLBACK_HandlerObject(){
465 if(!installed) return;
466 if(m_type == CALLBACK_HandlerObject::SETUP) {
467 if(vectorhandler.installed){
468 //See if we are the current handler. if so restore the old one
469 if(RealGetVec(vectorhandler.interrupt) == Get_RealPointer()) {
470 RealSetVec(vectorhandler.interrupt,vectorhandler.old_vector);
471 } else
472 LOG(LOG_MISC,LOG_WARN)("Interrupt vector changed on %X %s",vectorhandler.interrupt,CALLBACK_GetDescription(m_callback));
473 }
474 CALLBACK_RemoveSetup(m_callback);
475 } else if(m_type == CALLBACK_HandlerObject::SETUPAT){
476 E_Exit("Callback:SETUP at not handled yet.");
477 } else if(m_type == CALLBACK_HandlerObject::NONE){
478 //Do nothing. Merely DeAllocate the callback
479 } else E_Exit("what kind of callback is this!");
480 if(CallBack_Description[m_callback]) delete [] CallBack_Description[m_callback];
481 CallBack_Description[m_callback] = 0;
482 CALLBACK_DeAllocate(m_callback);
483 }
484
485 void CALLBACK_HandlerObject::Install(CallBack_Handler handler,Bitu type,const char* description){
486 if(!installed) {
487 installed=true;
488 m_type=SETUP;
489 m_callback=CALLBACK_Allocate();
490 CALLBACK_Setup(m_callback,handler,type,description);
491 } else E_Exit("Allready installed");
492 }
493 void CALLBACK_HandlerObject::Install(CallBack_Handler handler,Bitu type,PhysPt addr,const char* description){
494 if(!installed) {
495 installed=true;
496 m_type=SETUP;
497 m_callback=CALLBACK_Allocate();
498 CALLBACK_Setup(m_callback,handler,type,addr,description);
499 } else E_Exit("Allready installed");
500 }
501
502 void CALLBACK_HandlerObject::Allocate(CallBack_Handler handler,const char* description) {
503 if(!installed) {
504 installed=true;
505 m_type=NONE;
506 m_callback=CALLBACK_Allocate();
507 CALLBACK_SetDescription(m_callback,description);
508 CallBack_Handlers[m_callback]=handler;
509 } else E_Exit("Allready installed");
510 }
511
512 void CALLBACK_HandlerObject::Set_RealVec(Bit8u vec){
513 if(!vectorhandler.installed) {
514 vectorhandler.installed=true;
515 vectorhandler.interrupt=vec;
516 RealSetVec(vec,Get_RealPointer(),vectorhandler.old_vector);
517 } else E_Exit ("double usage of vector handler");
518 }
519
520 void CALLBACK_Init(Section* /*sec*/) {
521 Bitu i;
522 for (i=0;i<CB_MAX;i++) {
523 CallBack_Handlers[i]=&illegal_handler;
524 }
525
526 /* Setup the Stop Handler */
527 call_stop=CALLBACK_Allocate();
528 CallBack_Handlers[call_stop]=stop_handler;
529 CALLBACK_SetDescription(call_stop,"stop");
530 phys_writeb(CALLBACK_PhysPointer(call_stop)+0,0xFE);
531 phys_writeb(CALLBACK_PhysPointer(call_stop)+1,0x38);
532 phys_writew(CALLBACK_PhysPointer(call_stop)+2,(Bit16u)call_stop);
533
534 /* Setup the idle handler */
535 call_idle=CALLBACK_Allocate();
536 CallBack_Handlers[call_idle]=stop_handler;
537 CALLBACK_SetDescription(call_idle,"idle");
538 for (i=0;i<=11;i++) phys_writeb(CALLBACK_PhysPointer(call_idle)+i,0x90);
539 phys_writeb(CALLBACK_PhysPointer(call_idle)+12,0xFE);
540 phys_writeb(CALLBACK_PhysPointer(call_idle)+13,0x38);
541 phys_writew(CALLBACK_PhysPointer(call_idle)+14,(Bit16u)call_idle);
542
543 /* Default handlers for unhandled interrupts that have to be non-null */
544 call_default=CALLBACK_Allocate();
545 CALLBACK_Setup(call_default,&default_handler,CB_IRET,"default");
546 call_default2=CALLBACK_Allocate();
547 CALLBACK_Setup(call_default2,&default_handler,CB_IRET,"default");
548
549 /* Only setup default handler for first part of interrupt table */
550 for (Bit16u ct=0;ct<0x60;ct++) {
551 real_writed(0,ct*4,CALLBACK_RealPointer(call_default));
552 }
553 for (Bit16u ct=0x68;ct<0x70;ct++) {
554 real_writed(0,ct*4,CALLBACK_RealPointer(call_default));
555 }
556 /* Setup block of 0xCD 0xxx instructions */
557 PhysPt rint_base=CALLBACK_GetBase()+CB_MAX*CB_SIZE;
558 for (i=0;i<=0xff;i++) {
559 phys_writeb(rint_base,0xCD);
560 phys_writeb(rint_base+1,(Bit8u)i);
561 phys_writeb(rint_base+2,0xFE);
562 phys_writeb(rint_base+3,0x38);
563 phys_writew(rint_base+4,(Bit16u)call_stop);
564 rint_base+=6;
565
566 }
567 // setup a few interrupt handlers that point to bios IRETs by default
568 real_writed(0,0x0e*4,CALLBACK_RealPointer(call_default2)); //design your own railroad
569 real_writed(0,0x66*4,CALLBACK_RealPointer(call_default)); //war2d
570 real_writed(0,0x67*4,CALLBACK_RealPointer(call_default));
571 real_writed(0,0x68*4,CALLBACK_RealPointer(call_default));
572 real_writed(0,0x5c*4,CALLBACK_RealPointer(call_default)); //Network stuff
573 //real_writed(0,0xf*4,0); some games don't like it
574
575 call_priv_io=CALLBACK_Allocate();
576
577 // virtualizable in-out opcodes
578 phys_writeb(CALLBACK_PhysPointer(call_priv_io)+0x00,(Bit8u)0xec); // in al, dx
579 phys_writeb(CALLBACK_PhysPointer(call_priv_io)+0x01,(Bit8u)0xcb); // retf
580 phys_writeb(CALLBACK_PhysPointer(call_priv_io)+0x02,(Bit8u)0xed); // in ax, dx
581 phys_writeb(CALLBACK_PhysPointer(call_priv_io)+0x03,(Bit8u)0xcb); // retf
582 phys_writeb(CALLBACK_PhysPointer(call_priv_io)+0x04,(Bit8u)0x66); // in eax, dx
583 phys_writeb(CALLBACK_PhysPointer(call_priv_io)+0x05,(Bit8u)0xed);
584 phys_writeb(CALLBACK_PhysPointer(call_priv_io)+0x06,(Bit8u)0xcb); // retf
585
586 phys_writeb(CALLBACK_PhysPointer(call_priv_io)+0x08,(Bit8u)0xee); // out dx, al
587 phys_writeb(CALLBACK_PhysPointer(call_priv_io)+0x09,(Bit8u)0xcb); // retf
588 phys_writeb(CALLBACK_PhysPointer(call_priv_io)+0x0a,(Bit8u)0xef); // out dx, ax
589 phys_writeb(CALLBACK_PhysPointer(call_priv_io)+0x0b,(Bit8u)0xcb); // retf
590 phys_writeb(CALLBACK_PhysPointer(call_priv_io)+0x0c,(Bit8u)0x66); // out dx, eax
591 phys_writeb(CALLBACK_PhysPointer(call_priv_io)+0x0d,(Bit8u)0xef);
592 phys_writeb(CALLBACK_PhysPointer(call_priv_io)+0x0e,(Bit8u)0xcb); // retf
593 }