; ****************************************************************************
; TRDOS386.ASM (TRDOS 386 Kernel - v2.0.0) - UNINITIALIZED DATA : trdoskx.s
; ----------------------------------------------------------------------------
; Last Update: 29/07/2016
; ----------------------------------------------------------------------------
; 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)
; ****************************************************************************
; DRV_INIT.ASM [26/09/2009] Last Update: 07/08/2011
; MAINPROG.ASM [17/01/2004] Last Update: 09/11/2011
; DIR.ASM      [17/01/2004] Last Update: 09/10/2011
; CMD_INTR.ASM [29/01/2005] Last update: 09/11/2011
; DRV_FAT.ASM  [07/07/2009] Last update: 21/08/2011

alignb 4

; MAINPROG.ASM
MainProgCfg_FileSize:   resd 1 ; 14/04/2016
MainProgCfg_LineOffset: resd 1 ; 14/04/2016

Current_VolSerial: resd 1

Current_Dir_FCluster: resd 1

Current_Dir_Level: resb 1
Current_FATType: resb 1
Current_Drv: resb 1
Current_Dir_Drv:   resb 1 ; '?'
                   resb 1 ; ':'
Current_Dir_Root:  resb 1 ; '/' 
Current_Directory: resb 90
End_Of_Current_Dir_Str: resb 1
Current_Dir_StrLen: resb 1   

CursorColumn: 	resb 1
CmdArgStart:    resb 1

; 03/02/2016
Remark:		resb 78

CommandBuffer: 	resb 80

TextBuffer:	resb 256

MasterBootBuff:
MasterBootCode: resb 1BEh
PartitionTable: resb 64
MBIDCode: resw 1

PTable_Buffer:
PTable_hd0: resb 64
PTable_hd1: resb 64
PTable_hd2: resb 64
PTable_hd3: resb 64
PTable_ep0: resb 64
PTable_ep1: resb 64
PTable_ep2: resb 64
PTable_ep3: resb 64

scount:	resb 1 ; 16/05/2016 (diskio.s, 'int33h:')	
HD_LBA_yes: resb 1
PP_Counter: resb 1
EP_Counter: resb 1

EP_StartSector: resd 1
                resd 1
                resd 1
                resd 1

DOSBootSectorBuff: resb 512

FAT_BuffDescriptor:
FAT_CurrentCluster: resd 1
FAT_BuffValidData: resb 1
FAT_BuffDrvName: resb 1
FAT_BuffOffset: resw 1
FAT_BuffSector: resd 1

FAT_ClusterCounter: resd 1
LastCluster: resd 1

; 16/05/2016
;; 18/03/2016 (TRDOS v2.0)
;ClusterBuffer_Valid: resb 1

Dir_BuffDescriptor:
DirBuff_DRV: resb 1
DirBuff_FATType: resb 1
DirBuff_ValidData: resb 1
DirBuff_CurrentEntry: resw 1
DirBuff_LastEntry: resw 1
DirBuff_Cluster: resd 1 
DirBuffer_Size: resw 1
;DirBuff_EntryCounter: resw 1

; 01/02/2016
; these are on (real mode) segment 8000h and later
; FAT_Buffer:	resb 1536 ; 3 sectors
; Dir_Buffer:	resb 512*32
; Logical_DOSDisks:  resb 6656 ; 26 * 256 bytes

; 18/01/2016

FreeClusterCount: resd 1

VolSize_Unit1:   resd 1
VolSize_Unit2:   resd 1

Vol_Tot_Sec_Str_Start:	    resd 1
Vol_Tot_Sec_Str: 	    resb 10
Vol_Tot_Sec_Str_End:	    resb 1
resb 1
Vol_Free_Sectors_Str_Start: resd 1
Vol_Free_Sectors_Str:	    resb 10				
Vol_Free_Sectors_Str_End:   resb 1

; 10/02/2016
RUN_CDRV: resb 1 ; CMD_INTR.ASM  ; 09/11/2011

; 24/01/2016
PATH_Array:     resb 128 ; DIR.ASM ; 09/10/2011
; 06/02/2016
CCD_DriveDT:	resd 1 ; DIR.ASM ; (word)
CCD_Level:	resb 1 ; DIR.ASM
Last_Dir_Level:	resb 1 ; DIR.ASM
;
CDLF_AttributesMask: resw 1 ; DIR.ASM
CDLF_FNAddress:	resd 1 ; DIR.ASM (word)
CDLF_DEType:	resw 1 ; DIR.ASM
;
CD_COMMAND:	resb 1 ; DIR.ASM

alignb 4

; 29/01/2016
Program_Exit:	resb 1 ; CMD_INTR.ASM  ; 09/11/2011

;alignb 4
; 23/02/2016
disk_rw_op:	resb 1 ;  0 = disk read, 1 = disk write
;disk_rw_spt:	resb 1 ; sectors per track (<= 63) /// (<256)
; 31/01/2016
retry_count: 	resb 1 ; DISK_IO.ASM ; 20/07/2011 (CHS_RetryCount)
disk_rw_err: 	resb 1 ; DISK_IO.ASM ; (Disk_IO_err_code)
sector_count:	resd 1 ; DISK_IO.ASM ; (Disk_RW_SectorCount)

; 06/02/2016 (long name)
FDE_AttrMask:	   resw 1 ; DIR.ASM
AmbiguousFileName: resw 1 ; DIR.ASM
PreviousAttr:	   resb 1 ; DIR.ASM
;	
LongNameFound:   resb 1	  ; DIR.ASM
LFN_EntryLength: resb 1   ; DIR.ASM
LFN_CheckSum:    resb 1   ; DIR.ASM
LongFileName:    resb 132 ; DIR.ASM

;PATH_Array_Ptr: resw 1 ; DIR.ASM
PATH_CDLevel:	 resb 1 ; DIR.ASM
PATH_Level:	 resb 1 ; DIR.ASM

; 07/02/2016
Dir_File_Name:	resb 13 ; DIR.ASM ; 09/10/2011

; 10/02/2016
Dir_Entry_Name:	resb 13 ; DIR.ASM

alignb 2

AttributesMask: resw 1 ; CMD_INTR.ASM ; 09/11/2011

; 10/02/2016 (128 bytes -> 126 bytes)
; 08/02/2016
;FFF Structure (128 bytes) ; DIR.ASM ; 09/10/2011
FindFile_Drv:		  resb 1
FindFile_Directory:	  resb 65
FindFile_Name:		  resb 13
FindFile_LongNameEntryLength:
FindFile_LongNameYes: 	  resb 1 ; Sign for longname procedures
;Above 80 bytes form
;TR-DOS Source/Destination File FullName Format/Structure
FindFile_AttributesMask:  resw 1
FindFile_DirEntry:	  resb 32
FindFile_DirFirstCluster: resd 1
FindFile_DirCluster:	  resd 1
FindFile_DirEntryNumber:  resw 1
FindFile_MatchCounter:	  resw 1
FindFile_Reserved:	  resw 1 ; 06/03/2016

First_Path_Pos: resd 1	; DIR.ASM ; 09/10/2011
Last_Slash_Pos: resd 1	; DIR.ASM 

; 10/02/2016
File_Count:     resw 1 	; DIR.ASM ; 09/10/2011
Dir_Count:      resw 1
Total_FSize:    resd 1
TFS_Dec_Begin:  resd 1
                resb 10
TFS_Dec_End:    resb 1

PrintDir_RowCounter: resb 1

alignb 4
; 15/02/2015 ('show' command variables)
Show_FDT:	resd 1
Show_LDDDT:	resd 1
Show_Cluster:	resd 1
Show_FileSize:	resd 1
Show_FilePointer: resd 1
Show_ClusterPointer: resw 1
Show_ClusterSize: resw 1
Show_RowCount:	resb 1

alignb 4
; 21/02/2016
DelFile_FNPointer:	resd 1 ; ; CMD_INTR.ASM (word) ; 09/11/2011
; 27/02/2016
; DIR.ASM (09/10/2011)
DelFile_FCluster:	resd 1
DelFile_EntryCounter:	resw 1
DelFile_LNEL:		resb 1
resb 1

; DIR.ASM
mkdir_DirName_Offset: 	resd 1
mkdir_FFCluster:	resd 1
mkdir_LastDirCluster:	resd 1
mkdir_FreeSectors:	resd 1
mkdir_attrib:		resw 1 
mkdir_SecPerClust:	resb 1
mkdir_add_new_cluster:	resb 1
mkdir_Name:		resb 13
resw 1 ; 01/03/2016
; 27/02/2016
RmDir_MultiClusters:	resb 1  
RmDir_DirEntryOffset:	resd 1 ; 01/03/2016 (word -> dword)
RmDir_ParentDirCluster: resd 1
RmDir_DirLastCluster:   resd 1
RmDir_PreviousCluster:  resd 1
; 22/02/2016
UPDLMDT_CDirLevel:	resb 1
UPDLMDT_CDirFCluster:	resd 1
	
alignb 4
; DRV_FAT.ASM ; 21/08/2011
gffc_next_free_cluster:  resd 1
gffc_first_free_cluster: resd 1
gffc_last_free_cluster:  resd 1

;29/04/2016
Cluster_Index: ; resd 1
; 22/02/2016
ClusterValue:	resd 1
; 04/03/2016
Attributes:	resb 1 
;;CFS_error:  resb 1 ;; 01/03/2016
resb 1
CFS_OPType: resb 1
CFS_Drv:    resb 1
CFS_CC:	    resd 1
CFS_FAT32FSINFOSEC: resd 1
CFS_FAT32FC: resd 1

; 27/02/2016
;alignb 4
glc_prevcluster: resd 1 ; DRV_FAT.ASM (21/08/2011)

; DIR.ASM
DLN_EntryNumber: resw 1
DLN_40h:	 resb 1
; 28/02/2016
TCC_FATErr:	 resb 1 ; DRV_FAT.ASM

alignb 4
; DIR.ASM (09/10/2011)
LCDE_EntryIndex: resw 1 ; LCDE_EntryOffset
LCDE_ClusterSN:  resw 1
LCDE_Cluster: 	 resd 1
LCDE_ByteOffset: resd 1

;alignb4
; 06/03/2016 (word -> dword)
; CMD_INTR.ASM (01/08/2010)
SourceFilePath:	     resd 1
DestinationFilePath: resd 1

;alignb 4
; 06/03/2016
; FILE.ASM (09/10/2011)
;Source File Structure (same with 'Find File' Structure)
SourceFile_Drv:			resb 1
SourceFile_Directory:		resb 65
SourceFile_Name:		resb 13
SourceFile_LongNameEntryLength: 
SourceFile_LongNameYes:		resb 1 ; Sign for longname procedures
;Above 80 bytes
;is TR-DOS Source File FullName Format/Structure
SourceFile_AttributesMask:	resw 1
SourceFile_DirEntry:		resb 32
SourceFile_DirFirstCluster:	resd 1
SourceFile_DirCluster:		resd 1
SourceFile_DirEntryNumber:	resw 1
SourceFile_MatchCounter:	resw 1
; 16/03/2016
SourceFile_SecPerClust:		resb 1
SourceFile_Reserved:		resb 1
; Above is 128 bytes

;Destination File Structure (same with 'Find File' Structure)
DestinationFile_Drv:		resb 1
DestinationFile_Directory: 	resb 65
DestinationFile_Name:		resb 13
DestinationFile_LongNameEntryLength:
DestinationFile_LongNameYes:	resb 1 ; Sign for longname procedures
;Above 80 bytes
;is TR-DOS Destination File FullName Format/Structure
DestinationFile_AttributesMask: resw 1
DestinationFile_DirEntry:	resb 32
DestinationFile_DirFirstCluster: resd 1
DestinationFile_DirCluster:	resd 1
DestinationFile_DirEntryNumber: resw 1
DestinationFile_MatchCounter:	resw 1
; 16/03/2016
DestinationFile_SecPerClust:	resb 1
DestinationFile_Reserved:	resb 1
; Above is 128 bytes

; 24/04/2016
resw 1

; 10/03/2016
; FILE.ASM
move_cmd_phase:	   resb 1
msftdf_sf_df_drv:  resb 1
msftdf_drv_offset: resd 1

; 11/03/2016
; DRV_FAT.ASM (21/08/2011)
FAT_anc_LCluster:  resd 1
FAT_anc_FFCluster: resd 1

;alignb 4

; 14/03/2016
; TRDOS 386 = TRDOS v2.0 feature only !
; 'allocate_memory_block' in 'memory.s'
mem_ipg_count:	resd 1 ; page count (for contiguous allocation)
mem_pg_count:	resd 1 ; page count (for count down)
mem_aperture:	resd 1 ; contiguous free pages (current)
mem_max_aperture: resd 1 ; maximum value of contiguous free pages
mem_pg_pos:	resd 1 ; mem. position (page #) of current aperture
mem_max_pg_pos: resd 1 ; mem. position (page #) of max. aperture

; 15/03/2016
; FILE.ASM ('copy_source_file_to_destination_file')
copy_cmd_phase:       resb 1
csftdf_rw_err:	      resb 1
DestinationFileFound: resb 1
csftdf_cdrv: 	      resb 1
csftdf_filesize:      resd 1
; TRDOS386 (TRDOS v2.0)
csftdf_sf_mem_addr:   resd 1
csftdf_sf_mem_bsize:  resd 1
;

csftdf_sf_cluster:    resd 1 ; 16/03/2016
csftdf_df_cluster:    resd 1
; 16/03/2016
csftdf_r_size:        resd 1
csftdf_w_size:        resd 1
csftdf_sf_rbytes:     resd 1
csftdf_df_wbytes:     resd 1
csftdf_percentage:    resb 1
; 17/03/2016
csftdf_videopage:     resb 1
csftdf_cursorpos:     resw 1
csftdf_sf_drv_dt:     resd 1
csftdf_df_drv_dt:     resd 1

; 21/03/2016
; 20/03/2016
; FILE.ASM
createfile_Name_Offset:  resd 1
createfile_FreeSectors:  resd 1 
createfile_size:         resd 1
createfile_FFCluster:    resd 1 ; 11/03/2016
createfile_LastDirCluster: resd 1
createfile_Cluster:      resd 1
createfile_PCluster:     resd 1
createfile_attrib:	 resb 1
createfile_SecPerClust:  resb 1
createfile_DirIndex:     resw 1
createfile_CCount:	 resd 1
createfile_BytesPerSec:	 resw 1 ; 23/03/2016
createfile_wfc:	         resb 1
createfile_UpdatePDir:	 resb 1 ; 31/03/2016

;alignb 4

; 11/04/2016
env_var_length:	   resw 1

alignb 4

; 25/04/2016
readi.valid:	resb 1 ; valid data (>0 = valid for readi)
readi.drv:	resb 1 ; drive number (0, 1,2,3,4..)
readi.spc:	resb 1 ; sectors per cluster for 'readi' drive
readi.s_index:  resb 1 ; sector index in current cluster (buffer)
readi.sector:	resd 1 ; current disk sector
readi.bpc:	resw 1 ; bytes per cluster - 1
readi.offset:	resw 1 ; byte offset in cluster buffer
readi.cluster:  resd 1 ; current cluster number
readi.c_index:	resd 1 ; cluster index of the current cluster (0,1,2,3..)
readi.fclust:	resd 1 ; first cluster of the current cluster
readi.fs_index: resd 1 ; sector index in disk/file section (for Singlix FS)
;readi.buffer:	resd 1 ; readi sector buffer address

writei.valid:	resb 1 ; valid data (>0 = valid for writei)
writei.drv:	resb 1 ; drive number (0, 1,2,3,4..)
writei.spc:	resb 1 ; sectors per cluster for 'writei' drive
writei.s_index: resb 1 ; sector index in current cluster (buffer)
writei.sector:	resd 1 ; current disk sector
writei.bpc:	resw 1 ; bytes per cluster - 1
writei.offset:	resw 1 ; byte offset in cluster buffer
writei.cluster: resd 1 ; current cluster number
writei.c_index:	resd 1 ; cluster index of the current cluster (0,1,2,3..)
writei.fclust:  resd 1 ; first cluster of the current cluster
writei.fs_index: resd 1 ; sector index in disk/file section (for Singlix FS)
;writei.buffer:	resd 1 ; writei sector buffer address

; 29/04/2016
Run_CDirFC:	resd 1
Run_Auto_Path:	resb 1
Run_Manual_Path: resb 1 ; 0 -> auto path sequence needed
EXE_ID:		resb 1	
EXE_dot:	resb 1

; 06/05/2016
mainprog_return_addr: resd 1
last_error:	resd 1  ; this will be used to return error code to MainProg
			; 'lasterror' keyword will be used later to get the
			; last error code/number/status.
; 12/05/2016
video_eax:	resd 1  ; eax return value of video function

; 01/06/2016
user_buffer:	resd 1  ; 'diskio.s' (INT 33h, Function 08h, floppy disk type)

; 21/05/2016 - TRDOS 386 ('swap/switch', 'rswap', [u.pri])
priority:	resb 1  ; running priority level of process (0,1,2)
			; (run queue which is process comes from)
; 22/05/2016 - TRDOS 386 ('set_run_sequence', 'rtc_int', 'u_timer')
p_change:	resb 1  ; process change status (for timer events)
; 23/05/2016 - TRDOS 386 ('clock')
multi_tasking:	resb 1   ; Multi Tasking status (0 = disabled, >0 = enabled)
			; (EBX will return with user buffer addr or disk type)
; 07/06/2016
timer_events:	resb 1  ; number of (active) timer events, <= 16		

; 24/06/2016
w_str_cmd:	resb 1	; WRITE_STRING command (0,1,2,3) ; video.s
p_crt_mode:	resb 1  ; previous video mode (=3 or 0), backup mark/sign
; 26/06/2016
p_crt_page:	resb 1  ; previous active page (for 'set_mode')
; 04/07/2016
noclearmem:	resb 1  ; if set, 'SET MODE' (INT 31h) function (AH = 4)
			; will not clear the video memory
			; (usable for graphics modes only)
alignb 2
CRT_LEN:	resw 1  ; length of regen buffer in bytes
cursor_pposn:	resw 8  ; cursor positions backup

; 10/07/2016 ('VGA_FONT_SETUP', INT 43H address for x86 real mode bios)
VGA_INT43H:	resd 1	; 0 = default (not configured by user)
			; 0FFFFFFFFh = user defined fonts
			; address:
			; 	vgafont8
			; 	vgafont16
			; 	vgafont14

; 25/07/2016
VGA_MTYPE:	resb 1  ; 0=CTEXT,1=MTEXT,2=CGA,3=PLANAR1,4=PLANAR4,5=LINEAR 