User:Johnburger/Demo/Boot/CPU

From OSDev Wiki
Jump to navigation Jump to search

The following code checks to make sure that we're not about to start executing '386-specific instructions on a previous-generation CPU. It uses quirks in the different processors to identify which is actually running.

Note that this code isn't strictly necessary, since there's nothing we can really do if the CPU isn't a '386 or above anyway: a JMP $ loop is hardly user friendly! So this will be the first code up against The Wall if I need more room in the Boot sector...

Demo/Boot/CPU.inc

;
; Boot/CPU.inc
;

; Test whether CPU is a '386 (minimum)
Boot.CPU:

                CPU 8086                  ; Assume lowest possible option
; The 8086 pushes a different SP to the others
                PUSH            SP
                POP             AX

                CMP             SP,AX     ; Comparison designed to set Carry too
                JNE             $         ; Uh oh! ***STOP!***

                CPU 286                   ; At least we know it's this good!
; The '286 always clears IOPL
                PUSHF
                POP             AX

                OR              AX,x86.EFlags.IOPL

                PUSH            AX
                POPF

                PUSHF
                POP             CX

                CMP             CX,AX     ; Comparison designed to set Carry too
                JNE             $         ; Uh oh! ***STOP!***

                CPU 386                   ; From now on, we're confident!