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