]> 4ch.mooo.com Git - 16.git/blob - src/tsthimem.c
wwww
[16.git] / src / tsthimem.c
1 /* tsthimem.c
2  *
3  * Test program: HIMEM.SYS functions
4  * (C) 2010-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 <stdio.h>
15 #include <conio.h>
16 #include <dos.h>
17
18 //#include "src/lib/doslib/cpu.h"
19 #include "src/lib/doslib/dos.h"
20 //#include doswin.h>
21 #include "src/lib/doslib/himemsys.h"
22
23 int main() {
24         probe_dos();
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);
30         }
31         else {
32                 printf("Not running under Windows or OS/2\n");
33         }*/
34
35         if (probe_himem_sys()) {
36                 int h1,h2,h3;
37
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));
43
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");
48
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);
55                 printf("\n");
56
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);
62                 printf("\n");
63
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);
68
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");
73
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");
78
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");
83
84                 if (h1 != -1) {
85                         if (!himem_sys_free(h1)) printf(" - Free failed\n");
86                 }
87                 if (h2 != -1) {
88                         if (!himem_sys_free(h2)) printf(" - Free failed\n");
89                 }
90                 if (h3 != -1) {
91                         if (!himem_sys_free(h3)) printf(" - Free failed\n");
92                 }
93
94                 printf("Attempting to alloc 1MB (for writing to): ");
95                 h3 = himem_sys_alloc(1024); /* NTS: This is in KB, not bytes */
96                 if (h3 != -1) {
97                         uint32_t ofs;
98                         unsigned int i;
99                         struct himem_block_info binf;
100 #if TARGET_MSDOS == 32
101                         char *msg;
102                         unsigned char *tmp;
103                         uint16_t tmpsel=0,msgsel=0;
104                         const char *msgref = "Testing 123 hello";
105 #else
106                         unsigned char tmp[16];
107                         const char *msg = "Testing 123 hello";
108 #endif
109
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);
116 #endif
117
118                         printf("ok, handle %u\n",h3);
119
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);
126                         }
127                         else {
128                                 printf("Cannot get handle info\n");
129                         }
130
131 #if TARGET_MSDOS == 32
132                         if (!himem_sys_move(h3,0,0/*conventional memory*/,(unsigned long)(msg),16))
133 #else
134                         if (!himem_sys_move(h3,0,0/*conventional memory*/,((unsigned long)FP_SEG(msg) << 4UL) + (unsigned long)FP_OFF(msg),sizeof(tmp)))
135 #endif
136                                 printf("Copy didn't work\n");
137
138                         for (i=0;i < 16;i += 2) {
139                                 tmp[i+0] = 0x55;
140                                 tmp[i+1] = 0xAA;
141                         }
142
143 #if TARGET_MSDOS == 32
144                         if (!himem_sys_move(0/*conventional memory*/,(unsigned long)(tmp),h3,0,16))
145 #else
146                         if (!himem_sys_move(0/*conventional memory*/,((unsigned long)FP_SEG(tmp) << 4UL) + (unsigned long)FP_OFF(tmp),h3,0,sizeof(tmp)))
147 #endif
148                                 printf("Copy didn't work\n");
149
150                         for (i=0;i < 16;i++) printf("%02x ",tmp[i]);
151                         printf("\n");
152
153                         ofs = himem_sys_lock(h3);
154                         if (ofs != 0UL) {
155                                 printf(" - Locked: Physical memory address 0x%08lX\n",(unsigned long)ofs);
156                                 if (!himem_sys_unlock(h3)) printf(" - Cannot unlock\n");
157                         }
158                         else {
159                                 printf(" - Cannot lock\n");
160                         }
161
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))
166 #else
167                                 if (!himem_sys_move(0/*conventional memory*/,((unsigned long)FP_SEG(tmp) << 4UL) + (unsigned long)FP_OFF(tmp),h3,0,sizeof(tmp)))
168 #endif
169                                         printf("Copy didn't work\n");
170
171                                 for (i=0;i < 16;i++) printf("%02x ",tmp[i]);
172                                 printf("\n");
173
174                                 ofs = himem_sys_lock(h3);
175                                 if (ofs != 0UL) {
176                                         printf(" - Locked: Physical memory address 0x%08lX\n",(unsigned long)ofs);
177                                         if (!himem_sys_unlock(h3)) printf(" - Cannot unlock\n");
178                                 }
179                                 else {
180                                         printf(" - Cannot lock\n");
181                                 }
182                         }
183                         else {
184                                 printf(" - Cannot realloc\n");
185                         }
186
187                         if (!himem_sys_free(h3)) printf(" - Free failed\n");
188
189 #if TARGET_MSDOS == 32
190                         dpmi_free_dos(tmpsel); tmp=NULL;
191                         dpmi_free_dos(msgsel); msg=NULL;
192 #endif
193                 }
194                 else printf("failed\n");
195
196                 printf("Attempting to alloc 129MB (for writing to): ");
197                 h3 = himem_sys_alloc(129UL * 1024UL); /* NTS: This is in KB, not bytes */
198                 if (h3 != -1) {
199                         uint32_t ofs;
200                         unsigned int i;
201                         unsigned char tmp[16];
202                         struct himem_block_info binf;
203                         const char *msg = "Testing 123 hello";
204
205                         printf("ok, handle %u\n",h3);
206
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);
213                         }
214                         else {
215                                 printf("Cannot get handle info\n");
216                         }
217
218 #if TARGET_MSDOS == 32
219                         if (!himem_sys_move(h3,0,0/*conventional memory*/,(unsigned long)(msg),16))
220 #else
221                         if (!himem_sys_move(h3,0,0/*conventional memory*/,((unsigned long)FP_SEG(msg) << 4UL) + (unsigned long)FP_OFF(msg),sizeof(tmp)))
222 #endif
223                                 printf("Copy didn't work\n");
224
225                         for (i=0;i < 16;i += 2) {
226                                 tmp[i+0] = 0x55;
227                                 tmp[i+1] = 0xAA;
228                         }
229
230 #if TARGET_MSDOS == 32
231                         if (!himem_sys_move(0/*conventional memory*/,(unsigned long)(tmp),h3,0,16))
232 #else
233                         if (!himem_sys_move(0/*conventional memory*/,((unsigned long)FP_SEG(tmp) << 4UL) + (unsigned long)FP_OFF(tmp),h3,0,sizeof(tmp)))
234 #endif
235                                 printf("Copy didn't work\n");
236
237                         for (i=0;i < 16;i++) printf("%02x ",tmp[i]);
238                         printf("\n");
239
240                         ofs = himem_sys_lock(h3);
241                         if (ofs != 0UL) {
242                                 printf(" - Locked: Physical memory address 0x%08lX\n",(unsigned long)ofs);
243                                 if (!himem_sys_unlock(h3)) printf(" - Cannot unlock\n");
244                         }
245                         else {
246                                 printf(" - Cannot lock\n");
247                         }
248
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);
257                                 }
258                                 else {
259                                         printf("Cannot get handle info\n");
260                                 }
261
262 #if TARGET_MSDOS == 32
263                                 if (!himem_sys_move(0/*conventional memory*/,(unsigned long)(tmp),h3,0,16))
264 #else
265                                 if (!himem_sys_move(0/*conventional memory*/,((unsigned long)FP_SEG(tmp) << 4UL) + (unsigned long)FP_OFF(tmp),h3,0,sizeof(tmp)))
266 #endif
267                                         printf("Copy didn't work\n");
268
269                                 for (i=0;i < 16;i++) printf("%02x ",tmp[i]);
270                                 printf("\n");
271
272                                 ofs = himem_sys_lock(h3);
273                                 if (ofs != 0UL) {
274                                         printf(" - Locked: Physical memory address 0x%08lX\n",(unsigned long)ofs);
275                                         if (!himem_sys_unlock(h3)) printf(" - Cannot unlock\n");
276                                 }
277                                 else {
278                                         printf(" - Cannot lock\n");
279                                 }
280                         }
281                         else {
282                                 printf(" - Cannot realloc\n");
283                         }
284
285                         if (!himem_sys_free(h3)) printf(" - Free failed\n");
286                 }
287                 else printf("failed\n");
288         }
289         else {
290                 printf("HIMEM.SYS not found\n");
291         }
292
293         return 0;
294 }
295