]> 4ch.mooo.com Git - 16.git/blob - src/lib/doslib/hw/cpu/gdtlist.c
added a bunch of things~ and midi stuff~
[16.git] / src / lib / doslib / hw / cpu / gdtlist.c
1 /* gdt_list.c
2  *
3  * Test program for gdt_enum.c library.
4  * Dumps the contents of the GDT onto the screen, if possible.
5  * (C) 2009-2012 Jonathan Campbell.
6  * Hackipedia DOS library.
7  *
8  * This code is licensed under the LGPL.
9  * <insert LGPL legal text here>
10  *
11  * Compiles for intended target environments:
12  *   - MS-DOS
13  *   - Windows 3.0/3.1/95/98/ME
14  *   - Windows NT 3.1/3.51/4.0/2000/XP/Vista/7
15  *   - OS/2 16-bit
16  *   - OS/2 32-bit
17  *
18  */
19
20 #include <stdio.h>
21 #include <conio.h> /* this is where Open Watcom hides the outp() etc. functions */
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <assert.h>
25 #include <string.h>
26 #include <fcntl.h>
27 #include <dos.h>
28
29 #include <hw/cpu/cpu.h>
30 #include <hw/dos/dos.h>
31 #include <hw/cpu/gdt_enum.h>
32
33 #ifdef TARGET_WINDOWS
34 # define WINFCON_STOCK_WIN_MAIN
35 # include <hw/dos/winfcon.h>
36 #endif
37
38 int main() {
39         struct cpu_gdtlib_entry ent;
40         unsigned int i,prn=0;
41         int c;
42
43         if (!cpu_gdtlib_init()) {
44                 printf("Unable to init CPU GDT library\n");
45                 return 1;
46         }
47         if (!cpu_gdtlib_read_current_regs()) {
48                 printf("Cannot read current regs in any meaningful way\n");
49                 return 1;
50         }
51
52         printf("GDTR: Limit=0x%04x Base=0x%08lX   LDTR: 0x%04x  ",
53                 (unsigned int)cpu_gdtlib_gdtr.limit,
54                 (unsigned long)cpu_gdtlib_gdtr.base,
55                 (unsigned int)cpu_gdtlib_ldtr);
56
57         /* for reference: print my code and data segment/selector values */
58         {
59                 uint16_t v_cs=0,v_ds=0;
60
61                 __asm {
62                         mov     ax,cs
63                         mov     v_cs,ax
64
65                         mov     ax,ds
66                         mov     v_ds,ax
67                 }
68
69                 printf("CS=%04X DS=%04X\n",v_cs,v_ds);
70         }
71
72         prn = 2;
73         for (i=0;i < cpu_gdtlib_gdt_entries(&cpu_gdtlib_gdtr);i++) {
74                 if (cpu_gdtlib_gdt_read_entry(&ent,i)) {
75                         if (!cpu_gdtlib_empty_gdt_entry(&ent)) {
76                                 printf("%04X: Lim=%08lX Base=%08lX Acc=%02X G=%02X ",
77                                         (unsigned int)i << 3,
78                                         (unsigned long)ent.limit,
79                                         (unsigned long)ent.base,
80                                         ent.access,
81                                         ent.granularity);
82                                 if (cpu_gdtlib_entry_is_special(&ent)) {
83                                         printf("P=%u Other-%u Type=%u PL=%u",
84                                                 (ent.access>>7)&1,
85                                                 (ent.granularity&0x40) ? 32 : 16,
86                                                 (ent.access&0xF),
87                                                 (ent.access>>5)&3);
88                                 }
89                                 else if (cpu_gdtlib_entry_is_executable(&ent)) {
90                                         printf("P=%u CODE-%u C=%u Read=%u PL=%u",
91                                                 (ent.access>>7)&1,
92                                                 (ent.granularity&0x40) ? 32 : 16,
93                                                 ent.access&1,
94                                                 (ent.access>>1)&1,
95                                                 (ent.access>>5)&3);
96                                 }
97                                 else {
98                                         printf("P=%u DATA-%u D=%u RW=%u PL=%u",
99                                                 (ent.access>>7)&1,
100                                                 (ent.granularity&0x40) ? 32 : 16,
101                                                 ent.access&1,
102                                                 (ent.access>>1)&1,
103                                                 (ent.access>>5)&3);
104                                 }
105                                 printf("\n");
106
107                                 if (++prn >= 23) {
108                                         prn = 0;
109                                         do {
110                                                 c = getch();
111                                                 if (c == 27) return 1;
112                                         } while (c != 13);
113                                 }
114                         }
115                 }
116         }
117
118         if (cpu_gdtlib_prepare_to_read_ldt()) {
119                 printf("LDT: Base=%08lX Limit=%08lX\n",
120                                 (unsigned long)cpu_gdtlib_ldt_ent.base,
121                                 (unsigned long)cpu_gdtlib_ldt_ent.limit);
122
123                 for (i=0;i < cpu_gdtlib_ldt_entries(&cpu_gdtlib_ldt_ent);i++) {
124                         if (cpu_gdtlib_ldt_read_entry(&ent,i)) {
125                                 if (!cpu_gdtlib_empty_ldt_entry(&ent)) {
126                                         printf("%04X: Lim=%08lX Base=%08lX Acc=%02X G=%02X ",
127                                                 (unsigned int)(i << 3)+4,
128                                                 (unsigned long)ent.limit,
129                                                 (unsigned long)ent.base,
130                                                 ent.access,
131                                                 ent.granularity);
132                                         if (cpu_gdtlib_entry_is_special(&ent)) {
133                                                 printf("P=%u Other-%u Type=%u PL=%u",
134                                                         (ent.access>>7)&1,
135                                                         (ent.granularity&0x40) ? 32 : 16,
136                                                         (ent.access&0xF),
137                                                         (ent.access>>5)&3);
138                                         }
139                                         else if (cpu_gdtlib_entry_is_executable(&ent)) {
140                                                 printf("P=%u CODE-%u C=%u Read=%u PL=%u",
141                                                         (ent.access>>7)&1,
142                                                         (ent.granularity&0x40) ? 32 : 16,
143                                                         ent.access&1,
144                                                         (ent.access>>1)&1,
145                                                         (ent.access>>5)&3);
146                                         }
147                                         else {
148                                                 printf("P=%u DATA-%u D=%u RW=%u PL=%u",
149                                                         (ent.access>>7)&1,
150                                                         (ent.granularity&0x40) ? 32 : 16,
151                                                         ent.access&1,
152                                                         (ent.access>>1)&1,
153                                                         (ent.access>>5)&3);
154                                         }
155                                         printf("\n");
156
157                                         if (++prn >= 23) {
158                                                 prn = 0;
159                                                 do {
160                                                         c = getch();
161                                                         if (c == 27) return 1;
162                                                 } while (c != 13);
163                                         }
164                                 }
165                         }
166                 }
167         }
168
169         cpu_gdtlib_free();
170         return 0;
171 }
172