; ****************************************************************************
; TRDOS386.ASM (TRDOS 386 Kernel) - v2.0.0 - trfdboot.s
; ----------------------------------------------------------------------------
; Last Update: 12/02/2016
; ----------------------------------------------------------------------------
; Beginning: 25/01/2016
; ----------------------------------------------------------------------------
; Assembler: NASM version 2.11 (trfdboot.s)
; ----------------------------------------------------------------------------
; Turkish Rational DOS
; Operating System Project v2.0 by ERDOGAN TAN (Beginning: 04/01/2016)
;
; Derived from TRDOS Operating System v1.0 (8086) source code by Erdogan Tan
; TRFDBOOT.ASM (31/07/2011)
; ****************************************************************************

; TRFDBOOT.ASM
; Turkish Rational DOS
; Disk Operation System v1.0 Project
; 1.44 MB Floppy Disk Boot Code
;
; Copyright (C) 1998-2011  Erdogan TAN  [ Last Modification: 31/07/2011 ]
; 12/06/2011 -> Boot sector bugfix (dl = bsDriveNumber <-> DS=CS)
; 27/09/2009 -> 'Operation System' has been changed to 'Operating System'
; 2005 -> trfdboot utility
; 1998 -> beginning year of trdos project
;
; ****************************************************************************

bsDriveNumber	equ TRDOS_FAT12_fd_bs + 24h
bsVolumeID	equ TRDOS_FAT12_fd_bs + 27h

[BITS 16]
[ORG 100h]

	cli
	cld
	push	cs
	pop	ss
	mov	sp, 0FFFEh
	sti

;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; see if drive specified
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	mov	si, 80h			; PSP command tail
	mov	cl, [si]
	or	cl, cl                               
	jz	short T_05		; jump if zero

T_01:
	inc	si

	mov	al, [si]
	cmp	al, ' '			; is it SPACE ?
	jne	short T_02

	dec	cl                                  
	jnz	short T_01                  
	jmp	short T_05

T_02:
	cmp	al, '0'			; 0 - 9
	jb	short T_05
	cmp	al, '9'			; allow number for drive
	jna	short T_04                    
	cmp	al, 'A'
	jb	short T_05
	cmp	al, 'Z'			; A - Z
	jna	short T_03                    
	cmp	al, 'a'			; a - z 
	jb	short T_05                  
	cmp	al, 'z'                           
	ja	short T_05                 

	sub	al, 'a'-'A'		; to upper case

;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; get drive code
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

T_03:
	mov	[TrDOS_Drive], al
	jmp	short T_09

T_04:
	add	al, 'A'-'0'		; 0 based -> A based
	jmp	short T_03 

;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; Write message
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

T_05:
	mov	si, TrDOS_Welcome
	call	print_msg
	cmp	cl, 0
        ja      short T_12

T_06:
	mov	si, TrDOS_PressKeyWhenReady
	call	print_msg

T_07:
	xor	ax, ax
	int	16h			; wait for keyboard command
	cmp	al, 'M'-40h		; Enter (OK) key
	je	short T_08		; write
	cmp	al, 'C'-40h
	je	short T_12		; no write (exit)
	cmp	al, 27
	je	short T_12
	jmp	short T_07            

;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; get drive parameters
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

T_08:
	mov	si, TrDOS_CRLF
	call	print_msg

T_09:
	mov	ah, 08h
	mov	dl, [TrDOS_Drive]	; drive
	sub	dl, 'A'			; make it zero based 
	int	13h			; return disk parameters

	push	cs
	pop	es			; restore es

	cmp	bl, 4			; Drive Type
	jne	short T_14
	
T_10:
	mov	byte [RetryCount], 4

T_11:
	xor	ax, ax
	int	1Ah			; get time of day

	mov	si, bsVolumeID 

	mov	[si], dx
	mov	[si+2], cx		; set unique volume ID
	
	mov	ah, 02h			; Return Current Time
	int	1Ah
	xchg	ch, cl
	xchg	dh, dl

	add	cx, dx  
	add	[si+2], cx
		
	mov	ah, 04h			; Return Current Date
	int	1Ah
	xchg	ch, cl
	xchg	dh, dl
	
	add	cx, dx  
	add	[si+2], cx
              
	mov	ax, 0301h		; write to disk
	mov	bx, TRDOS_FAT12_fd_bs	; location of boot code

	mov	cx, 1			; cylinder = 0
					; sector = 1
	mov	dh, 0			; head = 0
	mov	dl, [TrDOS_Drive]	; drive 
	sub	dl, 'A'			; make it zero based
	mov	[bsDriveNumber], dl
	int	13h
	jc	short T_14		; if everything is ok -->

;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; success. try again ?
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	mov	si, TrDOS_disk_WrittenSuccesfully
	call	print_msg

T_12:
	mov	si, TrDOS_CRLF
	call	print_msg
	mov	ax, 4C00h		; terminate
	int	21h

T_13:
	xor	ax, ax
	int	16h			; wait for keyboard command
	cmp	al, 'y'
	je	short T_15		; retry
	cmp	al, 'Y'
	je	short T_15
	cmp	al, 'n'
	je	short T_12 		; exit
	cmp	al, 'N'
	je	short T_12
	cmp	al, 'C'-40h
	je	short T_12                   
	cmp	al, 27
	je	short T_12
	jmp	short T_13

T_14:
	dec	byte [RetryCount]
	jnz	short T_11

	mov	si, TrDOS_disk_NotReadyOrError
	call	print_msg
	jmp	short T_13

T_15:
	mov	si, TrDOS_CRLF
	call	print_msg
	jmp	T_10

print_msg:
T_16:
	lodsb				; Load byte at DS:SI to AL
	and	al, al            
	jz	short T_17       
	mov	ah, 0Eh			
	mov	bx, 07h             
	int	10h			; BIOS Service func ( ah ) = 0Eh
					; Write char as TTY
					; AL-char BH-page BL-color
	jmp     short T_16          

T_17:
	retn


;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;  messages
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

TrDOS_Welcome:
	db 0Dh, 0Ah
	db 'TR-DOS Floppy Disk Boot Sector Rebuilder v4.0'
	db 0Dh, 0Ah
	db '(c) Erdogan TAN 1998-2016'
	db 0Dh,0Ah
	db 0Dh,0Ah
	db 'Usage: trfdboot [drive] '
	db 0

db	"12/02/2016"

TrDOS_PressKeyWhenReady:
	db 0Dh, 0Ah
	db 0Dh, 0Ah
	db 'Press Enter to write boot sector on disk '
TrDOS_Drive:
	db 'A: ', 0

TrDOS_disk_WrittenSuccesfully:
	db 0Dh, 0Ah
	db 'Boot sector updated to TRDOS v2.0 format...'
TrDOS_CRLF:
	db 0Dh, 0Ah, 0

TrDOS_disk_NotReadyOrError:
	db 0Dh, 0Ah
	db 'Disk error or drive not ready. Try again? (Y/N) '
	db 0
align 2
	dw 417

TRDOS_FAT12_fd_bs:	; Boot Sector code

; 29/01/2016
incbin	"TRFDBS.BIN"	; Kernel file: 'TRDOS386.SYS'

RetryCount:
	db 0
