+++ /dev/null
-/* testemm.c\r
- *\r
- * Test program: Expanded Memory Manager functions\r
- * (C) 2011-2012 Jonathan Campbell.\r
- * Hackipedia DOS library.\r
- *\r
- * This code is licensed under the LGPL.\r
- * <insert LGPL legal text here>\r
- */\r
-\r
-#include <stdlib.h>\r
-#include <string.h>\r
-#include <stdint.h>\r
-#include <assert.h>\r
-#include <stdio.h>\r
-#include <conio.h>\r
-#include <dos.h>\r
-\r
-//#include "src/lib/doslib/cpu.h"\r
-#include "src/lib/doslib/dos.h"\r
-#include "src/lib/doslib/emm.h"\r
-//#include <hw/dos/doswin.h>\r
-\r
-static const char *message = "Hello world. How are you?";\r
-static const char *message2 = "Pingas. Sup dawg. Hello world. Dork. Hahahajajaja.";\r
-static char tmp[128],tmp2[128];\r
-extern int probe_emm0();\r
-\r
-#if 1\r
-# define x_memcpy(a,b,c) memcpy(a,b,c)\r
-#else\r
-/* what have we come to when friggin' memcpy() causes a GPF?!? */\r
-static void x_memcpy(unsigned char *dst,const unsigned char *src,size_t c) {\r
- fprintf(stderr,"memcpy %p -> %p (%lu)\n",\r
- dst,src,(unsigned long)c);\r
-\r
- while (c != 0) {\r
- *dst++ = *src++;\r
- c--;\r
- }\r
-}\r
-#endif\r
-\r
-int main() {\r
- size_t message_l = strlen(message),message2_l = strlen(message2);\r
-\r
- probe_dos();\r
- printf("DOS version %x.%02u\n",dos_version>>8,dos_version&0xFF);\r
- /*if (detect_windows()) {\r
- printf("I am running under Windows.\n");\r
- printf(" Mode: %s\n",windows_mode_str(windows_mode));\r
- printf(" Ver: %x.%02u\n",windows_version>>8,windows_version&0xFF);\r
- }\r
- else {\r
- printf("Not running under Windows or OS/2\n");\r
- }*/\r
-\r
- sanity();\r
- if (probe_emm()) {\r
- int h1,h2,h3;\r
-\r
- sanity();\r
- printf("Expanded memory manager present. Status = 0x%02x Page frame @0x%04x v%x.%x\n",emm_status,emm_page_frame_segment,emm_version>>4,emm_version&0x0F);\r
- emm_update_page_count();\r
- sanity();\r
- printf(" Unallocated pages: %u (%luKB)\n",\r
- emm_unallocated_pages,\r
- (unsigned long)emm_unallocated_pages << 4UL); /* 2^14 = 16384 */\r
- printf(" Total pages: %u (%luKB)\n",\r
- emm_total_pages,\r
- (unsigned long)emm_total_pages << 4UL);\r
- printf(" Physical pages: %u (%luKB)\n",\r
- emm_phys_pages,\r
- (unsigned long)emm_phys_pages << 4UL);\r
-\r
- while (getch() != 13);\r
- sanity();\r
-\r
- /* print out the mapping table, if available */\r
- if (emm_phys_map != NULL) {\r
- struct emm_phys_page_map *me;\r
- unsigned int i;\r
-\r
- printf("Physical page to segment table\n");\r
- for (i=0;i < emm_phys_pages;i++) {\r
- me = emm_phys_map + i;\r
- printf(" %02x: 0x%04x",me->number,me->segment);\r
- if ((i%5) == 4) printf("\n");\r
- }\r
- printf("\n");\r
- sanity();\r
- }\r
-\r
- printf("Allocating EMM pages (1): ");\r
- h1 = emm_alloc_pages(1);\r
- sanity();\r
- if (h1 >= 0) {\r
- printf("OK, handle=%u\n",h1);\r
- if (!emm_free_pages(h1)) printf("cannot free\n");\r
- }\r
- else printf("FAILED\n");\r
-\r
- printf("Allocating EMM pages (%luKB): ", (dword)emm_unallocated_pages << 4UL);\r
- h1 = emm_alloc_pages(emm_unallocated_pages);\r
- sanity();\r
- if (h1 >= 0) printf("OK, handle=%u\n",h1);\r
- else printf("FAILED\n");\r
-\r
-// printf("Allocating EMM pages (1.6MB): ");\r
- //h2 = emm_alloc_pages(0x19999AUL >> 14UL);\r
- /*h2 = emm_alloc_pages(1);\r
- sanity();\r
- if (h2 >= 0) printf("OK, handle=%u\n",h2);\r
- else printf("FAILED\n");\r
-\r
- //printf("Allocating EMM pages (12MB): ");\r
- //h3 = emm_alloc_pages(0xC00000UL >> 14UL);\r
- h3 = emm_alloc_pages(1);\r
- sanity();\r
- if (h3 >= 0) printf("OK, handle=%u\n",h3);\r
- else printf("FAILED\n");*/\r
-\r
- while (getch() != 13);\r
-\r
- if (h1 >= 0 && !emm_free_pages(h1)) printf("Cannot free\n");\r
- sanity();\r
- /*if (h2 >= 0 && !emm_free_pages(h2)) printf("Cannot free\n");\r
- sanity();\r
- if (h3 >= 0 && !emm_free_pages(h3)) printf("Cannot free\n");\r
- sanity();*/\r
-\r
- printf("Allocating EMM pages (32KB): ");\r
- h1 = emm_alloc_pages(2);\r
- sanity();\r
- if (h1 >= 0) {\r
- printf("OK, handle=%u\n",h1);\r
- if (emm_map_page(h1,/*physical*/0,/*logical*/0)) {\r
- unsigned int segm = emm_last_phys_page_segment(0);\r
- printf("Seg %04x\n",segm);\r
- sanity();\r
- if (segm > 0) {\r
-#if TARGET_MSDOS == 16\r
- char far *ptr = MK_FP(segm,0);\r
-#else\r
- char *ptr = (char*)(segm << 4UL);\r
-#endif\r
-\r
-#if TARGET_MSDOS == 16\r
- _fmemcpy(ptr,(char far*)message,message_l+1);\r
- _fmemcpy((char far*)tmp,ptr,message_l+1);\r
-#else\r
- x_memcpy(ptr,message,message_l+1);\r
- x_memcpy(tmp,ptr,message_l+1);\r
-#endif\r
- printf("After writing message there, I read back: '%s'\n",tmp);\r
-\r
- if (!emm_map_page(h1,0,1)) printf("Cannot remap\n");\r
-\r
-#if TARGET_MSDOS == 16\r
- _fmemcpy(ptr,(char far*)message2,message2_l+1);\r
- _fmemcpy((char far*)tmp,ptr,message2_l+1);\r
-#else\r
- x_memcpy(ptr,message2,message2_l+1);\r
- x_memcpy(tmp,ptr,message2_l+1);\r
-#endif\r
- printf("After mapping to page 2 and writing there, I read back: '%s'\n",tmp);\r
-\r
- if (!emm_map_page(h1,0,0)) printf("Cannot remap\n");\r
-\r
-#if TARGET_MSDOS == 16\r
- _fmemcpy((char far*)tmp,ptr,message_l+1);\r
-#else\r
- x_memcpy(tmp,ptr,message_l+1);\r
-#endif\r
- printf("After mapping back to 1, I read back: '%s'\n",tmp);\r
-\r
- if (emm_map_page(h1,0,2)) printf("Whoops, I was able to map logical pages beyond what I allocated\n");\r
- }\r
- else {\r
- printf("Cannot get segment\n");\r
- }\r
- if (!emm_map_page(h1,0,0xFFFF)) printf("Cannot unmap\n");\r
- }\r
- else {\r
- printf("Cannot map\n");\r
- }\r
- if (!emm_free_pages(h1)) printf("Cannot free\n");\r
- }\r
- else printf("FAILED\n");\r
-\r
- printf("Allocating EMM pages (32KB): ");\r
- h1 = emm_alloc_pages(2);\r
- if (h1 >= 0) {\r
- printf("OK, handle=%u\n",h1);\r
- if ( emm_map_page(h1,/*physical*/0,/*logical*/0) &&\r
- emm_map_page(h1,/*physical*/1,/*logical*/1)) {\r
- unsigned int seg1 = emm_last_phys_page_segment(0);\r
- unsigned int seg2 = emm_last_phys_page_segment(1);\r
- printf("Seg %04x,%04x\n",seg1,seg2);\r
- if (seg1 > 0 && seg2 > 0) {\r
-#if TARGET_MSDOS == 16\r
- char far *ptr1 = MK_FP(seg1,0);\r
- char far *ptr2 = MK_FP(seg2,0);\r
-#else\r
- char *ptr1 = (char*)(seg1 << 4UL);\r
- char *ptr2 = (char*)(seg2 << 4UL);\r
-#endif\r
-\r
-#if TARGET_MSDOS == 16\r
- _fmemcpy(ptr1,(char far*)message,message_l+1);\r
- _fmemcpy(ptr2,(char far*)message2,message2_l+1);\r
-#else\r
- memcpy(ptr1,message,message_l+1);\r
- memcpy(ptr2,message2,message2_l+1);\r
-#endif\r
-\r
-#if TARGET_MSDOS == 16\r
- _fmemcpy((char far*)tmp,ptr1,message_l+1);\r
- _fmemcpy((char far*)tmp2,ptr2,message2_l+1);\r
-#else\r
- memcpy(tmp,ptr1,message_l+1);\r
- memcpy(tmp2,ptr2,message2_l+1);\r
-#endif\r
-\r
- printf("After writing message there, I read back:\n'%s'\n'%s'\n",tmp,tmp2);\r
-\r
- /* now swap the pages */\r
- if (!emm_map_page(h1,1,0)) printf("cannot map log 1 -> phys 0\n");\r
- if (!emm_map_page(h1,0,1)) printf("cannot map log 0 -> phys 1\n");\r
-\r
-#if TARGET_MSDOS == 16\r
- _fmemcpy((char far*)tmp,ptr1,message2_l+1);\r
- _fmemcpy((char far*)tmp2,ptr2,message_l+1);\r
-#else\r
- memcpy(tmp,ptr1,message2_l+1);\r
- memcpy(tmp2,ptr2,message_l+1);\r
-#endif\r
-\r
- printf("After swapping pages, I read back:\n'%s'\n'%s'\n",tmp,tmp2);\r
- }\r
- else {\r
- printf("Cannot get segment\n");\r
- }\r
- if (!emm_map_page(h1,0,0xFFFF) || !emm_map_page(h1,1,0xFFFF)) printf("Cannot unmap\n");\r
- }\r
- else {\r
- printf("Cannot map\n");\r
- }\r
- if (!emm_free_pages(h1)) printf("Cannot free\n");\r
- }\r
- else printf("FAILED\n");\r
-\r
- getch();\r
-\r
- /* we do this test because Microsoft EMM386.EXE seems to max out at 32MB.\r
- * the host could have 256MB of total memory and it would still report 32MB in EMS */\r
- printf("we do this test because Microsoft EMM386.EXE seems to max out at 32MB.\n the host could have 256MB of total memory and it would still report 32MB in EMS");\r
- printf("Allocating EMM pages (48MB): ");\r
- h1 = emm_alloc_pages((48UL << 20UL) >> 14UL);\r
- if (h1 >= 0) {\r
- printf("OK, handle=%u\n",h1);\r
- if (!emm_free_pages(h1)) printf("cannot free\n");\r
- }\r
- else printf("FAILED\n");\r
-\r
- printf("Allocating EMM pages (96MB): ");\r
- h1 = emm_alloc_pages((96UL << 20UL) >> 14UL);\r
- if (h1 >= 0) {\r
- printf("OK, handle=%u\n",h1);\r
- if (!emm_free_pages(h1)) printf("cannot free\n");\r
- }\r
- else printf("FAILED\n");\r
- }\r
-\r
- return 0;\r
-}\r