; *****************************************************************************
; DRVINFO3.ASM  [ SINGLIX DRV INIT Sample & DRIVE INFO Utility ]
; Copyright (c) 2009  Erdogan TAN  [ 08/03/2009 ] 
; DRVINFO2.ASM  [ TR-DOS DRV INIT Sample & DRIVE INFO Utility ]
; Copyright (c) 2004  Erdogan TAN  [ 29/01/2005 ] 
;
; *****************************************************************************

; Masterboot / Partition Table at Beginning+1BEh
ptBootable       equ 0
ptBeginHead      equ 1
ptBeginSector    equ 2
ptBeginCylinder  equ 3
ptFileSystemName equ 4
ptEndHead        equ 5
ptEndSector      equ 6
ptEndCylinder    equ 7
ptStartSector    equ 8
ptSectors        equ 12


; BIOS Disk Parameters
DPDiskNumber  equ 0h
DPDType       equ 1h
DPReturn      equ 2h
DPHeads       equ 3h
DPCylinders   equ 4h
DPSecPerTrack equ 6h
DPDisks       equ 7h
DPTableOff    equ 8h
DPTableSeg    equ 0Ah
DPNumOfSecs   equ 0Ch

; BIOS INT 13h Extensions (LBA extensions)
; Just After DP Data (DPDiskNumber+)
DAP_PacketSize equ 10h  ; If extensions present, this byte will be >=10h
DAP_Reserved1 equ 11h   ; Reserved Byte 
DAP_NumOfBlocks equ 12h ; Value of this byte must be 0 to 127
DAP_Reserved2 equ 13h   ; Reserved Byte
DAP_Destination equ 14h ; Address of Transfer Buffer as SEGMENT:OFFSET
DAP_LBA_Address equ 18h ; LBA=(C1*H0+H1)*S0+S1-1
                        ; C1= Selected Cylinder Number
                        ; H0= Number Of Heads (Maximum Head Number + 1)
                        ; H1= Selected Head Number
                        ; S0= Maximum Sector Number
                        ; S1= Selected Sector Number
                        ; QUAD WORD
; DAP_Flat_Destination equ 20h ; 64 bit address, if value in 4h is FFFF:FFFFh
                             ; QUAD WORD (Also, value in 0h must be 18h) 
                             ; TR-DOS will not use 64 bit Flat Address

; INT 13h Function 48h "Get Enhanced Disk Drive Parameters"
; Just After DP Data (DPDiskNumber+)
GetDParams_48h equ 20h ; Word. Data Lenght, must be 26 (1Ah) for short data.
GDP_48h_InfoFlag equ 22h ; Word
; Bit 1 = 1 -> The geometry returned in bytes 4-15 is valid.
GDP_48h_NumOfPCyls equ 24h ; Double Word. Number physical cylinders.
GDP_48h_NumOfPHeads equ 28h ; Double Word. Number of physical heads.
GDP_48h_NumOfPSpT equ 2Ch ; Double word. Num of physical sectors per track.
GDP_48h_LBA_Sectors equ 30h ; 8 bytes. Number of physical/LBA sectors.
GDP_48h_BytesPerSec equ 38h ; Word. Number of bytes in a sector.

; SINGLIX Logical Disks
LD_DrvNumber equ 0
LD_DiskType equ 1
LD_PhyDrvNo equ 2
LD_LBAYes equ 3
LD_PartitionEntry equ 4
LD_MediaChanged equ 5
LD_BPB equ 6
LD_bsBytesPerSec   equ 6
LD_bsMediaAttrib   equ 8 
LD_bsFileSystemID  equ 9
LD_bsFSVersionMaj  equ 10
LD_bsFSVersionMin  equ 11
LD_bsStartSector   equ 12 
LD_bsVolumeSize    equ 16   
LD_bsStartupFD     equ 20
LD_bsMATLocation   equ 24
LD_bsRootDirD      equ 28
LD_bsSystemConfFD  equ 32
LD_bsSwapFD        equ 36
LD_bsUndeleteDirD  equ 40
LD_DATAddress      equ 44
LD_DATSectors      equ 48
LD_FreeSectors     equ 52
LD_rdtVolumeSerial equ 60
LD_rdtVolumeName   equ 64
LD_CDDT equ 128
LD_CDDT_DirNumber  equ 136
LD_CDDT_ParentDir  equ 148
LD_CDDT_ParentDirSerial equ 152
LD_CDDT_DirSerial equ 156
LD_CDDT_SubDirlevel equ 161

; 128 bytes
LD_CurrentDirectory equ 256
;256 bytes
; Logical disk parameters table total 512 bytes 


; Valid FS Types
FS_Version1 equ 1

; Cursor Location
CCCpointer equ  0450h   ; BIOS data, current cursor column


Present segment Para 'code'

                assume CS:Present, DS:Present, ES:Present, SS:Present


;±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
;±
;±              PROCEDURE proc_start
;±
;±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±

proc_start      proc    far

                org 100h
start:
                call proc_drv_init  ; DRV_INIT2.ASM

                xor dl, dl
                call floppy_drv_init
                mov al, byte ptr [Current_Singlix_Drv]
                adc al, 0
                push ax
                mov dl, 1
                call floppy_drv_init
                pop ax
                adc al, 0
                mov byte ptr [Current_Singlix_Drv], al

                mov si, offset Logical_FS_Disks
loc_check_singlix_drv_exists:
                cmp byte ptr [SI][LD_bsFileSystemID], 0A1h
                jne short pass_current_singlix_drv

                push si

                call proc_print_drv_info

                mov si, offset Press_Any_Key
                call proc_printmsg

                pop si
                
                xor ah, ah              ; "Press any key to continue"
                int 16h                

                cmp al, 1Bh
                je short end_of_prog_drvinfo

                call proc_clear_screen

                inc byte ptr [Current_Singlix_Drv]

pass_current_singlix_drv:
                mov al, byte ptr [Last_Logical_DiskNo]
                cmp al, byte ptr [Current_Singlix_Drv]
                jb short end_of_prog_drvinfo
                add si, 100h
                jmp short loc_check_singlix_drv_exists

end_of_prog_drvinfo:
                mov si, offset Program_Name
                call proc_printmsg

                mov ax, word ptr [File_Size]
                mov si, offset FileSize_StrPlus5
                mov cx, 10                  
loc_wfsize_rdivide:
                dec si
                xor dx, dx
                div cx
                add dl, '0'
                mov byte Ptr [si], dl
                cmp ax,0
                ja short loc_wfsize_rdivide

                call proc_printmsg

                int 20h

                xor ah, ah              ; "Press any key to continue"
                int 16h                

                int 19h 
     
proc_start      endp

proc_printmsg   proc near

                mov BX, 07
loc_print:
                lodsb                           ; Load byte at DS:SI to AL
                and     AL,AL            
                je      short loc_return        ; If AL = 00h then return
                mov     AH,0Eh                  
                int     10h                     ; BIOS Service func ( ah ) = 0Eh
                                                ; Write char as TTY
                                                ;AL-char BH-page BL-color
                jmp     short loc_print           
loc_return:
                retn

proc_printmsg   endp


proc_clear_screen proc near

                mov ah, 0Fh 
                int 10h
                mov ah, 0
                int 10h

                retn

proc_clear_screen endp

;'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''';
; Rx_DOS  32 bit Divide                                      ;
; (Special version by Erdogan Tan)                           ;
;- - - - - - - - - - - - - - - - - - - - - - - - - -- - - - -;
;                                                            ;
; input -> DX_AX = 32 bit dividend                           ;
; input -> CX = 16 bit divisor                               ;
; output -> DX_AX = 32 bit quotient                          ;
; output -> BX = 16 bit remainder                            ;
;                                                            ;
;  This procedure divides the requested 32 bit number        ;
;  and gives the result in DX, AX and BX (remainder)         ;
;                                                            ;
; Original Procedure by Michael Podanoffsky / Real Time DOS  ;
; (c) Erdogan TAN  1999                     [ RXDOSBIO.ASM ] ;
;............................................................;

Rx_Dos_Div32    proc near

                mov  bx, dx
                xchg ax, bx
                xor  dx, dx
                div  cx         ; at first, divide DX
                xchg ax, bx     ; remainder is in DX
                                ; now, BX has quotient
                                ; save remainder
                div  cx         ; so, DX_AX divided and
                                ; AX has quotient
                                ; DX has remainder
                xchg dx, bx     ; finally, BX has remainder

                retn

Rx_Dos_Div32    endp

;'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''';
; From binary (byte) to hexadecimal (character) converter    ;
;                                                            ;
; input -> AL = byte (binary number) to be converted         ;
; output -> AH = First character of hexadecimal number       ;
; output -> AL = Second character of hexadecimal number      ;
;                                                            ;
; (c) Erdogan TAN  1998 - 1999                               ;
;............................................................;

; 1998

proc_hex        proc    near

                db 0D4h,10h                     ; Undocumented inst. AAM
                                                ; AH = AL / 10h
                                                ; AL = AL MOD 10h
                or AX,'00'                      ; Make it ZERO (ASCII) based

                xchg AH,AL 

; 1999
                cmp AL,'9'
                jna pass_cc_al
                add AL,7
pass_cc_al:
                cmp AH,'9'
                jna pass_cc_ah
                add AH,7
pass_cc_ah:

; 1998
                retn

proc_hex        endp

;'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''';
; 32 bit Multiply                                            ;
;- - - - - - - - - - - - - - - - - - - - - - - - - -- - - - -;
;                                                            ;
; input -> DX_AX = 32 bit multiplier                         ;
; input -> BX = 16 bit number to be multiplied by DX_AX      ;
; output -> BX_DX_AX = 48 bit (16+32 bit) result number      ;
;                                                            ;
; (c) Erdogan TAN  1999                                      ;
;............................................................;

proc_mul32    proc near

              push cx

              mov cx, bx
              mov bx, dx

              mul cx

              xchg ax, bx

              push dx

              mul cx 

              pop cx 

              add ax, cx 
              adc dx, 0

              xchg bx, ax
              xchg dx, bx

              pop cx

              retn

proc_mul32    endp

INCLUDE DRVINIT2.ASM  ; SINGLIX Drive Initialization Procedures

proc_print_drv_info proc near
                ; input -> DS:SI = Logical Disk Parameters table


                mov al, byte ptr [SI][LD_DrvNumber]
                add al, '0'
                mov byte ptr [str_info_drv_name],al

                mov al, byte ptr [SI][LD_PhyDrvNo]
                call proc_hex
                mov word ptr [str_Phy_Drv_Num], ax

                mov al, byte ptr [SI][LD_bsBytesPerSec]
                call proc_hex
                mov word ptr [val_bytespersec]+2, ax
                mov al, byte ptr [SI][LD_bsBytesPerSec]+1
                call proc_hex
                mov word ptr [val_bytespersec], ax

                mov al, byte ptr [SI][LD_bsMediaAttrib]
                call proc_hex
                mov word ptr [str_media_attrib], ax

                mov al, byte ptr [SI][LD_bsFileSystemID]
                call proc_hex
                mov word ptr [str_fsystem_id], ax

                mov al, byte ptr [SI][LD_bsFSVersionMaj]
                add al, '0'
                mov byte ptr [str_fs_ver_major], al
                mov al, byte ptr [SI][LD_bsFSVersionMin]
                add al, '0'
                mov byte ptr [str_fs_ver_minor], al

                mov al, byte ptr [SI][LD_bsStartSector]
                call proc_hex
                mov word ptr [val_startsector]+6, ax
                mov al, byte ptr [SI][LD_bsStartSector]+1
                call proc_hex
                mov word ptr [val_startsector]+4, ax
                mov al, byte ptr [SI][LD_bsStartSector]+2
                call proc_hex
                mov word ptr [val_startsector]+2, ax
                mov al, byte ptr [SI][LD_bsStartSector]+3
                call proc_hex
                mov word ptr [val_startsector], ax

                mov al, byte ptr [SI][LD_bsVolumeSize]
                call proc_hex
                mov word ptr [val_volumesize]+6, ax
                mov al, byte ptr [SI][LD_bsVolumeSize]+1
                call proc_hex
                mov word ptr [val_volumesize]+4, ax
                mov al, byte ptr [SI][LD_bsVolumeSize]+2
                call proc_hex
                mov word ptr [val_volumesize]+2, ax
                mov al, byte ptr [SI][LD_bsVolumeSize]+3
                call proc_hex
                mov word ptr [val_volumesize], ax

                mov al, byte ptr [SI][LD_bsStartupFD]
                call proc_hex
                mov word ptr [val_startupfiled]+6, ax
                mov al, byte ptr [SI][LD_bsStartupFD]+1
                call proc_hex
                mov word ptr [val_startupfiled]+4, ax
                mov al, byte ptr [SI][LD_bsStartupFD]+2
                call proc_hex
                mov word ptr [val_startupfiled]+2, ax
                mov al, byte ptr [SI][LD_bsStartupFD]+3
                call proc_hex
                mov word ptr [val_startupfiled], ax


                mov al, byte ptr [SI][LD_bsMATLocation]
                call proc_hex
                mov word ptr [val_matlocation]+6, ax
                mov al, byte ptr [SI][LD_bsMATLocation]+1
                call proc_hex
                mov word ptr [val_matlocation]+4, ax
                mov al, byte ptr [SI][LD_bsMATLocation]+2
                call proc_hex
                mov word ptr [val_matlocation]+2, ax
                mov al, byte ptr [SI][LD_bsMATLocation]+3
                call proc_hex
                mov word ptr [val_matlocation], ax

                mov al, byte ptr [SI][LD_bsRootDirD]
                call proc_hex
                mov word ptr [val_rootdird]+6, ax
                mov al, byte ptr [SI][LD_bsRootDirD]+1
                call proc_hex
                mov word ptr [val_rootdird]+4, ax
                mov al, byte ptr [SI][LD_bsRootDirD]+2
                call proc_hex
                mov word ptr [val_rootdird]+2, ax
                mov al, byte ptr [SI][LD_bsRootDirD]+3
                call proc_hex
                mov word ptr [val_rootdird], ax

                mov si, offset msg_drive_info
                call proc_printmsg
                retn

proc_print_drv_info endp

                   db 4
                   db 1

Msg_drive_info:
                   db 7
                   db "[ SINGLIX DRIVE ] "
str_info_drv_name: db "0:"
                   db  0Dh, 0Ah
                   db  0Dh, 0Ah
                   db "Physical Drive Number :  "
str_phy_drv_num:   dw  0
                   db "h"
                   db 0Dh, 0Ah
                   db "Bytes Per Sector :  "
val_bytespersec:   dd 0
                   db "h"
                   db 0Dh, 0Ah
                   db "Media Attributes :  "
str_media_attrib:  dw 0
                   db "h"
                   db 0Dh, 0Ah
                   db "File System ID :  "
str_fsystem_id:    dw 0
                   db "h"
                   db 0Dh, 0Ah
                   db "FS Version :  "
str_fs_ver_major:  db 0
                   db "."
str_fs_ver_minor:  db 0
                   db 0Dh, 0Ah
                   db "Beginning Sector :  "
val_startsector:   dd 0
                   dd 0
                   db "h"
                   db 0Dh, 0Ah
                   db "Volume Size :  "
val_volumesize:    dd 0
                   dd 0
                   db "h"
                   db 0Dh, 0Ah
                   db "Startup File DT :  "
val_startupfiled:  dd 0
                   dd 0
                   db "h"
                   db 0Dh, 0Ah
                   db "MAT Location :  "
val_matlocation:   dd 0
                   dd 0
                   db "h"
                   db 0Dh, 0Ah
                   db "Root Dir DT :  "
val_rootdird:      dd 0
                   dd 0
                   db "h"
                   db 0Dh, 0Ah
                   db 0Dh, 0Ah
                   db 0
Press_Any_Key:
                   db 0Dh, 0Ah 
                   db "Press any key to continue ..."
                   db 0Dh, 0Ah, 0
Magic_Bytes:
                   db 4
                   db 1
Program_Name:
                   db 7
                   db "[ SINGLIX ] DRIVE INFO "
Version_Str:       db "v1.0  ¸ Erdogan Tan 2009"
                   db 0Dh, 0Ah
                   db "File Size = "
                   db 6 dup(0)
FileSize_StrPlus5: db " bytes"
                   db 0Dh, 0Ah
                   db 0

Current_SINGLIX_Drv: db 0
Logical_FS_Disks:  dd 3328 dup(0)
File_Size:         dw $-254

Present            ends

                   end  start
