]> 4ch.mooo.com Git - 16.git/blob - src/testemm.c
wwww
[16.git] / src / testemm.c
1 /* testemm.c
2  *
3  * Test program: Expanded Memory Manager functions
4  * (C) 2011-2012 Jonathan Campbell.
5  * Hackipedia DOS library.
6  *
7  * This code is licensed under the LGPL.
8  * <insert LGPL legal text here>
9  */
10
11 #include <stdlib.h>
12 #include <string.h>
13 #include <stdint.h>
14 #include <assert.h>
15 #include <stdio.h>
16 #include <conio.h>
17 #include <dos.h>
18
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>
23
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();
28
29 #if 1
30 # define x_memcpy(a,b,c) memcpy(a,b,c)
31 #else
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);
36
37         while (c != 0) {
38             *dst++ = *src++;
39             c--;
40         }
41 }
42 #endif
43
44 int main() {
45         size_t message_l = strlen(message),message2_l = strlen(message2);
46
47         probe_dos();
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);
53         }
54         else {
55                 printf("Not running under Windows or OS/2\n");
56         }*/
57
58         sanity();
59         if (probe_emm()) {
60                 int h1,h2,h3;
61
62                 sanity();
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();
65                 sanity();
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",
70                         emm_total_pages,
71                         (unsigned long)emm_total_pages << 4UL);
72                 printf("  Physical pages:        %u (%luKB)\n",
73                         emm_phys_pages,
74                         (unsigned long)emm_phys_pages << 4UL);
75
76                 while (getch() != 13);
77                 sanity();
78
79                 /* print out the mapping table, if available */
80                 if (emm_phys_map != NULL) {
81                         struct emm_phys_page_map *me;
82                         unsigned int i;
83
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");
89                         }
90                         printf("\n");
91                         sanity();
92                 }
93
94                 printf("Allocating EMM pages (1): ");
95                 h1 = emm_alloc_pages(1);
96                 sanity();
97                 if (h1 >= 0) {
98                         printf("OK, handle=%u\n",h1);
99                         if (!emm_free_pages(h1)) printf("cannot free\n");
100                 }
101                 else printf("FAILED\n");
102
103                 printf("Allocating EMM pages (16KB): ");
104                 h1 = emm_alloc_pages(1);
105                 sanity();
106                 if (h1 >= 0) printf("OK, handle=%u\n",h1);
107                 else printf("FAILED\n");
108
109                 printf("Allocating EMM pages (1.6MB): ");
110                 h2 = emm_alloc_pages(0x19999AUL >> 14UL);
111                 sanity();
112                 if (h2 >= 0) printf("OK, handle=%u\n",h2);
113                 else printf("FAILED\n");
114
115                 printf("Allocating EMM pages (12MB): ");
116                 h3 = emm_alloc_pages(0xC00000UL >> 14UL);
117                 sanity();
118                 if (h3 >= 0) printf("OK, handle=%u\n",h3);
119                 else printf("FAILED\n");
120
121                 while (getch() != 13);
122
123                 if (h1 >= 0 && !emm_free_pages(h1)) printf("Cannot free\n");
124                 sanity();
125                 if (h2 >= 0 && !emm_free_pages(h2)) printf("Cannot free\n");
126                 sanity();
127                 if (h3 >= 0 && !emm_free_pages(h3)) printf("Cannot free\n");
128                 sanity();
129
130                 printf("Allocating EMM pages (32KB): ");
131                 h1 = emm_alloc_pages(2);
132                 sanity();
133                 if (h1 >= 0) {
134                         printf("OK, handle=%u\n",h1);
135                         if (emm_map_page(h1,/*physical*/0,/*logical*/0)) {
136                                 unsigned int segm = emm_last_phys_page_segment(0);
137                                 printf("Seg %04x\n",segm);
138                                 sanity();
139                                 if (segm > 0) {
140 #if TARGET_MSDOS == 16
141                                         char far *ptr = MK_FP(segm,0);
142 #else
143                                         char *ptr = (char*)(segm << 4UL);
144 #endif
145
146 #if TARGET_MSDOS == 16
147                                         _fmemcpy(ptr,(char far*)message,message_l+1);
148                                         _fmemcpy((char far*)tmp,ptr,message_l+1);
149 #else
150                                         x_memcpy(ptr,message,message_l+1);
151                                         x_memcpy(tmp,ptr,message_l+1);
152 #endif
153                                         printf("After writing message there, I read back: '%s'\n",tmp);
154
155                                         if (!emm_map_page(h1,0,1)) printf("Cannot remap\n");
156
157 #if TARGET_MSDOS == 16
158                                         _fmemcpy(ptr,(char far*)message2,message2_l+1);
159                                         _fmemcpy((char far*)tmp,ptr,message2_l+1);
160 #else
161                                         x_memcpy(ptr,message2,message2_l+1);
162                                         x_memcpy(tmp,ptr,message2_l+1);
163 #endif
164                                         printf("After mapping to page 2 and writing there, I read back: '%s'\n",tmp);
165
166                                         if (!emm_map_page(h1,0,0)) printf("Cannot remap\n");
167
168 #if TARGET_MSDOS == 16
169                                         _fmemcpy((char far*)tmp,ptr,message_l+1);
170 #else
171                                         x_memcpy(tmp,ptr,message_l+1);
172 #endif
173                                         printf("After mapping back to 1, I read back: '%s'\n",tmp);
174
175                                         if (emm_map_page(h1,0,2)) printf("Whoops, I was able to map logical pages beyond what I allocated\n");
176                                 }
177                                 else {
178                                         printf("Cannot get segment\n");
179                                 }
180                                 if (!emm_map_page(h1,0,0xFFFF)) printf("Cannot unmap\n");
181                         }
182                         else {
183                                 printf("Cannot map\n");
184                         }
185                         if (!emm_free_pages(h1)) printf("Cannot free\n");
186                 }
187                 else printf("FAILED\n");
188
189                 printf("Allocating EMM pages (32KB): ");
190                 h1 = emm_alloc_pages(2);
191                 if (h1 >= 0) {
192                         printf("OK, handle=%u\n",h1);
193                         if (    emm_map_page(h1,/*physical*/0,/*logical*/0) &&
194                                 emm_map_page(h1,/*physical*/1,/*logical*/1)) {
195                                 unsigned int seg1 = emm_last_phys_page_segment(0);
196                                 unsigned int seg2 = emm_last_phys_page_segment(1);
197                                 printf("Seg %04x,%04x\n",seg1,seg2);
198                                 if (seg1 > 0 && seg2 > 0) {
199 #if TARGET_MSDOS == 16
200                                         char far *ptr1 = MK_FP(seg1,0);
201                                         char far *ptr2 = MK_FP(seg2,0);
202 #else
203                                         char *ptr1 = (char*)(seg1 << 4UL);
204                                         char *ptr2 = (char*)(seg2 << 4UL);
205 #endif
206
207 #if TARGET_MSDOS == 16
208                                         _fmemcpy(ptr1,(char far*)message,message_l+1);
209                                         _fmemcpy(ptr2,(char far*)message2,message2_l+1);
210 #else
211                                         memcpy(ptr1,message,message_l+1);
212                                         memcpy(ptr2,message2,message2_l+1);
213 #endif
214
215 #if TARGET_MSDOS == 16
216                                         _fmemcpy((char far*)tmp,ptr1,message_l+1);
217                                         _fmemcpy((char far*)tmp2,ptr2,message2_l+1);
218 #else
219                                         memcpy(tmp,ptr1,message_l+1);
220                                         memcpy(tmp2,ptr2,message2_l+1);
221 #endif
222
223                                         printf("After writing message there, I read back:\n'%s'\n'%s'\n",tmp,tmp2);
224
225                                         /* now swap the pages */
226                                         if (!emm_map_page(h1,1,0)) printf("cannot map log 1 -> phys 0\n");
227                                         if (!emm_map_page(h1,0,1)) printf("cannot map log 0 -> phys 1\n");
228
229 #if TARGET_MSDOS == 16
230                                         _fmemcpy((char far*)tmp,ptr1,message2_l+1);
231                                         _fmemcpy((char far*)tmp2,ptr2,message_l+1);
232 #else
233                                         memcpy(tmp,ptr1,message2_l+1);
234                                         memcpy(tmp2,ptr2,message_l+1);
235 #endif
236
237                                         printf("After swapping pages, I read back:\n'%s'\n'%s'\n",tmp,tmp2);
238                                 }
239                                 else {
240                                         printf("Cannot get segment\n");
241                                 }
242                                 if (!emm_map_page(h1,0,0xFFFF) || !emm_map_page(h1,1,0xFFFF)) printf("Cannot unmap\n");
243                         }
244                         else {
245                                 printf("Cannot map\n");
246                         }
247                         if (!emm_free_pages(h1)) printf("Cannot free\n");
248                 }
249                 else printf("FAILED\n");
250
251                 getch();
252
253                 /* we do this test because Microsoft EMM386.EXE seems to max out at 32MB.
254                  * the host could have 256MB of total memory and it would still report 32MB in EMS */
255                 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");
256                 printf("Allocating EMM pages (48MB): ");
257                 h1 = emm_alloc_pages((48UL << 20UL) >> 14UL);
258                 if (h1 >= 0) {
259                         printf("OK, handle=%u\n",h1);
260                         if (!emm_free_pages(h1)) printf("cannot free\n");
261                 }
262                 else printf("FAILED\n");
263
264                 printf("Allocating EMM pages (96MB): ");
265                 h1 = emm_alloc_pages((96UL << 20UL) >> 14UL);
266                 if (h1 >= 0) {
267                         printf("OK, handle=%u\n",h1);
268                         if (!emm_free_pages(h1)) printf("cannot free\n");
269                 }
270                 else printf("FAILED\n");
271         }
272
273         return 0;
274 }
275