[ORG  0x7C00]
; 覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧�
KERNELENTRY             EQU             0x00100000  ; Kernel's entry point (1MB Mark)
SIGNATURE               EQU             0xAA55      ; Boot Signature
; 覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧�
%IDEFINE PTR
%IDEFINE OFFSET
; 覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧�
%DEFINE KILOBYTE(KB)    (KB<<10)
%DEFINE MEGABYTE(MB)    (MB<<20)
; 覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧�
  _Jump:                JMP SHORT Boot
  _NOP:                 NOP
  _OEMid                DB              "ASSEMBLY"
  _BytesPerSect         DW              0x0200
  _SectsPerClus         DB              0x0001
  _ResrvedSects         DW              0x0001
  _NumofFATs            DB              0x0002
  _NumRootDirEntries    DW              0x00E0
  _NumSectors           DW              0x0B40
  _MediaType            DB              0x00F0
  _NumFATsectors        DW              0x0009
  _SectorsPerTrack      DW              0x0012
  _NumHeads             DW              0x0002
  _NumHiDDenSects       DD              0x00000000
  _NumSectorsHuge       DD              0x00000000
  _DriveNumber          DB              0x00
  _Reserved             DB              0x00
  _Signature            DB              0x29
  _VolumeID             DB              "    "
  _VolumeLabel          DB              "FLOPPYDRIVE"
  _FSType               DB              "FAT12   "
; 覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧
  PreviousDestination   DD              MEGABYTE(1)
; 覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧
  GDT:
    NULLSEL             EQU             $-GDT
                        DD              0x00000000
                        DD              0x00000000
    CODESEL             EQU             $-GDT
                        DW              0xFFFF
                        DW              0x0000
                        DB              0x00
                        DB              10011010b
                        DB              11001111b
                        DB              0x00
    DATASEL             EQU             $-GDT
                        DW              0xFFFF
                        DW              0x0000
                        DB              0x00
                        DB              10010010b
                        DB              11001111b
                        DB              0x00
  GDT_END:
  GDTR:
    DW      (GDT_END - GDT) - 1
    DD      GDT
; 覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧�
[SECTION .text]
  __ResetFloppy:
    PUSH    AX
    PUSH    DX
    XOR     AX , AX
    XOR     DX , DX
    INT     0x13
    JC      __ResetFloppy
    POP     DX
    POP     AX
    RET
; 覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧�
  Boot:

    CLI


    ; Set all default segment addresses
    MOV     AX , CS
    MOV     DS , AX
    MOV     ES , AX
    MOV     SS , AX
    MOV     FS , AX
    MOV     GS , AX
    MOV     SP , 0xFFFF
    MOV     BYTE PTR [BootDrive] , DL
    

    
    
    ; Enable the A#20 Pin
    .EnableA20Pin:
    .WaitInput1:
      IN      AL , 0x64
      TEST    AL , 0x02
      JNZ     .WaitInput1
    MOV     AL , 0xD0
    OUT     0x64 , AL
    .WaitOutput:
      IN      AL , 0x64
      TEST    AL , 0x01
      JZ      .WaitOutput
    IN      AL , 0x60
    MOV     AH , AL
    .WaitInput2:
      IN      AL , 0x64
      TEST    AL , 0x02
      JNZ     .WaitInput2
    MOV     AL , 0xD1
    OUT     0x64 , AL
    MOV     AL , AH
    OR      AL , 0x02
    OUT     0x60 , AL



    ; Go to Protected Mode and then back to (Un)real Mode
    PUSH    DS
    PUSH    ES
    PUSH    FS
    PUSH    GS
    CLI
    MOV     EAX , CR0
    OR      EAX , 0x00000001
    MOV     CR0 , EAX
    LGDT    [GDTR]
    JMP     $+2
    MOV     AX , DATASEL
    MOV     DS , AX
    MOV     ES , AX
    MOV     FS , AX
    MOV     GS , AX
    MOV     EAX , CR0
    AND     EAX , 0xFFFFFFFE
    MOV     CR0 , EAX
    JMP     $+2
    POP     GS
    POP     FS
    POP     ES
    POP     DS
    
    
    
    
    
    ; Now find the information related to the first file copied to the floppy disk which
    ; Should be the kernel. After that, Byte #60 of the structure contains the DWORD size of
    ; The file. Taking the size of the file and dividing it by 512 (Bytes Per Sector), we will
    ; Get the number of sectors that the file takes on the floppy.
    ; Sector 19 holds the 512 byte information related to the first file
    .ReadDirectoryInformation:
      CALL    __ResetFloppy
      MOV     BX , OFFSET DirectorySector
      MOV     AH , 0x02
      MOV     AL , 0x01
      MOV     CH , 0x00
      MOV     CL , 2
      MOV     DH , 0x01
      MOV     DL , 0x00
      INT     0x13
      JC      .ReadDirectoryInformation
      

      ; Get the size of the file
      MOV     ESI , OFFSET DirectorySector
      MOV     EAX , DWORD PTR [SI + 60]
      SHR     EAX , 0x00000009
      MOV     WORD PTR [FileSectorCount] , AX
      


    ; Sector 16 of Head #1, Cylinder #0 is the starting point of the KERNEL.O file.
    ; Sector 16 of Head #1, Cylinder #0 = LSN(Sector 34)
    ; Now if we start reading the kernel Sector by Sector and put it at the
    ; Physical address of 0x00000500 (Segmented = 0x0000:0x0500). After reading
    ; Each of the sectors (512 bytes), we will transfer that sector to the 1MB mark and up
    ; Therefore, the first sector will be read to 0x00000500 and then written 0x00100000.
    ; The second sector will again be read to 0x00000500 and written to 0x00100200 and so on.
    
    ; The below routine will also adjust the heads and cylinders. When the sector number
    ; Has reached 18 and is going to get to 19, the head numeber should be switched (1->0, 0->1).
    ; When a head number is 1 and is going ot be switched to 0, this means that the cylinder number
    ; should be increased. At this point, the sector number should be set to zero.


  	; AH    = 0x02
  	; AL    = Number of sectors to read
  	; CH    = Cylinder Number
  	; CL    = Sector Number (1-63)
  	; DH    = Head Number
  	; DL    = Drive Number
  	; ES:BX = Buffer
    ; 覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧�
    XOR     AX , AX                                     ; AX = 0x0000
    MOV     ES , AX                                     ; Destination's segment address = 0x0000
    MOV     BX , 0x0500                                 ; ES:BX = 0x0000:0x0500 (Where sectors are read)
    MOV     DH , 0x01                                   ; LSN(Sector 34) is on head #1
    MOV     DL , 0x00                                   ; The drive that we were booted from
    MOV     CH , 0x00                                   ; Cylinder number is zero
    MOV     CL , 0x10                                   ; The sector number is 16
    ; 覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧�
    .ReadKernel:                                        ; Start reading the kernel 1 Sector at a time
    	CALL    __ResetFloppy                             ; Reset the Floppy Disk Controller
    	MOV     AH , 0x02                                 ; Read Sector(s) Function
    	MOV     AL , 0x01                                 ; Read one sector in each interation
    	INT     0x13                                      ; Issue the interrupt
    	JC      .ReadKernel                               ; Do all these again upon failure
    	INC     CL                                        ; Move to the next sector
    	.AdjustCHS:                                       ; Start adjusting CHS
    	  CMP     CL , 0x12                               ; See if sector is greater than 18 now
    	  JBE     .NoNeedForCHSAdjustment                 ; Jump to ... if not
    	  MOV     CL , 0x01                               ; Start the sector from 1 again
    	  XOR     DH , 0x01                               ; Reverse the head (1 to 0) (0 to 1)
    	  JNZ     .NoNeedForCHSAdjustment                 ; See if head is now 0, if not then jump
    	  INC     CH                                      ; If (Head == 0) then Cylinder++
    	.NoNeedForCHSAdjustment:                          ; Here we didn't need to adjust the CHS
    	  MOV     AX , 0x0E23                             ; Print one pound sign as a mark of progression
    	  INT     0x10                                    ; Print the pound sing now
    	  MOV     EAX , ECX                               ; Save the modified CL and CH in EAX
        MOV     ESI , 0x00000500                        ; Start reading from where the sector is written to
        MOV     EDI , DWORD PTR [PreviousDestination]   ; The previous destination (Original = 1MB mark)
        MOV     ECX , 0x00000080                        ; 128 DWORDs = 512 Bytes
        CLD                                             ; Clear the direction flag
        REP     MOVSD                                   ; Transfer the read sector to 1 MB mark and up
        MOV     DWORD PTR [PreviousDestination] , EDI   ; Save the new destination for later iterations
        MOV     ECX , EAX                               ; Restore the previous CL and CH
    	  DEC     WORD PTR [FileSectorCount]              ; Decrement the number of sectors read
    	  JNZ      .ReadKernel                            ; Keep reading while [FileSectorCount] != 0
    ; 覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧�
    	


    [BITS 32]
    JMP     CODESEL:KERNELENTRY



    HLT

    

    TIMES 510-($-$$) DB 0x00
    DW SIGNATURE

  [SECTION .bss]
    FileSectorCount       RESW          1
    BootDrive             RESB          1
    DirectorySector       RESB          512
