summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/SNIP/cpucheck.asm
blob: f5ae682ef16d3e046c4b6cbf76057816e48b510d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
        page    55, 132

;  FUNCTION:  cpu_check
;
;  Attempt to discover the type of CPU. Use MASM 5.1 or greater.
;  Returns 86 for 8088/8086, 286 for 80286, 386 for 80386/80486.
;
;  Requires MASM 5.1 or later or equivalent
;
;  Assemble with:       MASM /Mx /z ...
;                       TASM /jMASM /mx /z ...
;

%       .MODEL  memodel,C               ;Add model support via
                                        ;command line macros, e.g.
                                        ;MASM /Dmemodel=LARGE

        .CODE
;
; int cpu_check(void) - returns 86, 186, 286, 386
;

        PUBLIC  cpu_check

cpu_check       PROC    USES BX
        pushf
        xor     ax,ax                   ; zero ax
        push    ax
        popf                            ; try to put 0 into flags
        pushf
        pop     ax                      ; see what went in flags
        and     ax,0f000h               ; mask off high flag bits
        cmp     ax,0f000h               ; was high nibble ones
        je      _86                     ; is 8086 or 8088
        push    sp                      ; see if sp is updated
        pop     bx                      ; before or after it is
        cmp     bx,sp                   ; pushed
        jne     _186
        mov     ax,0f000h               ; try to set high bits
        push    ax
        popf                            ; in the flags
        pushf
        pop     ax                      ; look at actual flags
        and     ax,0f000h               ; any high bits set?
        je      _286                    ; is 80286
_386:
        .386                            ; enable 386 instructions

        pushfd                          ; save extended flags
        mov     eax,040000h
        push    eax                     ; push 40000h onto stack
        popfd                           ; pop extended flags
        pushfd                          ; push extended flags
        pop     eax                     ; put in eax
        and     eax,040000h             ; is bit 18 set?
        jne     _486                    ; yes, it's a 486
        mov     ax,386                  ; no, it's a 386
        jmp     _386x
_486:
        mov     ax,486
_386x:
        popfd                           ; clean the stack
        jmp     ccexit
_286:
        mov     ax,286                  ; is an 80286
        jmp     ccexit
_186:
        mov     ax,186                  ; is an 80188/80186
        jmp     ccexit
_86:
        mov     ax,86                   ; is an 8088/8086
ccexit:
        popf                            ; restore original flags
        ret                             ; return

cpu_check       ENDP    

        end