; ****************************************************************************
; TRDOS386.ASM (TRDOS 386 Kernel - v2.0.0) - SYS INIT : trdosk1.s
; ----------------------------------------------------------------------------
; Last Update: 23/01/2017
; ----------------------------------------------------------------------------
; Beginning: 04/01/2016
; ----------------------------------------------------------------------------
; Assembler: NASM version 2.11 (trdos386.s)
; ----------------------------------------------------------------------------
; Derived from TRDOS Operating System v1.0 (8086) source code by Erdogan Tan
; TRDOS2.ASM (09/11/2011)
; ****************************************************************************
; TRDOS2.ASM (c) 2004-2011 Erdogan TAN [ 17/01/2004 ] Last Update: 09/11/2011
;

sys_init:
	; 23/01/2017
	; 07/05/2016
	; 02/05/2016
	; 24/04/2016
	; 14/04/2016
	; 13/04/2016
	; 30/03/2016
	; 24/01/2016
	; 06/01/2016
	; 04/01/2016

	; 23/01/2017 - reset timer frequency (to 18.2Hz)
	mov	al, 00110110b ; 36h
 	out	43h, al
	xor	eax, eax  ; sub	al, al ; 0
	out	40h, al ; LB
	out	40h, al ; HB
 	; 
	; 30/03/2016
	; Clear Logical DOS Disk Description Tables Area
	;xor	eax, eax
	mov	edi, Logical_DOSDisks
	mov	ecx, 6656/4 ; 26*256 = 6656 bytes
	rep	stosd ; 1664 times 4 bytes

	mov	eax, '?:/'
	mov	[Current_Dir_Drv], eax

	; Logical DRV INIT (only for hard disks)
	call 	ldrv_init  ; trdosk2.s
	
	; When floppy_drv_init call is disabled
	; media changed sign is needed
	; for proper drive initialization
        
	mov 	esi, Logical_DOSDisks
	mov 	al, 1 ; Initialization sign (invalid_fd_parameter)
	add 	esi, LD_MediaChanged ; Media Change Status = 1 (init needed)
	mov 	[esi], al ; A:
	add 	esi, 100h 
	mov 	[esi], al ; B: 
           
_current_drive_bootdisk:
	mov 	dl, [boot_drv] ; physical drive number
	cmp 	dl, 0FFh
	je 	short _last_dos_diskno_check
_boot_drive_check:
	cmp 	dl, 80h
	jb 	short _current_drive_a
	sub 	dl, 7Eh ; C = 2 , D = 3
	jmp 	short _current_drive_a 

_last_dos_diskno_check:
	mov 	dl, [Last_DOS_DiskNo]
	cmp 	dl, 2
	ja 	short _current_drive_c
	je 	short _current_drive_a
	xor 	dl, dl ; A:
	jmp 	short _current_drive_a

_current_drive_c:
	mov 	dl, 2 ; C:

_current_drive_a:
	mov	[drv], dl
        mov     esi, msg_CRLF_temp
	call 	print_msg

	mov	dl, [drv]
	call 	change_current_drive
	jnc 	short _start_mainprog

_drv_not_ready_error: 
	mov 	esi, msgl_drv_not_ready
	call 	print_msg
        jmp     _end_of_mainprog

_start_mainprog:
	; 07/01/2017
	; 07/05/2016
	; 02/05/2016
	; 24/04/2016
	; Retro UNIX 386 v1, 'sys_init' (u0.s)
	; 23/06/2015

	; 02/05/2016
	; 24/04/2016
	mov	ax, 1
	mov	[u.uno], al
	mov	[mpid], ax
	mov	[p.pid], ax
	mov	[p.stat], al
	mov	byte [u.quant], time_count  ; 07/01/2017
	;
	mov	eax, [k_page_dir]
	mov	[u.pgdir], eax ; reset
	;
	call	allocate_page
	jc	panic
	mov	[u.upage], eax ; user structure page	
	mov	[p.upage], eax
	call	clear_page
	;
	; 24/08/2015
	dec 	byte [sysflg] ; FFh = ready for system call
			      ; 0 = executing a system call
	; 13/04/2016
	; Clear Environment Variables Page/Area
	mov	edi, Env_Page ; 93000h
	mov	ecx, Env_Page_Size / 4 	; 512/4  (4096/4)				 	  		 	  
	xor	eax, eax
	rep	stosd

	; 14/04/2016
 	call	mainprog_startup_configuration

        call    dos_prompt
              
_end_of_mainprog:
        mov     esi, msg_CRLF_temp
	call 	print_msg
	mov 	esi, mainprog_Version
	call 	print_msg
	; 24/01/2016
	sub	ah, ah
	call	int16h ; call getch
	jmp	cpu_reset

infinitiveloop: jmp short infinitiveloop

print_msg:
	; 13/05/2016
	; 04/01/2016
	; 01/07/2015
	; 13/03/2015 (Retro UNIX 386 v1)
	; 07/03/2014 (Retro UNIX 8086 v1)
	; (Modified registers: EAX, EBX, ECX, EDX, ESI, EDI)
	;
	mov	bh, [ACTIVE_PAGE] ; 04/01/2016 (ptty)
	;mov	bl, 07h ; Black background, light gray forecolor

	lodsb
pmsg1:
	push 	esi
	;mov	bh, [ACTIVE_PAGE] ; 04/01/2016 (ptty)
	mov	bl, 07h ; Black background, light gray forecolor
	call 	_write_tty
	pop	esi
	lodsb
	and 	al, al
	jnz 	short pmsg1
	retn

clear_screen:
	; 13/05/2016
	; 30/01/2016
	; 24/01/2016
	; 04/01/2016
	movzx	ebx, byte [ACTIVE_PAGE] ; video page number (0 to 7)
	mov 	ah, [ebx+vmode] ; default = 03h (80x25 text)
	cmp	ah, 4
	jb	short cls1
	cmp	ah, 7
	jne	short vga_clear
cls1:
	;mov	bh, bl
	;mov	bl, 7
	cmp	ah, [CRT_MODE] ; current video mode ? 
	;je	short cls2 ; yes (current video mode = 3)
	;;call	set_mode_3 ; set video mode to 3 (& clear screen)
	;;retn
	;jmp	set_mode_3
	jne	set_mode_3
cls2:
	mov	bh, bl ; video page (0 to 7)
	mov	bl, 07h ; attribute to be used on blanked line
	sub 	al, al ; 0 =  entire window
	xor 	cx, cx
	mov 	dx, 184Fh
	call	_scroll_up ; 24/01/2016
	;
	;mov	bh, [ACTIVE_PAGE] ; video page number (0 to 7)
	xor 	dx, dx
	call	_set_cpos ; 24/01/2016 
	;retn
vga_clear:
	retn	

panic:
	; 13/05/2016 (TRDOS 386 = TRDOS v2)
	; 13/03/2015 (Retro UNIX 386 v1)
	; 07/03/2014 (Retro UNIX 8086 v1)
	mov 	esi, panic_msg
	call 	print_msg
key_to_reboot:
        ; 24/01/2016
        sub     ah, ah
        call    int16h ; call   getch
        ; wait for a character from the current tty
	;
	mov	al, 0Ah
	mov	bh, [ptty] ; [ACTIVE_PAGE]
	mov	bl, 07h ; Black background, 
			; light gray forecolor
	call 	_write_tty
	jmp	cpu_reset 

ctrlbrk:
	; 12/11/2015
	; 13/03/2015 (Retro UNIX 386 v1)
	; 06/12/2013 (Retro UNIX 8086 v1)
	;
	; INT 1Bh (control+break) handler		
	;
      	; Retro Unix 8086 v1 feature only!
      	;
	cmp 	word [u.intr], 0
	jna 	short cbrk4
cbrk0:
	; 12/11/2015
	; 06/12/2013
	cmp 	word [u.quit], 0
	jz	short cbrk4
	;
	; 20/09/2013	
	push 	ax
	mov	al, [ptty]
	;
	; 12/11/2015
	;
	; ctrl+break (EOT, CTRL+D) from serial port
	; or ctrl+break from console (pseudo) tty
	; (!redirection!)
	;
	cmp	al, 8 ; serial port tty nums > 7
        jb      short cbrk1 ; console (pseudo) tty
	;	
	; Serial port interrupt handler sets [ptty]
	; to the port's tty number (as temporary).
	;
	; If active process is using a stdin or 
	; stdout redirection (by the shell),
        ; console tty keyboard must be available
	; to terminate running process,
	; in order to prevent a deadlock. 
	;
	push	edx
	movzx	edx, byte [u.uno]
	cmp     al, [edx+p.ttyc-1] ; console tty (rw)
	pop	edx
	je	short cbrk2
cbrk1:
	inc 	al  ; [u.ttyp] : 1 based tty number
	; 06/12/2013
	cmp	al, [u.ttyp] ; recent open tty (r)
	je	short cbrk2	
        cmp     al, [u.ttyp+1] ; recent open tty (w)
	jne	short cbrk3	
cbrk2:
	;; 06/12/2013
	;mov	ax, [u.quit]
	;and	ax, ax
	;jz	short cbrk3
	;
	xor	ax, ax ; 0
	dec	ax
	; 0FFFFh = 'ctrl+brk' keystroke
	mov	[u.quit], ax
cbrk3:
	pop	ax
cbrk4:
	retn
