;   coreload.asm, Copyright (c) 1997-1998 by Lukas Ruf,
;   Swiss Federal Institute of Technology,
;   Computer Engineering and Networks Laboratory.
;
;   TOPSY -- A Teachable Operating System.
;            Implementation of a tiny and simple
;            micro kernel for teaching purposes.
;
;   For further information, please visit http://www.tik.ee.ethz.ch/~topsy
;
;   This software is provided under the terms of the GNU General Public Licence.
;   A full copy of the GNU GPL is provided in the file COPYING found in the 
;   development root of Topsy.
;
;       This copyright notice supercedes all originally or previously used 
;       copyrights being used within the source code.
;       
;       Author: Lukas Ruf, lr@lpr.ch


;Topsy i386                                                Lukas Ruf, August 1997',0Dh,0Ah
;--------------------------------------------------------------------------------',0Dh,0Ah,00h

; Rewrite of TASM code into as86 code.

        ;.model tiny
        ;.386p
        ;.code
        
        .TEXT

        ORG     0x0000

INIT:

        ; jmp     START
        BR      START

        ;////////////////////////////////////////
        ;// Set IDT, GDT for Switching
        ;////////////////////////////////////////
        INCLUDE coreload.inc

        ;IDTptr          DW #IDT_SIZE             ;LIMIT IDT Slots
        ;                DD #IDT_BASE             ;BASE (Linear)

        ;GDTptr          DW #GDTxSIZE             ;LIMIT GDT Slots
        ;                DD #GDT_BASE             ;BASE (Linear) 

        ; Create the IDT and GDT pointer spaces.
        ; Caution: the memory location are not yet initialized!!
        IDTptr:         .word IDT_SIZE 
                        .long IDT_BASE

        GDTxSIZE = 6 * 8
        GDTptr:         .word GDTxSIZE
                        .long GDT_BASE
        
        ; set the netbootflag to netboot = 1 or floppy boot = 0
#ifdef NETBOOT
        BOOTNET:        .word 1
#else
        BOOTNET:        .word 0
#endif

        ;###################################################################
        ;The include files should remain at exactly this location 
        ;###################################################################
        INCLUDE structur.inc
        INCLUDE coredisk.inc
        INCLUDE corescrn.inc
        INCLUDE waitkey.inc

        INCLUDE coreload.msg
        INCLUDE verify.inc
        INCLUDE a20gate.inc
        INCLUDE pm_swtch.inc
        INCLUDE rm_load.inc
        INCLUDE maskirq.inc
        INCLUDE creatgdt.inc
        INCLUDE rmreboot.inc



.align 16
START:

        PUSH    CS                      ;  Data Segment = Code Segment
        POP     DS

; Stak init from coreboot is done here again, since netbooting justs
; calls coreload and depends on the stack (however, no data is passed
; from coreboot to coreload so the stack can be reinitialized
; this makes life much easyer :-)

        CLD
        CLI

        MOV     AX, #STACKSEG
        MOV     SS, AX
        MOV     AX, #STACKTOP
        MOV     SP, AX

        STI

        CMP	BOOTNET,#0
        JE	InitFinished
        
        ;CALL    InitVideo       ; somehow not necessary (maybe
                                ; etherboot does the job?!)
        CALL    rmClear         ; clear the screen

; finished init from coreboot
InitFinished:
        MOV     AX,#4*160
        MOV     XYOffs,AX
        
        MOV     SI,#Message
        PUSH    SI
        CALL    rmWrite

        CALL    Check_Processor         ; Verify min. 386

        MOV     SI,#is386e
        PUSH    SI
        CALL    rmWrite

        CALL    Check_Mode              ; make sure to be loaded in Real Mode

        MOV     SI,#isreal
        PUSH    SI
        CALL    rmWrite

        CMP     BOOTNET,#1              ; kernel & user have been copied to 
        JE      NoDiskLoading           ; memory by etherboot

        ; Load Kernel
        MOV     SI,#K_LD_MSG
        PUSH    SI
        CALL    rmWrite
        MOV     AX,#KERNOFS
        PUSH    AX
        MOV     AX,#KERNSEG
        PUSH    AX
        MOV     AX,#KERN_TRACK
        PUSH    AX
        MOV     AX,#KERN_HEAD
        PUSH    AX
        CALL    RM_LOAD

        ; Load User
        MOV     SI,#U_LD_MSG
        PUSH    SI
        CALL    rmWrite
        MOV     AX,#USEROFS
        PUSH    AX
        MOV     AX,#USERSEG
        PUSH    AX
        MOV     AX,#USER_TRACK
        PUSH    AX
        MOV     AX,#USER_HEAD
        PUSH    AX
        CALL    RM_LOAD            ; Load Kernel from Disk and save KernelSize

NoDiskLoading:
        CLI                        ; Disable Interrupts

        MOV     SI,#try_Init_A20
        PUSH    DS
        PUSH    SI
        CALL    rmWrite

        CALL    Enable_A20         ; Turn Gate A20 on
        JC      BootPanic

        MOV     SI,#ok_Init_A20
        PUSH    DS
        PUSH    SI
        CALL    rmWrite

        MOV     SI,#try_Mask_IRQ
        PUSH    DS
        PUSH    SI
        CALL    rmWrite

        CALL    MASK_ALL_IRQ       ; Disable all IRQs

        MOV     SI,#ok_Mask_IRQ
        PUSH    DS
        PUSH    SI
        CALL    rmWrite

        MOV     SI,#try_Init_GDT
        PUSH    DS
        PUSH    SI
        CALL    rmWrite

        CALL    Create_GDT         ; Set Startup GDT

        MOV     SI,#ok_Init_GDT
        PUSH    DS
        PUSH    SI
        CALL    rmWrite

        MOV     SI,#try_Init_PM
        PUSH    DS
        PUSH    SI
        CALL    rmWrite

        CALL    PM_Switch          ; Turn Protected Mode on and jump into
                                   ; GNU-Kernel
; ///////////////////////////
; // Never Return here !!! //
; ///////////////////////////
BootPanic:
        HLT
        JMP     BootPanic

;Prg_End LABEL   WORD

        ;END     Init
        END

