3 * Test program: HIMEM.SYS functions
4 * (C) 2010-2012 Jonathan Campbell.
5 * Hackipedia DOS library.
7 * This code is licensed under the LGPL.
8 * <insert LGPL legal text here>
18 //#include "src/lib/doslib/cpu.h"
19 #include "src/lib/doslib/dos.h"
21 #include "src/lib/doslib/himemsys.h"
25 printf("DOS version %x.%02u\n",dos_version>>8,dos_version&0xFF);
26 /*if (detect_windows()) {
27 printf("I am running under Windows.\n");
28 printf(" Mode: %s\n",windows_mode_str(windows_mode));
29 printf(" Ver: %x.%02u\n",windows_version>>8,windows_version&0xFF);
32 printf("Not running under Windows or OS/2\n");
35 if (probe_himem_sys()) {
38 printf("HIMEM.SYS detected. Entry point %04x:%04x. v%x.%02x\n",
39 (unsigned int)((himem_sys_entry >> 16) & 0xFFFFUL),
40 (unsigned int)(himem_sys_entry & 0xFFFFUL),
41 (unsigned int)(himem_sys_version >> 8),
42 (unsigned int)(himem_sys_version & 0xFF));
44 if (himem_sys_flags & HIMEM_F_HMA)
45 printf(" - HMA is present\n");
46 if (himem_sys_flags & HIMEM_F_4GB)
47 printf(" - Extensions are present to address up to 4GB of memory\n");
49 printf("A20 status: %u\n",himem_sys_query_a20());
50 printf("Global A20 line: "); fflush(stdout);
51 printf("en=%d ",himem_sys_global_a20(1)); fflush(stdout);
52 printf("query=%d ",himem_sys_query_a20(1)); fflush(stdout);
53 printf("dis=%d ",himem_sys_global_a20(0)); fflush(stdout);
54 printf("query=%d ",himem_sys_query_a20(1)); fflush(stdout);
57 printf("Local A20 line: "); fflush(stdout);
58 printf("en=%d ",himem_sys_local_a20(1)); fflush(stdout);
59 printf("query=%d ",himem_sys_query_a20(1)); fflush(stdout);
60 printf("dis=%d ",himem_sys_local_a20(0)); fflush(stdout);
61 printf("query=%d ",himem_sys_query_a20(1)); fflush(stdout);
64 himem_sys_update_free_memory_status();
65 printf("Free memory: %luKB (largest block %luKB)\n",
66 (unsigned long)himem_sys_total_free,
67 (unsigned long)himem_sys_largest_free);
69 printf("Attempting to alloc 4KB: ");
70 h1 = himem_sys_alloc(4); /* NTS: This is in KB, not bytes */
71 if (h1 != -1) printf("ok, handle %u\n",h1);
72 else printf("failed\n");
74 printf("Attempting to alloc 64KB: ");
75 h2 = himem_sys_alloc(46); /* NTS: This is in KB, not bytes */
76 if (h2 != -1) printf("ok, handle %u\n",h2);
77 else printf("failed\n");
79 printf("Attempting to alloc 1MB: ");
80 h3 = himem_sys_alloc(1024); /* NTS: This is in KB, not bytes */
81 if (h3 != -1) printf("ok, handle %u\n",h3);
82 else printf("failed\n");
85 if (!himem_sys_free(h1)) printf(" - Free failed\n");
88 if (!himem_sys_free(h2)) printf(" - Free failed\n");
91 if (!himem_sys_free(h3)) printf(" - Free failed\n");
94 printf("Attempting to alloc 1MB (for writing to): ");
95 h3 = himem_sys_alloc(1024); /* NTS: This is in KB, not bytes */
99 struct himem_block_info binf;
100 #if TARGET_MSDOS == 32
103 uint16_t tmpsel=0,msgsel=0;
104 const char *msgref = "Testing 123 hello";
106 unsigned char tmp[16];
107 const char *msg = "Testing 123 hello";
110 #if TARGET_MSDOS == 32
111 tmp = dpmi_alloc_dos(16,&tmpsel);
112 if (tmp == NULL) abort();
113 msg = dpmi_alloc_dos(strlen(msgref)+16,&msgsel);
114 if (msg == NULL) abort();
115 memcpy(msg,msgref,strlen(msgref)+1);
118 printf("ok, handle %u\n",h3);
120 if (himem_sys_get_handle_info(h3,&binf)) {
121 printf("Handle info:\n");
122 printf(" Lock count=%u Free handles=%u Size=%luKB\n",
123 (unsigned int)binf.lock_count,
124 (unsigned int)binf.free_handles,
125 (unsigned long)binf.block_length_kb);
128 printf("Cannot get handle info\n");
131 #if TARGET_MSDOS == 32
132 if (!himem_sys_move(h3,0,0/*conventional memory*/,(unsigned long)(msg),16))
134 if (!himem_sys_move(h3,0,0/*conventional memory*/,((unsigned long)FP_SEG(msg) << 4UL) + (unsigned long)FP_OFF(msg),sizeof(tmp)))
136 printf("Copy didn't work\n");
138 for (i=0;i < 16;i += 2) {
143 #if TARGET_MSDOS == 32
144 if (!himem_sys_move(0/*conventional memory*/,(unsigned long)(tmp),h3,0,16))
146 if (!himem_sys_move(0/*conventional memory*/,((unsigned long)FP_SEG(tmp) << 4UL) + (unsigned long)FP_OFF(tmp),h3,0,sizeof(tmp)))
148 printf("Copy didn't work\n");
150 for (i=0;i < 16;i++) printf("%02x ",tmp[i]);
153 ofs = himem_sys_lock(h3);
155 printf(" - Locked: Physical memory address 0x%08lX\n",(unsigned long)ofs);
156 if (!himem_sys_unlock(h3)) printf(" - Cannot unlock\n");
159 printf(" - Cannot lock\n");
162 printf("now resizing to 2MB\n");
163 if (himem_sys_realloc(h3,2048)) {
164 #if TARGET_MSDOS == 32
165 if (!himem_sys_move(0/*conventional memory*/,(unsigned long)(tmp),h3,0,16))
167 if (!himem_sys_move(0/*conventional memory*/,((unsigned long)FP_SEG(tmp) << 4UL) + (unsigned long)FP_OFF(tmp),h3,0,sizeof(tmp)))
169 printf("Copy didn't work\n");
171 for (i=0;i < 16;i++) printf("%02x ",tmp[i]);
174 ofs = himem_sys_lock(h3);
176 printf(" - Locked: Physical memory address 0x%08lX\n",(unsigned long)ofs);
177 if (!himem_sys_unlock(h3)) printf(" - Cannot unlock\n");
180 printf(" - Cannot lock\n");
184 printf(" - Cannot realloc\n");
187 if (!himem_sys_free(h3)) printf(" - Free failed\n");
189 #if TARGET_MSDOS == 32
190 dpmi_free_dos(tmpsel); tmp=NULL;
191 dpmi_free_dos(msgsel); msg=NULL;
194 else printf("failed\n");
196 printf("Attempting to alloc 129MB (for writing to): ");
197 h3 = himem_sys_alloc(129UL * 1024UL); /* NTS: This is in KB, not bytes */
201 unsigned char tmp[16];
202 struct himem_block_info binf;
203 const char *msg = "Testing 123 hello";
205 printf("ok, handle %u\n",h3);
207 if (himem_sys_get_handle_info(h3,&binf)) {
208 printf("Handle info:\n");
209 printf(" Lock count=%u Free handles=%u Size=%luKB\n",
210 (unsigned int)binf.lock_count,
211 (unsigned int)binf.free_handles,
212 (unsigned long)binf.block_length_kb);
215 printf("Cannot get handle info\n");
218 #if TARGET_MSDOS == 32
219 if (!himem_sys_move(h3,0,0/*conventional memory*/,(unsigned long)(msg),16))
221 if (!himem_sys_move(h3,0,0/*conventional memory*/,((unsigned long)FP_SEG(msg) << 4UL) + (unsigned long)FP_OFF(msg),sizeof(tmp)))
223 printf("Copy didn't work\n");
225 for (i=0;i < 16;i += 2) {
230 #if TARGET_MSDOS == 32
231 if (!himem_sys_move(0/*conventional memory*/,(unsigned long)(tmp),h3,0,16))
233 if (!himem_sys_move(0/*conventional memory*/,((unsigned long)FP_SEG(tmp) << 4UL) + (unsigned long)FP_OFF(tmp),h3,0,sizeof(tmp)))
235 printf("Copy didn't work\n");
237 for (i=0;i < 16;i++) printf("%02x ",tmp[i]);
240 ofs = himem_sys_lock(h3);
242 printf(" - Locked: Physical memory address 0x%08lX\n",(unsigned long)ofs);
243 if (!himem_sys_unlock(h3)) printf(" - Cannot unlock\n");
246 printf(" - Cannot lock\n");
249 printf("now resizing to 144MB\n");
250 if (himem_sys_realloc(h3,144UL*1024UL)) {
251 if (himem_sys_get_handle_info(h3,&binf)) {
252 printf("Handle info:\n");
253 printf(" Lock count=%u Free handles=%u Size=%luKB\n",
254 (unsigned int)binf.lock_count,
255 (unsigned int)binf.free_handles,
256 (unsigned long)binf.block_length_kb);
259 printf("Cannot get handle info\n");
262 #if TARGET_MSDOS == 32
263 if (!himem_sys_move(0/*conventional memory*/,(unsigned long)(tmp),h3,0,16))
265 if (!himem_sys_move(0/*conventional memory*/,((unsigned long)FP_SEG(tmp) << 4UL) + (unsigned long)FP_OFF(tmp),h3,0,sizeof(tmp)))
267 printf("Copy didn't work\n");
269 for (i=0;i < 16;i++) printf("%02x ",tmp[i]);
272 ofs = himem_sys_lock(h3);
274 printf(" - Locked: Physical memory address 0x%08lX\n",(unsigned long)ofs);
275 if (!himem_sys_unlock(h3)) printf(" - Cannot unlock\n");
278 printf(" - Cannot lock\n");
282 printf(" - Cannot realloc\n");
285 if (!himem_sys_free(h3)) printf(" - Free failed\n");
287 else printf("failed\n");
290 printf("HIMEM.SYS not found\n");