]> 4ch.mooo.com Git - 16.git/blob - src/tsthimem.c
i need to migrate to the memory manager stuff sometime
[16.git] / src / tsthimem.c
1 /* tsthimem.c\r
2  *\r
3  * Test program: HIMEM.SYS functions\r
4  * (C) 2010-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 <stdio.h>\r
15 #include <conio.h>\r
16 #include <dos.h>\r
17 \r
18 //#include "src/lib/doslib/cpu.h"\r
19 #include "src/lib/doslib/dos.h"\r
20 //#include doswin.h>\r
21 #include "src/lib/doslib/himemsys.h"\r
22 \r
23 int main() {\r
24         probe_dos();\r
25         printf("DOS version %x.%02u\n",dos_version>>8,dos_version&0xFF);\r
26         /*if (detect_windows()) {\r
27                 printf("I am running under Windows.\n");\r
28                 printf("    Mode: %s\n",windows_mode_str(windows_mode));\r
29                 printf("    Ver:  %x.%02u\n",windows_version>>8,windows_version&0xFF);\r
30         }\r
31         else {\r
32                 printf("Not running under Windows or OS/2\n");\r
33         }*/\r
34 \r
35         if (probe_himem_sys()) {\r
36                 int h1,h2,h3;\r
37 \r
38                 printf("HIMEM.SYS detected. Entry point %04x:%04x. v%x.%02x\n",\r
39                                 (unsigned int)((himem_sys_entry >> 16) & 0xFFFFUL),\r
40                                 (unsigned int)(himem_sys_entry & 0xFFFFUL),\r
41                                 (unsigned int)(himem_sys_version >> 8),\r
42                                 (unsigned int)(himem_sys_version & 0xFF));\r
43 \r
44                 if (himem_sys_flags & HIMEM_F_HMA)\r
45                         printf("  - HMA is present\n");\r
46                 if (himem_sys_flags & HIMEM_F_4GB)\r
47                         printf("  - Extensions are present to address up to 4GB of memory\n");\r
48 \r
49                 printf("A20 status: %u\n",himem_sys_query_a20());\r
50                 printf("Global A20 line:         "); fflush(stdout);\r
51                 printf("en=%d ",himem_sys_global_a20(1)); fflush(stdout);\r
52                 printf("query=%d ",himem_sys_query_a20(1)); fflush(stdout);\r
53                 printf("dis=%d ",himem_sys_global_a20(0)); fflush(stdout);\r
54                 printf("query=%d ",himem_sys_query_a20(1)); fflush(stdout);\r
55                 printf("\n");\r
56 \r
57                 printf("Local A20 line:          "); fflush(stdout);\r
58                 printf("en=%d ",himem_sys_local_a20(1)); fflush(stdout);\r
59                 printf("query=%d ",himem_sys_query_a20(1)); fflush(stdout);\r
60                 printf("dis=%d ",himem_sys_local_a20(0)); fflush(stdout);\r
61                 printf("query=%d ",himem_sys_query_a20(1)); fflush(stdout);\r
62                 printf("\n");\r
63 \r
64                 himem_sys_update_free_memory_status();\r
65                 printf("Free memory: %luKB (largest block %luKB)\n",\r
66                         (unsigned long)himem_sys_total_free,\r
67                         (unsigned long)himem_sys_largest_free);\r
68 \r
69                 printf("Attempting to alloc 4KB: ");\r
70                 h1 = himem_sys_alloc(4); /* NTS: This is in KB, not bytes */\r
71                 if (h1 != -1) printf("ok, handle %u\n",h1);\r
72                 else printf("failed\n");\r
73 \r
74                 printf("Attempting to alloc 64KB: ");\r
75                 h2 = himem_sys_alloc(46); /* NTS: This is in KB, not bytes */\r
76                 if (h2 != -1) printf("ok, handle %u\n",h2);\r
77                 else printf("failed\n");\r
78 \r
79                 printf("Attempting to alloc 1MB: ");\r
80                 h3 = himem_sys_alloc(1024); /* NTS: This is in KB, not bytes */\r
81                 if (h3 != -1) printf("ok, handle %u\n",h3);\r
82                 else printf("failed\n");\r
83 \r
84                 if (h1 != -1) {\r
85                         if (!himem_sys_free(h1)) printf(" - Free failed\n");\r
86                 }\r
87                 if (h2 != -1) {\r
88                         if (!himem_sys_free(h2)) printf(" - Free failed\n");\r
89                 }\r
90                 if (h3 != -1) {\r
91                         if (!himem_sys_free(h3)) printf(" - Free failed\n");\r
92                 }\r
93 \r
94                 printf("Attempting to alloc 1MB (for writing to): ");\r
95                 h3 = himem_sys_alloc(1024); /* NTS: This is in KB, not bytes */\r
96                 if (h3 != -1) {\r
97                         uint32_t ofs;\r
98                         unsigned int i;\r
99                         struct himem_block_info binf;\r
100 #if TARGET_MSDOS == 32\r
101                         char *msg;\r
102                         unsigned char *tmp;\r
103                         uint16_t tmpsel=0,msgsel=0;\r
104                         const char *msgref = "Testing 123 hello";\r
105 #else\r
106                         unsigned char tmp[16];\r
107                         const char *msg = "Testing 123 hello";\r
108 #endif\r
109 \r
110 #if TARGET_MSDOS == 32\r
111                         tmp = dpmi_alloc_dos(16,&tmpsel);\r
112                         if (tmp == NULL) abort();\r
113                         msg = dpmi_alloc_dos(strlen(msgref)+16,&msgsel);\r
114                         if (msg == NULL) abort();\r
115                         memcpy(msg,msgref,strlen(msgref)+1);\r
116 #endif\r
117 \r
118                         printf("ok, handle %u\n",h3);\r
119 \r
120                         if (himem_sys_get_handle_info(h3,&binf)) {\r
121                                 printf("Handle info:\n");\r
122                                 printf("     Lock count=%u  Free handles=%u  Size=%luKB\n",\r
123                                         (unsigned int)binf.lock_count,\r
124                                         (unsigned int)binf.free_handles,\r
125                                         (unsigned long)binf.block_length_kb);\r
126                         }\r
127                         else {\r
128                                 printf("Cannot get handle info\n");\r
129                         }\r
130 \r
131 #if TARGET_MSDOS == 32\r
132                         if (!himem_sys_move(h3,0,0/*conventional memory*/,(unsigned long)(msg),16))\r
133 #else\r
134                         if (!himem_sys_move(h3,0,0/*conventional memory*/,((unsigned long)FP_SEG(msg) << 4UL) + (unsigned long)FP_OFF(msg),sizeof(tmp)))\r
135 #endif\r
136                                 printf("Copy didn't work\n");\r
137 \r
138                         for (i=0;i < 16;i += 2) {\r
139                                 tmp[i+0] = 0x55;\r
140                                 tmp[i+1] = 0xAA;\r
141                         }\r
142 \r
143 #if TARGET_MSDOS == 32\r
144                         if (!himem_sys_move(0/*conventional memory*/,(unsigned long)(tmp),h3,0,16))\r
145 #else\r
146                         if (!himem_sys_move(0/*conventional memory*/,((unsigned long)FP_SEG(tmp) << 4UL) + (unsigned long)FP_OFF(tmp),h3,0,sizeof(tmp)))\r
147 #endif\r
148                                 printf("Copy didn't work\n");\r
149 \r
150                         for (i=0;i < 16;i++) printf("%02x ",tmp[i]);\r
151                         printf("\n");\r
152 \r
153                         ofs = himem_sys_lock(h3);\r
154                         if (ofs != 0UL) {\r
155                                 printf(" - Locked: Physical memory address 0x%08lX\n",(unsigned long)ofs);\r
156                                 if (!himem_sys_unlock(h3)) printf(" - Cannot unlock\n");\r
157                         }\r
158                         else {\r
159                                 printf(" - Cannot lock\n");\r
160                         }\r
161 \r
162                         printf("now resizing to 2MB\n");\r
163                         if (himem_sys_realloc(h3,2048)) {\r
164 #if TARGET_MSDOS == 32\r
165                                 if (!himem_sys_move(0/*conventional memory*/,(unsigned long)(tmp),h3,0,16))\r
166 #else\r
167                                 if (!himem_sys_move(0/*conventional memory*/,((unsigned long)FP_SEG(tmp) << 4UL) + (unsigned long)FP_OFF(tmp),h3,0,sizeof(tmp)))\r
168 #endif\r
169                                         printf("Copy didn't work\n");\r
170 \r
171                                 for (i=0;i < 16;i++) printf("%02x ",tmp[i]);\r
172                                 printf("\n");\r
173 \r
174                                 ofs = himem_sys_lock(h3);\r
175                                 if (ofs != 0UL) {\r
176                                         printf(" - Locked: Physical memory address 0x%08lX\n",(unsigned long)ofs);\r
177                                         if (!himem_sys_unlock(h3)) printf(" - Cannot unlock\n");\r
178                                 }\r
179                                 else {\r
180                                         printf(" - Cannot lock\n");\r
181                                 }\r
182                         }\r
183                         else {\r
184                                 printf(" - Cannot realloc\n");\r
185                         }\r
186 \r
187                         if (!himem_sys_free(h3)) printf(" - Free failed\n");\r
188 \r
189 #if TARGET_MSDOS == 32\r
190                         dpmi_free_dos(tmpsel); tmp=NULL;\r
191                         dpmi_free_dos(msgsel); msg=NULL;\r
192 #endif\r
193                 }\r
194                 else printf("failed\n");\r
195 \r
196                 printf("Attempting to alloc 129MB (for writing to): ");\r
197                 h3 = himem_sys_alloc(129UL * 1024UL); /* NTS: This is in KB, not bytes */\r
198                 if (h3 != -1) {\r
199                         uint32_t ofs;\r
200                         unsigned int i;\r
201                         unsigned char tmp[16];\r
202                         struct himem_block_info binf;\r
203                         const char *msg = "Testing 123 hello";\r
204 \r
205                         printf("ok, handle %u\n",h3);\r
206 \r
207                         if (himem_sys_get_handle_info(h3,&binf)) {\r
208                                 printf("Handle info:\n");\r
209                                 printf("     Lock count=%u  Free handles=%u  Size=%luKB\n",\r
210                                         (unsigned int)binf.lock_count,\r
211                                         (unsigned int)binf.free_handles,\r
212                                         (unsigned long)binf.block_length_kb);\r
213                         }\r
214                         else {\r
215                                 printf("Cannot get handle info\n");\r
216                         }\r
217 \r
218 #if TARGET_MSDOS == 32\r
219                         if (!himem_sys_move(h3,0,0/*conventional memory*/,(unsigned long)(msg),16))\r
220 #else\r
221                         if (!himem_sys_move(h3,0,0/*conventional memory*/,((unsigned long)FP_SEG(msg) << 4UL) + (unsigned long)FP_OFF(msg),sizeof(tmp)))\r
222 #endif\r
223                                 printf("Copy didn't work\n");\r
224 \r
225                         for (i=0;i < 16;i += 2) {\r
226                                 tmp[i+0] = 0x55;\r
227                                 tmp[i+1] = 0xAA;\r
228                         }\r
229 \r
230 #if TARGET_MSDOS == 32\r
231                         if (!himem_sys_move(0/*conventional memory*/,(unsigned long)(tmp),h3,0,16))\r
232 #else\r
233                         if (!himem_sys_move(0/*conventional memory*/,((unsigned long)FP_SEG(tmp) << 4UL) + (unsigned long)FP_OFF(tmp),h3,0,sizeof(tmp)))\r
234 #endif\r
235                                 printf("Copy didn't work\n");\r
236 \r
237                         for (i=0;i < 16;i++) printf("%02x ",tmp[i]);\r
238                         printf("\n");\r
239 \r
240                         ofs = himem_sys_lock(h3);\r
241                         if (ofs != 0UL) {\r
242                                 printf(" - Locked: Physical memory address 0x%08lX\n",(unsigned long)ofs);\r
243                                 if (!himem_sys_unlock(h3)) printf(" - Cannot unlock\n");\r
244                         }\r
245                         else {\r
246                                 printf(" - Cannot lock\n");\r
247                         }\r
248 \r
249                         printf("now resizing to 144MB\n");\r
250                         if (himem_sys_realloc(h3,144UL*1024UL)) {\r
251                                 if (himem_sys_get_handle_info(h3,&binf)) {\r
252                                         printf("Handle info:\n");\r
253                                         printf("     Lock count=%u  Free handles=%u  Size=%luKB\n",\r
254                                                 (unsigned int)binf.lock_count,\r
255                                                 (unsigned int)binf.free_handles,\r
256                                                 (unsigned long)binf.block_length_kb);\r
257                                 }\r
258                                 else {\r
259                                         printf("Cannot get handle info\n");\r
260                                 }\r
261 \r
262 #if TARGET_MSDOS == 32\r
263                                 if (!himem_sys_move(0/*conventional memory*/,(unsigned long)(tmp),h3,0,16))\r
264 #else\r
265                                 if (!himem_sys_move(0/*conventional memory*/,((unsigned long)FP_SEG(tmp) << 4UL) + (unsigned long)FP_OFF(tmp),h3,0,sizeof(tmp)))\r
266 #endif\r
267                                         printf("Copy didn't work\n");\r
268 \r
269                                 for (i=0;i < 16;i++) printf("%02x ",tmp[i]);\r
270                                 printf("\n");\r
271 \r
272                                 ofs = himem_sys_lock(h3);\r
273                                 if (ofs != 0UL) {\r
274                                         printf(" - Locked: Physical memory address 0x%08lX\n",(unsigned long)ofs);\r
275                                         if (!himem_sys_unlock(h3)) printf(" - Cannot unlock\n");\r
276                                 }\r
277                                 else {\r
278                                         printf(" - Cannot lock\n");\r
279                                 }\r
280                         }\r
281                         else {\r
282                                 printf(" - Cannot realloc\n");\r
283                         }\r
284 \r
285                         if (!himem_sys_free(h3)) printf(" - Free failed\n");\r
286                 }\r
287                 else printf("failed\n");\r
288         }\r
289         else {\r
290                 printf("HIMEM.SYS not found\n");\r
291         }\r
292 \r
293         return 0;\r
294 }\r
295 \r