1 /////////////////////////////////////////////////////////////////////////
2 // $Id: iodev.h 14293 2021-06-27 14:50:26Z vruppert $
3 /////////////////////////////////////////////////////////////////////////
4 //
5 //  Copyright (C) 2001-2021  The Bochs Project
6 //
7 //  I/O port handlers API Copyright (C) 2003 by Frank Cornelis
8 //
9 //  This library is free software; you can redistribute it and/or
10 //  modify it under the terms of the GNU Lesser General Public
11 //  License as published by the Free Software Foundation; either
12 //  version 2 of the License, or (at your option) any later version.
13 //
14 //  This library is distributed in the hope that it will be useful,
15 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 //  Lesser General Public License for more details.
18 //
19 //  You should have received a copy of the GNU Lesser General Public
20 //  License along with this library; if not, write to the Free Software
21 //  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
22 //
23 /////////////////////////////////////////////////////////////////////////
24 
25 #ifndef IODEV_H
26 #define IODEV_H
27 
28 #include "bochs.h"
29 #include "plugin.h"
30 #include "param_names.h"
31 #include "pc_system.h"
32 #include "bx_debug/debug.h"
33 #include "memory/memory-bochs.h"
34 #include "gui/siminterface.h"
35 #include "gui/gui.h"
36 
37 /* number of IRQ lines supported.  In an ISA PC there are two
38    PIC chips cascaded together.  each has 8 IRQ lines, so there
39    should be 16 IRQ's total */
40 #define BX_MAX_IRQS 16
41 
42 /* keyboard indicators */
43 #define BX_KBD_LED_NUM  0
44 #define BX_KBD_LED_CAPS 1
45 #define BX_KBD_LED_SCRL 2
46 #define BX_KBD_LED_MASK_NUM 1
47 #define BX_KBD_LED_MASK_ALL 7
48 
49 /* size of internal buffer for keyboard devices */
50 #define BX_KBD_ELEMENTS 16
51 
52 /* size of internal buffer for mouse devices */
53 #define BX_MOUSE_BUFF_SIZE 48
54 
55 /* maximum size of the ISA DMA buffer */
56 #define BX_DMA_BUFFER_SIZE 512
57 
58 #define BX_MAX_PCI_DEVICES 20
59 
60 typedef Bit32u (*bx_read_handler_t)(void *, Bit32u, unsigned);
61 typedef void   (*bx_write_handler_t)(void *, Bit32u, Bit32u, unsigned);
62 
63 typedef bool (*bx_kbd_gen_scancode_t)(void *, Bit32u);
64 typedef Bit8u (*bx_kbd_get_elements_t)(void *);
65 typedef void (*bx_mouse_enq_t)(void *, int, int, int, unsigned, bool);
66 typedef void (*bx_mouse_enabled_changed_t)(void *, bool);
67 
68 #if BX_USE_DEV_SMF
69 #  define BX_DEV_SMF  static
70 #  define BX_DEV_THIS bx_devices.
71 #else
72 #  define BX_DEV_SMF
73 #  define BX_DEV_THIS this->
74 #endif
75 
76 //////////////////////////////////////////////////////////////////////
77 // bx_devmodel_c declaration
78 //////////////////////////////////////////////////////////////////////
79 
80 // This class defines virtual methods that are common to all devices.
81 // Child classes do not need to implement all of them, because in this
82 // definition they are defined as empty, as opposed to being pure
83 // virtual (= 0).
84 class BOCHSAPI bx_devmodel_c : public logfunctions {
85   public:
~bx_devmodel_c()86   virtual ~bx_devmodel_c() {}
init(void)87   virtual void init(void) {}
reset(unsigned type)88   virtual void reset(unsigned type) {}
register_state(void)89   virtual void register_state(void) {}
after_restore_state(void)90   virtual void after_restore_state(void) {}
91 #if BX_DEBUGGER
debug_dump(int argc,char ** argv)92   virtual void debug_dump(int argc, char **argv) {}
93 #endif
94 };
95 
96 // forward declarations
97 class bx_list_c;
98 class device_image_t;
99 class cdrom_base_c;
100 
101 //////////////////////////////////////////////////////////////////////
102 // bx_pci_device_c declaration
103 //////////////////////////////////////////////////////////////////////
104 
105 #if BX_SUPPORT_PCI
106 
107 #define BX_DEBUG_PCI_READ(addr, value, io_len) \
108   if (io_len == 1) \
109     BX_DEBUG(("read  PCI register 0x%02X value 0x%02X (len=1)", address, value)); \
110   else if (io_len == 2) \
111     BX_DEBUG(("read  PCI register 0x%02X value 0x%04X (len=2)", address, value)); \
112   else if (io_len == 4) \
113     BX_DEBUG(("read  PCI register 0x%02X value 0x%08X (len=4)", address, value));
114 
115 #define BX_DEBUG_PCI_WRITE(addr, value, io_len) \
116   if (io_len == 1) \
117     BX_DEBUG(("write PCI register 0x%02X value 0x%02X (len=1)", addr, value)); \
118   else if (io_len == 2) \
119     BX_DEBUG(("write PCI register 0x%02X value 0x%04X (len=2)", addr, value)); \
120   else if (io_len == 4) \
121     BX_DEBUG(("write PCI register 0x%02X value 0x%08X (len=4)", addr, value));
122 
123 #define BX_PCI_BAR_TYPE_NONE 0
124 #define BX_PCI_BAR_TYPE_MEM  1
125 #define BX_PCI_BAR_TYPE_IO   2
126 
127 #define BX_PCI_ADVOPT_NOACPI 0x01
128 #define BX_PCI_ADVOPT_NOHPET 0x02
129 #define BX_PCI_ADVOPT_NOAGP  0x04
130 
131 typedef struct {
132   Bit8u  type;
133   Bit32u size;
134   Bit32u addr;
135   union {
136     struct {
137       memory_handler_t rh;
138       memory_handler_t wh;
139       const Bit8u *dummy;
140     } mem;
141     struct {
142       bx_read_handler_t rh;
143       bx_write_handler_t wh;
144       const Bit8u *mask;
145     } io;
146   };
147 } bx_pci_bar_t;
148 
149 class BOCHSAPI bx_pci_device_c : public bx_devmodel_c {
150 public:
bx_pci_device_c()151   bx_pci_device_c(): pci_rom(NULL), pci_rom_size(0) {
152     for (int i = 0; i < 6; i++) memset(&pci_bar[i], 0, sizeof(bx_pci_bar_t));
153   }
~bx_pci_device_c()154   virtual ~bx_pci_device_c() {
155     if (pci_rom != NULL) delete [] pci_rom;
156   }
157 
158   virtual Bit32u pci_read_handler(Bit8u address, unsigned io_len);
159   void pci_write_handler_common(Bit8u address, Bit32u value, unsigned io_len);
pci_write_handler(Bit8u address,Bit32u value,unsigned io_len)160   virtual void pci_write_handler(Bit8u address, Bit32u value, unsigned io_len) {}
pci_bar_change_notify(void)161   virtual void pci_bar_change_notify(void) {}
162 
163   void init_pci_conf(Bit16u vid, Bit16u did, Bit8u rev, Bit32u classc,
164                      Bit8u headt, Bit8u intpin);
165   void init_bar_io(Bit8u num, Bit16u size, bx_read_handler_t rh,
166                    bx_write_handler_t wh, const Bit8u *mask);
167   void init_bar_mem(Bit8u num, Bit32u size, memory_handler_t rh, memory_handler_t wh);
168   void register_pci_state(bx_list_c *list);
169   void after_restore_pci_state(memory_handler_t mem_read_handler);
170   void load_pci_rom(const char *path);
171 
set_name(const char * name)172   void set_name(const char *name) {pci_name = name;}
get_name(void)173   const char* get_name(void) {return pci_name;}
174 
175 protected:
176   const char *pci_name;
177   Bit8u pci_conf[256];
178   bx_pci_bar_t pci_bar[6];
179   Bit8u  *pci_rom;
180   Bit32u pci_rom_address;
181   Bit32u pci_rom_size;
182   memory_handler_t pci_rom_read_handler;
183 };
184 #endif
185 
186 //////////////////////////////////////////////////////////////////////
187 // declare stubs for devices
188 //////////////////////////////////////////////////////////////////////
189 
190 //////////////////////////////////////////////////////////////////////
191 #define STUBFUNC(dev,method) \
192    pluginlog->panic("%s called in %s stub. you must not have loaded the %s plugin", #dev, #method, #dev)
193 //////////////////////////////////////////////////////////////////////
194 
195 class BOCHSAPI bx_hard_drive_stub_c : public bx_devmodel_c {
196 public:
virt_read_handler(Bit32u address,unsigned io_len)197   virtual Bit32u virt_read_handler(Bit32u address, unsigned io_len) { return 0; }
virt_write_handler(Bit32u address,Bit32u value,unsigned io_len)198   virtual void virt_write_handler(Bit32u address, Bit32u value, unsigned io_len) {}
199 
bmdma_read_sector(Bit8u channel,Bit8u * buffer,Bit32u * sector_size)200   virtual bool bmdma_read_sector(Bit8u channel, Bit8u *buffer, Bit32u *sector_size) {
201     STUBFUNC(HD, bmdma_read_sector); return 0;
202   }
bmdma_write_sector(Bit8u channel,Bit8u * buffer)203   virtual bool bmdma_write_sector(Bit8u channel, Bit8u *buffer) {
204     STUBFUNC(HD, bmdma_write_sector); return 0;
205   }
bmdma_complete(Bit8u channel)206   virtual void bmdma_complete(Bit8u channel) {
207     STUBFUNC(HD, bmdma_complete);
208   }
209 };
210 
211 class BOCHSAPI bx_cmos_stub_c : public bx_devmodel_c {
212 public:
get_reg(Bit8u reg)213   virtual Bit32u get_reg(Bit8u reg) {
214     STUBFUNC(cmos, get_reg); return 0;
215   }
set_reg(Bit8u reg,Bit32u val)216   virtual void set_reg(Bit8u reg, Bit32u val) {
217     STUBFUNC(cmos, set_reg);
218   }
checksum_cmos(void)219   virtual void checksum_cmos(void) {
220     STUBFUNC(cmos, checksum);
221   }
enable_irq(bool enabled)222   virtual void enable_irq(bool enabled) {
223     STUBFUNC(cmos, enable_irq);
224   }
225 };
226 
227 class BOCHSAPI bx_pit_stub_c : public bx_devmodel_c {
228 public:
enable_irq(bool enabled)229   virtual void enable_irq(bool enabled) {
230     STUBFUNC(pit, enable_irq);
231   }
232 };
233 
234 class BOCHSAPI bx_dma_stub_c : public bx_devmodel_c {
235 public:
registerDMA8Channel(unsigned channel,Bit16u (* dmaRead)(Bit8u * data_byte,Bit16u maxlen),Bit16u (* dmaWrite)(Bit8u * data_byte,Bit16u maxlen),const char * name)236   virtual unsigned registerDMA8Channel(
237     unsigned channel,
238     Bit16u (* dmaRead)(Bit8u *data_byte, Bit16u maxlen),
239     Bit16u (* dmaWrite)(Bit8u *data_byte, Bit16u maxlen),
240     const char *name)
241   {
242     STUBFUNC(dma, registerDMA8Channel); return 0;
243   }
registerDMA16Channel(unsigned channel,Bit16u (* dmaRead)(Bit16u * data_word,Bit16u maxlen),Bit16u (* dmaWrite)(Bit16u * data_word,Bit16u maxlen),const char * name)244   virtual unsigned registerDMA16Channel(
245     unsigned channel,
246     Bit16u (* dmaRead)(Bit16u *data_word, Bit16u maxlen),
247     Bit16u (* dmaWrite)(Bit16u *data_word, Bit16u maxlen),
248     const char *name)
249   {
250     STUBFUNC(dma, registerDMA16Channel); return 0;
251   }
unregisterDMAChannel(unsigned channel)252   virtual unsigned unregisterDMAChannel(unsigned channel) {
253     STUBFUNC(dma, unregisterDMAChannel); return 0;
254   }
get_TC(void)255   virtual unsigned get_TC(void) {
256     STUBFUNC(dma, get_TC); return 0;
257   }
set_DRQ(unsigned channel,bool val)258   virtual void set_DRQ(unsigned channel, bool val) {
259     STUBFUNC(dma, set_DRQ);
260   }
raise_HLDA(void)261   virtual void raise_HLDA(void) {
262     STUBFUNC(dma, raise_HLDA);
263   }
264 };
265 
266 class BOCHSAPI bx_pic_stub_c : public bx_devmodel_c {
267 public:
raise_irq(unsigned irq_no)268   virtual void raise_irq(unsigned irq_no) {
269     STUBFUNC(pic, raise_irq);
270   }
lower_irq(unsigned irq_no)271   virtual void lower_irq(unsigned irq_no) {
272     STUBFUNC(pic, lower_irq);
273   }
set_mode(bool ma_sl,Bit8u mode)274   virtual void set_mode(bool ma_sl, Bit8u mode) {
275     STUBFUNC(pic, set_mode);
276   }
IAC(void)277   virtual Bit8u IAC(void) {
278     STUBFUNC(pic, IAC); return 0;
279   }
280 };
281 
282 class BOCHSAPI bx_vga_stub_c
283 #if BX_SUPPORT_PCI
284 : public bx_pci_device_c
285 #else
286 : public bx_devmodel_c
287 #endif
288 {
289 public:
vga_redraw_area(unsigned x0,unsigned y0,unsigned width,unsigned height)290   virtual void vga_redraw_area(unsigned x0, unsigned y0, unsigned width,
291                                unsigned height) {
292     STUBFUNC(vga, vga_redraw_area);
293   }
mem_read(bx_phy_address addr)294   virtual Bit8u mem_read(bx_phy_address addr) {
295     STUBFUNC(vga, mem_read);  return 0;
296   }
mem_write(bx_phy_address addr,Bit8u value)297   virtual void mem_write(bx_phy_address addr, Bit8u value) {
298     STUBFUNC(vga, mem_write);
299   }
get_text_snapshot(Bit8u ** text_snapshot,unsigned * txHeight,unsigned * txWidth)300   virtual void get_text_snapshot(Bit8u **text_snapshot,
301                                  unsigned *txHeight, unsigned *txWidth) {
302     STUBFUNC(vga, get_text_snapshot);
303   }
set_override(bool enabled,void * dev)304   virtual void set_override(bool enabled, void *dev) {
305     STUBFUNC(vga, set_override);
306   }
refresh_display(void * this_ptr,bool redraw)307   virtual void refresh_display(void *this_ptr, bool redraw) {
308     STUBFUNC(vga, refresh_display);
309   }
310 };
311 
312 class BOCHSAPI bx_speaker_stub_c : public bx_devmodel_c {
313 public:
beep_on(float frequency)314   virtual void beep_on(float frequency) {
315     bx_gui->beep_on(frequency);
316   }
beep_off()317   virtual void beep_off() {
318     bx_gui->beep_off();
319   }
set_line(bool level)320   virtual void set_line(bool level) {}
321 };
322 
323 #if BX_SUPPORT_PCI
324 class BOCHSAPI bx_pci2isa_stub_c : public bx_pci_device_c {
325 public:
pci_set_irq(Bit8u devfunc,unsigned line,bool level)326   virtual void pci_set_irq (Bit8u devfunc, unsigned line, bool level) {
327     STUBFUNC(pci2isa, pci_set_irq);
328   }
329 };
330 
331 class BOCHSAPI bx_pci_ide_stub_c : public bx_pci_device_c {
332 public:
bmdma_present(void)333   virtual bool bmdma_present(void) {
334     return 0;
335   }
bmdma_start_transfer(Bit8u channel)336   virtual void bmdma_start_transfer(Bit8u channel) {}
bmdma_set_irq(Bit8u channel)337   virtual void bmdma_set_irq(Bit8u channel) {}
338 };
339 
340 class BOCHSAPI bx_acpi_ctrl_stub_c : public bx_pci_device_c {
341 public:
generate_smi(Bit8u value)342   virtual void generate_smi(Bit8u value) {}
343 };
344 #endif
345 
346 #if BX_SUPPORT_IODEBUG
347 class BOCHSAPI bx_iodebug_stub_c : public bx_devmodel_c {
348 public:
mem_write(BX_CPU_C * cpu,bx_phy_address addr,unsigned len,void * data)349   virtual void mem_write(BX_CPU_C *cpu, bx_phy_address addr, unsigned len, void *data) {}
mem_read(BX_CPU_C * cpu,bx_phy_address addr,unsigned len,void * data)350   virtual void mem_read(BX_CPU_C *cpu, bx_phy_address addr, unsigned len, void *data) {}
351 };
352 #endif
353 
354 #if BX_SUPPORT_APIC
355 class BOCHSAPI bx_ioapic_stub_c : public bx_devmodel_c {
356 public:
set_enabled(bool enabled,Bit16u base_offset)357   virtual void set_enabled(bool enabled, Bit16u base_offset) {}
receive_eoi(Bit8u vector)358   virtual void receive_eoi(Bit8u vector) {}
set_irq_level(Bit8u int_in,bool level)359   virtual void set_irq_level(Bit8u int_in, bool level) {}
360 };
361 #endif
362 
363 #if BX_SUPPORT_GAMEPORT
364 class BOCHSAPI bx_game_stub_c : public bx_devmodel_c {
365 public:
set_enabled(bool val)366   virtual void set_enabled(bool val) {
367     STUBFUNC(gameport, set_enabled);
368   }
369 };
370 #endif
371 
372 class BOCHSAPI bx_devices_c : public logfunctions {
373 public:
374   bx_devices_c();
375  ~bx_devices_c();
376 
377   // Initialize the device stubs (in constructur and exit())
378   void init_stubs(void);
379   // Register I/O addresses and IRQ lines. Initialize any internal
380   // structures.  init() is called only once, even if the simulator
381   // reboots or is restarted.
382   void init(BX_MEM_C *);
383   // Enter reset state in response to a reset condition.
384   // The types of reset conditions are defined in bochs.h:
385   // power-on, hardware, or software.
386   void reset(unsigned type);
387   // Cleanup the devices when the simulation quits.
388   void exit(void);
389   void register_state(void);
390   void after_restore_state(void);
391   BX_MEM_C *mem;  // address space associated with these devices
392   bool register_io_read_handler(void *this_ptr, bx_read_handler_t f,
393                                 Bit32u addr, const char *name, Bit8u mask);
394   bool unregister_io_read_handler(void *this_ptr, bx_read_handler_t f,
395                                   Bit32u addr, Bit8u mask);
396   bool register_io_write_handler(void *this_ptr, bx_write_handler_t f,
397                                     Bit32u addr, const char *name, Bit8u mask);
398   bool unregister_io_write_handler(void *this_ptr, bx_write_handler_t f,
399                                    Bit32u addr, Bit8u mask);
400   bool register_io_read_handler_range(void *this_ptr, bx_read_handler_t f,
401                                       Bit32u begin_addr, Bit32u end_addr,
402                                       const char *name, Bit8u mask);
403   bool register_io_write_handler_range(void *this_ptr, bx_write_handler_t f,
404                                        Bit32u begin_addr, Bit32u end_addr,
405                                        const char *name, Bit8u mask);
406   bool unregister_io_read_handler_range(void *this_ptr, bx_read_handler_t f,
407                                         Bit32u begin, Bit32u end, Bit8u mask);
408   bool unregister_io_write_handler_range(void *this_ptr, bx_write_handler_t f,
409                                          Bit32u begin, Bit32u end, Bit8u mask);
410   bool register_default_io_read_handler(void *this_ptr, bx_read_handler_t f, const char *name, Bit8u mask);
411   bool register_default_io_write_handler(void *this_ptr, bx_write_handler_t f, const char *name, Bit8u mask);
412   bool register_irq(unsigned irq, const char *name);
413   bool unregister_irq(unsigned irq, const char *name);
414   Bit32u inp(Bit16u addr, unsigned io_len) BX_CPP_AttrRegparmN(2);
415   void   outp(Bit16u addr, Bit32u value, unsigned io_len) BX_CPP_AttrRegparmN(3);
416 
417   void register_default_keyboard(void *dev, bx_kbd_gen_scancode_t kbd_gen_scancode,
418                                  bx_kbd_get_elements_t kbd_get_elements);
419   void register_removable_keyboard(void *dev, bx_kbd_gen_scancode_t kbd_gen_scancode,
420                                    bx_kbd_get_elements_t kbd_get_elements,
421                                    Bit8u led_mask);
422   void unregister_removable_keyboard(void *dev);
423   void register_default_mouse(void *dev, bx_mouse_enq_t mouse_enq, bx_mouse_enabled_changed_t mouse_enabled_changed);
424   void register_removable_mouse(void *dev, bx_mouse_enq_t mouse_enq, bx_mouse_enabled_changed_t mouse_enabled_changed);
425   void unregister_removable_mouse(void *dev);
426   void gen_scancode(Bit32u key);
427   Bit8u kbd_get_elements(void);
428   void release_keys(void);
429   void paste_bytes(Bit8u *data, Bit32s length);
430   void kbd_set_indicator(Bit8u devid, Bit8u ledid, bool state);
431   void mouse_enabled_changed(bool enabled);
432   void mouse_motion(int delta_x, int delta_y, int delta_z, unsigned button_state, bool absxy);
433   void add_sound_device(void);
434   void remove_sound_device(void);
435 
436 #if BX_SUPPORT_PCI
pci_get_confAddr(void)437   Bit32u pci_get_confAddr(void) {return pci.confAddr;}
pci_get_slot_mapping(void)438   Bit32u pci_get_slot_mapping(void) {return pci.map_slot_to_dev;}
439   bool register_pci_handlers(bx_pci_device_c *device, Bit8u *devfunc,
440                              const char *name, const char *descr, Bit8u bus = 0);
441   bool pci_set_base_mem(void *this_ptr, memory_handler_t f1, memory_handler_t f2,
442                         Bit32u *addr, Bit8u *pci_conf, unsigned size);
443   bool pci_set_base_io(void *this_ptr, bx_read_handler_t f1, bx_write_handler_t f2,
444                        Bit32u *addr, Bit8u *pci_conf, unsigned size,
445                        const Bit8u *iomask, const char *name);
446 #endif
447   bool is_agp_present();
448 
449   static void timer_handler(void *);
450   void timer(void);
451 
452   bx_cmos_stub_c    *pluginCmosDevice;
453   bx_dma_stub_c     *pluginDmaDevice;
454   bx_hard_drive_stub_c *pluginHardDrive;
455   bx_pic_stub_c     *pluginPicDevice;
456   bx_pit_stub_c     *pluginPitDevice;
457   bx_speaker_stub_c *pluginSpeaker;
458   bx_vga_stub_c     *pluginVgaDevice;
459 #if BX_SUPPORT_IODEBUG
460   bx_iodebug_stub_c *pluginIODebug;
461 #endif
462 #if BX_SUPPORT_APIC
463   bx_ioapic_stub_c  *pluginIOAPIC;
464 #endif
465 #if BX_SUPPORT_GAMEPORT
466   bx_game_stub_c  *pluginGameport;
467 #endif
468 #if BX_SUPPORT_PCI
469   bx_pci2isa_stub_c *pluginPci2IsaBridge;
470   bx_pci_ide_stub_c *pluginPciIdeController;
471   bx_acpi_ctrl_stub_c *pluginACPIController;
472 #endif
473 
474   // stub classes that the pointers (above) can point to until a plugin is
475   // loaded
476   bx_cmos_stub_c stubCmos;
477   bx_dma_stub_c  stubDma;
478   bx_hard_drive_stub_c stubHardDrive;
479   bx_pic_stub_c  stubPic;
480   bx_pit_stub_c  stubPit;
481   bx_speaker_stub_c stubSpeaker;
482   bx_vga_stub_c  stubVga;
483 #if BX_SUPPORT_IODEBUG
484   bx_iodebug_stub_c stubIODebug;
485 #endif
486 #if BX_SUPPORT_APIC
487   bx_ioapic_stub_c stubIOAPIC;
488 #endif
489 #if BX_SUPPORT_GAMEPORT
490   bx_game_stub_c stubGameport;
491 #endif
492 #if BX_SUPPORT_PCI
493   bx_pci2isa_stub_c stubPci2Isa;
494   bx_pci_ide_stub_c stubPciIde;
495   bx_acpi_ctrl_stub_c stubACPIController;
496 #endif
497 
498   // Some info to pass to devices which can handled bulk IO.  This allows
499   // the interface to remain the same for IO devices which can't handle
500   // bulk IO.  We should probably implement special INPBulk() and OUTBulk()
501   // functions which stick these values in the bx_devices_c class, and
502   // then call the normal functions rather than having gross globals
503   // variables.
504   Bit8u*   bulkIOHostAddr;
505   unsigned bulkIOQuantumsRequested;
506   unsigned bulkIOQuantumsTransferred;
507 
508 private:
509 
510   struct io_handler_struct {
511     struct io_handler_struct *next;
512     struct io_handler_struct *prev;
513     void *funct; // C++ type checking is great, but annoying
514     void *this_ptr;
515     char *handler_name;  // name of device
516     int usage_count;
517     Bit8u mask;          // io_len mask
518   };
519   struct io_handler_struct io_read_handlers;
520   struct io_handler_struct io_write_handlers;
521 #define PORTS 0x10000
522   struct io_handler_struct **read_port_to_handler;
523   struct io_handler_struct **write_port_to_handler;
524 
525   // more for informative purposes, the names of the devices which
526   // are use each of the IRQ 0..15 lines are stored here
527   char *irq_handler_name[BX_MAX_IRQS];
528 
529   static Bit32u read_handler(void *this_ptr, Bit32u address, unsigned io_len);
530   static void   write_handler(void *this_ptr, Bit32u address, Bit32u value, unsigned io_len);
531   BX_DEV_SMF Bit32u read(Bit32u address, unsigned io_len);
532   BX_DEV_SMF void   write(Bit32u address, Bit32u value, unsigned io_len);
533 
534   static Bit32u default_read_handler(void *this_ptr, Bit32u address, unsigned io_len);
535   static void   default_write_handler(void *this_ptr, Bit32u address, Bit32u value, unsigned io_len);
536 
537   // runtime options / paste feature
538   static Bit64s param_handler(bx_param_c *param, bool set, Bit64s val);
539   void paste_delay_changed(Bit32u value);
540   void service_paste_buf();
541 
542   bool mouse_captured; // host mouse capture enabled
543   Bit8u mouse_type;
544   struct {
545     void *dev;
546     bx_mouse_enq_t enq_event;
547     bx_mouse_enabled_changed_t enabled_changed;
548   } bx_mouse[2];
549 
550   struct {
551     void *dev;
552     bx_kbd_gen_scancode_t gen_scancode;
553     bx_kbd_get_elements_t get_elements;
554     Bit8u led_mask;
555     bool bxkey_state[BX_KEY_NBKEYS];
556   } bx_keyboard[2];
557 
558   // The paste buffer does NOT exist in the hardware.  It is a bochs
559   // construction that allows the user to "paste" arbitrary length sequences of
560   // keystrokes into the emulated machine.  Since the hardware buffer is only
561   // 16 bytes, a very small amount of data can be added to the hardware buffer
562   // at a time.  The paste buffer keeps track of the bytes that have not yet
563   // been pasted.
564   //
565   // Lifetime of a paste buffer: The paste data comes from the system
566   // clipboard, which must be accessed using platform independent code in the
567   // gui.  Because every gui has its own way of managing the clipboard memory
568   // (in X windows, you're supposed to call Xfree for example), in the platform
569   // specific code we make a copy of the clipboard buffer with
570   // "new Bit8u[length]".  Then the pointer is passed into
571   // bx_device_c::paste_bytes, along with the length.  The gui code never touches
572   // the pastebuf again, and does not free it.  The devices code is
573   // responsible for deallocating the paste buffer using delete [] buf.  The
574   // paste buffer is binary data, and it is probably NOT null terminated.
575   //
576   // Summary: A paste buffer is allocated (new) in the platform-specific gui
577   // code, passed to the devices code, and is freed (delete[]) when it is no
578   // longer needed.
579   struct {
580     Bit8u *buf;     // ptr to bytes to be pasted, or NULL if none in progress
581     Bit32u buf_len; // length of pastebuf
582     Bit32u buf_ptr; // ptr to next byte to be added to hw buffer
583     Bit32u delay;   // number of timer events before paste
584     Bit32u counter; // count before paste
585     bool service;   // set to 1 when gen_scancode() is called from paste service
586     bool stop;      // stop the current paste operation on keypress or hardware reset
587   } paste;
588 
589   struct {
590     bool enabled;
591 #if BX_SUPPORT_PCI
592     Bit32u advopts;
593     Bit8u handler_id[0x101];  // 256 PCI devices/functions + 1 AGP device
594     struct {
595       bx_pci_device_c *handler;
596     } pci_handler[BX_MAX_PCI_DEVICES];
597     unsigned num_pci_handlers;
598 
599     Bit8u map_slot_to_dev;
600     bool slot_used[BX_N_PCI_SLOTS];
601 
602     Bit32u confAddr;
603 #endif
604   } pci;
605 
606   int timer_handle;
607   int statusbar_id[3];
608 
609   Bit8u sound_device_count;
610 
611   bool is_harddrv_enabled();
612 };
613 
614 // memory stub has an assumption that there are no memory accesses splitting 4K page
DEV_MEM_READ_PHYSICAL(bx_phy_address phy_addr,unsigned len,Bit8u * ptr)615 BX_CPP_INLINE void DEV_MEM_READ_PHYSICAL(bx_phy_address phy_addr, unsigned len, Bit8u *ptr)
616 {
617   unsigned remainingInPage = 0x1000 - (phy_addr & 0xfff);
618   if (len <= remainingInPage) {
619     BX_MEM(0)->readPhysicalPage(NULL, phy_addr, len, ptr);
620   }
621   else {
622     BX_MEM(0)->readPhysicalPage(NULL, phy_addr, remainingInPage, ptr);
623     ptr += remainingInPage;
624     phy_addr += remainingInPage;
625     len -= remainingInPage;
626     BX_MEM(0)->readPhysicalPage(NULL, phy_addr, len, ptr);
627   }
628 }
629 
DEV_MEM_READ_PHYSICAL_DMA(bx_phy_address phy_addr,unsigned len,Bit8u * ptr)630 BX_CPP_INLINE void DEV_MEM_READ_PHYSICAL_DMA(bx_phy_address phy_addr, unsigned len, Bit8u *ptr)
631 {
632   while(len > 0) {
633     unsigned remainingInPage = 0x1000 - (phy_addr & 0xfff);
634     if (len < remainingInPage) remainingInPage = len;
635     BX_MEM(0)->dmaReadPhysicalPage(phy_addr, remainingInPage, ptr);
636     ptr += remainingInPage;
637     phy_addr += remainingInPage;
638     len -= remainingInPage;
639   }
640 }
641 
642 // memory stub has an assumption that there are no memory accesses splitting 4K page
DEV_MEM_WRITE_PHYSICAL(bx_phy_address phy_addr,unsigned len,Bit8u * ptr)643 BX_CPP_INLINE void DEV_MEM_WRITE_PHYSICAL(bx_phy_address phy_addr, unsigned len, Bit8u *ptr)
644 {
645   unsigned remainingInPage = 0x1000 - (phy_addr & 0xfff);
646   if (len <= remainingInPage) {
647     BX_MEM(0)->writePhysicalPage(NULL, phy_addr, len, ptr);
648   }
649   else {
650     BX_MEM(0)->writePhysicalPage(NULL, phy_addr, remainingInPage, ptr);
651     ptr += remainingInPage;
652     phy_addr += remainingInPage;
653     len -= remainingInPage;
654     BX_MEM(0)->writePhysicalPage(NULL, phy_addr, len, ptr);
655   }
656 }
657 
DEV_MEM_WRITE_PHYSICAL_DMA(bx_phy_address phy_addr,unsigned len,Bit8u * ptr)658 BX_CPP_INLINE void DEV_MEM_WRITE_PHYSICAL_DMA(bx_phy_address phy_addr, unsigned len, Bit8u *ptr)
659 {
660   while(len > 0) {
661     unsigned remainingInPage = 0x1000 - (phy_addr & 0xfff);
662     if (len < remainingInPage) remainingInPage = len;
663     BX_MEM(0)->dmaWritePhysicalPage(phy_addr, remainingInPage, ptr);
664     ptr += remainingInPage;
665     phy_addr += remainingInPage;
666     len -= remainingInPage;
667   }
668 }
669 
670 BOCHSAPI extern bx_devices_c bx_devices;
671 
672 #endif /* IODEV_H */
673