Annotation of msdos-player/source/msdos.h, revision 1.1.1.21

1.1       root        1: /*
                      2:        MS-DOS Player for Win32 console
                      3: 
                      4:        Author : Takeda.Toshiya
                      5:        Date   : 2009.11.09-
                      6: */
                      7: 
                      8: #ifndef _MSDOS_H_
                      9: #define _MSDOS_H_
                     10: 
1.1.1.12  root       11: #ifndef _WIN32_WINNT
                     12: #define _WIN32_WINNT 0x501
                     13: #endif
1.1       root       14: #include <windows.h>
                     15: #include <winioctl.h>
                     16: #include <tchar.h>
1.1.1.13  root       17: #include <stddef.h>
1.1       root       18: #include <stdlib.h>
                     19: #include <stdio.h>
1.1.1.2   root       20: #include <assert.h>
1.1       root       21: #include <conio.h>
1.1.1.2   root       22: #include <math.h>
1.1       root       23: #include <dos.h>
                     24: #include <fcntl.h>
                     25: #include <sys/types.h>
                     26: #include <sys/stat.h>
                     27: #include <io.h>
                     28: #include <sys/locking.h>
                     29: #include <mbctype.h>
                     30: #include <direct.h>
                     31: #include <errno.h>
1.1.1.9   root       32: #include <tlhelp32.h>
                     33: #include <psapi.h>
1.1.1.20  root       34: #include <setupapi.h>
1.1       root       35: 
1.1.1.15  root       36: #ifdef _DEBUG
                     37: #define _CRTDBG_MAP_ALLOC
                     38: #include <crtdbg.h>
                     39: #define calloc(c, s) _calloc_dbg(c, s, _NORMAL_BLOCK, __FILE__, __LINE__)
                     40: #define malloc(s) _malloc_dbg(s, _NORMAL_BLOCK, __FILE__, __LINE__)
                     41: #define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
                     42: #endif
                     43: 
1.1.1.9   root       44: // variable scope of 'for' loop for Microsoft Visual C++ 6.0
                     45: #if defined(_MSC_VER) && (_MSC_VER == 1200)
1.1       root       46: #define for if(0);else for
                     47: #endif
1.1.1.9   root       48: 
                     49: // disable warnings for Microsoft Visual C++ 2005 or later
1.1       root       50: #if defined(_MSC_VER) && (_MSC_VER >= 1400)
                     51: #pragma warning( disable : 4819 )
                     52: #pragma warning( disable : 4995 )
                     53: #pragma warning( disable : 4996 )
1.1.1.9   root       54: // for MAME i86/i386
                     55: #pragma warning( disable : 4018 )
                     56: #pragma warning( disable : 4065 )
                     57: #pragma warning( disable : 4146 )
                     58: #pragma warning( disable : 4244 )
                     59: #pragma warning( disable : 4267 )
                     60: #endif
                     61: 
                     62: // endian
                     63: #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
                     64:        #if defined(__BYTE_ORDER) && (defined(__LITTLE_ENDIAN) || defined(__BIG_ENDIAN))
                     65:                #if __BYTE_ORDER == __LITTLE_ENDIAN
                     66:                        #define __LITTLE_ENDIAN__
                     67:                #elif __BYTE_ORDER == __BIG_ENDIAN
                     68:                        #define __BIG_ENDIAN__
                     69:                #endif
                     70:        #elif defined(WORDS_LITTLEENDIAN)
                     71:                #define __LITTLE_ENDIAN__
                     72:        #elif defined(WORDS_BIGENDIAN)
                     73:                #define __BIG_ENDIAN__
                     74:        #endif
                     75: #endif
                     76: #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
                     77:        // Microsoft Visual C++
                     78:        #define __LITTLE_ENDIAN__
1.1       root       79: #endif
1.1.1.9   root       80: 
1.1       root       81: // compat for mingw32 headers
                     82: #ifndef COMMON_LVB_UNDERSCORE
                     83: #define COMMON_LVB_UNDERSCORE 0x8000
                     84: #endif
                     85: 
                     86: // type definition
                     87: #ifndef UINT8
                     88: typedef unsigned char UINT8;
                     89: #endif
                     90: #ifndef UINT16
                     91: typedef unsigned short UINT16;
                     92: #endif
                     93: #ifndef UINT32
                     94: typedef unsigned int UINT32;
                     95: #endif
                     96: #ifndef INT8
                     97: typedef signed char INT8;
                     98: #endif
                     99: #ifndef INT16
                    100: typedef signed short INT16;
                    101: #endif
                    102: #ifndef INT32
                    103: typedef signed int INT32;
                    104: #endif
                    105: 
                    106: #pragma pack(1)
                    107: typedef union {
                    108:        UINT32 dw;
                    109:        struct {
1.1.1.9   root      110: #ifdef __BIG_ENDIAN__
                    111:                UINT16 h, l;
                    112: #else
1.1       root      113:                UINT16 l, h;
1.1.1.9   root      114: #endif
1.1       root      115:        } w;
                    116: } PAIR32;
                    117: #pragma pack()
                    118: 
                    119: /* ----------------------------------------------------------------------------
                    120:        FIFO buffer
                    121: ---------------------------------------------------------------------------- */
                    122: 
                    123: class FIFO
                    124: {
                    125: private:
1.1.1.20  root      126:        int size;
                    127:        int *buf;
1.1       root      128:        int cnt, rpt, wpt, stored[3];
                    129: public:
1.1.1.20  root      130:        FIFO(int s) {
                    131:                size = s;
                    132:                buf = (int *)malloc(size * sizeof(int));
1.1       root      133:                cnt = rpt = wpt = 0;
                    134:        }
1.1.1.20  root      135:        void release()
                    136:        {
                    137:                free(buf);
                    138:        }
1.1       root      139:        void write(int val) {
1.1.1.20  root      140:                if(cnt < size) {
1.1       root      141:                        buf[wpt++] = val;
1.1.1.20  root      142:                        if(wpt >= size) {
1.1       root      143:                                wpt = 0;
                    144:                        }
                    145:                        cnt++;
                    146:                }
                    147:        }
                    148:        int read() {
                    149:                int val = 0;
                    150:                if(cnt) {
                    151:                        val = buf[rpt++];
1.1.1.20  root      152:                        if(rpt >= size) {
1.1       root      153:                                rpt = 0;
                    154:                        }
                    155:                        cnt--;
                    156:                }
1.1.1.20  root      157:                return(val);
1.1       root      158:        }
                    159:        int count() {
1.1.1.20  root      160:                return(cnt);
                    161:        }
                    162:        int remain() {
                    163:                return(size - cnt);
                    164:        }
                    165:        bool full()
                    166:        {
                    167:                return(cnt == size);
                    168:        }
                    169:        bool empty()
                    170:        {
                    171:                return(cnt == 0);
1.1       root      172:        }
                    173:        void store_buffer() {
                    174:                stored[0] = cnt;
                    175:                stored[1] = rpt;
                    176:                stored[2] = wpt;
                    177:        }
                    178:        void restore_buffer() {
                    179:                cnt = stored[0];
                    180:                rpt = stored[1];
                    181:                wpt = stored[2];
                    182:        }
                    183: };
                    184: 
                    185: /* ----------------------------------------------------------------------------
1.1.1.14  root      186:        MAME i86/i386
                    187: ---------------------------------------------------------------------------- */
                    188: 
                    189: #if defined(HAS_I86)
                    190:        #define CPU_MODEL i8086
                    191: #elif defined(HAS_I186)
                    192:        #define CPU_MODEL i80186
                    193: #elif defined(HAS_V30)
                    194:        #define CPU_MODEL v30
                    195: #elif defined(HAS_I286)
                    196:        #define CPU_MODEL i80286
                    197: #elif defined(HAS_I386)
                    198:        #define CPU_MODEL i386
                    199: #else
                    200: //     #if defined(HAS_I386SX)
                    201: //             #define CPU_MODEL i386SX
                    202: //     #else
                    203:                #if defined(HAS_I486)
                    204:                        #define CPU_MODEL i486
                    205:                #else
                    206:                        #if defined(HAS_PENTIUM)
                    207:                                #define CPU_MODEL pentium
                    208:                        #elif defined(HAS_MEDIAGX)
                    209:                                #define CPU_MODEL mediagx
                    210:                        #elif defined(HAS_PENTIUM_PRO)
                    211:                                #define CPU_MODEL pentium_pro
                    212:                        #elif defined(HAS_PENTIUM_MMX)
                    213:                                #define CPU_MODEL pentium_mmx
                    214:                        #elif defined(HAS_PENTIUM2)
                    215:                                #define CPU_MODEL pentium2
                    216:                        #elif defined(HAS_PENTIUM3)
                    217:                                #define CPU_MODEL pentium3
                    218:                        #elif defined(HAS_PENTIUM4)
                    219:                                #define CPU_MODEL pentium4
                    220:                        #endif
                    221:                        #define SUPPORT_RDTSC
                    222:                #endif
                    223:                #define SUPPORT_FPU
                    224: //     #endif
                    225:        #define HAS_I386
                    226: #endif
                    227: 
                    228: /* ----------------------------------------------------------------------------
                    229:        PC/AT hardware emulation
                    230: ---------------------------------------------------------------------------- */
                    231: 
                    232: void hardware_init();
                    233: void hardware_finish();
                    234: void hardware_run();
                    235: void hardware_update();
                    236: 
                    237: // memory
                    238: 
                    239: #if defined(HAS_I386)
                    240: #define MAX_MEM 0x2000000      /* 32MB */
                    241: #elif defined(HAS_I286)
                    242: #define MAX_MEM 0x1000000      /* 16MB */
                    243: #else
                    244: #define MAX_MEM 0x100000       /* 1MB */
                    245: #endif
                    246: UINT8 mem[MAX_MEM + 3];
                    247: 
                    248: // ems
                    249: 
                    250: #define MAX_EMS_HANDLES 16
                    251: #define MAX_EMS_PAGES 2048     /* 32MB */
                    252: 
1.1.1.21! root      253: typedef struct {
1.1.1.14  root      254:        char name[8];
                    255:        UINT8* buffer;
                    256:        int pages;
                    257:        bool allocated;
1.1.1.21! root      258: } ems_handle_t;
1.1.1.14  root      259: 
1.1.1.21! root      260: typedef struct {
1.1.1.14  root      261:        UINT16 handle;
                    262:        UINT16 page;
                    263:        bool mapped;
1.1.1.21! root      264: } ems_page_t;
1.1.1.14  root      265: 
1.1.1.21! root      266: ems_handle_t ems_handles[MAX_EMS_HANDLES];
        !           267: ems_page_t ems_pages[4];
1.1.1.14  root      268: int free_ems_pages;
                    269: 
                    270: void ems_init();
                    271: void ems_finish();
                    272: void ems_allocate_pages(int handle, int pages);
                    273: void ems_reallocate_pages(int handle, int pages);
                    274: void ems_release_pages(int handle);
                    275: void ems_map_page(int physical, int handle, int logical);
                    276: void ems_unmap_page(int physical);
                    277: 
                    278: // xms
                    279: 
                    280: #if defined(HAS_I286) || defined(HAS_I386)
                    281: #define SUPPORT_XMS
                    282: #endif
                    283: 
                    284: #ifdef SUPPORT_XMS
                    285: #define MAX_XMS_HANDLES 16
                    286: #define MAX_XMS_SIZE_KB 0x8000 // 32MB
                    287: 
1.1.1.21! root      288: typedef struct {
1.1.1.14  root      289:        int seg;
                    290:        int size_kb;
                    291:        int lock;
                    292:        bool allocated;
1.1.1.21! root      293: } xms_handle_t;
1.1.1.16  root      294: 
1.1.1.21! root      295: xms_handle_t xms_handles[MAX_XMS_HANDLES + 1];
        !           296: int xms_a20_local_enb_count;
1.1.1.14  root      297: #endif
                    298: 
1.1.1.20  root      299: // dma
                    300: 
                    301: typedef struct {
                    302:        struct {
                    303:                union {
                    304:                        UINT16 w;
                    305:                        struct {
                    306: #ifdef __BIG_ENDIAN__
                    307:                                UINT8 h, l;
                    308: #else
                    309:                                UINT8 l, h;
                    310: #endif
                    311:                        } b;
                    312:                } areg, creg, bareg, bcreg;
                    313:                UINT8 mode;
                    314:                UINT8 pagereg;
                    315:                UINT32 port;
                    316:        } ch[4];
                    317:        
                    318:        bool low_high;
                    319:        UINT8 cmd;
                    320:        UINT8 req;
                    321:        UINT8 mask;
                    322:        UINT8 tc;
                    323:        UINT16 tmp;
                    324: } dma_t;
                    325: 
1.1.1.21! root      326: dma_t dma[2];
1.1.1.20  root      327: 
                    328: void dma_init();
                    329: void dma_reset(int c);
                    330: void dma_write(int c, UINT32 addr, UINT8 data);
                    331: UINT8 dma_read(int c, UINT32 addr);
                    332: void dma_page_write(int c, int ch, UINT8 data);
                    333: UINT8 dma_page_read(int c, int ch);
                    334: void dma_run(int c, int ch);
                    335: 
1.1.1.14  root      336: // pic
                    337: 
                    338: typedef struct {
                    339:        UINT8 imr, isr, irr, prio;
                    340:        UINT8 icw1, icw2, icw3, icw4;
                    341:        UINT8 ocw3;
                    342:        UINT8 icw2_r, icw3_r, icw4_r;
                    343: } pic_t;
                    344: 
                    345: pic_t pic[2];
                    346: int pic_req_chip, pic_req_level;
                    347: UINT8 pic_req_bit;
                    348: 
                    349: void pic_init();
                    350: void pic_write(int c, UINT32 addr, UINT8 data);
                    351: UINT8 pic_read(int c, UINT32 addr);
                    352: void pic_req(int c, int level, int signal);
                    353: int pic_ack();
                    354: void pic_update();
                    355: 
1.1.1.20  root      356: // pio
                    357: 
                    358: typedef struct {
                    359:        UINT8 data, stat, ctrl;
                    360: } pio_t;
                    361: 
1.1.1.21! root      362: pio_t pio[2];
1.1.1.20  root      363: 
                    364: void pio_init();
                    365: void pio_write(int c, UINT32 addr, UINT8 data);
                    366: UINT8 pio_read(int c, UINT32 addr);
                    367: 
1.1.1.14  root      368: // pit
                    369: 
                    370: #define PIT_ALWAYS_RUNNING
                    371: 
                    372: typedef struct {
                    373:        INT32 count;
                    374:        UINT16 latch;
1.1.1.21! root      375:        UINT16 prev_latch, next_latch;
1.1.1.14  root      376:        UINT16 count_reg;
                    377:        UINT8 ctrl_reg;
                    378:        int count_latched;
                    379:        int low_read, high_read;
                    380:        int low_write, high_write;
                    381:        int mode;
                    382:        int status_latched;
                    383:        UINT8 status;
                    384:        // constant clock
                    385:        UINT32 expired_time;
                    386:        UINT32 prev_time;
1.1.1.17  root      387:        UINT64 accum;
1.1.1.14  root      388: } pit_t;
                    389: 
                    390: pit_t pit[3];
                    391: #ifndef PIT_ALWAYS_RUNNING
                    392: int pit_active;
                    393: #endif
                    394: 
                    395: void pit_init();
                    396: void pit_write(int ch, UINT8 val);
                    397: UINT8 pit_read(int ch);
                    398: int pit_run(int ch, UINT32 cur_time);
                    399: void pit_latch_count(int ch);
                    400: int pit_get_expired_time(int ch);
                    401: 
                    402: UINT8 system_port = 0;
                    403: 
1.1.1.20  root      404: // sio
                    405: 
                    406: #define SIO_BUFFER_SIZE 1024
                    407: 
                    408: typedef struct {
                    409:        int channel;
                    410:        
                    411:        FIFO *send_buffer;
                    412:        FIFO *recv_buffer;
                    413:        
                    414:        union {
                    415:                UINT16 w;
                    416:                struct {
                    417: #ifdef __BIG_ENDIAN__
                    418:                        UINT8 h, l;
                    419: #else
                    420:                        UINT8 l, h;
                    421: #endif
                    422:                } b;
                    423:        } divisor;
                    424:        UINT16 prev_divisor;
                    425:        UINT8 line_ctrl, prev_line_ctrl;
                    426:        UINT8 selector;
                    427:        
                    428:        UINT8 modem_ctrl;
1.1.1.21! root      429:        bool set_brk, set_rts, set_dtr;
        !           430:        bool prev_set_brk;
        !           431: //     bool prev_set_rts, prev_set_dtr;
1.1.1.20  root      432:        
                    433:        UINT8 line_stat_buf, line_stat_err;
                    434:        UINT8 modem_stat, prev_modem_stat;
                    435:        
                    436:        UINT8 irq_enable;
                    437:        UINT8 irq_identify;
                    438:        
                    439:        UINT8 scratch;
                    440: } sio_t;
                    441: 
                    442: typedef struct {
                    443:        HANDLE hThread;
                    444:        CRITICAL_SECTION csSendData;
                    445:        CRITICAL_SECTION csRecvData;
                    446:        CRITICAL_SECTION csLineCtrl;
                    447:        CRITICAL_SECTION csLineStat;
                    448:        CRITICAL_SECTION csModemCtrl;
                    449:        CRITICAL_SECTION csModemStat;
                    450: } sio_mt_t;
                    451: 
1.1.1.21! root      452: sio_t sio[2];
        !           453: sio_mt_t sio_mt[2];
1.1.1.20  root      454: 
                    455: void sio_init();
                    456: void sio_finish();
                    457: void sio_write(int c, UINT32 addr, UINT8 data);
                    458: UINT8 sio_read(int c, UINT32 addr);
                    459: void sio_update(int c);
                    460: void sio_update_irq(int c);
                    461: DWORD WINAPI sio_thread(void *lpx);
                    462: 
1.1.1.14  root      463: // cmos
                    464: 
                    465: UINT8 cmos[128];
                    466: UINT8 cmos_addr;
                    467: 
                    468: void cmos_init();
                    469: void cmos_write(int addr, UINT8 val);
                    470: UINT8 cmos_read(int addr);
                    471: 
                    472: // kbd (a20)
                    473: 
                    474: UINT8 kbd_data;
                    475: UINT8 kbd_status;
                    476: UINT8 kbd_command;
                    477: 
                    478: void kbd_init();
                    479: UINT8 kbd_read_data();
                    480: void kbd_write_data(UINT8 val);
                    481: UINT8 kbd_read_status();
                    482: void kbd_write_command(UINT8 val);
                    483: 
                    484: /* ----------------------------------------------------------------------------
1.1       root      485:        MS-DOS virtual machine
                    486: ---------------------------------------------------------------------------- */
                    487: 
                    488: #define VECTOR_TOP     0
                    489: #define VECTOR_SIZE    0x400
                    490: #define BIOS_TOP       (VECTOR_TOP + VECTOR_SIZE)
                    491: #define BIOS_SIZE      0x100
                    492: #define WORK_TOP       (BIOS_TOP + BIOS_SIZE)
1.1.1.14  root      493: #define WORK_SIZE      0x200
1.1.1.20  root      494: // IO.SYS 0070:0000
                    495: #define DEVICE_TOP     (WORK_TOP + WORK_SIZE)
1.1.1.21! root      496: #define DEVICE_SIZE    0x100   /* 22 + 18 * 12 + 7 */
1.1.1.20  root      497: #define DOS_INFO_TOP   (DEVICE_TOP + DEVICE_SIZE)
1.1       root      498: #define DOS_INFO_SIZE  0x100
1.1.1.20  root      499: #define EXT_BIOS_TOP   (DOS_INFO_TOP + DOS_INFO_SIZE)
                    500: #define EXT_BIOS_SIZE  0x400
                    501: #define DPB_TOP                (EXT_BIOS_TOP + EXT_BIOS_SIZE)
1.1       root      502: #define DPB_SIZE       0x400
1.1.1.16  root      503: #define SFT_TOP                (DPB_TOP + DPB_SIZE)
                    504: #define SFT_SIZE       0x4b0   /* 6 + 0x3b * 20 */
                    505: #define DISK_BUF_TOP   (SFT_TOP + SFT_SIZE)
1.1.1.14  root      506: #define DISK_BUF_SIZE  0x20
                    507: #define CDS_TOP                (DISK_BUF_TOP + DISK_BUF_SIZE)
1.1       root      508: #define CDS_SIZE       0x80
                    509: #define FCB_TABLE_TOP  (CDS_TOP + CDS_SIZE)
                    510: #define FCB_TABLE_SIZE 0x10
1.1.1.17  root      511: #define ERR_TABLE_TOP  (FCB_TABLE_TOP + FCB_TABLE_SIZE)
                    512: #define ERR_TABLE_SIZE 0x10
1.1.1.18  root      513: #define SDA_TOP                (ERR_TABLE_TOP + ERR_TABLE_SIZE)
                    514: #define SDA_SIZE       0xb0
1.1.1.13  root      515: // nls tables
1.1.1.18  root      516: #define UPPERTABLE_TOP (SDA_TOP + SDA_SIZE)
1.1.1.13  root      517: #define UPPERTABLE_SIZE        0x82
1.1.1.18  root      518: #define LOWERTABLE_TOP (UPPERTABLE_TOP + UPPERTABLE_SIZE)
                    519: #define LOWERTABLE_SIZE        0x82
                    520: #define FILENAME_UPPERTABLE_TOP (LOWERTABLE_TOP + LOWERTABLE_SIZE)
1.1.1.13  root      521: #define FILENAME_UPPERTABLE_SIZE 0x82
                    522: #define FILENAME_TERMINATOR_TOP (FILENAME_UPPERTABLE_TOP + FILENAME_UPPERTABLE_SIZE)
                    523: #define FILENAME_TERMINATOR_SIZE 0x20  /* requirement: 10 + 14(terminate chars) */
                    524: #define COLLATING_TABLE_TOP (FILENAME_TERMINATOR_TOP + FILENAME_TERMINATOR_SIZE)
                    525: #define COLLATING_TABLE_SIZE 0x102
                    526: #define DBCS_TOP       (COLLATING_TABLE_TOP + COLLATING_TABLE_SIZE)
1.1       root      527: #define DBCS_TABLE     (DBCS_TOP + 2)
                    528: #define DBCS_SIZE      0x10
1.1.1.13  root      529: #define MSDOS_SYSTEM_DATA_END (DBCS_TOP + DBCS_SIZE)
                    530: #define MEMORY_TOP     ((MSDOS_SYSTEM_DATA_END + 15) & ~15U)
1.1.1.7   root      531: #define MEMORY_END     0xb8000
                    532: #define TEXT_VRAM_TOP  0xb8000
1.1.1.14  root      533: #define EMS_TOP                0xc0000
                    534: #define EMS_SIZE       0x10000
                    535: UINT32 UMB_TOP = EMS_TOP; // EMS is disabled
1.1.1.7   root      536: #define UMB_END                0xf8000
                    537: #define SHADOW_BUF_TOP 0xf8000
1.1.1.14  root      538: #define EMB_TOP                0x10fff0
                    539: #define EMB_END                MAX_MEM
                    540: 
                    541: UINT32 IRET_TOP = 0;
                    542: #define IRET_SIZE      0x100
                    543: UINT32 XMS_TOP = 0;
                    544: #define XMS_SIZE       0x20
1.1       root      545: 
                    546: //#define ENV_SIZE     0x800
                    547: #define ENV_SIZE       0x2000
                    548: #define PSP_SIZE       0x100
                    549: 
                    550: #define MAX_FILES      128
                    551: #define MAX_PROCESS    16
1.1.1.12  root      552: #define MAX_DTAINFO    128
1.1.1.11  root      553: #define LFN_DTA_LADDR  0x10FFF0
1.1.1.12  root      554: #define FIND_MAGIC     0x46696e64
1.1       root      555: 
1.1.1.16  root      556: #define DUP_STDIN      27
                    557: #define DUP_STDOUT     28
                    558: #define DUP_STDERR     29
                    559: #define DUP_STDAUX     30
                    560: #define DUP_STDPRN     31
1.1       root      561: 
1.1.1.16  root      562: //#define MAP_AUX_DEVICE_TO_FILE
                    563: //#define MAP_PRN_DEVICE_TO_FILE
1.1       root      564: 
                    565: #pragma pack(1)
                    566: typedef struct {
                    567:        UINT8 mz;
                    568:        UINT16 psp;
1.1.1.14  root      569:        union {
                    570:                UINT8 reserved[5];
                    571:                UINT16 paragraphs16;
                    572:                UINT32 paragraphs32;
                    573:        };
1.1.1.3   root      574:        char prog_name[8];
1.1.1.14  root      575:        // use 32bit paragraphs for emb
                    576:        UINT32 paragraphs()
                    577:        {
                    578:                UINT32 offset = &(this->mz) - mem;
                    579:                return (offset >= EMB_TOP) ? paragraphs32 : paragraphs16;
                    580:        }
                    581:        UINT32 size_kb()
                    582:        {
                    583:                return paragraphs() >> 6;
                    584:        }
1.1       root      585: } mcb_t;
                    586: #pragma pack()
                    587: 
                    588: #pragma pack(1)
                    589: typedef struct {
                    590:        UINT16 env_seg;
                    591:        PAIR32 cmd_line;
                    592:        PAIR32 fcb1;
                    593:        PAIR32 fcb2;
                    594:        UINT16 sp;
                    595:        UINT16 ss;
                    596:        UINT16 ip;
                    597:        UINT16 cs;
                    598: } param_block_t;
                    599: #pragma pack()
                    600: 
                    601: #pragma pack(1)
                    602: typedef struct {
                    603:        UINT8 len;
                    604:        char cmd[127];
                    605: } cmd_line_t;
                    606: #pragma pack()
                    607: 
                    608: #pragma pack(1)
                    609: typedef struct {
                    610:        UINT8 exit[2];
                    611:        UINT16 first_mcb;
                    612:        UINT8 reserved_1;
                    613:        UINT8 far_call;
                    614:        PAIR32 cpm_entry;
                    615:        PAIR32 int_22h;
                    616:        PAIR32 int_23h;
                    617:        PAIR32 int_24h;
                    618:        UINT16 parent_psp;
                    619:        UINT8 file_table[20];
                    620:        UINT16 env_seg;
                    621:        PAIR32 stack;
1.1.1.12  root      622:        UINT16 file_table_size;
                    623:        PAIR32 file_table_ptr;
                    624:        UINT8 reserved_2[24];
1.1       root      625:        UINT8 service[3];
                    626:        UINT8 reserved_3[2];
                    627:        UINT8 ex_fcb[7];
                    628:        UINT8 fcb1[16];
                    629:        UINT8 fcb2[20];
                    630:        UINT8 buffer[128];
                    631: } psp_t;
                    632: #pragma pack()
                    633: 
                    634: #pragma pack(1)
                    635: typedef struct {
                    636:        UINT16 mz;
                    637:        UINT16 extra_bytes;
                    638:        UINT16 pages;
                    639:        UINT16 relocations;
                    640:        UINT16 header_size;
                    641:        UINT16 min_alloc;
                    642:        UINT16 max_alloc;
                    643:        UINT16 init_ss;
                    644:        UINT16 init_sp;
                    645:        UINT16 check_sum;
                    646:        UINT16 init_ip;
                    647:        UINT16 init_cs;
                    648:        UINT16 relocation_table;
                    649:        UINT16 overlay;
                    650: } exe_header_t;
                    651: #pragma pack()
                    652: 
                    653: #pragma pack(1)
                    654: typedef struct {
                    655:        UINT8 flag;
                    656:        UINT8 reserved[5];
                    657:        UINT8 attribute;
                    658: } ext_fcb_t;
                    659: #pragma pack()
                    660: 
                    661: #pragma pack(1)
                    662: typedef struct {
                    663:        UINT8 drive;
                    664:        UINT8 file_name[8 + 3];
                    665:        UINT16 current_block;
                    666:        UINT16 record_size;
                    667:        UINT32 file_size;
                    668:        UINT16 date;
                    669:        UINT16 time;
1.1.1.12  root      670:        union {
                    671:                UINT8 reserved[8];
                    672:                HANDLE handle;
                    673:        };
1.1       root      674:        UINT8 cur_record;
                    675:        UINT32 rand_record;
                    676: } fcb_t;
                    677: #pragma pack()
                    678: 
                    679: #pragma pack(1)
                    680: typedef struct {
                    681:        UINT8 drive;
                    682:        UINT8 file_name[8 + 3];
                    683:        UINT8 attribute;
                    684:        UINT8 nt_res;
                    685:        UINT8 create_time_ms;
                    686:        UINT16 creation_time;
                    687:        UINT16 creation_date;
                    688:        UINT16 last_access_date;
                    689:        UINT16 cluster_hi;
                    690:        UINT16 last_write_time;
                    691:        UINT16 last_write_date;
                    692:        UINT16 cluster_lo;
                    693:        UINT32 file_size;
                    694: } find_fcb_t;
                    695: #pragma pack()
                    696: 
                    697: #pragma pack(1)
                    698: typedef struct {
1.1.1.12  root      699:        union {
                    700:                UINT8 reserved[21];
                    701:                struct {
                    702:                        UINT32 find_magic;
                    703:                        UINT32 dta_index;
                    704:                };
                    705:        };
1.1       root      706:        UINT8 attrib;
                    707:        UINT16 time;
                    708:        UINT16 date;
                    709:        UINT32 size;
                    710:        char name[13];
                    711: } find_t;
                    712: #pragma pack()
                    713: 
                    714: #pragma pack(1)
                    715: typedef struct {
                    716:        UINT32 attrib;
                    717:        PAIR32 ctime_lo;
                    718:        PAIR32 ctime_hi;
                    719:        PAIR32 atime_lo;
                    720:        PAIR32 atime_hi;
                    721:        PAIR32 mtime_lo;
                    722:        PAIR32 mtime_hi;
                    723:        UINT32 size_hi;
                    724:        UINT32 size_lo;
                    725:        UINT8 reserved[8];
                    726:        char full_name[260];
                    727:        char short_name[14];
                    728: } find_lfn_t;
                    729: #pragma pack()
                    730: 
                    731: #pragma pack(1)
                    732: typedef struct {
                    733:        UINT16 info_level;
                    734:        UINT32 serial_number;
                    735:        char volume_label[11];
                    736:        char file_system[8];
                    737: } drive_info_t;
                    738: #pragma pack()
                    739: 
                    740: #pragma pack(1)
                    741: typedef struct {
                    742:        UINT16 size_of_structure;
                    743:        UINT16 structure_version;
                    744:        UINT32 sectors_per_cluster;
                    745:        UINT32 bytes_per_sector;
                    746:        UINT32 available_clusters_on_drive;
                    747:        UINT32 total_clusters_on_drive;
                    748:        UINT32 available_sectors_on_drive;
                    749:        UINT32 total_sectors_on_drive;
                    750:        UINT32 available_allocation_units;
                    751:        UINT32 total_allocation_units;
                    752:        UINT8 reserved[8];
                    753: } ext_space_info_t;
                    754: #pragma pack()
                    755: 
                    756: #pragma pack(1)
                    757: typedef struct {
                    758:        UINT8 drive_num;
                    759:        UINT8 unit_num;
                    760:        UINT16 bytes_per_sector;
                    761:        UINT8 highest_sector_num;
                    762:        UINT8 shift_count;
                    763:        UINT16 reserved_sectors;
                    764:        UINT8 fat_num;
                    765:        UINT16 root_entries;
                    766:        UINT16 first_data_sector;
                    767:        UINT16 highest_cluster_num;
                    768:        UINT16 sectors_per_fat;
                    769:        UINT16 first_dir_sector;
                    770:        UINT32 device_driver_header;
                    771:        UINT8 media_type;
                    772:        UINT8 drive_accessed;
                    773:        UINT16 next_dpb_ofs;
                    774:        UINT16 next_dpb_seg;
                    775:        UINT16 first_free_cluster;
                    776:        UINT16 free_clusters;
1.1.1.12  root      777:        // extended
                    778:        UINT16 fat_mirroring;
                    779:        UINT16 info_sector;
                    780:        UINT16 backup_boot_sector;
                    781:        UINT32 first_cluster_sector;
                    782:        UINT32 maximum_cluster_num;
                    783:        UINT32 fat_sectors;
                    784:        UINT32 root_cluster;
                    785:        UINT32 free_search_cluster;
1.1       root      786: } dpb_t;
                    787: #pragma pack()
                    788: 
                    789: #pragma pack(1)
                    790: typedef struct {
                    791:        char path_name[67];
                    792:        UINT16 drive_attrib;
                    793:        UINT8 physical_drive_number;
                    794: } cds_t;
                    795: #pragma pack()
                    796: 
                    797: #pragma pack(1)
                    798: typedef struct {
1.1.1.18  root      799:        UINT8 reserved_1[3];            // -34
                    800:        UINT8 switchar;                 // -31 current switch character
                    801:        UINT8 malloc_strategy;          // -30 current memory allocation strategy
                    802:        UINT8 reserved_2[29];           // -29
                    803:        // ----- from DOSBox -----
                    804:        UINT8 crit_error_flag;          // 0x00 Critical Error Flag
                    805:        UINT8 indos_flag;               // 0x01 InDOS flag (count of active INT 21 calls)
                    806:        UINT8 drive_crit_error;         // 0x02 Drive on which current critical error occurred or FFh
                    807:        UINT8 locus_of_last_error;      // 0x03 locus of last error
                    808:        UINT16 extended_error_code;     // 0x04 extended error code of last error
                    809:        UINT8 suggested_action;         // 0x06 suggested action for last error
                    810:        UINT8 error_class;              // 0x07 class of last error
                    811:        PAIR32 last_error_pointer;      // 0x08 ES:DI pointer for last error
                    812:        PAIR32 current_dta;             // 0x0C current DTA (Disk Transfer Address)
                    813:        UINT16 current_psp;             // 0x10 current PSP
                    814:        UINT16 sp_int_23;               // 0x12 stores SP across an INT 23
                    815:        UINT16 return_code;             // 0x14 return code from last process termination (zerod after reading with AH=4Dh)
                    816:        UINT8 current_drive;            // 0x16 current drive
                    817:        UINT8 extended_break_flag;      // 0x17 extended break flag
                    818:        UINT8 fill[2];                  // 0x18 flag: code page switching || flag: copy of previous byte in case of INT 24 Abort
                    819: } sda_t;
                    820: #pragma pack()
                    821: 
                    822: #pragma pack(1)
                    823: typedef struct {
1.1.1.14  root      824:        UINT8 reserved_0[4];    // -38
                    825:        UINT16 magic_word;      // -34 from DOSBox
                    826:        UINT8 reserved_1[30];   // -32
1.1       root      827:        UINT16 first_mcb;       // -2
                    828:        PAIR32 first_dpb;       // +0
1.1.1.14  root      829:        PAIR32 first_sft;       // +4
1.1.1.20  root      830:        PAIR32 clock_device;    // +8
                    831:        PAIR32 con_device;      // +12
1.1.1.14  root      832:        UINT16 max_sector_len;  // +16
                    833:        PAIR32 disk_buf_info;   // +18 from DOSBox
1.1       root      834:        PAIR32 cds;             // +22
                    835:        PAIR32 fcb_table;       // +26
1.1.1.20  root      836:        UINT8 reserved_2[3];    // +30
1.1       root      837:        UINT8 last_drive;       // +33
1.1.1.14  root      838:        struct {
1.1.1.20  root      839:                PAIR32 next_driver;     // +34
1.1.1.14  root      840:                UINT16 attributes;      // +38
                    841:                UINT16 strategy;        // +40
                    842:                UINT16 interrupt;       // +42
                    843:                char dev_name[8];       // +44
                    844:        } nul_device;
1.1.1.20  root      845:        UINT8 reserved_3[11];   // +52
1.1       root      846:        UINT16 buffers_x;       // +63
                    847:        UINT16 buffers_y;       // +65
                    848:        UINT8 boot_drive;       // +67
                    849:        UINT8 i386_or_later;    // +68
                    850:        UINT16 ext_mem_size;    // +69
1.1.1.14  root      851:        PAIR32 disk_buf_heads;  // +71 from DOSBox
1.1.1.20  root      852:        UINT8 reserved_4[21];   // +75
1.1.1.5   root      853:        UINT8 dos_flag;         // +96
1.1.1.20  root      854:        UINT8 reserved_5[2];    // +97
1.1.1.15  root      855:        UINT8 umb_linked;       // +99 from DOSBox
1.1.1.20  root      856:        UINT8 reserved_6[2];    // +100
1.1.1.15  root      857:        UINT16 first_umb_fcb;   // +102 from DOSBox
                    858:        UINT16 first_mcb_2;     // +104 from DOSBox
1.1.1.21! root      859:        UINT8 nul_device_routine[7];
1.1       root      860: } dos_info_t;
                    861: #pragma pack()
                    862: 
1.1.1.14  root      863: #pragma pack(1)
                    864: typedef struct {
1.1.1.20  root      865:        PAIR32 next_driver;
                    866:        UINT16 attributes;
                    867:        UINT16 strategy;
                    868:        UINT16 interrupt;
                    869:        char dev_name[8];
                    870: } device_t;
                    871: #pragma pack()
                    872: 
                    873: #pragma pack(1)
                    874: typedef struct {
1.1.1.14  root      875:        UINT16 date_format;
                    876:        char currency_symbol[5];
                    877:        char thou_sep[2];
                    878:        char dec_sep[2];
                    879:        char date_sep[2];
                    880:        char time_sep[2];
                    881:        char currency_format;
                    882:        char currency_dec_digits;
                    883:        char time_format;
                    884:        PAIR32 case_map;
                    885:        char list_sep[2];
                    886:        char reserved[10];
                    887: } country_info_t;
                    888: #pragma pack()
                    889: 
1.1       root      890: typedef struct {
                    891:        char path[MAX_PATH];
                    892:        int valid;
                    893:        int id;
                    894:        int atty;
                    895:        int mode;
                    896:        UINT16 info;
                    897:        UINT16 psp;
                    898: } file_handler_t;
                    899: 
                    900: static const struct {
                    901:        int mode;
                    902:        int in;
                    903:        int out;
                    904: } file_mode[] = {
1.1.1.11  root      905:        { _O_RDONLY | _O_BINARY, 1, 0 },
1.1       root      906:        { _O_WRONLY | _O_BINARY, 0, 1 },
                    907:        { _O_RDWR   | _O_BINARY, 1, 1 },
                    908: };
                    909: 
                    910: typedef struct {
                    911:        UINT16 psp;
                    912:        char module_dir[MAX_PATH];
                    913:        PAIR32 dta;
                    914:        UINT8 switchar;
                    915:        UINT8 verify;
                    916:        int max_files;
                    917:        char volume_label[MAX_PATH];
                    918:        bool parent_int_10h_feh_called;
                    919:        bool parent_int_10h_ffh_called;
1.1.1.12  root      920:        UINT16 parent_ds;
1.1.1.14  root      921:        struct {
                    922:                UINT16 handle;
                    923:                UINT16 page;
                    924:                bool mapped;
                    925:        } ems_pages[4];
                    926:        bool ems_pages_stored;
1.1       root      927: } process_t;
                    928: 
1.1.1.11  root      929: typedef struct {
                    930:        UINT16 psp;
                    931:        UINT32 dta;
1.1.1.12  root      932:        UINT8 allowable_mask;
                    933:        UINT8 required_mask;
1.1.1.11  root      934:        HANDLE find_handle;
                    935: } dtainfo_t;
                    936: 
1.1.1.8   root      937: UINT8 major_version = 7;
                    938: UINT8 minor_version = 10;
                    939: 
1.1.1.7   root      940: UINT16 first_mcb;
1.1       root      941: UINT16 current_psp;
                    942: 
                    943: int retval = 0;
                    944: UINT16 error_code = 0;
                    945: 
                    946: file_handler_t file_handler[MAX_FILES];
                    947: UINT8 file_buffer[0x100000];
                    948: 
                    949: process_t process[MAX_PROCESS];
1.1.1.11  root      950: dtainfo_t dtalist[MAX_DTAINFO];
1.1       root      951: 
1.1.1.6   root      952: UINT16 malloc_strategy = 0;
                    953: 
1.1.1.12  root      954: char comspec_path[MAX_PATH] = "C:\\COMMAND.COM";
                    955: 
1.1       root      956: void msdos_syscall(unsigned num);
                    957: int msdos_init(int argc, char *argv[], char *envp[], int standard_env);
                    958: void msdos_finish();
                    959: 
                    960: // console
                    961: 
1.1.1.12  root      962: #define SCR_BUF_WIDTH  256
                    963: #define SCR_BUF_HEIGHT 128
1.1       root      964: 
                    965: #define SET_RECT(rect, l, t, r, b) { \
                    966:        rect.Left = l; \
                    967:        rect.Top = t; \
                    968:        rect.Right = r; \
                    969:        rect.Bottom = b; \
                    970: }
                    971: 
1.1.1.19  root      972: DWORD dwConsoleMode = 0;
                    973: 
1.1.1.12  root      974: CHAR_INFO scr_buf[SCR_BUF_WIDTH * SCR_BUF_HEIGHT];
                    975: char scr_char[SCR_BUF_WIDTH * SCR_BUF_HEIGHT];
                    976: WORD scr_attr[SCR_BUF_WIDTH * SCR_BUF_HEIGHT];
1.1       root      977: COORD scr_buf_size;
                    978: COORD scr_buf_pos;
                    979: int scr_width, scr_height;
1.1.1.12  root      980: int scr_top;
1.1.1.10  root      981: bool restore_console_on_exit = false;
1.1       root      982: bool cursor_moved;
                    983: 
                    984: FIFO *key_buf_char;
                    985: FIFO *key_buf_scan;
1.1.1.19  root      986: bool key_changed = false;
1.1.1.4   root      987: UINT32 key_code = 0;
1.1       root      988: 
1.1.1.21! root      989: UINT8 ctrl_c_checking = 0x01; // ???
        !           990: bool ctrl_c_pressed = false;
        !           991: bool ctrl_c_detected = false;
        !           992: 
1.1       root      993: int active_code_page;
                    994: int system_code_page;
                    995: 
1.1.1.12  root      996: UINT32 text_vram_top_address;
                    997: UINT32 text_vram_end_address;
                    998: UINT32 shadow_buffer_top_address;
                    999: UINT32 shadow_buffer_end_address;
                   1000: int vram_pages;
1.1       root     1001: bool int_10h_feh_called = false;
                   1002: bool int_10h_ffh_called = false;
                   1003: 
1.1.1.19  root     1004: #define MAX_MOUSE_BUTTONS      3
                   1005: 
1.1.1.21! root     1006: typedef struct {
1.1.1.19  root     1007:        bool active;
                   1008:        struct {
                   1009:                int x, y;
                   1010:        } position, prev_position, max_position, min_position, mickey;
                   1011:        struct {
                   1012:                bool status;
                   1013:                int pressed_times;
                   1014:                int released_times;
                   1015:                struct {
                   1016:                        int x, y;
                   1017:                } pressed_position;
                   1018:                struct {
                   1019:                        int x, y;
                   1020:                } released_position;
                   1021:        } buttons[MAX_MOUSE_BUTTONS];
                   1022:        int get_buttons()
                   1023:        {
                   1024:                int val = 0;
                   1025:                for(int i = 0; i < MAX_MOUSE_BUTTONS; i++) {
                   1026:                        if(buttons[i].status) {
                   1027:                                val |= 1 << i;
                   1028:                        }
                   1029:                }
                   1030:                return(val);
                   1031:        }
                   1032:        UINT16 status;
                   1033:        UINT16 status_irq;
                   1034:        UINT16 call_mask;
                   1035:        PAIR32 call_addr;
                   1036:        // dummy
                   1037:        UINT16 sensitivity[3];
                   1038:        UINT16 display_page;
                   1039:        UINT16 language;
                   1040:        UINT16 hot_spot[2];
1.1.1.21! root     1041: } mouse_t;
        !          1042: 
        !          1043: mouse_t mouse;
1.1.1.19  root     1044: 
                   1045: UINT16 mouse_push_ax;
                   1046: UINT16 mouse_push_bx;
                   1047: UINT16 mouse_push_cx;
                   1048: UINT16 mouse_push_dx;
                   1049: UINT16 mouse_push_si;
                   1050: UINT16 mouse_push_di;
                   1051: 
1.1       root     1052: #endif

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.