OSDN Git Service

new file: 16/modex16.tar.7z
[proj16/16.git] / src / lib / wtest / wtest.c
1 /* tab size = 8 */
2
3 #include "lib\wtest\wtest.h"
4
5 static unsigned char detectcpu()
6 {
7         unsigned char cputype;
8         __asm
9         {
10                 PUSHF                   ; we gonna modify flags, so back them up
11                 ; first check if its 8086/8088 or 80186/80188
12                 PUSHF                   ; FLAGS -> STACK
13                 POP     AX              ; STACK -> AX
14                 AND     AX, 00FFFh      ; clear 12-15 bits (they are always 1 on 8086/8088 and 80186/80188)
15                 PUSH    AX              ; AX -> STACK
16                 POPF                    ; STACK -> FLAGS
17                 ; this is where magic happen
18                 PUSHF                   ; FLAGS -> STACK
19                 POP     AX              ; STACK -> AX
20                 AND     AX, 0F000h      ; 0-11 bits aren't important to us
21                 CMP     AX, 0F000h      ; check if all 12-15 bits are set (simple comparision)
22                 JNE     _286p           ; if no, 286+ CPU
23                 MOV     cputype, 0      ; if yes, we are done, set cputype to 0 and end this
24                 JMP     _done
25         _286p:
26                 ; now check if its 286 or newer
27                 PUSHF                   ; FLAGS -> STACK
28                 POP     AX              ; STACK -> AX
29                 OR      AX, 07000h      ; set 12-14 bits (they are always cleared by 286 if its real mode)
30                 PUSH    AX              ; AX -> STACK
31                 POPF                    ; STACK -> FLAGS
32                 ; this is where magic happen
33                 PUSHF                   ; FLAGS -> STACK
34                 POP     AX              ; STACK -> AX
35                 TEST    AX, 07000h      ; check if at least 1 bit in 12-14 bits range is set (15th won't be set anyway)
36                 JNZ     _386p           ; not all bits clear, its 386+
37                 MOV     cputype, 1      ; all bits clear, its 286, we are done
38                 JMP     _done
39         _386p:
40                 MOV     cputype, 2      ; its 386 or newer, but we don't care if its newer at this point
41         _done:
42                 POPF                    ; restore flags we backed up earlier
43         }
44         return cputype;
45 }
46
47 /*int main(int argc, char **argv)
48 {
49         const char *cpus;
50         unsigned char cput;
51
52         cput = detectcpu();
53         switch(cput)
54         {
55                 case 0: cpus = "8086/8088 or 186/88"; break;
56                 case 1: cpus = "286"; break;
57                 case 2: cpus = "386 or newer"; break;
58                 default: cpus = "internal error"; break;
59         }
60         printf("detected CPU type: %s\n", cpus);
61         return 0;
62 }*/
63