00001 #ifndef __IBMPC_H__
00002 #define __IBMPC_H__
00003
00008 #include <stdlib.h>
00009 #include <stdio.h>
00010 #include <string.h>
00011 #include <conio.h>
00012
00013 #include <vector>
00014 #include <deque>
00015 #include <string>
00016 #include <map>
00017
00018
00019
00021 namespace IBMPC
00022 {
00023 class Device;
00024 class VDD_DMA;
00025 class VDD_SYS_PPI_8255A;
00026
00062 class Memory
00063 {
00064 public:
00066 enum perm_t
00067 {
00068 PERM_READABLE = 0x0001,
00069 PERM_WRITEABLE = 0x0002,
00070 PAGE_ALLOCATED = 0x8000
00071 };
00072
00074 enum state_t
00075 {
00076 STATE_FAULT = 0x0001,
00077 STATE_DIR = 0x0002
00078 };
00079
00080
00081
00083 Memory();
00084
00086 ~Memory();
00087
00108 bool alloc_pagetable(unsigned int pages_count_pow, unsigned int page_size_pow);
00109
00110 unsigned int get_page_size()
00111 {
00112 return 1 << m_page_size_pow;
00113 }
00114
00115
00116
00127 void page_set_perm(unsigned int page_index, uint32 perm);
00128
00142 uint32 page_get_perm(unsigned int page_index);
00143
00154 bool page_alloc(unsigned int page_index);
00155
00166 void page_attach(unsigned int page_index, Device *driver);
00167
00175 void page_free(unsigned int page_index);
00176
00177
00178
00191 uint8 mem_read8(unsigned int addr);
00192
00203 void mem_write8(unsigned int addr, uint8 data);
00204
00205
00206
00220 uint32 get_state();
00221
00222 private:
00223
00225 struct page_t
00226 {
00228 uint32 flags;
00229
00231 union
00232 {
00233 Device *driver;
00234 uint8 *mem;
00235 };
00236 };
00237
00238 page_t *m_pages;
00239 unsigned int m_pages_count_pow;
00240 unsigned int m_page_size_pow;
00241 unsigned int m_addr_page_shift;
00242 unsigned int m_addr_page_mask;
00243 unsigned int m_addr_clamp_mask;
00244 uint32 m_state;
00245 };
00246
00273 class IO
00274 {
00275 public:
00276
00278 enum perm_t
00279 {
00280 PERM_READABLE = 0x0001,
00281 PERM_WRITEABLE = 0x0002,
00282 };
00283
00285 enum state_t
00286 {
00287 STATE_FAULT = 0x0001,
00288 STATE_DIR = 0x0002
00289 };
00290
00291
00292
00294 IO();
00295
00297 ~IO();
00298
00299
00300
00306 void port_set_perm(unsigned int port, uint32 perm);
00307
00314 uint32 port_get_perm(unsigned int port);
00315
00331 bool port_attach(unsigned int port_index, Device *device);
00332
00337 bool port_free(unsigned int port_index);
00338
00339
00340
00349 uint8 port_read8(unsigned int port_index);
00350
00358 void port_write8(unsigned int port_index, uint8 data);
00359
00360
00361
00373 uint32 get_state();
00374
00375 private:
00377 struct port_spec_t {
00378 Device *driver;
00379 uint32 flags;
00380 };
00381 std::map<unsigned int, port_spec_t> m_port_map;
00382 uint32 m_state;
00383 };
00384
00386 struct Core
00387 {
00388 friend class VirtualMachine;
00389
00390 #include "cpu/I8086/I8086.h"
00391
00392 i8086 cpu;
00393 Memory mem;
00394 IO io;
00396 VDD_DMA *dma;
00397 VDD_SYS_PPI_8255A *ppi;
00399 private:
00401 Core();
00402
00404 ~Core();
00405
00407 void attach(VDD_DMA *n_dma) { dma = n_dma; }
00408
00410 void attach(VDD_SYS_PPI_8255A *n_ppi) { ppi = n_ppi; }
00411 };
00412
00414 class Device
00415 {
00416 friend class VirtualMachine;
00417
00418 Core* m_core;
00419 std::string m_name;
00422 void set_core(Core *n_core) { m_core = n_core; }
00423
00424 public:
00425
00427 Device();
00428
00430 virtual ~Device();
00431
00433 Core *get_core() { return m_core; }
00434
00436 void set_name(std::string name);
00437
00439 std::string Device::get_name();
00440
00441
00442 virtual void init()=0;
00443 virtual void done()=0;
00445
00446 virtual uint8 mem_read8(unsigned int addr)=0;
00447 virtual void mem_write8(unsigned int addr, uint8 value)=0;
00449
00450 virtual uint8 port_read8(unsigned int port_index)=0;
00451 virtual void port_write8(unsigned int port_index, uint8 value)=0;
00454 virtual void irq_ack()=0;
00455 };
00456
00457
00459 class VirtualMachine
00460 {
00461 Core m_core;
00462 std::vector<Device *> m_drivers;
00463 const unsigned int m_mem_total_pages;
00464 bool m_was_error;
00465 public:
00470 VirtualMachine();
00471
00475 ~VirtualMachine();
00476
00480 void register_driver(Device *driver);
00481
00487 void unregister_driver(Device *driver);
00488
00490 int drivers_count();
00491
00496 Device* get_driver(int index);
00497
00501 Device* find_driver(std::string name);
00502
00504 void reset();
00505
00507 void run(unsigned int cycles);
00508
00510 bool was_error() const;
00511
00513 void set_debug(bool debug_on, bool debug_pause);
00514 };
00515
00516 void copy_to_mem(uint8 *buf, int buf_size, Memory &mem, unsigned int addr);
00517 };
00518
00519 #endif