3 * Test program: Expanded Memory Manager functions
4 * (C) 2011-2012 Jonathan Campbell.
5 * Hackipedia DOS library.
7 * This code is licensed under the LGPL.
8 * <insert LGPL legal text here>
19 //#include "src/lib/doslib/cpu.h"
20 #include "src/lib/doslib/dos.h"
21 #include "src/lib/doslib/emm.h"
22 //#include <hw/dos/doswin.h>
24 static const char *message = "Hello world. How are you?";
25 static const char *message2 = "Pingas. Sup dawg. Hello world. Dork. Hahahajajaja.";
26 static char tmp[128],tmp2[128];
27 extern int probe_emm0();
30 # define x_memcpy(a,b,c) memcpy(a,b,c)
32 /* what have we come to when friggin' memcpy() causes a GPF?!? */
33 static void x_memcpy(unsigned char *dst,const unsigned char *src,size_t c) {
34 fprintf(stderr,"memcpy %p -> %p (%lu)\n",
35 dst,src,(unsigned long)c);
45 size_t message_l = strlen(message),message2_l = strlen(message2);
48 printf("DOS version %x.%02u\n",dos_version>>8,dos_version&0xFF);
49 /*if (detect_windows()) {
50 printf("I am running under Windows.\n");
51 printf(" Mode: %s\n",windows_mode_str(windows_mode));
52 printf(" Ver: %x.%02u\n",windows_version>>8,windows_version&0xFF);
55 printf("Not running under Windows or OS/2\n");
63 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);
64 emm_update_page_count();
66 printf(" Unallocated pages: %u (%luKB)\n",
67 emm_unallocated_pages,
68 (unsigned long)emm_unallocated_pages << 4UL); /* 2^14 = 16384 */
69 printf(" Total pages: %u (%luKB)\n",
71 (unsigned long)emm_total_pages << 4UL);
72 printf(" Physical pages: %u (%luKB)\n",
74 (unsigned long)emm_phys_pages << 4UL);
76 while (getch() != 13);
79 /* print out the mapping table, if available */
80 if (emm_phys_map != NULL) {
81 struct emm_phys_page_map *me;
84 printf("Physical page to segment table\n");
85 for (i=0;i < emm_phys_pages;i++) {
86 me = emm_phys_map + i;
87 printf(" %02x: 0x%04x",me->number,me->segment);
88 if ((i%5) == 4) printf("\n");
94 printf("Allocating EMM pages (1): ");
95 h1 = emm_alloc_pages(1);
98 printf("OK, handle=%u\n",h1);
99 if (!emm_free_pages(h1)) printf("cannot free\n");
101 else printf("FAILED\n");
103 printf("Allocating EMM pages (%luKB): ", (dword)emm_unallocated_pages << 4UL);
104 h1 = emm_alloc_pages(emm_unallocated_pages);
106 if (h1 >= 0) printf("OK, handle=%u\n",h1);
107 else printf("FAILED\n");
109 // printf("Allocating EMM pages (1.6MB): ");
110 //h2 = emm_alloc_pages(0x19999AUL >> 14UL);
111 /*h2 = emm_alloc_pages(1);
113 if (h2 >= 0) printf("OK, handle=%u\n",h2);
114 else printf("FAILED\n");
116 //printf("Allocating EMM pages (12MB): ");
117 //h3 = emm_alloc_pages(0xC00000UL >> 14UL);
118 h3 = emm_alloc_pages(1);
120 if (h3 >= 0) printf("OK, handle=%u\n",h3);
121 else printf("FAILED\n");*/
123 while (getch() != 13);
125 if (h1 >= 0 && !emm_free_pages(h1)) printf("Cannot free\n");
127 /*if (h2 >= 0 && !emm_free_pages(h2)) printf("Cannot free\n");
129 if (h3 >= 0 && !emm_free_pages(h3)) printf("Cannot free\n");
132 printf("Allocating EMM pages (32KB): ");
133 h1 = emm_alloc_pages(2);
136 printf("OK, handle=%u\n",h1);
137 if (emm_map_page(h1,/*physical*/0,/*logical*/0)) {
138 unsigned int segm = emm_last_phys_page_segment(0);
139 printf("Seg %04x\n",segm);
142 #if TARGET_MSDOS == 16
143 char far *ptr = MK_FP(segm,0);
145 char *ptr = (char*)(segm << 4UL);
148 #if TARGET_MSDOS == 16
149 _fmemcpy(ptr,(char far*)message,message_l+1);
150 _fmemcpy((char far*)tmp,ptr,message_l+1);
152 x_memcpy(ptr,message,message_l+1);
153 x_memcpy(tmp,ptr,message_l+1);
155 printf("After writing message there, I read back: '%s'\n",tmp);
157 if (!emm_map_page(h1,0,1)) printf("Cannot remap\n");
159 #if TARGET_MSDOS == 16
160 _fmemcpy(ptr,(char far*)message2,message2_l+1);
161 _fmemcpy((char far*)tmp,ptr,message2_l+1);
163 x_memcpy(ptr,message2,message2_l+1);
164 x_memcpy(tmp,ptr,message2_l+1);
166 printf("After mapping to page 2 and writing there, I read back: '%s'\n",tmp);
168 if (!emm_map_page(h1,0,0)) printf("Cannot remap\n");
170 #if TARGET_MSDOS == 16
171 _fmemcpy((char far*)tmp,ptr,message_l+1);
173 x_memcpy(tmp,ptr,message_l+1);
175 printf("After mapping back to 1, I read back: '%s'\n",tmp);
177 if (emm_map_page(h1,0,2)) printf("Whoops, I was able to map logical pages beyond what I allocated\n");
180 printf("Cannot get segment\n");
182 if (!emm_map_page(h1,0,0xFFFF)) printf("Cannot unmap\n");
185 printf("Cannot map\n");
187 if (!emm_free_pages(h1)) printf("Cannot free\n");
189 else printf("FAILED\n");
191 printf("Allocating EMM pages (32KB): ");
192 h1 = emm_alloc_pages(2);
194 printf("OK, handle=%u\n",h1);
195 if ( emm_map_page(h1,/*physical*/0,/*logical*/0) &&
196 emm_map_page(h1,/*physical*/1,/*logical*/1)) {
197 unsigned int seg1 = emm_last_phys_page_segment(0);
198 unsigned int seg2 = emm_last_phys_page_segment(1);
199 printf("Seg %04x,%04x\n",seg1,seg2);
200 if (seg1 > 0 && seg2 > 0) {
201 #if TARGET_MSDOS == 16
202 char far *ptr1 = MK_FP(seg1,0);
203 char far *ptr2 = MK_FP(seg2,0);
205 char *ptr1 = (char*)(seg1 << 4UL);
206 char *ptr2 = (char*)(seg2 << 4UL);
209 #if TARGET_MSDOS == 16
210 _fmemcpy(ptr1,(char far*)message,message_l+1);
211 _fmemcpy(ptr2,(char far*)message2,message2_l+1);
213 memcpy(ptr1,message,message_l+1);
214 memcpy(ptr2,message2,message2_l+1);
217 #if TARGET_MSDOS == 16
218 _fmemcpy((char far*)tmp,ptr1,message_l+1);
219 _fmemcpy((char far*)tmp2,ptr2,message2_l+1);
221 memcpy(tmp,ptr1,message_l+1);
222 memcpy(tmp2,ptr2,message2_l+1);
225 printf("After writing message there, I read back:\n'%s'\n'%s'\n",tmp,tmp2);
227 /* now swap the pages */
228 if (!emm_map_page(h1,1,0)) printf("cannot map log 1 -> phys 0\n");
229 if (!emm_map_page(h1,0,1)) printf("cannot map log 0 -> phys 1\n");
231 #if TARGET_MSDOS == 16
232 _fmemcpy((char far*)tmp,ptr1,message2_l+1);
233 _fmemcpy((char far*)tmp2,ptr2,message_l+1);
235 memcpy(tmp,ptr1,message2_l+1);
236 memcpy(tmp2,ptr2,message_l+1);
239 printf("After swapping pages, I read back:\n'%s'\n'%s'\n",tmp,tmp2);
242 printf("Cannot get segment\n");
244 if (!emm_map_page(h1,0,0xFFFF) || !emm_map_page(h1,1,0xFFFF)) printf("Cannot unmap\n");
247 printf("Cannot map\n");
249 if (!emm_free_pages(h1)) printf("Cannot free\n");
251 else printf("FAILED\n");
255 /* we do this test because Microsoft EMM386.EXE seems to max out at 32MB.
256 * the host could have 256MB of total memory and it would still report 32MB in EMS */
257 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");
258 printf("Allocating EMM pages (48MB): ");
259 h1 = emm_alloc_pages((48UL << 20UL) >> 14UL);
261 printf("OK, handle=%u\n",h1);
262 if (!emm_free_pages(h1)) printf("cannot free\n");
264 else printf("FAILED\n");
266 printf("Allocating EMM pages (96MB): ");
267 h1 = emm_alloc_pages((96UL << 20UL) >> 14UL);
269 printf("OK, handle=%u\n",h1);
270 if (!emm_free_pages(h1)) printf("cannot free\n");
272 else printf("FAILED\n");