     1                                  ; Temporary Runix kernel v2.0 file for debug - 22/11/2021
     2                                  ; (re-write kernel for test by using previous version without a major defect)
     3                                  ; ****************************************************************************
     4                                  ; UNIX386.ASM (RETRO UNIX 386 Kernel) - v0.2.2.0
     5                                  ; ----------------------------------------------------------------------------
     6                                  ; NASM version 2.15 (unix386.s)
     7                                  ;
     8                                  ; RETRO UNIX 386 (Retro Unix == Turkish Rational Unix)
     9                                  ; Operating System Project (v0.2) by ERDOGAN TAN (Beginning: 24/12/2013)
    10                                  ;
    11                                  ; Derived from 'Retro UNIX 8086 v1' source code by Erdogan Tan
    12                                  ; (v0.1 - Beginning: 11/07/2012)
    13                                  ;
    14                                  ; [ Last Modification: 21/01/2022 ]
    15                                  ;
    16                                  ; Derived from UNIX Operating System (v1.0 for PDP-11) 
    17                                  ; (Original) Source Code by Ken Thompson (1971-1972)
    18                                  ; <Bell Laboratories (17/3/1972)>
    19                                  ; <Preliminary Release of UNIX Implementation Document>
    20                                  ;
    21                                  ; Derived from 'UNIX v7/x86' source code by Robert Nordier (1999)
    22                                  ; UNIX V7/x86 source code: see www.nordier.com/v7x86 for details.
    23                                  ;
    24                                  ; ****************************************************************************
    25                                  
    26                                  ; 24/12/2013
    27                                  
    28                                  ; Entering protected mode:
    29                                  ; Derived from 'simple_asm.txt' source code file and 
    30                                  ; 'The world of Protected mode' tutorial/article by Gregor Brunmar (2003)
    31                                  ; (gregor.brunmar@home.se)
    32                                  ; http://www.osdever.net/tutorials/view/the-world-of-protected-mode
    33                                  ;
    34                                  
    35                                  ; "The Real, Protected, Long mode assembly tutorial for PCs" 
    36                                  ; by Michael Chourdakis (2009) 
    37                                  ; http://www.codeproject.com/Articles/45788/
    38                                  ; http://www.michaelchourdakis.com
    39                                  ;
    40                                  
    41                                  ; Global Descriptor Table:
    42                                  ; Derived from 'head.s" source code of Linux v1.0 kernel
    43                                  ; by Linus Torvalds (1991-1992)
    44                                  ;
    45                                  
    46                                  KLOAD	equ 10000h ; Kernel loading address
    47                                  	; NOTE: Retro UNIX 8086 v1 /boot code loads kernel at 1000h:0000h
    48                                  KCODE	equ 08h	; Code segment descriptor (ring 0)
    49                                  KDATA	equ 10h	; Data segment descriptor (ring 0)
    50                                  ; 19/03/2015
    51                                  UCODE	equ 1Bh ; 18h + 3h  (ring 3)
    52                                  UDATA	equ 23h ; 20h + 3h  (ring 3)
    53                                  ; 24/03/2015
    54                                  TSS	equ 28h	; Task state segment descriptor (ring 0)
    55                                  ; 19/03/2015
    56                                  CORE	equ 400000h  ; Start of USER's virtual/linear address space 
    57                                  		     ; (at the end of the 1st 4MB)
    58                                  ECORE	equ 0FFC00000h ; End of USER's virtual address space (4GB - 4MB)
    59                                  		     ; ULIMIT = (ECORE/4096) - 1 = 0FFBFFh (in GDT)
    60                                  
    61                                  ; 01/01/2022 (Retro UNIX 386 v1.2)
    62                                  ;; 27/12/2013
    63                                  ;KEND	equ KLOAD + 65536 ; (28/12/2013) (end of kernel space)
    64                                  
    65                                  ; IBM PC/AT BIOS ----- 10/06/85 (postequ.inc)
    66                                  ;--------- CMOS TABLE LOCATION ADDRESS'S -------------------------------------
    67                                  CMOS_SECONDS	EQU	00H		; SECONDS (BCD)
    68                                  CMOS_MINUTES	EQU	02H		; MINUTES (BCD)	
    69                                  CMOS_HOURS	EQU	04H		; HOURS (BCD)
    70                                  CMOS_DAY_WEEK	EQU	06H		; DAY OF THE WEEK  (BCD)
    71                                  CMOS_DAY_MONTH	EQU	07H		; DAY OF THE MONTH (BCD) 
    72                                  CMOS_MONTH	EQU	08H		; MONTH (BCD)
    73                                  CMOS_YEAR	EQU	09H		; YEAR (TWO DIGITS) (BCD)
    74                                  CMOS_CENTURY	EQU	32H		; DATE CENTURY BYTE (BCD)
    75                                  CMOS_REG_A	EQU	0AH		; STATUS REGISTER A
    76                                  CMOS_REG_B	EQU	00BH		; STATUS REGISTER B  ALARM
    77                                  CMOS_REG_C	EQU	00CH		; STATUS REGISTER C  FLAGS
    78                                  CMOS_REG_D	EQU	0DH		; STATUS REGISTER D  BATTERY
    79                                  CMOS_SHUT_DOWN	EQU	0FH		; SHUTDOWN STATUS COMMAND BYTE
    80                                  ;----------------------------------------
    81                                  ;	CMOS EQUATES FOR THIS SYSTEM	;
    82                                  ;-----------------------------------------------------------------------------
    83                                  CMOS_PORT	EQU	070H		; I/O ADDRESS OF CMOS ADDRESS PORT
    84                                  CMOS_DATA	EQU	071H		; I/O ADDRESS OF CMOS DATA PORT
    85                                  NMI		EQU	10000000B	; DISABLE NMI INTERRUPTS MASK -
    86                                  					; HIGH BIT OF CMOS LOCATION ADDRESS
    87                                  
    88                                  ; Memory Allocation Table Address
    89                                  ; 05/11/2014
    90                                  ; 31/10/2014
    91                                  MEM_ALLOC_TBL	equ	100000h		; Memory Allocation Table at the end of
    92                                  					; the 1st 1 MB memory space.
    93                                  					; (This address must be aligned
    94                                  					;  on 128 KB boundary, if it will be
    95                                  					;  changed later.)
    96                                  					; ((lower 17 bits of 32 bit M.A.T.
    97                                  					;   address must be ZERO)).
    98                                  					; ((((Reason: 32 bit allocation 
    99                                  					;     instructions, dword steps)))
   100                                  					; (((byte >> 12 --> page >> 5)))  
   101                                  ;04/11/2014	
   102                                  PDE_A_PRESENT	equ	1		; Present flag for PDE
   103                                  PDE_A_WRITE	equ 	2		; Writable (write permission) flag
   104                                  PDE_A_USER	equ	4		; User (non-system/kernel) page flag
   105                                  ;
   106                                  PTE_A_PRESENT	equ	1		; Present flag for PTE (bit 0)
   107                                  PTE_A_WRITE	equ 	2		; Writable (write permission) flag (bit 1)
   108                                  PTE_A_USER	equ	4		; User (non-system/kernel) page flag (bit 2)
   109                                  PTE_A_ACCESS    equ	32		; Accessed flag (bit 5) ; 09/03/2015
   110                                  
   111                                  ; 17/02/2015 (unix386.s)
   112                                  ; 10/12/2014 - 30/12/2014 (0B000h -> 9000h) (dsectrm2.s)
   113                                  DPT_SEGM equ 09000h  ; FDPT segment (EDD v1.1, EDD v3)
   114                                  ;
   115                                  HD0_DPT	 equ 0	    ; Disk parameter table address for hd0
   116                                  HD1_DPT	 equ 32	    ; Disk parameter table address for hd1
   117                                  HD2_DPT	 equ 64	    ; Disk parameter table address for hd2
   118                                  HD3_DPT	 equ 96	    ; Disk parameter table address for hd3
   119                                  
   120                                  
   121                                  ; FDPT (Phoenix, Enhanced Disk Drive Specification v1.1, v3.0)
   122                                  ;      (HDPT: Programmer's Guide to the AMIBIOS, 1993)
   123                                  ;
   124                                  FDPT_CYLS	equ 0 ; 1 word, number of cylinders
   125                                  FDPT_HDS	equ 2 ; 1 byte, number of heads
   126                                  FDPT_TT		equ 3 ; 1 byte, A0h = translated FDPT with logical values
   127                                  		      ; otherwise it is standard FDPT with physical values 	
   128                                  FDPT_PCMP	equ 5 ; 1 word, starting write precompensation cylinder
   129                                  		      ; (obsolete for IDE/ATA drives)
   130                                  FDPT_CB		equ 8 ; 1 byte, drive control byte
   131                                  			; Bits 7-6 : Enable or disable retries (00h = enable)
   132                                  			; Bit 5	: 1 = Defect map is located at last cyl. + 1
   133                                  			; Bit 4 : Reserved. Always 0
   134                                  			; Bit 3 : Set to 1 if more than 8 heads
   135                                  			; Bit 2-0 : Reserved. Alsways 0
   136                                  FDPT_LZ		equ 12 ; 1 word, landing zone (obsolete for IDE/ATA drives)
   137                                  FDPT_SPT	equ 14 ; 1 byte, sectors per track
   138                                  
   139                                  ; Floppy Drive Parameters Table (Programmer's Guide to the AMIBIOS, 1993)
   140                                  ; (11 bytes long) will be used by diskette handler/bios
   141                                  ; which is derived from IBM PC-AT BIOS (DISKETTE.ASM, 21/04/1986).						 	  		 	  
   142                                  
   143                                  [BITS 16]       ; We need 16-bit intructions for Real mode
   144                                  
   145                                  [ORG 0] 
   146                                  
   147                                  KSTART: ; 01/01/2022
   148                                  
   149                                  	; 12/11/2014
   150                                  	; Save boot drive number (that is default root drive)
   151 00000000 8816[AA6A]              	mov	[boot_drv], dl ; physical drv number
   152                                  
   153                                  	; Determine installed memory
   154                                  	; 31/10/2014
   155                                  	;
   156 00000004 B801E8                  	mov	ax, 0E801h ; Get memory size 
   157 00000007 CD15                    	int	15h	   ; for large configurations
   158 00000009 7308                    	jnc	short chk_ms
   159 0000000B B488                    	mov	ah, 88h    ; Get extended memory size 
   160 0000000D CD15                    	int	15h
   161                                  	;	   
   162                                  	;mov	al, 17h	; Extended memory (1K blocks) low byte
   163                                  	;out	70h, al ; select CMOS register
   164                                  	;in	al, 71h ; read data (1 byte)
   165                                  	;mov	cl, al
   166                                  	;mov	al, 18h ; Extended memory (1K blocks) high byte
   167                                  	;out	70h, al ; select CMOS register
   168                                  	;in	al, 71h ; read data (1 byte)
   169                                  	;mov	ch, al
   170                                   	;      
   171 0000000F 89C1                    	mov	cx, ax
   172 00000011 31D2                    	xor	dx, dx
   173                                  chk_ms:
   174 00000013 890E[446D]              	mov	[mem_1m_1k], cx
   175 00000017 8916[486D]              	mov	[mem_16m_64k], dx
   176                                  	; 05/11/2014
   177                                  	;and	dx, dx
   178                                  	;jz	short L2
   179 0000001B 81F90004                        cmp     cx, 1024
   180 0000001F 7351                    	jnb	short L0
   181                                  		 ; insufficient memory_error	
   182                                  		 ; Minimum 2 MB memory is needed...
   183                                  oom_0: 
   184                                  	; 05/11/2014
   185                                  	; (real mode error printing)
   186 00000021 FB                      	sti
   187 00000022 BE[3600]                	mov	si, msg_out_of_memory
   188 00000025 BB0700                  	mov	bx, 7
   189 00000028 B40E                    	mov	ah, 0Eh	; write tty
   190                                  oom_1:
   191 0000002A AC                      	lodsb
   192 0000002B 08C0                    	or	al, al
   193 0000002D 7404                    	jz	short oom_2
   194 0000002F CD10                    	int	10h
   195 00000031 EBF7                    	jmp	short oom_1
   196                                  oom_2:
   197 00000033 F4                              hlt
   198 00000034 EBFD                    	jmp	short oom_2
   199                                  
   200                                  ; 28/11/2021 - Retro UNIX 386 v2 compatibility modification (on v1.1)
   201                                  ; 11/04/2021 - Retro UNIX 386 v2
   202                                  ; ((Real mode messages and buffers must be in the 1st 64K of the kernel))
   203                                  ; 20/02/2017 (TRDOS 386 v2)
   204                                  ; 05/11/2014
   205                                  msg_out_of_memory:
   206 00000036 070D0A                  	db 	07h, 0Dh, 0Ah
   207 00000039 496E73756666696369-             db      'Insufficient memory !'
   207 00000042 656E74206D656D6F72-
   207 0000004B 792021             
   208 0000004E 0D0A                    	db	0Dh, 0Ah
   209                                  _int13h_48h_buffer: ; 07/07/2016
   210 00000050 284D696E696D756D20-     	db	'(Minimum 2MB memory is needed.)'
   210 00000059 324D42206D656D6F72-
   210 00000062 79206973206E656564-
   210 0000006B 65642E29           
   211 0000006F 0D0A00                   	db	0Dh, 0Ah, 0
   212                                  
   213                                  L0:
   214                                  %include 'diskinit.s' ; 07/03/2015
   215                              <1> ; Temporary Runix kernel v2.0 file for debug - 22/11/2021
   216                              <1> ; (re-write kernel for test by using previous version without a major defect)
   217                              <1> ; ****************************************************************************
   218                              <1> ; Retro UNIX 386 v1 Kernel - DISKINIT.INC
   219                              <1> ; Last Modification: 02/01/2022
   220                              <1> 
   221                              <1> ; DISK I/O SYSTEM INITIALIZATION - Erdogan Tan (Retro UNIX 386 v1 project)
   222                              <1> 
   223                              <1> ; ///////// DISK I/O SYSTEM STRUCTURE INITIALIZATION ///////////////
   224                              <1> 
   225                              <1> 	; 02/01/2022 (Retro UNIX 386 v1.2)
   226                              <1> 	; 10/12/2014 - 02/02/2015 - dsectrm2.s
   227                              <1> ;L0:
   228                              <1> 	; 12/11/2014 (Retro UNIX 386 v1 - beginning)
   229                              <1> 	; Detecting disk drives... (by help of ROM-BIOS)
   230 00000072 BA7F00              <1> 	mov	dx, 7Fh
   231                              <1> L1:	
   232 00000075 FEC2                <1> 	inc	dl
   233 00000077 B441                <1> 	mov	ah, 41h ; Check extensions present
   234                              <1> 			; Phoenix EDD v1.1 - EDD v3
   235 00000079 BBAA55              <1> 	mov	bx, 55AAh
   236 0000007C CD13                <1> 	int 	13h
   237 0000007E 721A                <1> 	jc	short L2
   238                              <1> 
   239 00000080 81FB55AA            <1> 	cmp	bx, 0AA55h
   240 00000084 7514                <1> 	jne	short L2
   241 00000086 FE06[AD6A]          <1> 	inc	byte [hdc]	; count of hard disks (EDD present)
   242 0000008A 8816[AC6A]          <1>         mov     [last_drv], dl  ; last hard disk number
   243 0000008E BB[306A]            <1> 	mov	bx, hd0_type - 80h
   244 00000091 01D3                <1> 	add	bx, dx	 
   245 00000093 880F                <1> 	mov	[bx], cl ; Interface support bit map in CX
   246                              <1> 			 ; Bit 0 - 1, Fixed disk access subset ready
   247                              <1> 			 ; Bit 1 - 1, Drv locking and ejecting ready
   248                              <1> 			 ; Bit 2 - 1, Enhanced Disk Drive Support
   249                              <1>                          ;            (EDD) ready (DPTE ready)
   250                              <1> 			 ; Bit 3 - 1, 64bit extensions are present
   251                              <1>                          ;            (EDD-3)
   252                              <1> 			 ; Bit 4 to 15 - 0, Reserved
   253 00000095 80FA83              <1> 	cmp	dl, 83h	 ; drive number < 83h
   254 00000098 72DB                <1> 	jb	short L1
   255                              <1> L2:
   256                              <1> 	; 23/11/2014
   257                              <1> 	; 19/11/2014
   258 0000009A 30D2                <1> 	xor	dl, dl  ; 0
   259                              <1> 	; 04/02/2016 (esi -> si)
   260 0000009C BE[AE6A]            <1> 	mov	si, fd0_type
   261                              <1> L3:
   262                              <1> 	; 14/01/2015
   263 0000009F 8816[AB6A]          <1> 	mov	[drv], dl
   264                              <1> 	;
   265 000000A3 B408                <1> 	mov 	ah, 08h ; Return drive parameters
   266 000000A5 CD13                <1> 	int	13h	
   267 000000A7 7210                <1> 	jc	short L4
   268                              <1> 		; BL = drive type (for floppy drives)
   269                              <1> 		; DL = number of floppy drives
   270                              <1> 		;		
   271                              <1> 		; ES:DI = Address of DPT from BIOS
   272                              <1> 		;
   273 000000A9 881C                <1> 	mov	[si], bl ;  Drive type
   274                              <1> 			; 4 = 1.44 MB, 80 track, 3 1/2"
   275                              <1> 	; 14/01/2015
   276 000000AB E8BB01              <1> 	call	set_disk_parms
   277                              <1> 	; 10/12/2014
   278 000000AE 81FE[AE6A]          <1> 	cmp	si, fd0_type
   279 000000B2 7705                <1> 	ja	short L4
   280 000000B4 46                  <1> 	inc	si ; fd1_type
   281 000000B5 B201                <1> 	mov	dl, 1
   282 000000B7 EBE6                <1> 	jmp	short L3
   283                              <1> L4:
   284                              <1> 	; Older BIOS (INT 13h, AH = 48h is not available)
   285 000000B9 B27F                <1> 	mov	dl, 7Fh
   286                              <1> 	; 24/12/2014 (Temporary)
   287 000000BB 803E[AD6A]00        <1> 	cmp	byte [hdc], 0 ; EDD present or not ?	
   288                              <1> 	;ja	L10       ; yes, all fixed disk operations
   289                              <1> 			  ; will be performed according to
   290                              <1> 			  ; present EDD specification
   291                              <1> 	; 02/01/2022
   292 000000C0 7603                <1> 	jna	short L5
   293 000000C2 E99100              <1> 	jmp	L10
   294                              <1> L5:
   295 000000C5 FEC2                <1> 	inc 	dl
   296 000000C7 8816[AB6A]          <1>         mov     [drv], dl
   297 000000CB 8816[AC6A]          <1>         mov     [last_drv], dl ; 14/01/2015
   298 000000CF B408                <1> 	mov 	ah, 08h ; Return drive parameters
   299 000000D1 CD13                <1> 	int	13h	; (conventional function)
   300                              <1> 	;jc	L13	; fixed disk drive not ready
   301                              <1> 	; 02/01/2022
   302 000000D3 7303                <1> 	jnc	short L6
   303 000000D5 E98301              <1> 	jmp	L13
   304                              <1> L6:
   305 000000D8 8816[AD6A]          <1>         mov     [hdc], dl ; number of drives
   306                              <1> 	;; 14/01/2013
   307                              <1> 	;;push	cx
   308 000000DC E88A01              <1> 	call	set_disk_parms
   309                              <1> 	;;pop	cx
   310                              <1> 	;
   311                              <1> 	;;and	cl, 3Fh	 ; sectors per track (bits 0-6)
   312 000000DF 8A16[AB6A]          <1>         mov     dl, [drv]
   313 000000E3 BB0401              <1> 	mov	bx, 65*4 ; hd0 parameters table (INT 41h)	
   314 000000E6 80FA80              <1> 	cmp	dl, 80h
   315 000000E9 7603                <1> 	jna	short L7
   316 000000EB 83C314              <1> 	add	bx, 5*4	 ; hd1 parameters table (INT 46h)
   317                              <1> L7:	
   318 000000EE 31C0                <1> 	xor	ax, ax
   319 000000F0 8ED8                <1> 	mov	ds, ax
   320 000000F2 8B37                <1>         mov     si, [bx]
   321 000000F4 8B4702              <1>         mov     ax, [bx+2] 
   322 000000F7 8ED8                <1> 	mov	ds, ax
   323 000000F9 3A4C0E              <1>         cmp     cl, [si+FDPT_SPT] ; sectors per track 
   324                              <1> 	;jne	L12 ; invalid FDPT
   325                              <1> 	; 02/01/2022
   326 000000FC 7403                <1> 	je	short L7_8
   327 000000FE E95601              <1> 	jmp	L12
   328                              <1> L7_8:
   329 00000101 BF0000              <1> 	mov	di, HD0_DPT
   330 00000104 80FA80              <1> 	cmp	dl, 80h
   331 00000107 7603                <1> 	jna	short L8
   332 00000109 BF2000              <1> 	mov	di, HD1_DPT 
   333                              <1> L8:
   334                              <1> 	; 30/12/2014
   335 0000010C B80090              <1> 	mov	ax, DPT_SEGM
   336 0000010F 8EC0                <1> 	mov	es, ax
   337                              <1> 	; 24/12/2014
   338 00000111 B90800              <1> 	mov	cx, 8
   339 00000114 F3A5                <1> 	rep	movsw  ; copy 16 bytes to the kernel's DPT location
   340 00000116 8CC8                <1> 	mov	ax, cs
   341 00000118 8ED8                <1> 	mov	ds, ax
   342                              <1> 	; 02/02/2015
   343 0000011A 8A0E[AB6A]          <1>         mov     cl, [drv]
   344 0000011E 88CB                <1> 	mov	bl, cl
   345 00000120 B8F001              <1> 	mov	ax, 1F0h
   346 00000123 80E301              <1> 	and	bl, 1
   347 00000126 7406                <1> 	jz	short L9
   348 00000128 C0E304              <1> 	shl	bl, 4
   349 0000012B 2D8000              <1> 	sub	ax, 1F0h-170h
   350                              <1> L9:
   351 0000012E AB                  <1> 	stosw	; I/O PORT Base Address (1F0h, 170h)
   352 0000012F 050602              <1> 	add	ax, 206h
   353 00000132 AB                  <1> 	stosw	; CONTROL PORT Address (3F6h, 376h)	
   354 00000133 88D8                <1> 	mov	al, bl
   355 00000135 04A0                <1> 	add	al, 0A0h
   356 00000137 AA                  <1> 	stosb	; Device/Head Register upper nibble
   357                              <1> 	;
   358 00000138 FE06[AB6A]          <1> 	inc	byte [drv]
   359 0000013C BB[306A]            <1> 	mov	bx, hd0_type - 80h
   360 0000013F 01CB                <1> 	add	bx, cx
   361 00000141 800F80              <1>         or      byte [bx], 80h  ; present sign (when lower nibble is 0)
   362 00000144 A0[AD6A]            <1> 	mov	al, [hdc]
   363 00000147 FEC8                <1> 	dec	al
   364                              <1> 	;jz	L13
   365                              <1> 	; 02/01/2022
   366 00000149 7408                <1> 	jz	short L9_10
   367 0000014B 80FA80              <1> 	cmp	dl, 80h
   368                              <1>         ;jna	L5
   369                              <1> 	;jmp	L13
   370                              <1> 	; 02/01/2022
   371 0000014E 7703                <1> 	ja	short L9_10
   372 00000150 E972FF              <1> 	jmp	L5
   373                              <1> L9_10:
   374 00000153 E90501              <1>         jmp     L13
   375                              <1> L10:
   376 00000156 FEC2                <1> 	inc 	dl
   377                              <1> 	; 25/12/2014
   378 00000158 8816[AB6A]          <1> 	mov	[drv], dl
   379 0000015C B408                <1> 	mov 	ah, 08h ; Return drive parameters
   380 0000015E CD13                <1> 	int	13h	; (conventional function)
   381                              <1> 	;jc	L13
   382                              <1> 	; 02/01/2022
   383 00000160 72F1                <1> 	jc	short L9_10
   384                              <1> 	; 14/01/2015
   385 00000162 8A16[AB6A]          <1> 	mov	dl, [drv]
   386 00000166 52                  <1> 	push	dx
   387 00000167 51                  <1> 	push	cx
   388 00000168 E8FE00              <1> 	call	set_disk_parms
   389 0000016B 59                  <1> 	pop	cx
   390 0000016C 5A                  <1> 	pop	dx
   391                              <1> 	; 02/01/2022 - Retro UNIX 386 v1.2
   392                              <1> 	; 04/02/2016 (esi -> si)
   393                              <1> 	;mov	si, _end ; 30 byte temporary buffer address
   394                              <1> 	;		 ; at the '_end' of kernel.
   395                              <1> 	;mov	word [si], 30
   396                              <1> 	; 06/07/2016
   397 0000016D BE[5000]            <1> 	mov	si, _int13h_48h_buffer
   398                              <1> 	; 09/07/2016
   399 00000170 B81E00              <1> 	mov	ax, 001Eh
   400 00000173 8824                <1> 	mov	[si], ah ; 0
   401 00000175 46                  <1> 	inc	si
   402 00000176 8904                <1> 	mov	word [si], ax
   403                              <1>  	; word [si] = 30
   404                              <1> 	;
   405 00000178 B448                <1> 	mov	ah, 48h	 ; Get drive parameters (EDD function)
   406 0000017A CD13                <1> 	int	13h
   407                              <1>         ;jc	L13
   408                              <1> 	; 02/01/2022
   409 0000017C 72D5                <1> 	jc	short L9_10
   410                              <1> 	; 04/02/2016 (ebx -> bx)
   411                              <1> 	; 14/01/2015
   412                              <1> 	;sub	bx, bx
   413 0000017E 28FF                <1> 	sub	bh, bh ; 02/01/2022
   414 00000180 88D3                <1> 	mov	bl, dl
   415 00000182 80EB80              <1> 	sub	bl, 80h
   416 00000185 81C3[B06A]          <1> 	add	bx, hd0_type
   417 00000189 8A07                <1> 	mov 	al, [bx]
   418 0000018B 0C80                <1> 	or	al, 80h
   419 0000018D 8807                <1> 	mov 	[bx], al	
   420 0000018F 81EB[AE6A]          <1> 	sub	bx, hd0_type - 2 ; 15/01/2015
   421 00000193 81C3[FA6A]          <1> 	add	bx, drv.status
   422 00000197 8807                <1> 	mov	[bx], al
   423                              <1> 	; 04/02/2016 (eax -> ax)
   424 00000199 8B4410              <1> 	mov	ax, [si+16]
   425 0000019C 854412              <1> 	test	ax, [si+18]
   426 0000019F 7413                <1> 	jz	short L10_A0h 
   427                              <1> 			; 'CHS only' disks on EDD system 
   428                              <1> 			;  are reported with ZERO disk size
   429 000001A1 81EB[FA6A]          <1> 	sub	bx, drv.status
   430 000001A5 C1E302              <1> 	shl	bx, 2
   431 000001A8 81C3[DE6A]          <1> 	add	bx, drv.size ; disk size (in sectors)
   432 000001AC 8907                <1> 	mov	[bx], ax
   433 000001AE 8B4412              <1> 	mov	ax, [si+18]
   434                              <1> 	;mov	[bx], ax
   435                              <1> 	; 02/01/2022 (BugFix)
   436 000001B1 894702              <1> 	mov	[bx+2], ax
   437                              <1> 
   438                              <1> L10_A0h: ; Jump here to fix a ZERO (LBA) disk size problem 
   439                              <1> 	 ; for CHS disks (28/02/2015)
   440                              <1> 	; 30/12/2014
   441 000001B4 BF0000              <1> 	mov	di, HD0_DPT
   442 000001B7 88D0                <1> 	mov	al, dl
   443 000001B9 83E003              <1> 	and 	ax, 3
   444 000001BC C0E005              <1> 	shl	al, 5 ; *32
   445 000001BF 01C7                <1> 	add 	di, ax
   446 000001C1 B80090              <1> 	mov	ax, DPT_SEGM
   447 000001C4 8EC0                <1> 	mov	es, ax
   448                              <1> 	;
   449 000001C6 88E8                <1> 	mov	al, ch	; max. cylinder number (bits 0-7)
   450 000001C8 88CC                <1> 	mov	ah, cl	
   451 000001CA C0EC06              <1> 	shr	ah, 6	; max. cylinder number (bits 8-9)
   452 000001CD 40                  <1>  	inc	ax	; logical cylinders (limit 1024)
   453 000001CE AB                  <1> 	stosw		
   454 000001CF 88F0                <1> 	mov	al, dh	; max. head number
   455 000001D1 FEC0                <1> 	inc	al
   456 000001D3 AA                  <1> 	stosb		; logical heads (limits 256)
   457 000001D4 B0A0                <1> 	mov	al, 0A0h ; Indicates translated table
   458 000001D6 AA                  <1> 	stosb
   459 000001D7 8A440C              <1> 	mov	al, [si+12]
   460 000001DA AA                  <1> 	stosb		 ; physical sectors per track
   461 000001DB 31C0                <1>  	xor	ax, ax
   462                              <1> 	;dec	ax	 ; 02/01/2015 
   463 000001DD AB                  <1> 	stosw		 ; precompensation (obsolete)
   464                              <1> 	;xor	al, al	 ; 02/01/2015	
   465 000001DE AA                  <1> 	stosb		 ; reserved
   466 000001DF B008                <1> 	mov	al, 8	 ; drive control byte
   467                              <1> 		         ; (do not disable retries, 
   468                              <1> 			 ; more than 8 heads)
   469 000001E1 AA                  <1> 	stosb
   470 000001E2 8B4404              <1> 	mov	ax, [si+4]
   471 000001E5 AB                  <1> 	stosw		 ; physical number of cylinders	
   472                              <1> 	;push	ax	 ; 02/01/2015
   473 000001E6 8A4408              <1> 	mov	al, [si+8]
   474 000001E9 AA                  <1> 	stosb		 ; physical num. of heads (limit 16)
   475 000001EA 29C0                <1> 	sub 	ax, ax
   476                              <1> 	;pop	ax	 ; 02/01/2015	
   477 000001EC AB                  <1> 	stosw		 ; landing zone (obsolete)
   478 000001ED 88C8                <1> 	mov	al, cl	 ; logical sectors per track (limit 63)
   479 000001EF 243F                <1> 	and 	al, 3Fh	
   480 000001F1 AA                  <1> 	stosb
   481                              <1> 	;sub	al, al	 ; checksum
   482                              <1> 	;stosb
   483                              <1> 	;
   484 000001F2 83C61A              <1> 	add	si, 26   ; (BIOS) DPTE address pointer
   485 000001F5 AD                  <1> 	lodsw
   486 000001F6 50                  <1> 	push	ax	 ; (BIOS) DPTE offset
   487 000001F7 AD                  <1> 	lodsw
   488 000001F8 50                  <1> 	push	ax	 ; (BIOS) DPTE segment
   489                              <1> 	;
   490                              <1> 	; checksum calculation
   491 000001F9 89FE                <1> 	mov	si, di
   492 000001FB 06                  <1> 	push	es
   493 000001FC 1F                  <1> 	pop	ds
   494                              <1> 	;mov	cx, 16
   495 000001FD B90F00              <1> 	mov 	cx, 15
   496 00000200 29CE                <1> 	sub	si, cx
   497 00000202 30E4                <1> 	xor	ah, ah
   498                              <1> 	;del	cl
   499                              <1> L11:		
   500 00000204 AC                  <1> 	lodsb
   501 00000205 00C4                <1> 	add	ah, al
   502 00000207 E2FB                <1> 	loop	L11
   503                              <1> 	;
   504 00000209 88E0                <1> 	mov	al, ah
   505 0000020B F6D8                <1> 	neg	al	; -x+x = 0
   506 0000020D AA                  <1> 	stosb		; put checksum in byte 15 of the tbl
   507                              <1> 	;
   508 0000020E 1F                  <1> 	pop	ds	; (BIOS) DPTE segment
   509 0000020F 5E                  <1> 	pop	si	; (BIOS) DPTE offset	
   510                              <1> 	;
   511                              <1> 	; 23/02/2015
   512 00000210 57                  <1> 	push	di
   513                              <1> 	; ES:DI points to DPTE (FDPTE) location
   514                              <1> 	;mov	cx, 8
   515 00000211 B108                <1> 	mov	cl, 8
   516 00000213 F3A5                <1> 	rep	movsw	
   517                              <1> 	;
   518                              <1> 	; 23/02/2015
   519                              <1> 	; (P)ATA drive and LBA validation
   520                              <1> 	; (invalidating SATA drives and setting
   521                              <1> 	; CHS type I/O for old type fixed disks)
   522 00000215 5B                  <1> 	pop	bx
   523 00000216 8CC8                <1> 	mov	ax, cs
   524 00000218 8ED8                <1> 	mov	ds, ax
   525 0000021A 268B07              <1> 	mov	ax, [es:bx]
   526 0000021D 3DF001              <1> 	cmp	ax, 1F0h
   527 00000220 7418                <1> 	je	short L11a
   528 00000222 3D7001              <1> 	cmp	ax, 170h
   529 00000225 7413                <1> 	je	short L11a
   530                              <1> 	; invalidation 
   531                              <1> 	; (because base port address is not 1F0h or 170h)
   532 00000227 30FF                <1> 	xor	bh, bh
   533 00000229 88D3                <1> 	mov	bl, dl
   534 0000022B 80EB80              <1> 	sub	bl, 80h
   535 0000022E C687[B06A]00        <1> 	mov	byte [bx+hd0_type], 0 ; not a valid disk drive !		
   536 00000233 808F[FC6A]F0        <1>         or      byte [bx+drv.status+2], 0F0h ; (failure sign)
   537 00000238 EB14                <1> 	jmp	short L11b
   538                              <1> L11a:	
   539                              <1> 	; LBA validation
   540 0000023A 268A4704            <1> 	mov	al, [es:bx+4] ; Head register upper nibble
   541 0000023E A840                <1> 	test	al, 40h ; LBA bit (bit 6)
   542 00000240 750C                <1> 	jnz	short L11b ; LBA type I/O is OK! (E0h or F0h)
   543                              <1> 	; force CHS type I/O for this drive (A0h or B0h)
   544 00000242 28FF                <1> 	sub	bh, bh
   545 00000244 88D3                <1> 	mov	bl, dl
   546 00000246 80EB80              <1> 	sub	bl, 80h ; 26/02/2015
   547 00000249 80A7[FC6A]FE        <1>         and     byte [bx+drv.status+2], 0FEh ; clear bit 0
   548                              <1> 				; bit 0 = LBA ready bit
   549                              <1> 	; 'diskio' procedure will check this bit !
   550                              <1> L11b:
   551 0000024E 3A16[AC6A]          <1> 	cmp	dl, [last_drv] ; 25/12/2014
   552 00000252 7307                <1>         jnb     short L13
   553 00000254 E9FFFE              <1>         jmp     L10
   554                              <1> L12:
   555                              <1> 	; Restore data registers
   556 00000257 8CC8                <1> 	mov	ax, cs
   557 00000259 8ED8                <1> 	mov	ds, ax	
   558                              <1> L13:
   559                              <1> 	; 13/12/2014
   560 0000025B 0E                  <1> 	push	cs
   561 0000025C 07                  <1> 	pop	es
   562                              <1> L14:
   563 0000025D B411                <1> 	mov 	ah, 11h
   564 0000025F CD16                <1> 	int 	16h
   565                              <1> 	;jz 	short L15 ; no keys in keyboard buffer
   566                              <1> 	; 02/01/2022
   567 00000261 7466                <1> 	jz	short L16
   568 00000263 B010                <1> 	mov	al, 10h
   569 00000265 CD16                <1> 	int 	16h
   570 00000267 EBF4                <1> 	jmp 	short L14
   571                              <1> L15:
   572                              <1> 
   573                              <1> ; //////
   574                              <1> 
   575                              <1> ; 02/01/2022 - Retro UNIX 386 v1.2
   576                              <1> %if 0
   577                              <1> 	; 24/11/2014
   578                              <1> 	; 19/11/2014
   579                              <1> 	; 14/11/2014
   580                              <1> 	; Temporary code for disk searching code check
   581                              <1> 	;
   582                              <1> 	; This code will show existing (usable) drives and also
   583                              <1> 	; will show EDD interface support status for hard disks		
   584                              <1> 	; (If status bit 7 is 1, Identify Device info is ready,
   585                              <1> 	; no need to get it again in protected mode...) 
   586                              <1> 	;	
   587                              <1> 	; 13/11/2014
   588                              <1> 	mov	bx, 7
   589                              <1> 	mov	ah, 0Eh
   590                              <1> 	mov	al, [fd0_type]
   591                              <1> 	and	al, al
   592                              <1> 	jz	short L15a
   593                              <1> 	mov	dl, al
   594                              <1> 	mov	al, 'F'
   595                              <1> 	int 	10h
   596                              <1> 	mov	al, 'D'
   597                              <1> 	int 	10h
   598                              <1> 	mov	al, '0'
   599                              <1> 	int 	10h
   600                              <1> 	mov	al, ' '
   601                              <1> 	int	10h
   602                              <1> 	call	L15c
   603                              <1> 	mov	al, ' '
   604                              <1> 	int	10h
   605                              <1> 	;
   606                              <1> 	mov	al, [fd1_type]
   607                              <1> 	and	al, al
   608                              <1> 	jz	short L15a
   609                              <1> 	mov	dl, al
   610                              <1> 	mov	al, 'F'
   611                              <1> 	int 	10h
   612                              <1> 	mov	al, 'D'
   613                              <1> 	int 	10h
   614                              <1> 	mov	al, '1'
   615                              <1> 	int 	10h
   616                              <1> 	mov	al, ' '
   617                              <1> 	int	10h
   618                              <1> 	call	L15c
   619                              <1> 	mov	al, ' '
   620                              <1> 	int	10h
   621                              <1> 	mov	al, ' '
   622                              <1> 	int	10h
   623                              <1> L15a:
   624                              <1> 	mov	al, [hd0_type]
   625                              <1> 	and	al, al
   626                              <1> 	jz	short L15b
   627                              <1> 	mov	dl, al
   628                              <1> 	mov	al, 'H'
   629                              <1> 	int 	10h
   630                              <1> 	mov	al, 'D'
   631                              <1> 	int 	10h
   632                              <1> 	mov	al, '0'
   633                              <1> 	int 	10h
   634                              <1> 	mov	al, ' '
   635                              <1> 	int 	10h
   636                              <1> 	call	L15c
   637                              <1> 	mov	al, ' '
   638                              <1> 	int	10h
   639                              <1> 	;
   640                              <1> 	mov	al, [hd1_type]
   641                              <1> 	and	al, al
   642                              <1> 	jz	short L15b
   643                              <1> 	mov	dl, al
   644                              <1> 	mov	al, 'H'
   645                              <1> 	int 	10h
   646                              <1> 	mov	al, 'D'
   647                              <1> 	int 	10h
   648                              <1> 	mov	al, '1'
   649                              <1> 	int 	10h
   650                              <1> 	mov	al, ' '
   651                              <1> 	int 	10h
   652                              <1> 	call	L15c
   653                              <1> 	mov	al, ' '
   654                              <1> 	int	10h
   655                              <1> 	;
   656                              <1> 	mov	al, [hd2_type]
   657                              <1> 	and	al, al
   658                              <1> 	jz	short L15b
   659                              <1> 	mov	dl, al
   660                              <1> 	mov	al, 'H'
   661                              <1> 	int 	10h
   662                              <1> 	mov	al, 'D'
   663                              <1> 	int 	10h
   664                              <1> 	mov	al, '2'
   665                              <1> 	int 	10h
   666                              <1> 	mov	al, ' '
   667                              <1> 	int 	10h
   668                              <1> 	call	L15c
   669                              <1> 	mov	al, ' '
   670                              <1> 	int	10h
   671                              <1> 	;
   672                              <1> 	mov	al, [hd3_type]
   673                              <1> 	and	al, al
   674                              <1> 	jz	short L15b
   675                              <1> 	mov	dl, al
   676                              <1> 	mov	al, 'H'
   677                              <1> 	int 	10h
   678                              <1> 	mov	al, 'D'
   679                              <1> 	int 	10h
   680                              <1> 	mov	al, '3'
   681                              <1> 	int 	10h
   682                              <1> 	mov	al, ' '
   683                              <1> 	int 	10h
   684                              <1> 	call	L15c
   685                              <1> 	mov	al, ' '
   686                              <1> 	int	10h
   687                              <1> 	;
   688                              <1> L15b:
   689                              <1> 	mov	al, 0Dh
   690                              <1> 	int 	10h	
   691                              <1> 	mov	al, 0Ah
   692                              <1> 	int 	10h
   693                              <1> 	;;xor	ah, ah
   694                              <1> 	;;int 	16h	
   695                              <1> 	;
   696                              <1>         ;jmp	L16  ; jmp short L16
   697                              <1>         ; 02/01/2022
   698                              <1> 	jmp	short L16
   699                              <1> 	;
   700                              <1> L15c:
   701                              <1> 	mov	dh, dl
   702                              <1> 	shr	dh, 4
   703                              <1> 	add	dh, 30h
   704                              <1> 	and	dl, 15
   705                              <1> 	add	dl, 30h
   706                              <1> 	mov	al, dh
   707                              <1> 	int	10h
   708                              <1> 	mov	al, dl
   709                              <1> 	int	10h
   710                              <1> 	retn
   711                              <1> 	;
   712                              <1> 	; end of temporary code for disk searching code check
   713                              <1> 
   714                              <1> %endif
   715                              <1> 
   716                              <1> ; //////
   717                              <1> 
   718                              <1> set_disk_parms:
   719                              <1> 	; 04/02/2016 (ebx -> bx)
   720                              <1> 	; 10/07/2015
   721                              <1> 	; 14/01/2015
   722                              <1> 	;push	bx
   723 00000269 28FF                <1> 	sub	bh, bh
   724 0000026B 8A1E[AB6A]          <1> 	mov	bl, [drv]
   725 0000026F 80FB80              <1> 	cmp	bl, 80h
   726 00000272 7203                <1> 	jb	short sdp0
   727 00000274 80EB7E              <1> 	sub	bl, 7Eh
   728                              <1> sdp0:	
   729 00000277 81C3[FA6A]          <1> 	add	bx, drv.status
   730 0000027B C60780              <1>   	mov	byte [bx], 80h ; 'Present' flag
   731                              <1> 	;
   732 0000027E 88E8                <1> 	mov	al, ch ; last cylinder (bits 0-7)
   733 00000280 88CC                <1> 	mov	ah, cl ; 
   734 00000282 C0EC06              <1> 	shr	ah, 6  ; last cylinder (bits 8-9)
   735 00000285 81EB[FA6A]          <1> 	sub	bx, drv.status
   736 00000289 D0E3                <1> 	shl	bl, 1
   737 0000028B 81C3[B46A]          <1> 	add	bx, drv.cylinders
   738 0000028F 40                  <1> 	inc	ax  ; convert max. cyl number to cyl count		
   739 00000290 8907                <1> 	mov	[bx], ax
   740 00000292 50                  <1> 	push	ax ; ** cylinders
   741 00000293 81EB[B46A]          <1> 	sub	bx, drv.cylinders
   742 00000297 81C3[C26A]          <1> 	add	bx, drv.heads
   743 0000029B 30E4                <1> 	xor	ah, ah
   744 0000029D 88F0                <1> 	mov	al, dh ; heads
   745 0000029F 40                  <1> 	inc	ax
   746 000002A0 8907                <1> 	mov	[bx], ax
   747 000002A2 81EB[C26A]          <1>         sub     bx, drv.heads
   748 000002A6 81C3[D06A]          <1>         add     bx, drv.spt
   749 000002AA 30ED                <1> 	xor	ch, ch
   750 000002AC 80E13F              <1> 	and	cl, 3Fh	; sectors (bits 0-6)
   751 000002AF 890F                <1> 	mov	[bx], cx
   752 000002B1 81EB[D06A]          <1>         sub     bx, drv.spt
   753 000002B5 D1E3                <1> 	shl	bx, 1
   754 000002B7 81C3[DE6A]          <1> 	add	bx, drv.size ; disk size (in sectors)
   755                              <1> 	; LBA size = cylinders * heads * secpertrack
   756 000002BB F7E1                <1> 	mul	cx 
   757 000002BD 89C2                <1> 	mov	dx, ax	; heads*spt					
   758 000002BF 58                  <1> 	pop	ax ; ** cylinders
   759 000002C0 48                  <1> 	dec	ax ; 1 cylinder reserved (!?)
   760 000002C1 F7E2                <1> 	mul	dx ; cylinders * (heads*spt)		
   761 000002C3 8907                <1> 	mov	[bx], ax
   762 000002C5 895702              <1> 	mov	[bx+2], dx
   763                              <1> 	;
   764                              <1> 	;pop	bx
   765 000002C8 C3                  <1> 	retn
   766                              <1> 
   767                              <1> ;align 2
   768                              <1> 
   769                              <1> ;cylinders :  dw 0, 0, 0, 0, 0, 0
   770                              <1> ;heads	  :  dw 0, 0, 0, 0, 0, 0
   771                              <1> ;spt	  :  dw 0, 0, 0, 0, 0, 0
   772                              <1> ;disk_size :  dd 0, 0, 0, 0, 0, 0
   773                              <1> 
   774                              <1> ;last_drv:
   775                              <1> ;	db  0
   776                              <1> ;drv_status:
   777                              <1> ;	db  0,0,0,0,0,0
   778                              <1> ;	db 0
   779                              <1> 
   780                              <1> ; End Of DISK I/O SYSTEM STRUCTURE INITIALIZATION /// 06/02/2015
   781                              <1> 
   782                              <1> L16:
   215                                  _L0:	
   216                                  	; 10/11/2014
   217 000002C9 FA                           	cli	; Disable interrupts (clear interrupt flag)
   218                                  		; Reset Interrupt MASK Registers (Master&Slave)
   219                                  	;mov	al, 0FFh	; mask off all interrupts
   220                                  	;out	21h, al		; on master PIC (8259)
   221                                  	;jmp 	$+2  ; (delay)
   222                                  	;out	0A1h, al	; on slave PIC (8259)
   223                                  	;
   224                                  	; Disable NMI 
   225 000002CA B080                    	mov   	al, 80h 
   226 000002CC E670                    	out   	70h, al		; set bit 7 to 1 for disabling NMI
   227                                  	;23/02/2015
   228 000002CE 90                      	nop			;
   229                                  	;in	al, 71h		; read in 71h just after writing out to 70h
   230                                  				; for preventing unknown state (!?)
   231                                  
   232                                  	; 01/01/2022 (Retro UNIX 386 v1.2)
   233                                  	%define KERNELFSIZE KEND-KSTART 
   234                                  	;
   235                                   	; 20/08/2014
   236                                  	; Moving the kernel 64 KB back (to physical address 0)
   237                                  	; DS = CS = 1000h
   238                                  	; 05/11/2014
   239 000002CF 31C0                    	xor	ax, ax
   240 000002D1 8EC0                    	mov	es, ax ; ES = 0
   241                                  	;
   242                                  	;;mov	cx, (KEND - KLOAD)/4
   243                                  	;mov	cx, (KERNELFSIZE+3)/4 ; 01/01/2022
   244 000002D3 B9A636                  	mov	cx, (KERNELFSIZE+1)/2 ; 02/01/2022
   245 000002D6 31F6                    	xor	si, si
   246 000002D8 31FF                    	xor	di, di
   247                                  	;rep	movsd
   248 000002DA F3A5                    	rep	movsw ; 02/01/2022
   249                                  	;
   250 000002DC 06                      	push	es ; 0
   251 000002DD 68[E102]                	push	L17
   252 000002E0 CB                      	retf
   253                                  	;
   254                                  L17:
   255                                  	; Turn off the floppy drive motor
   256 000002E1 BAF203                          mov     dx, 3F2h
   257 000002E4 EE                              out     dx, al ; 0 ; 31/12/2013
   258                                  
   259                                  	; Enable access to memory above one megabyte
   260                                  L18:
   261 000002E5 E464                    	in	al, 64h
   262 000002E7 A802                    	test	al, 2
   263 000002E9 75FA                            jnz     short L18
   264 000002EB B0D1                    	mov	al, 0D1h	; Write output port
   265 000002ED E664                    	out	64h, al
   266                                  L19:
   267 000002EF E464                    	in	al, 64h
   268 000002F1 A802                    	test	al, 2
   269 000002F3 75FA                            jnz     short L19
   270 000002F5 B0DF                    	mov	al, 0DFh	; Enable A20 line
   271 000002F7 E660                    	out	60h, al
   272                                  ;L20:
   273                                  	;
   274                                  	; Load global descriptor table register
   275                                  
   276                                          ;mov	ax, cs
   277                                          ;mov	ds, ax
   278                                  
   279 000002F9 2E0F0116[D067]                  lgdt    [cs:gdtd]
   280                                  
   281 000002FF 0F20C0                          mov     eax, cr0
   282                                  	;or 	eax, 1
   283 00000302 40                      	inc     ax
   284 00000303 0F22C0                  	mov     cr0, eax
   285                                  
   286                                  	; Jump to 32 bit code
   287                                  	
   288 00000306 66                      	db	66h 		; Prefix for 32-bit
   289 00000307 EA                      	db	0EAh 		; Opcode for far jump
   290 00000308 [0E030000]              	dd	StartPM 	; Offset to start, 32-bit
   291                                  				; (1000h:StartPM = StartPM + 10000h)
   292 0000030C 0800                    	dw	KCODE		; This is the selector for CODE32_DESCRIPTOR,
   293                                  				; assuming that StartPM resides in code32
   294                                  
   295                                  [BITS 32] 
   296                                  
   297                                  StartPM:
   298                                  	; Kernel Base Address = 0 ; 30/12/2013
   299 0000030E 66B81000                	mov	ax, KDATA	; Save data segment identifier
   300 00000312 8ED8                            mov	ds, ax		; Move a valid data segment into DS register
   301 00000314 8EC0                           	mov	es, ax		; Move data segment into ES register
   302 00000316 8EE0                           	mov	fs, ax		; Move data segment into FS register
   303 00000318 8EE8                          	mov	gs, ax		; Move data segment into GS register
   304 0000031A 8ED0                            mov	ss, ax		; Move data segment into SS register
   305 0000031C BC00000900                      mov	esp, 90000h	; Move the stack pointer to 090000h
   306                                  
   307                                  clear_bss: ; Clear uninitialized data area
   308                                  	; 11/03/2015
   309 00000321 31C0                    	xor	eax, eax ; 0
   310                                  	;mov	ecx, (bss_end - bss_start)/4
   311                                  	;;shr	ecx, 2 ; bss section is already aligned for double words
   312                                  	; 12/12/2021
   313 00000323 B91B070000              	mov	ecx, BSS_SIZE/4
   314 00000328 BF[506D0000]            	mov	edi, bss_start	
   315 0000032D F3AB                    	rep	stosd  		
   316                                  
   317                                  memory_init:
   318                                  	; Initialize memory allocation table and page tables
   319                                  	;
   320                                  	; 02/01/2022 (Retro UNIX 386 v1.2)
   321                                  	; 16/11/2014
   322                                  	; 15/11/2014
   323                                  	; 07/11/2014
   324                                  	; 06/11/2014
   325                                  	; 05/11/2014
   326                                  	; 04/11/2014
   327                                  	; 31/10/2014 (Retro UNIX 386 v1 - Beginning) 
   328                                  	;
   329                                  ;	xor	eax, eax
   330                                  ;	xor 	ecx, ecx
   331 0000032F B108                    	mov	cl, 8
   332 00000331 BF00001000              	mov	edi, MEM_ALLOC_TBL	
   333 00000336 F3AB                    	rep	stosd		   ; clear Memory Allocation Table
   334                                  				   ; for the first 1 MB memory
   335                                  	;
   336 00000338 8B0D[446D0000]          	mov	ecx, [mem_1m_1k]   ; 02/01/2022	
   337                                  	;mov	cx, [mem_1m_1k]	   ; Number of contiguous KB between
   338                                  				   ; 1 and 16 MB, max. 3C00h = 15 MB.
   339                                  	;shr	cx, 2		   ; convert 1 KB count to 4 KB count
   340 0000033E C1E902                  	shr	ecx, 2	; 02/01/2022
   341 00000341 890D[C06F0000]          	mov	[free_pages], ecx
   342 00000347 8B15[486D0000]          	mov	edx, [mem_16m_64k] ; 02/01/2022
   343                                  	;mov	dx, [mem_16m_64k]  ; Number of contiguous 64 KB blocks
   344                                  				   ; between 16 MB and 4 GB.
   345                                  	;or	dx, dx
   346 0000034D 09D2                    	or	edx, edx ; 02/01/2022
   347 0000034F 7412                    	jz	short mi_0
   348                                  	;
   349                                  	;mov	ax, dx
   350 00000351 89D0                    	mov	eax, edx ; 02/01/2022
   351 00000353 C1E004                  	shl	eax, 4		   ; 64 KB -> 4 KB (page count)
   352 00000356 0105[C06F0000]          	add	[free_pages], eax
   353 0000035C 0500100000              	add	eax, 4096	   ; 16 MB = 4096 pages
   354 00000361 EB06                    	jmp	short mi_1
   355                                  mi_0:
   356                                  	;mov	ax, cx
   357 00000363 89C8                    	mov	eax, ecx ; 02/01/2022	 
   358 00000365 66050001                	add	ax, 256		   ; add 256 pages for the first 1 MB
   359                                  mi_1:
   360 00000369 A3[BC6F0000]            	mov	[memory_size], eax ; Total available memory in pages
   361                                  				   ; 1 alloc. tbl. bit = 1 memory page
   362                                  				   ; 32 allocation bits = 32 mem. pages
   363                                  	;
   364 0000036E 05FF7F0000              	add	eax, 32767	   ; 32768 memory pages per 1 M.A.T. page
   365 00000373 C1E80F                  	shr	eax, 15		   ; ((32768 * x) + y) pages (y < 32768)
   366                                  				   ;  --> x + 1 M.A.T. pages, if y > 0
   367                                  				   ;  --> x M.A.T. pages, if y = 0
   368                                  	;mov	[mat_size], ax	   ; Memory Alloc. Table Size in pages
   369 00000376 A3[D06F0000]            	mov	[mat_size], eax ; 02/01/2022
   370 0000037B C1E00C                  	shl	eax, 12		   ; 1 M.A.T. page = 4096 bytes
   371                                  	;			   ; Max. 32 M.A.T. pages (4 GB memory)
   372 0000037E 89C3                    	mov	ebx, eax	   ; M.A.T. size in bytes
   373                                  	; Set/Calculate Kernel's Page Directory Address
   374 00000380 81C300001000            	add	ebx, MEM_ALLOC_TBL
   375 00000386 891D[B86F0000]          	mov	[k_page_dir], ebx  ; Kernel's Page Directory address
   376                                  				   ; just after the last M.A.T. page
   377                                  	;
   378 0000038C 83E804                  	sub	eax, 4		   ; convert M.A.T. size to offset value
   379 0000038F A3[C86F0000]            	mov	[last_page], eax   ; last page ofset in the M.A.T.
   380                                  	;			   ; (allocation status search must be
   381                                  				   ; stopped after here)
   382 00000394 31C0                    	xor	eax, eax
   383 00000396 48                      	dec	eax		   ; FFFFFFFFh (set all bits to 1)
   384                                  	;push	cx
   385 00000397 51                      	push	ecx ; 02/01/2022 
   386 00000398 C1E905                  	shr	ecx, 5		   ; convert 1 - 16 MB page count to
   387                                  				   ; count of 32 allocation bits
   388 0000039B F3AB                    	rep	stosd
   389                                  	;pop	cx
   390 0000039D 59                      	pop	ecx ; 02/01/2022
   391 0000039E 40                      	inc	eax		   ; 0
   392 0000039F 80E11F                  	and	cl, 31		   ; remain bits
   393 000003A2 7412                    	jz	short mi_4
   394 000003A4 8907                    	mov	[edi], eax	   ; reset
   395                                  mi_2:
   396 000003A6 0FAB07                  	bts	[edi], eax	   ; 06/11/2014
   397 000003A9 FEC9                    	dec	cl
   398 000003AB 7404                    	jz	short mi_3
   399 000003AD FEC0                    	inc	al
   400 000003AF EBF5                    	jmp	short mi_2
   401                                  mi_3:
   402 000003B1 28C0                    	sub	al, al	   	   ; 0
   403 000003B3 83C704                  	add	edi, 4		   ; 15/11/2014
   404                                  mi_4:
   405 000003B6 09D2                    	or	edx, edx ; 02/01/2022
   406                                  	;or	dx, dx		  ; check 16M to 4G memory space
   407 000003B8 741F                    	jz	short mi_6	  ; max. 16 MB memory, no more...
   408                                  	;	
   409 000003BA B900021000              	mov	ecx, MEM_ALLOC_TBL + 512 ; End of first 16 MB memory
   410                                  	;	
   411 000003BF 29F9                    	sub	ecx, edi	  ; displacement (to end of 16 MB)
   412 000003C1 7405                    	jz	short mi_5	  ; jump if EDI points to 
   413                                  				  ;         end of first 16 MB	
   414                                  	;shr	ecx, 1		  ; convert to dword count
   415                                  	;shr	ecx, 1		  ; (shift 2 bits right) 
   416 000003C3 C1E902                  	shr	ecx, 2	; 02/01/2022
   417 000003C6 F3AB                    	rep 	stosd		  ; reset all bits for reserved pages
   418                                  				  ; (memory hole under 16 MB)
   419                                  mi_5:
   420 000003C8 89D1                    	mov	ecx, edx ; 02/01/2022
   421                                  	;mov	cx, dx		  ; count of 64 KB memory blocks
   422 000003CA D1E9                    	shr	ecx, 1		  ; 1 alloc. dword per 128 KB memory
   423 000003CC 9C                      	pushf			  ; 16/11/2014		
   424 000003CD 48                      	dec	eax		  ; FFFFFFFFh (set all bits to 1)
   425 000003CE F3AB                    	rep	stosd
   426 000003D0 40                      	inc	eax		  ; 0
   427 000003D1 9D                      	popf			  ; 16/11/2014
   428 000003D2 7305                    	jnc	short mi_6
   429 000003D4 6648                    	dec	ax		  ; eax = 0000FFFFh
   430 000003D6 AB                      	stosd
   431 000003D7 6640                    	inc	ax		  ; 0		
   432                                  mi_6:
   433 000003D9 39DF                    	cmp	edi, ebx	  ; check if EDI points to 	
   434 000003DB 7309                    	jnb	short mi_7	  ; end of memory allocation table
   435                                  	;			  ; (>= MEM_ALLOC_TBL + 4906) 
   436 000003DD 89D9                    	mov	ecx, ebx	  ; end of memory allocation table
   437 000003DF 29F9                    	sub	ecx, edi	  ; convert displacement/offset
   438                                  	;shr	ecx, 1		  ; to dword count
   439                                  	;shr	ecx, 1		  ; (shift 2 bits right) 
   440 000003E1 C1E902                  	shr	ecx, 2 ; 02/01/2022	
   441 000003E4 F3AB                    	rep 	stosd		  ; reset all remain M.A.T. bits
   442                                  mi_7:
   443                                  	; Reset M.A.T. bits in M.A.T. (allocate M.A.T. pages)
   444 000003E6 BA00001000              	mov	edx, MEM_ALLOC_TBL
   445                                  	;sub	ebx, edx	  ; Mem. Alloc. Tbl. size in bytes
   446                                  	;shr	ebx, 12		  ; Mem. Alloc. Tbl. size in pages	
   447 000003EB 8B0D[D06F0000]          	mov	ecx, [mat_size] ; 02/01/2022
   448                                  	;mov	cx, [mat_size]	  ; Mem. Alloc. Tbl. size in pages
   449 000003F1 89D7                    	mov	edi, edx
   450 000003F3 C1EF0F                  	shr	edi, 15		  ; convert M.A.T. address to
   451                                  				  ; byte offset in M.A.T.
   452                                  				  ; (1 M.A.T. byte points to 
   453                                  				  ;	      32768 bytes)
   454                                  				  ; Note: MEM_ALLOC_TBL address 
   455                                  				  ; must be aligned on 128 KB 
   456                                  				  ; boundary!
   457 000003F6 01D7                    	add	edi, edx	  ; points to M.A.T.'s itself	
   458                                  	; eax = 0
   459 000003F8 290D[C06F0000]          	sub	[free_pages], ecx ; 07/11/2014
   460                                  mi_8:
   461 000003FE 0FB307                  	btr	[edi], eax	  ; clear bit 0 to bit x (1 to 31)
   462                                  	;dec	bl
   463 00000401 FEC9                    	dec	cl
   464 00000403 7404                    	jz	short mi_9
   465 00000405 FEC0                    	inc	al
   466 00000407 EBF5                    	jmp	short mi_8
   467                                  mi_9:
   468                                  	;
   469                                  	; Reset Kernel's Page Dir. and Page Table bits in M.A.T.
   470                                  	;		(allocate pages for system page tables)
   471                                  
   472                                  	; edx = MEM_ALLOC_TBL
   473 00000409 8B0D[BC6F0000]          	mov	ecx, [memory_size] ; memory size in pages (PTEs)
   474 0000040F 81C1FF030000            	add	ecx, 1023	 ; round up (1024 PTEs per table)
   475 00000415 C1E90A                  	shr	ecx, 10		 ; convert memory page count to 
   476                                  				 ; page table count (PDE count)
   477                                  	;
   478 00000418 51                      	push	ecx		 ; (**) PDE count (<= 1024)
   479                                  	;
   480 00000419 41                      	inc	ecx		 ; +1 for kernel page directory	
   481                                  	;
   482 0000041A 290D[C06F0000]          	sub	[free_pages], ecx ; 07/11/2014
   483                                  	;
   484 00000420 8B35[B86F0000]          	mov	esi, [k_page_dir] ; Kernel's Page Directory address
   485 00000426 C1EE0C                  	shr	esi, 12		 ; convert to page number
   486                                  mi_10:
   487 00000429 89F0                    	mov	eax, esi	 ; allocation bit offset
   488 0000042B 89C3                    	mov	ebx, eax
   489 0000042D C1EB03                  	shr	ebx, 3		 ; convert to alloc. byte offset
   490 00000430 80E3FC                  	and	bl,  0FCh	 ; clear bit 0 and bit 1
   491                                  				 ;   to align on dword boundary
   492 00000433 83E01F                  	and	eax, 31		 ; set allocation bit position 
   493                                  				 ;  (bit 0 to bit 31)
   494                                  	;
   495 00000436 01D3                    	add	ebx, edx	 ; offset in M.A.T. + M.A.T. address 
   496                                  	;
   497 00000438 0FB303                  	btr 	[ebx], eax	 ; reset relevant bit (0 to 31)
   498                                  	;
   499 0000043B 46                      	inc	esi		 ; next page table
   500 0000043C E2EB                    	loop	mi_10		 ; allocate next kernel page table 
   501                                  				 ; (ecx = page table count + 1)
   502                                  	;
   503 0000043E 59                      	pop	ecx		 ; (**) PDE count (= pg. tbl. count)
   504                                  	;
   505                                  	; Initialize Kernel Page Directory and Kernel Page Tables
   506                                  	;
   507                                  	; Initialize Kernel's Page Directory
   508 0000043F 8B3D[B86F0000]          	mov	edi, [k_page_dir]
   509 00000445 89F8                    	mov	eax, edi
   510 00000447 0C03                    	or	al, PDE_A_PRESENT + PDE_A_WRITE
   511                                  		     	      ; supervisor + read&write + present
   512 00000449 89CA                    	mov	edx, ecx 	; (**) PDE count (= pg. tbl. count)
   513                                  mi_11:
   514 0000044B 0500100000              	add	eax, 4096	; Add page size (PGSZ)
   515                                  			        ; EAX points to next page table
   516 00000450 AB                      	stosd
   517 00000451 E2F8                    	loop	mi_11
   518 00000453 29C0                    	sub	eax, eax	; Empty PDE
   519                                  	;mov	cx, 1024	; Entry count (PGSZ/4)
   520                                  	; 02/01/2022
   521 00000455 B504                    	mov	ch, 4 ; cx = 4*256 = 1024
   522 00000457 29D1                    	sub	ecx, edx
   523 00000459 7402                    	jz	short mi_12
   524 0000045B F3AB                    	rep	stosd 		; clear remain (empty) PDEs
   525                                  	;
   526                                  	; Initialization of Kernel's Page Directory is OK, here.
   527                                  mi_12:
   528                                  	; Initialize Kernel's Page Tables
   529                                  	;
   530                                  	; (EDI points to address of page table 0)
   531                                  	; eax = 0
   532 0000045D 8B0D[BC6F0000]          	mov	ecx, [memory_size] ; memory size in pages
   533 00000463 89CA                    	mov	edx, ecx	; (***)
   534 00000465 B003                    	mov	al, PTE_A_PRESENT + PTE_A_WRITE
   535                                  			     ; supervisor + read&write + present
   536                                  mi_13:
   537 00000467 AB                      	stosd
   538 00000468 0500100000              	add	eax, 4096	
   539 0000046D E2F8                    	loop	mi_13
   540                                  	; 02/01/2022
   541 0000046F 66B9FF03                	mov	cx, 1023
   542 00000473 21CA                    	and	edx, ecx
   543                                  	;and	dx, 1023	; (***)
   544 00000475 7407                    	jz	short mi_14
   545                                  	;mov	cx, 1024	
   546                                  	; 02/01/2022
   547                                  	;mov	ch, 4 ; cx = 4*256 = 1024
   548 00000477 41                      	inc	ecx ; ecx = 1024
   549 00000478 29D1                    	sub	ecx, edx
   550                                  	;sub	cx, dx		; from dx (<= 1023) to 1024
   551 0000047A 31C0                    	xor	eax, eax
   552 0000047C F3AB                    	rep	stosd		; clear remain (empty) PTEs 
   553                                  				; of the last page table
   554                                  mi_14:
   555                                  	;  Initialization of Kernel's Page Tables is OK, here.
   556                                  	;
   557 0000047E 89F8                    	mov	eax, edi	; end of the last page table page
   558                                  			        ; (beginging of user space pages)
   559 00000480 C1E80F                  	shr	eax, 15		; convert to M.A.T. byte offset
   560 00000483 24FC                    	and	al, 0FCh	; clear bit 0 and bit 1 for
   561                                  				; aligning on dword boundary
   562                                  	 
   563 00000485 A3[CC6F0000]            	mov	[first_page], eax
   564 0000048A A3[C46F0000]            	mov	[next_page], eax ; The first free page pointer
   565                                  				 ; for user programs
   566                                  				 ; (Offset in Mem. Alloc. Tbl.)	
   567                                  	;
   568                                  	; Linear/FLAT (1 to 1) memory paging for the kernel is OK, here.
   569                                  	;
   570                                  	
   571                                  	; Enable paging
   572                                  	;
   573 0000048F A1[B86F0000]                    mov     eax, [k_page_dir]
   574 00000494 0F22D8                  	mov	cr3, eax
   575 00000497 0F20C0                  	mov	eax, cr0
   576 0000049A 0D00000080              	or	eax, 80000000h	; set paging bit (bit 31)
   577 0000049F 0F22C0                  	mov	cr0, eax
   578                                          ;jmp    KCODE:StartPMP
   579                                  
   580 000004A2 EA                      	db 0EAh 		; Opcode for far jump
   581 000004A3 [A9040000]                      dd StartPMP		; 32 bit offset
   582 000004A7 0800                    	dw KCODE		; kernel code segment descriptor
   583                                  
   584                                  StartPMP:
   585                                  	; 06/11//2014
   586                                  	; Clear video page 0
   587                                  	;
   588                                  	; Temporary Code
   589                                  	;
   590                                  	;mov	ecx, 80*25/2
   591 000004A9 66B9E803                	mov	cx, (80*25)/2 ; 02/01/2022
   592 000004AD BF00800B00              	mov	edi, 0B8000h
   593 000004B2 57                      	push	edi ; * ; 02/01/2022
   594 000004B3 31C0                    	xor	eax, eax	; black background, black fore color
   595 000004B5 F3AB                    	rep	stosd
   596                                  	
   597                                  	; 19/08/2014
   598                                  	; Kernel Base Address = 0
   599                                  	; It is mapped to (physically) 0 in the page table.
   600                                  	; So, here is exactly 'StartPMP' address.
   601                                  	;
   602                                  	;;mov	ah, 4Eh	; Red background, yellow forecolor
   603                                  	;;mov	esi, msgPM
   604                                  	;; 14/08/2015 (kernel version message will appear
   605                                  	;;	       when protected mode and paging is enabled)
   606 000004B7 B40B                    	mov	ah, 0Bh ; Black background, light cyan forecolor
   607 000004B9 BE[106B0000]            	mov	esi, msgKVER
   608 000004BE 5F                      	pop	edi ; * ; 02/01/2022
   609                                  	;mov	edi, 0B8000h ; 27/08/2014
   610                                  	; 20/08/2014
   611 000004BF E890010000              	call	printk
   612                                  
   613                                  	; 'UNIX v7/x86' source code by Robert Nordier (1999)
   614                                  	; // Set IRQ offsets
   615                                  	;
   616                                  	;  Linux (v0.12) source code by Linus Torvalds (1991)
   617                                  	;
   618                                  					;; ICW1
   619 000004C4 B011                    	mov	al, 11h			; Initialization sequence
   620 000004C6 E620                    	out	20h, al			; 	8259A-1
   621                                  	; jmp 	$+2
   622 000004C8 E6A0                    	out	0A0h, al		; 	8259A-2
   623                                  					;; ICW2
   624 000004CA B020                    	mov	al, 20h			; Start of hardware ints (20h)
   625 000004CC E621                    	out	21h, al			;	for 8259A-1
   626                                  	; jmp 	$+2
   627 000004CE B028                    	mov	al, 28h			; Start of hardware ints (28h)
   628 000004D0 E6A1                    	out	0A1h, al		; 	for 8259A-2
   629                                  					;
   630 000004D2 B004                    	mov	al, 04h			;; ICW3
   631 000004D4 E621                    	out	21h, al			; 	IRQ2 of 8259A-1 (master)
   632                                  	; jmp 	$+2
   633 000004D6 B002                    	mov	al, 02h			; 	is 8259A-2 (slave)
   634 000004D8 E6A1                    	out	0A1h, al		;
   635                                  					;; ICW4
   636 000004DA B001                    	mov	al, 01h	 		;
   637 000004DC E621                    	out	21h, al			; 	8086 mode, normal EOI	
   638                                  	; jmp 	$+2
   639 000004DE E6A1                    	out	0A1h, al		;	for both chips.
   640                                  
   641                                  	;mov	al, 0FFh	; mask off all interrupts for now
   642                                  	;out	21h, al
   643                                  	;; jmp 	$+2
   644                                  	;out	0A1h, al
   645                                  
   646                                  	; 02/04/2015
   647                                  	; 26/03/2015 System call (INT 30h) modification
   648                                  	;  DPL = 3 (Interrupt service routine can be called from user mode)			
   649                                  	;
   650                                  	;; Linux (v0.12) source code by Linus Torvalds (1991)
   651                                  	;  setup_idt:
   652                                  	;
   653                                          ;; 16/02/2015
   654                                  	;;mov	dword [DISKETTE_INT], fdc_int ; IRQ 6 handler
   655                                  	; 21/08/2014 (timer_int)
   656 000004E0 BE[DC670000]            	mov	esi, ilist
   657 000004E5 8D3D[506D0000]          	lea	edi, [idt]
   658                                  	; 26/03/2015
   659                                  	;mov	ecx, 48		; 48 hardware interrupts (INT 0 to INT 2Fh)
   660                                  	; 02/01/2022
   661 000004EB B130                    	mov	cl, 48 ; ecx = 48
   662                                  	; 02/04/2015
   663 000004ED BB00000800              	mov	ebx, 80000h
   664                                  rp_sidt1:
   665 000004F2 AD                      	lodsd
   666 000004F3 89C2                    	mov	edx, eax
   667 000004F5 66BA008E                	mov	dx, 8E00h
   668 000004F9 6689C3                  	mov	bx, ax
   669 000004FC 89D8                    	mov	eax, ebx	; /* selector = 0x0008 = cs */
   670                                         			        ; /* interrupt gate - dpl=0, present */
   671 000004FE AB                      	stosd	; selector & offset bits 0-15 	
   672 000004FF 89D0                    	mov	eax, edx
   673 00000501 AB                      	stosd	; attributes & offset bits 16-23
   674 00000502 E2EE                    	loop	rp_sidt1
   675 00000504 B110                    	mov	cl, 16		; 16 software interrupts (INT 30h to INT 3Fh)
   676                                  rp_sidt2:
   677 00000506 AD                      	lodsd
   678 00000507 21C0                    	and	eax, eax
   679 00000509 7413                    	jz	short rp_sidt3
   680 0000050B 89C2                    	mov	edx, eax
   681 0000050D 66BA00EE                	mov	dx, 0EE00h	; P=1b/DPL=11b/01110b
   682 00000511 6689C3                  	mov	bx, ax
   683 00000514 89D8                    	mov	eax, ebx	; selector & offset bits 0-15 	
   684 00000516 AB                      	stosd
   685 00000517 89D0                    	mov	eax, edx
   686 00000519 AB                      	stosd
   687 0000051A E2EA                    	loop	rp_sidt2
   688 0000051C EB16                    	jmp	short sidt_OK
   689                                  rp_sidt3:
   690 0000051E B8[74090000]            	mov	eax, ignore_int
   691 00000523 89C2                    	mov	edx, eax
   692 00000525 66BA00EE                	mov	dx, 0EE00h	; P=1b/DPL=11b/01110b
   693 00000529 6689C3                  	mov	bx, ax
   694 0000052C 89D8                    	mov	eax, ebx	; selector & offset bits 0-15 	
   695                                  rp_sidt4:
   696 0000052E AB                      	stosd
   697 0000052F 92                      	xchg	eax, edx
   698 00000530 AB                      	stosd
   699 00000531 92                      	xchg	edx, eax
   700 00000532 E2FA                    	loop	rp_sidt4
   701                                  sidt_OK: 
   702 00000534 0F011D[D6670000]        	lidt 	[idtd]
   703                                  	;
   704                                  	; TSS descriptor setup ; 24/03/2015
   705 0000053B B8[506F0000]            	mov	eax, task_state_segment
   706 00000540 66A3[CA670000]          	mov	[gdt_tss0], ax
   707 00000546 C1C010                  	rol	eax, 16
   708 00000549 A2[CC670000]            	mov	[gdt_tss1], al
   709 0000054E 8825[CF670000]          	mov	[gdt_tss2], ah
   710 00000554 66C705[B66F0000]68-     	mov	word [tss.IOPB], tss_end - task_state_segment
   710 0000055C 00                 
   711                                  		; 
   712                                  		; IO Map Base address (When this address points
   713                                  		; to end of the TSS, CPU does not use IO port 
   714                                  		; permission bit map for RING 3 IO permissions, 
   715                                  		; access to any IO ports in ring 3 will be forbidden.)
   716                                   		;
   717                                  	;mov	[tss.esp0], esp ; TSS offset 4
   718                                  	;mov	word [tss.ss0], KDATA ; TSS offset 8 (SS)
   719 0000055D 66B82800                   	mov	ax, TSS  ; It is needed when an interrupt 
   720                                  			 ; occurs (or a system call -software INT- is requested)
   721                                  			 ; while cpu running in ring 3 (in user mode).				
   722                                  			 ; (Kernel stack pointer and segment will be loaded
   723                                  			 ; from offset 4 and 8 of the TSS, by the CPU.)	 
   724 00000561 0F00D8                  	ltr	ax  ; Load task register
   725                                  	;
   726                                  esp0_set0:
   727                                  	; 30/07/2015
   728 00000564 8B0D[BC6F0000]          	mov 	ecx, [memory_size] ; memory size in pages
   729 0000056A C1E10C                  	shl 	ecx, 12 ; convert page count to byte count
   730 0000056D 81F900004000            	cmp	ecx, CORE ; beginning of user's memory space (400000h)
   731                                  			  ; (kernel mode virtual address)
   732 00000573 7605                    	jna	short esp0_set1
   733                                  	;
   734                                  	; If available memory > CORE (end of the 1st 4 MB)
   735                                  	; set stack pointer to CORE
   736                                  	;(Because, PDE 0 is reserved for kernel space in user's page directory)
   737                                  	;(PDE 0 points to page table of the 1st 4 MB virtual address space)
   738 00000575 B900004000              	mov	ecx, CORE
   739                                  esp0_set1:
   740 0000057A 89CC                    	mov	esp, ecx ; top of kernel stack (**tss.esp0**)
   741                                  esp0_set_ok:
   742                                  	; 30/07/2015 (**tss.esp0**) 
   743 0000057C 8925[546F0000]          	mov	[tss.esp0], esp
   744 00000582 66C705[586F0000]10-             mov     word [tss.ss0], KDATA
   744 0000058A 00                 
   745                                  	; 14/08/2015
   746                                  	; 10/11/2014 (Retro UNIX 386 v1 - Erdogan Tan)
   747                                  	;
   748                                  	;cli	; Disable interrupts (for CPU)
   749                                  	;    (CPU will not handle hardware interrupts, except NMI!)
   750                                  	;
   751 0000058B 30C0                    	xor	al, al		; Enable all hardware interrupts!
   752 0000058D E621                    	out	21h, al		; (IBM PC-AT compatibility)
   753 0000058F EB00                    	jmp 	$+2		; (All conventional PC-AT hardware
   754 00000591 E6A1                    	out	0A1h, al	;  interrupts will be in use.)	
   755                                  				; (Even if related hardware component
   756                                  				;  does not exist!)
   757                                  	; Enable NMI 
   758 00000593 B07F                    	mov	al, 7Fh		; Clear bit 7 to enable NMI (again)
   759 00000595 E670                    	out  	70h, al
   760                                  	; 23/02/2015
   761 00000597 90                      	nop
   762 00000598 E471                    	in	al, 71h		; read in 71h just after writing out to 70h
   763                                  				; for preventing unknown state (!?)
   764                                  	;
   765                                  	; Only a NMI can occur here... (Before a 'STI' instruction)
   766                                  	;
   767                                  	; 02/09/2014
   768 0000059A 6631DB                  	xor	bx, bx
   769 0000059D 66BA0002                	mov	dx, 0200h	; Row 2, column 0  ; 07/03/2015
   770 000005A1 E8360F0000              	call	set_cpos
   771                                  	;
   772                                  	; 06/11/2014
   773                                  	; Temporary Code
   774                                  	;
   775 000005A6 E853110000              	call	memory_info
   776                                  	; 14/08/2015
   777                                  	;call	getch ; 28/02/2015
   778                                  drv_init:
   779 000005AB FB                      	sti	; Enable Interrupts 
   780                                  	; 06/02/2015
   781 000005AC 8B15[B06A0000]          	mov	edx, [hd0_type] ; hd0, hd1, hd2, hd3
   782 000005B2 668B1D[AE6A0000]        	mov	bx, [fd0_type] ; fd0, fd1
   783                                  	; 22/02/2015
   784 000005B9 6621DB                  	and	bx, bx
   785 000005BC 751B                    	jnz	short di1
   786                                  	;
   787 000005BE 09D2                    	or 	edx, edx
   788 000005C0 7529                    	jnz	short di2
   789                                  	;
   790                                  setup_error:
   791 000005C2 BE[F96B0000]            	mov 	esi, setup_error_msg
   792                                  psem:	
   793 000005C7 AC                      	lodsb
   794 000005C8 08C0                    	or	al, al
   795                                  	;jz	short haltx ; 22/02/2015
   796 000005CA 7426                    	jz	short di3
   797 000005CC 56                      	push	esi
   798 000005CD 31DB                    	xor	ebx, ebx ; 0
   799                                  			; Video page 0 (bl=0)
   800 000005CF B407                    	mov	ah, 07h ; Black background, 
   801                                  			; light gray forecolor
   802 000005D1 E8E40D0000              	call	write_tty
   803 000005D6 5E                      	pop	esi
   804 000005D7 EBEE                    	jmp	short psem
   805                                  
   806                                  di1:
   807                                  	; supress 'jmp short T6'
   808                                  	;  (activate fdc motor control code)
   809 000005D9 66C705[D5060000]90-     	mov	word [T5], 9090h ; nop
   809 000005E1 90                 
   810                                  	;
   811                                  	;mov	ax, int_0Eh	; IRQ 6 handler
   812                                  	;mov	di, 0Eh*4	; IRQ 6 vector
   813                                  	;stosw
   814                                  	;mov 	ax, cs
   815                                  	;stosw
   816                                  	;; 16/02/2015
   817                                          ;;mov	dword [DISKETTE_INT], fdc_int ; IRQ 6 handler
   818                                  	;
   819 000005E2 E828200000              	CALL	DSKETTE_SETUP	; Initialize Floppy Disks
   820                                  	;
   821 000005E7 09D2                    	or	edx, edx
   822 000005E9 7407                            jz      short di3
   823                                  di2:
   824 000005EB E864200000              	call   	DISK_SETUP	; Initialize Fixed Disks
   825 000005F0 72D0                            jc      short setup_error
   826                                  di3:
   827 000005F2 E8DB100000              	call	setup_rtc_int	; 22/05/2015 (dsectrpm.s)
   828                                  	;
   829 000005F7 E8F9600000              	call	display_disks ; 07/03/2015  (Temporary)
   830                                  ;haltx:
   831                                  	; 14/08/2015
   832                                  	;call	getch ; 22/02/2015
   833 000005FC FB                      	sti	; Enable interrupts (for CPU)
   834                                  	; 14/08/2015
   835                                  	;mov 	ecx, 0FFFFFFFh
   836                                  	; 22/11/2021
   837 000005FD B9FFFF2F00              	mov 	ecx, 02FFFFFh
   838                                  md_info_msg_wait:
   839 00000602 51                      	push 	ecx
   840 00000603 B001                    	mov	al, 1
   841 00000605 8A25[E66F0000]          	mov 	ah, [ptty] ; active (current) video page
   842 0000060B E8325E0000              	call	getc_n
   843 00000610 59                      	pop	ecx
   844 00000611 7502                    	jnz	short md_info_msg_ok
   845 00000613 E2ED                    	loop	md_info_msg_wait
   846                                  md_info_msg_ok:
   847                                  	; 30/06/2015
   848 00000615 E8452D0000              	call	sys_init
   849                                  	;
   850                                  	;jmp 	cpu_reset ; 22/02/2015
   851                                  hang:
   852                                  	; 04/12/2021
   853 0000061A 29C0                    	sub	eax, eax
   854                                  _hang:	
   855                                  	; 23/02/2015
   856                                  	;sti			; Enable interrupts
   857 0000061C F4                      	hlt
   858                                  	;
   859                                  	;nop
   860                                  	;; 03/12/2014
   861                                  	;; 28/08/2014
   862                                  	;mov	ah, 11h
   863                                  	;call	getc
   864                                  	;jz     _c8
   865                                  	;
   866                                  	; 23/02/2015
   867                                  	; 06/02/2015
   868                                  	; 07/09/2014
   869 0000061D 31DB                    	xor	ebx, ebx
   870 0000061F 8A1D[E66F0000]          	mov	bl, [ptty]	; active_page
   871 00000625 89DE                    	mov	esi, ebx
   872 00000627 66D1E6                  	shl 	si, 1
   873 0000062A 81C6[E86F0000]          	add	esi, ttychr
   874 00000630 668B06                  	mov	ax, [esi]
   875                                  	;and	ax, ax
   876                                  	;;jz	short _c8
   877                                  	;jz	short hang
   878                                  	; 04/12/2021
   879 00000633 21C0                    	and	eax, eax
   880 00000635 74E5                    	jz	short _hang
   881 00000637 66C7060000              	mov	word [esi], 0
   882 0000063C 80FB03                  	cmp	bl, 3		; Video page 3
   883                                  	;jb	short _c8
   884 0000063F 72D9                    	jb	short hang
   885                                  	;	
   886                                  	; 02/09/2014
   887 00000641 B40E                    	mov	ah, 0Eh		; Yellow character 
   888                                  				; on black background
   889                                  	; 30/11/2021 (32 bit reg push-pop)
   890                                  	; 07/09/2014
   891                                  nxtl:
   892 00000643 53                      	push	ebx
   893                                  	;
   894                                  	;xor	ebx, ebx	; bl = 0 (video page 0)
   895                                  				; bh = 0 (video mode)
   896                                  				; Retro UNIX 386 v1 - Video Mode 0
   897                                  				; (PC/AT Video Mode 3 - 80x25 Alpha.)
   898 00000644 50                      	push	eax
   899 00000645 E8700D0000              	call 	write_tty
   900 0000064A 58                      	pop	eax
   901                                  	;pop	bx
   902 0000064B 5B                      	pop	ebx
   903 0000064C 3C0D                    	cmp	al, 0Dh		; carriage return (enter)
   904                                  	;jne	short _c8
   905 0000064E 75CA                    	jne	short hang
   906 00000650 B00A                    	mov	al, 0Ah		; next line
   907 00000652 EBEF                    	jmp	short nxtl
   908                                  	
   909                                  ;_c8:
   910                                  ;	; 25/08/2014
   911                                  ;	cli			; Disable interrupts
   912                                  ;	mov	al, [scounter + 1]
   913                                  ;	and	al, al
   914                                  ;	jnz	hang
   915                                  ;	call	rtc_p
   916                                  ;	jmp     hang
   917                                  
   918                                  
   919                                  	; 27/08/2014
   920                                  	; 20/08/2014
   921                                  printk:
   922                                          ;mov    edi, [scr_row]
   923                                  pkl:
   924 00000654 AC                      	lodsb
   925 00000655 08C0                    	or 	al, al
   926 00000657 7404                    	jz	short pkr
   927 00000659 66AB                    	stosw
   928 0000065B EBF7                    	jmp	short pkl
   929                                  pkr:
   930 0000065D C3                      	retn
   931                                  
   932                                  ; 25/07/2015
   933                                  ; 14/05/2015 (multi tasking -time sharing- 'clock', x_timer)
   934                                  ; 17/02/2015
   935                                  ; 06/02/2015 (unix386.s)
   936                                  ; 11/12/2014 - 22/12/2014 (dsectrm2.s) 
   937                                  ;
   938                                  ; IBM PC-XT Model 286 Source Code - BIOS2.ASM (06/10/85)
   939                                  ;
   940                                  ;-- HARDWARE INT  08 H - ( IRQ LEVEL 0 ) ---------------------------------------
   941                                  ;	THIS ROUTINE HANDLES THE TIMER INTERRUPT FROM FROM CHANNEL 0 OF        :
   942                                  ;	THE 8254 TIMER.  INPUT FREQUENCY IS 1.19318 MHZ AND THE DIVISOR        :
   943                                  ;	IS 65536, RESULTING IN APPROXIMATELY 18.2 INTERRUPTS EVERY SECOND.     :
   944                                  ;									       :
   945                                  ;	THE INTERRUPT HANDLER MAINTAINS A COUNT (40:6C) OF INTERRUPTS SINCE    :
   946                                  ;	POWER ON TIME, WHICH MAY BE USED TO ESTABLISH TIME OF DAY.	       :
   947                                  ;	THE INTERRUPT HANDLER ALSO DECREMENTS THE MOTOR CONTROL COUNT (40:40)  :
   948                                  ;	OF THE DISKETTE, AND WHEN IT EXPIRES, WILL TURN OFF THE 	       :
   949                                  ;	DISKETTE MOTOR(s), AND RESET THE MOTOR RUNNING FLAGS.		       :
   950                                  ;	THE INTERRUPT HANDLER WILL ALSO INVOKE A USER ROUTINE THROUGH	       :
   951                                  ;	INTERRUPT 1CH AT EVERY TIME TICK.  THE USER MUST CODE A 	       :
   952                                  ;	ROUTINE AND PLACE THE CORRECT ADDRESS IN THE VECTOR TABLE.	       :
   953                                  ;-------------------------------------------------------------------------------
   954                                  ;
   955                                  
   956                                  timer_int:	; IRQ 0
   957                                  ;int_08h:	; Timer
   958                                  	; 14/10/2015
   959                                  	; Here, we are simulating system call entry (for task switch)
   960                                  	; (If multitasking is enabled, 
   961                                  	; 'clock' procedure may jump to 'sysrelease')
   962 0000065E 1E                      	push	ds
   963 0000065F 06                      	push	es
   964 00000660 0FA0                    	push	fs
   965 00000662 0FA8                    	push	gs
   966 00000664 60                      	pushad  ; eax, ecx, edx, ebx, esp -before pushad-, ebp, esi, edi
   967 00000665 66B91000                	mov     cx, KDATA
   968 00000669 8ED9                            mov     ds, cx
   969 0000066B 8EC1                            mov     es, cx
   970 0000066D 8EE1                            mov     fs, cx
   971 0000066F 8EE9                            mov     gs, cx
   972                                  	;
   973 00000671 0F20D9                  	mov	ecx, cr3
   974 00000674 890D[13070000]          	mov	[cr3reg], ecx ; save current cr3 register value/content
   975                                  	;
   976 0000067A 3B0D[B86F0000]          	cmp 	ecx, [k_page_dir]
   977 00000680 741F                    	je	short T3
   978                                  	;
   979                                  	; timer interrupt has been occurred while OS is in user mode
   980 00000682 A3[D8740000]            	mov 	[u.r0], eax
   981 00000687 89E1                    	mov	ecx, esp
   982 00000689 83C130                  	add	ecx, ESPACE ; 4 * 12 (stack frame)	
   983 0000068C 890D[D0740000]          	mov	[u.sp], ecx ; kernel stack pointer at the start of interrupt
   984 00000692 8925[D4740000]          	mov	[u.usp], esp ; kernel stack points to user's registers   
   985                                  	;
   986 00000698 8B0D[B86F0000]          	mov	ecx, [k_page_dir]
   987 0000069E 0F22D9                  	mov	cr3, ecx
   988                                  T3:
   989 000006A1 FB                      	sti				; INTERRUPTS BACK ON
   990 000006A2 66FF05[34700000]        	INC	word [TIMER_LOW]	; INCREMENT TIME
   991 000006A9 7507                    	JNZ	short T4		; GO TO TEST_DAY
   992 000006AB 66FF05[36700000]        	INC	word [TIMER_HIGH]	; INCREMENT HIGH WORD OF TIME
   993                                  T4:					; TEST_DAY
   994 000006B2 66833D[36700000]18      	CMP	word [TIMER_HIGH],018H	; TEST FOR COUNT EQUALING 24 HOURS
   995 000006BA 7519                    	JNZ	short T5		; GO TO DISKETTE_CTL
   996 000006BC 66813D[34700000]B0-     	CMP	word [TIMER_LOW],0B0H
   996 000006C4 00                 
   997 000006C5 750E                    	JNZ	short T5		; GO TO DISKETTE_CTL
   998                                  
   999                                  ;-----	TIMER HAS GONE 24 HOURS
  1000                                  	;;SUB	AX,AX
  1001                                  	;MOV	[TIMER_HIGH],AX
  1002                                  	;MOV	[TIMER_LOW],AX
  1003 000006C7 29C0                    	sub	eax, eax
  1004 000006C9 A3[34700000]            	mov	[TIMER_LH], eax
  1005                                  	;	
  1006 000006CE C605[38700000]01        	MOV	byte [TIMER_OFL],1
  1007                                  
  1008                                  ;-----	TEST FOR DISKETTE TIME OUT
  1009                                  
  1010                                  T5:
  1011                                  	; 23/12/2014
  1012 000006D5 EB1D                    	jmp	short T6		; will be replaced with nop, nop
  1013                                  					; (9090h) if a floppy disk
  1014                                  					; is detected.
  1015                                  	;mov	al,[CS:MOTOR_COUNT]
  1016 000006D7 A0[3B700000]            	mov	al, [MOTOR_COUNT]
  1017 000006DC FEC8                    	dec	al
  1018                                  	;mov	[CS:MOTOR_COUNT], al	; DECREMENT DISKETTE MOTOR CONTROL
  1019 000006DE A2[3B700000]            	mov	[MOTOR_COUNT], al
  1020                                  	;mov	[ORG_MOTOR_COUNT], al
  1021 000006E3 750F                    	JNZ	short T6		; RETURN IF COUNT NOT OUT
  1022 000006E5 B0F0                    	mov 	al,0F0h
  1023                                  	;AND	[CS:MOTOR_STATUS],al 	; TURN OFF MOTOR RUNNING BITS
  1024 000006E7 2005[3A700000]          	and	[MOTOR_STATUS], al
  1025                                  	;and	[ORG_MOTOR_STATUS], al
  1026 000006ED B00C                    	MOV	AL,0CH			; bit 3 = enable IRQ & DMA, 
  1027                                  					; bit 2 = enable controller
  1028                                  					;	1 = normal operation
  1029                                  					;	0 = reset	
  1030                                  					; bit 0, 1 = drive select
  1031                                  					; bit 4-7 = motor running bits 
  1032 000006EF 66BAF203                	MOV	DX,03F2H		; FDC CTL PORT
  1033 000006F3 EE                      	OUT	DX,AL			; TURN OFF THE MOTOR
  1034                                  T6:	
  1035                                  	;inc	word [CS:wait_count]	; 22/12/2014 (byte -> word)
  1036                                  					; TIMER TICK INTERRUPT
  1037                                  	;;inc	word [wait_count] ;;27/02/2015
  1038                                  	;INT	1CH			; TRANSFER CONTROL TO A USER ROUTINE
  1039                                  	;;;;cli
  1040                                  	;call 	u_timer			; TRANSFER CONTROL TO A USER ROUTINE
  1041 000006F4 FF15[0F070000]          	call	[x_timer] ; 14/05/2015
  1042                                  T7:
  1043                                  	; 14/10/2015
  1044 000006FA B020                    	MOV	AL,EOI			; GET END OF INTERRUPT MASK
  1045 000006FC FA                      	CLI				; DISABLE INTERRUPTS TILL STACK CLEARED
  1046 000006FD E620                    	OUT	INTA00,AL		; END OF INTERRUPT TO 8259 - 1	
  1047                                  	;
  1048 000006FF A1[13070000]            	mov 	eax, [cr3reg] 		; previous value/content of cr3 register
  1049 00000704 0F22D8                   	mov	cr3, eax  ; restore cr3 register content
  1050                                  	;
  1051 00000707 61                      	popad ; edi, esi, ebp, temp (icrement esp by 4), ebx, edx, ecx, eax
  1052                                  	;
  1053 00000708 0FA9                    	pop	gs
  1054 0000070A 0FA1                    	pop	fs
  1055 0000070C 07                      	pop	es
  1056 0000070D 1F                      	pop	ds
  1057 0000070E CF                      	iretd	; return from interrupt
  1058                                  
  1059                                  
  1060                                  ; ////////////////
  1061                                  
  1062                                  ; 14/05/2015 - Multi tasking 'clock' procedure (sys emt)
  1063                                  x_timer:
  1064 0000070F [17070000]              	dd 	u_timer			; 14/05/2015
  1065                                  	;dd	clock
  1066                                  
  1067                                  ; 14/10/2015
  1068 00000713 00000000                cr3reg: dd 0
  1069                                  
  1070                                  	; 04/12/2021 - Retro UNIX 386 v1.2
  1071                                  	; 06/02/2015
  1072                                  	; 07/09/2014
  1073                                  	; 21/08/2014
  1074                                  u_timer:
  1075                                  ;timer_int:	; IRQ 0
  1076                                  	; 06/02/2015
  1077                                  	;push	eax
  1078                                  	;push	edx
  1079                                  	;push	ecx
  1080                                  	;push	ebx
  1081                                  	;push	ds
  1082                                  	;push	es
  1083                                  	;mov	eax, KDATA
  1084                                  	;mov	ds, ax
  1085                                  	;mov	es, ax
  1086 00000717 FF05[FC6F0000]          	inc	dword [tcount]
  1087 0000071D BB[826B0000]            	mov	ebx, tcountstr + 4
  1088                                  	;mov	ax, [tcount]
  1089                                  	; 04/12/2021
  1090 00000722 A1[FC6F0000]            	mov	eax, [tcount]
  1091 00000727 B90A000000              	mov	ecx, 10
  1092                                  rp_divtcnt:
  1093 0000072C 31D2                    	xor	edx, edx
  1094 0000072E F7F1                    	div	ecx
  1095 00000730 80C230                  	add	dl, 30h
  1096 00000733 8813                    	mov	[ebx], dl
  1097                                  	;or	ax, ax
  1098                                  	; 04/12/2021
  1099 00000735 09C0                    	or	eax, eax
  1100 00000737 7403                    	jz	short print_lzero
  1101 00000739 4B                      	dec	ebx
  1102 0000073A EBF0                    	jmp	short rp_divtcnt
  1103                                  print_lzero:
  1104 0000073C 81FB[7E6B0000]          	cmp	ebx, tcountstr
  1105 00000742 7606                    	jna	short print_tcount
  1106 00000744 4B                      	dec	ebx
  1107 00000745 C60330                   	mov	byte [ebx], 30h
  1108 00000748 EBF2                    	jmp	short print_lzero
  1109                                  print_tcount:
  1110 0000074A 56                      	push	esi
  1111 0000074B 57                      	push	edi
  1112 0000074C BE[5A6B0000]            	mov	esi, timer_msg ; Timer interrupt message
  1113                                  	; 07/09/2014
  1114                                  	;mov	bx, 1	; Video page 1
  1115                                  	; 04/12/2021
  1116 00000751 29DB                    	sub	ebx, ebx
  1117                                  	;inc	bl ; ebx = 1
  1118                                  	; 02/01/2022
  1119 00000753 B306                    	mov	bl, 6	; Video page 6
  1120                                  ptmsg:
  1121 00000755 AC                      	lodsb
  1122 00000756 08C0                    	or	al, al
  1123 00000758 740D                    	jz	short ptmsg_ok
  1124 0000075A 56                      	push	esi
  1125                                  	;push	bx
  1126                                  	; 04/12/2021
  1127 0000075B 53                              push	ebx
  1128 0000075C B42F                    	mov     ah, 2Fh ; Green background, white forecolor
  1129 0000075E E8570C0000              	call 	write_tty
  1130                                  	;pop	bx
  1131                                  	; 04/12/2021
  1132 00000763 5B                      	pop	ebx
  1133 00000764 5E                      	pop	esi
  1134 00000765 EBEE                    	jmp	short ptmsg
  1135                                  	;; 27/08/2014
  1136                                  	;mov	edi, 0B8000h + 0A0h ; Row 1
  1137                                  	;call	printk
  1138                                  	;
  1139                                  ptmsg_ok:
  1140                                  	; 07/09/2014
  1141                                  	;xor	dx, dx		; column 0, row 0
  1142                                  	; 04/12/2021
  1143 00000767 31D2                    	xor	edx, edx
  1144 00000769 E86E0D0000              	call	set_cpos	; set cursor position to 0,0 
  1145                                  	; 23/02/2015
  1146                                  	; 25/08/2014
  1147                                  	;mov	ebx, scounter		; (seconds counter)
  1148                                  	;dec	byte [ebx+1]		; (for reading real time clock)
  1149                                  ;	dec	byte [scounter+1]
  1150                                  ;;	jns	short timer_eoi		; 0 -> 0FFh ?
  1151                                  ;	jns	short u_timer_retn
  1152                                  	; 26/02/2015
  1153                                  ;	call	rtc_p
  1154                                  ;	mov	ebx, scounter		; (seconds counter)
  1155                                  ;	mov	byte [ebx+1], 18	; (18.2 timer ticks per second)
  1156                                  ;	dec 	byte [ebx]		; 19+18+18+18+18 (5)	
  1157                                  ;	jnz	short timer_eoi		; (109 timer ticks in 5 seconds)
  1158                                  ;	jnz	short u_timer_retn ; 06/02/2015
  1159                                  ;	mov	byte [ebx], 5
  1160                                  ;	inc	byte [ebx+1] ; 19
  1161                                  ;;timer_eoi:
  1162                                  ;;	mov	al, 20h ; END OF INTERRUPT COMMAND TO 8259
  1163                                  ;;	out	20h, al	; 8259 PORT
  1164                                  	;
  1165                                  ;u_timer_retn:  ; 06/02/2015
  1166 0000076E 5F                      	pop	edi
  1167 0000076F 5E                      	pop	esi
  1168                                  	;pop	es
  1169                                  	;pop	ds
  1170                                  	;pop	ebx
  1171                                  	;pop	ecx
  1172                                  	;pop	edx
  1173                                  	;pop	eax
  1174                                  	;iret
  1175 00000770 C3                      	retn	; 06/02/2015
  1176                                  
  1177                                  	; 28/08/2014
  1178                                  irq0:
  1179 00000771 6A00                            push 	dword 0
  1180 00000773 EB48                    	jmp	short which_irq
  1181                                  irq1:
  1182 00000775 6A01                            push 	dword 1
  1183 00000777 EB44                    	jmp	short which_irq
  1184                                  irq2:
  1185 00000779 6A02                            push 	dword 2
  1186 0000077B EB40                    	jmp	short which_irq
  1187                                  irq3:
  1188                                  	; 20/11/2015
  1189                                  	; 24/10/2015
  1190 0000077D 2EFF15[A13A0000]        	call	dword [cs:com2_irq3]
  1191 00000784 6A03                    	push 	dword 3
  1192 00000786 EB35                    	jmp	short which_irq
  1193                                  irq4:
  1194                                  	; 20/11/2015
  1195                                  	; 24/10/2015
  1196 00000788 2EFF15[9D3A0000]        	call	dword [cs:com1_irq4]
  1197 0000078F 6A04                            push 	dword 4
  1198 00000791 EB2A                    	jmp	short which_irq
  1199                                  irq5:
  1200 00000793 6A05                            push 	dword 5
  1201 00000795 EB26                    	jmp	short which_irq
  1202                                  irq6:
  1203 00000797 6A06                            push 	dword 6
  1204 00000799 EB22                    	jmp	short which_irq
  1205                                  irq7:
  1206 0000079B 6A07                            push 	dword 7
  1207 0000079D EB1E                    	jmp	short which_irq
  1208                                  irq8:
  1209 0000079F 6A08                            push 	dword 8
  1210 000007A1 EB1A                    	jmp	short which_irq
  1211                                  irq9:
  1212 000007A3 6A09                            push 	dword 9
  1213 000007A5 EB16                    	jmp	short which_irq
  1214                                  irq10:
  1215 000007A7 6A0A                            push 	dword 10
  1216 000007A9 EB12                    	jmp	short which_irq
  1217                                  irq11:
  1218 000007AB 6A0B                            push 	dword 11
  1219 000007AD EB0E                    	jmp	short which_irq
  1220                                  irq12:
  1221 000007AF 6A0C                            push 	dword 12
  1222 000007B1 EB0A                    	jmp	short which_irq
  1223                                  irq13:
  1224 000007B3 6A0D                            push 	dword 13
  1225 000007B5 EB06                    	jmp	short which_irq
  1226                                  irq14:
  1227 000007B7 6A0E                            push 	dword 14
  1228 000007B9 EB02                    	jmp	short which_irq
  1229                                  irq15:
  1230 000007BB 6A0F                            push 	dword 15
  1231                                  	;jmp	short which_irq
  1232                                  
  1233                                  	; 19/10/2015
  1234                                  	; 29/08/2014
  1235                                  	; 21/08/2014
  1236                                  which_irq:
  1237 000007BD 870424                  	xchg	eax, [esp]  ; 28/08/2014
  1238 000007C0 53                      	push	ebx
  1239 000007C1 56                      	push	esi
  1240 000007C2 57                      	push	edi
  1241 000007C3 1E                      	push 	ds
  1242 000007C4 06                      	push 	es
  1243                                  	;
  1244 000007C5 88C3                    	mov	bl, al
  1245                                  	;
  1246 000007C7 B810000000              	mov	eax, KDATA
  1247 000007CC 8ED8                    	mov	ds, ax
  1248 000007CE 8EC0                    	mov	es, ax
  1249                                  	; 19/10/2015
  1250 000007D0 FC                      	cld
  1251                                          ; 27/08/2014
  1252 000007D1 8105[086B0000]A000-             add     dword [scr_row], 0A0h
  1252 000007D9 0000               
  1253                                  	;
  1254 000007DB B417                    	mov	ah, 17h	; blue (1) background, 
  1255                                  			; light gray (7) forecolor
  1256 000007DD 8B3D[086B0000]                  mov     edi, [scr_row]
  1257 000007E3 B049                    	mov	al, 'I'
  1258 000007E5 66AB                    	stosw
  1259 000007E7 B052                    	mov	al, 'R'
  1260 000007E9 66AB                    	stosw
  1261 000007EB B051                    	mov	al, 'Q'
  1262 000007ED 66AB                    	stosw
  1263 000007EF B020                    	mov	al, ' '
  1264 000007F1 66AB                    	stosw
  1265 000007F3 88D8                    	mov	al, bl
  1266 000007F5 3C0A                    	cmp	al, 10
  1267 000007F7 7208                    	jb	short iix
  1268 000007F9 B031                    	mov	al, '1'
  1269 000007FB 66AB                    	stosw
  1270 000007FD 88D8                    	mov	al, bl
  1271 000007FF 2C0A                    	sub	al, 10
  1272                                  iix:
  1273 00000801 0430                    	add	al, '0'
  1274 00000803 66AB                    	stosw
  1275 00000805 B020                    	mov	al, ' '
  1276 00000807 66AB                    	stosw
  1277 00000809 B021                    	mov	al, '!'
  1278 0000080B 66AB                    	stosw
  1279 0000080D B020                    	mov	al, ' '
  1280 0000080F 66AB                    	stosw
  1281                                  	; 23/02/2015
  1282 00000811 80FB07                  	cmp	bl, 7 ; check for IRQ 8 to IRQ 15 
  1283                                  	;jna	iiret
  1284                                  	; 04/12/2021
  1285 00000814 7604                    	jna	short iiz
  1286                                  iiy:
  1287 00000816 B020                    	mov	al, 20h  ; END OF INTERRUPT COMMAND TO
  1288 00000818 E6A0                    	out	0A0h, al ; the 2nd 8259
  1289                                  iiz:
  1290 0000081A E983010000              	jmp     iiret
  1291                                  	;
  1292                                  	; 22/08/2014
  1293                                  	;mov	al, 20h ; END OF INTERRUPT COMMAND TO 8259
  1294                                  	;out	20h, al	; 8259 PORT
  1295                                  	;
  1296                                  	;pop	es
  1297                                  	;pop	ds
  1298                                  	;pop	edi
  1299                                  	;pop	esi
  1300                                  	;pop	ebx
  1301                                  	;pop 	eax
  1302                                  	;iret
  1303                                  
  1304                                  	; 02/04/2015
  1305                                  	; 25/08/2014
  1306                                  exc0:
  1307 0000081F 6A00                            push 	dword 0
  1308 00000821 E990000000                      jmp     cpu_except
  1309                                  exc1:
  1310 00000826 6A01                            push 	dword 1
  1311 00000828 E989000000                      jmp     cpu_except
  1312                                  exc2:
  1313 0000082D 6A02                            push 	dword 2
  1314 0000082F E982000000                      jmp     cpu_except
  1315                                  exc3:
  1316 00000834 6A03                            push 	dword 3
  1317 00000836 EB7E                            jmp     cpu_except
  1318                                  exc4:
  1319 00000838 6A04                            push 	dword 4
  1320 0000083A EB7A                            jmp     cpu_except
  1321                                  exc5:
  1322 0000083C 6A05                            push 	dword 5
  1323 0000083E EB76                            jmp     cpu_except
  1324                                  exc6:
  1325 00000840 6A06                            push 	dword 6
  1326 00000842 EB72                            jmp     cpu_except
  1327                                  exc7:
  1328 00000844 6A07                            push 	dword 7
  1329 00000846 EB6E                            jmp     cpu_except
  1330                                  exc8:
  1331                                  	; [esp] = Error code
  1332 00000848 6A08                            push 	dword 8
  1333 0000084A EB5C                            jmp     cpu_except_en
  1334                                  exc9:
  1335 0000084C 6A09                            push 	dword 9
  1336 0000084E EB66                            jmp     cpu_except
  1337                                  exc10:
  1338                                  	; [esp] = Error code
  1339 00000850 6A0A                            push 	dword 10
  1340 00000852 EB54                            jmp     cpu_except_en
  1341                                  exc11:
  1342                                  	; [esp] = Error code
  1343 00000854 6A0B                            push 	dword 11
  1344 00000856 EB50                            jmp     cpu_except_en
  1345                                  exc12:
  1346                                  	; [esp] = Error code
  1347 00000858 6A0C                            push 	dword 12
  1348 0000085A EB4C                            jmp     cpu_except_en
  1349                                  exc13:
  1350                                  	; [esp] = Error code
  1351 0000085C 6A0D                            push 	dword 13
  1352 0000085E EB48                            jmp     cpu_except_en
  1353                                  exc14:
  1354                                  	; [esp] = Error code
  1355 00000860 6A0E                            push 	dword 14
  1356 00000862 EB44                    	jmp	short cpu_except_en
  1357                                  exc15:
  1358 00000864 6A0F                            push 	dword 15
  1359 00000866 EB4E                            jmp     cpu_except
  1360                                  exc16:
  1361 00000868 6A10                            push 	dword 16
  1362 0000086A EB4A                            jmp     cpu_except
  1363                                  exc17:
  1364                                  	; [esp] = Error code
  1365 0000086C 6A11                            push 	dword 17
  1366 0000086E EB38                    	jmp	short cpu_except_en
  1367                                  exc18:
  1368 00000870 6A12                            push 	dword 18
  1369 00000872 EB42                    	jmp	short cpu_except
  1370                                  exc19:
  1371 00000874 6A13                            push 	dword 19
  1372 00000876 EB3E                    	jmp	short cpu_except
  1373                                  exc20:
  1374 00000878 6A14                            push 	dword 20
  1375 0000087A EB3A                    	jmp	short cpu_except
  1376                                  exc21:
  1377 0000087C 6A15                            push 	dword 21
  1378 0000087E EB36                    	jmp	short cpu_except
  1379                                  exc22:
  1380 00000880 6A16                            push 	dword 22
  1381 00000882 EB32                    	jmp	short cpu_except
  1382                                  exc23:
  1383 00000884 6A17                            push 	dword 23
  1384 00000886 EB2E                    	jmp	short cpu_except
  1385                                  exc24:
  1386 00000888 6A18                            push 	dword 24
  1387 0000088A EB2A                    	jmp	short cpu_except
  1388                                  exc25:
  1389 0000088C 6A19                            push 	dword 25
  1390 0000088E EB26                    	jmp	short cpu_except
  1391                                  exc26:
  1392 00000890 6A1A                            push 	dword 26
  1393 00000892 EB22                    	jmp	short cpu_except
  1394                                  exc27:
  1395 00000894 6A1B                            push 	dword 27
  1396 00000896 EB1E                    	jmp	short cpu_except
  1397                                  exc28:
  1398 00000898 6A1C                            push 	dword 28
  1399 0000089A EB1A                    	jmp	short cpu_except
  1400                                  exc29:
  1401 0000089C 6A1D                            push 	dword 29
  1402 0000089E EB16                    	jmp	short cpu_except
  1403                                  exc30:
  1404 000008A0 6A1E                            push 	dword 30
  1405 000008A2 EB04                    	jmp	short cpu_except_en
  1406                                  exc31:
  1407 000008A4 6A1F                            push 	dword 31
  1408 000008A6 EB0E                            jmp     short cpu_except
  1409                                  
  1410                                  	; 02/01/2022 (Retro UNIX 386 v1.2)
  1411                                  	; 19/10/2015
  1412                                  	; 19/09/2015
  1413                                  	; 01/09/2015
  1414                                  	; 28/08/2015
  1415                                  	; 28/08/2014
  1416                                  cpu_except_en:
  1417 000008A8 87442404                	xchg	eax, [esp+4] ; Error code
  1418 000008AC 36A3[68700000]          	mov	[ss:error_code], eax
  1419 000008B2 58                      	pop	eax  ; Exception number
  1420 000008B3 870424                  	xchg	eax, [esp]
  1421                                  		; eax = eax before exception
  1422                                  		; [esp] -> exception number
  1423                                  		; [esp+4] -> EIP to return
  1424                                  	; 19/10/2015
  1425                                  	; 19/09/2015
  1426                                  	; 01/09/2015
  1427                                  	; 28/08/2015
  1428                                  	; 29/08/2014
  1429                                  	; 28/08/2014
  1430                                  	; 25/08/2014
  1431                                  	; 21/08/2014
  1432                                  cpu_except:	; CPU Exceptions
  1433 000008B6 FC                      	cld
  1434 000008B7 870424                  	xchg	eax, [esp] 
  1435                                  		; eax = Exception number
  1436                                  		; [esp] = eax (before exception)
  1437 000008BA 53                      	push	ebx
  1438 000008BB 56                      	push	esi
  1439 000008BC 57                      	push	edi
  1440 000008BD 1E                      	push 	ds
  1441 000008BE 06                      	push 	es
  1442                                  	; 28/08/2015
  1443 000008BF 66BB1000                	mov	bx, KDATA
  1444 000008C3 8EDB                    	mov	ds, bx
  1445 000008C5 8EC3                    	mov	es, bx
  1446 000008C7 0F20DB                  	mov	ebx, cr3
  1447 000008CA 53                      	push	ebx ; (*) page directory
  1448                                  	; 19/10/2015
  1449 000008CB FC                      	cld
  1450                                  	; 25/03/2015
  1451 000008CC 8B1D[B86F0000]          	mov	ebx, [k_page_dir]
  1452 000008D2 0F22DB                  	mov	cr3, ebx
  1453                                  	; 28/08/2015
  1454 000008D5 83F80E                  	cmp	eax, 0Eh ; 14, PAGE FAULT
  1455 000008D8 7513                    	jne	short cpu_except_nfp
  1456 000008DA E83A280000              	call	page_fault_handler
  1457 000008DF 21C0                    	and 	eax, eax
  1458                                  	;jz	iiretp ; 01/09/2015
  1459                                  	; 02/01/2022
  1460 000008E1 7505                    	jnz	short cpu_except_pf
  1461 000008E3 E9B6000000              	jmp	iiretp
  1462                                  cpu_except_pf:
  1463 000008E8 B80E000000              	mov	eax, 0Eh ; 14
  1464                                  cpu_except_nfp:
  1465                                  	; 02/04/2015
  1466 000008ED BB[1A060000]            	mov	ebx, hang
  1467 000008F2 875C241C                	xchg	ebx, [esp+28]
  1468                                  		; EIP (points to instruction which faults)
  1469                                  	  	; New EIP (hang)
  1470 000008F6 891D[6C700000]          	mov	[FaultOffset], ebx
  1471 000008FC C744242008000000        	mov	dword [esp+32], KCODE ; kernel's code segment
  1472 00000904 814C242400020000        	or	dword [esp+36], 200h ; enable interrupts (set IF)
  1473                                  	;
  1474 0000090C 88C4                    	mov	ah, al
  1475 0000090E 240F                    	and	al, 0Fh
  1476 00000910 3C09                    	cmp	al, 9
  1477 00000912 7602                    	jna	short h1ok
  1478 00000914 0407                    	add	al, 'A'-':'
  1479                                  h1ok:
  1480 00000916 D0EC                    	shr	ah, 1
  1481 00000918 D0EC                    	shr	ah, 1
  1482 0000091A D0EC                    	shr	ah, 1
  1483 0000091C D0EC                    	shr	ah, 1
  1484 0000091E 80FC09                  	cmp	ah, 9
  1485 00000921 7603                    	jna	short h2ok
  1486 00000923 80C407                  	add	ah, 'A'-':'
  1487                                  h2ok:	
  1488 00000926 86E0                    	xchg 	ah, al	
  1489 00000928 66053030                	add	ax, '00'
  1490 0000092C 66A3[966B0000]          	mov	[excnstr], ax
  1491                                  	;
  1492                                  	; 29/08/2014
  1493 00000932 A1[6C700000]            	mov	eax, [FaultOffset]
  1494 00000937 51                      	push	ecx
  1495 00000938 52                      	push	edx
  1496 00000939 89E3                    	mov	ebx, esp
  1497                                  	; 28/08/2015
  1498 0000093B B910000000              	mov	ecx, 16	  ; divisor value to convert binary number
  1499                                  			  ; to hexadecimal string
  1500                                  	;mov	ecx, 10	    ; divisor to convert
  1501                                  			    ; binary number to decimal string
  1502                                  b2d1:
  1503 00000940 31D2                    	xor	edx, edx
  1504 00000942 F7F1                    	div	ecx
  1505                                  	;push	dx
  1506                                  	; 02/01/2022
  1507 00000944 52                      	push	edx
  1508 00000945 39C8                    	cmp	eax, ecx
  1509 00000947 73F7                    	jnb	short b2d1
  1510 00000949 BF[A16B0000]            	mov	edi, EIPstr ; EIP value
  1511                                  			    ; points to instruction which faults
  1512                                  	; 28/08/2015
  1513 0000094E 89C2                    	mov	edx, eax
  1514                                  b2d2:
  1515                                  	;add	al, '0'
  1516 00000950 8A82[B6170000]          	mov	al, [edx+hexchrs]
  1517 00000956 AA                      	stosb		    ; write hexadecimal digit to its place
  1518 00000957 39E3                    	cmp	ebx, esp
  1519 00000959 7605                    	jna	short b2d3
  1520                                  	; 02/01/2022
  1521 0000095B 58                      	pop	eax
  1522                                  	;pop	ax
  1523 0000095C 88C2                    	mov	dl, al
  1524 0000095E EBF0                    	jmp	short b2d2
  1525                                  b2d3:
  1526 00000960 B068                    	mov 	al, 'h' ; 28/08/2015
  1527 00000962 AA                      	stosb
  1528 00000963 B020                    	mov	al, 20h	    ; space
  1529 00000965 AA                      	stosb
  1530 00000966 30C0                    	xor	al, al	    ; to do it an ASCIIZ string	
  1531 00000968 AA                      	stosb
  1532                                  	;
  1533 00000969 5A                      	pop	edx
  1534 0000096A 59                      	pop	ecx
  1535                                  	;
  1536 0000096B B44F                    	mov	ah, 4Fh	; red (4) background, 
  1537                                  			; white (F) forecolor
  1538 0000096D BE[866B0000]            	mov	esi, exc_msg ; message offset
  1539                                  	;
  1540 00000972 EB11                    	jmp	short piemsg
  1541                                  	;
  1542                                          ;add    dword [scr_row], 0A0h
  1543                                          ;mov    edi, [scr_row]
  1544                                          ;
  1545                                  	;call 	printk
  1546                                  	;
  1547                                  	;mov	al, 20h ; END OF INTERRUPT COMMAND TO 8259
  1548                                  	;out	20h, al	; 8259 PORT
  1549                                  	;
  1550                                  	;pop	es
  1551                                  	;pop	ds
  1552                                  	;pop	edi
  1553                                  	;pop	esi
  1554                                  	;pop 	eax
  1555                                  	;iret
  1556                                  	
  1557                                  	; 28/08/2015
  1558                                  	; 23/02/2015
  1559                                  	; 20/08/2014
  1560                                  ignore_int:
  1561 00000974 50                      	push	eax
  1562 00000975 53                      	push	ebx ; 23/02/2015
  1563 00000976 56                      	push	esi
  1564 00000977 57                      	push	edi
  1565 00000978 1E                      	push 	ds
  1566 00000979 06                      	push 	es
  1567                                  	; 28/08/2015
  1568 0000097A 0F20D8                  	mov	eax, cr3
  1569 0000097D 50                      	push	eax ; (*) page directory
  1570                                  	;
  1571 0000097E B467                    	mov	ah, 67h	; brown (6) background, 
  1572                                  			; light gray (7) forecolor
  1573 00000980 BE[446B0000]            	mov	esi, int_msg ; message offset
  1574                                  piemsg:
  1575                                          ; 27/08/2014
  1576 00000985 8105[086B0000]A000-             add     dword [scr_row], 0A0h
  1576 0000098D 0000               
  1577 0000098F 8B3D[086B0000]                  mov     edi, [scr_row]
  1578                                          ;
  1579 00000995 E8BAFCFFFF              	call 	printk
  1580                                  	;
  1581                                  	; 23/02/2015
  1582 0000099A B020                    	mov	al, 20h  ; END OF INTERRUPT COMMAND TO
  1583 0000099C E6A0                    	out	0A0h, al ; the 2nd 8259
  1584                                  iiretp: ; 01/09/2015
  1585                                  	; 28/08/2015
  1586 0000099E 58                      	pop	eax ; (*) page directory
  1587 0000099F 0F22D8                  	mov	cr3, eax
  1588                                  	;
  1589                                  iiret:
  1590                                  	; 22/08/2014
  1591 000009A2 B020                    	mov	al, 20h ; END OF INTERRUPT COMMAND TO 8259
  1592 000009A4 E620                    	out	20h, al	; 8259 PORT
  1593                                  	;
  1594 000009A6 07                      	pop	es
  1595 000009A7 1F                      	pop	ds
  1596 000009A8 5F                      	pop	edi
  1597 000009A9 5E                      	pop	esi
  1598 000009AA 5B                      	pop	ebx ; 29/08/2014
  1599 000009AB 58                      	pop 	eax
  1600 000009AC CF                      	iretd
  1601                                  
  1602                                  	; 26/02/2015
  1603                                  	; 07/09/2014
  1604                                  	; 25/08/2014
  1605                                  rtc_int:       ; Real Time Clock Interrupt (IRQ 8)
  1606                                  	; 22/08/2014
  1607 000009AD 50                      	push	eax
  1608 000009AE 53                      	push	ebx ; 29/08/2014
  1609 000009AF 56                      	push	esi
  1610 000009B0 57                      	push	edi
  1611 000009B1 1E                      	push 	ds
  1612 000009B2 06                      	push 	es
  1613                                  	;
  1614 000009B3 B810000000              	mov	eax, KDATA
  1615 000009B8 8ED8                    	mov	ds, ax
  1616 000009BA 8EC0                    	mov	es, ax
  1617                                  	;
  1618                                  	; 25/08/2014
  1619 000009BC E884000000              	call	rtc_p
  1620                                  	;
  1621                                  	; 22/02/2015 - dsectpm.s
  1622                                  	; [ source: http://wiki.osdev.org/RTC ]
  1623                                  	; read status register C to complete procedure
  1624                                  	;(it is needed to get a next IRQ 8) 
  1625 000009C1 B00C                    	mov	al, 0Ch ; 
  1626 000009C3 E670                    	out	70h, al ; select register C
  1627 000009C5 90                      	nop
  1628 000009C6 E471                    	in	al, 71h ; just throw away contents
  1629                                  	; 22/02/2015
  1630 000009C8 B020                    	MOV	AL,EOI		; END OF INTERRUPT
  1631 000009CA E6A0                    	OUT	INTB00,AL	; FOR CONTROLLER #2
  1632                                  	;
  1633 000009CC EBD4                    	jmp	short iiret	
  1634                                  
  1635                                  	; 22/08/2014
  1636                                  	; IBM PC/AT BIOS source code ----- 10/06/85 (bios.asm)
  1637                                  	; (INT 1Ah)
  1638                                  	;; Linux (v0.12) source code (main.c) by Linus Torvalds (1991)
  1639                                  time_of_day:
  1640 000009CE E85F010000              	call	UPD_IPR		; WAIT TILL UPDATE NOT IN PROGRESS
  1641 000009D3 726F                            jc      short rtc_retn 
  1642 000009D5 B000                    	mov	al, CMOS_SECONDS
  1643 000009D7 E840010000              	call	CMOS_READ
  1644 000009DC A2[2C700000]            	mov	[time_seconds], al 
  1645 000009E1 B002                    	mov	al, CMOS_MINUTES
  1646 000009E3 E834010000              	call	CMOS_READ
  1647 000009E8 A2[2D700000]            	mov	[time_minutes], al 
  1648 000009ED B004                    	mov	al, CMOS_HOURS
  1649 000009EF E828010000              	call	CMOS_READ
  1650 000009F4 A2[2E700000]                    mov     [time_hours], al
  1651 000009F9 B006                    	mov	al, CMOS_DAY_WEEK 
  1652 000009FB E81C010000              	call	CMOS_READ
  1653 00000A00 A2[2F700000]            	mov	[date_wday], al
  1654 00000A05 B007                     	mov	al, CMOS_DAY_MONTH
  1655 00000A07 E810010000              	call	CMOS_READ
  1656 00000A0C A2[30700000]            	mov	[date_day], al
  1657 00000A11 B008                    	mov	al, CMOS_MONTH
  1658 00000A13 E804010000              	call	CMOS_READ
  1659 00000A18 A2[31700000]            	mov	[date_month], al
  1660 00000A1D B009                    	mov	al, CMOS_YEAR
  1661 00000A1F E8F8000000              	call	CMOS_READ
  1662 00000A24 A2[32700000]            	mov	[date_year], al
  1663 00000A29 B032                    	mov	al, CMOS_CENTURY
  1664 00000A2B E8EC000000              	call	CMOS_READ
  1665 00000A30 A2[33700000]            	mov	[date_century], al
  1666                                  	;
  1667 00000A35 B000                    	mov	al, CMOS_SECONDS
  1668 00000A37 E8E0000000              	call 	CMOS_READ
  1669 00000A3C 3A05[2C700000]          	cmp	al, [time_seconds]
  1670 00000A42 758A                    	jne	short time_of_day
  1671                                  
  1672                                  rtc_retn:
  1673 00000A44 C3                      	retn
  1674                                  
  1675                                  rtc_p:
  1676                                  	; 27/12/2021 (Retro UNIX 386 v1.2)
  1677                                  	; 07/09/2014
  1678                                  	; 29/08/2014
  1679                                  	; 27/08/2014
  1680                                  	; 25/08/2014
  1681                                   	; Print Real Time Clock content
  1682                                  	;
  1683                                  	;
  1684 00000A45 E884FFFFFF              	call	time_of_day
  1685 00000A4A 72F8                    	jc	short rtc_retn
  1686                                  	;
  1687 00000A4C 3A05[F86B0000]          	cmp	al, [ptime_seconds]
  1688 00000A52 74F0                            je      short rtc_retn ; 29/08/2014
  1689                                  	;
  1690 00000A54 A2[F86B0000]            	mov	[ptime_seconds], al
  1691                                  	;
  1692 00000A59 A0[33700000]            	mov	al, [date_century]
  1693 00000A5E E8EA000000              	call	bcd_to_ascii
  1694 00000A63 66A3[C56B0000]          	mov	[datestr+6], ax
  1695 00000A69 A0[32700000]            	mov	al, [date_year]
  1696 00000A6E E8DA000000              	call	bcd_to_ascii
  1697 00000A73 66A3[C76B0000]          	mov	[datestr+8], ax
  1698 00000A79 A0[31700000]            	mov	al, [date_month]
  1699 00000A7E E8CA000000              	call	bcd_to_ascii
  1700 00000A83 66A3[C26B0000]          	mov	[datestr+3], ax
  1701 00000A89 A0[30700000]            	mov	al, [date_day]
  1702 00000A8E E8BA000000              	call	bcd_to_ascii
  1703 00000A93 66A3[BF6B0000]          	mov	[datestr], ax
  1704                                  	;
  1705 00000A99 0FB61D[2F700000]        	movzx	ebx, byte [date_wday]
  1706 00000AA0 C0E302                  	shl 	bl, 2
  1707 00000AA3 81C3[D86B0000]          	add	ebx, daytmp
  1708 00000AA9 8B03                    	mov	eax, [ebx]
  1709 00000AAB A3[CA6B0000]            	mov	[daystr], eax
  1710                                  	;
  1711 00000AB0 A0[2E700000]            	mov	al, [time_hours]
  1712 00000AB5 E893000000              	call	bcd_to_ascii
  1713 00000ABA 66A3[CE6B0000]          	mov	[timestr], ax
  1714 00000AC0 A0[2D700000]            	mov	al, [time_minutes]
  1715 00000AC5 E883000000              	call	bcd_to_ascii
  1716 00000ACA 66A3[D16B0000]          	mov	[timestr+3], ax
  1717 00000AD0 A0[2C700000]            	mov	al, [time_seconds]
  1718 00000AD5 E873000000              	call	bcd_to_ascii
  1719 00000ADA 66A3[D46B0000]          	mov	[timestr+6], ax
  1720                                  	;		
  1721 00000AE0 BE[AD6B0000]            	mov	esi, rtc_msg ; message offset
  1722                                  	; 23/02/2015
  1723 00000AE5 52                      	push	edx
  1724 00000AE6 51                      	push	ecx
  1725                                  	; 07/09/2014
  1726                                  	;mov	bx, 2	; Video page 2
  1727                                  	; 27/12/2021
  1728 00000AE7 29DB                    	sub	ebx, ebx
  1729 00000AE9 B302                    	mov	bl, 2
  1730                                  prtmsg:
  1731 00000AEB AC                      	lodsb
  1732 00000AEC 08C0                    	or	al, al
  1733 00000AEE 740D                    	jz	short prtmsg_ok
  1734 00000AF0 56                      	push	esi
  1735                                  	;push	bx
  1736 00000AF1 53                              push	ebx ; 27/12/2021
  1737 00000AF2 B43F                    	mov	ah, 3Fh	; cyan (6) background, 
  1738                                  			; white (F) forecolor
  1739 00000AF4 E8C1080000              	call 	write_tty
  1740                                  	;pop	bx
  1741 00000AF9 5B                      	pop	ebx ; 27/12/2021
  1742 00000AFA 5E                      	pop	esi
  1743 00000AFB EBEE                    	jmp	short prtmsg
  1744                                  	;
  1745                                  	;mov	edi, 0B8000h+0A0h+0A0h ; Row 2
  1746                                  	;call	printk
  1747                                  prtmsg_ok:
  1748                                  	; 07/09/2014
  1749                                  	;xor	dx, dx		; column 0, row 0
  1750                                  	; 27/12/2021
  1751 00000AFD 31D2                    	xor	edx, edx
  1752 00000AFF E8D8090000              	call	set_cpos	; set curspor position to 0,0 
  1753                                  	; 23/02/2015
  1754 00000B04 59                      	pop	ecx
  1755 00000B05 5A                      	pop	edx
  1756 00000B06 C3                      	retn
  1757                                  
  1758                                  ; Default IRQ 7 handler against spurious IRQs (from master PIC)
  1759                                  ; 25/02/2015 (source: http://wiki.osdev.org/8259_PIC)
  1760                                  default_irq7:
  1761                                  	; 04/12/2021
  1762                                  	;push	ax
  1763 00000B07 50                      	push	eax
  1764 00000B08 B00B                    	mov	al, 0Bh  ; In-Service register
  1765 00000B0A E620                    	out	20h, al
  1766 00000B0C EB00                            jmp short $+2
  1767 00000B0E EB00                    	jmp short $+2
  1768 00000B10 E420                    	in	al, 20h
  1769 00000B12 2480                    	and 	al, 80h ; bit 7 (is it real IRQ 7 or fake?)
  1770 00000B14 7404                            jz      short irq7_iret ; Fake (spurious) IRQ, do not send EOI 
  1771 00000B16 B020                            mov     al, 20h ; EOI
  1772 00000B18 E620                    	out	20h, al 
  1773                                  irq7_iret:
  1774                                  	;pop	ax
  1775                                  	; 04/12/2021
  1776 00000B1A 58                      	pop	eax
  1777 00000B1B CF                      	iretd
  1778                                  	
  1779                                  	; 04/12/2021
  1780                                  	; 22/08/2014
  1781                                  	; IBM PC/AT BIOS source code ----- 10/06/85 (test4.asm)
  1782                                  CMOS_READ:
  1783 00000B1C 9C                      	pushf		; SAVE INTERRUPT ENABLE STATUS AND FLAGS
  1784 00000B1D D0C0                    	rol	al, 1	; MOVE NMI BIT TO LOW POSITION
  1785 00000B1F F9                      	stc		; FORCE NMI BIT ON IN CARRY FLAG
  1786 00000B20 D0D8                    	rcr	al, 1	; HIGH BIT ON TO DISABLE NMI - OLD IN CY
  1787 00000B22 FA                      	cli		; DISABLE INTERRUPTS
  1788 00000B23 E670                    	out	CMOS_PORT, al	; ADDRESS LOCATION AND DISABLE NMI
  1789 00000B25 90                      	nop		; I/O DELAY
  1790 00000B26 E471                    	in	al, CMOS_DATA	; READ THE REQUESTED CMOS LOCATION
  1791                                  	;push	ax	; SAVE (AH) REGISTER VALUE AND CMOS BYTE
  1792                                  	; 04/12/2021
  1793 00000B28 50                      	push	eax
  1794                                  	; 15/03/2015 ; IBM PC/XT Model 286 BIOS source code 
  1795                                  		     ; ----- 10/06/85 (test4.asm)
  1796 00000B29 B01E                    	mov	al, CMOS_SHUT_DOWN*2 ; GET ADDRESS OF DEFAULT LOCATION
  1797                                  	;mov	al, CMOS_REG_D*2 ; GET ADDRESS OF DEFAULT LOCATION
  1798 00000B2B D0D8                    	rcr	al, 1	; PUT ORIGINAL NMI MASK BIT INTO ADDRESS
  1799 00000B2D E670                    	out	CMOS_PORT, al	; SET DEFAULT TO READ ONLY REGISTER
  1800                                  	;pop	ax	; RESTORE (AH) AND (AL), CMOS BYTE
  1801                                  	; 04/12/2021
  1802 00000B2F 58                      	pop	eax
  1803 00000B30 9D                      	popf	
  1804 00000B31 C3                      	retn		; RETURN WITH FLAGS RESTORED
  1805                                  
  1806                                  	; 22/08/2014
  1807                                  	; IBM PC/AT BIOS source code ----- 10/06/85 (bios2.asm)
  1808                                  UPD_IPR:				; WAIT TILL UPDATE NOT IN PROGRESS
  1809 00000B32 51                      	push	ecx
  1810 00000B33 B9FFFF0000              	mov	ecx, 65535		; SET TIMEOUT LOOP COUNT (= 800)
  1811                                  		; mov cx, 800	
  1812                                  UPD_10:
  1813 00000B38 B00A                    	mov	al, CMOS_REG_A		; ADDRESS STATUS REGISTER A
  1814 00000B3A FA                      	cli				; NO TIMER INTERRUPTS DURING UPDATES
  1815 00000B3B E8DCFFFFFF              	call	CMOS_READ		; READ UPDATE IN PROCESS FLAG
  1816 00000B40 A880                    	test	al, 80h			; IF UIP BIT IS ON ( CANNOT READ TIME )
  1817 00000B42 7406                    	jz	short UPD_90		; EXIT WITH CY= 0 IF CAN READ CLOCK NOW
  1818 00000B44 FB                      	sti				; ALLOW INTERRUPTS WHILE WAITING
  1819 00000B45 E2F1                    	loop	UPD_10			; LOOP TILL READY OR TIMEOUT
  1820 00000B47 31C0                    	xor	eax, eax		; CLEAR RESULTS IF ERROR
  1821                                  		; xor ax, ax
  1822 00000B49 F9                      	stc				; SET CARRY FOR ERROR
  1823                                  UPD_90:
  1824 00000B4A 59                      	pop	ecx			; RESTORE CALLERS REGISTER
  1825 00000B4B FA                      	cli				; INTERRUPTS OFF DURING SET
  1826 00000B4C C3                      	retn				; RETURN WITH CY FLAG SET
  1827                                  
  1828                                  bcd_to_ascii:
  1829                                  	; 25/08/2014
  1830                                  	; INPUT ->
  1831                                  	;	al = Packed BCD number
  1832                                  	; OUTPUT ->
  1833                                  	;	ax  = ASCII word/number
  1834                                  	;
  1835                                  	; Erdogan Tan - 1998 (proc_hex) - TRDOS.ASM (2004-2011)
  1836                                  	;
  1837 00000B4D D410                    	db	0D4h, 10h		; Undocumented inst. AAM
  1838                                  					; AH = AL / 10h
  1839                                  					; AL = AL MOD 10h
  1840 00000B4F 660D3030                	or	ax, '00'		; Make it ASCII based
  1841                                  
  1842 00000B53 86E0                            xchg	ah, al 
  1843                                  	
  1844 00000B55 C3                      	retn	
  1845                                  	
  1846                                  
  1847                                  %include 'keyboard.s' ; 07/03/2015
  1848                              <1> ; Temporary Runix kernel v2.0 file for debug - 22/11/2021
  1849                              <1> ; (re-write kernel for test by using previous version without a major defect)
  1850                              <1> ; ****************************************************************************
  1851                              <1> ; Retro UNIX 386 v1 Kernel - KEYBOARD.INC
  1852                              <1> ; Last Modification: 05/12/2021
  1853                              <1> ;		    (Keyboard Data is in 'KYBDATA.INC')	
  1854                              <1> ;
  1855                              <1> ; ///////// KEYBOARD FUNCTIONS (PROCEDURES) ///////////////
  1856                              <1> 
  1857                              <1> ; 05/12/2021 (Retro UNIX 386 v1.2)
  1858                              <1> ; 30/06/2015
  1859                              <1> ; 11/03/2015
  1860                              <1> ; 28/02/2015
  1861                              <1> ; 25/02/2015
  1862                              <1> ; 20/02/2015
  1863                              <1> ; 18/02/2015
  1864                              <1> ; 03/12/2014
  1865                              <1> ; 07/09/2014
  1866                              <1> ; KEYBOARD INTERRUPT HANDLER
  1867                              <1> ; (kb_int - Retro UNIX 8086 v1 - U0.ASM, 30/06/2014)
  1868                              <1> 
  1869                              <1> ;getch:
  1870                              <1> ;	; 18/02/2015
  1871                              <1> ;	; This routine will be replaced with Retro UNIX 386
  1872                              <1> ;	; version of Retro UNIX 8086 getch (tty input)
  1873                              <1> ;	; routine, later... (multi tasking ability)
  1874                              <1> ;	; 28/02/2015
  1875                              <1> ;	sti	; enable interrupts
  1876                              <1> ;	;
  1877                              <1> ;	;push	esi
  1878                              <1> ;	;push	ebx
  1879                              <1> ;	;xor	ebx, ebx
  1880                              <1> ;	;mov	bl, [ptty]  ; active_page
  1881                              <1> ;	;mov	esi, ebx
  1882                              <1> ;	;shl 	si, 1
  1883                              <1> ;	;add	esi, ttychr
  1884                              <1> ;getch_1:
  1885                              <1> ;	;mov	ax, [esi]
  1886                              <1> ;	mov	ax, [ttychr] ; video page 0 (tty0)
  1887                              <1> ;	and	ax, ax
  1888                              <1> ;	jz	short getch_2
  1889                              <1> ;	mov	word [ttychr], 0
  1890                              <1> ;	;mov	word [esi], 0
  1891                              <1> ;	;pop	ebx
  1892                              <1> ;	;pop	esi
  1893                              <1> ;	retn
  1894                              <1> ;getch_2:
  1895                              <1> ;	hlt	; not proper for multi tasking!
  1896                              <1> ;		; (temporary halt for now)
  1897                              <1> ;		; 'sleep' on tty 
  1898                              <1> ;		; will (must) be located here		
  1899                              <1> ;	nop
  1900                              <1> ;	jmp	short getch_1
  1901                              <1> 
  1902                              <1> keyb_int:
  1903                              <1> 	; 30/06/2015
  1904                              <1> 	; 25/02/2015
  1905                              <1> 	; 20/02/2015
  1906                              <1> 	; 03/12/2014 (getc_int - INT 16h modifications)
  1907                              <1> 	; 07/09/2014 - Retro UNIX 386 v1
  1908                              <1> 	; 30/06/2014
  1909                              <1> 	; 10/05/2013	
  1910                              <1>       	; Retro Unix 8086 v1 feature only!
  1911                              <1> 	; 03/03/2014
  1912                              <1> 	
  1913 00000B56 1E                  <1> 	push	ds
  1914 00000B57 53                  <1> 	push	ebx
  1915 00000B58 50                  <1> 	push	eax
  1916                              <1> 	;
  1917 00000B59 66B81000            <1> 	mov	ax, KDATA
  1918 00000B5D 8ED8                <1> 	mov	ds, ax
  1919                              <1> 	;
  1920 00000B5F 9C                  <1> 	pushfd
  1921 00000B60 0E                  <1> 	push	cs
  1922 00000B61 E812020000          <1> 	call	kb_int  ; int_09h
  1923                              <1> 	;
  1924 00000B66 B411                <1> 	mov	ah, 11h	 ; 03/12/2014	
  1925                              <1> 	;call	getc
  1926 00000B68 E856000000          <1> 	call	int_16h ; 30/06/2015
  1927 00000B6D 7450                <1> 	jz	short keyb_int4
  1928                              <1> 	;
  1929 00000B6F B410                <1> 	mov	ah, 10h	 ; 03/12/2014
  1930                              <1> 	;call	getc
  1931 00000B71 E84D000000          <1> 	call	int_16h ; 30/06/2015
  1932                              <1> 	;
  1933                              <1> 	; 20/02/2015
  1934 00000B76 0FB61D[E66F0000]    <1>         movzx   ebx, byte [ptty]  ; active_page
  1935                              <1> 	;
  1936 00000B7D 20C0                <1> 	and 	al, al
  1937 00000B7F 751E                <1> 	jnz	short keyb_int1
  1938                              <1> 	;
  1939 00000B81 80FC68              <1> 	cmp	ah, 68h	; ALT + F1 key
  1940 00000B84 7219                <1> 	jb	short keyb_int1
  1941 00000B86 80FC6F              <1> 	cmp	ah, 6Fh  ; ALT + F8 key	
  1942 00000B89 7714                <1> 	ja	short keyb_int1
  1943                              <1> 	;
  1944 00000B8B 88D8                <1> 	mov	al, bl
  1945 00000B8D 0468                <1> 	add	al, 68h
  1946 00000B8F 38E0                <1> 	cmp	al, ah
  1947 00000B91 7409                <1> 	je	short keyb_int0
  1948 00000B93 88E0                <1> 	mov	al, ah
  1949 00000B95 2C68                <1> 	sub	al, 68h
  1950 00000B97 E8FB0A0000          <1> 	call	tty_sw
  1951                              <1> 	;movzx	ebx, [ptty]  ; active_page
  1952                              <1> keyb_int0: ; 30/06/2015
  1953 00000B9C 6631C0              <1> 	xor	ax, ax
  1954                              <1> keyb_int1:
  1955 00000B9F D0E3                <1> 	shl	bl, 1
  1956 00000BA1 81C3[E86F0000]      <1> 	add	ebx, ttychr
  1957                              <1> 	;
  1958 00000BA7 6609C0              <1> 	or	ax, ax
  1959 00000BAA 7406                <1> 	jz	short keyb_int2
  1960                              <1> 	;
  1961 00000BAC 66833B00            <1> 	cmp 	word [ebx], 0
  1962 00000BB0 7703                <1>         ja      short keyb_int3 
  1963                              <1> keyb_int2:
  1964 00000BB2 668903              <1>         mov	[ebx], ax  ; Save ascii code
  1965                              <1> 			   ; and scan code of the character
  1966                              <1> 			   ; for current tty (or last tty
  1967                              <1> 			   ; just before tty switch).
  1968                              <1> keyb_int3:
  1969 00000BB5 A0[E66F0000]        <1>         mov     al, [ptty]
  1970 00000BBA E8A6440000          <1> 	call	wakeup
  1971                              <1> 	;
  1972                              <1> keyb_int4:
  1973 00000BBF 58                  <1> 	pop	eax
  1974 00000BC0 5B                  <1> 	pop	ebx
  1975 00000BC1 1F                  <1> 	pop	ds
  1976 00000BC2 CF                  <1> 	iret
  1977                              <1> 
  1978                              <1> ; 18/02/2015
  1979                              <1> ; REMINDER: Only 'keyb_int' (IRQ 9) must call getc.
  1980                              <1> ; 'keyb_int' always handles 'getc' at 1st and puts the
  1981                              <1> ; scancode and ascii code of the character 
  1982                              <1> ; in the tty input (ttychr) buffer. 
  1983                              <1> ; Test procedures must call 'getch' for tty input
  1984                              <1> ; otherwise, 'getc' will not be able to return to the caller
  1985                              <1> ; due to infinite (key press) waiting loop.
  1986                              <1> ; 
  1987                              <1> ; 03/12/2014
  1988                              <1> ; 26/08/2014
  1989                              <1> ; KEYBOARD I/O
  1990                              <1> ; (INT_16h - Retro UNIX 8086 v1 - U9.ASM, 30/06/2014)
  1991                              <1> 
  1992                              <1> ;NOTE: 'k0' to 'k7' are name of OPMASK registers.
  1993                              <1> ;	(The reason of using '_k' labels!!!) (27/08/2014)    
  1994                              <1> ;NOTE: 'NOT' keyword is '~' unary operator in NASM.
  1995                              <1> ;	('NOT LC_HC' --> '~LC_HC') (bit reversing operator)
  1996                              <1> 
  1997                              <1> int_16h: ; 30/06/2015
  1998                              <1> ;getc:
  1999 00000BC3 9C                  <1> 	pushfd	; 28/08/2014
  2000 00000BC4 0E                  <1> 	push 	cs
  2001 00000BC5 E826000000          <1> 	call 	getc_int
  2002 00000BCA C3                  <1> 	retn
  2003                              <1> 
  2004                              <1> ; 05/12/2021
  2005                              <1> 
  2006                              <1> 	;-----	SHIFT STATUS
  2007                              <1> _K3E:                                   ; GET THE EXTENDED SHIFT STATUS FLAGS
  2008 00000BCB 8A25[E4690000]      <1> 	mov	ah, [KB_FLAG_1]		; GET SYSTEM SHIFT KEY STATUS
  2009 00000BD1 80E404              <1> 	and	ah, SYS_SHIFT		; MASK ALL BUT SYS KEY BIT
  2010                              <1> 	;mov	cl, 5			; SHIFT THEW SYSTEMKEY BIT OVER TO
  2011                              <1> 	;shl	ah, cl			; BIT 7 POSITION
  2012 00000BD4 C0E405              <1>         shl	ah, 5
  2013 00000BD7 A0[E4690000]        <1> 	mov	al, [KB_FLAG_1]		; GET SYSTEM SHIFT STATES BACK
  2014 00000BDC 2473                <1> 	and	al, 01110011b		; ELIMINATE SYS SHIFT, HOLD_STATE AND INS_SHIFT
  2015 00000BDE 08C4                <1> 	or	ah, al                  ; MERGE REMAINING BITS INTO AH
  2016 00000BE0 A0[E6690000]        <1> 	mov	al, [KB_FLAG_3]		; GET RIGHT CTL AND ALT
  2017 00000BE5 240C                <1> 	and	al, 00001100b		; ELIMINATE LC_E0 AND LC_E1
  2018 00000BE7 08C4                <1> 	or	ah, al			; OR THE SHIFT FLAGS TOGETHER
  2019                              <1> _K3:
  2020 00000BE9 A0[E3690000]        <1> 	mov	al, [KB_FLAG]		; GET THE SHIFT STATUS FLAGS
  2021 00000BEE EB2B                <1> 	jmp	short _KIO_EXIT		; RETURN TO CALLER
  2022                              <1> 
  2023                              <1> getc_int:
  2024                              <1> 	; 05/12/2021 (Retro UNIX 386 v1.2)
  2025                              <1> 	; 28/02/2015
  2026                              <1> 	; 03/12/2014 (derivation from pc-xt-286 bios source code -1986-, 
  2027                              <1> 	;	      instead of pc-at bios - 1985-)
  2028                              <1> 	; 28/08/2014 (_k1d)
  2029                              <1> 	; 30/06/2014
  2030                              <1> 	; 03/03/2014
  2031                              <1> 	; 28/02/2014
  2032                              <1> 	; Derived from "KEYBOARD_IO_1" procedure of IBM "pc-xt-286" 
  2033                              <1> 	; rombios source code (21/04/1986)
  2034                              <1> 	;	 'keybd.asm', INT 16H, KEYBOARD_IO
  2035                              <1> 	;
  2036                              <1> 	; KYBD --- 03/06/86  KEYBOARD BIOS
  2037                              <1> 	;
  2038                              <1> 	;--- INT 16 H -----------------------------------------------------------------
  2039                              <1> 	; KEYBOARD I/O								      :
  2040                              <1> 	;	THESE ROUTINES PROVIDE READ KEYBOARD SUPPORT			      :
  2041                              <1> 	; INPUT									      :
  2042                              <1> 	;	(AH)= 00H  READ THE NEXT ASCII CHARACTER ENTERED FROM THE KEYBOARD,   :
  2043                              <1> 	;		   RETURN THE RESULT IN (AL), SCAN CODE IN (AH).              :
  2044                              <1> 	;		   THIS IS THE COMPATIBLE READ INTERFACE, EQUIVALENT TO THE   :
  2045                              <1> 	;                  STANDARD PC OR PCAT KEYBOARD				      :	
  2046                              <1> 	;-----------------------------------------------------------------------------:
  2047                              <1> 	;	(AH)= 01H  SET THE ZERO FLAG TO INDICATE IF AN ASCII CHARACTER IS     :
  2048                              <1> 	;		   AVAILABLE TO BE READ FROM THE KEYBOARD BUFFER.	      :
  2049                              <1> 	;		   (ZF)= 1 -- NO CODE AVAILABLE			              :
  2050                              <1> 	;		   (ZF)= 0 -- CODE IS AVAILABLE  (AX)= CHARACTER              :
  2051                              <1> 	;		   IF (ZF)= 0, THE NEXT CHARACTER IN THE BUFFER TO BE READ IS :
  2052                              <1> 	;		   IN (AX), AND THE ENTRY REMAINS IN THE BUFFER.              :
  2053                              <1> 	;		   THIS WILL RETURN ONLY PC/PCAT KEYBOARD COMPATIBLE CODES    :
  2054                              <1> 	;-----------------------------------------------------------------------------:	
  2055                              <1> 	;	(AH)= 02H  RETURN THE CURRENT SHIFT STATUS IN AL REGISTER             :
  2056                              <1> 	;		   THE BIT SETTINGS FOR THIS CODE ARE INDICATED IN THE        :
  2057                              <1> 	;		   EQUATES FOR @KB_FLAG		                              :
  2058                              <1> 	;-----------------------------------------------------------------------------:	
  2059                              <1> 	;	(AH)= 03H  SET TYPAMATIC RATE AND DELAY                               :
  2060                              <1> 	;	      (AL) = 05H                                                      :
  2061                              <1> 	;	      (BL) = TYPAMATIC RATE (BITS 5 - 7 MUST BE RESET TO 0)           :
  2062                              <1> 	;		       							      :
  2063                              <1> 	;                     REGISTER     RATE      REGISTER     RATE                :
  2064                              <1> 	;                      VALUE     SELECTED     VALUE     SELECTED              :
  2065                              <1> 	;                     --------------------------------------------            :
  2066                              <1> 	;			00H        30.0        10H        7.5                 :
  2067                              <1> 	;			01H        26.7        11H        6.7                 :
  2068                              <1> 	;			02H        24.0        12H        6.0                 :
  2069                              <1> 	;			03H        21.8        13H        5.5                 :
  2070                              <1> 	;			04H        20.0        14H        5.0                 :
  2071                              <1> 	;			05H        18.5        15H        4.6                 :
  2072                              <1> 	;			06H        17.1        16H        4.3                 :
  2073                              <1> 	;			07H        16.0        17H        4.0                 :
  2074                              <1> 	;			08H        15.0        18H        3.7                 :
  2075                              <1> 	;			09H        13.3        19H        3.3                 :
  2076                              <1> 	;			0AH        12.0        1AH        3.0                 :
  2077                              <1> 	;			0BH        10.9        1BH        2.7                 :
  2078                              <1>         ;			0CH        10.0        1CH        2.5                 :
  2079                              <1> 	;			0DH         9.2        1DH        2.3                 :
  2080                              <1> 	;			0EH         8.6        1EH        2.1                 :
  2081                              <1> 	;			0FH         8.0        1FH        2.0                 :
  2082                              <1> 	;									      :
  2083                              <1> 	;	      (BH) = TYPAMATIC DELAY  (BITS 2 - 7 MUST BE RESET TO 0)         :
  2084                              <1> 	;		       							      :
  2085                              <1> 	;                     REGISTER     DELAY                                      :
  2086                              <1> 	;                      VALUE       VALUE                                      :
  2087                              <1> 	;                     ------------------                                      :
  2088                              <1> 	;			00H        250 ms                                     :
  2089                              <1> 	;			01H        500 ms                                     :
  2090                              <1> 	;			02H        750 ms                                     :
  2091                              <1> 	;			03H       1000 ms                                     :
  2092                              <1> 	;-----------------------------------------------------------------------------:
  2093                              <1> 	;	(AH)= 05H  PLACE ASCII CHARACTER/SCAN CODE COMBINATION IN KEYBOARD    :
  2094                              <1> 	;		   BUFFER AS IF STRUCK FROM KEYBOARD                          :
  2095                              <1> 	;		   ENTRY:  (CL) = ASCII CHARACTER		              :
  2096                              <1> 	;		           (CH) = SCAN CODE                                   :
  2097                              <1> 	;		   EXIT:   (AH) = 00H = SUCCESSFUL OPERATION                  :
  2098                              <1> 	;		           (AL) = 01H = UNSUCCESSFUL - BUFFER FULL            :
  2099                              <1> 	;		   FLAGS:  CARRY IF ERROR                                     :
  2100                              <1> 	;-----------------------------------------------------------------------------:		
  2101                              <1> 	;	(AH)= 10H  EXTENDED READ INTERFACE FOR THE ENHANCED KEYBOARD,         :
  2102                              <1> 	;		   OTHERWISE SAME AS FUNCTION AH=0                            :
  2103                              <1> 	;-----------------------------------------------------------------------------:
  2104                              <1> 	;	(AH)= 11H  EXTENDED ASCII STATUS FOR THE ENHANCED KEYBOARD,           :
  2105                              <1> 	;		   OTHERWISE SAME AS FUNCTION AH=1                            :
  2106                              <1> 	;-----------------------------------------------------------------------------:	
  2107                              <1> 	;	(AH)= 12H  RETURN THE EXTENDED SHIFT STATUS IN AX REGISTER            :
  2108                              <1> 	;		   AL = BITS FROM KB_FLAG, AH = BITS FOR LEFT AND RIGHT       :
  2109                              <1> 	;		   CTL AND ALT KEYS FROM KB_FLAG_1 AND KB_FLAG_3              :
  2110                              <1> 	; OUTPUT					                              :
  2111                              <1> 	;	AS NOTED ABOVE, ONLY (AX) AND FLAGS CHANGED	                      :
  2112                              <1> 	;	ALL REGISTERS RETAINED		                                      :
  2113                              <1> 	;------------------------------------------------------------------------------
  2114                              <1> 	
  2115 00000BF0 FB                  <1> 	sti				; INTERRUPTS BACK ON
  2116 00000BF1 1E                  <1> 	push	ds			; SAVE CURRENT DS
  2117 00000BF2 53                  <1> 	push	ebx			; SAVE BX TEMPORARILY
  2118                              <1> 	;push	ecx			; SAVE CX TEMPORARILY
  2119 00000BF3 66BB1000            <1>         mov     bx, KDATA 
  2120 00000BF7 8EDB                <1> 	mov	ds, bx			; PUT SEGMENT VALUE OF DATA AREA INTO DS
  2121 00000BF9 08E4                <1> 	or	ah, ah			; CHECK FOR (AH)= 00H
  2122 00000BFB 742D                <1> 	jz	short _K1		; ASCII_READ
  2123 00000BFD FECC                <1> 	dec	ah                      ; CHECK FOR (AH)= 01H
  2124 00000BFF 7446                <1>         jz      short _K2               ; ASCII_STATUS
  2125 00000C01 FECC                <1> 	dec	ah			; CHECK FOR (AH)= 02H
  2126 00000C03 74E4                <1>         jz      short _K3               ; SHIFT STATUS
  2127 00000C05 FECC                <1> 	dec	ah			; CHECK FOR (AH)= 03H	
  2128 00000C07 745B                <1>         jz      short _K300             ; SET TYPAMATIC RATE/DELAY
  2129 00000C09 80EC02              <1> 	sub	ah, 2			; CHECK FOR (AH)= 05H	
  2130 00000C0C 7479                <1>         jz      short _K500             ; KEYBOARD WRITE         
  2131                              <1> _KIO1:	
  2132 00000C0E 80EC0B              <1> 	sub	ah, 11			; AH =  10H
  2133 00000C11 740B                <1> 	jz	short _K1E		; EXTENDED ASCII READ
  2134 00000C13 FECC                <1> 	dec	ah			; CHECK FOR (AH)= 11H
  2135 00000C15 7421                <1> 	jz	short _K2E		; EXTENDED_ASCII_STATUS
  2136 00000C17 FECC                <1> 	dec	ah			; CHECK FOR (AH)= 12H
  2137 00000C19 74B0                <1> 	jz	short _K3E		; EXTENDED_SHIFT_STATUS
  2138                              <1> _KIO_EXIT:
  2139                              <1> 	;pop	ecx			; RECOVER REGISTER
  2140 00000C1B 5B                  <1> 	pop	ebx			; RECOVER REGISTER
  2141 00000C1C 1F                  <1> 	pop	ds			; RECOVER SEGMENT
  2142 00000C1D CF                  <1> 	iretd				; INVALID COMMAND, EXIT
  2143                              <1> 
  2144                              <1> 	;-----	ASCII CHARACTER
  2145                              <1> _K1E:	
  2146 00000C1E E891000000          <1> 	call	_K1S			; GET A CHARACTER FROM THE BUFFER (EXTENDED)
  2147 00000C23 E804010000          <1> 	call	_KIO_E_XLAT		; ROUTINE TO XLATE FOR EXTENDED CALLS
  2148 00000C28 EBF1                <1> 	jmp	short _KIO_EXIT         ; GIVE IT TO THE CALLER
  2149                              <1> _K1:	
  2150 00000C2A E885000000          <1> 	call	_K1S			; GET A CHARACTER FROM THE BUFFER
  2151 00000C2F E803010000          <1> 	call	_KIO_S_XLAT		; ROUTINE TO XLATE FOR STANDARD CALLS
  2152 00000C34 72F4                <1> 	jc	short _K1		; CARRY SET MEANS TROW CODE AWAY
  2153                              <1> _K1A:
  2154 00000C36 EBE3                <1> 	jmp	short _KIO_EXIT         ; RETURN TO CALLER
  2155                              <1> 
  2156                              <1> 	;-----	ASCII STATUS
  2157                              <1> _K2E:	
  2158 00000C38 E8C2000000          <1> 	call	_K2S			; TEST FOR CHARACTER IN BUFFER (EXTENDED)
  2159 00000C3D 7420                <1> 	jz	short _K2B		; RETURN IF BUFFER EMPTY
  2160 00000C3F 9C                  <1> 	pushf				; SAVE ZF FROM TEST
  2161 00000C40 E8E7000000          <1> 	call	_KIO_E_XLAT		; ROUTINE TO XLATE FOR EXTENDED CALLS
  2162 00000C45 EB17                <1> 	jmp	short _K2A	        ; GIVE IT TO THE CALLER
  2163                              <1> _K2:	
  2164 00000C47 E8B3000000          <1> 	call	_K2S			; TEST FOR CHARACTER IN BUFFER
  2165 00000C4C 7411                <1> 	jz	short _K2B		; RETURN IF BUFFER EMPTY
  2166 00000C4E 9C                  <1> 	pushf				; SAVE ZF FROM TEST
  2167 00000C4F E8E3000000          <1> 	call	_KIO_S_XLAT		; ROUTINE TO XLATE FOR STANDARD CALLS
  2168 00000C54 7308                <1> 	jnc	short _K2A	        ; CARRY CLEAR MEANS PASS VALID CODE
  2169 00000C56 9D                  <1> 	popf				; INVALID CODE FOR THIS TYPE OF CALL
  2170 00000C57 E858000000          <1> 	call	_K1S			; THROW THE CHARACTER AWAY
  2171 00000C5C EBE9                <1> 	jmp	short _K2		; GO LOOK FOR NEXT CHAR, IF ANY
  2172                              <1> _K2A:
  2173 00000C5E 9D                  <1> 	popf				; RESTORE ZF FROM TEST
  2174                              <1> _K2B:
  2175                              <1> 	;pop	ecx			; RECOVER REGISTER
  2176 00000C5F 5B                  <1> 	pop	ebx			; RECOVER REGISTER
  2177 00000C60 1F                  <1> 	pop	ds			; RECOVER SEGMENT
  2178 00000C61 CA0400              <1> 	retf	4			; THROW AWAY (e)FLAGS
  2179                              <1> 
  2180                              <1> ; 05/12/2021
  2181                              <1> ;	;-----	SHIFT STATUS
  2182                              <1> ;_K3E:                                  ; GET THE EXTENDED SHIFT STATUS FLAGS
  2183                              <1> ;	mov	ah, [KB_FLAG_1]		; GET SYSTEM SHIFT KEY STATUS
  2184                              <1> ;	and	ah, SYS_SHIFT		; MASK ALL BUT SYS KEY BIT
  2185                              <1> ;	;mov	cl, 5			; SHIFT THEW SYSTEMKEY BIT OVER TO
  2186                              <1> ;	;shl	ah, cl			; BIT 7 POSITION
  2187                              <1> ;       shl	ah, 5
  2188                              <1> ;	mov	al, [KB_FLAG_1]		; GET SYSTEM SHIFT STATES BACK
  2189                              <1> ;	and	al, 01110011b		; ELIMINATE SYS SHIFT, HOLD_STATE AND INS_SHIFT
  2190                              <1> ;	or	ah, al                  ; MERGE REMAINING BITS INTO AH
  2191                              <1> ;	mov	al, [KB_FLAG_3]		; GET RIGHT CTL AND ALT
  2192                              <1> ;	and	al, 00001100b		; ELIMINATE LC_E0 AND LC_E1
  2193                              <1> ;	or	ah, al			; OR THE SHIFT FLAGS TOGETHER
  2194                              <1> ;_K3:
  2195                              <1> ;	mov	al, [KB_FLAG]		; GET THE SHIFT STATUS FLAGS
  2196                              <1> ;	jmp	short _KIO_EXIT		; RETURN TO CALLER
  2197                              <1> 
  2198                              <1> 	;-----	SET TYPAMATIC RATE AND DELAY
  2199                              <1> _K300:
  2200 00000C64 3C05                <1> 	cmp	al, 5			; CORRECT FUNCTION CALL?
  2201 00000C66 75B3                <1> 	jne	short _KIO_EXIT		; NO, RETURN
  2202 00000C68 F6C3E0              <1>      	test	bl, 0E0h		; TEST FOR OUT-OF-RANGE RATE
  2203 00000C6B 75AE                <1> 	jnz	short _KIO_EXIT		; RETURN IF SO
  2204 00000C6D F6C7FC              <1> 	test	BH, 0FCh		; TEST FOR OUT-OF-RANGE DELAY
  2205 00000C70 75A9                <1> 	jnz	short _KIO_EXIT		; RETURN IF SO
  2206 00000C72 B0F3                <1> 	mov	al, KB_TYPA_RD		; COMMAND FOR TYPAMATIC RATE/DELAY		
  2207 00000C74 E880060000          <1> 	call	SND_DATA		; SEND TO KEYBOARD	
  2208                              <1> 	;mov	cx, 5			; SHIFT COUNT
  2209                              <1> 	;shl	bh, cl			; SHIFT DELAY OVER
  2210 00000C79 C0E705              <1> 	shl	bh, 5
  2211 00000C7C 88D8                <1> 	mov	al, bl			; PUT IN RATE
  2212 00000C7E 08F8                <1> 	or	al, bh			; AND DELAY
  2213 00000C80 E874060000          <1> 	call	SND_DATA		; SEND TO KEYBOARD	
  2214 00000C85 EB94                <1>         jmp     _KIO_EXIT               ; RETURN TO CALLER
  2215                              <1> 
  2216                              <1> 	;-----	WRITE TO KEYBOARD BUFFER
  2217                              <1> _K500:
  2218 00000C87 56                  <1> 	push	esi			; SAVE SI (esi)
  2219 00000C88 FA                  <1> 	cli				; 
  2220 00000C89 8B1D[F4690000]      <1>      	mov	ebx, [BUFFER_TAIL]	; GET THE 'IN TO' POINTER TO THE BUFFER
  2221 00000C8F 89DE                <1> 	mov	esi, ebx		; SAVE A COPY IN CASE BUFFER NOT FULL
  2222 00000C91 E8D1000000          <1> 	call	_K4			; BUMP THE POINTER TO SEE IF BUFFER IS FULL
  2223 00000C96 3B1D[F0690000]      <1> 	cmp	ebx, [BUFFER_HEAD]	; WILL THE BUFFER OVERRUN IF WE STORE THIS?
  2224 00000C9C 740D                <1> 	je	short _K502		; YES - INFORM CALLER OF ERROR		
  2225 00000C9E 66890E              <1> 	mov	[esi], cx		; NO - PUT ASCII/SCAN CODE INTO BUFFER	
  2226 00000CA1 891D[F4690000]      <1> 	mov	[BUFFER_TAIL], ebx	; ADJUST 'IN TO' POINTER TO REFLECT CHANGE
  2227 00000CA7 28C0                <1> 	sub	al, al			; TELL CALLER THAT OPERATION WAS SUCCESSFUL
  2228 00000CA9 EB02                <1> 	jmp	short _K504		; SUB INSTRUCTION ALSO RESETS CARRY FLAG
  2229                              <1> _K502:
  2230 00000CAB B001                <1> 	mov	al, 01h			; BUFFER FULL INDICATION
  2231                              <1> _K504:
  2232 00000CAD FB                  <1> 	sti				
  2233 00000CAE 5E                  <1> 	pop	esi			; RECOVER SI (esi)
  2234 00000CAF E967FFFFFF          <1>         jmp     _KIO_EXIT               ; RETURN TO CALLER WITH STATUS IN AL
  2235                              <1> 
  2236                              <1> 	;-----	READ THE KEY TO FIGURE OUT WHAT TO DO -----
  2237                              <1> _K1S:
  2238 00000CB4 FA                  <1> 	cli	; 03/12/2014
  2239 00000CB5 8B1D[F0690000]      <1>         mov     ebx, [BUFFER_HEAD] 	; GET POINTER TO HEAD OF BUFFER
  2240 00000CBB 3B1D[F4690000]      <1>         cmp     ebx, [BUFFER_TAIL] 	; TEST END OF BUFFER
  2241                              <1> 	;jne	short _K1U		; IF ANYTHING IN BUFFER SKIP INTERRUPT
  2242 00000CC1 750F                <1> 	jne	short _k1x ; 03/12/2014
  2243                              <1> 	;
  2244                              <1> 	; 03/12/2014
  2245                              <1> 	; 28/08/2014
  2246                              <1> 	; PERFORM OTHER FUNCTION ?? here !
  2247                              <1> 	;; MOV	AX, 9002h		; MOVE IN WAIT CODE & TYPE
  2248                              <1> 	;; INT 	15H			; PERFORM OTHER FUNCTION
  2249                              <1> _K1T:                                   ; ASCII READ
  2250 00000CC3 FB                  <1> 	sti				; INTERRUPTS BACK ON DURING LOOP
  2251 00000CC4 90                  <1> 	nop				; ALLOW AN INTERRUPT TO OCCUR
  2252                              <1> _K1U:	
  2253 00000CC5 FA                  <1> 	cli				; INTERRUPTS BACK OFF
  2254 00000CC6 8B1D[F0690000]      <1>         mov    	ebx, [BUFFER_HEAD] 	; GET POINTER TO HEAD OF BUFFER
  2255 00000CCC 3B1D[F4690000]      <1>         cmp     ebx, [BUFFER_TAIL] 	; TEST END OF BUFFER
  2256                              <1> _k1x:
  2257 00000CD2 53                  <1> 	push	ebx			; SAVE ADDRESS		
  2258 00000CD3 9C                  <1> 	pushf				; SAVE FLAGS
  2259 00000CD4 E8D4060000          <1> 	call	MAKE_LED		; GO GET MODE INDICATOR DATA BYTE
  2260 00000CD9 8A1D[E5690000]      <1> 	mov	bl, [KB_FLAG_2] 	; GET PREVIOUS BITS
  2261 00000CDF 30C3                <1> 	xor	bl, al			; SEE IF ANY DIFFERENT
  2262 00000CE1 80E307              <1> 	and	bl, 07h	; KB_LEDS	; ISOLATE INDICATOR BITS
  2263 00000CE4 7406                <1> 	jz	short _K1V		; IF NO CHANGE BYPASS UPDATE
  2264 00000CE6 E86E060000          <1> 	call	SND_LED1
  2265 00000CEB FA                  <1> 	cli				; DISABLE INTERRUPTS
  2266                              <1> _K1V:
  2267 00000CEC 9D                  <1> 	popf				; RESTORE FLAGS
  2268 00000CED 5B                  <1> 	pop	ebx			; RESTORE ADDRESS
  2269 00000CEE 74D3                <1>         je      short _K1T              ; LOOP UNTIL SOMETHING IN BUFFER
  2270                              <1> 	;
  2271 00000CF0 668B03              <1> 	mov	ax, [ebx] 		; GET SCAN CODE AND ASCII CODE
  2272 00000CF3 E86F000000          <1>         call    _K4                     ; MOVE POINTER TO NEXT POSITION
  2273 00000CF8 891D[F0690000]      <1>         mov     [BUFFER_HEAD], ebx      ; STORE VALUE IN VARIABLE
  2274 00000CFE C3                  <1> 	retn				; RETURN
  2275                              <1> 
  2276                              <1> 	;-----	READ THE KEY TO SEE IF ONE IS PRESENT -----
  2277                              <1> _K2S:
  2278 00000CFF FA                  <1> 	cli				; INTERRUPTS OFF
  2279 00000D00 8B1D[F0690000]      <1>         mov     ebx, [BUFFER_HEAD]      ; GET HEAD POINTER
  2280 00000D06 3B1D[F4690000]      <1>         cmp     ebx, [BUFFER_TAIL]      ; IF EQUAL (Z=1) THEN NOTHING THERE
  2281 00000D0C 668B03              <1> 	mov	ax, [ebx]
  2282 00000D0F 9C                  <1> 	pushf				; SAVE FLAGS
  2283                              <1> 	;push	ax			; SAVE CODE
  2284                              <1> 	; 05/12/2021
  2285 00000D10 50                  <1> 	push	eax
  2286 00000D11 E897060000          <1> 	call	MAKE_LED		; GO GET MODE INDICATOR DATA BYTE
  2287 00000D16 8A1D[E5690000]      <1> 	mov	bl, [KB_FLAG_2] 	; GET PREVIOUS BITS
  2288 00000D1C 30C3                <1> 	xor	bl, al			; SEE IF ANY DIFFERENT
  2289 00000D1E 80E307              <1> 	and	bl, 07h ; KB_LEDS	; ISOLATE INDICATOR BITS
  2290 00000D21 7405                <1> 	jz	short _K2T		; IF NO CHANGE BYPASS UPDATE
  2291 00000D23 E81A060000          <1> 	call	SND_LED			; GO TURN ON MODE INDICATORS
  2292                              <1> _K2T:
  2293                              <1> 	;pop	ax			; RESTORE CODE
  2294                              <1> 	; 05/12/2021
  2295 00000D28 58                  <1> 	pop	eax
  2296 00000D29 9D                  <1> 	popf				; RESTORE FLAGS
  2297 00000D2A FB                  <1> 	sti				; INTERRUPTS BACK ON
  2298 00000D2B C3                  <1> 	retn				; RETURN
  2299                              <1> 
  2300                              <1> 	;-----	ROUTINE TO TRANSLATE SCAN CODE PAIRS FOR EXTENDED CALLS -----
  2301                              <1> _KIO_E_XLAT:
  2302 00000D2C 3CF0                <1> 	cmp	al, 0F0h		; IS IT ONE OF THE FILL-INs?
  2303 00000D2E 7506                <1> 	jne	short _KIO_E_RET	; NO, PASS IT ON
  2304 00000D30 08E4                <1>         or 	ah, ah			; AH = 0 IS SPECIAL CASE
  2305 00000D32 7402                <1>         jz	short _KIO_E_RET        ; PASS THIS ON UNCHANGED
  2306 00000D34 30C0                <1> 	xor	al, al			; OTHERWISE SET AL = 0
  2307                              <1> _KIO_E_RET:				
  2308 00000D36 C3                  <1> 	retn				; GO BACK
  2309                              <1> 
  2310                              <1> 	;-----	ROUTINE TO TRANSLATE SCAN CODE PAIRS FOR STANDARD CALLS -----
  2311                              <1> _KIO_S_XLAT:
  2312 00000D37 80FCE0              <1> 	cmp	ah, 0E0h		; IS IT KEYPAD ENTER OR / ?
  2313 00000D3A 750F                <1> 	jne	short _KIO_S2		; NO, CONTINUE
  2314 00000D3C 3C0D                <1> 	cmp	al, 0Dh			; KEYPAD ENTER CODE?
  2315 00000D3E 7408                <1>         je	short _KIO_S1		; YES, MASSAGE A BIT
  2316 00000D40 3C0A                <1> 	cmp	al, 0Ah			; CTRL KEYPAD ENTER CODE?
  2317 00000D42 7404                <1>         je	short _KIO_S1		; YES, MASSAGE THE SAME
  2318 00000D44 B435                <1> 	mov	ah, 35h			; NO, MUST BE KEYPAD /
  2319                              <1> _kio_ret: ; 03/12/2014
  2320 00000D46 F8                  <1> 	clc
  2321 00000D47 C3                  <1> 	retn
  2322                              <1> 	;jmp	short _KIO_USE		; GIVE TO CALLER
  2323                              <1> _KIO_S1:				
  2324 00000D48 B41C                <1> 	mov	ah, 1Ch			; CONVERT TO COMPATIBLE OUTPUT
  2325                              <1> 	;jmp	short _KIO_USE		; GIVE TO CALLER
  2326 00000D4A C3                  <1> 	retn
  2327                              <1> _KIO_S2:		
  2328 00000D4B 80FC84              <1> 	cmp	ah, 84h			; IS IT ONE OF EXTENDED ONES?
  2329 00000D4E 7715                <1> 	ja	short _KIO_DIS		; YES, THROW AWAY AND GET ANOTHER CHAR
  2330 00000D50 3CF0                <1> 	cmp	al, 0F0h		; IS IT ONE OF THE FILL-INs?
  2331 00000D52 7506                <1>         jne	short _KIO_S3		; NO, TRY LAST TEST
  2332 00000D54 08E4                <1> 	or	ah, ah			; AH = 0 IS SPECIAL CASE
  2333 00000D56 740C                <1>         jz	short _KIO_USE		; PASS THIS ON UNCHANGED
  2334 00000D58 EB0B                <1> 	jmp	short _KIO_DIS		; THROW AWAY THE REST
  2335                              <1> _KIO_S3:
  2336 00000D5A 3CE0                <1> 	cmp	al, 0E0h		; IS IT AN EXTENSION OF A PREVIOUS ONE?
  2337                              <1> 	;jne	short _KIO_USE		; NO, MUST BE A STANDARD CODE
  2338 00000D5C 75E8                <1> 	jne	short _kio_ret
  2339 00000D5E 08E4                <1> 	or	ah, ah			; AH = 0 IS SPECIAL CASE
  2340 00000D60 7402                <1>         jz	short _KIO_USE		; JUMP IF AH = 0
  2341 00000D62 30C0                <1> 	xor	al, al			; CONVERT TO COMPATIBLE OUTPUT
  2342                              <1> 	;jmp	short _KIO_USE		; PASS IT ON TO CALLER
  2343                              <1> _KIO_USE:
  2344                              <1> 	;clc				; CLEAR CARRY TO INDICATE GOOD CODE
  2345 00000D64 C3                  <1> 	retn				; RETURN	
  2346                              <1> _KIO_DIS:
  2347 00000D65 F9                  <1> 	stc				; SET CARRY TO INDICATE DISCARD CODE
  2348 00000D66 C3                  <1> 	retn				; RETURN
  2349                              <1> 
  2350                              <1> 	;-----	INCREMENT BUFFER POINTER ROUTINE -----
  2351                              <1> _K4:    
  2352 00000D67 43                  <1> 	inc     ebx
  2353 00000D68 43                  <1> 	inc	ebx			; MOVE TO NEXT WORD IN LIST
  2354 00000D69 3B1D[EC690000]      <1>         cmp     ebx, [BUFFER_END] 	; AT END OF BUFFER?
  2355                              <1>         ;jne    short _K5               ; NO, CONTINUE
  2356 00000D6F 7206                <1> 	jb	short _K5
  2357 00000D71 8B1D[E8690000]      <1>         mov     ebx, [BUFFER_START]     ; YES, RESET TO BUFFER BEGINNING
  2358                              <1> _K5:
  2359 00000D77 C3                  <1> 	retn
  2360                              <1> 
  2361                              <1> ; 20/02/2015
  2362                              <1> ; 05/12/2014
  2363                              <1> ; 26/08/2014
  2364                              <1> ; KEYBOARD (HARDWARE) INTERRUPT -  IRQ LEVEL 1
  2365                              <1> ; (INT_09h - Retro UNIX 8086 v1 - U9.ASM, 07/03/2014)
  2366                              <1> ;
  2367                              <1> ; Derived from "KB_INT_1" procedure of IBM "pc-at" 
  2368                              <1> ; rombios source code (06/10/1985)
  2369                              <1> ; 'keybd.asm', HARDWARE INT 09h - (IRQ Level 1)
  2370                              <1> 
  2371                              <1> ;--------- 8042 COMMANDS -------------------------------------------------------
  2372                              <1> ENA_KBD		equ	0AEh	; ENABLE KEYBOARD COMMAND
  2373                              <1> DIS_KBD		equ	0ADh	; DISABLE KEYBOARD COMMAND
  2374                              <1> SHUT_CMD	equ	0FEh	; CAUSE A SHUTDOWN COMMAND
  2375                              <1> ;--------- 8042 KEYBOARD INTERFACE AND DIAGNOSTIC CONTROL REGISTERS ------------
  2376                              <1> STATUS_PORT	equ	064h	; 8042 STATUS PORT
  2377                              <1> INPT_BUF_FULL	equ	00000010b ; 1 = +INPUT BUFFER FULL
  2378                              <1> PORT_A		equ	060h	; 8042 KEYBOARD SCAN CODE/CONTROL PORT
  2379                              <1> ;---------- 8042 KEYBOARD RESPONSE ---------------------------------------------
  2380                              <1> KB_ACK		equ	0FAh	; ACKNOWLEDGE PROM TRANSMISSION
  2381                              <1> KB_RESEND	equ	0FEh	; RESEND REQUEST
  2382                              <1> KB_OVER_RUN	equ	0FFh	; OVER RUN SCAN CODE
  2383                              <1> ;---------- KEYBOARD/LED COMMANDS ----------------------------------------------
  2384                              <1> KB_ENABLE	equ	0F4h		; KEYBOARD ENABLE
  2385                              <1> LED_CMD		equ	0EDh		; LED WRITE COMMAND
  2386                              <1> KB_TYPA_RD	equ	0F3h		; TYPAMATIC RATE/DELAY COMMAND
  2387                              <1> ;---------- KEYBOARD SCAN CODES ------------------------------------------------
  2388                              <1> NUM_KEY		equ	69		; SCAN CODE FOR	 NUMBER LOCK KEY
  2389                              <1> SCROLL_KEY	equ	70		; SCAN CODE FOR	 SCROLL LOCK KEY
  2390                              <1> ALT_KEY		equ	56		; SCAN CODE FOR	 ALTERNATE SHIFT KEY
  2391                              <1> CTL_KEY		equ	29		; SCAN CODE FOR	 CONTROL KEY
  2392                              <1> CAPS_KEY	equ	58		; SCAN CODE FOR	 SHIFT LOCK KEY
  2393                              <1> DEL_KEY		equ	83		; SCAN CODE FOR	 DELETE KEY
  2394                              <1> INS_KEY		equ	82		; SCAN CODE FOR	 INSERT KEY
  2395                              <1> LEFT_KEY	equ	42		; SCAN CODE FOR	 LEFT SHIFT
  2396                              <1> RIGHT_KEY	equ	54		; SCAN CODE FOR	 RIGHT SHIFT
  2397                              <1> SYS_KEY		equ	84		; SCAN CODE FOR	 SYSTEM KEY
  2398                              <1> ;---------- ENHANCED KEYBOARD SCAN CODES ---------------------------------------
  2399                              <1> ID_1		equ	0ABh		; 1ST ID CHARACTER FOR KBX
  2400                              <1> ID_2		equ	041h		; 2ND ID CHARACTER FOR KBX
  2401                              <1> ID_2A		equ	054h		; ALTERNATE 2ND ID CHARACTER FOR KBX
  2402                              <1> F11_M		equ	87		; F11 KEY MAKE
  2403                              <1> F12_M		equ	88		; F12 KEY MAKE
  2404                              <1> MC_E0		equ	224		; GENERAL MARKER CODE
  2405                              <1> MC_E1		equ	225		; PAUSE KEY MARKER CODE
  2406                              <1> ;---------- FLAG EQUATES WITHIN @KB_FLAG----------------------------------------
  2407                              <1> RIGHT_SHIFT	equ	00000001b	; RIGHT SHIFT KEY DEPRESSED
  2408                              <1> LEFT_SHIFT	equ	00000010b	; LEFT SHIFT KEY DEPRESSED
  2409                              <1> CTL_SHIFT	equ	00000100b	; CONTROL SHIFT KEY DEPRESSED
  2410                              <1> ALT_SHIFT	equ	00001000b	; ALTERNATE SHIFT KEY DEPRESSED
  2411                              <1> SCROLL_STATE	equ	00010000b	; SCROLL LOCK STATE IS ACTIVE
  2412                              <1> NUM_STATE	equ	00100000b	; NUM LOCK STATE IS ACTIVE
  2413                              <1> CAPS_STATE	equ	01000000b	; CAPS LOCK STATE IS ACTIVE
  2414                              <1> INS_STATE	equ	10000000b	; INSERT STATE IS ACTIVE
  2415                              <1> ;---------- FLAG EQUATES WITHIN	@KB_FLAG_1 -------------------------------------
  2416                              <1> L_CTL_SHIFT	equ	00000001b	; LEFT CTL KEY DOWN
  2417                              <1> L_ALT_SHIFT	equ	00000010b	; LEFT ALT KEY DOWN
  2418                              <1> SYS_SHIFT	equ	00000100b	; SYSTEM KEY DEPRESSED AND HELD
  2419                              <1> HOLD_STATE	equ	00001000b	; SUSPEND KEY HAS BEEN TOGGLED
  2420                              <1> SCROLL_SHIFT	equ	00010000b	; SCROLL LOCK KEY IS DEPRESSED
  2421                              <1> NUM_SHIFT	equ	00100000b	; NUM LOCK KEY IS DEPRESSED
  2422                              <1> CAPS_SHIFT	equ	01000000b	; CAPS LOCK KEY IS DEPRE55ED
  2423                              <1> INS_SHIFT	equ	10000000b	; INSERT KEY IS DEPRESSED
  2424                              <1> ;---------- FLAGS EQUATES WITHIN @KB_FLAG_2 -----------------------------------
  2425                              <1> KB_LEDS		equ	00000111b	; KEYBOARD LED STATE BITS
  2426                              <1> ;		equ	00000001b	; SCROLL LOCK INDICATOR
  2427                              <1> ;		equ	00000010b	; NUM LOCK INDICATOR
  2428                              <1> ;		equ	00000100b	; CAPS LOCK INDICATOR
  2429                              <1> ;		equ	00001000b	; RESERVED (MUST BE ZERO)
  2430                              <1> KB_FA		equ	00010000b	; ACKNOWLEDGMENT RECEIVED
  2431                              <1> KB_FE		equ	00100000b	; RESEND RECEIVED FLAG
  2432                              <1> KB_PR_LED	equ	01000000b	; MODE INDICATOR UPDATE
  2433                              <1> KB_ERR		equ	10000000b	; KEYBOARD TRANSMIT ERROR FLAG
  2434                              <1> ;----------- FLAGS EQUATES WITHIN @KB_FLAG_3 -----------------------------------
  2435                              <1> LC_E1		equ	00000001b	; LAST CODE WAS THE E1 HIDDEN CODE
  2436                              <1> LC_E0		equ	00000010b	; LAST CODE WAS THE E0 HIDDEN CODE
  2437                              <1> R_CTL_SHIFT	equ	00000100b	; RIGHT CTL KEY DOWN
  2438                              <1> R_ALT_SHIFT	equ	00001000b	; RIGHT ALT KEY DOWN
  2439                              <1> GRAPH_ON	equ	00001000b	; ALT GRAPHICS KEY DOWN (WT ONLY)	
  2440                              <1> KBX		equ	00010000b	; ENHANCED KEYBOARD INSTALLED
  2441                              <1> SET_NUM_LK	equ	00100000b	; FORCE NUM LOCK IF READ ID AND KBX
  2442                              <1> LC_AB		equ	01000000b	; LAST CHARACTER WAS FIRST ID CHARACTER
  2443                              <1> RD_ID		equ	10000000b	; DOING A READ ID (MUST BE BIT0)
  2444                              <1> ;
  2445                              <1> ;----------- INTERRUPT EQUATES -------------------------------------------------
  2446                              <1> EOI		equ	020h		; END OF INTERRUPT COMMAND TO 8259
  2447                              <1> INTA00		equ	020h		; 8259 PORT
  2448                              <1> 
  2449                              <1> 
  2450                              <1> kb_int:
  2451                              <1> 
  2452                              <1> ; 05/12/2021 (Retro UNIX 386 v1.2)
  2453                              <1> ; 17/10/2015 ('ctrlbrk') 
  2454                              <1> ; 05/12/2014
  2455                              <1> ; 04/12/2014 (derivation from pc-xt-286 bios source code -1986-, 
  2456                              <1> 	;	      instead of pc-at bios - 1985-)
  2457                              <1> ; 26/08/2014
  2458                              <1> ;
  2459                              <1> ; 03/06/86  KEYBOARD BIOS
  2460                              <1> ;
  2461                              <1> ;--- HARDWARE INT 09H -- (IRQ LEVEL 1) ------------------------------------------
  2462                              <1> ;										;
  2463                              <1> ;	KEYBOARD INTERRUPT ROUTINE						;
  2464                              <1> ;										;
  2465                              <1> ;--------------------------------------------------------------------------------
  2466                              <1> 
  2467                              <1> KB_INT_1:
  2468 00000D78 FB                  <1> 	sti				; ENABLE INTERRUPTS
  2469                              <1> 	;push	ebp
  2470 00000D79 50                  <1> 	push	eax
  2471 00000D7A 53                  <1> 	push	ebx
  2472 00000D7B 51                  <1> 	push	ecx
  2473 00000D7C 52                  <1> 	push	edx
  2474 00000D7D 56                  <1> 	push	esi
  2475 00000D7E 57                  <1> 	push	edi
  2476 00000D7F 1E                  <1> 	push	ds
  2477 00000D80 06                  <1> 	push	es
  2478 00000D81 FC                  <1> 	cld				; FORWARD DIRECTION
  2479 00000D82 66B81000            <1> 	mov	ax, KDATA
  2480 00000D86 8ED8                <1> 	mov	ds, ax
  2481 00000D88 8EC0                <1> 	mov	es, ax
  2482                              <1> 	;
  2483                              <1> 	;-----	WAIT FOR KEYBOARD DISABLE COMMAND TO BE ACCEPTED
  2484 00000D8A B0AD                <1> 	mov	al, DIS_KBD		; DISABLE THE KEYBOARD COMMAND
  2485 00000D8C E856050000          <1> 	call	SHIP_IT			; EXECUTE DISABLE
  2486 00000D91 FA                  <1> 	cli				; DISABLE INTERRUPTS
  2487 00000D92 B900000100          <1> 	mov	ecx, 10000h		; SET MAXIMUM TIMEOUT
  2488                              <1> KB_INT_01:
  2489 00000D97 E464                <1> 	in	al, STATUS_PORT		; READ ADAPTER STATUS
  2490 00000D99 A802                <1> 	test	al, INPT_BUF_FULL	; CHECK INPUT BUFFER FULL STATUS BIT
  2491 00000D9B E0FA                <1> 	loopnz	KB_INT_01		; WAIT FOR COMMAND TO BE ACCEPTED
  2492                              <1> 	;
  2493                              <1> 	;-----	READ CHARACTER FROM KEYBOARD INTERFACE
  2494 00000D9D E460                <1> 	in	al, PORT_A		; READ IN THE CHARACTER
  2495                              <1> 	;
  2496                              <1> 	;-----	SYSTEM HOOK INT 15H - FUNCTION 4FH (ON HARDWARE INT LEVEL 9H) 	
  2497                              <1> 	;MOV	AH, 04FH		; SYSTEM INTERCEPT - KEY CODE FUNCTION
  2498                              <1> 	;STC				; SET CY=1 (IN CASE OF IRET)
  2499                              <1> 	;INT	15H			; CASETTE CALL (AL)=KEY SCAN CODE
  2500                              <1> 	;				; RETURNS CY=1 FOR INVALID FUNCTION
  2501                              <1> 	;JC	KB_INT_02		; CONTINUE IF CARRY FLAG SET ((AL)=CODE)
  2502                              <1> 	;JMP	K26			; EXIT IF SYSTEM HANDLES SCAN CODE
  2503                              <1> 	;				; EXT HANDLES HARDWARE EOI AND ENABLE		
  2504                              <1> 	;
  2505                              <1> 	;-----	CHECK FOR A RESEND COMMAND TO KEYBOARD
  2506                              <1> KB_INT_02:				; 	  (AL)= SCAN CODE
  2507 00000D9F FB                  <1> 	sti				; ENABLE INTERRUPTS AGAIN
  2508 00000DA0 3CFE                <1> 	cmp	al, KB_RESEND		; IS THE INPUT A RESEND
  2509 00000DA2 7411                <1>         je      short KB_INT_4          ; GO IF RESEND
  2510                              <1> 	;
  2511                              <1> 	;-----	CHECK FOR RESPONSE TO A COMMAND TO KEYBOARD
  2512 00000DA4 3CFA                <1> 	cmp	al, KB_ACK		; IS THE INPUT AN ACKNOWLEDGE
  2513 00000DA6 751A                <1>         jne     short KB_INT_2          ; GO IF NOT
  2514                              <1> 	;
  2515                              <1> 	;-----	A COMMAND TO THE KEYBOARD WAS ISSUED
  2516 00000DA8 FA                  <1> 	cli				; DISABLE INTERRUPTS
  2517 00000DA9 800D[E5690000]10    <1> 	or	byte [KB_FLAG_2], KB_FA ; INDICATE ACK RECEIVED
  2518 00000DB0 E963020000          <1>         jmp     K26                     ; RETURN IF NOT (ACK RETURNED FOR DATA)
  2519                              <1> 	;
  2520                              <1> 	;-----	RESEND THE LAST BYTE
  2521                              <1> KB_INT_4:
  2522 00000DB5 FA                  <1> 	cli				; DISABLE INTERRUPTS
  2523 00000DB6 800D[E5690000]20    <1> 	or	byte [KB_FLAG_2], KB_FE ; INDICATE RESEND RECEIVED
  2524 00000DBD E956020000          <1>         jmp     K26                     ; RETURN IF NOT ACK RETURNED FOR DATA)
  2525                              <1> 	;
  2526                              <1> ;-----	UPDATE MODE INDICATORS IF CHANGE IN STATE
  2527                              <1> KB_INT_2:
  2528                              <1> 	;push 	ax			; SAVE DATA IN
  2529                              <1> 	; 05/12/2021
  2530 00000DC2 50                  <1> 	push	eax
  2531 00000DC3 E8E5050000          <1> 	call	MAKE_LED		; GO GET MODE INDICATOR DATA BYTE
  2532 00000DC8 8A1D[E5690000]      <1> 	mov	bl, [KB_FLAG_2] 	; GET PREVIOUS BITS
  2533 00000DCE 30C3                <1> 	xor	bl, al			; SEE IF ANY DIFFERENT
  2534 00000DD0 80E307              <1> 	and	bl, KB_LEDS		; ISOLATE INDICATOR BITS
  2535 00000DD3 7405                <1> 	jz	short UP0		; IF NO CHANGE BYPASS UPDATE
  2536 00000DD5 E868050000          <1> 	call	SND_LED			; GO TURN ON MODE INDICATORS
  2537                              <1> UP0:
  2538                              <1> 	;pop	ax			; RESTORE DATA IN
  2539                              <1> 	; 05/12/2021
  2540 00000DDA 58                  <1> 	pop	eax
  2541                              <1> ;------------------------------------------------------------------------
  2542                              <1> ;	START OF KEY PROCESSING						;
  2543                              <1> ;------------------------------------------------------------------------
  2544 00000DDB 88C4                <1> 	mov	ah, al			; SAVE SCAN CODE IN AH ALSO
  2545                              <1> 	;
  2546                              <1> 	;-----	TEST FOR OVERRUN SCAN CODE FROM KEYBOARD
  2547 00000DDD 3CFF                <1> 	cmp	al, KB_OVER_RUN		; IS THIS AN OVERRUN CHAR
  2548                              <1>         ;je	K62			; BUFFER_FULL_BEEP
  2549                              <1> 	; 05/12/2021
  2550 00000DDF 7505                <1> 	jne	short K16
  2551 00000DE1 E9ED040000          <1> 	jmp	K62
  2552                              <1> K16:	
  2553 00000DE6 8A3D[E6690000]      <1> 	mov	bh, [KB_FLAG_3]		; LOAD FLAGS FOR TESTING
  2554                              <1> 	;
  2555                              <1> 	;-----	TEST TO SEE IF A READ_ID IS IN PROGRESS
  2556 00000DEC F6C7C0              <1> 	test 	bh, RD_ID+LC_AB 	; ARE WE DOING A READ ID?
  2557 00000DEF 7442                <1> 	jz	short NOT_ID		; CONTINUE IF NOT
  2558 00000DF1 7914                <1> 	jns	short TST_ID_2		; IS THE RD_ID FLAG ON?
  2559 00000DF3 3CAB                <1> 	cmp	al, ID_1		; IS THIS THE 1ST ID CHARACTER?
  2560 00000DF5 7507                <1> 	jne	short RST_RD_ID
  2561 00000DF7 800D[E6690000]40    <1> 	or	byte [KB_FLAG_3], LC_AB ; INDICATE 1ST ID WAS OK
  2562                              <1> RST_RD_ID:
  2563 00000DFE 8025[E6690000]7F    <1> 	and	byte [KB_FLAG_3], ~RD_ID ; RESET THE READ ID FLAG
  2564 00000E05 EB27                <1>         jmp    short ID_EX		; AND EXIT
  2565                              <1> 	; 05/12/2021
  2566                              <1> 	;jmp	K26
  2567                              <1> 	;
  2568                              <1> TST_ID_2:
  2569 00000E07 8025[E6690000]BF    <1> 	and	byte [KB_FLAG_3], ~LC_AB ; RESET FLAG
  2570 00000E0E 3C54                <1> 	cmp	al, ID_2A		; IS THIS THE 2ND ID CHARACTER?
  2571 00000E10 7415                <1>         je	short KX_BIT		; JUMP IF SO
  2572 00000E12 3C41                <1> 	cmp	al, ID_2		; IS THIS THE 2ND ID CHARACTER?
  2573 00000E14 7518                <1>         jne	short ID_EX		; LEAVE IF NOT
  2574                              <1> 	; 05/12/2021
  2575                              <1> 	;jne	K26
  2576                              <1> 	;
  2577                              <1> 	;-----	A READ ID SAID THAT IT WAS ENHANCED KEYBOARD
  2578 00000E16 F6C720              <1> 	test	bh, SET_NUM_LK 		; SHOULD WE SET NUM LOCK?
  2579 00000E19 740C                <1>         jz      short KX_BIT		; EXIT IF NOT
  2580 00000E1B 800D[E3690000]20    <1> 	or	byte [KB_FLAG], NUM_STATE ; FORCE NUM LOCK ON
  2581 00000E22 E81B050000          <1> 	call	SND_LED			; GO SET THE NUM LOCK INDICATOR
  2582                              <1> KX_BIT:
  2583 00000E27 800D[E6690000]10    <1> 	or	byte [KB_FLAG_3], KBX	; INDICATE ENHANCED KEYBOARD WAS FOUND
  2584 00000E2E E9E5010000          <1> ID_EX:	jmp     K26			; EXIT
  2585                              <1> 	;
  2586                              <1> NOT_ID:
  2587 00000E33 3CE0                <1> 	cmp	al, MC_E0		; IS THIS THE GENERAL MARKER CODE?
  2588 00000E35 750E                <1> 	jne	short TEST_E1
  2589 00000E37 800D[E6690000]12    <1> 	or	byte [KB_FLAG_3], LC_E0+KBX ; SET FLAG BIT, SET KBX, AND
  2590 00000E3E EB10                <1> 	jmp	short EXIT		; THROW AWAY THIS CODE
  2591                              <1> 	; 05/12/2021
  2592 00000E40 E9DA010000          <1> 	jmp	K26A	
  2593                              <1> TEST_E1:	
  2594 00000E45 3CE1                <1> 	cmp	al, MC_E1		; IS THIS THE PAUSE KEY?
  2595 00000E47 750C                <1> 	jne	short NOT_HC
  2596 00000E49 800D[E6690000]11    <1> 	or	byte [KB_FLAG_3], LC_E1+KBX ; SET FLAG BIT, SET KBX, AND
  2597 00000E50 E9CA010000          <1> EXIT:	jmp	K26A			; THROW AWAY THIS CODE
  2598                              <1> 	;
  2599                              <1> NOT_HC:
  2600 00000E55 247F                <1> 	and	al, 07Fh		; TURN OFF THE BREAK BIT
  2601 00000E57 F6C702              <1> 	test	bh, LC_E0		; LAST CODE THE E0 MARKER CODE
  2602 00000E5A 740D                <1> 	jz	short NOT_LC_E0		; JUMP IF NOT
  2603                              <1> 	;
  2604 00000E5C BF[CE680000]        <1> 	mov	edi, _K6+6		; IS THIS A SHIFT KEY?
  2605 00000E61 AE                  <1> 	scasb
  2606                              <1> 	;je	K26 ; K16B              ; YES, THROW AWAY & RESET FLAG
  2607                              <1> 	; 05/12/2021
  2608 00000E62 7458                <1> 	je	short K16B
  2609 00000E64 AE                  <1> 	scasb
  2610 00000E65 756A                <1> 	jne	short K16A		; NO, CONTINUE KEY PROCESSING
  2611 00000E67 EB53                <1> 	jmp	short K16B		; YES, THROW AWAY & RESET FLAG
  2612                              <1> 	; 05/12/2021
  2613                              <1> 	;jmp	K26
  2614                              <1> 	;
  2615                              <1> NOT_LC_E0:
  2616 00000E69 F6C701              <1> 	test	bh, LC_E1		; LAST CODE THE E1 MARKER CODE?
  2617 00000E6C 7425                <1> 	jz	short T_SYS_KEY		; JUMP IF NOT
  2618 00000E6E B904000000          <1> 	mov	ecx, 4			; LENGHT OF SEARCH
  2619 00000E73 BF[CC680000]        <1> 	mov	edi, _K6+4		; IS THIS AN ALT, CTL, OR SHIFT?
  2620 00000E78 F2AE                <1> 	repne	scasb			; CHECK IT
  2621 00000E7A 74D4                <1> 	je	short EXIT		; THROW AWAY IF SO
  2622                              <1> 	; 05/12/2021
  2623                              <1> 	;je	K26A			
  2624                              <1> 	;
  2625 00000E7C 3C45                <1> 	cmp	al, NUM_KEY		; IS IT THE PAUSE KEY?
  2626 00000E7E 753C                <1> 	jne	short K16B		; NO, THROW AWAY & RESET FLAG
  2627                              <1> 	; 05/12/2021
  2628                              <1> 	;jne	K26
  2629 00000E80 F6C480              <1> 	test	ah, 80h			; YES, IS IT THE BREAK OF THE KEY?
  2630 00000E83 7537                <1> 	jnz	short K16B		; YES, THROW THIS AWAY, TOO	
  2631                              <1> 	; 05/12/2021
  2632                              <1> 	;jnz	K26
  2633                              <1>         ; 20/02/2015 
  2634 00000E85 F605[E4690000]08    <1> 	test	byte [KB_FLAG_1],HOLD_STATE ; NO, ARE WE PAUSED ALREADY?
  2635 00000E8C 752E                <1> 	jnz	short K16B		; YES, THROW AWAY
  2636                              <1> 	; 05/12/2021
  2637                              <1> 	;jnz	K26
  2638 00000E8E E9D6020000          <1> 	jmp     K39P                    ; NO, THIS IS THE REAL PAUSE STATE
  2639                              <1> 	;
  2640                              <1> 	;-----	TEST FOR SYSTEM KEY
  2641                              <1> T_SYS_KEY:
  2642 00000E93 3C54                <1> 	cmp	al, SYS_KEY		; IS IT THE SYSTEM KEY?
  2643 00000E95 753A                <1> 	jnz	short K16A		; CONTINUE IF NOT
  2644                              <1> 	;
  2645 00000E97 F6C480              <1> 	test	ah, 80h			; CHECK IF THIS A BREAK CODE
  2646 00000E9A 7525                <1> 	jnz	short K16C		; DO NOT TOUCH SYSTEM INDICATOR IF TRUE
  2647                              <1> 	;
  2648 00000E9C F605[E4690000]04    <1> 	test	byte [KB_FLAG_1], SYS_SHIFT ; SEE IF IN SYSTEM KEY HELD DOWN 
  2649 00000EA3 7517                <1> 	jnz	short K16B		; IF YES, DO NOT PROCESS SYSTEM INDICATOR	
  2650                              <1> 	;jnz	K26			
  2651                              <1> 	;
  2652 00000EA5 800D[E4690000]04    <1> 	or	byte [KB_FLAG_1], SYS_SHIFT ; INDICATE SYSTEM KEY DEPRESSED
  2653 00000EAC B020                <1> 	mov	al, EOI			; END OF INTERRUPT COMMAND
  2654 00000EAE E620                <1> 	out	20h, al ;out INTA00, al	; SEND COMMAND TO INTERRUPT CONTROL PORT
  2655                              <1> 					; INTERRUPT-RETURN-NO-EOI
  2656 00000EB0 B0AE                <1> 	mov	al, ENA_KBD		; INSURE KEYBOARD IS ENABLED
  2657 00000EB2 E830040000          <1> 	call	SHIP_IT			; EXECUTE ENABLE
  2658                              <1> 	; !!! SYSREQ !!! function/system call (INTERRUPT) must be here !!!
  2659                              <1> 	;MOV	AL, 8500H		; FUNCTION VALUE FOR MAKE OF SYSTEM KEY
  2660                              <1> 	;STI				; MAKE SURE INTERRUPTS ENABLED
  2661                              <1> 	;INT	15H			; USER INTERRUPT	
  2662 00000EB7 E96F010000          <1>         jmp     K27A                    ; END PROCESSING
  2663                              <1> 	;
  2664 00000EBC E957010000          <1> K16B:	jmp	K26			; IGNORE SYSTEM KEY
  2665                              <1> 	;
  2666                              <1> K16C:
  2667 00000EC1 8025[E4690000]FB    <1> 	and	byte [KB_FLAG_1], ~SYS_SHIFT ; TURN OFF SHIFT KEY HELD DOWN
  2668 00000EC8 B020                <1> 	mov	al, EOI			; END OF INTERRUPT COMMAND
  2669 00000ECA E620                <1> 	out	20h, al ;out INTA00, al ; SEND COMMAND TO INTERRUPT CONTROL PORT
  2670                              <1> 					; INTERRUPT-RETURN-NO-EOI
  2671                              <1> 	;MOV	AL, ENA_KBD		; INSURE KEYBOARD IS ENABLED
  2672                              <1> 	;CALL	SHIP_IT			; EXECUTE ENABLE
  2673                              <1> 	;
  2674                              <1> 	;MOV	AX, 8501H		; FUNCTION VALUE FOR BREAK OF SYSTEM KEY
  2675                              <1> 	;STI				; MAKE SURE INTERRUPTS ENABLED
  2676                              <1> 	;INT	15H			; USER INTERRUPT
  2677                              <1> 	;JMP	K27A			; INGONRE SYSTEM KEY				
  2678                              <1> 	;
  2679 00000ECC E953010000          <1> 	jmp     K27			; IGNORE SYSTEM KEY
  2680                              <1> 	;
  2681                              <1> 	;-----	TEST FOR SHIFT KEYS
  2682                              <1> K16A:
  2683 00000ED1 8A1D[E3690000]      <1> 	mov	bl, [KB_FLAG]		; PUT STATE FLAGS IN BL
  2684 00000ED7 BF[C8680000]        <1> 	mov	edi, _K6		; SHIFT KEY TABLE offset
  2685 00000EDC B908000000          <1> 	mov	ecx, _K6L		; LENGTH
  2686 00000EE1 F2AE                <1> 	repne	scasb			; LOOK THROUGH THE TABLE FOR A MATCH
  2687 00000EE3 88E0                <1> 	mov	al, ah			; RECOVER SCAN CODE
  2688                              <1>         ;jne    K25                     ; IF NO MATCH, THEN SHIFT NOT FOUND
  2689                              <1> 	; 05/12/2021
  2690 00000EE5 7405                <1> 	je	short K17
  2691 00000EE7 E914010000          <1> 	jmp	K25
  2692                              <1> 	;
  2693                              <1> 	;------	SHIFT KEY FOUND
  2694                              <1> K17:
  2695 00000EEC 81EF[C9680000]      <1>         sub     edi, _K6+1              ; ADJUST PTR TO SCAN CODE MATCH
  2696 00000EF2 8AA7[D0680000]      <1>        	mov     ah, [edi+_K7]       	; GET MASK INTO AH
  2697 00000EF8 B102                <1> 	mov	cl, 2			; SETUP COUNT FOR FLAG SHIFTS
  2698 00000EFA A880                <1> 	test	al, 80h			; TEST FOR BREAK KEY
  2699                              <1>         ;jnz	K23                     ; JUMP OF BREAK
  2700                              <1> 	; 05/12/2021
  2701 00000EFC 7405                <1> 	jz	short K17C
  2702 00000EFE E999000000          <1> 	jmp	K23
  2703                              <1> 	;
  2704                              <1> 	;-----	SHIFT MAKE FOUND, DETERMINE SET OR TOGGLE
  2705                              <1> K17C:
  2706 00000F03 80FC10              <1> 	cmp	ah, SCROLL_SHIFT
  2707 00000F06 732C                <1> 	jae	short K18		; IF SCROLL SHIFT OR ABOVE, TOGGLE KEY
  2708                              <1> 	;
  2709                              <1> 	;-----	PLAIN SHIFT KEY, SET SHIFT ON
  2710 00000F08 0825[E3690000]      <1> 	or	[KB_FLAG], ah		; TURN ON SHIFT BIT
  2711 00000F0E A80C                <1>         test	al, CTL_SHIFT+ALT_SHIFT ; IS IT ALT OR CTRL?
  2712 00000F10 7505                <1> 	jnz	short K17D		; YES, MORE FLAGS TO SET
  2713                              <1> 	;jz	K26			; NO, INTERRUPT RETURN
  2714                              <1> 	; 05/12/2021
  2715 00000F12 E901010000          <1> 	jmp	K26
  2716                              <1> K17D:
  2717 00000F17 F6C702              <1> 	test	bh, LC_E0		; IS THIS ONE OF NEW KEYS?
  2718 00000F1A 740B                <1> 	jz 	short K17E		; NO, JUMP
  2719 00000F1C 0825[E6690000]      <1> 	or	[KB_FLAG_3], ah		; SET BITS FOR RIGHT CTRL, ALT
  2720 00000F22 E9F1000000          <1> 	jmp	K26			; INTERRUPT RETURN
  2721                              <1> K17E:
  2722 00000F27 D2EC                <1> 	shr	ah, cl			; MOVE FLAG BITS TWO POSITIONS
  2723 00000F29 0825[E4690000]      <1> 	or	[KB_FLAG_1], ah		; SET BITS FOR LEFT CTRL, ALT
  2724 00000F2F E9E4000000          <1> 	jmp	K26
  2725                              <1> 	;
  2726                              <1> 	;-----	TOGGLED SHIFT KEY, TEST FOR 1ST MAKE OR NOT
  2727                              <1> K18:					; SHIFT-TOGGLE
  2728 00000F34 F6C304              <1> 	test	bl, CTL_SHIFT 		; CHECK CTL SHIFT STATE
  2729 00000F37 7405                <1>         jz    	short K18A              ; JUMP IF NOT CTL STATE
  2730                              <1>         ;jnz	K25                     ; JUMP IF CTL STATE
  2731                              <1> 	; 05/12/2021
  2732 00000F39 E9C2000000          <1> 	jmp	K25
  2733                              <1> K18A:
  2734 00000F3E 3C52                <1> 	cmp	al, INS_KEY		; CHECK FOR INSERT KEY
  2735 00000F40 7525                <1> 	jne	short K22		; JUMP IF NOT INSERT KEY
  2736 00000F42 F6C308              <1> 	test	bl, ALT_SHIFT 		; CHECK FOR ALTERNATE SHIFT
  2737 00000F45 7405                <1>       	jz	short K18B		; JUMP IF NOT ALTERNATE SHIFT	
  2738                              <1> 	;jnz	K25                     ; JUMP IF ALTERNATE SHIFT
  2739                              <1> 	; 05/12/2021
  2740 00000F47 E9B4000000          <1> 	jmp	K25
  2741                              <1> K18B:
  2742 00000F4C F6C702              <1> 	test	bh, LC_E0 ;20/02/2015	; IS THIS NEW INSERT KEY?
  2743 00000F4F 7516                <1> 	jnz	short K22		; YES, THIS ONE'S NEVER A '0'
  2744                              <1> K19:	
  2745 00000F51 F6C320              <1> 	test	bl, NUM_STATE 		; CHECK FOR BASE STATE
  2746 00000F54 750C                <1> 	jnz	short K21		; JUMP IF NUM LOCK IS ON
  2747 00000F56 F6C303              <1> 	test	bl, LEFT_SHIFT+RIGHT_SHIFT ; TEST FOR SHIFT STATE
  2748 00000F59 740C                <1> 	jz	short K22		; JUMP IF BASE STATE
  2749                              <1> K20:					; NUMERIC ZERO, NOT INSERT KEY
  2750 00000F5B 88C4                <1> 	mov	ah, al			; PUT SCAN CODE BACK IN AH
  2751 00000F5D E99E000000          <1>         jmp	K25               	; NUMERAL '0', STNDRD. PROCESSING
  2752                              <1> K21:					; MIGHT BE NUMERIC
  2753 00000F62 F6C303              <1> 	test	bl, LEFT_SHIFT+RIGHT_SHIFT
  2754 00000F65 74F4                <1> 	jz	short K20		; IS NUMERIC, STD. PROC.
  2755                              <1> 	;
  2756                              <1> K22:					; SHIFT TOGGLE KEY HIT; PROCESS IT
  2757 00000F67 8425[E4690000]      <1> 	test	ah, [KB_FLAG_1] 	; IS KEY ALREADY DEPRESSED
  2758                              <1> 	;jnz	K26
  2759                              <1> 	; 05/12/2021
  2760 00000F6D 7405                <1> 	jz	short K22A
  2761 00000F6F E9A4000000          <1> 	jmp	K26			; JUMP IF KEY ALREADY DEPRESSED
  2762                              <1> K22A:
  2763 00000F74 0825[E4690000]      <1>         or      [KB_FLAG_1], ah 	; INDICATE THAT THE KEY IS DEPRESSED
  2764 00000F7A 3025[E3690000]      <1> 	xor	[KB_FLAG], ah		; TOGGLE THE SHIFT STATE
  2765                              <1> 	;
  2766                              <1> 	;-----	TOGGLE LED IF CAPS, NUM  OR SCROLL KEY DEPRESSED
  2767 00000F80 F6C470              <1> 	test	ah, CAPS_SHIFT+NUM_SHIFT+SCROLL_SHIFT ; SHIFT TOGGLE?
  2768 00000F83 7407                <1> 	jz	short K22B		; GO IF NOT
  2769                              <1> 	;
  2770                              <1> 	; 05/12/2021
  2771                              <1> 	;push	ax			; SAVE SCAN CODE AND SHIFT MASK
  2772 00000F85 50                  <1> 	push	eax
  2773 00000F86 E8B7030000          <1> 	call	SND_LED			; GO TURN MODE INDICATORS ON
  2774                              <1> 	;pop	ax			; RESTORE SCAN CODE
  2775 00000F8B 58                  <1> 	pop	eax
  2776                              <1> K22B:
  2777 00000F8C 3C52                <1> 	cmp	al, INS_KEY		; TEST FOR 1ST MAKE OF INSERT KEY
  2778                              <1>         ;jne	K26			; JUMP IF NOT INSERT KEY
  2779                              <1> 	; 05/12/2021
  2780 00000F8E 7405                <1> 	je	short K22C
  2781 00000F90 E983000000          <1> 	jmp	K26			; JUMP IF NOT INSERT KEY
  2782                              <1> K22C:
  2783 00000F95 88C4                <1> 	mov	ah, al		        ; SCAN CODE IN BOTH HALVES OF AX
  2784 00000F97 E999000000          <1>         jmp	K28			; FLAGS UPDATED, PROC. FOR BUFFER
  2785                              <1> 	;
  2786                              <1> 	;-----	BREAK SHIFT FOUND
  2787                              <1> K23:					; BREAK-SHIFT-FOUND
  2788 00000F9C 80FC10              <1> 	cmp	ah, SCROLL_SHIFT	; IS THIS A TOGGLE KEY
  2789 00000F9F F6D4                <1> 	not	ah			; INVERT MASK
  2790 00000FA1 7355                <1> 	jae	short K24		; YES, HANDLE BREAK TOGGLE
  2791 00000FA3 2025[E3690000]      <1> 	and	[KB_FLAG], ah		; TURN OFF SHIFT BIT
  2792 00000FA9 80FCFB              <1> 	cmp	ah, ~CTL_SHIFT		; IS THIS ALT OR CTL?
  2793 00000FAC 7730                <1> 	ja	short K23D		; NO, ALL DONE
  2794                              <1> 	;
  2795 00000FAE F6C702              <1> 	test	bh, LC_E0		; 2ND ALT OR CTL?
  2796 00000FB1 7408                <1> 	jz	short K23A		; NO, HANSLE NORMALLY
  2797 00000FB3 2025[E6690000]      <1> 	and 	[KB_FLAG_3], ah		; RESET BIT FOR RIGHT ALT OR CTL
  2798 00000FB9 EB08                <1> 	jmp	short K23B		; CONTINUE
  2799                              <1> K23A:
  2800 00000FBB D2FC                <1> 	sar	ah, cl			; MOVE THE MASK BIT TWO POSITIONS
  2801 00000FBD 2025[E4690000]      <1> 	and	[KB_FLAG_1], ah		; RESET BIT FOR LEFT ALT AND CTL
  2802                              <1> K23B:
  2803 00000FC3 88C4                <1> 	mov	ah, al			; SAVE SCAN CODE
  2804 00000FC5 A0[E6690000]        <1> 	mov	al, [KB_FLAG_3]		; GET RIGHT ALT & CTRL FLAGS
  2805 00000FCA D2E8                <1> 	shr	al, cl			; MOVE TO BITS 1 & 0
  2806 00000FCC 0A05[E4690000]      <1> 	or	al, [KB_FLAG_1]		; PUT IN LEFT ALT & CTL FLAGS
  2807 00000FD2 D2E0                <1> 	shl	al, cl			; MOVE BACK TO BITS 3 & 2
  2808 00000FD4 240C                <1> 	and	al, ALT_SHIFT+CTL_SHIFT ; FILTER OUT OTHER GARBAGE
  2809 00000FD6 0805[E3690000]      <1> 	or	[KB_FLAG], al		; PUT RESULT IN THE REAL FLAGS	
  2810 00000FDC 88E0                <1> 	mov	al, ah
  2811                              <1> K23D:
  2812 00000FDE 3CB8                <1> 	cmp	al, ALT_KEY+80h		; IS THIS ALTERNATE SHIFT RELEASE
  2813 00000FE0 7536                <1> 	jne	short K26		; INTERRUPT RETURN
  2814                              <1> 	;	
  2815                              <1> 	;-----	ALTERNATE SHIFT KEY RELEASED, GET THE VALUE INTO BUFFER
  2816 00000FE2 A0[E7690000]        <1> 	mov	al, [ALT_INPUT]
  2817 00000FE7 B400                <1> 	mov	ah, 0			; SCAN CODE OF 0
  2818 00000FE9 8825[E7690000]      <1> 	mov	[ALT_INPUT], ah 	; ZERO OUT THE FIELD
  2819 00000FEF 3C00                <1> 	cmp	al, 0			; WAS THE INPUT = 0?
  2820 00000FF1 7425                <1> 	je	short K26		; INTERRUPT_RETURN
  2821 00000FF3 E9B8020000          <1>         jmp     K61                     ; IT WASN'T, SO PUT IN BUFFER
  2822                              <1> 	;
  2823                              <1> K24:					; BREAK-TOGGLE
  2824 00000FF8 2025[E4690000]      <1> 	and	[KB_FLAG_1], ah 	; INDICATE NO LONGER DEPRESSED
  2825 00000FFE EB18                <1> 	jmp	short K26		; INTERRUPT_RETURN
  2826                              <1> 	;
  2827                              <1> 	;-----	TEST FOR HOLD STATE
  2828                              <1> 					; AL, AH = SCAN CODE
  2829                              <1> K25:					; NO-SHIFT-FOUND
  2830 00001000 3C80                <1> 	cmp	al, 80h			; TEST FOR BREAK KEY
  2831 00001002 7314                <1> 	jae	short K26		; NOTHING FOR BREAK CHARS FROM HERE ON
  2832 00001004 F605[E4690000]08    <1> 	test	byte [KB_FLAG_1], HOLD_STATE ; ARE WE IN HOLD STATE
  2833 0000100B 7428                <1> 	jz	short K28		; BRANCH AROUND TEST IF NOT
  2834 0000100D 3C45                <1> 	cmp	al, NUM_KEY
  2835 0000100F 7407                <1> 	je	short K26		; CAN'T END HOLD ON NUM_LOCK
  2836 00001011 8025[E4690000]F7    <1> 	and	byte [KB_FLAG_1], ~HOLD_STATE ; TURN OFF THE HOLD STATE BIT
  2837                              <1> 	;
  2838                              <1> K26:
  2839 00001018 8025[E6690000]FC    <1> 	and	byte [KB_FLAG_3], ~(LC_E0+LC_E1) ; RESET LAST CHAR H.C. FLAG
  2840                              <1> K26A:					; INTERRUPT-RETURN
  2841 0000101F FA                  <1> 	cli				; TURN OFF INTERRUPTS
  2842 00001020 B020                <1> 	mov	al, EOI			; END OF INTERRUPT COMMAND
  2843 00001022 E620                <1> 	out	20h, al	;out INTA00, al	; SEND COMMAND TO INTERRUPT CONTROL PORT
  2844                              <1> K27:					; INTERRUPT-RETURN-NO-EOI
  2845 00001024 B0AE                <1> 	mov	al, ENA_KBD		; INSURE KEYBOARD IS ENABLED
  2846 00001026 E8BC020000          <1> 	call	SHIP_IT			; EXECUTE ENABLE
  2847                              <1> K27A:
  2848 0000102B FA                  <1> 	cli				; DISABLE INTERRUPTS
  2849 0000102C 07                  <1> 	pop	es			; RESTORE REGISTERS
  2850 0000102D 1F                  <1> 	pop	ds
  2851 0000102E 5F                  <1> 	pop	edi
  2852 0000102F 5E                  <1> 	pop	esi
  2853 00001030 5A                  <1> 	pop	edx
  2854 00001031 59                  <1> 	pop	ecx
  2855 00001032 5B                  <1> 	pop	ebx
  2856 00001033 58                  <1> 	pop	eax
  2857                              <1> 	;pop	ebp
  2858 00001034 CF                  <1> 	iret				; RETURN
  2859                              <1> 
  2860                              <1> 	;-----	NOT IN	HOLD STATE
  2861                              <1> K28:					; NO-HOLD-STATE
  2862 00001035 3C58                <1> 	cmp	al, 88			; TEST FOR OUT-OF-RANGE SCAN CODES
  2863 00001037 77DF                <1> 	ja	short K26		; IGNORE IF OUT-OF-RANGE	
  2864                              <1> 	;
  2865 00001039 F6C308              <1> 	test	bl, ALT_SHIFT 		; ARE WE IN ALTERNATE SHIFT
  2866 0000103C 740E                <1>         jz	short K28A		; IF NOT ALTERNATE
  2867                              <1>         ; 05/12/2021
  2868                              <1> 	;jz      K38
  2869                              <1> 	;
  2870 0000103E F6C710              <1> 	test	bh, KBX			; IS THIS THE ENCHANCED KEYBOARD?
  2871 00001041 740E                <1> 	jz	short K29		; NO, ALT STATE IS REAL
  2872                              <1> 	; 28/02/2015
  2873 00001043 F605[E4690000]04    <1> 	test	byte [KB_FLAG_1], SYS_SHIFT ; YES, IS SYSREQ KEY DOWN?
  2874 0000104A 7405                <1> 	jz	short K29		; NO, ALT STATE IS REAL
  2875                              <1> 	; 05/12/2021
  2876                              <1> 	;jnz	K38			; YES, THIS IS PHONY ALT STATE 
  2877                              <1>         ;				; DUE TO PRESSING SYSREQ	
  2878 0000104C E9D1000000          <1> K28A:	jmp	K38
  2879                              <1> 	;
  2880                              <1> 	;-----	TEST FOR RESET KEY SEQUENCE (CTL ALT DEL)
  2881                              <1> K29:					; TEST-RESET
  2882 00001051 F6C304              <1> 	test	bl, CTL_SHIFT 		; ARE WE IN CONTROL SHIFT ALSO?
  2883 00001054 740B                <1> 	jz	short K31		; NO_RESET
  2884 00001056 3C53                <1> 	cmp	al, DEL_KEY		; CTL-ALT STATE, TEST FOR DELETE KEY
  2885 00001058 7507                <1> 	jne	short K31		; NO_RESET, IGNORE
  2886                              <1> 	;
  2887                              <1> 	;-----	CTL-ALT-DEL HAS BEEN FOUND
  2888                              <1>  	; 26/08/2014
  2889                              <1> cpu_reset:
  2890                              <1> 	; IBM PC/AT ROM BIOS source code - 10/06/85 (TEST4.ASM - PROC_SHUTDOWN)
  2891                              <1> 	; Send FEh (system reset command) to the keyboard controller.
  2892 0000105A B0FE                <1> 	mov	al, SHUT_CMD		; SHUTDOWN COMMAND
  2893 0000105C E664                <1> 	out	STATUS_PORT, al		; SEND TO KEYBOARD CONTROL PORT
  2894                              <1> khere:
  2895 0000105E F4                  <1> 	hlt				; WAIT FOR 80286 RESET
  2896 0000105F EBFD                <1> 	jmp 	short khere		; INSURE HALT
  2897                              <1> 
  2898                              <1> 	;
  2899                              <1> 	;-----	IN ALTERNATE SHIFT, RESET NOT FOUND
  2900                              <1> K31:					; NO-RESET
  2901 00001061 3C39                <1> 	cmp	al, 57			; TEST FOR SPACE KEY
  2902 00001063 7507                <1> 	jne	short K311		; NOT THERE
  2903 00001065 B020                <1> 	mov	al, ' '			; SET SPACE CHAR
  2904 00001067 E936020000          <1>         jmp     K57                     ; BUFFER_FILL
  2905                              <1> K311:
  2906 0000106C 3C0F                <1> 	cmp	al, 15			; TEST FOR TAB KEY
  2907 0000106E 7509                <1> 	jne	short K312		; NOT THERE
  2908 00001070 66B800A5            <1> 	mov	ax, 0A500h		; SET SPECIAL CODE FOR ALT-TAB
  2909 00001074 E929020000          <1>         jmp     K57                     ; BUFFER_FILL
  2910                              <1> K312:
  2911 00001079 3C4A                <1> 	cmp	al, 74			; TEST FOR KEY PAD -
  2912 0000107B 7471                <1>         je	short K37B              ; GO PROCESS
  2913 0000107D 3C4E                <1> 	cmp	al, 78			; TEST FOR KEY PAD +
  2914 0000107F 746D                <1>         je	short K37B              ; GO PROCESS
  2915                              <1> 	;
  2916                              <1> 	;-----	LOOK FOR KEY PAD ENTRY
  2917                              <1> K32:					; ALT-KEY-PAD
  2918 00001081 BF[A4680000]        <1> 	mov	edi, K30		; ALT-INPUT-TABLE offset
  2919 00001086 B90A000000          <1> 	mov	ecx, 10			; LOOK FOR ENTRY USING KEYPAD
  2920 0000108B F2AE                <1> 	repne	scasb			; LOOK FOR MATCH
  2921 0000108D 7523                <1> 	jne	short K33		; NO_ALT_KEYPAD
  2922 0000108F F6C702              <1> 	test	bh, LC_E0		; IS THIS ONE OF THE NEW KEYS?
  2923                              <1>         ;jnz	short K37C		; YES, JUMP, NOT NUMPAD KEY
  2924                              <1> 	; 05/12/2021
  2925 00001092 751C                <1> 	jnz	short K32B
  2926 00001094 81EF[A5680000]      <1> 	sub	edi, K30+1		; DI NOW HAS ENTRY VALUE
  2927 0000109A A0[E7690000]        <1> 	mov	al, [ALT_INPUT] 	; GET THE CURRENT BYTE
  2928 0000109F B40A                <1> 	mov	ah, 10			; MULTIPLY BY 10
  2929 000010A1 F6E4                <1> 	mul	ah
  2930 000010A3 6601F8              <1> 	add	ax, di			; ADD IN THE LATEST ENTRY
  2931 000010A6 A2[E7690000]        <1> 	mov	[ALT_INPUT], al 	; STORE IT AWAY
  2932                              <1> K32A:
  2933 000010AB E968FFFFFF          <1>         jmp     K26                     ; THROW AWAY THAT KEYSTROKE
  2934                              <1> K32B:
  2935                              <1> 	; 05/12/2021
  2936 000010B0 EB6A                <1> 	jmp	K37C
  2937                              <1> 	;
  2938                              <1> 	;-----	LOOK FOR SUPERSHIFT ENTRY
  2939                              <1> K33:					; NO-ALT-KEYPAD
  2940 000010B2 C605[E7690000]00    <1>         mov     byte [ALT_INPUT], 0     ; ZERO ANY PREVIOUS ENTRY INTO INPUT
  2941 000010B9 B91A000000          <1> 	mov	ecx, 26			; (DI),(ES) ALREADY POINTING
  2942 000010BE F2AE                <1> 	repne	scasb			; LOOK FOR MATCH IN ALPHABET
  2943 000010C0 7453                <1> 	je	short K37A		; MATCH FOUND, GO FILLL THE BUFFER
  2944                              <1> 	;
  2945                              <1> 	;-----	LOOK FOR TOP ROW OF ALTERNATE SHIFT
  2946                              <1> K34:					; ALT-TOP-ROW
  2947 000010C2 3C02                <1> 	cmp	al, 2			; KEY WITH '1' ON IT
  2948 000010C4 7228                <1> 	jb	short K37B		; MUST BE ESCAPE
  2949 000010C6 3C0D                <1> 	cmp	al, 13			; IS IT IN THE REGION
  2950 000010C8 7705                <1> 	ja	short K35		; NO, ALT SOMETHING ELSE
  2951 000010CA 80C476              <1> 	add	ah, 118			; CONVERT PSEUDO SCAN CODE TO RANGE
  2952 000010CD EB46                <1> 	jmp	short K37A		; GO FILL THE BUFFER
  2953                              <1> 	;
  2954                              <1> 	;-----	TRANSLATE ALTERNATE SHIFT PSEUDO SCAN CODES
  2955                              <1> K35:					; ALT-FUNCTION
  2956 000010CF 3C57                <1> 	cmp	al, F11_M		; IS IT F11?	
  2957 000010D1 7209                <1> 	jb	short K35A ; 20/02/2015	; NO, BRANCH
  2958 000010D3 3C58                <1> 	cmp	al, F12_M		; IS IT F12?
  2959 000010D5 7705                <1> 	ja	short K35A ; 20/02/2015	; NO, BRANCH
  2960 000010D7 80C434              <1> 	add	ah, 52			; CONVERT TO PSEUDO SCAN CODE
  2961 000010DA EB39                <1> 	jmp	short K37A		; GO FILL THE BUFFER
  2962                              <1> K35A:
  2963 000010DC F6C702              <1> 	test	bh, LC_E0		; DO WE HAVE ONE OF THE NEW KEYS?
  2964 000010DF 7425                <1> 	jz	short K37		; NO, JUMP
  2965 000010E1 3C1C                <1> 	cmp	al, 28			; TEST FOR KEYPAD ENTER
  2966 000010E3 7510                <1>         jne     short K35B              ; NOT THERE
  2967 000010E5 66B800A6            <1> 	mov	ax, 0A600h		; SPECIAL CODE
  2968 000010E9 E9B4010000          <1> 	jmp	K57			; BUFFER FILL
  2969                              <1> K37B:
  2970 000010EE B0F0                <1> 	mov	al, 0F0h		; USE SPECIAL ASCII CODE
  2971 000010F0 E9AD010000          <1> 	jmp     K57                     ; PUT IT IN THE BUFFER
  2972                              <1> K35B:
  2973 000010F5 3C53                <1> 	cmp	al, 83			; TEST FOR DELETE KEY
  2974 000010F7 7423                <1> 	je	short K37C		; HANDLE WITH OTHER EDIT KEYS
  2975 000010F9 3C35                <1> 	cmp	al, 53			; TEST FOR KEYPAD /
  2976 000010FB 75AE                <1> 	jne	short K32A		; NOT THERE, NO OTHER E0 SPECIALS	
  2977                              <1>         ; 05/12/2021
  2978                              <1> 	;jne	K26
  2979 000010FD 66B800A4            <1> 	mov	ax, 0A400h		; SPECIAL CODE
  2980 00001101 E99C010000          <1> 	jmp	K57			; BUFFER FILL
  2981                              <1> K37:
  2982 00001106 3C3B                <1> 	cmp	al, 59			; TEST FOR FUNCTION KEYS (F1)
  2983 00001108 72E4                <1>         jb      short K37B		; NO FN, HANDLE W/OTHER EXTENDED
  2984 0000110A 3C44                <1> 	cmp	al, 68			; IN KEYPAD REGION?
  2985                              <1>         ;ja	short K32A		; IF SO, IGNORE
  2986 0000110C 0F8706FFFFFF        <1>         ja      K26
  2987 00001112 80C42D              <1> 	add	ah, 45			; CONVERT TO PSEUDO SCAN CODE
  2988                              <1> K37A:
  2989 00001115 B000                <1> 	mov	al, 0			; ASCII CODE OF ZERO
  2990 00001117 E986010000          <1>         jmp     K57                     ; PUT IT IN THE BUFFER
  2991                              <1> K37C:
  2992 0000111C 0450                <1> 	add	al, 80			; CONVERT SCAN CODE (EDIT KEYS)
  2993 0000111E 88C4                <1> 	mov	ah, al			; (SCAN CODE NOT IN AH FOR INSERT)
  2994 00001120 EBF3                <1> 	jmp     short K37A              ; PUT IT IN THE BUFFER
  2995                              <1> 	;
  2996                              <1> 	;-----	NOT IN ALTERNATE SHIFT
  2997                              <1> K38:					; NOT-ALT-SHIFT
  2998                              <1> 					; BL STILL HAS SHIFT FLAGS
  2999 00001122 F6C304              <1> 	test	bl, CTL_SHIFT 		; ARE WE IN CONTROL SHIFT?
  3000 00001125 7505                <1> 	jnz	short K38A		; YES, START PROCESSING	
  3001                              <1>         ;jz	K44                     ; NOT-CTL-SHIFT
  3002                              <1> 	; 05/12/2021
  3003 00001127 E9AB000000          <1> 	jmp	K44
  3004                              <1> 	;
  3005                              <1> 	;-----	CONTROL SHIFT, TEST SPECIAL CHARACTERS
  3006                              <1> 	;-----	TEST FOR BREAK
  3007                              <1> K38A:
  3008 0000112C 3C46                <1> 	cmp	al, SCROLL_KEY		; TEST FOR BREAK
  3009 0000112E 7530                <1> 	jne	short K39		; JUMP, NO-BREAK
  3010 00001130 F6C710              <1> 	test	bh, KBX			; IS THIS THE ENHANCED KEYBOARD?
  3011 00001133 7405                <1> 	jz	short K38B		; NO, BREAK IS VALID	
  3012 00001135 F6C702              <1> 	test	bh, LC_E0		; YES, WAS LAST CODE AN E0?
  3013 00001138 7426                <1> 	jz	short K39		; NO-BREAK, TEST FOR PAUSE	
  3014                              <1> K38B:
  3015 0000113A 8B1D[F0690000]      <1> 	mov	ebx, [BUFFER_HEAD] 	; RESET BUFFER TO EMPTY
  3016 00001140 891D[F4690000]      <1> 	mov	[BUFFER_TAIL], ebx
  3017 00001146 C605[E2690000]80    <1> 	mov	byte [BIOS_BREAK], 80h  ; TURN ON BIOS_BREAK BIT
  3018                              <1> 	;
  3019                              <1> 	;-----	ENABLE KEYBOARD
  3020 0000114D B0AE                <1> 	mov	al, ENA_KBD		; ENABLE KEYBOARD
  3021 0000114F E893010000          <1> 	call	SHIP_IT			; EXECUTE ENABLE
  3022                              <1> 	;
  3023                              <1> 	; CTRL+BREAK code here !!!
  3024                              <1> 	;INT	1BH			; BREAK INTERRUPT VECTOR
  3025                              <1> 	; 17/10/2015	
  3026 00001154 E823230000          <1> 	call	ctrlbrk ; control+break subroutine
  3027                              <1> 	;
  3028                              <1> 	;sub	ax, ax			; PUT OUT DUMMY CHARACTER
  3029                              <1> 	; 05/12/2021
  3030 00001159 29C0                <1> 	sub	eax, eax
  3031 0000115B E942010000          <1> 	jmp     K57                     ; BUFFER_FILL
  3032                              <1> 	;
  3033                              <1> 	;-----	TEST FOR PAUSE
  3034                              <1> K39:					; NO_BREAK
  3035 00001160 F6C710              <1> 	test	bh, KBX			; IS THIS THE ENHANCED KEYBOARD?
  3036 00001163 7537                <1> 	jnz	short K41		; YES, THEN THIS CAN'T BE PAUSE	
  3037 00001165 3C45                <1> 	cmp	al, NUM_KEY		; LOOK FOR PAUSE KEY
  3038 00001167 7533                <1> 	jne	short K41		; NO-PAUSE
  3039                              <1> K39P:
  3040 00001169 800D[E4690000]08    <1> 	or	byte [KB_FLAG_1], HOLD_STATE ; TURN ON THE HOLD FLAG
  3041                              <1> 	;
  3042                              <1> 	;-----	ENABLE KEYBOARD
  3043 00001170 B0AE                <1> 	mov	al, ENA_KBD		; ENABLE KEYBOARD
  3044 00001172 E870010000          <1> 	call	SHIP_IT			; EXECUTE ENABLE
  3045                              <1> K39A:
  3046 00001177 B020                <1> 	mov	al, EOI			; END OF INTERRUPT TO CONTROL PORT
  3047 00001179 E620                <1> 	out	20h, al ;out INTA00, al	; ALLOW FURTHER KEYSTROKE INTERRUPTS
  3048                              <1> 	;
  3049                              <1> 	;-----	DURING PAUSE INTERVAL, TURN COLOR CRT BACK ON
  3050 0000117B 803D[E0690000]07    <1>         cmp     byte [CRT_MODE], 7      ; IS THIS BLACK AND WHITE CARD
  3051 00001182 740A                <1>         je      short K40              	; YES, NOTHING TO DO
  3052 00001184 66BAD803            <1> 	mov	dx, 03D8h		; PORT FOR COLOR CARD
  3053 00001188 A0[E1690000]        <1>         mov     al, [CRT_MODE_SET] 	; GET THE VALUE OF THE CURRENT MODE
  3054 0000118D EE                  <1> 	out	dx, al			; SET THE CRT MODE, SO THAT CRT IS ON
  3055                              <1> 	;
  3056                              <1> K40:					; PAUSE-LOOP
  3057 0000118E F605[E4690000]08    <1>         test    byte [KB_FLAG_1], HOLD_STATE ; CHECK HOLD STATE FLAG
  3058 00001195 75F7                <1> 	jnz	short K40		; LOOP UNTIL FLAG TURNED OFF
  3059                              <1> 	;
  3060 00001197 E988FEFFFF          <1>         jmp     K27                     ; INTERRUPT_RETURN_NO_EOI
  3061                              <1>         ;
  3062                              <1> 	;-----	TEST SPECIAL CASE KEY 55
  3063                              <1> K41:					; NO-PAUSE
  3064 0000119C 3C37                <1> 	cmp	al, 55			; TEST FOR */PRTSC KEY
  3065 0000119E 7513                <1> 	jne	short K42		; NOT-KEY-55
  3066 000011A0 F6C710              <1> 	test	bh, KBX			; IS THIS THE ENHANCED KEYBOARD?
  3067 000011A3 7405                <1> 	jz	short K41A		; NO, CTL-PRTSC IS VALID	
  3068 000011A5 F6C702              <1> 	test	bh, LC_E0		; YES, WAS LAST CODE AN E0?
  3069 000011A8 7421                <1> 	jz	short K42B		; NO, TRANSLATE TO A FUNCTION
  3070                              <1> K41A:	
  3071 000011AA 66B80072            <1> 	mov	ax, 114*256		; START/STOP PRINTING SWITCH
  3072 000011AE E9EF000000          <1>         jmp     K57                     ; BUFFER_FILL
  3073                              <1> 	;
  3074                              <1> 	;-----	SET UP TO TRANSLATE CONTROL SHIFT
  3075                              <1> K42:					; NOT-KEY-55
  3076 000011B3 3C0F                <1> 	cmp	al, 15			; IS IT THE TAB KEY?
  3077 000011B5 7414                <1> 	je	short K42B		; YES, XLATE TO FUNCTION CODE
  3078 000011B7 3C35                <1> 	cmp	al, 53			; IS IT THE / KEY?
  3079 000011B9 750E                <1> 	jne	short K42A		; NO, NO MORE SPECIAL CASES	
  3080 000011BB F6C702              <1> 	test	bh, LC_E0		; YES, IS IT FROM THE KEY PAD?
  3081 000011BE 7409                <1> 	jz	short K42A		; NO, JUST TRANSLATE
  3082 000011C0 66B80095            <1> 	mov	ax, 9500h		; YES, SPECIAL CODE FOR THIS ONE
  3083 000011C4 E9D9000000          <1> 	jmp	K57			; BUFFER FILL	
  3084                              <1> K42A:
  3085                              <1> 	;mov	ebx, _K8		; SET UP TO TRANSLATE CTL
  3086 000011C9 3C3B                <1> 	cmp	al, 59			; IS IT IN CHARACTER TABLE?
  3087                              <1>         ;jb	short K45F              ; YES, GO TRANSLATE CHAR
  3088                              <1> 	;;jb	K56 ; 20/02/2015
  3089                              <1> 	;;jmp	K64 ; 20/02/2015
  3090                              <1> K42B:
  3091 000011CB BB[D8680000]        <1> 	mov	ebx, _K8		; SET UP TO TRANSLATE CTL
  3092                              <1> 	;;jmp	K64
  3093                              <1> 	;jb	K56 ;; 20/02/2015	
  3094                              <1> 	; 05/12/2021
  3095 000011D0 7267                <1> 	jb	short K45F
  3096 000011D2 E9B9000000          <1> 	jmp	K64	
  3097                              <1>         ;
  3098                              <1> 	;-----	NOT IN CONTROL SHIFT
  3099                              <1> K44:					; NOT-CTL-SHIFT
  3100 000011D7 3C37                <1> 	cmp	al, 55			; PRINT SCREEN KEY?
  3101 000011D9 7528                <1> 	jne	short K45		; NOT PRINT SCREEN
  3102 000011DB F6C710              <1> 	test	bh, KBX			; IS THIS ENHANCED KEYBOARD?
  3103 000011DE 7407                <1> 	jz	short K44A		; NO, TEST FOR SHIFT STATE	
  3104 000011E0 F6C702              <1> 	test	bh, LC_E0		; YES, LAST CODE A MARKER?
  3105 000011E3 7507                <1> 	jnz	short K44B		; YES, IS PRINT SCREEN
  3106 000011E5 EB41                <1> 	jmp	short K45C		; NO, TRANSLATE TO '*' CHARACTER
  3107                              <1> K44A:
  3108 000011E7 F6C303              <1> 	test	bl, LEFT_SHIFT+RIGHT_SHIFT ; NOT 101 KBD, SHIFT KEY DOWN?
  3109 000011EA 743C                <1> 	jz	short K45C		; NO, TRANSLATE TO '*' CHARACTER
  3110                              <1> 	;
  3111                              <1> 	;-----	ISSUE INTERRUPT TO INDICATE PRINT SCREEN FUNCTION
  3112                              <1> K44B:
  3113 000011EC B0AE                <1> 	mov	al, ENA_KBD		; INSURE KEYBOARD IS ENABLED
  3114 000011EE E8F4000000          <1> 	call	SHIP_IT			; EXECUTE ENABLE
  3115 000011F3 B020                <1> 	mov	al, EOI			; END OF CURRENT INTERRUPT
  3116 000011F5 E620                <1> 	out	20h, al ;out INTA00, al	; SO FURTHER THINGS CAN HAPPEN
  3117                              <1> 	; Print Screen !!!		; ISSUE PRINT SCREEN INTERRUPT (INT 05h)
  3118                              <1> 	;PUSH 	BP			; SAVE POINTER
  3119                              <1> 	;INT 	5H			; ISSUE PRINT SCREEN INTERRUPT
  3120                              <1> 	;POP	BP			; RESTORE POINTER
  3121 000011F7 8025[E6690000]FC    <1>         and     byte [KB_FLAG_3], ~(LC_E0+LC_E1) ; ZERO OUT THESE FLAGS
  3122 000011FE E921FEFFFF          <1>         jmp     K27                     ; GO BACK WITHOUT EOI OCCURRING
  3123                              <1> 	;
  3124                              <1> 	;-----	HANDLE IN-CORE KEYS
  3125                              <1> K45:					; NOT-PRINT-SCREEN
  3126 00001203 3C3A                <1> 	cmp	al, 58			; TEST FOR IN-CORE AREA
  3127 00001205 7734                <1> 	ja	short K46		; JUMP IF NOT
  3128 00001207 3C35                <1> 	cmp	al, 53			; IS THIS THE '/' KEY?
  3129 00001209 7505                <1> 	jne	short K45A		; NO, JUMP
  3130 0000120B F6C702              <1> 	test	bh, LC_E0		; WAS THE LAST CODE THE MARKER?
  3131 0000120E 7518                <1> 	jnz	short K45C		; YES, TRANSLATE TO CHARACTER
  3132                              <1> K45A:
  3133 00001210 B91A000000          <1> 	mov	ecx, 26			; LENGHT OF SEARCH
  3134 00001215 BF[AE680000]        <1> 	mov	edi, K30+10		; POINT TO TABLE OF A-Z CHARS
  3135 0000121A F2AE                <1> 	repne	scasb			; IS THIS A LETTER KEY?
  3136                              <1> 		; 20/02/2015
  3137 0000121C 7505                <1> 	jne	short K45B              ; NO, SYMBOL KEY
  3138                              <1> 	;
  3139 0000121E F6C340              <1> 	test	bl, CAPS_STATE		; ARE WE IN CAPS_LOCK?
  3140 00001221 750C                <1> 	jnz	short K45D		; TEST FOR SURE
  3141                              <1> K45B:
  3142 00001223 F6C303              <1> 	test	bl, LEFT_SHIFT+RIGHT_SHIFT ; ARE WE IN SHIFT STATE?
  3143 00001226 750C                <1> 	jnz	short K45E		; YES, UPPERCASE
  3144                              <1> 					; NO, LOWERCASE
  3145                              <1> K45C:
  3146 00001228 BB[30690000]        <1> 	mov	ebx, K10		; TRANSLATE TO LOWERCASE LETTERS
  3147 0000122D EB51                <1> 	jmp	short K56	
  3148                              <1> K45D:					; ALMOST-CAPS-STATE
  3149 0000122F F6C303              <1> 	test	bl, LEFT_SHIFT+RIGHT_SHIFT ; CL ON. IS SHIFT ON, TOO?
  3150 00001232 75F4                <1> 	jnz	short K45C		; SHIFTED TEMP OUT OF CAPS STATE
  3151                              <1> K45E:
  3152 00001234 BB[88690000]        <1> 	mov	ebx, K11		; TRANSLATE TO UPPER CASE LETTERS
  3153 00001239 EB45                <1> K45F:	jmp	short K56
  3154                              <1> 	;
  3155                              <1> 	;-----	TEST FOR KEYS F1 - F10
  3156                              <1> K46:					; NOT IN-CORE AREA
  3157 0000123B 3C44                <1> 	cmp	al, 68			; TEST FOR F1 - F10
  3158                              <1> 	;ja	short K47		; JUMP IF NOT
  3159                              <1> 	;jmp	short K53		; YES, GO DO FN KEY PROCESS			
  3160 0000123D 7635                <1> 	jna	short K53		
  3161                              <1> 	;
  3162                              <1> 	;-----	HANDLE THE NUMERIC PAD KEYS
  3163                              <1> K47:					; NOT F1 - F10
  3164 0000123F 3C53                <1> 	cmp	al, 83			; TEST NUMPAD KEYS
  3165 00001241 772D                <1> 	ja	short K52		; JUMP IF NOT
  3166                              <1> 	;
  3167                              <1> 	;-----	KEYPAD KEYS, MUST TEST NUM LOCK FOR DETERMINATION
  3168                              <1> K48:
  3169 00001243 3C4A                <1> 	cmp	al , 74			; SPECIAL CASE FOR MINUS
  3170 00001245 74ED                <1> 	je	short K45E		; GO TRANSLATE
  3171 00001247 3C4E                <1> 	cmp	al , 78			; SPECIAL CASE FOR PLUS
  3172 00001249 74E9                <1> 	je	short K45E		; GO TRANSLATE
  3173 0000124B F6C702              <1> 	test	bh, LC_E0		; IS THIS ONE OFTHE NEW KEYS?
  3174 0000124E 750A                <1> 	jnz	short K49		; YES, TRANSLATE TO BASE STATE
  3175                              <1> 	;		
  3176 00001250 F6C320              <1> 	test 	bl, NUM_STATE		; ARE WE IN NUM LOCK
  3177 00001253 7514                <1> 	jnz	short K50		; TEST FOR SURE
  3178 00001255 F6C303              <1> 	test	bl, LEFT_SHIFT+RIGHT_SHIFT ; ARE WE IN SHIFT STATE?
  3179                              <1> 	;jnz	short K51		; IF SHIFTED, REALLY NUM STATE
  3180 00001258 75DA                <1> 	jnz	short K45E
  3181                              <1> 	;
  3182                              <1> 	;-----	BASE CASE FOR KEYPAD
  3183                              <1> K49:					
  3184 0000125A 3C4C                <1> 	cmp	al, 76			; SPECIAL CASE FOR BASE STATE 5
  3185 0000125C 7504                <1> 	jne	short K49A		; CONTINUE IF NOT KEYPAD 5
  3186 0000125E B0F0                <1> 	mov	al, 0F0h		; SPECIAL ASCII CODE	
  3187 00001260 EB40                <1> 	jmp	short K57		; BUFFER FILL
  3188                              <1> K49A:
  3189 00001262 BB[30690000]        <1> 	mov	ebx, K10		; BASE CASE TABLE	
  3190 00001267 EB27                <1> 	jmp	short K64		; CONVERT TO PSEUDO SCAN
  3191                              <1> 	;
  3192                              <1> 	;-----	MIGHT BE NUM LOCK, TEST SHIFT STATUS
  3193                              <1> K50:					; ALMOST-NUM-STATE
  3194 00001269 F6C303              <1>         test    bl, LEFT_SHIFT+RIGHT_SHIFT
  3195 0000126C 75EC                <1> 	jnz 	short K49		; SHIFTED TEMP OUT OF NUM STATE
  3196 0000126E EBC4                <1> K51:	jmp	short K45E		; REALLY NUM STATE
  3197                              <1> 	;
  3198                              <1> 	;-----	TEST FOR THE NEW KEYS ON WT KEYBOARDS 
  3199                              <1> K52:					; NOT A NUMPAD KEY
  3200 00001270 3C56                <1> 	cmp	al, 86			; IS IT THE NEW WT KEY?
  3201                              <1> 	;jne	short K53		; JUMP IF NOT
  3202                              <1> 	;jmp	short K45B		; HANDLE WITH REST OF LETTER KEYS
  3203 00001272 74AF                <1> 	je	short K45B		
  3204                              <1> 	;
  3205                              <1> 	;-----	MUST BE F11 OR F12 
  3206                              <1> K53:					; F1 - F10 COME HERE, TOO
  3207 00001274 F6C303              <1> 	test	bl, LEFT_SHIFT+RIGHT_SHIFT ; TEST SHIFT STATE
  3208 00001277 74E1                <1> 	jz	short K49		; JUMP, LOWER CASE PSEUDO SC'S
  3209                              <1> 		; 20/02/2015 
  3210 00001279 BB[88690000]        <1> 	mov	ebx, K11		; UPPER CASE PSEUDO SCAN CODES
  3211 0000127E EB10                <1> 	jmp	short K64		; TRANSLATE SCAN
  3212                              <1> 	;
  3213                              <1> 	;-----	TRANSLATE THE CHARACTER
  3214                              <1> K56:					; TRANSLATE-CHAR
  3215 00001280 FEC8                <1> 	dec	al			; CONVERT ORIGIN
  3216 00001282 D7                  <1> 	xlat    			; CONVERT THE SCAN CODE TO ASCII
  3217 00001283 F605[E6690000]02    <1> 	test	byte [KB_FLAG_3], LC_E0	; IS THIS A NEW KEY?
  3218 0000128A 7416                <1> 	jz	short K57		; NO, GO FILL BUFFER
  3219 0000128C B4E0                <1> 	mov	ah, MC_E0		; YES, PUT SPECIAL MARKER IN AH
  3220 0000128E EB12                <1> 	jmp	short K57		; PUT IT INTO THE BUFFER	
  3221                              <1> 	;
  3222                              <1> 	;-----	TRANSLATE SCAN FOR PSEUDO SCAN CODES
  3223                              <1> K64:					; TRANSLATE-SCAN-ORGD
  3224 00001290 FEC8                <1> 	dec	al			; CONVERT ORIGIN
  3225 00001292 D7                  <1>        	xlat    	                ; CTL TABLE SCAN
  3226 00001293 88C4                <1> 	mov	ah, al			; PUT VALUE INTO AH
  3227 00001295 B000                <1> 	mov	al, 0			; ZERO ASCII CODE
  3228 00001297 F605[E6690000]02    <1> 	test	byte [KB_FLAG_3], LC_E0	; IS THIS A NEW KEY?
  3229 0000129E 7402                <1> 	jz	short K57		; NO, GO FILL BUFFER
  3230 000012A0 B0E0                <1> 	mov	al, MC_E0		; YES, PUT SPECIAL MARKER IN AL
  3231                              <1> 	;
  3232                              <1> 	;-----	PUT CHARACTER INTO BUFFER
  3233                              <1> K57:					; BUFFER_FILL
  3234 000012A2 3CFF                <1> 	cmp	al, -1			; IS THIS AN IGNORE CHAR
  3235 000012A4 7405                <1>         je	short K59		; YES, DO NOTHING WITH IT
  3236                              <1> 	; 05/12/2021
  3237                              <1> 	;je	K26			; YES, DO NOTHING WITH IT
  3238 000012A6 80FCFF              <1> 	cmp	ah, -1			; LOOK FOR -1 PSEUDO SCAN
  3239                              <1> 	; 05/12/2021
  3240 000012A9 7505                <1>         jne	short K61		; NEAR_INTERRUPT_RETURN
  3241                              <1> 	;je	K26			; INTERRUPT_RETURN
  3242                              <1> K59:					; NEAR_INTERRUPT_RETURN
  3243 000012AB E968FDFFFF          <1> 	jmp	K26			; INTERRUPT_RETURN
  3244                              <1> K61:					; NOT-CAPS-STATE
  3245 000012B0 8B1D[F4690000]      <1> 	mov	ebx, [BUFFER_TAIL] 	; GET THE END POINTER TO THE BUFFER
  3246 000012B6 89DE                <1> 	mov	esi, ebx		; SAVE THE VALUE
  3247 000012B8 E8AAFAFFFF          <1> 	call	_K4			; ADVANCE THE TAIL
  3248 000012BD 3B1D[F0690000]      <1> 	cmp	ebx, [BUFFER_HEAD] 	; HAS THE BUFFER WRAPPED AROUND
  3249 000012C3 740E                <1> 	je	short K62		; BUFFER_FULL_BEEP
  3250 000012C5 668906              <1> 	mov	[esi], ax		; STORE THE VALUE
  3251 000012C8 891D[F4690000]      <1> 	mov	[BUFFER_TAIL], ebx 	; MOVE THE POINTER UP
  3252 000012CE E945FDFFFF          <1> 	jmp	K26
  3253                              <1> 	;;cli				; TURN OFF INTERRUPTS
  3254                              <1> 	;;mov	al, EOI			; END OF INTERRUPT COMMAND
  3255                              <1> 	;;out	INTA00, al		; SEND COMMAND TO INTERRUPT CONTROL PORT
  3256                              <1> 	;MOV	AL, ENA_KBD		; INSURE KEYBOARD IS ENABLED
  3257                              <1> 	;CALL	SHIP_IT			; EXECUTE ENABLE
  3258                              <1> 	;MOV	AX, 9102H		; MOVE IN POST CODE & TYPE
  3259                              <1> 	;INT	15H			; PERFORM OTHER FUNCTION
  3260                              <1> 	;;and	byte [KB_FLAG_3],~(LC_E0+LC_E1) ; RESET LAST CHAR H.C. FLAG
  3261                              <1> 	;JMP	K27A			; INTERRUPT_RETURN
  3262                              <1> 	;;jmp   K27                    
  3263                              <1> 	;
  3264                              <1> 	;-----	BUFFER IS FULL SOUND THE BEEPER
  3265                              <1> K62:
  3266 000012D3 B020                <1> 	mov	al, EOI			; ENABLE INTERRUPT CONTROLLER CHIP
  3267 000012D5 E620                <1> 	out	INTA00, al
  3268 000012D7 66B9A602            <1> 	mov	cx, 678			; DIVISOR FOR 1760 HZ
  3269 000012DB B304                <1> 	mov	bl, 4			; SHORT BEEP COUNT (1/16 + 1/64 DELAY)
  3270 000012DD E89B010000          <1> 	call	beep			; GO TO COMMON BEEP HANDLER
  3271 000012E2 E93DFDFFFF          <1> 	jmp     K27			; EXIT   
  3272                              <1> 
  3273                              <1> SHIP_IT:
  3274                              <1> 	;---------------------------------------------------------------------------------
  3275                              <1> 	; SHIP_IT
  3276                              <1> 	;	THIS ROUTINES HANDLES TRANSMISSION OF COMMAND AND DATA BYTES
  3277                              <1> 	;	TO THE KEYBOARD CONTROLLER.
  3278                              <1> 	;---------------------------------------------------------------------------------
  3279                              <1> 	;
  3280                              <1> 	;push	ax			; SAVE DATA TO SEND
  3281                              <1> 	; 05/12/2021
  3282 000012E7 50                  <1> 	push	eax
  3283                              <1> 	;-----	WAIT FOR COMMAND TO ACCEPTED
  3284 000012E8 FA                  <1> 	cli				; DISABLE INTERRUPTS TILL DATA SENT
  3285                              <1> 	; xor	ecx, ecx		; CLEAR TIMEOUT COUNTER
  3286 000012E9 B900000100          <1> 	mov	ecx, 10000h			
  3287                              <1> S10:
  3288 000012EE E464                <1> 	in	al, STATUS_PORT		; READ KEYBOARD CONTROLLER STATUS
  3289 000012F0 A802                <1> 	test	al, INPT_BUF_FULL	; CHECK FOR ITS INPUT BUFFER BUSY
  3290 000012F2 E0FA                <1> 	loopnz	S10			; WAIT FOR COMMAND TO BE ACCEPTED
  3291                              <1> 
  3292                              <1> 	;pop	ax			; GET DATA TO SEND
  3293                              <1> 	; 05/12/2021
  3294 000012F4 58                  <1> 	pop	eax
  3295 000012F5 E664                <1> 	out	STATUS_PORT, al		; SEND TO KEYBOARD CONTROLLER
  3296 000012F7 FB                  <1> 	sti				; ENABLE INTERRUPTS AGAIN
  3297 000012F8 C3                  <1> 	retn				; RETURN TO CALLER
  3298                              <1> 
  3299                              <1> SND_DATA:
  3300                              <1> 	; ---------------------------------------------------------------------------------
  3301                              <1> 	; SND_DATA
  3302                              <1> 	;	THIS ROUTINES HANDLES TRANSMISSION OF COMMAND AND DATA BYTES
  3303                              <1> 	;	TO THE KEYBOARD AND RECEIPT OF ACKNOWLEDGEMENTS. IT ALSO
  3304                              <1> 	;	HANDLES ANY RETRIES IF REQUIRED
  3305                              <1> 	; ---------------------------------------------------------------------------------
  3306                              <1> 	;
  3307                              <1> 	;push	ax			; SAVE REGISTERS
  3308                              <1> 	;push	bx
  3309                              <1> 	; 05/12/2021
  3310 000012F9 50                  <1> 	push	eax
  3311 000012FA 53                  <1> 	push	ebx
  3312 000012FB 51                  <1> 	push	ecx
  3313 000012FC 88C7                <1> 	mov	bh, al			; SAVE TRANSMITTED BYTE FOR RETRIES
  3314 000012FE B303                <1> 	mov	bl, 3			; LOAD RETRY COUNT
  3315                              <1> SD0:
  3316 00001300 FA                  <1> 	cli				; DISABLE INTERRUPTS
  3317 00001301 8025[E5690000]CF    <1> 	and	byte [KB_FLAG_2], ~(KB_FE+KB_FA) ; CLEAR ACK AND RESEND FLAGS
  3318                              <1> 	;
  3319                              <1> 	;-----	WAIT FOR COMMAND TO BE ACCEPTED
  3320 00001308 B900000100          <1> 	mov	ecx, 10000h		; MAXIMUM WAIT COUNT
  3321                              <1> SD5:
  3322 0000130D E464                <1> 	in	al, STATUS_PORT		; READ KEYBOARD PROCESSOR STATUS PORT
  3323 0000130F A802                <1> 	test	al, INPT_BUF_FULL	; CHECK FOR ANY PENDING COMMAND
  3324 00001311 E0FA                <1> 	loopnz	SD5			; WAIT FOR COMMAND TO BE ACCEPTED
  3325                              <1> 	;
  3326 00001313 88F8                <1> 	mov	al, bh			; REESTABLISH BYTE TO TRANSMIT
  3327 00001315 E660                <1> 	out	PORT_A, al		; SEND BYTE
  3328 00001317 FB                  <1> 	sti				; ENABLE INTERRUPTS
  3329                              <1> 	;mov	cx, 01A00h		; LOAD COUNT FOR 10 ms+
  3330 00001318 B9FFFF0000          <1> 	mov	ecx, 0FFFFh
  3331                              <1> SD1:
  3332 0000131D F605[E5690000]30    <1> 	test	byte [KB_FLAG_2], KB_FE+KB_FA ; SEE IF EITHER BIT SET
  3333 00001324 750F                <1> 	jnz	short SD3		; IF SET, SOMETHING RECEIVED GO PROCESS
  3334 00001326 E2F5                <1> 	loop	SD1			; OTHERWISE WAIT
  3335                              <1> SD2:
  3336 00001328 FECB                <1> 	dec	bl			; DECREMENT RETRY COUNT
  3337 0000132A 75D4                <1> 	jnz	short SD0		; RETRY TRANSMISSION
  3338 0000132C 800D[E5690000]80    <1> 	or	byte [KB_FLAG_2], KB_ERR ; TURN ON TRANSMIT ERROR FLAG
  3339 00001333 EB09                <1> 	jmp	short SD4		; RETRIES EXHAUSTED FORGET TRANSMISSION
  3340                              <1> SD3:
  3341 00001335 F605[E5690000]10    <1> 	test	byte [KB_FLAG_2], KB_FA ; SEE IF THIS IS AN ACKNOWLEDGE
  3342 0000133C 74EA                <1> 	jz	short SD2		; IF NOT, GO RESEND
  3343                              <1> SD4:	
  3344 0000133E 59                  <1> 	pop	ecx			; RESTORE REGISTERS
  3345                              <1> 	;pop	bx
  3346                              <1> 	;pop	ax
  3347                              <1> 	; 05/12/2021
  3348 0000133F 5B                  <1> 	pop	ebx
  3349 00001340 58                  <1> 	pop	eax
  3350 00001341 C3                  <1> 	retn				; RETURN, GOOD TRANSMISSION
  3351                              <1> 
  3352                              <1> SND_LED:
  3353                              <1> 	; ---------------------------------------------------------------------------------
  3354                              <1> 	; SND_LED
  3355                              <1> 	;	THIS ROUTINES TURNS ON THE MODE INDICATORS.
  3356                              <1> 	;
  3357                              <1> 	;----------------------------------------------------------------------------------
  3358                              <1> 	;
  3359 00001342 FA                  <1> 	cli				; TURN OFF INTERRUPTS
  3360 00001343 F605[E5690000]40    <1> 	test	byte [KB_FLAG_2], KB_PR_LED ; CHECK FOR MODE INDICATOR UPDATE
  3361 0000134A 755F                <1> 	jnz 	short SL1		; DON'T UPDATE AGAIN IF UPDATE UNDERWAY
  3362                              <1> 	;
  3363 0000134C 800D[E5690000]40    <1> 	or	byte [KB_FLAG_2], KB_PR_LED ; TURN ON UPDATE IN PROCESS
  3364 00001353 B020                <1> 	mov	al, EOI			; END OF INTERRUPT COMMAND
  3365 00001355 E620                <1> 	out	20h, al ;out INTA00, al	; SEND COMMAND TO INTERRUPT CONTROL PORT
  3366 00001357 EB11                <1> 	jmp	short SL0		; GO SEND MODE INDICATOR COMMAND
  3367                              <1> SND_LED1:
  3368 00001359 FA                  <1> 	cli				; TURN OFF INTERRUPTS
  3369 0000135A F605[E5690000]40    <1> 	test	byte [KB_FLAG_2], KB_PR_LED ; CHECK FOR MODE INDICATOR UPDATE
  3370 00001361 7548                <1> 	jnz	short SL1		; DON'T UPDATE AGAIN IF UPDATE UNDERWAY
  3371                              <1> 	;
  3372 00001363 800D[E5690000]40    <1> 	or	byte [KB_FLAG_2], KB_PR_LED ; TURN ON UPDATE IN PROCESS
  3373                              <1> SL0:
  3374 0000136A B0ED                <1> 	mov	al, LED_CMD		; LED CMD BYTE
  3375 0000136C E888FFFFFF          <1> 	call	SND_DATA		; SEND DATA TO KEYBOARD
  3376 00001371 FA                  <1> 	cli
  3377 00001372 E836000000          <1> 	call	MAKE_LED		; GO FORM INDICATOR DATA BYTE
  3378 00001377 8025[E5690000]F8    <1> 	and	byte [KB_FLAG_2], 0F8h	; ~KB_LEDS ; CLEAR MODE INDICATOR BITS
  3379 0000137E 0805[E5690000]      <1> 	or	[KB_FLAG_2], al 	; SAVE PRESENT INDICATORS FOR NEXT TIME
  3380 00001384 F605[E5690000]80    <1> 	test	byte [KB_FLAG_2], KB_ERR ; TRANSMIT ERROR DETECTED
  3381 0000138B 750F                <1> 	jnz	short SL2		; IF SO, BYPASS SECOND BYTE TRANSMISSION
  3382                              <1> 	;
  3383 0000138D E867FFFFFF          <1> 	call	SND_DATA		; SEND DATA TO KEYBOARD
  3384 00001392 FA                  <1> 	cli				; TURN OFF INTERRUPTS
  3385 00001393 F605[E5690000]80    <1> 	test	byte [KB_FLAG_2], KB_ERR ; TRANSMIT ERROR DETECTED
  3386 0000139A 7408                <1> 	jz	short SL3		; IF NOT, DON'T SEND AN ENABLE COMMAND
  3387                              <1> SL2:
  3388 0000139C B0F4                <1> 	mov	al, KB_ENABLE		; GET KEYBOARD CSA ENABLE COMMAND
  3389 0000139E E856FFFFFF          <1> 	call	SND_DATA		; SEND DATA TO KEYBOARD
  3390 000013A3 FA                  <1> 	cli				; TURN OFF INTERRUPTS
  3391                              <1> SL3:
  3392 000013A4 8025[E5690000]3F    <1> 	and	byte [KB_FLAG_2], ~(KB_PR_LED+KB_ERR) ; TURN OFF MODE INDICATOR
  3393                              <1> SL1:					; UPDATE AND TRANSMIT ERROR FLAG
  3394 000013AB FB                  <1> 	sti				; ENABLE INTERRUPTS
  3395 000013AC C3                  <1> 	retn				; RETURN TO CALLER
  3396                              <1> 
  3397                              <1> MAKE_LED:
  3398                              <1> 	;---------------------------------------------------------------------------------
  3399                              <1> 	; MAKE_LED
  3400                              <1> 	;	THIS ROUTINES FORMS THE DATA BYTE NECESSARY TO TURN ON/OFF
  3401                              <1> 	;	THE MODE INDICATORS.
  3402                              <1> 	;---------------------------------------------------------------------------------
  3403                              <1> 	;
  3404                              <1> 	;push 	cx			; SAVE CX
  3405 000013AD A0[E3690000]        <1> 	mov	al, [KB_FLAG]		; GET CAPS & NUM LOCK INDICATORS
  3406 000013B2 2470                <1> 	and	al, CAPS_STATE+NUM_STATE+SCROLL_STATE ; ISOLATE INDICATORS
  3407                              <1> 	;mov	cl, 4			; SHIFT COUNT
  3408                              <1> 	;rol	al, cl			; SHIFT BITS OVER TO TURN ON INDICATORS
  3409 000013B4 C0C004              <1> 	rol	al, 4 ; 20/02/2015
  3410 000013B7 2407                <1> 	and	al, 07h			; MAKE SURE ONLY MODE BITS ON
  3411                              <1> 	;pop	cx
  3412 000013B9 C3                  <1> 	retn				; RETURN TO CALLER
  3413                              <1> 
  3414                              <1> ; % include 'kybdata.inc'   ; KEYBOARD DATA ; 11/03/2015
  3415                              <1> 
  3416                              <1> 
  3417                              <1> ; /// End Of KEYBOARD FUNCTIONS ///
  1848                                  
  1849                                  %include 'video.s' ; 07/03/2015
  1850                              <1> ; Temporary Runix kernel v2.0 file for debug - 22/11/2021
  1851                              <1> ; (re-write kernel for test by using previous version without a major defect)
  1852                              <1> ; ****************************************************************************
  1853                              <1> ; Retro UNIX 386 v1 Kernel - VIDEO.INC
  1854                              <1> ; Last Modification: 05/12/2021
  1855                              <1> ;		  (Video Data is in 'VIDATA.INC')
  1856                              <1> ;
  1857                              <1> ; ///////// VIDEO (CGA) FUNCTIONS ///////////////
  1858                              <1> 
  1859                              <1> ; 05/12/2021
  1860                              <1> ; 30/06/2015
  1861                              <1> ; 27/06/2015
  1862                              <1> ; 11/03/2015
  1863                              <1> ; 02/09/2014
  1864                              <1> ; 30/08/2014
  1865                              <1> ; VIDEO FUNCTIONS
  1866                              <1> ; (write_tty - Retro UNIX 8086 v1 - U9.ASM, 01/02/2014)
  1867                              <1> 
  1868                              <1> write_tty:
  1869                              <1> 	; 13/08/2015
  1870                              <1> 	; 02/09/2014
  1871                              <1> 	; 30/08/2014 (Retro UNIX 386 v1 - beginning)
  1872                              <1> 	; 01/02/2014 (Retro UNIX 8086 v1 - last update)
  1873                              <1> 	; 03/12/2013 (Retro UNIX 8086 v1 - beginning)	
  1874                              <1> 	; (Modified registers: EAX, EBX, ECX, EDX, ESI, EDI)
  1875                              <1> 	;
  1876                              <1> 	; INPUT -> AH = Color (Forecolor, Backcolor)
  1877                              <1> 	;	   AL = Character to be written
  1878                              <1> 	;	   EBX = Video Page (0 to 7)
  1879                              <1> 	;	   (BH = 0 --> Video Mode 3)
  1880                              <1> 
  1881                              <1> RVRT	equ	00001000b	; VIDEO VERTICAL RETRACE BIT
  1882                              <1> RHRZ	equ	00000001b	; VIDEO HORIZONTAL RETRACE BIT
  1883                              <1> 
  1884                              <1> ; Derived from "WRITE_TTY" procedure of IBM "pc-at" rombios source code
  1885                              <1> ; (06/10/1985), 'video.asm', INT 10H, VIDEO_IO
  1886                              <1> ;
  1887                              <1> ; 06/10/85  VIDEO DISPLAY BIOS
  1888                              <1> ;
  1889                              <1> ;--- WRITE_TTY ------------------------------------------------------------------
  1890                              <1> ;										:
  1891                              <1> ;   THIS INTERFACE PROVIDES A TELETYPE LIKE INTERFACE TO THE			:
  1892                              <1> ;   VIDEO CARDS. THE INPUT CHARACTER IS WRITTEN TO THE CURRENT			:
  1893                              <1> ;   CURSOR POSITION, AND THE CURSOR IS MOVED TO THE NEXT POSITION.		:
  1894                              <1> ;   IF THE CURSOR LEAVES THE LAST COLUMN OF THE FIELD, THE COLUMN		:
  1895                              <1> ;   IS SET TO ZERO, AND THE ROW VALUE IS INCREMENTED. IF THE ROW		:
  1896                              <1> ;   ROW VALUE LEAVES THE FIELD, THE CURSOR IS PLACED ON THE LAST ROW,		:
  1897                              <1> ;   FIRST COLUMN, AND THE ENTIRE SCREEN IS SCROLLED UP ONE LINE.		:
  1898                              <1> ;   WHEN THE SCREEN IS SCROLLED UP, THE ATTRIBUTE FOR FILLING THE		:
  1899                              <1> ;   NEWLY BLANKED LINE IS READ FROM THE CURSOR POSITION ON THE PREVIOUS		:
  1900                              <1> ;   LINE BEFORE THE SCROLL, IN CHARACTER MODE. IN GRAPHICS MODE,		:
  1901                              <1> ;   THE 0 COLOR IS USED.							:
  1902                              <1> ;   ENTRY --									:
  1903                              <1> ;     (AH) = CURRENT CRT MODE							:
  1904                              <1> ;     (AL) = CHARACTER TO BE WRITTEN						:
  1905                              <1> ;	    NOTE THAT BACK SPACE, CARRIAGE RETURN, BELL AND LINE FEED ARE	:
  1906                              <1> ;	    HANDLED AS COMMANDS RATHER THAN AS DISPLAY GRAPHICS CHARACTERS	:
  1907                              <1> ;     (BL) = FOREGROUND COLOR FOR CHAR WRITE IF CURRENTLY IN A GRAPHICS MODE	:
  1908                              <1> ;   EXIT -- 									:
  1909                              <1> ;     ALL REGISTERS SAVED							:
  1910                              <1> ;--------------------------------------------------------------------------------
  1911                              <1> 
  1912 000013BA FA                  <1> 	cli
  1913                              <1> 	;
  1914                              <1> 	; READ CURSOR (04/12/2013)
  1915                              <1> 	; Retro UNIX 386 v1 Modifications: 30/08/2014
  1916 000013BB 08FF                <1> 	or	bh, bh
  1917 000013BD 0F85AB000000        <1> 	jnz	beeper
  1918                              <1> 	; 01/09/2014
  1919 000013C3 803D[E0690000]03    <1> 	cmp	byte [CRT_MODE], 3
  1920 000013CA 7405                <1> 	je	short m3
  1921                              <1> 	;
  1922 000013CC E876020000          <1> 	call	set_mode
  1923                              <1> m3:
  1924 000013D1 89DE                <1> 	mov 	esi, ebx ; 13/08/2015 (0 to 7)
  1925 000013D3 66D1E6              <1> 	shl	si, 1
  1926 000013D6 81C6[D66F0000]      <1> 	add	esi, cursor_posn
  1927 000013DC 668B16              <1> 	mov	dx, [esi]
  1928                              <1> 	;
  1929                              <1> 	; dx now has the current cursor position
  1930                              <1> 	;
  1931 000013DF 3C0D                <1> 	cmp	al, 0Dh		; is it carriage return or control character
  1932 000013E1 764D                <1> 	jbe	short u8
  1933                              <1> 	;
  1934                              <1> 	; write the char to the screen
  1935                              <1> u0:	
  1936                              <1> 	; ah = attribute/color
  1937                              <1> 	; al = character
  1938                              <1> 	; bl = video page number (0 to 7)
  1939                              <1> 	; bh = 0
  1940                              <1> 	;
  1941 000013E3 E832020000          <1> 	call	write_c_current
  1942                              <1> 	;
  1943                              <1> 	; position the cursor for next char
  1944 000013E8 FEC2                <1> 	inc	dl		; next column
  1945                              <1> 	;cmp	dl, [CRT_COLS]
  1946 000013EA 80FA50              <1> 	cmp	dl, 80		; test for column overflow 
  1947 000013ED 0F85E9000000        <1>         jne     set_cpos
  1948 000013F3 B200                <1> 	mov	dl, 0		; column = 0
  1949                              <1> u10:				; (line feed found)
  1950 000013F5 80FE18              <1> 	cmp	dh, 25-1 	; check for last row
  1951 000013F8 722F                <1> 	jb 	short u6
  1952                              <1> 	;
  1953                              <1> 	; scroll required
  1954                              <1> u1:	
  1955                              <1> 	; SET CURSOR POSITION (04/12/2013)
  1956 000013FA E8DD000000          <1> 	call	set_cpos
  1957                              <1> 	;
  1958                              <1> 	; determine value to fill with during scroll
  1959                              <1> u2:
  1960                              <1> 	; READ_AC_CURRENT		:
  1961                              <1> 	;   THIS ROUTINE READS THE ATTRIBUTE AND CHARACTER
  1962                              <1> 	;    AT THE CURRENT CURSOR POSITION
  1963                              <1> 	;
  1964                              <1> 	; INPUT				
  1965                              <1> 	;	(AH) = CURRENT CRT MODE
  1966                              <1> 	;	(BH) = DISPLAY PAGE ( ALPHA MODES ONLY )
  1967                              <1> 	;	(DS) = DATA SEGMENT
  1968                              <1> 	;	(ES) = REGEN SEGMENT
  1969                              <1> 	; OUTPUT			
  1970                              <1> 	;	(AL) = CHARACTER READ
  1971                              <1> 	;	(AH) = ATTRIBUTE READ
  1972                              <1> 	;
  1973                              <1> 	; mov	ah, [CRT_MODE] ; move current mode into ah
  1974                              <1> 	;
  1975                              <1> 	; bl = video page number
  1976                              <1> 	;
  1977 000013FF E834010000          <1> 	call	find_position	; get regen location and port address
  1978                              <1> 	; dx = status port
  1979                              <1> 	; esi = cursor location/address
  1980                              <1> p11:
  1981 00001404 FB                  <1> 	sti			; enable interrupts
  1982 00001405 90                  <1> 	nop			; allow for small interupts window
  1983 00001406 FA                  <1> 	cli			; blocks interrupts for single loop
  1984 00001407 EC                  <1> 	in	al, dx		; get status from adapter
  1985 00001408 A801                <1> 	test	al, RHRZ	; is horizontal retrace low
  1986 0000140A 75F8                <1> 	jnz	short p11	; wait until it is
  1987                              <1> p12:				; now wait for either retrace high
  1988 0000140C EC                  <1> 	in	al, dx		; get status
  1989 0000140D A809                <1> 	test	al, RVRT+RHRZ	; is horizontal or vertical retrace high
  1990 0000140F 74FB                <1> 	jz	short p12	; wait until either is active	
  1991                              <1> p13:
  1992 00001411 81C600800B00        <1> 	add	esi, 0B8000h	; 30/08/2014 (Retro UNIX 386 v1)
  1993 00001417 668B06              <1> 	mov 	ax, [esi]	; get the character and attribute
  1994                              <1> 	;
  1995                              <1> 	; al = character, ah = attribute
  1996                              <1> 	;
  1997 0000141A FB                  <1> 	sti
  1998                              <1> 	; bl = video page number 	
  1999                              <1> u3:
  2000                              <1> 	;;mov	ax, 0601h 	; scroll one line
  2001                              <1> 	;;sub	cx, cx		; upper left corner
  2002                              <1> 	;;mov	dh, 25-1 	; lower right row
  2003                              <1> 	;;;mov	dl, [CRT_COLS]
  2004                              <1> 	;mov	dl, 80		; lower right column	
  2005                              <1> 	;;dec	dl
  2006                              <1> 	;;mov	dl, 79
  2007                              <1> 
  2008                              <1> 	;;call	scroll_up	; 04/12/2013
  2009                              <1> 	;;; 11/03/2015
  2010                              <1> 	; 02/09/2014
  2011                              <1> 	;;;mov	cx, [crt_ulc] ; Upper left corner  (0000h)
  2012                              <1> 	;;;mov	dx, [crt_lrc] ; Lower right corner (184Fh)
  2013                              <1> 	; 11/03/2015
  2014 0000141B 6629C9              <1> 	sub	cx, cx
  2015 0000141E 66BA4F18            <1> 	mov	dx, 184Fh ; dl= 79 (column), dh = 24 (row)
  2016                              <1> 	;
  2017 00001422 B001                <1> 	mov	al, 1		; scroll 1 line up
  2018                              <1> 		; ah = attribute
  2019 00001424 E939010000          <1> 	jmp	scroll_up
  2020                              <1> ;u4:
  2021                              <1> 	;;int	10h		; video-call return
  2022                              <1> 				; scroll up the screen
  2023                              <1> 				; tty return
  2024                              <1> ;u5:
  2025                              <1> 	;retn			; return to the caller
  2026                              <1> 
  2027                              <1> u6:				; set-cursor-inc
  2028 00001429 FEC6                <1> 	inc	dh		; next row
  2029                              <1> 				; set cursor
  2030                              <1> ;u7:					
  2031                              <1> 	;;mov	ah, 02h
  2032                              <1> 	;;jmp	short u4 	; establish the new cursor
  2033                              <1> 	;call	set_cpos
  2034                              <1> 	;jmp 	short u5
  2035 0000142B E9AC000000          <1> 	jmp     set_cpos
  2036                              <1> 
  2037                              <1> 	; check for control characters
  2038                              <1> u8:
  2039 00001430 7438                <1> 	je	short u9
  2040 00001432 3C0A                <1> 	cmp	al, 0Ah		; is it a line feed (0Ah)
  2041 00001434 74BF                <1> 	je	short u10
  2042 00001436 3C07                <1> 	cmp	al, 07h 	; is it a bell
  2043 00001438 7434                <1> 	je	short u11
  2044 0000143A 3C08                <1> 	cmp	al, 08h		; is it a backspace
  2045                              <1> 	;jne	short u0
  2046 0000143C 7424                <1> 	je	short bs	; 12/12/2013
  2047                              <1> 	; 12/12/2013 (tab stop)
  2048 0000143E 3C09                <1> 	cmp	al, 09h		; is it a tab stop
  2049 00001440 75A1                <1> 	jne	short u0
  2050 00001442 88D0                <1> 	mov	al, dl
  2051 00001444 6698                <1> 	cbw
  2052 00001446 B108                <1> 	mov	cl, 8
  2053 00001448 F6F1                <1> 	div	cl
  2054 0000144A 28E1                <1> 	sub	cl, ah
  2055                              <1> ts:
  2056                              <1> 	; 02/09/2014
  2057                              <1> 	; 01/09/2014
  2058 0000144C B020                <1> 	mov	al, 20h
  2059                              <1> tsloop:
  2060 0000144E 6651                <1> 	push	cx
  2061 00001450 6650                <1> 	push	ax
  2062 00001452 30FF                <1> 	xor 	bh, bh
  2063                              <1> 	;mov	bl, [active_page]
  2064 00001454 E878FFFFFF          <1> 	call	m3
  2065 00001459 6658                <1> 	pop	ax  ; ah = attribute/color
  2066 0000145B 6659                <1> 	pop	cx
  2067 0000145D FEC9                <1> 	dec	cl
  2068 0000145F 75ED                <1> 	jnz	short tsloop
  2069 00001461 C3                  <1> 	retn
  2070                              <1> bs:	
  2071                              <1> 	; back space found
  2072                              <1> 
  2073 00001462 08D2                <1> 	or	dl, dl 		; is it already at start of line
  2074                              <1> 	;je	short u7 	; set_cursor
  2075 00001464 7476                <1> 	jz	short set_cpos
  2076 00001466 664A                <1> 	dec	dx     		; no -- just move it back
  2077                              <1> 	;jmp	short u7
  2078 00001468 EB72                <1> 	jmp	short set_cpos
  2079                              <1> 
  2080                              <1> 	; carriage return found
  2081                              <1> u9:
  2082 0000146A B200                <1> 	mov	dl, 0 		; move to first column
  2083                              <1> 	;jmp	short u7
  2084 0000146C EB6E                <1> 	jmp	short set_cpos
  2085                              <1> 
  2086                              <1> 	; line feed found
  2087                              <1> ;u10:
  2088                              <1> ;	cmp	dh, 25-1 	; bottom of screen
  2089                              <1> ;	jne	short u6 	; no, just set the cursor
  2090                              <1> ;       jmp     u1              ; yes, scroll the screen
  2091                              <1> 
  2092                              <1> beeper: 
  2093                              <1> 	; 30/08/2014 (Retro UNIX 386 v1)
  2094                              <1> 	; 18/01/2014
  2095                              <1> 	; 03/12/2013
  2096                              <1> 	; bell found
  2097                              <1> u11:
  2098 0000146E FB                  <1> 	sti
  2099 0000146F 3A1D[E66F0000]      <1> 	cmp	bl, [active_page]
  2100 00001475 7551                <1> 	jne	short u12	; Do not sound the beep 
  2101                              <1> 				; if it is not written on the active page
  2102 00001477 66B93305            <1> 	mov	cx, 1331 	; divisor for 896 hz tone
  2103 0000147B B31F                <1> 	mov	bl, 31		; set count for 31/64 second for beep
  2104                              <1> 	;call	beep		; sound the pod bell
  2105                              <1> 	;jmp	short u5 	; tty_return
  2106                              <1> 	;retn
  2107                              <1> 	
  2108                              <1> TIMER	equ 	040h   		; 8254 TIMER - BASE ADDRESS
  2109                              <1> PORT_B	equ	061h		; PORT B READ/WRITE DIAGNOSTIC REGISTER
  2110                              <1> GATE2	equ	00000001b	; TIMER 2 INPUT CATE CLOCK BIT
  2111                              <1> SPK2	equ	00000010b	; SPEAKER OUTPUT DATA ENABLE BIT
  2112                              <1> 
  2113                              <1> beep:
  2114                              <1> 	; 07/02/2015
  2115                              <1> 	; 30/08/2014 (Retro UNIX 386 v1)
  2116                              <1> 	; 18/01/2014
  2117                              <1> 	; 03/12/2013
  2118                              <1> 	;
  2119                              <1> 	; TEST4.ASM - 06/10/85  POST AND BIOS UTILITY ROUTINES
  2120                              <1> 	;
  2121                              <1> 	; ROUTINE TO SOUND THE BEEPER USING TIMER 2 FOR TONE
  2122                              <1> 	;
  2123                              <1> 	; ENTRY:
  2124                              <1> 	;    (BL) = DURATION COUNTER ( 1 FOR 1/64 SECOND )
  2125                              <1> 	;    (CX) = FREQUENCY DIVISOR (1193180/FREQUENCY) (1331 FOR 886 HZ)
  2126                              <1> 	; EXIT:				:
  2127                              <1> 	;    (AX),(BL),(CX) MODIFIED.
  2128                              <1> 
  2129 0000147D 9C                  <1> 	pushf  ; 18/01/2014	; save interrupt status
  2130 0000147E FA                  <1> 	cli			; block interrupts during update
  2131 0000147F B0B6                <1> 	mov	al, 10110110b	; select timer 2, lsb, msb binary
  2132 00001481 E643                <1> 	out	TIMER+3, al 	; write timer mode register
  2133 00001483 EB00                <1> 	jmp	$+2		; I/O delay
  2134 00001485 88C8                <1> 	mov	al, cl		; divisor for hz (low)
  2135 00001487 E642                <1> 	out	TIMER+2,AL	; write timer 2 count - lsb
  2136 00001489 EB00                <1> 	jmp	$+2		; I/O delay
  2137 0000148B 88E8                <1> 	mov	al, ch		; divisor for hz (high)
  2138 0000148D E642                <1> 	out	TIMER+2, al	; write timer 2 count - msb
  2139 0000148F E461                <1> 	in	al, PORT_B	; get current setting of port
  2140 00001491 88C4                <1> 	mov	ah, al		; save that setting
  2141 00001493 0C03                <1> 	or	al, GATE2+SPK2	; gate timer 2 and turn speaker on
  2142 00001495 E661                <1> 	out	PORT_B, al	; and restore interrupt status
  2143                              <1> 	;popf	; 18/01/2014
  2144 00001497 FB                  <1> 	sti
  2145                              <1> g7:				; 1/64 second per count (bl)
  2146 00001498 B90B040000          <1> 	mov	ecx, 1035	; delay count for 1/64 of a second	
  2147 0000149D E827000000          <1> 	call	waitf		; go to beep delay 1/64 count
  2148 000014A2 FECB                <1> 	dec	bl		; (bl) length count expired?
  2149 000014A4 75F2                <1> 	jnz	short g7	; no - continue beeping speaker
  2150                              <1> 	;
  2151                              <1> 	;pushf			; save interrupt status
  2152 000014A6 FA                  <1> 	cli  	; 18/01/2014	; block interrupts during update
  2153 000014A7 E461                <1> 	in	al, PORT_B	; get current port value
  2154                              <1>         ;or      al, not (GATE2+SPK2) ; isolate current speaker bits in case
  2155 000014A9 0CFC                <1>         or      al, ~(GATE2+SPK2)
  2156 000014AB 20C4                <1>         and	ah, al		; someone turned them off during beep
  2157 000014AD 88E0                <1> 	mov	al, ah		; recover value of port
  2158                              <1>         ;or      al, not (GATE2+SPK2) ; force speaker data off
  2159 000014AF 0CFC                <1> 	or 	al, ~(GATE2+SPK2) ; isolate current speaker bits in case
  2160 000014B1 E661                <1> 	out	PORT_B, al	; and stop speaker timer
  2161                              <1> 	;popf			; restore interrupt flag state
  2162 000014B3 FB                  <1> 	sti
  2163 000014B4 B90B040000          <1> 	mov	ecx, 1035	; force 1/64 second delay (short)
  2164 000014B9 E80B000000          <1> 	call	waitf		; minimum delay between all beeps
  2165                              <1> 	;pushf			; save interrupt status
  2166 000014BE FA                  <1> 	cli			; block interrupts during update
  2167 000014BF E461                <1> 	in	al, PORT_B	; get current port value in case	
  2168 000014C1 2403                <1> 	and	al, GATE2+SPK2	; someone turned them on
  2169 000014C3 08E0                <1> 	or	al, ah		; recover value of port_b
  2170 000014C5 E661                <1> 	out	PORT_B, al	; restore speaker status
  2171 000014C7 9D                  <1> 	popf			; restore interrupt flag state
  2172                              <1> u12:	
  2173 000014C8 C3                  <1> 	retn
  2174                              <1> 
  2175                              <1> REFRESH_BIT equ	00010000b 	; REFRESH TEST BIT
  2176                              <1> 
  2177                              <1> WAITF:
  2178                              <1> waitf:
  2179                              <1> 	; 30/08/2014 (Retro UNIX 386 v1)
  2180                              <1> 	; 03/12/2013
  2181                              <1> 	;
  2182                              <1> ;	push ax			; save work register (ah)	
  2183                              <1> ;waitf1:
  2184                              <1> 				; use timer 1 output bits
  2185                              <1> ;	in	al, PORT_B	; read current counter output status
  2186                              <1> ;	and	al, REFRESH_BIT	; mask for refresh determine bit
  2187                              <1> ;	cmp	al, ah		; did it just change
  2188                              <1> ;	je	short waitf1	; wait for a change in output line
  2189                              <1> ;	;
  2190                              <1> ;	mov	ah, al		; save new lflag state
  2191                              <1> ;	loop	waitf1		; decrement half cycles till count end		
  2192                              <1> ;	;
  2193                              <1> ;	pop	ax		; restore (ah)
  2194                              <1> ;	retn			; return (cx)=0
  2195                              <1> 
  2196                              <1> ; 05/12/2021 (Retro UNIX 386 v1.2)
  2197                              <1> ; 06/02/2015 (unix386.s <-- dsectrm2.s)
  2198                              <1> ; 17/12/2014 (dsectrm2.s)
  2199                              <1> ; WAITF
  2200                              <1> ; /// IBM PC-XT Model 286 System BIOS Source Code - Test 4 - 06/10/85 ///
  2201                              <1> ;
  2202                              <1> ;---WAITF-----------------------------------------------------------------------
  2203                              <1> ;	FIXED TIME WAIT ROUTINE (HARDWARE CONTROLLED - NOT PROCESSOR)
  2204                              <1> ; ENTRY:
  2205                              <1> ;	(CX) =	COUNT OF 15.085737 MICROSECOND INTERVALS TO WAIT
  2206                              <1> ;	      	MEMORY REFRESH TIMER 1 OUTPUT USED AS REFERENCE
  2207                              <1> ; EXIT:
  2208                              <1> ;	       	AFTER (CX) TIME COUNT (PLUS OR MINUS 16 MICROSECONDS)
  2209                              <1> ;	(CX) = 0	
  2210                              <1> ;-------------------------------------------------------------------------------
  2211                              <1> 
  2212                              <1> ; Refresh period: 30 micro seconds (15-80 us)
  2213                              <1> ; (16/12/2014 - AWARDBIOS 1999 - ATORGS.ASM, WAIT_REFRESH)
  2214                              <1> 
  2215                              <1> ;WAITF:					; DELAY FOR (CX)*15.085737 US
  2216                              <1> 	;PUSH	AX			; SAVE WORK REGISTER (AH)
  2217                              <1> 	; 05/12/2021
  2218 000014C9 50                  <1> 	push	eax
  2219                              <1> 	; 16/12/2014
  2220                              <1> 	;shr	cx, 1			; convert to count of 30 micro seconds
  2221 000014CA D1E9                <1> 	shr	ecx, 1	; 21/02/2015
  2222                              <1> ;17/12/2014	
  2223                              <1> ;WAITF1:
  2224                              <1> ;	IN	AL, PORT_B   ;061h	; READ CURRENT COUNTER OUTPUT STATUS
  2225                              <1> ;	AND	AL, REFRESH_BIT	;00010000b ; MASK FOR REFRESH DETERMINE BIT
  2226                              <1> ;	CMP	AL, AH			; DID IT JUST CHANGE
  2227                              <1> ;	JE	short WAITF1		; WAIT FOR A CHANGE IN OUTPUT LINE
  2228                              <1> ;	MOV	AH, AL			; SAVE NEW FLAG STATE
  2229                              <1> ;	LOOP	WAITF1			; DECREMENT HALF CYCLES TILL COUNT END		
  2230                              <1> 	;
  2231                              <1> 	; 17/12/2014
  2232                              <1> 	;
  2233                              <1> 	; Modification from 'WAIT_REFRESH' procedure of AWARD BIOS - 1999
  2234                              <1> 	;
  2235                              <1> ;WAIT_REFRESH:  Uses port 61, bit 4 to have CPU speed independent waiting.
  2236                              <1> ;   	INPUT:  CX = number of refresh periods to wait
  2237                              <1> ;     	       (refresh periods = 1 per 30 microseconds on most machines)
  2238                              <1> WR_STATE_0:
  2239 000014CC E461                <1> 	IN	AL,PORT_B		; IN AL,SYS1
  2240 000014CE A810                <1> 	TEST	AL,010H
  2241 000014D0 74FA                <1> 	JZ	SHORT WR_STATE_0
  2242                              <1> WR_STATE_1:
  2243 000014D2 E461                <1> 	IN	AL,PORT_B		; IN AL,SYS1
  2244 000014D4 A810                <1> 	TEST	AL,010H
  2245 000014D6 75FA                <1> 	JNZ	SHORT WR_STATE_1
  2246 000014D8 E2F2                <1>         LOOP    WR_STATE_0
  2247                              <1> 	;
  2248                              <1> 	;POP	AX			; RESTORE (AH)
  2249                              <1> 	; 05/12/2021
  2250 000014DA 58                  <1> 	pop	eax
  2251 000014DB C3                  <1> 	RETn				; (CX) = 0
  2252                              <1> 
  2253                              <1> set_cpos:
  2254                              <1> 	; 27/06/2015
  2255                              <1> 	; 01/09/2014
  2256                              <1> 	; 30/08/2014 (Retro UNIX 386 v1 - beginning)
  2257                              <1> 	;
  2258                              <1> 	; 12/12/2013 (Retro UNIX 8086 v1 - last update) 
  2259                              <1> 	; 04/12/2013 (Retro UNIX 8086 v1 - beginning)
  2260                              <1> 	;
  2261                              <1> 	; VIDEO.ASM - 06/10/85  VIDEO DISPLAY BIOS
  2262                              <1> 	;
  2263                              <1> 	; SET_CPOS
  2264                              <1> 	;	THIS ROUTINE SETS THE CURRENT CURSOR POSITION TO THE
  2265                              <1> 	;	NEW X-Y VALUES PASSED
  2266                              <1> 	; INPUT
  2267                              <1> 	;	DX - ROW,COLUMN OF NEW CURSOR
  2268                              <1> 	;	BH - DISPLAY PAGE OF CURSOR
  2269                              <1> 	; OUTPUT
  2270                              <1> 	;	CURSOR IS SET AT 6845 IF DISPLAY PAGE IS CURRENT DISPLAY
  2271                              <1> 	;
  2272 000014DC 0FB6C3              <1>         movzx   eax, bl  ; BL = video page number ; 27/06/2015 (movzx)
  2273 000014DF D0E0                <1>         shl     al, 1   ; word offset
  2274 000014E1 BE[D66F0000]        <1> 	mov	esi, cursor_posn
  2275 000014E6 01C6                <1>         add     esi, eax
  2276 000014E8 668916              <1> 	mov	[esi], dx ; save the pointer
  2277 000014EB 381D[E66F0000]      <1> 	cmp	[active_page], bl
  2278 000014F1 7532                <1> 	jne	short m17
  2279                              <1> 	;call	m18	; CURSOR SET
  2280                              <1> ;m17:			; SET_CPOS_RETURN
  2281                              <1> 	; 01/09/2014
  2282                              <1> ;	retn
  2283                              <1> 		; DX  = row/column
  2284                              <1> m18:
  2285 000014F3 E832000000          <1> 	call	position ; determine location in regen buffer	
  2286 000014F8 668B0D[D46F0000]    <1> 	mov	cx, [CRT_START]
  2287 000014FF 6601C1              <1> 	add	cx, ax  ; add char position in regen buffer
  2288                              <1> 			; to the start address (offset) for this page
  2289 00001502 66D1E9              <1> 	shr	cx, 1	; divide by 2 for char only count
  2290 00001505 B40E                <1> 	mov	ah, 14	; register number for cursor
  2291                              <1> 	;call	m16	; output value to the 6845	
  2292                              <1> 	;retn
  2293                              <1> 
  2294                              <1> 	;-----	THIS ROUTINE OUTPUTS THE CX REGISTER
  2295                              <1> 	;	TO THE 6845 REGISTERS NAMED IN (AH)
  2296                              <1> m16:
  2297 00001507 FA                  <1> 	cli
  2298                              <1> 	;mov	dx, [addr_6845] ; address register
  2299 00001508 66BAD403            <1> 	mov 	dx, 03D4h ; I/O address of color card
  2300 0000150C 88E0                <1> 	mov	al, ah	; get value
  2301 0000150E EE                  <1> 	out	dx, al	; register set
  2302 0000150F 6642                <1> 	inc	dx	; data register
  2303 00001511 EB00                <1> 	jmp	$+2	; i/o delay
  2304 00001513 88E8                <1> 	mov	al, ch	; data
  2305 00001515 EE                  <1> 	out	dx, al	
  2306 00001516 664A                <1> 	dec	dx	
  2307 00001518 88E0                <1> 	mov	al, ah
  2308 0000151A FEC0                <1> 	inc	al	; point to other data register
  2309 0000151C EE                  <1> 	out	dx, al	; set for second register
  2310 0000151D 6642                <1> 	inc	dx
  2311 0000151F EB00                <1> 	jmp	$+2	; i/o delay
  2312 00001521 88C8                <1> 	mov	al, cl	; second data value
  2313 00001523 EE                  <1> 	out	dx, al
  2314 00001524 FB                  <1> 	sti
  2315                              <1> m17:
  2316 00001525 C3                  <1> 	retn
  2317                              <1> 
  2318                              <1> set_ctype:
  2319                              <1> 	; 02/09/2014 (Retro UNIX 386 v1)
  2320                              <1> 	;
  2321                              <1> 	; VIDEO.ASM - 06/10/85  VIDEO DISPLAY BIOS
  2322                              <1> 
  2323                              <1> ;	CH) = BITS 4-0 = START LINE FOR CURSOR
  2324                              <1> ;       ** HARDWARE WILL ALWAYS CAUSE BLINK
  2325                              <1> ;       ** SETTING BIT 5 OR 6 WILL CAUSE ERRATIC BLINKING
  2326                              <1> ;          OR NO CURSOR AT ALL
  2327                              <1> ;	(CL) = BITS 4-0 = END LINE FOR CURSOR
  2328                              <1> 
  2329                              <1> ;------------------------------------------------
  2330                              <1> ; SET_CTYPE
  2331                              <1> ;	THIS ROUTINE SETS THE CURSOR VALUE
  2332                              <1> ; INPUT
  2333                              <1> ;	(CX) HAS CURSOR VALUE CH-START LINE, CL-STOP LINE
  2334                              <1> ; OUTPUT	
  2335                              <1> ;	NONE
  2336                              <1> ;------------------------------------------------
  2337                              <1> 
  2338 00001526 B40A                <1> 	mov	ah, 10	; 6845 register for cursor set
  2339                              <1> 	;mov	[CURSOR_MODE], cx ; save in data area
  2340                              <1> 	;call	m16	; output cx register
  2341                              <1> 	;retn
  2342 00001528 EBDD                <1> 	jmp	m16
  2343                              <1> 
  2344                              <1> 
  2345                              <1> position:
  2346                              <1> 	; 05/12/2021 (Retro UNIX 386 v1.2)
  2347                              <1> 	; 27/06/2015
  2348                              <1> 	; 02/09/2014
  2349                              <1> 	; 30/08/2014 (Retro UNIX 386 v1)
  2350                              <1> 	; 04/12/2013 (Retro UNIX 8086 v1)
  2351                              <1> 	;
  2352                              <1> 	; VIDEO.ASM - 06/10/85  VIDEO DISPLAY BIOS
  2353                              <1> 	;
  2354                              <1> 	; POSITION
  2355                              <1> 	;	THIS SERVICE ROUTINE CALCULATES THE REGEN BUFFER ADDRESS
  2356                              <1> 	;	OF A CHARACTER IN THE ALPHA MODE
  2357                              <1> 	; INPUT
  2358                              <1> 	;	AX = ROW, COLUMN POSITION
  2359                              <1> 	; OUTPUT
  2360                              <1> 	;	AX = OFFSET OF CHAR POSITION IN REGEN BUFFER
  2361                              <1> 
  2362                              <1> 		; DX = ROW, COLUMN POSITION
  2363                              <1> 	;movzx	eax, byte [CRT_COLS] ; 27/06/2015
  2364 0000152A 31C0                <1> 	xor	eax, eax ; 02/09/2014
  2365 0000152C B050                <1> 	mov	al, 80   ; determine bytes to row	
  2366 0000152E F6E6                <1> 	mul	dh 	 ; row value
  2367 00001530 30F6                <1> 	xor	dh, dh   ; 0	
  2368 00001532 6601D0              <1> 	add	ax, dx	 ; add column value to the result
  2369                              <1> 	;shl	ax, 1	; * 2 for attribute bytes
  2370                              <1> 	; 05/12/2021
  2371 00001535 D1E0                <1> 	shl	eax, 1
  2372                              <1> 		; EAX = AX = OFFSET OF CHAR POSITION IN REGEN BUFFER 
  2373 00001537 C3                  <1> 	retn
  2374                              <1> 
  2375                              <1> find_position:
  2376                              <1> 	; 05/12/2021 (Retro UNIX 386 v1.2)
  2377                              <1> 	; 27/06/2015
  2378                              <1> 	; 07/09/2014
  2379                              <1> 	; 02/09/2014
  2380                              <1> 	; 30/08/2014 (Retro UNIX 386 v1)
  2381                              <1> 	; VIDEO.ASM - 06/10/85  VIDEO DISPLAY BIOS
  2382 00001538 0FB6CB              <1> 	movzx	ecx, bl ; video page number ; 27/06/2015 (movzx)
  2383 0000153B 89CE                <1> 	mov	esi, ecx
  2384                              <1> 	;shl	si, 1
  2385 0000153D D1E6                <1> 	shl	esi, 1 ; 05/12/2021
  2386 0000153F 668B96[D66F0000]    <1> 	mov	dx, [esi+cursor_posn]
  2387 00001546 7409                <1> 	jz	short p21
  2388                              <1> 	;xor	si, si
  2389 00001548 31F6                <1> 	xor	esi, esi ; 05/12/2021
  2390                              <1> p20:
  2391                              <1> 	;add	si, [CRT_LEN]
  2392 0000154A 6681C6A00F          <1> 	add	si, 80*25*2 ; add length of buffer for one page		
  2393 0000154F E2F9                <1> 	loop	p20
  2394                              <1> p21:
  2395 00001551 6621D2              <1> 	and	dx, dx
  2396 00001554 7407                <1> 	jz	short p22
  2397 00001556 E8CFFFFFFF          <1> 	call 	position ; determine location in regen in page
  2398 0000155B 01C6                <1> 	add	esi, eax ; add location to start of regen page
  2399                              <1> p22:	
  2400                              <1> 	;mov	dx, [addr_6845] ; get base address of active display			
  2401                              <1> 	;mov	dx, 03D4h ; I/O address of color card
  2402                              <1> 	;add	dx, 6	; point at status port
  2403 0000155D 66BADA03            <1> 	mov	dx, 03DAh ; status port
  2404                              <1> 	; cx = 0
  2405 00001561 C3                  <1> 	retn
  2406                              <1> 
  2407                              <1> scroll_up:
  2408                              <1> 	; 05/12/2021 (Retro UNIX 386 v1.2)
  2409                              <1> 	; 16/01/2016
  2410                              <1> 	; 07/09/2014
  2411                              <1> 	; 02/09/2014
  2412                              <1> 	; 01/09/2014 (Retro UNIX 386 v1 - beginning)
  2413                              <1> 	; 04/04/2014
  2414                              <1> 	; 04/12/2013
  2415                              <1> 	;
  2416                              <1> 	; VIDEO.ASM - 06/10/85  VIDEO DISPLAY BIOS
  2417                              <1> 	;
  2418                              <1> 	; SCROLL UP
  2419                              <1> 	;	THIS ROUTINE MOVES A BLOCK OF CHARACTERS UP
  2420                              <1> 	;	ON THE SCREEN
  2421                              <1> 	; INPUT
  2422                              <1> 	;	(AH) = CURRENT CRT MODE
  2423                              <1> 	;	(AL) = NUMBER OF ROWS TO SCROLL
  2424                              <1> 	;	(CX) = ROW/COLUMN OF UPPER LEFT CORNER
  2425                              <1> 	;	(DX) = ROW/COLUMN OF LOWER RIGHT CORNER
  2426                              <1> 	;	(BH) = ATTRIBUTE TO BE USED ON BLANKED LINE
  2427                              <1> 	;	(DS) = DATA SEGMENT
  2428                              <1> 	;	(ES) = REGEN BUFFER SEGMENT
  2429                              <1> 	; OUTPUT
  2430                              <1> 	;	NONE -- THE REGEN BUFFER IS MODIFIED
  2431                              <1> 	;
  2432                              <1> 	;	bh = 0  (02/09/2014)
  2433                              <1> 	;
  2434                              <1> 	; ((ah = 3))
  2435                              <1> 	; cl = left upper column
  2436                              <1> 	; ch = left upper row
  2437                              <1> 	; dl = right lower column
  2438                              <1> 	; dh = right lower row
  2439                              <1> 	;
  2440                              <1> 	; al = line count 
  2441                              <1> 	; ah = attribute to be used on blanked line	
  2442                              <1> 	; bl = video page number (0 to 7)
  2443                              <1> 	; 
  2444                              <1> 
  2445                              <1> 	; Test	Line Count
  2446 00001562 08C0                <1> 	or	al, al
  2447 00001564 740C                <1> 	jz	short al_set
  2448 00001566 88F7                <1> 	mov	bh, dh	; subtract lower row from upper row
  2449 00001568 28EF                <1> 	sub	bh, ch
  2450 0000156A FEC7                <1> 	inc	bh	; adjust difference by 1
  2451 0000156C 38C7                <1> 	cmp	bh, al 	; line count = amount of rows in window?
  2452 0000156E 7502                <1> 	jne	short al_set ; if not the we're all set
  2453 00001570 30C0                <1> 	xor	al, al	; otherwise set al to zero
  2454                              <1> al_set:
  2455 00001572 30FF                <1> 	xor	bh, bh	; 0
  2456                              <1> 	;push	ax
  2457 00001574 50                  <1> 	push	eax ; 05/12/2021
  2458                              <1> 	;mov 	esi, [crt_base]
  2459 00001575 BE00800B00          <1>         mov     esi, 0B8000h  
  2460 0000157A 3A1D[E66F0000]      <1>         cmp     bl, [active_page]
  2461 00001580 750B                <1> 	jne	short n0
  2462                              <1> 	;
  2463 00001582 66A1[D46F0000]      <1>         mov     ax, [CRT_START]
  2464 00001588 6601C6              <1>         add     si, ax
  2465 0000158B EB0F                <1>         jmp     short n1
  2466                              <1> n0:
  2467 0000158D 20DB                <1>         and     bl, bl
  2468 0000158F 740B                <1> 	jz	short n1
  2469 00001591 88D8                <1> 	mov	al, bl
  2470                              <1> n0x:
  2471                              <1>         ;add    si, [CRT_LEN]
  2472                              <1>         ;add    esi, 80*25*2 
  2473 00001593 6681C6A00F          <1>         add     si, 80*25*2
  2474 00001598 FEC8                <1>         dec	al
  2475 0000159A 75F7                <1> 	jnz	short n0x
  2476                              <1> n1:	
  2477                              <1>         ;Scroll position
  2478                              <1> 	;push	dx
  2479                              <1> 	; 05/12/2021
  2480 0000159C 52                  <1> 	push	edx
  2481 0000159D 6689CA              <1> 	mov	dx, cx	; now, upper left position in DX
  2482 000015A0 E885FFFFFF          <1> 	call	position
  2483 000015A5 01C6                <1> 	add	esi, eax
  2484 000015A7 89F7                <1> 	mov	edi, esi
  2485                              <1> 	;pop	dx	; lower right position in DX
  2486 000015A9 5A                  <1> 	pop	edx ; 05/12/2021
  2487 000015AA 6629CA              <1> 	sub	dx, cx
  2488 000015AD FEC6                <1> 	inc	dh	; dh = #rows 
  2489 000015AF FEC2                <1> 	inc	dl	; dl = #cols in block
  2490                              <1> 	;pop	ax	; al = line count, ah = attribute
  2491                              <1> 	; 05/12/2021
  2492 000015B1 58                  <1> 	pop	eax	; al = line count, ah = attribute
  2493 000015B2 31C9                <1> 	xor	ecx, ecx
  2494 000015B4 6689C1              <1> 	mov	cx, ax
  2495                              <1> 	;mov	ah, [CRT_COLS]
  2496 000015B7 B450                <1> 	mov	ah, 80
  2497 000015B9 F6E4                <1> 	mul	ah	; determine offset to from address
  2498 000015BB 6601C0              <1> 	add	ax, ax  ; *2 for attribute byte
  2499                              <1> 	;
  2500                              <1> 	;push	ax	; offset 
  2501                              <1> 	;push	dx
  2502                              <1> 	; 05/12/2021
  2503 000015BE 50                  <1> 	push	eax
  2504 000015BF 52                  <1> 	push	edx
  2505                              <1> 	;
  2506                              <1> 	; 04/04/2014
  2507 000015C0 66BADA03            <1> 	mov	dx, 3DAh ; guaranteed to be color card here	
  2508                              <1> n8:                      ; wait_display_enable
  2509 000015C4 EC                  <1>         in      al, dx   ; get port
  2510 000015C5 A808                <1> 	test	al, RVRT ; wait for vertical retrace	
  2511 000015C7 74FB                <1> 	jz	short n8 ; wait_display_enable
  2512 000015C9 B025                <1> 	mov	al, 25h
  2513 000015CB B2D8                <1> 	mov	dl, 0D8h ; address control port
  2514 000015CD EE                  <1> 	out	dx, al	; turn off video during vertical retrace
  2515                              <1> 	;pop	dx	; #rows, #cols
  2516                              <1>        	;pop	ax	; offset
  2517                              <1> 	; 05/12/2021
  2518 000015CE 5A                  <1> 	pop	edx
  2519 000015CF 58                  <1> 	pop	eax
  2520 000015D0 6691                <1> 	xchg	ax, cx	; 
  2521                              <1> 	; ecx = offset, al = line count, ah = attribute
  2522                              <1> ;n9:
  2523 000015D2 08C0                <1> 	or	al, al
  2524 000015D4 7420                <1>         jz      short n3 
  2525 000015D6 01CE                <1>         add     esi, ecx ; from address for scroll
  2526 000015D8 88F7                <1> 	mov	bh, dh  ; #rows in block
  2527 000015DA 28C7                <1> 	sub	bh, al	; #rows to be moved
  2528                              <1> n2:
  2529                              <1> 	; Move rows
  2530 000015DC 88D1                <1> 	mov	cl, dl	; get # of cols to move
  2531 000015DE 56                  <1> 	push	esi
  2532 000015DF 57                  <1> 	push	edi	; save start address
  2533                              <1> n10:
  2534 000015E0 66A5                <1> 	movsw		; move that line on screen
  2535 000015E2 FEC9                <1> 	dec	cl
  2536 000015E4 75FA                <1>         jnz     short n10
  2537 000015E6 5F                  <1> 	pop	edi
  2538 000015E7 5E                  <1> 	pop	esi	; recover addresses
  2539                              <1>         ;mov    cl, [CRT_COLS] 
  2540                              <1> 	;add	cl, cl
  2541                              <1>         ;mov    ecx, 80*2
  2542 000015E8 66B9A000            <1>         mov     cx, 80*2
  2543 000015EC 01CE                <1>         add     esi, ecx  ; next line
  2544 000015EE 01CF                <1>         add     edi, ecx
  2545 000015F0 FECF                <1> 	dec	bh	 ; count of lines to move
  2546 000015F2 75E8                <1> 	jnz	short n2 ; row loop
  2547                              <1> 	; bh = 0
  2548 000015F4 88C6                <1> 	mov	dh, al	 ; #rows	
  2549                              <1> n3:
  2550                              <1> 	; attribute in ah
  2551 000015F6 B020                <1> 	mov	al, ' '	 ; fill with blanks
  2552                              <1> n3x:
  2553                              <1> 	; Clear rows
  2554                              <1>                 ; dh =  #rows
  2555 000015F8 88D1                <1>         mov	cl, dl	; get # of cols to clear
  2556 000015FA 57                  <1>         push    edi     ; save address
  2557                              <1> n11:
  2558 000015FB 66AB                <1>         stosw           ; store fill character
  2559 000015FD FEC9                <1> 	dec	cl
  2560 000015FF 75FA                <1>         jnz     short n11
  2561 00001601 5F                  <1>         pop     edi     ; recover address
  2562                              <1> 	;mov	cl, [CRT_COLS]
  2563                              <1> 	;add	cl, cl
  2564                              <1>         ;mov    ecx, 80*2
  2565 00001602 B1A0                <1>         mov	cl, 80*2
  2566 00001604 01CF                <1>         add     edi, ecx
  2567 00001606 FECE                <1> 	dec	dh
  2568 00001608 75EE                <1> 	jnz	short n3x ; 16/01/2016
  2569                              <1> 	;
  2570 0000160A 3A1D[E66F0000]      <1> 	cmp	bl, [active_page]
  2571 00001610 7507                <1> 	jne	short n6
  2572                              <1> 	;mov	al, [CRT_MODE_SET] ; get the value of mode set
  2573 00001612 B029                <1> 	mov	al, 29h ; (ORGS.ASM), M7 mode set table value for mode 3
  2574 00001614 66BAD803            <1> 	mov	dx, 03D8h ; always set color card port
  2575 00001618 EE                  <1> 	out	dx, al
  2576                              <1> n6:
  2577 00001619 C3                  <1> 	retn
  2578                              <1> 
  2579                              <1> 
  2580                              <1> write_c_current:
  2581                              <1> 	; 05/12/2021 (Retro UNIX 386 v1.2)
  2582                              <1> 	; 30/08/2014 (Retro UNIX 386 v1)
  2583                              <1> 	; 18/01/2014
  2584                              <1> 	; 04/12/2013
  2585                              <1> 	;
  2586                              <1> 	; VIDEO.ASM - 06/10/85  VIDEO DISPLAY BIOS
  2587                              <1> 	;
  2588                              <1> 	; WRITE_C_CURRENT
  2589                              <1> 	;	THIS ROUTINE WRITES THE CHARACTER AT
  2590                              <1> 	;	THE CURRENT CURSOR POSITION, ATTRIBUTE UNCHANGED
  2591                              <1> 	; INPUT	
  2592                              <1> 	;	(AH) = CURRENT CRT MODE
  2593                              <1> 	;	(BH) = DISPLAY PAGE
  2594                              <1> 	;	(CX) = COUNT OF CHARACTERS TO WRITE
  2595                              <1> 	;	(AL) = CHAR TO WRITE
  2596                              <1> 	;	(DS) = DATA SEGMENT
  2597                              <1> 	;	(ES) = REGEN SEGMENT
  2598                              <1> 	; OUTPUT
  2599                              <1> 	;	DISPLAY REGEN BUFFER UPDATED
  2600                              <1> 
  2601 0000161A FA                  <1> 	cli		
  2602                              <1> 	; bl = video page
  2603                              <1> 	; al = character
  2604                              <1> 	; ah = color/attribute
  2605                              <1> 	;push	dx
  2606                              <1> 	;push	ax	; save character & attribute/color
  2607                              <1> 	; 05/12/2021
  2608 0000161B 52                  <1> 	push	edx
  2609 0000161C 50                  <1> 	push	eax
  2610 0000161D E816FFFFFF          <1> 	call 	find_position  ; get regen location and port address
  2611                              <1> 	; esi = regen location
  2612                              <1> 	; dx = status port
  2613                              <1> 	;
  2614                              <1> 	; WAIT FOR HORIZONTAL RETRACE OR VERTICAL RETRACE
  2615                              <1> 	;
  2616                              <1> p41:			; wait for horizontal retrace is low or vertical
  2617 00001622 FB                  <1> 	sti		; enable interrupts first
  2618 00001623 3A1D[E66F0000]      <1>         cmp     bl, [active_page]
  2619 00001629 7510                <1> 	jne	short p44 
  2620 0000162B FA                  <1> 	cli 		; block interrupts for single loop
  2621 0000162C EC                  <1> 	in	al, dx	; get status from the adapter
  2622 0000162D A808                <1> 	test	al, RVRT ; check for vertical retrace first
  2623 0000162F 7509                <1> 	jnz	short p43 ; Do fast write now if vertical retrace
  2624 00001631 A801                <1> 	test	al, RHRZ ; is horizontal retrace low
  2625 00001633 75ED                <1> 	jnz	short p41 ; wait until it is
  2626                              <1> p42:			;  wait for either retrace high
  2627 00001635 EC                  <1> 	in	al, dx ; get status again
  2628 00001636 A809                <1> 	test	al, RVRT+RHRZ ; is horizontal or vertical retrace high
  2629 00001638 74FB                <1> 	jz	short p42 ; wait until either retrace active
  2630                              <1> p43:	
  2631 0000163A FB                  <1> 	sti
  2632                              <1> p44:
  2633                              <1> 	;pop	ax	; restore the character (al) & attribute (ah)
  2634                              <1> 	; 05/12/2021
  2635 0000163B 58                  <1> 	pop	eax
  2636 0000163C 81C600800B00        <1> 	add	esi, 0B8000h ; 30/08/2014 (crt_base) 
  2637                              <1> 				; Retro UNIX 386 v1 feature only!
  2638 00001642 668906              <1> 	mov	[esi], ax
  2639                              <1> 	;pop	dx
  2640                              <1> 	; 05/12/2021
  2641 00001645 5A                  <1> 	pop	edx
  2642 00001646 C3                  <1> 	retn
  2643                              <1> 
  2644                              <1> set_mode:
  2645                              <1> 	; 16/01/2016
  2646                              <1> 	; 02/09/2014 (Retro UNIX 386 v1)
  2647                              <1> 	;
  2648                              <1> 	; VIDEO.ASM - 06/10/85  VIDEO DISPLAY BIOS
  2649                              <1> 
  2650                              <1> ;------------------------------------------------------
  2651                              <1> ; SET MODE					      :
  2652                              <1> ;	THIS ROUTINE INITIALIZES THE ATTACHMENT TO    :
  2653                              <1> ;	THE SELECTED MODE, THE SCREEN IS BLANKED.     :
  2654                              <1> ; INPUT						      :
  2655                              <1> ;	(AL) - MODE SELECTED (RANGE 0-7)	      :
  2656                              <1> ; OUTPUT					      :
  2657                              <1> ;	NONE					      :
  2658                              <1> ;------------------------------------------------------
  2659                              <1> 
  2660 00001647 57                  <1> 	push	edi ; 16/01/2016
  2661 00001648 53                  <1> 	push	ebx
  2662 00001649 52                  <1> 	push	edx
  2663 0000164A 51                  <1> 	push	ecx ; 16/01/2016
  2664 0000164B 50                  <1>         push    eax
  2665                              <1> 
  2666                              <1> 	;mov	dx, 03D4h 	; address or color card
  2667 0000164C B003                <1> 	mov	al, 3
  2668                              <1> ;M8:
  2669 0000164E A2[E0690000]        <1> 	mov	[CRT_MODE], al  ; save mode in global variable
  2670 00001653 B029                <1> 	mov	al, 29h
  2671                              <1> 	;mov	[CRT_MODE_SET], al ; save the mode set value
  2672 00001655 2437                <1> 	and	al, 037h	; video off, save high resolution bit	
  2673                              <1> 	;push	dx  		; save port value
  2674                              <1> 	;add	dx, 4		; point to control register
  2675 00001657 66BAD803            <1> 	mov	dx, 3D8h
  2676 0000165B EE                  <1> 	out	dx, al		; reset video to off to suppress rolling
  2677                              <1> 	;pop	dx
  2678                              <1> ;M9:
  2679 0000165C BB[186A0000]        <1> 	mov	ebx, video_params ; initialization table
  2680                              <1> 	;mov	ax, [ebx+10]      ; get the cursor mode from the table	
  2681                              <1> 	;xchg 	ah, al
  2682                              <1> 	;mov	[CURSOR_MODE], ax ; save cursor mode
  2683 00001661 30E4                <1> 	xor	ah, ah		  ; ah is register number during loop 
  2684                              <1> 	
  2685                              <1> ;-----	LOOP THROUGH TABLE, OUTPUTTING REGISTER ADDRESS, THEN VALUE FROM TABLE
  2686 00001663 B910000000          <1> 	mov	ecx, 16 ; 16/01/2016
  2687                              <1> M10:			;  initialization loop
  2688 00001668 88E0                <1> 	mov	al, ah 	; get 6845 register number
  2689 0000166A EE                  <1> 	out	dx, al
  2690 0000166B 6642                <1> 	inc	dx      ; point to data port
  2691 0000166D FEC4                <1> 	inc	ah	; next register value
  2692 0000166F 8A03                <1> 	mov	al, [ebx] ; get table value
  2693 00001671 EE                  <1> 	out	dx, al	; out to chip
  2694 00001672 43                  <1> 	inc	ebx	; next in table
  2695 00001673 664A                <1> 	dec	dx	; back to pointer register
  2696 00001675 E2F1                <1> 	loop	M10	; do the whole table
  2697                              <1> 	
  2698                              <1> ;-----	FILL REGEN AREA WITH BLANK
  2699                              <1> 	;xor	ax, ax  
  2700                              <1> 	;mov	[CRT_START], ax  ; start address saved in global
  2701                              <1> 	;mov	[ACTIVE_PAGE], al ; 0 ; (re)set page value
  2702                              <1> 	;mov	ecx, 8192 ; number of words in color card
  2703                              <1> 	; black background, light gray characeter color, space character
  2704                              <1> 	;mov	ax, 0720h ; fill char for alpha - attribute
  2705                              <1> ;M13:			  ; clear buffer
  2706                              <1> 	;add	edi, 0B8000h ; [crt_base]
  2707                              <1> 	;rep	stosw	; FILL THE REGEN BUFFER WITH BLANKS
  2708                              <1> 
  2709                              <1> ;-----	ENABLE VIDEO AND CORRECT PORT SETTING
  2710                              <1> 	;mov	dx, 3D4h ; mov dx, word [ADDR_6845]
  2711                              <1> 			 ; prepare to output to video enable port
  2712                              <1> 	;add	dx,4	 ; point to the mode control gerister
  2713 00001677 66BAD803            <1> 	mov	dx, 3D8h
  2714                              <1> 	;mov	al, [CRT_MODE_SET] ; get the mode set value
  2715 0000167B B029                <1> 	mov	al, 29h
  2716 0000167D EE                  <1> 	out	dx, al	 ; set video enable port
  2717                              <1> 
  2718                              <1> ;----- 	DETERMINE NUMBER OF COLUMNS, BOTH FOR ENTIRE DISPLAY
  2719                              <1> ;----- 	AND THE NUMBER TO BE USED FOR TTY INTERFACE
  2720                              <1> 	;
  2721                              <1> 	;mov byte [CRT_COLS], 80h ; initialize number of columns count
  2722                              <1> 	;
  2723                              <1> ;-----	SET CURSOR POSITIONS
  2724                              <1> 	;push	edi
  2725                              <1> 	;mov	word [CRT_LEN], 80*25*2
  2726 0000167E BF[D66F0000]        <1> 	mov	edi, cursor_posn
  2727 00001683 B904000000          <1> 	mov	ecx, 4	; clear all cursor positions (16 bytes)
  2728 00001688 31C0                <1> 	xor	eax, eax
  2729 0000168A F3AB                <1> 	rep 	stosd	; fill with zeroes
  2730                              <1> 	;pop	edi
  2731                              <1> 
  2732                              <1> ;-----	SET UP OVERSCAN REGISTER
  2733 0000168C 6642                <1> 	inc	dx	; set overscan port to a default
  2734 0000168E B030                <1> 	mov	al, 30h	; 30H valuye for all modes except 640X200 bw
  2735                              <1> ;M14:
  2736 00001690 EE                  <1> 	out	dx, al	; output the correct value to 3D9 port
  2737                              <1> 	;mov	[CRT_PALETTE], al ; save the value for future use
  2738                              <1> 
  2739                              <1> ;-----	NORMAL RETURN FROM ALL VIDEO RETURNS
  2740                              <1> 	;
  2741 00001691 58                  <1> 	pop	eax
  2742 00001692 59                  <1> 	pop	ecx ; 16/01/2016
  2743 00001693 5A                  <1> 	pop	edx
  2744 00001694 5B                  <1> 	pop	ebx
  2745 00001695 5F                  <1> 	pop	edi ; 16/01/2016
  2746 00001696 C3                  <1> 	retn
  2747                              <1> 	
  2748                              <1> tty_sw:
  2749                              <1> 	; 05/12/2021 (Retro UNIX 386 v1.2)
  2750                              <1> 	; 30/06/2015
  2751                              <1> 	; 27/06/2015 
  2752                              <1> 	; 07/09/2014
  2753                              <1> 	; 02/09/2014 (Retro UNIX 386 v1 - beginning)
  2754                              <1> 	;
  2755                              <1> 	; (Modified registers : EAX)
  2756                              <1> 	;
  2757                              <1>         ;mov     byte [u.quant], 0  ; 04/03/2014
  2758                              <1> 	;
  2759                              <1> ;act_disp_page:
  2760                              <1> 	; 30/06/2015
  2761                              <1> 	; 04/03/2014  (act_disp_page --> tty_sw)
  2762                              <1> 	; 10/12/2013
  2763                              <1> 	; 04/12/2013
  2764                              <1> 	;
  2765                              <1> 	; VIDEO.ASM - 06/10/85  VIDEO DISPLAY BIOS
  2766                              <1> 	;
  2767                              <1> 	; ACT_DISP_PAGE
  2768                              <1> 	;	THIS ROUTINE SETS THE ACTIVE DISPLAY PAGE, ALLOWING
  2769                              <1> 	;	THE FULL USE OF THE MEMORY SET ASIDE FOR THE VIDEO ATTACHMENT
  2770                              <1> 	; INPUT
  2771                              <1> 	;	AL HAS THE NEW ACTIVE DISPLAY PAGE
  2772                              <1> 	; OUTPUT
  2773                              <1> 	;	THE 6845 IS RESET TO DISPLAY THAT PAGE
  2774                              <1> 
  2775                              <1> 	;cli
  2776                              <1> 
  2777 00001697 53                  <1> 	push	ebx
  2778                              <1> 	;push	cx
  2779                              <1> 	;push	dx
  2780                              <1> 	; 05/12/2021
  2781 00001698 51                  <1> 	push	ecx
  2782 00001699 52                  <1> 	push	edx
  2783                              <1> 	;
  2784 0000169A A2[E66F0000]        <1> 	mov	[active_page], al ; save active page value ; [ptty]
  2785                              <1> 	;mov	cx, [CRT_LEN] ; get saved length of regen buffer
  2786 0000169F 66B9A00F            <1> 	mov	cx, 25*80*2
  2787                              <1> 	; 27/06/2015
  2788 000016A3 0FB6D8              <1> 	movzx	ebx, al
  2789                              <1> 	;
  2790 000016A6 6698                <1> 	cbw	; 07/09/2014 (ah=0)
  2791 000016A8 66F7E1              <1> 	mul 	cx	; display page times regen length
  2792                              <1> 	; 10/12/2013
  2793 000016AB 66A3[D46F0000]      <1> 	mov	[CRT_START], ax ; save start address for later
  2794 000016B1 6689C1              <1> 	mov	cx, ax ; start address to cx
  2795                              <1> 	;sar	cx, 1
  2796 000016B4 66D1E9              <1> 	shr	cx, 1	; divide by 2 for 6845 handling
  2797 000016B7 B40C                <1> 	mov	ah, 12	; 6845 register for start address
  2798 000016B9 E849FEFFFF          <1> 	call	m16
  2799                              <1> 	;sal	bx, 1
  2800                              <1> 	; 01/09/2014
  2801 000016BE D0E3                <1> 	shl	bl, 1	; *2 for word offset
  2802 000016C0 81C3[D66F0000]      <1> 	add	ebx, cursor_posn
  2803 000016C6 668B13              <1> 	mov	dx, [ebx] ; get cursor for this page
  2804 000016C9 E825FEFFFF          <1> 	call	m18
  2805                              <1> 	;
  2806                              <1> 	;pop	dx
  2807                              <1> 	;pop	cx
  2808                              <1> 	; 05/12/2021
  2809 000016CE 5A                  <1> 	pop	edx
  2810 000016CF 59                  <1> 	pop	ecx
  2811 000016D0 5B                  <1> 	pop	ebx
  2812                              <1> 	;
  2813                              <1> 	;sti
  2814                              <1> 	;
  2815 000016D1 C3                  <1> 	retn
  2816                              <1> 
  2817                              <1> ; % include 'vidata.inc' ; VIDEO DATA ; 11/03/2015
  2818                              <1> 
  2819                              <1> ; /// End Of VIDEO FUNCTIONS ///
  1850                                  
  1851                                  setup_rtc_int:
  1852                                  ; source: http://wiki.osdev.org/RTC
  1853 000016D2 FA                      	cli		; disable interrupts
  1854                                  	; default int frequency is 1024 Hz (Lower 4 bits of register A is 0110b or 6)
  1855                                  	; in order to change this ...
  1856                                  	; frequency = 32768 >> (rate-1) --> 32768 >> 5 = 1024
  1857                                  	; (rate must be above 2 and not over 15)
  1858                                  	; new rate = 15 --> 32768 >> (15-1) = 2 Hz
  1859 000016D3 B08A                    	mov	al, 8Ah 
  1860 000016D5 E670                    	out	70h, al ; set index to register A, disable NMI
  1861 000016D7 90                      	nop
  1862 000016D8 E471                    	in	al, 71h ; get initial value of register A
  1863 000016DA 88C4                    	mov 	ah, al
  1864 000016DC 80E4F0                  	and	ah, 0F0h
  1865 000016DF B08A                    	mov	al, 8Ah 
  1866 000016E1 E670                    	out	70h, al ; reset index to register A
  1867 000016E3 88E0                    	mov	al, ah
  1868 000016E5 0C0F                    	or	al, 0Fh	; new rate (0Fh -> 15)
  1869 000016E7 E671                    	out	71h, al ; write only our rate to A. Note, rate is the bottom 4 bits. 
  1870                                  	; enable RTC interrupt
  1871 000016E9 B08B                    	mov	al, 8Bh ;
  1872 000016EB E670                    	out	70h, al ; select register B and disable NMI
  1873 000016ED 90                      	nop
  1874 000016EE E471                    	in	al, 71h ; read the current value of register B
  1875 000016F0 88C4                    	mov	ah, al  ;
  1876 000016F2 B08B                    	mov 	al, 8Bh ;
  1877 000016F4 E670                    	out	70h, al ; set the index again (a read will reset the index to register B)	
  1878 000016F6 88E0                    	mov	al, ah  ;
  1879 000016F8 0C40                    	or	al, 40h ;
  1880 000016FA E671                    	out	71h, al ; write the previous value ORed with 0x40. This turns on bit 6 of register B
  1881 000016FC FB                      	sti
  1882 000016FD C3                      	retn
  1883                                  
  1884                                  ; Write memory information
  1885                                  ; Temporary Code
  1886                                  ; 06/11/2014
  1887                                  ; 14/08/2015 
  1888                                  memory_info:	
  1889 000016FE A1[BC6F0000]            	mov	eax, [memory_size] ; in pages
  1890 00001703 50                      	push	eax
  1891 00001704 C1E00C                  	shl	eax, 12		   ; in bytes
  1892 00001707 BB0A000000              	mov	ebx, 10
  1893 0000170C 89D9                    	mov	ecx, ebx	   ; 10
  1894 0000170E BE[216C0000]            	mov	esi, mem_total_b_str	
  1895 00001713 E8AE000000              	call	bintdstr
  1896 00001718 58                      	pop	eax
  1897 00001719 B107                    	mov	cl, 7
  1898 0000171B BE[456C0000]            	mov	esi, mem_total_p_str
  1899 00001720 E8A1000000              	call	bintdstr	
  1900                                  	; 14/08/2015
  1901 00001725 E8B9000000              	call	calc_free_mem
  1902                                  	; edx = calculated free pages
  1903                                  	; ecx = 0
  1904 0000172A A1[C06F0000]            	mov 	eax, [free_pages]
  1905 0000172F 39D0                    	cmp	eax, edx ; calculated free mem value 
  1906                                  		; and initial free mem value are same or not?
  1907 00001731 751D                    	jne 	short pmim ; print mem info with '?' if not
  1908 00001733 52                      	push 	edx ; free memory in pages	
  1909                                  	;mov 	eax, edx
  1910 00001734 C1E00C                  	shl	eax, 12 ; convert page count
  1911                                  			; to byte count
  1912 00001737 B10A                    	mov	cl, 10
  1913 00001739 BE[656C0000]            	mov	esi, free_mem_b_str
  1914 0000173E E883000000              	call	bintdstr
  1915 00001743 58                      	pop	eax
  1916 00001744 B107                    	mov	cl, 7
  1917 00001746 BE[896C0000]            	mov	esi, free_mem_p_str
  1918 0000174B E876000000              	call	bintdstr
  1919                                  pmim:
  1920 00001750 BE[0F6C0000]            	mov	esi, msg_memory_info
  1921                                  pmim_nb:	
  1922 00001755 AC                      	lodsb
  1923 00001756 08C0                    	or	al, al
  1924 00001758 740D                    	jz	short pmim_ok
  1925 0000175A 56                      	push	esi
  1926 0000175B 31DB                    	xor	ebx, ebx ; 0
  1927                                  			; Video page 0 (bl=0)
  1928 0000175D B407                    	mov	ah, 07h ; Black background, 
  1929                                  			; light gray forecolor
  1930 0000175F E856FCFFFF              	call	write_tty
  1931 00001764 5E                      	pop	esi
  1932 00001765 EBEE                    	jmp	short pmim_nb
  1933                                  pmim_ok:
  1934 00001767 C3                      	retn
  1935                                  
  1936                                  ; Convert binary number to hexadecimal string
  1937                                  ; 10/05/2015  
  1938                                  ; dsectpm.s (28/02/2015)
  1939                                  ; Retro UNIX 386 v1 - Kernel v0.2.0.6  
  1940                                  ; 01/12/2014
  1941                                  ; 25/11/2014
  1942                                  ;
  1943                                  bytetohex:
  1944                                  	; INPUT ->
  1945                                  	; 	AL = byte (binary number)
  1946                                  	; OUTPUT ->
  1947                                  	;	AX = hexadecimal string
  1948                                  	;
  1949 00001768 53                      	push	ebx
  1950 00001769 31DB                    	xor	ebx, ebx
  1951 0000176B 88C3                    	mov	bl, al
  1952 0000176D C0EB04                  	shr	bl, 4
  1953 00001770 8A9B[B6170000]          	mov	bl, [ebx+hexchrs]
  1954 00001776 86D8                    	xchg	bl, al
  1955 00001778 80E30F                  	and	bl, 0Fh
  1956 0000177B 8AA3[B6170000]          	mov	ah, [ebx+hexchrs]
  1957 00001781 5B                      	pop	ebx	
  1958 00001782 C3                      	retn
  1959                                  
  1960                                  wordtohex:
  1961                                  	; INPUT ->
  1962                                  	; 	AX = word (binary number)
  1963                                  	; OUTPUT ->
  1964                                  	;	EAX = hexadecimal string
  1965                                  	;
  1966 00001783 53                      	push	ebx
  1967 00001784 31DB                    	xor	ebx, ebx
  1968 00001786 86E0                    	xchg	ah, al
  1969 00001788 6650                    	push	ax
  1970 0000178A 88E3                    	mov	bl, ah
  1971 0000178C C0EB04                  	shr	bl, 4
  1972 0000178F 8A83[B6170000]          	mov	al, [ebx+hexchrs]
  1973 00001795 88E3                    	mov	bl, ah
  1974 00001797 80E30F                  	and	bl, 0Fh
  1975 0000179A 8AA3[B6170000]          	mov	ah, [ebx+hexchrs]
  1976 000017A0 C1E010                  	shl	eax, 16
  1977 000017A3 6658                    	pop	ax
  1978 000017A5 5B                      	pop	ebx
  1979 000017A6 EBC0                    	jmp	short bytetohex
  1980                                  	;mov	bl, al
  1981                                  	;shr	bl, 4
  1982                                  	;mov	bl, [ebx+hexchrs]
  1983                                  	;xchg	bl, al	 	
  1984                                  	;and	bl, 0Fh
  1985                                  	;mov	ah, [ebx+hexchrs]
  1986                                  	;pop	ebx	
  1987                                  	;retn
  1988                                  
  1989                                  dwordtohex:
  1990                                  	; INPUT ->
  1991                                  	; 	EAX = dword (binary number)
  1992                                  	; OUTPUT ->
  1993                                  	;	EDX:EAX = hexadecimal string
  1994                                  	;
  1995 000017A8 50                      	push	eax
  1996 000017A9 C1E810                  	shr	eax, 16
  1997 000017AC E8D2FFFFFF              	call	wordtohex
  1998 000017B1 89C2                    	mov	edx, eax
  1999 000017B3 58                      	pop	eax
  2000                                  	;call	wordtohex
  2001                                  	;retn
  2002                                  	; 02/01/2022
  2003 000017B4 EBCD                    	jmp	short wordtohex
  2004                                  
  2005                                  ; 10/05/2015
  2006                                  hex_digits:
  2007                                  hexchrs:
  2008 000017B6 303132333435363738-     	db '0123456789ABCDEF'
  2008 000017BF 39414243444546     
  2009                                  
  2010                                  ; Convert binary number to decimal/numeric string
  2011                                  ; 06/11/2014
  2012                                  ; Temporary Code
  2013                                  ;
  2014                                  
  2015                                  bintdstr:
  2016                                  	; EAX = binary number
  2017                                  	; ESI = decimal/numeric string address
  2018                                  	; EBX = divisor (10)
  2019                                  	; ECX = string length (<=10)
  2020 000017C6 01CE                    	add	esi, ecx
  2021                                  btdstr0:
  2022 000017C8 4E                      	dec	esi
  2023 000017C9 31D2                    	xor	edx, edx
  2024 000017CB F7F3                    	div	ebx
  2025 000017CD 80C230                  	add	dl, 30h
  2026 000017D0 8816                    	mov	[esi], dl
  2027 000017D2 FEC9                    	dec	cl
  2028 000017D4 740C                    	jz	short btdstr2 ; 02/01/2022 (short jump)
  2029 000017D6 09C0                    	or	eax, eax
  2030 000017D8 75EE                    	jnz	short btdstr0
  2031                                  btdstr1:
  2032 000017DA 4E                      	dec	esi
  2033 000017DB C60620                          mov     byte [esi], 20h ; blank space
  2034 000017DE FEC9                    	dec	cl
  2035 000017E0 75F8                    	jnz	short btdstr1
  2036                                  btdstr2:
  2037 000017E2 C3                      	retn
  2038                                  
  2039                                  ; Calculate free memory pages on M.A.T.
  2040                                  ; 06/11/2014
  2041                                  ; Temporary Code
  2042                                  ;
  2043                                  
  2044                                  calc_free_mem:
  2045 000017E3 31D2                    	xor	edx, edx
  2046                                  	;xor	ecx, ecx
  2047                                  	;mov	cx, [mat_size] ; in pages
  2048                                  	; 02/01/2022
  2049 000017E5 8B0D[D06F0000]          	mov	ecx, [mat_size] ; in pages
  2050 000017EB C1E10A                  	shl	ecx, 10	; 1024 dwords per page
  2051 000017EE BE00001000              	mov	esi, MEM_ALLOC_TBL
  2052                                  cfm0:
  2053 000017F3 AD                      	lodsd
  2054 000017F4 51                      	push	ecx
  2055 000017F5 B920000000              	mov	ecx, 32
  2056                                  cfm1:
  2057 000017FA D1E8                    	shr	eax, 1
  2058 000017FC 7301                    	jnc	short cfm2
  2059 000017FE 42                      	inc	edx
  2060                                  cfm2:
  2061 000017FF E2F9                    	loop	cfm1
  2062 00001801 59                      	pop	ecx
  2063 00001802 E2EF                    	loop	cfm0
  2064 00001804 C3                      	retn
  2065                                  
  2066                                  %include 'diskio.s'  ; 07/03/2015
  2067                              <1> ; Temporary Runix kernel v2.0 file for debug - 22/11/2021
  2068                              <1> ; (re-write kernel for test by using previous version without a major defect)
  2069                              <1> ; ****************************************************************************
  2070                              <1> ; Retro UNIX 386 v1 Kernel - DISKIO.INC
  2071                              <1> ; Last Modification: 05/12/2021
  2072                              <1> ;
  2073                              <1> ; 	(Initialized Disk Parameters Data is in 'DISKDATA.INC') 
  2074                              <1> ; 	(Uninitialized Disk Parameters Data is in 'DISKBSS.INC') 
  2075                              <1> 
  2076                              <1> ; DISK I/O SYSTEM - Erdogan Tan (Retro UNIX 386 v1 project)
  2077                              <1> 
  2078                              <1> ; ///////// DISK I/O SYSTEM ///////////////
  2079                              <1> 
  2080                              <1> ; 06/02/2015
  2081                              <1> diskette_io:
  2082 00001805 9C                  <1> 	pushfd
  2083 00001806 0E                  <1> 	push 	cs
  2084 00001807 E809000000          <1> 	call 	DISKETTE_IO_1
  2085 0000180C C3                  <1> 	retn
  2086                              <1> 	
  2087                              <1> ;;;;;; DISKETTE I/O ;;;;;;;;;;;;;;;;;;;; 06/02/2015 ;;;
  2088                              <1> ;//////////////////////////////////////////////////////
  2089                              <1> 
  2090                              <1> ; DISKETTE I/O - Erdogan Tan (Retro UNIX 386 v1 project)
  2091                              <1> ; 20/02/2015
  2092                              <1> ; 06/02/2015 (unix386.s)
  2093                              <1> ; 16/12/2014 - 02/01/2015 (dsectrm2.s)
  2094                              <1> ;
  2095                              <1> ; Code (DELAY) modifications - AWARD BIOS 1999 (ADISK.EQU, COMMON.MAC)
  2096                              <1> ;
  2097                              <1> ; ADISK.EQU
  2098                              <1> 
  2099                              <1> ;----- Wait control constants 
  2100                              <1> 
  2101                              <1> ;amount of time to wait while RESET is active.
  2102                              <1> 
  2103                              <1> WAITCPU_RESET_ON   EQU	21		;Reset on must last at least 14us
  2104                              <1> 					;at 250 KBS xfer rate.
  2105                              <1> 					;see INTEL MCS, 1985, pg. 5-456
  2106                              <1> 
  2107                              <1> WAITCPU_FOR_STATUS EQU	100		;allow 30 microseconds for
  2108                              <1> 					;status register to become valid
  2109                              <1> 					;before re-reading.
  2110                              <1> 
  2111                              <1> ;After sending a byte to NEC, status register may remain
  2112                              <1> ;incorrectly set for 24 us.
  2113                              <1> 
  2114                              <1> WAITCPU_RQM_LOW	   EQU	24		;number of loops to check for
  2115                              <1> 					;RQM low.
  2116                              <1> 
  2117                              <1> ; COMMON.MAC
  2118                              <1> ;
  2119                              <1> ;	Timing macros
  2120                              <1> ;
  2121                              <1> 
  2122                              <1> %macro 		SIODELAY 0 		; SHORT IODELAY
  2123                              <1> 		jmp short $+2
  2124                              <1> %endmacro		
  2125                              <1> 
  2126                              <1> %macro		IODELAY  0		; NORMAL IODELAY
  2127                              <1> 		jmp short $+2
  2128                              <1> 		jmp short $+2
  2129                              <1> %endmacro
  2130                              <1> 
  2131                              <1> %macro		NEWIODELAY 0
  2132                              <1> 		out	0EBh,al
  2133                              <1> %endmacro 
  2134                              <1> 
  2135                              <1> ; (According to) AWARD BIOS 1999 - ATORGS.ASM (dw -> equ, db -> equ)
  2136                              <1> ;;; WAIT_FOR_MEM
  2137                              <1> ;WAIT_FDU_INT_LO	equ	017798		; 2.5 secs in 30 micro units.
  2138                              <1> ;WAIT_FDU_INT_HI	equ	1
  2139                              <1> WAIT_FDU_INT_LH		equ	83334		; 27/02/2015 (2.5 seconds waiting)
  2140                              <1> ;;; WAIT_FOR_PORT
  2141                              <1> ;WAIT_FDU_SEND_LO	equ	16667		; .5 secons in 30 us units.
  2142                              <1> ;WAIT_FDU_SEND_HI	equ	0
  2143                              <1> WAIT_FDU_SEND_LH	equ 	16667		; 27/02/2015	
  2144                              <1> ;Time to wait while waiting for each byte of NEC results = .5
  2145                              <1> ;seconds.  .5 seconds = 500,000 micros.  500,000/30 = 16,667.
  2146                              <1> ;WAIT_FDU_RESULTS_LO	equ	16667		; .5 seconds in 30 micro units.
  2147                              <1> ;WAIT_FDU_RESULTS_HI	equ	0
  2148                              <1> WAIT_FDU_RESULTS_LH	equ	16667  ; 27/02/2015
  2149                              <1> ;;; WAIT_REFRESH
  2150                              <1> ;amount of time to wait for head settle, per unit in parameter
  2151                              <1> ;table = 1 ms.
  2152                              <1> WAIT_FDU_HEAD_SETTLE	equ	33		; 1 ms in 30 micro units.
  2153                              <1> 
  2154                              <1> 
  2155                              <1> ; //////////////// DISKETTE I/O ////////////////
  2156                              <1> 
  2157                              <1> ; 11/12/2014 (copy from IBM PC-XT Model 286 BIOS - POSTEQU.INC)
  2158                              <1> 
  2159                              <1> ;----------------------------------------
  2160                              <1> ;	EQUATES USED BY POST AND BIOS	:
  2161                              <1> ;----------------------------------------
  2162                              <1> 
  2163                              <1> ;--------- 8042 KEYBOARD INTERFACE AND DIAGNOSTIC CONTROL REGISTERS ------------
  2164                              <1> ;PORT_A		EQU	060H		; 8042 KEYBOARD SCAN CODE/CONTROL PORT
  2165                              <1> ;PORT_B		EQU	061H		; PORT B READ/WRITE DIAGNOSTIC REGISTER
  2166                              <1> ;REFRESH_BIT	EQU	00010000B	; REFRESH TEST BIT
  2167                              <1> 
  2168                              <1> ;----------------------------------------
  2169                              <1> ;	CMOS EQUATES FOR THIS SYSTEM	:
  2170                              <1> ;-------------------------------------------------------------------------------
  2171                              <1> ;CMOS_PORT	EQU	070H		; I/O ADDRESS OF CMOS ADDRESS PORT
  2172                              <1> ;CMOS_DATA	EQU	071H		; I/O ADDRESS OF CMOS DATA PORT
  2173                              <1> ;NMI		EQU	10000000B	; DISABLE NMI INTERRUPTS MASK -
  2174                              <1> 					;  HIGH BIT OF CMOS LOCATION ADDRESS
  2175                              <1> 
  2176                              <1> ;---------- CMOS TABLE LOCATION ADDRESS'S ## -----------------------------------
  2177                              <1> CMOS_DISKETTE	EQU	010H		; DISKETTE DRIVE TYPE BYTE	      ;
  2178                              <1> ;		EQU	011H		; - RESERVED			      ;C
  2179                              <1> CMOS_DISK	EQU	012H		; FIXED DISK TYPE BYTE		      ;H
  2180                              <1> ;		EQU	013H		; - RESERVED			      ;E
  2181                              <1> CMOS_EQUIP	EQU	014H		; EQUIPMENT WORD LOW BYTE	      ;C
  2182                              <1> 
  2183                              <1> ;---------- DISKETTE EQUATES ---------------------------------------------------
  2184                              <1> INT_FLAG	EQU	10000000B	; INTERRUPT OCCURRENCE FLAG
  2185                              <1> DSK_CHG 	EQU	10000000B	; DISKETTE CHANGE FLAG MASK BIT
  2186                              <1> DETERMINED	EQU	00010000B	; SET STATE DETERMINED IN STATE BITS
  2187                              <1> HOME		EQU	00010000B	; TRACK 0 MASK
  2188                              <1> SENSE_DRV_ST	EQU	00000100B	; SENSE DRIVE STATUS COMMAND
  2189                              <1> TRK_SLAP	EQU	030H		; CRASH STOP (48 TPI DRIVES)
  2190                              <1> QUIET_SEEK	EQU	00AH		; SEEK TO TRACK 10
  2191                              <1> ;MAX_DRV 	EQU	2		; MAX NUMBER OF DRIVES
  2192                              <1> HD12_SETTLE	EQU	15		; 1.2 M HEAD SETTLE TIME
  2193                              <1> HD320_SETTLE	EQU	20		; 320 K HEAD SETTLE TIME
  2194                              <1> MOTOR_WAIT	EQU	37		; 2 SECONDS OF COUNTS FOR MOTOR TURN OFF
  2195                              <1> 
  2196                              <1> ;---------- DISKETTE ERRORS ----------------------------------------------------
  2197                              <1> ;TIME_OUT	EQU	080H		; ATTACHMENT FAILED TO RESPOND
  2198                              <1> ;BAD_SEEK	EQU	040H		; SEEK OPERATION FAILED
  2199                              <1> BAD_NEC 	EQU	020H		; DISKETTE CONTROLLER HAS FAILED
  2200                              <1> BAD_CRC 	EQU	010H		; BAD CRC ON DISKETTE READ
  2201                              <1> MED_NOT_FND	EQU	00CH		; MEDIA TYPE NOT FOUND
  2202                              <1> DMA_BOUNDARY	EQU	009H		; ATTEMPT TO DMA ACROSS 64K BOUNDARY
  2203                              <1> BAD_DMA 	EQU	008H		; DMA OVERRUN ON OPERATION
  2204                              <1> MEDIA_CHANGE	EQU	006H		; MEDIA REMOVED ON DUAL ATTACH CARD
  2205                              <1> RECORD_NOT_FND	EQU	004H		; REQUESTED SECTOR NOT FOUND
  2206                              <1> WRITE_PROTECT	EQU	003H		; WRITE ATTEMPTED ON WRITE PROTECT DISK
  2207                              <1> BAD_ADDR_MARK	EQU	002H		; ADDRESS MARK NOT FOUND
  2208                              <1> BAD_CMD 	EQU	001H		; BAD COMMAND PASSED TO DISKETTE I/O
  2209                              <1> 
  2210                              <1> ;---------- DISK CHANGE LINE EQUATES -------------------------------------------
  2211                              <1> NOCHGLN 	EQU	001H		; NO DISK CHANGE LINE AVAILABLE
  2212                              <1> CHGLN		EQU	002H		; DISK CHANGE LINE AVAILABLE
  2213                              <1> 
  2214                              <1> ;---------- MEDIA/DRIVE STATE INDICATORS ---------------------------------------
  2215                              <1> TRK_CAPA	EQU	00000001B	; 80 TRACK CAPABILITY
  2216                              <1> FMT_CAPA	EQU	00000010B	; MULTIPLE FORMAT CAPABILITY (1.2M)
  2217                              <1> DRV_DET 	EQU	00000100B	; DRIVE DETERMINED
  2218                              <1> MED_DET 	EQU	00010000B	; MEDIA DETERMINED BIT
  2219                              <1> DBL_STEP	EQU	00100000B	; DOUBLE STEP BIT
  2220                              <1> RATE_MSK	EQU	11000000B	; MASK FOR CLEARING ALL BUT RATE
  2221                              <1> RATE_500	EQU	00000000B	; 500 KBS DATA RATE
  2222                              <1> RATE_300	EQU	01000000B	; 300 KBS DATA RATE
  2223                              <1> RATE_250	EQU	10000000B	; 250 KBS DATA RATE
  2224                              <1> STRT_MSK	EQU	00001100B	; OPERATION START RATE MASK
  2225                              <1> SEND_MSK	EQU	11000000B	; MASK FOR SEND RATE BITS
  2226                              <1> 
  2227                              <1> ;---------- MEDIA/DRIVE STATE INDICATORS COMPATIBILITY -------------------------
  2228                              <1> M3D3U		EQU	00000000B	; 360 MEDIA/DRIVE NOT ESTABLISHED
  2229                              <1> M3D1U		EQU	00000001B	; 360 MEDIA,1.2DRIVE NOT ESTABLISHED
  2230                              <1> M1D1U		EQU	00000010B	; 1.2 MEDIA/DRIVE NOT ESTABLISHED
  2231                              <1> MED_UNK 	EQU	00000111B	; NONE OF THE ABOVE
  2232                              <1> 
  2233                              <1> ;---------- INTERRUPT EQUATES --------------------------------------------------
  2234                              <1> ;EOI		EQU	020H		; END OF INTERRUPT COMMAND TO 8259
  2235                              <1> ;INTA00		EQU	020H		; 8259 PORT
  2236                              <1> INTA01		EQU	021H		; 8259 PORT
  2237                              <1> INTB00		EQU	0A0H		; 2ND 8259
  2238                              <1> INTB01		EQU	0A1H		;
  2239                              <1> 
  2240                              <1> ;-------------------------------------------------------------------------------
  2241                              <1> DMA08		EQU	008H		; DMA STATUS REGISTER PORT ADDRESS
  2242                              <1> DMA		EQU	000H		; DMA CH.0 ADDRESS REGISTER PORT ADDRESS
  2243                              <1> DMA18		EQU	0D0H		; 2ND DMA STATUS PORT ADDRESS
  2244                              <1> DMA1		EQU	0C0H		; 2ND DMA CH.0 ADDRESS REGISTER ADDRESS
  2245                              <1> ;-------------------------------------------------------------------------------
  2246                              <1> ;TIMER		EQU	040H		; 8254 TIMER - BASE ADDRESS
  2247                              <1> 
  2248                              <1> ;-------------------------------------------------------------------------------
  2249                              <1> DMA_PAGE	EQU	081H		; START OF DMA PAGE REGISTERS
  2250                              <1> 
  2251                              <1> ; 06/02/2015 (unix386.s, protected mode modifications)
  2252                              <1> ; (unix386.s <-- dsectrm2.s)
  2253                              <1> ; 11/12/2014 (copy from IBM PC-XT Model 286 BIOS - DSEG.INC)
  2254                              <1> 
  2255                              <1> ; 10/12/2014
  2256                              <1> ;
  2257                              <1> ;int40h:
  2258                              <1> ;	pushf
  2259                              <1> ;	push 	cs
  2260                              <1> ;	;cli
  2261                              <1> ;	call 	DISKETTE_IO_1
  2262                              <1> ;	retn
  2263                              <1> 
  2264                              <1> ; DSKETTE ----- 04/21/86 DISKETTE BIOS
  2265                              <1> ; (IBM PC XT Model 286 System BIOS Source Code, 04-21-86)
  2266                              <1> ;
  2267                              <1> 
  2268                              <1> ;-- INT13H ---------------------------------------------------------------------
  2269                              <1> ; DISKETTE I/O
  2270                              <1> ;	THIS INTERFACE PROVIDES ACCESS TO THE 5 1/4 INCH 360 KB,
  2271                              <1> ;	1.2 MB, 720 KB AND 1.44 MB DISKETTE DRIVES.
  2272                              <1> ; INPUT
  2273                              <1> ;	(AH) =  00H RESET DISKETTE SYSTEM
  2274                              <1> ;		HARD RESET TO NEC, PREPARE COMMAND, RECALIBRATE REQUIRED
  2275                              <1> ;		ON ALL DRIVES
  2276                              <1> ;------------------------------------------------------------------------------- 
  2277                              <1> ;	(AH)= 01H  READ THE STATUS OF THE SYSTEM INTO (AH)
  2278                              <1> ;		@DISKETTE_STATUS FROM LAST OPERATION IS USED
  2279                              <1> ;-------------------------------------------------------------------------------
  2280                              <1> ;	REGISTERS FOR READ/WRITE/VERIFY/FORMAT
  2281                              <1> ;	(DL) - DRIVE NUMBER (0-1 ALLOWED, VALUE CHECKED)
  2282                              <1> ;	(DH) - HEAD NUMBER (0-1 ALLOWED, NOT VALUE CHECKED)
  2283                              <1> ;	(CH) - TRACK NUMBER (NOT VALUE CHECKED)
  2284                              <1> ;		MEDIA	DRIVE	TRACK NUMBER
  2285                              <1> ;		320/360	320/360	    0-39
  2286                              <1> ;		320/360	1.2M	    0-39
  2287                              <1> ;		1.2M	1.2M	    0-79
  2288                              <1> ;		720K	720K	    0-79
  2289                              <1> ;		1.44M	1.44M	    0-79	
  2290                              <1> ;	(CL) - 	SECTOR NUMBER (NOT VALUE CHECKED, NOT USED FOR FORMAT)
  2291                              <1> ;		MEDIA	DRIVE	SECTOR NUMBER
  2292                              <1> ;		320/360	320/360	     1-8/9
  2293                              <1> ;		320/360	1.2M	     1-8/9
  2294                              <1> ;		1.2M	1.2M	     1-15
  2295                              <1> ;		720K	720K	     1-9
  2296                              <1> ;		1.44M	1.44M	     1-18		
  2297                              <1> ;	(AL)	NUMBER OF SECTORS (NOT VALUE CHECKED)
  2298                              <1> ;		MEDIA	DRIVE	MAX NUMBER OF SECTORS
  2299                              <1> ;		320/360	320/360	        8/9
  2300                              <1> ;		320/360	1.2M	        8/9
  2301                              <1> ;		1.2M	1.2M		15
  2302                              <1> ;		720K	720K		9
  2303                              <1> ;		1.44M	1.44M		18
  2304                              <1> ;
  2305                              <1> ;	(ES:BX) - ADDRESS OF BUFFER (NOT REQUIRED FOR VERIFY)
  2306                              <1> ;
  2307                              <1> ;-------------------------------------------------------------------------------
  2308                              <1> ;	(AH)= 02H  READ THE DESIRED SECTORS INTO MEMORY
  2309                              <1> ;-------------------------------------------------------------------------------
  2310                              <1> ;	(AH)= 03H  WRITE THE DESIRED SECTORS FROM MEMORY
  2311                              <1> ;-------------------------------------------------------------------------------
  2312                              <1> ;	(AH)= 04H  VERIFY THE DESIRED SECTORS
  2313                              <1> ;-------------------------------------------------------------------------------
  2314                              <1> ;	(AH)= 05H  FORMAT THE DESIRED TRACK
  2315                              <1> ;		(ES,BX) MUST POINT TO THE COLLECTION OF DESIRED ADDRESS FIELDS
  2316                              <1> ;		FOR THE	TRACK. EACH FIELD IS COMPOSED OF 4 BYTES, (C,H,R,N),
  2317                              <1> ;		WHERE C = TRACK NUMBER, H=HEAD NUMBER, R = SECTOR NUMBER, 
  2318                              <1> ;		N= NUMBER OF BYTES PER SECTOR (00=128,01=256,02=512,03=1024),
  2319                              <1> ;		THERE MUST BE ONE ENTRY FOR EVERY SECTOR ON THE TRACK.
  2320                              <1> ;		THIS INFORMATION IS USED TO FIND THE REQUESTED SECTOR DURING 
  2321                              <1> ;		READ/WRITE ACCESS.
  2322                              <1> ;		PRIOR TO FORMATTING A DISKETTE, IF THERE EXISTS MORE THAN
  2323                              <1> ;		ONE SUPPORTED MEDIA FORMAT TYPE WITHIN THE DRIVE IN QUESTION,
  2324                              <1> ;		THEN "SET DASD TYPE" (INT 13H, AH = 17H) OR 'SET MEDIA TYPE'
  2325                              <1> ;		(INT 13H, AH =  18H) MUST BE CALLED TO SET THE DISKETTE TYPE
  2326                              <1> ;		THAT IS TO BE FORMATTED. IF "SET DASD TYPE" OR "SET MEDIA TYPE"
  2327                              <1> ;		IS NOT CALLED, THE FORMAT ROUTINE WILL ASSUME THE 
  2328                              <1> ;		MEDIA FORMAT TO BE THE MAXIMUM CAPACITY OF THE DRIVE.
  2329                              <1> ;
  2330                              <1> ;		THESE PARAMETERS OF DISK BASE MUST BE CHANGED IN ORDER TO
  2331                              <1> ;		FORMAT THE FOLLOWING MEDIAS:
  2332                              <1> ;		---------------------------------------------
  2333                              <1> ;		: MEDIA  :     DRIVE      : PARM 1 : PARM 2 :
  2334                              <1> ;		---------------------------------------------
  2335                              <1> ;		: 320K	 : 320K/360K/1.2M :  50H   :   8    :
  2336                              <1> ;		: 360K	 : 320K/360K/1.2M :  50H   :   9    :
  2337                              <1> ;		: 1.2M	 : 1.2M           :  54H   :  15    :
  2338                              <1> ;		: 720K	 : 720K/1.44M     :  50H   :   9    :
  2339                              <1> ;		: 1.44M	 : 1.44M          :  6CH   :  18    :		  	
  2340                              <1> ;		---------------------------------------------
  2341                              <1> ;		NOTES: - PARM 1 = GAP LENGTH FOR FORMAT
  2342                              <1> ;		       - PARM 2 = EOT (LAST SECTOR ON TRACK)
  2343                              <1> ;		       - DISK BASE IS POINTED BY DISK POINTER LOCATED
  2344                              <1> ;			 AT ABSOLUTE ADDRESS 0:78.
  2345                              <1> ;		       - WHEN FORMAT OPERATIONS ARE COMPLETE, THE PARAMETERS
  2346                              <1> ;			 SHOULD BE RESTORED TO THEIR RESPECTIVE INITIAL VALUES.			
  2347                              <1> ;-------------------------------------------------------------------------------
  2348                              <1> ;	(AH) = 08H READ DRIVE PARAMETERS
  2349                              <1> ;	REGISTERS
  2350                              <1> ;	  INPUT
  2351                              <1> ;	    (DL) - DRIVE NUMBER (0-1 ALLOWED, VALUE CHECKED)
  2352                              <1> ;	  OUTPUT
  2353                              <1> ;	    (ES:DI) POINTS TO DRIVE PARAMETER TABLE
  2354                              <1> ;	    (CH) - LOW ORDER 8 OF 10 BITS MAXIMUM NUMBER OF TRACKS
  2355                              <1> ;	    (CL) - BITS 7 & 6 - HIGH ORDER TWO BITS OF MAXIMUM TRACKS
  2356                              <1> ;	           BITS 5 THRU 0 - MAXIMUM SECTORS PER TRACK
  2357                              <1> ;	    (DH) - MAXIMUM HEAD NUMBER
  2358                              <1> ;	    (DL) - NUMBER OF DISKETTE DRIVES INSTALLED
  2359                              <1> ;	    (BH) - 0
  2360                              <1> ;	    (BL) - BITS 7 THRU 4 - 0
  2361                              <1> ;	           BITS 3 THRU 0 - VALID DRIVE TYPE VALUE IN CMOS
  2362                              <1> ;	    (AX) - 0
  2363                              <1> ;	 UNDER THE FOLLOWING CIRCUMSTANCES:
  2364                              <1> ;	    (1) THE DRIVE NUMBER IS INVALID,
  2365                              <1> ;	    (2) THE DRIVE TYPE IS UNKNOWN AND CMOS IS NOT PRESENT, 
  2366                              <1> ;	    (3) THE DRIVE TYPE IS UNKNOWN AND CMOS IS BAD,
  2367                              <1> ;	    (4) OR THE DRIVE TYPE IS UNKNOWN AND THE CMOS DRIVE TYPE IS INVALID
  2368                              <1> ;	    THEN ES,AX,BX,CX,DH,DI=0 ; DL=NUMBER OF DRIVES. 
  2369                              <1> ;	    IF NO DRIVES ARE PRESENT THEN: ES,AX,BX,CX,DX,DI=0.
  2370                              <1> ;	    @DISKETTE_STATUS = 0 AND CY IS RESET.
  2371                              <1> ;-------------------------------------------------------------------------------
  2372                              <1> ;	(AH)= 15H  READ DASD TYPE
  2373                              <1> ;	OUTPUT REGISTERS
  2374                              <1> ;	(AH) - ON RETURN IF CARRY FLAG NOT SET, OTHERWISE ERROR	
  2375                              <1> ;		00 - DRIVE NOT PRESENT	
  2376                              <1> ;		01 - DISKETTE, NO CHANGE LINE AVAILABLE
  2377                              <1> ;		02 - DISKETTE, CHANGE LINE AVAILABLE	
  2378                              <1> ;		03 - RESERVED (FIXED DISK)
  2379                              <1> ;	(DL) - DRIVE NUMBER (0-1 ALLOWED, VALUE CHECKED)
  2380                              <1> ;-------------------------------------------------------------------------------
  2381                              <1> ;	(AH)= 16H  DISK CHANGE LINE STATUS
  2382                              <1> ;	OUTPUT REGISTERS
  2383                              <1> ;	(AH) - 00 - DISK CHANGE LINE NOT ACTIVE	
  2384                              <1> ;	       06 - DISK CHANGE LINE ACTIVE & CARRY BIT ON
  2385                              <1> ;	(DL) - DRIVE NUMBER (0-1 ALLOWED, VALUE CHECKED)
  2386                              <1> ;-------------------------------------------------------------------------------
  2387                              <1> ;	(AH)= 17H  SET DASD TYPE FOR FORMAT
  2388                              <1> ;	INPUT REGISTERS
  2389                              <1> ;	(AL) -	00 - NOT USED	
  2390                              <1> ;		01 - DISKETTE 320/360K IN 360K DRIVE	
  2391                              <1> ;		02 - DISKETTE 360K IN 1.2M DRIVE
  2392                              <1> ;		03 - DISKETTE 1.2M IN 1.2M DRIVE
  2393                              <1> ;		04 - DISKETTE 720K IN 720K DRIVE
  2394                              <1> ;	(DL) - DRIVE NUMBER (0-1 ALLOWED, VALUE CHECKED:
  2395                              <1> ;	       (DO NOT USE WHEN DISKETTE ATTACH CARD USED)
  2396                              <1> ;-------------------------------------------------------------------------------
  2397                              <1> ;	(AH)= 18H  SET MEDIA TYPE FOR FORMAT
  2398                              <1> ;	INPUT REGISTERS
  2399                              <1> ;	(CH) - LOW ORDER 8 OF 10 BITS MAXIMUM TRACKS
  2400                              <1> ;	(CL) - BITS 7 & 6 - HIGH ORDER TWO BITS OF MAXIMUM TRACKS
  2401                              <1> ;	       BITS 5 THRU 0 - MAXIMUM SECTORS PER TRACK
  2402                              <1> ;	(DL) - DRIVE NUMBER (0-1 ALLOWED, VALUE CHACKED)
  2403                              <1> ;	OUTPUT REGISTERS:
  2404                              <1> ;	(ES:DI) - POINTER TO DRIVE PARAMETERS TABLE FOR THIS MEDIA TYPE,
  2405                              <1> ;		  UNCHANGED IF (AH) IS NON-ZERO
  2406                              <1> ;	(AH) - 00H, CY = 0, TRACK AND SECTORS/TRACK COMBINATION IS SUPPORTED
  2407                              <1> ;	     - 01H, CY = 1, FUNCTION IS NOT AVAILABLE
  2408                              <1> ;	     - 0CH, CY = 1, TRACK AND SECTORS/TRACK COMBINATION IS NOT SUPPORTED
  2409                              <1> ;	     - 80H, CY = 1, TIME OUT (DISKETTE NOT PRESENT)		
  2410                              <1> ;-------------------------------------------------------------------------------
  2411                              <1> ;	DISK CHANGE STATUS IS ONLY CHECKED WHEN A MEDIA SPECIFIED IS OTHER
  2412                              <1> ;	THAN 360 KB DRIVE. IF THE DISK CHANGE LINE IS FOUND TO BE
  2413                              <1> ;	ACTIVE THE FOLLOWING ACTIONS TAKE PLACE:
  2414                              <1> ;		ATTEMPT TO RESET DISK CHANGE LINE TO INACTIVE STATE. 
  2415                              <1> ;		IF ATTEMPT SUCCEEDS SET DASD TYPE FOR FORMAT AND RETURN DISK 
  2416                              <1> ;		CHANGE ERROR CODE
  2417                              <1> ;		IF ATTEMPT FAILS RETURN TIMEOUT ERROR CODE AND SET DASD TYPE 
  2418                              <1> ;		TO A PREDETERMINED STATE INDICATING MEDIA TYPE UNKNOWN.
  2419                              <1> ;	IF THE DISK CHANGE LINE IN INACTIVE PERFORM SET DASD TYPE FOR FORMAT.
  2420                              <1> ;
  2421                              <1> ; DATA VARIABLE -- @DISK_POINTER
  2422                              <1> ;	DOUBLE WORD POINTER TO THE CURRENT SET OF DISKETTE PARAMETERS
  2423                              <1> ;-------------------------------------------------------------------------------
  2424                              <1> ; OUTPUT FOR ALL FUNCTIONS
  2425                              <1> ;	AH = STATUS OF OPERATION
  2426                              <1> ;		STATUS BITS ARE DEFINED IN THE EQUATES FOR @DISKETTE_STATUS
  2427                              <1> ;		VARIABLE IN THE DATA SEGMENT OF THIS MODULE
  2428                              <1> ;	CY = 0	SUCCESSFUL OPERATION (AH=0 ON RETURN, EXCEPT FOR READ DASD
  2429                              <1> ;		TYPE AH=(15)).
  2430                              <1> ;	CY = 1	FAILED OPERATION (AH HAS ERROR REASON)
  2431                              <1> ;	FOR READ/WRITE/VERIFY
  2432                              <1> ;		DS,BX,DX,CX PRESERVED
  2433                              <1> ;	NOTE: IF AN ERROR IS REPORTED BY THE DISKETTE CODE, THE APPROPRIATE 
  2434                              <1> ;		ACTION IS TO RESET THE DISKETTE, THEN RETRY THE OPERATION.
  2435                              <1> ;		ON READ ACCESSES, NO MOTOR START DELAY IS TAKEN, SO THAT 
  2436                              <1> ;		THREE RETRIES ARE REQUIRED ON READS TO ENSURE THAT THE 
  2437                              <1> ;		PROBLEM IS NOT DUE TO MOTOR START-UP.
  2438                              <1> ;-------------------------------------------------------------------------------
  2439                              <1> ;
  2440                              <1> ; DISKETTE STATE MACHINE - ABSOLUTE ADDRESS 40:90 (DRIVE A) & 91 (DRIVE B)
  2441                              <1> ;
  2442                              <1> ;   -----------------------------------------------------------------
  2443                              <1> ;   |       |       |       |       |       |       |       |       |
  2444                              <1> ;   |   7   |   6   |   5   |   4   |   3   |   2   |   1   |   0   |
  2445                              <1> ;   |       |       |       |       |       |       |       |       |
  2446                              <1> ;   -----------------------------------------------------------------
  2447                              <1> ;	|	|	|	|	|	|	|	|
  2448                              <1> ;	|	|	|	|	|	-----------------
  2449                              <1> ;	|	|	|	|	|		|
  2450                              <1> ;	|	|	|	|    RESERVED		|
  2451                              <1> ;	|	|	|	|		  PRESENT STATE
  2452                              <1> ;	|	|	|	|	000: 360K IN 360K DRIVE UNESTABLISHED
  2453                              <1> ;	|	|	|	|	001: 360K IN 1.2M DRIVE UNESTABLISHED
  2454                              <1> ;	|	|	|	|	010: 1.2M IN 1.2M DRIVE UNESTABLISHED
  2455                              <1> ;	|	|	|	|	011: 360K IN 360K DRIVE ESTABLISHED
  2456                              <1> ;	|	|	|	|	100: 360K IN 1.2M DRIVE ESTABLISHED
  2457                              <1> ;	|	|	|	|	101: 1.2M IN 1.2M DRIVE ESTABLISHED
  2458                              <1> ;	|	|	|	|	110: RESERVED
  2459                              <1> ;	|	|	|	|	111: NONE OF THE ABOVE
  2460                              <1> ;	|	|	|	|
  2461                              <1> ;	|	|	|	------>	MEDIA/DRIVE ESTABLISHED
  2462                              <1> ;	|	|	|
  2463                              <1> ;	|	|	-------------->	DOUBLE STEPPING REQUIRED (360K IN 1.2M
  2464                              <1> ;	|	|			DRIVE)
  2465                              <1> ;	|	|
  2466                              <1> ;	------------------------------>	DATA TRANSFER RATE FOR THIS DRIVE:
  2467                              <1> ;
  2468                              <1> ;						00: 500 KBS
  2469                              <1> ;						01: 300 KBS
  2470                              <1> ;						10: 250 KBS
  2471                              <1> ;						11: RESERVED
  2472                              <1> ;
  2473                              <1> ;
  2474                              <1> ;-------------------------------------------------------------------------------
  2475                              <1> ; STATE OPERATION STARTED - ABSOLUTE ADDRESS 40:92 (DRIVE A) & 93 (DRIVE B)
  2476                              <1> ;-------------------------------------------------------------------------------
  2477                              <1> ; PRESENT CYLINDER NUMBER - ABSOLUTE ADDRESS 40:94 (DRIVE A) & 95 (DRIVE B)
  2478                              <1> ;-------------------------------------------------------------------------------
  2479                              <1> 
  2480                              <1> struc MD
  2481 00000000 ??                  <1> 	.SPEC1		resb	1	; SRT=D, HD UNLOAD=0F - 1ST SPECIFY BYTE
  2482 00000001 ??                  <1> 	.SPEC2		resb	1	; HD LOAD=1, MODE=DMA - 2ND SPECIFY BYTE
  2483 00000002 ??                  <1> 	.OFF_TIM	resb	1	; WAIT TIME AFTER OPERATION TILL MOTOR OFF
  2484 00000003 ??                  <1> 	.BYT_SEC	resb	1	; 512 BYTES/SECTOR
  2485 00000004 ??                  <1> 	.SEC_TRK	resb	1	; EOT (LAST SECTOR ON TRACK)
  2486 00000005 ??                  <1> 	.GAP		resb	1	; GAP LENGTH
  2487 00000006 ??                  <1> 	.DTL		resb	1	; DTL
  2488 00000007 ??                  <1> 	.GAP3		resb	1	; GAP LENGTH FOR FORMAT
  2489 00000008 ??                  <1> 	.FIL_BYT	resb	1	; FILL BYTE FOR FORMAT
  2490 00000009 ??                  <1> 	.HD_TIM		resb	1	; HEAD SETTLE TIME (MILLISECONDS)
  2491 0000000A ??                  <1> 	.STR_TIM	resb	1	; MOTOR START TIME (1/8 SECONDS)
  2492 0000000B ??                  <1> 	.MAX_TRK	resb	1	; MAX. TRACK NUMBER
  2493 0000000C ??                  <1> 	.RATE		resb	1	; DATA TRANSFER RATE
  2494                              <1> endstruc
  2495                              <1> 
  2496                              <1> BIT7OFF	EQU	7FH
  2497                              <1> BIT7ON	EQU	80H
  2498                              <1> 
  2499                              <1> ;;int13h: ; 16/02/2015
  2500                              <1> ;; 16/02/2015 - 21/02/2015
  2501                              <1> int40h:
  2502 0000180D 9C                  <1> 	pushfd
  2503 0000180E 0E                  <1> 	push 	cs
  2504 0000180F E801000000          <1> 	call 	DISKETTE_IO_1
  2505 00001814 C3                  <1> 	retn	
  2506                              <1> 
  2507                              <1> DISKETTE_IO_1:
  2508                              <1> 
  2509 00001815 FB                  <1> 	STI				; INTERRUPTS BACK ON
  2510 00001816 55                  <1> 	PUSH	eBP			; USER REGISTER
  2511 00001817 57                  <1> 	PUSH	eDI			; USER REGISTER
  2512 00001818 52                  <1> 	PUSH	eDX			; HEAD #, DRIVE # OR USER REGISTER
  2513 00001819 53                  <1> 	PUSH	eBX			; BUFFER OFFSET PARAMETER OR REGISTER
  2514 0000181A 51                  <1> 	PUSH	eCX			; TRACK #-SECTOR # OR USER REGISTER
  2515 0000181B 89E5                <1> 	MOV	eBP,eSP			; BP     => PARAMETER LIST DEP. ON AH
  2516                              <1> 					; [BP]   = SECTOR #
  2517                              <1> 					; [BP+1] = TRACK #
  2518                              <1> 					; [BP+2] = BUFFER OFFSET
  2519                              <1> 					; FOR RETURN OF DRIVE PARAMETERS:
  2520                              <1> 					; CL/[BP] = BITS 7&6 HI BITS OF MAX CYL
  2521                              <1> 					; 	    BITS 0-5 MAX SECTORS/TRACK
  2522                              <1> 					; CH/[BP+1] = LOW 8 BITS OF MAX CYL.
  2523                              <1> 					; BL/[BP+2] = BITS 7-4 = 0
  2524                              <1> 					;	      BITS 3-0 = VALID CMOS TYPE
  2525                              <1> 					; BH/[BP+3] = 0
  2526                              <1> 					; DL/[BP+4] = # DRIVES INSTALLED
  2527                              <1> 					; DH/[BP+5] = MAX HEAD #
  2528                              <1> 					; DI/[BP+6] = OFFSET TO DISK BASE
  2529 0000181D 06                  <1> 	push	es ; 06/02/2015	
  2530 0000181E 1E                  <1> 	PUSH	DS			; BUFFER SEGMENT PARM OR USER REGISTER
  2531 0000181F 56                  <1> 	PUSH	eSI			; USER REGISTERS
  2532                              <1> 	;CALL	DDS			; SEGMENT OF BIOS DATA AREA TO DS
  2533                              <1> 	;mov	cx, cs
  2534                              <1> 	;mov	ds, cx
  2535 00001820 66B91000            <1> 	mov	cx, KDATA
  2536 00001824 8ED9                <1>         mov     ds, cx
  2537 00001826 8EC1                <1>         mov     es, cx
  2538                              <1> 
  2539                              <1> 	;CMP	AH,(FNC_TAE-FNC_TAB)/2	; CHECK FOR > LARGEST FUNCTION
  2540 00001828 80FC19              <1> 	cmp	ah,(FNC_TAE-FNC_TAB)/4 ; 18/02/2015
  2541 0000182B 7202                <1> 	JB	short OK_FUNC		; FUNCTION OK
  2542 0000182D B414                <1> 	MOV	AH,14H			; REPLACE WITH KNOWN INVALID FUNCTION
  2543                              <1> OK_FUNC:
  2544 0000182F 80FC01              <1> 	CMP	AH,1			; RESET OR STATUS ?
  2545 00001832 760C                <1> 	JBE	short OK_DRV		; IF RESET OR STATUS DRIVE ALWAYS OK
  2546 00001834 80FC08              <1> 	CMP	AH,8			; READ DRIVE PARMS ?
  2547 00001837 7407                <1> 	JZ	short OK_DRV		; IF SO DRIVE CHECKED LATER
  2548 00001839 80FA01              <1> 	CMP	DL,1			; DRIVES 0 AND 1 OK
  2549 0000183C 7602                <1> 	JBE	short OK_DRV		; IF 0 OR 1 THEN JUMP
  2550 0000183E B414                <1> 	MOV	AH,14H			; REPLACE WITH KNOWN INVALID FUNCTION
  2551                              <1> OK_DRV:
  2552 00001840 31C9                <1> 	xor	ecx, ecx
  2553                              <1> 	;mov	esi, ecx ; 08/02/2015
  2554                              <1> 	; 05/12/2021
  2555 00001842 89CE                <1> 	mov	esi, ecx
  2556 00001844 89CF                <1> 	mov	edi, ecx ; 08/02/2015
  2557 00001846 88E1                <1> 	MOV	CL,AH			; CL = FUNCTION
  2558                              <1> 	;XOR	CH,CH			; CX = FUNCTION
  2559                              <1> 	;SHL	CL, 1			; FUNCTION TIMES 2
  2560 00001848 C0E102              <1> 	SHL	CL, 2 ; 20/02/2015	; FUNCTION TIMES 4 (for 32 bit offset)
  2561 0000184B BB[83180000]        <1> 	MOV	eBX,FNC_TAB		; LOAD START OF FUNCTION TABLE
  2562 00001850 01CB                <1> 	ADD	eBX,eCX			; ADD OFFSET INTO TABLE => ROUTINE
  2563 00001852 88F4                <1> 	MOV	AH,DH			; AX = HEAD #,# OF SECTORS OR DASD TYPE
  2564 00001854 30F6                <1> 	XOR	DH,DH			; DX = DRIVE #
  2565 00001856 6689C6              <1> 	MOV	SI,AX			; SI = HEAD #,# OF SECTORS OR DASD TYPE
  2566 00001859 6689D7              <1> 	MOV     DI,DX                   ; DI = DRIVE #
  2567                              <1> 	;
  2568                              <1> 	; 11/12/2014
  2569 0000185C 8815[A16A0000]      <1>         mov     [cfd], dl               ; current floppy drive (for 'GET_PARM')        
  2570                              <1> 	;
  2571 00001862 8A25[3C700000]      <1> 	MOV	AH, [DSKETTE_STATUS]	; LOAD STATUS TO AH FOR STATUS FUNCTION
  2572 00001868 C605[3C700000]00    <1> 	MOV	byte [DSKETTE_STATUS],0	; INITIALIZE FOR ALL OTHERS
  2573                              <1> 
  2574                              <1> ;	THROUGHOUT THE DISKETTE BIOS, THE FOLLOWING INFORMATION IS CONTAINED IN
  2575                              <1> ;	THE FOLLOWING MEMORY LOCATIONS AND REGISTERS. NOT ALL DISKETTE BIOS
  2576                              <1> ;	FUNCTIONS REQUIRE ALL OF THESE PARAMETERS.
  2577                              <1> ;
  2578                              <1> ;		DI	: DRIVE #
  2579                              <1> ;		SI-HI	: HEAD #
  2580                              <1> ;		SI-LOW	: # OF SECTORS OR DASD TYPE FOR FORMAT
  2581                              <1> ;		ES	: BUFFER SEGMENT
  2582                              <1> ;		[BP]	: SECTOR #
  2583                              <1> ;		[BP+1]	: TRACK #
  2584                              <1> ;		[BP+2]	: BUFFER OFFSET
  2585                              <1> ;
  2586                              <1> ;	ACROSS CALLS TO SUBROUTINES THE CARRY FLAG (CY=1), WHERE INDICATED IN 
  2587                              <1> ;	SUBROUTINE PROLOGUES, REPRESENTS AN EXCEPTION RETURN (NORMALLY AN ERROR 
  2588                              <1> ;	CONDITION). IN MOST CASES, WHEN CY = 1, @DSKETTE_STATUS CONTAINS THE 
  2589                              <1> ;	SPECIFIC ERROR CODE.
  2590                              <1> ;
  2591                              <1> 					; (AH) = @DSKETTE_STATUS
  2592 0000186F FF13                <1> 	CALL	dWORD [eBX]		; CALL THE REQUESTED FUNCTION
  2593 00001871 5E                  <1> 	POP	eSI			; RESTORE ALL REGISTERS
  2594 00001872 1F                  <1> 	POP	DS
  2595 00001873 07                  <1> 	pop	es	; 06/02/2015
  2596 00001874 59                  <1> 	POP	eCX
  2597 00001875 5B                  <1> 	POP	eBX
  2598 00001876 5A                  <1> 	POP	eDX
  2599 00001877 5F                  <1> 	POP	eDI
  2600 00001878 89E5                <1> 	MOV	eBP, eSP
  2601 0000187A 50                  <1> 	PUSH	eAX
  2602 0000187B 9C                  <1> 	PUSHFd
  2603 0000187C 58                  <1> 	POP	eAX
  2604                              <1> 	;MOV	[BP+6], AX
  2605 0000187D 89450C              <1> 	mov	[ebp+12], eax  ; 18/02/2015, flags
  2606 00001880 58                  <1> 	POP	eAX
  2607 00001881 5D                  <1> 	POP	eBP
  2608 00001882 CF                  <1> 	IRETd
  2609                              <1> 
  2610                              <1> ;-------------------------------------------------------------------------------
  2611                              <1> ; DW --> dd (06/02/2015)
  2612 00001883 [E7180000]          <1> FNC_TAB	dd	DSK_RESET		; AH = 00H; RESET
  2613 00001887 [5C190000]          <1> 	dd	DSK_STATUS		; AH = 01H; STATUS
  2614 0000188B [6C190000]          <1> 	dd	DSK_READ		; AH = 02H; READ
  2615 0000188F [7D190000]          <1> 	dd	DSK_WRITE		; AH = 03H; WRITE
  2616 00001893 [8E190000]          <1> 	dd	DSK_VERF		; AH = 04H; VERIFY
  2617 00001897 [9F190000]          <1> 	dd	DSK_FORMAT		; AH = 05H; FORMAT
  2618 0000189B [231A0000]          <1> 	dd	FNC_ERR			; AH = 06H; INVALID
  2619 0000189F [231A0000]          <1> 	dd	FNC_ERR			; AH = 07H; INVALID
  2620 000018A3 [2F1A0000]          <1> 	dd	DSK_PARMS		; AH = 08H; READ DRIVE PARAMETERS
  2621 000018A7 [231A0000]          <1> 	dd	FNC_ERR			; AH = 09H; INVALID
  2622 000018AB [231A0000]          <1> 	dd	FNC_ERR			; AH = 0AH; INVALID
  2623 000018AF [231A0000]          <1> 	dd	FNC_ERR			; AH = 0BH; INVALID
  2624 000018B3 [231A0000]          <1> 	dd	FNC_ERR			; AH = 0CH; INVALID
  2625 000018B7 [231A0000]          <1> 	dd	FNC_ERR			; AH = 0DH; INVALID
  2626 000018BB [231A0000]          <1> 	dd	FNC_ERR			; AH = 0EH; INVALID
  2627 000018BF [231A0000]          <1> 	dd	FNC_ERR			; AH = 0FH; INVALID
  2628 000018C3 [231A0000]          <1> 	dd	FNC_ERR			; AH = 10H; INVALID
  2629 000018C7 [231A0000]          <1> 	dd	FNC_ERR			; AH = 11H; INVALID
  2630 000018CB [231A0000]          <1> 	dd	FNC_ERR			; AH = 12H; INVALID
  2631 000018CF [231A0000]          <1> 	dd	FNC_ERR			; AH = 13H; INVALID
  2632 000018D3 [231A0000]          <1> 	dd	FNC_ERR			; AH = 14H; INVALID
  2633 000018D7 [EE1A0000]          <1> 	dd	DSK_TYPE		; AH = 15H; READ DASD TYPE
  2634 000018DB [161B0000]          <1> 	dd	DSK_CHANGE		; AH = 16H; CHANGE STATUS
  2635 000018DF [4F1B0000]          <1> 	dd	FORMAT_SET		; AH = 17H; SET DASD TYPE
  2636 000018E3 [D01B0000]          <1> 	dd	SET_MEDIA		; AH = 18H; SET MEDIA TYPE	
  2637                              <1> FNC_TAE EQU     $                       ; END
  2638                              <1> 
  2639                              <1> ;-------------------------------------------------------------------------------
  2640                              <1> ; DISK_RESET	(AH = 00H)	
  2641                              <1> ;		RESET THE DISKETTE SYSTEM.
  2642                              <1> ;
  2643                              <1> ; ON EXIT:	@DSKETTE_STATUS, CY REFLECT STATUS OF OPERATION
  2644                              <1> ;-------------------------------------------------------------------------------
  2645                              <1> DSK_RESET:
  2646 000018E7 66BAF203            <1> 	MOV	DX,03F2H		; ADAPTER CONTROL PORT
  2647 000018EB FA                  <1> 	CLI				; NO INTERRUPTS
  2648 000018EC A0[3A700000]        <1> 	MOV	AL,[MOTOR_STATUS]	; GET DIGITAL OUTPUT REGISTER REFLECTION
  2649 000018F1 243F                <1> 	AND	AL,00111111B		; KEEP SELECTED AND MOTOR ON BITS
  2650 000018F3 C0C004              <1> 	ROL	AL,4			; MOTOR VALUE TO HIGH NIBBLE
  2651                              <1> 					; DRIVE SELECT TO LOW NIBBLE
  2652 000018F6 0C08                <1> 	OR	AL,00001000B		; TURN ON INTERRUPT ENABLE
  2653 000018F8 EE                  <1> 	OUT	DX,AL			; RESET THE ADAPTER
  2654 000018F9 C605[39700000]00    <1> 	MOV	byte [SEEK_STATUS],0	; SET RECALIBRATE REQUIRED ON ALL DRIVES
  2655                              <1> 	;JMP	$+2			; WAIT FOR I/O
  2656                              <1> 	;JMP	$+2			; WAIT FOR I/O (TO INSURE MINIMUM
  2657                              <1> 					;      PULSE WIDTH)
  2658                              <1> 	; 19/12/2014
  2659                              <1> 	NEWIODELAY
  2132 00001900 E6EB                <2>  out 0EBh,al
  2660                              <1> 
  2661                              <1> 	; 17/12/2014 
  2662                              <1> 	; AWARD BIOS 1999 - RESETDRIVES (ADISK.ASM)
  2663 00001902 B915000000          <1> 	mov	ecx, WAITCPU_RESET_ON	; cx = 21 -- Min. 14 micro seconds !?
  2664                              <1> wdw1:
  2665                              <1> 	NEWIODELAY   ; 27/02/2015
  2132 00001907 E6EB                <2>  out 0EBh,al
  2666 00001909 E2FC                <1> 	loop	wdw1
  2667                              <1> 	;
  2668 0000190B 0C04                <1> 	OR	AL,00000100B		; TURN OFF RESET BIT
  2669 0000190D EE                  <1> 	OUT	DX,AL			; RESET THE ADAPTER
  2670                              <1> 	; 16/12/2014
  2671                              <1> 	IODELAY
  2127 0000190E EB00                <2>  jmp short $+2
  2128 00001910 EB00                <2>  jmp short $+2
  2672                              <1> 	;
  2673                              <1> 	;STI				; ENABLE THE INTERRUPTS
  2674 00001912 E8EC0B0000          <1> 	CALL	WAIT_INT		; WAIT FOR THE INTERRUPT
  2675 00001917 723A                <1> 	JC	short DR_ERR		; IF ERROR, RETURN IT
  2676 00001919 66B9C000            <1> 	MOV	CX,11000000B		; CL = EXPECTED @NEC_STATUS
  2677                              <1> NXT_DRV:
  2678                              <1> 	;PUSH	CX			; SAVE FOR CALL
  2679                              <1> 	; 05/12/2021
  2680 0000191D 51                  <1> 	push	ecx
  2681 0000191E B8[52190000]        <1> 	MOV	eAX, DR_POP_ERR 	; LOAD NEC_OUTPUT ERROR ADDRESS
  2682 00001923 50                  <1> 	PUSH	eAX			; "
  2683 00001924 B408                <1> 	MOV	AH,08H			; SENSE INTERRUPT STATUS COMMAND
  2684 00001926 E8CE0A0000          <1> 	CALL	NEC_OUTPUT
  2685 0000192B 58                  <1> 	POP	eAX			; THROW AWAY ERROR RETURN
  2686 0000192C E8020C0000          <1> 	CALL	RESULTS			; READ IN THE RESULTS
  2687                              <1> 	;POP	CX			; RESTORE AFTER CALL
  2688                              <1> 	; 05/12/2021
  2689 00001931 59                  <1> 	pop	ecx
  2690 00001932 721F                <1> 	JC	short DR_ERR		; ERROR RETURN
  2691 00001934 3A0D[3D700000]      <1> 	CMP	CL, [NEC_STATUS]	; TEST FOR DRIVE READY TRANSITION
  2692 0000193A 7517                <1> 	JNZ	short DR_ERR		; EVERYTHING OK
  2693 0000193C FEC1                <1> 	INC	CL			; NEXT EXPECTED @NEC_STATUS
  2694 0000193E 80F9C3              <1> 	CMP	CL,11000011B		; ALL POSSIBLE DRIVES CLEARED
  2695 00001941 76DA                <1> 	JBE	short NXT_DRV		; FALL THRU IF 11000100B OR >
  2696                              <1> 	;
  2697 00001943 E843030000          <1> 	CALL	SEND_SPEC		; SEND SPECIFY COMMAND TO NEC
  2698                              <1> RESBAC:
  2699 00001948 E8D8080000          <1> 	CALL	SETUP_END		; VARIOUS CLEANUPS
  2700                              <1> 	;MOV	BX,SI			; GET SAVED AL TO BL
  2701                              <1> 	; 05/12/2021
  2702 0000194D 89F3                <1> 	mov	ebx, esi
  2703 0000194F 88D8                <1> 	MOV	AL,BL			; PUT BACK FOR RETURN
  2704 00001951 C3                  <1> 	RETn		
  2705                              <1> DR_POP_ERR:
  2706                              <1> 	;POP	CX			; CLEAR STACK
  2707                              <1> 	; 05/12/2021
  2708 00001952 59                  <1> 	pop	ecx
  2709                              <1> DR_ERR:
  2710 00001953 800D[3C700000]20    <1> 	OR	byte [DSKETTE_STATUS],BAD_NEC ; SET ERROR CODE
  2711 0000195A EBEC                <1> 	JMP	SHORT RESBAC		; RETURN FROM RESET
  2712                              <1> 
  2713                              <1> ;-------------------------------------------------------------------------------
  2714                              <1> ; DISK_STATUS	(AH = 01H)
  2715                              <1> ;	DISKETTE STATUS.
  2716                              <1> ;
  2717                              <1> ; ON ENTRY:	AH : STATUS OF PREVIOUS OPERATION
  2718                              <1> ;
  2719                              <1> ; ON EXIT:	AH, @DSKETTE_STATUS, CY REFLECT STATUS OF PREVIOUS OPERATION.
  2720                              <1> ;-------------------------------------------------------------------------------
  2721                              <1> DSK_STATUS:
  2722 0000195C 8825[3C700000]      <1> 	MOV	[DSKETTE_STATUS],AH	; PUT BACK FOR SETUP END
  2723 00001962 E8BE080000          <1> 	CALL	SETUP_END		; VARIOUS CLEANUPS
  2724                              <1> 	;MOV	BX,SI			; GET SAVED AL TO BL
  2725                              <1> 	; 05/12/2021
  2726 00001967 89F3                <1> 	mov	ebx, esi
  2727 00001969 88D8                <1> 	MOV	AL,BL			; PUT BACK FOR RETURN
  2728 0000196B C3                  <1> 	RETn		
  2729                              <1> 
  2730                              <1> ;-------------------------------------------------------------------------------
  2731                              <1> ; DISK_READ	(AH = 02H)	
  2732                              <1> ;	DISKETTE READ.
  2733                              <1> ;
  2734                              <1> ; ON ENTRY:	DI	: DRIVE #
  2735                              <1> ;		SI-HI	: HEAD #
  2736                              <1> ;		SI-LOW	: # OF SECTORS
  2737                              <1> ;		ES	: BUFFER SEGMENT
  2738                              <1> ;		[BP]	: SECTOR #
  2739                              <1> ;		[BP+1]	: TRACK #
  2740                              <1> ;		[BP+2]	: BUFFER OFFSET
  2741                              <1> ;
  2742                              <1> ; ON EXIT:	@DSKETTE_STATUS, CY REFLECT STATUS OF OPERATION
  2743                              <1> ;-------------------------------------------------------------------------------
  2744                              <1> 
  2745                              <1> ; 06/02/2015, ES:BX -> EBX (unix386.s)
  2746                              <1> 
  2747                              <1> DSK_READ:
  2748 0000196C 8025[3A700000]7F    <1> 	AND	byte [MOTOR_STATUS],01111111B ; INDICATE A READ OPERATION
  2749 00001973 66B846E6            <1> 	MOV	AX,0E646H		; AX = NEC COMMAND, DMA COMMAND
  2750 00001977 E815040000          <1> 	CALL	RD_WR_VF		; COMMON READ/WRITE/VERIFY
  2751 0000197C C3                  <1> 	RETn
  2752                              <1> 
  2753                              <1> ;-------------------------------------------------------------------------------
  2754                              <1> ; DISK_WRITE	(AH = 03H)
  2755                              <1> ;	DISKETTE WRITE.
  2756                              <1> ;
  2757                              <1> ; ON ENTRY:	DI	: DRIVE #
  2758                              <1> ;		SI-HI	: HEAD #
  2759                              <1> ;		SI-LOW	: # OF SECTORS
  2760                              <1> ;		ES	: BUFFER SEGMENT
  2761                              <1> ;		[BP]	: SECTOR #
  2762                              <1> ;		[BP+1]	: TRACK #
  2763                              <1> ;		[BP+2]	: BUFFER OFFSET
  2764                              <1> ;
  2765                              <1> ; ON EXIT:	@DSKETTE_STATUS, CY REFLECT STATUS OF OPERATION
  2766                              <1> ;-------------------------------------------------------------------------------
  2767                              <1> 
  2768                              <1> ; 06/02/2015, ES:BX -> EBX (unix386.s)
  2769                              <1> 
  2770                              <1> DSK_WRITE:
  2771 0000197D 66B84AC5            <1> 	MOV	AX,0C54AH		; AX = NEC COMMAND, DMA COMMAND
  2772 00001981 800D[3A700000]80    <1>         OR      byte [MOTOR_STATUS],10000000B ; INDICATE WRITE OPERATION
  2773 00001988 E804040000          <1> 	CALL	RD_WR_VF		; COMMON READ/WRITE/VERIFY
  2774 0000198D C3                  <1> 	RETn
  2775                              <1> 
  2776                              <1> ;-------------------------------------------------------------------------------
  2777                              <1> ; DISK_VERF	(AH = 04H)
  2778                              <1> ;	DISKETTE VERIFY.
  2779                              <1> ;
  2780                              <1> ; ON ENTRY:	DI	: DRIVE #
  2781                              <1> ;		SI-HI	: HEAD #
  2782                              <1> ;		SI-LOW	: # OF SECTORS
  2783                              <1> ;		ES	: BUFFER SEGMENT
  2784                              <1> ;		[BP]	: SECTOR #
  2785                              <1> ;		[BP+1]	: TRACK #
  2786                              <1> ;		[BP+2]	: BUFFER OFFSET
  2787                              <1> ;
  2788                              <1> ; ON EXIT:	@DSKETTE_STATUS, CY REFLECT STATUS OF OPERATION
  2789                              <1> ;-------------------------------------------------------------------------------
  2790                              <1> DSK_VERF:
  2791 0000198E 8025[3A700000]7F    <1> 	AND	byte [MOTOR_STATUS],01111111B ; INDICATE A READ OPERATION
  2792 00001995 66B842E6            <1> 	MOV	AX,0E642H		; AX = NEC COMMAND, DMA COMMAND
  2793 00001999 E8F3030000          <1> 	CALL	RD_WR_VF		; COMMON READ/WRITE/VERIFY
  2794 0000199E C3                  <1> 	RETn
  2795                              <1> 
  2796                              <1> ;-------------------------------------------------------------------------------
  2797                              <1> ; DISK_FORMAT	(AH = 05H)
  2798                              <1> ;	DISKETTE FORMAT.
  2799                              <1> ;
  2800                              <1> ; ON ENTRY:	DI	: DRIVE #
  2801                              <1> ;		SI-HI	: HEAD #
  2802                              <1> ;		SI-LOW	: # OF SECTORS
  2803                              <1> ;		ES	: BUFFER SEGMENT
  2804                              <1> ;		[BP]	: SECTOR #
  2805                              <1> ;		[BP+1]	: TRACK #
  2806                              <1> ;		[BP+2]	: BUFFER OFFSET
  2807                              <1> ;		@DISK_POINTER POINTS TO THE PARAMETER TABLE OF THIS DRIVE
  2808                              <1> ;
  2809                              <1> ; ON EXIT:	@DSKETTE_STATUS, CY REFLECT STATUS OF OPERATION
  2810                              <1> ;-------------------------------------------------------------------------------
  2811                              <1> DSK_FORMAT:
  2812 0000199F E830030000          <1> 	CALL	XLAT_NEW		; TRANSLATE STATE TO PRESENT ARCH.
  2813 000019A4 E81D050000          <1> 	CALL	FMT_INIT		; ESTABLISH STATE IF UNESTABLISHED
  2814 000019A9 800D[3A700000]80    <1>         OR      byte [MOTOR_STATUS], 10000000B ; INDICATE WRITE OPERATION
  2815 000019B0 E865050000          <1> 	CALL	MED_CHANGE		; CHECK MEDIA CHANGE AND RESET IF SO
  2816 000019B5 725D                <1>         JC      short FM_DON            ; MEDIA CHANGED, SKIP
  2817 000019B7 E8CF020000          <1> 	CALL	SEND_SPEC		; SEND SPECIFY COMMAND TO NEC
  2818 000019BC E8C9050000          <1> 	CALL	CHK_LASTRATE		; ZF=1 ATTEMPT RATE IS SAME AS LAST RATE
  2819 000019C1 7405                <1>         JZ      short FM_WR             ; YES, SKIP SPECIFY COMMAND
  2820 000019C3 E8A2050000          <1> 	CALL	SEND_RATE		; SEND DATA RATE TO CONTROLLER
  2821                              <1> FM_WR:
  2822 000019C8 E84F060000          <1> 	CALL	FMTDMA_SET		; SET UP THE DMA FOR FORMAT
  2823 000019CD 7245                <1>         JC      short FM_DON            ; RETURN WITH ERROR
  2824 000019CF B44D                <1> 	MOV	AH,04DH			; ESTABLISH THE FORMAT COMMAND
  2825 000019D1 E8A7060000          <1> 	CALL	NEC_INIT		; INITIALIZE THE NEC
  2826 000019D6 723C                <1>         JC      short FM_DON            ; ERROR - EXIT
  2827 000019D8 B8[141A0000]        <1>         MOV     eAX, FM_DON             ; LOAD ERROR ADDRESS
  2828 000019DD 50                  <1> 	PUSH	eAX			; PUSH NEC_OUT ERROR RETURN
  2829 000019DE B203                <1> 	MOV	DL,3			; BYTES/SECTOR VALUE TO NEC
  2830 000019E0 E80E090000          <1> 	CALL	GET_PARM
  2831 000019E5 E80F0A0000          <1> 	CALL	NEC_OUTPUT
  2832 000019EA B204                <1> 	MOV	DL,4			; SECTORS/TRACK VALUE TO NEC
  2833 000019EC E802090000          <1> 	CALL	GET_PARM
  2834 000019F1 E8030A0000          <1> 	CALL	NEC_OUTPUT
  2835 000019F6 B207                <1> 	MOV	DL,7			; GAP LENGTH VALUE TO NEC
  2836 000019F8 E8F6080000          <1> 	CALL	GET_PARM
  2837 000019FD E8F7090000          <1> 	CALL	NEC_OUTPUT
  2838 00001A02 B208                <1> 	MOV	DL,8			; FILLER BYTE TO NEC
  2839 00001A04 E8EA080000          <1> 	CALL	GET_PARM
  2840 00001A09 E8EB090000          <1> 	CALL	NEC_OUTPUT
  2841 00001A0E 58                  <1> 	POP	eAX			; THROW AWAY ERROR
  2842 00001A0F E8E5060000          <1> 	CALL	NEC_TERM		; TERMINATE, RECEIVE STATUS, ETC,
  2843                              <1> FM_DON:
  2844 00001A14 E8EC020000          <1> 	CALL	XLAT_OLD		; TRANSLATE STATE TO COMPATIBLE MODE
  2845 00001A19 E807080000          <1> 	CALL	SETUP_END		; VARIOUS CLEANUPS
  2846                              <1> 	;MOV	BX,SI			; GET SAVED AL TO BL
  2847 00001A1E 89F3                <1> 	mov	ebx, esi ; 05/12/2021
  2848 00001A20 88D8                <1> 	MOV	AL,BL			; PUT BACK FOR RETURN
  2849 00001A22 C3                  <1> 	RETn
  2850                              <1> 
  2851                              <1> ;-------------------------------------------------------------------------------
  2852                              <1> ; FNC_ERR
  2853                              <1> ;	INVALID FUNCTION REQUESTED OR INVALID DRIVE: 
  2854                              <1> ;	SET BAD COMMAND IN STATUS.
  2855                              <1> ;
  2856                              <1> ; ON EXIT: 	@DSKETTE_STATUS, CY REFLECT STATUS OF OPERATION
  2857                              <1> ;-------------------------------------------------------------------------------
  2858                              <1> FNC_ERR:				; INVALID FUNCTION REQUEST
  2859                              <1> 	;MOV	AX,SI			; RESTORE AL
  2860                              <1> 	; 05/12/2021
  2861 00001A23 89F0                <1> 	mov	eax, esi
  2862 00001A25 B401                <1> 	MOV	AH,BAD_CMD		; SET BAD COMMAND ERROR
  2863 00001A27 8825[3C700000]      <1> 	MOV	[DSKETTE_STATUS],AH	; STORE IN DATA AREA
  2864 00001A2D F9                  <1> 	STC				; SET CARRY INDICATING ERROR
  2865 00001A2E C3                  <1> 	RETn
  2866                              <1> 
  2867                              <1> ;-------------------------------------------------------------------------------
  2868                              <1> ; DISK_PARMS	(AH = 08H)	
  2869                              <1> ;	READ DRIVE PARAMETERS.
  2870                              <1> ;
  2871                              <1> ; ON ENTRY:	DI : DRIVE #
  2872                              <1> ;
  2873                              <1> ; ON EXIT:	CL/[BP]   = BITS 7 & 6 HI 2 BITS OF MAX CYLINDER
  2874                              <1> ;		            BITS 0-5 MAX SECTORS/TRACK
  2875                              <1> ;		CH/[BP+1] = LOW 8 BITS OF MAX CYLINDER
  2876                              <1> ;		BL/[BP+2] = BITS 7-4 = 0
  2877                              <1> ;		            BITS 3-0 = VALID CMOS DRIVE TYPE
  2878                              <1> ;		BH/[BP+3] = 0
  2879                              <1> ;		DL/[BP+4] = # DRIVES INSTALLED (VALUE CHECKED)
  2880                              <1> ;		DH/[BP+5] = MAX HEAD #
  2881                              <1> ;		DI/[BP+6] = OFFSET TO DISK_BASE
  2882                              <1> ;		ES        = SEGMENT OF DISK_BASE
  2883                              <1> ;		AX        = 0
  2884                              <1> ;
  2885                              <1> ;		NOTE : THE ABOVE INFORMATION IS STORED IN THE USERS STACK AT
  2886                              <1> ;		       THE LOCATIONS WHERE THE MAIN ROUTINE WILL POP THEM
  2887                              <1> ;		       INTO THE APPROPRIATE REGISTERS BEFORE RETURNING TO THE
  2888                              <1> ;		       CALLER.
  2889                              <1> ;-------------------------------------------------------------------------------
  2890                              <1> DSK_PARMS:
  2891 00001A2F E8A0020000          <1> 	CALL	XLAT_NEW		; TRANSLATE STATE TO PRESENT ARCH,
  2892                              <1>      ;	MOV	WORD [BP+2],0		; DRIVE TYPE = 0
  2893 00001A34 29D2                <1> 	sub     edx, edx ; 20/02/2015
  2894 00001A36 895504              <1>         mov	[ebp+4], edx ; 20/02/2015
  2895                              <1>      ;  MOV     AX, [EQUIP_FLAG]        ; LOAD EQUIPMENT FLAG FOR # DISKETTES
  2896                              <1>      ;  AND     AL,11000001B            ; KEEP DISKETTE DRIVE BITS
  2897                              <1>      ;  MOV     DL,2                    ; DISKETTE DRIVES = 2
  2898                              <1>      ;  CMP     AL,01000001B            ; 2 DRIVES INSTALLED ?
  2899                              <1>      ;  JZ      short STO_DL            ; IF YES JUMP
  2900                              <1>      ;  DEC     DL                      ; DISKETTE DRIVES = 1
  2901                              <1>      ;  CMP     AL,00000001B            ; 1 DRIVE INSTALLED ?
  2902                              <1>      ;  JNZ     short NON_DRV           ; IF NO JUMP
  2903                              <1> 	;sub	edx, edx
  2904 00001A39 66A1[AE6A0000]      <1> 	mov     ax, [fd0_type]
  2905 00001A3F 6621C0              <1> 	and     ax, ax
  2906 00001A42 7473                <1> 	jz      short NON_DRV
  2907 00001A44 FEC2                <1> 	inc     dl
  2908 00001A46 20E4                <1> 	and     ah, ah
  2909 00001A48 7402                <1> 	jz      short STO_DL
  2910 00001A4A FEC2                <1> 	inc     dl
  2911                              <1> STO_DL:
  2912                              <1> 	;MOV	[BP+4],DL		; STORE NUMBER OF DRIVES
  2913 00001A4C 895508              <1> 	mov	[ebp+8], edx ; 20/02/2015	 	
  2914 00001A4F 6683FF01            <1> 	CMP	DI,1			; CHECK FOR VALID DRIVE
  2915 00001A53 7765                <1> 	JA	short NON_DRV1		; DRIVE INVALID
  2916                              <1> 	;MOV	BYTE [BP+5],1		; MAXIMUM HEAD NUMBER =	1
  2917 00001A55 C6450901            <1> 	mov	byte [ebp+9], 1  ; 20/02/2015	
  2918 00001A59 E88C080000          <1> 	CALL	CMOS_TYPE		; RETURN DRIVE TYPE IN AL
  2919                              <1> 	;;20/02/2015
  2920                              <1> 	;;JC	short CHK_EST		; IF CMOS BAD CHECKSUM ESTABLISHED
  2921                              <1> 	;;OR	AL,AL			; TEST FOR NO DRIVE TYPE
  2922 00001A5E 7412                <1> 	JZ	short CHK_EST		; JUMP IF SO
  2923 00001A60 E8FD010000          <1> 	CALL	DR_TYPE_CHECK		; RTN CS:BX = MEDIA/DRIVE PARAM TBL
  2924 00001A65 720B                <1> 	JC	short CHK_EST		; TYPE NOT IN TABLE (POSSIBLE BAD CMOS)
  2925                              <1> 	;MOV	[BP+2],AL		; STORE VALID CMOS DRIVE TYPE
  2926 00001A67 884504              <1>         mov	[ebp+4], al ; 06/02/2015
  2927 00001A6A 8A4B04              <1> 	MOV     CL, [eBX+MD.SEC_TRK]     ; GET SECTOR/TRACK
  2928 00001A6D 8A6B0B              <1>         MOV     CH, [eBX+MD.MAX_TRK]     ; GET MAX. TRACK NUMBER
  2929 00001A70 EB36                <1> 	JMP	SHORT STO_CX		; CMOS GOOD, USE CMOS
  2930                              <1> CHK_EST:
  2931 00001A72 8AA7[49700000]      <1> 	MOV	AH, [DSK_STATE+eDI]	; LOAD STATE FOR THIS DRIVE
  2932 00001A78 F6C410              <1> 	TEST	AH,MED_DET		; CHECK FOR ESTABLISHED STATE
  2933 00001A7B 743D                <1> 	JZ	short NON_DRV1		; CMOS BAD/INVALID OR UNESTABLISHED
  2934                              <1> USE_EST:
  2935 00001A7D 80E4C0              <1> 	AND	AH,RATE_MSK		; ISOLATE STATE
  2936 00001A80 80FC80              <1> 	CMP	AH,RATE_250		; RATE 250 ?
  2937 00001A83 7555                <1> 	JNE	short USE_EST2		; NO, GO CHECK OTHER RATE
  2938                              <1> 
  2939                              <1> ;-----	DATA RATE IS 250 KBS, TRY 360 KB TABLE FIRST
  2940                              <1> 
  2941 00001A85 B001                <1> 	MOV	AL,01			; DRIVE TYPE 1 (360KB)
  2942 00001A87 E8D6010000          <1> 	CALL	DR_TYPE_CHECK		; RTN CS:BX = MEDIA/DRIVE PARAM TBL
  2943 00001A8C 8A4B04              <1>         MOV     CL, [eBX+MD.SEC_TRK]    ; GET SECTOR/TRACK
  2944 00001A8F 8A6B0B              <1>         MOV     CH, [eBX+MD.MAX_TRK]    ; GET MAX. TRACK NUMBER
  2945 00001A92 F687[49700000]01    <1> 	TEST	byte [DSK_STATE+eDI],TRK_CAPA ; 80 TRACK ?
  2946 00001A99 740D                <1> 	JZ	short STO_CX		; MUST BE 360KB DRIVE 
  2947                              <1> 
  2948                              <1> ;-----	IT IS 1.44 MB DRIVE
  2949                              <1> 
  2950                              <1> PARM144:
  2951 00001A9B B004                <1> 	MOV	AL,04			; DRIVE TYPE 4 (1.44MB)
  2952 00001A9D E8C0010000          <1> 	CALL	DR_TYPE_CHECK		; RTN CS:BX = MEDIA/DRIVE PARAM TBL
  2953 00001AA2 8A4B04              <1>         MOV     CL, [eBX+MD.SEC_TRK]    ; GET SECTOR/TRACK
  2954 00001AA5 8A6B0B              <1>         MOV     CH, [eBX+MD.MAX_TRK]    ; GET MAX. TRACK NUMBER
  2955                              <1> STO_CX:
  2956 00001AA8 894D00              <1> 	MOV	[eBP],eCX		; SAVE POINTER IN STACK FOR RETURN
  2957                              <1> ES_DI:
  2958                              <1> 	;MOV	[BP+6],BX		; ADDRESS OF MEDIA/DRIVE PARM TABLE 
  2959 00001AAB 895D0C              <1> 	mov	[ebp+12], ebx ; 06/02/2015
  2960                              <1> 	;MOV	AX,CS			; SEGMENT MEDIA/DRIVE PARAMETER TABLE
  2961                              <1> 	;MOV	ES,AX			; ES IS SEGMENT OF TABLE
  2962                              <1> DP_OUT:
  2963 00001AAE E852020000          <1> 	CALL	XLAT_OLD		; TRANSLATE STATE TO COMPATIBLE MODE
  2964                              <1> 	;XOR	AX,AX			; CLEAR
  2965                              <1> 	; 05/12/2021
  2966 00001AB3 31C0                <1> 	xor	eax, eax
  2967 00001AB5 F8                  <1> 	CLC
  2968 00001AB6 C3                  <1> 	RETn
  2969                              <1> 
  2970                              <1> ;-----	NO DRIYE PRESENT HANDLER
  2971                              <1> 
  2972                              <1> NON_DRV:
  2973                              <1> 	;MOV	BYTE [BP+4],0		; CLEAR NUMBER OF DRIVES
  2974 00001AB7 895508              <1> 	mov	[ebp+8], edx ; 0 ; 20/02/2015
  2975                              <1> NON_DRV1:
  2976 00001ABA 6681FF8000          <1> 	CMP	DI,80H			; CHECK FOR FIXED MEDIA TYPE REQUEST
  2977 00001ABF 720B                <1> 	JB	short NON_DRV2		; CONTINUE IF NOT REQUEST FALL THROUGH
  2978                              <1> 
  2979                              <1> ;-----	FIXED DISK REQUEST FALL THROUGH ERROR
  2980                              <1> 	
  2981 00001AC1 E83F020000          <1> 	CALL	XLAT_OLD		; ELSE TRANSLATE TO COMPATIBLE MODE
  2982                              <1> 	;MOV	AX,SI			; RESTORE AL
  2983 00001AC6 89F0                <1> 	mov	eax, esi ; 05/12/2021
  2984 00001AC8 B401                <1> 	MOV	AH,BAD_CMD		; SET BAD COMMAND ERROR
  2985 00001ACA F9                  <1> 	STC
  2986 00001ACB C3                  <1> 	RETn
  2987                              <1> 
  2988                              <1> NON_DRV2:
  2989                              <1> 	;XOR	AX,AX			; CLEAR PARMS IF NO DRIVES OR CMOS BAD
  2990 00001ACC 31C0                <1> 	xor	eax, eax	
  2991 00001ACE 66894500            <1> 	MOV	[eBP],AX		; TRACKS, SECTORS/TRACK = 0
  2992                              <1> 	;MOV	[BP+5],AH		; HEAD = 0
  2993 00001AD2 886509              <1> 	mov	[ebp+9], ah ; 06/02/2015
  2994                              <1> 	;MOV	[BP+6],AX		; OFFSET TO DISK_BASE = 0
  2995 00001AD5 89450C              <1> 	mov	[ebp+12], eax
  2996                              <1> 	;MOV	ES,AX			; ES IS SEGMENT OF TABLE
  2997 00001AD8 EBD4                <1> 	JMP	SHORT DP_OUT
  2998                              <1> 
  2999                              <1> ;-----	DATA RATE IS EITHER 300 KBS OR 500 KBS, TRY 1.2 MB TABLE FIRST
  3000                              <1> 
  3001                              <1> USE_EST2:
  3002 00001ADA B002                <1> 	MOV	AL,02			; DRIVE TYPE 2 (1.2MB)
  3003 00001ADC E881010000          <1> 	CALL	DR_TYPE_CHECK		; RTN CS:BX = MEDIA/DRIVE PARAM TBL
  3004 00001AE1 8A4B04              <1>         MOV     CL, [eBX+MD.SEC_TRK]    ; GET SECTOR/TRACK
  3005 00001AE4 8A6B0B              <1>         MOV     CH, [eBX+MD.MAX_TRK]    ; GET MAX. TRACK NUMBER
  3006 00001AE7 80FC40              <1> 	CMP	AH,RATE_300		; RATE 300 ?
  3007 00001AEA 74BC                <1> 	JZ	short STO_CX		; MUST BE 1.2MB DRIVE
  3008 00001AEC EBAD                <1> 	JMP	SHORT PARM144		; ELSE, IT IS 1.44MB DRIVE 
  3009                              <1> 
  3010                              <1> ;-------------------------------------------------------------------------------
  3011                              <1> ; DISK_TYPE (AH = 15H)	
  3012                              <1> ;	THIS ROUTINE RETURNS THE TYPE OF MEDIA INSTALLED.
  3013                              <1> ;
  3014                              <1> ;  ON ENTRY:	DI = DRIVE #
  3015                              <1> ;
  3016                              <1> ;  ON EXIT:	AH = DRIVE TYPE, CY=0
  3017                              <1> ;-------------------------------------------------------------------------------
  3018                              <1> DSK_TYPE:
  3019 00001AEE E8E1010000          <1> 	CALL	XLAT_NEW		; TRANSLATE STATE TO PRESENT ARCH.
  3020 00001AF3 8A87[49700000]      <1> 	MOV	AL, [DSK_STATE+eDI]	; GET PRESENT STATE INFORMATION
  3021 00001AF9 08C0                <1> 	OR	AL,AL			; CHECK FOR NO DRIVE
  3022 00001AFB 7415                <1> 	JZ	short NO_DRV
  3023 00001AFD B401                <1> 	MOV	AH,NOCHGLN		; NO CHANGE LINE FOR 40 TRACK DRIVE
  3024 00001AFF A801                <1> 	TEST	AL,TRK_CAPA		; IS THIS DRIVE AN 80 TRACK DRIVE?
  3025 00001B01 7402                <1> 	JZ	short DT_BACK			; IF NO JUMP
  3026 00001B03 B402                <1> 	MOV	AH,CHGLN		; CHANGE LINE FOR 80 TRACK DRIVE
  3027                              <1> DT_BACK:
  3028                              <1> 	;PUSH	AX			; SAVE RETURN VALUE
  3029 00001B05 50                  <1> 	push	eax ; 05/12/2021
  3030 00001B06 E8FA010000          <1> 	CALL	XLAT_OLD		; TRANSLATE STATE TO COMPATIBLE MODE
  3031                              <1> 	;POP	AX			; RESTORE RETURN VALUE
  3032 00001B0B 58                  <1> 	pop	eax ; 05/12/2021
  3033 00001B0C F8                  <1> 	CLC				; NO ERROR
  3034                              <1> 	;MOV	BX,SI			; GET SAVED AL TO BL
  3035                              <1> 	; 05/12/2021
  3036 00001B0D 89F3                <1> 	mov	ebx, esi
  3037 00001B0F 88D8                <1> 	MOV	AL,BL			; PUT BACK FOR RETURN
  3038 00001B11 C3                  <1> 	RETn
  3039                              <1> NO_DRV:	
  3040 00001B12 30E4                <1> 	XOR	AH,AH			; NO DRIVE PRESENT OR UNKNOWN
  3041 00001B14 EBEF                <1> 	JMP	SHORT DT_BACK
  3042                              <1> 
  3043                              <1> ;-------------------------------------------------------------------------------
  3044                              <1> ; DISK_CHANGE	(AH = 16H)
  3045                              <1> ;	THIS ROUTINE RETURNS THE STATE OF THE DISK CHANGE LINE.
  3046                              <1> ;
  3047                              <1> ; ON ENTRY:	DI = DRIVE #
  3048                              <1> ;
  3049                              <1> ; ON EXIT:	AH = @DSKETTE_STATUS
  3050                              <1> ;		     00 - DISK CHANGE LINE INACTIVE, CY = 0
  3051                              <1> ;		     06 - DISK CHANGE LINE ACTIVE, CY = 1
  3052                              <1> ;-------------------------------------------------------------------------------
  3053                              <1> DSK_CHANGE:
  3054 00001B16 E8B9010000          <1> 	CALL	XLAT_NEW		; TRANSLATE STATE TO PRESENT ARCH.
  3055 00001B1B 8A87[49700000]      <1> 	MOV	AL, [DSK_STATE+eDI]	; GET MEDIA STATE INFORMATION
  3056 00001B21 08C0                <1> 	OR	AL,AL			; DRIVE PRESENT ?
  3057 00001B23 7421                <1> 	JZ	short DC_NON		; JUMP IF NO DRIVE
  3058 00001B25 A801                <1> 	TEST	AL,TRK_CAPA		; 80 TRACK DRIVE ?
  3059 00001B27 7407                <1> 	JZ	short SETIT		; IF SO , CHECK CHANGE LINE
  3060                              <1> DC0:
  3061 00001B29 E8600A0000          <1>         CALL    READ_DSKCHNG            ; GO CHECK STATE OF DISK CHANGE LINE
  3062 00001B2E 7407                <1> 	JZ	short FINIS		; CHANGE LINE NOT ACTIVE
  3063                              <1> 
  3064 00001B30 C605[3C700000]06    <1> SETIT:	MOV	byte [DSKETTE_STATUS], MEDIA_CHANGE ; INDICATE MEDIA REMOVED
  3065                              <1> 
  3066 00001B37 E8C9010000          <1> FINIS:	CALL	XLAT_OLD		; TRANSLATE STATE TO COMPATIBLE MODE
  3067 00001B3C E8E4060000          <1> 	CALL	SETUP_END		; VARIOUS CLEANUPS
  3068                              <1> 	;MOV	BX,SI			; GET SAVED AL TO BL
  3069                              <1> 	; 05/12/2021
  3070 00001B41 89F3                <1> 	mov	ebx, esi
  3071 00001B43 88D8                <1> 	MOV	AL,BL			; PUT BACK FOR RETURN
  3072 00001B45 C3                  <1> 	RETn
  3073                              <1> DC_NON:
  3074 00001B46 800D[3C700000]80    <1> 	OR	byte [DSKETTE_STATUS], TIME_OUT ; SET TIMEOUT, NO DRIVE
  3075 00001B4D EBE8                <1> 	JMP	SHORT FINIS
  3076                              <1> 
  3077                              <1> ;-------------------------------------------------------------------------------
  3078                              <1> ; FORMAT_SET	(AH = 17H)
  3079                              <1> ;	THIS ROUTINE IS USED TO ESTABLISH THE TYPE OF MEDIA TO BE USED
  3080                              <1> ;	FOR THE FOLLOWING FORMAT OPERATION.
  3081                              <1> ;
  3082                              <1> ; ON ENTRY:	SI LOW = DASD TYPE FOR FORMAT
  3083                              <1> ;		DI     = DRIVE #
  3084                              <1> ;
  3085                              <1> ; ON EXIT:	@DSKETTE_STATUS REFLECTS STATUS
  3086                              <1> ;		AH = @DSKETTE_STATUS
  3087                              <1> ;		CY = 1 IF ERROR
  3088                              <1> ;-------------------------------------------------------------------------------
  3089                              <1> FORMAT_SET:
  3090 00001B4F E880010000          <1> 	CALL	XLAT_NEW		; TRANSLATE STATE TO PRESENT ARCH.
  3091                              <1> 	;PUSH	SI			; SAVE DASD TYPE
  3092                              <1> 	; 05/12/2021
  3093 00001B54 56                  <1> 	push	esi
  3094 00001B55 6689F0              <1> 	MOV	AX,SI			; AH = ? , AL , DASD TYPE
  3095 00001B58 30E4                <1> 	XOR	AH,AH			; AH , 0 , AL , DASD TYPE
  3096 00001B5A 6689C6              <1> 	MOV	SI,AX			; SI = DASD TYPE
  3097 00001B5D 80A7[49700000]0F    <1> 	AND	byte [DSK_STATE+eDI], ~(MED_DET+DBL_STEP+RATE_MSK) ; CLEAR STATE
  3098 00001B64 664E                <1> 	DEC	SI			; CHECK FOR 320/360K MEDIA & DRIVE
  3099 00001B66 7509                <1> 	JNZ	short NOT_320		; BYPASS IF NOT
  3100 00001B68 808F[49700000]90    <1> 	OR	byte [DSK_STATE+eDI], MED_DET+RATE_250 ; SET TO 320/360
  3101 00001B6F EB48                <1> 	JMP	SHORT S0
  3102                              <1> NOT_320:
  3103 00001B71 E8A4030000          <1> 	CALL	MED_CHANGE		; CHECK FOR TIME_OUT
  3104 00001B76 803D[3C700000]80    <1> 	CMP	byte [DSKETTE_STATUS], TIME_OUT
  3105 00001B7D 743A                <1> 	JZ	short S0		; IF TIME OUT TELL CALLER
  3106                              <1> S3:
  3107 00001B7F 664E                <1> 	DEC	SI			; CHECK FOR 320/360K IN 1.2M DRIVE
  3108 00001B81 7509                <1> 	JNZ	short NOT_320_12	; BYPASS IF NOT
  3109 00001B83 808F[49700000]70    <1> 	OR	byte [DSK_STATE+eDI], MED_DET+DBL_STEP+RATE_300 ; SET STATE
  3110 00001B8A EB2D                <1> 	JMP	SHORT S0
  3111                              <1> NOT_320_12:
  3112 00001B8C 664E                <1> 	DEC	SI			; CHECK FOR 1.2M MEDIA IN 1.2M DRIVE
  3113 00001B8E 7509                <1> 	JNZ	short NOT_12		; BYPASS IF NOT
  3114 00001B90 808F[49700000]10    <1> 	OR	byte [DSK_STATE+eDI], MED_DET+RATE_500 ; SET STATE VARIABLE
  3115 00001B97 EB20                <1> 	JMP	SHORT S0		; RETURN TO CALLER
  3116                              <1> NOT_12:	
  3117 00001B99 664E                <1> 	DEC	SI			; CHECK FOR SET DASD TYPE 04
  3118 00001B9B 752A                <1> 	JNZ	short FS_ERR		; BAD COMMAND EXIT IF NOT VALID TYPE
  3119                              <1> 
  3120 00001B9D F687[49700000]04    <1> 	TEST	byte [DSK_STATE+eDI], DRV_DET ; DRIVE DETERMINED ?
  3121 00001BA4 740B                <1> 	JZ	short ASSUME		; IF STILL NOT DETERMINED ASSUME
  3122 00001BA6 B050                <1> 	MOV	AL,MED_DET+RATE_300
  3123 00001BA8 F687[49700000]02    <1>         TEST    byte [DSK_STATE+eDI], FMT_CAPA ; MULTIPLE FORMAT CAPABILITY ?
  3124 00001BAF 7502                <1> 	JNZ	short OR_IT_IN		; IF 1.2 M THEN DATA RATE 300
  3125                              <1> ASSUME:
  3126 00001BB1 B090                <1> 	MOV	AL,MED_DET+RATE_250	; SET UP
  3127                              <1> OR_IT_IN:
  3128 00001BB3 0887[49700000]      <1> 	OR	[DSK_STATE+eDI], AL	; OR IN THE CORRECT STATE
  3129                              <1> S0:
  3130 00001BB9 E847010000          <1> 	CALL	XLAT_OLD		; TRANSLATE STATE TO COMPATIBLE MODE
  3131 00001BBE E862060000          <1> 	CALL	SETUP_END		; VARIOUS CLEANUPS
  3132                              <1> 	;POP	BX			; GET SAVED AL TO BL
  3133                              <1> 	; 05/12/2021
  3134 00001BC3 5B                  <1> 	pop	ebx
  3135 00001BC4 88D8                <1> 	MOV	AL,BL			; PUT BACK FOR RETURN
  3136 00001BC6 C3                  <1> 	RETn
  3137                              <1> FS_ERR:
  3138 00001BC7 C605[3C700000]01    <1> 	MOV	byte [DSKETTE_STATUS], BAD_CMD ; UNKNOWN STATE,BAD COMMAND
  3139 00001BCE EBE9                <1> 	JMP	SHORT S0
  3140                              <1> 
  3141                              <1> ;-------------------------------------------------------------------------------
  3142                              <1> ; SET_MEDIA	(AH = 18H)
  3143                              <1> ;	THIS ROUTINE SETS THE TYPE OF MEDIA AND DATA RATE 
  3144                              <1> ;	TO BE USED FOR THE FOLLOWING FORMAT OPERATION.
  3145                              <1> ;
  3146                              <1> ; ON ENTRY:
  3147                              <1> ;	[BP]	= SECTOR PER TRACK
  3148                              <1> ;	[BP+1]	= TRACK #
  3149                              <1> ;	DI	= DRIVE #
  3150                              <1> ;
  3151                              <1> ; ON EXIT:
  3152                              <1> ;	@DSKETTE_STATUS REFLECTS STATUS
  3153                              <1> ;	IF NO ERROR:
  3154                              <1> ;		AH = 0
  3155                              <1> ;		CY = 0
  3156                              <1> ;		ES = SEGMENT OF MEDIA/DRIVE PARAMETER TABLE
  3157                              <1> ;		DI/[BP+6] = OFFSET OF MEDIA/DRIVE PARAMETER TABLE
  3158                              <1> ;	IF ERROR:	
  3159                              <1> ;		AH = @DSKETTE_STATUS
  3160                              <1> ;		CY = 1
  3161                              <1> ;-------------------------------------------------------------------------------
  3162                              <1> SET_MEDIA:
  3163 00001BD0 E8FF000000          <1> 	CALL	XLAT_NEW		; TRANSLATE STATE TO PRESENT ARCH.
  3164 00001BD5 F687[49700000]01    <1>         TEST    byte [DSK_STATE+eDI], TRK_CAPA ; CHECK FOR CHANGE LINE AVAILABLE
  3165 00001BDC 7415                <1> 	JZ	short SM_CMOS		; JUMP IF 40 TRACK DRIVE
  3166 00001BDE E837030000          <1> 	CALL	MED_CHANGE		; RESET CHANGE LINE
  3167 00001BE3 803D[3C700000]80    <1> 	CMP	byte [DSKETTE_STATUS], TIME_OUT ; IF TIME OUT TELL CALLER
  3168 00001BEA 746B                <1> 	JE	short SM_RTN
  3169 00001BEC C605[3C700000]00    <1> 	MOV	byte [DSKETTE_STATUS], 0 ; CLEAR STATUS
  3170                              <1> SM_CMOS:
  3171 00001BF3 E8F2060000          <1> 	CALL	CMOS_TYPE		; RETURN DRIVE TYPE IN (AL)
  3172                              <1> 	;;20/02/2015
  3173                              <1> 	;;JC	short MD_NOT_FND	; ERROR IN CMOS
  3174                              <1> 	;;OR	AL,AL			; TEST FOR NO DRIVE
  3175 00001BF8 745D                <1> 	JZ	short SM_RTN		; RETURN IF SO
  3176 00001BFA E863000000          <1> 	CALL	DR_TYPE_CHECK		; RTN CS:BX = MEDIA/DRIVE PARAM TBL
  3177 00001BFF 7231                <1> 	JC	short MD_NOT_FND	; TYPE NOT IN TABLE (BAD CMOS)
  3178 00001C01 57                  <1> 	PUSH	eDI			; SAVE REG.
  3179 00001C02 31DB                <1> 	XOR	eBX,eBX			; BX = INDEX TO DR. TYPE TABLE
  3180 00001C04 B906000000          <1> 	MOV	eCX,DR_CNT		; CX = LOOP COUNT
  3181                              <1> DR_SEARCH:
  3182 00001C09 8AA3[2C6A0000]      <1> 	MOV	AH, [DR_TYPE+eBX]	; GET DRIVE TYPE
  3183 00001C0F 80E47F              <1> 	AND	AH,BIT7OFF		; MASK OUT MSB
  3184 00001C12 38E0                <1> 	CMP	AL,AH			; DRIVE TYPE MATCH ?
  3185 00001C14 7516                <1> 	JNE	short NXT_MD		; NO, CHECK NEXT DRIVE TYPE
  3186                              <1> DR_FND:
  3187 00001C16 8BBB[2D6A0000]      <1> 	MOV	eDI, [DR_TYPE+eBX+1] 	; DI = MEDIA/DRIVE PARAM TABLE
  3188                              <1> MD_SEARCH:
  3189 00001C1C 8A6704              <1>         MOV     AH, [eDI+MD.SEC_TRK]    ; GET SECTOR/TRACK
  3190 00001C1F 386500              <1> 	CMP	[eBP],AH		; MATCH?
  3191 00001C22 7508                <1> 	JNE	short NXT_MD		; NO, CHECK NEXT MEDIA
  3192 00001C24 8A670B              <1>         MOV     AH, [eDI+MD.MAX_TRK]    ; GET MAX. TRACK #
  3193 00001C27 386501              <1> 	CMP 	[eBP+1],AH		; MATCH?
  3194 00001C2A 740F                <1> 	JE	short MD_FND		; YES, GO GET RATE
  3195                              <1> NXT_MD:
  3196                              <1> 	;ADD	BX,3			; CHECK NEXT DRIVE TYPE
  3197 00001C2C 83C305              <1>         add	ebx, 5 ; 18/02/2015
  3198 00001C2F E2D8                <1> 	LOOP    DR_SEARCH
  3199 00001C31 5F                  <1> 	POP	eDI			; RESTORE REG.
  3200                              <1> MD_NOT_FND:
  3201 00001C32 C605[3C700000]0C    <1> 	MOV	byte [DSKETTE_STATUS], MED_NOT_FND ; ERROR, MEDIA TYPE NOT FOUND
  3202 00001C39 EB1C                <1> 	JMP	SHORT SM_RTN		; RETURN
  3203                              <1> MD_FND:
  3204 00001C3B 8A470C              <1>         MOV     AL, [eDI+MD.RATE]       ; GET RATE
  3205 00001C3E 3C40                <1> 	CMP	AL,RATE_300		; DOUBLE STEP REQUIRED FOR RATE 300
  3206 00001C40 7502                <1> 	JNE	short MD_SET
  3207 00001C42 0C20                <1> 	OR	AL,DBL_STEP
  3208                              <1> MD_SET:
  3209                              <1> 	;MOV	[BP+6],DI		; SAVE TABLE POINTER IN STACK
  3210 00001C44 897D0C              <1> 	mov	[ebp+12], edi ; 18/02/2015
  3211 00001C47 0C10                <1> 	OR	AL,MED_DET		; SET MEDIA ESTABLISHED
  3212 00001C49 5F                  <1> 	POP	eDI
  3213 00001C4A 80A7[49700000]0F    <1> 	AND	byte [DSK_STATE+eDI], ~(MED_DET+DBL_STEP+RATE_MSK) ; CLEAR STATE
  3214 00001C51 0887[49700000]      <1> 	OR	[DSK_STATE+eDI], AL
  3215                              <1> 	;MOV	AX, CS			; SEGMENT OF MEDIA/DRIVE PARAMETER TABLE
  3216                              <1> 	;MOV	ES, AX			; ES IS SEGMENT OF TABLE
  3217                              <1> SM_RTN:
  3218 00001C57 E8A9000000          <1> 	CALL	XLAT_OLD		; TRANSLATE STATE TO COMPATIBLE MODE
  3219 00001C5C E8C4050000          <1> 	CALL	SETUP_END		; VARIOUS CLEANUPS
  3220 00001C61 C3                  <1> 	RETn
  3221                              <1> 
  3222                              <1> ;----------------------------------------------------------------
  3223                              <1> ; DR_TYPE_CHECK							:
  3224                              <1> ;	CHECK IF THE GIVEN DRIVE TYPE IN REGISTER (AL)		:
  3225                              <1> ;	IS SUPPORTED IN BIOS DRIVE TYPE TABLE			:
  3226                              <1> ; ON ENTRY:							:
  3227                              <1> ;	AL = DRIVE TYPE						:
  3228                              <1> ; ON EXIT:							:
  3229                              <1> ;	CS = SEGMENT MEDIA/DRIVE PARAMETER TABLE (CODE)		:
  3230                              <1> ;	CY = 0 	DRIVE TYPE SUPPORTED				:
  3231                              <1> ;	     BX = OFFSET TO MEDIA/DRIVE PARAMETER TABLE		:
  3232                              <1> ;	CY = 1	DRIVE TYPE NOT SUPPORTED 			:
  3233                              <1> ; REGISTERS ALTERED: eBX						:
  3234                              <1> ;----------------------------------------------------------------		
  3235                              <1> DR_TYPE_CHECK:
  3236                              <1> 	;PUSH	AX			
  3237                              <1> 	; 05/12/2021
  3238 00001C62 50                  <1> 	push	eax
  3239 00001C63 51                  <1> 	PUSH	eCX
  3240 00001C64 31DB                <1> 	XOR	eBX,eBX			; BX = INDEX TO DR_TYPE TABLE
  3241 00001C66 B906000000          <1> 	MOV	eCX,DR_CNT		; CX = LOOP COUNT
  3242                              <1> TYPE_CHK:	
  3243 00001C6B 8AA3[2C6A0000]      <1> 	MOV	AH,[DR_TYPE+eBX]	; GET DRIVE TYPE
  3244 00001C71 38E0                <1> 	CMP	AL,AH			; DRIVE TYPE MATCH?
  3245 00001C73 740D                <1> 	JE	short DR_TYPE_VALID	; YES, RETURN WITH CARRY RESET
  3246                              <1> 	;ADD	BX,3			; CHECK NEXT DRIVE TYPE
  3247 00001C75 83C305              <1>         add	ebx, 5	; 16/02/2015 (32 bit address modification)
  3248 00001C78 E2F1                <1> 	LOOP    TYPE_CHK
  3249                              <1> 	;
  3250 00001C7A BB[8B6A0000]        <1> 	mov	ebx, MD_TBL6		; 1.44MB fd parameter table
  3251                              <1> 					; Default for GET_PARM (11/12/2014)
  3252                              <1> 	;
  3253 00001C7F F9                  <1> 	STC				; DRIVE TYPE NOT FOUND IN TABLE
  3254 00001C80 EB06                <1> 	JMP	SHORT TYPE_RTN
  3255                              <1> DR_TYPE_VALID:
  3256 00001C82 8B9B[2D6A0000]      <1> 	MOV	eBX,[DR_TYPE+eBX+1] 	; BX = MEDIA TABLE
  3257                              <1> TYPE_RTN:
  3258 00001C88 59                  <1> 	POP	eCX
  3259                              <1> 	;POP	AX
  3260                              <1> 	; 05/12/2021
  3261 00001C89 58                  <1> 	pop	eax
  3262 00001C8A C3                  <1> 	RETn	
  3263                              <1> 		
  3264                              <1> ;----------------------------------------------------------------
  3265                              <1> ; SEND_SPEC							:
  3266                              <1> ;	SEND THE SPECIFY COMMAND TO CONTROLLER USING DATA FROM	:
  3267                              <1> ;	THE DRIVE PARAMETER TABLE POINTED BY @DISK_POINTER	:
  3268                              <1> ; ON ENTRY:	@DISK_POINTER = DRIVE PARAMETER TABLE		:
  3269                              <1> ; ON EXIT:	NONE						:	
  3270                              <1> ; REGISTERS ALTERED: CX, DX					:
  3271                              <1> ;----------------------------------------------------------------		
  3272                              <1> SEND_SPEC:
  3273 00001C8B 50                  <1> 	PUSH	eAX			; SAVE AX
  3274 00001C8C B8[B21C0000]        <1> 	MOV	eAX, SPECBAC		; LOAD ERROR ADDRESS
  3275 00001C91 50                  <1> 	PUSH	eAX			; PUSH NEC_OUT ERROR RETURN
  3276 00001C92 B403                <1> 	MOV	AH,03H			; SPECIFY COMMAND
  3277 00001C94 E860070000          <1> 	CALL	NEC_OUTPUT		; OUTPUT THE COMMAND
  3278 00001C99 28D2                <1> 	SUB	DL,DL			; FIRST SPECIFY BYTE
  3279 00001C9B E853060000          <1> 	CALL	GET_PARM		; GET PARAMETER TO AH
  3280 00001CA0 E854070000          <1> 	CALL	NEC_OUTPUT		; OUTPUT THE COMMAND
  3281 00001CA5 B201                <1> 	MOV	DL,1			; SECOND SPECIFY BYTE
  3282 00001CA7 E847060000          <1> 	CALL	GET_PARM		; GET PARAMETER TO AH
  3283 00001CAC E848070000          <1> 	CALL	NEC_OUTPUT		; OUTPUT THE COMMAND
  3284 00001CB1 58                  <1> 	POP	eAX			; POP ERROR RETURN
  3285                              <1> SPECBAC:
  3286 00001CB2 58                  <1> 	POP	eAX			; RESTORE ORIGINAL AX VALUE
  3287 00001CB3 C3                  <1> 	RETn
  3288                              <1> 
  3289                              <1> ;----------------------------------------------------------------
  3290                              <1> ; SEND_SPEC_MD							:
  3291                              <1> ;	SEND THE SPECIFY COMMAND TO CONTROLLER USING DATA FROM	:
  3292                              <1> ;	THE MEDIA/DRIVE PARAMETER TABLE POINTED BY (CS:BX)	:
  3293                              <1> ; ON ENTRY:	CS:BX = MEDIA/DRIVE PARAMETER TABLE		:
  3294                              <1> ; ON EXIT:	NONE						:	
  3295                              <1> ; REGISTERS ALTERED: AX						:
  3296                              <1> ;----------------------------------------------------------------		
  3297                              <1> SEND_SPEC_MD:
  3298 00001CB4 50                  <1> 	PUSH	eAX			; SAVE RATE DATA
  3299 00001CB5 B8[D21C0000]        <1> 	MOV	eAX, SPEC_ESBAC		; LOAD ERROR ADDRESS
  3300 00001CBA 50                  <1> 	PUSH	eAX			; PUSH NEC_OUT ERROR RETURN
  3301 00001CBB B403                <1> 	MOV	AH,03H			; SPECIFY COMMAND
  3302 00001CBD E837070000          <1> 	CALL	NEC_OUTPUT		; OUTPUT THE COMMAND
  3303 00001CC2 8A23                <1>         MOV     AH, [eBX+MD.SPEC1]      ; GET 1ST SPECIFY BYTE
  3304 00001CC4 E830070000          <1> 	CALL	NEC_OUTPUT		; OUTPUT THE COMMAND
  3305 00001CC9 8A6301              <1>         MOV     AH, [eBX+MD.SPEC2]      ; GET SECOND SPECIFY BYTE
  3306 00001CCC E828070000          <1> 	CALL	NEC_OUTPUT		; OUTPUT THE COMMAND
  3307 00001CD1 58                  <1> 	POP	eAX			; POP ERROR RETURN
  3308                              <1> SPEC_ESBAC:
  3309 00001CD2 58                  <1> 	POP	eAX			; RESTORE ORIGINAL AX VALUE
  3310 00001CD3 C3                  <1> 	RETn
  3311                              <1> 
  3312                              <1> ;-------------------------------------------------------------------------------
  3313                              <1> ; XLAT_NEW  
  3314                              <1> ;	TRANSLATES DISKETTE STATE LOCATIONS FROM COMPATIBLE
  3315                              <1> ;	MODE TO NEW ARCHITECTURE.
  3316                              <1> ;
  3317                              <1> ; ON ENTRY:	DI = DRIVE #
  3318                              <1> ;-------------------------------------------------------------------------------
  3319                              <1> XLAT_NEW:
  3320 00001CD4 83FF01              <1> 	CMP	eDI,1				; VALID DRIVE
  3321 00001CD7 7725                <1> 	JA	short XN_OUT			; IF INVALID BACK
  3322 00001CD9 80BF[49700000]00    <1> 	CMP	byte [DSK_STATE+eDI], 0		; NO DRIVE ?
  3323 00001CE0 741D                <1> 	JZ	short DO_DET			; IF NO DRIVE ATTEMPT DETERMINE
  3324 00001CE2 6689F9              <1> 	MOV	CX,DI				; CX = DRIVE NUMBER
  3325 00001CE5 C0E102              <1> 	SHL	CL,2				; CL = SHIFT COUNT, A=0, B=4
  3326 00001CE8 A0[48700000]        <1> 	MOV	AL, [HF_CNTRL]			; DRIVE INFORMATION
  3327 00001CED D2C8                <1> 	ROR	AL,CL				; TO LOW NIBBLE
  3328 00001CEF 2407                <1> 	AND	AL,DRV_DET+FMT_CAPA+TRK_CAPA	; KEEP DRIVE BITS
  3329 00001CF1 80A7[49700000]F8    <1>         AND     byte [DSK_STATE+eDI], ~(DRV_DET+FMT_CAPA+TRK_CAPA)
  3330 00001CF8 0887[49700000]      <1> 	OR	[DSK_STATE+eDI], AL		; UPDATE DRIVE STATE
  3331                              <1> XN_OUT:
  3332 00001CFE C3                  <1> 	RETn
  3333                              <1> DO_DET:
  3334 00001CFF E897080000          <1> 	CALL	DRIVE_DET			; TRY TO DETERMINE
  3335 00001D04 C3                  <1> 	RETn
  3336                              <1> 
  3337                              <1> ;-------------------------------------------------------------------------------
  3338                              <1> ; XLAT_OLD 
  3339                              <1> ;	TRANSLATES DISKETTE STATE LOCATIONS FROM NEW
  3340                              <1> ;	ARCHITECTURE TO COMPATIBLE MODE.
  3341                              <1> ;
  3342                              <1> ; ON ENTRY:	DI = DRIVE
  3343                              <1> ;-------------------------------------------------------------------------------
  3344                              <1> XLAT_OLD:
  3345 00001D05 83FF01              <1> 	CMP	eDI,1			; VALID DRIVE ?
  3346                              <1>         ;JA     short XO_OUT            ; IF INVALID BACK
  3347                              <1>         ;ja	XO_OUT
  3348                              <1>         ; 05/12/2021
  3349 00001D08 77F4                <1> 	ja	short XN_OUT
  3350 00001D0A 80BF[49700000]00    <1> 	CMP	byte [DSK_STATE+eDI],0	; NO DRIVE ?
  3351 00001D11 747D                <1> 	JZ	short XO_OUT		; IF NO DRIVE TRANSLATE DONE
  3352                              <1> 
  3353                              <1> ;-----	TEST FOR SAVED DRIVE INFORMATION ALREADY SET
  3354                              <1> 
  3355 00001D13 6689F9              <1> 	MOV	CX,DI			; CX = DRIVE NUMBER
  3356 00001D16 C0E102              <1> 	SHL	CL,2			; CL = SHIFT COUNT, A=0, B=4
  3357 00001D19 B402                <1> 	MOV	AH,FMT_CAPA		; LOAD MULTIPLE DATA RATE BIT MASK
  3358 00001D1B D2CC                <1> 	ROR	AH,CL			; ROTATE BY MASK
  3359 00001D1D 8425[48700000]      <1> 	TEST	[HF_CNTRL], AH		; MULTIPLE-DATA RATE DETERMINED ?
  3360 00001D23 751C                <1> 	JNZ	short SAVE_SET		; IF SO, NO NEED TO RE-SAVE
  3361                              <1> 
  3362                              <1> ;-----	ERASE DRIVE BITS IN @HF_CNTRL FOR THIS DRIVE
  3363                              <1> 
  3364 00001D25 B407                <1> 	MOV	AH,DRV_DET+FMT_CAPA+TRK_CAPA ; MASK TO KEEP
  3365 00001D27 D2CC                <1> 	ROR	AH,CL			; FIX MASK TO KEEP
  3366 00001D29 F6D4                <1> 	NOT	AH			; TRANSLATE MASK
  3367 00001D2B 2025[48700000]      <1> 	AND	[HF_CNTRL], AH		; KEEP BITS FROM OTHER DRIVE INTACT
  3368                              <1> 
  3369                              <1> ;-----	ACCESS CURRENT DRIVE BITS AND STORE IN @HF_CNTRL
  3370                              <1> 
  3371 00001D31 8A87[49700000]      <1> 	MOV	AL,[DSK_STATE+eDI]	; ACCESS STATE
  3372 00001D37 2407                <1> 	AND	AL,DRV_DET+FMT_CAPA+TRK_CAPA ; KEEP DRIVE BITS
  3373 00001D39 D2C8                <1> 	ROR	AL,CL			; FIX FOR THIS DRIVE
  3374 00001D3B 0805[48700000]      <1> 	OR	[HF_CNTRL], AL		; UPDATE SAVED DRIVE STATE
  3375                              <1> 
  3376                              <1> ;-----	TRANSLATE TO COMPATIBILITY MODE
  3377                              <1> 
  3378                              <1> SAVE_SET:
  3379 00001D41 8AA7[49700000]      <1> 	MOV	AH, [DSK_STATE+eDI]	; ACCESS STATE
  3380 00001D47 88E7                <1> 	MOV	BH,AH			; TO BH FOR LATER
  3381 00001D49 80E4C0              <1> 	AND	AH,RATE_MSK		; KEEP ONLY RATE
  3382 00001D4C 80FC00              <1> 	CMP	AH,RATE_500		; RATE 500 ?
  3383 00001D4F 7410                <1> 	JZ	short CHK_144		; YES 1.2/1.2 OR 1.44/1.44
  3384 00001D51 B001                <1> 	MOV	AL,M3D1U		; AL = 360 IN 1.2 UNESTABLISHED
  3385 00001D53 80FC40              <1> 	CMP	AH,RATE_300		; RATE 300 ?
  3386 00001D56 7518                <1> 	JNZ	short CHK_250		; NO, 360/360, 720/720 OR 720/1.44
  3387 00001D58 F6C720              <1> 	TEST	BH,DBL_STEP		; CHECK FOR DOUBLE STEP
  3388 00001D5B 751F                <1> 	JNZ	short TST_DET		; MUST BE 360 IN 1.2
  3389                              <1> UNKNO:
  3390 00001D5D B007                <1> 	MOV	AL,MED_UNK		; NONE OF THE ABOVE
  3391 00001D5F EB22                <1> 	JMP	SHORT AL_SET		; PROCESS COMPLETE
  3392                              <1> CHK_144:
  3393 00001D61 E884050000          <1> 	CALL	CMOS_TYPE		; RETURN DRIVE TYPE IN (AL)
  3394                              <1> 	;;20/02/2015
  3395                              <1> 	;;JC	short UNKNO		; ERROR, SET 'NONE OF ABOVE'
  3396 00001D66 74F5                <1> 	jz	short UNKNO ;; 20/02/2015
  3397 00001D68 3C02                <1> 	CMP	AL,2			; 1.2MB DRIVE ?
  3398 00001D6A 75F1                <1> 	JNE	short UNKNO		; NO, GO SET 'NONE OF ABOVE'
  3399 00001D6C B002                <1> 	MOV	AL,M1D1U		; AL = 1.2 IN 1.2 UNESTABLISHED
  3400 00001D6E EB0C                <1> 	JMP	SHORT TST_DET
  3401                              <1> CHK_250:
  3402 00001D70 B000                <1> 	MOV	AL,M3D3U		; AL = 360 IN 360 UNESTABLISHED
  3403 00001D72 80FC80              <1> 	CMP	AH,RATE_250		; RATE 250 ?
  3404 00001D75 75E6                <1> 	JNZ	short UNKNO		; IF SO FALL IHRU
  3405 00001D77 F6C701              <1> 	TEST	BH,TRK_CAPA		; 80 TRACK CAPABILITY ?
  3406 00001D7A 75E1                <1> 	JNZ	short UNKNO		; IF SO JUMP, FALL THRU TEST DET
  3407                              <1> TST_DET:
  3408 00001D7C F6C710              <1> 	TEST	BH,MED_DET		; DETERMINED ?
  3409 00001D7F 7402                <1> 	JZ	short AL_SET		; IF NOT THEN SET
  3410 00001D81 0403                <1> 	ADD	AL,3			; MAKE DETERMINED/ESTABLISHED
  3411                              <1> AL_SET:
  3412 00001D83 80A7[49700000]F8    <1> 	AND	byte [DSK_STATE+eDI], ~(DRV_DET+FMT_CAPA+TRK_CAPA) ; CLEAR DRIVE
  3413 00001D8A 0887[49700000]      <1> 	OR	[DSK_STATE+eDI], AL	; REPLACE WITH COMPATIBLE MODE
  3414                              <1> XO_OUT:
  3415 00001D90 C3                  <1> 	RETn
  3416                              <1> 
  3417                              <1> ;-------------------------------------------------------------------------------
  3418                              <1> ; RD_WR_VF
  3419                              <1> ;	COMMON READ, WRITE AND VERIFY: 
  3420                              <1> ;	MAIN LOOP FOR STATE RETRIES.
  3421                              <1> ;
  3422                              <1> ; ON ENTRY:	AH = READ/WRITE/VERIFY NEC PARAMETER
  3423                              <1> ;		AL = READ/WRITE/VERIFY DMA PARAMETER
  3424                              <1> ;
  3425                              <1> ; ON EXIT:	@DSKETTE_STATUS, CY REFLECT STATUS OF OPERATION
  3426                              <1> ;-------------------------------------------------------------------------------
  3427                              <1> RD_WR_VF:
  3428                              <1> 	;PUSH	AX			; SAVE DMA, NEC PARAMETERS
  3429 00001D91 50                  <1> 	push	eax ; 05/12/2021
  3430 00001D92 E83DFFFFFF          <1> 	CALL	XLAT_NEW		; TRANSLATE STATE TO PRESENT ARCH.
  3431 00001D97 E8E9000000          <1> 	CALL	SETUP_STATE		; INITIALIZE START AND END RATE
  3432                              <1> 	;POP	AX			; RESTORE READ/WRITE/VERIFY
  3433 00001D9C 58                  <1> 	pop	eax ; 05/12/2021
  3434                              <1> DO_AGAIN:
  3435                              <1> 	;PUSH	AX			; SAVE READ/WRITE/VERIFY PARAMETER
  3436 00001D9D 50                  <1> 	push	eax ; 05/12/2021
  3437 00001D9E E877010000          <1> 	CALL	MED_CHANGE		; MEDIA CHANGE AND RESET IF CHANGED
  3438                              <1> 	;POP	AX			; RESTORE READ/WRITE/VERIFY
  3439 00001DA3 58                  <1> 	pop	eax ; 05/12/2021
  3440                              <1> 	;JC	RWV_END                 ; MEDIA CHANGE ERROR OR TIME-OUT
  3441                              <1> 	; 05/12/2021
  3442 00001DA4 7305                <1> 	jnc	short RWV
  3443 00001DA6 E9C3000000          <1> 	jmp	RWV_END
  3444                              <1> RWV:
  3445                              <1> 	;PUSH	AX			; SAVE READ/WRITE/VERIFY PARAMETER
  3446 00001DAB 50                  <1> 	push	eax ; 05/12/2021
  3447 00001DAC 8AB7[49700000]      <1> 	MOV	DH, [DSK_STATE+eDI]	; GET RATE STATE OF THIS DRIVE
  3448 00001DB2 80E6C0              <1> 	AND	DH,RATE_MSK		; KEEP ONLY RATE
  3449 00001DB5 E830050000          <1> 	CALL	CMOS_TYPE		; RETURN DRIVE TYPE IN AL (AL)
  3450                              <1> 	;;20/02/2015
  3451                              <1> 	;;JC	short RWV_ASSUME	; ERROR IN CMOS
  3452 00001DBA 7451                <1> 	jz	short RWV_ASSUME ; 20/02/2015
  3453 00001DBC 3C01                <1> 	CMP	AL,1			; 40 TRACK DRIVE?
  3454 00001DBE 750D                <1> 	JNE	short RWV_1		; NO, BYPASS CMOS VALIDITY CHECK
  3455 00001DC0 F687[49700000]01    <1> 	TEST	byte [DSK_STATE+eDI], TRK_CAPA ; CHECK FOR 40 TRACK DRIVE
  3456 00001DC7 7413                <1> 	JZ	short RWV_2		; YES, CMOS IS CORRECT
  3457 00001DC9 B002                <1> 	MOV	AL,2			; CHANGE TO 1.2M
  3458 00001DCB EB0F                <1> 	JMP	SHORT RWV_2
  3459                              <1> RWV_1:
  3460 00001DCD 720D                <1> 	JB	short RWV_2		; NO DRIVE SPECIFIED, CONTINUE
  3461 00001DCF F687[49700000]01    <1> 	TEST    byte [DSK_STATE+eDI], TRK_CAPA ; IS IT REALLY 40 TRACK?
  3462 00001DD6 7504                <1> 	JNZ	short RWV_2		; NO, 80 TRACK
  3463 00001DD8 B001                <1> 	MOV	AL,1			; IT IS 40 TRACK, FIX CMOS VALUE
  3464 00001DDA EB04                <1> 	jmp	short rwv_3
  3465                              <1> RWV_2:
  3466 00001DDC 08C0                <1> 	OR	AL,AL			; TEST FOR NO DRIVE
  3467 00001DDE 742D                <1> 	JZ	short RWV_ASSUME	; ASSUME TYPE, USE MAX TRACK
  3468                              <1> rwv_3:
  3469 00001DE0 E87DFEFFFF          <1> 	CALL	DR_TYPE_CHECK		; RTN CS:BX = MEDIA/DRIVE PARAM TBL.
  3470 00001DE5 7226                <1> 	JC	short RWV_ASSUME	; TYPE NOT IN TABLE (BAD CMOS)
  3471                              <1> 
  3472                              <1> ;-----	SEARCH FOR MEDIA/DRIVE PARAMETER TABLE
  3473                              <1> 
  3474 00001DE7 57                  <1> 	PUSH	eDI			; SAVE DRIVE #
  3475 00001DE8 31DB                <1> 	XOR	eBX,eBX			; BX = INDEX TO DR_TYPE TABLE
  3476 00001DEA B906000000          <1> 	MOV	eCX,DR_CNT		; CX = LOOP COUNT
  3477                              <1> RWV_DR_SEARCH:
  3478 00001DEF 8AA3[2C6A0000]      <1> 	MOV	AH, [DR_TYPE+eBX]	; GET DRIVE TYPE
  3479 00001DF5 80E47F              <1> 	AND	AH,BIT7OFF		; MASK OUT MSB
  3480 00001DF8 38E0                <1> 	CMP	AL,AH			; DRIVE TYPE MATCH?
  3481 00001DFA 750B                <1> 	JNE	short RWV_NXT_MD	; NO, CHECK NEXT DRIVE TYPE
  3482                              <1> RWV_DR_FND:
  3483 00001DFC 8BBB[2D6A0000]      <1> 	MOV	eDI, [DR_TYPE+eBX+1] 	; DI = MEDIA/DRIVE PARAMETER TABLE
  3484                              <1> RWV_MD_SEARH:
  3485 00001E02 3A770C              <1>         CMP     DH, [eDI+MD.RATE]       ; MATCH?
  3486 00001E05 741B                <1> 	JE	short RWV_MD_FND	; YES, GO GET 1ST SPECIFY BYTE
  3487                              <1> RWV_NXT_MD:
  3488                              <1> 	;ADD	BX,3			; CHECK NEXT DRIVE TYPE
  3489 00001E07 83C305              <1> 	add	eBX, 5
  3490 00001E0A E2E3                <1> 	LOOP	RWV_DR_SEARCH
  3491 00001E0C 5F                  <1> 	POP	eDI			; RESTORE DRIVE #
  3492                              <1> 
  3493                              <1> ;-----	ASSUME PRIMARY DRIVE IS INSTALLED AS SHIPPED
  3494                              <1> 
  3495                              <1> RWV_ASSUME:
  3496 00001E0D BB[4A6A0000]        <1> 	MOV	eBX, MD_TBL1		; POINT TO 40 TRACK 250 KBS
  3497 00001E12 F687[49700000]01    <1> 	TEST 	byte [DSK_STATE+eDI], TRK_CAPA ; TEST FOR 80 TRACK
  3498 00001E19 740A                <1> 	JZ	short RWV_MD_FND1	; MUST BE 40 TRACK
  3499 00001E1B BB[646A0000]        <1> 	MOV	eBX, MD_TBL3		; POINT TO 80 TRACK 500 KBS
  3500 00001E20 EB03                <1> 	JMP	short RWV_MD_FND1	; GO SPECIFY PARAMTERS
  3501                              <1> 
  3502                              <1> ;-----	CS:BX POINTS TO MEDIA/DRIVE PARAMETER TABLE
  3503                              <1> 	 			
  3504                              <1> RWV_MD_FND:
  3505 00001E22 89FB                <1> 	MOV	eBX,eDI			; BX = MEDIA/DRIVE PARAMETER TABLE
  3506 00001E24 5F                  <1> 	POP	eDI			; RESTORE DRIVE #
  3507                              <1> 	
  3508                              <1> ;-----	SEND THE SPECIFY COMMAND TO THE CONTROLLER
  3509                              <1> 
  3510                              <1> RWV_MD_FND1:
  3511 00001E25 E88AFEFFFF          <1> 	CALL	SEND_SPEC_MD
  3512 00001E2A E85B010000          <1> 	CALL	CHK_LASTRATE		; ZF=1 ATTEMP RATE IS SAME AS LAST RATE
  3513 00001E2F 7405                <1> 	JZ	short RWV_DBL		; YES,SKIP SEND RATE COMMAND
  3514 00001E31 E834010000          <1> 	CALL	SEND_RATE		; SEND DATA RATE TO NEC
  3515                              <1> RWV_DBL:
  3516 00001E36 53                  <1> 	PUSH	eBX			; SAVE MEDIA/DRIVE PARAM TBL ADDRESS
  3517 00001E37 E809040000          <1> 	CALL	SETUP_DBL		; CHECK FOR DOUBLE STEP
  3518 00001E3C 5B                  <1> 	POP	eBX			; RESTORE ADDRESS
  3519 00001E3D 7222                <1> 	JC	short CHK_RET		; ERROR FROM READ ID, POSSIBLE RETRY
  3520                              <1> 	;POP	AX			; RESTORE NEC, DMA COMMAND
  3521 00001E3F 58                  <1> 	pop	eax ; 05/12/2021
  3522                              <1> 	;PUSH	AX			; SAVE NEC COMMAND
  3523 00001E40 50                  <1> 	push	eax ; 05/12/2021
  3524 00001E41 53                  <1> 	PUSH	eBX			; SAVE MEDIA/DRIVE PARAM TBL ADDRESS
  3525 00001E42 E858010000          <1> 	CALL	DMA_SETUP		; SET UP THE DMA
  3526 00001E47 5B                  <1> 	POP	eBX 
  3527                              <1> 	;POP	AX			; RESTORE NEC COMMAND
  3528 00001E48 58                  <1> 	pop	eax ; 05/12/2021
  3529 00001E49 722D                <1> 	JC	short RWV_BAC		; CHECK FOR DMA BOUNDARY ERROR
  3530                              <1> 	;PUSH	AX			; SAVE NEC COMMAND
  3531 00001E4B 50                  <1> 	push	eax ; 05/12/2021
  3532 00001E4C 53                  <1> 	PUSH	eBX			; SAVE MEDIA/DRIVE PARAM TBL ADDRESS
  3533 00001E4D E82B020000          <1> 	CALL	NEC_INIT		; INITIALIZE NEC
  3534 00001E52 5B                  <1> 	POP	eBX			; RESTORE ADDRESS
  3535 00001E53 720C                <1> 	JC	short CHK_RET		; ERROR - EXIT
  3536 00001E55 E853020000          <1> 	CALL	RWV_COM			; OP CODE COMMON TO READ/WRITE/VERIFY
  3537 00001E5A 7205                <1> 	JC	short CHK_RET		; ERROR - EXIT
  3538 00001E5C E898020000          <1> 	CALL	NEC_TERM		; TERMINATE, GET STATUS, ETC.
  3539                              <1> CHK_RET:
  3540 00001E61 E837030000          <1> 	CALL	RETRY			; CHECK FOR, SETUP RETRY
  3541                              <1> 	;POP	AX			; RESTORE READ/WRITE/VERIFY PARAMETER
  3542 00001E66 58                  <1> 	pop	eax ; 05/12/2021
  3543 00001E67 7305                <1> 	JNC	short RWV_END		; CY = 0 NO RETRY
  3544 00001E69 E92FFFFFFF          <1>         JMP     DO_AGAIN                ; CY = 1 MEANS RETRY
  3545                              <1> RWV_END:
  3546 00001E6E E8E2020000          <1> 	CALL	DSTATE			; ESTABLISH STATE IF SUCCESSFUL
  3547 00001E73 E875030000          <1> 	CALL	NUM_TRANS		; AL = NUMBER TRANSFERRED
  3548                              <1> RWV_BAC:				; BAD DMA ERROR ENTRY
  3549                              <1> 	;PUSH	AX			; SAVE NUMBER TRANSFERRED
  3550 00001E78 50                  <1> 	push	eax ; 05/12/2021
  3551 00001E79 E887FEFFFF          <1> 	CALL	XLAT_OLD		; TRANSLATE STATE TO COMPATIBLE MODE
  3552                              <1> 	;POP	AX			; RESTORE NUMBER TRANSFERRED
  3553 00001E7E 58                  <1> 	pop	eax ; 05/12/2021
  3554 00001E7F E8A1030000          <1> 	CALL	SETUP_END		; VARIOUS CLEANUPS
  3555 00001E84 C3                  <1> 	RETn
  3556                              <1> 
  3557                              <1> ;-------------------------------------------------------------------------------
  3558                              <1> ; SETUP_STATE:	INITIALIZES START AND END RATES.
  3559                              <1> ;-------------------------------------------------------------------------------
  3560                              <1> SETUP_STATE:
  3561 00001E85 F687[49700000]10    <1> 	TEST	byte [DSK_STATE+eDI], MED_DET ; MEDIA DETERMINED ?
  3562 00001E8C 7537                <1> 	JNZ	short J1C		; NO STATES IF DETERMINED
  3563 00001E8E 66B84000            <1>         MOV     AX,(RATE_500*256)+RATE_300  ; AH = START RATE, AL = END RATE
  3564 00001E92 F687[49700000]04    <1> 	TEST	byte [DSK_STATE+eDI],DRV_DET ; DRIVE ?
  3565 00001E99 740D                <1> 	JZ	short AX_SET		; DO NOT KNOW DRIVE
  3566 00001E9B F687[49700000]02    <1> 	TEST	byte [DSK_STATE+eDI], FMT_CAPA ; MULTI-RATE?
  3567 00001EA2 7504                <1> 	JNZ	short AX_SET		; JUMP IF YES
  3568 00001EA4 66B88080            <1>         MOV     AX,RATE_250*257         ; START A END RATE 250 FOR 360 DRIVE
  3569                              <1> AX_SET:	
  3570 00001EA8 80A7[49700000]1F    <1> 	AND	byte [DSK_STATE+eDI], ~(RATE_MSK+DBL_STEP) ; TURN OFF THE RATE
  3571 00001EAF 08A7[49700000]      <1> 	OR	[DSK_STATE+eDI], AH	; RATE FIRST TO TRY
  3572 00001EB5 8025[44700000]F3    <1> 	AND	byte [LASTRATE], ~STRT_MSK ; ERASE LAST TO TRY RATE BITS
  3573 00001EBC C0C804              <1> 	ROR	AL,4			; TO OPERATION LAST RATE LOCATION
  3574 00001EBF 0805[44700000]      <1> 	OR	[LASTRATE], AL		; LAST RATE
  3575                              <1> J1C:	
  3576 00001EC5 C3                  <1> 	RETn
  3577                              <1> 
  3578                              <1> ;-------------------------------------------------------------------------------
  3579                              <1> ;  FMT_INIT: ESTABLISH STATE IF UNESTABLISHED AT FORMAT TIME.
  3580                              <1> ;-------------------------------------------------------------------------------
  3581                              <1> FMT_INIT:
  3582 00001EC6 F687[49700000]10    <1> 	TEST	byte [DSK_STATE+eDI], MED_DET ; IS MEDIA ESTABLISHED
  3583 00001ECD 7546                <1> 	JNZ	short F1_OUT		; IF SO RETURN
  3584 00001ECF E816040000          <1> 	CALL	CMOS_TYPE		; RETURN DRIVE TYPE IN AL
  3585                              <1> 	;; 20/02/2015
  3586                              <1> 	;;JC	short CL_DRV		; ERROR IN CMOS ASSUME NO DRIVE
  3587 00001ED4 7440                <1> 	jz	short CL_DRV ;; 20/02/2015
  3588 00001ED6 FEC8                <1> 	DEC	AL			; MAKE ZERO ORIGIN
  3589                              <1> 	;;JS	short CL_DRV		; NO DRIVE IF AL 0
  3590 00001ED8 8AA7[49700000]      <1> 	MOV	AH, [DSK_STATE+eDI]	; AH = CURRENT STATE
  3591 00001EDE 80E40F              <1> 	AND	AH, ~(MED_DET+DBL_STEP+RATE_MSK) ; CLEAR
  3592 00001EE1 08C0                <1> 	OR	AL,AL			; CHECK FOR 360
  3593 00001EE3 7505                <1> 	JNZ	short N_360		; IF 360 WILL BE 0
  3594 00001EE5 80CC90              <1> 	OR	AH,MED_DET+RATE_250	; ESTABLISH MEDIA
  3595 00001EE8 EB25                <1> 	JMP	SHORT SKP_STATE		; SKIP OTHER STATE PROCESSING
  3596                              <1> N_360:	
  3597 00001EEA FEC8                <1> 	DEC	AL			; 1.2 M DRIVE
  3598 00001EEC 7505                <1> 	JNZ	short N_12		; JUMP IF NOT
  3599                              <1> F1_RATE:
  3600 00001EEE 80CC10              <1> 	OR	AH,MED_DET+RATE_500	; SET FORMAT RATE
  3601 00001EF1 EB1C                <1> 	JMP	SHORT SKP_STATE		; SKIP OTHER STATE PROCESSING
  3602                              <1> N_12:	
  3603 00001EF3 FEC8                <1> 	DEC	AL			; CHECK FOR TYPE 3
  3604 00001EF5 750F                <1> 	JNZ	short N_720		; JUMP IF NOT
  3605 00001EF7 F6C404              <1> 	TEST	AH,DRV_DET		; IS DRIVE DETERMINED
  3606 00001EFA 7410                <1> 	JZ	short ISNT_12		; TREAT AS NON 1.2 DRIVE
  3607 00001EFC F6C402              <1> 	TEST	AH,FMT_CAPA		; IS 1.2M
  3608 00001EFF 740B                <1> 	JZ	short ISNT_12		; JUMP IF NOT
  3609 00001F01 80CC50              <1> 	OR	AH,MED_DET+RATE_300	; RATE 300
  3610 00001F04 EB09                <1> 	JMP	SHORT SKP_STATE		; CONTINUE
  3611                              <1> N_720:
  3612 00001F06 FEC8                <1> 	DEC	AL			; CHECK FOR TYPE 4
  3613 00001F08 750C                <1> 	JNZ	short CL_DRV		; NO DRIVE, CMOS BAD
  3614 00001F0A EBE2                <1> 	JMP	SHORT F1_RATE
  3615                              <1> ISNT_12: 
  3616 00001F0C 80CC90              <1> 	OR	AH,MED_DET+RATE_250	; MUST BE RATE 250
  3617                              <1> 
  3618                              <1> SKP_STATE:
  3619 00001F0F 88A7[49700000]      <1> 	MOV	[DSK_STATE+eDI], AH	; STORE AWAY
  3620                              <1> F1_OUT:
  3621 00001F15 C3                  <1> 	RETn
  3622                              <1> CL_DRV:	
  3623 00001F16 30E4                <1> 	XOR	AH,AH			; CLEAR STATE
  3624 00001F18 EBF5                <1> 	JMP	SHORT SKP_STATE		; SAVE IT
  3625                              <1> 
  3626                              <1> ;-------------------------------------------------------------------------------
  3627                              <1> ; MED_CHANGE	
  3628                              <1> ;	CHECKS FOR MEDIA CHANGE, RESETS MEDIA CHANGE, 
  3629                              <1> ;	CHECKS MEDIA CHANGE AGAIN.
  3630                              <1> ;
  3631                              <1> ; ON EXIT:	CY = 1 MEANS MEDIA CHANGE OR TIMEOUT
  3632                              <1> ;		@DSKETTE_STATUS = ERROR CODE
  3633                              <1> ;-------------------------------------------------------------------------------
  3634                              <1> MED_CHANGE:
  3635 00001F1A E86F060000          <1> 	CALL	READ_DSKCHNG		; READ DISK CHANCE LINE STATE
  3636 00001F1F 7447                <1> 	JZ	short MC_OUT		; BYPASS HANDLING DISK CHANGE LINE
  3637 00001F21 80A7[49700000]EF    <1> 	AND	byte [DSK_STATE+eDI], ~MED_DET ; CLEAR STATE FOR THIS DRIVE
  3638                              <1> 
  3639                              <1> ;	THIS SEQUENCE ENSURES WHENEVER A DISKETTE IS CHANGED THAT
  3640                              <1> ;	ON THE NEXT OPERATION THE REQUIRED MOTOR START UP TIME WILL
  3641                              <1> ;	BE WAITED. (DRIVE MOTOR MAY GO OFF UPON DOOR OPENING).
  3642                              <1> 
  3643 00001F28 6689F9              <1> 	MOV	CX,DI			; CL = DRIVE 0
  3644 00001F2B B001                <1> 	MOV	AL,1			; MOTOR ON BIT MASK
  3645 00001F2D D2E0                <1> 	SHL	AL,CL			; TO APPROPRIATE POSITION
  3646 00001F2F F6D0                <1> 	NOT	AL			; KEEP ALL BUT MOTOR ON
  3647 00001F31 FA                  <1> 	CLI				; NO INTERRUPTS
  3648 00001F32 2005[3A700000]      <1> 	AND	[MOTOR_STATUS], AL	; TURN MOTOR OFF INDICATOR
  3649 00001F38 FB                  <1> 	STI				; INTERRUPTS ENABLED
  3650 00001F39 E8FA030000          <1> 	CALL	MOTOR_ON		; TURN MOTOR ON
  3651                              <1> 
  3652                              <1> ;-----	THIS SEQUENCE OF SEEKS IS USED TO RESET DISKETTE CHANGE SIGNAL
  3653                              <1> 
  3654 00001F3E E8A4F9FFFF          <1> 	CALL	DSK_RESET		; RESET NEC
  3655 00001F43 B501                <1> 	MOV	CH,01H			; MOVE TO CYLINDER 1
  3656 00001F45 E8E8040000          <1> 	CALL	SEEK			; ISSUE SEEK
  3657 00001F4A 30ED                <1> 	XOR	CH,CH			; MOVE TO CYLINDER 0
  3658 00001F4C E8E1040000          <1> 	CALL	SEEK			; ISSUE SEEK
  3659 00001F51 C605[3C700000]06    <1> 	MOV	byte [DSKETTE_STATUS], MEDIA_CHANGE ; STORE IN STATUS
  3660                              <1> OK1:
  3661 00001F58 E831060000          <1> 	CALL	READ_DSKCHNG		; CHECK MEDIA CHANGED AGAIN
  3662 00001F5D 7407                <1> 	JZ	short OK2		; IF ACTIVE, NO DISKETTE, TIMEOUT
  3663                              <1> OK4:
  3664 00001F5F C605[3C700000]80    <1> 	MOV	byte [DSKETTE_STATUS], TIME_OUT ; TIMEOUT IF DRIVE EMPTY
  3665                              <1> OK2:		
  3666 00001F66 F9                  <1> 	STC				; MEDIA CHANGED, SET CY
  3667 00001F67 C3                  <1> 	RETn
  3668                              <1> MC_OUT:
  3669 00001F68 F8                  <1> 	CLC				; NO MEDIA CHANGED, CLEAR CY
  3670 00001F69 C3                  <1> 	RETn
  3671                              <1> 
  3672                              <1> ;-------------------------------------------------------------------------------
  3673                              <1> ; SEND_RATE
  3674                              <1> ;	SENDS DATA RATE COMMAND TO NEC
  3675                              <1> ; ON ENTRY:	DI = DRIVE #
  3676                              <1> ; ON EXIT:	NONE
  3677                              <1> ; REGISTERS ALTERED: DX
  3678                              <1> ;-------------------------------------------------------------------------------
  3679                              <1> SEND_RATE:
  3680                              <1> 	;PUSH	AX			; SAVE REG.
  3681                              <1> 	; 05/12/2021
  3682 00001F6A 50                  <1> 	push	eax
  3683 00001F6B 8025[44700000]3F    <1> 	AND	byte [LASTRATE], ~SEND_MSK ; ELSE CLEAR LAST RATE ATTEMPTED
  3684 00001F72 8A87[49700000]      <1> 	MOV	AL, [DSK_STATE+eDI]	; GET RATE STATE OF THIS DRIVE
  3685 00001F78 24C0                <1> 	AND	AL,SEND_MSK		; KEEP ONLY RATE BITS
  3686 00001F7A 0805[44700000]      <1> 	OR	[LASTRATE], AL		; SAVE NEW RATE FOR NEXT CHECK
  3687 00001F80 C0C002              <1> 	ROL	AL,2			; MOVE TO BIT OUTPUT POSITIONS
  3688 00001F83 66BAF703            <1> 	MOV	DX,03F7H		; OUTPUT NEW DATA RATE
  3689 00001F87 EE                  <1> 	OUT	DX,AL
  3690                              <1> 	;POP	AX			; RESTORE REG.
  3691                              <1> 	; 05/12/2021
  3692 00001F88 58                  <1> 	pop	eax
  3693 00001F89 C3                  <1> 	RETn
  3694                              <1> 
  3695                              <1> ;-------------------------------------------------------------------------------
  3696                              <1> ; CHK_LASTRATE
  3697                              <1> ;	CHECK PREVIOUS DATE RATE SNT TO THE CONTROLLER.
  3698                              <1> ; ON ENTRY:
  3699                              <1> ;	DI = DRIVE #
  3700                              <1> ; ON EXIT:
  3701                              <1> ;	ZF =  1 DATA RATE IS THE SAME AS THE LAST RATE SENT TO NEC
  3702                              <1> ;	ZF =  0 DATA RATE IS DIFFERENT FROM LAST RATE
  3703                              <1> ; REGISTERS ALTERED: DX
  3704                              <1> ;-------------------------------------------------------------------------------
  3705                              <1> CHK_LASTRATE:
  3706                              <1> 	;PUSH	AX			; SAVE REG.
  3707                              <1> 	; 05/12/2021
  3708 00001F8A 50                  <1> 	push	eax
  3709 00001F8B 2225[44700000]      <1> 	AND	AH, [LASTRATE]		; GET LAST DATA RATE SELECTED
  3710 00001F91 8A87[49700000]      <1> 	MOV	AL, [DSK_STATE+eDI]	; GET RATE STATE OF THIS DRIVE
  3711 00001F97 6625C0C0            <1>         AND     AX, SEND_MSK*257        ; KEEP ONLY RATE BITS OF BOTH
  3712 00001F9B 38E0                <1> 	CMP	AL, AH			; COMPARE TO PREVIOUSLY TRIED
  3713                              <1> 					; ZF = 1 RATE IS THE SAME
  3714                              <1> 	;POP	AX			; RESTORE REG.
  3715                              <1> 	; 05/12/2021
  3716 00001F9D 58                  <1> 	pop	eax
  3717 00001F9E C3                  <1> 	RETn
  3718                              <1> 
  3719                              <1> ;-------------------------------------------------------------------------------
  3720                              <1> ; DMA_SETUP
  3721                              <1> ;	THIS ROUTINE SETS UP THE DMA FOR READ/WRITE/VERIFY OPERATIONS.
  3722                              <1> ;
  3723                              <1> ; ON ENTRY:	AL = DMA COMMAND
  3724                              <1> ;
  3725                              <1> ; ON EXIT:	@DSKETTE_STATUS, CY REFLECT STATUS OF OPERATION
  3726                              <1> ;-------------------------------------------------------------------------------
  3727                              <1> 
  3728                              <1> ; SI = Head #, # of Sectors or DASD Type
  3729                              <1> 
  3730                              <1> ; 22/08/2015
  3731                              <1> ; 08/02/2015 - Protected Mode Modification
  3732                              <1> ; 06/02/2015 - 07/02/2015
  3733                              <1> ; NOTE: Buffer address must be in 1st 16MB of Physical Memory (24 bit limit).
  3734                              <1> ; (DMA Addres = Physical Address)
  3735                              <1> ; (Retro UNIX 386 v1 Kernel/System Mode Virtual Address = Physical Address)
  3736                              <1> ;
  3737                              <1> ; 04/02/2016 (clc)
  3738                              <1> ; 20/02/2015 modification (source: AWARD BIOS 1999, DMA_SETUP)
  3739                              <1> ; 16/12/2014 (IODELAY)
  3740                              <1> 
  3741                              <1> DMA_SETUP:
  3742                              <1> 
  3743                              <1> ;; 20/02/2015
  3744 00001F9F 8B5504              <1> 	mov	edx, [ebp+4] 		; Buffer address
  3745 00001FA2 F7C2000000FF        <1> 	test	edx, 0FF000000h		; 16 MB limit (22/08/2015, bugfix)
  3746 00001FA8 7569                <1> 	jnz	short dma_bnd_err_stc
  3747                              <1> 	;
  3748                              <1> 	;push	ax			; DMA command
  3749                              <1> 	; 05/12/2021
  3750 00001FAA 50                  <1> 	push	eax
  3751 00001FAB 52                  <1> 	push	edx			; *
  3752 00001FAC B203                <1> 	mov	dl, 3			; GET BYTES/SECTOR PARAMETER
  3753 00001FAE E840030000          <1> 	call	GET_PARM		; 
  3754 00001FB3 88E1                <1> 	mov	cl, ah 			; SHIFT COUNT (0=128, 1=256, 2=512 ETC)
  3755                              <1> 	;mov	ax, si			; Sector count
  3756                              <1> 	;mov	ah, al			; AH = # OF SECTORS
  3757                              <1> 	;sub	al, al			; AL = 0, AX = # SECTORS * 256
  3758                              <1> 	;shr	ax, 1			; AX = # SECTORS * 128
  3759                              <1> 	;shl	ax, cl			; SHIFT BY PARAMETER VALUE
  3760                              <1> 	; 05/12/2021
  3761 00001FB5 89F2                <1> 	mov	edx, esi
  3762 00001FB7 29C0                <1> 	sub	eax, eax
  3763 00001FB9 88D4                <1> 	mov	ah, dl
  3764 00001FBB D1E8                <1> 	shr	eax, 1
  3765 00001FBD D3E0                <1> 	shl	eax, cl
  3766                              <1> 	;
  3767 00001FBF 6648                <1> 	dec	ax			; -1 FOR DMA VALUE
  3768 00001FC1 6689C1              <1> 	mov	cx, ax
  3769 00001FC4 5A                  <1> 	pop	edx			; *
  3770                              <1> 	;pop	ax
  3771                              <1> 	; 05/12/2021
  3772 00001FC5 58                  <1> 	pop	eax
  3773 00001FC6 3C42                <1> 	cmp	al, 42h
  3774 00001FC8 7507                <1>         jne     short NOT_VERF
  3775 00001FCA BA0000FF00          <1> 	mov	edx, 0FF0000h
  3776 00001FCF EB08                <1> 	jmp	short J33
  3777                              <1> NOT_VERF:
  3778 00001FD1 6601CA              <1> 	add	dx, cx			; check for overflow
  3779 00001FD4 723E                <1> 	jc	short dma_bnd_err
  3780                              <1> 	;
  3781 00001FD6 6629CA              <1> 	sub	dx, cx			; Restore start address
  3782                              <1> J33:
  3783 00001FD9 FA                  <1> 	CLI				; DISABLE INTERRUPTS DURING DMA SET-UP
  3784 00001FDA E60C                <1> 	OUT	DMA+12,AL		; SET THE FIRST/LA5T F/F
  3785                              <1> 	IODELAY				; WAIT FOR I/O
  2127 00001FDC EB00                <2>  jmp short $+2
  2128 00001FDE EB00                <2>  jmp short $+2
  3786 00001FE0 E60B                <1> 	OUT	DMA+11,AL		; OUTPUT THE MODE BYTE
  3787 00001FE2 89D0                <1> 	mov	eax,edx			; Buffer address
  3788 00001FE4 E604                <1> 	OUT	DMA+4,AL		; OUTPUT LOW ADDRESS
  3789                              <1> 	IODELAY				; WAIT FOR I/O
  2127 00001FE6 EB00                <2>  jmp short $+2
  2128 00001FE8 EB00                <2>  jmp short $+2
  3790 00001FEA 88E0                <1> 	MOV	AL,AH
  3791 00001FEC E604                <1> 	OUT	DMA+4,AL		; OUTPUT HIGH ADDRESS
  3792 00001FEE C1E810              <1> 	shr	eax,16
  3793                              <1> 	IODELAY				; I/O WAIT STATE
  2127 00001FF1 EB00                <2>  jmp short $+2
  2128 00001FF3 EB00                <2>  jmp short $+2
  3794 00001FF5 E681                <1> 	OUT	081H,AL			; OUTPUT highest BITS TO PAGE REGISTER
  3795                              <1> 	IODELAY
  2127 00001FF7 EB00                <2>  jmp short $+2
  2128 00001FF9 EB00                <2>  jmp short $+2
  3796 00001FFB 6689C8              <1> 	mov	ax,cx			; Byte count - 1
  3797 00001FFE E605                <1> 	OUT	DMA+5,AL		; LOW BYTE OF COUNT
  3798                              <1> 	IODELAY				; WAIT FOR I/O
  2127 00002000 EB00                <2>  jmp short $+2
  2128 00002002 EB00                <2>  jmp short $+2
  3799 00002004 88E0                <1> 	MOV	AL,AH
  3800 00002006 E605                <1> 	OUT	DMA+5,AL		; HIGH BYTE OF COUNT
  3801                              <1> 	IODELAY
  2127 00002008 EB00                <2>  jmp short $+2
  2128 0000200A EB00                <2>  jmp short $+2
  3802 0000200C FB                  <1> 	STI				; RE-ENABLE INTERRUPTS
  3803 0000200D B002                <1> 	MOV	AL,2			; MODE FOR 8237
  3804 0000200F E60A                <1> 	OUT	DMA+10,AL		; INITIALIZE THE DISKETTE CHANNEL
  3805                              <1> 
  3806 00002011 F8                  <1> 	clc	; 04/02/2016
  3807 00002012 C3                  <1> 	retn
  3808                              <1> 
  3809                              <1> dma_bnd_err_stc:
  3810 00002013 F9                  <1> 	stc
  3811                              <1> dma_bnd_err:
  3812 00002014 C605[3C700000]09    <1> 	MOV	byte [DSKETTE_STATUS], DMA_BOUNDARY ; SET ERROR
  3813 0000201B C3                  <1> 	RETn				; CY SET BY ABOVE IF ERROR
  3814                              <1> 
  3815                              <1> ;; 16/12/2014
  3816                              <1> ;;	CLI				; DISABLE INTERRUPTS DURING DMA SET-UP
  3817                              <1> ;;	OUT	DMA+12,AL		; SET THE FIRST/LA5T F/F
  3818                              <1> ;;	;JMP	$+2			; WAIT FOR I/O
  3819                              <1> ;;	IODELAY
  3820                              <1> ;; 	OUT	DMA+11,AL		; OUTPUT THE MODE BYTE
  3821                              <1> ;;	;SIODELAY
  3822                              <1> ;;      ;CMP	AL, 42H			; DMA VERIFY COMMAND
  3823                              <1> ;;      ;JNE	short NOT_VERF		; NO
  3824                              <1> ;;      ;XOR	AX, AX			; START ADDRESS
  3825                              <1> ;;      ;JMP	SHORT J33
  3826                              <1> ;;;NOT_VERF:	
  3827                              <1> ;;	;MOV	AX,ES			; GET THE ES VALUE
  3828                              <1> ;;	;ROL	AX,4			; ROTATE LEFT
  3829                              <1> ;;	;MOV	CH,AL			; GET HIGHEST NIBBLE OF ES TO CH
  3830                              <1> ;;	;AND	AL,11110000B		; ZERO THE LOW NIBBLE FROM SEGMENT
  3831                              <1> ;;	;ADD	AX,[BP+2]		; TEST FOR CARRY FROM ADDITION
  3832                              <1> ;;	mov	eax, [ebp+4] ; 06/02/2015	
  3833                              <1> ;;	;JNC	short J33
  3834                              <1> ;;	;INC	CH			; CARRY MEANS HIGH 4 BITS MUST BE INC
  3835                              <1> ;;;J33:
  3836                              <1> ;;	PUSH	eAX			; SAVE START ADDRESS
  3837                              <1> ;;	OUT	DMA+4,AL		; OUTPUT LOW ADDRESS
  3838                              <1> ;;	;JMP	$+2			; WAIT FOR I/O
  3839                              <1> ;;	IODELAY
  3840                              <1> ;;	MOV	AL,AH
  3841                              <1> ;;	OUT	DMA+4,AL		; OUTPUT HIGH ADDRESS
  3842                              <1> ;;	shr	eax, 16	     ; 07/02/2015
  3843                              <1> ;;	;MOV	AL,CH			; GET HIGH 4 BITS
  3844                              <1> ;;	;JMP	$+2			; I/O WAIT STATE
  3845                              <1> ;;	IODELAY
  3846                              <1> ;;	;AND	AL,00001111B
  3847                              <1> ;;	OUT	081H,AL			; OUTPUT HIGH 4 BITS TO PAGE REGISTER
  3848                              <1> ;;	;SIODELAY
  3849                              <1> ;;
  3850                              <1> ;;;----- DETERMINE COUNT
  3851                              <1> ;;	sub	eax, eax ; 08/02/2015
  3852                              <1> ;;	MOV	AX, SI			; AL =  # OF SECTORS
  3853                              <1> ;;	XCHG	AL, AH			; AH =  # OF SECTORS
  3854                              <1> ;;	SUB	AL, AL			; AL = 0, AX = # SECTORS * 256
  3855                              <1> ;;	SHR	AX, 1			; AX = # SECTORS * 128
  3856                              <1> ;;	PUSH	AX			; SAVE # OF SECTORS * 128
  3857                              <1> ;;	MOV	DL, 3			; GET BYTES/SECTOR PARAMETER
  3858                              <1> ;;	CALL	GET_PARM		; "
  3859                              <1> ;;	MOV	CL,AH			; SHIFT COUNT (0=128, 1=256, 2=512 ETC)
  3860                              <1> ;;	POP	AX			; AX = # SECTORS * 128
  3861                              <1> ;;	SHL	AX,CL			; SHIFT BY PARAMETER VALUE
  3862                              <1> ;;	DEC	AX			; -1 FOR DMA VALUE
  3863                              <1> ;;	PUSH	eAX  ; 08/02/2015	; SAVE COUNT VALUE
  3864                              <1> ;;	OUT	DMA+5,AL		; LOW BYTE OF COUNT
  3865                              <1> ;;	;JMP	$+2			; WAIT FOR I/O
  3866                              <1> ;;	IODELAY
  3867                              <1> ;;	MOV	AL, AH
  3868                              <1> ;;	OUT	DMA+5,AL		; HIGH BYTE OF COUNT
  3869                              <1> ;;	;IODELAY
  3870                              <1> ;;	STI				; RE-ENABLE INTERRUPTS
  3871                              <1> ;;	POP	eCX  ; 08/02/2015 	; RECOVER COUNT VALUE
  3872                              <1> ;;	POP	eAX  ; 08/02/2015	; RECOVER ADDRESS VALUE
  3873                              <1> ;;	;ADD	AX, CX			; ADD, TEST FOR 64K OVERFLOW
  3874                              <1> ;;	add	ecx, eax ; 08/02/2015
  3875                              <1> ;;	MOV	AL, 2			; MODE FOR 8237
  3876                              <1> ;;	;JMP	$+2			; WAIT FOR I/O
  3877                              <1> ;;	SIODELAY
  3878                              <1> ;;	OUT	DMA+10, AL		; INITIALIZE THE DISKETTE CHANNEL
  3879                              <1> ;;	;JNC	short NO_BAD		; CHECK FOR ERROR
  3880                              <1> ;;	jc	short dma_bnd_err ; 08/02/2015
  3881                              <1> ;;	and	ecx, 0FFF00000h ; 16 MB limit
  3882                              <1> ;;	jz	short NO_BAD
  3883                              <1> ;;dma_bnd_err:
  3884                              <1> ;;	MOV	byte [DSKETTE_STATUS], DMA_BOUNDARY ; SET ERROR
  3885                              <1> ;;NO_BAD:
  3886                              <1> ;;	RETn				; CY SET BY ABOVE IF ERROR
  3887                              <1> 
  3888                              <1> ;-------------------------------------------------------------------------------
  3889                              <1> ; FMTDMA_SET
  3890                              <1> ;	THIS ROUTINE SETS UP THE DMA CONTROLLER FOR A FORMAT OPERATION.
  3891                              <1> ;
  3892                              <1> ; ON ENTRY:	NOTHING REQUIRED
  3893                              <1> ;
  3894                              <1> ; ON EXIT:	@DSKETTE_STATUS, CY REFLECT STATUS OF OPERATION
  3895                              <1> ;-------------------------------------------------------------------------------
  3896                              <1> 
  3897                              <1> 	; 05/12/2021
  3898                              <1> FMTDMA_SET:
  3899                              <1> ;; 20/02/2015 modification	
  3900 0000201C 8B5504              <1> 	mov	edx, [ebp+4] 		; Buffer address
  3901 0000201F F7C20000F0FF        <1> 	test	edx, 0FFF00000h		; 16 MB limit
  3902 00002025 75EC                <1> 	jnz	short dma_bnd_err_stc
  3903                              <1> 	;
  3904                              <1> 	;push	dx			; *
  3905                              <1> 	; 05/12/2021
  3906 00002027 52                  <1> 	push	edx
  3907 00002028 B204                <1> 	mov	DL, 4			; SECTORS/TRACK VALUE IN PARM TABLE
  3908 0000202A E8C4020000          <1> 	call	GET_PARM		; "
  3909                              <1> 	;mov	al, ah			; AL = SECTORS/TRACK VALUE
  3910                              <1> 	;sub	ah, ah			; AX = SECTORS/TRACK VALUE
  3911                              <1> 	;shl	ax, 2			; AX = SEC/TRK * 4 (OFFSET C,H,R,N)
  3912                              <1> 	;dec	ax			; -1 FOR DMA VALUE
  3913                              <1> 	;mov	cx, ax
  3914                              <1> 	; 05/12/2021
  3915 0000202F 29C9                <1> 	sub	ecx, ecx
  3916 00002031 88E1                <1> 	mov	cl, ah			; SECTORS/TRACK VALUE
  3917 00002033 D1E1                <1> 	shl	ecx, 1
  3918 00002035 D1E1                <1> 	shl	ecx, 1			; cx = SEC/TRK * 4 (OFFSET C,H,R,N)
  3919 00002037 6649                <1> 	dec	cx
  3920                              <1> 	;pop	dx			; *
  3921                              <1> 	; 05/12/2021
  3922 00002039 5A                  <1> 	pop	edx
  3923 0000203A 6601CA              <1> 	add	dx, cx			; check for overflow
  3924 0000203D 72D5                <1> 	jc	short dma_bnd_err
  3925                              <1> 	;
  3926 0000203F 6629CA              <1> 	sub	dx, cx			; Restore start address
  3927                              <1> 	;
  3928 00002042 B04A                <1> 	MOV	AL,04AH			; WILL WRITE TO THE DISKETTE
  3929 00002044 FA                  <1> 	CLI				; DISABLE INTERRUPTS DURING DMA SET-UP
  3930 00002045 E60C                <1> 	OUT	DMA+12,AL		; SET THE FIRST/LA5T F/F
  3931                              <1> 	IODELAY				; WAIT FOR I/O
  2127 00002047 EB00                <2>  jmp short $+2
  2128 00002049 EB00                <2>  jmp short $+2
  3932 0000204B E60B                <1> 	OUT	DMA+11,AL		; OUTPUT THE MODE BYTE
  3933 0000204D 89D0                <1> 	mov	eax,edx			; Buffer address
  3934 0000204F E604                <1> 	OUT	DMA+4,AL		; OUTPUT LOW ADDRESS
  3935                              <1> 	IODELAY				; WAIT FOR I/O
  2127 00002051 EB00                <2>  jmp short $+2
  2128 00002053 EB00                <2>  jmp short $+2
  3936 00002055 88E0                <1> 	MOV	AL,AH
  3937 00002057 E604                <1> 	OUT	DMA+4,AL		; OUTPUT HIGH ADDRESS
  3938 00002059 C1E810              <1> 	shr	eax,16
  3939                              <1> 	IODELAY				; I/O WAIT STATE
  2127 0000205C EB00                <2>  jmp short $+2
  2128 0000205E EB00                <2>  jmp short $+2
  3940 00002060 E681                <1> 	OUT	081H,AL			; OUTPUT highest BITS TO PAGE REGISTER
  3941                              <1> 	IODELAY
  2127 00002062 EB00                <2>  jmp short $+2
  2128 00002064 EB00                <2>  jmp short $+2
  3942 00002066 6689C8              <1> 	mov	ax,cx			; Byte count - 1
  3943 00002069 E605                <1> 	OUT	DMA+5,AL		; LOW BYTE OF COUNT
  3944                              <1> 	IODELAY				; WAIT FOR I/O
  2127 0000206B EB00                <2>  jmp short $+2
  2128 0000206D EB00                <2>  jmp short $+2
  3945 0000206F 88E0                <1> 	MOV	AL,AH
  3946 00002071 E605                <1> 	OUT	DMA+5,AL		; HIGH BYTE OF COUNT
  3947                              <1> 	IODELAY
  2127 00002073 EB00                <2>  jmp short $+2
  2128 00002075 EB00                <2>  jmp short $+2
  3948 00002077 FB                  <1> 	STI				; RE-ENABLE INTERRUPTS
  3949 00002078 B002                <1> 	MOV	AL,2			; MODE FOR 8237
  3950 0000207A E60A                <1> 	OUT	DMA+10,AL		; INITIALIZE THE DISKETTE CHANNEL
  3951 0000207C C3                  <1> 	retn
  3952                              <1> 
  3953                              <1> ;; 08/02/2015 - Protected Mode Modification
  3954                              <1> ;;	MOV	AL, 04AH		; WILL WRITE TO THE DISKETTE
  3955                              <1> ;;	CLI				; DISABLE INTERRUPTS DURING DMA SET-UP
  3956                              <1> ;;	OUT	DMA+12,AL		; SET THE FIRST/LA5T F/F
  3957                              <1> ;;	;JMP	$+2			; WAIT FOR I/O
  3958                              <1> ;;	IODELAY
  3959                              <1> ;;	OUT	DMA+11,AL		; OUTPUT THE MODE BYTE
  3960                              <1> ;;	;MOV	AX,ES			; GET THE ES VALUE
  3961                              <1> ;;	;ROL	AX,4			; ROTATE LEFT
  3962                              <1> ;;	;MOV	CH,AL			; GET HIGHEST NIBBLE OF ES TO CH
  3963                              <1> ;;	;AND	AL,11110000B		; ZERO THE LOW NIBBLE FROM SEGMENT
  3964                              <1> ;;	;ADD	AX,[BP+2]		; TEST FOR CARRY FROM ADDITION
  3965                              <1> ;;	;JNC	short J33A
  3966                              <1> ;;	;INC	CH			; CARRY MEANS HIGH 4 BITS MUST BE INC
  3967                              <1> ;;	mov	eax, [ebp+4] ; 08/02/2015
  3968                              <1> ;;;J33A:
  3969                              <1> ;;	PUSH	eAX ; 08/02/2015	; SAVE START ADDRESS
  3970                              <1> ;;	OUT	DMA+4,AL		; OUTPUT LOW ADDRESS
  3971                              <1> ;;	;JMP	$+2			; WAIT FOR I/O
  3972                              <1> ;;	IODELAY
  3973                              <1> ;;	MOV	AL,AH
  3974                              <1> ;;	OUT	DMA+4,AL		; OUTPUT HIGH ADDRESS
  3975                              <1> ;;	shr 	eax, 16 ; 08/02/2015
  3976                              <1> ;;	;MOV	AL,CH			; GET HIGH 4 BITS
  3977                              <1> ;;	;JMP	$+2			; I/O WAIT STATE
  3978                              <1> ;;	IODELAY
  3979                              <1> ;;	;AND	AL,00001111B
  3980                              <1> ;;	OUT	081H,AL			; OUTPUT HIGH 4 BITS TO PAGE REGISTER
  3981                              <1> ;;
  3982                              <1> ;;;----- DETERMINE COUNT
  3983                              <1> ;;	sub	eax, eax ; 08/02/2015
  3984                              <1> ;;	MOV	DL, 4			; SECTORS/TRACK VALUE IN PARM TABLE
  3985                              <1> ;;	CALL	GET_PARM		; "
  3986                              <1> ;;	XCHG	AL, AH			; AL = SECTORS/TRACK VALUE
  3987                              <1> ;;	SUB	AH, AH			; AX = SECTORS/TRACK VALUE
  3988                              <1> ;;	SHL	AX, 2			; AX = SEC/TRK * 4 (OFFSET C,H,R,N)
  3989                              <1> ;;	DEC	AX			; -1 FOR DMA VALUE
  3990                              <1> ;;	PUSH	eAX 	; 08/02/2015	; SAVE # OF BYTES TO BE TRANSFERED
  3991                              <1> ;;	OUT	DMA+5,AL		; LOW BYTE OF COUNT
  3992                              <1> ;;	;JMP	$+2			; WAIT FOR I/O
  3993                              <1> ;;	IODELAY
  3994                              <1> ;;	MOV	AL, AH
  3995                              <1> ;;	OUT	DMA+5,AL		; HIGH BYTE OF COUNT
  3996                              <1> ;;	STI				; RE-ENABLE INTERRUPTS
  3997                              <1> ;;	POP	eCX	; 08/02/2015	; RECOVER COUNT VALUE
  3998                              <1> ;;	POP	eAX	; 08/02/2015	; RECOVER ADDRESS VALUE
  3999                              <1> ;;	;ADD	AX, CX			; ADD, TEST FOR 64K OVERFLOW
  4000                              <1> ;;	add	ecx, eax ; 08/02/2015
  4001                              <1> ;;	MOV	AL, 2			; MODE FOR 8237
  4002                              <1> ;;	;JMP	$+2			; WAIT FOR I/O
  4003                              <1> ;;	SIODELAY
  4004                              <1> ;;	OUT	DMA+10, AL		; INITIALIZE THE DISKETTE CHANNEL
  4005                              <1> ;;	;JNC	short FMTDMA_OK		; CHECK FOR ERROR
  4006                              <1> ;;	jc	short fmtdma_bnd_err ; 08/02/2015
  4007                              <1> ;;	and	ecx, 0FFF00000h  ; 16 MB limit
  4008                              <1> ;;	jz	short FMTDMA_OK
  4009                              <1> ;;	stc	; 20/02/2015
  4010                              <1> ;;fmtdma_bnd_err:
  4011                              <1> ;;	MOV	byte [DSKETTE_STATUS], DMA_BOUNDARY ; SET ERROR
  4012                              <1> ;;FMTDMA_OK:
  4013                              <1> ;;	RETn				; CY SET BY ABOVE IF ERROR
  4014                              <1> 
  4015                              <1> ;-------------------------------------------------------------------------------
  4016                              <1> ; NEC_INIT	
  4017                              <1> ;	THIS ROUTINE SEEKS TO THE REQUESTED TRACK AND INITIALIZES
  4018                              <1> ;	THE NEC FOR THE READ/WRITE/VERIFY/FORMAT OPERATION.
  4019                              <1> ;
  4020                              <1> ; ON ENTRY:	AH = NEC COMMAND TO BE PERFORMED
  4021                              <1> ;
  4022                              <1> ; ON EXIT:	@DSKETTE_STATUS, CY REFLECT STATUS OF OPERATION
  4023                              <1> ;-------------------------------------------------------------------------------
  4024                              <1> NEC_INIT:
  4025                              <1> 	;PUSH	AX			; SAVE NEC COMMAND
  4026                              <1> 	; 05/12/2021
  4027 0000207D 50                  <1> 	push	eax
  4028 0000207E E8B5020000          <1> 	CALL	MOTOR_ON		; TURN MOTOR ON FOR SPECIFIC DRIVE
  4029                              <1> 
  4030                              <1> ;-----	DO THE SEEK OPERATION
  4031                              <1> 
  4032 00002083 8A6D01              <1> 	MOV	CH,[eBP+1]		; CH = TRACK #
  4033 00002086 E8A7030000          <1> 	CALL	SEEK			; MOVE TO CORRECT TRACK
  4034                              <1> 	;POP	AX			; RECOVER COMMAND
  4035                              <1> 	; 05/12/2021
  4036 0000208B 58                  <1> 	pop	eax
  4037 0000208C 721E                <1> 	JC	short ER_1		; ERROR ON SEEK
  4038 0000208E BB[AC200000]        <1> 	MOV	eBX, ER_1		; LOAD ERROR ADDRESS
  4039 00002093 53                  <1> 	PUSH	eBX			; PUSH NEC_OUT ERROR RETURN
  4040                              <1> 
  4041                              <1> ;-----	SEND OUT THE PARAMETERS TO THE CONTROLLER
  4042                              <1> 
  4043 00002094 E860030000          <1> 	CALL	NEC_OUTPUT		; OUTPUT THE OPERATION COMMAND
  4044 00002099 6689F0              <1> 	MOV	AX,SI			; AH = HEAD #
  4045 0000209C 89FB                <1> 	MOV	eBX,eDI			; BL = DRIVE #
  4046 0000209E C0E402              <1> 	SAL	AH,2			; MOVE IT TO BIT 2
  4047 000020A1 80E404              <1> 	AND	AH,00000100B		; ISOLATE THAT BIT
  4048 000020A4 08DC                <1> 	OR	AH,BL			; OR IN THE DRIVE NUMBER
  4049 000020A6 E84E030000          <1> 	CALL	NEC_OUTPUT		; FALL THRU CY SET IF ERROR
  4050 000020AB 5B                  <1> 	POP	eBX			; THROW AWAY ERROR RETURN
  4051                              <1> ER_1:
  4052 000020AC C3                  <1> 	RETn
  4053                              <1> 
  4054                              <1> ;-------------------------------------------------------------------------------
  4055                              <1> ; RWV_COM
  4056                              <1> ;	THIS ROUTINE SENDS PARAMETERS TO THE NEC SPECIFIC TO THE 
  4057                              <1> ;	READ/WRITE/VERIFY OPERATIONS.
  4058                              <1> ;
  4059                              <1> ; ON ENTRY:	CS:BX = ADDRESS OF MEDIA/DRIVE PARAMETER TABLE
  4060                              <1> ; ON EXIT:	@DSKETTE_STATUS, CY REFLECT STATUS OF OPERATION
  4061                              <1> ;-------------------------------------------------------------------------------
  4062                              <1> RWV_COM:
  4063 000020AD B8[F8200000]        <1> 	MOV	eAX, ER_2		; LOAD ERROR ADDRESS
  4064 000020B2 50                  <1> 	PUSH	eAX			; PUSH NEC_OUT ERROR RETURN
  4065 000020B3 8A6501              <1> 	MOV	AH,[eBP+1]		; OUTPUT TRACK #
  4066 000020B6 E83E030000          <1> 	CALL	NEC_OUTPUT
  4067 000020BB 6689F0              <1> 	MOV	AX,SI			; OUTPUT HEAD #
  4068 000020BE E836030000          <1> 	CALL	NEC_OUTPUT
  4069 000020C3 8A6500              <1>         MOV     AH,[eBP]                ; OUTPUT SECTOR #
  4070 000020C6 E82E030000          <1> 	CALL	NEC_OUTPUT
  4071 000020CB B203                <1> 	MOV	DL,3			; BYTES/SECTOR PARAMETER FROM BLOCK
  4072 000020CD E821020000          <1> 	CALL	GET_PARM 		; ... TO THE NEC
  4073 000020D2 E822030000          <1> 	CALL	NEC_OUTPUT		; OUTPUT TO CONTROLLER
  4074 000020D7 B204                <1> 	MOV	DL,4			; EOT PARAMETER FROM BLOCK
  4075 000020D9 E815020000          <1> 	CALL	GET_PARM 		; ... TO THE NEC
  4076 000020DE E816030000          <1> 	CALL	NEC_OUTPUT		; OUTPUT TO CONTROLLER
  4077 000020E3 8A6305              <1>         MOV     AH, [eBX+MD.GAP]        ; GET GAP LENGTH
  4078                              <1> _R15:
  4079 000020E6 E80E030000          <1> 	CALL	NEC_OUTPUT
  4080 000020EB B206                <1> 	MOV	DL,6			; DTL PARAMETER PROM BLOCK
  4081 000020ED E801020000          <1> 	CALL	GET_PARM		;  TO THE NEC
  4082 000020F2 E802030000          <1> 	CALL	NEC_OUTPUT		; OUTPUT TO CONTROLLER
  4083 000020F7 58                  <1> 	POP	eAX			; THROW AWAY ERROR EXIT
  4084                              <1> ER_2:
  4085 000020F8 C3                  <1> 	RETn
  4086                              <1> 
  4087                              <1> ;-------------------------------------------------------------------------------
  4088                              <1> ; NEC_TERM
  4089                              <1> ;	THIS ROUTINE WAITS FOR THE OPERATION THEN ACCEPTS THE STATUS 
  4090                              <1> ;	FROM THE NEC FOR THE READ/WRITE/VERIFY/FORWAT OPERATION.
  4091                              <1> ;
  4092                              <1> ; ON EXIT:	@DSKETTE_STATUS, CY REFLECT STATUS OF OPERATION
  4093                              <1> ;-------------------------------------------------------------------------------
  4094                              <1> NEC_TERM:
  4095                              <1> 
  4096                              <1> ;-----	LET THE OPERATION HAPPEN
  4097                              <1> 
  4098 000020F9 56                  <1> 	PUSH	eSI			; SAVE HEAD #, # OF SECTORS
  4099 000020FA E804040000          <1> 	CALL	WAIT_INT		; WAIT FOR THE INTERRUPT
  4100 000020FF 9C                  <1> 	PUSHFd	; 05/12/2021
  4101 00002100 E82E040000          <1> 	CALL	RESULTS			; GET THE NEC STATUS
  4102 00002105 724B                <1> 	JC	short SET_END_POP
  4103 00002107 9D                  <1> 	POPFd	; 05/12/2021
  4104 00002108 723E                <1> 	JC	short SET_END		; LOOK FOR ERROR
  4105                              <1> 
  4106                              <1> ;-----	CHECK THE RESULTS RETURNED BY THE CONTROLLER
  4107                              <1> 
  4108 0000210A FC                  <1> 	CLD				; SET THE CORRECT DIRECTION
  4109 0000210B BE[3D700000]        <1> 	MOV	eSI, NEC_STATUS		; POINT TO STATUS FIELD
  4110 00002110 AC                  <1> 	lodsb				; GET ST0
  4111 00002111 24C0                <1> 	AND	AL,11000000B		; TEST FOR NORMAL TERMINATION
  4112 00002113 7433                <1> 	JZ	short SET_END
  4113 00002115 3C40                <1> 	CMP	AL,01000000B		; TEST FOR ABNORMAL TERMINATION
  4114 00002117 7527                <1> 	JNZ	short J18		; NOT ABNORMAL, BAD NEC
  4115                              <1> 
  4116                              <1> ;-----	ABNORMAL TERMINATION, FIND OUT WHY
  4117                              <1> 
  4118 00002119 AC                  <1> 	lodsb				; GET ST1
  4119 0000211A D0E0                <1> 	SAL	AL,1			; TEST FOR EDT FOUND
  4120 0000211C B404                <1> 	MOV	AH,RECORD_NOT_FND
  4121 0000211E 7222                <1> 	JC	short J19
  4122 00002120 C0E002              <1> 	SAL	AL,2
  4123 00002123 B410                <1> 	MOV	AH,BAD_CRC
  4124 00002125 721B                <1> 	JC	short J19
  4125 00002127 D0E0                <1> 	SAL	AL,1			; TEST FOR DMA OVERRUN
  4126 00002129 B408                <1> 	MOV	AH,BAD_DMA
  4127 0000212B 7215                <1> 	JC	short J19
  4128 0000212D C0E002              <1> 	SAL	AL,2			; TEST FOR RECORD NOT FOUND
  4129 00002130 B404                <1> 	MOV	AH,RECORD_NOT_FND
  4130 00002132 720E                <1> 	JC	short J19
  4131 00002134 D0E0                <1> 	SAL	AL,1
  4132 00002136 B403                <1> 	MOV	AH,WRITE_PROTECT	; TEST FOR WRITE_PROTECT
  4133 00002138 7208                <1> 	JC	short J19
  4134 0000213A D0E0                <1> 	SAL	AL,1			; TEST MISSING ADDRESS MARK
  4135 0000213C B402                <1> 	MOV	AH,BAD_ADDR_MARK
  4136 0000213E 7202                <1> 	JC	short J19
  4137                              <1> 
  4138                              <1> ;----- 	NEC MUST HAVE FAILED
  4139                              <1> J18:
  4140 00002140 B420                <1> 	MOV	AH,BAD_NEC
  4141                              <1> J19:
  4142 00002142 0825[3C700000]      <1> 	OR	[DSKETTE_STATUS],AH
  4143                              <1> SET_END:
  4144 00002148 803D[3C700000]01    <1> 	CMP	byte [DSKETTE_STATUS],1 ; SET ERROR CONDITION
  4145 0000214F F5                  <1> 	CMC
  4146 00002150 5E                  <1> 	POP	eSI
  4147 00002151 C3                  <1> 	RETn				; RESTORE HEAD #, # OF SECTORS
  4148                              <1> 
  4149                              <1> SET_END_POP:
  4150 00002152 9D                  <1> 	POPFd	; 05/12/2021
  4151 00002153 EBF3                <1> 	JMP	SHORT SET_END
  4152                              <1> 
  4153                              <1> ;-------------------------------------------------------------------------------
  4154                              <1> ; DSTATE:	ESTABLISH STATE UPON SUCCESSFUL OPERATION.
  4155                              <1> ;-------------------------------------------------------------------------------
  4156                              <1> DSTATE:
  4157 00002155 803D[3C700000]00    <1> 	CMP	byte [DSKETTE_STATUS],0	; CHECK FOR ERROR
  4158 0000215C 753E                <1> 	JNZ	short SETBAC		    ; IF ERROR JUMP
  4159 0000215E 808F[49700000]10    <1> 	OR	byte [DSK_STATE+eDI],MED_DET ; NO ERROR, MARK MEDIA AS DETERMINED
  4160 00002165 F687[49700000]04    <1> 	TEST	byte [DSK_STATE+eDI],DRV_DET ; DRIVE DETERMINED ?
  4161 0000216C 752E                <1> 	JNZ	short SETBAC		; IF DETERMINED NO TRY TO DETERMINE
  4162 0000216E 8A87[49700000]      <1> 	MOV	AL,[DSK_STATE+eDI]	; LOAD STATE
  4163 00002174 24C0                <1> 	AND	AL,RATE_MSK		; KEEP ONLY RATE
  4164 00002176 3C80                <1> 	CMP	AL,RATE_250		; RATE 250 ?
  4165 00002178 751B                <1> 	JNE	short M_12		; NO, MUST BE 1.2M OR 1.44M DRIVE
  4166                              <1> 
  4167                              <1> ;----- 	CHECK IF IT IS 1.44M
  4168                              <1> 
  4169 0000217A E86B010000          <1> 	CALL	CMOS_TYPE		; RETURN DRIVE TYPE IN (AL)
  4170                              <1> 	;;20/02/2015
  4171                              <1> 	;;JC	short M_12		; CMOS BAD
  4172 0000217F 7414                <1> 	jz	short M_12 ;; 20/02/2015
  4173 00002181 3C04                <1> 	CMP	AL, 4			; 1.44MB DRIVE ?
  4174 00002183 7410                <1> 	JE	short M_12		; YES
  4175                              <1> M_720:
  4176 00002185 80A7[49700000]FD    <1> 	AND	byte [DSK_STATE+eDI], ~FMT_CAPA ; TURN OFF FORMAT CAPABILITY
  4177 0000218C 808F[49700000]04    <1> 	OR	byte [DSK_STATE+eDI],DRV_DET  ; MARK DRIVE DETERMINED
  4178 00002193 EB07                <1> 	JMP	SHORT SETBAC		; BACK
  4179                              <1> M_12:	
  4180 00002195 808F[49700000]06    <1> 	OR	byte [DSK_STATE+eDI],DRV_DET+FMT_CAPA 
  4181                              <1> 					; TURN ON DETERMINED & FMT CAPA
  4182                              <1> SETBAC:
  4183 0000219C C3                  <1> 	RETn
  4184                              <1> 
  4185                              <1> ;-------------------------------------------------------------------------------
  4186                              <1> ; RETRY	
  4187                              <1> ;	DETERMINES WHETHER A RETRY IS NECESSARY. 
  4188                              <1> ;	IF RETRY IS REQUIRED THEN STATE INFORMATION IS UPDATED FOR RETRY.
  4189                              <1> ;
  4190                              <1> ; ON EXIT:	CY = 1 FOR RETRY, CY = 0 FOR NO RETRY
  4191                              <1> ;-------------------------------------------------------------------------------
  4192                              <1> RETRY:
  4193 0000219D 803D[3C700000]00    <1> 	CMP	byte [DSKETTE_STATUS],0	; GET STATUS OF OPERATION
  4194 000021A4 7445                <1> 	JZ	short NO_RETRY		; SUCCESSFUL OPERATION
  4195 000021A6 803D[3C700000]80    <1> 	CMP	byte [DSKETTE_STATUS],TIME_OUT ; IF TIME OUT NO RETRY
  4196 000021AD 743C                <1> 	JZ	short NO_RETRY
  4197 000021AF 8AA7[49700000]      <1> 	MOV	AH,[DSK_STATE+eDI]	; GET MEDIA STATE OF DRIVE
  4198 000021B5 F6C410              <1> 	TEST	AH,MED_DET		; ESTABLISHED/DETERMINED ?
  4199 000021B8 7531                <1> 	JNZ	short NO_RETRY		; IF ESTABLISHED STATE THEN TRUE ERROR
  4200 000021BA 80E4C0              <1> 	AND	AH,RATE_MSK		; ISOLATE RATE
  4201 000021BD 8A2D[44700000]      <1> 	MOV	CH,[LASTRATE]		; GET START OPERATION STATE
  4202 000021C3 C0C504              <1> 	ROL	CH,4			; TO CORRESPONDING BITS
  4203 000021C6 80E5C0              <1> 	AND	CH,RATE_MSK		; ISOLATE RATE BITS
  4204 000021C9 38E5                <1> 	CMP	CH,AH			; ALL RATES TRIED
  4205 000021CB 741E                <1> 	JE	short NO_RETRY		; IF YES, THEN TRUE ERROR
  4206                              <1> 
  4207                              <1> ;	SETUP STATE INDICATOR FOR RETRY ATTEMPT TO NEXT RATE
  4208                              <1> ;	 00000000B (500) -> 10000000B	(250)
  4209                              <1> ;	 10000000B (250) -> 01000000B	(300)
  4210                              <1> ;	 01000000B (300) -> 00000000B	(500)
  4211                              <1> 
  4212 000021CD 80FC01              <1> 	CMP	AH,RATE_500+1		; SET CY FOR RATE 500
  4213 000021D0 D0DC                <1> 	RCR	AH,1			; TO NEXT STATE
  4214 000021D2 80E4C0              <1> 	AND	AH,RATE_MSK		; KEEP ONLY RATE BITS
  4215 000021D5 80A7[49700000]1F    <1> 	AND	byte [DSK_STATE+eDI], ~(RATE_MSK+DBL_STEP)
  4216                              <1> 					; RATE, DBL STEP OFF
  4217 000021DC 08A7[49700000]      <1> 	OR	[DSK_STATE+eDI],AH	; TURN ON NEW RATE
  4218 000021E2 C605[3C700000]00    <1> 	MOV	byte [DSKETTE_STATUS],0	; RESET STATUS FOR RETRY
  4219 000021E9 F9                  <1> 	STC				; SET CARRY FOR RETRY
  4220 000021EA C3                  <1> 	RETn				; RETRY RETURN
  4221                              <1> 
  4222                              <1> NO_RETRY:
  4223 000021EB F8                  <1> 	CLC				; CLEAR CARRY NO RETRY
  4224 000021EC C3                  <1> 	RETn				; NO RETRY RETURN
  4225                              <1> 
  4226                              <1> ;-------------------------------------------------------------------------------
  4227                              <1> ; NUM_TRANS
  4228                              <1> ;	THIS ROUTINE CALCULATES THE NUMBER OF SECTORS THAT WERE
  4229                              <1> ;	ACTUALLY TRANSFERRED TO/FROM THE DISKETTE.
  4230                              <1> ;
  4231                              <1> ; ON ENTRY:	[BP+1] = TRACK
  4232                              <1> ;		SI-HI  = HEAD
  4233                              <1> ;		[BP]   = START SECTOR
  4234                              <1> ;
  4235                              <1> ; ON EXIT:	AL = NUMBER ACTUALLY TRANSFERRED
  4236                              <1> ;-------------------------------------------------------------------------------
  4237                              <1> NUM_TRANS:
  4238 000021ED 30C0                <1> 	XOR	AL,AL			; CLEAR FOR ERROR
  4239 000021EF 803D[3C700000]00    <1> 	CMP	byte [DSKETTE_STATUS],0	; CHECK FOR ERROR
  4240                              <1> 	; 05/12/2021
  4241 000021F6 752C                <1> 	JNZ	short NT_OUT		; IF ERROR 0 TRANSFERRED
  4242 000021F8 B204                <1> 	MOV	DL,4			; SECTORS/TRACK OFFSET TO DL
  4243 000021FA E8F4000000          <1> 	CALL	GET_PARM		; AH = SECTORS/TRACK
  4244 000021FF 8A1D[42700000]      <1> 	MOV	BL, [NEC_STATUS+5]	; GET ENDING SECTOR
  4245 00002205 6689F1              <1> 	MOV	CX,SI			; CH = HEAD # STARTED
  4246 00002208 3A2D[41700000]      <1> 	CMP	CH, [NEC_STATUS+4]	; GET HEAD ENDED UP ON
  4247 0000220E 750D                <1> 	JNZ	DIF_HD			; IF ON SAME HEAD, THEN NO ADJUST
  4248 00002210 8A2D[40700000]      <1> 	MOV	CH, [NEC_STATUS+3]	; GET TRACK ENDED UP ON
  4249 00002216 3A6D01              <1> 	CMP	CH,[eBP+1]		; IS IT ASKED FOR TRACK
  4250 00002219 7404                <1> 	JZ	short SAME_TRK		; IF SAME TRACK NO INCREASE
  4251 0000221B 00E3                <1> 	ADD	BL,AH			; ADD SECTORS/TRACK
  4252                              <1> DIF_HD:
  4253 0000221D 00E3                <1> 	ADD	BL,AH			; ADD SECTORS/TRACK
  4254                              <1> SAME_TRK:
  4255 0000221F 2A5D00              <1> 	SUB	BL,[eBP]		; SUBTRACT START FROM END
  4256 00002222 88D8                <1> 	MOV	AL,BL			; TO AL
  4257                              <1> NT_OUT:
  4258 00002224 C3                  <1> 	RETn
  4259                              <1> 
  4260                              <1> ;-------------------------------------------------------------------------------
  4261                              <1> ; SETUP_END
  4262                              <1> ;	RESTORES @MOTOR_COUNT TO PARAMETER PROVIDED IN TABLE 
  4263                              <1> ;	AND LOADS @DSKETTE_STATUS TO AH, AND SETS CY.
  4264                              <1> ;
  4265                              <1> ; ON EXIT:
  4266                              <1> ;	AH, @DSKETTE_STATUS, CY REFLECT STATUS OF OPERATION
  4267                              <1> ;-------------------------------------------------------------------------------
  4268                              <1> SETUP_END:
  4269 00002225 B202                <1> 	MOV	DL,2			; GET THE MOTOR WAIT PARAMETER
  4270                              <1> 	;PUSH	AX			; SAVE NUMBER TRANSFERRED
  4271 00002227 50                  <1> 	push	eax ; 05/12/2021
  4272 00002228 E8C6000000          <1> 	CALL	GET_PARM
  4273 0000222D 8825[3B700000]      <1> 	MOV	[MOTOR_COUNT],AH	; STORE UPON RETURN
  4274                              <1> 	;POP	AX			; RESTORE NUMBER TRANSFERRED
  4275 00002233 58                  <1> 	pop	eax ; 05/12/2021
  4276 00002234 8A25[3C700000]      <1> 	MOV	AH, [DSKETTE_STATUS]	; GET STATUS OF OPERATION
  4277 0000223A 08E4                <1> 	OR	AH,AH			; CHECK FOR ERROR
  4278 0000223C 7402                <1> 	JZ	short NUN_ERR			; NO ERROR
  4279 0000223E 30C0                <1> 	XOR	AL,AL			; CLEAR NUMBER RETURNED
  4280                              <1> NUN_ERR: 
  4281 00002240 80FC01              <1> 	CMP	AH,1			; SET THE CARRY FLAG TO INDICATE
  4282 00002243 F5                  <1> 	CMC				; SUCCESS OR FAILURE
  4283 00002244 C3                  <1> 	RETn
  4284                              <1> 
  4285                              <1> ;-------------------------------------------------------------------------------
  4286                              <1> ; SETUP_DBL
  4287                              <1> ;	CHECK DOUBLE STEP.
  4288                              <1> ;
  4289                              <1> ; ON ENTRY :	DI = DRIVE
  4290                              <1> ;
  4291                              <1> ; ON EXIT :	CY = 1 MEANS ERROR
  4292                              <1> ;-------------------------------------------------------------------------------
  4293                              <1> SETUP_DBL:
  4294 00002245 8AA7[49700000]      <1> 	MOV	AH, [DSK_STATE+eDI]	; ACCESS STATE
  4295 0000224B F6C410              <1> 	TEST	AH,MED_DET		; ESTABLISHED STATE ?
  4296 0000224E 757A                <1> 	JNZ	short NO_DBL			; IF ESTABLISHED THEN DOUBLE DONE
  4297                              <1> 
  4298                              <1> ;-----	CHECK FOR TRACK 0 TO SPEED UP ACKNOWLEDGE OF UNFORMATTED DISKETTE
  4299                              <1> 
  4300 00002250 C605[39700000]00    <1> 	MOV	byte [SEEK_STATUS],0	; SET RECALIBRATE REQUIRED ON ALL DRIVES
  4301 00002257 E8DC000000          <1> 	CALL	MOTOR_ON		; ENSURE MOTOR STAY ON
  4302 0000225C B500                <1> 	MOV	CH,0			; LOAD TRACK 0
  4303 0000225E E8CF010000          <1> 	CALL	SEEK			; SEEK TO TRACK 0
  4304 00002263 E864000000          <1> 	CALL	READ_ID			; READ ID FUNCTION
  4305 00002268 7245                <1> 	JC	short SD_ERR		; IF ERROR NO TRACK 0
  4306                              <1> 
  4307                              <1> ;-----	INITIALIZE START AND MAX TRACKS (TIMES 2 FOR BOTH HEADS)
  4308                              <1> 
  4309 0000226A 66B95004            <1> 	MOV	CX,0450H 		; START, MAX TRACKS
  4310 0000226E F687[49700000]01    <1> 	TEST	byte [DSK_STATE+eDI],TRK_CAPA ; TEST FOR 80 TRACK CAPABILITY
  4311 00002275 7402                <1> 	JZ	short CNT_OK		; IF NOT COUNT IS SETUP
  4312 00002277 B1A0                <1> 	MOV	CL,0A0H			; MAXIMUM TRACK 1.2 MB
  4313                              <1> 
  4314                              <1> ;	ATTEMPT READ ID OF ALL TRACKS, ALL HEADS UNTIL SUCCESS; UPON SUCCESS,
  4315                              <1> ;	MUST SEE IF ASKED FOR TRACK IN SINGLE STEP MODE = TRACK ID READ; IF NOT
  4316                              <1> ;	THEN SET DOUBLE STEP ON.
  4317                              <1> 
  4318                              <1> CNT_OK:
  4319 00002279 C605[3B700000]FF    <1>         MOV     byte [MOTOR_COUNT], 0FFH ; ENSURE MOTOR STAYS ON FOR OPERATION 
  4320                              <1> 	;PUSH	CX			; SAVE TRACK, COUNT
  4321                              <1> 	; 05/12/2021
  4322 00002280 51                  <1> 	push	ecx
  4323 00002281 C605[3C700000]00    <1> 	MOV	byte [DSKETTE_STATUS],0	; CLEAR STATUS, EXPECT ERRORS
  4324 00002288 6631C0              <1> 	XOR	AX,AX			; CLEAR AX
  4325 0000228B D0ED                <1> 	SHR	CH,1			; HALVE TRACK, CY = HEAD
  4326 0000228D C0D003              <1> 	RCL	AL,3			; AX = HEAD IN CORRECT BIT
  4327                              <1> 	;PUSH	AX			; SAVE HEAD
  4328                              <1> 	; 05/12/2021
  4329 00002290 50                  <1> 	push	eax
  4330 00002291 E89C010000          <1> 	CALL	SEEK			; SEEK TO TRACK
  4331                              <1> 	;POP	AX			; RESTORE HEAD
  4332                              <1> 	; 05/12/2021
  4333 00002296 58                  <1> 	pop	eax
  4334 00002297 6609C7              <1> 	OR	DI,AX			; DI = HEAD OR'ED DRIVE
  4335 0000229A E82D000000          <1> 	CALL	READ_ID			; READ ID HEAD 0
  4336 0000229F 9C                  <1> 	PUSHF				; SAVE RETURN FROM READ_ID
  4337 000022A0 6681E7FB00          <1> 	AND	DI,11111011B		; TURN OFF HEAD 1 BIT
  4338 000022A5 9D                  <1> 	POPF				; RESTORE ERROR RETURN
  4339                              <1> 	;POP	CX			; RESTORE COUNT
  4340                              <1> 	; 05/12/2021
  4341 000022A6 59                  <1> 	pop	ecx
  4342 000022A7 7308                <1> 	JNC	short DO_CHK		; IF OK, ASKED = RETURNED TRACK ?
  4343 000022A9 FEC5                <1> 	INC	CH			; INC FOR NEXT TRACK
  4344 000022AB 38CD                <1> 	CMP	CH,CL			; REACHED MAXIMUM YET
  4345 000022AD 75CA                <1> 	JNZ	short CNT_OK		; CONTINUE TILL ALL TRIED
  4346                              <1> 
  4347                              <1> ;-----	FALL THRU, READ ID FAILED FOR ALL TRACKS
  4348                              <1> 
  4349                              <1> SD_ERR:	
  4350 000022AF F9                  <1> 	STC				; SET CARRY FOR ERROR
  4351 000022B0 C3                  <1> 	RETn				; SETUP_DBL ERROR EXIT
  4352                              <1> 
  4353                              <1> DO_CHK:
  4354 000022B1 8A0D[40700000]      <1> 	MOV	CL, [NEC_STATUS+3]	; LOAD RETURNED TRACK
  4355 000022B7 888F[4D700000]      <1> 	MOV	[DSK_TRK+eDI], CL	; STORE TRACK NUMBER
  4356 000022BD D0ED                <1> 	SHR	CH,1			; HALVE TRACK
  4357 000022BF 38CD                <1> 	CMP	CH,CL			; IS IT THE SAME AS ASKED FOR TRACK
  4358 000022C1 7407                <1> 	JZ	short NO_DBL		; IF SAME THEN NO DOUBLE STEP
  4359 000022C3 808F[49700000]20    <1> 	OR	byte [DSK_STATE+eDI],DBL_STEP ; TURN ON DOUBLE STEP REQUIRED
  4360                              <1> NO_DBL:
  4361 000022CA F8                  <1> 	CLC				; CLEAR ERROR FLAG
  4362 000022CB C3                  <1> 	RETn
  4363                              <1> 
  4364                              <1> ;-------------------------------------------------------------------------------
  4365                              <1> ; READ_ID
  4366                              <1> ;	READ ID FUNCTION.
  4367                              <1> ;
  4368                              <1> ; ON ENTRY:	DI : BIT 2 = HEAD; BITS 1,0 = DRIVE
  4369                              <1> ;
  4370                              <1> ; ON EXIT: 	DI : BIT 2 IS RESET, BITS 1,0 = DRIVE
  4371                              <1> ;		@DSKETTE_STATUS, CY REFLECT STATUS OF OPERATION
  4372                              <1> ;-------------------------------------------------------------------------------
  4373                              <1> READ_ID:
  4374 000022CC B8[E9220000]        <1> 	MOV	eAX, ER_3		; MOVE NEC OUTPUT ERROR ADDRESS
  4375 000022D1 50                  <1> 	PUSH	eAX
  4376 000022D2 B44A                <1> 	MOV	AH,4AH			; READ ID COMMAND
  4377 000022D4 E820010000          <1> 	CALL	NEC_OUTPUT		; TO CONTROLLER
  4378 000022D9 6689F8              <1> 	MOV	AX,DI			; DRIVE # TO AH, HEAD 0
  4379 000022DC 88C4                <1> 	MOV	AH,AL
  4380 000022DE E816010000          <1> 	CALL	NEC_OUTPUT		; TO CONTROLLER
  4381 000022E3 E811FEFFFF          <1> 	CALL	NEC_TERM		; WAIT FOR OPERATION, GET STATUS
  4382 000022E8 58                  <1> 	POP	eAX			; THROW AWAY ERROR ADDRESS
  4383                              <1> ER_3:
  4384 000022E9 C3                  <1> 	RETn
  4385                              <1> 
  4386                              <1> ;-------------------------------------------------------------------------------
  4387                              <1> ; CMOS_TYPE
  4388                              <1> ;	RETURNS DISKETTE TYPE FROM CMOS
  4389                              <1> ;
  4390                              <1> ; ON ENTRY:	DI = DRIVE #
  4391                              <1> ;
  4392                              <1> ; ON EXIT:	AL = TYPE; CY REFLECTS STATUS
  4393                              <1> ;-------------------------------------------------------------------------------
  4394                              <1> 
  4395                              <1> CMOS_TYPE: ; 11/12/2014
  4396 000022EA 8A87[AE6A0000]      <1> 	mov	al, [eDI+fd0_type]
  4397 000022F0 20C0                <1> 	and 	al, al ; 18/12/2014
  4398 000022F2 C3                  <1> 	retn
  4399                              <1> 
  4400                              <1> ;CMOS_TYPE:
  4401                              <1> ;	MOV	AL, CMOS_DIAG		; CMOS DIAGNOSTIC STATUS BYTE ADDRESS
  4402                              <1> ;	CALL	CMOS_READ		; GET CMOS STATUS
  4403                              <1> ;	TEST	AL,BAD_BAT+BAD_CKSUM	; BATTERY GOOD AND CHECKSUM VALID
  4404                              <1> ;	STC				; SET CY = 1 INDICATING ERROR FOR RETURN
  4405                              <1> ;	JNZ	short BAD_CM		; ERROR IF EITHER BIT ON
  4406                              <1> ;	MOV	AL,CMOS_DISKETTE	; ADDRESS OF DISKETTE BYTE IN CMOS
  4407                              <1> ;	CALL	CMOS_READ		; GET DISKETTE BYTE
  4408                              <1> ;	OR	DI,DI			; SEE WHICH DRIVE IN QUESTION
  4409                              <1> ;	JNZ	short TB		; IF DRIVE 1, DATA IN LOW NIBBLE
  4410                              <1> ;	ROR	AL,4			; EXCHANGE NIBBLES IF SECOND DRIVE
  4411                              <1> ;TB:
  4412                              <1> ;	AND	AL,0FH			; KEEP ONLY DRIVE DATA, RESET CY, 0
  4413                              <1> ;BAD_CM:
  4414                              <1> ;	RETn				; CY, STATUS OF READ
  4415                              <1> 
  4416                              <1> ;-------------------------------------------------------------------------------
  4417                              <1> ; GET_PARM
  4418                              <1> ;	THIS ROUTINE FETCHES THE INDEXED POINTER FROM THE DISK_BASE
  4419                              <1> ;	BLOCK POINTED TO BY THE DATA VARIABLE @DISK_POINTER. A BYTE FROM
  4420                              <1> ;	THAT TABLE IS THEN MOVED INTO AH, THE INDEX OF THAT BYTE BEING
  4421                              <1> ;	THE PARAMETER IN DL.
  4422                              <1> ;
  4423                              <1> ; ON ENTRY:	DL = INDEX OF BYTE TO BE FETCHED
  4424                              <1> ;
  4425                              <1> ; ON EXIT:	AH = THAT BYTE FROM BLOCK
  4426                              <1> ;		AL,DH DESTROYED
  4427                              <1> ;-------------------------------------------------------------------------------
  4428                              <1> GET_PARM:
  4429                              <1> 	;PUSH	DS
  4430 000022F3 56                  <1> 	PUSH	eSI
  4431                              <1>     	;SUB	AX,AX			; DS = 0, BIOS DATA AREA
  4432                              <1>     	;MOV	DS,AX
  4433                              <1> 	;;mov	ax, cs
  4434                              <1> 	;;mov	ds, ax
  4435                              <1> 	; 08/02/2015 (protected mode modifications, bx -> ebx)
  4436 000022F4 87D3                <1> 	XCHG	eDX,eBX			; BL = INDEX
  4437                              <1> 	;SUB	BH,BH			; BX = INDEX
  4438 000022F6 81E3FF000000        <1> 	and	ebx, 0FFh
  4439                              <1>     	;LDS	SI, [DISK_POINTER]	; POINT TO BLOCK
  4440                              <1> 	;
  4441                              <1> 	; 17/12/2014
  4442 000022FC 66A1[A16A0000]      <1> 	mov	ax, [cfd] ; current (AL) and previous fd (AH)
  4443 00002302 38E0                <1> 	cmp	al, ah
  4444 00002304 7425                <1> 	je	short gpndc
  4445 00002306 A2[A26A0000]        <1> 	mov	[pfd], al ; current drive -> previous drive
  4446 0000230B 53                  <1> 	push	ebx ; 08/02/2015
  4447 0000230C 88C3                <1> 	mov	bl, al 
  4448                              <1> 	; 11/12/2014
  4449 0000230E 8A83[AE6A0000]      <1> 	mov	al, [eBX+fd0_type]	; Drive type (0,1,2,3,4)
  4450                              <1> 	; 18/12/2014
  4451 00002314 20C0                <1> 	and	al, al
  4452 00002316 7507                <1> 	jnz	short gpdtc
  4453 00002318 BB[8B6A0000]        <1> 	mov	ebx, MD_TBL6		; 1.44 MB param. tbl. (default)
  4454 0000231D EB05                <1>         jmp     short gpdpu
  4455                              <1> gpdtc:	
  4456 0000231F E83EF9FFFF          <1> 	call	DR_TYPE_CHECK
  4457                              <1> 	; cf = 1 -> eBX points to 1.44MB fd parameter table (default)
  4458                              <1> gpdpu:
  4459 00002324 891D[286A0000]      <1> 	mov	[DISK_POINTER], ebx
  4460 0000232A 5B                  <1> 	pop	ebx
  4461                              <1> gpndc:
  4462 0000232B 8B35[286A0000]      <1> 	mov	esi, [DISK_POINTER] ; 08/02/2015, si -> esi
  4463 00002331 8A241E              <1> 	MOV	AH, [eSI+eBX]		; GET THE WORD
  4464 00002334 87D3                <1> 	XCHG	eDX,eBX			; RESTORE BX
  4465 00002336 5E                  <1> 	POP	eSI
  4466                              <1> 	;POP	DS
  4467 00002337 C3                  <1> 	RETn
  4468                              <1> 
  4469                              <1> ;-------------------------------------------------------------------------------
  4470                              <1> ; MOTOR_ON
  4471                              <1> ;	TURN MOTOR ON AND WAIT FOR MOTOR START UP TIME. THE @MOTOR_COUNT
  4472                              <1> ;	IS REPLACED WITH A SUFFICIENTLY HIGH NUMBER (0FFH) TO ENSURE
  4473                              <1> ;	THAT THE MOTOR DOES NOT GO OFF DURING THE OPERATION. IF THE
  4474                              <1> ;	MOTOR NEEDED TO BE TURNED ON, THE MULTI-TASKING HOOK FUNCTION
  4475                              <1> ;	(AX=90FDH, INT 15) IS CALLED TELLING THE OPERATING SYSTEM
  4476                              <1> ;	THAT THE BIOS IS ABOUT TO WAIT FOR MOTOR START UP. IF THIS
  4477                              <1> ;	FUNCTION RETURNS WITH CY = 1, IT MEANS THAT THE MINIMUM WAIT
  4478                              <1> ;	HAS BEEN COMPLETED. AT THIS POINT A CHECK IS MADE TO ENSURE
  4479                              <1> ;	THAT THE MOTOR WASN'T TURNED OFF BY THE TIMER. IF THE HOOK DID
  4480                              <1> ;	NOT WAIT, THE WAIT FUNCTION (AH=086H) IS CALLED TO WAIT THE
  4481                              <1> ;	PRESCRIBED AMOUNT OF TIME. IF THE CARRY FLAG IS SET ON RETURN,
  4482                              <1> ;	IT MEANS THAT THE FUNCTION IS IN USE AND DID NOT PERFORM THE
  4483                              <1> ;	WAIT. A TIMER 1 WAIT LOOP WILL THEN DO THE WAIT.
  4484                              <1> ;
  4485                              <1> ; ON ENTRY:	DI = DRIVE #
  4486                              <1> ; ON EXIT:	AX,CX,DX DESTROYED
  4487                              <1> ;-------------------------------------------------------------------------------
  4488                              <1> MOTOR_ON:
  4489 00002338 53                  <1> 	PUSH	eBX			; SAVE REG.
  4490 00002339 E82A000000          <1> 	CALL	TURN_ON			; TURN ON MOTOR
  4491 0000233E 7226                <1> 	JC	short MOT_IS_ON		; IF CY=1 NO WAIT
  4492 00002340 E8C0F9FFFF          <1> 	CALL	XLAT_OLD		; TRANSLATE STATE TO COMPATIBLE MODE
  4493 00002345 E88AF9FFFF          <1> 	CALL	XLAT_NEW		; TRANSLATE STATE TO PRESENT ARCH,
  4494                              <1> 	;CALL	TURN_ON 		; CHECK AGAIN IF MOTOR ON
  4495                              <1> 	;JC	MOT_IS_ON		; IF NO WAIT MEANS IT IS ON
  4496                              <1> M_WAIT:
  4497 0000234A B20A                <1> 	MOV	DL,10			; GET THE MOTOR WAIT PARAMETER
  4498 0000234C E8A2FFFFFF          <1> 	CALL	GET_PARM
  4499                              <1> 	;MOV	AL,AH			; AL = MOTOR WAIT PARAMETER
  4500                              <1> 	;XOR	AH,AH			; AX = MOTOR WAIT PARAMETER
  4501                              <1> 	;CMP	AL,8			; SEE IF AT LEAST A SECOND IS SPECIFIED
  4502 00002351 80FC08              <1> 	cmp	ah, 8
  4503                              <1> 	;JAE	short GP2		; IF YES, CONTINUE
  4504 00002354 7702                <1> 	ja	short J13
  4505                              <1> 	;MOV	AL,8			; ONE SECOND WAIT FOR MOTOR START UP
  4506 00002356 B408                <1> 	mov	ah, 8
  4507                              <1> 
  4508                              <1> ;-----	AS CONTAINS NUMBER OF 1/8 SECONDS (125000 MICROSECONDS) TO WAIT
  4509                              <1> GP2:	
  4510                              <1> ;----- 	FOLLOWING LOOPS REQUIRED WHEN RTC WAIT FUNCTION IS ALREADY IN USE
  4511                              <1> J13:					; WAIT FOR 1/8 SECOND PER (AL)
  4512 00002358 B95E200000          <1> 	MOV	eCX,8286		; COUNT FOR 1/8 SECOND AT 15.085737 US
  4513 0000235D E867F1FFFF          <1> 	CALL	WAITF			; GO TO FIXED WAIT ROUTINE
  4514                              <1> 	;DEC	AL			; DECREMENT TIME VALUE
  4515 00002362 FECC                <1> 	dec	ah
  4516 00002364 75F2                <1> 	JNZ	short J13		; ARE WE DONE YET
  4517                              <1> MOT_IS_ON:
  4518 00002366 5B                  <1> 	POP	eBX			; RESTORE REG.
  4519 00002367 C3                  <1> 	RETn
  4520                              <1> 
  4521                              <1> ;-------------------------------------------------------------------------------
  4522                              <1> ; TURN_ON
  4523                              <1> ;	TURN MOTOR ON AND RETURN WAIT STATE.
  4524                              <1> ;
  4525                              <1> ; ON ENTRY:	DI = DRIVE #
  4526                              <1> ;
  4527                              <1> ; ON EXIT:	CY = 0 MEANS WAIT REQUIRED
  4528                              <1> ;		CY = 1 MEANS NO WAIT REQUIRED
  4529                              <1> ;		AX,BX,CX,DX DESTROYED
  4530                              <1> ;-------------------------------------------------------------------------------
  4531                              <1> TURN_ON:
  4532 00002368 89FB                <1> 	MOV	eBX,eDI			; BX = DRIVE #
  4533 0000236A 88D9                <1> 	MOV	CL,BL			; CL = DRIVE #
  4534 0000236C C0C304              <1> 	ROL	BL,4			; BL = DRIVE SELECT
  4535 0000236F FA                  <1> 	CLI				; NO INTERRUPTS WHILE DETERMINING STATUS
  4536 00002370 C605[3B700000]FF    <1> 	MOV	byte [MOTOR_COUNT],0FFH	; ENSURE MOTOR STAYS ON FOR OPERATION
  4537 00002377 A0[3A700000]        <1> 	MOV	AL, [MOTOR_STATUS]	; GET DIGITAL OUTPUT REGISTER REFLECTION
  4538 0000237C 2430                <1> 	AND	AL,00110000B		; KEEP ONLY DRIVE SELECT BITS
  4539 0000237E B401                <1> 	MOV	AH,1			; MASK FOR DETERMINING MOTOR BIT
  4540 00002380 D2E4                <1> 	SHL	AH,CL			; AH = MOTOR ON, A=00000001, B=00000010
  4541                              <1> 
  4542                              <1> ;  AL = DRIVE SELECT FROM @MOTOR_STATUS
  4543                              <1> ;  BL = DRIVE SELECT DESIRED
  4544                              <1> ;  AH = MOTOR ON MASK DESIRED
  4545                              <1> 
  4546 00002382 38D8                <1> 	CMP	AL,BL			; REQUESTED DRIVE ALREADY SELECTED ?
  4547 00002384 7508                <1> 	JNZ	short TURN_IT_ON	; IF NOT SELECTED JUMP
  4548 00002386 8425[3A700000]      <1> 	TEST	AH, [MOTOR_STATUS]	; TEST MOTOR ON BIT
  4549 0000238C 7535                <1> 	JNZ	short NO_MOT_WAIT	; JUMP IF MOTOR ON AND SELECTED
  4550                              <1> 
  4551                              <1> TURN_IT_ON:
  4552 0000238E 08DC                <1> 	OR	AH,BL			; AH = DRIVE SELECT AND MOTOR ON
  4553 00002390 8A3D[3A700000]      <1> 	MOV	BH,[MOTOR_STATUS]	; SAVE COPY OF @MOTOR_STATUS BEFORE
  4554 00002396 80E70F              <1> 	AND	BH,00001111B		; KEEP ONLY MOTOR BITS
  4555 00002399 8025[3A700000]CF    <1> 	AND	byte [MOTOR_STATUS],11001111B ; CLEAR OUT DRIVE SELECT
  4556 000023A0 0825[3A700000]      <1> 	OR	[MOTOR_STATUS],AH	; OR IN DRIVE SELECTED AND MOTOR ON
  4557 000023A6 A0[3A700000]        <1> 	MOV	AL,[MOTOR_STATUS]	; GET DIGITAL OUTPUT REGISTER REFLECTION
  4558 000023AB 88C3                <1> 	MOV	BL,AL			; BL=@MOTOR_STATUS AFTER, BH=BEFORE
  4559 000023AD 80E30F              <1> 	AND	BL,00001111B		; KEEP ONLY MOTOR BITS
  4560 000023B0 FB                  <1> 	STI				; ENABLE INTERRUPTS AGAIN
  4561 000023B1 243F                <1> 	AND	AL,00111111B		; STRIP AWAY UNWANTED BITS
  4562 000023B3 C0C004              <1> 	ROL	AL,4			; PUT BITS IN DESIRED POSITIONS
  4563 000023B6 0C0C                <1> 	OR	AL,00001100B		; NO RESET, ENABLE DMA/INTERRUPT
  4564 000023B8 66BAF203            <1> 	MOV	DX,03F2H		; SELECT DRIVE AND TURN ON MOTOR
  4565 000023BC EE                  <1> 	OUT	DX,AL
  4566 000023BD 38FB                <1> 	CMP	BL,BH			; NEW MOTOR TURNED ON ?
  4567                              <1> 	;JZ	short NO_MOT_WAIT	; NO WAIT REQUIRED IF JUST SELECT
  4568 000023BF 7403                <1> 	je	short no_mot_w1 ; 27/02/2015 
  4569 000023C1 F8                  <1> 	CLC				; (re)SET CARRY MEANING WAIT
  4570 000023C2 C3                  <1> 	RETn
  4571                              <1> 
  4572                              <1> NO_MOT_WAIT:
  4573 000023C3 FB                  <1> 	sti
  4574                              <1> no_mot_w1: ; 27/02/2015
  4575 000023C4 F9                  <1> 	STC				; SET NO WAIT REQUIRED
  4576                              <1> 	;STI				; INTERRUPTS BACK ON
  4577 000023C5 C3                  <1> 	RETn
  4578                              <1> 
  4579                              <1> ;-------------------------------------------------------------------------------
  4580                              <1> ; HD_WAIT
  4581                              <1> ;	WAIT FOR HEAD SETTLE TIME.
  4582                              <1> ;
  4583                              <1> ; ON ENTRY:	DI = DRIVE #
  4584                              <1> ;
  4585                              <1> ; ON EXIT:	AX,BX,CX,DX DESTROYED
  4586                              <1> ;-------------------------------------------------------------------------------
  4587                              <1> HD_WAIT:
  4588 000023C6 B209                <1> 	MOV	DL,9			; GET HEAD SETTLE PARAMETER
  4589 000023C8 E826FFFFFF          <1> 	CALL	GET_PARM
  4590 000023CD 08E4                <1> 	or	ah, ah	; 17/12/2014
  4591 000023CF 7519                <1> 	jnz	short DO_WAT
  4592 000023D1 F605[3A700000]80    <1>         TEST    byte [MOTOR_STATUS],10000000B ; SEE IF A WRITE OPERATION
  4593                              <1> 	;JZ	short ISNT_WRITE	; IF NOT, DO NOT ENFORCE ANY VALUES
  4594                              <1> 	;OR	AH,AH			; CHECK FOR ANY WAIT?
  4595                              <1> 	;JNZ	short DO_WAT		; IF THERE DO NOT ENFORCE
  4596 000023D8 741E                <1> 	jz	short HW_DONE
  4597 000023DA B40F                <1> 	MOV	AH,HD12_SETTLE		; LOAD 1.2M HEAD SETTLE MINIMUM
  4598 000023DC 8A87[49700000]      <1> 	MOV	AL,[DSK_STATE+eDI]	; LOAD STATE
  4599 000023E2 24C0                <1> 	AND	AL,RATE_MSK		; KEEP ONLY RATE
  4600 000023E4 3C80                <1> 	CMP	AL,RATE_250		; 1.2 M DRIVE ?
  4601 000023E6 7502                <1> 	JNZ	short DO_WAT		; DEFAULT HEAD SETTLE LOADED
  4602                              <1> ;GP3:
  4603 000023E8 B414                <1> 	MOV	AH,HD320_SETTLE		; USE 320/360 HEAD SETTLE
  4604                              <1> ;	JMP	SHORT DO_WAT
  4605                              <1> 
  4606                              <1> ;ISNT_WRITE:
  4607                              <1> ;	OR	AH,AH			; CHECK FOR NO WAIT
  4608                              <1> ;	JZ	short HW_DONE		; IF NOT WRITE AND 0 ITS OK
  4609                              <1> 
  4610                              <1> ;-----	AH CONTAINS NUMBER OF MILLISECONDS TO WAIT
  4611                              <1> DO_WAT:
  4612                              <1> ;	MOV	AL,AH			; AL = # MILLISECONDS
  4613                              <1> ;	;XOR	AH,AH			; AX = # MILLISECONDS
  4614                              <1> J29:					; 	1 MILLISECOND LOOP
  4615                              <1> 	;mov	cx, WAIT_FDU_HEAD_SETTLE ; 33 ; 1 ms in 30 micro units.
  4616 000023EA B942000000          <1> 	MOV	eCX,66			; COUNT AT 15.085737 US PER COUNT
  4617 000023EF E8D5F0FFFF          <1> 	CALL	WAITF			; DELAY FOR 1 MILLISECOND
  4618                              <1> 	;DEC	AL			; DECREMENT THE COUNT
  4619 000023F4 FECC                <1> 	dec	ah
  4620 000023F6 75F2                <1> 	JNZ	short J29		; DO AL MILLISECOND # OF TIMES
  4621                              <1> HW_DONE:
  4622 000023F8 C3                  <1> 	RETn
  4623                              <1> 
  4624                              <1> ;-------------------------------------------------------------------------------
  4625                              <1> ; NEC_OUTPUT
  4626                              <1> ;	THIS ROUTINE SENDS A BYTE TO THE NEC CONTROLLER AFTER TESTING
  4627                              <1> ;	FOR CORRECT DIRECTION AND CONTROLLER READY THIS ROUTINE WILL
  4628                              <1> ;	TIME OUT IF THE BYTE IS NOT ACCEPTED WITHIN A REASONABLE AMOUNT
  4629                              <1> ;	OF TIME, SETTING THE DISKETTE STATUS ON COMPLETION.
  4630                              <1> ; 
  4631                              <1> ; ON ENTRY: 	AH = BYTE TO BE OUTPUT
  4632                              <1> ;
  4633                              <1> ; ON EXIT:	CY = 0  SUCCESS
  4634                              <1> ;		CY = 1  FAILURE -- DISKETTE STATUS UPDATED
  4635                              <1> ;		        IF A FAILURE HAS OCCURRED, THE RETURN IS MADE ONE LEVEL
  4636                              <1> ;		        HIGHER THAN THE CALLER OF NEC OUTPUT. THIS REMOVES THE
  4637                              <1> ;		        REQUIREMENT OF TESTING AFTER EVERY CALL OF NEC_OUTPUT.
  4638                              <1> ;		AX,CX,DX DESTROYED
  4639                              <1> ;-------------------------------------------------------------------------------
  4640                              <1> 
  4641                              <1> ; 09/12/2014 [Erdogan Tan] 
  4642                              <1> ;	(from 'PS2 Hardware Interface Tech. Ref. May 88', Page 09-05.)
  4643                              <1> ; Diskette Drive Controller Status Register (3F4h)
  4644                              <1> ;	This read only register facilitates the transfer of data between
  4645                              <1> ;	the system microprocessor and the controller.
  4646                              <1> ; Bit 7 - When set to 1, the Data register is ready to transfer data 
  4647                              <1> ;	  with the system micrprocessor.
  4648                              <1> ; Bit 6 - The direction of data transfer. If this bit is set to 0,
  4649                              <1> ;	  the transfer is to the controller.
  4650                              <1> ; Bit 5 - When this bit is set to 1, the controller is in the non-DMA mode.
  4651                              <1> ; Bit 4 - When this bit is set to 1, a Read or Write command is being executed.
  4652                              <1> ; Bit 3 - Reserved.
  4653                              <1> ; Bit 2 - Reserved.
  4654                              <1> ; Bit 1 - When this bit is set to 1, dskette drive 1 is in the seek mode.
  4655                              <1> ; Bit 0 - When this bit is set to 1, dskette drive 1 is in the seek mode.
  4656                              <1> 
  4657                              <1> ; Data Register (3F5h)
  4658                              <1> ; This read/write register passes data, commands and parameters, and provides
  4659                              <1> ; diskette status information.
  4660                              <1>   		
  4661                              <1> NEC_OUTPUT:
  4662                              <1> 	;PUSH	BX			; SAVE REG.
  4663 000023F9 66BAF403            <1> 	MOV	DX,03F4H		; STATUS PORT
  4664                              <1> 	;MOV	BL,2			; HIGH ORDER COUNTER
  4665                              <1> 	;XOR	CX,CX			; COUNT FOR TIME OUT
  4666                              <1> 	; 16/12/2014
  4667                              <1> 	; waiting for (max.) 0.5 seconds
  4668                              <1>         ;;mov	byte [wait_count], 0 ;; 27/02/2015
  4669                              <1> 	;
  4670                              <1> 	; 17/12/2014
  4671                              <1> 	; Modified from AWARD BIOS 1999 - ADISK.ASM - SEND_COMMAND
  4672                              <1> 	;
  4673                              <1> 	;WAIT_FOR_PORT:	Waits for a bit at a port pointed to by DX to
  4674                              <1> 	;		go on.
  4675                              <1> 	;INPUT:
  4676                              <1> 	;	AH=Mask for isolation bits.
  4677                              <1> 	;	AL=pattern to look for.
  4678                              <1> 	;	DX=Port to test for
  4679                              <1> 	;	BH:CX=Number of memory refresh periods to delay.
  4680                              <1> 	;	     (normally 30 microseconds per period.)
  4681                              <1> 	;
  4682                              <1> 	;WFP_SHORT:  
  4683                              <1> 	;	Wait for port if refresh cycle is short (15-80 Us range).
  4684                              <1> 	;
  4685                              <1> 
  4686                              <1> ;	mov	bl, WAIT_FDU_SEND_HI+1	; 0+1
  4687                              <1> ;	mov	cx, WAIT_FDU_SEND_LO	; 16667
  4688 000023FD B91B410000          <1> 	mov	ecx, WAIT_FDU_SEND_LH   ; 16667 (27/02/2015)
  4689                              <1> ;
  4690                              <1> ;WFPS_OUTER_LP:
  4691                              <1> ;	;
  4692                              <1> ;WFPS_CHECK_PORT:
  4693                              <1> J23:
  4694 00002402 EC                  <1> 	IN	AL,DX			; GET STATUS
  4695 00002403 24C0                <1> 	AND	AL,11000000B		; KEEP STATUS AND DIRECTION
  4696 00002405 3C80                <1> 	CMP	AL,10000000B		; STATUS 1 AND DIRECTION 0 ?
  4697 00002407 7418                <1> 	JZ	short J27		; STATUS AND DIRECTION OK
  4698                              <1> WFPS_HI:
  4699 00002409 E461                <1> 	IN	AL, PORT_B	;061h	; SYS1	; wait for hi to lo
  4700 0000240B A810                <1> 	TEST	AL,010H			; transition on memory
  4701 0000240D 75FA                <1> 	JNZ	SHORT WFPS_HI		; refresh.
  4702                              <1> WFPS_LO:
  4703 0000240F E461                <1> 	IN	AL, PORT_B		; SYS1
  4704 00002411 A810                <1> 	TEST	AL,010H
  4705 00002413 74FA                <1> 	JZ	SHORT WFPS_LO
  4706                              <1> 	;LOOP	SHORT WFPS_CHECK_PORT
  4707 00002415 E2EB                <1> 	loop	J23	; 27/02/2015
  4708                              <1> ;	;
  4709                              <1> ;	dec	bl
  4710                              <1> ;	jnz	short WFPS_OUTER_LP
  4711                              <1> ;	jmp	short WFPS_TIMEOUT	; fail
  4712                              <1> ;J23:
  4713                              <1> ;	IN	AL,DX			; GET STATUS
  4714                              <1> ;	AND	AL,11000000B		; KEEP STATUS AND DIRECTION
  4715                              <1> ;	CMP	AL,10000000B		; STATUS 1 AND DIRECTION 0 ?
  4716                              <1> ;	JZ	short J27		; STATUS AND DIRECTION OK
  4717                              <1> 	;LOOP	J23			; CONTINUE TILL CX EXHAUSTED
  4718                              <1> 	;DEC	BL			; DECREMENT COUNTER
  4719                              <1> 	;JNZ	short J23		; REPEAT TILL DELAY FINISHED, CX = 0
  4720                              <1>    
  4721                              <1> 	;;27/02/2015
  4722                              <1> 	;16/12/2014
  4723                              <1>         ;;cmp	byte [wait_count], 10   ; (10/18.2 seconds)
  4724                              <1> 	;;jb	short J23
  4725                              <1> 
  4726                              <1> ;WFPS_TIMEOUT:
  4727                              <1> 
  4728                              <1> ;-----	FALL THRU TO ERROR RETURN
  4729                              <1> 
  4730 00002417 800D[3C700000]80    <1> 	OR	byte [DSKETTE_STATUS],TIME_OUT
  4731                              <1> 	;POP	BX			; RESTORE REG.
  4732 0000241E 58                  <1> 	POP	eAX ; 08/02/2015	; DISCARD THE RETURN ADDRESS
  4733 0000241F F9                  <1> 	STC				; INDICATE ERROR TO CALLER
  4734 00002420 C3                  <1> 	RETn
  4735                              <1> 
  4736                              <1> ;-----	DIRECTION AND STATUS OK; OUTPUT BYTE
  4737                              <1> 
  4738                              <1> J27:	
  4739 00002421 88E0                <1> 	MOV	AL,AH			; GET BYTE TO OUTPUT
  4740 00002423 6642                <1> 	INC	DX			; DATA PORT = STATUS PORT + 1
  4741 00002425 EE                  <1> 	OUT	DX,AL			; OUTPUT THE BYTE
  4742                              <1> 	;;NEWIODELAY  ;; 27/02/2015
  4743                              <1> 	; 27/02/2015
  4744 00002426 9C                  <1> 	PUSHFd	; 05/12/2021		; SAVE FLAGS
  4745                              <1> 	;MOV	eCX, 3			; 30 TO 45 MICROSECONDS WAIT FOR
  4746 00002427 29C9                <1> 	sub	ecx, ecx
  4747 00002429 B103                <1> 	mov	cl, 3 ; 05/12/2021
  4748 0000242B E899F0FFFF          <1> 	CALL 	WAITF			; NEC FLAGS UPDATE CYCLE
  4749 00002430 9D                  <1> 	POPFd	; 05/12/2021			; RESTORE FLAGS FOR EXIT
  4750                              <1> 	;POP	BX			; RESTORE REG
  4751 00002431 C3                  <1> 	RETn				; CY = 0 FROM TEST INSTRUCTION
  4752                              <1> 
  4753                              <1> ;-------------------------------------------------------------------------------
  4754                              <1> ; SEEK
  4755                              <1> ;	THIS ROUTINE WILL MOVE THE HEAD ON THE NAMED DRIVE TO THE NAMED
  4756                              <1> ;	TRACK. IF THE DRIVE HAS NOT BEEN ACCESSED SINCE THE DRIVE
  4757                              <1> ;	RESET COMMAND WAS ISSUED, THE DRIVE WILL BE RECALIBRATED.
  4758                              <1> ;
  4759                              <1> ; ON ENTRY:	DI = DRIVE #
  4760                              <1> ;		CH = TRACK #
  4761                              <1> ;
  4762                              <1> ; ON EXIT:	@DSKETTE_STATUS, CY REFLECT STATUS OF OPERATION.
  4763                              <1> ;		AX,BX,CX DX DESTROYED
  4764                              <1> ;-------------------------------------------------------------------------------
  4765                              <1> SEEK:
  4766 00002432 89FB                <1> 	MOV	eBX,eDI			; BX = DRIVE #
  4767 00002434 B001                <1> 	MOV	AL,1			; ESTABLISH MASK FOR RECALIBRATE TEST
  4768 00002436 86CB                <1> 	XCHG	CL,BL			; SET DRIVE VALULE INTO CL
  4769 00002438 D2C0                <1> 	ROL	AL,CL			; SHIFT MASK BY THE DRIVE VALUE
  4770 0000243A 86CB                <1> 	XCHG	CL,BL			; RECOVER TRACK VALUE
  4771 0000243C 8405[39700000]      <1> 	TEST	AL,[SEEK_STATUS]	; TEST FOR RECALIBRATE REQUIRED
  4772 00002442 7526                <1> 	JNZ	short J28A		; JUMP IF RECALIBRATE NOT REQUIRED
  4773                              <1> 
  4774 00002444 0805[39700000]      <1> 	OR	[SEEK_STATUS],AL	; TURN ON THE NO RECALIBRATE BIT IN FLAG
  4775 0000244A E862000000          <1> 	CALL	RECAL			; RECALIBRATE DRIVE
  4776 0000244F 730E                <1> 	JNC	short AFT_RECAL		; RECALIBRATE DONE
  4777                              <1> 
  4778                              <1> ;-----	ISSUE RECALIBRATE FOR 80 TRACK DISKETTES
  4779                              <1> 
  4780 00002451 C605[3C700000]00    <1> 	MOV	byte [DSKETTE_STATUS],0	; CLEAR OUT INVALID STATUS
  4781 00002458 E854000000          <1> 	CALL	RECAL			; RECALIBRATE DRIVE
  4782 0000245D 7251                <1> 	JC	short RB		; IF RECALIBRATE FAILS TWICE THEN ERROR
  4783                              <1> 
  4784                              <1> AFT_RECAL:
  4785 0000245F C687[4D700000]00    <1>         MOV     byte [DSK_TRK+eDI],0    ; SAVE NEW CYLINDER AS PRESENT POSITION
  4786 00002466 08ED                <1> 	OR	CH,CH			; CHECK FOR SEEK TO TRACK 0
  4787 00002468 743F                <1> 	JZ	short DO_WAIT		; HEAD SETTLE, CY = 0 IF JUMP
  4788                              <1> 
  4789                              <1> ;-----	DRIVE IS IN SYNCHRONIZATION WITH CONTROLLER, SEEK TO TRACK
  4790                              <1> 
  4791 0000246A F687[49700000]20    <1> J28A:	TEST	byte [DSK_STATE+eDI],DBL_STEP ; CHECK FOR DOUBLE STEP REQUIRED
  4792 00002471 7402                <1> 	JZ	short _R7		; SINGLE STEP REQUIRED BYPASS DOUBLE
  4793 00002473 D0E5                <1> 	SHL	CH,1			; DOUBLE NUMBER OF STEP TO TAKE
  4794                              <1> 
  4795 00002475 3AAF[4D700000]      <1> _R7:	CMP	CH, [DSK_TRK+eDI]	; SEE IF ALREADY AT THE DESIRED TRACK
  4796 0000247B 7433                <1> 	JE	short RB		; IF YES, DO NOT NEED TO SEEK
  4797                              <1> 
  4798 0000247D BA[B0240000]        <1> 	MOV	eDX, NEC_ERR		; LOAD RETURN ADDRESS
  4799 00002482 52                  <1> 	PUSH	eDX ; (*)		; ON STACK FOR NEC OUTPUT ERROR
  4800 00002483 88AF[4D700000]      <1> 	MOV	[DSK_TRK+eDI],CH	; SAVE NEW CYLINDER AS PRESENT POSITION
  4801 00002489 B40F                <1> 	MOV	AH,0FH			; SEEK COMMAND TO NEC
  4802 0000248B E869FFFFFF          <1> 	CALL	NEC_OUTPUT
  4803 00002490 89FB                <1> 	MOV	eBX,eDI			; BX = DRIVE #
  4804 00002492 88DC                <1> 	MOV	AH,BL			; OUTPUT DRIVE NUMBER
  4805 00002494 E860FFFFFF          <1> 	CALL	NEC_OUTPUT
  4806 00002499 8AA7[4D700000]      <1> 	MOV	AH, [DSK_TRK+eDI]	; GET CYLINDER NUMBER
  4807 0000249F E855FFFFFF          <1> 	CALL	NEC_OUTPUT
  4808 000024A4 E827000000          <1> 	CALL	CHK_STAT_2		; ENDING INTERRUPT AND SENSE STATUS
  4809                              <1> 
  4810                              <1> ;-----	WAIT FOR HEAD SETTLE
  4811                              <1> 
  4812                              <1> DO_WAIT:
  4813 000024A9 9C                  <1> 	PUSHFd	; 05/12/2021		; SAVE STATUS
  4814 000024AA E817FFFFFF          <1> 	CALL	HD_WAIT			; WAIT FOR HEAD SETTLE TIME
  4815 000024AF 9D                  <1> 	POPFd	; 05/12/2021		; RESTORE STATUS
  4816                              <1> RB:
  4817                              <1> NEC_ERR:
  4818                              <1> 	; 08/02/2015 (code trick here from original IBM PC/AT DISKETTE.ASM)
  4819                              <1> 	; (*) nec_err -> retn (push edx -> pop edx) -> nec_err -> retn
  4820 000024B0 C3                  <1> 	RETn				; RETURN TO CALLER
  4821                              <1> 
  4822                              <1> ;-------------------------------------------------------------------------------
  4823                              <1> ; RECAL
  4824                              <1> ;	RECALIBRATE DRIVE
  4825                              <1> ;
  4826                              <1> ; ON ENTRY:	DI = DRIVE #
  4827                              <1> ;
  4828                              <1> ; ON EXIT:	CY REFLECTS STATUS OF OPERATION.
  4829                              <1> ;-------------------------------------------------------------------------------
  4830                              <1> RECAL:
  4831                              <1> 	;PUSH	CX
  4832                              <1> 	; 05/12/2021
  4833 000024B1 51                  <1> 	push	ecx
  4834 000024B2 B8[CE240000]        <1> 	MOV	eAX, RC_BACK		; LOAD NEC_OUTPUT ERROR
  4835 000024B7 50                  <1> 	PUSH	eAX
  4836 000024B8 B407                <1> 	MOV	AH,07H			; RECALIBRATE COMMAND
  4837 000024BA E83AFFFFFF          <1> 	CALL	NEC_OUTPUT
  4838 000024BF 89FB                <1> 	MOV	eBX,eDI			; BX = DRIVE #
  4839 000024C1 88DC                <1> 	MOV	AH,BL
  4840 000024C3 E831FFFFFF          <1> 	CALL	NEC_OUTPUT		; OUTPUT THE DRIVE NUMBER
  4841 000024C8 E803000000          <1> 	CALL	CHK_STAT_2		; GET THE INTERRUPT AND SENSE INT STATUS
  4842 000024CD 58                  <1> 	POP	eAX			; THROW AWAY ERROR
  4843                              <1> RC_BACK:
  4844                              <1> 	;POP	CX
  4845                              <1> 	; 05/12/2021
  4846 000024CE 59                  <1> 	pop	ecx
  4847 000024CF C3                  <1> 	RETn
  4848                              <1> 
  4849                              <1> ;-------------------------------------------------------------------------------
  4850                              <1> ; CHK_STAT_2
  4851                              <1> ;	THIS ROUTINE HANDLES THE INTERRUPT RECEIVED AFTER RECALIBRATE,
  4852                              <1> ;	OR SEEK TO THE ADAPTER. THE INTERRUPT IS WAITED FOR, THE
  4853                              <1> ;	INTERRUPT STATUS SENSED, AND THE RESULT RETURNED TO THE CALLER.
  4854                              <1> ;
  4855                              <1> ; ON EXIT:	@DSKETTE_STATUS, CY REFLECT STATUS OF OPERATION.
  4856                              <1> ;-------------------------------------------------------------------------------
  4857                              <1> CHK_STAT_2:
  4858 000024D0 B8[F8240000]        <1>         MOV     eAX, CS_BACK            ; LOAD NEC_OUTPUT ERROR ADDRESS
  4859 000024D5 50                  <1> 	PUSH	eAX
  4860 000024D6 E828000000          <1> 	CALL	WAIT_INT		; WAIT FOR THE INTERRUPT
  4861 000024DB 721A                <1> 	JC	short J34		; IF ERROR, RETURN IT
  4862 000024DD B408                <1> 	MOV	AH,08H			; SENSE INTERRUPT STATUS COMMAND
  4863 000024DF E815FFFFFF          <1> 	CALL	NEC_OUTPUT
  4864 000024E4 E84A000000          <1> 	CALL	RESULTS			; READ IN THE RESULTS
  4865 000024E9 720C                <1> 	JC	short J34
  4866 000024EB A0[3D700000]        <1> 	MOV	AL,[NEC_STATUS]		; GET THE FIRST STATUS BYTE
  4867 000024F0 2460                <1> 	AND	AL,01100000B		; ISOLATE THE BITS
  4868 000024F2 3C60                <1> 	CMP	AL,01100000B		; TEST FOR CORRECT VALUE
  4869 000024F4 7403                <1> 	JZ	short J35		; IF ERROR, GO MARK IT
  4870 000024F6 F8                  <1> 	CLC				; GOOD RETURN
  4871                              <1> J34:
  4872 000024F7 58                  <1> 	POP	eAX			; THROW AWAY ERROR RETURN
  4873                              <1> CS_BACK:
  4874 000024F8 C3                  <1> 	RETn
  4875                              <1> J35:
  4876 000024F9 800D[3C700000]40    <1> 	OR	byte [DSKETTE_STATUS], BAD_SEEK
  4877 00002500 F9                  <1> 	STC				; ERROR RETURN CODE
  4878 00002501 EBF4                <1> 	JMP	SHORT J34
  4879                              <1> 
  4880                              <1> ;-------------------------------------------------------------------------------
  4881                              <1> ; WAIT_INT
  4882                              <1> ;	THIS ROUTINE WAITS FOR AN INTERRUPT TO OCCUR A TIME OUT ROUTINE
  4883                              <1> ;	TAKES PLACE DURING THE WAIT, SO THAT AN ERROR MAY BE RETURNED
  4884                              <1> ;	IF THE DRIVE IS NOT READY.
  4885                              <1> ;
  4886                              <1> ; ON EXIT: 	@DSKETTE_STATUS, CY REFLECT STATUS OF OPERATION.
  4887                              <1> ;-------------------------------------------------------------------------------
  4888                              <1> 
  4889                              <1> ; 17/12/2014
  4890                              <1> ; 2.5 seconds waiting !
  4891                              <1> ;(AWARD BIOS - 1999, WAIT_FDU_INT_LOW, WAIT_FDU_INT_HI)
  4892                              <1> ; amount of time to wait for completion interrupt from NEC.
  4893                              <1> 
  4894                              <1> 
  4895                              <1> WAIT_INT:
  4896 00002503 FB                  <1> 	STI				; TURN ON INTERRUPTS, JUST IN CASE
  4897 00002504 F8                  <1> 	CLC				; CLEAR TIMEOUT INDICATOR
  4898                              <1>        ;MOV	BL,10			; CLEAR THE COUNTERS
  4899                              <1>        ;XOR	CX,CX			; FOR 2 SECOND WAIT
  4900                              <1> 
  4901                              <1> 	; Modification from AWARD BIOS - 1999 (ATORGS.ASM, WAIT
  4902                              <1> 	;
  4903                              <1> 	;WAIT_FOR_MEM:	
  4904                              <1> 	;	Waits for a bit at a specified memory location pointed
  4905                              <1> 	;	to by ES:[DI] to become set.
  4906                              <1> 	;INPUT:
  4907                              <1> 	;	AH=Mask to test with.
  4908                              <1> 	;	ES:[DI] = memory location to watch.
  4909                              <1> 	;	BH:CX=Number of memory refresh periods to delay.
  4910                              <1> 	;	     (normally 30 microseconds per period.)
  4911                              <1> 
  4912                              <1> 	; waiting for (max.) 2.5 secs in 30 micro units.
  4913                              <1> ;	mov 	cx, WAIT_FDU_INT_LO		; 017798
  4914                              <1> ;;	mov 	bl, WAIT_FDU_INT_HI
  4915                              <1> ;	mov 	bl, WAIT_FDU_INT_HI + 1
  4916                              <1> 	; 27/02/2015
  4917 00002505 B986450100          <1> 	mov 	ecx, WAIT_FDU_INT_LH	; 83334 (2.5 seconds)		
  4918                              <1> WFMS_CHECK_MEM:
  4919 0000250A F605[39700000]80    <1> 	test	byte [SEEK_STATUS],INT_FLAG ; TEST FOR INTERRUPT OCCURRING
  4920 00002511 7516                <1>         jnz     short J37
  4921                              <1> WFMS_HI:
  4922 00002513 E461                <1> 	IN	AL,PORT_B  ; 061h	; SYS1, wait for lo to hi
  4923 00002515 A810                <1> 	TEST	AL,010H			; transition on memory
  4924 00002517 75FA                <1> 	JNZ	SHORT WFMS_HI		; refresh.
  4925                              <1> WFMS_LO:
  4926 00002519 E461                <1> 	IN	AL,PORT_B		;SYS1
  4927 0000251B A810                <1> 	TEST	AL,010H
  4928 0000251D 74FA                <1> 	JZ	SHORT WFMS_LO
  4929 0000251F E2E9                <1>         LOOP    WFMS_CHECK_MEM
  4930                              <1> ;WFMS_OUTER_LP:
  4931                              <1> ;;	or	bl, bl			; check outer counter
  4932                              <1> ;;	jz	short J36A		; WFMS_TIMEOUT
  4933                              <1> ;	dec	bl
  4934                              <1> ;	jz	short J36A	
  4935                              <1> ;	jmp	short WFMS_CHECK_MEM
  4936                              <1> 
  4937                              <1> 	;17/12/2014
  4938                              <1> 	;16/12/2014
  4939                              <1> ;	mov     byte [wait_count], 0    ; Reset (INT 08H) counter
  4940                              <1> ;J36:
  4941                              <1> ;	TEST	byte [SEEK_STATUS],INT_FLAG ; TEST FOR INTERRUPT OCCURRING
  4942                              <1> ;	JNZ	short J37
  4943                              <1> 	;16/12/2014
  4944                              <1> 	;LOOP	J36			; COUNT DOWN WHILE WAITING
  4945                              <1> 	;DEC	BL			; SECOND LEVEL COUNTER
  4946                              <1> 	;JNZ	short J36
  4947                              <1> ;       cmp     byte [wait_count], 46   ; (46/18.2 seconds)
  4948                              <1> ;	jb	short J36
  4949                              <1> 
  4950                              <1> ;WFMS_TIMEOUT:
  4951                              <1> ;J36A:
  4952 00002521 800D[3C700000]80    <1> 	OR	byte [DSKETTE_STATUS], TIME_OUT ; NOTHING HAPPENED
  4953 00002528 F9                  <1> 	STC				; ERROR RETURN
  4954                              <1> J37:
  4955 00002529 9C                  <1> 	PUSHF				; SAVE CURRENT CARRY
  4956 0000252A 8025[39700000]7F    <1> 	AND	byte [SEEK_STATUS], ~INT_FLAG ; TURN OFF INTERRUPT FLAG
  4957 00002531 9D                  <1> 	POPF				; RECOVER CARRY
  4958 00002532 C3                  <1> 	RETn				; GOOD RETURN CODE
  4959                              <1> 
  4960                              <1> ;-------------------------------------------------------------------------------
  4961                              <1> ; RESULTS
  4962                              <1> ;	THIS ROUTINE WILL READ ANYTHING THAT THE NEC CONTROLLER RETURNS 
  4963                              <1> ;	FOLLOWING AN INTERRUPT.
  4964                              <1> ;
  4965                              <1> ; ON EXIT:	@DSKETTE_STATUS, CY REFLECT STATUS OF OPERATION.
  4966                              <1> ;		AX,BX,CX,DX DESTROYED
  4967                              <1> ;-------------------------------------------------------------------------------
  4968                              <1> RESULTS:
  4969 00002533 57                  <1> 	PUSH	eDI
  4970 00002534 BF[3D700000]        <1> 	MOV	eDI, NEC_STATUS		; POINTER TO DATA AREA
  4971 00002539 B307                <1> 	MOV	BL,7			; MAX STATUS BYTES
  4972 0000253B 66BAF403            <1> 	MOV	DX,03F4H		; STATUS PORT
  4973                              <1> 
  4974                              <1> ;-----	WAIT FOR REQUEST FOR MASTER
  4975                              <1> 
  4976                              <1> _R10: 
  4977                              <1> 	; 16/12/2014
  4978                              <1> 	; wait for (max) 0.5 seconds
  4979                              <1> 	;MOV	BH,2			; HIGH ORDER COUNTER
  4980                              <1> 	;XOR	CX,CX			; COUNTER
  4981                              <1> 
  4982                              <1> 	;Time to wait while waiting for each byte of NEC results = .5
  4983                              <1> 	;seconds.  .5 seconds = 500,000 micros.  500,000/30 = 16,667.
  4984                              <1> 	; 27/02/2015
  4985 0000253F B91B410000          <1> 	mov 	ecx, WAIT_FDU_RESULTS_LH ; 16667  
  4986                              <1> 	;mov	cx, WAIT_FDU_RESULTS_LO  ; 16667
  4987                              <1> 	;mov	bh, WAIT_FDU_RESULTS_HI+1 ; 0+1
  4988                              <1> 
  4989                              <1> WFPSR_OUTER_LP:
  4990                              <1> 	;
  4991                              <1> WFPSR_CHECK_PORT:
  4992                              <1> J39:					; WAIT FOR MASTER
  4993 00002544 EC                  <1> 	IN	AL,DX			; GET STATUS
  4994 00002545 24C0                <1> 	AND	AL,11000000B		; KEEP ONLY STATUS AND DIRECTION
  4995 00002547 3CC0                <1> 	CMP	AL,11000000B		; STATUS 1 AND DIRECTION 1 ?
  4996 00002549 7418                <1> 	JZ	short J42		; STATUS AND DIRECTION OK
  4997                              <1> WFPSR_HI:
  4998 0000254B E461                <1> 	IN	AL, PORT_B	;061h	; SYS1	; wait for hi to lo
  4999 0000254D A810                <1> 	TEST	AL,010H			; transition on memory
  5000 0000254F 75FA                <1> 	JNZ	SHORT WFPSR_HI		; refresh.
  5001                              <1> WFPSR_LO:
  5002 00002551 E461                <1> 	IN	AL, PORT_B		; SYS1
  5003 00002553 A810                <1> 	TEST	AL,010H
  5004 00002555 74FA                <1> 	JZ	SHORT WFPSR_LO
  5005 00002557 E2EB                <1>         LOOP    WFPSR_CHECK_PORT
  5006                              <1> 	;; 27/02/2015
  5007                              <1> 	;;dec	bh
  5008                              <1> 	;;jnz	short WFPSR_OUTER_LP
  5009                              <1> 	;jmp	short WFPSR_TIMEOUT	; fail
  5010                              <1> 
  5011                              <1> 	;;mov	byte [wait_count], 0
  5012                              <1> ;J39:					; WAIT FOR MASTER
  5013                              <1> ;	IN	AL,DX			; GET STATUS
  5014                              <1> ;	AND	AL,11000000B		; KEEP ONLY STATUS AND DIRECTION
  5015                              <1> ;	CMP	AL,11000000B		; STATUS 1 AND DIRECTION 1 ?
  5016                              <1> ;	JZ	short J42		; STATUS AND DIRECTION OK
  5017                              <1> 	;LOOP	J39			; LOOP TILL TIMEOUT
  5018                              <1> 	;DEC	BH			; DECREMENT HIGH ORDER COUNTER
  5019                              <1> 	;JNZ	short J39		; REPEAT TILL DELAY DONE
  5020                              <1> 	;
  5021                              <1> 	;;cmp	byte [wait_count], 10  ; (10/18.2 seconds)
  5022                              <1> 	;;jb	short J39	
  5023                              <1> 
  5024                              <1> ;WFPSR_TIMEOUT:
  5025 00002559 800D[3C700000]80    <1> 	OR	byte [DSKETTE_STATUS],TIME_OUT
  5026 00002560 F9                  <1> 	STC				; SET ERROR RETURN
  5027 00002561 EB29                <1> 	JMP	SHORT POPRES		; POP REGISTERS AND RETURN
  5028                              <1> 
  5029                              <1> ;-----	READ IN THE STATUS
  5030                              <1> 
  5031                              <1> J42:
  5032 00002563 EB00                <1> 	JMP	$+2			; I/O DELAY
  5033 00002565 6642                <1> 	INC	DX			; POINT AT DATA PORT
  5034 00002567 EC                  <1> 	IN	AL,DX			; GET THE DATA
  5035                              <1> 	; 16/12/2014
  5036                              <1> 	NEWIODELAY
  2132 00002568 E6EB                <2>  out 0EBh,al
  5037 0000256A 8807                <1>         MOV     [eDI],AL                ; STORE THE BYTE
  5038 0000256C 47                  <1> 	INC	eDI			; INCREMENT THE POINTER
  5039                              <1> 	; 16/12/2014
  5040                              <1> ;	push	cx
  5041                              <1> ;	mov	cx, 30
  5042                              <1> ;wdw2:
  5043                              <1> ;	NEWIODELAY
  5044                              <1> ;	loop	wdw2
  5045                              <1> ;	pop	cx
  5046                              <1> 
  5047 0000256D B903000000          <1> 	MOV	eCX,3			; MINIMUM 24 MICROSECONDS FOR NEC
  5048 00002572 E852EFFFFF          <1> 	CALL	WAITF			; WAIT 30 TO 45 MICROSECONDS
  5049 00002577 664A                <1> 	DEC	DX			; POINT AT STATUS PORT
  5050 00002579 EC                  <1> 	IN	AL,DX			; GET STATUS
  5051                              <1> 	; 16/12/2014
  5052                              <1> 	NEWIODELAY
  2132 0000257A E6EB                <2>  out 0EBh,al
  5053                              <1> 	;
  5054 0000257C A810                <1> 	TEST	AL,00010000B		; TEST FOR NEC STILL BUSY
  5055 0000257E 740C                <1> 	JZ	short POPRES		; RESULTS DONE ?
  5056                              <1> 
  5057 00002580 FECB                <1> 	DEC	BL			; DECREMENT THE STATUS COUNTER
  5058 00002582 75BB                <1>         JNZ     short _R10              ; GO BACK FOR MORE
  5059 00002584 800D[3C700000]20    <1> 	OR	byte [DSKETTE_STATUS],BAD_NEC ; TOO MANY STATUS BYTES
  5060 0000258B F9                  <1> 	STC				; SET ERROR FLAG
  5061                              <1> 
  5062                              <1> ;-----	RESULT OPERATION IS DONE
  5063                              <1> POPRES:
  5064 0000258C 5F                  <1> 	POP	eDI
  5065 0000258D C3                  <1> 	RETn				; RETURN WITH CARRY SET
  5066                              <1> 
  5067                              <1> ;-------------------------------------------------------------------------------
  5068                              <1> ; READ_DSKCHNG
  5069                              <1> ;	READS THE STATE OF THE DISK CHANGE LINE.
  5070                              <1> ;
  5071                              <1> ; ON ENTRY:	DI = DRIVE #
  5072                              <1> ;
  5073                              <1> ; ON EXIT:	DI = DRIVE #
  5074                              <1> ;		ZF = 0 : DISK CHANGE LINE INACTIVE
  5075                              <1> ;		ZF = 1 : DISK CHANGE LINE ACTIVE
  5076                              <1> ;		AX,CX,DX DESTROYED
  5077                              <1> ;-------------------------------------------------------------------------------
  5078                              <1> READ_DSKCHNG:
  5079 0000258E E8A5FDFFFF          <1> 	CALL	MOTOR_ON		; TURN ON THE MOTOR IF OFF
  5080 00002593 66BAF703            <1> 	MOV	DX,03F7H		; ADDRESS DIGITAL INPUT REGISTER
  5081 00002597 EC                  <1> 	IN	AL,DX			; INPUT DIGITAL INPUT REGISTER
  5082 00002598 A880                <1> 	TEST	AL,DSK_CHG		; CHECK FOR DISK CHANGE LINE ACTIVE
  5083 0000259A C3                  <1> 	RETn				; RETURN TO CALLER WITH ZERO FLAG SET
  5084                              <1> 
  5085                              <1> ;-------------------------------------------------------------------------------
  5086                              <1> ; DRIVE_DET
  5087                              <1> ;	DETERMINES WHETHER DRIVE IS 80 OR 40 TRACKS AND
  5088                              <1> ;	UPDATES STATE INFORMATION ACCORDINGLY.
  5089                              <1> ; ON ENTRY:	DI = DRIVE #
  5090                              <1> ;-------------------------------------------------------------------------------
  5091                              <1> DRIVE_DET:
  5092 0000259B E898FDFFFF          <1> 	CALL	MOTOR_ON		; TURN ON MOTOR IF NOT ALREADY ON
  5093 000025A0 E80CFFFFFF          <1> 	CALL	RECAL			; RECALIBRATE DRIVE
  5094 000025A5 724F                <1> 	JC	short DD_BAC		; ASSUME NO DRIVE PRESENT
  5095 000025A7 B530                <1> 	MOV	CH,TRK_SLAP		; SEEK TO TRACK 48
  5096 000025A9 E884FEFFFF          <1> 	CALL	SEEK
  5097 000025AE 7246                <1> 	JC	short DD_BAC		; ERROR NO DRIVE
  5098 000025B0 B50B                <1> 	MOV	CH,QUIET_SEEK+1		; SEEK TO TRACK 10
  5099                              <1> SK_GIN:
  5100 000025B2 FECD                <1> 	DEC	CH			; DECREMENT TO NEXT TRACK
  5101                              <1> 	;PUSH	CX			; SAVE TRACK
  5102                              <1> 	; 05/12/2021
  5103 000025B4 51                  <1> 	push	ecx
  5104 000025B5 E878FEFFFF          <1> 	CALL	SEEK
  5105 000025BA 723B                <1> 	JC	short POP_BAC		; POP AND RETURN
  5106 000025BC B8[F7250000]        <1> 	MOV	eAX, POP_BAC		; LOAD NEC OUTPUT ERROR ADDRESS
  5107 000025C1 50                  <1> 	PUSH	eAX
  5108 000025C2 B404                <1> 	MOV	AH,SENSE_DRV_ST		; SENSE DRIVE STATUS COMMAND BYTE
  5109 000025C4 E830FEFFFF          <1> 	CALL	NEC_OUTPUT		; OUTPUT TO NEC
  5110 000025C9 6689F8              <1> 	MOV	AX,DI			; AL = DRIVE
  5111 000025CC 88C4                <1> 	MOV	AH,AL			; AH = DRIVE
  5112 000025CE E826FEFFFF          <1> 	CALL	NEC_OUTPUT		; OUTPUT TO NEC
  5113 000025D3 E85BFFFFFF          <1> 	CALL	RESULTS			; GO GET STATUS
  5114 000025D8 58                  <1> 	POP	eAX			; THROW AWAY ERROR ADDRESS
  5115                              <1> 	;POP	CX			; RESTORE TRACK
  5116                              <1> 	; 05/12/2021
  5117 000025D9 59                  <1> 	pop	ecx
  5118 000025DA F605[3D700000]10    <1> 	TEST	byte [NEC_STATUS], HOME	; TRACK 0 ?
  5119 000025E1 74CF                <1> 	JZ	short SK_GIN		; GO TILL TRACK 0
  5120 000025E3 08ED                <1> 	OR	CH,CH			; IS HOME AT TRACK 0
  5121 000025E5 7408                <1> 	JZ	short IS_80		; MUST BE 80 TRACK DRIVE
  5122                              <1> 
  5123                              <1> ;	DRIVE IS A 360; SET DRIVE TO DETERMINED;
  5124                              <1> ;	SET MEDIA TO DETERMINED AT RATE 250.
  5125                              <1> 
  5126 000025E7 808F[49700000]94    <1> 	OR	byte [DSK_STATE+eDI], DRV_DET+MED_DET+RATE_250
  5127 000025EE C3                  <1> 	RETn				; ALL INFORMATION SET
  5128                              <1> IS_80:
  5129 000025EF 808F[49700000]01    <1> 	OR	byte [DSK_STATE+eDI], TRK_CAPA ; SETUP 80 TRACK CAPABILITY
  5130                              <1> DD_BAC:
  5131 000025F6 C3                  <1> 	RETn
  5132                              <1> POP_BAC:
  5133                              <1> 	;POP	CX			; THROW AWAY
  5134                              <1> 	; 05/12/2021
  5135 000025F7 59                  <1> 	pop	ecx
  5136 000025F8 C3                  <1> 	RETn
  5137                              <1> 
  5138                              <1> fdc_int:  
  5139                              <1> 	  ; 30/07/2015	
  5140                              <1> 	  ; 16/02/2015
  5141                              <1> ;int_0Eh: ; 11/12/2014
  5142                              <1> 
  5143                              <1> ;--- HARDWARE INT 0EH -- ( IRQ LEVEL  6 ) --------------------------------------
  5144                              <1> ; DISK_INT
  5145                              <1> ;	THIS ROUTINE HANDLES THE DISKETTE INTERRUPT.
  5146                              <1> ;
  5147                              <1> ; ON EXIT:	THE INTERRUPT FLAG IS SET IN @SEEK_STATUS.
  5148                              <1> ;-------------------------------------------------------------------------------
  5149                              <1> DISK_INT_1:
  5150                              <1> 
  5151                              <1> 	;PUSH	AX			; SAVE WORK REGISTER
  5152                              <1> 	; 05/12/2021
  5153 000025F9 50                  <1> 	push	eax
  5154 000025FA 1E                  <1> 	push	ds
  5155 000025FB 66B81000            <1> 	mov	ax, KDATA
  5156 000025FF 8ED8                <1> 	mov 	ds, ax
  5157 00002601 800D[39700000]80    <1>         OR      byte [SEEK_STATUS], INT_FLAG ; TURN ON INTERRUPT OCCURRED
  5158 00002608 B020                <1> 	MOV     AL,EOI                  ; END OF INTERRUPT MARKER
  5159 0000260A E620                <1> 	OUT	INTA00,AL		; INTERRUPT CONTROL PORT
  5160 0000260C 1F                  <1> 	pop	ds
  5161                              <1> 	;POP	AX			; RECOVER REGISTER
  5162                              <1> 	; 05/12/2021
  5163 0000260D 58                  <1> 	pop	eax
  5164 0000260E CF                  <1> 	IRETd				; RETURN FROM INTERRUPT
  5165                              <1> 
  5166                              <1> ;-------------------------------------------------------------------------------
  5167                              <1> ; DSKETTE_SETUP
  5168                              <1> ;	THIS ROUTINE DOES A PRELIMINARY CHECK TO SEE WHAT TYPE OF
  5169                              <1> ;	DISKETTE DRIVES ARE ATTACH TO THE SYSTEM.
  5170                              <1> ;-------------------------------------------------------------------------------
  5171                              <1> DSKETTE_SETUP:
  5172                              <1> 	;PUSH	AX			; SAVE REGISTERS
  5173                              <1> 	;PUSH	BX
  5174                              <1> 	;PUSH	CX
  5175 0000260F 52                  <1> 	PUSH	eDX
  5176                              <1> 	;PUSH	DI
  5177                              <1> 	;;PUSH	DS
  5178                              <1> 	; 14/12/2014
  5179                              <1> 	;mov	word [DISK_POINTER], MD_TBL6
  5180                              <1> 	;mov	[DISK_POINTER+2], cs
  5181                              <1> 	;
  5182                              <1> 	;OR	byte [RTC_WAIT_FLAG], 1	; NO RTC WAIT, FORCE USE OF LOOP
  5183 00002610 31FF                <1> 	XOR	eDI,eDI			; INITIALIZE DRIVE POINTER
  5184 00002612 66C705[49700000]00- <1> 	MOV	WORD [DSK_STATE],0	; INITIALIZE STATES
  5184 0000261A 00                  <1>
  5185 0000261B 8025[44700000]33    <1> 	AND	byte [LASTRATE],~(STRT_MSK+SEND_MSK) ; CLEAR START & SEND
  5186 00002622 800D[44700000]C0    <1> 	OR	byte [LASTRATE],SEND_MSK ; INITIALIZE SENT TO IMPOSSIBLE
  5187 00002629 C605[39700000]00    <1> 	MOV	byte [SEEK_STATUS],0	; INDICATE RECALIBRATE NEEDED
  5188 00002630 C605[3B700000]00    <1> 	MOV	byte [MOTOR_COUNT],0	; INITIALIZE MOTOR COUNT
  5189 00002637 C605[3A700000]00    <1> 	MOV	byte [MOTOR_STATUS],0	; INITIALIZE DRIVES TO OFF STATE
  5190 0000263E C605[3C700000]00    <1> 	MOV	byte [DSKETTE_STATUS],0	; NO ERRORS
  5191                              <1> 	;
  5192                              <1> 	; 28/02/2015
  5193                              <1> 	;mov	word [cfd], 100h 
  5194 00002645 E89DF2FFFF          <1> 	call	DSK_RESET
  5195 0000264A 5A                  <1> 	pop	edx
  5196 0000264B C3                  <1> 	retn
  5197                              <1> 
  5198                              <1> ;SUP0:
  5199                              <1> ;	CALL	DRIVE_DET		; DETERMINE DRIVE
  5200                              <1> ;	CALL	XLAT_OLD		; TRANSLATE STATE TO COMPATIBLE MODE
  5201                              <1> ;	; 02/01/2015
  5202                              <1> ;	;INC	DI			; POINT TO NEXT DRIVE
  5203                              <1> ;	;CMP	DI,MAX_DRV		; SEE IF DONE
  5204                              <1> ;	;JNZ	short SUP0		; REPEAT FOR EACH ORIVE
  5205                              <1> ;       cmp     byte [fd1_type], 0	
  5206                              <1> ;	jna	short sup1
  5207                              <1> ;	or	di, di
  5208                              <1> ;	jnz	short sup1
  5209                              <1> ;	inc	di
  5210                              <1> ;       jmp     short SUP0
  5211                              <1> ;sup1:
  5212                              <1> ;	MOV	byte [SEEK_STATUS],0	; FORCE RECALIBRATE
  5213                              <1> ;	;AND	byte [RTC_WAIT_FLAG],0FEH ; ALLOW FOR RTC WAIT
  5214                              <1> ;	CALL	SETUP_END		; VARIOUS CLEANUPS
  5215                              <1> ;	;;POP	DS			; RESTORE CALLERS REGISTERS
  5216                              <1> ;	;POP	DI
  5217                              <1> ;	POP	eDX
  5218                              <1> ;	;POP	CX
  5219                              <1> ;	;POP	BX
  5220                              <1> ;	;POP	AX
  5221                              <1> ;	RETn
  5222                              <1> 
  5223                              <1> ;//////////////////////////////////////////////////////
  5224                              <1> ;; END OF DISKETTE I/O ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  5225                              <1> ;
  5226                              <1> 
  5227                              <1> int13h: ; 21/02/2015
  5228 0000264C 9C                  <1> 	pushfd
  5229 0000264D 0E                  <1> 	push 	cs
  5230 0000264E E858000000          <1> 	call 	DISK_IO
  5231 00002653 C3                  <1> 	retn
  5232                              <1> 
  5233                              <1> ;;;;;; DISK I/O ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 21/02/2015 ;;;
  5234                              <1> ;/////////////////////////////////////////////////////////////////////
  5235                              <1> 
  5236                              <1> ; DISK I/O - Erdogan Tan (Retro UNIX 386 v1 project)
  5237                              <1> ; 23/02/2015
  5238                              <1> ; 21/02/2015 (unix386.s)
  5239                              <1> ; 22/12/2014 - 14/02/2015 (dsectrm2.s)
  5240                              <1> ;
  5241                              <1> ; Original Source Code:
  5242                              <1> ; DISK ----- 09/25/85 FIXED DISK BIOS
  5243                              <1> ; (IBM PC XT Model 286 System BIOS Source Code, 04-21-86)
  5244                              <1> ;
  5245                              <1> ; Modifications: by reference of AWARD BIOS 1999 (D1A0622) 
  5246                              <1> ;		 Source Code - ATORGS.ASM, AHDSK.ASM
  5247                              <1> ;
  5248                              <1> 
  5249                              <1> ;The wait for controller to be not busy is 10 seconds.
  5250                              <1> ;10,000,000 / 30 = 333,333.  333,333 decimal = 051615h
  5251                              <1> ;;WAIT_HDU_CTLR_BUSY_LO	equ	1615h		
  5252                              <1> ;;WAIT_HDU_CTLR_BUSY_HI	equ	  05h
  5253                              <1> WAIT_HDU_CTRL_BUSY_LH	equ	51615h	 ;21/02/2015		
  5254                              <1> 
  5255                              <1> ;The wait for controller to issue completion interrupt is 10 seconds.
  5256                              <1> ;10,000,000 / 30 = 333,333.  333,333 decimal = 051615h
  5257                              <1> ;;WAIT_HDU_INT_LO	equ	1615h
  5258                              <1> ;;WAIT_HDU_INT_HI	equ	  05h
  5259                              <1> WAIT_HDU_INT_LH		equ	51615h	; 21/02/2015
  5260                              <1> 
  5261                              <1> ;The wait for Data request on read and write longs is
  5262                              <1> ;2000 us. (?)
  5263                              <1> ;;WAIT_HDU_DRQ_LO	equ	1000	; 03E8h
  5264                              <1> ;;WAIT_HDU_DRQ_HI	equ	0
  5265                              <1> WAIT_HDU_DRQ_LH		equ	1000	; 21/02/2015
  5266                              <1> 
  5267                              <1> ; Port 61h (PORT_B)
  5268                              <1> SYS1		equ	61h	; PORT_B  (diskette.inc)
  5269                              <1> 
  5270                              <1> ; 23/12/2014
  5271                              <1> %define CMD_BLOCK       eBP-8  ; 21/02/2015
  5272                              <1> 
  5273                              <1> ;--- INT 13H -------------------------------------------------------------------
  5274                              <1> ;									       :
  5275                              <1> ; FIXED DISK I/O INTERFACE						       :
  5276                              <1> ;									       :
  5277                              <1> ;	THIS INTERFACE PROVIDES ACCESS TO 5 1/4" FIXED DISKS THROUGH           :
  5278                              <1> ;	THE IBM FIXED DISK CONTROLLER.					       :
  5279                              <1> ;									       :
  5280                              <1> ;	THE  BIOS  ROUTINES  ARE  MEANT  TO  BE  ACCESSED  THROUGH	       :
  5281                              <1> ;	SOFTWARE  INTERRUPTS  ONLY.    ANY  ADDRESSES  PRESENT	IN	       :
  5282                              <1> ;	THESE  LISTINGS  ARE  INCLUDED	 ONLY	FOR  COMPLETENESS,	       :
  5283                              <1> ;	NOT  FOR  REFERENCE.  APPLICATIONS   WHICH  REFERENCE  ANY	       :
  5284                              <1> ;	ABSOLUTE  ADDRESSES  WITHIN  THE  CODE	SEGMENTS  OF  BIOS	       :
  5285                              <1> ;	VIOLATE  THE  STRUCTURE  AND  DESIGN  OF  BIOS. 		       :
  5286                              <1> ;									       :
  5287                              <1> ;------------------------------------------------------------------------------:
  5288                              <1> ;									       :
  5289                              <1> ; INPUT  (AH)= HEX COMMAND VALUE					       :
  5290                              <1> ;									       :
  5291                              <1> ;	(AH)= 00H  RESET DISK (DL = 80H,81H) / DISKETTE 		       :
  5292                              <1> ;	(AH)= 01H  READ THE STATUS OF THE LAST DISK OPERATION INTO (AL)        :
  5293                              <1> ;		    NOTE: DL < 80H - DISKETTE				       :
  5294                              <1> ;			  DL > 80H - DISK				       :
  5295                              <1> ;	(AH)= 02H  READ THE DESIRED SECTORS INTO MEMORY 		       :
  5296                              <1> ;	(AH)= 03H  WRITE THE DESIRED SECTORS FROM MEMORY		       :
  5297                              <1> ;	(AH)= 04H  VERIFY THE DESIRED SECTORS				       :
  5298                              <1> ;	(AH)= 05H  FORMAT THE DESIRED TRACK				       :
  5299                              <1> ;	(AH)= 06H  UNUSED						       :
  5300                              <1> ;	(AH)= 07H  UNUSED						       :
  5301                              <1> ;	(AH)= 08H  RETURN THE CURRENT DRIVE PARAMETERS			       :
  5302                              <1> ;	(AH)= 09H  INITIALIZE DRIVE PAIR CHARACTERISTICS		       :
  5303                              <1> ;		    INTERRUPT 41 POINTS TO DATA BLOCK FOR DRIVE 0	       :
  5304                              <1> ;		    INTERRUPT 46 POINTS TO DATA BLOCK FOR DRIVE 1	       :
  5305                              <1> ;	(AH)= 0AH  READ LONG						       :
  5306                              <1> ;	(AH)= 0BH  WRITE LONG  (READ & WRITE LONG ENCOMPASS 512 + 4 BYTES ECC) :
  5307                              <1> ;	(AH)= 0CH  SEEK 						       :
  5308                              <1> ;	(AH)= 0DH  ALTERNATE DISK RESET (SEE DL)			       :
  5309                              <1> ;	(AH)= 0EH  UNUSED						       :
  5310                              <1> ;	(AH)= 0FH  UNUSED						       :
  5311                              <1> ;	(AH)= 10H  TEST DRIVE READY					       :
  5312                              <1> ;	(AH)= 11H  RECALIBRATE						       :
  5313                              <1> ;	(AH)= 12H  UNUSED						       :
  5314                              <1> ;	(AH)= 13H  UNUSED						       :
  5315                              <1> ;	(AH)= 14H  CONTROLLER INTERNAL DIAGNOSTIC			       :
  5316                              <1> ;	(AH)= 15H  READ DASD TYPE					       :
  5317                              <1> ;									       :
  5318                              <1> ;-------------------------------------------------------------------------------
  5319                              <1> ;									       :
  5320                              <1> ;	REGISTERS USED FOR FIXED DISK OPERATIONS			       :
  5321                              <1> ;									       :
  5322                              <1> ;		(DL)	-  DRIVE NUMBER     (80H-81H FOR DISK. VALUE CHECKED)  :
  5323                              <1> ;		(DH)	-  HEAD NUMBER	    (0-15 ALLOWED, NOT VALUE CHECKED)  :
  5324                              <1> ;		(CH)	-  CYLINDER NUMBER  (0-1023, NOT VALUE CHECKED)(SEE CL):
  5325                              <1> ;		(CL)	-  SECTOR NUMBER    (1-17, NOT VALUE CHECKED)	       :
  5326                              <1> ;									       :
  5327                              <1> ;			   NOTE: HIGH 2 BITS OF CYLINDER NUMBER ARE PLACED     :
  5328                              <1> ;				 IN THE HIGH 2 BITS OF THE CL REGISTER	       :
  5329                              <1> ;				 (10 BITS TOTAL)			       :
  5330                              <1> ;									       :
  5331                              <1> ;		(AL)	-  NUMBER OF SECTORS (MAXIMUM POSSIBLE RANGE 1-80H,    :
  5332                              <1> ;					      FOR READ/WRITE LONG 1-79H)       :
  5333                              <1> ;									       :
  5334                              <1> ;		(ES:BX) -  ADDRESS OF BUFFER FOR READS AND WRITES,	       :
  5335                              <1> ;			   (NOT REQUIRED FOR VERIFY)			       :
  5336                              <1> ;									       :
  5337                              <1> ;		FORMAT (AH=5) ES:BX POINTS TO A 512 BYTE BUFFER. THE FIRST     :
  5338                              <1> ;			   2*(SECTORS/TRACK) BYTES CONTAIN F,N FOR EACH SECTOR.:
  5339                              <1> ;			   F = 00H FOR A GOOD SECTOR			       :
  5340                              <1> ;			       80H FOR A BAD SECTOR			       :
  5341                              <1> ;			   N = SECTOR NUMBER				       :
  5342                              <1> ;			   FOR AN INTERLEAVE OF 2 AND 17 SECTORS/TRACK	       :
  5343                              <1> ;			   THE TABLE SHOULD BE: 			       :
  5344                              <1> ;									       :
  5345                              <1> ;		   DB	   00H,01H,00H,0AH,00H,02H,00H,0BH,00H,03H,00H,0CH     :
  5346                              <1> ;		   DB	   00H,04H,00H,0DH,00H,05H,00H,0EH,00H,06H,00H,0FH     :
  5347                              <1> ;		   DB	   00H,07H,00H,10H,00H,08H,00H,11H,00H,09H	       :
  5348                              <1> ;									       :
  5349                              <1> ;-------------------------------------------------------------------------------
  5350                              <1> 
  5351                              <1> ;-------------------------------------------------------------------------------
  5352                              <1> ; OUTPUT								       :
  5353                              <1> ;	AH = STATUS OF CURRENT OPERATION				       :
  5354                              <1> ;	     STATUS BITS ARE DEFINED IN THE EQUATES BELOW		       :
  5355                              <1> ;	CY = 0	SUCCESSFUL OPERATION (AH=0 ON RETURN)			       :
  5356                              <1> ;	CY = 1	FAILED OPERATION (AH HAS ERROR REASON)			       :
  5357                              <1> ;									       :
  5358                              <1> ;	NOTE:	ERROR 11H  INDICATES THAT THE DATA READ HAD A RECOVERABLE      :
  5359                              <1> ;		ERROR WHICH WAS CORRECTED BY THE ECC ALGORITHM.  THE DATA      :
  5360                              <1> ;		IS PROBABLY GOOD,   HOWEVER THE BIOS ROUTINE INDICATES AN      :
  5361                              <1> ;		ERROR TO ALLOW THE CONTROLLING PROGRAM A CHANCE TO DECIDE      :
  5362                              <1> ;		FOR ITSELF.  THE  ERROR  MAY  NOT  RECUR  IF  THE DATA IS      :
  5363                              <1> ;		REWRITTEN.						       :
  5364                              <1> ;									       :
  5365                              <1> ;	IF DRIVE PARAMETERS WERE REQUESTED (DL >= 80H), 		       :
  5366                              <1> ;	   INPUT:							       :
  5367                              <1> ;	     (DL) = DRIVE NUMBER					       :
  5368                              <1> ;	   OUTPUT:							       :
  5369                              <1> ;	     (DL) = NUMBER OF CONSECUTIVE ACKNOWLEDGING DRIVES ATTACHED (1-2)  :
  5370                              <1> ;		    (CONTROLLER CARD ZERO TALLY ONLY)			       :
  5371                              <1> ;	     (DH) = MAXIMUM USEABLE VALUE FOR HEAD NUMBER		       :
  5372                              <1> ;	     (CH) = MAXIMUM USEABLE VALUE FOR CYLINDER NUMBER		       :
  5373                              <1> ;	     (CL) = MAXIMUM USEABLE VALUE FOR SECTOR NUMBER		       :
  5374                              <1> ;		    AND CYLINDER NUMBER HIGH BITS			       :
  5375                              <1> ;									       :
  5376                              <1> ;	IF READ DASD TYPE WAS REQUESTED,				       :
  5377                              <1> ;									       :
  5378                              <1> ;	AH = 0 - NOT PRESENT						       :
  5379                              <1> ;	     1 - DISKETTE - NO CHANGE LINE AVAILABLE			       :
  5380                              <1> ;	     2 - DISKETTE - CHANGE LINE AVAILABLE			       :
  5381                              <1> ;	     3 - FIXED DISK						       :
  5382                              <1> ;									       :
  5383                              <1> ;	CX,DX = NUMBER OF 512 BYTE BLOCKS WHEN AH = 3			       :
  5384                              <1> ;									       :
  5385                              <1> ;	REGISTERS WILL BE PRESERVED EXCEPT WHEN THEY ARE USED TO RETURN        :
  5386                              <1> ;	INFORMATION.							       :
  5387                              <1> ;									       :
  5388                              <1> ;	NOTE: IF AN ERROR IS REPORTED BY THE DISK CODE, THE APPROPRIATE        :
  5389                              <1> ;		ACTION IS TO RESET THE DISK, THEN RETRY THE OPERATION.	       :
  5390                              <1> ;									       :
  5391                              <1> ;-------------------------------------------------------------------------------
  5392                              <1> 
  5393                              <1> SENSE_FAIL	EQU	0FFH		; NOT IMPLEMENTED
  5394                              <1> NO_ERR		EQU	0E0H		; STATUS ERROR/ERROR REGISTER=0
  5395                              <1> WRITE_FAULT	EQU	0CCH		; WRITE FAULT ON SELECTED DRIVE
  5396                              <1> UNDEF_ERR	EQU	0BBH		; UNDEFINED ERROR OCCURRED
  5397                              <1> NOT_RDY 	EQU	0AAH		; DRIVE NOT READY
  5398                              <1> TIME_OUT	EQU	80H		; ATTACHMENT FAILED TO RESPOND
  5399                              <1> BAD_SEEK	EQU	40H		; SEEK OPERATION FAILED
  5400                              <1> BAD_CNTLR	EQU	20H		; CONTROLLER HAS FAILED
  5401                              <1> DATA_CORRECTED	EQU	11H		; ECC CORRECTED DATA ERROR
  5402                              <1> BAD_ECC 	EQU	10H		; BAD ECC ON DISK READ
  5403                              <1> BAD_TRACK	EQU	0BH		; NOT IMPLEMENTED
  5404                              <1> BAD_SECTOR	EQU	0AH		; BAD SECTOR FLAG DETECTED
  5405                              <1> ;DMA_BOUNDARY	EQU	09H		; DATA EXTENDS TOO FAR
  5406                              <1> INIT_FAIL	EQU	07H		; DRIVE PARAMETER ACTIVITY FAILED
  5407                              <1> BAD_RESET	EQU	05H		; RESET FAILED
  5408                              <1> ;RECORD_NOT_FND	EQU	04H		; REQUESTED SECTOR NOT FOUND
  5409                              <1> ;BAD_ADDR_MARK	EQU	02H		; ADDRESS MARK NOT FOUND
  5410                              <1> ;BAD_CMD 	EQU	01H		; BAD COMMAND PASSED TO DISK I/O
  5411                              <1> 
  5412                              <1> ;--------------------------------------------------------
  5413                              <1> ;							:
  5414                              <1> ; FIXED DISK PARAMETER TABLE				:
  5415                              <1> ;  -  THE TABLE IS COMPOSED OF A BLOCK DEFINED AS:	:
  5416                              <1> ;							:
  5417                              <1> ;  +0	(1 WORD) - MAXIMUM NUMBER OF CYLINDERS		:
  5418                              <1> ;  +2	(1 BYTE) - MAXIMUM NUMBER OF HEADS		:
  5419                              <1> ;  +3	(1 WORD) - NOT USED/SEE PC-XT			:
  5420                              <1> ;  +5	(1 WORD) - STARTING WRITE PRECOMPENSATION CYL	:
  5421                              <1> ;  +7	(1 BYTE) - MAXIMUM ECC DATA BURST LENGTH	:
  5422                              <1> ;  +8	(1 BYTE) - CONTROL BYTE 			:
  5423                              <1> ;		   BIT	  7 DISABLE RETRIES -OR-	:
  5424                              <1> ;		   BIT	  6 DISABLE RETRIES		:
  5425                              <1> ;		   BIT	  3 MORE THAN 8 HEADS		:
  5426                              <1> ;  +9	(3 BYTES)- NOT USED/SEE PC-XT			:
  5427                              <1> ; +12	(1 WORD) - LANDING ZONE 			:
  5428                              <1> ; +14	(1 BYTE) - NUMBER OF SECTORS/TRACK		:
  5429                              <1> ; +15	(1 BYTE) - RESERVED FOR FUTURE USE		:
  5430                              <1> ;							:
  5431                              <1> ;	 - TO DYNAMICALLY DEFINE A SET OF PARAMETERS	:
  5432                              <1> ;	   BUILD A TABLE FOR UP TO 15 TYPES AND PLACE	:
  5433                              <1> ;	   THE CORRESPONDING VECTOR INTO INTERRUPT 41	:
  5434                              <1> ;	   FOR DRIVE 0 AND INTERRUPT 46 FOR DRIVE 1.	:
  5435                              <1> ;							:
  5436                              <1> ;--------------------------------------------------------
  5437                              <1> 
  5438                              <1> ;--------------------------------------------------------
  5439                              <1> ;							:
  5440                              <1> ; HARDWARE SPECIFIC VALUES				:
  5441                              <1> ;							:
  5442                              <1> ;  -  CONTROLLER I/O PORT				:
  5443                              <1> ;							:
  5444                              <1> ;     > WHEN READ FROM: 				:
  5445                              <1> ;	HF_PORT+0 - READ DATA (FROM CONTROLLER TO CPU)	:
  5446                              <1> ;	HF_PORT+1 - GET ERROR REGISTER			:
  5447                              <1> ;	HF_PORT+2 - GET SECTOR COUNT			:
  5448                              <1> ;	HF_PORT+3 - GET SECTOR NUMBER			:
  5449                              <1> ;	HF_PORT+4 - GET CYLINDER LOW			:
  5450                              <1> ;	HF_PORT+5 - GET CYLINDER HIGH (2 BITS)		:
  5451                              <1> ;	HF_PORT+6 - GET SIZE/DRIVE/HEAD 		:
  5452                              <1> ;	HF_PORT+7 - GET STATUS REGISTER 		:
  5453                              <1> ;							:
  5454                              <1> ;     > WHEN WRITTEN TO:				:
  5455                              <1> ;	HF_PORT+0 - WRITE DATA (FROM CPU TO CONTROLLER) :
  5456                              <1> ;	HF_PORT+1 - SET PRECOMPENSATION CYLINDER	:
  5457                              <1> ;	HF_PORT+2 - SET SECTOR COUNT			:
  5458                              <1> ;	HF_PORT+3 - SET SECTOR NUMBER			:
  5459                              <1> ;	HF_PORT+4 - SET CYLINDER LOW			:
  5460                              <1> ;	HF_PORT+5 - SET CYLINDER HIGH (2 BITS)		:
  5461                              <1> ;	HF_PORT+6 - SET SIZE/DRIVE/HEAD 		:
  5462                              <1> ;	HF_PORT+7 - SET COMMAND REGISTER		:
  5463                              <1> ;							:
  5464                              <1> ;--------------------------------------------------------
  5465                              <1> 
  5466                              <1> ;HF_PORT 	EQU	01F0H	; DISK PORT
  5467                              <1> ;HF1_PORT	equ	0170h	
  5468                              <1> ;HF_REG_PORT	EQU	03F6H
  5469                              <1> ;HF1_REG_PORT	equ	0376h
  5470                              <1> 
  5471                              <1> HDC1_BASEPORT	equ	1F0h
  5472                              <1> HDC2_BASEPORT	equ	170h		
  5473                              <1> 
  5474                              <1> align 2
  5475                              <1> 
  5476                              <1> ;-----		STATUS REGISTER
  5477                              <1> 
  5478                              <1> ST_ERROR	EQU	00000001B	;
  5479                              <1> ST_INDEX	EQU	00000010B	;
  5480                              <1> ST_CORRCTD	EQU	00000100B	; ECC CORRECTION SUCCESSFUL
  5481                              <1> ST_DRQ		EQU	00001000B	;
  5482                              <1> ST_SEEK_COMPL	EQU	00010000B	; SEEK COMPLETE
  5483                              <1> ST_WRT_FLT	EQU	00100000B	; WRITE FAULT
  5484                              <1> ST_READY	EQU	01000000B	;
  5485                              <1> ST_BUSY 	EQU	10000000B	;
  5486                              <1> 
  5487                              <1> ;-----		ERROR REGISTER
  5488                              <1> 
  5489                              <1> ERR_DAM 	EQU	00000001B	; DATA ADDRESS MARK NOT FOUND
  5490                              <1> ERR_TRK_0	EQU	00000010B	; TRACK 0 NOT FOUND ON RECAL
  5491                              <1> ERR_ABORT	EQU	00000100B	; ABORTED COMMAND
  5492                              <1> ;		EQU	00001000B	; NOT USED
  5493                              <1> ERR_ID		EQU	00010000B	; ID NOT FOUND
  5494                              <1> ;		EQU	00100000B	; NOT USED
  5495                              <1> ERR_DATA_ECC	EQU	01000000B
  5496                              <1> ERR_BAD_BLOCK	EQU	10000000B
  5497                              <1> 
  5498                              <1> 
  5499                              <1> RECAL_CMD	EQU	00010000B	; DRIVE RECAL	(10H)
  5500                              <1> READ_CMD	EQU	00100000B	;	READ	(20H)
  5501                              <1> WRITE_CMD	EQU	00110000B	;	WRITE	(30H)
  5502                              <1> VERIFY_CMD	EQU	01000000B	;	VERIFY	(40H)
  5503                              <1> FMTTRK_CMD	EQU	01010000B	; FORMAT TRACK	(50H)
  5504                              <1> INIT_CMD	EQU	01100000B	;   INITIALIZE	(60H)
  5505                              <1> SEEK_CMD	EQU	01110000B	;	SEEK	(70H)
  5506                              <1> DIAG_CMD	EQU	10010000B	; DIAGNOSTIC	(90H)
  5507                              <1> SET_PARM_CMD	EQU	10010001B	; DRIVE PARMS	(91H)
  5508                              <1> NO_RETRIES	EQU	00000001B	; CHD MODIFIER	(01H)
  5509                              <1> ECC_MODE	EQU	00000010B	; CMD MODIFIER	(02H)
  5510                              <1> BUFFER_MODE	EQU	00001000B	; CMD MODIFIER	(08H)
  5511                              <1> 
  5512                              <1> ;MAX_FILE	EQU	2
  5513                              <1> ;S_MAX_FILE	EQU	2
  5514                              <1> MAX_FILE	equ	4		; 22/12/2014
  5515                              <1> S_MAX_FILE	equ	4		; 22/12/2014
  5516                              <1> 
  5517                              <1> DELAY_1 	EQU	25H		; DELAY FOR OPERATION COMPLETE
  5518                              <1> DELAY_2 	EQU	0600H		; DELAY FOR READY
  5519                              <1> DELAY_3 	EQU	0100H		; DELAY FOR DATA REQUEST
  5520                              <1> 
  5521                              <1> HF_FAIL 	EQU	08H		; CMOS FLAG IN BYTE 0EH
  5522                              <1> 
  5523                              <1> ;-----		COMMAND BLOCK REFERENCE
  5524                              <1> 
  5525                              <1> ;CMD_BLOCK      EQU     BP-8            ; @CMD_BLOCK REFERENCES BLOCK HEAD IN SS
  5526                              <1> 					;  (BP) POINTS TO COMMAND BLOCK TAIL
  5527                              <1> 					;	AS DEFINED BY THE "ENTER" PARMS
  5528                              <1> ; 19/12/2014
  5529                              <1> ORG_VECTOR	equ	4*13h		; INT 13h vector
  5530                              <1> DISK_VECTOR	equ	4*40h		; INT 40h vector (for floppy disks)
  5531                              <1> ;HDISK_INT	equ	4*76h		; Primary HDC - Hardware interrupt (IRQ14)
  5532                              <1> ;HDISK_INT1	equ	4*76h		; Primary HDC - Hardware interrupt (IRQ14)
  5533                              <1> ;HDISK_INT2	equ	4*77h		; Secondary HDC - Hardware interrupt (IRQ15)
  5534                              <1> ;HF_TBL_VEC	equ	4*41h		; Pointer to 1st fixed disk parameter table
  5535                              <1> ;HF1_TBL_VEC	equ	4*46h		; Pointer to 2nd fixed disk parameter table
  5536                              <1> 
  5537                              <1> align 2
  5538                              <1> 
  5539                              <1> ;----------------------------------------------------------------
  5540                              <1> ; FIXED DISK I/O SETUP						:
  5541                              <1> ;								:
  5542                              <1> ;  -  ESTABLISH TRANSFER VECTORS FOR THE FIXED DISK		:
  5543                              <1> ;  -  PERFORM POWER ON DIAGNOSTICS				:
  5544                              <1> ;     SHOULD AN ERROR OCCUR A "1701" MESSAGE IS DISPLAYED       :
  5545                              <1> ;								:
  5546                              <1> ;----------------------------------------------------------------
  5547                              <1> 
  5548                              <1> DISK_SETUP:
  5549                              <1> 	;CLI
  5550                              <1> 	;;MOV	AX,ABS0 			; GET ABSOLUTE SEGMENT
  5551                              <1> 	;xor	ax,ax
  5552                              <1> 	;MOV	DS,AX				; SET SEGMENT REGISTER
  5553                              <1> 	;MOV	AX, [ORG_VECTOR] 		; GET DISKETTE VECTOR
  5554                              <1> 	;MOV	[DISK_VECTOR],AX		;  INTO INT 40H
  5555                              <1> 	;MOV	AX, [ORG_VECTOR+2]
  5556                              <1> 	;MOV	[DISK_VECTOR+2],AX
  5557                              <1> 	;MOV	word [ORG_VECTOR],DISK_IO	; FIXED DISK HANDLER
  5558                              <1> 	;MOV	[ORG_VECTOR+2],CS
  5559                              <1> 	; 1st controller (primary master, slave)   - IRQ 14
  5560                              <1> 	;;MOV	word [HDISK_INT],HD_INT		; FIXED DISK INTERRUPT
  5561                              <1> 	;mov	word [HDISK_INT1],HD_INT	;
  5562                              <1> 	;;MOV	[HDISK_INT+2],CS
  5563                              <1> 	;mov	[HDISK_INT1+2],CS
  5564                              <1> 	; 2nd controller (secondary master, slave) - IRQ 15
  5565                              <1> 	;mov	word [HDISK_INT2],HD1_INT	;
  5566                              <1> 	;mov	[HDISK_INT2+2],CS
  5567                              <1> 	;
  5568                              <1> 	;;MOV	word [HF_TBL_VEC],HD0_DPT	; PARM TABLE DRIVE 80
  5569                              <1> 	;;MOV	word [HF_TBL_VEC+2],DPT_SEGM
  5570                              <1> 	;;MOV	word [HF1_TBL_VEC],HD1_DPT	; PARM TABLE DRIVE 81
  5571                              <1> 	;;MOV	word [HF1_TBL_VEC+2],DPT_SEGM
  5572                              <1> 	;push	cs
  5573                              <1> 	;pop	ds
  5574                              <1> 	;mov	word [HDPM_TBL_VEC],HD0_DPT	; PARM TABLE DRIVE 80h
  5575                              <1> 	;mov	word [HDPM_TBL_VEC+2],DPT_SEGM
  5576 00002654 C705[54700000]0000- <1> 	mov 	dword [HDPM_TBL_VEC], (DPT_SEGM*16)+HD0_DPT
  5576 0000265C 0900                <1>
  5577                              <1> 	;mov	word [HDPS_TBL_VEC],HD1_DPT	; PARM TABLE DRIVE 81h
  5578                              <1> 	;mov	word [HDPS_TBL_VEC+2],DPT_SEGM
  5579 0000265E C705[58700000]2000- <1> 	mov 	dword [HDPS_TBL_VEC], (DPT_SEGM*16)+HD1_DPT
  5579 00002666 0900                <1>
  5580                              <1> 	;mov	word [HDSM_TBL_VEC],HD2_DPT	; PARM TABLE DRIVE 82h
  5581                              <1> 	;mov	word [HDSM_TBL_VEC+2],DPT_SEGM
  5582 00002668 C705[5C700000]4000- <1> 	mov 	dword [HDSM_TBL_VEC], (DPT_SEGM*16)+HD2_DPT
  5582 00002670 0900                <1>
  5583                              <1> 	;mov	word [HDSS_TBL_VEC],HD3_DPT	; PARM TABLE DRIVE 83h
  5584                              <1> 	;mov	word [HDSS_TBL_VEC+2],DPT_SEGM
  5585 00002672 C705[60700000]6000- <1> 	mov 	dword [HDSS_TBL_VEC], (DPT_SEGM*16)+HD3_DPT
  5585 0000267A 0900                <1>
  5586                              <1> 	;
  5587                              <1> 	;;IN	AL,INTB01		; TURN ON SECOND INTERRUPT CHIP
  5588                              <1> 	;;;AND	AL,0BFH
  5589                              <1> 	;;and	al, 3Fh			; enable IRQ 14 and IRQ 15
  5590                              <1> 	;;;JMP	$+2
  5591                              <1> 	;;IODELAY
  5592                              <1> 	;;OUT	INTB01,AL
  5593                              <1> 	;;IODELAY
  5594                              <1> 	;;IN	AL,INTA01		; LET INTERRUPTS PASS THRU TO
  5595                              <1> 	;;AND	AL,0FBH 		;  SECOND CHIP
  5596                              <1> 	;;;JMP	$+2
  5597                              <1> 	;;IODELAY
  5598                              <1> 	;;OUT	INTA01,AL
  5599                              <1> 	;
  5600                              <1> 	;STI
  5601                              <1> 	;;PUSH	DS			; MOVE ABS0 POINTER TO
  5602                              <1> 	;;POP	ES			; EXTRA SEGMENT POINTER
  5603                              <1> 	;;;CALL	DDS			; ESTABLISH DATA SEGMENT
  5604                              <1> 	;;MOV	byte [DISK_STATUS1],0 	; RESET THE STATUS INDICATOR
  5605                              <1> 	;;MOV	byte [HF_NUM],0		; ZERO NUMBER OF FIXED DISKS
  5606                              <1> 	;;MOV	byte [CONTROL_BYTE],0
  5607                              <1> 	;;MOV	byte [PORT_OFF],0	; ZERO CARD OFFSET
  5608                              <1> 	; 20/12/2014 - private code by Erdogan Tan
  5609                              <1> 		      ; (out of original PC-AT, PC-XT BIOS code)
  5610                              <1> 	;mov	si, hd0_type
  5611 0000267C BE[B06A0000]        <1> 	mov	esi, hd0_type
  5612                              <1> 	;mov	cx, 4
  5613 00002681 B904000000          <1> 	mov	ecx, 4
  5614                              <1> hde_l:
  5615 00002686 AC                  <1> 	lodsb
  5616 00002687 3C80                <1> 	cmp	al, 80h			; 8?h = existing
  5617 00002689 7206                <1> 	jb	short _L4
  5618 0000268B FE05[50700000]      <1> 	inc	byte [HF_NUM]		; + 1 hard (fixed) disk drives
  5619                              <1> _L4: ; 26/02/2015
  5620 00002691 E2F3                <1> 	loop	hde_l	
  5621                              <1> ;_L4:					; 0 <= [HF_NUM] =< 4
  5622                              <1> ;L4:
  5623                              <1> 	; 
  5624                              <1> 	;; 31/12/2014 - cancel controller diagnostics here
  5625                              <1> 	;;;mov 	cx, 3  ; 26/12/2014 (Award BIOS 1999)
  5626                              <1> 	;;mov 	cl, 3
  5627                              <1> 	;;
  5628                              <1> 	;;MOV	DL,80H			; CHECK THE CONTROLLER
  5629                              <1> ;;hdc_dl:
  5630                              <1> 	;;MOV	AH,14H			; USE CONTROLLER DIAGNOSTIC COMMAND
  5631                              <1> 	;;INT	13H			; CALL BIOS WITH DIAGNOSTIC COMMAND
  5632                              <1> 	;;;JC	short CTL_ERRX		; DISPLAY ERROR MESSAGE IF BAD RETURN
  5633                              <1> 	;;;jc	short POD_DONE ;22/12/2014
  5634                              <1> 	;;jnc	short hdc_reset0
  5635                              <1> 	;;loop	hdc_dl
  5636                              <1> 	;;; 27/12/2014
  5637                              <1> 	;;stc
  5638                              <1> 	;;retn
  5639                              <1> 	;
  5640                              <1> ;;hdc_reset0:
  5641                              <1> 	; 18/01/2015
  5642 00002693 8A0D[50700000]      <1> 	mov	cl, [HF_NUM]
  5643 00002699 20C9                <1> 	and	cl, cl
  5644 0000269B 740D                <1> 	jz	short POD_DONE
  5645                              <1> 	;
  5646 0000269D B27F                <1> 	mov	dl, 7Fh
  5647                              <1> hdc_reset1:
  5648 0000269F FEC2                <1> 	inc	dl
  5649                              <1> 	;; 31/12/2015
  5650                              <1> 	;;push	dx
  5651                              <1> 	;;push	cx
  5652                              <1> 	;;push	ds
  5653                              <1> 	;;sub	ax, ax
  5654                              <1> 	;;mov	ds, ax
  5655                              <1> 	;;MOV	AX, [TIMER_LOW]		; GET START TIMER COUNTS
  5656                              <1> 	;;pop	ds
  5657                              <1> 	;;MOV	BX,AX
  5658                              <1> 	;;ADD	AX,6*182		; 60 SECONDS* 18.2
  5659                              <1> 	;;MOV	CX,AX
  5660                              <1> 	;;mov	word [wait_count], 0	; 22/12/2014 (reset wait counter)
  5661                              <1> 	;;
  5662                              <1> 	;; 31/12/2014 - cancel HD_RESET_1
  5663                              <1> 	;;CALL	HD_RESET_1		; SET UP DRIVE 0, (1,2,3)
  5664                              <1> 	;;pop	cx
  5665                              <1> 	;;pop	dx
  5666                              <1> 	;;
  5667                              <1> 	; 18/01/2015
  5668 000026A1 B40D                <1> 	mov	ah, 0Dh ; ALTERNATE RESET
  5669                              <1> 	;int	13h
  5670 000026A3 E8A4FFFFFF          <1> 	call	int13h
  5671 000026A8 E2F5                <1> 	loop	hdc_reset1
  5672                              <1> POD_DONE:
  5673 000026AA C3                  <1> 	RETn
  5674                              <1> 
  5675                              <1> ;;-----	POD_ERROR
  5676                              <1> 
  5677                              <1> ;;CTL_ERRX:
  5678                              <1> ;	;MOV	SI,OFFSET F1782 	; CONTROLLER ERROR
  5679                              <1> ;	;CALL	SET_FAIL		; DO NOT IPL FROM DISK
  5680                              <1> ;	;CALL	E_MSG			; DISPLAY ERROR AND SET (BP) ERROR FLAG
  5681                              <1> ;	;JMP	short POD_DONE
  5682                              <1> 
  5683                              <1> ;;HD_RESET_1:
  5684                              <1> ;;	;PUSH	BX			; SAVE TIMER LIMITS
  5685                              <1> ;;	;PUSH	CX
  5686                              <1> ;;RES_1: MOV	AH,09H			; SET DRIVE PARAMETERS
  5687                              <1> ;;	INT	13H
  5688                              <1> ;;	JC	short RES_2
  5689                              <1> ;;	MOV	AH,11H			; RECALIBRATE DRIVE
  5690                              <1> ;;	INT	13H
  5691                              <1> ;;	JNC	short RES_CK		; DRIVE OK
  5692                              <1> ;;RES_2: ;CALL	POD_TCHK		; CHECK TIME OUT
  5693                              <1> ;;	cmp	word [wait_count], 6*182 ; waiting time (in timer ticks)
  5694                              <1> ;;					; (30 seconds)		
  5695                              <1> ;;	;cmc
  5696                              <1> ;;	;JNC	short RES_1
  5697                              <1> ;;	jb	short RES_1
  5698                              <1> ;;;RES_FL: ;MOV	SI,OFFSET F1781 	; INDICATE DISK 1 FAILURE;
  5699                              <1> ;;	;TEST	DL,1
  5700                              <1> ;;	;JNZ	RES_E1
  5701                              <1> ;;	;MOV	SI,OFFSET F1780 	; INDICATE DISK 0 FAILURE
  5702                              <1> ;;	;CALL	SET_FAIL		; DO NOT TRY TO IPL DISK 0
  5703                              <1> ;;	;JMP	SHORT RES_E1
  5704                              <1> ;;RES_ER: ; 22/12/2014
  5705                              <1> ;;RES_OK:
  5706                              <1> ;;	;POP	CX			; RESTORE TIMER LIMITS
  5707                              <1> ;;	;POP	BX
  5708                              <1> ;;	RETn
  5709                              <1> ;;
  5710                              <1> ;;RES_RS: MOV	AH,00H			; RESET THE DRIVE
  5711                              <1> ;;	INT	13H
  5712                              <1> ;;RES_CK: MOV	AH,08H			; GET MAX CYLINDER,HEAD,SECTOR
  5713                              <1> ;;	MOV	BL,DL			; SAVE DRIVE CODE
  5714                              <1> ;;	INT	13H
  5715                              <1> ;;	JC	short RES_ER
  5716                              <1> ;;	MOV	[NEC_STATUS],CX 	; SAVE MAX CYLINDER, SECTOR
  5717                              <1> ;;	MOV	DL,BL			; RESTORE DRIVE CODE
  5718                              <1> ;;RES_3: MOV	AX,0401H		; VERIFY THE LAST SECTOR
  5719                              <1> ;;	INT	13H
  5720                              <1> ;;	JNC	short RES_OK		; VERIFY OK
  5721                              <1> ;;	CMP	AH,BAD_SECTOR		; OK ALSO IF JUST ID READ
  5722                              <1> ;;	JE	short RES_OK
  5723                              <1> ;;	CMP	AH,DATA_CORRECTED
  5724                              <1> ;;	JE	short RES_OK
  5725                              <1> ;;	CMP	AH,BAD_ECC
  5726                              <1> ;;	JE	short RES_OK
  5727                              <1> ;;	;CALL	POD_TCHK		; CHECK FOR TIME OUT
  5728                              <1> ;;	cmp	word [wait_count], 6*182 ; waiting time (in timer ticks)
  5729                              <1> ;;					; (60 seconds)		
  5730                              <1> ;;	cmc
  5731                              <1> ;;	JC	short RES_ER		; FAILED
  5732                              <1> ;;	MOV	CX,[NEC_STATUS] 	; GET SECTOR ADDRESS, AND CYLINDER
  5733                              <1> ;;	MOV	AL,CL			; SEPARATE OUT SECTOR NUMBER
  5734                              <1> ;;	AND	AL,3FH
  5735                              <1> ;;	DEC	AL			; TRY PREVIOUS ONE
  5736                              <1> ;;	JZ	short RES_RS		; WE'VE TRIED ALL SECTORS ON TRACK
  5737                              <1> ;;	AND	CL,0C0H 		; KEEP CYLINDER BITS
  5738                              <1> ;;	OR	CL,AL			; MERGE SECTOR WITH CYLINDER BITS
  5739                              <1> ;;	MOV	[NEC_STATUS],CX 	; SAVE CYLINDER, NEW SECTOR NUMBER
  5740                              <1> ;;	JMP	short RES_3		; TRY AGAIN
  5741                              <1> ;;;RES_ER: MOV	SI,OFFSET F1791 	; INDICATE DISK 1 ERROR
  5742                              <1> ;;	;TEST	DL,1
  5743                              <1> ;;	;JNZ	short RES_E1
  5744                              <1> ;;	;MOV	SI,OFFSET F1790 	; INDICATE DISK 0 ERROR
  5745                              <1> ;;;RES_E1:
  5746                              <1> ;;	;CALL	E_MSG			; DISPLAY ERROR AND SET (BP) ERROR FLAG
  5747                              <1> ;;;RES_OK:
  5748                              <1> ;;	;POP	CX			; RESTORE TIMER LIMITS
  5749                              <1> ;;	;POP	BX
  5750                              <1> ;;	;RETn
  5751                              <1> ;
  5752                              <1> ;;SET_FAIL:
  5753                              <1> ;	;MOV	AX,X*(CMOS_DIAG+NMI)	; GET CMOS ERROR BYTE
  5754                              <1> ;	;CALL	CMOS_READ
  5755                              <1> ;	;OR	AL,HF_FAIL		; SET DO NOT IPL FROM DISK FLAG
  5756                              <1> ;	;XCHG	AH,AL			; SAVE IT
  5757                              <1> ;	;CALL	CMOS_WRITE		; PUT IT OUT
  5758                              <1> ;	;RETn
  5759                              <1> ;
  5760                              <1> ;;POD_TCHK:				; CHECK FOR 30 SECOND TIME OUT
  5761                              <1> ;	;POP	AX			; SAVE RETURN
  5762                              <1> ;	;POP	CX			; GET TIME OUT LIMITS
  5763                              <1> ;	;POP	BX
  5764                              <1> ;	;PUSH	BX			; AND SAVE THEM AGAIN
  5765                              <1> ;	;PUSH	CX
  5766                              <1> ;	;PUSH	AX
  5767                              <1> ;	;push	ds
  5768                              <1> ;	;xor	ax, ax
  5769                              <1> ;	;mov	ds, ax			; RESTORE RETURN
  5770                              <1> ;	;MOV	AX, [TIMER_LOW]		; AX = CURRENT TIME
  5771                              <1> ;	;				; BX = START TIME
  5772                              <1> ;	;				; CX = END TIME
  5773                              <1> ;	;pop	ds
  5774                              <1> ;	;CMP	BX,CX
  5775                              <1> ;	;JB	short TCHK1		; START < END
  5776                              <1> ;	;CMP	BX,AX
  5777                              <1> ;	;JB	short TCHKG		; END < START < CURRENT
  5778                              <1> ;	;JMP	SHORT TCHK2		; END, CURRENT < START
  5779                              <1> ;;TCHK1: CMP	AX,BX
  5780                              <1> ;;	JB	short TCHKNG		; CURRENT < START < END
  5781                              <1> ;;TCHK2: CMP	AX,CX
  5782                              <1> ;;	JB	short TCHKG		; START < CURRENT < END
  5783                              <1> ;;					; OR CURRENT < END < START
  5784                              <1> ;;TCHKNG: STC				; CARRY SET INDICATES TIME OUT
  5785                              <1> ;;	RETn
  5786                              <1> ;;TCHKG: CLC				; INDICATE STILL TIME
  5787                              <1> ;;	RETn
  5788                              <1> ;;
  5789                              <1> ;;int_13h:
  5790                              <1> 
  5791                              <1> ;----------------------------------------
  5792                              <1> ;	FIXED DISK BIOS ENTRY POINT	:
  5793                              <1> ;----------------------------------------
  5794                              <1> 
  5795                              <1> DISK_IO:
  5796 000026AB 80FA80              <1> 	CMP	DL,80H			; TEST FOR FIXED DISK DRIVE
  5797                              <1> 	;JAE	short A1		; YES, HANDLE HERE
  5798                              <1> 	;;;INT	40H			; DISKETTE HANDLER
  5799                              <1> 	;;call	int40h
  5800                              <1> 	;jb	DISKETTE_IO_1
  5801                              <1> 	; 05/12/2021
  5802 000026AE 7305                <1> 	jnb	short A1
  5803 000026B0 E960F1FFFF          <1> 	jmp	DISKETTE_IO_1
  5804                              <1> ;RET_2:
  5805                              <1> 	;RETf	2			; BACK TO CALLER
  5806                              <1> ;	retf	4
  5807                              <1> A1:
  5808 000026B5 FB                  <1> 	STI				; ENABLE INTERRUPTS
  5809                              <1> 	;; 04/01/2015
  5810                              <1> 	;;OR	AH,AH
  5811                              <1> 	;;JNZ	short A2
  5812                              <1> 	;;INT	40H			; RESET NEC WHEN AH=0
  5813                              <1> 	;;SUB	AH,AH
  5814 000026B6 80FA83              <1> 	CMP	DL,(80H + S_MAX_FILE - 1)
  5815 000026B9 772E                <1> 	JA	short RET_2
  5816                              <1> 	; 18/01/2015
  5817 000026BB 08E4                <1> 	or	ah,ah
  5818 000026BD 742D                <1> 	jz	short A4
  5819 000026BF 80FC0D              <1> 	cmp	ah, 0Dh	; Alternate reset
  5820 000026C2 7504                <1> 	jne	short A2
  5821 000026C4 28E4                <1> 	sub	ah,ah	; Reset
  5822 000026C6 EB24                <1> 	jmp	short A4
  5823                              <1> A2:
  5824 000026C8 80FC08              <1> 	CMP	AH,08H			; GET PARAMETERS IS A SPECIAL CASE
  5825                              <1> 	;JNZ	short A3
  5826                              <1>         ;JMP    GET_PARM_N
  5827                              <1> 	;je	GET_PARM_N
  5828                              <1> 	; 05/12/2021
  5829 000026CB 7505                <1> 	jne	short A3
  5830 000026CD E90D030000          <1> 	jmp	GET_PARM_N
  5831                              <1> A3:	
  5832 000026D2 80FC15              <1> 	CMP	AH,15H			; READ DASD TYPE IS ALSO
  5833                              <1> 	;JNZ	short A4
  5834                              <1>         ;JMP    READ_DASD_TYPE
  5835                              <1> 	;je	READ_DASD_TYPE
  5836                              <1> 	; 05/12/2021
  5837 000026D5 7505                <1> 	jne	short A3_A4
  5838 000026D7 E9BA020000          <1> 	jmp	READ_DASD_TYPE
  5839                              <1> A3_A4:
  5840                              <1> 	; 02/02/2015
  5841 000026DC 80FC1D              <1> 	cmp	ah, 1Dh			; (Temporary for Retro UNIX 386 v1)
  5842                              <1> 	; 12/01/2015
  5843 000026DF F5                  <1> 	cmc
  5844 000026E0 730A                <1> 	jnc	short A4
  5845                              <1> 	; 30/01/2015
  5846                              <1> 	;mov	byte [CS:DISK_STATUS1],BAD_CMD  ; COMMAND ERROR
  5847 000026E2 C605[4F700000]01    <1>         mov     byte [DISK_STATUS1], BAD_CMD
  5848                              <1> 	;jmp	short RET_2
  5849                              <1> RET_2:
  5850 000026E9 CA0400              <1> 	retf	4
  5851                              <1> A4:					; SAVE REGISTERS DURING OPERATION
  5852 000026EC C8080000            <1> 	ENTER	8,0			; SAVE (BP) AND MAKE ROOM FOR @CMD_BLOCK
  5853 000026F0 53                  <1> 	PUSH	eBX			;  IN THE STACK, THE COMMAND BLOCK IS:
  5854 000026F1 51                  <1> 	PUSH	eCX			;   @CMD_BLOCK == BYTE PTR [BP]-8
  5855 000026F2 52                  <1> 	PUSH	eDX
  5856 000026F3 1E                  <1> 	PUSH	DS
  5857 000026F4 06                  <1> 	PUSH	ES
  5858 000026F5 56                  <1> 	PUSH	eSI
  5859 000026F6 57                  <1> 	PUSH	eDI
  5860                              <1> 	;;04/01/2015
  5861                              <1> 	;;OR	AH,AH			; CHECK FOR RESET
  5862                              <1> 	;;JNZ	short A5
  5863                              <1> 	;;MOV	DL,80H			; FORCE DRIVE 80 FOR RESET
  5864                              <1> ;;A5:	
  5865                              <1> 	;push	cs
  5866                              <1> 	;pop	ds
  5867                              <1> 	; 21/02/2015
  5868                              <1> 	;push	ax
  5869                              <1> 	; 05/12/2021
  5870 000026F7 50                  <1> 	push	eax
  5871 000026F8 66B81000            <1> 	mov	ax, KDATA
  5872 000026FC 8ED8                <1> 	mov	ds, ax
  5873 000026FE 8EC0                <1> 	mov	es, ax	
  5874                              <1> 	;pop	ax
  5875                              <1> 	; 05/12/2021
  5876 00002700 58                  <1> 	pop	eax
  5877 00002701 E889000000          <1> 	CALL	DISK_IO_CONT		; PERFORM THE OPERATION
  5878                              <1> 	;;CALL	DDS			; ESTABLISH SEGMENT
  5879 00002706 8A25[4F700000]      <1> 	MOV	AH,[DISK_STATUS1]	; GET STATUS FROM OPERATION
  5880 0000270C 80FC01              <1> 	CMP	AH,1			; SET THE CARRY FLAG TO INDICATE
  5881 0000270F F5                  <1> 	CMC				; SUCCESS OR FAILURE
  5882 00002710 5F                  <1> 	POP	eDI			; RESTORE REGISTERS
  5883 00002711 5E                  <1> 	POP	eSI
  5884 00002712 07                  <1>         POP     ES
  5885 00002713 1F                  <1>         POP     DS
  5886 00002714 5A                  <1> 	POP	eDX
  5887 00002715 59                  <1> 	POP	eCX
  5888 00002716 5B                  <1> 	POP	eBX
  5889 00002717 C9                  <1> 	LEAVE				; ADJUST (SP) AND RESTORE (BP)
  5890                              <1> 	;RETf	2			; THROW AWAY SAVED FLAGS
  5891 00002718 CA0400              <1> 	retf	4
  5892                              <1> ; 21/02/2015
  5893                              <1> ;       dw --> dd
  5894                              <1> M1:					; FUNCTION TRANSFER TABLE
  5895 0000271B [D4280000]          <1> 	dd	DISK_RESET		; 000H
  5896 0000271F [49290000]          <1> 	dd	RETURN_STATUS		; 001H
  5897 00002723 [56290000]          <1> 	dd	DISK_READ		; 002H
  5898 00002727 [5F290000]          <1> 	dd	DISK_WRITE		; 003H
  5899 0000272B [68290000]          <1> 	dd	DISK_VERF		; 004H
  5900 0000272F [80290000]          <1> 	dd	FMT_TRK 		; 005H
  5901 00002733 [CA280000]          <1> 	dd	BAD_COMMAND		; 006H	FORMAT BAD SECTORS
  5902 00002737 [CA280000]          <1> 	dd	BAD_COMMAND		; 007H	FORMAT DRIVE
  5903 0000273B [CA280000]          <1> 	dd	BAD_COMMAND		; 008H	RETURN PARAMETERS
  5904 0000273F [412A0000]          <1> 	dd	INIT_DRV		; 009H
  5905 00002743 [A02A0000]          <1> 	dd	RD_LONG 		; 00AH
  5906 00002747 [A92A0000]          <1> 	dd	WR_LONG 		; 00BH
  5907 0000274B [B22A0000]          <1> 	dd	DISK_SEEK		; 00CH
  5908 0000274F [D4280000]          <1> 	dd	DISK_RESET		; 00DH
  5909 00002753 [CA280000]          <1> 	dd	BAD_COMMAND		; 00EH	READ BUFFER
  5910 00002757 [CA280000]          <1> 	dd	BAD_COMMAND		; 00FH	WRITE BUFFER
  5911 0000275B [DA2A0000]          <1> 	dd	TST_RDY 		; 010H
  5912 0000275F [FE2A0000]          <1> 	dd	HDISK_RECAL		; 011H
  5913 00002763 [CA280000]          <1> 	dd	BAD_COMMAND		; 012H	MEMORY DIAGNOSTIC
  5914 00002767 [CA280000]          <1> 	dd	BAD_COMMAND		; 013H	DRIVE DIAGNOSTIC
  5915 0000276B [342B0000]          <1> 	dd	CTLR_DIAGNOSTIC 	; 014H	CONTROLLER DIAGNOSTIC
  5916                              <1> 	; 02/02/2015 (Temporary - Retro UNIX 386 v1 - DISK I/O test)
  5917 0000276F [CA280000]          <1> 	dd	BAD_COMMAND		; 015h
  5918 00002773 [CA280000]          <1> 	dd	BAD_COMMAND		; 016h
  5919 00002777 [CA280000]          <1> 	dd	BAD_COMMAND		; 017h
  5920 0000277B [CA280000]          <1> 	dd	BAD_COMMAND		; 018h
  5921 0000277F [CA280000]          <1> 	dd	BAD_COMMAND		; 019h
  5922 00002783 [CA280000]          <1> 	dd	BAD_COMMAND		; 01Ah
  5923 00002787 [56290000]          <1> 	dd	DISK_READ		; 01Bh ; LBA read
  5924 0000278B [5F290000]          <1> 	dd	DISK_WRITE		; 01Ch ; LBA write
  5925                              <1> M1L     EQU    $-M1
  5926                              <1> 
  5927                              <1> DISK_IO_CONT:
  5928                              <1> 	;;CALL	DDS			; ESTABLISH SEGMENT
  5929 0000278F 80FC01              <1> 	CMP	AH,01H			; RETURN STATUS
  5930                              <1> 	;;JNZ	short SU0
  5931                              <1>         ;;JMP	RETURN_STATUS
  5932                              <1> 	;je	RETURN_STATUS
  5933                              <1> 	; 05/12/2021
  5934 00002792 7505                <1> 	jne	short SU0
  5935 00002794 E9B0010000          <1> 	jmp	RETURN_STATUS
  5936                              <1> SU0:
  5937 00002799 C605[4F700000]00    <1> 	MOV	byte [DISK_STATUS1],0 	; RESET THE STATUS INDICATOR
  5938                              <1> 	;;PUSH	BX			; SAVE DATA ADDRESS
  5939                              <1> 	;mov	si, bx ;; 14/02/2015
  5940 000027A0 89DE                <1> 	mov	esi, ebx ; 21/02/2015
  5941 000027A2 8A1D[50700000]      <1> 	MOV	BL,[HF_NUM]		; GET NUMBER OF DRIVES
  5942                              <1> 	;; 04/01/2015
  5943                              <1> 	;;PUSH	AX
  5944 000027A8 80E27F              <1> 	AND	DL,7FH			; GET DRIVE AS 0 OR 1
  5945                              <1> 					; (get drive number as 0 to 3)
  5946 000027AB 38D3                <1> 	CMP	BL,DL
  5947                              <1>         ;;JBE   BAD_COMMAND_POP         ; INVALID DRIVE
  5948                              <1>         ;jbe    BAD_COMMAND ;; 14/02/2015
  5949                              <1> 	; 05/12/2021
  5950 000027AD 7705                <1> 	ja	short su0_su1
  5951 000027AF E916010000          <1> 	jmp	BAD_COMMAND
  5952                              <1> su0_su1:
  5953                              <1>         ;;03/01/2015
  5954 000027B4 29DB                <1> 	sub	ebx, ebx
  5955 000027B6 88D3                <1> 	mov	bl, dl
  5956                              <1> 	;sub	bh, bh
  5957 000027B8 883D[64700000]      <1> 	mov	[LBAMode], bh 	; 0
  5958                              <1> 	;;test	byte [bx+hd0_type], 1	; LBA ready ?
  5959                              <1> 	;test	byte [ebx+hd0_type], 1
  5960                              <1> 	;jz	short su1		; no
  5961                              <1> 	;inc	byte [LBAMode]
  5962                              <1> ;su1:
  5963                              <1> 	; 21/02/2015 (32 bit modification)
  5964                              <1> 	; 04/01/2015
  5965                              <1> 	;push	ax ; ***
  5966                              <1> 	; 05/12/2021
  5967 000027BE 50                  <1> 	push	eax ; ***
  5968                              <1> 	;PUSH	ES ; **
  5969                              <1> 	;PUSH	DX ; *
  5970                              <1> 	; 05/12/2021
  5971 000027BF 52                  <1> 	push	edx ; *
  5972                              <1> 	;push	ax
  5973 000027C0 50                  <1> 	push	eax
  5974 000027C1 E849060000          <1> 	CALL	GET_VEC 		; GET DISK PARAMETERS
  5975                              <1> 	; 02/02/2015
  5976                              <1> 	;mov	ax, [ES:BX+16] ; I/O port base address (1F0h, 170h)
  5977 000027C6 668B4310            <1> 	mov	ax, [ebx+16]
  5978 000027CA 66A3[A46A0000]      <1> 	mov	[HF_PORT], ax
  5979                              <1> 	;mov	dx, [ES:BX+18] ; control port address (3F6h, 376h)
  5980 000027D0 668B5312            <1> 	mov	dx, [ebx+18]
  5981 000027D4 668915[A66A0000]    <1> 	mov	[HF_REG_PORT], dx
  5982                              <1> 	;mov	al, [ES:BX+20] ; head register upper nibble (A0h,B0h,E0h,F0h)
  5983 000027DB 8A4314              <1> 	mov	al, [ebx+20]
  5984                              <1> 	; 23/02/2015
  5985 000027DE A840                <1> 	test	al, 40h	 ; LBA bit (bit 6)
  5986 000027E0 7406                <1> 	jz 	short su1
  5987 000027E2 FE05[64700000]      <1> 	inc	byte [LBAMode] ; 1 
  5988                              <1> su1: 	 
  5989 000027E8 C0E804              <1> 	shr 	al, 4
  5990 000027EB 2401                <1> 	and	al, 1			
  5991 000027ED A2[A86A0000]        <1> 	mov	[hf_m_s], al 
  5992                              <1> 	;
  5993                              <1> 	; 03/01/2015
  5994                              <1> 	;MOV	AL,byte [ES:BX+8]	; GET CONTROL BYTE MODIFIER
  5995 000027F2 8A4308              <1> 	mov	al, [ebx+8]
  5996                              <1> 	;MOV	DX,[HF_REG_PORT]	; Device Control register	
  5997 000027F5 EE                  <1> 	OUT	DX,AL			; SET EXTRA HEAD OPTION
  5998                              <1> 					; Control Byte:  (= 08h, here)
  5999                              <1> 					; bit 0 - 0
  6000                              <1> 					; bit 1 - nIEN (1 = disable irq)
  6001                              <1> 					; bit 2 - SRST (software RESET)
  6002                              <1> 					; bit 3 - use extra heads (8 to 15)
  6003                              <1> 					;         -always set to 1-	
  6004                              <1> 					; (bits 3 to 7 are reserved
  6005                              <1> 					;          for ATA devices)
  6006 000027F6 8A25[51700000]      <1> 	MOV	AH,[CONTROL_BYTE]	; SET EXTRA HEAD OPTION IN
  6007 000027FC 80E4C0              <1> 	AND	AH,0C0H 		; CONTROL BYTE
  6008 000027FF 08C4                <1> 	OR	AH,AL
  6009 00002801 8825[51700000]      <1> 	MOV	[CONTROL_BYTE],AH	
  6010                              <1> 	; 04/01/2015
  6011                              <1> 	;pop	ax
  6012                              <1> 	; 05/12/2021
  6013 00002807 58                  <1> 	pop	eax
  6014                              <1> 	;pop	dx ; * ;; 14/02/2015
  6015                              <1> 	; 05/12/2021
  6016 00002808 5A                  <1> 	pop	edx ; *
  6017 00002809 20E4                <1> 	and	ah, ah	; Reset function ?
  6018 0000280B 7506                <1> 	jnz	short su2
  6019                              <1> 	;;pop	dx ; * ;; 14/02/2015
  6020                              <1> 	;pop	es ; **
  6021                              <1> 	;pop	ax ; ***
  6022                              <1> 	; 05/12/2021
  6023 0000280D 58                  <1> 	pop	eax ; ***	
  6024                              <1> 	;;pop	bx
  6025 0000280E E9C1000000          <1>         jmp     DISK_RESET
  6026                              <1> su2:
  6027 00002813 803D[64700000]00    <1> 	cmp	byte [LBAMode], 0
  6028 0000281A 765E                <1> 	jna	short su3
  6029                              <1> 	;
  6030                              <1> 	; 02/02/2015 (LBA read/write function calls)
  6031 0000281C 80FC1B              <1> 	cmp	ah, 1Bh
  6032 0000281F 720B                <1> 	jb	short lbarw1
  6033 00002821 80FC1C              <1> 	cmp	ah, 1Ch
  6034 00002824 7759                <1> 	ja 	short invldfnc
  6035                              <1> 	;;pop	dx ; * ; 14/02/2015
  6036                              <1> 	;mov	ax, cx ; Lower word of LBA address (bits 0-15)
  6037 00002826 89C8                <1> 	mov	eax, ecx ; LBA address (21/02/2015)
  6038                              <1> 	;; 14/02/2015
  6039 00002828 88D1                <1> 	mov	cl, dl ; 14/02/2015
  6040                              <1> 	;;mov	dx, bx
  6041                              <1> 	;mov	dx, si ; higher word of LBA address (bits 16-23)
  6042                              <1> 	;;mov	bx, di
  6043                              <1> 	;mov	si, di ; Buffer offset
  6044 0000282A EB2E                <1> 	jmp	short lbarw2
  6045                              <1> lbarw1:
  6046                              <1> 	; convert CHS to LBA
  6047                              <1> 	;
  6048                              <1> 	; LBA calculation - AWARD BIOS - 1999 - AHDSK.ASM
  6049                              <1> 	; LBA = "# of Heads" * Sectors/Track * Cylinder + Head * Sectors/Track
  6050                              <1> 	;	+ Sector - 1
  6051                              <1> 	;push	dx ; * ;; 14/02/2015
  6052                              <1> 	; 05/12/2021
  6053 0000282C 52                  <1> 	push	edx ; *
  6054                              <1> 	;xor	dh, dh
  6055 0000282D 31D2                <1> 	xor	edx, edx
  6056                              <1> 	;mov	dl, [ES:BX+14]	; sectors per track (logical)
  6057 0000282F 8A530E              <1> 	mov	dl, [ebx+14]
  6058                              <1> 	;xor	ah, ah
  6059 00002832 31C0                <1> 	xor	eax, eax
  6060                              <1> 	;mov	al, [ES:BX+2]	; heads (logical) 	
  6061 00002834 8A4302              <1> 	mov	al, [ebx+2]
  6062 00002837 FEC8                <1> 	dec	al
  6063                              <1> 	;inc	ax		; 0 =  256
  6064 00002839 40                  <1> 	inc	eax ; 05/12/2021
  6065 0000283A 66F7E2              <1> 	mul 	dx
  6066                              <1> 		; AX = # of Heads" * Sectors/Track
  6067 0000283D 6689CA              <1> 	mov	dx, cx
  6068                              <1> 	;and	cx, 3Fh	 ; sector  (1 to 63)
  6069 00002840 83E13F              <1> 	and	ecx, 3fh
  6070 00002843 86D6                <1> 	xchg	dl, dh
  6071 00002845 C0EE06              <1> 	shr	dh, 6
  6072                              <1> 		; DX = cylinder (0 to 1023)
  6073                              <1> 	;mul 	dx
  6074                              <1> 		; DX:AX = # of Heads" * Sectors/Track * Cylinder
  6075 00002848 F7E2                <1> 	mul	edx
  6076 0000284A FEC9                <1> 	dec	cl  ; sector - 1
  6077                              <1> 	;add	ax, cx
  6078                              <1> 	;adc	dx, 0
  6079                              <1> 		; DX:AX = # of Heads" * Sectors/Track * Cylinder + Sector -1
  6080 0000284C 01C8                <1> 	add	eax, ecx
  6081                              <1> 	;pop	cx ; * ; ch = head, cl = drive number (zero based)
  6082                              <1> 	; 05/12/2021
  6083 0000284E 59                  <1> 	pop	ecx ; * ; ch = head, cl = drive number (zero based)
  6084                              <1> 	;push	dx
  6085                              <1> 	;push	ax
  6086 0000284F 50                  <1> 	push	eax
  6087                              <1> 	;mov	al, [ES:BX+14]	; sectors per track (logical)	
  6088 00002850 8A430E              <1> 	mov	al, [ebx+14]
  6089 00002853 F6E5                <1> 	mul	ch
  6090                              <1> 		; AX = Head * Sectors/Track
  6091 00002855 6699                <1>         cwd
  6092                              <1> 	;pop	dx
  6093 00002857 5A                  <1> 	pop	edx
  6094                              <1> 	;add	ax, dx
  6095                              <1> 	;pop	dx
  6096                              <1> 	;adc	dx, 0 ; add carry bit
  6097 00002858 01D0                <1> 	add	eax, edx
  6098                              <1> lbarw2:
  6099 0000285A 29D2                <1> 	sub	edx, edx ; 21/02/2015
  6100 0000285C 88CA                <1> 	mov	dl, cl ; 21/02/2015
  6101 0000285E C645F800            <1>         mov     byte [CMD_BLOCK], 0 ; Features Register
  6102                              <1> 				; NOTE: Features register (1F1h, 171h)
  6103                              <1> 				; is not used for ATA device R/W functions. 
  6104                              <1> 				; It is old/obsolete 'write precompensation'
  6105                              <1> 				; register and error register
  6106                              <1> 				; for old ATA/IDE devices.
  6107                              <1> 	; 18/01/2014
  6108                              <1> 	;mov	ch, [hf_m_s]	; Drive 0 (master) or 1 (slave)
  6109 00002862 8A0D[A86A0000]      <1> 	mov	cl, [hf_m_s]
  6110                              <1> 	;shl	ch, 4		; bit 4 (drive bit)
  6111                              <1> 	;or	ch, 0E0h	; bit 5 = 1
  6112                              <1> 				; bit 6 = 1 = LBA mode
  6113                              <1> 				; bit 7 = 1
  6114 00002868 80C90E              <1> 	or	cl, 0Eh ; 1110b
  6115                              <1> 	;and	dh, 0Fh		; LBA byte 4 (bits 24 to 27)
  6116 0000286B 25FFFFFF0F          <1> 	and	eax, 0FFFFFFFh
  6117 00002870 C1E11C              <1> 	shl	ecx, 28 ; 21/02/2015
  6118                              <1> 	;or	dh, ch
  6119 00002873 09C8                <1> 	or	eax, ecx	
  6120                              <1> 	;;mov	[CMD_BLOCK+2], al ; LBA byte 1 (bits 0 to 7)
  6121                              <1> 				  ; (Sector Number Register)
  6122                              <1> 	;;mov	[CMD_BLOCK+3], ah ; LBA byte 2 (bits 8 to 15)
  6123                              <1> 				  ; (Cylinder Low Register)
  6124                              <1> 	;mov	[CMD_BLOCK+2], ax ; LBA byte 1, 2
  6125                              <1> 	;mov	[CMD_BLOCK+4], dl ; LBA byte 3 (bits 16 to 23)
  6126                              <1> 				  ; (Cylinder High Register)
  6127                              <1> 	;;mov	[CMD_BLOCK+5], dh ; LBA byte 4 (bits 24 to 27)
  6128                              <1> 				  ; (Drive/Head Register)
  6129                              <1> 	
  6130                              <1> 	;mov	[CMD_BLOCK+4], dx ; LBA byte 4, LBA & DEV select bits
  6131 00002875 8945FA              <1> 	mov	[CMD_BLOCK+2], eax ; 21/02/2015
  6132                              <1> 	;14/02/2015
  6133                              <1> 	;mov	dl, cl ; Drive number (INIT_DRV)		
  6134 00002878 EB37                <1> 	jmp	short su4
  6135                              <1> su3:
  6136                              <1> 	; 02/02/2015 
  6137                              <1> 	; (Temporary functions 1Bh & 1Ch are not valid for CHS mode) 
  6138 0000287A 80FC14              <1> 	cmp 	ah, 14h
  6139 0000287D 7603                <1> 	jna 	short chsfnc
  6140                              <1> invldfnc:
  6141                              <1>         ; 14/02/2015  
  6142                              <1> 	;pop	es ; **
  6143                              <1>         ;pop	ax ; ***
  6144                              <1>         ; 05/12/2021
  6145 0000287F 58                  <1> 	pop	eax ; ***
  6146                              <1> 	;jmp	short BAD_COMMAND_POP
  6147 00002880 EB48                <1>         jmp     short BAD_COMMAND
  6148                              <1> chsfnc:	
  6149                              <1> 	;MOV	AX,[ES:BX+5]		; GET WRITE PRE-COMPENSATION CYLINDER
  6150 00002882 668B4305            <1> 	mov	ax, [ebx+5]
  6151 00002886 66C1E802            <1> 	SHR	AX,2
  6152 0000288A 8845F8              <1> 	MOV	[CMD_BLOCK],AL
  6153                              <1> 	;;MOV	AL,[ES:BX+8]		; GET CONTROL BYTE MODIFIER
  6154                              <1> 	;;PUSH	DX
  6155                              <1> 	;;MOV	DX,[HF_REG_PORT]
  6156                              <1> 	;;OUT	DX,AL			; SET EXTRA HEAD OPTION
  6157                              <1> 	;;POP	DX ; * 
  6158                              <1> 	;;POP	ES ; **
  6159                              <1> 	;;MOV	AH,[CONTROL_BYTE]	; SET EXTRA HEAD OPTION IN
  6160                              <1> 	;;AND	AH,0C0H 		; CONTROL BYTE	
  6161                              <1> 	;;OR	AH,AL
  6162                              <1> 	;;MOV	[CONTROL_BYTE],AH
  6163                              <1> 	;
  6164 0000288D 88C8                <1> 	MOV	AL,CL			; GET SECTOR NUMBER
  6165 0000288F 243F                <1> 	AND	AL,3FH
  6166 00002891 8845FA              <1> 	MOV	[CMD_BLOCK+2],AL
  6167 00002894 886DFB              <1> 	MOV	[CMD_BLOCK+3],CH 	; GET CYLINDER NUMBER
  6168 00002897 88C8                <1> 	MOV	AL,CL
  6169 00002899 C0E806              <1> 	SHR	AL,6
  6170 0000289C 8845FC              <1> 	MOV	[CMD_BLOCK+4],AL 	; CYLINDER HIGH ORDER 2 BITS
  6171                              <1> 	;;05/01/2015
  6172                              <1> 	;;MOV	AL,DL			; DRIVE NUMBER
  6173 0000289F A0[A86A0000]        <1> 	mov	al, [hf_m_s]
  6174 000028A4 C0E004              <1> 	SHL	AL,4
  6175 000028A7 80E60F              <1> 	AND	DH,0FH			; HEAD NUMBER
  6176 000028AA 08F0                <1> 	OR	AL,DH
  6177                              <1> 	;OR	AL,80H or 20H
  6178 000028AC 0CA0                <1> 	OR	AL,80h+20h		; ECC AND 512 BYTE SECTORS
  6179 000028AE 8845FD              <1> 	MOV	[CMD_BLOCK+5],AL 	; ECC/SIZE/DRIVE/HEAD
  6180                              <1> su4:
  6181                              <1> 	;POP	ES ; **
  6182                              <1>         ;; 14/02/2015
  6183                              <1>         ;;POP   AX
  6184                              <1>         ;;MOV   [CMD_BLOCK+1],AL        ; SECTOR COUNT
  6185                              <1>         ;;PUSH  AX
  6186                              <1>         ;;MOV   AL,AH                   ; GET INTO LOW BYTE
  6187                              <1>         ;;XOR   AH,AH                   ; ZERO HIGH BYTE
  6188                              <1>         ;;SAL   AX,1                    ; *2 FOR TABLE LOOKUP
  6189                              <1> 	;pop	ax ; ***
  6190                              <1> 	; 05/12/2021
  6191 000028B1 58                  <1> 	pop	eax ; ***
  6192 000028B2 8845F9              <1> 	mov     [CMD_BLOCK+1], al
  6193 000028B5 29DB                <1>         sub	ebx, ebx
  6194 000028B7 88E3                <1> 	mov     bl, ah
  6195                              <1>         ;xor	bh, bh
  6196                              <1>         ;sal	bx, 1
  6197 000028B9 66C1E302            <1>         sal	bx, 2	; 32 bit offset (21/02/2015)
  6198                              <1> 	;;MOV   SI,AX                   ; PUT INTO SI FOR BRANCH
  6199                              <1>         ;;CMP   AX,M1L                  ; TEST WITHIN RANGE
  6200                              <1>         ;;JNB   short BAD_COMMAND_POP
  6201                              <1>         ;cmp	bx, M1L
  6202 000028BD 83FB74              <1> 	cmp	ebx, M1L
  6203 000028C0 7308                <1> 	jnb	short BAD_COMMAND
  6204                              <1>         ;xchg	bx, si
  6205 000028C2 87DE                <1>         xchg	ebx, esi
  6206                              <1> 	;;;POP	AX			; RESTORE AX
  6207                              <1> 	;;;POP	BX			; AND DATA ADDRESS
  6208                              <1> 	
  6209                              <1> 	;;PUSH	CX
  6210                              <1> 	;;PUSH	AX			; ADJUST ES:BX
  6211                              <1> 	;MOV	CX,BX			; GET 3 HIGH ORDER NIBBLES OF BX
  6212                              <1> 	;SHR	CX,4
  6213                              <1> 	;MOV	AX,ES
  6214                              <1> 	;ADD	AX,CX
  6215                              <1> 	;MOV	ES,AX
  6216                              <1> 	;AND	BX,000FH		; ES:BX CHANGED TO ES:000X
  6217                              <1> 	;;POP	AX
  6218                              <1> 	;;POP	CX
  6219                              <1> 	;;JMP	word [CS:SI+M1]
  6220                              <1> 	;jmp	word [SI+M1]
  6221 000028C4 FFA6[1B270000]      <1> 	jmp	dword [esi+M1]
  6222                              <1> ;;BAD_COMMAND_POP:
  6223                              <1> ;;	POP	AX
  6224                              <1> ;;	POP	BX
  6225                              <1> BAD_COMMAND:
  6226 000028CA C605[4F700000]01    <1>         MOV     byte [DISK_STATUS1],BAD_CMD  ; COMMAND ERROR
  6227 000028D1 B000                <1> 	MOV	AL,0
  6228 000028D3 C3                  <1> 	RETn
  6229                              <1> 
  6230                              <1> ;----------------------------------------
  6231                              <1> ;	RESET THE DISK SYSTEM  (AH=00H) :
  6232                              <1> ;----------------------------------------
  6233                              <1> 
  6234                              <1> ; 18-1-2015 : one controller reset (not other one)
  6235                              <1> 
  6236                              <1> DISK_RESET:
  6237 000028D4 FA                  <1> 	CLI
  6238 000028D5 E4A1                <1> 	IN	AL,INTB01		; GET THE MASK REGISTER
  6239                              <1> 	;JMP	$+2
  6240                              <1> 	IODELAY
  2127 000028D7 EB00                <2>  jmp short $+2
  2128 000028D9 EB00                <2>  jmp short $+2
  6241                              <1> 	;AND	AL,0BFH 		; ENABLE FIXED DISK INTERRUPT
  6242 000028DB 243F                <1> 	and	al,3Fh			; 22/12/2014 (IRQ 14 & IRQ 15)
  6243 000028DD E6A1                <1> 	OUT	INTB01,AL
  6244 000028DF FB                  <1> 	STI				; START INTERRUPTS
  6245                              <1> 	; 14/02/2015
  6246                              <1> 	;mov	di, dx
  6247                              <1> 	; 05/12/2021
  6248 000028E0 89D7                <1> 	mov	edi, edx	
  6249                              <1> 	; 04/01/2015
  6250                              <1> 	;xor	di,di
  6251                              <1> drst0:
  6252 000028E2 B004                <1> 	MOV	AL,04H  ; bit 2 - SRST 
  6253                              <1> 	;MOV	DX,HF_REG_PORT
  6254 000028E4 668B15[A66A0000]    <1> 	MOV	DX,[HF_REG_PORT]
  6255 000028EB EE                  <1> 	OUT	DX,AL			; RESET
  6256                              <1> ;	MOV	CX,10			; DELAY COUNT
  6257                              <1> ;DRD:	DEC	CX
  6258                              <1> ;	JNZ	short DRD		; WAIT 4.8 MICRO-SEC
  6259                              <1> 	;mov	cx,2			; wait for 30 micro seconds	
  6260 000028EC B902000000          <1>         mov	ecx, 2 ; 21/02/2015
  6261 000028F1 E8D3EBFFFF          <1> 	call    WAITF                   ; (Award Bios 1999 - WAIT_REFRESH,
  6262                              <1>                                         ; 40 micro seconds)
  6263 000028F6 A0[51700000]        <1> 	mov	al,[CONTROL_BYTE]
  6264 000028FB 240F                <1> 	AND	AL,0FH			; SET HEAD OPTION
  6265 000028FD EE                  <1> 	OUT	DX,AL			; TURN RESET OFF
  6266 000028FE E807040000          <1> 	CALL	NOT_BUSY
  6267 00002903 7515                <1> 	JNZ	short DRERR		; TIME OUT ON RESET
  6268 00002905 668B15[A46A0000]    <1> 	MOV	DX,[HF_PORT]
  6269 0000290C FEC2                <1> 	inc	dl  ; HF_PORT+1
  6270                              <1> 	; 02/01/2015 - Award BIOS 1999 - AHDSK.ASM
  6271                              <1>         ;mov	cl, 10
  6272 0000290E B90A000000          <1>         mov     ecx, 10 ; 21/02/2015 
  6273                              <1> drst1:
  6274 00002913 EC                  <1> 	IN	AL,DX			; GET RESET STATUS
  6275 00002914 3C01                <1> 	CMP	AL,1
  6276                              <1> 	; 04/01/2015
  6277 00002916 740A                <1> 	jz	short drst2
  6278                              <1> 	;JNZ	short DRERR		; BAD RESET STATUS
  6279                              <1>         	; Drive/Head Register - bit 4
  6280 00002918 E2F9                <1> 	loop	drst1
  6281                              <1> DRERR:	
  6282 0000291A C605[4F700000]05    <1> 	MOV	byte [DISK_STATUS1],BAD_RESET ; CARD FAILED
  6283 00002921 C3                  <1> 	RETn
  6284                              <1> drst2:
  6285                              <1> 	; 14/02/2015
  6286                              <1> 	;mov	dx, di
  6287                              <1> 	; 05/12/2021
  6288 00002922 89FA                <1> 	mov	edx, edi
  6289                              <1> ;drst3:
  6290                              <1> ;	; 05/01/2015
  6291                              <1> ;	shl 	di,1
  6292                              <1> ;	; 04/01/2015
  6293                              <1> ;	mov	ax,[di+hd_cports]
  6294                              <1> ;	cmp	ax,[HF_REG_PORT]
  6295                              <1> ;	je	short drst4
  6296                              <1> ;	mov	[HF_REG_PORT], ax
  6297                              <1> ;	; 03/01/2015
  6298                              <1> ;	mov	ax,[di+hd_ports]
  6299                              <1> ;       mov     [HF_PORT], ax
  6300                              <1> ;	; 05/01/2014
  6301                              <1> ;	shr	di,1
  6302                              <1> ;	; 04/01/2015
  6303                              <1> ;	jmp	short drst0	; reset other controller
  6304                              <1> ;drst4:
  6305                              <1> ;	; 05/01/2015
  6306                              <1> ;	shr	di,1
  6307                              <1> ;	mov	al,[di+hd_dregs]
  6308                              <1> ;	and	al,10h ; bit 4 only
  6309                              <1> ;	shr	al,4 ; bit 4  -> bit 0
  6310                              <1> ;	mov	[hf_m_s], al ; (0 = master, 1 = slave)
  6311                              <1> 	;
  6312 00002924 A0[A86A0000]        <1> 	mov	al, [hf_m_s] ; 18/01/2015
  6313 00002929 A801                <1> 	test	al,1
  6314                              <1> ;	jnz	short drst6
  6315 0000292B 7516                <1>         jnz     short drst4
  6316 0000292D 8065FDEF            <1> 	AND     byte [CMD_BLOCK+5],0EFH ; SET TO DRIVE 0
  6317                              <1> ;drst5:
  6318                              <1> drst3:
  6319 00002931 E80B010000          <1> 	CALL	INIT_DRV		; SET MAX HEADS
  6320                              <1> 	;mov	dx,di
  6321 00002936 E8C3010000          <1> 	CALL	HDISK_RECAL		; RECAL TO RESET SEEK SPEED
  6322                              <1> 	; 04/01/2014
  6323                              <1> ;	inc	di
  6324                              <1> ;	mov	dx,di
  6325                              <1> ;	cmp	dl,[HF_NUM]
  6326                              <1> ;	jb	short drst3
  6327                              <1> ;DRE:
  6328 0000293B C605[4F700000]00    <1> 	MOV	byte [DISK_STATUS1],0 	; IGNORE ANY SET UP ERRORS
  6329 00002942 C3                  <1> 	RETn
  6330                              <1> ;drst6:
  6331                              <1> drst4:		; Drive/Head Register - bit 4
  6332 00002943 804DFD10            <1> 	OR      byte [CMD_BLOCK+5],010H ; SET TO DRIVE 1     
  6333                              <1>         ;jmp    short drst5
  6334 00002947 EBE8                <1>         jmp     short drst3
  6335                              <1> 
  6336                              <1> ;----------------------------------------
  6337                              <1> ;	DISK STATUS ROUTINE  (AH = 01H) :
  6338                              <1> ;----------------------------------------
  6339                              <1> 
  6340                              <1> RETURN_STATUS:
  6341 00002949 A0[4F700000]        <1> 	MOV	AL,[DISK_STATUS1]	; OBTAIN PREVIOUS STATUS
  6342 0000294E C605[4F700000]00    <1>         MOV     byte [DISK_STATUS1],0   ; RESET STATUS
  6343 00002955 C3                  <1> 	RETn
  6344                              <1> 
  6345                              <1> ;----------------------------------------
  6346                              <1> ;	DISK READ ROUTINE    (AH = 02H) :
  6347                              <1> ;----------------------------------------
  6348                              <1> 
  6349                              <1> DISK_READ:
  6350 00002956 C645FE20            <1> 	MOV	byte [CMD_BLOCK+6],READ_CMD
  6351 0000295A E92A020000          <1>         JMP     COMMANDI
  6352                              <1> 
  6353                              <1> ;----------------------------------------
  6354                              <1> ;	DISK WRITE ROUTINE   (AH = 03H) :
  6355                              <1> ;----------------------------------------
  6356                              <1> 
  6357                              <1> DISK_WRITE:
  6358 0000295F C645FE30            <1> 	MOV	byte [CMD_BLOCK+6],WRITE_CMD
  6359 00002963 E976020000          <1>         JMP     COMMANDO
  6360                              <1> 
  6361                              <1> ;----------------------------------------
  6362                              <1> ;	DISK VERIFY	     (AH = 04H) :
  6363                              <1> ;----------------------------------------
  6364                              <1> 
  6365                              <1> DISK_VERF:
  6366 00002968 C645FE40            <1> 	MOV	byte [CMD_BLOCK+6],VERIFY_CMD
  6367 0000296C E8E4020000          <1> 	CALL	COMMAND
  6368 00002971 750C                <1> 	JNZ	short VERF_EXIT		; CONTROLLER STILL BUSY
  6369 00002973 E856030000          <1> 	CALL	_WAIT			; (Original: CALL WAIT)	
  6370 00002978 7505                <1> 	JNZ	short VERF_EXIT		; TIME OUT
  6371 0000297A E8E3030000          <1> 	CALL	CHECK_STATUS
  6372                              <1> VERF_EXIT:
  6373 0000297F C3                  <1> 	RETn
  6374                              <1> 
  6375                              <1> ;----------------------------------------
  6376                              <1> ;	FORMATTING	     (AH = 05H) :
  6377                              <1> ;----------------------------------------
  6378                              <1> 
  6379                              <1> FMT_TRK:				; FORMAT TRACK	(AH = 005H)
  6380 00002980 C645FE50            <1> 	MOV	byte [CMD_BLOCK+6],FMTTRK_CMD
  6381                              <1> 	;PUSH	ES
  6382                              <1> 	;PUSH	BX
  6383 00002984 53                  <1> 	push	ebx
  6384 00002985 E885040000          <1> 	CALL	GET_VEC 		; GET DISK PARAMETERS ADDRESS
  6385                              <1> 	;MOV	AL,[ES:BX+14]		; GET SECTORS/TRACK
  6386 0000298A 8A430E              <1> 	mov	al, [ebx+14]
  6387 0000298D 8845F9              <1> 	MOV	[CMD_BLOCK+1],AL 	; SET SECTOR COUNT IN COMMAND
  6388 00002990 5B                  <1> 	pop	ebx
  6389                              <1> 	;POP	BX
  6390                              <1> 	;POP	ES
  6391 00002991 E94F020000          <1>         JMP     CMD_OF                  ; GO EXECUTE THE COMMAND
  6392                              <1> 
  6393                              <1> ;----------------------------------------
  6394                              <1> ;	READ DASD TYPE	     (AH = 15H) :
  6395                              <1> ;----------------------------------------
  6396                              <1> 
  6397                              <1> READ_DASD_TYPE:
  6398                              <1> READ_D_T:				; GET DRIVE PARAMETERS
  6399 00002996 1E                  <1> 	PUSH	DS			; SAVE REGISTERS
  6400                              <1> 	;PUSH	ES
  6401 00002997 53                  <1> 	PUSH	eBX
  6402                              <1> 	;CALL	DDS			; ESTABLISH ADDRESSING
  6403                              <1> 	;push	cs
  6404                              <1> 	;pop	ds
  6405 00002998 66BB1000            <1>         mov	bx, KDATA
  6406 0000299C 8EDB                <1> 	mov	ds, bx
  6407                              <1> 	;mov	es, bx
  6408 0000299E C605[4F700000]00    <1> 	MOV     byte [DISK_STATUS1],0
  6409 000029A5 8A1D[50700000]      <1> 	MOV	BL,[HF_NUM]		; GET NUMBER OF DRIVES
  6410 000029AB 80E27F              <1> 	AND	DL,7FH			; GET DRIVE NUMBER
  6411 000029AE 38D3                <1> 	CMP	BL,DL
  6412 000029B0 7625                <1> 	JBE	short RDT_NOT_PRESENT 	; RETURN DRIVE NOT PRESENT
  6413 000029B2 E858040000          <1> 	CALL	GET_VEC 		; GET DISK PARAMETER ADDRESS
  6414                              <1> 	;MOV	AL,[ES:BX+2]		; HEADS
  6415 000029B7 8A4302              <1> 	mov	al, [ebx+2]
  6416                              <1> 	;MOV	CL,[ES:BX+14]
  6417 000029BA 8A4B0E              <1> 	mov	cl, [ebx+14]
  6418 000029BD F6E9                <1> 	IMUL	CL			; * NUMBER OF SECTORS
  6419                              <1> 	;MOV	CX,[ES:BX]		; MAX NUMBER OF CYLINDERS
  6420 000029BF 668B0B              <1> 	mov	cx, [ebx]
  6421                              <1> 	;
  6422                              <1> 	; 02/01/2015 
  6423                              <1> 	; ** leave the last cylinder as reserved for diagnostics **
  6424                              <1> 	; (Also in Award BIOS - 1999, AHDSK.ASM, FUN15 -> sub ax, 1)
  6425 000029C2 6649                <1> 	DEC	CX			; LEAVE ONE FOR DIAGNOSTICS
  6426                              <1> 	;
  6427 000029C4 66F7E9              <1> 	IMUL	CX			; NUMBER OF SECTORS
  6428 000029C7 6689D1              <1> 	MOV	CX,DX			; HIGH ORDER HALF
  6429 000029CA 6689C2              <1> 	MOV	DX,AX			; LOW ORDER HALF
  6430                              <1> 	;SUB	AX,AX
  6431 000029CD 28C0                <1> 	sub	al, al
  6432 000029CF B403                <1> 	MOV	AH,03H			; INDICATE FIXED DISK
  6433 000029D1 5B                  <1> RDT2:	POP	eBX			; RESTORE REGISTERS
  6434                              <1> 	;POP	ES
  6435 000029D2 1F                  <1> 	POP	DS
  6436 000029D3 F8                  <1> 	CLC				; CLEAR CARRY
  6437                              <1> 	;RETf	2
  6438 000029D4 CA0400              <1> 	retf	4
  6439                              <1> RDT_NOT_PRESENT:
  6440                              <1> 	;SUB	AX,AX			; DRIVE NOT PRESENT RETURN
  6441                              <1> 	; 05/12/2021
  6442 000029D7 29C0                <1> 	sub	eax, eax
  6443                              <1> 	;MOV	CX,AX			; ZERO BLOCK COUNT
  6444                              <1> 	;MOV	DX,AX
  6445 000029D9 89C1                <1> 	mov	ecx, eax
  6446 000029DB 89C2                <1> 	mov	edx, eax
  6447 000029DD EBF2                <1> 	JMP	short RDT2
  6448                              <1> 
  6449                              <1> ;----------------------------------------
  6450                              <1> ;	GET PARAMETERS	     (AH = 08H) :
  6451                              <1> ;----------------------------------------
  6452                              <1> 
  6453                              <1> GET_PARM_N:
  6454                              <1> ;GET_PARM:				; GET DRIVE PARAMETERS
  6455 000029DF 1E                  <1> 	PUSH	DS			; SAVE REGISTERS
  6456                              <1> 	;PUSH	ES
  6457 000029E0 53                  <1> 	PUSH	eBX
  6458                              <1> 	;MOV	AX,ABS0 		; ESTABLISH ADDRESSING
  6459                              <1> 	;MOV	DS,AX
  6460                              <1> 	;TEST	DL,1			; CHECK FOR DRIVE 1
  6461                              <1> 	;JZ	short G0
  6462                              <1> 	;LES	BX,@HF1_TBL_VEC
  6463                              <1> 	;JMP	SHORT G1
  6464                              <1> ;G0:	LES	BX,@HF_TBL_VEC
  6465                              <1> ;G1:
  6466                              <1> 	;CALL	DDS			; ESTABLISH SEGMENT
  6467                              <1> 	; 22/12/2014
  6468                              <1> 	;push	cs
  6469                              <1> 	;pop	ds
  6470 000029E1 66BB1000            <1> 	mov	bx, KDATA
  6471 000029E5 8EDB                <1> 	mov	ds, bx
  6472                              <1> 	;mov	es, bx
  6473                              <1> 	;
  6474 000029E7 80EA80              <1> 	SUB	DL,80H
  6475 000029EA 80FA04              <1> 	CMP	DL,MAX_FILE		; TEST WITHIN RANGE
  6476 000029ED 7340                <1> 	JAE	short G4
  6477                              <1> 	;
  6478 000029EF 31DB                <1> 	xor	ebx, ebx ; 21/02/2015
  6479                              <1> 	; 22/12/2014
  6480 000029F1 88D3                <1> 	mov	bl, dl
  6481                              <1> 	;xor	bh, bh  
  6482 000029F3 C0E302              <1> 	shl	bl, 2			; convert index to offset
  6483                              <1> 	;add	bx, HF_TBL_VEC
  6484 000029F6 81C3[54700000]      <1> 	add	ebx, HF_TBL_VEC
  6485                              <1> 	;mov	ax, [bx+2]
  6486                              <1> 	;mov	es, ax			; dpt segment
  6487                              <1> 	;mov	bx, [bx]		; dpt offset
  6488 000029FC 8B1B                <1> 	mov	ebx, [ebx] ; 32 bit offset	
  6489                              <1> 
  6490 000029FE C605[4F700000]00    <1> 	MOV	byte [DISK_STATUS1],0
  6491                              <1>         ;MOV     AX,[ES:BX]              ; MAX NUMBER OF CYLINDERS
  6492 00002A05 668B03              <1> 	mov	ax, [ebx]
  6493                              <1> 	;;SUB	AX,2			; ADJUST FOR 0-N
  6494 00002A08 6648                <1> 	dec	ax			; max. cylinder number
  6495 00002A0A 88C5                <1> 	MOV	CH,AL
  6496 00002A0C 66250003            <1> 	AND	AX,0300H		; HIGH TWO BITS OF CYLINDER
  6497 00002A10 66D1E8              <1> 	SHR	AX,1
  6498 00002A13 66D1E8              <1> 	SHR	AX,1
  6499                              <1> 	;OR	AL,[ES:BX+14]		; SECTORS
  6500 00002A16 0A430E              <1> 	or	al, [ebx+14]
  6501 00002A19 88C1                <1> 	MOV	CL,AL
  6502                              <1> 	;MOV	DH,[ES:BX+2]		; HEADS
  6503 00002A1B 8A7302              <1> 	mov	dh, [ebx+2]
  6504 00002A1E FECE                <1> 	DEC	DH			; 0-N RANGE
  6505 00002A20 8A15[50700000]      <1> 	MOV	DL,[HF_NUM]		; DRIVE COUNT
  6506                              <1> 	;SUB	AX,AX
  6507                              <1> 	; 05/12/2021
  6508 00002A26 29C0                <1> 	sub	eax, eax
  6509                              <1> 	; 27/12/2014 
  6510                              <1> 	; ES:DI = Address of disk parameter table from BIOS
  6511                              <1> 	; (Programmer's Guide to the AMIBIOS - 1993)
  6512                              <1> 	;mov	di, bx			; HDPT offset
  6513 00002A28 89DF                <1> 	mov	edi, ebx
  6514                              <1> G5:
  6515 00002A2A 5B                  <1> 	POP	eBX			; RESTORE REGISTERS
  6516                              <1> 	;POP	ES
  6517 00002A2B 1F                  <1> 	POP	DS
  6518                              <1> 	;RETf	2
  6519 00002A2C CA0400              <1> 	retf	4
  6520                              <1> G4:
  6521 00002A2F C605[4F700000]07    <1> 	MOV     byte [DISK_STATUS1],INIT_FAIL ; OPERATION FAILED
  6522                              <1> 	; 05/12/2021
  6523 00002A36 29C0                <1> 	sub	eax, eax
  6524 00002A38 B407                <1> 	MOV	AH,INIT_FAIL
  6525                              <1> 	;SUB	AL,AL
  6526                              <1> 	;SUB	DX,DX
  6527 00002A3A 29D2                <1> 	sub	edx, edx
  6528                              <1> 	;SUB	CX,CX
  6529 00002A3C 29C9                <1> 	sub	ecx, ecx
  6530 00002A3E F9                  <1> 	STC				; SET ERROR FLAG
  6531 00002A3F EBE9                <1> 	JMP	short G5
  6532                              <1> 
  6533                              <1> ;----------------------------------------
  6534                              <1> ;	INITIALIZE DRIVE     (AH = 09H) :
  6535                              <1> ;----------------------------------------
  6536                              <1> 	; 03/01/2015
  6537                              <1> 	; According to ATA-ATAPI specification v2.0 to v5.0
  6538                              <1> 	; logical sector per logical track
  6539                              <1> 	; and logical heads - 1 would be set but
  6540                              <1> 	; it is seen as it will be good
  6541                              <1> 	; if physical parameters will be set here
  6542                              <1> 	; because, number of heads <= 16.
  6543                              <1> 	; (logical heads usually more than 16)
  6544                              <1> 	; NOTE: ATA logical parameters (software C, H, S)
  6545                              <1> 	;	== INT 13h physical parameters
  6546                              <1> 
  6547                              <1> ;INIT_DRV:
  6548                              <1> ;	MOV	byte [CMD_BLOCK+6],SET_PARM_CMD
  6549                              <1> ;	CALL	GET_VEC 		; ES:BX -> PARAMETER BLOCK
  6550                              <1> ;	MOV	AL,[ES:BX+2]		; GET NUMBER OF HEADS
  6551                              <1> ;	DEC	AL			; CONVERT TO 0-INDEX
  6552                              <1> ;	MOV	AH,[CMD_BLOCK+5] 	; GET SDH REGISTER
  6553                              <1> ;	AND	AH,0F0H 		; CHANGE HEAD NUMBER
  6554                              <1> ;	OR	AH,AL			; TO MAX HEAD
  6555                              <1> ;	MOV	[CMD_BLOCK+5],AH
  6556                              <1> ;	MOV	AL,[ES:BX+14]		; MAX SECTOR NUMBER
  6557                              <1> ;	MOV	[CMD_BLOCK+1],AL
  6558                              <1> ;	SUB	AX,AX
  6559                              <1> ;	MOV	[CMD_BLOCK+3],AL 	; ZERO FLAGS
  6560                              <1> ;	CALL	COMMAND 		; TELL CONTROLLER
  6561                              <1> ;	JNZ	short INIT_EXIT		; CONTROLLER BUSY ERROR
  6562                              <1> ;	CALL	NOT_BUSY		; WAIT FOR IT TO BE DONE
  6563                              <1> ;	JNZ	short INIT_EXIT		; TIME OUT
  6564                              <1> ;	CALL	CHECK_STATUS
  6565                              <1> ;INIT_EXIT:
  6566                              <1> ;	RETn
  6567                              <1> 
  6568                              <1> ; 04/01/2015
  6569                              <1> ; 02/01/2015 - Derived from from AWARD BIOS 1999
  6570                              <1> ;				 AHDSK.ASM - INIT_DRIVE
  6571                              <1> INIT_DRV:
  6572                              <1> 	;xor	ah,ah
  6573 00002A41 31C0                <1> 	xor	eax,eax ; 21/02/2015
  6574 00002A43 B00B                <1> 	mov	al,11 ; Physical heads from translated HDPT
  6575 00002A45 3825[64700000]      <1>         cmp     [LBAMode], ah   ; 0
  6576 00002A4B 7702                <1> 	ja	short idrv0
  6577 00002A4D B002                <1> 	mov	al,2  ; Physical heads from standard HDPT
  6578                              <1> idrv0:
  6579                              <1> 	; DL = drive number (0 based)
  6580 00002A4F E8BB030000          <1> 	call	GET_VEC
  6581                              <1> 	;push	bx
  6582 00002A54 53                  <1> 	push	ebx ; 21/02/2015
  6583                              <1> 	;add	bx,ax
  6584 00002A55 01C3                <1> 	add	ebx,eax
  6585                              <1> 	;; 05/01/2015
  6586 00002A57 8A25[A86A0000]      <1> 	mov	ah,[hf_m_s] ; drive number (0= master, 1= slave)
  6587                              <1> 	;;and 	ah,1 
  6588 00002A5D C0E404              <1> 	shl	ah,4
  6589 00002A60 80CCA0              <1> 	or	ah,0A0h  ; Drive/Head register - 10100000b (A0h)	
  6590                              <1> 	;mov	al,[es:bx]
  6591 00002A63 8A03                <1> 	mov	al,[ebx] ; 21/02/2015
  6592 00002A65 FEC8                <1> 	dec	al	 ; last head number 
  6593                              <1> 	;and	al,0Fh
  6594 00002A67 08E0                <1> 	or	al,ah	 ; lower 4 bits for head number
  6595                              <1> 	;
  6596 00002A69 C645FE91            <1> 	mov	byte [CMD_BLOCK+6],SET_PARM_CMD
  6597 00002A6D 8845FD              <1> 	mov	[CMD_BLOCK+5],al
  6598                              <1> 	;pop	bx
  6599 00002A70 5B                  <1> 	pop	ebx
  6600 00002A71 29C0                <1> 	sub	eax,eax ; 21/02/2015
  6601 00002A73 B004                <1> 	mov	al,4 ; Physical sec per track from translated HDPT
  6602 00002A75 803D[64700000]00    <1> 	cmp	byte [LBAMode],0
  6603 00002A7C 7702                <1> 	ja	short idrv1
  6604 00002A7E B00E                <1> 	mov	al,14 ; Physical sec per track from standard HDPT
  6605                              <1> idrv1:
  6606                              <1> 	;xor	ah,ah
  6607                              <1> 	;add	bx,ax
  6608 00002A80 01C3                <1> 	add	ebx,eax ; 21/02/2015
  6609                              <1> 	;mov	al,[es:bx]
  6610                              <1> 			; sector number
  6611 00002A82 8A03                <1> 	mov	al,[ebx]
  6612 00002A84 8845F9              <1> 	mov	[CMD_BLOCK+1],al
  6613 00002A87 28C0                <1> 	sub	al,al
  6614 00002A89 8845FB              <1> 	mov	[CMD_BLOCK+3],al  ; ZERO FLAGS
  6615 00002A8C E8C4010000          <1> 	call	COMMAND 	  ; TELL CONTROLLER
  6616 00002A91 750C                <1> 	jnz	short INIT_EXIT	  ; CONTROLLER BUSY ERROR
  6617 00002A93 E872020000          <1> 	call	NOT_BUSY	  ; WAIT FOR IT TO BE DONE
  6618 00002A98 7505                <1> 	jnz	short INIT_EXIT	  ; TIME OUT
  6619 00002A9A E8C3020000          <1> 	call	CHECK_STATUS
  6620                              <1> INIT_EXIT:
  6621 00002A9F C3                  <1> 	RETn
  6622                              <1> 
  6623                              <1> ;----------------------------------------
  6624                              <1> ;	READ LONG	     (AH = 0AH) :
  6625                              <1> ;----------------------------------------
  6626                              <1> 
  6627                              <1> RD_LONG:
  6628                              <1> 	;MOV	@CMD_BLOCK+6,READ_CMD OR ECC_MODE
  6629 00002AA0 C645FE22            <1>         mov     byte [CMD_BLOCK+6],READ_CMD + ECC_MODE 
  6630 00002AA4 E9E0000000          <1>         JMP     COMMANDI
  6631                              <1> 
  6632                              <1> ;----------------------------------------
  6633                              <1> ;	WRITE LONG	     (AH = 0BH) :
  6634                              <1> ;----------------------------------------
  6635                              <1> 
  6636                              <1> WR_LONG:
  6637                              <1> 	;MOV	@CMD_BLOCK+6,WRITE_CMD OR ECC_MODE
  6638 00002AA9 C645FE32            <1>         MOV     byte [CMD_BLOCK+6],WRITE_CMD + ECC_MODE
  6639 00002AAD E92C010000          <1>         JMP     COMMANDO
  6640                              <1> 
  6641                              <1> ;----------------------------------------
  6642                              <1> ;	SEEK		     (AH = 0CH) :
  6643                              <1> ;----------------------------------------
  6644                              <1> 
  6645                              <1> DISK_SEEK:
  6646 00002AB2 C645FE70            <1>         MOV     byte [CMD_BLOCK+6],SEEK_CMD
  6647 00002AB6 E89A010000          <1> 	CALL	COMMAND
  6648 00002ABB 751C                <1> 	JNZ	short DS_EXIT 		; CONTROLLER BUSY ERROR
  6649 00002ABD E80C020000          <1> 	CALL	_WAIT
  6650 00002AC2 7515                <1>         JNZ     DS_EXIT                 ; TIME OUT ON SEEK
  6651 00002AC4 E899020000          <1> 	CALL	CHECK_STATUS
  6652 00002AC9 803D[4F700000]40    <1>         CMP     byte [DISK_STATUS1],BAD_SEEK
  6653 00002AD0 7507                <1> 	JNE	short DS_EXIT
  6654 00002AD2 C605[4F700000]00    <1>         MOV     byte [DISK_STATUS1],0
  6655                              <1> DS_EXIT:
  6656 00002AD9 C3                  <1> 	RETn
  6657                              <1> 
  6658                              <1> ;----------------------------------------
  6659                              <1> ;	TEST DISK READY      (AH = 10H) :
  6660                              <1> ;----------------------------------------
  6661                              <1> 
  6662                              <1> TST_RDY:				; WAIT FOR CONTROLLER
  6663 00002ADA E82B020000          <1> 	CALL	NOT_BUSY
  6664 00002ADF 751C                <1> 	JNZ	short TR_EX
  6665 00002AE1 8A45FD              <1> 	MOV	AL,[CMD_BLOCK+5] 	; SELECT DRIVE
  6666 00002AE4 668B15[A46A0000]    <1> 	MOV	DX,[HF_PORT]
  6667 00002AEB 80C206              <1> 	add	dl,6
  6668 00002AEE EE                  <1> 	OUT	DX,AL
  6669 00002AEF E886020000          <1> 	CALL	CHECK_ST		; CHECK STATUS ONLY
  6670 00002AF4 7507                <1> 	JNZ	short TR_EX
  6671 00002AF6 C605[4F700000]00    <1> 	MOV	byte [DISK_STATUS1],0 	; WIPE OUT DATA CORRECTED ERROR
  6672                              <1> TR_EX:	
  6673 00002AFD C3                  <1> 	RETn
  6674                              <1> 
  6675                              <1> ;----------------------------------------
  6676                              <1> ;	RECALIBRATE	     (AH = 11H) :
  6677                              <1> ;----------------------------------------
  6678                              <1> 
  6679                              <1> HDISK_RECAL:
  6680 00002AFE C645FE10            <1>         MOV     byte [CMD_BLOCK+6],RECAL_CMD ; 10h, 16
  6681 00002B02 E84E010000          <1> 	CALL	COMMAND 		; START THE OPERATION
  6682 00002B07 7523                <1> 	JNZ	short RECAL_EXIT	; ERROR
  6683 00002B09 E8C0010000          <1> 	CALL	_WAIT			; WAIT FOR COMPLETION
  6684 00002B0E 7407                <1> 	JZ	short RECAL_X 		; TIME OUT ONE OK ?
  6685 00002B10 E8B9010000          <1> 	CALL	_WAIT			; WAIT FOR COMPLETION LONGER
  6686 00002B15 7515                <1> 	JNZ	short RECAL_EXIT	; TIME OUT TWO TIMES IS ERROR
  6687                              <1> RECAL_X:
  6688 00002B17 E846020000          <1> 	CALL	CHECK_STATUS
  6689 00002B1C 803D[4F700000]40    <1> 	CMP	byte [DISK_STATUS1],BAD_SEEK ; SEEK NOT COMPLETE
  6690 00002B23 7507                <1> 	JNE	short RECAL_EXIT	; IS OK
  6691 00002B25 C605[4F700000]00    <1> 	MOV	byte [DISK_STATUS1],0
  6692                              <1> RECAL_EXIT:
  6693 00002B2C 803D[4F700000]00    <1>         CMP     byte [DISK_STATUS1],0
  6694 00002B33 C3                  <1> 	RETn
  6695                              <1> 
  6696                              <1> ;----------------------------------------
  6697                              <1> ;      CONTROLLER DIAGNOSTIC (AH = 14H) :
  6698                              <1> ;----------------------------------------
  6699                              <1> 
  6700                              <1> CTLR_DIAGNOSTIC:
  6701 00002B34 FA                  <1>         CLI                             ; DISABLE INTERRUPTS WHILE CHANGING MASK
  6702 00002B35 E4A1                <1> 	IN	AL,INTB01		; TURN ON SECOND INTERRUPT CHIP
  6703                              <1> 	;AND	AL,0BFH
  6704 00002B37 243F                <1> 	and	al, 3Fh			; enable IRQ 14 & IRQ 15
  6705                              <1> 	;JMP	$+2
  6706                              <1> 	IODELAY
  2127 00002B39 EB00                <2>  jmp short $+2
  2128 00002B3B EB00                <2>  jmp short $+2
  6707 00002B3D E6A1                <1> 	OUT	INTB01,AL
  6708                              <1> 	IODELAY
  2127 00002B3F EB00                <2>  jmp short $+2
  2128 00002B41 EB00                <2>  jmp short $+2
  6709 00002B43 E421                <1> 	IN	AL,INTA01		; LET INTERRUPTS PASS THRU TO
  6710 00002B45 24FB                <1> 	AND	AL,0FBH 		;  SECOND CHIP
  6711                              <1> 	;JMP	$+2
  6712                              <1> 	IODELAY
  2127 00002B47 EB00                <2>  jmp short $+2
  2128 00002B49 EB00                <2>  jmp short $+2
  6713 00002B4B E621                <1> 	OUT	INTA01,AL
  6714 00002B4D FB                  <1> 	STI
  6715 00002B4E E8B7010000          <1> 	CALL	NOT_BUSY		; WAIT FOR CARD
  6716 00002B53 752B                <1> 	JNZ	short CD_ERR		; BAD CARD
  6717                              <1> 	;MOV	DX, HF_PORT+7
  6718 00002B55 668B15[A46A0000]    <1> 	mov	dx, [HF_PORT]
  6719 00002B5C 80C207              <1> 	add	dl, 7
  6720 00002B5F B090                <1> 	MOV	AL,DIAG_CMD		; START DIAGNOSE
  6721 00002B61 EE                  <1> 	OUT	DX,AL
  6722 00002B62 E8A3010000          <1> 	CALL	NOT_BUSY		; WAIT FOR IT TO COMPLETE
  6723 00002B67 B480                <1> 	MOV	AH,TIME_OUT
  6724 00002B69 7517                <1> 	JNZ	short CD_EXIT 		; TIME OUT ON DIAGNOSTIC
  6725                              <1> 	;MOV	DX,HF_PORT+1		; GET ERROR REGISTER
  6726 00002B6B 668B15[A46A0000]    <1> 	mov	dx, [HF_PORT]
  6727 00002B72 FEC2                <1> 	inc	dl
  6728 00002B74 EC                  <1> 	IN	AL,DX
  6729 00002B75 A2[46700000]        <1> 	MOV	[HF_ERROR],AL		; SAVE IT
  6730 00002B7A B400                <1> 	MOV	AH,0
  6731 00002B7C 3C01                <1> 	CMP	AL,1			; CHECK FOR ALL OK
  6732 00002B7E 7402                <1> 	JE	SHORT CD_EXIT
  6733 00002B80 B420                <1> CD_ERR: MOV	AH,BAD_CNTLR
  6734                              <1> CD_EXIT:
  6735 00002B82 8825[4F700000]      <1> 	MOV	[DISK_STATUS1],AH
  6736 00002B88 C3                  <1> 	RETn
  6737                              <1> 
  6738                              <1> ;----------------------------------------
  6739                              <1> ; COMMANDI				:
  6740                              <1> ;	REPEATEDLY INPUTS DATA TILL	:
  6741                              <1> ;	NSECTOR RETURNS ZERO		:
  6742                              <1> ;----------------------------------------
  6743                              <1> COMMANDI:
  6744 00002B89 E85A020000          <1> 	CALL	CHECK_DMA		; CHECK 64K BOUNDARY ERROR
  6745 00002B8E 724D                <1> 	JC	short CMD_ABORT
  6746                              <1> 	;MOV	DI,BX
  6747 00002B90 89DF                <1> 	mov	edi, ebx ; 21/02/2015
  6748 00002B92 E8BE000000          <1> 	CALL	COMMAND 		; OUTPUT COMMAND
  6749 00002B97 7544                <1> 	JNZ	short CMD_ABORT
  6750                              <1> CMD_I1:
  6751 00002B99 E830010000          <1> 	CALL	_WAIT			; WAIT FOR DATA REQUEST INTERRUPT
  6752 00002B9E 753D                <1> 	JNZ	short TM_OUT		; TIME OUT
  6753                              <1> 	;MOV	CX,256			; SECTOR SIZE IN WORDS
  6754 00002BA0 B900010000          <1> 	mov	ecx, 256 ; 21/02/2015	
  6755                              <1> 	;MOV	DX,HF_PORT
  6756 00002BA5 668B15[A46A0000]    <1> 	mov	dx,[HF_PORT]
  6757 00002BAC FA                  <1> 	CLI
  6758 00002BAD FC                  <1> 	CLD
  6759 00002BAE F3666D              <1> 	REP	INSW			; GET THE SECTOR
  6760 00002BB1 FB                  <1> 	STI
  6761 00002BB2 F645FE02            <1> 	TEST	byte [CMD_BLOCK+6],ECC_MODE ; CHECK FOR NORMAL INPUT
  6762 00002BB6 7419                <1> 	JZ	CMD_I3
  6763 00002BB8 E87A010000          <1> 	CALL	WAIT_DRQ		; WAIT FOR DATA REQUEST
  6764 00002BBD 721E                <1> 	JC	short TM_OUT
  6765                              <1> 	;MOV	DX,HF_PORT
  6766 00002BBF 668B15[A46A0000]    <1> 	mov	dx,[HF_PORT]
  6767                              <1> 	;MOV	CX,4			; GET ECC BYTES
  6768 00002BC6 B904000000          <1> 	mov 	ecx, 4 ; mov cx, 4 
  6769 00002BCB EC                  <1> CMD_I2: IN	AL,DX
  6770                              <1> 	;MOV	[ES:DI],AL		; GO SLOW FOR BOARD
  6771 00002BCC 8807                <1> 	mov 	[edi], al ; 21/02/2015
  6772 00002BCE 47                  <1> 	INC	eDI
  6773 00002BCF E2FA                <1> 	LOOP	CMD_I2
  6774 00002BD1 E88C010000          <1> CMD_I3: CALL	CHECK_STATUS
  6775 00002BD6 7505                <1> 	JNZ	short CMD_ABORT		; ERROR RETURNED
  6776 00002BD8 FE4DF9              <1> 	DEC	byte [CMD_BLOCK+1]	; CHECK FOR MORE
  6777 00002BDB 75BC                <1> 	JNZ	SHORT CMD_I1
  6778                              <1> CMD_ABORT:
  6779 00002BDD C3                  <1> TM_OUT: RETn
  6780                              <1> 
  6781                              <1> ;----------------------------------------
  6782                              <1> ; COMMANDO				:
  6783                              <1> ;	REPEATEDLY OUTPUTS DATA TILL	:
  6784                              <1> ;	NSECTOR RETURNS ZERO		:
  6785                              <1> ;----------------------------------------
  6786                              <1> COMMANDO:
  6787 00002BDE E805020000          <1> 	CALL	CHECK_DMA		; CHECK 64K BOUNDARY ERROR
  6788 00002BE3 72F8                <1> 	JC	short CMD_ABORT
  6789 00002BE5 89DE                <1> CMD_OF: MOV	eSI,eBX ; 21/02/2015
  6790 00002BE7 E869000000          <1> 	CALL	COMMAND 		; OUTPUT COMMAND
  6791 00002BEC 75EF                <1> 	JNZ	short CMD_ABORT
  6792 00002BEE E844010000          <1> 	CALL	WAIT_DRQ		; WAIT FOR DATA REQUEST
  6793 00002BF3 72E8                <1> 	JC	short TM_OUT			; TOO LONG
  6794                              <1> CMD_O1: ;PUSH	DS
  6795                              <1> 	;PUSH	ES			; MOVE ES TO DS
  6796                              <1> 	;POP	DS
  6797                              <1> 	;MOV	CX,256			; PUT THE DATA OUT TO THE CARD
  6798                              <1> 	;MOV	DX,HF_PORT
  6799                              <1> 	; 01/02/2015
  6800 00002BF5 668B15[A46A0000]    <1> 	mov	dx, [HF_PORT]
  6801                              <1> 	;push	es
  6802                              <1> 	;pop	ds
  6803                              <1> 	;mov	cx, 256
  6804 00002BFC B900010000          <1> 	mov	ecx, 256 ; 21/02/2015
  6805 00002C01 FA                  <1> 	CLI
  6806 00002C02 FC                  <1> 	CLD
  6807 00002C03 F3666F              <1> 	REP	OUTSW
  6808 00002C06 FB                  <1> 	STI
  6809                              <1> 	;POP	DS			; RESTORE DS
  6810 00002C07 F645FE02            <1> 	TEST	byte [CMD_BLOCK+6],ECC_MODE ; CHECK FOR NORMAL OUTPUT
  6811 00002C0B 7419                <1> 	JZ	short CMD_O3
  6812 00002C0D E825010000          <1> 	CALL	WAIT_DRQ		; WAIT FOR DATA REQUEST
  6813 00002C12 72C9                <1> 	JC	short TM_OUT
  6814                              <1> 	;MOV	DX,HF_PORT
  6815 00002C14 668B15[A46A0000]    <1> 	mov	dx, [HF_PORT]
  6816                              <1> 	;MOV	CX,4			; OUTPUT THE ECC BYTES
  6817 00002C1B B904000000          <1> 	mov	ecx, 4  ; mov cx, 4
  6818                              <1> CMD_O2: ;MOV	AL,[ES:SI]
  6819 00002C20 8A06                <1> 	mov	al, [esi]
  6820 00002C22 EE                  <1> 	OUT	DX,AL
  6821 00002C23 46                  <1> 	INC	eSI
  6822 00002C24 E2FA                <1> 	LOOP	CMD_O2
  6823                              <1> CMD_O3:
  6824 00002C26 E8A3000000          <1> 	CALL	_WAIT			; WAIT FOR SECTOR COMPLETE INTERRUPT
  6825 00002C2B 75B0                <1> 	JNZ	short TM_OUT		; ERROR RETURNED
  6826 00002C2D E830010000          <1> 	CALL	CHECK_STATUS
  6827 00002C32 75A9                <1> 	JNZ	short CMD_ABORT
  6828 00002C34 F605[45700000]08    <1> 	TEST	byte [HF_STATUS],ST_DRQ	; CHECK FOR MORE
  6829 00002C3B 75B8                <1> 	JNZ	SHORT CMD_O1
  6830                              <1> 	;MOV	DX,HF_PORT+2		; CHECK RESIDUAL SECTOR COUNT
  6831 00002C3D 668B15[A46A0000]    <1> 	mov	dx, [HF_PORT]
  6832                              <1> 	;add	dl, 2
  6833 00002C44 FEC2                <1> 	inc	dl
  6834 00002C46 FEC2                <1> 	inc	dl
  6835 00002C48 EC                  <1> 	IN	AL,DX			;
  6836 00002C49 A8FF                <1> 	TEST	AL,0FFH 		;
  6837 00002C4B 7407                <1> 	JZ	short CMD_O4			; COUNT = 0  OK
  6838 00002C4D C605[4F700000]BB    <1> 	MOV	byte [DISK_STATUS1],UNDEF_ERR 
  6839                              <1> 					; OPERATION ABORTED - PARTIAL TRANSFER
  6840                              <1> CMD_O4:
  6841 00002C54 C3                  <1> 	RETn
  6842                              <1> 
  6843                              <1> ;--------------------------------------------------------
  6844                              <1> ; COMMAND						:
  6845                              <1> ;	THIS ROUTINE OUTPUTS THE COMMAND BLOCK		:
  6846                              <1> ; OUTPUT						:
  6847                              <1> ;	BL = STATUS					:
  6848                              <1> ;	BH = ERROR REGISTER				:
  6849                              <1> ;--------------------------------------------------------
  6850                              <1> 
  6851                              <1> COMMAND:
  6852 00002C55 53                  <1> 	PUSH	eBX			; WAIT FOR SEEK COMPLETE AND READY
  6853                              <1> 	;;MOV	CX,DELAY_2		; SET INITIAL DELAY BEFORE TEST
  6854                              <1> COMMAND1:
  6855                              <1> 	;;PUSH	CX			; SAVE LOOP COUNT
  6856 00002C56 E87FFEFFFF          <1> 	CALL	TST_RDY 		; CHECK DRIVE READY
  6857                              <1> 	;;POP	CX
  6858 00002C5B 7419                <1> 	JZ	short COMMAND2		; DRIVE IS READY
  6859 00002C5D 803D[4F700000]80    <1>         CMP     byte [DISK_STATUS1],TIME_OUT ; TST_RDY TIMED OUT--GIVE UP
  6860                              <1> 	;JZ	short CMD_TIMEOUT
  6861                              <1> 	;;LOOP	COMMAND1		; KEEP TRYING FOR A WHILE
  6862                              <1> 	;JMP	SHORT COMMAND4		; ITS NOT GOING TO GET READY
  6863 00002C64 7507                <1> 	jne	short COMMAND4
  6864                              <1> CMD_TIMEOUT:
  6865 00002C66 C605[4F700000]20    <1> 	MOV	byte [DISK_STATUS1],BAD_CNTLR
  6866                              <1> COMMAND4:
  6867 00002C6D 5B                  <1> 	POP	eBX
  6868 00002C6E 803D[4F700000]00    <1>         CMP     byte [DISK_STATUS1],0   ; SET CONDITION CODE FOR CALLER
  6869 00002C75 C3                  <1> 	RETn
  6870                              <1> COMMAND2:
  6871 00002C76 5B                  <1> 	POP	eBX
  6872 00002C77 57                  <1> 	PUSH	eDI
  6873 00002C78 C605[47700000]00    <1> 	MOV	byte [HF_INT_FLAG],0	; RESET INTERRUPT FLAG
  6874 00002C7F FA                  <1> 	CLI				; INHIBIT INTERRUPTS WHILE CHANGING MASK
  6875 00002C80 E4A1                <1> 	IN	AL,INTB01		; TURN ON SECOND INTERRUPT CHIP
  6876                              <1> 	;AND	AL,0BFH
  6877 00002C82 243F                <1> 	and	al, 3Fh			; Enable IRQ 14 & 15
  6878                              <1> 	;JMP	$+2
  6879                              <1> 	IODELAY
  2127 00002C84 EB00                <2>  jmp short $+2
  2128 00002C86 EB00                <2>  jmp short $+2
  6880 00002C88 E6A1                <1> 	OUT	INTB01,AL
  6881 00002C8A E421                <1> 	IN	AL,INTA01		; LET INTERRUPTS PASS THRU TO
  6882 00002C8C 24FB                <1> 	AND	AL,0FBH 		;  SECOND CHIP
  6883                              <1> 	;JMP	$+2
  6884                              <1> 	IODELAY
  2127 00002C8E EB00                <2>  jmp short $+2
  2128 00002C90 EB00                <2>  jmp short $+2
  6885 00002C92 E621                <1> 	OUT	INTA01,AL
  6886 00002C94 FB                  <1> 	STI
  6887 00002C95 31FF                <1> 	XOR	eDI,eDI			; INDEX THE COMMAND TABLE
  6888                              <1> 	;MOV	DX,HF_PORT+1		; DISK ADDRESS
  6889 00002C97 668B15[A46A0000]    <1> 	mov	dx, [HF_PORT]
  6890 00002C9E FEC2                <1> 	inc	dl
  6891 00002CA0 F605[51700000]C0    <1> 	TEST	byte [CONTROL_BYTE],0C0H ; CHECK FOR RETRY SUPPRESSION
  6892 00002CA7 7411                <1> 	JZ	short COMMAND3
  6893 00002CA9 8A45FE              <1> 	MOV	AL, [CMD_BLOCK+6] 	; YES-GET OPERATION CODE
  6894 00002CAC 24F0                <1> 	AND	AL,0F0H 		; GET RID OF MODIFIERS
  6895 00002CAE 3C20                <1> 	CMP	AL,20H			; 20H-40H IS READ, WRITE, VERIFY
  6896 00002CB0 7208                <1> 	JB	short COMMAND3
  6897 00002CB2 3C40                <1> 	CMP	AL,40H
  6898 00002CB4 7704                <1> 	JA	short COMMAND3
  6899 00002CB6 804DFE01            <1> 	OR	byte [CMD_BLOCK+6],NO_RETRIES 
  6900                              <1> 					; VALID OPERATION FOR RETRY SUPPRESS
  6901                              <1> COMMAND3:
  6902 00002CBA 8A443DF8            <1> 	MOV	AL,[CMD_BLOCK+eDI]	; GET THE COMMAND STRING BYTE
  6903 00002CBE EE                  <1> 	OUT	DX,AL			; GIVE IT TO CONTROLLER
  6904                              <1> 	IODELAY
  2127 00002CBF EB00                <2>  jmp short $+2
  2128 00002CC1 EB00                <2>  jmp short $+2
  6905 00002CC3 47                  <1> 	INC	eDI			; NEXT BYTE IN COMMAND BLOCK
  6906 00002CC4 6642                <1> 	INC	DX			; NEXT DISK ADAPTER REGISTER
  6907 00002CC6 6683FF07            <1> 	cmp	di, 7	; 1/1/2015	; ALL DONE?
  6908 00002CCA 75EE                <1> 	JNZ	short COMMAND3		; NO--GO DO NEXT ONE
  6909 00002CCC 5F                  <1> 	POP	eDI
  6910 00002CCD C3                  <1> 	RETn				; ZERO FLAG IS SET
  6911                              <1> 
  6912                              <1> ;CMD_TIMEOUT:
  6913                              <1> ;	MOV	byte [DISK_STATUS1],BAD_CNTLR
  6914                              <1> ;COMMAND4:
  6915                              <1> ;	POP	BX
  6916                              <1> ;	CMP	[DISK_STATUS1],0 	; SET CONDITION CODE FOR CALLER
  6917                              <1> ;	RETn
  6918                              <1> 
  6919                              <1> ;----------------------------------------
  6920                              <1> ;	WAIT FOR INTERRUPT		:
  6921                              <1> ;----------------------------------------
  6922                              <1> ;WAIT:
  6923                              <1> _WAIT:
  6924 00002CCE FB                  <1> 	STI				; MAKE SURE INTERRUPTS ARE ON
  6925                              <1> 	;SUB	CX,CX			; SET INITIAL DELAY BEFORE TEST
  6926                              <1> 	;CLC
  6927                              <1> 	;MOV	AX,9000H		; DEVICE WAIT INTERRUPT
  6928                              <1> 	;INT	15H
  6929                              <1> 	;JC	WT2			; DEVICE TIMED OUT
  6930                              <1> 	;MOV	BL,DELAY_1		; SET DELAY COUNT
  6931                              <1> 
  6932                              <1> 	;mov	bl, WAIT_HDU_INT_HI
  6933                              <1> 	;; 21/02/2015
  6934                              <1> 	;;mov	bl, WAIT_HDU_INT_HI + 1
  6935                              <1> 	;;mov	cx, WAIT_HDU_INT_LO
  6936 00002CCF B915160500          <1> 	mov	ecx, WAIT_HDU_INT_LH
  6937                              <1> 					; (AWARD BIOS -> WAIT_FOR_MEM)
  6938                              <1> ;-----	WAIT LOOP
  6939                              <1> 
  6940                              <1> WT1:	
  6941                              <1> 	;TEST	byte [HF_INT_FLAG],80H	; TEST FOR INTERRUPT
  6942 00002CD4 F605[47700000]C0    <1> 	test 	byte [HF_INT_FLAG],0C0h
  6943                              <1> 	;LOOPZ	WT1
  6944 00002CDB 7517                <1> 	JNZ	short WT3		; INTERRUPT--LETS GO
  6945                              <1> 	;DEC	BL
  6946                              <1> 	;JNZ	short WT1		; KEEP TRYING FOR A WHILE
  6947                              <1> 
  6948                              <1> WT1_hi:
  6949 00002CDD E461                <1> 	in	al, SYS1 ; 61h (PORT_B)	; wait for lo to hi
  6950 00002CDF A810                <1> 	test	al, 10h			; transition on memory
  6951 00002CE1 75FA                <1> 	jnz	short WT1_hi		; refresh.
  6952                              <1> WT1_lo:
  6953 00002CE3 E461                <1> 	in	al, SYS1 		; 061h (PORT_B)	
  6954 00002CE5 A810                <1> 	test	al, 10h			
  6955 00002CE7 74FA                <1> 	jz	short WT1_lo
  6956 00002CE9 E2E9                <1> 	loop	WT1
  6957                              <1> 	;;or	bl, bl
  6958                              <1> 	;;jz	short WT2	
  6959                              <1> 	;;dec	bl
  6960                              <1> 	;;jmp	short WT1
  6961                              <1> 	;dec	bl
  6962                              <1> 	;jnz	short WT1	
  6963                              <1> 
  6964 00002CEB C605[4F700000]80    <1> WT2:	MOV	byte [DISK_STATUS1],TIME_OUT ; REPORT TIME OUT ERROR
  6965 00002CF2 EB0E                <1> 	JMP	SHORT WT4
  6966 00002CF4 C605[4F700000]00    <1> WT3:	MOV	byte [DISK_STATUS1],0
  6967 00002CFB C605[47700000]00    <1> 	MOV	byte [HF_INT_FLAG],0
  6968 00002D02 803D[4F700000]00    <1> WT4:	CMP	byte [DISK_STATUS1],0 	; SET CONDITION CODE FOR CALLER
  6969 00002D09 C3                  <1> 	RETn
  6970                              <1> 
  6971                              <1> ;----------------------------------------
  6972                              <1> ;	WAIT FOR CONTROLLER NOT BUSY	:
  6973                              <1> ;----------------------------------------
  6974                              <1> NOT_BUSY:
  6975 00002D0A FB                  <1> 	STI				; MAKE SURE INTERRUPTS ARE ON
  6976                              <1> 	;PUSH	eBX
  6977                              <1> 	;SUB	CX,CX			; SET INITIAL DELAY BEFORE TEST
  6978 00002D0B 668B15[A46A0000]    <1> 	mov	DX, [HF_PORT]
  6979 00002D12 80C207              <1> 	add	dl, 7			; Status port (HF_PORT+7)
  6980                              <1> 	;MOV	BL,DELAY_1
  6981                              <1> 					; wait for 10 seconds
  6982                              <1> 	;mov 	cx, WAIT_HDU_INT_LO	; 1615h
  6983                              <1> 	;;mov 	bl, WAIT_HDU_INT_HI	;   05h
  6984                              <1> 	;mov	bl, WAIT_HDU_INT_HI + 1
  6985 00002D15 B915160500          <1> 	mov	ecx, WAIT_HDU_INT_LH  ; 21/02/2015
  6986                              <1> 	;
  6987                              <1> ;;      mov     byte [wait_count], 0    ; Reset wait counter
  6988                              <1> NB1:	
  6989 00002D1A EC                  <1> 	IN	AL,DX			; CHECK STATUS
  6990                              <1> 	;TEST	AL,ST_BUSY
  6991 00002D1B 2480                <1> 	and	al, ST_BUSY
  6992                              <1> 	;LOOPNZ	NB1
  6993 00002D1D 7410                <1> 	JZ	short NB2		; NOT BUSY--LETS GO
  6994                              <1> 	;DEC	BL			
  6995                              <1> 	;JNZ	short NB1		; KEEP TRYING FOR A WHILE
  6996                              <1> 
  6997 00002D1F E461                <1> NB1_hi: IN	AL,SYS1			; wait for hi to lo
  6998 00002D21 A810                <1> 	TEST	AL,010H			; transition on memory
  6999 00002D23 75FA                <1> 	JNZ	SHORT NB1_hi		; refresh.
  7000 00002D25 E461                <1> NB1_lo: IN	AL,SYS1
  7001 00002D27 A810                <1> 	TEST	AL,010H
  7002 00002D29 74FA                <1> 	JZ	short NB1_lo
  7003 00002D2B E2ED                <1> 	LOOP	NB1
  7004                              <1> 	;dec	bl
  7005                              <1> 	;jnz	short NB1
  7006                              <1> 	;
  7007                              <1> ;;      cmp     byte [wait_count], 182  ; 10 seconds (182 timer ticks)
  7008                              <1> ;;	jb	short NB1
  7009                              <1> 	;
  7010                              <1> 	;MOV	[DISK_STATUS1],TIME_OUT	; REPORT TIME OUT ERROR
  7011                              <1> 	;JMP	SHORT NB3
  7012 00002D2D B080                <1> 	mov	al, TIME_OUT
  7013                              <1> NB2:	
  7014                              <1> 	;MOV	byte [DISK_STATUS1],0
  7015                              <1> ;NB3:	
  7016                              <1> 	;POP	eBX
  7017 00002D2F A2[4F700000]        <1> 	mov	[DISK_STATUS1], al	;;; will be set after return
  7018                              <1> 	;CMP	byte [DISK_STATUS1],0 	; SET CONDITION CODE FOR CALLER
  7019 00002D34 08C0                <1> 	or	al, al			; (zf = 0 --> timeout)
  7020 00002D36 C3                  <1> 	RETn
  7021                              <1> 
  7022                              <1> ;----------------------------------------
  7023                              <1> ;	WAIT FOR DATA REQUEST		:
  7024                              <1> ;----------------------------------------
  7025                              <1> WAIT_DRQ:
  7026                              <1> 	;MOV	CX,DELAY_3
  7027                              <1> 	;MOV	DX,HF_PORT+7
  7028 00002D37 668B15[A46A0000]    <1> 	mov	dx, [HF_PORT]
  7029 00002D3E 80C207              <1> 	add	dl, 7
  7030                              <1> 	;;MOV	bl, WAIT_HDU_DRQ_HI	; 0
  7031                              <1> 	;MOV	cx, WAIT_HDU_DRQ_LO	; 1000 (30 milli seconds)
  7032                              <1> 					; (but it is written as 2000
  7033                              <1> 					; micro seconds in ATORGS.ASM file
  7034                              <1> 					; of Award Bios - 1999, D1A0622)
  7035 00002D41 B9E8030000          <1> 	mov 	ecx, WAIT_HDU_DRQ_LH ; 21/02/2015 
  7036 00002D46 EC                  <1> WQ_1:	IN	AL,DX			; GET STATUS
  7037 00002D47 A808                <1> 	TEST	AL,ST_DRQ		; WAIT FOR DRQ
  7038 00002D49 7516                <1> 	JNZ	short WQ_OK
  7039                              <1> 	;LOOP	WQ_1			; KEEP TRYING FOR A SHORT WHILE
  7040                              <1> WQ_hi:	
  7041 00002D4B E461                <1> 	IN	AL,SYS1			; wait for hi to lo
  7042 00002D4D A810                <1> 	TEST	AL,010H			; transition on memory
  7043 00002D4F 75FA                <1> 	JNZ	SHORT WQ_hi		; refresh.
  7044 00002D51 E461                <1> WQ_lo:  IN      AL,SYS1
  7045 00002D53 A810                <1> 	TEST	AL,010H
  7046 00002D55 74FA                <1> 	JZ	SHORT WQ_lo
  7047 00002D57 E2ED                <1> 	LOOP	WQ_1
  7048                              <1> 
  7049 00002D59 C605[4F700000]80    <1>         MOV     byte [DISK_STATUS1],TIME_OUT ; ERROR
  7050 00002D60 F9                  <1> 	STC
  7051                              <1> WQ_OK:
  7052 00002D61 C3                  <1> 	RETn
  7053                              <1> ;WQ_OK:	;CLC
  7054                              <1> ;	RETn
  7055                              <1> 
  7056                              <1> ;----------------------------------------
  7057                              <1> ;	CHECK FIXED DISK STATUS 	:
  7058                              <1> ;----------------------------------------
  7059                              <1> CHECK_STATUS:
  7060 00002D62 E813000000          <1> 	CALL	CHECK_ST		; CHECK THE STATUS BYTE
  7061 00002D67 7509                <1> 	JNZ	short CHECK_S1		; AN ERROR WAS FOUND
  7062 00002D69 A801                <1> 	TEST	AL,ST_ERROR		; WERE THERE ANY OTHER ERRORS
  7063 00002D6B 7405                <1> 	JZ	short CHECK_S1		; NO ERROR REPORTED
  7064 00002D6D E847000000          <1> 	CALL	CHECK_ER		; ERROR REPORTED
  7065                              <1> CHECK_S1:
  7066 00002D72 803D[4F700000]00    <1> 	CMP	byte [DISK_STATUS1],0 	; SET STATUS FOR CALLER
  7067 00002D79 C3                  <1> 	RETn
  7068                              <1> 
  7069                              <1> ;----------------------------------------
  7070                              <1> ;	CHECK FIXED DISK STATUS BYTE	:
  7071                              <1> ;----------------------------------------
  7072                              <1> CHECK_ST:
  7073                              <1> 	;MOV	DX,HF_PORT+7		; GET THE STATUS
  7074 00002D7A 668B15[A46A0000]    <1> 	mov	dx, [HF_PORT]
  7075 00002D81 80C207              <1> 	add	dl, 7
  7076 00002D84 EC                  <1> 	IN	AL,DX
  7077 00002D85 A2[45700000]        <1> 	MOV	[HF_STATUS],AL
  7078 00002D8A B400                <1> 	MOV	AH,0
  7079 00002D8C A880                <1> 	TEST	AL,ST_BUSY		; IF STILL BUSY
  7080 00002D8E 751A                <1> 	JNZ	short CKST_EXIT		;  REPORT OK
  7081 00002D90 B4CC                <1> 	MOV	AH,WRITE_FAULT
  7082 00002D92 A820                <1> 	TEST	AL,ST_WRT_FLT		; CHECK FOR WRITE FAULT
  7083 00002D94 7514                <1> 	JNZ	short CKST_EXIT
  7084 00002D96 B4AA                <1> 	MOV	AH,NOT_RDY
  7085 00002D98 A840                <1> 	TEST	AL,ST_READY		; CHECK FOR NOT READY
  7086 00002D9A 740E                <1> 	JZ	short CKST_EXIT
  7087 00002D9C B440                <1> 	MOV	AH,BAD_SEEK
  7088 00002D9E A810                <1> 	TEST	AL,ST_SEEK_COMPL	; CHECK FOR SEEK NOT COMPLETE
  7089 00002DA0 7408                <1> 	JZ	short CKST_EXIT
  7090 00002DA2 B411                <1> 	MOV	AH,DATA_CORRECTED
  7091 00002DA4 A804                <1> 	TEST	AL,ST_CORRCTD		; CHECK FOR CORRECTED ECC
  7092 00002DA6 7502                <1> 	JNZ	short CKST_EXIT
  7093 00002DA8 B400                <1> 	MOV	AH,0
  7094                              <1> CKST_EXIT:
  7095 00002DAA 8825[4F700000]      <1> 	MOV	[DISK_STATUS1],AH	; SET ERROR FLAG
  7096 00002DB0 80FC11              <1> 	CMP	AH,DATA_CORRECTED	; KEEP GOING WITH DATA CORRECTED
  7097 00002DB3 7403                <1> 	JZ	short CKST_EX1
  7098 00002DB5 80FC00              <1> 	CMP	AH,0
  7099                              <1> CKST_EX1:
  7100 00002DB8 C3                  <1> 	RETn
  7101                              <1> 
  7102                              <1> ;----------------------------------------
  7103                              <1> ;	CHECK FIXED DISK ERROR REGISTER :
  7104                              <1> ;----------------------------------------
  7105                              <1> CHECK_ER:
  7106                              <1> 	;MOV	DX, HF_PORT+1		; GET THE ERROR REGISTER
  7107 00002DB9 668B15[A46A0000]    <1> 	mov	dx, [HF_PORT]		;
  7108 00002DC0 FEC2                <1> 	inc	dl
  7109 00002DC2 EC                  <1> 	IN	AL,DX
  7110 00002DC3 A2[46700000]        <1> 	MOV	[HF_ERROR],AL
  7111 00002DC8 53                  <1> 	PUSH	eBX ; 21/02/2015
  7112 00002DC9 B908000000          <1> 	MOV	eCX,8			; TEST ALL 8 BITS
  7113 00002DCE D0E0                <1> CK1:	SHL	AL,1			; MOVE NEXT ERROR BIT TO CARRY
  7114 00002DD0 7202                <1> 	JC	short CK2		; FOUND THE ERROR
  7115 00002DD2 E2FA                <1> 	LOOP	CK1			; KEEP TRYING
  7116 00002DD4 BB[986A0000]        <1> CK2:	MOV	eBX, ERR_TBL		; COMPUTE ADDRESS OF
  7117 00002DD9 01CB                <1> 	ADD	eBX,eCX			; ERROR CODE
  7118                              <1> 	;;MOV	AH,BYTE [CS:BX]		; GET ERROR CODE
  7119                              <1> 	;mov	ah, [bx]
  7120 00002DDB 8A23                <1> 	mov	ah, [ebx] ; 21/02/2015	
  7121 00002DDD 8825[4F700000]      <1> CKEX:	MOV	[DISK_STATUS1],AH	; SAVE ERROR CODE
  7122 00002DE3 5B                  <1> 	POP	eBX
  7123 00002DE4 80FC00              <1> 	CMP	AH,0
  7124 00002DE7 C3                  <1> 	RETn
  7125                              <1> 
  7126                              <1> ;--------------------------------------------------------
  7127                              <1> ; CHECK_DMA						:
  7128                              <1> ;  -CHECK ES:BX AND # SECTORS TO MAKE SURE THAT IT WILL :
  7129                              <1> ;   FIT WITHOUT SEGMENT OVERFLOW.			:
  7130                              <1> ;  -ES:BX HAS BEEN REVISED TO THE FORMAT SSSS:000X	:
  7131                              <1> ;  -OK IF # SECTORS < 80H (7FH IF LONG READ OR WRITE)	:
  7132                              <1> ;  -OK IF # SECTORS = 80H (7FH) AND BX <= 00H (04H)	:
  7133                              <1> ;  -ERROR OTHERWISE					:
  7134                              <1> ;--------------------------------------------------------
  7135                              <1> CHECK_DMA:
  7136                              <1> 	;PUSH	AX			; SAVE REGISTERS
  7137                              <1> 	; 05/12/2021
  7138 00002DE8 50                  <1> 	push	eax
  7139 00002DE9 66B80080            <1> 	MOV	AX,8000H		; AH = MAX # SECTORS AL = MAX OFFSET
  7140 00002DED F645FE02            <1>         TEST    byte [CMD_BLOCK+6],ECC_MODE
  7141 00002DF1 7404                <1> 	JZ	short CKD1
  7142 00002DF3 66B8047F            <1> 	MOV	AX,7F04H		; ECC IS 4 MORE BYTES
  7143 00002DF7 3A65F9              <1> CKD1:	CMP	AH, [CMD_BLOCK+1] 	; NUMBER OF SECTORS
  7144 00002DFA 7706                <1> 	JA	short CKDOK		; IT WILL FIT
  7145 00002DFC 7207                <1> 	JB	short CKDERR		; TOO MANY
  7146 00002DFE 38D8                <1> 	CMP	AL,BL			; CHECK OFFSET ON MAX SECTORS
  7147 00002E00 7203                <1> 	JB	short CKDERR		; ERROR
  7148 00002E02 F8                  <1> CKDOK:	CLC				; CLEAR CARRY
  7149                              <1> 	;POP	AX
  7150                              <1> 	; 05/12/2021
  7151 00002E03 58                  <1> 	pop	eax
  7152 00002E04 C3                  <1> 	RETn				; NORMAL RETURN
  7153 00002E05 F9                  <1> CKDERR: STC				; INDICATE ERROR
  7154 00002E06 C605[4F700000]09    <1>         MOV     byte [DISK_STATUS1],DMA_BOUNDARY
  7155                              <1> 	;POP	AX
  7156                              <1> 	; 05/12/2021
  7157 00002E0D 58                  <1> 	pop	eax	
  7158 00002E0E C3                  <1> 	RETn
  7159                              <1> 
  7160                              <1> ;----------------------------------------
  7161                              <1> ;	SET UP ES:BX-> DISK PARMS	:
  7162                              <1> ;----------------------------------------
  7163                              <1> 					
  7164                              <1> ; INPUT -> DL = 0 based drive number
  7165                              <1> ; OUTPUT -> ES:BX = disk parameter table address
  7166                              <1> 
  7167                              <1> GET_VEC:
  7168                              <1> 	;SUB	AX,AX			; GET DISK PARAMETER ADDRESS
  7169                              <1> 	;MOV	ES,AX
  7170                              <1> 	;TEST	DL,1
  7171                              <1> 	;JZ	short GV_0
  7172                              <1> ;	LES	BX,[HF1_TBL_VEC] 	; ES:BX -> DRIVE PARAMETERS
  7173                              <1> ;	JMP	SHORT GV_EXIT
  7174                              <1> ;GV_0:
  7175                              <1> ;	LES	BX,[HF_TBL_VEC]		; ES:BX -> DRIVE PARAMETERS
  7176                              <1> ;
  7177                              <1> 	;xor	bh, bh
  7178 00002E0F 31DB                <1> 	xor	ebx, ebx
  7179 00002E11 88D3                <1> 	mov	bl, dl
  7180                              <1> 	;;02/01/2015
  7181                              <1> 	;;shl	bl, 1			; port address offset
  7182                              <1> 	;;mov	ax, [bx+hd_ports]	; Base port address (1F0h, 170h)
  7183                              <1> 	;;shl	bl, 1			; dpt pointer offset
  7184 00002E13 C0E302              <1> 	shl	bl, 2	;;
  7185                              <1> 	;add	bx, HF_TBL_VEC		; Disk parameter table pointer
  7186 00002E16 81C3[54700000]      <1> 	add	ebx, HF_TBL_VEC ; 21/02/2015
  7187                              <1> 	;push	word [bx+2]		; dpt segment
  7188                              <1> 	;pop	es
  7189                              <1> 	;mov	bx, [bx]		; dpt offset
  7190 00002E1C 8B1B                <1> 	mov	ebx, [ebx]		
  7191                              <1> ;GV_EXIT:
  7192 00002E1E C3                  <1> 	RETn
  7193                              <1> 
  7194                              <1> 	; 05/12/2021 - Retro UNIX 386 v1.2
  7195                              <1> hdc1_int: ; 21/02/2015
  7196                              <1> ;--- HARDWARE INT 76H -- ( IRQ LEVEL  14 ) ----------------------
  7197                              <1> ;								:
  7198                              <1> ;	FIXED DISK INTERRUPT ROUTINE				:
  7199                              <1> ;								:
  7200                              <1> ;----------------------------------------------------------------
  7201                              <1> 
  7202                              <1> ; 22/12/2014
  7203                              <1> ; IBM PC-XT Model 286 System BIOS Source Code - DISK.ASM (HD_INT)
  7204                              <1> ;	 '11/15/85'
  7205                              <1> ; AWARD BIOS 1999 (D1A0622) 
  7206                              <1> ;	Source Code - ATORGS.ASM (INT_HDISK, INT_HDISK1)
  7207                              <1> 
  7208                              <1> ;int_76h:
  7209                              <1> HD_INT:
  7210                              <1> 	;push	ax
  7211                              <1> 	; 05/12/2021
  7212 00002E1F 50                  <1> 	push	eax
  7213 00002E20 1E                  <1> 	push	ds
  7214                              <1> 	;CALL	DDS
  7215                              <1> 	; 21/02/2015 (32 bit, 386 pm modification)
  7216 00002E21 66B81000            <1> 	mov	ax, KDATA
  7217 00002E25 8ED8                <1> 	mov 	ds, ax
  7218                              <1> 	;
  7219                              <1> 	;;MOV	@HF_INT_FLAG,0FFH	; ALL DONE
  7220                              <1>         ;mov     byte [CS:HF_INT_FLAG], 0FFh
  7221 00002E27 C605[47700000]FF    <1> 	mov	byte [HF_INT_FLAG], 0FFh
  7222                              <1> 	;
  7223                              <1> 	;push	dx
  7224                              <1> 	; 05/12/2021
  7225 00002E2E 52                  <1> 	push	edx
  7226 00002E2F 66BAF701            <1> 	mov	dx, HDC1_BASEPORT+7	; Status Register (1F7h)
  7227                              <1> 					; Clear Controller
  7228                              <1> Clear_IRQ1415:				; (Award BIOS - 1999)
  7229 00002E33 EC                  <1> 	in	al, dx			;
  7230                              <1> 	;pop	dx
  7231                              <1> 	; 05/12/2021
  7232 00002E34 5A                  <1> 	pop	edx
  7233                              <1> 	NEWIODELAY
  2132 00002E35 E6EB                <2>  out 0EBh,al
  7234                              <1> 	;
  7235 00002E37 B020                <1> 	MOV	AL,EOI			; NON-SPECIFIC END OF INTERRUPT
  7236 00002E39 E6A0                <1> 	OUT	INTB00,AL		; FOR CONTROLLER #2
  7237                              <1> 	;JMP	$+2			; WAIT
  7238                              <1> 	NEWIODELAY
  2132 00002E3B E6EB                <2>  out 0EBh,al
  7239 00002E3D E620                <1> 	OUT	INTA00,AL		; FOR CONTROLLER #1
  7240 00002E3F 1F                  <1> 	pop	ds
  7241                              <1> 	;STI				; RE-ENABLE INTERRUPTS
  7242                              <1> 	;MOV	AX,9100H		; DEVICE POST
  7243                              <1> 	;INT	15H			;  INTERRUPT
  7244                              <1> irq15_iret: ; 25/02/2015
  7245                              <1> 	;pop	ax
  7246                              <1> 	; 05/12/2021
  7247 00002E40 58                  <1> 	pop	eax
  7248 00002E41 CF                  <1> 	iretd			; RETURN FROM INTERRUPT
  7249                              <1> 
  7250                              <1> 	; 05/12/2021 - Retro UNIX 386 v1.2
  7251                              <1> hdc2_int: ; 21/02/2015
  7252                              <1> ;++++ HARDWARE INT 77H ++ ( IRQ LEVEL  15 ) +++++++++++++++++++++
  7253                              <1> ;								:
  7254                              <1> ;	FIXED DISK INTERRUPT ROUTINE				:
  7255                              <1> ;								:
  7256                              <1> ;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  7257                              <1> 
  7258                              <1> ;int_77h:
  7259                              <1> HD1_INT:
  7260                              <1> 	;push	ax
  7261                              <1> 	; 05/12/2021
  7262 00002E42 50                  <1> 	push	eax
  7263                              <1> 	; Check if that is a spurious IRQ (from slave PIC)
  7264                              <1> 	; 25/02/2015 (source: http://wiki.osdev.org/8259_PIC)
  7265 00002E43 B00B                <1> 	mov	al, 0Bh  ; In-Service Register
  7266 00002E45 E6A0                <1> 	out	0A0h, al
  7267 00002E47 EB00                <1>         jmp short $+2
  7268 00002E49 EB00                <1> 	jmp short $+2
  7269 00002E4B E4A0                <1> 	in	al, 0A0h
  7270 00002E4D 2480                <1> 	and 	al, 80h ; bit 7 (is it real IRQ 15 or fake?)
  7271 00002E4F 74EF                <1> 	jz	short irq15_iret ; Fake (spurious)IRQ, do not send EOI)
  7272                              <1> 	;
  7273 00002E51 1E                  <1> 	push	ds
  7274                              <1> 	;CALL	DDS
  7275                              <1> 	; 21/02/2015 (32 bit, 386 pm modification)
  7276 00002E52 66B81000            <1> 	mov	ax, KDATA
  7277 00002E56 8ED8                <1> 	mov 	ds, ax
  7278                              <1> 	;
  7279                              <1> 	;;MOV	@HF_INT_FLAG,0FFH	; ALL DONE
  7280                              <1>         ;or	byte [CS:HF_INT_FLAG],0C0h 
  7281 00002E58 800D[47700000]C0    <1> 	or	byte [HF_INT_FLAG], 0C0h
  7282                              <1> 	;
  7283                              <1> 	;push	dx
  7284                              <1> 	; 05/12/2021
  7285 00002E5F 52                  <1> 	push	edx
  7286 00002E60 66BA7701            <1> 	mov	dx, HDC2_BASEPORT+7	; Status Register (177h)
  7287                              <1> 					; Clear Controller (Award BIOS 1999)
  7288 00002E64 EBCD                <1> 	jmp	short Clear_IRQ1415
  7289                              <1> 
  7290                              <1> ;%include 'diskdata.inc' ; 11/03/2015
  7291                              <1> ;%include 'diskbss.inc' ; 11/03/2015
  7292                              <1> 
  7293                              <1> ;////////////////////////////////////////////////////////////////////
  7294                              <1> ;; END OF DISK I/O SYTEM ///
  2067                                  %include 'memory.s'  ; 09/03/2015
  2068                              <1> ; Retro UNIX 386 v2 - memory.s - 26/01/2020
  2069                              <1> ; Last Modification: 31/12/2021 (Retro UNIX 386 v1.2)
  2070                              <1> ; ----------------------------------------------------------------------------
  2071                              <1> ;
  2072                              <1> ; MEMORY.ASM - Retro UNIX 386 v1 MEMORY MANAGEMENT FUNCTIONS (PROCEDURES)
  2073                              <1> ; Retro UNIX 386 v1 Kernel (unix386.s, v0.2.0.14) - MEMORY.INC
  2074                              <1> ; 18/10/2015 (!not completed!)
  2075                              <1> ;
  2076                              <1> ; Source code for NASM - Netwide Assembler (2.11)
  2077                              <1> 
  2078                              <1> ; ///////// MEMORY MANAGEMENT FUNCTIONS (PROCEDURES) ///////////////
  2079                              <1> 
  2080                              <1> ;;04/11/2014 (unix386.s)	
  2081                              <1> ;PDE_A_PRESENT	equ 1		; Present flag for PDE
  2082                              <1> ;PDE_A_WRITE	equ 2		; Writable (write permission) flag
  2083                              <1> ;PDE_A_USER	equ 4		; User (non-system/kernel) page flag
  2084                              <1> ;;
  2085                              <1> ;PTE_A_PRESENT	equ 1		; Present flag for PTE (bit 0)
  2086                              <1> ;PTE_A_WRITE	equ 2		; Writable (write permission) flag (bit 1)
  2087                              <1> ;PTE_A_USER	equ 4		; User (non-system/kernel) page flag (bit 2)
  2088                              <1> ;PTE_A_ACCESS   equ 32		; Accessed flag (bit 5) ; 09/03/2015
  2089                              <1> 
  2090                              <1> ; 27/04/2015
  2091                              <1> ; 09/03/2015
  2092                              <1> PAGE_SIZE 	equ 4096	; page size in bytes
  2093                              <1> PAGE_SHIFT 	equ 12		; page table shift count
  2094                              <1> PAGE_D_SHIFT 	equ 22 ; 12+10	; page directory shift count
  2095                              <1> PAGE_OFF	equ 0FFFh	; 12 bit byte offset in page frame
  2096                              <1> PTE_MASK 	equ 03FFh	; page table entry mask
  2097                              <1> PTE_DUPLICATED  equ 200h	; duplicated page sign (AVL bit 0)
  2098                              <1> PDE_A_CLEAR	equ 0F000h	; to clear PDE attribute bits
  2099                              <1> PTE_A_CLEAR	equ 0F000h	; to clear PTE attribute bits
  2100                              <1> LOGIC_SECT_SIZE equ 512		; logical sector size
  2101                              <1> ERR_MAJOR_PF	equ 0E0h	; major error: page fault
  2102                              <1> ; 15/10/2016 (TRDOS 386 v2)
  2103                              <1> ERR_MINOR_IM	equ 4 ;15/10/2016 (1->4); insufficient (out of) memory
  2104                              <1> ERR_MINOR_PV	equ 6 ;15/10/2016 (3->6); protection violation
  2105                              <1> SWP_DISK_READ_ERR 	   equ 40
  2106                              <1> SWP_DISK_NOT_PRESENT_ERR   equ 41
  2107                              <1> SWP_SECTOR_NOT_PRESENT_ERR equ 42
  2108                              <1> SWP_NO_FREE_SPACE_ERR      equ 43
  2109                              <1> SWP_DISK_WRITE_ERR         equ 44
  2110                              <1> SWP_NO_PAGE_TO_SWAP_ERR    equ 45
  2111                              <1> PTE_A_ACCESS_BIT equ 5	; Bit 5 (accessed flag)        
  2112                              <1> SECTOR_SHIFT     equ 3	; sector shift (to convert page block number)
  2113                              <1> ; 10/06/2021 (Retro UNIX 386 v2)
  2114                              <1> ; 12/07/2016 (TRDOS 386 v2) 
  2115                              <1> PTE_SHARED	 equ 400h		; AVL bit 1, direct memory access bit	
  2116                              <1> 					; (Indicates that the page is not allocated
  2117                              <1> 					; for the process, it is a shared or system
  2118                              <1>                                         ; page, it must not be deallocated!)
  2119                              <1> ; 14/12/2020
  2120                              <1> ; (Linear Frame Buffer - video memory mark : AVL bit 1, outside M.A.T.)
  2121                              <1> PDE_EXTERNAL	equ 400h	; Page directory entry for external memory blocks
  2122                              <1> PTE_EXTERNAL	equ 400h	; Allocated kernel pages for Linear Frame Buffer
  2123                              <1> 				; (Out of memory allocation table)	
  2124                              <1> ;
  2125                              <1> ;; Retro Unix 386 v1 - paging method/principles
  2126                              <1> ;;
  2127                              <1> ;; 10/10/2014
  2128                              <1> ;; RETRO UNIX 386 v1 - PAGING METHOD/PRINCIPLES
  2129                              <1> ;;
  2130                              <1> ;; KERNEL PAGE MAP: 1 to 1 physical memory page map
  2131                              <1> ;;	(virtual address = physical address)
  2132                              <1> ;; KERNEL PAGE TABLES:
  2133                              <1> ;;	Kernel page directory and all page tables are
  2134                              <1> ;;	on memory as initialized, as equal to physical memory
  2135                              <1> ;;	layout. Kernel pages can/must not be swapped out/in.
  2136                              <1> ;;
  2137                              <1> ;;	what for: User pages may be swapped out, when accessing
  2138                              <1> ;;	a page in kernel/system mode, if it would be swapped out,
  2139                              <1> ;;	kernel would have to swap it in! But it is also may be
  2140                              <1> ;;	in use by a user process. (In system/kernel mode
  2141                              <1> ;;	kernel can access all memory pages even if they are
  2142                              <1> ;;	reserved/allocated for user processes. Swap out/in would
  2143                              <1> ;;	cause conflicts.) 
  2144                              <1> ;;	
  2145                              <1> ;;	As result of these conditions,
  2146                              <1> ;;	all kernel pages must be initialized as equal to 
  2147                              <1> ;;	physical layout for preventing page faults. 
  2148                              <1> ;;	Also, calling "allocate page" procedure after
  2149                              <1> ;;	a page fault can cause another page fault (double fault)
  2150                              <1> ;;	if all kernel page tables would not be initialized.
  2151                              <1> ;;
  2152                              <1> ;;	[first_page] = Beginning of users space, as offset to 
  2153                              <1> ;;	memory allocation table. (double word aligned)
  2154                              <1> ;;
  2155                              <1> ;;	[next_page] = first/next free space to be searched
  2156                              <1> ;;	as offset to memory allocation table. (dw aligned)
  2157                              <1> ;;
  2158                              <1> ;;	[last_page] = End of memory (users space), as offset
  2159                              <1> ;;	to memory allocation table. (double word aligned)
  2160                              <1> ;;
  2161                              <1> ;; USER PAGE TABLES:
  2162                              <1> ;;	Demand paging (& 'copy on write' allocation method) ...
  2163                              <1> ;;		'ready only' marked copies of the 
  2164                              <1> ;;		parent process's page table entries (for
  2165                              <1> ;;		same physical memory).
  2166                              <1> ;;		(A page will be copied to a new page after
  2167                              <1> ;;		 if it causes R/W page fault.)
  2168                              <1> ;;
  2169                              <1> ;;	Every user process has own (different)
  2170                              <1> ;;	page directory and page tables.	
  2171                              <1> ;;
  2172                              <1> ;;	Code starts at virtual address 0, always.
  2173                              <1> ;;	(Initial value of EIP is 0 in user mode.)
  2174                              <1> ;;	(Programs can be written/developed as simple
  2175                              <1> ;;	 flat memory programs.)
  2176                              <1> ;;
  2177                              <1> ;; MEMORY ALLOCATION STRATEGY:
  2178                              <1> ;;	Memory page will be allocated by kernel only 
  2179                              <1> ;;		(in kernel/system mode only).
  2180                              <1> ;;	* After a
  2181                              <1> ;;	  - 'not present' page fault
  2182                              <1> ;;	  - 'writing attempt on read only page' page fault 	 	
  2183                              <1> ;;	* For loading (opening, reading) a file or disk/drive
  2184                              <1> ;;	* As responce to 'allocate additional memory blocks' 
  2185                              <1> ;;	  request by running process.
  2186                              <1> ;;	* While creating a process, allocating a new buffer,
  2187                              <1> ;;	  new page tables etc.
  2188                              <1> ;;
  2189                              <1> ;;	At first,
  2190                              <1> ;;	- 'allocate page' procedure will be called;
  2191                              <1> ;,	   if it will return with a valid (>0) physical address
  2192                              <1> ;;	   (that means the relevant M.A.T. bit has been RESET)	
  2193                              <1> ;;	   relevant memory page/block will be cleared (zeroed).
  2194                              <1> ;;	- 'allocate page' will be called for allocating page
  2195                              <1> ;;	   directory, page table and running space (data/code).
  2196                              <1> ;;	- every successful 'allocate page' call will decrease
  2197                              <1> ;;	  'free_pages' count (pointer).
  2198                              <1> ;;	- 'out of (insufficient) memory error' will be returned
  2199                              <1> ;;	  if 'free_pages' points to a ZERO.
  2200                              <1> ;;	- swapping out and swapping in (if it is not a new page)
  2201                              <1> ;;	  procedures will be called as responce to 'out of memory'
  2202                              <1> ;;	  error except errors caused by attribute conflicts.
  2203                              <1> ;;	 (swapper functions)	 
  2204                              <1> ;;					
  2205                              <1> ;;	At second,
  2206                              <1> ;;	- page directory entry will be updated then page table
  2207                              <1> ;;	  entry will be updated.		
  2208                              <1> ;;
  2209                              <1> ;; MEMORY ALLOCATION TABLE FORMAT:
  2210                              <1> ;;	- M.A.T. has a size according to available memory as
  2211                              <1> ;;	  follows:
  2212                              <1> ;;		  - 1 (allocation) bit per 1 page (4096 bytes)
  2213                              <1> ;;		  - a bit with value of 0 means allocated page
  2214                              <1> ;;		  - a bit with value of 1 means a free page
  2215                              <1> ;,	- 'free_pages' pointer holds count of free pages
  2216                              <1> ;;	  depending on M.A.T.
  2217                              <1> ;;		(NOTE: Free page count will not be checked
  2218                              <1> ;;		again -on M.A.T.- after initialization. 
  2219                              <1> ;;		Kernel will trust on initial count.)
  2220                              <1> ;,	- 'free_pages' count will be decreased by allocation
  2221                              <1> ;;	  and it will be increased by deallocation procedures.
  2222                              <1> ;;	
  2223                              <1> ;;	- Available memory will be calculated during
  2224                              <1> ;;	  the kernel's initialization stage (in real mode).
  2225                              <1> ;;	  Memory allocation table and kernel page tables 
  2226                              <1> ;;	  will be formatted/sized as result of available
  2227                              <1> ;;	  memory calculation before paging is enabled.
  2228                              <1> ;;
  2229                              <1> ;; For 4GB Available/Present Memory: (max. possible memory size)
  2230                              <1> ;;	- Memory Allocation Table size will be 128 KB.
  2231                              <1> ;;	- Memory allocation for kernel page directory size 
  2232                              <1> ;;	  is always 4 KB. (in addition to total allocation size
  2233                              <1> ;;	  for page tables)
  2234                              <1> ;;	- Memory allocation for kernel page tables (1024 tables)
  2235                              <1> ;;	  is 4 MB (1024*4*1024 bytes).
  2236                              <1> ;;	- User (available) space will be started 
  2237                              <1> ;;	  at 6th MB of the memory (after 1MB+4MB).
  2238                              <1> ;;	- The first 640 KB is for kernel's itself plus
  2239                              <1> ;;	  memory allocation table and kernel's page directory
  2240                              <1> ;;	  (D0000h-EFFFFh may be used as kernel space...)	
  2241                              <1> ;;	- B0000h to B7FFFh address space (32 KB) will be used
  2242                              <1> ;; 	  for buffers.
  2243                              <1> ;;	- ROMBIOS, VIDEO BUFFER and VIDEO ROM space are reserved.
  2244                              <1> ;,	  (A0000h-AFFFFh, C0000h-CFFFFh, F0000h-FFFFFh)
  2245                              <1> ;;	- Kernel page tables start at 100000h (2nd MB)
  2246                              <1> ;;
  2247                              <1> ;; For 1GB Available Memory:
  2248                              <1> ;;	- Memory Allocation Table size will be 32 KB.
  2249                              <1> ;;	- Memory allocation for kernel page directory size 
  2250                              <1> ;;	  is always 4 KB. (in addition to total allocation size
  2251                              <1> ;;	  for page tables)
  2252                              <1> ;;	- Memory allocation for kernel page tables (256 tables)
  2253                              <1> ;;	  is 1 MB (256*4*1024 bytes).
  2254                              <1> ;;	- User (available) space will be started 
  2255                              <1> ;;	  at 3th MB of the memory (after 1MB+1MB).
  2256                              <1> ;;	- The first 640 KB is for kernel's itself plus
  2257                              <1> ;;	  memory allocation table and kernel's page directory
  2258                              <1> ;;	  (D0000h-EFFFFh may be used as kernel space...)	
  2259                              <1> ;;	- B0000h to B7FFFh address space (32 KB) will be used
  2260                              <1> ;; 	  for buffers.
  2261                              <1> ;;	- ROMBIOS, VIDEO BUFFER and VIDEO ROM space are reserved.
  2262                              <1> ;,	  (A0000h-AFFFFh, C0000h-CFFFFh, F0000h-FFFFFh)
  2263                              <1> ;;	- Kernel page tables start at 100000h (2nd MB).	
  2264                              <1> ;;
  2265                              <1> ;;
  2266                              <1> 
  2267                              <1> ;;************************************************************************************
  2268                              <1> ;; 
  2269                              <1> ;; RETRO UNIX 386 v1 - Paging (Method for Copy On Write paging principle)
  2270                              <1> ;; DEMAND PAGING - PARENT&CHILD PAGE TABLE DUPLICATION PRINCIPLES (23/04/2015)
  2271                              <1> 
  2272                              <1> ;; Main factor: "sys fork" system call 
  2273                              <1> ;;	
  2274                              <1> ;; 		FORK
  2275                              <1> ;;                      |----> parent - duplicated PTEs, read only pages
  2276                              <1> ;;  writable pages ---->|
  2277                              <1> ;;                      |----> child - duplicated PTEs, read only pages
  2278                              <1> ;; 
  2279                              <1> ;; AVL bit (0) of Page Table Entry is used as duplication sign 
  2280                              <1> ;; 
  2281                              <1> ;; AVL Bit 0 [PTE Bit 9] = 'Duplicated PTE belongs to child' sign/flag (if it is set)
  2282                              <1> ;; Note: Dirty bit (PTE bit 6) may be used instead of AVL bit 0 (PTE bit 9)
  2283                              <1> ;;       -while R/W bit is 0-. 
  2284                              <1> ;; 
  2285                              <1> ;; Duplicate page tables with writable pages (the 1st sys fork in the process):
  2286                              <1> ;; # Parent's Page Table Entries are updated to point same pages as read only, 
  2287                              <1> ;;   as duplicated PTE bit  -AVL bit 0, PTE bit 9- are reset/clear.
  2288                              <1> ;; # Then Parent's Page Table is copied to Child's Page Table.
  2289                              <1> ;; # Child's Page Table Entries are updated as duplicated child bit
  2290                              <1> ;;   -AVL bit 0, PTE bit 9- is set.	  
  2291                              <1> ;; 
  2292                              <1> ;; Duplicate page tables with read only pages (several sys fork system calls):
  2293                              <1> ;; # Parent's read only pages are copied to new child pages. 
  2294                              <1> ;;   Parent's PTE attributes are not changed.
  2295                              <1> ;;   (Because, there is another parent-child fork before this fork! We must not
  2296                              <1> ;;    destroy/mix previous fork result).
  2297                              <1> ;; # Child's Page Table Entries (which are corresponding to Parent's 
  2298                              <1> ;;   read only pages) are set as writable (while duplicated PTE bit is clear). 
  2299                              <1> ;; # Parent's PTEs with writable page attribute are updated to point same pages 
  2300                              <1> ;;   as read only, (while) duplicated PTE bit is reset (clear).
  2301                              <1> ;; # Parent's Page Table Entries (with writable page attribute) are duplicated 
  2302                              <1> ;;   as Child's Page Table Entries without copying actual page.
  2303                              <1> ;; # Child 's Page Table Entries (which are corresponding to Parent's writable 
  2304                              <1> ;;   pages) are updated as duplicated PTE bit (AVL bit 0, PTE bit 9- is set.
  2305                              <1> ;; 
  2306                              <1> ;; !? WHAT FOR (duplication after duplication):
  2307                              <1> ;; In UNIX method for sys fork (a typical 'fork' application in /etc/init)
  2308                              <1> ;; program/executable code continues from specified location as child process, 
  2309                              <1> ;; returns back previous code location as parent process, every child after 
  2310                              <1> ;; every sys fork uses last image of code and data just prior the fork.
  2311                              <1> ;; Even if the parent code changes data, the child will not see the changed data 
  2312                              <1> ;; after the fork. In Retro UNIX 8086 v1, parent's process segment (32KB)
  2313                              <1> ;; was copied to child's process segment (all of code and data) according to
  2314                              <1> ;; original UNIX v1 which copies all of parent process code and data -core- 
  2315                              <1> ;; to child space -core- but swaps that core image -of child- on to disk.
  2316                              <1> ;; If I (Erdogan Tan) would use a method of to copy parent's core
  2317                              <1> ;; (complete running image of parent process) to the child process; 
  2318                              <1> ;; for big sizes, i would force Retro UNIX 386 v1 to spend many memory pages 
  2319                              <1> ;; and times only for a sys fork. (It would excessive reservation for sys fork,
  2320                              <1> ;; because sys fork usually is prior to sys exec; sys exec always establishes
  2321                              <1> ;; a new/fresh core -running space-, by clearing all code/data content). 
  2322                              <1> ;; 'Read Only' page flag ensures page fault handler is needed only for a few write
  2323                              <1> ;; attempts between sys fork and sys exec, not more... (I say so by thinking 
  2324                              <1> ;; of "/etc/init" content, specially.) sys exec will clear page tables and
  2325                              <1> ;; new/fresh pages will be used to load and run new executable/program.
  2326                              <1> ;; That is what for i have preferred "copy on write", "duplication" method
  2327                              <1> ;; for sharing same read only pages between parent and child processes.
  2328                              <1> ;; That is a pitty i have to use new private flag (AVL bit 0, "duplicated PTE 
  2329                              <1> ;; belongs to child" sign) for cooperation on duplicated pages between a parent 
  2330                              <1> ;; and it's child processes; otherwise parent process would destroy data belongs
  2331                              <1> ;; to its child or vice versa; or some pages would remain unclaimed 
  2332                              <1> ;; -deallocation problem-.
  2333                              <1> ;; Note: to prevent conflicts, read only pages must not be swapped out... 
  2334                              <1> ;; 
  2335                              <1> ;; WHEN PARENT TRIES TO WRITE IT'S READ ONLY (DUPLICATED) PAGE:
  2336                              <1> ;; # Page fault handler will do those:
  2337                              <1> ;;   - 'Duplicated PTE' flag (PTE bit 9) is checked (on the failed PTE).
  2338                              <1> ;;   - If it is reset/clear, there is a child uses same page.
  2339                              <1> ;;   - Parent's read only page -previous page- is copied to a new writable page. 
  2340                              <1> ;;   - Parent's PTE is updated as writable page, as unique page (AVL=0)
  2341                              <1> ;;   - (Page fault handler whill check this PTE later, if child process causes to
  2342                              <1> ;;     page fault due to write attempt on read only page. Of course, the previous 
  2343                              <1> ;;     read only page will be converted to writable and unique page which belongs
  2344                              <1> ;;     to child process.)	
  2345                              <1> ;; WHEN CHILD TRIES TO WRITE IT'S READ ONLY (DUPLICATED) PAGE:
  2346                              <1> ;; # Page fault handler will do those:
  2347                              <1> ;;   - 'Duplicated PTE' flag (PTE bit 9) is checked (on the failed PTE).
  2348                              <1> ;;   - If it is set, there is a parent uses -or was using- same page.
  2349                              <1> ;;   - Same PTE address within parent's page table is checked if it has same page
  2350                              <1> ;;     address or not. 
  2351                              <1> ;;   - If parent's PTE has same address, child will continue with a new writable page.
  2352                              <1> ;;     Parent's PTE will point to same (previous) page as writable, unique (AVL=0).	
  2353                              <1> ;;   - If parent's PTE has different address, child will continue with it's 
  2354                              <1> ;;     own/same page but read only flag (0) will be changed to writable flag (1) and
  2355                              <1> ;;     'duplicated PTE (belongs to child)' flag/sign will be cleared/reset. 	  	
  2356                              <1> ;; 
  2357                              <1> ;; NOTE: When a child process is terminated, read only flags of parent's page tables
  2358                              <1> ;;       will be set as writable (and unique) in case of child process was using 
  2359                              <1> ;;       same pages with duplicated child PTE sign... Depending on sys fork and 
  2360                              <1> ;;       duplication method details, it is not possible multiple child processes
  2361                              <1> ;;       were using same page with duplicated PTEs.
  2362                              <1> ;; 
  2363                              <1> ;;************************************************************************************   
  2364                              <1> 
  2365                              <1> ;; 08/10/2014
  2366                              <1> ;; 11/09/2014 - Retro UNIX 386 v1 PAGING (further) draft
  2367                              <1> ;;		by Erdogan Tan (Based on KolibriOS 'memory.inc')
  2368                              <1> 
  2369                              <1> ;; 'allocate_page' code is derived and modified from KolibriOS
  2370                              <1> ;; 'alloc_page' procedure in 'memory.inc' 
  2371                              <1> ;; (25/08/2014, Revision: 5057) file 
  2372                              <1> ;; by KolibriOS Team (2004-2012)
  2373                              <1> 
  2374                              <1> allocate_page:
  2375                              <1> 	; 17/04/2021 - Retro UNIX 386 v2
  2376                              <1> 	;	 (temporary modifications)
  2377                              <1> 	; 01/07/2015
  2378                              <1> 	; 05/05/2015
  2379                              <1> 	; 30/04/2015
  2380                              <1> 	; 16/10/2014
  2381                              <1> 	; 08/10/2014
  2382                              <1> 	; 09/09/2014 (Retro UNIX 386 v1 - beginning)
  2383                              <1> 	;
  2384                              <1> 	; INPUT -> none
  2385                              <1> 	;
  2386                              <1> 	; OUTPUT ->
  2387                              <1> 	;	EAX = PHYSICAL (real/flat) ADDRESS OF THE ALLOCATED PAGE
  2388                              <1> 	;	(corresponding MEMORY ALLOCATION TABLE bit is RESET)
  2389                              <1> 	;
  2390                              <1> 	;	CF = 1 and EAX = 0 
  2391                              <1> 	; 		   if there is not a free page to be allocated	
  2392                              <1> 	;
  2393                              <1> 	; Modified Registers -> none (except EAX)
  2394                              <1> 	;
  2395 00002E66 A1[C06F0000]        <1> 	mov	eax, [free_pages]
  2396 00002E6B 21C0                <1> 	and	eax, eax
  2397 00002E6D 7438                <1> 	jz	short out_of_memory
  2398                              <1> 	;
  2399 00002E6F 53                  <1> 	push	ebx
  2400 00002E70 51                  <1> 	push	ecx
  2401                              <1> 	;
  2402 00002E71 BB00001000          <1> 	mov	ebx, MEM_ALLOC_TBL   ; Memory Allocation Table offset
  2403 00002E76 89D9                <1> 	mov	ecx, ebx
  2404                              <1>  				     ; NOTE: 32 (first_page) is initial
  2405                              <1> 				     ; value of [next_page].
  2406                              <1> 				     ; It points to the first available
  2407                              <1> 				     ; page block for users (ring 3) ...	
  2408                              <1> 				     ; (MAT offset 32 = 1024/32)	
  2409                              <1> 				     ; (at the of the first 4 MB)		
  2410 00002E78 031D[C46F0000]      <1> 	add	ebx, [next_page] ; Free page searching starts from here
  2411                              <1> 				 ; next_free_page >> 5
  2412 00002E7E 030D[C86F0000]      <1> 	add	ecx, [last_page] ; Free page searching ends here
  2413                              <1> 				 ; (total_pages - 1) >> 5
  2414                              <1> al_p_scan:
  2415 00002E84 39CB                <1> 	cmp	ebx, ecx
  2416 00002E86 770A                <1> 	ja	short al_p_notfound
  2417                              <1> 	;
  2418                              <1> 	; 01/07/2015
  2419                              <1> 	; AMD64 Architecture Programmers Manual
  2420                              <1> 	; Volume 3:
  2421                              <1> 	; General-Purpose and System Instructions
  2422                              <1> 	;
  2423                              <1> 	; BSF - Bit Scan Forward
  2424                              <1> 	;
  2425                              <1> 	;   Searches the value in a register or a memory location
  2426                              <1> 	;   (second operand) for the least-significant set bit. 
  2427                              <1> 	;   If a set bit is found, the instruction clears the zero flag (ZF)
  2428                              <1> 	;   and stores the index of the least-significant set bit in a destination
  2429                              <1> 	;   register (first operand). If the second operand contains 0, 
  2430                              <1> 	;   the instruction sets ZF to 1 and does not change the contents of the 
  2431                              <1> 	;   destination register. The bit index is an unsigned offset from bit 0 
  2432                              <1> 	;   of the searched value
  2433                              <1> 	;
  2434 00002E88 0FBC03              <1> 	bsf	eax, [ebx] ; Scans source operand for first bit set (1).
  2435                              <1> 			   ; Clear ZF if a bit is found set (1) and 
  2436                              <1> 			   ; loads the destination with an index to
  2437                              <1> 			   ; first set bit. (0 -> 31) 
  2438                              <1> 			   ; Sets ZF to 1 if no bits are found set.
  2439 00002E8B 751C                <1> 	jnz	short al_p_found ; ZF = 0 -> a free page has been found
  2440                              <1> 			 ;
  2441                              <1> 			 ; NOTE:  a Memory Allocation Table bit 
  2442                              <1> 			 ;	  with value of 1 means 
  2443                              <1> 			 ;	  the corresponding page is free 
  2444                              <1> 			 ;	  (Retro UNIX 386 v1 feature only!)
  2445 00002E8D 83C304              <1> 	add	ebx, 4
  2446                              <1> 			 ; We return back for searching next page block
  2447                              <1> 			 ; NOTE: [free_pages] is not ZERO; so, 
  2448                              <1> 			 ;	 we always will find at least 1 free page here.
  2449 00002E90 EBF2                <1>         jmp     short al_p_scan
  2450                              <1> 	;
  2451                              <1> al_p_notfound:
  2452 00002E92 81E900001000        <1> 	sub	ecx, MEM_ALLOC_TBL
  2453 00002E98 890D[C46F0000]      <1> 	mov	[next_page], ecx ; next/first free page = last page 
  2454                              <1> 				 ; (deallocate_page procedure will change it)
  2455 00002E9E 31C0                <1> 	xor	eax, eax
  2456 00002EA0 A3[C06F0000]        <1> 	mov	[free_pages], eax ; 0
  2457 00002EA5 59                  <1> 	pop	ecx
  2458 00002EA6 5B                  <1> 	pop	ebx
  2459                              <1> 	;
  2460                              <1> ; 17/04/2021
  2461                              <1> ; ('swap_out' procedure call is disabled as temporary)
  2462                              <1> 
  2463                              <1> out_of_memory:
  2464                              <1> ;	call	swap_out
  2465                              <1> ;	jnc	short al_p_ok  ; [free_pages] = 0, re-allocation by swap_out
  2466                              <1> ;	;
  2467                              <1> ;	sub 	eax, eax ; 0
  2468 00002EA7 F9                  <1> 	stc
  2469 00002EA8 C3                  <1> 	retn
  2470                              <1> 
  2471                              <1> al_p_found:
  2472 00002EA9 89D9                <1> 	mov	ecx, ebx
  2473 00002EAB 81E900001000        <1> 	sub	ecx, MEM_ALLOC_TBL
  2474 00002EB1 890D[C46F0000]      <1> 	mov	[next_page], ecx ; Set first free page searching start
  2475                              <1> 				 ; address/offset (to the next)
  2476 00002EB7 FF0D[C06F0000]      <1>         dec     dword [free_pages] ; 1 page has been allocated (X = X-1) 
  2477                              <1> 	;
  2478 00002EBD 0FB303              <1> 	btr	[ebx], eax	 ; The destination bit indexed by the source value
  2479                              <1> 				 ; is copied into the Carry Flag and then cleared
  2480                              <1> 				 ; in the destination.
  2481                              <1> 				 ;
  2482                              <1> 				 ; Reset the bit which is corresponding to the 
  2483                              <1> 				 ; (just) allocated page.
  2484                              <1> 	; 01/07/2015 (4*8 = 32, 1 allocation byte = 8 pages)	
  2485 00002EC0 C1E103              <1> 	shl	ecx, 3		 ; (page block offset * 32) + page index
  2486 00002EC3 01C8                <1> 	add	eax, ecx	 ; = page number
  2487 00002EC5 C1E00C              <1> 	shl	eax, 12		 ; physical address of the page (flat/real value)
  2488                              <1> 	; EAX = physical address of memory page
  2489                              <1> 	;
  2490                              <1> 	; NOTE: The relevant page directory and page table entry will be updated
  2491                              <1> 	;       according to this EAX value...
  2492 00002EC8 59                  <1> 	pop	ecx
  2493 00002EC9 5B                  <1> 	pop	ebx
  2494                              <1> al_p_ok:
  2495 00002ECA C3                  <1> 	retn
  2496                              <1> 
  2497                              <1> make_page_dir:
  2498                              <1> 	; 18/04/2015
  2499                              <1> 	; 12/04/2015
  2500                              <1> 	; 23/10/2014
  2501                              <1> 	; 16/10/2014
  2502                              <1> 	; 09/10/2014 ; (Retro UNIX 386 v1 - beginning)
  2503                              <1> 	;
  2504                              <1> 	; INPUT ->
  2505                              <1> 	;	none
  2506                              <1> 	; OUTPUT ->
  2507                              <1> 	;	(EAX = 0)
  2508                              <1> 	;	cf = 1 -> insufficient (out of) memory error
  2509                              <1> 	;	cf = 0 ->
  2510                              <1> 	;	u.pgdir = page directory (physical) address of the current
  2511                              <1> 	;		  process/user.
  2512                              <1> 	;
  2513                              <1> 	; Modified Registers -> EAX
  2514                              <1> 	;
  2515 00002ECB E896FFFFFF          <1> 	call	allocate_page
  2516 00002ED0 7216                <1> 	jc	short mkpd_error
  2517                              <1> 	;
  2518 00002ED2 A3[38750000]        <1> 	mov	[u.pgdir], eax    ; Page dir address for current user/process
  2519                              <1> 				  ; (Physical address)
  2520                              <1> clear_page:
  2521                              <1> 	; 18/04/2015
  2522                              <1> 	; 09/10/2014 ; (Retro UNIX 386 v1 - beginning)
  2523                              <1> 	;
  2524                              <1> 	; INPUT ->
  2525                              <1> 	;	EAX = physical address of the page
  2526                              <1> 	; OUTPUT ->
  2527                              <1> 	;	all bytes of the page will be cleared
  2528                              <1> 	;
  2529                              <1> 	; Modified Registers -> none
  2530                              <1> 	;
  2531 00002ED7 57                  <1> 	push	edi
  2532 00002ED8 51                  <1> 	push	ecx
  2533 00002ED9 50                  <1> 	push	eax
  2534 00002EDA B900040000          <1> 	mov	ecx, PAGE_SIZE / 4
  2535 00002EDF 89C7                <1> 	mov	edi, eax
  2536 00002EE1 31C0                <1> 	xor	eax, eax
  2537 00002EE3 F3AB                <1> 	rep	stosd
  2538 00002EE5 58                  <1> 	pop	eax
  2539 00002EE6 59                  <1> 	pop	ecx
  2540 00002EE7 5F                  <1> 	pop	edi
  2541                              <1> mkpd_error:
  2542                              <1> mkpt_error:
  2543 00002EE8 C3                  <1> 	retn
  2544                              <1> 
  2545                              <1> make_page_table:
  2546                              <1> 	; 23/06/2015
  2547                              <1> 	; 18/04/2015
  2548                              <1> 	; 12/04/2015
  2549                              <1> 	; 16/10/2014
  2550                              <1> 	; 09/10/2014 ; (Retro UNIX 386 v1 - beginning)
  2551                              <1> 	;
  2552                              <1> 	; INPUT ->
  2553                              <1> 	;	EBX = virtual (linear) address
  2554                              <1> 	;	ECX = page table attributes (lower 12 bits)
  2555                              <1> 	;	      (higher 20 bits must be ZERO)
  2556                              <1> 	;	      (bit 0 must be 1)	 
  2557                              <1> 	;	u.pgdir = page directory (physical) address
  2558                              <1> 	; OUTPUT ->
  2559                              <1> 	;	EDX = Page directory entry address
  2560                              <1> 	;	EAX = Page table address
  2561                              <1> 	;	cf = 1 -> insufficient (out of) memory error
  2562                              <1> 	;	cf = 0 -> page table address in the PDE (EDX)
  2563                              <1> 	;
  2564                              <1> 	; Modified Registers -> EAX, EDX
  2565                              <1> 	;
  2566 00002EE9 E878FFFFFF          <1> 	call	allocate_page
  2567 00002EEE 72F8                <1> 	jc	short mkpt_error
  2568 00002EF0 E811000000          <1> 	call	set_pde	
  2569 00002EF5 EBE0                <1> 	jmp	short clear_page
  2570                              <1> 
  2571                              <1> make_page:
  2572                              <1> 	; 24/07/2015
  2573                              <1> 	; 23/06/2015 ; (Retro UNIX 386 v1 - beginning)
  2574                              <1> 	;
  2575                              <1> 	; INPUT ->
  2576                              <1> 	;	EBX = virtual (linear) address
  2577                              <1> 	;	ECX = page attributes (lower 12 bits)
  2578                              <1> 	;	      (higher 20 bits must be ZERO)
  2579                              <1> 	;	      (bit 0 must be 1)	 
  2580                              <1> 	;	u.pgdir = page directory (physical) address
  2581                              <1> 	; OUTPUT ->
  2582                              <1> 	;	EBX = Virtual address
  2583                              <1> 	;	(EDX = PTE value)
  2584                              <1> 	;	EAX = Physical address
  2585                              <1> 	;	cf = 1 -> insufficient (out of) memory error
  2586                              <1> 	;
  2587                              <1> 	; Modified Registers -> EAX, EDX
  2588                              <1> 	;
  2589 00002EF7 E86AFFFFFF          <1> 	call	allocate_page
  2590 00002EFC 7207                <1> 	jc	short mkp_err
  2591 00002EFE E821000000          <1> 	call	set_pte	
  2592 00002F03 73D2                <1> 	jnc	short clear_page ; 18/04/2015
  2593                              <1> mkp_err:
  2594 00002F05 C3                  <1> 	retn
  2595                              <1> 
  2596                              <1> set_pde:	; Set page directory entry (PDE)
  2597                              <1> 	; 20/07/2015
  2598                              <1> 	; 18/04/2015
  2599                              <1> 	; 12/04/2015
  2600                              <1> 	; 23/10/2014
  2601                              <1> 	; 10/10/2014 ; (Retro UNIX 386 v1 - beginning)
  2602                              <1> 	;
  2603                              <1> 	; INPUT ->
  2604                              <1> 	;	EAX = physical address
  2605                              <1> 	;	      (use present value if EAX = 0)
  2606                              <1> 	;	EBX = virtual (linear) address
  2607                              <1> 	;	ECX = page table attributes (lower 12 bits)
  2608                              <1> 	;	      (higher 20 bits must be ZERO)
  2609                              <1> 	;	      (bit 0 must be 1)	 
  2610                              <1> 	;	u.pgdir = page directory (physical) address
  2611                              <1> 	; OUTPUT ->
  2612                              <1> 	;	EDX = PDE address
  2613                              <1> 	;	EAX = page table address (physical)
  2614                              <1> 	;	;(CF=1 -> Invalid page address)
  2615                              <1> 	;
  2616                              <1> 	; Modified Registers -> EDX
  2617                              <1> 	;
  2618 00002F06 89DA                <1> 	mov	edx, ebx
  2619 00002F08 C1EA16              <1> 	shr	edx, PAGE_D_SHIFT ; 22
  2620 00002F0B C1E202              <1> 	shl	edx, 2 ; offset to page directory (1024*4)
  2621 00002F0E 0315[38750000]      <1> 	add	edx, [u.pgdir]
  2622                              <1> 	;
  2623 00002F14 21C0                <1> 	and	eax, eax
  2624 00002F16 7506                <1> 	jnz	short spde_1
  2625                              <1> 	;
  2626 00002F18 8B02                <1> 	mov	eax, [edx]  ; old PDE value
  2627                              <1> 	;test	al, 1
  2628                              <1> 	;jz	short spde_2
  2629 00002F1A 662500F0            <1> 	and	ax, PDE_A_CLEAR ; 0F000h  ; clear lower 12 bits
  2630                              <1> spde_1:
  2631                              <1> 	;and	cx, 0FFFh
  2632 00002F1E 8902                <1> 	mov	[edx], eax
  2633 00002F20 66090A              <1> 	or	[edx], cx
  2634 00002F23 C3                  <1> 	retn
  2635                              <1> ;spde_2: ; error
  2636                              <1> ;	stc
  2637                              <1> ;	retn
  2638                              <1> 
  2639                              <1> set_pte:	; Set page table entry (PTE)
  2640                              <1> 	; 24/07/2015
  2641                              <1> 	; 20/07/2015
  2642                              <1> 	; 23/06/2015
  2643                              <1> 	; 18/04/2015
  2644                              <1> 	; 12/04/2015
  2645                              <1> 	; 10/10/2014 ; (Retro UNIX 386 v1 - beginning)
  2646                              <1> 	;
  2647                              <1> 	; INPUT ->
  2648                              <1> 	;	EAX = physical page address
  2649                              <1> 	;	      (use present value if EAX = 0)
  2650                              <1> 	;	EBX = virtual (linear) address
  2651                              <1> 	;	ECX = page attributes (lower 12 bits)
  2652                              <1> 	;	      (higher 20 bits must be ZERO)
  2653                              <1> 	;	      (bit 0 must be 1)	 
  2654                              <1> 	;	u.pgdir = page directory (physical) address
  2655                              <1> 	; OUTPUT ->
  2656                              <1> 	;	EAX = physical page address
  2657                              <1> 	;	(EDX = PTE value)
  2658                              <1> 	;	EBX = virtual address
  2659                              <1> 	;
  2660                              <1> 	;	CF = 1 -> error
  2661                              <1> 	;
  2662                              <1> 	; Modified Registers -> EAX, EDX
  2663                              <1> 	;
  2664 00002F24 50                  <1> 	push	eax
  2665 00002F25 A1[38750000]        <1> 	mov	eax, [u.pgdir] ; 20/07/2015
  2666 00002F2A E837000000          <1> 	call 	get_pde
  2667                              <1> 		; EDX = PDE address
  2668                              <1> 		; EAX = PDE value
  2669 00002F2F 5A                  <1> 	pop	edx ; physical page address
  2670 00002F30 722A                <1> 	jc	short spte_err ; PDE not present
  2671                              <1> 	;
  2672 00002F32 53                  <1> 	push	ebx ; 24/07/2015
  2673 00002F33 662500F0            <1> 	and	ax, PDE_A_CLEAR ; 0F000h ; clear lower 12 bits
  2674                              <1> 			    ; EDX = PT address (physical)	
  2675 00002F37 C1EB0C              <1> 	shr	ebx, PAGE_SHIFT ; 12
  2676 00002F3A 81E3FF030000        <1> 	and	ebx, PTE_MASK	; 03FFh
  2677                              <1> 			 ; clear higher 10 bits (PD bits)
  2678 00002F40 C1E302              <1> 	shl	ebx, 2   ; offset to page table (1024*4)
  2679 00002F43 01C3                <1> 	add	ebx, eax
  2680                              <1> 	;
  2681 00002F45 8B03                <1> 	mov	eax, [ebx] ; Old PTE value
  2682 00002F47 A801                <1> 	test	al, 1
  2683 00002F49 740C                <1> 	jz	short spte_0
  2684 00002F4B 09D2                <1> 	or	edx, edx
  2685 00002F4D 750F                <1> 	jnz	short spte_1
  2686 00002F4F 662500F0            <1> 	and	ax, PTE_A_CLEAR ; 0F000h ; clear lower 12 bits
  2687 00002F53 89C2                <1> 	mov	edx, eax
  2688 00002F55 EB09                <1> 	jmp	short spte_2	
  2689                              <1> spte_0:
  2690                              <1> 	; If this PTE contains a swap (disk) address,
  2691                              <1> 	; it can be updated by using 'swap_in' procedure
  2692                              <1> 	; only!
  2693 00002F57 21C0                <1> 	and	eax, eax
  2694 00002F59 7403                <1> 	jz	short spte_1
  2695                              <1> 	; 24/07/2015
  2696                              <1> 	; swapped page ! (on disk)
  2697 00002F5B 5B                  <1> 	pop	ebx
  2698                              <1> spte_err:
  2699 00002F5C F9                  <1> 	stc
  2700 00002F5D C3                  <1> 	retn
  2701                              <1> spte_1: 
  2702 00002F5E 89D0                <1> 	mov	eax, edx
  2703                              <1> spte_2:
  2704 00002F60 09CA                <1> 	or	edx, ecx
  2705                              <1> 	; 23/06/2015
  2706 00002F62 8913                <1> 	mov	[ebx], edx ; PTE value in EDX
  2707                              <1> 	; 24/07/2015
  2708 00002F64 5B                  <1> 	pop	ebx
  2709 00002F65 C3                  <1> 	retn
  2710                              <1> 
  2711                              <1> get_pde:	; Get present value of the relevant PDE
  2712                              <1> 	; 20/07/2015
  2713                              <1> 	; 18/04/2015
  2714                              <1> 	; 12/04/2015
  2715                              <1> 	; 10/10/2014 ; (Retro UNIX 386 v1 - beginning)
  2716                              <1> 	;
  2717                              <1> 	; INPUT ->
  2718                              <1> 	;	EBX = virtual (linear) address
  2719                              <1> 	;	EAX = page directory (physical) address
  2720                              <1> 	; OUTPUT ->
  2721                              <1> 	;	EDX = Page directory entry address
  2722                              <1> 	;	EAX = Page directory entry value
  2723                              <1> 	;	CF = 1 -> PDE not present or invalid ? 
  2724                              <1> 	; Modified Registers -> EDX, EAX
  2725                              <1> 	;
  2726 00002F66 89DA                <1> 	mov	edx, ebx
  2727 00002F68 C1EA16              <1> 	shr	edx, PAGE_D_SHIFT ; 22  (12+10)
  2728 00002F6B C1E202              <1> 	shl 	edx, 2 ; offset to page directory (1024*4)
  2729 00002F6E 01C2                <1> 	add	edx, eax ; page directory address (physical)
  2730 00002F70 8B02                <1> 	mov	eax, [edx]
  2731 00002F72 A801                <1> 	test	al, PDE_A_PRESENT ; page table is present or not !
  2732 00002F74 751F                <1> 	jnz	short gpte_retn
  2733 00002F76 F9                  <1> 	stc
  2734                              <1> gpde_retn:	
  2735 00002F77 C3                  <1> 	retn
  2736                              <1> 
  2737                              <1> get_pte:
  2738                              <1> 		; Get present value of the relevant PTE
  2739                              <1> 	; 29/07/2015
  2740                              <1> 	; 20/07/2015
  2741                              <1> 	; 18/04/2015
  2742                              <1> 	; 12/04/2015
  2743                              <1> 	; 10/10/2014 ; (Retro UNIX 386 v1 - beginning)
  2744                              <1> 	;
  2745                              <1> 	; INPUT ->
  2746                              <1> 	;	EBX = virtual (linear) address
  2747                              <1> 	;	EAX = page directory (physical) address
  2748                              <1> 	; OUTPUT ->
  2749                              <1> 	;	EDX = Page table entry address (if CF=0)
  2750                              <1> 	;	      Page directory entry address (if CF=1)
  2751                              <1> 	;            (Bit 0 value is 0 if PT is not present)
  2752                              <1> 	;	EAX = Page table entry value (page address)
  2753                              <1> 	;	CF = 1 -> PDE not present or invalid ? 
  2754                              <1> 	; Modified Registers -> EAX, EDX
  2755                              <1> 	;
  2756 00002F78 E8E9FFFFFF          <1> 	call 	get_pde
  2757 00002F7D 72F8                <1> 	jc	short gpde_retn	; page table is not present
  2758                              <1> 	;jnc	short gpte_1
  2759                              <1> 	;retn
  2760                              <1> ;gpte_1:
  2761 00002F7F 662500F0            <1> 	and	ax, PDE_A_CLEAR ; 0F000h ; clear lower 12 bits
  2762 00002F83 89DA                <1> 	mov	edx, ebx
  2763 00002F85 C1EA0C              <1> 	shr	edx, PAGE_SHIFT ; 12
  2764 00002F88 81E2FF030000        <1> 	and	edx, PTE_MASK	; 03FFh
  2765                              <1> 			 ; clear higher 10 bits (PD bits)
  2766 00002F8E C1E202              <1> 	shl	edx, 2 ; offset from start of page table (1024*4)
  2767 00002F91 01C2                <1> 	add	edx, eax
  2768 00002F93 8B02                <1> 	mov	eax, [edx]
  2769                              <1> gpte_retn:
  2770 00002F95 C3                  <1> 	retn
  2771                              <1> 
  2772                              <1> deallocate_page_dir:
  2773                              <1> 	; 15/09/2015
  2774                              <1> 	; 05/08/2015
  2775                              <1> 	; 30/04/2015
  2776                              <1> 	; 28/04/2015
  2777                              <1> 	; 17/10/2014
  2778                              <1> 	; 12/10/2014 (Retro UNIX 386 v1 - beginning)
  2779                              <1> 	;
  2780                              <1> 	; INPUT ->
  2781                              <1> 	;	EAX = PHYSICAL ADDRESS OF THE PAGE DIRECTORY (CHILD)
  2782                              <1> 	;	EBX = PHYSICAL ADDRESS OF THE PARENT'S PAGE DIRECTORY
  2783                              <1> 	; OUTPUT ->
  2784                              <1> 	;	All of page tables in the page directory
  2785                              <1> 	;	and page dir's itself will be deallocated
  2786                              <1> 	;	except 'read only' duplicated pages (will be converted
  2787                              <1> 	;	to writable pages).
  2788                              <1> 	;
  2789                              <1> 	; Modified Registers -> EAX
  2790                              <1> 	;
  2791                              <1> 	;
  2792 00002F96 56                  <1> 	push	esi
  2793 00002F97 51                  <1> 	push	ecx
  2794 00002F98 50                  <1> 	push	eax
  2795 00002F99 89C6                <1> 	mov	esi, eax 
  2796 00002F9B 31C9                <1> 	xor	ecx, ecx
  2797                              <1> 	; The 1st PDE points to Kernel Page Table 0 (the 1st 4MB),
  2798                              <1> 	; it must not be deallocated
  2799 00002F9D 890E                <1> 	mov	[esi], ecx ; 0 ; clear PDE 0
  2800                              <1> dapd_0:
  2801 00002F9F AD                  <1> 	lodsd
  2802 00002FA0 A801                <1> 	test	al, PDE_A_PRESENT ; bit 0, present flag (must be 1)
  2803 00002FA2 7409                <1> 	jz	short dapd_1	
  2804 00002FA4 662500F0            <1> 	and	ax, PDE_A_CLEAR ; 0F000h ; clear lower 12 (attribute) bits
  2805 00002FA8 E812000000          <1> 	call	deallocate_page_table			
  2806                              <1> dapd_1:
  2807 00002FAD 41                  <1> 	inc	ecx ; page directory entry index
  2808 00002FAE 81F900040000        <1> 	cmp	ecx, PAGE_SIZE / 4 ; 1024
  2809 00002FB4 72E9                <1> 	jb	short dapd_0
  2810                              <1> dapd_2:
  2811 00002FB6 58                  <1> 	pop	eax
  2812 00002FB7 E872000000          <1> 	call	deallocate_page	; deallocate the page dir's itself
  2813 00002FBC 59                  <1> 	pop	ecx
  2814 00002FBD 5E                  <1> 	pop	esi
  2815 00002FBE C3                  <1> 	retn
  2816                              <1> 
  2817                              <1> deallocate_page_table:
  2818                              <1> 	; 17/04/2021 - Retro UNIX 386 v2
  2819                              <1> 	;	 (temporary modifications)
  2820                              <1> 	; 12/07/2016 (TRDOS 386 v2)
  2821                              <1> 	; 19/09/2015
  2822                              <1> 	; 15/09/2015
  2823                              <1> 	; 05/08/2015
  2824                              <1> 	; 30/04/2015
  2825                              <1> 	; 28/04/2015
  2826                              <1> 	; 24/10/2014
  2827                              <1> 	; 23/10/2014
  2828                              <1> 	; 12/10/2014 (Retro UNIX 386 v1 - beginning)
  2829                              <1> 	;
  2830                              <1> 	; INPUT ->
  2831                              <1> 	;	EAX = PHYSICAL (real/flat) ADDRESS OF THE PAGE TABLE
  2832                              <1> 	;	EBX = PHYSICAL ADDRESS OF THE PARENT'S PAGE DIRECTORY
  2833                              <1> 	;	(ECX = page directory entry index)
  2834                              <1> 	; OUTPUT ->
  2835                              <1> 	;	All of pages in the page table and page table's itself
  2836                              <1> 	;	will be deallocated except 'read only' duplicated pages
  2837                              <1> 	;	(will be converted to writable pages).
  2838                              <1> 	;
  2839                              <1> 	; Modified Registers -> EAX
  2840                              <1> 	;
  2841 00002FBF 56                  <1> 	push	esi
  2842 00002FC0 57                  <1> 	push	edi
  2843 00002FC1 52                  <1> 	push	edx
  2844 00002FC2 50                  <1> 	push	eax ; *
  2845 00002FC3 89C6                <1> 	mov	esi, eax 
  2846 00002FC5 31FF                <1> 	xor	edi, edi ; 0
  2847                              <1> dapt_0:
  2848 00002FC7 AD                  <1> 	lodsd
  2849 00002FC8 A801                <1> 	test	al, PTE_A_PRESENT ; bit 0, present flag (must be 1)
  2850 00002FCA 7455                <1> 	jz	short dapt_1
  2851                              <1> 	;
  2852 00002FCC A802                <1> 	test	al, PTE_A_WRITE   ; bit 1, writable (r/w) flag
  2853                              <1> 				  ; (must be 1)
  2854 00002FCE 753F                <1> 	jnz	short dapt_3
  2855                              <1> 	; Read only -duplicated- page (belongs to a parent or a child)
  2856 00002FD0 66A90002            <1>         test    ax, PTE_DUPLICATED ; Was this page duplicated 
  2857                              <1> 				   ; as child's page ?
  2858 00002FD4 7444                <1> 	jz	short dapt_4 ; Clear PTE but don't deallocate the page!
  2859                              <1> 	; check the parent's PTE value is read only & same page or not.. 
  2860                              <1> 	; ECX = page directory entry index (0-1023)
  2861 00002FD6 53                  <1> 	push	ebx
  2862 00002FD7 51                  <1> 	push	ecx
  2863 00002FD8 66C1E102            <1> 	shl	cx, 2 ; *4 
  2864 00002FDC 01CB                <1> 	add	ebx, ecx ; PDE offset (for the parent)
  2865 00002FDE 8B0B                <1> 	mov	ecx, [ebx]
  2866 00002FE0 F6C101              <1> 	test	cl, PDE_A_PRESENT ; present (valid) or not ?
  2867 00002FE3 7428                <1> 	jz	short dapt_2	; parent process does not use this page
  2868 00002FE5 6681E100F0          <1> 	and	cx, PDE_A_CLEAR ; 0F000h ; Clear attribute bits
  2869                              <1> 	; EDI = page table entry index (0-1023)
  2870 00002FEA 89FA                <1> 	mov	edx, edi 
  2871 00002FEC 66C1E202            <1> 	shl	dx, 2 ; *4 
  2872 00002FF0 01CA                <1> 	add	edx, ecx ; PTE offset (for the parent)
  2873 00002FF2 8B1A                <1> 	mov	ebx, [edx]
  2874 00002FF4 F6C301              <1> 	test	bl, PTE_A_PRESENT ; present or not ?
  2875 00002FF7 7414                <1> 	jz	short dapt_2	; parent process does not use this page
  2876 00002FF9 662500F0            <1> 	and	ax, PTE_A_CLEAR ; 0F000h ; Clear attribute bits 
  2877 00002FFD 6681E300F0          <1> 	and	bx, PTE_A_CLEAR ; 0F000h ; Clear attribute bits
  2878 00003002 39D8                <1> 	cmp	eax, ebx	; parent's and child's pages are same ?
  2879 00003004 7507                <1> 	jne	short dapt_2	; not same page
  2880                              <1> 				; deallocate the child's page
  2881 00003006 800A02              <1>         or      byte [edx], PTE_A_WRITE ; convert to writable page (parent)
  2882 00003009 59                  <1> 	pop	ecx
  2883 0000300A 5B                  <1> 	pop	ebx
  2884 0000300B EB0D                <1> 	jmp	short dapt_4
  2885                              <1> 
  2886                              <1> ; 17/04/2021
  2887                              <1> ; ('dapt_1' is disabled as temporary)
  2888                              <1> ;
  2889                              <1> ;dapt_1:
  2890                              <1> ;	or	eax, eax	; swapped page ?
  2891                              <1> ;	jz	short dapt_5	; no
  2892                              <1> ;				; yes
  2893                              <1> ;	shr	eax, 1
  2894                              <1> ;	call	unlink_swap_block ; Deallocate swapped page block
  2895                              <1> ;				  ; on the swap disk (or in file)
  2896                              <1> ;	jmp	short dapt_5
  2897                              <1> dapt_2:
  2898 0000300D 59                  <1> 	pop	ecx
  2899 0000300E 5B                  <1> 	pop	ebx
  2900                              <1> dapt_3:	
  2901                              <1> 	; 12/07/2016
  2902 0000300F 66A90004            <1> 	test	ax, PTE_SHARED ; shared or direct memory access indicator
  2903 00003013 7505                <1> 	jnz	short dapt_4   ; AVL bit 1 = 1, do not deallocate this page!
  2904                              <1> 	;
  2905                              <1> 	;and	ax, PTE_A_CLEAR ; 0F000h ; clear lower 12 (attribute) bits
  2906 00003015 E814000000          <1> 	call	deallocate_page ; set the mem allocation bit of this page
  2907                              <1> dapt_4:
  2908 0000301A C746FC00000000      <1> 	mov	dword [esi-4], 0 ; clear/reset PTE (child, dupl. as parent)
  2909                              <1> dapt_1:	; 17/04/2021 (temporary)
  2910                              <1> dapt_5:
  2911 00003021 47                  <1> 	inc	edi ; page table entry index
  2912 00003022 81FF00040000        <1> 	cmp	edi, PAGE_SIZE / 4 ; 1024
  2913 00003028 729D                <1> 	jb	short dapt_0
  2914                              <1> 	;
  2915 0000302A 58                  <1> 	pop	eax ; *
  2916 0000302B 5A                  <1> 	pop	edx
  2917 0000302C 5F                  <1> 	pop	edi	
  2918 0000302D 5E                  <1> 	pop	esi
  2919                              <1> 	;
  2920                              <1> 	;call	deallocate_page	; deallocate the page table's itself
  2921                              <1> 	;retn
  2922                              <1> 
  2923                              <1> deallocate_page:
  2924                              <1> 	; 15/09/2015
  2925                              <1> 	; 28/04/2015
  2926                              <1> 	; 10/03/2015
  2927                              <1> 	; 17/10/2014
  2928                              <1> 	; 12/10/2014 (Retro UNIX 386 v1 - beginning)
  2929                              <1> 	;
  2930                              <1> 	; INPUT -> 
  2931                              <1> 	;	EAX = PHYSICAL (real/flat) ADDRESS OF THE ALLOCATED PAGE
  2932                              <1> 	; OUTPUT ->
  2933                              <1> 	;	[free_pages] is increased
  2934                              <1> 	;	(corresponding MEMORY ALLOCATION TABLE bit is SET)
  2935                              <1> 	;	CF = 1 if the page is already deallocated
  2936                              <1> 	; 	       (or not allocated) before.  
  2937                              <1> 	;
  2938                              <1> 	; Modified Registers -> EAX
  2939                              <1> 	;
  2940 0000302E 53                  <1> 	push	ebx
  2941 0000302F 52                  <1> 	push	edx
  2942                              <1> 	;
  2943 00003030 C1E80C              <1> 	shr	eax, PAGE_SHIFT      ; shift physical address to 
  2944                              <1> 				     ; 12 bits right
  2945                              <1> 				     ; to get page number
  2946 00003033 89C2                <1> 	mov	edx, eax
  2947                              <1> 	; 15/09/2015
  2948 00003035 C1EA03              <1> 	shr	edx, 3		     ; to get offset to M.A.T.
  2949                              <1> 				     ; (1 allocation bit = 1 page)
  2950                              <1> 				     ; (1 allocation bytes = 8 pages)
  2951 00003038 80E2FC              <1> 	and	dl, 0FCh 	     ; clear lower 2 bits
  2952                              <1> 				     ; (to get 32 bit position)			
  2953                              <1> 	;
  2954 0000303B BB00001000          <1> 	mov	ebx, MEM_ALLOC_TBL   ; Memory Allocation Table address
  2955 00003040 01D3                <1> 	add	ebx, edx
  2956 00003042 83E01F              <1> 	and	eax, 1Fh	     ; lower 5 bits only
  2957                              <1> 				     ; (allocation bit position)	 
  2958 00003045 3B15[C46F0000]      <1> 	cmp 	edx, [next_page]     ; is the new free page address lower
  2959                              <1> 				     ; than the address in 'next_page' ?
  2960                              <1> 				     ; (next/first free page value)		
  2961 0000304B 7306                <1> 	jnb	short dap_1	     ; no	
  2962 0000304D 8915[C46F0000]      <1> 	mov	[next_page], edx     ; yes
  2963                              <1> dap_1:
  2964 00003053 0FAB03              <1> 	bts	[ebx], eax	     ; unlink/release/deallocate page
  2965                              <1> 				     ; set relevant bit to 1.
  2966                              <1> 				     ; set CF to the previous bit value	
  2967                              <1> 	;cmc			     ; complement carry flag	
  2968                              <1> 	;jc	short dap_2	     ; do not increase free_pages count
  2969                              <1> 				     ; if the page is already deallocated
  2970                              <1> 				     ; before.	
  2971 00003056 FF05[C06F0000]      <1>         inc     dword [free_pages]
  2972                              <1> dap_2:
  2973 0000305C 5A                  <1> 	pop	edx
  2974 0000305D 5B                  <1> 	pop	ebx
  2975 0000305E C3                  <1> 	retn
  2976                              <1> 
  2977                              <1> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2978                              <1> ;;                                                              ;;
  2979                              <1> ;; Copyright (C) KolibriOS team 2004-2012. All rights reserved. ;;
  2980                              <1> ;; Distributed under terms of the GNU General Public License    ;;
  2981                              <1> ;;                                                              ;;
  2982                              <1> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2983                              <1> 
  2984                              <1> ;;$Revision: 5057 $
  2985                              <1> 
  2986                              <1> 
  2987                              <1> ;;align 4
  2988                              <1> ;;proc alloc_page
  2989                              <1> 
  2990                              <1> ;;        pushfd
  2991                              <1> ;;        cli
  2992                              <1> ;;        push    ebx
  2993                              <1> ;;;//-
  2994                              <1> ;;        cmp     [pg_data.pages_free], 1
  2995                              <1> ;;        jle     .out_of_memory
  2996                              <1> ;;;//-
  2997                              <1> ;;
  2998                              <1> ;;        mov     ebx, [page_start]
  2999                              <1> ;;        mov     ecx, [page_end]
  3000                              <1> ;;.l1:
  3001                              <1> ;;        bsf     eax, [ebx];
  3002                              <1> ;;        jnz     .found
  3003                              <1> ;;        add     ebx, 4
  3004                              <1> ;;        cmp     ebx, ecx
  3005                              <1> ;;        jb      .l1
  3006                              <1> ;;        pop     ebx
  3007                              <1> ;;        popfd
  3008                              <1> ;;        xor     eax, eax
  3009                              <1> ;;        ret
  3010                              <1> ;;.found:
  3011                              <1> ;;;//-
  3012                              <1> ;;        dec     [pg_data.pages_free]
  3013                              <1> ;;        jz      .out_of_memory
  3014                              <1> ;;;//-
  3015                              <1> ;;        btr     [ebx], eax
  3016                              <1> ;;        mov     [page_start], ebx
  3017                              <1> ;;        sub     ebx, sys_pgmap
  3018                              <1> ;;        lea     eax, [eax+ebx*8]
  3019                              <1> ;;        shl     eax, 12
  3020                              <1> ;;;//-       dec [pg_data.pages_free]
  3021                              <1> ;;        pop     ebx
  3022                              <1> ;;        popfd
  3023                              <1> ;;        ret
  3024                              <1> ;;;//-
  3025                              <1> ;;.out_of_memory:
  3026                              <1> ;;        mov     [pg_data.pages_free], 1
  3027                              <1> ;;        xor     eax, eax
  3028                              <1> ;;        pop     ebx
  3029                              <1> ;;        popfd
  3030                              <1> ;;        ret
  3031                              <1> ;;;//-
  3032                              <1> ;;endp
  3033                              <1> 
  3034                              <1> duplicate_page_dir:
  3035                              <1> 	; 21/09/2015
  3036                              <1> 	; 31/08/2015
  3037                              <1> 	; 20/07/2015
  3038                              <1> 	; 28/04/2015
  3039                              <1> 	; 27/04/2015
  3040                              <1> 	; 18/04/2015
  3041                              <1> 	; 12/04/2015
  3042                              <1> 	; 18/10/2014
  3043                              <1> 	; 16/10/2014 (Retro UNIX 386 v1 - beginning)
  3044                              <1> 	;
  3045                              <1> 	; INPUT -> 
  3046                              <1> 	;	[u.pgdir] = PHYSICAL (real/flat) ADDRESS of the parent's
  3047                              <1> 	;		    page directory.
  3048                              <1> 	; OUTPUT ->
  3049                              <1> 	;	EAX =  PHYSICAL (real/flat) ADDRESS of the child's
  3050                              <1> 	;	       page directory.
  3051                              <1> 	;	(New page directory with new page table entries.)
  3052                              <1> 	;	(New page tables with read only copies of the parent's
  3053                              <1> 	;	pages.)
  3054                              <1> 	;	EAX = 0 -> Error (CF = 1)
  3055                              <1> 	;
  3056                              <1> 	; Modified Registers -> none (except EAX)
  3057                              <1> 	;
  3058 0000305F E802FEFFFF          <1> 	call	allocate_page
  3059 00003064 723E                <1> 	jc	short dpd_err
  3060                              <1> 	;
  3061 00003066 55                  <1> 	push	ebp ; 20/07/2015
  3062 00003067 56                  <1> 	push	esi
  3063 00003068 57                  <1> 	push	edi
  3064 00003069 53                  <1> 	push	ebx
  3065 0000306A 51                  <1> 	push	ecx
  3066 0000306B 8B35[38750000]      <1> 	mov	esi, [u.pgdir]
  3067 00003071 89C7                <1> 	mov	edi, eax
  3068 00003073 50                  <1> 	push	eax ; save child's page directory address
  3069                              <1> 	; 31/08/2015
  3070                              <1> 	; copy PDE 0 from the parent's page dir to the child's page dir
  3071                              <1> 	; (use same system space for all user page tables) 
  3072 00003074 A5                  <1> 	movsd
  3073 00003075 BD00004000          <1> 	mov	ebp, 1024*4096 ; pass the 1st 4MB (system space)
  3074 0000307A B9FF030000          <1> 	mov	ecx, (PAGE_SIZE / 4) - 1 ; 1023
  3075                              <1> dpd_0:	
  3076 0000307F AD                  <1> 	lodsd
  3077                              <1> 	;or	eax, eax
  3078                              <1>         ;jnz     short dpd_1
  3079 00003080 A801                <1> 	test	al, PDE_A_PRESENT ;  bit 0 =  1
  3080 00003082 7508                <1> 	jnz	short dpd_1
  3081                              <1>  	; 20/07/2015 (virtual address at the end of the page table)	
  3082 00003084 81C500004000        <1> 	add	ebp, 1024*4096 ; page size * PTE count
  3083 0000308A EB0F                <1> 	jmp	short dpd_2
  3084                              <1> dpd_1:	
  3085 0000308C 662500F0            <1> 	and	ax, PDE_A_CLEAR ; 0F000h ; clear attribute bits
  3086 00003090 89C3                <1> 	mov	ebx, eax
  3087                              <1> 	; EBX = Parent's page table address
  3088 00003092 E81F000000          <1> 	call	duplicate_page_table
  3089 00003097 720C                <1> 	jc	short dpd_p_err
  3090                              <1> 	; EAX = Child's page table address
  3091 00003099 0C07                <1> 	or	al, PDE_A_PRESENT + PDE_A_WRITE + PDE_A_USER
  3092                              <1> 			 ; set bit 0, bit 1 and bit 2 to 1
  3093                              <1> 			 ; (present, writable, user)
  3094                              <1> dpd_2:
  3095 0000309B AB                  <1> 	stosd
  3096 0000309C E2E1                <1> 	loop	dpd_0
  3097                              <1> 	;
  3098 0000309E 58                  <1> 	pop	eax  ; restore child's page directory address
  3099                              <1> dpd_3:
  3100 0000309F 59                  <1> 	pop	ecx
  3101 000030A0 5B                  <1> 	pop	ebx
  3102 000030A1 5F                  <1> 	pop	edi
  3103 000030A2 5E                  <1> 	pop	esi
  3104 000030A3 5D                  <1> 	pop	ebp ; 20/07/2015
  3105                              <1> dpd_err:
  3106 000030A4 C3                  <1> 	retn
  3107                              <1> dpd_p_err:
  3108                              <1> 	; release the allocated pages missing (recover free space)
  3109 000030A5 58                  <1> 	pop	eax  ; the new page directory address (physical)
  3110 000030A6 8B1D[38750000]      <1> 	mov	ebx, [u.pgdir] ; parent's page directory address 
  3111 000030AC E8E5FEFFFF          <1> 	call 	deallocate_page_dir
  3112 000030B1 29C0                <1> 	sub	eax, eax ; 0
  3113 000030B3 F9                  <1> 	stc
  3114 000030B4 EBE9                <1> 	jmp	short dpd_3	
  3115                              <1> 
  3116                              <1> duplicate_page_table:
  3117                              <1> 	; 31/12/2021 - Retro UNIX 386 v1.2
  3118                              <1> 	; 17/04/2021 - Retro UNIX 386 v2
  3119                              <1> 	;	 (temporary modifications)
  3120                              <1> 	; 16/04/2021 (Retro UNIX 386 v2)
  3121                              <1> 	; 20/02/2017 (TRDOS 386 v2)
  3122                              <1> 	; 21/09/2015
  3123                              <1> 	; 20/07/2015
  3124                              <1> 	; 05/05/2015
  3125                              <1> 	; 28/04/2015
  3126                              <1> 	; 27/04/2015
  3127                              <1> 	; 18/04/2015
  3128                              <1> 	; 18/10/2014
  3129                              <1> 	; 16/10/2014 (Retro UNIX 386 v1 - beginning)
  3130                              <1> 	;
  3131                              <1> 	; INPUT -> 
  3132                              <1> 	;	EBX = PHYSICAL (real/flat) ADDRESS of the parent's page table.
  3133                              <1> 	;       20/02/2017		 
  3134                              <1> 	;	EBP = Linear address of the page (from 'duplicate_page_dir')
  3135                              <1> 	;	      (Linear address = CORE + user's virtual address) 	
  3136                              <1> 	; OUTPUT ->
  3137                              <1> 	;	EAX = PHYSICAL (real/flat) ADDRESS of the child's page table.
  3138                              <1> 	;	      (with 'read only' attribute of page table entries)
  3139                              <1> 	;	20/02/2017
  3140                              <1> 	;	EBP = Next linear page address (for 'duplicate_page_dir')
  3141                              <1> 	;	
  3142                              <1> 	;	CF = 1 -> error 
  3143                              <1> 	;
  3144                              <1> 	; Modified Registers -> EBP (except EAX)
  3145                              <1> 	;
  3146 000030B6 E8ABFDFFFF          <1> 	call	allocate_page
  3147 000030BB 725B                <1> 	jc	short dpt_err
  3148                              <1> 	;
  3149 000030BD 50                  <1> 	push	eax ; *
  3150 000030BE 56                  <1> 	push	esi
  3151 000030BF 57                  <1> 	push	edi
  3152 000030C0 52                  <1> 	push	edx
  3153 000030C1 51                  <1> 	push	ecx
  3154                              <1> 	;
  3155 000030C2 89DE                <1> 	mov	esi, ebx
  3156 000030C4 89C7                <1> 	mov	edi, eax
  3157 000030C6 89C2                <1> 	mov	edx, eax
  3158 000030C8 81C200100000        <1> 	add	edx, PAGE_SIZE 	
  3159                              <1> dpt_0:
  3160 000030CE AD                  <1> 	lodsd
  3161 000030CF 21C0                <1> 	and	eax, eax
  3162 000030D1 7435                <1> 	jz	short dpt_3
  3163 000030D3 A801                <1> 	test	al, PTE_A_PRESENT ;  bit 0 =  1
  3164                              <1> 	; 17/04/2021 (temporary)
  3165 000030D5 7503                <1> 	jnz	short dpt_1
  3166                              <1> 	;jz	short dpt_p_err
  3167                              <1> 	; 31/12/2021
  3168 000030D7 F9                  <1> 	stc
  3169 000030D8 EB39                <1> 	jmp	short dpt_p_err
  3170                              <1> 
  3171                              <1> ; 17/04/2021
  3172                              <1> ; ('reload_page' procedure call is disabled as temporary)
  3173                              <1> ;
  3174                              <1> ;	; 20/07/2015
  3175                              <1> ;	; ebp = virtual (linear) address of the memory page
  3176                              <1> ;	call	reload_page ; 28/04/2015
  3177                              <1> ;	jc	short dpt_p_err
  3178                              <1> dpt_1:
  3179                              <1> 	; 21/09/2015
  3180 000030DA 89C1                <1> 	mov	ecx, eax
  3181 000030DC 662500F0            <1> 	and	ax, PTE_A_CLEAR ; 0F000h ; clear attribute bits
  3182 000030E0 F6C102              <1> 	test	cl, PTE_A_WRITE ; writable page ?
  3183 000030E3 751A                <1> 	jnz	short dpt_2
  3184                              <1> 	; Read only (parent) page
  3185                              <1> 	; 	- there is a third process which uses this page -
  3186                              <1> 	; Allocate a new page for the child process
  3187 000030E5 E87CFDFFFF          <1> 	call	allocate_page
  3188 000030EA 7227                <1> 	jc	short dpt_p_err
  3189 000030EC 57                  <1> 	push	edi
  3190 000030ED 56                  <1> 	push	esi
  3191 000030EE 89CE                <1> 	mov	esi, ecx
  3192 000030F0 89C7                <1> 	mov	edi, eax
  3193 000030F2 B900040000          <1> 	mov	ecx, PAGE_SIZE/4
  3194 000030F7 F3A5                <1> 	rep	movsd	; copy page (4096 bytes)
  3195 000030F9 5E                  <1> 	pop	esi
  3196 000030FA 5F                  <1> 	pop	edi
  3197                              <1> 	;
  3198                              <1> 
  3199                              <1> ; 17/04/2021
  3200                              <1> ; ('add_to_swap_queue' procedure call is disabled as temporary)
  3201                              <1> ; 
  3202                              <1> ;	push	ebx
  3203                              <1> ;	push	eax
  3204                              <1> ;	; 20/07/2015
  3205                              <1> ;	mov	ebx, ebp
  3206                              <1> ;	; ebx = virtual (linear) address of the memory page
  3207                              <1> ;	call	add_to_swap_queue
  3208                              <1> ;	pop	eax
  3209                              <1> ;	pop	ebx
  3210                              <1> 
  3211                              <1> 	; 21/09/2015
  3212 000030FB 0C07                <1> 	or	al, PTE_A_USER+PTE_A_WRITE+PTE_A_PRESENT 
  3213                              <1> 		; user + writable + present page
  3214 000030FD EB09                <1> 	jmp	short dpt_3
  3215                              <1> dpt_2:
  3216                              <1> 	;or	ax, PTE_A_USER+PTE_A_PRESENT 
  3217 000030FF 0C05                <1> 	or	al, PTE_A_USER+PTE_A_PRESENT 
  3218                              <1> 		    ; (read only page!)
  3219 00003101 8946FC              <1> 	mov	[esi-4], eax ; update parent's PTE
  3220 00003104 660D0002            <1> 	or      ax, PTE_DUPLICATED  ; (read only page & duplicated PTE!)
  3221                              <1> dpt_3:
  3222 00003108 AB                  <1> 	stosd  ; EDI points to child's PTE  	 
  3223                              <1> 	;
  3224 00003109 81C500100000        <1> 	add	ebp, 4096 ; 20/07/2015 (next page)
  3225                              <1> 	;
  3226 0000310F 39D7                <1> 	cmp	edi, edx
  3227 00003111 72BB                <1> 	jb	short dpt_0
  3228                              <1> dpt_p_err:
  3229 00003113 59                  <1> 	pop	ecx
  3230 00003114 5A                  <1> 	pop	edx
  3231 00003115 5F                  <1> 	pop	edi
  3232 00003116 5E                  <1> 	pop	esi
  3233 00003117 58                  <1> 	pop	eax ; *
  3234                              <1> dpt_err:
  3235 00003118 C3                  <1> 	retn
  3236                              <1> 
  3237                              <1> page_fault_handler: ; CPU EXCEPTION 0Eh (14) : Page Fault !
  3238                              <1> 	; 31/12/2021 - Retro UNIX 386 v1.2
  3239                              <1> 	; 17/04/2021 - Retro UNIX 386 v2
  3240                              <1> 	;	 (temporary modifications)
  3241                              <1> 	; 21/09/2015
  3242                              <1> 	; 19/09/2015
  3243                              <1> 	; 17/09/2015
  3244                              <1> 	; 28/08/2015
  3245                              <1> 	; 20/07/2015
  3246                              <1> 	; 28/06/2015
  3247                              <1> 	; 03/05/2015
  3248                              <1> 	; 30/04/2015
  3249                              <1> 	; 18/04/2015
  3250                              <1> 	; 12/04/2015
  3251                              <1> 	; 30/10/2014
  3252                              <1> 	; 11/09/2014
  3253                              <1> 	; 10/09/2014 (Retro UNIX 386 v1 - beginning)
  3254                              <1> 	;
  3255                              <1> 	; Note: This is not an interrupt/exception handler.
  3256                              <1> 	;	This is a 'page fault remedy' subroutine 
  3257                              <1> 	;	which will be called by standard/uniform
  3258                              <1> 	;	exception handler.
  3259                              <1> 	;
  3260                              <1> 	; INPUT -> 
  3261                              <1> 	;	[error_code] = 32 bit ERROR CODE (lower 5 bits are valid)
  3262                              <1> 	;
  3263                              <1> 	;	cr2 = the virtual (linear) address 
  3264                              <1> 	;	      which has caused to page fault (19/09/2015)
  3265                              <1> 	;
  3266                              <1> 	; OUTPUT ->
  3267                              <1> 	;	(corresponding PAGE TABLE ENTRY is mapped/set)
  3268                              <1> 	;	EAX = 0 -> no error
  3269                              <1> 	;	EAX > 0 -> error code in EAX (also CF = 1)
  3270                              <1> 	;
  3271                              <1> 	; Modified Registers -> none (except EAX)
  3272                              <1> 	;	
  3273                              <1>         ;
  3274                              <1>         ; ERROR CODE:
  3275                              <1> 	;	 31  .....	4   3	2   1	0
  3276                              <1> 	;	+---+-- --+---+---+---+---+---+---+
  3277                              <1> 	;	|   Reserved  | I | R | U | W | P |
  3278                              <1> 	;	+---+-- --+---+---+---+---+---+---+
  3279                              <1> 	;
  3280                              <1> 	; P : PRESENT -	When set, the page fault was caused by 
  3281                              <1>     	;		a page-protection violation. When not set,
  3282                              <1> 	;		it was caused by a non-present page.
  3283                              <1> 	; W : WRITE   -	When set, the page fault was caused by
  3284                              <1> 	;		a page write. When not set, it was caused
  3285                              <1> 	;		by a page read.
  3286                              <1> 	; U : USER    -	When set, the page fault was caused 
  3287                              <1> 	;		while CPL = 3. 
  3288                              <1> 	;		This does not necessarily mean that
  3289                              <1> 	;		the page fault was a privilege violation.
  3290                              <1> 	; R : RESERVD -	When set, the page fault was caused by
  3291                              <1> 	;     WRITE	reading a 1 in a reserved field.
  3292                              <1> 	; I : INSTRUC -	When set, the page fault was caused by
  3293                              <1> 	;     FETCH	an instruction fetch
  3294                              <1> 	;
  3295                              <1> 	;; x86 (32 bit) VIRTUAL ADDRESS TRANSLATION
  3296                              <1> 	;  31               22                  12 11                    0
  3297                              <1> 	; +-------------------+-------------------+-----------------------+
  3298                              <1>        	; | PAGE DIR. ENTRY # | PAGE TAB. ENTRY # |        OFFSET         |
  3299                              <1>        	; +-------------------+-------------------+-----------------------+
  3300                              <1> 	;
  3301                              <1> 
  3302                              <1> 	;; CR3 REGISTER (Control Register 3)
  3303                              <1> 	;  31                                   12             5 4 3 2   0
  3304                              <1> 	; +---------------------------------------+-------------+---+-----+
  3305                              <1>       	; |                                       |  		|P|P|     |
  3306                              <1>       	; |   PAGE DIRECTORY TABLE BASE ADDRESS   |  reserved	|C|W|rsvrd|
  3307                              <1>       	; |                                       | 		|D|T|     |
  3308                              <1>    	; +---------------------------------------+-------------+---+-----+
  3309                              <1> 	;
  3310                              <1> 	;	PWT    - WRITE THROUGH
  3311                              <1> 	;	PCD    - CACHE DISABLE		
  3312                              <1> 	;
  3313                              <1> 	;
  3314                              <1> 	;; x86 PAGE DIRECTORY ENTRY (4 KByte Page)
  3315                              <1> 	;  31                                   12 11  9 8 7 6 5 4 3 2 1 0
  3316                              <1> 	; +---------------------------------------+-----+---+-+-+---+-+-+-+
  3317                              <1>       	; |                                       |     | | | | |P|P|U|R| |
  3318                              <1>       	; |     PAGE TABLE BASE ADDRESS 31..12    | AVL |G|0|D|A|C|W|/|/|P|
  3319                              <1>       	; |                                       |     | | | | |D|T|S|W| |
  3320                              <1>    	; +---------------------------------------+-----+---+-+-+---+-+-+-+
  3321                              <1> 	;
  3322                              <1>         ;       P      - PRESENT
  3323                              <1>         ;       R/W    - READ/WRITE
  3324                              <1>         ;       U/S    - USER/SUPERVISOR
  3325                              <1> 	;	PWT    - WRITE THROUGH
  3326                              <1> 	;	PCD    - CACHE DISABLE	
  3327                              <1> 	;	A      - ACCESSED	
  3328                              <1>         ;       D      - DIRTY (IGNORED)
  3329                              <1> 	;	PAT    - PAGE ATTRIBUTE TABLE INDEX (CACHE BEHAVIOR)
  3330                              <1> 	;	G      - GLOBAL	(IGNORED) 
  3331                              <1>         ;       AVL    - AVAILABLE FOR SYSTEMS PROGRAMMER USE
  3332                              <1> 	;
  3333                              <1> 	;
  3334                              <1> 	;; x86 PAGE TABLE ENTRY (4 KByte Page)
  3335                              <1> 	;  31                                   12 11  9 8 7 6 5 4 3 2 1 0
  3336                              <1> 	; +---------------------------------------+-----+---+-+-+---+-+-+-+
  3337                              <1>       	; |                                       |     | |P| | |P|P|U|R| |
  3338                              <1>       	; |     PAGE FRAME BASE ADDRESS 31..12    | AVL |G|A|D|A|C|W|/|/|P|
  3339                              <1>       	; |                                       |     | |T| | |D|T|S|W| |
  3340                              <1>    	; +---------------------------------------+-----+---+-+-+---+-+-+-+
  3341                              <1> 	;
  3342                              <1>         ;       P      - PRESENT
  3343                              <1>         ;       R/W    - READ/WRITE
  3344                              <1>         ;       U/S    - USER/SUPERVISOR
  3345                              <1> 	;	PWT    - WRITE THROUGH
  3346                              <1> 	;	PCD    - CACHE DISABLE	
  3347                              <1> 	;	A      - ACCESSED	
  3348                              <1>         ;       D      - DIRTY
  3349                              <1> 	;	PAT    - PAGE ATTRIBUTE TABLE INDEX (CACHE BEHAVIOR)
  3350                              <1> 	;	G      - GLOBAL	 
  3351                              <1>         ;       AVL    - AVAILABLE FOR SYSTEMS PROGRAMMER USE
  3352                              <1> 	;
  3353                              <1> 	;
  3354                              <1> 	;; 80386 PAGE TABLE ENTRY (4 KByte Page)
  3355                              <1> 	;  31                                   12 11  9 8 7 6 5 4 3 2 1 0
  3356                              <1> 	; +---------------------------------------+-----+-+-+-+-+---+-+-+-+
  3357                              <1>       	; |                                       |     | | | | | | |U|R| |
  3358                              <1>       	; |     PAGE FRAME BASE ADDRESS 31..12    | AVL |0|0|D|A|0|0|/|/|P|
  3359                              <1>       	; |                                       |     | | | | | | |S|W| |
  3360                              <1>       	; +---------------------------------------+-----+-+-+-+-+---+-+-+-+
  3361                              <1> 	;
  3362                              <1>         ;       P      - PRESENT
  3363                              <1>         ;       R/W    - READ/WRITE
  3364                              <1>         ;       U/S    - USER/SUPERVISOR
  3365                              <1>         ;       D      - DIRTY
  3366                              <1>         ;       AVL    - AVAILABLE FOR SYSTEMS PROGRAMMER USE
  3367                              <1> 	;
  3368                              <1>         ;       NOTE: 0 INDICATES INTEL RESERVED. DO NOT DEFINE.
  3369                              <1> 	;
  3370                              <1> 	;
  3371                              <1> 	;; Invalid Page Table Entry
  3372                              <1> 	; 31                                                           1 0
  3373                              <1>       	; +-------------------------------------------------------------+-+
  3374                              <1>       	; |                                                             | |
  3375                              <1>       	; |                          AVAILABLE                          |0|
  3376                              <1>       	; |                                                             | |
  3377                              <1>       	; +-------------------------------------------------------------+-+
  3378                              <1> 	;
  3379                              <1> 
  3380 00003119 53                  <1> 	push	ebx
  3381 0000311A 52                  <1> 	push	edx
  3382 0000311B 51                  <1> 	push	ecx
  3383                              <1> 	;
  3384                              <1> 	; 21/09/2015 (debugging)
  3385 0000311C FF05[50750000]      <1> 	inc	dword [u.pfcount] ; page fault count for running process
  3386 00003122 FF05[70700000]      <1> 	inc	dword [PF_Count]  ; total page fault count	
  3387                              <1> 	; 28/06/2015
  3388                              <1> 	;mov	edx, [error_code] ; Lower 5 bits are valid
  3389 00003128 8A15[68700000]      <1> 	mov	dl, [error_code]
  3390                              <1> 	;
  3391 0000312E F6C201              <1> 	test	dl, 1	; page fault was caused by a non-present page
  3392                              <1> 			; sign
  3393 00003131 7416                <1> 	jz	short pfh_alloc_np
  3394                              <1> 	; 
  3395                              <1> 	; If it is not a 'write on read only page' type page fault
  3396                              <1> 	; major page fault error with minor reason must be returned without 
  3397                              <1> 	; fixing the problem. 'sys_exit with error' will be needed
  3398                              <1> 	; after return here!
  3399                              <1> 	; Page fault will be remedied, by copying page contents
  3400                              <1> 	; to newly allocated page with write permission;
  3401                              <1> 	; sys_fork -> sys_exec -> copy on write, demand paging method is 
  3402                              <1> 	; used for working with minimum possible memory usage. 
  3403                              <1> 	; sys_fork will duplicate page directory and tables of parent  
  3404                              <1> 	; process with 'read only' flag. If the child process attempts to
  3405                              <1> 	; write on these read only pages, page fault will be directed here
  3406                              <1> 	; for allocating a new page with same data/content. 
  3407                              <1> 	;
  3408                              <1> 	; IMPORTANT : Retro UNIX 386 v1 (and SINGLIX and TR-DOS)
  3409                              <1> 	; will not force to separate CODE and DATA space 
  3410                              <1> 	; in a process/program... 
  3411                              <1> 	; CODE segment/section may contain DATA!
  3412                              <1> 	; It is flat, smoth and simplest programming method already as in 
  3413                              <1> 	; Retro UNIX 8086 v1 and MS-DOS programs.
  3414                              <1> 	;	
  3415 00003133 F6C202              <1> 	test	dl, 2	; page fault was caused by a page write
  3416                              <1> 			; sign
  3417 00003136 744F                <1>         jz	short pfh_p_err
  3418                              <1> 	; 31/08/2015
  3419 00003138 F6C204              <1> 	test	dl, 4	; page fault was caused while CPL = 3 (user mode)
  3420                              <1> 			; sign.  (U+W+P = 4+2+1 = 7)
  3421 0000313B 744A                <1>         jz	short pfh_pv_err
  3422                              <1> 	;
  3423                              <1> 	; make a new page and copy the parent's page content
  3424                              <1> 	; as the child's new page content
  3425                              <1> 	;
  3426 0000313D 0F20D3              <1> 	mov	ebx, cr2 ; CR2 contains the linear address 
  3427                              <1> 			 ; which has caused to page fault
  3428 00003140 E87C000000          <1> 	call 	copy_page
  3429 00003145 7239                <1>         jc	short pfh_im_err ; insufficient memory
  3430                              <1> 	;
  3431 00003147 EB72                <1>         jmp     pfh_cpp_ok
  3432                              <1> 	;
  3433                              <1> pfh_alloc_np:
  3434 00003149 E818FDFFFF          <1> 	call	allocate_page	; (allocate a new page)
  3435 0000314E 7230                <1>         jc	short pfh_im_err ; 'insufficient memory' error
  3436                              <1> pfh_chk_cpl:
  3437                              <1> 	; EAX = Physical (base) address of the allocated (new) page
  3438                              <1> 		; (Lower 12 bits are ZERO, because 
  3439                              <1> 		;	the address is on a page boundary)
  3440 00003150 80E204              <1> 	and	dl, 4	; CPL = 3 ?
  3441 00003153 7505                <1> 	jnz	short pfh_um
  3442                              <1> 			; Page fault handler for kernel/system mode (CPL=0)		
  3443 00003155 0F20DB              <1> 	mov	ebx, cr3 ; CR3 (Control Register 3) contains physical address
  3444                              <1> 			 ; of the current/active page directory
  3445                              <1> 			 ; (Always kernel/system mode page directory, here!)
  3446                              <1> 			 ; Note: Lower 12 bits are 0. (page boundary)
  3447 00003158 EB06                <1> 	jmp	short pfh_get_pde
  3448                              <1> 	;
  3449                              <1> pfh_um:			; Page fault handler for user/appl. mode (CPL=3)
  3450 0000315A 8B1D[38750000]      <1>  	mov	ebx, [u.pgdir] ; Page directory of current/active process
  3451                              <1> 			; Physical address of the USER's page directory
  3452                              <1> 			; Note: Lower 12 bits are 0. (page boundary)
  3453                              <1> pfh_get_pde:
  3454 00003160 80CA03              <1> 	or	dl, 3	; USER + WRITE + PRESENT or SYSTEM + WRITE + PRESENT
  3455 00003163 0F20D1              <1> 	mov	ecx, cr2 ; CR2 contains the virtual address 
  3456                              <1> 			 ; which has been caused to page fault
  3457                              <1> 			 ;
  3458 00003166 C1E914              <1> 	shr	ecx, 20	 ; shift 20 bits right
  3459 00003169 80E1FC              <1> 	and	cl, 0FCh ; mask lower 2 bits to get PDE offset		
  3460                              <1> 	;
  3461 0000316C 01CB                <1> 	add	ebx, ecx ; now, EBX points to the relevant page dir entry 
  3462 0000316E 8B0B                <1> 	mov	ecx, [ebx] ; physical (base) address of the page table 	
  3463 00003170 F6C101              <1> 	test	cl, 1	 ; check bit 0 is set (1) or not (0).
  3464 00003173 741A                <1> 	jz	short pfh_set_pde ; Page directory entry is not valid,
  3465                              <1> 			  	  ; set/validate page directory entry
  3466 00003175 6681E100F0          <1> 	and	cx, PDE_A_CLEAR ; 0F000h ; Clear attribute bits
  3467 0000317A 89CB                <1> 	mov	ebx, ecx ; Physical address of the page table
  3468 0000317C 89C1                <1> 	mov	ecx, eax ; new page address (physical) 	
  3469 0000317E EB25                <1> 	jmp	short pfh_get_pte
  3470                              <1> 
  3471                              <1> 	; 31/12/2021 (short jump)
  3472                              <1> pfh_im_err:
  3473 00003180 B8E4000000          <1> 	mov	eax, ERR_MAJOR_PF + ERR_MINOR_IM ; Error code in AX
  3474                              <1> 			; Major (Primary) Error: Page Fault
  3475                              <1> 			; Minor (Secondary) Error: Insufficient Memory !
  3476 00003185 EB36                <1> 	jmp	short pfh_err_retn
  3477                              <1> 
  3478                              <1> 	; 31/12/2021
  3479                              <1> pfh_p_err: ; 09/03/2015
  3480                              <1> pfh_pv_err:
  3481                              <1> 	; Page fault was caused by a protection-violation
  3482 00003187 B8E6000000          <1> 	mov	eax, ERR_MAJOR_PF + ERR_MINOR_PV ; Error code in AX
  3483                              <1> 			; Major (Primary) Error: Page Fault
  3484                              <1> 			; Minor (Secondary) Error: Protection violation !
  3485 0000318C F9                  <1> 	stc
  3486 0000318D EB2E                <1> 	jmp	short pfh_err_retn
  3487                              <1> 
  3488                              <1> pfh_set_pde:
  3489                              <1> 	;; NOTE: Page directories and page tables never be swapped out!
  3490                              <1> 	;;	 (So, we know this PDE is empty or invalid)
  3491                              <1> 	;
  3492 0000318F 08D0                <1> 	or	al, dl	 ; lower 3 bits are used as U/S, R/W, P flags
  3493 00003191 8903                <1> 	mov	[ebx], eax ; Let's put the new page directory entry here !
  3494 00003193 30C0                <1> 	xor	al, al	 ; clear lower (3..8) bits
  3495 00003195 89C3                <1> 	mov	ebx, eax
  3496 00003197 E8CAFCFFFF          <1> 	call	allocate_page	 ; (allocate a new page)
  3497 0000319C 72E2                <1> 	jc	short pfh_im_err   ; 'insufficient memory' error
  3498                              <1> pfh_spde_1:
  3499                              <1> 	; EAX = Physical (base) address of the allocated (new) page
  3500 0000319E 89C1                <1> 	mov	ecx, eax
  3501 000031A0 E832FDFFFF          <1> 	call	clear_page ; Clear page content
  3502                              <1> pfh_get_pte:
  3503 000031A5 0F20D0              <1> 	mov	eax, cr2 ; virtual address
  3504                              <1> 			 ; which has been caused to page fault
  3505 000031A8 89C7                <1> 	mov	edi, eax ; 20/07/2015
  3506 000031AA C1E80C              <1> 	shr	eax, 12	 ; shift 12 bit right to get 
  3507                              <1> 			 ; higher 20 bits of the page fault address 
  3508 000031AD 25FF030000          <1> 	and	eax, 3FFh ; mask PDE# bits, the result is PTE# (0 to 1023)
  3509 000031B2 C1E002              <1> 	shl	eax, 2	; shift 2 bits left to get PTE offset
  3510 000031B5 01C3                <1> 	add	ebx, eax ; now, EBX points to the relevant page table entry 
  3511                              <1> ; 17/04/2021 temporary
  3512                              <1> ;	mov	eax, [ebx] ; get previous value of pte
  3513                              <1> ;		; bit 0 of EAX is always 0 (otherwise we would not be here)
  3514                              <1> ; 17/04/2021
  3515                              <1> ; ('swap_in' procedure call has been disabled as temporary)
  3516                              <1> ;
  3517                              <1> ;	and	eax, eax
  3518                              <1> ;	jz	short pfh_gpte_1
  3519                              <1> ;	; 20/07/2015
  3520                              <1> ;	xchg	ebx, ecx ; new page address (physical)
  3521                              <1> ;	push	ebp ; 20/07/2015
  3522                              <1> ;	mov	ebp, cr2
  3523                              <1> ;		; ECX = physical address of the page table entry
  3524                              <1> ;		; EBX = Memory page address (physical!)
  3525                              <1> ;		; EAX = Swap disk (offset) address
  3526                              <1> ;		; EBP = virtual address (page fault address)
  3527                              <1> ;	call	swap_in
  3528                              <1> ;	pop	ebp
  3529                              <1> ;	jc      short pfh_err_retn
  3530                              <1> ;	xchg	ecx, ebx
  3531                              <1> ;		; EBX = physical address of the page table entry
  3532                              <1> ;		; ECX = new page
  3533                              <1> pfh_gpte_1:
  3534 000031B7 08D1                <1> 	or	cl, dl	; lower 3 bits are used as U/S, R/W, P flags
  3535 000031B9 890B                <1> 	mov	[ebx], ecx ; Let's put the new page table entry here !
  3536                              <1> pfh_cpp_ok:
  3537                              <1> ; 17/04/2021
  3538                              <1> ; ('add_to_swap_queue' procedure call has been disabled as temporary)
  3539                              <1> ;
  3540                              <1> ;	; 20/07/2015
  3541                              <1> ;	mov	ebx, cr2
  3542                              <1> ;	call 	add_to_swap_queue
  3543                              <1> 	;
  3544                              <1> 	; The new PTE (which contains the new page) will be added to 
  3545                              <1> 	; the swap queue, here. 
  3546                              <1> 	; (Later, if memory will become insufficient, 
  3547                              <1> 	; one page will be swapped out which is at the head of 
  3548                              <1> 	; the swap queue by using FIFO and access check methods.)
  3549                              <1> 	;
  3550 000031BB 31C0                <1> 	xor	eax, eax  ; 0
  3551                              <1> 	;
  3552                              <1> pfh_err_retn:
  3553 000031BD 59                  <1> 	pop	ecx
  3554 000031BE 5A                  <1> 	pop	edx
  3555 000031BF 5B                  <1> 	pop	ebx
  3556 000031C0 C3                  <1> 	retn 
  3557                              <1> 	
  3558                              <1> copy_page:
  3559                              <1> 	; 16/04/2021
  3560                              <1> 	; 19/04/2020 - Retro UNIX 386 v2
  3561                              <1> 	; 22/09/2015
  3562                              <1> 	; 21/09/2015
  3563                              <1> 	; 19/09/2015
  3564                              <1> 	; 07/09/2015
  3565                              <1> 	; 31/08/2015
  3566                              <1> 	; 20/07/2015
  3567                              <1> 	; 05/05/2015
  3568                              <1> 	; 03/05/2015
  3569                              <1> 	; 18/04/2015
  3570                              <1> 	; 12/04/2015
  3571                              <1> 	; 30/10/2014
  3572                              <1> 	; 18/10/2014 (Retro UNIX 386 v1 - beginning)
  3573                              <1> 	;
  3574                              <1> 	; INPUT -> 
  3575                              <1> 	;	EBX = Virtual (linear) address of source page
  3576                              <1> 	;	     (Page fault address)
  3577                              <1> 	; OUTPUT ->
  3578                              <1> 	;	EAX = PHYSICAL (real/flat) ADDRESS OF THE ALLOCATED PAGE
  3579                              <1> 	;	(corresponding PAGE TABLE ENTRY is mapped/set)
  3580                              <1> 	;	EAX = 0 (CF = 1) 
  3581                              <1> 	;		if there is not a free page to be allocated
  3582                              <1> 	;	(page content of the source page will be copied
  3583                              <1> 	;	onto the target/new page) 	
  3584                              <1> 	;
  3585                              <1> 	; Modified Registers -> ecx, ebx (except EAX)
  3586                              <1> 	;
  3587                              <1> 
  3588                              <1> 	; 19/04/2020 - Retro UNIX 386 v2
  3589                              <1> 	; INPUT: 
  3590                              <1> 	;	EBX = Virtual (linear) address of source page
  3591                              <1> 	;	     (Page fault address)
  3592                              <1> 	; OUTPUT:
  3593                              <1> 	;	EAX = PHYSICAL (real/flat) ADDRESS OF THE ALLOCATED PAGE
  3594                              <1> 	;	(corresponding PAGE TABLE ENTRY is mapped/set)
  3595                              <1> 	;	EAX = 0 (CF = 1) 
  3596                              <1> 	;		if there is not a free page to be allocated
  3597                              <1> 	;	(page content of the source page will be copied
  3598                              <1> 	;	onto the target/new page) 	
  3599                              <1> 	;
  3600                              <1> 	; Modified Registers -> ecx, ebx (except EAX) ; 16/04/2021
  3601                              <1> 	
  3602 000031C1 56                  <1> 	push	esi ; *
  3603 000031C2 57                  <1> 	push	edi ; **
  3604                              <1> 	; 16/04/2021
  3605                              <1> 	; 19/04/2020 - Retro UNIX 386 v2
  3606                              <1> 	;push	ebx ; ***
  3607                              <1> 	;push	ecx ; ****
  3608 000031C3 31F6                <1> 	xor 	esi, esi
  3609 000031C5 C1EB0C              <1> 	shr	ebx, 12 ; shift 12 bits right to get PDE & PTE numbers
  3610 000031C8 89D9                <1> 	mov	ecx, ebx ; save page fault address (as 12 bit shifted)
  3611 000031CA C1EB08              <1> 	shr	ebx, 8	 ; shift 8 bits right and then
  3612 000031CD 80E3FC              <1> 	and	bl, 0FCh ; mask lower 2 bits to get PDE offset	
  3613 000031D0 89DF                <1> 	mov 	edi, ebx ; save it for the parent of current process
  3614 000031D2 031D[38750000]      <1> 	add	ebx, [u.pgdir] ; EBX points to the relevant page dir entry 
  3615 000031D8 8B03                <1> 	mov	eax, [ebx] ; physical (base) address of the page table
  3616 000031DA 662500F0            <1> 	and	ax, PTE_A_CLEAR ; 0F000h ; clear attribute bits 	
  3617 000031DE 89CB                <1> 	mov	ebx, ecx   ; (restore higher 20 bits of page fault address)
  3618 000031E0 81E3FF030000        <1> 	and	ebx, 3FFh  ; mask PDE# bits, the result is PTE# (0 to 1023)
  3619 000031E6 66C1E302            <1> 	shl	bx, 2	   ; shift 2 bits left to get PTE offset
  3620 000031EA 01C3                <1> 	add	ebx, eax   ; EBX points to the relevant page table entry 
  3621                              <1> 	; 07/09/2015
  3622 000031EC 66F7030002          <1>         test    word [ebx], PTE_DUPLICATED ; (Does current process share this
  3623                              <1> 				     ; read only page as a child process?)	
  3624 000031F1 7509                <1> 	jnz	short cpp_0 ; yes
  3625 000031F3 8B0B                <1> 	mov	ecx, [ebx] ; PTE value
  3626 000031F5 6681E100F0          <1> 	and	cx, PTE_A_CLEAR ; 0F000h  ; clear page attributes
  3627 000031FA EB32                <1> 	jmp	short cpp_1
  3628                              <1> cpp_0:
  3629 000031FC 89FE                <1> 	mov	esi, edi
  3630 000031FE 0335[3C750000]      <1> 	add	esi, [u.ppgdir] ; the parent's page directory entry
  3631 00003204 8B06                <1> 	mov	eax, [esi] ; physical (base) address of the page table
  3632 00003206 662500F0            <1> 	and	ax, PTE_A_CLEAR ; 0F000h ; clear attribute bits
  3633 0000320A 89CE                <1> 	mov	esi, ecx   ; (restore higher 20 bits of page fault address)	
  3634 0000320C 81E6FF030000        <1> 	and	esi, 3FFh  ; mask PDE# bits, the result is PTE# (0 to 1023)
  3635 00003212 66C1E602            <1> 	shl	si, 2	   ; shift 2 bits left to get PTE offset
  3636 00003216 01C6                <1> 	add	esi, eax   ; EDX points to the relevant page table entry  	
  3637 00003218 8B0E                <1> 	mov	ecx, [esi] ; PTE value of the parent process
  3638                              <1> 	; 21/09/2015
  3639 0000321A 8B03                <1> 	mov	eax, [ebx] ; PTE value of the child process
  3640 0000321C 662500F0            <1> 	and	ax, PTE_A_CLEAR ; 0F000h ; clear page attributes	
  3641                              <1> 	;
  3642 00003220 F6C101              <1> 	test	cl, PTE_A_PRESENT ; is it a present/valid page ?
  3643 00003223 7424                <1> 	jz	short cpp_3 ; the parent's page is not same page  	
  3644                              <1> 	;
  3645 00003225 6681E100F0          <1> 	and	cx, PTE_A_CLEAR ; 0F000h ; clear page attributes
  3646 0000322A 39C8                <1> 	cmp	eax, ecx   ; Same page?	
  3647 0000322C 751B                <1> 	jne	short cpp_3 ; Parent page and child page are not same 
  3648                              <1> 			    ; Convert child's page to writable page
  3649                              <1> cpp_1:
  3650 0000322E E833FCFFFF          <1> 	call	allocate_page
  3651 00003233 721A                <1> 	jc	short cpp_4 ; 'insufficient memory' error
  3652 00003235 21F6                <1> 	and	esi, esi    ; check ESI is valid or not
  3653 00003237 7405                <1> 	jz	short cpp_2
  3654                              <1> 		; Convert read only page to writable page 
  3655                              <1> 		;(for the parent of the current process)
  3656                              <1> 	;and	word [esi], PTE_A_CLEAR ; 0F000h
  3657                              <1> 	; 22/09/2015
  3658 00003239 890E                <1> 	mov	[esi], ecx
  3659 0000323B 800E07              <1> 	or	byte [esi], PTE_A_PRESENT + PTE_A_WRITE + PTE_A_USER
  3660                              <1> 				 ; 1+2+4 = 7
  3661                              <1> cpp_2:
  3662 0000323E 89C7                <1> 	mov	edi, eax ; new page address of the child process
  3663                              <1> 	; 07/09/2015
  3664 00003240 89CE                <1> 	mov	esi, ecx ; the page address of the parent process
  3665 00003242 B900040000          <1> 	mov	ecx, PAGE_SIZE / 4
  3666 00003247 F3A5                <1> 	rep	movsd ; 31/08/2015
  3667                              <1> cpp_3:		
  3668 00003249 0C07                <1> 	or	al, PTE_A_PRESENT + PTE_A_WRITE + PTE_A_USER ; 1+2+4 = 7
  3669 0000324B 8903                <1> 	mov	[ebx], eax ; Update PTE
  3670 0000324D 28C0                <1> 	sub	al, al ; clear attributes
  3671                              <1> cpp_4:
  3672                              <1> 	; 16/04/2021
  3673                              <1> 	; 19/04/2020 - Retro UNIX 386 v2
  3674                              <1> 	;pop	ecx ; ****
  3675                              <1> 	;pop	ebx ; ***
  3676 0000324F 5F                  <1> 	pop	edi ; **
  3677 00003250 5E                  <1> 	pop	esi ; *
  3678 00003251 C3                  <1> 	retn
  3679                              <1> 
  3680                              <1> ;; 28/04/2015
  3681                              <1> ;; 24/10/2014
  3682                              <1> ;; 21/10/2014 (Retro UNIX 386 v1 - beginning)
  3683                              <1> ;; SWAP_PAGE_QUEUE (4096 bytes)
  3684                              <1> ;;
  3685                              <1> ;;   0000   0001   0002   0003   ....   1020   1021   1022   1023	
  3686                              <1> ;; +------+------+------+------+-    -+------+------+------+------+
  3687                              <1> ;; |  pg1 |  pg2 |  pg3 |  pg4 | .... |pg1021|pg1022|pg1023|pg1024|
  3688                              <1> ;; +------+------+------+------+-    -+------+------+------+------+    
  3689                              <1> ;;
  3690                              <1> ;; [swpq_last] = 0 to 4096 (step 4) -> the last position on the queue
  3691                              <1> ;;
  3692                              <1> ;; Method:
  3693                              <1> ;;	Swap page queue is a list of allocated pages with physical
  3694                              <1> ;;	addresses (system mode virtual adresses = physical addresses).
  3695                              <1> ;;	It is used for 'swap_in' and 'swap_out' procedures.
  3696                              <1> ;;	When a new page is being allocated, swap queue is updated
  3697                              <1> ;;	by 'swap_queue_shift' procedure, header of the queue (offset 0)
  3698                              <1> ;;	is checked for 'accessed' flag. If the 1st page on the queue
  3699                              <1> ;;	is 'accessed' or 'read only', it is dropped from the list;
  3700                              <1> ;;	other pages from the 2nd to the last (in [swpq_last]) shifted
  3701                              <1> ;; 	to head then the 2nd page becomes the 1st and '[swpq_last]' 
  3702                              <1> ;;	offset value becomes it's previous offset value - 4.
  3703                              <1> ;;	If the 1st page of the swap page queue is not 'accessed'	
  3704                              <1> ;;	the queue/list is not shifted.
  3705                              <1> ;;	After the queue/list shift, newly allocated page is added
  3706                              <1> ;;	to the tail of the queue at the [swpq_count*4] position.
  3707                              <1> ;;	But, if [swpq_count] > 1023, the newly allocated page
  3708                              <1> ;;	will not be added to the tail of swap page queue.  		 
  3709                              <1> ;;	
  3710                              <1> ;;	During 'swap_out' procedure, swap page queue is checked for
  3711                              <1> ;;	the first non-accessed, writable page in the list, 
  3712                              <1> ;;	from the head to the tail. The list is shifted to left 
  3713                              <1> ;;	(to the head) till a non-accessed page will be found in the list.
  3714                              <1> ;;	Then, this page	is swapped out (to disk) and then it is dropped
  3715                              <1> ;;	from the list by a final swap queue shift. [swpq_count] value
  3716                              <1> ;;	is changed. If all pages on the queue' are 'accessed', 
  3717                              <1> ;;	'insufficient memory' error will be returned ('swap_out' 
  3718                              <1> ;;	procedure will be failed)...
  3719                              <1> ;;
  3720                              <1> ;;	Note: If the 1st page of the queue is an 'accessed' page,
  3721                              <1> ;;	'accessed' flag of the page will be reset (0) and that page
  3722                              <1> ;;	(PTE) will be added to the tail of the queue after
  3723                              <1> ;;	the check, if [swpq_count] < 1023. If [swpq_count] = 1024
  3724                              <1> ;;	the queue will be rotated and the PTE in the head will be
  3725                              <1> ;;	added to the tail after resetting 'accessed' bit. 
  3726                              <1> ;;
  3727                              <1> ;;
  3728                              <1> ;;	
  3729                              <1> ;; SWAP DISK/FILE (with 4096 bytes swapped page blocks)
  3730                              <1> ;;
  3731                              <1> ;;  00000000  00000004  00000008  0000000C   ...   size-8    size-4
  3732                              <1> ;; +---------+---------+---------+---------+-- --+---------+---------+
  3733                              <1> ;; |descriptr| page(1) | page(2) | page(3) | ... |page(n-1)| page(n) |
  3734                              <1> ;; +---------+---------+---------+---------+-- --+---------+---------+    
  3735                              <1> ;;
  3736                              <1> ;; [swpd_next] = the first free block address in swapped page records
  3737                              <1> ;;    		 for next free block search by 'swap_out' procedure.
  3738                              <1> ;; [swpd_size] = swap disk/file size in sectors (512 bytes)
  3739                              <1> ;;		 NOTE: max. possible swap disk size is 1024 GB
  3740                              <1> ;; 		 (entire swap space must be accessed by using
  3741                              <1> ;;		 31 bit offset address) 
  3742                              <1> ;; [swpd_free] = free block (4096 bytes) count in swap disk/file space
  3743                              <1> ;; [swpd_start] = absolute/start address of the swap disk/file
  3744                              <1> ;;		  0 for file, or beginning sector of the swap partition
  3745                              <1> ;; [swp_drv] = logical drive description table addr. of swap disk/file
  3746                              <1> ;;
  3747                              <1> ;; 					
  3748                              <1> ;; Method:
  3749                              <1> ;;	When the memory (ram) becomes insufficient, page allocation
  3750                              <1> ;;	procedure swaps out a page from memory to the swap disk 
  3751                              <1> ;;	(partition) or swap file to get a new free page at the memory.
  3752                              <1> ;;	Swapping out is performed by using swap page queue.
  3753                              <1> ;;
  3754                              <1> ;; 	Allocation block size of swap disk/file is equal to page size
  3755                              <1> ;;	(4096 bytes). Swapping address (in sectors) is recorded
  3756                              <1> ;;	into relevant page file entry as 31 bit physical (logical)
  3757                              <1> ;;	offset address as 1 bit shifted to left for present flag (0).
  3758                              <1> ;;	Swapped page address is between 1 and swap disk/file size - 4.	  
  3759                              <1> ;;	Absolute physical (logical) address of the swapped page is 
  3760                              <1> ;;	calculated by adding offset value to the swap partition's 
  3761                              <1> ;;	start address. If the swap device (disk) is a virtual disk 
  3762                              <1> ;;	or it is a file, start address of the swap disk/volume is 0, 
  3763                              <1> ;;	and offset value is equal to absolute (physical or logical)
  3764                              <1> ;;	address/position. (It has not to be ZERO if the swap partition 
  3765                              <1> ;;	is in a partitioned virtual hard disk.) 
  3766                              <1> ;;
  3767                              <1> ;;	Note: Swap addresses are always specified/declared in sectors, 
  3768                              <1> ;;	not in bytes or	in blocks/zones/clusters (4096 bytes) as unit.
  3769                              <1> ;;
  3770                              <1> ;;	Swap disk/file allocation is mapped via 'Swap Allocation Table'
  3771                              <1> ;;	at memory as similar to 'Memory Allocation Table'.
  3772                              <1> ;;
  3773                              <1> ;;	Every bit of Swap Allocation Table repsesents one swap block
  3774                              <1> ;;	(equal to page size) respectively. Bit 0 of the S.A.T. byte 0
  3775                              <1> ;;	is reserved for swap disk/file block 0 as descriptor block
  3776                              <1> ;;	(also for compatibility with PTE). If bit value is ZERO,
  3777                              <1> ;;	it means relevant (respective) block is in use, and, 
  3778                              <1> ;;	of course, if bit value is 1, it means relevant (respective)
  3779                              <1> ;;      swap disk/file block is free.
  3780                              <1> ;;	For example: bit 1 of the byte 128 repsesents block 1025 
  3781                              <1> ;;	(128*8+1) or sector (offset) 8200 on the swap disk or
  3782                              <1> ;;	byte (offset/position) 4198400 in the swap file. 
  3783                              <1> ;;	4GB swap space is represented via 128KB Swap Allocation Table.
  3784                              <1> ;;	Initial layout of Swap Allocation Table is as follows:
  3785                              <1> ;;	------------------------------------------------------------
  3786                              <1> ;;	0111111111111111111111111 .... 11111111111111111111111111111
  3787                              <1> ;;	------------------------------------------------------------
  3788                              <1> ;;	(0 is reserved block, 1s represent free blocks respectively.)
  3789                              <1> ;;	(Note: Allocation cell/unit of the table is bit, not byte)
  3790                              <1> ;;
  3791                              <1> ;;	..............................................................
  3792                              <1> ;;
  3793                              <1> ;;	'swap_out' procedure checks 'free_swap_blocks' count at first,
  3794                              <1> ;;	then it searches Swap Allocation Table if free count is not
  3795                              <1> ;;	zero. From begining the [swpd_next] dword value, the first bit 
  3796                              <1> ;;	position with value of 1 on the table is converted to swap
  3797                              <1> ;;	disk/file offset address, in sectors (not 4096 bytes block).
  3798                              <1> ;;	'ldrv_write' procedure is called with ldrv (logical drive
  3799                              <1> ;;	number of physical swap disk or virtual swap disk)
  3800                              <1> ;;	number, sector offset (not absolute sector -LBA- number),
  3801                              <1> ;;	and sector count (8, 512*8 = 4096) and buffer adress
  3802                              <1> ;;	(memory page). That will be a direct disk write procedure.
  3803                              <1> ;;	(for preventing late memory allocation, significant waiting). 
  3804                              <1> ;;	If disk write procedure returns with error or free count of 
  3805                              <1> ;;	swap blocks is ZERO, 'swap_out' procedure will return with
  3806                              <1> ;;	'insufficient memory error' (cf=1). 
  3807                              <1> ;;
  3808                              <1> ;;	(Note: Even if free swap disk/file blocks was not zero,
  3809                              <1> ;;	any disk write error will not be fixed by 'swap_out' procedure,
  3810                              <1> ;;	in other words, 'swap_out' will not check the table for other
  3811                              <1> ;;	free blocks after a disk write error. It will return to 
  3812                              <1> ;;	the caller with error (CF=1) which means swapping is failed. 
  3813                              <1> ;;
  3814                              <1> ;;	After writing the page on to swap disk/file address/sector,
  3815                              <1> ;;	'swap_out' procedure returns with that swap (offset) sector
  3816                              <1> ;;	address (cf=0). 
  3817                              <1> ;;
  3818                              <1> ;;	..............................................................
  3819                              <1> ;;
  3820                              <1> ;;	'swap_in' procedure loads addressed (relevant) swap disk or
  3821                              <1> ;;	file sectors at specified memory page. Then page allocation
  3822                              <1> ;;	procedure updates relevant page table entry with 'present' 
  3823                              <1> ;;	attribute. If swap disk or file reading fails there is nothing
  3824                              <1> ;;	to do, except to terminate the process which is the owner of
  3825                              <1> ;;	the swapped page.
  3826                              <1> ;;
  3827                              <1> ;;	'swap_in' procedure sets the relevant/respective bit value
  3828                              <1> ;;	in the Swap Allocation Table (as free block). 'swap_in' also
  3829                              <1> ;;	updates [swpd_first] pointer if it is required.
  3830                              <1> ;;
  3831                              <1> ;;	..............................................................	 
  3832                              <1> ;;
  3833                              <1> ;;	Note: If [swap_enabled] value is ZERO, that means there is not
  3834                              <1> ;;	a swap disk or swap file in use... 'swap_in' and 'swap_out'
  3835                              <1> ;;	procedures ans 'swap page que' procedures will not be active...
  3836                              <1> ;;	'Insufficient memory' error will be returned by 'swap_out'
  3837                              <1> ;;	and 'general protection fault' will be returned by 'swap_in'
  3838                              <1> ;;	procedure, if it is called mistakenly (a wrong value in a PTE).		
  3839                              <1> ;;
  3840                              <1> 
  3841                              <1> ; 17/04/2021
  3842                              <1> ; ('swap_in' procedure call is disabled as temporary)
  3843                              <1> 
  3844                              <1> ; 30/11/2021 - temporary !
  3845                              <1> ;swap_in:
  3846                              <1> 	; 31/08/2015
  3847                              <1> 	; 20/07/2015
  3848                              <1> 	; 28/04/2015
  3849                              <1> 	; 18/04/2015
  3850                              <1> 	; 24/10/2014 (Retro UNIX 386 v1 - beginning)
  3851                              <1> 	;
  3852                              <1> 	; INPUT -> 
  3853                              <1> 	;	EBX = PHYSICAL (real/flat) ADDRESS OF THE MEMORY PAGE
  3854                              <1> 	;	EBP = VIRTUAL (LINEAR) ADDRESS (page fault address)
  3855                              <1> 	;	EAX = Offset Address for the swapped page on the
  3856                              <1> 	;	      swap disk or in the swap file.
  3857                              <1> 	;
  3858                              <1> 	; OUTPUT ->
  3859                              <1> 	;	EAX = 0 if loading at memory has been successful
  3860                              <1> 	;
  3861                              <1> 	;	CF = 1 -> swap disk reading error (disk/file not present
  3862                              <1> 	;		  or sector not present or drive not ready
  3863                              <1> 	;	     EAX = Error code
  3864                              <1> 	;	     [u.error] = EAX 
  3865                              <1> 	;		       = The last error code for the process
  3866                              <1> 	;		         (will be reset after returning to user)	  
  3867                              <1> 	;
  3868                              <1> 	; Modified Registers -> EAX
  3869                              <1> 	;
  3870                              <1> 
  3871                              <1> ;       cmp     dword [swp_drv], 0
  3872                              <1> ;	jna	short swpin_dnp_err
  3873                              <1> ;
  3874                              <1> ;	cmp	eax, [swpd_size]
  3875                              <1> ;	jnb	short swpin_snp_err
  3876                              <1> ;
  3877                              <1> ;	push	esi
  3878                              <1> ;	push	ebx
  3879                              <1> ;	push	ecx
  3880                              <1> ;	mov	esi, [swp_drv]	
  3881                              <1> ;	mov	ecx, PAGE_SIZE / LOGIC_SECT_SIZE  ; 8 !
  3882                              <1> ;		; Note: Even if corresponding physical disk's sector 
  3883                              <1> ;		; size different than 512 bytes, logical disk sector
  3884                              <1> ;		; size is 512 bytes and disk reading procedure
  3885                              <1> ;		; will be performed for reading 4096 bytes
  3886                              <1> ;		; (2*2048, 8*512). 
  3887                              <1> ;	; ESI = Logical disk description table address
  3888                              <1> ;	; EBX = Memory page (buffer) address (physical!)
  3889                              <1> ;	; EAX = Sector adress (offset address, logical sector number)
  3890                              <1> ;	; ECX = Sector count ; 8 sectors
  3891                              <1> ;	push	eax
  3892                              <1> ;	call	logical_disk_read
  3893                              <1> ;	pop	eax
  3894                              <1> ;	jnc	short swpin_read_ok
  3895                              <1> ;	;
  3896                              <1> ;	mov	eax, SWP_DISK_READ_ERR ; drive not ready or read error
  3897                              <1> ;	mov	[u.error], eax
  3898                              <1> ;	jmp	short swpin_retn
  3899                              <1> ;	;
  3900                              <1> ;swpin_read_ok:
  3901                              <1> ;	; EAX = Offset address (logical sector number)
  3902                              <1> ;	call	unlink_swap_block  ; Deallocate swap block	
  3903                              <1> ;	;
  3904                              <1> ;	; EBX = Memory page (buffer) address (physical!)
  3905                              <1> ;	; 20/07/2015
  3906                              <1> ;	mov	ebx, ebp ; virtual address (page fault address)
  3907                              <1> ;       and     bx, ~PAGE_OFF ; ~0FFFh ; reset bits, 0 to 11
  3908                              <1> ;	mov	bl, [u.uno] ; current process number
  3909                              <1> ;	; EBX = Virtual (Linear) address & process number combination
  3910                              <1> ;	call	swap_queue_shift
  3911                              <1> ;	; eax = 0 ; 10/06/2016 (if ebx input > 0, eax output = 0)
  3912                              <1> ;	;sub	eax, eax  ; 0 ; Error Code = 0  (no error)
  3913                              <1> ;	; zf = 1
  3914                              <1> ;swpin_retn:
  3915                              <1> ;	pop	ecx
  3916                              <1> ;	pop	ebx
  3917                              <1> ;	pop	esi
  3918                              <1> ;	retn
  3919                              <1> ;
  3920                              <1> ;swpin_dnp_err:
  3921                              <1> ;	mov	eax, SWP_DISK_NOT_PRESENT_ERR
  3922                              <1> ;swpin_err_retn:
  3923                              <1> ;	mov	[u.error], eax
  3924                              <1> ;	stc
  3925                              <1> ;	retn
  3926                              <1> ;
  3927                              <1> ;swpin_snp_err:
  3928                              <1> ;	mov	eax, SWP_SECTOR_NOT_PRESENT_ERR
  3929                              <1> ;	jmp	short swpin_err_retn
  3930                              <1> 
  3931                              <1> ; 17/04/2021
  3932                              <1> ; ('swap_out' procedure call is disabled as temporary)
  3933                              <1> 
  3934                              <1> ; 30/11/2021 - temporary !
  3935                              <1> ;swap_out:
  3936                              <1> 	; 10/06/2016
  3937                              <1> 	; 07/06/2016
  3938                              <1>         ; 23/05/2016
  3939                              <1> 	; 19/05/2016 - TRDOS 386 (TRDOS v2.0)
  3940                              <1> 	; 24/10/2014 - 31/08/2015 (Retro UNIX 386 v1)
  3941                              <1> 	;
  3942                              <1> 	; INPUT -> 
  3943                              <1> 	;	none
  3944                              <1> 	;
  3945                              <1> 	; OUTPUT ->
  3946                              <1> 	;	EAX = Physical page address (which is swapped out
  3947                              <1> 	;	      for allocating a new page)
  3948                              <1> 	;	CF = 1 -> swap disk writing error (disk/file not present
  3949                              <1> 	;		  or sector not present or drive not ready
  3950                              <1> 	;	     EAX = Error code
  3951                              <1> 	;	     [u.error] = EAX 
  3952                              <1> 	;		       = The last error code for the process
  3953                              <1> 	;		         (will be reset after returning to user)	  
  3954                              <1> 	;
  3955                              <1> 	; Modified Registers -> none (except EAX)
  3956                              <1> 	;
  3957                              <1> 
  3958                              <1> ;	cmp 	word [swpq_count], 1
  3959                              <1> ;       jc      swpout_im_err ; 'insufficient memory'
  3960                              <1> ;
  3961                              <1> ;       ;cmp    dword [swp_drv], 1
  3962                              <1> ;	;jc	short swpout_dnp_err ; 'swap disk/file not present'
  3963                              <1> ;
  3964                              <1> ;       cmp     dword [swpd_free], 1
  3965                              <1> ;       jc      swpout_nfspc_err ; 'no free space on swap disk'
  3966                              <1> ;
  3967                              <1> ;	push	ebx ; *
  3968                              <1> ;swpout_1:
  3969                              <1> ;	; 10/06/2016
  3970                              <1> ;	xor	ebx, ebx ; shift the queue and return a PTE value
  3971                              <1> ;	call	swap_queue_shift
  3972                              <1> ;	and	eax, eax	; 0 = empty queue (improper entries)
  3973                              <1> ;       jz      swpout_npts_err        ; There is not any proper PTE
  3974                              <1> ;				       ; pointer in the swap queue
  3975                              <1> ;	; EAX = PTE value of the page
  3976                              <1> ;	; EBX = PTE address of the page
  3977                              <1> ;	and	ax, PTE_A_CLEAR ; 0F000h ; clear attribute bits
  3978                              <1> ;	;
  3979                              <1> ;	; 07/06/2016
  3980                              <1> ;	; 19/05/2016
  3981                              <1> ;	; check this page is in timer events or not
  3982                              <1> ;	
  3983                              <1> ;swpout_timer_page_0:
  3984                              <1> ;	push	edx ; **
  3985                              <1> ;
  3986                              <1> ;	; 07/06/2016
  3987                              <1> ;	cmp	byte [timer_events], 0 
  3988                              <1> ;	jna	short swpout_2
  3989                              <1> ;	;
  3990                              <1> ;	mov	dl, [timer_events]
  3991                              <1> ;
  3992                              <1> ;	push	ecx ; ***
  3993                              <1> ;	push	ebx ; ****
  3994                              <1> ;	mov	ebx, timer_set ; beginning address of timer event
  3995                              <1> ;			       ; structures 
  3996                              <1> ;swpout_timer_page_1:
  3997                              <1> ;	mov	cl, [ebx]
  3998                              <1> ;	or	cl, cl ; 0 = free, >0 = process number
  3999                              <1> ;	jz	short swpout_timer_page_3
  4000                              <1> ;	mov	ecx, [ebx+12] ; response (signal return) address
  4001                              <1> ;	and	cx, PTE_A_CLEAR ; clear offset part (right 12 bits)
  4002                              <1> ;				; of the response byte address, to
  4003                              <1> ;				; get beginning of the page address)
  4004                              <1> ;	cmp	eax, ecx
  4005                              <1> ;	jne	short swpout_timer_page_2 ; not same page
  4006                              <1> ;	
  4007                              <1> ;	; !same page!
  4008                              <1> ;	;
  4009                              <1> ;	; NOTE: // 19/05/2016 // - TRDOS 386 feature only ! -
  4010                              <1> ;	; This page will be used by the kernel to put timer event
  4011                              <1> ;	; response (signal return) byte at the requested address;
  4012                              <1> ;	; in order to prevent a possible wrong write (while
  4013                              <1> ;	; this page is swapped out) on physical memory,
  4014                              <1> ;	; we must protect this page against to be swapped out!
  4015                              <1> ;	;
  4016                              <1> ;	pop	ebx ; ****
  4017                              <1> ;	pop	ecx ; ***
  4018                              <1> ;	pop	edx ; **
  4019                              <1> ;	jmp	short swpout_1	; do not swap out this page !
  4020                              <1> ; 
  4021                              <1> ;swpout_timer_page_2:
  4022                              <1> ;	; 07/06/2016
  4023                              <1> ;	dec	dl
  4024                              <1> ;	jz	short swpout_timer_page_4
  4025                              <1> ;swpout_timer_page_3:
  4026                              <1> ;	;cmp	ebx, timer_set + 240 ; last timer event (15*16) 
  4027                              <1> ;	;jnb	short swpout_timer_page_4
  4028                              <1> ;	add	ebx, 16
  4029                              <1> ;	jmp	short swpout_timer_page_1	
  4030                              <1> ;
  4031                              <1> ;swpout_timer_page_4:
  4032                              <1> ;	pop	ebx ; ****
  4033                              <1> ;	pop	ecx ; ***
  4034                              <1> ;swpout_2:
  4035                              <1> ;	mov	edx, ebx	       ; Page table entry address	
  4036                              <1> ;	mov	ebx, eax	       ; Buffer (Page) Address				
  4037                              <1> ;	;
  4038                              <1> ;	call	link_swap_block
  4039                              <1> ;	jnc	short swpout_3	       ; It may not be needed here	
  4040                              <1> ;				       ; because [swpd_free] value
  4041                              <1> ;				       ; was checked at the beginging. 	
  4042                              <1> ;	pop	edx ; **
  4043                              <1> ;	pop	ebx ; *
  4044                              <1> ;	jmp	short swpout_nfspc_err 
  4045                              <1> ;swpout_3:
  4046                              <1> ;	test	eax, 80000000h ; test bit 31 (this may not be needed!)
  4047                              <1> ;	jnz	short swpout_nfspc_err  ; 10/06/2016 (bit 31 = 1 !)
  4048                              <1> ;	;	
  4049                              <1> ;	push	esi ; **
  4050                              <1> ;	push	ecx ; ***
  4051                              <1> ;	push	eax ; sector address ; (31 bit !, bit 31 = 0)
  4052                              <1> ;	mov	esi, [swp_drv]	
  4053                              <1> ;	mov	ecx, PAGE_SIZE / LOGIC_SECT_SIZE  ; 8 !
  4054                              <1> ;		; Note: Even if corresponding physical disk's sector 
  4055                              <1> ;		; size different than 512 bytes, logical disk sector
  4056                              <1> ;		; size is 512 bytes and disk writing procedure
  4057                              <1> ;		; will be performed for writing 4096 bytes
  4058                              <1> ;		; (2*2048, 8*512). 
  4059                              <1> ;	; ESI = Logical disk description table address
  4060                              <1> ;	; EBX = Buffer (Page) address
  4061                              <1> ;	; EAX = Sector adress (offset address, logical sector number)
  4062                              <1> ;	; ECX = Sector count ; 8 sectors
  4063                              <1> ;	; edx = PTE address
  4064                              <1> ;	call	logical_disk_write
  4065                              <1> ;	; edx = PTE address
  4066                              <1> ;	pop	ecx ; sector address	
  4067                              <1> ;	jnc	short swpout_write_ok
  4068                              <1> ;	;
  4069                              <1> ;	;; call	unlink_swap_block ; this block must be left as 'in use'
  4070                              <1> ;swpout_dw_err:
  4071                              <1> ;	mov	eax, SWP_DISK_WRITE_ERR ; drive not ready or write error
  4072                              <1> ;	mov	[u.error], eax
  4073                              <1> ;	jmp	short swpout_retn
  4074                              <1> ;	;
  4075                              <1> ;swpout_write_ok:
  4076                              <1> ;	; EBX = Buffer (page) address
  4077                              <1> ;	; EDX = Page Table Entry address
  4078                              <1> ;	; ECX = Swap disk sector (file block) address (31 bit)
  4079                              <1> ;	shl 	ecx, 1  ; 31 bit sector address from bit 1 to bit 31 
  4080                              <1> ;	mov 	[edx], ecx 
  4081                              <1> ;		; bit 0 = 0 (swapped page)
  4082                              <1> ;	mov	eax, ebx
  4083                              <1> ;swpout_retn:
  4084                              <1> ;	pop	ecx ; ***
  4085                              <1> ;	pop	esi ; **
  4086                              <1> ;	pop	ebx ; *
  4087                              <1> ;	retn
  4088                              <1> ;
  4089                              <1> ;;swpout_dnp_err:
  4090                              <1> ;;	mov	eax, SWP_DISK_NOT_PRESENT_ERR ; disk not present
  4091                              <1> ;;	jmp	short swpout_err_retn
  4092                              <1> ;swpout_nfspc_err:
  4093                              <1> ;	mov	eax, SWP_NO_FREE_SPACE_ERR ; no free space
  4094                              <1> ;swpout_err_retn:
  4095                              <1> ;	mov	[u.error], eax
  4096                              <1> ;	;stc
  4097                              <1> ;	retn
  4098                              <1> ;swpout_npts_err:
  4099                              <1> ;	mov	eax, SWP_NO_PAGE_TO_SWAP_ERR
  4100                              <1> ;	pop	ebx
  4101                              <1> ;	jmp	short swpout_err_retn
  4102                              <1> ;swpout_im_err:
  4103                              <1> ;	mov	eax, ERR_MINOR_IM ; insufficient (out of) memory
  4104                              <1> ;	jmp	short swpout_err_retn
  4105                              <1> 
  4106                              <1> ; 17/04/2021
  4107                              <1> ; ('swap_queue_shift' procedure call is disabled as temporary)
  4108                              <1> 
  4109                              <1> ; 30/11/2021 - temporary !
  4110                              <1> ;swap_queue_shift:
  4111                              <1> 	; 26/03/2017
  4112                              <1> 	; 10/06/2016
  4113                              <1> 	; 09/06/2016 - TRDOS 386 (TRDOS v2.0)
  4114                              <1> 	; 23/10/2014 - 20/07/2015 (Retro UNIX 386 v1)
  4115                              <1> 	;
  4116                              <1> 	; INPUT ->
  4117                              <1> 	;	EBX = Virtual (linear) address (bit 12 to 31) 
  4118                              <1> 	;	      and process number combination (bit 0 to 11)
  4119                              <1> 	;	EBX = 0 -> shift/drop from the head (offset 0)
  4120                              <1> 	;	
  4121                              <1> 	; OUTPUT ->
  4122                              <1> 	;	If EBX input > 0 
  4123                              <1> 	;	   the queue will be shifted 4 bytes (dword),
  4124                              <1> 	; 	   from the tail to the head, up to entry offset
  4125                              <1> 	; 	   which points to EBX input value or nothing
  4126                              <1> 	;	   to do if EBX value is not found on the queue.
  4127                              <1> 	;	   (The entry -with EBX value- will be removed
  4128                              <1> 	;	   from the queue if it is found.)
  4129                              <1> 	;
  4130                              <1> 	;	   EAX = 0		
  4131                              <1> 	;
  4132                              <1> 	;	If EBX input = 0
  4133                              <1> 	;	   the queue will be shifted 4 bytes (dword),
  4134                              <1> 	; 	   from the tail to the head, if the PTE address
  4135                              <1> 	;	   which is pointed in head of the queue is marked
  4136                              <1> 	;	   as "accessed" or it is marked as "non present".
  4137                              <1> 	;	   (If "accessed" flag of the PTE -which is pointed
  4138                              <1> 	;	   in the head- is set -to 1-, it will be reset
  4139                              <1> 	;	   -to 0- and then, the queue will be rotated 
  4140                              <1> 	;	   -without dropping pointer of the PTE from 
  4141                              <1> 	;	   the queue- for 4 bytes on head to tail direction.
  4142                              <1> 	;	   Pointer in the head will be moved into the tail,
  4143                              <1> 	;	   other PTEs will be shifted on head direction.)
  4144                              <1> 	;
  4145                              <1> 	;	   Swap queue will be shifted up to the first
  4146                              <1> 	;	   'present' or 'non accessed' page will be found
  4147                              <1> 	;	   (as pointed) on the queue head (then it will be
  4148                              <1>         ;          removed/dropped from the queue).
  4149                              <1> 	;
  4150                              <1> 	;	   EAX (> 0) = PTE value of the page which is
  4151                              <1> 	;		 (it's pointer -virtual address-) dropped
  4152                              <1> 	;		 (removed) from swap queue.
  4153                              <1> 	;	   EBX = PTE address of the page (if EAX > 0)
  4154                              <1> 	;	         which is (it's pointer -virtual address-)
  4155                              <1> 	;		 dropped (removed) from swap queue.
  4156                              <1> 	;
  4157                              <1> 	;	   EAX = 0 -> empty swap queue ! 
  4158                              <1> 	;
  4159                              <1> 	; Modified Registers -> EAX, EBX
  4160                              <1> 	;
  4161                              <1> ;	movzx   eax, word [swpq_count]  ; Max. 1024
  4162                              <1> ;	and	ax, ax
  4163                              <1> ;	jz	short swpqs_retn
  4164                              <1> ;	push	edi
  4165                              <1> ;	push	esi
  4166                              <1> ;	push	ecx
  4167                              <1> ;	mov	esi, swap_queue
  4168                              <1> ;	mov	ecx, eax
  4169                              <1> ;	or	ebx, ebx
  4170                              <1> ;	jz	short swpqs_7
  4171                              <1> ;swpqs_1:
  4172                              <1> ;	lodsd
  4173                              <1> ;	cmp	eax, ebx
  4174                              <1> ;	je	short swpqs_2
  4175                              <1> ;	loop	swpqs_1
  4176                              <1> ;	; 10/06/2016
  4177                              <1> ;	sub	eax, eax 
  4178                              <1> ;	jmp	short swpqs_6
  4179                              <1> ;swpqs_2:
  4180                              <1> ;	mov	edi, esi
  4181                              <1> ;	sub 	edi, 4
  4182                              <1> ;swpqs_3:
  4183                              <1> ;	dec	word [swpq_count]
  4184                              <1> ;	jz	short swpqs_5
  4185                              <1> ;swpqs_4:
  4186                              <1> ;	dec 	ecx
  4187                              <1> ;	rep	movsd	; shift up (to the head)
  4188                              <1> ;swpqs_5:
  4189                              <1> ;	xor	eax, eax
  4190                              <1> ;	mov	[edi], eax
  4191                              <1> ;swpqs_6:
  4192                              <1> ;	pop	ecx
  4193                              <1> ;	pop	esi
  4194                              <1> ;	pop	edi
  4195                              <1> ;swpqs_retn:
  4196                              <1> ;	retn		
  4197                              <1> ;swpqs_7:
  4198                              <1> ;	mov	edi, esi ; head
  4199                              <1> ;	lodsd
  4200                              <1> ;	; 20/07/2015
  4201                              <1> ;	mov	ebx, eax
  4202                              <1> ;	and	ebx, ~PAGE_OFF ; ~0FFFh 
  4203                              <1> ;		      ; ebx = virtual address (at page boundary)	
  4204                              <1> ;	and	eax, PAGE_OFF ; 0FFFh
  4205                              <1> ;		      ; ax = process number (1 to 4095)
  4206                              <1> ;	cmp	al, [u.uno]
  4207                              <1> ;		; Max. 16 (nproc) processes for Retro UNIX 386 v1
  4208                              <1> ;	jne	short swpqs_8
  4209                              <1> ;	mov	eax, [u.pgdir]
  4210                              <1> ;	jmp	short swpqs_9
  4211                              <1> ;swpqs_8:
  4212                              <1> ;	; 09/06/2016
  4213                              <1> ;	cmp	byte [eax+p.stat-1], 0
  4214                              <1> ;	jna	short swpqs_3     ; free (or terminated) process
  4215                              <1> ;	cmp	byte [eax+p.stat-1], 2 ; waiting
  4216                              <1> ;	ja	short swpqs_3 	  ; zombie (3) or undefined ?	
  4217                              <1> ;
  4218                              <1> ;	;shl	ax, 2
  4219                              <1> ;	shl	al, 2
  4220                              <1> ;	mov 	eax, [eax+p.upage-4]
  4221                              <1> ;	or	eax, eax
  4222                              <1> ;	jz	short swpqs_3 ; invalid upage
  4223                              <1> ;	add	eax, u.pgdir - user
  4224                              <1> ;			 ; u.pgdir value for the process
  4225                              <1> ;			 ; is in [eax]
  4226                              <1> ;	mov	eax, [eax]
  4227                              <1> ;	and	eax, eax
  4228                              <1> ;	jz	short swpqs_3 ; invalid page directory
  4229                              <1> ;swpqs_9:
  4230                              <1> ;	push	edx
  4231                              <1> ;	; eax = page directory
  4232                              <1> ;	; ebx = virtual address
  4233                              <1> ;	call	get_pte
  4234                              <1> ;	mov	ebx, edx	; PTE address
  4235                              <1> ;	pop	edx
  4236                              <1> ;	; 10/06/2016
  4237                              <1> ;	jc	short swpqs_13 ; empty PDE
  4238                              <1> ;	; EAX = PTE value
  4239                              <1> ;	test	al, PTE_A_PRESENT ; bit 0 = 1
  4240                              <1> ;	jz	short swpqs_13  ; Drop non-present page
  4241                              <1> ;			        ; from the queue (head)
  4242                              <1> ;	test	al, PTE_A_WRITE	; bit 1 = 0 (read only)
  4243                              <1> ;	jz	short swpqs_13  ; Drop read only page
  4244                              <1> ;			        ; from the queue (head) 	
  4245                              <1> ;	;test	al, PTE_A_ACCESS ; bit 5 = 1 (Accessed)
  4246                              <1> ;	;jnz	short swpqs_11  ; present
  4247                              <1> ;			        ; accessed page
  4248                              <1> ;       btr     eax, PTE_A_ACCESS_BIT ; reset 'accessed' bit
  4249                              <1> ;	jc	short swpqs_11  ; accessed page
  4250                              <1> ;
  4251                              <1> ;	dec	ecx
  4252                              <1> ;	mov	[swpq_count], cx
  4253                              <1> ;       jz      short swpqs_10
  4254                              <1> ;		; esi = head + 4
  4255                              <1> ;		; edi = head
  4256                              <1> ;	rep	movsd	 ; n = 1 to k-1, [n - 1] = [n]
  4257                              <1> ;swpqs_10:
  4258                              <1> ;	mov	[edi], ecx ; 0
  4259                              <1> ;	jmp	short swpqs_6 ; 26/03/2017
  4260                              <1> ;
  4261                              <1> ;swpqs_11:
  4262                              <1> ;	mov	[ebx], eax     ; save changed attribute
  4263                              <1> ;	; Rotation (head -> tail)
  4264                              <1> ;	dec	ecx     ; entry count -> last entry number		
  4265                              <1> ;	jz	short swpqs_10
  4266                              <1> ;		; esi = head + 4
  4267                              <1> ;		; edi = head
  4268                              <1> ;	mov	eax, [edi] ; 20/07/2015
  4269                              <1> ;	rep	movsd	 ; n = 1 to k-1, [n - 1] = [n]
  4270                              <1> ;	mov	[edi], eax ; head -> tail ; [k] = [1]
  4271                              <1> ;
  4272                              <1> ;	mov	cx, [swpq_count]
  4273                              <1> ;
  4274                              <1> ;swpqs_12:
  4275                              <1> ;	mov	esi, swap_queue ; head
  4276                              <1> ;       jmp     swpqs_7
  4277                              <1> ;
  4278                              <1> ;swpqs_13:
  4279                              <1> ;	dec	ecx
  4280                              <1> ;	mov	[swpq_count], cx
  4281                              <1> ;       jz      swpqs_5
  4282                              <1> ;	jmp	short swpqs_12
  4283                              <1> 
  4284                              <1> ; 17/04/2021
  4285                              <1> ; ('add_to_swp_queue' procedure call is disabled as temporary)
  4286                              <1> 
  4287                              <1> ; 30/11/2021 - temporary !
  4288                              <1> ;add_to_swap_queue:
  4289                              <1> 	; 20/02/2017
  4290                              <1> 	; 20/07/2015
  4291                              <1> 	; 24/10/2014 (Retro UNIX 386 v1 - beginning)
  4292                              <1> 	;
  4293                              <1> 	; Adds new page to swap queue
  4294                              <1> 	; (page directories and page tables must not be added
  4295                              <1> 	; to swap queue)	
  4296                              <1> 	;
  4297                              <1> 	; INPUT ->
  4298                              <1> 	;	EBX = Linear (Virtual) addr for current process
  4299                              <1> 	;	[u.uno]
  4300                              <1> 	;	20/02/2017
  4301                              <1> 	;	(Linear address = CORE + user's virtual address)
  4302                              <1> 	;
  4303                              <1> 	; OUTPUT ->
  4304                              <1> 	;	EAX = [swpq_count]
  4305                              <1> 	;	      (after the PTE has been added)
  4306                              <1> 	;	EAX = 0 -> Swap queue is full, (1024 entries)
  4307                              <1> 	;	      the PTE could not be added.
  4308                              <1> 	;
  4309                              <1> 	; Modified Registers -> EAX
  4310                              <1> 	;
  4311                              <1> ;	push	ebx
  4312                              <1> ;       and     bx, ~PAGE_OFF ; ~0FFFh ; reset bits, 0 to 11
  4313                              <1> ;	mov	bl, [u.uno] ; current process number
  4314                              <1> ;	call	swap_queue_shift ; drop from the queue if
  4315                              <1> ;				 ; it is already on the queue
  4316                              <1> ;		; then add it to the tail of the queue
  4317                              <1> ;	movzx	eax, word [swpq_count]
  4318                              <1> ;	cmp	ax, 1024
  4319                              <1> ;	jb	short atsq_1
  4320                              <1> ;	sub	ax, ax
  4321                              <1> ;	pop	ebx
  4322                              <1> ;	retn
  4323                              <1> ;atsq_1:
  4324                              <1> ;	push	esi
  4325                              <1> ;	mov	esi, swap_queue
  4326                              <1> ;	and	ax, ax
  4327                              <1> ;	jz	short atsq_2
  4328                              <1> ;	shl	ax, 2	; convert to offset
  4329                              <1> ;	add	esi, eax
  4330                              <1> ;	shr	ax, 2
  4331                              <1> ;atsq_2:
  4332                              <1> ;	inc	ax
  4333                              <1> ;	mov	[esi], ebx ; Virtual address + [u.uno] combination
  4334                              <1> ;	mov	[swpq_count], ax
  4335                              <1> ;	pop	esi
  4336                              <1> ;	pop	ebx
  4337                              <1> ;	retn
  4338                              <1> 
  4339                              <1> ; 17/04/2021
  4340                              <1> ; ('unlink_swap_block' procedure call is disabled as temporary)
  4341                              <1> 
  4342                              <1> ; 30/11/2021 - temporary !
  4343                              <1> ;unlink_swap_block:
  4344                              <1> 	; 15/09/2015
  4345                              <1> 	; 30/04/2015
  4346                              <1> 	; 18/04/2015
  4347                              <1> 	; 24/10/2014 (Retro UNIX 386 v1 - beginning)
  4348                              <1> 	;
  4349                              <1> 	; INPUT -> 
  4350                              <1> 	;	EAX = swap disk/file offset address
  4351                              <1> 	;	      (bit 1 to bit 31)
  4352                              <1> 	; OUTPUT ->
  4353                              <1> 	;	[swpd_free] is increased
  4354                              <1> 	;	(corresponding SWAP DISK ALLOC. TABLE bit is SET)
  4355                              <1> 	;
  4356                              <1> 	; Modified Registers -> EAX
  4357                              <1> 	;
  4358                              <1> ;	push	ebx
  4359                              <1> ;	push	edx
  4360                              <1> ;	;
  4361                              <1> ;	shr	eax, SECTOR_SHIFT+1  ;3+1 ; shift sector address to 
  4362                              <1> ;				     ; 3 bits right
  4363                              <1> ;				     ; to get swap block/page number
  4364                              <1> ;	mov	edx, eax
  4365                              <1> ;	; 15/09/2015
  4366                              <1> ;	shr	edx, 3		     ; to get offset to S.A.T.
  4367                              <1> ;				     ; (1 allocation bit = 1 page)
  4368                              <1> ;				     ; (1 allocation bytes = 8 pages)
  4369                              <1> ;	and	dl, 0FCh 	     ; clear lower 2 bits
  4370                              <1> ;				     ; (to get 32 bit position)			
  4371                              <1> ;	;
  4372                              <1> ;	mov	ebx, swap_alloc_table ; Swap Allocation Table address
  4373                              <1> ;	add	ebx, edx
  4374                              <1> ;	and	eax, 1Fh	     ; lower 5 bits only
  4375                              <1> ;				     ; (allocation bit position)	 
  4376                              <1> ;	cmp 	eax, [swpd_next]     ; is the new free block addr. lower
  4377                              <1> ;				     ; than the address in 'swpd_next' ?
  4378                              <1> ;				     ; (next/first free block value)		
  4379                              <1> ;	jnb	short uswpbl_1	     ; no	
  4380                              <1> ;	mov	[swpd_next], eax     ; yes	
  4381                              <1> ;uswpbl_1:
  4382                              <1> ;	bts	[ebx], eax	     ; unlink/release/deallocate block
  4383                              <1> ;				     ; set relevant bit to 1.
  4384                              <1> ;				     ; set CF to the previous bit value	
  4385                              <1> ;	cmc			     ; complement carry flag	
  4386                              <1> ;	jc	short uswpbl_2	     ; do not increase swfd_free count
  4387                              <1> ;				     ; if the block is already deallocated
  4388                              <1> ;				     ; before.	
  4389                              <1> ;       inc     dword [swpd_free]
  4390                              <1> ;uswpbl_2:
  4391                              <1> ;	pop	edx
  4392                              <1> ;	pop	ebx
  4393                              <1> ;	retn
  4394                              <1> 
  4395                              <1> ; 17/04/2021
  4396                              <1> ; ('link_swap_block' procedure call is disabled as temporary)
  4397                              <1> 
  4398                              <1> ; 30/11/2021 - temporary !
  4399                              <1> ;link_swap_block:
  4400                              <1> 	; 01/07/2015
  4401                              <1> 	; 18/04/2015
  4402                              <1> 	; 24/10/2014 (Retro UNIX 386 v1 - beginning)
  4403                              <1> 	;
  4404                              <1> 	; INPUT -> none
  4405                              <1> 	;
  4406                              <1> 	; OUTPUT ->
  4407                              <1> 	;	EAX = OFFSET ADDRESS OF THE ALLOCATED BLOCK (4096 bytes)
  4408                              <1> 	;	      in sectors (corresponding 
  4409                              <1> 	;	      SWAP DISK ALLOCATION TABLE bit is RESET)
  4410                              <1> 	;
  4411                              <1> 	;	CF = 1 and EAX = 0 
  4412                              <1> 	; 		   if there is not a free block to be allocated	
  4413                              <1> 	;
  4414                              <1> 	; Modified Registers -> none (except EAX)
  4415                              <1> 	;
  4416                              <1> 
  4417                              <1> ;	;mov	eax, [swpd_free]
  4418                              <1> ;	;and	eax, eax
  4419                              <1> ;	;jz	short out_of_swpspc
  4420                              <1> ;	;
  4421                              <1> ;	push	ebx
  4422                              <1> ;	push	ecx
  4423                              <1> ;	;
  4424                              <1> ;	mov	ebx, swap_alloc_table ; Swap Allocation Table offset
  4425                              <1> ;	mov	ecx, ebx
  4426                              <1> ;	add	ebx, [swpd_next] ; Free block searching starts from here
  4427                              <1> ;				 ; next_free_swap_block >> 5
  4428                              <1> ;	add	ecx, [swpd_last] ; Free block searching ends here
  4429                              <1> ;				 ; (total_swap_blocks - 1) >> 5
  4430                              <1> ;lswbl_scan:
  4431                              <1> ;	cmp	ebx, ecx
  4432                              <1> ;	ja	short lswbl_notfound
  4433                              <1> ;	;
  4434                              <1> ;	bsf	eax, [ebx] ; Scans source operand for first bit set (1).
  4435                              <1> ;			   ; Clears ZF if a bit is found set (1) and 
  4436                              <1> ;			   ; loads the destination with an index to
  4437                              <1> ;			   ; first set bit. (0 -> 31) 
  4438                              <1> ;			   ; Sets ZF to 1 if no bits are found set.
  4439                              <1> ;	; 01/07/2015
  4440                              <1> ;	jnz	short lswbl_found ; ZF = 0 -> a free block has been found
  4441                              <1> ;			 ;
  4442                              <1> ;			 ; NOTE:  a Swap Disk Allocation Table bit 
  4443                              <1> ;			 ;	  with value of 1 means 
  4444                              <1> ;			 ;	  the corresponding page is free 
  4445                              <1> ;			 ;	  (Retro UNIX 386 v1 feaure only!)
  4446                              <1> ;	add	ebx, 4
  4447                              <1> ;			 ; We return back for searching next page block
  4448                              <1> ;			 ; NOTE: [swpd_free] is not ZERO; so, 
  4449                              <1> ;			 ;	 we always will find at least 1 free block here.
  4450                              <1> ;	jmp    	short lswbl_scan
  4451                              <1> ;	;
  4452                              <1> ;lswbl_notfound:	
  4453                              <1> ;	sub	ecx, swap_alloc_table
  4454                              <1> ;	mov	[swpd_next], ecx ; next/first free page = last page 
  4455                              <1> ;				 ; (unlink_swap_block procedure will change it)
  4456                              <1> ;	xor	eax, eax
  4457                              <1> ;	mov	[swpd_free], eax
  4458                              <1> ;	stc
  4459                              <1> ;lswbl_ok:
  4460                              <1> ;	pop	ecx
  4461                              <1> ;	pop	ebx
  4462                              <1> ;	retn
  4463                              <1> ;	;
  4464                              <1> ;;out_of_swpspc:
  4465                              <1> ;;	stc
  4466                              <1> ;;	retn
  4467                              <1> ;
  4468                              <1> ;lswbl_found:
  4469                              <1> ;	mov	ecx, ebx
  4470                              <1> ;	sub	ecx, swap_alloc_table
  4471                              <1> ;	mov	[swpd_next], ecx ; Set first free block searching start
  4472                              <1> ;				 ; address/offset (to the next)
  4473                              <1> ;       dec     dword [swpd_free] ; 1 block has been allocated (X = X-1) 
  4474                              <1> ;	;
  4475                              <1> ;	btr	[ebx], eax	 ; The destination bit indexed by the source value
  4476                              <1> ;				 ; is copied into the Carry Flag and then cleared
  4477                              <1> ;				 ; in the destination.
  4478                              <1> ;				 ;
  4479                              <1> ;				 ; Reset the bit which is corresponding to the 
  4480                              <1> ;				 ; (just) allocated block.
  4481                              <1> ;	shl	ecx, 5		 ; (block offset * 32) + block index
  4482                              <1> ;	add	eax, ecx	 ; = block number
  4483                              <1> ;	shl	eax, SECTOR_SHIFT ; 3, sector (offset) address of the block
  4484                              <1> ;				 ; 1 block =  8 sectors
  4485                              <1> ;	;
  4486                              <1> ;	; EAX = offset address of swap disk/file sector (beginning of the block)
  4487                              <1> ;	;
  4488                              <1> ;	; NOTE: The relevant page table entry will be updated
  4489                              <1> ;	;       according to this EAX value...
  4490                              <1> ;	;
  4491                              <1> ;	jmp	short lswbl_ok
  4492                              <1> 
  4493                              <1> ; 17/04/2021
  4494                              <1> ; ('logical_disk_read' procedure call is disabled as temporary)
  4495                              <1> 
  4496                              <1> ; 30/11/2021 - temporary !
  4497                              <1> ;logical_disk_read:
  4498                              <1> 	; 20/07/2015
  4499                              <1> 	; 09/03/2015 (temporary code here)
  4500                              <1> 	;
  4501                              <1> 	; INPUT ->
  4502                              <1> 	; 	ESI = Logical disk description table address
  4503                              <1> 	; 	EBX = Memory page (buffer) address (physical!)
  4504                              <1> 	; 	EAX = Sector adress (offset address, logical sector number)
  4505                              <1> 	; 	ECX = Sector count
  4506                              <1> 	;
  4507                              <1> 	;
  4508                              <1> ;	retn
  4509                              <1> 
  4510                              <1> ; 17/04/2021
  4511                              <1> ; ('logical_disk_write' procedure call is disabled as temporary)
  4512                              <1> 
  4513                              <1> ; 30/11/2021 - temporary !
  4514                              <1> ;logical_disk_write:
  4515                              <1> 	; 20/07/2015
  4516                              <1> 	; 09/03/2015 (temporary code here)
  4517                              <1> 	;
  4518                              <1> 	; INPUT ->
  4519                              <1> 	; 	ESI = Logical disk description table address
  4520                              <1> 	; 	EBX = Memory page (buffer) address (physical!)
  4521                              <1> 	; 	EAX = Sector adress (offset address, logical sector number)
  4522                              <1> 	; 	ECX = Sector count
  4523                              <1> 	;
  4524                              <1> ;	retn
  4525                              <1> 
  4526                              <1> get_physical_addr:
  4527                              <1> 	; 17/04/2021 - Retro UNIX 386 v2
  4528                              <1> 	;	(temporary modifications)
  4529                              <1> 	; 19/04/2020 - Retro UNIX 386 v2
  4530                              <1> 	; 18/10/2015
  4531                              <1> 	; 29/07/2015
  4532                              <1> 	; 20/07/2015
  4533                              <1> 	; 04/06/2015
  4534                              <1> 	; 20/05/2015
  4535                              <1> 	; 28/04/2015
  4536                              <1> 	; 18/04/2015
  4537                              <1> 	; Get physical address
  4538                              <1> 	;     (allocates a new page for user if it is not present)
  4539                              <1> 	;	
  4540                              <1> 	; (This subroutine is needed for mapping user's virtual 
  4541                              <1> 	; (buffer) address to physical address (of the buffer).)
  4542                              <1> 	; ('sys write', 'sys read' system calls...)
  4543                              <1> 	;
  4544                              <1> 	; INPUT ->
  4545                              <1> 	;	EBX = virtual address
  4546                              <1> 	;	u.pgdir = page directory (physical) address
  4547                              <1> 	;
  4548                              <1> 	; OUTPUT ->
  4549                              <1> 	;	EAX = physical address 
  4550                              <1> 	;	EBX = linear address	
  4551                              <1> 	;	EDX = physical address of the page frame
  4552                              <1> 	;	      (with attribute bits)
  4553                              <1> 	;	ECX = byte count within the page frame
  4554                              <1> 	;
  4555                              <1> 	; Modified Registers -> EAX, EBX, ECX, EDX
  4556                              <1> 	;
  4557                              <1> 
  4558                              <1> 	; 19/04/2020 - Retro UNIX386 v2
  4559 00003252 A1[38750000]        <1> 	mov	eax, [u.pgdir]
  4560                              <1> 
  4561                              <1> ifs_get_physical_addr: ; 19/04/2020 - Retro UNIX 386 v2
  4562                              <1> 	
  4563 00003257 81C300004000        <1> 	add	ebx, CORE ; 18/10/2015
  4564                              <1> 	;
  4565                              <1> 	;mov	eax, [u.pgdir]
  4566 0000325D E816FDFFFF          <1> 	call	get_pte
  4567                              <1> 		; EDX = Page table entry address (if CF=0)
  4568                              <1> 	        ;       Page directory entry address (if CF=1)
  4569                              <1> 		;       (Bit 0 value is 0 if PT is not present)
  4570                              <1> 		; EAX = Page table entry value (page address)
  4571                              <1> 		;	CF = 1 -> PDE not present or invalid ? 
  4572 00003262 731C                <1> 	jnc	short gpa_1
  4573                              <1> 	;
  4574 00003264 E8FDFBFFFF          <1> 	call	allocate_page
  4575 00003269 724B                <1> 	jc	short gpa_im_err  ; 'insufficient memory' error
  4576                              <1> gpa_0:
  4577 0000326B E867FCFFFF          <1> 	call 	clear_page
  4578                              <1> 	; EAX = Physical (base) address of the allocated (new) page
  4579 00003270 0C07                <1> 	or	al, PDE_A_PRESENT + PDE_A_WRITE + PDE_A_USER ; 4+2+1 = 7
  4580                              <1> 			   ; lower 3 bits are used as U/S, R/W, P flags
  4581                              <1> 			   ; (user, writable, present page)	
  4582 00003272 8902                <1> 	mov	[edx], eax ; Let's put the new page directory entry here !
  4583 00003274 A1[38750000]        <1> 	mov	eax, [u.pgdir]	
  4584 00003279 E8FAFCFFFF          <1> 	call	get_pte
  4585 0000327E 7236                <1> 	jc	short gpa_im_err ; 'insufficient memory' error
  4586                              <1> gpa_1:
  4587                              <1> 	; EAX = PTE value, EDX = PTE address
  4588 00003280 A801                <1> 	test 	al, PTE_A_PRESENT
  4589 00003282 750A                <1> 	jnz	short gpa_3
  4590 00003284 09C0                <1> 	or	eax, eax
  4591 00003286 7420                <1> 	jz	short gpa_4  ; Allocate a new page
  4592                              <1> 
  4593                              <1> ; 17/04/2021
  4594                              <1> ; ('reload_page' procedure call is disabled as temporary)
  4595 00003288 EB2C                <1> 	jmp	short gpa_im_err  ; temporary !
  4596                              <1> 
  4597                              <1> 	; 20/07/2015
  4598                              <1> ;	push	ebp
  4599                              <1> ;	mov	ebp, ebx ; virtual (linear) address
  4600                              <1> ;	; reload swapped page
  4601                              <1> ;	call	reload_page ; 28/04/2015
  4602                              <1> ;	pop	ebp
  4603                              <1> ;	jc	short gpa_retn
  4604                              <1> gpa_2:
  4605                              <1> ; 17/04/2021
  4606                              <1> ; ('add_to_swap_queue' procedure call is disabled as temporary)
  4607                              <1> 
  4608                              <1> 	; 20/07/2015
  4609                              <1> 	; 20/05/2015
  4610                              <1> 	; add this page to swap queue
  4611                              <1> ;	push	eax 
  4612                              <1> ;	; EBX = Linear (CORE+virtual) address ; 20/02/2017 
  4613                              <1> ;	call 	add_to_swap_queue
  4614                              <1> ;	pop	eax
  4615                              <1> 		; PTE address in EDX
  4616                              <1> 		; virtual address in EBX
  4617                              <1> 
  4618                              <1> 	; EAX = memory page address
  4619 0000328A 0C07                <1> 	or	al, PTE_A_PRESENT + PTE_A_USER + PTE_A_WRITE
  4620                              <1> 				  ; present flag, bit 0 = 1
  4621                              <1> 				  ; user flag, bit 2 = 1	
  4622                              <1> 				  ; writable flag, bit 1 = 1
  4623 0000328C 8902                <1> 	mov	[edx], eax  ; Update PTE value
  4624                              <1> gpa_3:
  4625                              <1> 	; 18/10/2015
  4626 0000328E 89D9                <1> 	mov	ecx, ebx
  4627 00003290 81E1FF0F0000        <1> 	and	ecx, PAGE_OFF
  4628 00003296 89C2                <1> 	mov 	edx, eax
  4629 00003298 662500F0            <1> 	and	ax, PTE_A_CLEAR
  4630 0000329C 01C8                <1> 	add	eax, ecx
  4631 0000329E F7D9                <1> 	neg	ecx ; 1 -> -1 (0FFFFFFFFh), 4095 (0FFFh) -> -4095
  4632 000032A0 81C100100000        <1> 	add	ecx, PAGE_SIZE
  4633 000032A6 F8                  <1> 	clc
  4634                              <1> gpa_retn:
  4635 000032A7 C3                  <1> 	retn	
  4636                              <1> gpa_4:	
  4637 000032A8 E8B9FBFFFF          <1> 	call	allocate_page
  4638 000032AD 7207                <1> 	jc	short gpa_im_err ; 'insufficient memory' error
  4639 000032AF E823FCFFFF          <1> 	call	clear_page
  4640 000032B4 EBD4                <1> 	jmp	short gpa_2
  4641                              <1> 
  4642                              <1> gpa_im_err:	
  4643 000032B6 B804000000          <1> 	mov	eax, ERR_MINOR_IM ; Insufficient memory (minor) error!
  4644                              <1> 				  ; Major error = 0 (No protection fault)	
  4645 000032BB C3                  <1> 	retn
  4646                              <1> 
  4647                              <1> ; 17/04/2021
  4648                              <1> ; ('reload_page' procedure call is disabled as temporary)
  4649                              <1> 
  4650                              <1> ; 30/11/2021 - temporary !
  4651                              <1> ;reload_page:
  4652                              <1> 	; 20/07/2015
  4653                              <1> 	; 28/04/2015 (Retro UNIX 386 v1 - beginning)
  4654                              <1> 	;
  4655                              <1> 	; Reload (Restore) swapped page at memory
  4656                              <1> 	;
  4657                              <1> 	; INPUT -> 
  4658                              <1> 	;	EBP = Virtual (linear) memory address
  4659                              <1> 	;	EAX = PTE value (swap disk sector address)
  4660                              <1> 	;	(Swap disk sector address = bit 1 to bit 31 of EAX)	
  4661                              <1> 	; OUTPUT ->
  4662                              <1> 	;	EAX = PHYSICAL (real/flat) ADDRESS OF RELOADED PAGE
  4663                              <1> 	;
  4664                              <1> 	;	CF = 1 and EAX = error code
  4665                              <1> 	;
  4666                              <1> 	; Modified Registers -> none (except EAX)
  4667                              <1> 	;
  4668                              <1> ;	shr	eax, 1   ; Convert PTE value to swap disk address 
  4669                              <1> ;	push	ebx      ;
  4670                              <1> ;	mov	ebx, eax ; Swap disk (offset) address	
  4671                              <1> ;	call	allocate_page
  4672                              <1> ;	jc	short rlp_im_err
  4673                              <1> ;	xchg 	eax, ebx	
  4674                              <1> ;	; EBX = Physical memory (page) address
  4675                              <1> ;	; EAX = Swap disk (offset) address
  4676                              <1> ;	; EBP = Virtual (linear) memory address
  4677                              <1> ;	call	swap_in
  4678                              <1> ;	jc	short rlp_swp_err  ; (swap disk/file read error)
  4679                              <1> ;	mov	eax, ebx	
  4680                              <1> ;rlp_retn:
  4681                              <1> ;	pop	ebx
  4682                              <1> ;	retn
  4683                              <1> ;	
  4684                              <1> ;rlp_im_err:	
  4685                              <1> ;	mov	eax, ERR_MINOR_IM ; Insufficient memory (minor) error!
  4686                              <1> ;				  ; Major error = 0 (No protection fault)	
  4687                              <1> ;	jmp	short rlp_retn
  4688                              <1> ;
  4689                              <1> ;rlp_swp_err:
  4690                              <1> ;	mov 	eax, SWP_DISK_READ_ERR ; Swap disk read error !
  4691                              <1> ;	jmp	short rlp_retn
  4692                              <1> 
  4693                              <1> copy_page_dir:
  4694                              <1> 	; 17/04/2021 (temporary modifications)
  4695                              <1> 	; 19/09/2015
  4696                              <1> 	; temporary - 07/09/2015
  4697                              <1> 	; 07/09/2015 (Retro UNIX 386 v1 - beginning)
  4698                              <1> 	;
  4699                              <1> 	; INPUT -> 
  4700                              <1> 	;	[u.pgdir] = PHYSICAL (real/flat) ADDRESS of the parent's
  4701                              <1> 	;		    page directory.
  4702                              <1> 	; OUTPUT ->
  4703                              <1> 	;	EAX =  PHYSICAL (real/flat) ADDRESS of the child's
  4704                              <1> 	;	       page directory.
  4705                              <1> 	;	(New page directory with new page table entries.)
  4706                              <1> 	;	(New page tables with read only copies of the parent's
  4707                              <1> 	;	pages.)
  4708                              <1> 	;	EAX = 0 -> Error (CF = 1)
  4709                              <1> 	;
  4710                              <1> 	; Modified Registers -> none (except EAX)
  4711                              <1> 	;
  4712 000032BC E8A5FBFFFF          <1> 	call	allocate_page
  4713 000032C1 723E                <1> 	jc	short cpd_err
  4714                              <1> 	;
  4715 000032C3 55                  <1> 	push	ebp ; 20/07/2015
  4716 000032C4 56                  <1> 	push	esi
  4717 000032C5 57                  <1> 	push	edi
  4718 000032C6 53                  <1> 	push	ebx
  4719 000032C7 51                  <1> 	push	ecx
  4720 000032C8 8B35[38750000]      <1> 	mov	esi, [u.pgdir]
  4721 000032CE 89C7                <1> 	mov	edi, eax
  4722 000032D0 50                  <1> 	push	eax ; save child's page directory address
  4723                              <1> 	; copy PDE 0 from the parent's page dir to the child's page dir
  4724                              <1> 	; (use same system space for all user page tables) 
  4725 000032D1 A5                  <1> 	movsd
  4726 000032D2 BD00004000          <1> 	mov	ebp, 1024*4096 ; pass the 1st 4MB (system space)
  4727 000032D7 B9FF030000          <1> 	mov	ecx, (PAGE_SIZE / 4) - 1 ; 1023
  4728                              <1> cpd_0:	
  4729 000032DC AD                  <1> 	lodsd
  4730                              <1> 	;or	eax, eax
  4731                              <1>         ;jnz	short cpd_1
  4732 000032DD A801                <1> 	test	al, PDE_A_PRESENT ;  bit 0 =  1
  4733 000032DF 7508                <1> 	jnz	short cpd_1
  4734                              <1>  	; (virtual address at the end of the page table)	
  4735 000032E1 81C500004000        <1> 	add	ebp, 1024*4096 ; page size * PTE count
  4736 000032E7 EB0F                <1> 	jmp	short cpd_2
  4737                              <1> cpd_1:	
  4738 000032E9 662500F0            <1> 	and	ax, PDE_A_CLEAR ; 0F000h ; clear attribute bits
  4739 000032ED 89C3                <1> 	mov	ebx, eax
  4740                              <1> 	; EBX = Parent's page table address
  4741 000032EF E81F000000          <1> 	call	copy_page_table
  4742 000032F4 720C                <1> 	jc	short cpd_p_err
  4743                              <1> 	; EAX = Child's page table address
  4744 000032F6 0C07                <1> 	or	al, PDE_A_PRESENT + PDE_A_WRITE + PDE_A_USER
  4745                              <1> 			 ; set bit 0, bit 1 and bit 2 to 1
  4746                              <1> 			 ; (present, writable, user)
  4747                              <1> cpd_2:
  4748 000032F8 AB                  <1> 	stosd
  4749 000032F9 E2E1                <1> 	loop	cpd_0
  4750                              <1> 	;
  4751 000032FB 58                  <1> 	pop	eax  ; restore child's page directory address
  4752                              <1> cpd_3:
  4753 000032FC 59                  <1> 	pop	ecx
  4754 000032FD 5B                  <1> 	pop	ebx
  4755 000032FE 5F                  <1> 	pop	edi
  4756 000032FF 5E                  <1> 	pop	esi
  4757 00003300 5D                  <1> 	pop	ebp
  4758                              <1> cpd_err:
  4759 00003301 C3                  <1> 	retn
  4760                              <1> cpd_p_err:
  4761                              <1> 	; release the allocated pages missing (recover free space)
  4762 00003302 58                  <1> 	pop	eax  ; the new page directory address (physical)
  4763 00003303 8B1D[38750000]      <1> 	mov	ebx, [u.pgdir] ; parent's page directory address 
  4764 00003309 E888FCFFFF          <1> 	call 	deallocate_page_dir
  4765 0000330E 29C0                <1> 	sub	eax, eax ; 0
  4766 00003310 F9                  <1> 	stc
  4767 00003311 EBE9                <1> 	jmp	short cpd_3	
  4768                              <1> 
  4769                              <1> copy_page_table:
  4770                              <1> 	; 17/04/2021 (temporary modifications)
  4771                              <1> 	; 19/09/2015
  4772                              <1> 	; temporary - 07/09/2015
  4773                              <1> 	; 07/09/2015 (Retro UNIX 386 v1 - beginning)
  4774                              <1> 	;
  4775                              <1> 	; INPUT -> 
  4776                              <1> 	;	EBX = PHYSICAL (real/flat) ADDRESS of the parent's page table.
  4777                              <1> 	;	EBP = page table entry index (from 'copy_page_dir')
  4778                              <1> 	; OUTPUT ->
  4779                              <1> 	;	EAX = PHYSICAL (real/flat) ADDRESS of the child's page table.
  4780                              <1> 	;	EBP = (recent) page table index (for 'add_to_swap_queue')	
  4781                              <1> 	;	CF = 1 -> error 
  4782                              <1> 	;
  4783                              <1> 	; Modified Registers -> EBP (except EAX)
  4784                              <1> 	;
  4785 00003313 E84EFBFFFF          <1> 	call	allocate_page
  4786 00003318 7244                <1> 	jc	short cpt_err
  4787                              <1> 	;
  4788 0000331A 50                  <1> 	push	eax ; *
  4789                              <1> 	;push 	ebx
  4790 0000331B 56                  <1> 	push	esi
  4791 0000331C 57                  <1> 	push	edi
  4792 0000331D 52                  <1> 	push	edx
  4793 0000331E 51                  <1> 	push	ecx
  4794                              <1> 	;
  4795 0000331F 89DE                <1> 	mov	esi, ebx
  4796 00003321 89C7                <1> 	mov	edi, eax
  4797 00003323 89C2                <1> 	mov	edx, eax
  4798 00003325 81C200100000        <1> 	add	edx, PAGE_SIZE 	
  4799                              <1> cpt_0:
  4800 0000332B AD                  <1> 	lodsd
  4801 0000332C A801                <1> 	test	al, PTE_A_PRESENT ;  bit 0 = 1
  4802                              <1> 	;jnz	short cpt_1 (*)
  4803                              <1> 	; 17/04/2021 (temporary (*)
  4804                              <1> 	;and	eax, eax (*)
  4805 0000332E 741E                <1> 	jz	short cpt_2  ; 17/04/2021
  4806                              <1> 	
  4807                              <1> ; 17/04/2021
  4808                              <1> ; ('reload_page' procedure call is disabled as temporary)
  4809                              <1> ;
  4810                              <1> ;	; ebp = virtual (linear) address of the memory page
  4811                              <1> ;	call	reload_page ; 28/04/2015
  4812                              <1> ;	jc	short cpt_p_err
  4813                              <1> cpt_1:
  4814 00003330 662500F0            <1> 	and	ax, PTE_A_CLEAR ; 0F000h ; clear attribute bits
  4815 00003334 89C1                <1> 	mov	ecx, eax
  4816                              <1> 	; Allocate a new page for the child process
  4817 00003336 E82BFBFFFF          <1> 	call	allocate_page
  4818 0000333B 721C                <1> 	jc	short cpt_p_err
  4819 0000333D 57                  <1> 	push	edi
  4820 0000333E 56                  <1> 	push	esi
  4821 0000333F 89CE                <1> 	mov	esi, ecx
  4822 00003341 89C7                <1> 	mov	edi, eax
  4823 00003343 B900040000          <1> 	mov	ecx, PAGE_SIZE/4
  4824 00003348 F3A5                <1> 	rep	movsd	; copy page (4096 bytes)
  4825 0000334A 5E                  <1> 	pop	esi
  4826 0000334B 5F                  <1> 	pop	edi
  4827                              <1> 	; 
  4828                              <1> ; 17/04/2021
  4829                              <1> ; ('add_to_swap_queue' procedure call is disabled as temporary)	
  4830                              <1> ;
  4831                              <1> ;	push	ebx
  4832                              <1> ;	push	eax
  4833                              <1> ;	mov	ebx, ebp
  4834                              <1> ;	; ebx = virtual address of the memory page
  4835                              <1> ;	call	add_to_swap_queue
  4836                              <1> ;	pop	eax
  4837                              <1> ;	pop	ebx
  4838                              <1> 	;
  4839                              <1> 	;or	ax, PTE_A_USER+PTE_A_PRESENT 
  4840 0000334C 0C07                <1> 	or	al, PTE_A_USER+PTE_A_WRITE+PTE_A_PRESENT 
  4841                              <1> cpt_2:
  4842 0000334E AB                  <1> 	stosd  ; EDI points to child's PTE  	 
  4843                              <1> 	;
  4844 0000334F 81C500100000        <1> 	add	ebp, 4096 ; 20/07/2015 (next page)
  4845                              <1> 	;
  4846 00003355 39D7                <1> 	cmp	edi, edx
  4847 00003357 72D2                <1> 	jb	short cpt_0
  4848                              <1> cpt_p_err:
  4849 00003359 59                  <1> 	pop	ecx
  4850 0000335A 5A                  <1> 	pop	edx
  4851 0000335B 5F                  <1> 	pop	edi
  4852 0000335C 5E                  <1> 	pop	esi
  4853                              <1> 	;pop	ebx
  4854 0000335D 58                  <1> 	pop	eax ; *
  4855                              <1> cpt_err:
  4856 0000335E C3                  <1> 	retn
  4857                              <1> 
  4858                              <1> ; /// End Of MEMORY MANAGEMENT FUNCTIONS ///
  4859                              <1> 
  4860                              <1> ;; Data:
  4861                              <1> 
  4862                              <1> ; 09/03/2015
  4863                              <1> ;swpq_count: dw 0 ; count of pages on the swap que
  4864                              <1> ;swp_drv:    dd 0 ; logical drive description table address of the swap drive/disk
  4865                              <1> ;swpd_size:  dd 0 ; size of swap drive/disk (volume) in sectors (512 bytes). 		  				
  4866                              <1> ;swpd_free:  dd 0 ; free page blocks (4096 bytes) on swap disk/drive (logical)
  4867                              <1> ;swpd_next:  dd 0 ; next free page block
  4868                              <1> ;swpd_last:  dd 0 ; last swap page block		 		
  2068                                  %include 'sysdefs.s' ; 09/03/2015
  2069                              <1> ; Temporary Runix kernel v2.0 file for debug - 22/11/2021
  2070                              <1> ; (re-write kernel for test by using previous version without a major defect)
  2071                              <1> ; ****************************************************************************
  2072                              <1> ; Retro UNIX 386 v1 Kernel - SYSDEFS.INC
  2073                              <1> ; Last Modification: 12/01/2022
  2074                              <1> ;
  2075                              <1> ; ///////// RETRO UNIX 386 V1 SYSTEM DEFINITIONS ///////////////
  2076                              <1> ; (Modified from 
  2077                              <1> ;	Retro UNIX 8086 v1 system definitions in 'UNIX.ASM', 01/09/2014)
  2078                              <1> ; ((UNIX.ASM (RETRO UNIX 8086 V1 Kernel), 11/03/2013 - 01/09/2014))
  2079                              <1> ; 	UNIX.ASM (MASM 6.11) --> SYSDEFS.INC (NASM 2.11)
  2080                              <1> ; ----------------------------------------------------------------------------
  2081                              <1> ;
  2082                              <1> ; Derived from UNIX Operating System (v1.0 for PDP-11) 
  2083                              <1> ; (Original) Source Code by Ken Thompson (1971-1972)
  2084                              <1> ; <Bell Laboratories (17/3/1972)>
  2085                              <1> ; <Preliminary Release of UNIX Implementation Document>
  2086                              <1> ;
  2087                              <1> ; ****************************************************************************
  2088                              <1> 
  2089                              <1> ; 10/01/2022 - Retro UNIX 386 v1.2
  2090                              <1> ; (SB structure has been moved here from 'ux.s' 
  2091                              <1> ;	to overcome NASM's bss addressing bug !!!)
  2092                              <1> ; (('ux.s' bss section addresses are being overlapped
  2093                              <1> ;    when SB structure is defined in 'ux.s'))
  2094                              <1> 
  2095                              <1> struc SB ; SuperBlock
  2096                              <1> 
  2097 00000000 ????????            <1> .Header:	resd 1
  2098                              <1> ;.HiddenSects:
  2099 00000004 ????????            <1> .BootSectAddr:	resd 1	; Hidden Sectors
  2100                              <1> ;.TotalSects:
  2101 00000008 ????????            <1> .VolumeSize:	resd 1	; Entire Volume/Partition Size (includes ext. volume)
  2102 0000000C ????????            <1> .Version:	resd 1	
  2103 00000010 ????????            <1> .BlockSize:	resd 1	
  2104 00000014 ????????            <1> .InodeCount:	resd 1	
  2105 00000018 ????????            <1> .FreeMapAddr:	resd 1	
  2106 0000001C ????????            <1> .FreeMapSize:	resd 1	
  2107 00000020 ????????            <1> .InodeMapAddr:	resd 1	
  2108 00000024 ????????            <1> .InodeMapSize:	resd 1	
  2109 00000028 ????????            <1> .InodeTblAddr:	resd 1	
  2110 0000002C ????????            <1> .InodeTblSize:	resd 1	
  2111 00000030 ????????            <1> .FreeInodes:	resd 1	
  2112 00000034 ????????            <1> .FirstFreeIno:	resd 1	
  2113 00000038 ????????            <1> .FreeBlocks:	resd 1	
  2114 0000003C ????????            <1> .FirstFreeBlk:	resd 1	
  2115 00000040 <res 13h>           <1> .BootSecParms:	resb 19	; v1
  2116 00000053 ??????????          <1> .BSExtension:	resb 5	; v2 HDFS
  2117 00000058 ??                  <1> .Status:	resb 1	; 12/05/2021 (system modification status) (*)
  2118 00000059 ??                  <1> .Pdrv:		resb 1  ; Physical disk number (index) ; 12/05/2021 (*) 
  2119 0000005A ????                <1> .Uno:		resw 1	; user/process number ; 12/05/2021 (*)
  2120 0000005C ????????            <1> .ModifTime:	resd 1	; (last) modification time (*)
  2121 00000060 ????????            <1> .ExtdVolTbl:	resd 1	; Extended Volume Start/Table Address
  2122 00000064 ????????            <1> .ExtdVolSize:	resd 1	; Extended Volume (swap section etc.) Size	
  2123 00000068 ??                  <1> .LBA_rw:	resb 1
  2124 00000069 ??                  <1> .ClusterSize:	resb 1
  2125 0000006A ??                  <1> .ReadOnly:	resb 1	; (SB will not be written to disk if bit 0 is 1)
  2126 0000006B ??                  <1> .Mounted:	resb 1
  2127 0000006C ????????            <1> .MountInode:	resd 1  ; double word
  2128 00000070 ??                  <1> .DevMajor:	resb 1
  2129 00000071 ??                  <1> .DevMinor:	resb 1
  2130 00000072 ??                  <1> .LongName:	resb 1
  2131 00000073 ??                  <1> .Direntry32:	resb 1
  2132                              <1> ; 18/07/2021
  2133 00000074 ????????            <1> .FileBuffer:	resd 1
  2134 00000078 ????????            <1> .ItabBuffer:	resd 1
  2135 0000007C ????????            <1> .ImapBuffer:	resd 1
  2136 00000080 ????????            <1> .FmapBuffer:	resd 1
  2137                              <1>  ; 15/07/2021
  2138 00000084 ????????            <1> .LastInode:	resd 1
  2139                              <1> ; 02/05/2021
  2140 00000088 ????????            <1> .FmapIndex:	resd 1
  2141 0000008C ????????            <1> .ImapIndex:	resd 1
  2142 00000090 ????????            <1> .ItableIndex:	resd 1
  2143 00000094 <res 168h>          <1> .Reserved:	resb 508-148 ; 18/07/2021
  2144 000001FC ????????            <1> .Footer:	resd 1
  2145                              <1> 
  2146                              <1> endstruc
  2147                              <1> 
  2148                              <1> ; 10/01/2022 - Retro UNIX 386 v1.2
  2149                              <1> ; (file structure has been moved here from 'ux.s' 
  2150                              <1> ;	to overcome NASM's bss addressing bug !!!)
  2151                              <1> ; (('ux.s' bss section addresses are being overlapped
  2152                              <1> ;    when file structure is defined in 'ux.s'))
  2153                              <1> 
  2154                              <1> ; 22/11/2021
  2155                              <1> ; 21/07/2021 - Retro UNIX 386 v2 open file structure revision
  2156                              <1> 
  2157                              <1> struc file	; open files (fsp) structure
  2158 00000000 ????                <1>   .inode:  resw 1  ; inode number of open file (32 bit)
  2159 00000002 ????                <1>   .i32:	   resw 1  ; higher word of inode number (reserved)
  2160 00000004 ??                  <1>   .drive:  resb 1  ; logical drive (disk) number
  2161 00000005 ??                  <1>   .flags:  resb 1  ; open mode and status
  2162 00000006 ??                  <1>   .count:  resb 1  ; number of processes that have file open
  2163                              <1>   ;.rsvd:  resb 1  ; reserved byte (for next versions)
  2164 00000007 ??                  <1>   .mnt:    resb 1  ; mnttab index+1 (0 = not mounted)
  2165 00000008 ????????            <1>   .offset: resd 1  ; file offset/pointer (64 bit) 
  2166 0000000C ????????            <1>   .o64:	   resd 1  ; higher 32 bit of file offset
  2167                              <1>  .size:  ; = 16		
  2168                              <1> endstruc 
  2169                              <1> 
  2170                              <1> nproc 	equ	16  ; number of processes
  2171                              <1> ntty	equ     8   ; 8+1 -> 8 (10/05/2013)
  2172                              <1> nbuf	equ	6   ; number of buffers (04/02/2016)
  2173                              <1> nfiles  equ	50  ; NFILES equ 50 ; 02/01/2022
  2174                              <1> 
  2175                              <1> ; 27/12/2021
  2176                              <1> ;NFILES	  equ   32  ; temporary ! ( 27/12/2021)
  2177                              <1> ; 02/01/2022
  2178                              <1> ;OPENFILES equ	10  ; open files (for user) ; 28/03/2020
  2179                              <1> 
  2180                              <1> ;csgmnt	equ	2000h	; 26/05/2013 (segment of process 1)
  2181                              <1> ;core	equ 	0  	    ; 19/04/2013	
  2182                              <1> ;ecore	equ	32768 - 64  ; 04/06/2013 (24/05/2013)
  2183                              <1> 	; (if total size of argument list and arguments is 128 bytes)
  2184                              <1> 	; maximum executable file size = 32768-(64+40+128-6) = 32530 bytes
  2185                              <1> 	; maximum stack size = 40 bytes (+6 bytes for 'IRET' at 32570)	
  2186                              <1> 	; initial value of user's stack pointer = 32768-64-128-2 = 32574
  2187                              <1> 	; 	(sp=32768-args_space-2 at the beginning of execution)
  2188                              <1> 	; argument list offset = 32768-64-128 = 32576 (if it is 128 bytes)
  2189                              <1> 	; 'u' structure offset (for the '/core' dump file) = 32704
  2190                              <1> 	; '/core' dump file size = 32768 bytes
  2191                              <1>  
  2192                              <1> ; 08/03/2014 
  2193                              <1> ;sdsegmnt equ	6C0h  ; 256*16 bytes (swap data segment size for 16 processes)
  2194                              <1> ; 19/04/2013 Retro UNIX 8086 v1 feaure only !
  2195                              <1> ;;sdsegmnt equ 	740h  ; swap data segment (for user structures and registers)
  2196                              <1> 
  2197                              <1> ; 30/08/2013
  2198                              <1> time_count equ 4 ; 10 --> 4 01/02/2014
  2199                              <1> 
  2200                              <1> ; 05/02/2014
  2201                              <1> ; process status
  2202                              <1> ;SFREE 	equ 0
  2203                              <1> ;SRUN	equ 1
  2204                              <1> ;SWAIT	equ 2
  2205                              <1> ;SZOMB	equ 3
  2206                              <1> ;SSLEEP	equ 4 ; Retro UNIX 8086 V1 extension (for sleep and wakeup)
  2207                              <1> 
  2208                              <1> ; 09/03/2015
  2209                              <1> userdata equ 80000h ; user structure data address for current user ; temporary
  2210                              <1> swap_queue equ 90000h - 2000h ; swap queue address ; temporary
  2211                              <1> swap_alloc_table equ 0D0000h  ;  swap allocation table address ; temporary
  2212                              <1> 
  2213                              <1> ; 17/09/2015
  2214                              <1> ESPACE equ 48 ; [u.usp] (at 'sysent') - [u.sp] value for error return
  2215                              <1> 
  2216                              <1> ; 12/01/2022 (37,38,39)
  2217                              <1> ; 21/09/2015 (36) 
  2218                              <1> ; 01/07/2015 (35)
  2219                              <1> ; 14/07/2013 (0-34)
  2220                              <1> ; UNIX v1 system calls
  2221                              <1> _rele 	equ 0
  2222                              <1> _exit 	equ 1
  2223                              <1> _fork 	equ 2
  2224                              <1> _read 	equ 3
  2225                              <1> _write	equ 4
  2226                              <1> _open	equ 5
  2227                              <1> _close 	equ 6
  2228                              <1> _wait 	equ 7
  2229                              <1> _creat 	equ 8
  2230                              <1> _link 	equ 9
  2231                              <1> _unlink	equ 10
  2232                              <1> _exec	equ 11
  2233                              <1> _chdir	equ 12
  2234                              <1> _time 	equ 13
  2235                              <1> _mkdir 	equ 14
  2236                              <1> _chmod	equ 15
  2237                              <1> _chown	equ 16
  2238                              <1> _break	equ 17
  2239                              <1> _stat	equ 18
  2240                              <1> _seek	equ 19
  2241                              <1> _tell 	equ 20
  2242                              <1> _mount	equ 21
  2243                              <1> _umount	equ 22
  2244                              <1> _setuid	equ 23
  2245                              <1> _getuid	equ 24
  2246                              <1> _stime	equ 25
  2247                              <1> _quit	equ 26	
  2248                              <1> _intr	equ 27
  2249                              <1> _fstat	equ 28
  2250                              <1> _emt 	equ 29
  2251                              <1> _mdate 	equ 30
  2252                              <1> _stty 	equ 31
  2253                              <1> _gtty	equ 32
  2254                              <1> _ilgins	equ 33
  2255                              <1> _sleep	equ 34 ; Retro UNIX 8086 v1 feature only !
  2256                              <1> _msg	equ 35 ; Retro UNIX 386 v1 feature only !
  2257                              <1> _geterr	equ 36 ; Retro UNIX 386 v1 feature only !
  2258                              <1> ; 12/01/2022 - Retro UNIX 386 v1.2
  2259                              <1> ; Retro UNIX 386 v2 system calls
  2260                              <1> _setgid	equ 37
  2261                              <1> _getgid	equ 38
  2262                              <1> _sysver	equ 39 ; (get) Retro Unix 386 version
  2263                              <1> 
  2264                              <1> %macro sys 1-4
  2265                              <1>     ; 13/04/2015
  2266                              <1>     ; Retro UNIX 386 v1 system call.
  2267                              <1>     mov eax, %1
  2268                              <1>     %if %0 >= 2   
  2269                              <1>         mov ebx, %2
  2270                              <1>         %if %0 >= 3    
  2271                              <1>             mov ecx, %3
  2272                              <1>             %if %0 = 4
  2273                              <1>                mov edx, %4   
  2274                              <1>             %endif
  2275                              <1>         %endif
  2276                              <1>     %endif
  2277                              <1>     int 30h	   
  2278                              <1> %endmacro
  2279                              <1> 
  2280                              <1> ; 13/05/2015 - ERROR CODES
  2281                              <1> ERR_FILE_NOT_OPEN  equ 10 ; 'file not open !' error
  2282                              <1> ERR_FILE_ACCESS    equ 11 ; 'permission denied !' error
  2283                              <1> ; 14/05/2015
  2284                              <1> ERR_DIR_ACCESS     equ 11 ; 'permission denied !' error
  2285                              <1> ERR_FILE_NOT_FOUND equ 12 ; 'file not found !' error
  2286                              <1> ERR_TOO_MANY_FILES equ 13 ; 'too many open files !' error
  2287                              <1> ERR_DIR_EXISTS     equ 14 ; 'directory already exists !' error
  2288                              <1> ; 16/05/2015		
  2289                              <1> ERR_DRV_NOT_RDY    equ 15 ; 'drive not ready !' error
  2290                              <1> ; 18/05/2015
  2291                              <1> ERR_DEV_NOT_RDY    equ 15 ; 'device not ready !' error
  2292                              <1> ERR_DEV_ACCESS     equ 11 ; 'permission denied !' error 
  2293                              <1> ERR_DEV_NOT_OPEN   equ 10 ; 'device not open !' error	
  2294                              <1> ; 07/06/2015
  2295                              <1> ERR_FILE_EOF	   equ 16 ; 'end of file !' error
  2296                              <1> ERR_DEV_VOL_SIZE   equ 16 ; 'out of volume' error
  2297                              <1> ; 09/06/2015
  2298                              <1> ERR_DRV_READ	   equ 17 ; 'disk read error !'
  2299                              <1> ERR_DRV_WRITE	   equ 18 ; 'disk write error !'
  2300                              <1> ; 16/06/2015
  2301                              <1> ERR_NOT_DIR	   equ 19 ; 'not a (valid) directory !' error
  2302                              <1> ERR_FILE_SIZE	   equ 20 ; 'file size error !'	
  2303                              <1> ; 22/06/2015
  2304                              <1> ERR_NOT_SUPERUSER  equ 11 ; 'permission denied !' error
  2305                              <1> ERR_NOT_OWNER      equ 11 ; 'permission denied !' error
  2306                              <1> ERR_NOT_FILE       equ 11 ; 'permission denied !' error	
  2307                              <1> ; 23/06/2015
  2308                              <1> ERR_FILE_EXISTS    equ 14 ; 'file already exists !' error
  2309                              <1> ERR_DRV_NOT_SAME   equ 21 ; 'not same drive !' error
  2310                              <1> ERR_DIR_NOT_FOUND  equ 12 ; 'directory not found !' error
  2311                              <1> ERR_NOT_EXECUTABLE equ 22 ; 'not executable file !' error
  2312                              <1> ; 27/06/2015
  2313                              <1> ERR_INV_PARAMETER  equ 23 ; 'invalid parameter !' error
  2314                              <1> ERR_INV_DEV_NAME   equ 24 ; 'invalid device name !' error
  2315                              <1> ; 29/06/2015
  2316                              <1> ERR_TIME_OUT	   equ 25 ; 'time out !' error
  2317                              <1> ERR_DEV_NOT_RESP   equ 25 ; 'device not responding !' error
  2318                              <1> 
  2319                              <1> ; 26/08/2015
  2320                              <1> ; 24/07/2015
  2321                              <1> ; 24/06/2015
  2322                              <1> MAX_ARG_LEN	   equ 256 ; max. length of sys exec arguments
  2323                              <1> ; 01/07/2015
  2324                              <1> MAX_MSG_LEN	   equ 255 ; max. msg length for 'sysmsg'
  2325                              <1> ;
  2326                              <1> ; 26/11/2021 ('no free blocks on disk !' error)
  2327                              <1> ERR_ALLOC	   equ 33  ; 'disk allocation error !'	
  2328                              <1> ; 22/11/2021
  2329                              <1> ERR_READ_ONLY_FS   equ 30  ; 'read only file system !' error
  2330                              <1> ; 28/11/2021
  2331                              <1> ERR_INV_FILE	   equ 255 ; 'invalid file (inode) !' error
  2332                              <1> ; 04/12/2021
  2333                              <1> ERR_PERM_DENIED	   equ 11  ; 'permission denied !' error
  2334                              <1> ; 11/12/2021
  2335                              <1> ERR_INV_FUNC	   equ 1   ; 'invalid system call !' error
  2336                              <1> ; 10/01/2022
  2337                              <1> ERR_INO_ALLOC	   equ 33  ; 'inode allocation error !'
  2338                              <1> ERR_NOT_REGULAR    equ 255 ; 'not regular file directory !' error
  2339                              <1> 
  2340                              <1> ; 12/01/2022 - Retro UNIX 386 v1.2
  2341                              <1> %define s.time systm+504 ; boot/sysinit time (or current time)
  2342                              <1> 		
  2069                                  %include 'u0.s'      ; 15/03/2015
  2070                              <1> ; Temporary Runix kernel v2.0 file for debug - 22/11/2021
  2071                              <1> ; (re-write kernel for test by using previous version without a major defect)
  2072                              <1> ; ****************************************************************************
  2073                              <1> ; Retro UNIX 386 v1 Kernel (v0.2) - SYS0.INC
  2074                              <1> ; Last Modification: 08/01/2022
  2075                              <1> ; ----------------------------------------------------------------------------
  2076                              <1> ; Derived from 'Retro UNIX 8086 v1' source code by Erdogan Tan
  2077                              <1> ; (v0.1 - Beginning: 11/07/2012)
  2078                              <1> ;
  2079                              <1> ; Derived from UNIX Operating System (v1.0 for PDP-11) 
  2080                              <1> ; (Original) Source Code by Ken Thompson (1971-1972)
  2081                              <1> ; <Bell Laboratories (17/3/1972)>
  2082                              <1> ; <Preliminary Release of UNIX Implementation Document>
  2083                              <1> ;
  2084                              <1> ; Retro UNIX 8086 v1 - U0.ASM (28/07/2014) //// UNIX v1 -> u0.s
  2085                              <1> ;
  2086                              <1> ; ****************************************************************************
  2087                              <1> 
  2088                              <1> sys_init:
  2089                              <1> 	; 08/01/2022
  2090                              <1> 	; 01/01/2022
  2091                              <1> 	; 26/12/2021
  2092                              <1> 	; 27/11/2021
  2093                              <1> 	; 22/11/2021 - Retro UNIX 386 v2 (test) compatibility modification
  2094                              <1> 	; 18/10/2015
  2095                              <1> 	; 28/08/2015
  2096                              <1> 	; 24/08/2015
  2097                              <1> 	; 14/08/2015
  2098                              <1> 	; 24/07/2015 
  2099                              <1> 	; 02/07/2015
  2100                              <1> 	; 01/07/2015
  2101                              <1> 	; 23/06/2015
  2102                              <1> 	; 15/04/2015
  2103                              <1> 	; 13/04/2015
  2104                              <1> 	; 11/03/2015 (Retro UNIX 386 v1 - Beginning)
  2105                              <1> 	; 28/07/2014 (Retro UNIX 8086 v1)
  2106                              <1> 	;
  2107                              <1> 	;call	ldrv_init ; Logical drive description tables initialization
  2108                              <1> 	;
  2109                              <1> 	;; 14/02/2014
  2110                              <1> 	;; 14/07/2013
  2111                              <1> 	;mov	ax, 41
  2112                              <1> 	;mov	[rootdir], ax
  2113                              <1> 	;mov	[u.cdir], ax
  2114                              <1> 	;and	al, 1 ; 15/04/2015
  2115                              <1> 	
  2116                              <1> 	; 27/11/2021
  2117                              <1> 	; 18/04/2021 - Retro UNIX 386 v2
  2118 0000335F 31C0                <1> 	xor	eax, eax
  2119                              <1> 	; 12/12/2021 - Temporary
  2120 00003361 A2[F4700000]        <1> 	mov	[p.ttyc], al ; 0  ; test !!!
  2121 00003366 FEC0                <1> 	inc	al	; eax = 1 (root directory inode) ; runix v2 fs
  2122 00003368 A3[C0740000]        <1> 	mov	[rootdir], eax ; = 1 ; 28/10/2021 (32 bit)
  2123 0000336D A3[DC740000]        <1> 	mov	[u.cdir], eax ; 28/10/2021 (32 bit)
  2124                              <1> 
  2125 00003372 A2[29750000]        <1> 	mov	[u.uno], al ; = 1
  2126                              <1> 	;mov	[mpid], ax ; 1 
  2127                              <1> 	;mov	[p.pid], ax ; 1
  2128                              <1> 	; 01/01/2022
  2129 00003377 A2[BE740000]        <1> 	mov	[mpid], al
  2130 0000337C A2[B4700000]        <1> 	mov	[p.pid], al
  2131 00003381 A2[24710000]        <1> 	mov	[p.stat], al ; SRUN, 05/02/2014
  2132                              <1> 	;
  2133 00003386 B004                <1> 	mov	al, time_count ; 30/08/2013
  2134 00003388 A2[20750000]        <1> 	mov	[u.quant], al ; 14/07/2013
  2135                              <1> 	; 02/07/2015
  2136 0000338D A1[B86F0000]        <1> 	mov	eax, [k_page_dir]
  2137 00003392 A3[38750000]        <1> 	mov	[u.pgdir], eax ; reset
  2138                              <1> 	; 18/10/2015
  2139                              <1> 	;sub	eax, eax
  2140                              <1> 	;mov	[u.ppgdir], eax ; 0
  2141                              <1>         ;
  2142 00003397 E82B030000          <1>  	call	epoch
  2143 0000339C A3[7C770000]        <1> 	mov	[s.time], eax ; 13/03/2015
  2144                              <1> 	; 17/07/2013
  2145 000033A1 E883060000          <1> 	call 	bf_init ; buffer initialization 
  2146                              <1> 	; 23/06/2015
  2147 000033A6 E8BBFAFFFF          <1> 	call	allocate_page
  2148                              <1> 	;;jc	error
  2149                              <1> 	;jc	panic   ; jc short panic (01/07/2015)
  2150                              <1> 	; 05/12/2021
  2151 000033AB 7305                <1> 	jnc	short sysinit_1
  2152 000033AD E990000000          <1> 	jmp	panic
  2153                              <1> sysinit_1:
  2154 000033B2 A3[34750000]        <1> 	mov	[u.upage], eax ; user structure page	
  2155 000033B7 A3[34710000]        <1> 	mov	[p.upage], eax
  2156                              <1> 	;
  2157 000033BC E816FBFFFF          <1> 	call	clear_page
  2158                              <1> 	;
  2159                              <1> 	; 14/08/2015
  2160 000033C1 FA                  <1> 	cli
  2161                              <1> 	; 14/03/2015
  2162                              <1> 	; 17/01/2014
  2163 000033C2 E8CD010000          <1> 	call	sp_init ; serial port initialization
  2164                              <1> 	; 14/08/2015
  2165 000033C7 FB                  <1> 	sti
  2166                              <1> 	;
  2167                              <1> 	; 30/06/2015
  2168                              <1> 	;mov	esi, kernel_init_ok_msg
  2169                              <1> 	;call 	print_msg
  2170                              <1> 	;
  2171 000033C8 30DB                <1> 	xor	bl, bl ; video page 0
  2172                              <1> vp_clr_nxt:  ; clear video pages (reset cursor positions)
  2173 000033CA E815320000          <1> 	call 	vp_clr  ; 17/07/2013
  2174 000033CF FEC3                <1> 	inc	bl
  2175 000033D1 80FB08              <1> 	cmp	bl, 8
  2176 000033D4 72F4                <1> 	jb	short vp_clr_nxt
  2177                              <1> 	;
  2178                              <1> 	; 24/07/2015
  2179                              <1> 	;push	KDATA
  2180                              <1>         ;push	esp
  2181                              <1> 	;mov	[tss.esp0], esp
  2182                              <1>         ;mov	word [tss.ss0], KDATA
  2183                              <1> 	;
  2184                              <1> 	; 08/01/2022
  2185                              <1> 	; 24/08/2015
  2186                              <1> 	;; temporary (01/07/2015)
  2187                              <1> 	;mov	byte [u.quant], time_count ; 4 
  2188                              <1> 	;		       ; it is not needed here !
  2189                              <1> 	;;inc	byte [u.kcall] ; 'the caller is kernel' sign
  2190 000033D6 FE0D[CE740000]      <1> 	dec 	byte [sysflg] ; FFh = ready for system call
  2191                              <1> 			      ; 0 = executing a system call
  2192                              <1> 	;;sys 	_msg, kernel_init_ok_msg, 255, 0
  2193                              <1> 	;
  2194                              <1> 	;;; 06/08/2015
  2195                              <1> 	;;;call	getch ; wait for a key stroke
  2196                              <1> 	;;mov 	ecx, 0FFFFFFFh	
  2197                              <1> ;;sys_init_msg_wait:
  2198                              <1> ;;	push 	ecx
  2199                              <1> ;;	mov	al, 1
  2200                              <1> ;;	mov 	ah, [ptty] ; active (current) video page
  2201                              <1> ;;	call	getc_n
  2202                              <1> ;;	pop	ecx
  2203                              <1> ;;	jnz	short sys_init_msg_ok
  2204                              <1> ;;	loop	sys_init_msg_wait
  2205                              <1> 	;
  2206                              <1> ;;sys_init_msg_ok:
  2207                              <1> 	; 28/08/2015 (initial settings for the 1st 'rswap')
  2208 000033DC 6A10                <1> 	push	KDATA ; ss
  2209 000033DE 54                  <1> 	push	esp
  2210 000033DF 9C                  <1> 	pushfd
  2211 000033E0 6A08                <1> 	push	KCODE ; cs
  2212 000033E2 68[0F340000]        <1> 	push	init_exec ; eip
  2213 000033E7 8925[D0740000]      <1> 	mov	[u.sp], esp
  2214 000033ED 1E                  <1> 	push	ds
  2215 000033EE 06                  <1> 	push	es
  2216 000033EF 0FA0                <1> 	push	fs
  2217 000033F1 0FA8                <1> 	push	gs	
  2218 000033F3 60                  <1> 	pushad
  2219 000033F4 8925[D4740000]      <1> 	mov	[u.usp], esp
  2220 000033FA E88C1B0000          <1> 	call	wswap ; save current user (u) structure, user registers
  2221                              <1> 		      ; and interrupt return components (for IRET)
  2222 000033FF 61                  <1> 	popad
  2223 00003400 6658                <1> 	pop	ax ; gs
  2224 00003402 6658                <1> 	pop	ax ; fs
  2225 00003404 6658                <1> 	pop	ax ; es
  2226 00003406 6658                <1> 	pop	ax ; ds	
  2227 00003408 58                  <1> 	pop	eax ; eip (init_exec)
  2228 00003409 6658                <1> 	pop	ax ; cs (KCODE)
  2229 0000340B 58                  <1> 	pop	eax ; E-FLAGS
  2230 0000340C 58                  <1> 	pop	eax ; esp
  2231 0000340D 6658                <1> 	pop	ax ; ss (KDATA)
  2232                              <1> 	;
  2233                              <1> 	; 26/12/2021 ([u.ppgdir] is zero already)
  2234                              <1> 	;xor	eax, eax ; 0
  2235                              <1> 	;mov	[u.ppgdir], eax ; reset (to zero) for '/etc/init'
  2236                              <1> 	;
  2237                              <1> 	; 02/07/2015
  2238                              <1> 	; [u.pgdir ] = [k_page_dir]
  2239                              <1> 	; [u.ppgdir] = 0 (page dir of the parent process)
  2240                              <1> 	;     (The caller is os kernel sign for 'sysexec')
  2241                              <1> init_exec:
  2242                              <1> 
  2243                              <1> ; temporary - 27/12/2021
  2244                              <1> ; clear faulty areas
  2245                              <1> ;mov ecx, (NFILES*16)/4	
  2246                              <1> ;mov edi, fsp
  2247 0000340F 31C0                <1> xor eax, eax
  2248                              <1> ;rep stosd
  2249                              <1> ;mov edi, u.fp
  2250                              <1> ;mov cl, 10
  2251                              <1> ;rep stosb
  2252 00003411 A3[74710000]        <1> mov [fsp], eax	
  2253                              <1> 
  2254                              <1> 	; 13/03/2013
  2255                              <1> 	; 24/07/2013
  2256 00003416 BB[38340000]        <1> 	mov	ebx, init_file
  2257 0000341B B9[30340000]        <1> 	mov	ecx, init_argp
  2258                              <1> 	; EBX contains 'etc/init' asciiz file name address  
  2259                              <1> 	; ECX contains address of argument list pointer
  2260                              <1> 	;
  2261                              <1> 	;dec 	byte [sysflg] ; FFh = ready for system call
  2262                              <1> 			      ; 0 = executing a system call
  2263                              <1> 	sys	_exec  ; execute file
  2265                              <2> 
  2266                              <2> 
  2267 00003420 B80B000000          <2>  mov eax, %1
  2268                              <2>  %if %0 >= 2
  2269                              <2>  mov ebx, %2
  2270                              <2>  %if %0 >= 3
  2271                              <2>  mov ecx, %3
  2272                              <2>  %if %0 = 4
  2273                              <2>  mov edx, %4
  2274                              <2>  %endif
  2275                              <2>  %endif
  2276                              <2>  %endif
  2277 00003425 CD30                <2>  int 30h
  2264 00003427 7319                <1> 	jnc	short panic
  2265                              <1> 	;
  2266 00003429 BE[CC6C0000]        <1> 	mov	esi, etc_init_err_msg
  2267                              <1> 	; 22/11/2021
  2268                              <1> 	;call 	print_msg
  2269 0000342E EB17                <1> 	jmp	short key_to_reboot
  2270                              <1> 
  2271                              <1> ;align 4
  2272                              <1> init_argp:
  2273 00003430 [38340000]00000000  <1> 	dd 	init_file, 0  ; 23/06/2015 (dw -> dd)
  2274                              <1> init_file:
  2275                              <1> 	; 24/08/2015
  2276 00003438 2F6574632F696E6974- <1> 	db 	'/etc/init', 0
  2276 00003441 00                  <1>
  2277                              <1> panic:
  2278                              <1> 	; 13/03/2015 (Retro UNIX 386 v1)
  2279                              <1> 	; 07/03/2014 (Retro UNIX 8086 v1)
  2280 00003442 BE[B16C0000]        <1> 	mov 	esi, panic_msg
  2281                              <1> key_to_reboot: ; 22/11/2021
  2282 00003447 E819000000          <1> 	call 	print_msg
  2283                              <1> ;key_to_reboot:
  2284                              <1> 	; 15/11/2015
  2285 0000344C E8E12F0000          <1> 	call 	getch 
  2286                              <1> 		; wait for a character from the current tty
  2287                              <1> 	;
  2288 00003451 B00A                <1> 	mov	al, 0Ah
  2289 00003453 8A1D[E66F0000]      <1> 	mov	bl, [ptty] ; [active_page]
  2290 00003459 B407                <1> 	mov	ah, 07h ; Black background, 
  2291                              <1> 			; light gray forecolor
  2292 0000345B E85ADFFFFF          <1> 	call 	write_tty
  2293 00003460 E9F5DBFFFF          <1> 	jmp	cpu_reset 
  2294                              <1> 
  2295                              <1> print_msg:
  2296                              <1> 	; 01/07/2015
  2297                              <1> 	; 13/03/2015 (Retro UNIX 386 v1)
  2298                              <1> 	; 07/03/2014 (Retro UNIX 8086 v1)
  2299                              <1> 	; (Modified registers: EAX, EBX, ECX, EDX, ESI, EDI)
  2300                              <1> 	;
  2301                              <1> 	;
  2302 00003465 AC                  <1> 	lodsb
  2303                              <1> pmsg1:
  2304 00003466 56                  <1> 	push 	esi
  2305 00003467 0FB61D[E66F0000]    <1> 	movzx	ebx, byte [ptty]
  2306 0000346E B407                <1> 	mov	ah, 07h ; Black background, light gray forecolor
  2307 00003470 E845DFFFFF          <1> 	call 	write_tty
  2308 00003475 5E                  <1> 	pop	esi
  2309 00003476 AC                  <1> 	lodsb
  2310 00003477 20C0                <1> 	and 	al, al
  2311 00003479 75EB                <1> 	jnz 	short pmsg1
  2312 0000347B C3                  <1> 	retn
  2313                              <1> 	
  2314                              <1> ctrlbrk:
  2315                              <1> 	; 04/12/2021 (Retro UNIX 386 v1.2)
  2316                              <1> 	; 12/11/2015
  2317                              <1> 	; 13/03/2015 (Retro UNIX 386 v1)
  2318                              <1> 	; 06/12/2013 (Retro UNIX 8086 v1)
  2319                              <1> 	;
  2320                              <1> 	; INT 1Bh (control+break) handler		
  2321                              <1> 	;
  2322                              <1>       	; Retro Unix 8086 v1 feature only!
  2323                              <1>       	;
  2324 0000347C 66833D[24750000]00  <1> 	cmp 	word [u.intr], 0
  2325 00003484 7641                <1> 	jna 	short cbrk4
  2326                              <1> cbrk0:
  2327                              <1> 	; 12/11/2015
  2328                              <1> 	; 06/12/2013
  2329 00003486 66833D[26750000]00  <1> 	cmp 	word [u.quit], 0
  2330 0000348E 7437                <1> 	jz	short cbrk4
  2331                              <1> 	;
  2332                              <1> 	; 20/09/2013	
  2333                              <1> 	;push 	ax
  2334                              <1> 	; 04/12/2021
  2335 00003490 50                  <1> 	push	eax
  2336 00003491 A0[E66F0000]        <1> 	mov	al, [ptty]
  2337                              <1> 	;
  2338                              <1> 	; 12/11/2015
  2339                              <1> 	;
  2340                              <1> 	; ctrl+break (EOT, CTRL+D) from serial port
  2341                              <1> 	; or ctrl+break from console (pseudo) tty
  2342                              <1> 	; (!redirection!)
  2343                              <1> 	;
  2344 00003496 3C08                <1> 	cmp	al, 8 ; serial port tty nums > 7
  2345 00003498 7211                <1>         jb      short cbrk1 ; console (pseudo) tty
  2346                              <1> 	;	
  2347                              <1> 	; Serial port interrupt handler sets [ptty]
  2348                              <1> 	; to the port's tty number (as temporary).
  2349                              <1> 	;
  2350                              <1> 	; If active process is using a stdin or 
  2351                              <1> 	; stdout redirection (by the shell),
  2352                              <1>         ; console tty keyboard must be available
  2353                              <1> 	; to terminate running process,
  2354                              <1> 	; in order to prevent a deadlock. 
  2355                              <1> 	;
  2356 0000349A 52                  <1> 	push	edx
  2357 0000349B 0FB615[29750000]    <1> 	movzx	edx, byte [u.uno]
  2358 000034A2 3A82[F3700000]      <1> 	cmp     al, [edx+p.ttyc-1] ; console tty (rw)
  2359 000034A8 5A                  <1> 	pop	edx
  2360 000034A9 7412                <1> 	je	short cbrk2
  2361                              <1> cbrk1:
  2362 000034AB FEC0                <1> 	inc 	al  ; [u.ttyp] : 1 based tty number
  2363                              <1> 	; 06/12/2013
  2364 000034AD 3A05[0E750000]      <1> 	cmp	al, [u.ttyp] ; recent open tty (r)
  2365 000034B3 7408                <1> 	je	short cbrk2	
  2366 000034B5 3A05[0F750000]      <1>         cmp     al, [u.ttyp+1] ; recent open tty (w)
  2367 000034BB 7509                <1> 	jne	short cbrk3	
  2368                              <1> cbrk2:
  2369                              <1> 	;; 06/12/2013
  2370                              <1> 	;mov	ax, [u.quit]
  2371                              <1> 	;and	ax, ax
  2372                              <1> 	;jz	short cbrk3
  2373                              <1> 	;
  2374                              <1> 	;xor	ax, ax ; 0
  2375                              <1> 	;dec	ax
  2376                              <1> 	; 04/12/2021
  2377 000034BD 31C0                <1> 	xor	eax, eax ; 0
  2378 000034BF 48                  <1> 	dec	eax ; 0FFFFFFFFh
  2379                              <1> 	; 0FFFFh = 'ctrl+brk' keystroke
  2380 000034C0 66A3[26750000]      <1> 	mov	[u.quit], ax
  2381                              <1> cbrk3:
  2382                              <1> 	;pop	ax
  2383                              <1> 	; 04/12/2021
  2384 000034C6 58                  <1> 	pop	eax
  2385                              <1> cbrk4:
  2386 000034C7 C3                  <1> 	retn
  2387                              <1> 
  2388                              <1> com2_int:
  2389                              <1> 	; 08/01/2022 (Retro UNIX 386 v1.2)
  2390                              <1> 	; 07/11/2015 
  2391                              <1> 	; 24/10/2015
  2392                              <1> 	; 23/10/2015
  2393                              <1> 	; 14/03/2015 (Retro UNIX 386 v1 - Beginning)
  2394                              <1> 	; 28/07/2014 (Retro UNIX 8086 v1)
  2395                              <1> 	; < serial port 2 interrupt handler >
  2396                              <1> 	;
  2397 000034C8 890424              <1> 	mov 	[esp], eax ; overwrite call return address
  2398                              <1> 	;;push	eax
  2399                              <1> 	; 08/01/2022
  2400 000034CB 29C0                <1> 	sub	eax, eax
  2401 000034CD B009                <1> 	mov	al, 9
  2402                              <1> 	;mov	ax, 9
  2403 000034CF EB07                <1> 	jmp	short comm_int
  2404                              <1> com1_int:
  2405                              <1> 	; 07/11/2015
  2406                              <1> 	; 24/10/2015
  2407 000034D1 890424              <1> 	mov 	[esp], eax ; overwrite call return address
  2408                              <1> 	; 23/10/2015
  2409                              <1> 	;push	eax
  2410                              <1> 	; 08/01/2022
  2411 000034D4 29C0                <1> 	sub	eax, eax
  2412 000034D6 B008                <1> 	mov	al, 8
  2413                              <1> 	;mov	ax, 8
  2414                              <1> comm_int:
  2415                              <1> 	; 08/01/2022
  2416                              <1> 	; 20/11/2015
  2417                              <1> 	; 18/11/2015
  2418                              <1> 	; 17/11/2015
  2419                              <1> 	; 16/11/2015
  2420                              <1> 	; 09/11/2015
  2421                              <1> 	; 08/11/2015
  2422                              <1> 	; 07/11/2015
  2423                              <1> 	; 06/11/2015 (serial4.asm, 'serial')	
  2424                              <1> 	; 01/11/2015
  2425                              <1> 	; 26/10/2015
  2426                              <1> 	; 23/10/2015
  2427 000034D8 53                  <1> 	push	ebx
  2428 000034D9 56                  <1> 	push	esi
  2429 000034DA 57                  <1> 	push	edi
  2430 000034DB 1E                  <1> 	push 	ds
  2431 000034DC 06                  <1> 	push 	es
  2432                              <1> 	; 18/11/2015
  2433 000034DD 0F20DB              <1> 	mov	ebx, cr3
  2434 000034E0 53                  <1> 	push	ebx ; ****
  2435                              <1> 	;
  2436 000034E1 51                  <1> 	push	ecx ; ***
  2437 000034E2 52                  <1> 	push	edx ; **
  2438                              <1> 	;
  2439 000034E3 BB10000000          <1> 	mov	ebx, KDATA
  2440 000034E8 8EDB                <1> 	mov	ds, bx
  2441 000034EA 8EC3                <1> 	mov	es, bx
  2442                              <1> 	;
  2443 000034EC 8B0D[B86F0000]      <1> 	mov	ecx, [k_page_dir]
  2444 000034F2 0F22D9              <1> 	mov	cr3, ecx
  2445                              <1> 	; 20/11/2015
  2446                              <1> 	; Interrupt identification register
  2447 000034F5 66BAFA02            <1> 	mov	dx, 2FAh ; COM2
  2448                              <1> 	;
  2449 000034F9 3C08                <1> 	cmp 	al, 8 
  2450 000034FB 7702                <1> 	ja 	short com_i0
  2451                              <1> 	;
  2452                              <1> 	; 08/01/2022 (Retro UNIX 386 v1.1)
  2453                              <1> 	; 20/11/2015
  2454                              <1> 	; 17/11/2015
  2455                              <1> 	; 16/11/2015
  2456                              <1> 	; 15/11/2015
  2457                              <1> 	; 24/10/2015
  2458                              <1> 	; 14/03/2015 (Retro UNIX 386 v1 - Beginning)
  2459                              <1> 	; 28/07/2014 (Retro UNIX 8086 v1)
  2460                              <1> 	; < serial port 1 interrupt handler >
  2461                              <1> 	;
  2462 000034FD FEC6                <1> 	inc	dh ; 3FAh ; COM1 Interrupt id. register
  2463                              <1> com_i0:
  2464                              <1> 	;push	eax ; *
  2465                              <1> 	; 07/11/2015
  2466 000034FF A2[26700000]        <1> 	mov 	byte [ccomport], al
  2467                              <1> 	; 09/11/2015
  2468                              <1> 	;movzx	ebx, ax ; 8 or 9
  2469                              <1> 	; 08/01/2022
  2470 00003504 89C3                <1> 	mov	ebx, eax ; 8 or 9
  2471                              <1> 	; 17/11/2015
  2472                              <1>  	; reset request for response status
  2473 00003506 88A3[1C700000]      <1> 	mov	[ebx+req_resp-8], ah ; 0
  2474                              <1> 	;
  2475                              <1> 	; 20/11/2015
  2476 0000350C EC                  <1> 	in	al, dx		; read interrupt id. register
  2477 0000350D EB00                <1> 	JMP	$+2	   	; I/O DELAY
  2478 0000350F 2404                <1> 	and	al, 4		; received data available?	
  2479 00003511 7470                <1> 	jz	short com_eoi	; (transmit. holding reg. empty)
  2480                              <1> 	;
  2481                              <1> 	; 20/11/2015
  2482 00003513 80EA02              <1> 	sub	dl, 3FAh-3F8h	; data register (3F8h, 2F8h)
  2483 00003516 EC                  <1> 	in	al, dx     	; read character
  2484                              <1> 	;JMP	$+2	   	; I/O DELAY
  2485                              <1> 	; 08/11/2015
  2486                              <1> 	; 07/11/2015
  2487 00003517 89DE                <1> 	mov	esi, ebx 
  2488 00003519 89DF                <1> 	mov	edi, ebx
  2489 0000351B 81C6[20700000]      <1> 	add 	esi, rchar - 8 ; points to last received char
  2490 00003521 81C7[22700000]      <1> 	add	edi, schar - 8 ; points to last sent char
  2491 00003527 8806                <1> 	mov	[esi], al ; received char (current char)
  2492                              <1> 	; query
  2493 00003529 20C0                <1> 	and	al, al
  2494 0000352B 7527                <1> 	jnz	short com_i2
  2495                              <1>    	; response
  2496                              <1> 	; 17/11/2015
  2497                              <1> 	; set request for response status
  2498 0000352D FE83[1C700000]      <1>         inc     byte [ebx+req_resp-8] ; 1 
  2499                              <1> 	;
  2500 00003533 6683C205            <1> 	add	dx, 3FDh-3F8h	; (3FDh, 2FDh)
  2501 00003537 EC                  <1> 	in	al, dx	   	; read line status register 
  2502 00003538 EB00                <1> 	JMP	$+2	   	; I/O DELAY
  2503 0000353A 2420                <1> 	and	al, 20h	   	; transmitter holding reg. empty?
  2504 0000353C 7445                <1> 	jz	short com_eoi 	; no
  2505 0000353E B0FF                <1> 	mov 	al, 0FFh   	; response			
  2506 00003540 6683EA05            <1> 	sub	dx, 3FDh-3F8h 	; data port (3F8h, 2F8h)
  2507 00003544 EE                  <1> 	out	dx, al	   	; send on serial port
  2508                              <1> 	; 17/11/2015
  2509 00003545 803F00              <1> 	cmp 	byte [edi], 0   ; query ? (schar)
  2510 00003548 7502                <1> 	jne 	short com_i1    ; no
  2511 0000354A 8807                <1> 	mov	[edi], al 	; 0FFh (responded)
  2512                              <1> com_i1:
  2513                              <1> 	; 17/11/2015
  2514                              <1> 	; reset request for response status (again)
  2515 0000354C FE8B[1C700000]      <1>         dec     byte [ebx+req_resp-8] ; 0 
  2516 00003552 EB2F                <1> 	jmp	short com_eoi
  2517                              <1> com_i2:	
  2518                              <1> 	; 08/11/2015
  2519 00003554 3CFF                <1> 	cmp 	al, 0FFh	; (response ?)
  2520 00003556 7417                <1> 	je	short com_i3	; (check for response signal)
  2521                              <1> 	; 07/11/2015
  2522 00003558 3C04                <1> 	cmp	al, 04h	; EOT
  2523 0000355A 751C                <1> 	jne	short com_i4	
  2524                              <1> 	; EOT = 04h (End of Transmit) - 'CTRL + D'
  2525                              <1> 	;(an EOT char is supposed as a ctrl+brk from the terminal)
  2526                              <1> 	; 08/11/2015
  2527                              <1> 		; ptty -> tty 0 to 7 (pseudo screens)
  2528 0000355C 861D[E66F0000]      <1> 	xchg	bl, [ptty]  ; tty number (8 or 9)
  2529 00003562 E815FFFFFF          <1> 	call 	ctrlbrk
  2530 00003567 861D[E66F0000]      <1> 	xchg	[ptty], bl ; (restore ptty value and BL value)
  2531                              <1> 	;mov	al, 04h ; EOT
  2532                              <1> 	; 08/11/2015
  2533 0000356D EB09                <1> 	jmp	short com_i4	
  2534                              <1> com_i3:
  2535                              <1> 	; 08/11/2015
  2536                              <1> 	; If 0FFh has been received just after a query
  2537                              <1> 	; (schar, ZERO), it is a response signal.
  2538                              <1> 	; 17/11/2015
  2539 0000356F 803F00              <1>         cmp     byte [edi], 0 ; query ? (schar)
  2540 00003572 7704                <1> 	ja	short com_i4 ; no
  2541                              <1> 	; reset query status (schar)
  2542 00003574 8807                <1> 	mov	[edi], al ; 0FFh
  2543 00003576 FEC0                <1> 	inc	al ; 0
  2544                              <1> com_i4:
  2545                              <1> 	; 27/07/2014
  2546                              <1> 	; 09/07/2014
  2547 00003578 D0E3                <1> 	shl	bl, 1	
  2548 0000357A 81C3[E86F0000]      <1> 	add	ebx, ttychr
  2549                              <1> 	; 23/07/2014 (always overwrite)
  2550                              <1> 	;;cmp	word [ebx], 0
  2551                              <1> 	;;ja	short com_eoi
  2552                              <1> 	;
  2553 00003580 668903              <1> 	mov	[ebx], ax   ; Save ascii code
  2554                              <1> 			    ; scan code = 0
  2555                              <1> com_eoi:
  2556                              <1> 	;mov	al, 20h
  2557                              <1> 	;out	20h, al	   ; end of interrupt
  2558                              <1> 	;
  2559                              <1> 	; 07/11/2015
  2560                              <1>       	;pop	eax ; *
  2561 00003583 A0[26700000]        <1> 	mov	al, byte [ccomport] ; current COM port
  2562                              <1> 	; al = tty number (8 or 9)
  2563 00003588 E8D81A0000          <1>         call	wakeup
  2564                              <1> com_iret:
  2565                              <1> 	; 23/10/2015
  2566 0000358D 5A                  <1> 	pop	edx ; **
  2567 0000358E 59                  <1> 	pop	ecx ; ***
  2568                              <1> 	; 18/11/2015
  2569                              <1> 	;pop	eax ; ****
  2570                              <1> 	;mov	cr3, eax
  2571                              <1> 	;jmp	iiret
  2572 0000358F E90AD4FFFF          <1> 	jmp	iiretp
  2573                              <1> 
  2574                              <1> ;iiretp: ; 01/09/2015
  2575                              <1> ;	; 28/08/2015
  2576                              <1> ;	pop	eax ; (*) page directory
  2577                              <1> ;	mov	cr3, eax
  2578                              <1> ;iiret:
  2579                              <1> ;	; 22/08/2014
  2580                              <1> ;	mov	al, 20h ; END OF INTERRUPT COMMAND TO 8259
  2581                              <1> ;	out	20h, al	; 8259 PORT
  2582                              <1> ;	;
  2583                              <1> ;	pop	es
  2584                              <1> ;	pop	ds
  2585                              <1> ;	pop	edi
  2586                              <1> ;	pop	esi
  2587                              <1> ;	pop	ebx ; 29/08/2014
  2588                              <1> ;	pop 	eax
  2589                              <1> ;	iretd
  2590                              <1> 
  2591                              <1> sp_init:
  2592                              <1> 	; 08/01/2022 (Retro UNIX 386 v1.2)
  2593                              <1> 	; 07/11/2015
  2594                              <1> 	; 29/10/2015
  2595                              <1> 	; 26/10/2015
  2596                              <1> 	; 23/10/2015
  2597                              <1> 	; 29/06/2015
  2598                              <1> 	; 14/03/2015 (Retro UNIX 386 v1 - 115200 baud)
  2599                              <1> 	; 28/07/2014 (Retro UNIX 8086 v1 - 9600 baud)
  2600                              <1> 	; Initialization of Serial Port Communication Parameters
  2601                              <1> 	; (COM1 base port address = 3F8h, COM1 Interrupt = IRQ 4)
  2602                              <1> 	; (COM2 base port address = 2F8h, COM1 Interrupt = IRQ 3)
  2603                              <1> 	;
  2604                              <1> 	; ((Modified registers: EAX, ECX, EDX, EBX))
  2605                              <1> 	;
  2606                              <1> 	; INPUT:  (29/06/2015)
  2607                              <1> 	;	AL = 0 for COM1
  2608                              <1> 	;	     1 for COM2
  2609                              <1> 	;	AH = Communication parameters	
  2610                              <1> 	;
  2611                              <1> 	;  (*) Communication parameters (except BAUD RATE):
  2612                              <1> 	;	Bit	4	3	2	1	0
  2613                              <1> 	;		-PARITY--   STOP BIT  -WORD LENGTH-	 		 
  2614                              <1> 	;  this one -->	00 = none    0 = 1 bit  11 = 8 bits
  2615                              <1> 	;		01 = odd     1 = 2 bits	10 = 7 bits
  2616                              <1> 	;		11 = even
  2617                              <1> 	;  Baud rate setting bits: (29/06/2015)
  2618                              <1> 	;		Retro UNIX 386 v1 feature only !
  2619                              <1> 	;	Bit	7    6    5  | Baud rate
  2620                              <1> 	;		------------------------
  2621                              <1> 	;	value	0    0    0  | Default (Divisor = 1)
  2622                              <1> 	;		0    0    1  | 9600 (12)
  2623                              <1> 	;		0    1    0  | 19200 (6) 
  2624                              <1> 	;		0    1	  1  | 38400 (3) 
  2625                              <1> 	;		1    0	  0  | 14400 (8)
  2626                              <1> 	;		1    0	  1  | 28800 (4)
  2627                              <1> 	;		1    1    0  | 57600 (2)
  2628                              <1> 	;		1    1    1  | 115200 (1) 	
  2629                              <1> 	
  2630                              <1> 	; References:	
  2631                              <1> 	; (1) IBM PC-XT Model 286 BIOS Source Code
  2632                              <1> 	;     RS232.ASM --- 10/06/1985 COMMUNICATIONS BIOS (RS232)
  2633                              <1> 	; (2) Award BIOS 1999 - ATORGS.ASM
  2634                              <1> 	; (3) http://wiki.osdev.org/Serial_Ports
  2635                              <1> 	;
  2636                              <1> 	; Set communication parameters for COM1 (= 03h)	
  2637                              <1> 	;
  2638 00003594 BB[22700000]        <1> 	mov	ebx, com1p		; COM1 parameters  
  2639 00003599 66BAF803            <1> 	mov	dx, 3F8h		; COM1
  2640                              <1> 	; 29/10/2015
  2641 0000359D 66B90103            <1> 	mov	cx, 301h  ; divisor = 1 (115200 baud)
  2642 000035A1 E84F000000          <1> 	call	sp_i3	; call A4	
  2643 000035A6 A880                <1> 	test	al, 80h
  2644 000035A8 740E                <1> 	jz	short sp_i0 ; OK..
  2645                              <1> 		; Error !
  2646                              <1> 	;mov	dx, 3F8h
  2647 000035AA 80EA05              <1> 	sub	dl, 5 ; 3FDh -> 3F8h
  2648                              <1> 	;mov	cx, 30Eh  ; divisor = 12 (9600 baud)
  2649                              <1> 	; 08/01/2022
  2650 000035AD B10E                <1> 	mov	cl, 0Eh ; cx = 30Eh, divisor = 12 (9600 baud)
  2651 000035AF E841000000          <1> 	call	sp_i3	; call A4	
  2652 000035B4 A880                <1> 	test	al, 80h
  2653 000035B6 7508                <1> 	jnz	short sp_i1
  2654                              <1> sp_i0:
  2655                              <1>         ; (Note: Serial port interrupts will be disabled here...)	
  2656                              <1>         ; (INT 14h initialization code disables interrupts.)
  2657                              <1> 	;
  2658 000035B8 C603E3              <1> 	mov	byte [ebx], 0E3h ; 11100011b
  2659 000035BB E8BF000000          <1> 	call	sp_i5 ; 29/06/2015
  2660                              <1> sp_i1:
  2661 000035C0 43                  <1> 	inc	ebx
  2662 000035C1 66BAF802            <1> 	mov	dx, 2F8h		; COM2
  2663                              <1> 	; 29/10/2015
  2664                              <1> 	;mov	cx, 301h  ; divisor = 1 (115200 baud)
  2665                              <1> 	; 08/01/2022
  2666 000035C5 B101                <1> 	mov	cl, 01h ; cx = 301h, divisor = 1 (115200 baud)
  2667 000035C7 E829000000          <1> 	call	sp_i3	; call A4	
  2668 000035CC A880                <1> 	test	al, 80h
  2669 000035CE 740E                <1> 	jz	short sp_i2 ; OK..
  2670                              <1> 		; Error !
  2671                              <1> 	;mov	dx, 2F8h
  2672 000035D0 80EA05              <1> 	sub	dl, 5 ; 2FDh -> 2F8h
  2673                              <1> 	;mov	cx, 30Eh  ; divisor = 12 (9600 baud)
  2674                              <1> 	; 08/01/2022
  2675 000035D3 B10E                <1> 	mov	cl, 0Eh ; cx = 30Eh, divisor = 12 (9600 baud)
  2676 000035D5 E81B000000          <1> 	call	sp_i3	; call A4	
  2677 000035DA A880                <1> 	test	al, 80h
  2678 000035DC 7516                <1> 	jnz	short sp_i7
  2679                              <1> sp_i2:
  2680 000035DE C603E3              <1> 	mov	byte [ebx], 0E3h ; 11100011b
  2681                              <1> sp_i6:
  2682                              <1> 	;; COM2 - enabling IRQ 3
  2683                              <1> 	; 08/01/2022
  2684 000035E1 B4F7                <1> 	mov	ah, 0F7h ; enable IRQ 3 (COM2)
  2685                              <1> 	; 07/11/2015
  2686                              <1> 	; 26/10/2015
  2687                              <1> 	;pushf
  2688                              <1> 	;cli
  2689                              <1> 	;;
  2690                              <1> 	;;mov	dx, 2FCh   		; modem control register
  2691                              <1> 	;mov	dl, 0FCh ; 08/01/2022
  2692                              <1> 	;in	al, dx 	   		; read register
  2693                              <1> 	;JMP	$+2	   		; I/O DELAY
  2694                              <1> 	;or	al, 8      		; enable bit 3 (OUT2)
  2695                              <1> 	;out	dx, al     		; write back to register
  2696                              <1> 	;JMP	$+2	   		; I/O DELAY
  2697                              <1> 	;;mov	dx, 2F9h   		; interrupt enable register
  2698                              <1> 	;mov	dl, 0F9h ; 08/01/2022
  2699                              <1> 	;in	al, dx     		; read register
  2700                              <1> 	;JMP	$+2	   		; I/O DELAY
  2701                              <1> 	;;or	al, 1      		; receiver data interrupt enable and
  2702                              <1> 	;or	al, 3	   		; transmitter empty interrupt enable
  2703                              <1> 	;out	dx, al 	   		; write back to register
  2704                              <1> 	;JMP	$+2        		; I/O DELAY
  2705                              <1> 	;in	al, 21h    		; read interrupt mask register
  2706                              <1> 	;JMP	$+2	   		; I/O DELAY
  2707                              <1> 	;and	al, 0F7h   		; enable IRQ 3 (COM2)
  2708                              <1> 	;out	21h, al    		; write back to register
  2709                              <1> 	;
  2710                              <1> 	; 08/01/2022
  2711 000035E3 9C                  <1> 	pushf
  2712 000035E4 E8AA000000          <1> 	call	sp_i8
  2713                              <1> 	; 23/10/2015
  2714 000035E9 B8[C8340000]        <1> 	mov 	eax, com2_int
  2715 000035EE A3[A13A0000]        <1> 	mov	[com2_irq3], eax
  2716                              <1> 	; 26/10/2015
  2717 000035F3 9D                  <1> 	popf	
  2718                              <1> sp_i7:
  2719 000035F4 C3                  <1> 	retn
  2720                              <1> 
  2721                              <1> sp_i3:
  2722                              <1> ;A4:  	;-----	INITIALIZE THE COMMUNICATIONS PORT
  2723                              <1> 	; 28/10/2015
  2724 000035F5 FEC2                <1> 	inc	dl	; 3F9h (2F9h)	; 3F9h, COM1 Interrupt enable register 
  2725 000035F7 B000                <1> 	mov	al, 0
  2726 000035F9 EE                  <1> 	out	dx, al			; disable serial port interrupt
  2727 000035FA EB00                <1> 	JMP	$+2			; I/O DELAY
  2728 000035FC 80C202              <1> 	add	dl, 2 	; 3FBh (2FBh)	; COM1 Line control register (3FBh)
  2729 000035FF B080                <1> 	mov	al, 80h			
  2730 00003601 EE                  <1> 	out	dx, al			; SET DLAB=1 ; divisor latch access bit
  2731                              <1> 	;-----	SET BAUD RATE DIVISOR
  2732                              <1> 	; 26/10/2015
  2733 00003602 80EA03              <1> 	sub 	dl, 3   ; 3F8h (2F8h)	; register for least significant byte
  2734                              <1> 					; of the divisor value
  2735 00003605 88C8                <1> 	mov	al, cl	; 1
  2736 00003607 EE                  <1> 	out	dx, al			; 1 = 115200 baud (Retro UNIX 386 v1)
  2737                              <1> 					; 2 = 57600 baud
  2738                              <1> 					; 3 = 38400 baud
  2739                              <1> 					; 6 = 19200 baud
  2740                              <1> 					; 12 = 9600 baud (Retro UNIX 8086 v1)
  2741 00003608 EB00                <1> 	JMP	$+2			; I/O DELAY
  2742 0000360A 28C0                <1> 	sub	al, al
  2743 0000360C FEC2                <1> 	inc	dl      ; 3F9h (2F9h)	; register for most significant byte
  2744                              <1> 					; of the divisor value
  2745 0000360E EE                  <1> 	out	dx, al ; 0
  2746 0000360F EB00                <1> 	JMP	$+2			; I/O DELAY
  2747                              <1> 	;	
  2748 00003611 88E8                <1> 	mov	al, ch ; 3		; 8 data bits, 1 stop bit, no parity
  2749                              <1> 	;and	al, 1Fh ; Bits 0,1,2,3,4	
  2750 00003613 80C202              <1> 	add	dl, 2	; 3FBh (2FBh)	; Line control register
  2751 00003616 EE                  <1> 	out	dx, al			
  2752 00003617 EB00                <1> 	JMP	$+2			; I/O DELAY
  2753                              <1> 	; 29/10/2015
  2754 00003619 FECA                <1> 	dec 	dl 	; 3FAh (2FAh)	; FIFO Control register (16550/16750)
  2755 0000361B 30C0                <1> 	xor	al, al			; 0
  2756 0000361D EE                  <1> 	out	dx, al			; Disable FIFOs (reset to 8250 mode)
  2757 0000361E EB00                <1> 	JMP	$+2	
  2758                              <1> sp_i4:
  2759                              <1> ;A18:	;-----	COMM PORT STATUS ROUTINE
  2760                              <1> 	; 29/06/2015 (line status after modem status)
  2761 00003620 80C204              <1> 	add	dl, 4	; 3FEh (2FEh)	; Modem status register
  2762                              <1> sp_i4s:
  2763 00003623 EC                  <1> 	in	al, dx			; GET MODEM CONTROL STATUS
  2764 00003624 EB00                <1> 	JMP	$+2			; I/O DELAY
  2765 00003626 88C4                <1> 	mov	ah, al			; PUT IN (AH) FOR RETURN
  2766 00003628 FECA                <1> 	dec	dl	; 3FDh (2FDh)	; POINT TO LINE STATUS REGISTER
  2767                              <1> 					; dx = 3FDh for COM1, 2FDh for COM2
  2768 0000362A EC                  <1> 	in	al, dx			; GET LINE CONTROL STATUS
  2769                              <1> 	; AL = Line status, AH = Modem status
  2770 0000362B C3                  <1> 	retn
  2771                              <1> 
  2772                              <1> sp_status:
  2773                              <1> 	; 29/06/2015
  2774                              <1> 	; 27/06/2015 (Retro UNIX 386 v1)
  2775                              <1> 	; Get serial port status
  2776 0000362C 66BAFE03            <1> 	mov	dx, 3FEh		; Modem status register (COM1)
  2777 00003630 28C6                <1> 	sub	dh, al			; dh = 2 for COM2 (al = 1)
  2778                              <1> 					; dx = 2FEh for COM2
  2779 00003632 EBEF                <1> 	jmp	short sp_i4s
  2780                              <1> 
  2781                              <1> sp_setp: ; Set serial port communication parameters
  2782                              <1> 	; 08/01/2022
  2783                              <1> 	; 24/12/2021 (Retro UNIX 386 v1.2)
  2784                              <1> 	; 07/11/2015
  2785                              <1> 	; 29/10/2015
  2786                              <1> 	; 29/06/2015
  2787                              <1> 	; Retro UNIX 386 v1 feature only !	
  2788                              <1> 	;
  2789                              <1> 	; INPUT:
  2790                              <1> 	;	AL = 0 for COM1
  2791                              <1> 	;	     1 for COM2
  2792                              <1> 	;	AH = Communication parameters (*)
  2793                              <1> 	; OUTPUT:
  2794                              <1> 	;	CL = Line status
  2795                              <1> 	;	CH = Modem status
  2796                              <1> 	;   If cf = 1 -> Error code in [u.error]
  2797                              <1> 	;		 'invalid parameter !' 
  2798                              <1> 	;		 	 or
  2799                              <1> 	;		 'device not ready !' error
  2800                              <1> 	;	
  2801                              <1> 	;  (*) Communication parameters (except BAUD RATE):
  2802                              <1> 	;	Bit	4	3	2	1	0
  2803                              <1> 	;		-PARITY--   STOP BIT  -WORD LENGTH-	 		 
  2804                              <1> 	;  this one -->	00 = none    0 = 1 bit  11 = 8 bits
  2805                              <1> 	;		01 = odd     1 = 2 bits	10 = 7 bits
  2806                              <1> 	;		11 = even
  2807                              <1> 	;  Baud rate setting bits: (29/06/2015)
  2808                              <1> 	;		Retro UNIX 386 v1 feature only !
  2809                              <1> 	;	Bit	7    6    5  | Baud rate
  2810                              <1> 	;		------------------------
  2811                              <1> 	;	value	0    0    0  | Default (Divisor = 1)
  2812                              <1> 	;		0    0    1  | 9600 (12)
  2813                              <1> 	;		0    1    0  | 19200 (6) 
  2814                              <1> 	;		0    1	  1  | 38400 (3) 
  2815                              <1> 	;		1    0	  0  | 14400 (8)
  2816                              <1> 	;		1    0	  1  | 28800 (4)
  2817                              <1> 	;		1    1    0  | 57600 (2)
  2818                              <1> 	;		1    1    1  | 115200 (1) 
  2819                              <1> 	;
  2820                              <1> 	; (COM1 base port address = 3F8h, COM1 Interrupt = IRQ 4)
  2821                              <1> 	; (COM2 base port address = 2F8h, COM1 Interrupt = IRQ 3)
  2822                              <1> 	;
  2823                              <1> 	; ((Modified registers: EAX, ECX, EDX, EBX))
  2824                              <1> 	;
  2825 00003634 66BAF803            <1> 	mov	dx, 3F8h
  2826 00003638 BB[22700000]        <1> 	mov	ebx, com1p ; COM1 control byte offset
  2827 0000363D 3C01                <1> 	cmp	al, 1
  2828 0000363F 7770                <1> 	ja 	short sp_invp_err
  2829 00003641 7203                <1> 	jb	short sp_setp1 ;  COM1 (AL = 0)
  2830 00003643 FECE                <1> 	dec	dh ; 2F8h
  2831 00003645 43                  <1> 	inc	ebx ; COM2 control byte offset
  2832                              <1> sp_setp1:
  2833                              <1> 	; 29/10/2015
  2834 00003646 8823                <1> 	mov	[ebx], ah
  2835 00003648 0FB6CC              <1> 	movzx 	ecx, ah
  2836 0000364B C0E905              <1> 	shr	cl, 5 ; -> baud rate index
  2837 0000364E 80E41F              <1> 	and	ah, 1Fh ; communication parameters except baud rate
  2838 00003651 8A81[C0360000]      <1> 	mov	al, [ecx+b_div_tbl]
  2839 00003657 6689C1              <1> 	mov	cx, ax
  2840 0000365A E896FFFFFF          <1> 	call	sp_i3
  2841 0000365F 6689C1              <1> 	mov	cx, ax ; CL = Line status, CH = Modem status
  2842 00003662 A880                <1> 	test	al, 80h
  2843 00003664 740F                <1> 	jz	short sp_setp2
  2844 00003666 C603E3              <1>         mov     byte [ebx], 0E3h ; Reset to initial value (11100011b)
  2845                              <1> stp_dnr_err:
  2846 00003669 C705[4C750000]0F00- <1> 	mov	dword [u.error], ERR_DEV_NOT_RDY ; 'device not ready !'
  2846 00003671 0000                <1>
  2847                              <1> 	; CL = Line status, CH = Modem status
  2848 00003673 F9                  <1> 	stc
  2849 00003674 C3                  <1> 	retn
  2850                              <1> sp_setp2:
  2851 00003675 80FE02              <1> 	cmp	dh, 2 ; COM2 (2F?h)
  2852                              <1>         ;jna	sp_i6
  2853                              <1> 		      ; COM1 (3F?h)
  2854                              <1> 	; 24/12/2021
  2855 00003678 7705                <1> 	ja	short sp_i5
  2856 0000367A E962FFFFFF          <1> 	jmp	sp_i6
  2857                              <1> sp_i5: 
  2858                              <1> 	; 08/01/2022
  2859 0000367F B4EF                <1> 	mov	ah, 0EFh ; enable IRQ 4 (COM1)
  2860                              <1> 	; 07/11/2015
  2861                              <1> 	; 26/10/2015
  2862                              <1> 	; 29/06/2015
  2863                              <1> 	;
  2864                              <1> 	;; COM1 - enabling IRQ 4
  2865                              <1> 	;pushf
  2866                              <1> 	;cli
  2867                              <1> 	;;mov	dx, 3FCh   		; modem control register
  2868                              <1> 	;mov	dl, 0FCh ; 08/01/2022
  2869                              <1> 	;in	al, dx 	   		; read register
  2870                              <1> 	;JMP	$+2			; I/O DELAY
  2871                              <1> 	;or	al, 8      		; enable bit 3 (OUT2)
  2872                              <1> 	;out	dx, al     		; write back to register
  2873                              <1> 	;JMP	$+2			; I/O DELAY
  2874                              <1> 	;;mov	dx, 3F9h   		; interrupt enable register
  2875                              <1> 	;mov	dl, 0F9h ; 08/01/2022
  2876                              <1> 	;in	al, dx     		; read register
  2877                              <1> 	;JMP	$+2			; I/O DELAY
  2878                              <1> 	;;or	al, 1      		; receiver data interrupt enable and
  2879                              <1> 	;or	al, 3	   		; transmitter empty interrupt enable
  2880                              <1> 	;out	dx, al 	   		; write back to register
  2881                              <1> 	;JMP	$+2        		; I/O DELAY
  2882                              <1> 	;in	al, 21h    		; read interrupt mask register
  2883                              <1> 	;JMP	$+2			; I/O DELAY
  2884                              <1> 	;and	al, 0EFh   		; enable IRQ 4 (COM1)
  2885                              <1> 	;out	21h, al    		; write back to register
  2886                              <1> 	;
  2887                              <1> 	; 08/01/2022
  2888 00003681 9C                  <1> 	pushf
  2889 00003682 E80C000000          <1> 	call	sp_i8
  2890                              <1> 	; 23/10/2015
  2891 00003687 B8[D1340000]        <1> 	mov 	eax, com1_int
  2892 0000368C A3[9D3A0000]        <1> 	mov	[com1_irq4], eax
  2893                              <1> 	; 26/10/2015
  2894 00003691 9D                  <1> 	popf
  2895 00003692 C3                  <1> 	retn
  2896                              <1> 
  2897                              <1> sp_i8:
  2898                              <1> 	; 08/01/2022
  2899                              <1> 	;pushf
  2900 00003693 FA                  <1> 	cli
  2901                              <1> 	;
  2902                              <1> 	;mov	dx, 2FCh  ; 3FCh	; modem control register
  2903 00003694 B2FC                <1> 	mov	dl, 0FCh
  2904 00003696 EC                  <1> 	in	al, dx 	   		; read register
  2905 00003697 EB00                <1> 	JMP	$+2	   		; I/O DELAY
  2906 00003699 0C08                <1> 	or	al, 8      		; enable bit 3 (OUT2)
  2907 0000369B EE                  <1> 	out	dx, al     		; write back to register
  2908 0000369C EB00                <1> 	JMP	$+2	   		; I/O DELAY
  2909                              <1> 	;mov	dx, 2F9h  ; 3F9h 		; interrupt enable register
  2910 0000369E B2F9                <1> 	mov	dl, 0F9h
  2911 000036A0 EC                  <1> 	in	al, dx     		; read register
  2912 000036A1 EB00                <1> 	JMP	$+2	   		; I/O DELAY
  2913                              <1> 	;or	al, 1      		; receiver data interrupt enable and
  2914 000036A3 0C03                <1> 	or	al, 3	   		; transmitter empty interrupt enable
  2915 000036A5 EE                  <1> 	out	dx, al 	   		; write back to register
  2916 000036A6 EB00                <1> 	JMP	$+2        		; I/O DELAY
  2917 000036A8 E421                <1> 	in	al, 21h    		; read interrupt mask register
  2918 000036AA EB00                <1> 	JMP	$+2	   		; I/O DELAY
  2919                              <1> 	;and	al, 0F7h  ; 0EFh	; enable IRQ 3 (COM2)
  2920 000036AC 20E0                <1> 	and	al, ah	; 0F7h or 0EFh 
  2921 000036AE E621                <1> 	out	21h, al    		; write back to register
  2922                              <1> 	;
  2923                              <1> 	;popf	
  2924 000036B0 C3                  <1> 	retn
  2925                              <1> 
  2926                              <1> sp_invp_err:
  2927 000036B1 C705[4C750000]1700- <1> 	mov	dword [u.error], ERR_INV_PARAMETER ; 'invalid parameter !' 
  2927 000036B9 0000                <1>
  2928 000036BB 31C9                <1> 	xor	ecx, ecx
  2929 000036BD 49                  <1> 	dec	ecx ; 0FFFFh
  2930 000036BE F9                  <1> 	stc
  2931 000036BF C3                  <1> 	retn
  2932                              <1> 
  2933                              <1> ; 29/10/2015
  2934                              <1> b_div_tbl: ; Baud rate divisor table (115200/divisor)
  2935 000036C0 010C0603080401      <1> 	db	1, 12, 6, 3, 8, 4, 1
  2936                              <1> 
  2937                              <1> ; Retro UNIX 8086 v1 - UNIX.ASM (01/09/2014) 
  2938                              <1> epoch:
  2939                              <1> 	; 04/12/2021 (Retro UNIX 386 v1.2)
  2940                              <1> 	; 15/03/2015 (Retro UNIX 386 v1 - 32 bit version)
  2941                              <1> 	; 09/04/2013 (Retro UNIX 8086 v1 - UNIX.ASM)
  2942                              <1> 	; 'epoch' procedure prototype: 
  2943                              <1> 	; 	            UNIXCOPY.ASM, 10/03/2013
  2944                              <1> 	; 14/11/2012
  2945                              <1> 	; unixboot.asm (boot file configuration)
  2946                              <1> 	; version of "epoch" procedure in "unixproc.asm"
  2947                              <1> 	; 21/7/2012
  2948                              <1> 	; 15/7/2012
  2949                              <1> 	; 14/7/2012		
  2950                              <1> 	; Erdogan Tan - RETRO UNIX v0.1
  2951                              <1> 	; compute current date and time as UNIX Epoch/Time
  2952                              <1> 	; UNIX Epoch: seconds since 1/1/1970 00:00:00
  2953                              <1> 	;
  2954                              <1>         ;  ((Modified registers: EAX, EDX, ECX, EBX))  
  2955                              <1> 	;
  2956 000036C7 E81B010000          <1> 	call 	get_rtc_time		; Return Current Time
  2957 000036CC 86E9                <1>         xchg 	ch,cl
  2958 000036CE 66890D[266D0000]    <1>         mov 	[hour], cx
  2959 000036D5 86F2                <1>         xchg 	dh,dl
  2960 000036D7 668915[2A6D0000]    <1>         mov 	[second], dx
  2961                              <1> 	;
  2962 000036DE E835010000          <1>         call 	get_rtc_date		; Return Current Date
  2963 000036E3 86E9                <1>         xchg 	ch,cl
  2964 000036E5 66890D[206D0000]    <1>         mov 	[year], cx
  2965 000036EC 86F2                <1>         xchg 	dh,dl
  2966 000036EE 668915[226D0000]    <1>         mov 	[month], dx
  2967                              <1> 	;
  2968 000036F5 66B93030            <1> 	mov 	cx, 3030h
  2969                              <1> 	;
  2970 000036F9 A0[266D0000]        <1> 	mov 	al, [hour] ; Hour
  2971                              <1>         	; AL <= BCD number)
  2972 000036FE D410                <1>         db 	0D4h,10h		; Undocumented inst. AAM
  2973                              <1> 					; AH = AL / 10h
  2974                              <1> 					; AL = AL MOD 10h
  2975 00003700 D50A                <1>         aad 	; AX= AH*10+AL
  2976 00003702 A2[266D0000]        <1> 	mov 	[hour], al
  2977 00003707 A0[276D0000]        <1> 	mov 	al, [hour+1] ; Minute
  2978                              <1>         	; AL <= BCD number)
  2979 0000370C D410                <1>         db 	0D4h,10h		; Undocumented inst. AAM
  2980                              <1> 					; AH = AL / 10h
  2981                              <1> 					; AL = AL MOD 10h
  2982 0000370E D50A                <1>         aad 	; AX= AH*10+AL
  2983 00003710 A2[286D0000]        <1> 	mov 	[minute], al
  2984 00003715 A0[2A6D0000]        <1> 	mov 	al, [second] ; Second
  2985                              <1>         	; AL <= BCD number)
  2986 0000371A D410                <1>         db 	0D4h,10h		; Undocumented inst. AAM
  2987                              <1> 					; AH = AL / 10h
  2988                              <1> 					; AL = AL MOD 10h
  2989 0000371C D50A                <1>         aad 	; AX= AH*10+AL
  2990 0000371E A2[2A6D0000]        <1> 	mov 	[second], al
  2991 00003723 66A1[206D0000]      <1> 	mov 	ax, [year] ; Year (century)
  2992                              <1> 	;push 	ax
  2993                              <1> 	; 04/12/2021
  2994 00003729 50                  <1> 	push	eax
  2995                              <1> 	   	; AL <= BCD number)
  2996 0000372A D410                <1>         db 	0D4h,10h		; Undocumented inst. AAM
  2997                              <1> 					; AH = AL / 10h
  2998                              <1> 					; AL = AL MOD 10h
  2999 0000372C D50A                <1>         aad 	; AX= AH*10+AL
  3000 0000372E B464                <1> 	mov 	ah, 100
  3001 00003730 F6E4                <1> 	mul 	ah
  3002 00003732 66A3[206D0000]      <1> 	mov 	[year], ax
  3003                              <1> 	;pop	ax
  3004                              <1> 	; 04/12/2021
  3005 00003738 58                  <1> 	pop	eax
  3006 00003739 88E0                <1> 	mov	al, ah
  3007                              <1>         	; AL <= BCD number)
  3008 0000373B D410                <1>         db 	0D4h,10h		; Undocumented inst. AAM
  3009                              <1> 					; AH = AL / 10h
  3010                              <1> 					; AL = AL MOD 10h
  3011 0000373D D50A                <1>         aad 	; AX= AH*10+AL
  3012 0000373F 660105[206D0000]    <1> 	add 	[year], ax
  3013 00003746 A0[226D0000]        <1> 	mov 	al, [month] ; Month
  3014                              <1>            	; AL <= BCD number)
  3015 0000374B D410                <1>         db 	0D4h,10h		; Undocumented inst. AAM
  3016                              <1> 					; AH = AL / 10h
  3017                              <1> 					; AL = AL MOD 10h
  3018 0000374D D50A                <1>         aad 	; AX= AH*10+AL
  3019 0000374F A2[226D0000]        <1> 	mov 	[month], al	
  3020 00003754 A0[236D0000]        <1>         mov     al, [month+1]      	; Day
  3021                              <1>            	; AL <= BCD number)
  3022 00003759 D410                <1>         db 	0D4h,10h		; Undocumented inst. AAM
  3023                              <1> 					; AH = AL / 10h
  3024                              <1> 					; AL = AL MOD 10h
  3025 0000375B D50A                <1>         aad 	; AX= AH*10+AL
  3026 0000375D A2[246D0000]        <1>         mov     [day], al
  3027                              <1> 	
  3028                              <1> convert_to_epoch:
  3029                              <1> 	; 15/03/2015 (Retro UNIX 386 v1 - 32 bit modification)
  3030                              <1> 	; 09/04/2013 (Retro UNIX 8086 v1)
  3031                              <1> 	;
  3032                              <1> 	; ((Modified registers: EAX, EDX, EBX)) 
  3033                              <1> 	;
  3034                              <1> 	; Derived from DALLAS Semiconductor
  3035                              <1> 	; Application Note 31 (DS1602/DS1603)
  3036                              <1> 	; 6 May 1998
  3037 00003762 29C0                <1> 	sub 	eax, eax
  3038 00003764 66A1[206D0000]      <1> 	mov 	ax, [year]
  3039 0000376A 662DB207            <1> 	sub 	ax, 1970
  3040 0000376E BA6D010000          <1> 	mov 	edx, 365
  3041 00003773 F7E2                <1> 	mul 	edx
  3042 00003775 31DB                <1> 	xor 	ebx, ebx
  3043 00003777 8A1D[226D0000]      <1> 	mov 	bl, [month]
  3044 0000377D FECB                <1> 	dec 	bl
  3045 0000377F D0E3                <1> 	shl 	bl, 1
  3046                              <1> 	;sub	edx, edx
  3047 00003781 668B93[2C6D0000]    <1> 	mov 	dx, [EBX+DMonth]
  3048 00003788 8A1D[246D0000]      <1>         mov     bl, [day]
  3049 0000378E FECB                <1> 	dec 	bl
  3050 00003790 01D0                <1> 	add 	eax, edx
  3051 00003792 01D8                <1> 	add 	eax, ebx
  3052                              <1> 			; EAX = days since 1/1/1970
  3053 00003794 668B15[206D0000]    <1> 	mov 	dx, [year]
  3054 0000379B 6681EAB107          <1> 	sub 	dx, 1969
  3055 000037A0 66D1EA              <1> 	shr 	dx, 1
  3056 000037A3 66D1EA              <1> 	shr 	dx, 1		
  3057                              <1> 		; (year-1969)/4
  3058 000037A6 01D0                <1> 	add 	eax, edx
  3059                              <1> 			; + leap days since 1/1/1970
  3060 000037A8 803D[226D0000]02    <1> 	cmp 	byte [month], 2	; if past february
  3061 000037AF 7610                <1> 	jna 	short cte1
  3062 000037B1 668B15[206D0000]    <1> 	mov 	dx, [year]
  3063 000037B8 6683E203            <1> 	and 	dx, 3 ; year mod 4
  3064 000037BC 7503                <1> 	jnz 	short cte1		
  3065                              <1> 			; and if leap year
  3066 000037BE 83C001              <1> 	add 	eax, 1 	; add this year's leap day (february 29)
  3067                              <1> cte1: 			; compute seconds since 1/1/1970
  3068 000037C1 BA18000000          <1> 	mov 	edx, 24
  3069 000037C6 F7E2                <1> 	mul	edx
  3070 000037C8 8A15[266D0000]      <1> 	mov 	dl, [hour]
  3071 000037CE 01D0                <1> 	add 	eax, edx
  3072                              <1> 		; EAX = hours since 1/1/1970 00:00:00
  3073                              <1> 	;mov	ebx, 60
  3074 000037D0 B33C                <1> 	mov	bl, 60
  3075 000037D2 F7E3                <1> 	mul	ebx
  3076 000037D4 8A15[286D0000]      <1> 	mov 	dl, [minute]
  3077 000037DA 01D0                <1> 	add 	eax, edx
  3078                              <1> 		; EAX = minutes since 1/1/1970 00:00:00
  3079                              <1> 	;mov 	ebx, 60
  3080 000037DC F7E3                <1> 	mul	ebx
  3081 000037DE 8A15[2A6D0000]      <1> 	mov 	dl, [second]
  3082 000037E4 01D0                <1> 	add 	eax, edx
  3083                              <1>  		; EAX -> seconds since 1/1/1970 00:00:00
  3084 000037E6 C3                  <1> 	retn
  3085                              <1> 
  3086                              <1> get_rtc_time:
  3087                              <1> 	; 15/03/2015
  3088                              <1> 	; Derived from IBM PC-XT Model 286 BIOS Source Code
  3089                              <1> 	; BIOS2.ASM ---- 10/06/1985 BIOS INTERRUPT ROUTINES
  3090                              <1> 	; INT 1Ah
  3091                              <1> 	; (AH) = 02H  READ THE REAL TIME CLOCK AND RETURN WITH,	:
  3092                              <1> 	;       (CH) = HOURS IN BCD (00-23)			:
  3093                              <1> 	;       (CL) = MINUTES IN BCD (00-59)			:
  3094                              <1> 	;       (DH) = SECONDS IN BCD (00-59)			:
  3095                              <1> 	;       (DL) = DAYLIGHT SAVINGS ENABLE (00-01).		:
  3096                              <1> 	;								
  3097                              <1> RTC_20: 				; GET RTC TIME
  3098 000037E7 FA                  <1> 	cli
  3099 000037E8 E845D3FFFF          <1> 	CALL	UPD_IPR 		; CHECK FOR UPDATE IN PROCESS
  3100 000037ED 7227                <1> 	JC	short RTC_29		; EXIT IF ERROR (CY= 1)
  3101                              <1> 
  3102 000037EF B000                <1> 	MOV	AL, CMOS_SECONDS 	; SET ADDRESS OF SECONDS
  3103 000037F1 E826D3FFFF          <1> 	CALL	CMOS_READ		; GET SECONDS
  3104 000037F6 88C6                <1> 	MOV	DH, AL			; SAVE
  3105 000037F8 B00B                <1> 	MOV	AL, CMOS_REG_B		; ADDRESS ALARM REGISTER
  3106 000037FA E81DD3FFFF          <1> 	CALL	CMOS_READ		; READ CURRENT VALUE OF DSE BIT
  3107 000037FF 2401                <1> 	AND	AL, 00000001B		; MASK FOR VALID DSE BIT
  3108 00003801 88C2                <1> 	MOV	DL, AL			; SET [DL] TO ZERO FOR NO DSE BIT
  3109 00003803 B002                <1> 	MOV	AL, CMOS_MINUTES 	; SET ADDRESS OF MINUTES
  3110 00003805 E812D3FFFF          <1> 	CALL	CMOS_READ		; GET MINUTES
  3111 0000380A 88C1                <1> 	MOV	CL, AL			; SAVE
  3112 0000380C B004                <1> 	MOV	AL, CMOS_HOURS		; SET ADDRESS OF HOURS
  3113 0000380E E809D3FFFF          <1> 	CALL	CMOS_READ		; GET HOURS
  3114 00003813 88C5                <1> 	MOV	CH, AL			; SAVE
  3115 00003815 F8                  <1> 	CLC				; SET CY= 0
  3116                              <1> RTC_29:
  3117 00003816 FB                  <1> 	sti
  3118 00003817 C3                  <1> 	RETn				; RETURN WITH RESULT IN CARRY FLAG
  3119                              <1> 
  3120                              <1> get_rtc_date:
  3121                              <1> 	; 15/03/2015
  3122                              <1> 	; Derived from IBM PC-XT Model 286 BIOS Source Code
  3123                              <1> 	; BIOS2.ASM ---- 10/06/1985 BIOS INTERRUPT ROUTINES
  3124                              <1> 	; INT 1Ah
  3125                              <1> 	; (AH) = 04H  READ THE DATE FROM THE REAL TIME CLOCK AND RETURN WITH,:
  3126                              <1> 	;      (CH) = CENTURY IN BCD (19 OR 20) 		       :
  3127                              <1> 	;      (CL) = YEAR IN BCD (00-99)			       :
  3128                              <1> 	;      (DH) = MONTH IN BCD (01-12)			       :
  3129                              <1> 	;      (DL) = DAY IN BCD (01-31).		
  3130                              <1> 	;
  3131                              <1> RTC_40: 				; GET RTC DATE
  3132 00003818 FA                  <1> 	cli
  3133 00003819 E814D3FFFF          <1> 	CALL	UPD_IPR 		; CHECK FOR UPDATE IN PROCESS
  3134 0000381E 7225                <1> 	JC	short RTC_49		; EXIT IF ERROR (CY= 1)
  3135                              <1> 
  3136 00003820 B007                <1> 	MOV	AL, CMOS_DAY_MONTH	; ADDRESS DAY OF MONTH
  3137 00003822 E8F5D2FFFF          <1> 	CALL	CMOS_READ		; READ DAY OF MONTH
  3138 00003827 88C2                <1> 	MOV	DL, AL			; SAVE
  3139 00003829 B008                <1> 	MOV	AL, CMOS_MONTH		; ADDRESS MONTH
  3140 0000382B E8ECD2FFFF          <1> 	CALL	CMOS_READ		; READ MONTH
  3141 00003830 88C6                <1> 	MOV	DH, AL			; SAVE
  3142 00003832 B009                <1> 	MOV	AL, CMOS_YEAR		; ADDRESS YEAR
  3143 00003834 E8E3D2FFFF          <1> 	CALL	CMOS_READ		; READ YEAR
  3144 00003839 88C1                <1> 	MOV	CL, AL			; SAVE
  3145 0000383B B032                <1> 	MOV	AL, CMOS_CENTURY 	; ADDRESS CENTURY LOCATION
  3146 0000383D E8DAD2FFFF          <1> 	CALL	CMOS_READ		; GET CENTURY BYTE
  3147 00003842 88C5                <1> 	MOV	CH, AL			; SAVE
  3148 00003844 F8                  <1> 	CLC				; SET CY=0
  3149                              <1> RTC_49:
  3150 00003845 FB                  <1> 	sti
  3151 00003846 C3                  <1> 	RETn				; RETURN WITH RESULTS IN CARRY FLAG
  3152                              <1> 
  3153                              <1> set_date_time:
  3154                              <1> convert_from_epoch:
  3155                              <1> 	; 15/03/2015 (Retro UNIX 386 v1 - 32 bit version)
  3156                              <1> 	; 20/06/2013 (Retro UNIX 8086 v1)
  3157                              <1> 	; 'convert_from_epoch' procedure prototype: 
  3158                              <1> 	; 	            UNIXCOPY.ASM, 10/03/2013
  3159                              <1> 	;
  3160                              <1> 	; ((Modified registers: EAX, EDX, ECX, EBX))	
  3161                              <1> 	;
  3162                              <1> 	; Derived from DALLAS Semiconductor
  3163                              <1> 	; Application Note 31 (DS1602/DS1603)
  3164                              <1> 	; 6 May 1998
  3165                              <1> 	;
  3166                              <1> 	; INPUT:
  3167                              <1> 	; EAX = Unix (Epoch) Time
  3168                              <1> 	;
  3169 00003847 31D2                <1> 	xor 	edx, edx
  3170 00003849 B93C000000          <1> 	mov 	ecx, 60
  3171 0000384E F7F1                <1> 	div	ecx
  3172                              <1> 	;mov 	[imin], eax   ; whole minutes
  3173                              <1> 			  ; since 1/1/1970
  3174 00003850 668915[2A6D0000]    <1> 	mov 	[second], dx  ; leftover seconds
  3175 00003857 29D2                <1> 	sub 	edx, edx
  3176 00003859 F7F1                <1> 	div	ecx
  3177                              <1> 	;mov 	[ihrs], eax   ; whole hours
  3178                              <1> 	;		      ; since 1/1/1970
  3179 0000385B 668915[286D0000]    <1> 	mov 	[minute], dx  ; leftover minutes
  3180 00003862 31D2                <1> 	xor	edx, edx
  3181                              <1> 	;mov 	cx, 24
  3182 00003864 B118                <1> 	mov 	cl, 24
  3183 00003866 F7F1                <1> 	div	ecx
  3184                              <1> 	;mov 	[iday], ax   ; whole days
  3185                              <1> 			     ; since 1/1/1970
  3186 00003868 668915[266D0000]    <1> 	mov 	[hour], dx   ; leftover hours
  3187 0000386F 05DB020000          <1> 	add 	eax, 365+366 ; whole day since
  3188                              <1> 			     ; 1/1/1968 	
  3189                              <1> 	;mov 	[iday], ax
  3190 00003874 50                  <1> 	push 	eax
  3191 00003875 29D2                <1> 	sub	edx, edx
  3192 00003877 B9B5050000          <1> 	mov 	ecx, (4*365)+1 ; 4 years = 1461 days
  3193 0000387C F7F1                <1> 	div	ecx
  3194 0000387E 59                  <1> 	pop 	ecx
  3195                              <1> 	;mov 	[lday], ax   ; count of quadyrs (4 years)
  3196 0000387F 6652                <1> 	push 	dx
  3197                              <1> 	;mov 	[qday], dx   ; days since quadyr began
  3198 00003881 6683FA3C            <1> 	cmp 	dx, 31 + 29  ; if past feb 29 then
  3199 00003885 F5                  <1> 	cmc		     ; add this quadyr's leap day
  3200 00003886 83D000              <1> 	adc 	eax, 0	     ; to # of qadyrs (leap days)
  3201                              <1> 	;mov 	[lday], ax   ; since 1968			  
  3202                              <1> 	;mov 	cx, [iday]
  3203 00003889 91                  <1> 	xchg 	ecx, eax     ; ECX = lday, EAX = iday		  
  3204 0000388A 29C8                <1> 	sub 	eax, ecx     ; iday - lday
  3205 0000388C B96D010000          <1> 	mov 	ecx, 365
  3206 00003891 31D2                <1> 	xor	edx, edx
  3207                              <1> 	; EAX = iday-lday, EDX = 0
  3208 00003893 F7F1                <1> 	div	ecx
  3209                              <1> 	;mov 	[iyrs], ax   ; whole years since 1968
  3210                              <1> 	;jday = iday - (iyrs*365) - lday
  3211                              <1> 	;mov [jday], dx      ; days since 1/1 of current year
  3212                              <1> 	;add	eax, 1968
  3213 00003895 6605B007            <1> 	add 	ax, 1968     ; compute year
  3214 00003899 66A3[206D0000]      <1> 	mov 	[year], ax
  3215 0000389F 6689D1              <1> 	mov 	cx, dx
  3216                              <1> 	;mov 	dx, [qday]
  3217 000038A2 665A                <1> 	pop 	dx
  3218 000038A4 6681FA6D01          <1> 	cmp 	dx, 365	     ; if qday <= 365 and qday >= 60	
  3219 000038A9 7709                <1> 	ja 	short cfe1   ; jday = jday +1
  3220 000038AB 6683FA3C            <1> 	cmp 	dx, 60       ; if past 2/29 and leap year then
  3221 000038AF F5                  <1>         cmc		     ; add a leap day to the # of whole
  3222 000038B0 6683D100            <1> 	adc 	cx, 0        ; days since 1/1 of current year
  3223                              <1> cfe1:			
  3224                              <1> 	;mov 	[jday], cx
  3225 000038B4 66BB0C00            <1> 	mov 	bx, 12       ; estimate month
  3226 000038B8 66BA6E01            <1> 	mov 	dx, 366      ; mday, max. days since 1/1 is 365
  3227 000038BC 6683E003            <1> 	and 	ax, 11b      ; year mod 4 (and dx, 3) 
  3228                              <1> cfe2:	; Month calculation  ; 0 to 11  (11 to 0)	
  3229 000038C0 6639D1              <1> 	cmp 	cx, dx       ; mday = # of days passed from 1/1
  3230 000038C3 731D                <1> 	jnb 	short cfe3
  3231 000038C5 664B                <1> 	dec 	bx           ; month = month - 1
  3232 000038C7 66D1E3              <1> 	shl 	bx, 1 
  3233 000038CA 668B93[2C6D0000]    <1> 	mov 	dx, [EBX+DMonth] ; # elapsed days at 1st of month
  3234 000038D1 66D1EB              <1> 	shr 	bx, 1        ; bx = month - 1 (0 to 11)
  3235 000038D4 6683FB01            <1> 	cmp	bx, 1        ; if month > 2 and year mod 4  = 0	
  3236 000038D8 76E6                <1> 	jna 	short cfe2   ; then mday = mday + 1
  3237 000038DA 08C0                <1> 	or 	al, al       ; if past 2/29 and leap year then
  3238 000038DC 75E2                <1> 	jnz 	short cfe2   ; add leap day (to mday)
  3239 000038DE 6642                <1> 	inc 	dx           ; mday = mday + 1
  3240 000038E0 EBDE                <1> 	jmp 	short cfe2
  3241                              <1> cfe3:
  3242 000038E2 6643                <1> 	inc 	bx	     ; -> bx = month, 1 to 12
  3243 000038E4 66891D[226D0000]    <1> 	mov 	[month], bx
  3244 000038EB 6629D1              <1> 	sub 	cx, dx	     ; day = jday - mday + 1	
  3245 000038EE 6641                <1> 	inc 	cx 			  
  3246 000038F0 66890D[246D0000]    <1> 	mov 	[day], cx
  3247                              <1> 	
  3248                              <1> 	; eax, ebx, ecx, edx is changed at return
  3249                              <1> 	; output ->
  3250                              <1> 	; [year], [month], [day], [hour], [minute], [second]
  3251                              <1> 	
  3252                              <1> 	; 15/03/2015 (Retro UNIX 386 v1 - 32 bit version)
  3253                              <1> 	; 20/06/2013 (Retro UNIX 8086 v1)
  3254                              <1> set_date:
  3255 000038F7 A0[216D0000]        <1>         mov     al, [year+1]
  3256 000038FC D40A                <1> 	aam 	; ah = al / 10, al = al mod 10
  3257 000038FE D510                <1> 	db 	0D5h, 10h    ; Undocumented inst. AAD
  3258                              <1> 			     ; AL = AH * 10h + AL
  3259 00003900 88C5                <1> 	mov 	ch, al ; century (BCD)
  3260 00003902 A0[206D0000]        <1> 	mov 	al, [year]
  3261 00003907 D40A                <1> 	aam 	; ah = al / 10, al = al mod 10
  3262 00003909 D510                <1> 	db 	0D5h, 10h    ; Undocumented inst. AAD
  3263                              <1> 			     ; AL = AH * 10h + AL
  3264 0000390B 88C1                <1> 	mov 	cl, al ; year (BCD)
  3265 0000390D A0[226D0000]        <1>         mov 	al, [month]
  3266 00003912 D40A                <1> 	aam 	; ah = al / 10, al = al mod 10
  3267 00003914 D510                <1> 	db 	0D5h, 10h    ; Undocumented inst. AAD
  3268                              <1> 			     ; AL = AH * 10h + AL
  3269 00003916 88C6                <1> 	mov 	dh, al ; month (BCD)
  3270 00003918 A0[246D0000]        <1> 	mov 	al, [day]
  3271 0000391D D40A                <1> 	aam 	; ah = al / 10, al = al mod 10
  3272 0000391F D510                <1> 	db 	0D5h, 10h    ; Undocumented inst. AAD
  3273                              <1> 			     ; AL = AH * 10h + AL
  3274 00003921 88C6                <1> 	mov 	dh, al ; day (BCD)
  3275                              <1> 	; Set real-time clock date
  3276 00003923 E879000000          <1> 	call	set_rtc_date
  3277                              <1> set_time:
  3278                              <1>         ; Read real-time clock time 
  3279                              <1> 	; (get day light saving time bit status)
  3280 00003928 FA                  <1>  	cli
  3281 00003929 E804D2FFFF          <1> 	CALL	UPD_IPR 	; CHECK FOR UPDATE IN PROCESS
  3282                              <1> 	; cf = 1 -> al = 0
  3283 0000392E 7207                <1>         jc      short stime1
  3284 00003930 B00B                <1> 	MOV	AL, CMOS_REG_B	; ADDRESS ALARM REGISTER
  3285 00003932 E8E5D1FFFF          <1> 	CALL	CMOS_READ	; READ CURRENT VALUE OF DSE BIT
  3286                              <1> stime1:
  3287 00003937 FB                  <1> 	sti
  3288 00003938 2401                <1> 	AND	AL, 00000001B	; MASK FOR VALID DSE BIT
  3289 0000393A 88C2                <1> 	MOV	DL, AL		; SET [DL] TO ZERO FOR NO DSE BIT
  3290                              <1> 	; DL = 1 or 0 (day light saving time)
  3291                              <1> 	;	
  3292 0000393C A0[266D0000]        <1> 	mov 	al, [hour]
  3293 00003941 D40A                <1> 	aam 	; ah = al / 10, al = al mod 10
  3294 00003943 D510                <1> 	db 	0D5h,10h     ; Undocumented inst. AAD
  3295                              <1> 			     ; AL = AH * 10h + AL
  3296 00003945 88C5                <1> 	mov 	ch, al ; hour (BCD)
  3297 00003947 A0[286D0000]        <1>         mov     al, [minute]
  3298 0000394C D40A                <1> 	aam 	; ah = al / 10, al = al mod 10
  3299 0000394E D510                <1> 	db 	0D5h,10h     ; Undocumented inst. AAD
  3300                              <1> 			     ; AL = AH * 10h + AL
  3301 00003950 88C1                <1> 	mov 	cl, al       ; minute (BCD)
  3302 00003952 A0[2A6D0000]        <1>         mov     al, [second]
  3303 00003957 D40A                <1> 	aam 	; ah = al / 10, al = al mod 10
  3304 00003959 D510                <1> 	db 	0D5h,10h     ; Undocumented inst. AAD
  3305                              <1> 			     ; AL = AH * 10h + AL
  3306 0000395B 88C6                <1> 	mov 	dh, al	     ; second (BCD)
  3307                              <1> 	; Set real-time clock time
  3308                              <1>  	; call	set_rtc_time
  3309                              <1> set_rtc_time:
  3310                              <1> 	; 15/04/2015 (257, POSTEQU.INC -> H EQU 256, X EQU H+1)
  3311                              <1> 	; 15/03/2015
  3312                              <1> 	; Derived from IBM PC-XT Model 286 BIOS Source Code
  3313                              <1> 	; BIOS2.ASM ---- 10/06/1985 BIOS INTERRUPT ROUTINES
  3314                              <1> 	; INT 1Ah
  3315                              <1> 	; (AH) = 03H  SET THE REAL TIME CLOCK USING,			:
  3316                              <1> 	;      (CH) = HOURS IN BCD (00-23)			       	:
  3317                              <1> 	;      (CL) = MINUTES IN BCD (00-59)			       	:
  3318                              <1> 	;      (DH) = SECONDS IN BCD (00-59)			       	:
  3319                              <1> 	;      (DL) = 01 IF DAYLIGHT SAVINGS ENABLE OPTION, ELSE 00.    :
  3320                              <1> 	;								:
  3321                              <1> 	;  NOTE: (DL)= 00 IF DAYLIGHT SAVINGS TIME ENABLE IS NOT ENABLED. :
  3322                              <1> 	;        (DL)= 01 ENABLES TWO SPECIAL UPDATES THE LAST SUNDAY IN  :
  3323                              <1> 	;         APRIL   (1:59:59 --> 3:00:00 AM) AND THE LAST SUNDAY IN :
  3324                              <1> 	;         OCTOBER (1:59:59 --> 1:00:00 AM) THE FIRST TIME.	  :
  3325                              <1> 	;
  3326                              <1> RTC_30: 				; SET RTC TIME
  3327 0000395D FA                  <1> 	cli
  3328 0000395E E8CFD1FFFF          <1> 	CALL	UPD_IPR 		; CHECK FOR UPDATE IN PROCESS
  3329 00003963 7305                <1> 	JNC	short RTC_35		; GO AROUND IF CLOCK OPERATING
  3330 00003965 E886000000          <1> 	CALL	RTC_STA 		; ELSE TRY INITIALIZING CLOCK
  3331                              <1> RTC_35:
  3332 0000396A 88F4                <1> 	MOV	AH, DH			; GET TIME BYTE - SECONDS
  3333 0000396C B000                <1> 	MOV	AL, CMOS_SECONDS 	; ADDRESS SECONDS
  3334 0000396E E89E000000          <1> 	CALL	CMOS_WRITE		; UPDATE SECONDS
  3335 00003973 88CC                <1> 	MOV	AH, CL			; GET TIME BYTE - MINUTES
  3336 00003975 B002                <1> 	MOV	AL, CMOS_MINUTES 	; ADDRESS MINUTES
  3337 00003977 E895000000          <1> 	CALL	CMOS_WRITE		; UPDATE MINUTES
  3338 0000397C 88EC                <1> 	MOV	AH, CH			; GET TIME BYTE - HOURS
  3339 0000397E B004                <1> 	MOV	AL, CMOS_HOURS		; ADDRESS HOURS
  3340 00003980 E88C000000          <1> 	CALL	CMOS_WRITE		; UPDATE ADDRESS
  3341                              <1> 	;MOV	AX, X*CMOS_REG_B 	; ADDRESS ALARM REGISTER
  3342 00003985 66B80B0B            <1> 	MOV	AX, 257*CMOS_REG_B 	; 
  3343 00003989 E88ED1FFFF          <1> 	CALL	CMOS_READ		; READ CURRENT TIME
  3344 0000398E 2462                <1> 	AND	AL, 01100010B		; MASK FOR VALID BIT POSITIONS
  3345 00003990 0C02                <1> 	OR	AL, 00000010B		; TURN ON 24 HOUR MODE
  3346 00003992 80E201              <1> 	AND	DL, 00000001B		; USE ONLY THE DSE BIT
  3347 00003995 08D0                <1> 	OR	AL, DL			; GET DAY LIGHT SAVINGS TIME BIT (OSE)
  3348 00003997 86E0                <1> 	XCHG	AH, AL			; PLACE IN WORK REGISTER AND GET ADDRESS
  3349 00003999 E873000000          <1> 	CALL	CMOS_WRITE		; SET NEW ALARM BITS
  3350 0000399E F8                  <1> 	CLC				; SET CY= 0
  3351 0000399F FB                  <1> 	sti
  3352 000039A0 C3                  <1> 	RETn				; RETURN WITH CY= 0
  3353                              <1> 
  3354                              <1> set_rtc_date:
  3355                              <1> 	; 15/04/2015 (257, POSTEQU.INC -> H EQU 256, X EQU H+1)
  3356                              <1> 	; 15/03/2015
  3357                              <1> 	; Derived from IBM PC-XT Model 286 BIOS Source Code
  3358                              <1> 	; BIOS2.ASM ---- 10/06/1985 BIOS INTERRUPT ROUTINES
  3359                              <1> 	; INT 1Ah
  3360                              <1> 	; (AH) = 05H  SET THE DATE INTO THE REAL TIME CLOCK USING, :
  3361                              <1> 	;     (CH) = CENTURY IN BCD (19 OR 20)			   :
  3362                              <1> 	;     (CL) = YEAR IN BCD (00-99)			   :
  3363                              <1> 	;     (DH) = MONTH IN BCD (01-12)			   :
  3364                              <1> 	;     (DL) = DAY IN BCD (01-31).
  3365                              <1> 	;
  3366                              <1> RTC_50: 				; SET RTC DATE
  3367 000039A1 FA                  <1> 	cli
  3368 000039A2 E88BD1FFFF          <1> 	CALL	UPD_IPR 		; CHECK FOR UPDATE IN PROCESS
  3369 000039A7 7305                <1> 	JNC	short RTC_55		; GO AROUND IF NO ERROR
  3370 000039A9 E842000000          <1> 	CALL	RTC_STA 		; ELSE INITIALIZE CLOCK
  3371                              <1> RTC_55:
  3372 000039AE 66B80600            <1> 	MOV	AX, CMOS_DAY_WEEK	; ADDRESS OF DAY OF WEEK BYTE
  3373 000039B2 E85A000000          <1> 	CALL	CMOS_WRITE		; LOAD ZEROS TO DAY OF WEEK
  3374 000039B7 88D4                <1> 	MOV	AH, DL			; GET DAY OF MONTH BYTE
  3375 000039B9 B007                <1> 	MOV	AL, CMOS_DAY_MONTH	; ADDRESS DAY OF MONTH BYTE
  3376 000039BB E851000000          <1> 	CALL	CMOS_WRITE		; WRITE OF DAY OF MONTH REGISTER
  3377 000039C0 88F4                <1> 	MOV	AH, DH			; GET MONTH
  3378 000039C2 B008                <1> 	MOV	AL, CMOS_MONTH		; ADDRESS MONTH BYTE
  3379 000039C4 E848000000          <1> 	CALL	CMOS_WRITE		; WRITE MONTH REGISTER
  3380 000039C9 88CC                <1> 	MOV	AH, CL			; GET YEAR BYTE
  3381 000039CB B009                <1> 	MOV	AL, CMOS_YEAR		; ADDRESS YEAR REGISTER
  3382 000039CD E83F000000          <1> 	CALL	CMOS_WRITE		; WRITE YEAR REGISTER
  3383 000039D2 88EC                <1> 	MOV	AH, CH			; GET CENTURY BYTE
  3384 000039D4 B032                <1> 	MOV	AL, CMOS_CENTURY 	; ADDRESS CENTURY BYTE
  3385 000039D6 E836000000          <1> 	CALL	CMOS_WRITE		; WRITE CENTURY LOCATION
  3386                              <1> 	;MOV	AX, X*CMOS_REG_B 	; ADDRESS ALARM REGISTER
  3387 000039DB 66B80B0B            <1> 	MOV	AX, 257*CMOS_REG_B 	; 
  3388 000039DF E838D1FFFF          <1> 	CALL	CMOS_READ		; READ CURRENT SETTINGS
  3389 000039E4 247F                <1> 	AND	AL, 07FH 		; CLEAR 'SET BIT'
  3390 000039E6 86E0                <1> 	XCHG	AH, AL			; MOVE TO WORK REGISTER
  3391 000039E8 E824000000          <1> 	CALL	CMOS_WRITE		; AND START CLOCK UPDATING
  3392 000039ED F8                  <1> 	CLC				; SET CY= 0
  3393 000039EE FB                  <1> 	sti
  3394 000039EF C3                  <1> 	RETn				; RETURN CY=0
  3395                              <1> 
  3396                              <1> 	; 15/03/2015
  3397                              <1> RTC_STA:				; INITIALIZE REAL TIME CLOCK
  3398 000039F0 B426                <1> 	mov	ah, 26h
  3399 000039F2 B00A                <1> 	mov	al, CMOS_REG_A		; ADDRESS REGISTER A AND LOAD DATA MASK
  3400 000039F4 E818000000          <1> 	CALL	CMOS_WRITE		; INITIALIZE STATUS REGISTER A
  3401 000039F9 B482                <1> 	mov	ah, 82h
  3402 000039FB B00B                <1> 	mov 	al, CMOS_REG_B		; SET "SET BIT" FOR CLOCK INITIALIZATION
  3403 000039FD E80F000000          <1> 	CALL	CMOS_WRITE		; AND 24 HOUR MODE TO REGISTER B
  3404 00003A02 B00C                <1> 	MOV	AL, CMOS_REG_C		; ADDRESS REGISTER C
  3405 00003A04 E813D1FFFF          <1> 	CALL	CMOS_READ		; READ REGISTER C TO INITIALIZE
  3406 00003A09 B00D                <1> 	MOV	AL, CMOS_REG_D		; ADDRESS REGISTER D
  3407 00003A0B E80CD1FFFF          <1> 	CALL	CMOS_READ		; READ REGISTER D TO INITIALIZE
  3408 00003A10 C3                  <1> 	RETn
  3409                              <1> 
  3410                              <1> 	; 15/03/2015
  3411                              <1> 	; IBM PC/XT Model 286 BIOS source code ----- 10/06/85 (test4.asm)
  3412                              <1> CMOS_WRITE:			; WRITE (AH) TO LOCATION (AL)
  3413 00003A11 9C                  <1> 	pushf			; SAVE INTERRUPT ENABLE STATUS AND FLAGS
  3414                              <1> 	;push	ax		; SAVE WORK REGISTER VALUES
  3415 00003A12 D0C0                <1> 	rol	al, 1		; MOVE NMI BIT TO LOW POSITION
  3416 00003A14 F9                  <1> 	stc			; FORCE NMI BIT ON IN CARRY FLAG
  3417 00003A15 D0D8                <1> 	rcr	al, 1		; HIGH BIT ON TO DISABLE NMI - OLD IN CY
  3418 00003A17 FA                  <1> 	cli			; DISABLE INTERRUPTS
  3419 00003A18 E670                <1> 	out	CMOS_PORT, al	; ADDRESS LOCATION AND DISABLE NMI
  3420 00003A1A 88E0                <1> 	mov	al, ah		; GET THE DATA BYTE TO WRITE
  3421 00003A1C E671                <1> 	out	CMOS_DATA, al	; PLACE IN REQUESTED CMOS LOCATION
  3422 00003A1E B01E                <1> 	mov	al, CMOS_SHUT_DOWN*2 ; GET ADDRESS OF DEFAULT LOCATION
  3423 00003A20 D0D8                <1> 	rcr	al, 1		; PUT ORIGINAL NMI MASK BIT INTO ADDRESS
  3424 00003A22 E670                <1> 	out	CMOS_PORT, al	; SET DEFAULT TO READ ONLY REGISTER
  3425 00003A24 90                  <1> 	nop			; I/O DELAY
  3426 00003A25 E471                <1> 	in	al, CMOS_DATA	; OPEN STANDBY LATCH
  3427                              <1> 	;pop	ax		; RESTORE WORK REGISTERS
  3428 00003A27 9D                  <1> 	popf	
  3429 00003A28 C3                  <1> 	RETn
  3430                              <1> 
  3431                              <1> bf_init:
  3432                              <1> 	; 28/11/2021
  3433                              <1> 	; 14/08/2015
  3434                              <1> 	; 02/07/2015
  3435                              <1> 	; 01/07/2015
  3436                              <1> 	; 15/04/2015 (Retro UNIX 386 v1 - Beginning)
  3437                              <1> 	; Buffer (pointer) initialization !
  3438                              <1> 	; 
  3439                              <1> 	; 17/07/2013 - 24/07/2013
  3440                              <1> 	; Retro UNIX 8086 v1 (U9.ASM)
  3441                              <1> 	; (Retro UNIX 8086 v1 feature only !)
  3442                              <1> 	;
  3443 00003A29 BF[94740000]        <1> 	mov	edi, bufp 
  3444 00003A2E B8[BC890000]        <1> 	mov	eax, buffer + (nbuf*520) 
  3445 00003A33 29D2                <1> 	sub	edx, edx
  3446 00003A35 FECA                <1> 	dec	dl
  3447 00003A37 31C9                <1> 	xor	ecx, ecx
  3448 00003A39 49                  <1> 	dec	ecx
  3449                              <1> bi0:
  3450 00003A3A 2D08020000          <1> 	sub	eax, 520 ; 8 header + 512 data
  3451 00003A3F AB                  <1> 	stosd
  3452 00003A40 89C6                <1> 	mov	esi, eax
  3453 00003A42 8916                <1> 	mov	[esi], edx ; 000000FFh
  3454                              <1> 			    ; Not a valid device sign
  3455 00003A44 894E04              <1> 	mov	[esi+4], ecx ; 0FFFFFFFFh
  3456                              <1> 		      ; Not a valid block number sign 	 	
  3457 00003A47 3D[8C7D0000]        <1> 	cmp	eax, buffer
  3458 00003A4C 77EC                <1> 	ja	short bi0
  3459 00003A4E B8[7C750000]        <1> 	mov	eax, sb0
  3460 00003A53 AB                  <1> 	stosd
  3461 00003A54 B8[84770000]        <1> 	mov	eax, sb1
  3462 00003A59 AB                  <1> 	stosd
  3463 00003A5A 89C6                <1> 	mov	esi, eax ; offset sb1
  3464 00003A5C 8916                <1> 	mov	[esi], edx ; 000000FFh
  3465                              <1> 			    ; Not a valid device sign
  3466 00003A5E 894E04              <1> 	mov	[esi+4], ecx ; 0FFFFFFFFh
  3467                              <1> 		      ; Not a valid block number sign 	 
  3468                              <1> 	; 14/08/2015
  3469                              <1> 	;call 	rdev_init
  3470                              <1> 	;retn
  3471                              <1> 
  3472                              <1> 	; 28/11/2021
  3473                              <1> rdev_init: ; root device, super block buffer initialization
  3474                              <1> 	; 14/08/2015
  3475                              <1> 	; Retro UNIX 386 v1 feature only !
  3476                              <1> 	;
  3477                              <1> 	; NOTE: Disk partitions (file systems), logical
  3478                              <1> 	; drive initialization, partition's start sector etc.
  3479                              <1> 	; will be coded here, later in 'ldrv_init'	
  3480                              <1> 
  3481 00003A61 0FB605[AA6A0000]    <1> 	movzx	eax, byte [boot_drv]
  3482                              <1> rdi_0:
  3483 00003A68 3C80                <1> 	cmp	al, 80h
  3484 00003A6A 7202                <1> 	jb	short rdi_1
  3485 00003A6C 2C7E                <1> 	sub	al, 7Eh ; 80h = 2 (hd0), 81h = 3 (hd1)
  3486                              <1> rdi_1:
  3487                              <1> ; temporary - 05/12/2021
  3488 00003A6E 20C0                <1> and	al, al
  3489 00003A70 7515                <1> jnz	short rdi_2
  3490 00003A72 C605[D06A0000]12    <1> mov	byte [drv.spt], 18
  3491 00003A79 C605[C26A0000]02    <1> mov	byte [drv.heads], 2
  3492 00003A80 C605[B46A0000]50    <1> mov	byte [drv.cylinders], 80
  3493                              <1> rdi_2:
  3494 00003A87 A2[BA740000]        <1> 	mov	[rdev], al
  3495 00003A8C BB[7C750000]        <1>         mov	ebx, sb0 ; super block buffer
  3496 00003A91 8903                <1> 	mov 	[ebx], eax
  3497 00003A93 B001                <1> 	mov	al, 1 ; eax = 1
  3498 00003A95 894304              <1> 	mov	[ebx+4], eax ; super block address on disk
  3499                              <1> 	;call 	diskio
  3500                              <1> 	;retn
  3501                              <1> 	; 28/11/2021
  3502 00003A98 E972280000          <1> 	jmp	diskio
  3503                              <1> 
  3504                              <1> ; 23/10/2015
  3505                              <1> com1_irq4:
  3506 00003A9D [A53A0000]          <1> 	dd dummy_retn
  3507                              <1> com2_irq3:
  3508 00003AA1 [A53A0000]          <1> 	dd dummy_retn
  3509                              <1> 
  3510                              <1> dummy_retn:
  3511 00003AA5 C3                  <1> 	retn
  2070                                  %include 'u1.s'      ; 10/05/2015
  2071                              <1> ; Temporary Runix kernel v2.0 file for debug - 22/11/2021
  2072                              <1> ; (re-write kernel for test by using previous version without a major defect)
  2073                              <1> ; ****************************************************************************
  2074                              <1> ; Retro UNIX 386 v1 Kernel (v0.2) - SYS1.INC
  2075                              <1> ; Last Modification: 21/01/2022
  2076                              <1> ; ----------------------------------------------------------------------------
  2077                              <1> ; Derived from 'Retro UNIX 8086 v1' source code by Erdogan Tan
  2078                              <1> ; (v0.1 - Beginning: 11/07/2012)
  2079                              <1> ;
  2080                              <1> ; Derived from UNIX Operating System (v1.0 for PDP-11) 
  2081                              <1> ; (Original) Source Code by Ken Thompson (1971-1972)
  2082                              <1> ; <Bell Laboratories (17/3/1972)>
  2083                              <1> ; <Preliminary Release of UNIX Implementation Document>
  2084                              <1> ;
  2085                              <1> ; Retro UNIX 8086 v1 - U1.ASM (12/07/2014) //// UNIX v1 -> u1.s
  2086                              <1> ;
  2087                              <1> ; ****************************************************************************
  2088                              <1> 
  2089                              <1> unkni: ; / used for all system calls
  2090                              <1> sysent: ; < enter to system call >
  2091                              <1> 	; 25/12/2021 (Retro UNIX 386 v1.2)
  2092                              <1> 	; 19/10/2015
  2093                              <1> 	; 21/09/2015
  2094                              <1> 	; 01/07/2015
  2095                              <1> 	; 19/05/2015
  2096                              <1> 	; 16/04/2015 (Retro UNIX 386 v1 - Beginning)
  2097                              <1> 	; 10/04/2013 - 18/01/2014 (Retro UNIX 8086 v1)
  2098                              <1> 	;
  2099                              <1> 	; 'unkni' or 'sysent' is sytem entry from various traps. 
  2100                              <1> 	; The trap type is determined and an indirect jump is made to 
  2101                              <1> 	; the appropriate system call handler. If there is a trap inside
  2102                              <1> 	; the system a jump to panic is made. All user registers are saved 
  2103                              <1> 	; and u.sp points to the end of the users stack. The sys (trap)
  2104                              <1> 	; instructor is decoded to get the the system code part (see
  2105                              <1> 	; trap instruction in the PDP-11 handbook) and from this 
  2106                              <1> 	; the indirect jump address is calculated. If a bad system call is
  2107                              <1> 	; made, i.e., the limits of the jump table are exceeded, 'badsys'
  2108                              <1> 	; is called. If the call is legitimate control passes to the
  2109                              <1> 	; appropriate system routine.
  2110                              <1> 	;
  2111                              <1> 	; Calling sequence:
  2112                              <1> 	;	Through a trap caused by any sys call outside the system.
  2113                              <1> 	; Arguments:
  2114                              <1> 	;	Arguments of particular system call.	
  2115                              <1> 	; ...............................................................
  2116                              <1> 	;	
  2117                              <1> 	; Retro UNIX 8086 v1 modification: 
  2118                              <1> 	;       System call number is in EAX register.
  2119                              <1> 	;
  2120                              <1> 	;       Other parameters are in EDX, EBX, ECX, ESI, EDI, EBP
  2121                              <1> 	;	registers depending of function details.
  2122                              <1>   	;
  2123                              <1> 	; 16/04/2015
  2124 00003AA6 368925[D0740000]    <1>         mov     [ss:u.sp], esp ; Kernel stack points to return address
  2125                              <1> 	; save user registers
  2126 00003AAD 1E                  <1> 	push	ds
  2127 00003AAE 06                  <1> 	push	es
  2128 00003AAF 0FA0                <1> 	push	fs
  2129 00003AB1 0FA8                <1> 	push	gs
  2130 00003AB3 60                  <1> 	pushad  ; eax, ecx, edx, ebx, esp -before pushad-, ebp, esi, edi
  2131                              <1> 	;
  2132                              <1> 	; ESPACE = esp - [ss:u.sp] ; 4*12 = 48 ; 17/09/2015
  2133                              <1> 	; 	(ESPACE is size of space in kernel stack 
  2134                              <1> 	;	for saving/restoring user registers.)
  2135                              <1> 	;
  2136 00003AB4 50                  <1> 	push	eax ; 01/07/2015
  2137 00003AB5 66B81000            <1> 	mov     ax, KDATA
  2138 00003AB9 8ED8                <1>         mov     ds, ax
  2139 00003ABB 8EC0                <1>         mov     es, ax
  2140 00003ABD 8EE0                <1>         mov     fs, ax
  2141 00003ABF 8EE8                <1>         mov     gs, ax
  2142 00003AC1 A1[B86F0000]        <1> 	mov	eax, [k_page_dir]
  2143 00003AC6 0F22D8              <1> 	mov	cr3, eax
  2144 00003AC9 58                  <1> 	pop	eax ; 01/07/2015
  2145                              <1> 	; 19/10/2015
  2146 00003ACA FC                  <1> 	cld
  2147                              <1> 	;
  2148 00003ACB FE05[CE740000]      <1> 	inc	byte [sysflg]
  2149                              <1> 		; incb sysflg / indicate a system routine is in progress
  2150 00003AD1 FB                  <1>         sti 	; 18/01/2014
  2151                              <1> 	;jnz     panic ; 24/05/2013
  2152                              <1> 	; 04/12/2021
  2153 00003AD2 7405                <1> 	jz	short _1
  2154 00003AD4 E969F9FFFF          <1> 	jmp	panic
  2155                              <1> 		; beq 1f
  2156                              <1> 		; jmp panic ; / called if trap inside system
  2157                              <1> ;1:
  2158                              <1> _1:	; 04/12/2021
  2159                              <1> 	; 16/04/2015
  2160 00003AD9 A3[D8740000]        <1> 	mov	[u.r0], eax
  2161 00003ADE 8925[D4740000]      <1> 	mov	[u.usp], esp ; kernel stack points to user's registers
  2162                              <1> 	;
  2163                              <1> 		; mov $s.syst+2,clockp
  2164                              <1> 		; mov r0,-(sp) / save user registers 
  2165                              <1> 		; mov sp,u.r0 / pointer to bottom of users stack 
  2166                              <1> 			   ; / in u.r0
  2167                              <1> 		; mov r1,-(sp)
  2168                              <1> 		; mov r2,-(sp)
  2169                              <1> 		; mov r3,-(sp)
  2170                              <1> 		; mov r4,-(sp)
  2171                              <1> 		; mov r5,-(sp)
  2172                              <1> 		; mov ac,-(sp) / "accumulator" register for extended
  2173                              <1> 		             ; / arithmetic unit
  2174                              <1> 		; mov mq,-(sp) / "multiplier quotient" register for the
  2175                              <1> 		             ; / extended arithmetic unit
  2176                              <1> 		; mov sc,-(sp) / "step count" register for the extended
  2177                              <1> 		             ; / arithmetic unit
  2178                              <1> 		; mov sp,u.sp / u.sp points to top of users stack
  2179                              <1> 		; mov 18.(sp),r0 / store pc in r0
  2180                              <1> 		; mov -(r0),r0 / sys inst in r0 10400xxx
  2181                              <1> 		; sub $sys,r0 / get xxx code
  2182 00003AE4 C1E002              <1> 	shl	eax, 2
  2183                              <1> 		; asl r0 / multiply by 2 to jump indirect in bytes
  2184 00003AE7 3DA0000000          <1> 	cmp	eax, end_of_syscalls - syscalls
  2185                              <1> 		; cmp r0,$2f-1f / limit of table (35) exceeded
  2186                              <1> 	;jnb	short badsys
  2187                              <1> 		; bhis badsys / yes, bad system call
  2188                              <1> 	; 25/12/2021
  2189 00003AEC 7205                <1> 	jb	short _2
  2190 00003AEE E969010000          <1> 	jmp	badsys
  2191                              <1> _2:
  2192                              <1> 	; 25/12/2021
  2193                              <1> 	;cmc
  2194                              <1> 	;pushf	
  2195                              <1> 	;push	eax
  2196 00003AF3 8B2D[D0740000]      <1>  	mov 	ebp, [u.sp] ; Kernel stack at the beginning of sys call
  2197                              <1> 	;mov	al, 0FEh ; 11111110b
  2198                              <1> 	;;adc	al, 0 ; al = al + cf
  2199                              <1> 	;and	[ebp+8], al ; flags (reset carry flag)
  2200 00003AF9 806508FE            <1> 	and	byte [ebp+8], 0FEh ; 11111110b ; 25/12/2021
  2201                              <1> 		; bic $341,20.(sp) / set users processor priority to 0 
  2202                              <1> 				 ; / and clear carry bit
  2203                              <1> 	;pop	ebp ; eax
  2204 00003AFD 89C5                <1> 	mov	ebp, eax ; 25/12/2021
  2205                              <1> 	;popf
  2206                              <1>         ;;jc	badsys
  2207                              <1> 	;; 04/12/2021
  2208                              <1> 	;jnc	short _3
  2209                              <1> 	;jmp	badsys
  2210                              <1> ;_3:
  2211 00003AFF A1[D8740000]        <1> 	mov	eax, [u.r0]
  2212                              <1> 	; system call registers: EAX, EDX, ECX, EBX, ESI, EDI
  2213 00003B04 FFA5[0A3B0000]      <1> 	jmp	dword [ebp+syscalls]
  2214                              <1> 		; jmp *1f(r0) / jump indirect thru table of addresses
  2215                              <1> 		            ; / to proper system routine.
  2216                              <1> 
  2217                              <1> 	; 09/01/2022
  2218                              <1> 	; 01/01/2022 (/etc/init error, BugFix efforts, test)
  2219                              <1> syscalls: ; 1:
  2220                              <1> 	; 21/09/2015
  2221                              <1> 	; 01/07/2015
  2222                              <1> 	; 16/04/2015 (32 bit address modification) 
  2223 00003B0A [263C0000]          <1> 	dd sysrele	; / 0
  2224 00003B0E [CE3C0000]          <1> 	dd sysexit 	; / 1
  2225 00003B12 [F13D0000]          <1> 	dd sysfork 	; / 2
  2226 00003B16 [F73E0000]          <1> 	dd sysread 	; / 3
  2227 00003B1A [0C3F0000]          <1> 	dd syswrite 	; / 4
  2228 00003B1E [6F3F0000]          <1> 	dd sysopen 	; / 5
  2229 00003B22 [8D400000]          <1> 	dd sysclose 	; / 6
  2230 00003B26 [743D0000]          <1> 	dd syswait 	; / 7
  2231 00003B2A [07400000]          <1> 	dd syscreat 	; / 8
  2232 00003B2E [A0430000]          <1> 	dd syslink 	; / 9
  2233 00003B32 [2E440000]          <1> 	dd sysunlink 	; / 10
  2234 00003B36 [36450000]          <1> 	dd sysexec 	; / 11
  2235 00003B3A [594B0000]          <1> 	dd syschdir 	; / 12
  2236 00003B3E [7D4C0000]          <1> 	dd systime 	; / 13
  2237 00003B42 [5E400000]          <1> 	dd sysmkdir 	; / 14
  2238 00003B46 [B44B0000]          <1> 	dd syschmod 	; / 15
  2239 00003B4A [E34B0000]          <1> 	dd syschown 	; / 16
  2240 00003B4E [B14C0000]          <1> 	dd sysbreak 	; / 17
  2241 00003B52 [D8480000]          <1> 	dd sysstat 	; / 18
  2242 00003B56 [BE4D0000]          <1> 	dd sysseek 	; / 19
  2243 00003B5A [D04D0000]          <1> 	dd systell 	; / 20
  2244 00003B5E [CF5C0000]          <1> 	dd sysmount 	; / 21
  2245 00003B62 [7F5D0000]          <1> 	dd sysumount 	; / 22
  2246 00003B66 [434E0000]          <1> 	dd syssetuid 	; / 23
  2247 00003B6A [784E0000]          <1> 	dd sysgetuid 	; / 24
  2248 00003B6E [8C4C0000]          <1> 	dd sysstime 	; / 25
  2249 00003B72 [374E0000]          <1> 	dd sysquit 	; / 26
  2250 00003B76 [2B4E0000]          <1> 	dd sysintr 	; / 27
  2251 00003B7A [BE480000]          <1> 	dd sysfstat 	; / 28
  2252 00003B7E [A8400000]          <1> 	dd sysemt 	; / 29
  2253 00003B82 [D6400000]          <1> 	dd sysmdate 	; / 30
  2254 00003B86 [25410000]          <1> 	dd sysstty 	; / 31
  2255 00003B8A [93420000]          <1> 	dd sysgtty 	; / 32
  2256 00003B8E [D1400000]          <1> 	dd sysilgins 	; / 33
  2257 00003B92 [CD650000]          <1> 	dd syssleep 	; 34 ; Retro UNIX 8086 v1 feature only !
  2258                              <1> 			     ; 11/06/2014
  2259 00003B96 [FA650000]          <1> 	dd sysmsg	; 35 ; Retro UNIX 386 v1 feature only !
  2260                              <1> 			     ; 01/07/2015
  2261 00003B9A [D2660000]          <1> 	dd sysgeterr	; 36 ; Retro UNIX 386 v1 feature only !
  2262                              <1> 			     ; 21/09/2015 - get last error number
  2263                              <1> 	; 09/01/2022 - Retro UNIX 386 v1.2
  2264                              <1> 	; 27/03/2021 - Retro UNIX 386 v2
  2265 00003B9E [884E0000]          <1> 	dd syssetgid	; 37
  2266 00003BA2 [AB4E0000]          <1> 	dd sysgetgid	; 38
  2267                              <1> 	; 18/06/2021 - Retro UNIX 386 v2 (ref: TRDOS 386 v2)
  2268 00003BA6 [C34E0000]          <1> 	dd sysver	; 39 ; (get) Retro Unix 386 version
  2269                              <1> 
  2270                              <1> end_of_syscalls:
  2271                              <1> 
  2272                              <1> error:
  2273                              <1> 	; 11/12/2021 (Retro UNIX 386 v1.2)
  2274                              <1> 	; 17/09/2015
  2275                              <1> 	; 03/09/2015
  2276                              <1> 	; 01/09/2015
  2277                              <1> 	; 09/06/2015
  2278                              <1> 	; 13/05/2015
  2279                              <1> 	; 16/04/2015 (Retro UNIX 386 v1 - Beginning)
  2280                              <1> 	; 10/04/2013 - 07/08/2013 (Retro UNIX 8086 v1)
  2281                              <1> 	;
  2282                              <1> 	; 'error' merely sets the error bit off the processor status (c-bit)
  2283                              <1> 	; then falls right into the 'sysret', 'sysrele' return sequence.
  2284                              <1> 	;
  2285                              <1> 	; INPUTS -> none
  2286                              <1> 	; OUTPUTS ->
  2287                              <1> 	;	processor status - carry (c) bit is set (means error)
  2288                              <1> 	;
  2289                              <1> 	; 26/05/2013 (Stack pointer must be reset here! 
  2290                              <1> 	; 	      Because, jumps to error procedure
  2291                              <1> 	;	      disrupts push-pop nesting balance)
  2292                              <1> 	;
  2293 00003BAA 8B2D[D0740000]      <1> 	mov	ebp, [u.sp] ; interrupt (system call) return (iretd) address
  2294 00003BB0 804D0801            <1> 	or	byte [ebp+8], 1  ; set carry bit of flags register
  2295                              <1> 				 ; (system call will return with cf = 1)
  2296                              <1> 		; bis $1,20.(r1) / set c bit in processor status word below
  2297                              <1> 		               ; / users stack
  2298                              <1> 	; 17/09/2015
  2299 00003BB4 83ED30              <1> 	sub	ebp, ESPACE ; 48 ; total size of stack frame ('sysdefs.inc')
  2300                              <1> 				 ; for saving/restoring user registers	
  2301                              <1> 	;cmp	ebp, [u.usp]
  2302                              <1> 	;je	short err0	
  2303 00003BB7 892D[D4740000]      <1> 	mov	[u.usp], ebp
  2304                              <1> ;err0:
  2305                              <1> 	; 01/09/2015
  2306 00003BBD 8B25[D4740000]      <1> 	mov	esp, [u.usp]	; Retro Unix 8086 v1 modification!
  2307                              <1> 				; 10/04/2013
  2308                              <1> 				; (If an I/O error occurs during disk I/O,
  2309                              <1> 				; related procedures will jump to 'error'
  2310                              <1> 				; procedure directly without returning to 
  2311                              <1> 				; the caller procedure. So, stack pointer
  2312                              <1> 				; must be restored here.)
  2313                              <1> 	; 13/05/2015
  2314                              <1> 	; NOTE: (The last) error code is in 'u.error', it can be retrieved by
  2315                              <1> 	;	'get last error' system call later. 	
  2316                              <1> 
  2317                              <1> 	; 03/09/2015 - 09/06/2015 - 07/08/2013
  2318 00003BC3 C605[4A750000]00    <1> 	mov 	byte [u.kcall], 0 ; namei_r, mkdir_w reset
  2319                              <1> 
  2320                              <1> sysret: ; < return from system call>
  2321                              <1> 	; 11/12/2021
  2322                              <1> 	; 04/12/2021 (Retro UNIX 386 v1.2)
  2323                              <1> 	; 30/11/2021
  2324                              <1> 	; 10/09/2015
  2325                              <1> 	; 29/07/2015
  2326                              <1> 	; 25/06/2015
  2327                              <1> 	; 16/04/2015 (Retro UNIX 386 v1 - Beginning)
  2328                              <1> 	; 10/04/2013 - 23/02/2014 (Retro UNIX 8086 v1)
  2329                              <1> 	;
  2330                              <1> 	; 'sysret' first checks to see if process is about to be 
  2331                              <1> 	; terminated (u.bsys). If it is, 'sysexit' is called.
  2332                              <1> 	; If not, following happens:	 
  2333                              <1> 	; 	1) The user's stack pointer is restored.
  2334                              <1> 	;	2) r1=0 and 'iget' is called to see if last mentioned
  2335                              <1> 	;	   i-node has been modified. If it has, it is written out
  2336                              <1> 	;	   via 'ppoke'.
  2337                              <1> 	;	3) If the super block has been modified, it is written out
  2338                              <1> 	;	   via 'ppoke'.				
  2339                              <1> 	;	4) If the dismountable file system's super block has been
  2340                              <1> 	;	   modified, it is written out to the specified device
  2341                              <1> 	;	   via 'ppoke'.
  2342                              <1> 	;	5) A check is made if user's time quantum (uquant) ran out
  2343                              <1> 	;	   during his execution. If so, 'tswap' is called to give
  2344                              <1> 	;	   another user a chance to run.
  2345                              <1> 	;	6) 'sysret' now goes into 'sysrele'. 
  2346                              <1> 	;	    (See 'sysrele' for conclusion.)		
  2347                              <1> 	;
  2348                              <1> 	; Calling sequence:
  2349                              <1> 	;	jump table or 'br sysret'
  2350                              <1> 	; Arguments: 
  2351                              <1> 	;	-	
  2352                              <1> 	; ...............................................................
  2353                              <1> 	;	
  2354                              <1> 	; ((AX=r1 for 'iget' input))
  2355                              <1> 	;	
  2356                              <1> 	;xor	ax, ax ; 04/05/2013
  2357                              <1> 	; 30/11/2021
  2358 00003BCA 31C0                <1> 	xor	eax, eax
  2359                              <1> sysret0: ; 29/07/2015 (eax = 0, jump from sysexec)
  2360 00003BCC FEC0                <1> 	inc	al ; 04/05/2013
  2361 00003BCE 3805[28750000]      <1> 	cmp	[u.bsys], al ; 1
  2362                              <1> 		; tstb u.bsys / is a process about to be terminated because
  2363                              <1> 	;jnb	sysexit ; 04/05/2013
  2364                              <1> 	;	; bne sysexit / of an error? yes, go to sysexit
  2365                              <1> 	; 04/12/2021
  2366 00003BD4 720F                <1> 	jb	short _3
  2367                              <1> 	; 11/12/2021
  2368 00003BD6 C705[4C750000]0100- <1> 	mov	dword [u.error], ERR_INV_FUNC ; 1 ; 'invalid system call !'
  2368 00003BDE 0000                <1>
  2369 00003BE0 E9E9000000          <1> 	jmp	sysexit
  2370                              <1> _3:
  2371                              <1> 	;mov	esp, [u.usp] ; 24/05/2013 (that is not needed here)
  2372                              <1> 		; mov u.sp,sp / no point stack to users stack
  2373 00003BE5 FEC8                <1> 	dec 	al ; mov ax, 0
  2374                              <1> 		; clr r1 / zero r1 to check last mentioned i-node
  2375 00003BE7 E89B180000          <1> 	call	iget
  2376                              <1> 		; jsr r0,iget / if last mentioned i-node has been modified
  2377                              <1> 		            ; / it is written out
  2378                              <1> 	;xor 	ax, ax ; 0
  2379                              <1> 	; 30/11/2021
  2380 00003BEC 31C0                <1> 	xor	eax, eax
  2381 00003BEE 3805[CC740000]      <1> 	cmp	[smod], al ; 0
  2382                              <1> 		; tstb	smod / has the super block been modified
  2383 00003BF4 7614                <1> 	jna	short sysret1
  2384                              <1> 		; beq	1f / no, 1f
  2385 00003BF6 A2[CC740000]        <1> 	mov	[smod], al ; 0
  2386                              <1> 		; clrb smod / yes, clear smod
  2387 00003BFB BB[7C750000]        <1> 	mov	ebx, sb0 ;; 07/08//2013
  2388 00003C00 66810B0002          <1>    	or	word [ebx], 200h ;;
  2389                              <1> 	;or	word [sb0], 200h ; write bit, bit 9
  2390                              <1> 		; bis $1000,sb0 / set write bit in I/O queue for super block
  2391                              <1> 		      	      ; / output
  2392                              <1> 	; AX = 0
  2393 00003C05 E8E5250000          <1> 	call 	poke ; 07/08/2013
  2394                              <1> 	;call	ppoke
  2395                              <1> 	; AX = 0
  2396                              <1> 		; jsr r0,ppoke / write out modified super block to disk
  2397                              <1> sysret1: ;1:
  2398 00003C0A 3805[CD740000]      <1> 	cmp	[mmod], al ; 0
  2399                              <1> 		; tstb	mmod / has the super block for the dismountable file
  2400                              <1> 		           ; / system
  2401 00003C10 7614                <1> 	jna	short sysrel0
  2402                              <1> 		; beq 1f / been modified?  no, 1f
  2403 00003C12 A2[CD740000]        <1> 	mov	[mmod], al ; 0	
  2404                              <1> 		; clrb	mmod / yes, clear mmod
  2405                              <1>         ;mov    ax, [mntd]
  2406                              <1>         ;;mov   al, [mdev] ; 26/04/2013
  2407 00003C17 BB[84770000]        <1> 	mov	ebx, sb1 ;; 07/08//2013
  2408                              <1>         ;;mov	[ebx], al
  2409                              <1> 	;mov    [sb1], al
  2410                              <1> 		; movb	mntd,sb1 / set the I/O queue
  2411 00003C1C 66810B0002          <1> 	or	word [ebx], 200h
  2412                              <1> 	;or	word [sb1], 200h ; write bit, bit 9
  2413                              <1> 		; bis $1000,sb1 / set write bit in I/O queue for detached sb
  2414 00003C21 E8C9250000          <1> 	call	poke ; 07/08/2013
  2415                              <1> 	;call	ppoke 
  2416                              <1> 		; jsr r0,ppoke / write it out to its device
  2417                              <1>         ;xor    al, al ; 26/04/2013       
  2418                              <1> ;1:
  2419                              <1> 		; tstb uquant / is the time quantum 0?
  2420                              <1> 		; bne 1f / no, don't swap it out
  2421                              <1> 
  2422                              <1> sysrele: ; < release >
  2423                              <1> 	; 14/10/2015
  2424                              <1> 	; 01/09/2015
  2425                              <1> 	; 24/07/2015
  2426                              <1> 	; 14/05/2015
  2427                              <1> 	; 16/04/2015 (Retro UNIX 386 v1 - Beginning)
  2428                              <1> 	; 10/04/2013 - 07/03/2014 (Retro UNIX 8086 v1)
  2429                              <1> 	;
  2430                              <1> 	; 'sysrele' first calls 'tswap' if the time quantum for a user is
  2431                              <1> 	;  zero (see 'sysret'). It then restores the user's registers and
  2432                              <1> 	; turns off the system flag. It then checked to see if there is
  2433                              <1> 	; an interrupt from the user by calling 'isintr'. If there is, 
  2434                              <1> 	; the output gets flashed (see isintr) and interrupt action is
  2435                              <1> 	; taken by a branch to 'intract'. If there is no interrupt from
  2436                              <1> 	; the user, a rti is made.
  2437                              <1> 	;
  2438                              <1> 	; Calling sequence:
  2439                              <1> 	;	Fall through a 'bne' in 'sysret' & ?
  2440                              <1> 	; Arguments:
  2441                              <1> 	;	-	
  2442                              <1> 	; ...............................................................
  2443                              <1> 	;	
  2444                              <1> 	; 23/02/2014 (swapret)
  2445                              <1> 	; 22/09/2013
  2446                              <1> sysrel0: ;1:
  2447 00003C26 803D[20750000]00    <1> 	cmp	byte [u.quant], 0 ; 16/05/2013
  2448                              <1> 		; tstb uquant / is the time quantum 0?
  2449 00003C2D 7705                <1>         ja      short swapret
  2450                              <1> 		; bne 1f / no, don't swap it out
  2451                              <1> sysrelease: ; 07/12/2013 (jump from 'clock')
  2452 00003C2F E8D6120000          <1> 	call	tswap
  2453                              <1> 		; jsr r0,tswap / yes, swap it out
  2454                              <1> ;
  2455                              <1> ; Retro Unix 8086 v1 feature: return from 'swap' to 'swapret' address.
  2456                              <1> swapret: ;1:
  2457                              <1> 	; 10/09/2015
  2458                              <1> 	; 01/09/2015
  2459                              <1> 	; 14/05/2015
  2460                              <1> 	; 16/04/2015 (Retro UNIX 386 v1 - 32 bit, pm modifications)
  2461                              <1> 	; 26/05/2013 (Retro UNIX 8086 v1)
  2462                              <1> 	; cli
  2463                              <1> 	; 24/07/2015
  2464                              <1> 	;
  2465                              <1> 	;; 'esp' must be already equal to '[u.usp]' here ! 
  2466                              <1> 	;; mov	esp, [u.usp]
  2467                              <1> 
  2468                              <1> 	; 22/09/2013
  2469 00003C34 E8B6140000          <1> 	call	isintr
  2470                              <1> 	; 20/10/2013
  2471 00003C39 7405                <1> 	jz	short sysrel1
  2472 00003C3B E877000000          <1> 	call	intract
  2473                              <1> 		; jsr r0,isintr / is there an interrupt from the user
  2474                              <1> 		;     br intract / yes, output gets flushed, take interrupt
  2475                              <1> 		               ; / action
  2476                              <1> sysrel1:
  2477 00003C40 FA                  <1> 	cli ; 14/10/2015
  2478 00003C41 FE0D[CE740000]      <1> 	dec	byte [sysflg]
  2479                              <1> 		; decb sysflg / turn system flag off
  2480 00003C47 A1[38750000]        <1> 	mov     eax, [u.pgdir]
  2481 00003C4C 0F22D8              <1> 	mov	cr3, eax  ; 1st PDE points to Kernel Page Table 0 (1st 4 MB)
  2482                              <1> 			  ; (others are different than kernel page tables) 
  2483                              <1> 	; 10/09/2015
  2484 00003C4F 61                  <1> 	popad ; edi, esi, ebp, temp (increment esp by 4), ebx, edx, ecx, eax
  2485                              <1> 		; mov (sp)+,sc / restore user registers
  2486                              <1> 		; mov (sp)+,mq
  2487                              <1> 		; mov (sp)+,ac
  2488                              <1> 		; mov (sp)+,r5
  2489                              <1> 		; mov (sp)+,r4
  2490                              <1> 		; mov (sp)+,r3
  2491                              <1> 		; mov (sp)+,r2
  2492                              <1> 	;
  2493 00003C50 A1[D8740000]        <1> 	mov	eax, [u.r0]  ; ((return value in EAX))
  2494 00003C55 0FA9                <1> 	pop	gs
  2495 00003C57 0FA1                <1> 	pop	fs
  2496 00003C59 07                  <1> 	pop	es
  2497 00003C5A 1F                  <1> 	pop	ds
  2498 00003C5B CF                  <1> 	iretd	
  2499                              <1> 		; rti / no, return from interrupt
  2500                              <1> 
  2501                              <1> badsys:
  2502                              <1> 	; 16/04/2015 (Retro UNIX 386 v1 - Beginning)
  2503                              <1> 	; (Major Modification: 'core' dumping procedure in
  2504                              <1>         ;       original UNIX v1 and Retro UNIX 8086 v1
  2505                              <1> 	;	has been changed to print 'Invalid System Call !'
  2506                              <1> 	;	message on the user's console tty.)
  2507                              <1> 	; (EIP, EAX values will be shown on screen with error message)
  2508                              <1> 	; (EIP = Return address just after the system call -INT 30h-)
  2509                              <1> 	; (EAX = Function number)  
  2510                              <1> 	;
  2511 00003C5C FE05[28750000]      <1> 	inc	byte [u.bsys]
  2512                              <1> 	;
  2513 00003C62 8B1D[D0740000]      <1> 	mov	ebx, [u.sp] ; esp at the beginning of 'sysent'
  2514 00003C68 8B03                <1> 	mov	eax, [ebx] ; EIP (return address, not 'INT 30h' address)
  2515 00003C6A E839DBFFFF          <1> 	call	dwordtohex
  2516 00003C6F 8915[146D0000]      <1> 	mov	[bsys_msg_eip], edx
  2517 00003C75 A3[186D0000]        <1> 	mov	[bsys_msg_eip+4], eax
  2518 00003C7A A1[D8740000]        <1> 	mov	eax, [u.r0]
  2519 00003C7F E824DBFFFF          <1> 	call	dwordtohex
  2520 00003C84 8915[046D0000]      <1> 	mov	[bsys_msg_eax], edx
  2521 00003C8A A3[086D0000]        <1> 	mov	[bsys_msg_eax+4], eax
  2522                              <1> 	; 11/12/2021
  2523                              <1> 	;xor	eax, eax
  2524                              <1> 	;mov	dword [u.base], badsys_msg ; "Invalid System Call !"
  2525                              <1> 	;mov	ebx, [u.fofp]
  2526                              <1> 	;mov	[ebx], eax
  2527                              <1> 	;;mov	eax, 1 ; inode number of console tty (for user)	
  2528                              <1> 	;inc	eax
  2529                              <1> 	; 11/12/2021 - Retro UNIX 386 v2 fs compatibility 
  2530                              <1> 	; (Retro UNIX 386 v1.2)
  2531                              <1> 	;mov	al, 8 ; /dev/tty inode number (runix v2 fs)
  2532                              <1> 	;; eax = 8 ; inode number of console tty (for user)	
  2533                              <1> 	;mov	dword [u.count], BSYS_M_SIZE
  2534                              <1> 		; writei
  2535                              <1> 		; INPUTS ->
  2536                              <1> 		;    r1 - inode number
  2537                              <1> 		;    u.count - byte count to be written
  2538                              <1> 		;    u.base - points to user buffer
  2539                              <1> 		;    u.fofp - points to word with current file offset
  2540                              <1> 		; OUTPUTS ->
  2541                              <1> 		;    u.count - cleared
  2542                              <1> 		;    u.nread - accumulates total bytes passed back	
  2543                              <1> 		;
  2544                              <1> 		; ((Modified registers: EDX, EBX, ECX, ESI, EDI, EBP)) 	
  2545                              <1> 	;call	writei
  2546                              <1> 	;;mov	eax, 1
  2547                              <1> 	;jmp	sysexit
  2548                              <1> 
  2549                              <1> 	; 11/12/2021 - Retro UNIX 386 v1.2
  2550 00003C8F BE[E56C0000]        <1> 	mov	esi, badsys_msg ; "Invalid System Call !"
  2551 00003C94 0FB61D[29750000]    <1> 	movzx	ebx, byte [u.uno] ; process number
  2552 00003C9B 8A83[F3700000]      <1> 	mov	al, [ebx+p.ttyc-1] ; current/console tty
  2553 00003CA1 C605[E76F0000]0F    <1> 	mov	byte [ccolor], 0Fh ; white (message) color
  2554 00003CA8 A2[0D750000]        <1> 	mov 	[u.ttyn], al ; current (active) tty (for user)
  2555 00003CAD E8052A0000          <1> 	call	print_cmsg
  2556                              <1> 	;mov	dword [u.error], ERR_INV_FUNC ; 1 ; 'invalid system call !'
  2557 00003CB2 E9F3FEFFFF          <1> 	jmp	error
  2558                              <1> 
  2559                              <1> 		; incb u.bsys / turn on the user's bad-system flag
  2560                              <1> 		; mov $3f,u.namep / point u.namep to "core\0\0"
  2561                              <1> 		; jsr r0,namei / get the i-number for the core image file
  2562                              <1> 		; br 1f / error
  2563                              <1> 		; neg r1 / negate the i-number to open the core image file
  2564                              <1> 		       ; / for writing
  2565                              <1> 		; jsr r0,iopen / open the core image file
  2566                              <1> 		; jsr r0,itrunc / free all associated blocks
  2567                              <1> 		; br 2f
  2568                              <1> ;1:
  2569                              <1> 		; mov $17,r1 / put i-node mode (17) in r1
  2570                              <1> 		; jsr r0,maknod / make an i-node
  2571                              <1> 		; mov u.dirbuf,r1 / put i-node number in r1
  2572                              <1> ;2:
  2573                              <1> 		; mov $core,u.base / move address core to u.base
  2574                              <1> 		; mov $ecore-core,u.count / put the byte count in u.count
  2575                              <1> 		; mov $u.off,u.fofp / more user offset to u.fofp
  2576                              <1> 		; clr u.off / clear user offset
  2577                              <1> 		; jsr r0,writei / write out the core image to the user
  2578                              <1> 		; mov $user,u.base / pt. u.base to user
  2579                              <1> 		; mov $64.,u.count / u.count = 64
  2580                              <1> 		; jsr r0,writei / write out all the user parameters
  2581                              <1> 		; neg r1 / make i-number positive
  2582                              <1> 		; jsr r0,iclose / close the core image file
  2583                              <1> 		; br sysexit /
  2584                              <1> ;3:
  2585                              <1> 		; <core\0\0>
  2586                              <1> 
  2587                              <1> intract: ; / interrupt action
  2588                              <1> 	; 14/10/2015
  2589                              <1> 	; 16/04/2015 (Retro UNIX 386 v1 - Beginning)
  2590                              <1> 	; 09/05/2013 - 07/12/2013 (Retro UNIX 8086 v1)
  2591                              <1> 	;
  2592                              <1> 	; Retro UNIX 8086 v1 modification !
  2593                              <1> 	; (Process/task switching and quit routine by using
  2594                              <1> 	; Retro UNIX 8086 v1 keyboard interrupt output.))
  2595                              <1> 	;
  2596                              <1> 	; input -> 'u.quit'  (also value of 'u.intr' > 0)
  2597                              <1> 	; output -> If value of 'u.quit' = FFFFh ('ctrl+brk' sign)
  2598                              <1> 	;		'intract' will jump to 'sysexit'.
  2599                              <1> 	;	    Intract will return to the caller 
  2600                              <1> 	;		if value of 'u.quit' <> FFFFh. 	 
  2601                              <1> 	; 14/10/2015
  2602 00003CB7 FB                  <1> 	sti
  2603                              <1> 	; 07/12/2013	
  2604 00003CB8 66FF05[26750000]    <1> 	inc 	word [u.quit]
  2605 00003CBF 7408                <1> 	jz	short intrct0 ; FFFFh -> 0
  2606 00003CC1 66FF0D[26750000]    <1> 	dec	word [u.quit]
  2607                              <1> 	; 16/04/2015
  2608 00003CC8 C3                  <1> 	retn
  2609                              <1> intrct0:	
  2610 00003CC9 58                  <1> 	pop	eax ; call intract -> retn
  2611                              <1> 	;
  2612 00003CCA 31C0                <1> 	xor 	eax, eax
  2613 00003CCC FEC0                <1> 	inc	al  ; mov ax, 1
  2614                              <1> ;;;
  2615                              <1> 	; UNIX v1 original 'intract' routine... 
  2616                              <1> 	; / interrupt action
  2617                              <1> 		;cmp *(sp),$rti / are you in a clock interrupt?
  2618                              <1> 		; bne 1f / no, 1f
  2619                              <1> 		; cmp (sp)+,(sp)+ / pop clock pointer
  2620                              <1> 	; 1: / now in user area
  2621                              <1> 		; mov r1,-(sp) / save r1
  2622                              <1> 		; mov u.ttyp,r1 
  2623                              <1> 			; / pointer to tty buffer in control-to r1
  2624                              <1> 		; cmpb 6(r1),$177
  2625                              <1> 			; / is the interrupt char equal to "del"
  2626                              <1> 		; beq 1f / yes, 1f
  2627                              <1> 		; clrb 6(r1) 
  2628                              <1> 		        ; / no, clear the byte 
  2629                              <1> 			; / (must be a quit character)
  2630                              <1> 		; mov (sp)+,r1 / restore r1
  2631                              <1> 		; clr u.quit / clear quit flag
  2632                              <1> 		; bis $20,2(sp) 
  2633                              <1> 		    	; / set trace for quit (sets t bit of 
  2634                              <1> 			; / ps-trace trap)
  2635                              <1> 		; rti   ;  / return from interrupt
  2636                              <1> 	; 1: / interrupt char = del
  2637                              <1> 		; clrb 6(r1) / clear the interrupt byte 
  2638                              <1> 			   ; / in the buffer
  2639                              <1> 		; mov (sp)+,r1 / restore r1
  2640                              <1> 		; cmp u.intr,$core / should control be 
  2641                              <1> 				; / transferred to loc core?
  2642                              <1> 		; blo 1f
  2643                              <1> 		; jmp *u.intr / user to do rti yes, 
  2644                              <1> 				; / transfer to loc core
  2645                              <1> 	; 1:
  2646                              <1> 		; sys 1 / exit
  2647                              <1> 
  2648                              <1> sysexit: ; <terminate process>
  2649                              <1> 	; 30/11/2021 - etro UNIX 386 v1.2
  2650                              <1> 	; 01/09/2015
  2651                              <1> 	; 31/08/2015
  2652                              <1> 	; 14/05/2015
  2653                              <1> 	; 16/04/2015 (Retro UNIX 386 v1 - Beginning)
  2654                              <1> 	; 19/04/2013 - 14/02/2014 (Retro UNIX 8086 v1)
  2655                              <1> 	;
  2656                              <1> 	; 'sysexit' terminates a process. First each file that
  2657                              <1> 	; the process has opened is closed by 'flose'. The process
  2658                              <1> 	; status is then set to unused. The 'p.pid' table is then
  2659                              <1> 	; searched to find children of the dying process. If any of
  2660                              <1> 	; children are zombies (died by not waited for), they are
  2661                              <1> 	; set free. The 'p.pid' table is then searched to find the
  2662                              <1> 	; dying process's parent. When the parent is found, it is
  2663                              <1> 	; checked to see if it is free or it is a zombie. If it is
  2664                              <1> 	; one of these, the dying process just dies. If it is waiting
  2665                              <1> 	; for a child process to die, it notified that it doesn't 
  2666                              <1> 	; have to wait anymore by setting it's status from 2 to 1
  2667                              <1> 	; (waiting to active). It is awakened and put on runq by
  2668                              <1> 	; 'putlu'. The dying process enters a zombie state in which
  2669                              <1> 	; it will never be run again but stays around until a 'wait'
  2670                              <1> 	; is completed by it's parent process. If the parent is not
  2671                              <1> 	; found, process just dies. This means 'swap' is called with
  2672                              <1> 	; 'u.uno=0'. What this does is the 'wswap' is not called
  2673                              <1> 	; to write out the process and 'rswap' reads the new process
  2674                              <1> 	; over the one that dies..i.e., the dying process is 
  2675                              <1> 	; overwritten and destroyed.	
  2676                              <1>  	;
  2677                              <1> 	; Calling sequence:
  2678                              <1> 	;	sysexit or conditional branch.
  2679                              <1> 	; Arguments:
  2680                              <1> 	;	-	
  2681                              <1> 	; ...............................................................
  2682                              <1> 	;	
  2683                              <1> 	; Retro UNIX 8086 v1 modification: 
  2684                              <1> 	;       System call number (=1) is in EAX register.
  2685                              <1> 	;
  2686                              <1> 	;       Other parameters are in EDX, EBX, ECX, ESI, EDI, EBP
  2687                              <1> 	;       registers depending of function details.
  2688                              <1> 	;
  2689                              <1> 	; ('swap' procedure is mostly different than original UNIX v1.)
  2690                              <1> 	;
  2691                              <1> ; / terminate process
  2692                              <1> 	; AX = 1
  2693                              <1> 	;dec 	ax ; 0
  2694                              <1> 	; 30/11/2021
  2695 00003CCE 48                  <1> 	dec	eax ; 0
  2696 00003CCF 66A3[24750000]      <1> 	mov	[u.intr], ax ; 0
  2697                              <1> 		; clr u.intr / clear interrupt control word
  2698                              <1> 		; clr r1 / clear r1
  2699                              <1> 	; AX = 0
  2700                              <1> sysexit_1: ; 1:
  2701                              <1> 	; AX = File descriptor
  2702                              <1> 		; / r1 has file descriptor (index to u.fp list)
  2703                              <1> 		; / Search the whole list
  2704 00003CD5 E8900C0000          <1> 	call	fclose
  2705                              <1> 		; jsr r0,fclose / close all files the process opened
  2706                              <1> 	;; ignore error return
  2707                              <1> 		; br .+2 / ignore error return
  2708                              <1> 	;inc	ax
  2709 00003CDA FEC0                <1> 	inc	al
  2710                              <1> 		; inc r1 / increment file descriptor
  2711                              <1> 	;cmp	ax, 10
  2712 00003CDC 3C0A                <1> 	cmp	al, 10
  2713                              <1> 		; cmp r1,$10. / end of u.fp list?
  2714 00003CDE 72F5                <1> 	jb	short sysexit_1
  2715                              <1> 		; blt 1b / no, go back
  2716 00003CE0 0FB61D[29750000]    <1> 	movzx	ebx, byte [u.uno] ; 01/09/2015
  2717                              <1> 		; movb	u.uno,r1 / yes, move dying process's number to r1
  2718 00003CE7 88A3[23710000]      <1> 	mov	[ebx+p.stat-1], ah ; 0, SFREE, 05/02/2014
  2719                              <1> 		; clrb p.stat-1(r1) / free the process
  2720                              <1> 	;shl	bx, 1
  2721 00003CED D0E3                <1> 	shl	bl, 1
  2722                              <1> 		; asl r1 / use r1 for index into the below tables
  2723 00003CEF 668B8B[B2700000]    <1> 	mov	cx, [ebx+p.pid-2]
  2724                              <1> 		; mov p.pid-2(r1),r3 / move dying process's name to r3
  2725 00003CF6 668B93[D2700000]    <1> 	mov	dx, [ebx+p.ppid-2]
  2726                              <1> 		; mov p.ppid-2(r1),r4 / move its parents name to r4
  2727                              <1> 	; xor 	bx, bx ; 0
  2728 00003CFD 30DB                <1> 	xor	bl, bl ; 0
  2729                              <1> 		; clr r2
  2730 00003CFF 31F6                <1> 	xor	esi, esi ; 0
  2731                              <1> 		; clr r5 / initialize reg
  2732                              <1> sysexit_2: ; 1:
  2733                              <1> 	        ; / find children of this dying process, 
  2734                              <1> 		; / if they are zombies, free them
  2735                              <1> 	;add	bx, 2
  2736 00003D01 80C302              <1> 	add	bl, 2
  2737                              <1> 		; add $2,r2 / search parent process table 
  2738                              <1> 		          ; / for dying process's name
  2739 00003D04 66398B[D2700000]    <1> 	cmp	[ebx+p.ppid-2], cx
  2740                              <1> 		; cmp p.ppid-2(r2),r3 / found it?
  2741 00003D0B 7513                <1> 	jne	short sysexit_4
  2742                              <1> 		; bne 3f / no
  2743                              <1> 	;shr	bx, 1
  2744 00003D0D D0EB                <1> 	shr	bl, 1
  2745                              <1> 		; asr r2 / yes, it is a parent
  2746 00003D0F 80BB[23710000]03    <1> 	cmp	byte [ebx+p.stat-1], 3 ; SZOMB, 05/02/2014
  2747                              <1> 		; cmpb p.stat-1(r2),$3 / is the child of this 
  2748                              <1> 				     ; / dying process a zombie
  2749 00003D16 7506                <1> 	jne	short sysexit_3 
  2750                              <1> 		; bne 2f / no
  2751 00003D18 88A3[23710000]      <1> 	mov	[ebx+p.stat-1], ah ; 0, SFREE, 05/02/2014
  2752                              <1> 		; clrb p.stat-1(r2) / yes, free the child process
  2753                              <1> sysexit_3: ; 2:
  2754                              <1> 	;shr	bx, 1
  2755 00003D1E D0E3                <1> 	shl	bl, 1
  2756                              <1> 		; asl r2
  2757                              <1> sysexit_4: ; 3:
  2758                              <1> 		; / search the process name table 
  2759                              <1> 		; / for the dying process's parent
  2760 00003D20 663993[B2700000]    <1> 	cmp	[ebx+p.pid-2], dx ; 17/09/2013	
  2761                              <1> 		; cmp p.pid-2(r2),r4 / found it?
  2762 00003D27 7502                <1> 	jne	short sysexit_5
  2763                              <1> 		; bne 3f / no
  2764 00003D29 89DE                <1> 	mov	esi, ebx
  2765                              <1> 		; mov r2,r5 / yes, put index to p.pid table (parents
  2766                              <1> 		          ; / process # x2) in r5
  2767                              <1> sysexit_5: ; 3:
  2768                              <1> 	;cmp	bx, nproc + nproc
  2769 00003D2B 80FB20              <1> 	cmp	bl, nproc + nproc
  2770                              <1> 		; cmp r2,$nproc+nproc / has whole table been searched?
  2771 00003D2E 72D1                <1> 	jb	short sysexit_2
  2772                              <1> 		; blt 1b / no, go back
  2773                              <1> 		; mov r5,r1 / yes, r1 now has parents process # x2
  2774 00003D30 21F6                <1> 	and	esi, esi ; r5=r1
  2775 00003D32 7431                <1> 	jz	short sysexit_6
  2776                              <1> 		; beq 2f / no parent has been found. 
  2777                              <1> 		       ; / The process just dies
  2778 00003D34 66D1EE              <1> 	shr	si, 1
  2779                              <1> 		; asr r1 / set up index to p.stat
  2780 00003D37 8A86[23710000]      <1> 	mov	al, [esi+p.stat-1]
  2781                              <1> 		; movb p.stat-1(r1),r2 / move status of parent to r2
  2782 00003D3D 20C0                <1> 	and	al, al
  2783 00003D3F 7424                <1> 	jz	short sysexit_6
  2784                              <1> 		; beq 2f / if its been freed, 2f
  2785 00003D41 3C03                <1> 	cmp	al, 3
  2786                              <1> 		; cmp r2,$3 / is parent a zombie?
  2787 00003D43 7420                <1> 	je	short sysexit_6
  2788                              <1> 		; beq 2f / yes, 2f
  2789                              <1> 	; BH = 0
  2790 00003D45 8A1D[29750000]      <1> 	mov	bl, [u.uno]
  2791                              <1> 		; movb u.uno,r3 / move dying process's number to r3
  2792 00003D4B C683[23710000]03    <1> 	mov	byte [ebx+p.stat-1], 3  ; SZOMB, 05/02/2014
  2793                              <1> 		; movb $3,p.stat-1(r3) / make the process a zombie
  2794                              <1> 	; 05/02/2014
  2795 00003D52 3C01                <1> 	cmp	al, 1 ; SRUN
  2796 00003D54 740F                <1> 	je	short sysexit_6
  2797                              <1> 	;cmp	al, 2
  2798                              <1> 		; cmp r2,$2 / is the parent waiting for 
  2799                              <1> 			  ; / this child to die
  2800                              <1> 	;jne	short sysexit_6	
  2801                              <1> 		; bne 2f / yes, notify parent not to wait any more
  2802                              <1> 	; 05/02/2014
  2803                              <1> 	; p.stat = 2 --> waiting
  2804                              <1> 	; p.stat = 4 --> sleeping
  2805 00003D56 C686[23710000]01    <1> 	mov	byte [esi+p.stat-1], 1 ; SRUN ; 05/02/2014
  2806                              <1> 	;dec	byte [esi+p.stat-1]
  2807                              <1> 		; decb	p.stat-1(r1) / awaken it by putting it (parent)
  2808 00003D5D 6689F0              <1> 	mov	ax, si ; r1  (process number in AL)
  2809                              <1> 	; 
  2810                              <1> 	;mov	ebx, runq + 4
  2811                              <1> 		; mov $runq+4,r2 / on the runq
  2812 00003D60 E87C120000          <1> 	call	putlu
  2813                              <1> 		; jsr r0, putlu
  2814                              <1> sysexit_6: ; 2:
  2815                              <1> 	; 31/08/2015
  2816                              <1> 		; / the process dies
  2817 00003D65 C605[29750000]00    <1> 	mov	byte [u.uno], 0
  2818                              <1> 		; clrb u.uno / put zero as the process number, 
  2819                              <1> 	           ; / so "swap" will
  2820 00003D6C E8A3110000          <1> 	call	swap
  2821                              <1> 		; jsr r0,swap / overwrite process with another process
  2822                              <1> hlt_sys:
  2823                              <1> 	;sti ; 18/01/2014
  2824                              <1> hlts0:
  2825 00003D71 F4                  <1> 	hlt
  2826 00003D72 EBFD                <1> 	jmp	short hlts0
  2827                              <1> 		; 0 / and thereby kill it; halt?
  2828                              <1> 
  2829                              <1> 
  2830                              <1> syswait: ; < wait for a processs to die >
  2831                              <1> 	; 08/01/2022 (Retro UNIX 386 v1.2)
  2832                              <1> 	; 17/09/2015
  2833                              <1> 	; 02/09/2015
  2834                              <1> 	; 01/09/2015
  2835                              <1> 	; 16/04/2015 (Retro UNIX 386 v1 - Beginning)
  2836                              <1> 	; 24/05/2013 - 05/02/2014 (Retro UNIX 8086 v1)
  2837                              <1> 	;
  2838                              <1> 	; 'syswait' waits for a process die. 
  2839                              <1> 	; It works in following way:
  2840                              <1> 	;    1) From the parent process number, the parent's 
  2841                              <1> 	; 	process name is found. The p.ppid table of parent
  2842                              <1> 	;	names is then searched for this process name.
  2843                              <1> 	;	If a match occurs, r2 contains child's process
  2844                              <1> 	;	number. The child status is checked to see if it is
  2845                              <1> 	;	a zombie, i.e; dead but not waited for (p.stat=3)
  2846                              <1> 	;	If it is, the child process is freed and it's name
  2847                              <1> 	;	is put in (u.r0). A return is then made via 'sysret'.
  2848                              <1> 	;	If the child is not a zombie, nothing happens and
  2849                              <1> 	;	the search goes on through the p.ppid table until
  2850                              <1> 	;	all processes are checked or a zombie is found.
  2851                              <1> 	;    2) If no zombies are found, a check is made to see if
  2852                              <1> 	;	there are any children at all. If there are none,
  2853                              <1> 	;	an error return is made. If there are, the parent's
  2854                              <1> 	;	status is set to 2 (waiting for child to die),
  2855                              <1> 	;	the parent is swapped out, and a branch to 'syswait'
  2856                              <1> 	;	is made to wait on the next process.
  2857                              <1> 	;
  2858                              <1> 	; Calling sequence:
  2859                              <1> 	;	?
  2860                              <1> 	; Arguments:
  2861                              <1> 	;	-
  2862                              <1> 	; Inputs: - 
  2863                              <1> 	; Outputs: if zombie found, it's name put in u.r0.	
  2864                              <1> 	; ...............................................................
  2865                              <1> 	;				
  2866                              <1> 	
  2867                              <1> ; / wait for a process to die
  2868                              <1> 
  2869                              <1> syswait_0:
  2870 00003D74 0FB61D[29750000]    <1> 	movzx	ebx, byte [u.uno] ; 01/09/2015
  2871                              <1> 		; movb u.uno,r1 / put parents process number in r1
  2872 00003D7B D0E3                <1> 	shl	bl, 1
  2873                              <1> 	;shl	bx, 1
  2874                              <1> 		; asl r1 / x2 to get index into p.pid table
  2875 00003D7D 668B83[B2700000]    <1> 	mov	ax, [ebx+p.pid-2]
  2876                              <1> 		; mov p.pid-2(r1),r1 / get the name of this process
  2877 00003D84 31F6                <1> 	xor	esi, esi
  2878                              <1> 		; clr r2
  2879 00003D86 31C9                <1> 	xor	ecx, ecx ; 30/10/2013
  2880                              <1> 	;xor 	cl, cl
  2881                              <1> 		; clr r3 / initialize reg 3
  2882                              <1> syswait_1: ; 1:
  2883 00003D88 6683C602            <1> 	add	si, 2
  2884                              <1> 		; add $2,r2 / use r2 for index into p.ppid table
  2885                              <1> 			  ; / search table of parent processes 
  2886                              <1> 			  ; / for this process name
  2887 00003D8C 663B86[D2700000]    <1> 	cmp	ax, [esi+p.ppid-2]
  2888                              <1> 		; cmp p.ppid-2(r2),r1 / r2 will contain the childs 
  2889                              <1> 			            ; / process number
  2890 00003D93 7534                <1> 	jne	short syswait_3
  2891                              <1> 		;bne 3f / branch if no match of parent process name
  2892                              <1> 	;inc	cx
  2893 00003D95 FEC1                <1> 	inc	cl
  2894                              <1> 		;inc r3 / yes, a match, r3 indicates number of children
  2895 00003D97 66D1EE              <1> 	shr	si, 1
  2896                              <1> 		; asr r2 / r2/2 to get index to p.stat table
  2897                              <1> 	; The possible states ('p.stat' values) of a process are:
  2898                              <1> 	;	0 = free or unused
  2899                              <1> 	;	1 = active
  2900                              <1> 	;	2 = waiting for a child process to die
  2901                              <1> 	;	3 = terminated, but not yet waited for (zombie).	
  2902 00003D9A 80BE[23710000]03    <1> 	cmp	byte [esi+p.stat-1], 3 ; SZOMB, 05/02/2014
  2903                              <1> 		; cmpb p.stat-1(r2),$3 / is the child process a zombie?
  2904 00003DA1 7523                <1> 	jne	short syswait_2
  2905                              <1> 		; bne 2f / no, skip it
  2906 00003DA3 88BE[23710000]      <1> 	mov	[esi+p.stat-1], bh ; 0
  2907                              <1> 		; clrb p.stat-1(r2) / yes, free it
  2908 00003DA9 66D1E6              <1> 	shl	si, 1
  2909                              <1> 		; asl r2 / r2x2 to get index into p.pid table
  2910 00003DAC 0FB786[B2700000]    <1> 	movzx	eax, word [esi+p.pid-2]
  2911 00003DB3 A3[D8740000]        <1> 	mov	[u.r0], eax
  2912                              <1> 		; mov p.pid-2(r2),*u.r0 
  2913                              <1> 			      ; / put childs process name in (u.r0)
  2914                              <1> 	;
  2915                              <1> 	; Retro UNIX 386 v1 modification ! (17/09/2015)
  2916                              <1> 	;
  2917                              <1> 	; Parent process ID -p.ppid- field (of the child process)
  2918                              <1> 	; must be cleared in order to prevent infinitive 'syswait'
  2919                              <1> 	; system call loop from the application/program if it calls
  2920                              <1> 	; 'syswait' again (mistakenly) while there is not a zombie
  2921                              <1> 	; or running child process to wait. ('forktest.s', 17/09/2015)
  2922                              <1> 	;
  2923                              <1> 	; Note: syswait will return with error if there is not a
  2924                              <1> 	;       zombie or running process to wait.	
  2925                              <1> 	;
  2926                              <1> 	;sub	ax, ax
  2927                              <1> 	; 08/01/2022
  2928 00003DB8 29C0                <1> 	sub	eax, eax
  2929 00003DBA 668986[D2700000]    <1> 	mov 	[esi+p.ppid-2], ax ; 0 ; 17/09/2015
  2930 00003DC1 E906FEFFFF          <1> 	jmp	sysret0 ; ax = 0
  2931                              <1> 	;
  2932                              <1> 	;jmp	sysret
  2933                              <1> 		; br sysret1 / return cause child is dead
  2934                              <1> syswait_2: ; 2:
  2935 00003DC6 66D1E6              <1> 	shl	si, 1
  2936                              <1> 		; asl r2 / r2x2 to get index into p.ppid table
  2937                              <1> syswait_3: ; 3:
  2938 00003DC9 6683FE20            <1> 	cmp	si, nproc+nproc
  2939                              <1> 		; cmp r2,$nproc+nproc / have all processes been checked?
  2940 00003DCD 72B9                <1> 	jb	short syswait_1
  2941                              <1> 		; blt 1b / no, continue search
  2942                              <1> 	;and	cx, cx
  2943 00003DCF 20C9                <1> 	and	cl, cl
  2944                              <1> 		; tst r3 / one gets here if there are no children 
  2945                              <1> 		       ; / or children that are still active
  2946                              <1> 	; 30/10/2013
  2947 00003DD1 750B                <1> 	jnz	short syswait_4
  2948                              <1> 	;jz	error
  2949                              <1> 		; beq error1 / there are no children, error
  2950 00003DD3 890D[D8740000]      <1> 	mov	[u.r0], ecx ; 0
  2951 00003DD9 E9CCFDFFFF          <1> 	jmp	error
  2952                              <1> syswait_4:
  2953 00003DDE 8A1D[29750000]      <1> 	mov	bl, [u.uno]
  2954                              <1> 		; movb u.uno,r1 / there are children so put 
  2955                              <1> 			      ; / parent process number in r1
  2956 00003DE4 FE83[23710000]      <1> 	inc	byte [ebx+p.stat-1] ; 2, SWAIT, 05/02/2014
  2957                              <1> 		; incb p.stat-1(r1) / it is waiting for 
  2958                              <1> 				  ; / other children to die
  2959                              <1> 	; 04/11/2013
  2960 00003DEA E825110000          <1> 	call	swap
  2961                              <1> 		; jsr r0,swap / swap it out, because it's waiting
  2962 00003DEF EB83                <1> 	jmp	syswait_0
  2963                              <1> 		; br syswait / wait on next process
  2964                              <1> 
  2965                              <1> sysfork: ; < create a new process >
  2966                              <1> 	; 08/01/2022 (Retro UNIX 386 v1.2)
  2967                              <1> 	; 18/09/2015
  2968                              <1> 	; 04/09/2015
  2969                              <1> 	; 02/09/2015
  2970                              <1> 	; 01/09/2015
  2971                              <1> 	; 28/08/2015
  2972                              <1> 	; 14/05/2015
  2973                              <1> 	; 10/05/2015
  2974                              <1> 	; 09/05/2015
  2975                              <1> 	; 06/05/2015 (Retro UNIX 386 v1 - Beginning)
  2976                              <1> 	; 24/05/2013 - 14/02/2014 (Retro UNIX 8086 v1)
  2977                              <1> 	;
  2978                              <1> 	; 'sysfork' creates a new process. This process is referred
  2979                              <1> 	; to as the child process. This new process core image is
  2980                              <1> 	; a copy of that of the caller of 'sysfork'. The only
  2981                              <1> 	; distinction is the return location and the fact that (u.r0)
  2982                              <1> 	; in the old process (parent) contains the process id (p.pid)
  2983                              <1> 	; of the new process (child). This id is used by 'syswait'.
  2984                              <1> 	; 'sysfork' works in the following manner: 	
  2985                              <1> 	;    1) The process status table (p.stat) is searched to find
  2986                              <1> 	;	a process number that is unused. If none are found
  2987                              <1> 	;	an error occurs.
  2988                              <1> 	;    2) when one is found, it becomes the child process number
  2989                              <1> 	;	and it's status (p.stat) is set to active.
  2990                              <1> 	;    3) If the parent had a control tty, the interrupt 
  2991                              <1> 	;	character in that tty buffer is cleared.
  2992                              <1> 	;    4) The child process is put on the lowest priority run 
  2993                              <1> 	;	queue via 'putlu'.
  2994                              <1> 	;    5) A new process name is gotten from 'mpid' (actually 
  2995                              <1> 	;	it is a unique number) and is put in the child's unique
  2996                              <1> 	;	identifier; process id (p.pid).
  2997                              <1> 	;    6) The process name of the parent is then obtained and
  2998                              <1> 	;	placed in the unique identifier of the parent process
  2999                              <1> 	;	name is then put in 'u.r0'.	
  3000                              <1> 	;    7) The child process is then written out on disk by
  3001                              <1> 	;	'wswap',i.e., the parent process is copied onto disk
  3002                              <1> 	;	and the child is born. (The child process is written 
  3003                              <1> 	;	out on disk/drum with 'u.uno' being the child process
  3004                              <1> 	;	number.)
  3005                              <1> 	;    8) The parent process number is then restored to 'u.uno'.
  3006                              <1> 	;    9) The child process name is put in 'u.r0'.
  3007                              <1> 	;   10) The pc on the stack sp + 18 is incremented by 2 to
  3008                              <1> 	;	create the return address for the parent process.
  3009                              <1> 	;   11) The 'u.fp' list as then searched to see what files
  3010                              <1> 	;	the parent has opened. For each file the parent has
  3011                              <1> 	;	opened, the corresponding 'fsp' entry must be updated
  3012                              <1> 	;	to indicate that the child process also has opened
  3013                              <1> 	;	the file. A branch to 'sysret' is then made.	 			 				
  3014                              <1> 	;
  3015                              <1> 	; Calling sequence:
  3016                              <1> 	;	from shell ?
  3017                              <1> 	; Arguments:
  3018                              <1> 	;	-
  3019                              <1> 	; Inputs: -
  3020                              <1> 	; Outputs: *u.r0 - child process name
  3021                              <1> 	; ...............................................................
  3022                              <1> 	;	
  3023                              <1> 	; Retro UNIX 8086 v1 modification: 
  3024                              <1> 	;	AX = r0 = PID (>0) (at the return of 'sysfork')
  3025                              <1> 	;	= process id of child a parent process returns
  3026                              <1> 	;	= process id of parent when a child process returns
  3027                              <1> 	;
  3028                              <1> 	;       In original UNIX v1, sysfork is called and returns as
  3029                              <1> 	;	in following manner: (with an example: c library, fork)
  3030                              <1> 	;	
  3031                              <1> 	;	1:
  3032                              <1> 	;		sys	fork
  3033                              <1> 	;			br 1f  / child process returns here
  3034                              <1> 	;		bes	2f     / parent process returns here
  3035                              <1> 	;		/ pid of new process in r0
  3036                              <1> 	;		rts	pc
  3037                              <1> 	;	2: / parent process condionally branches here
  3038                              <1> 	;		mov	$-1,r0 / pid = -1 means error return
  3039                              <1> 	;		rts	pc
  3040                              <1> 	;
  3041                              <1> 	;	1: / child process brances here
  3042                              <1> 	;		clr	r0   / pid = 0 in child process
  3043                              <1> 	;		rts	pc
  3044                              <1> 	;
  3045                              <1> 	;	In UNIX v7x86 (386) by Robert Nordier (1999)
  3046                              <1> 	;		// pid = fork();
  3047                              <1> 	;		//
  3048                              <1> 	;		// pid == 0 in child process; 
  3049                              <1> 	;		// pid == -1 means error return
  3050                              <1> 	;		// in child, 
  3051                              <1> 	;		//	parents id is in par_uid if needed
  3052                              <1> 	;		
  3053                              <1> 	;		_fork:
  3054                              <1> 	;			mov	$.fork,eax
  3055                              <1> 	;			int	$0x30
  3056                              <1> 	;			jmp	1f
  3057                              <1> 	;			jnc	2f
  3058                              <1> 	;			jmp	cerror
  3059                              <1> 	;		1:
  3060                              <1> 	;			mov	eax,_par_uid
  3061                              <1> 	;			xor	eax,eax
  3062                              <1> 	;		2:
  3063                              <1> 	;			ret
  3064                              <1> 	;
  3065                              <1> 	;	In Retro UNIX 8086 v1,
  3066                              <1> 	;	'sysfork' returns in following manner:
  3067                              <1> 	;	
  3068                              <1> 	;		mov	ax, sys_fork
  3069                              <1> 	;		mov	bx, offset @f ; routine for child
  3070                              <1> 	;		int	20h
  3071                              <1> 	;		jc	error
  3072                              <1> 	;		
  3073                              <1> 	;	; Routine for parent process here (just after 'jc')
  3074                              <1> 	;		mov	word ptr [pid_of_child], ax
  3075                              <1> 	;		jmp	next_routine_for_parent	
  3076                              <1> 	;
  3077                              <1> 	;	@@: ; routine for child process here				
  3078                              <1> 	;		....	
  3079                              <1> 	;	NOTE: 'sysfork' returns to specified offset
  3080                              <1> 	;	       for child process by using BX input.
  3081                              <1> 	;	      (at first, parent process will return then 
  3082                              <1> 	;	      child process will return -after swapped in-
  3083                              <1> 	;	      'syswait' is needed in parent process
  3084                              <1> 	;	      if return from child process will be waited for.)
  3085                              <1> 	;	  				
  3086                              <1> 	
  3087                              <1> ; / create a new process
  3088                              <1> 	; EBX = return address for child process 
  3089                              <1> 	     ; (Retro UNIX 8086 v1 modification !)
  3090 00003DF1 31F6                <1> 	xor 	esi, esi
  3091                              <1> 		; clr r1
  3092                              <1> sysfork_1: ; 1: / search p.stat table for unused process number
  3093 00003DF3 46                  <1> 	inc	esi
  3094                              <1> 		; inc r1
  3095 00003DF4 80BE[23710000]00    <1> 	cmp	byte [esi+p.stat-1], 0 ; SFREE, 05/02/2014
  3096                              <1> 		; tstb p.stat-1(r1) / is process active, unused, dead
  3097 00003DFB 760B                <1> 	jna	short sysfork_2	
  3098                              <1> 		; beq 1f / it's unused so branch
  3099 00003DFD 6683FE10            <1> 	cmp	si, nproc
  3100                              <1> 		; cmp r1,$nproc / all processes checked
  3101 00003E01 72F0                <1> 	jb	short sysfork_1
  3102                              <1> 		; blt 1b / no, branch back
  3103                              <1> 	;
  3104                              <1> 	; Retro UNIX 8086 v1. modification:
  3105                              <1> 	;	Parent process returns from 'sysfork' to address 
  3106                              <1> 	;	which is just after 'sysfork' system call in parent
  3107                              <1> 	;	process. Child process returns to address which is put
  3108                              <1> 	;	in BX register by parent process for 'sysfork'. 
  3109                              <1> 	;
  3110                              <1> 		;add $2,18.(sp) / add 2 to pc when trap occured, points
  3111                              <1> 		             ; / to old process return
  3112                              <1> 		; br error1 / no room for a new process
  3113                              <1> sysfork_0:
  3114 00003E03 E9A2FDFFFF          <1> 	jmp	error
  3115                              <1> sysfork_2: ; 1:
  3116 00003E08 E859F0FFFF          <1> 	call	allocate_page
  3117                              <1> 	;jc	error
  3118                              <1> 	; 08/01/2022
  3119 00003E0D 72F4                <1> 	jc	short sysfork_0
  3120                              <1> 
  3121 00003E0F 50                  <1> 	push	eax   ; UPAGE (user structure page) address
  3122                              <1> 	; Retro UNIX 386 v1 modification!
  3123 00003E10 E84AF2FFFF          <1> 	call	duplicate_page_dir
  3124                              <1> 		; EAX = New page directory 
  3125 00003E15 7308                <1> 	jnc	short sysfork_3
  3126 00003E17 58                  <1> 	pop	eax   ; UPAGE (user structure page) address
  3127 00003E18 E811F2FFFF          <1> 	call 	deallocate_page
  3128                              <1> 	;jmp	error
  3129                              <1> 	; 08/01/2022
  3130 00003E1D EBE4                <1> 	jmp	short sysfork_0  ; error
  3131                              <1> sysfork_3:
  3132                              <1> 	; Retro UNIX 386 v1 modification !
  3133 00003E1F 56                  <1> 	push	esi
  3134 00003E20 E866110000          <1> 	call	wswap ; save current user (u) structure, user registers
  3135                              <1> 		      ; and interrupt return components (for IRET)
  3136 00003E25 8705[38750000]      <1> 	xchg	eax, [u.pgdir] ; page directory of the child process
  3137 00003E2B A3[3C750000]        <1> 	mov	[u.ppgdir], eax ; page directory of the parent process
  3138 00003E30 5E                  <1> 	pop	esi
  3139 00003E31 58                  <1> 	pop	eax   ; UPAGE (user structure page) address
  3140                              <1> 		; [u.usp] = esp
  3141 00003E32 89F7                <1> 	mov	edi, esi
  3142                              <1> 	;shl	di, 2
  3143                              <1> 	; 08/01/2022
  3144 00003E34 C1E702              <1> 	shl	edi, 2
  3145 00003E37 8987[30710000]      <1> 	mov	[edi+p.upage-4], eax ; memory page for 'user' struct
  3146 00003E3D A3[34750000]        <1> 	mov	[u.upage], eax ; memory page for 'user' struct (child)
  3147                              <1> 	; 28/08/2015
  3148 00003E42 0FB605[29750000]    <1> 	movzx	eax, byte [u.uno] ; parent process number
  3149                              <1> 		; movb u.uno,-(sp) / save parent process number
  3150 00003E49 89C7                <1> 	mov	edi, eax
  3151 00003E4B 50                  <1>         push	eax ; ** 
  3152 00003E4C 8A87[F3700000]      <1> 	mov     al, [edi+p.ttyc-1] ; console tty (parent)
  3153                              <1> 	; 18/09/2015
  3154                              <1> 	;mov	[esi+p.ttyc-1], al ; set child's console tty
  3155                              <1> 	;mov	[esi+p.waitc-1], ah ; 0 ; reset child's wait channel
  3156 00003E52 668986[F3700000]    <1> 	mov     [esi+p.ttyc-1], ax ; al - set child's console tty
  3157                              <1> 				   ; ah - reset child's wait channel	
  3158 00003E59 89F0                <1> 	mov	eax, esi
  3159 00003E5B A2[29750000]        <1> 	mov	[u.uno], al ; child process number
  3160                              <1> 		;movb r1,u.uno / set child process number to r1
  3161 00003E60 FE86[23710000]      <1>         inc     byte [esi+p.stat-1] ; 1, SRUN, 05/02/2014
  3162                              <1> 		; incb p.stat-1(r1) / set p.stat entry for child 
  3163                              <1> 				; / process to active status
  3164                              <1> 		; mov u.ttyp,r2 / put pointer to parent process' 
  3165                              <1> 			      ; / control tty buffer in r2
  3166                              <1>                 ; beq 2f / branch, if no such tty assigned
  3167                              <1> 		; clrb 6(r2) / clear interrupt character in tty buffer
  3168                              <1> 	; 2:
  3169 00003E66 53                  <1> 	push	ebx  ; * return address for the child process
  3170                              <1> 		     ; * Retro UNIX 8086 v1 feature only !	
  3171                              <1> 	; (Retro UNIX 8086 v1 modification!)
  3172                              <1> 		; mov $runq+4,r2
  3173 00003E67 E875110000          <1> 	call	putlu 
  3174                              <1>  		; jsr r0,putlu / put child process on lowest priority 
  3175                              <1> 			   ; / run queue
  3176                              <1> 	; 08/01/2022
  3177 00003E6C D1E6                <1> 	shl	esi, 1
  3178                              <1> 	;shl	si, 1
  3179                              <1> 		; asl r1 / multiply r1 by 2 to get index 
  3180                              <1> 		       ; / into p.pid table
  3181 00003E6E 66FF05[BE740000]    <1> 	inc	word [mpid]
  3182                              <1> 		; inc mpid / increment m.pid; get a new process name
  3183 00003E75 66A1[BE740000]      <1> 	mov	ax, [mpid]
  3184 00003E7B 668986[B2700000]    <1> 	mov	[esi+p.pid-2], ax
  3185                              <1> 		;mov mpid,p.pid-2(r1) / put new process name 
  3186                              <1> 				    ; / in child process' name slot
  3187 00003E82 5A                  <1> 	pop	edx  ; * return address for the child process
  3188                              <1> 		     ; * Retro UNIX 8086 v1 feature only !	
  3189 00003E83 5B                  <1>   	pop	ebx  ; **
  3190                              <1> 	;mov	ebx, [esp] ; ** parent process number
  3191                              <1> 		; movb (sp),r2 / put parent process number in r2
  3192                              <1> 	; 08/01/2022
  3193 00003E84 D1E3                <1> 	shl	ebx, 1
  3194                              <1> 	;shl 	bx, 1
  3195                              <1> 		; asl r2 / multiply by 2 to get index into below tables
  3196                              <1> 	;movzx eax, word [ebx+p.pid-2]
  3197 00003E86 668B83[B2700000]    <1> 	mov	ax, [ebx+p.pid-2]
  3198                              <1> 		; mov p.pid-2(r2),r2 / get process name of parent
  3199                              <1> 				   ; / process
  3200 00003E8D 668986[D2700000]    <1> 	mov	[esi+p.ppid-2], ax
  3201                              <1> 		; mov r2,p.ppid-2(r1) / put parent process name 
  3202                              <1> 			  ; / in parent process slot for child
  3203 00003E94 A3[D8740000]        <1> 	mov	[u.r0], eax	
  3204                              <1> 		; mov r2,*u.r0 / put parent process name on stack 
  3205                              <1> 			     ; / at location where r0 was saved
  3206 00003E99 8B2D[D0740000]      <1> 	mov 	ebp, [u.sp] ; points to return address (EIP for IRET)
  3207 00003E9F 895500              <1> 	mov	[ebp], edx ; *, CS:EIP -> EIP
  3208                              <1> 			   ; * return address for the child process
  3209                              <1> 		; mov $sysret1,-(sp) /
  3210                              <1> 		; mov sp,u.usp / contents of sp at the time when 
  3211                              <1> 			      ; / user is swapped out
  3212                              <1> 		; mov $sstack,sp / point sp to swapping stack space
  3213                              <1> 	; 04/09/2015 - 01/09/2015
  3214                              <1> 	; [u.usp] = esp
  3215 00003EA2 68[CA3B0000]        <1> 	push	sysret ; ***
  3216 00003EA7 8925[D4740000]      <1> 	mov	[u.usp], esp ; points to 'sysret' address (***)
  3217                              <1> 			     ; (for child process)	
  3218 00003EAD 31C0                <1> 	xor 	eax, eax
  3219 00003EAF 66A3[0E750000]      <1> 	mov 	[u.ttyp], ax ; 0
  3220                              <1> 	;
  3221 00003EB5 E8D1100000          <1> 	call	wswap ; Retro UNIX 8086 v1 modification !
  3222                              <1> 		;jsr r0,wswap / put child process out on drum
  3223                              <1> 		;jsr r0,unpack / unpack user stack
  3224                              <1> 		;mov u.usp,sp / restore user stack pointer
  3225                              <1> 		; tst (sp)+ / bump stack pointer
  3226                              <1> 	; Retro UNIX 386 v1 modification !
  3227 00003EBA 58                  <1> 	pop	eax ; ***
  3228                              <1> 	; 08/01/2022
  3229 00003EBB D1E3                <1> 	shl	ebx, 1
  3230                              <1> 	;shl 	bx, 1
  3231 00003EBD 8B83[30710000]      <1> 	mov     eax, [ebx+p.upage-4] ; UPAGE address ; 14/05/2015
  3232 00003EC3 E8EC100000          <1> 	call	rswap ; restore parent process 'u' structure, 
  3233                              <1> 		      ; registers and return address (for IRET)
  3234                              <1> 		;movb (sp)+,u.uno / put parent process number in u.uno
  3235 00003EC8 0FB705[BE740000]    <1>         movzx   eax, word [mpid]
  3236 00003ECF A3[D8740000]        <1> 	mov	[u.r0], eax
  3237                              <1> 		; mov mpid,*u.r0 / put child process name on stack 
  3238                              <1> 			       ; / where r0 was saved
  3239                              <1> 		; add $2,18.(sp) / add 2 to pc on stack; gives parent
  3240                              <1> 			          ; / process return
  3241                              <1> 	;xor	ebx, ebx
  3242 00003ED4 31F6                <1> 	xor     esi, esi
  3243                              <1> 		;clr r1
  3244                              <1> sysfork_4: ; 1: / search u.fp list to find the files 
  3245                              <1> 	      ; / opened by the parent process
  3246                              <1> 	; 08/01/2022
  3247                              <1> 	; 01/09/2015
  3248 00003ED6 30FF                <1> 	xor	bh, bh
  3249 00003ED8 8A9E[E2740000]      <1> 	mov 	bl, [esi+u.fp]
  3250                              <1> 	; 08/01/2022
  3251                              <1> 	;mov 	al, [esi+u.fp]
  3252                              <1> 		; movb u.fp(r1),r2 / get an open file for this process
  3253 00003EDE 08DB                <1> 	or	bl, bl
  3254                              <1> 	; 08/01/2022
  3255                              <1> 	;or	al, al
  3256 00003EE0 7409                <1> 	jz	short sysfork_5	
  3257                              <1> 		; beq 2f / file has not been opened by parent, 
  3258                              <1> 		       ; / so branch
  3259                              <1> 	;mov	ah, 10 ; Retro UNIX 386 v1 fsp structure size = 10 bytes
  3260                              <1> 	; 08/01/2022
  3261                              <1> 	;mov	ah, 16 ; Retro UNIX 386 v2 fsp structure size = 16 bytes
  3262                              <1> 	;mul	ah
  3263                              <1> 	;;movzx	ebx, ax
  3264                              <1> 	;mov	bx, ax
  3265                              <1> 	;mov	ebx, eax ; 08/01/2022
  3266                              <1> 	;shl	bx, 3
  3267                              <1> 		; asl r2 / multiply by 8
  3268                              <1>        		; asl r2 / to get index into fsp table
  3269                              <1>        		; asl r2
  3270                              <1> 	; 08/01/2022
  3271 00003EE2 C1E304              <1> 	shl	ebx, 4 ; multiply by 16
  3272                              <1> 	; 08/01/2022
  3273 00003EE5 FE83[6A710000]      <1> 	inc	byte [ebx+fsp-10] ; Retro UNIX 386 v2 fs fsp structure
  3274                              <1>   	;inc	byte [ebx+fsp-2]
  3275                              <1> 		; incb fsp-2(r2) / increment number of processes
  3276                              <1> 			     ; / using file, because child will now be
  3277                              <1> 			     ; / using this file
  3278                              <1> sysfork_5: ; 2:
  3279 00003EEB 46                  <1>         inc     esi
  3280                              <1> 		; inc r1 / get next open file
  3281 00003EEC 6683FE0A            <1>         cmp     si, 10
  3282                              <1> 		; cmp r1,$10. / 10. files is the maximum number which
  3283                              <1> 			  ; / can be opened
  3284 00003EF0 72E4                <1> 	jb	short sysfork_4	
  3285                              <1> 		; blt 1b / check next entry
  3286 00003EF2 E9D3FCFFFF          <1> 	jmp	sysret
  3287                              <1> 		; br sysret1
  3288                              <1> 
  3289                              <1> sysread: ; < read from file >
  3290                              <1> 	; 24/12/2021
  3291                              <1> 	; 04/12/2021 (Retro UNIX 386 v1.2)
  3292                              <1> 	; 27/03/2020
  3293                              <1> 	; 26/03/2020 - Retro UNIX 386 v2
  3294                              <1> 	; 13/05/2015
  3295                              <1> 	; 11/05/2015 (Retro UNIX 386 v1 - Beginning)
  3296                              <1> 	; 23/05/2013 (Retro UNIX 8086 v1)
  3297                              <1> 	;
  3298                              <1> 	; 'sysread' is given a buffer to read into and the number of
  3299                              <1> 	; characters to be read. If finds the file from the file
  3300                              <1> 	; descriptor located in *u.r0 (r0). This file descriptor
  3301                              <1> 	; is returned from a successful open call (sysopen).
  3302                              <1> 	; The i-number of file is obtained via 'rw1' and the data
  3303                              <1> 	; is read into core via 'readi'.
  3304                              <1> 	;
  3305                              <1> 	; Calling sequence:
  3306                              <1> 	;	sysread; buffer; nchars
  3307                              <1> 	; Arguments:
  3308                              <1> 	;	buffer - location of contiguous bytes where 
  3309                              <1> 	;		 input will be placed.
  3310                              <1> 	;	nchars - number of bytes or characters to be read.
  3311                              <1> 	; Inputs: *u.r0 - file descriptor (& arguments)
  3312                              <1> 	; Outputs: *u.r0 - number of bytes read.	
  3313                              <1> 	; ...............................................................
  3314                              <1> 	;				
  3315                              <1> 	; Retro UNIX 8086 v1 modification: 
  3316                              <1> 	;       'sysread' system call has three arguments; so,
  3317                              <1> 	;	* 1st argument, file descriptor is in BX register
  3318                              <1> 	;	* 2nd argument, buffer address/offset in CX register
  3319                              <1> 	;	* 3rd argument, number of bytes is in DX register
  3320                              <1> 	;
  3321                              <1> 	;	AX register (will be restored via 'u.r0') will return
  3322                              <1> 	;	to the user with number of bytes read. 
  3323                              <1> 	;
  3324                              <1> 
  3325 00003EF7 E839000000          <1> 	call	rw1
  3326                              <1> 	;jc	error ; 13/05/2015, ax < 1
  3327                              <1> 	;	; jsr r0,rw1 / get i-number of file to be read into r1
  3328                              <1>        	; 04/12/2021
  3329 00003EFC 7209                <1> 	jc	short sysread_err
  3330 00003EFE 7529                <1> 	jnz	short rw3 ; error ! (permission denied)
  3331                              <1> 	;		; read a file while it is open for write!
  3332                              <1> 
  3333                              <1> 	; eax = inode number
  3334                              <1> 
  3335                              <1> 	; 04/12/2021
  3336                              <1> 	;((Retro Unix 386 v1 code)) (old code below)
  3337                              <1> 	;test	ah, 80h
  3338                              <1> 	;	; tst r1 / negative i-number?
  3339                              <1> 	;;jnz	error
  3340                              <1> 	;;	; ble error1 / yes, error 1 to read
  3341                              <1> 	;;		   ; / it should be positive
  3342                              <1> 	;; 04/12/2021
  3343                              <1> 	;jnz	short sysread_err
  3344                              <1> 
  3345 00003F00 E8FC180000          <1> 	call	readi
  3346                              <1> 		; jsr r0,readi / read data into core
  3347 00003F05 EB13                <1> 	jmp	short rw0
  3348                              <1> 		; br 1f
  3349                              <1> sysread_err:
  3350                              <1> syswrite_err:
  3351                              <1> 	; 04/12/2021
  3352 00003F07 E99EFCFFFF          <1> 	jmp	error
  3353                              <1> 
  3354                              <1> syswrite: ; < write to file >
  3355                              <1> 	; 24/12/2021
  3356                              <1> 	; 04/12/2021 (Retro UNIX 386 v1.2)
  3357                              <1> 	; 27/03/2020
  3358                              <1> 	; 26/03/2020 - Retro UNIX 386 v2
  3359                              <1> 	; 13/05/2015
  3360                              <1> 	; 11/05/2015 (Retro UNIX 386 v1 - Beginning)
  3361                              <1> 	; 23/05/2013 (Retro UNIX 8086 v1)
  3362                              <1> 	;
  3363                              <1> 	; 'syswrite' is given a buffer to write onto an output file
  3364                              <1> 	; and the number of characters to write. If finds the file
  3365                              <1> 	; from the file descriptor located in *u.r0 (r0). This file 
  3366                              <1> 	; descriptor is returned from a successful open or create call
  3367                              <1> 	; (sysopen or syscreat). The i-number of file is obtained via
  3368                              <1> 	; 'rw1' and buffer is written on the output file via 'write'.
  3369                              <1> 	;
  3370                              <1> 	; Calling sequence:
  3371                              <1> 	;	syswrite; buffer; nchars
  3372                              <1> 	; Arguments:
  3373                              <1> 	;	buffer - location of contiguous bytes to be writtten.
  3374                              <1> 	;	nchars - number of characters to be written.
  3375                              <1> 	; Inputs: *u.r0 - file descriptor (& arguments)
  3376                              <1> 	; Outputs: *u.r0 - number of bytes written.	
  3377                              <1> 	; ...............................................................
  3378                              <1> 	;				
  3379                              <1> 	; Retro UNIX 8086 v1 modification: 
  3380                              <1> 	;       'syswrite' system call has three arguments; so,
  3381                              <1> 	;	* 1st argument, file descriptor is in BX register
  3382                              <1> 	;	* 2nd argument, buffer address/offset in CX register
  3383                              <1> 	;	* 3rd argument, number of bytes is in DX register
  3384                              <1> 	;
  3385                              <1> 	;	AX register (will be restored via 'u.r0') will return
  3386                              <1> 	;	to the user with number of bytes written. 
  3387                              <1> 	;
  3388                              <1> 
  3389 00003F0C E824000000          <1> 	call	rw1
  3390                              <1> 	;jc	error ; 13/05/2015, ax < 1
  3391                              <1> 	;	; jsr r0,rw1 / get i-number in r1 of file to write
  3392                              <1>        	; 04/12/2021
  3393 00003F11 72F4                <1> 	jc	short syswrite_err
  3394 00003F13 7414                <1> 	jz	short rw3 ; error ! (permission denied)
  3395                              <1> 			; write to a file while it is open for read
  3396                              <1> 	; eax = inode number
  3397                              <1> 	
  3398                              <1> 	; 04/12/2021
  3399                              <1> 	;((Retro Unix 386 v1 code)) (old code below)
  3400                              <1> 	;test	ah, 80h
  3401                              <1> 	;	; tst r1 / positive i-number ?
  3402                              <1>         ;jz	short rw3 ; 13/05/2015
  3403                              <1> 	;;jz	error
  3404                              <1> 	;	; bge error1 / yes, error 1 
  3405                              <1> 	;		   ; / negative i-number means write
  3406                              <1>         ;neg	ax
  3407                              <1> 	;	; neg r1 / make it positive
  3408                              <1> 	
  3409 00003F15 E8F71A0000          <1> 	call	writei
  3410                              <1>         	; jsr r0,writei / write data
  3411                              <1> rw0: ; 1:
  3412 00003F1A A1[04750000]        <1>         mov	eax, [u.nread]
  3413 00003F1F A3[D8740000]        <1> 	mov	[u.r0], eax
  3414                              <1> 		; mov u.nread,*u.r0 / put no. of bytes transferred
  3415                              <1> 				  ; / into (u.r0)
  3416 00003F24 E9A1FCFFFF          <1> 	jmp	sysret
  3417                              <1>         	; br sysret1
  3418                              <1> rw3: 
  3419                              <1> 	; 13/05/2015
  3420 00003F29 C705[4C750000]0B00- <1> 	mov	dword [u.error], ERR_FILE_ACCESS ; permission denied !
  3420 00003F31 0000                <1>
  3421                              <1> 	;stc
  3422                              <1> 	;retn
  3423                              <1> 	; 24/12/2021 - Retro UNIX 386 v1.2
  3424 00003F33 EBD2                <1> 	jmp	short syswrite_err
  3425                              <1> 
  3426                              <1> rw1:	
  3427                              <1> 	; 04/12/2021 - Retro UNIX 386 v1.2
  3428                              <1> 	; 27/03/2020
  3429                              <1> 	; 26/03/2020 - Retro UNIX 386 v2
  3430                              <1> 	; 14/05/2015
  3431                              <1> 	; 13/05/2015
  3432                              <1> 	; 11/05/2015 (Retro UNIX 386 v1 - Beginning)
  3433                              <1> 	; 23/05/2013 - 24/05/2013 (Retro UNIX 8086 v1)
  3434                              <1> 	; System call registers: bx, cx, dx (through 'sysenter')
  3435                              <1> 	;
  3436                              <1> 	;mov	[u.base], ecx 	; buffer address/offset 
  3437                              <1> 				;(in the user's virtual memory space)
  3438                              <1> 	;mov	[u.count], edx 
  3439                              <1> 		; jsr r0,arg; u.base / get buffer pointer
  3440                              <1>         	; jsr r0,arg; u.count / get no. of characters
  3441                              <1> 	;;mov	eax, ebx ; file descriptor
  3442                              <1> 		; mov *u.r0,r1 / put file descriptor 
  3443                              <1> 		             ; / (index to u.fp table) in r1
  3444                              <1> 	; 13/05/2015
  3445 00003F35 C705[D8740000]0000- <1> 	mov	dword [u.r0], 0 ; r/w transfer count = 0 (reset)
  3445 00003F3D 0000                <1>
  3446                              <1> 	;
  3447                              <1> 	;; call	getf
  3448                              <1>         ; ebx = File descriptor
  3449 00003F3F E8720A0000          <1> 	call	getf1 ; calling point in 'getf' from 'rw1'
  3450                              <1> 		; jsr r0,getf / get i-number of the file in r1
  3451                              <1> 	; AX = I-number of the file ; negative i-number means write
  3452                              <1> 
  3453                              <1> 	; 04/12/2021 - Retro UNIX 386 v1.2
  3454                              <1> 	; 26/03/2020 - Retro UNIX 386 v2
  3455                              <1> 	; BL = open mode & status flag (0 = read, 1 = write)
  3456                              <1> 	; eax = inode number (ax)
  3457                              <1> 	; 27/03/2020
  3458                              <1> 	; [cdev] = logical drive number 	
  3459                              <1> 
  3460                              <1> 	; 13/05/2015
  3461 00003F44 6683F801            <1> 	cmp 	ax, 1
  3462 00003F48 721A                <1> 	jb	short rw2
  3463                              <1> 	;
  3464 00003F4A 890D[FC740000]      <1> 	mov	[u.base], ecx 	; buffer address/offset 
  3465                              <1> 				;(in the user's virtual memory space)
  3466 00003F50 8915[00750000]      <1> 	mov	[u.count], edx 
  3467                              <1> 	; 14/05/2015
  3468 00003F56 C705[4C750000]0000- <1>         mov     dword [u.error], 0 ; reset the last error code
  3468 00003F5E 0000                <1>
  3469                              <1> 
  3470                              <1> 	; 04/12/2021 - Retro UNIX 386 v1.2
  3471                              <1> 	; 27/03/2020 - Retro UNIX 386 v2
  3472 00003F60 80E301              <1> 	and	bl, 1
  3473                              <1> 		; zf = 0 -> open for write	
  3474                              <1> 		; zf = 1 -> open for read
  3475 00003F63 C3                  <1> 	retn
  3476                              <1>         	; rts r0
  3477                              <1> rw2:
  3478                              <1> 	; 13/05/2015
  3479 00003F64 C705[4C750000]0A00- <1> 	mov	dword [u.error], ERR_FILE_NOT_OPEN ; file not open !
  3479 00003F6C 0000                <1>
  3480 00003F6E C3                  <1> 	retn
  3481                              <1> 
  3482                              <1> 	; 08/01/2022
  3483                              <1> 	; 02/01/2022
  3484                              <1> 	; 01/01/2022
  3485                              <1> 	; 27/12/2021
  3486                              <1> 	; 22/11/2021 - Retro UNIX 386 v2 fs compatibility code
  3487                              <1> sysopen: ;<open file>
  3488                              <1> 	; 04/12/2021
  3489                              <1> 	; 07/08/2020 (Retro UNIX 386 v2)
  3490                              <1> 	; 14/05/2015 (Retro UNIX 386 v1 - Beginning)
  3491                              <1> 	; 22/05/2013 - 27/05/2013 (Retro UNIX 8086 v1)
  3492                              <1> 	;
  3493                              <1> 	; 'sysopen' opens a file in following manner:
  3494                              <1> 	;    1) The second argument in a sysopen says whether to
  3495                              <1> 	;	open the file ro read (0) or write (>0).
  3496                              <1> 	;    2) I-node of the particular file is obtained via 'namei'.
  3497                              <1> 	;    3) The file is opened by 'iopen'.
  3498                              <1> 	;    4) Next housekeeping is performed on the fsp table
  3499                              <1> 	;	and the user's open file list - u.fp.
  3500                              <1> 	;	a) u.fp and fsp are scanned for the next available slot.
  3501                              <1> 	;	b) An entry for the file is created in the fsp table.
  3502                              <1> 	;	c) The number of this entry is put on u.fp list.
  3503                              <1> 	;	d) The file descriptor index to u.fp list is pointed
  3504                              <1> 	;	   to by u.r0.
  3505                              <1> 	;
  3506                              <1> 	; Calling sequence:
  3507                              <1> 	;	sysopen; name; mode
  3508                              <1> 	; Arguments:
  3509                              <1> 	;	name - file name or path name
  3510                              <1> 	;	mode - 0 to open for reading
  3511                              <1> 	;	       1 to open for writing
  3512                              <1> 	; Inputs: (arguments)
  3513                              <1> 	; Outputs: *u.r0 - index to u.fp list (the file descriptor)
  3514                              <1> 	;		  is put into r0's location on the stack.	
  3515                              <1> 	; ...............................................................
  3516                              <1> 	;				
  3517                              <1> 	; Retro UNIX 8086 v1 modification: 
  3518                              <1> 	;       'sysopen' system call has two arguments; so,
  3519                              <1> 	;	* 1st argument, name is pointed to by BX register
  3520                              <1> 	;	* 2nd argument, mode is in CX register
  3521                              <1> 	;
  3522                              <1> 	;	AX register (will be restored via 'u.r0') will return
  3523                              <1> 	;	to the user with the file descriptor/number 
  3524                              <1> 	;	(index to u.fp list).
  3525                              <1> 	;
  3526                              <1> 	;call	arg2
  3527                              <1> 	; * name - 'u.namep' points to address of file/path name
  3528                              <1> 	;          in the user's program segment ('u.segmnt')
  3529                              <1> 	;          with offset in BX register (as sysopen argument 1).
  3530                              <1> 	; * mode - sysopen argument 2 is in CX register 
  3531                              <1> 	;          which is on top of stack.
  3532                              <1> 	;
  3533                              <1> 	; jsr r0,arg2 / get sys args into u.namep and on stack
  3534                              <1> 	;
  3535                              <1>        	; system call registers: ebx, ecx (through 'sysenter')
  3536                              <1> 
  3537 00003F6F 891D[F4740000]      <1> 	mov	[u.namep], ebx
  3538 00003F75 51                  <1> 	push	ecx ; * ; 04/12/2021 (cx -> ecx)
  3539 00003F76 E8710A0000          <1> 	call	namei
  3540                              <1> 		; jsr r0,namei / i-number of file in r1
  3541                              <1>      	;and	ax, ax
  3542                              <1> 	;jz	error ; File not found
  3543 00003F7B 722B                <1> 	jc	short fnotfound ; 14/05/2015
  3544                              <1> 	;jc	error ; 27/05/2013
  3545                              <1> 		; br  error2 / file not found
  3546                              <1>    	;pop	edx ; * ; mode ; 04/12/2021
  3547                              <1> 	;push	edx ; *
  3548                              <1> 	;;or	dx, dx
  3549                              <1> 	;or	dl, dl
  3550                              <1> 	;	; tst (sp) / is mode = 0 (2nd arg of call;
  3551                              <1> 	;	         ; / 0 means, open for read)
  3552                              <1> 	;;jz	short sysopen_0
  3553                              <1> 	;;	; beq 1f / yes, leave i-number positive
  3554                              <1> 	
  3555 00003F7D 8B1424              <1> 	mov	edx, [esp] ; *
  3556                              <1> 	; edx = open mode (0 or 1)
  3557                              <1> 
  3558                              <1> ;syscreat_0: ;op0: ; 27/12/2015
  3559                              <1> ;	neg	ax
  3560                              <1> ;        	; neg r1 / open for writing so make i-number negative
  3561                              <1> 
  3562                              <1> sysopen_0: ;1:
  3563 00003F80 E8461E0000          <1> 	call	iopen
  3564                              <1> 		; jsr r0,iopen / open file whose i-number is in r1
  3565 00003F85 5A                  <1> 	pop	edx ; * ; mode ; 04/12/2021
  3566                              <1> 	;;and	dx, dx
  3567                              <1> 	;and	dl, dl
  3568                              <1>         ;	; tst (sp)+ / pop the stack and test the mode
  3569                              <1> 	;jz	short sysopen_2
  3570                              <1>         ;	; beq op1 / is open for read op1
  3571                              <1> ;sysopen_1: ;op0:
  3572                              <1> 	;neg	ax
  3573                              <1>         ;	; neg r1 
  3574                              <1> 	;	     ;/ make i-number positive if open for writing [???]
  3575                              <1> 
  3576                              <1> 	;; NOTE: iopen always make i-number positive.
  3577                              <1> 	;; Here i-number becomes negative again. [22/05/2013]
  3578                              <1> sysopen_1: ; 04/12/2021
  3579                              <1> sysopen_2: ;op1:
  3580 00003F86 31F6                <1>         xor     esi, esi
  3581                              <1>         	; clr r2 / clear registers
  3582 00003F88 31DB                <1>         xor     ebx, ebx
  3583                              <1> 		; clr r3
  3584                              <1> sysopen_3: ;1: / scan the list of entries in fsp table
  3585 00003F8A 389E[E2740000]      <1>         cmp     [esi+u.fp], bl ; 0
  3586                              <1> 		; tstb u.fp(r2) / test the entry in the u.fp list
  3587 00003F90 7625                <1> 	jna	short sysopen_4
  3588                              <1> 		; beq 1f / if byte in list is 0 branch
  3589 00003F92 46                  <1>         inc     esi
  3590                              <1> 		; inc r2 / bump r2 so next byte can be checked
  3591                              <1> 	; 02/01/2022
  3592                              <1> 	;cmp	si, OPENFILES ; 04/12/2021
  3593 00003F93 6683FE0A            <1> 	cmp	si, 10
  3594                              <1> 		; cmp r2,$10. / reached end of list?
  3595 00003F97 72F1                <1> 	jb	short sysopen_3
  3596                              <1> 		; blt 1b / no, go back
  3597                              <1> toomanyf:
  3598                              <1> 	; 14/05/2015
  3599 00003F99 C705[4C750000]0D00- <1> 	mov	dword [u.error], ERR_TOO_MANY_FILES ; too many open files !
  3599 00003FA1 0000                <1>
  3600 00003FA3 E902FCFFFF          <1> 	jmp	error
  3601                              <1>         	; br error2 / yes, error (no files open)
  3602                              <1> fnotfound: 
  3603                              <1> 	; 14/05/2015
  3604 00003FA8 C705[4C750000]0C00- <1> 	mov	dword [u.error], ERR_FILE_NOT_FOUND ; file not found !
  3604 00003FB0 0000                <1>
  3605 00003FB2 E9F3FBFFFF          <1> 	jmp	error
  3606                              <1> 
  3607                              <1> sysopen_4: ; 1:
  3608 00003FB7 6683BB[74710000]00  <1>         cmp     word [ebx+fsp], 0
  3609                              <1> 		; tst fsp(r3) / scan fsp entries
  3610 00003FBF 760D                <1>         jna     short sysopen_5
  3611                              <1> 		; beq 1f / if 0 branch
  3612                              <1> 	;; 14/05/2015 - Retro UNIX 386 v1 modification !
  3613                              <1> 	;add	bx, 10 ; fsp structure size = 10 bytes/entry
  3614                              <1> 		; add $8.,r3 / add 8 to r3 
  3615                              <1> 			; / to bump it to next entry mfsp table
  3616                              <1> 	; 01/01/2022
  3617                              <1> 	; 07/08/2020 - Retro UNIX 386 v2
  3618 00003FC1 6683C310            <1> 	add	bx, 16 ; fsp structure size = 16 bytes/entry ; runix v2
  3619                              <1> 	; 27/12/2021 - Retro UNIX 386 v1.2 (runix v2 fs)
  3620                              <1> 	;add	bx, fp.size
  3621                              <1> 
  3622                              <1> 	; 22/11/2021
  3623                              <1> 	;cmp	bx, NFILES*fp.size ; NFILES*16
  3624                              <1> 	; 02/01/2022
  3625 00003FC5 6681FB2003          <1> 	cmp	bx, nfiles*16
  3626                              <1> 	; 01/01/2022
  3627                              <1> 	;cmp	bx, NFILES*16 ; NFILES*fp.size
  3628                              <1> 	;;cmp	bx, nfiles*10
  3629                              <1> 		; cmp r3,$[nfiles*8.] / done scanning
  3630 00003FCA 72EB                <1> 	jb	short sysopen_4
  3631                              <1>        		; blt 1b / no, back
  3632                              <1> 	;jmp	error
  3633                              <1>         ;	; br error2 / yes, error
  3634                              <1> 	; 04/12/2021
  3635                              <1> 	; 07/08/2020
  3636 00003FCC EBCB                <1> 	jmp	short toomanyf
  3637                              <1> 
  3638                              <1> sysopen_5: ; 1: / r2 has index to u.fp list; r3, has index to fsp table
  3639 00003FCE 668983[74710000]    <1>         mov     [ebx+fsp], ax
  3640                              <1> 		; mov r1,fsp(r3) / put i-number of open file 
  3641                              <1> 			; / into next available entry in fsp table,
  3642                              <1> 	;mov	di, [cdev] ; word ? byte ?
  3643                              <1>         ;mov	[ebx+fsp+2], di ; device number
  3644                              <1> 	;	; mov cdev,fsp+2(r3) / put # of device in next word
  3645                              <1> 
  3646                              <1> 	; 04/12/2021        
  3647 00003FD5 A0[B9740000]        <1> 	mov	al, [cdev]
  3648 00003FDA 88D4                <1> 	mov	ah, dl ; open mode, 0 = read, 1 = write
  3649 00003FDC 668983[78710000]    <1> 	mov	[ebx+fsp+4], ax ; device number & open mode
  3650                              <1> 
  3651                              <1> 	;xor	edi, edi
  3652                              <1>         ;mov	[ebx+fsp+4], edi ; offset pointer (0)
  3653                              <1> 	;	; clr fsp+4(r3)
  3654                              <1>         ;mov	[ebx+fsp+8], di ; open count (0), deleted flag (0)
  3655                              <1>        	;	; clr fsp+6(r3) / clear the next two words
  3656                              <1> 
  3657                              <1> 	; 04/12/2021 
  3658 00003FE3 31C0                <1> 	xor	eax, eax
  3659 00003FE5 8983[7C710000]      <1>   	mov	[ebx+fsp+8], eax ; offset pointer = 0
  3660                              <1> 	;inc	word [ebx+fsp+6]
  3661 00003FEB FE83[7A710000]      <1> 	inc	byte [ebx+fsp+6] ; open count = open count + 1	
  3662                              <1> 
  3663                              <1>   	;mov	eax, ebx
  3664                              <1> 	;mov	bl, 10
  3665                              <1> 	;div	bl 
  3666                              <1> 	;	; asr r3
  3667                              <1> 	;	; asr r3 / divide by 8 
  3668                              <1> 	;	; asr r3 ; / to get number of the fsp entry-1
  3669                              <1> 	;inc	al
  3670                              <1>         ;	; inc r3 / add 1 to get fsp entry number
  3671                              <1>         ;mov	[esi+u.fp], al
  3672                              <1> 	;	; movb r3,u.fp(r2) / move entry number into 
  3673                              <1> 			; / next available slot in u.fp list
  3674                              <1> 	; 04/12/2021
  3675 00003FF1 C1EB04              <1> 	shr	ebx, 4	; / 16
  3676                              <1> 	;shr	bx, 4	; bx = fsp entry number (index)	
  3677                              <1> 			; bx <= 49 for current runix 386 version
  3678 00003FF4 FEC3                <1> 	inc 	bl	; bl = 1 to 50
  3679 00003FF6 889E[E2740000]      <1> 	mov	[esi+u.fp], bl 	
  3680                              <1> 
  3681 00003FFC 8935[D8740000]      <1>         mov     [u.r0], esi
  3682                              <1> 		; mov r2,*u.r0 / move index to u.fp list 
  3683                              <1> 			     ; / into r0 loc on stack
  3684 00004002 E9C3FBFFFF          <1>         jmp	sysret
  3685                              <1> 		; br sysret2
  3686                              <1> 
  3687                              <1> ; 27/03/2020 - Retro UNIX 386 v2 - FSP (OPEN FILES) TABLE 
  3688                              <1> ;
  3689                              <1> ;         15                    7                   0
  3690                              <1> ;  1     |-------------------------------------------|
  3691                              <1> ;        |   	     i-number of open file           |
  3692                              <1> ;        |-------------------------------------------| 
  3693                              <1> ;        |        high word of 32 bit i-number       |
  3694                              <1> ;        |-------------------------------------------|
  3695                              <1> ;        | open mode & status  |   device number     |
  3696                              <1> ;        |-------------------------------------------|			
  3697                              <1> ;        |    reserved byte    |     open count      |
  3698                              <1> ;        |-------------------------------------------| 
  3699                              <1> ;        | offset pointer, i.e., r/w pointer to file |
  3700                              <1> ;        |-------------------------------------------|
  3701                              <1> ;        |   64 bit file offset pointer (bit 16-31)  | 
  3702                              <1> ;        |-------------------------------------------|
  3703                              <1> ;        |   64 bit file offset pointer (bit 32-47)  | 
  3704                              <1> ;        |-------------------------------------------|
  3705                              <1> ;        |   64 bit file offset pointer (bit 48-63)  | 
  3706                              <1> ;        |-------------------------------------------|
  3707                              <1> ;  2     |                                           |
  3708                              <1> ;        |-------------------------------------------| 
  3709                              <1> ;        |                                           |
  3710                              <1> ;        |-------------------------------------------|
  3711                              <1> ;        |                                           |
  3712                              <1> ;        |-------------------------------------------|
  3713                              <1> ;        |                                           |
  3714                              <1> ;        |-------------------------------------------| 
  3715                              <1> ;        |                                           | 
  3716                              <1> 
  3717                              <1> 	;
  3718                              <1> 	; 'fsp' table (10 bytes/entry)
  3719                              <1> 	; bit 15				   bit 0
  3720                              <1> 	; ---|-------------------------------------------
  3721                              <1> 	; r/w|		i-number of open file
  3722                              <1> 	; ---|-------------------------------------------
  3723                              <1> 	;		   device number
  3724                              <1> 	; -----------------------------------------------
  3725                              <1> 	; offset pointer, r/w pointer to file (bit 0-15)
  3726                              <1> 	; -----------------------------------------------
  3727                              <1> 	; offset pointer, r/w pointer to file (bit 16-31)
  3728                              <1> 	; ----------------------|------------------------
  3729                              <1> 	;  flag that says file 	| number of processes
  3730                              <1> 	;   has been deleted	| that have file open 
  3731                              <1> 	; ----------------------|------------------------
  3732                              <1> 	;
  3733                              <1> 
  3734                              <1> 	; 01/01/2022
  3735                              <1> 	; 04/12/2021 - Retro UNIX 386 v2 fs compatibility code
  3736                              <1> syscreat: ; < create file >
  3737                              <1> 	; 04/12/2021 (Retro UNIX 386 v1.2)
  3738                              <1> 	; 04/04/2021
  3739                              <1> 	; 27/03/2021 (Retro UNIX 386 v2 - Beginning)
  3740                              <1> 	; 27/12/2015 (Retro UNIX 386 v1.1)
  3741                              <1> 	; 14/05/2015 (Retro UNIX 386 v1 - Beginning)
  3742                              <1> 	; 27/05/2013 (Retro UNIX 8086 v1)
  3743                              <1> 	;
  3744                              <1> 	; 'syscreat' called with two arguments; name and mode.
  3745                              <1> 	; u.namep points to name of the file and mode is put
  3746                              <1> 	; on the stack. 'namei' is called to get i-number of the file.		
  3747                              <1> 	; If the file aready exists, it's mode and owner remain 
  3748                              <1> 	; unchanged, but it is truncated to zero length. If the file
  3749                              <1> 	; did not exist, an i-node is created with the new mode via
  3750                              <1> 	; 'maknod' whether or not the file already existed, it is
  3751                              <1> 	; open for writing. The fsp table is then searched for a free
  3752                              <1> 	; entry. When a free entry is found, proper data is placed
  3753                              <1> 	; in it and the number of this entry is put in the u.fp list.
  3754                              <1> 	; The index to the u.fp (also know as the file descriptor)
  3755                              <1> 	; is put in the user's r0. 			
  3756                              <1> 	;
  3757                              <1> 	; Calling sequence:
  3758                              <1> 	;	syscreate; name; mode
  3759                              <1> 	; Arguments:
  3760                              <1> 	;	name - name of the file to be created
  3761                              <1> 	;	mode - mode of the file to be created
  3762                              <1> 	; Inputs: (arguments)
  3763                              <1> 	; Outputs: *u.r0 - index to u.fp list 
  3764                              <1> 	;		   (the file descriptor of new file)
  3765                              <1> 	; ...............................................................
  3766                              <1> 	;				
  3767                              <1> 	; Retro UNIX 8086 v1 modification: 
  3768                              <1> 	;       'syscreate' system call has two arguments; so,
  3769                              <1> 	;	* 1st argument, name is pointed to by BX register
  3770                              <1> 	;	* 2nd argument, mode is in CX register
  3771                              <1> 	;
  3772                              <1> 	;	AX register (will be restored via 'u.r0') will return
  3773                              <1> 	;	to the user with the file descriptor/number 
  3774                              <1> 	;	(index to u.fp list).
  3775                              <1> 	;
  3776                              <1> 	;call	arg2
  3777                              <1> 	; * name - 'u.namep' points to address of file/path name
  3778                              <1> 	;          in the user's program segment ('u.segmnt')
  3779                              <1> 	;          with offset in BX register (as sysopen argument 1).
  3780                              <1> 	; * mode - sysopen argument 2 is in CX register 
  3781                              <1> 	;          which is on top of stack.
  3782                              <1> 	;
  3783                              <1>         	; jsr r0,arg2 / put file name in u.namep put mode 
  3784                              <1> 			    ; / on stack
  3785 00004007 891D[F4740000]      <1> 	mov	[u.namep], ebx ; file name address
  3786                              <1> 	;push	cx ; mode
  3787                              <1> 	; 04/12/2021
  3788 0000400D 51                  <1> 	push	ecx ; cx = mode (permission flags)
  3789 0000400E E8D9090000          <1> 	call 	namei        	
  3790                              <1> 		; jsr r0,namei / get the i-number
  3791                              <1>         ;and	ax, ax
  3792                              <1> 	;jz	short syscreat_1	       	
  3793 00004013 7226                <1> 	jc	short syscreat_1
  3794                              <1> 		; br  2f / if file doesn't exist 2f
  3795                              <1> 	; 27/12/2015
  3796                              <1> 	;cmp	ax, 41 ; device inode ?
  3797                              <1>         ;jb	syscreat_0 ; yes
  3798                              <1> 	;
  3799                              <1> 	;neg 	ax
  3800                              <1>         ;	; neg r1 / if file already exists make i-number 
  3801                              <1> 		       ; / negative (open for writing)
  3802                              <1> 	; 01/01/2021
  3803                              <1> 	; 27/03/2021
  3804 00004015 F605[75700000]80    <1> 	test	byte [i.flgs+1], 80h ; regular file
  3805 0000401C 7431                <1> 	jz	short f_create_error ; device inode !!!
  3806 0000401E F605[75700000]40    <1> 	test	byte [i.flgs+1], 40h ; directory
  3807 00004025 7528                <1> 	jnz	short f_create_error ; directory inode !!!
  3808                              <1> 
  3809                              <1> 	; Truncate existing file
  3810                              <1> ;syscreate_2:
  3811                              <1> 	; 27/03/2021
  3812                              <1> 	;xor	edx, edx ; 0
  3813                              <1> 	;inc	dl ; DL = 1 ; create/truncate (open for write)
  3814 00004027 B201                <1> 	mov	dl, 1
  3815                              <1> 	;
  3816 00004029 E89D1D0000          <1> 	call	iopen
  3817                              <1>         	; jsr r0,iopen /
  3818 0000402E E83D160000          <1> 	call	itrunc
  3819                              <1>         	; jsr r0,itrunc / truncate to 0 length
  3820                              <1> 	;pop	cx ; pop mode (did not exist in original Unix v1 !?)
  3821                              <1> 	; 04/12/2021
  3822 00004033 59                  <1> 	pop	ecx 
  3823                              <1> syscreat_2:
  3824 00004034 B201                <1> 	mov	dl, 1 ; open for writing
  3825 00004036 E94BFFFFFF          <1>         jmp     sysopen_1
  3826                              <1>         	; br op0
  3827                              <1> syscreat_1: ; 2: / file doesn't exist
  3828                              <1> 	;pop	ax
  3829                              <1>         ;	; mov (sp)+,r1 / put the mode in r1
  3830                              <1> 	; 27/03/2021
  3831 0000403B 58                  <1> 	pop	eax  ; ax = mode (permission flags)
  3832                              <1> 	;xor	ah, ah	
  3833                              <1>         ;	; bic $!377,r1 / clear upper byte
  3834                              <1> 	; 27/03/2021
  3835                              <1> 	;(ref: Retro UNIX 386 v2 inode flags,'ux.s')
  3836                              <1> 	; clear bits 15,14,13,12,9 of mode (input from user)
  3837 0000403C 80E40D              <1> 	and	ah, 0Dh ; ISUID (800h) & ISGID (400h) & IREAD (100h)
  3838 0000403F 80CC8D              <1> 	or	ah, 8Dh	; IFREG (8000h) ; Regular file
  3839 00004042 E8AF0C0000          <1> 	call 	maknod
  3840                              <1>         	; jsr r0,maknod / make an i-node for this file
  3841                              <1> 	;sub	eax, eax ; 04/12/2021
  3842 00004047 66A1[10750000]      <1> 	mov	ax, [u.dirbuf]
  3843                              <1>         	; mov u.dirbuf,r1 / put i-number 
  3844                              <1> 			        ; / for this new file in r1
  3845                              <1> 	; 04/04/2021
  3846                              <1> 	;mov	dl, 1 ; open for writing
  3847                              <1>         ;
  3848                              <1> 	;jmp	sysopen_1
  3849                              <1>         ;	; br op0 / open the file
  3850                              <1> 	; 04/04/2021
  3851 0000404D EBE5                <1> 	jmp	short syscreat_2
  3852                              <1> 
  3853                              <1> 	; 04/12/2021
  3854                              <1> dir_access_err:
  3855                              <1> 	; 14/05/2015
  3856                              <1> 	;mov	dword [u.error], ERR_DIR_ACCESS ; permission denied !
  3857                              <1> f_create_error:
  3858                              <1> 	; 27/03/2021
  3859 0000404F C705[4C750000]0B00- <1> 	mov	dword [u.error], ERR_PERM_DENIED ; permission denided !	
  3859 00004057 0000                <1>
  3860 00004059 E94CFBFFFF          <1> 	jmp	error
  3861                              <1> 
  3862                              <1> 	; 04/12/2021 - Retro UNIX 386 v2 fs compatibility code
  3863                              <1> sysmkdir: ; < make directory >
  3864                              <1> 	; 04/12/2021 (Retro UNIX 386 v1.2)
  3865                              <1> 	; 02/04/2021
  3866                              <1> 	; 27/03/2021 (Retro UNIX 386 v2 - Beginning)
  3867                              <1> 	; 14/05/2015 (Retro UNIX 386 v1 - Beginning)
  3868                              <1> 	; 27/05/2013 - 02/08/2013 (Retro UNIX 8086 v1)
  3869                              <1> 	;
  3870                              <1> 	; 'sysmkdir' creates an empty directory whose name is
  3871                              <1> 	; pointed to by arg 1. The mode of the directory is arg 2.	
  3872                              <1> 	; The special entries '.' and '..' are not present.
  3873                              <1> 	; Errors are indicated if the directory already exists or		
  3874                              <1> 	; user is not the super user. 
  3875                              <1> 	;
  3876                              <1> 	; Calling sequence:
  3877                              <1> 	;	sysmkdir; name; mode
  3878                              <1> 	; Arguments:
  3879                              <1> 	;	name - points to the name of the directory
  3880                              <1> 	;	mode - mode of the directory
  3881                              <1> 	; Inputs: (arguments)
  3882                              <1> 	; Outputs: -
  3883                              <1> 	;    (sets 'directory' flag to 1; 
  3884                              <1> 	;    'set user id on execution' and 'executable' flags to 0)
  3885                              <1> 	; ...............................................................
  3886                              <1> 	;				
  3887                              <1> 	; Retro UNIX 8086 v1 modification: 
  3888                              <1> 	;       'sysmkdir' system call has two arguments; so,
  3889                              <1> 	;	* 1st argument, name is pointed to by BX register
  3890                              <1> 	;	* 2nd argument, mode is in CX register
  3891                              <1> 	;
  3892                              <1> 		
  3893                              <1> ; / make a directory
  3894                              <1> 
  3895                              <1> 	;call	arg2
  3896                              <1> 	; * name - 'u.namep' points to address of file/path name
  3897                              <1> 	;          in the user's program segment ('u.segmnt')
  3898                              <1> 	;          with offset in BX register (as sysopen argument 1).
  3899                              <1> 	; * mode - sysopen argument 2 is in CX register 
  3900                              <1> 	;          which is on top of stack.
  3901                              <1> 
  3902                              <1> 		; jsr r0,arg2 / put file name in u.namep put mode 
  3903                              <1> 			    ; / on stack
  3904 0000405E 891D[F4740000]      <1> 	mov	[u.namep], ebx
  3905                              <1> 	;push	cx ; mode
  3906                              <1> 	; 27/03/2021
  3907 00004064 51                  <1> 	push	ecx ; cx = mode
  3908 00004065 E882090000          <1> 	call	namei
  3909                              <1>         	; jsr r0,namei / get the i-number
  3910                              <1>         	;     br .+4 / if file not found branch around error
  3911                              <1>         ;xor 	ax, ax
  3912                              <1> 	;jnz	error
  3913 0000406A 7312                <1> 	jnc	short dir_exists ; 14/05/2015
  3914                              <1> 	;jnc	error	
  3915                              <1> 		; br  error2 / directory already exists (error)
  3916                              <1> 
  3917                              <1> 	;cmp	byte [u.uid], 0 ; 02/08/2013
  3918                              <1>         ;	;tstb u.uid / is user the super user
  3919                              <1> 	;jna	short dir_access_err ; 14/05/2015
  3920                              <1> 	;;jna	error
  3921                              <1>         ;	;bne error2 / no, not allowed
  3922                              <1> 	;pop	ax
  3923                              <1>         ;	;mov (sp)+,r1 / put the mode in r1
  3924                              <1> 	;and	ax, 0FFCFh ; 1111111111001111b
  3925                              <1>         ;	;bic $!317,r1 / all but su and ex
  3926                              <1> 	;;or	ax, 4000h ; 1011111111111111b
  3927                              <1> 	;or	ah, 40h ; Set bit 14 to 1
  3928                              <1>         ; 	;bis $40000,r1 / directory flag
  3929                              <1> 
  3930                              <1> 	; 02/04/2021
  3931                              <1> 	;cmp	word [u.uid], 0
  3932                              <1> 	;ja	error
  3933                              <1> 
  3934                              <1> 	; 02/04/2021
  3935                              <1> 	; ('mkdir' procedure will be called from 'maknod'
  3936                              <1> 	; and then 'mkdir' will check write access permission) 
  3937                              <1> 	; ((so, 'access_w' call is not needed here.))
  3938                              <1> 
  3939                              <1> 	;; 02/04/2021
  3940                              <1> 	;; ('make directory' user permission check)
  3941                              <1> 	;; ((current directory's write permission flags
  3942                              <1> 	;;  will be checked against user's 'uid' & gid'))
  3943                              <1> 	;mov	dx, 80h ; IWRITE
  3944                              <1> 	;call	access_w  ; (in 'access', 'u5.s')
  3945                              <1> 	;; (If cpu will return here, the user has write permission)
  3946                              <1> 		
  3947                              <1> 	; 27/03/2021
  3948 0000406C 58                  <1> 	pop	eax  ; ax = mode
  3949                              <1> 	; [ii] = current directory's inode number
  3950                              <1> 
  3951                              <1> 	; 27/03/2021
  3952                              <1> 	;(ref: Retro UNIX 386 v2 inode flags,'ux.s')
  3953                              <1> 	; clear bits 13,12,11,10,9,6,3,0 of mode (input from user)
  3954                              <1> 	;and	ax, 0C1B6h  ; bits 15,14,8,7,5,4,2,1
  3955 0000406D 6625B601            <1> 	and	ax, 01B6h
  3956 00004071 80CCC0              <1> 	or	ah, 0C0h ; IFREG (8000h) + IFDIR (4000h) 
  3957                              <1> 			 ; Directory
  3958 00004074 E87D0C0000          <1> 	call	maknod
  3959                              <1>         	;jsr r0,maknod / make the i-node for the directory
  3960 00004079 E94CFBFFFF          <1> 	jmp	sysret
  3961                              <1>         	;br sysret2 /
  3962                              <1> dir_exists:
  3963                              <1> 	; 27/03/2021
  3964                              <1> 	; (same error number for files and directories)
  3965                              <1> 	; Error Number: 14 (ERR_DIR_EXISTS)
  3966 0000407E C705[4C750000]0E00- <1> 	mov	dword [u.error], ERR_FILE_EXISTS
  3966 00004086 0000                <1>
  3967                              <1> 				; 'file already exists !' error
  3968                              <1> 	; 04/12/2021
  3969 00004088 E91DFBFFFF          <1> 	jmp	error
  3970                              <1> ;dir_access_err:
  3971                              <1> ;	; 14/05/2015
  3972                              <1> ;	mov	dword [u.error], ERR_DIR_ACCESS ; permission denied !
  3973                              <1> ;	jmp	error
  3974                              <1> 
  3975                              <1> 	; 04/12/2021 - Retro UNIX 386 v2 fs compatibility code
  3976                              <1> sysclose: ;<close file>
  3977                              <1> 	; 04/12/2021 (Retro UNIX 386 v1.2)
  3978                              <1> 	; 14/05/2015 (Retro UNIX 386 v1 - Beginning)
  3979                              <1> 	; 22/05/2013 - 26/05/2013 (Retro UNIX 8086 v1)
  3980                              <1> 	;
  3981                              <1> 	; 'sysclose', given a file descriptor in 'u.r0', closes the
  3982                              <1> 	; associated file. The file descriptor (index to 'u.fp' list)
  3983                              <1> 	; is put in r1 and 'fclose' is called.
  3984                              <1> 	;
  3985                              <1> 	; Calling sequence:
  3986                              <1> 	;	sysclose
  3987                              <1> 	; Arguments:
  3988                              <1> 	;	-  
  3989                              <1> 	; Inputs: *u.r0 - file descriptor
  3990                              <1> 	; Outputs: -
  3991                              <1> 	; ...............................................................
  3992                              <1> 	;				
  3993                              <1> 	; Retro UNIX 8086 v1 modification:
  3994                              <1> 	;	 The user/application program puts file descriptor
  3995                              <1> 	;        in BX register as 'sysclose' system call argument.
  3996                              <1> 	; 	 (argument transfer method 1)
  3997                              <1> 
  3998                              <1> 	; / close the file
  3999                              <1> 	
  4000                              <1> 	;mov 	eax, ebx
  4001                              <1> 	; 04/12/2021
  4002                              <1> 	; ebx = file descriptor/number
  4003 0000408D E8D8080000          <1> 	call 	fclose
  4004                              <1> 		; mov *u.r0,r1 / move index to u.fp list into r1
  4005                              <1> 		; jsr r0,fclose / close the file
  4006                              <1>                	; br error2 / unknown file descriptor
  4007                              <1> 		; br sysret2
  4008                              <1> 	;; 14/05/2015
  4009                              <1> 	;jnc	sysret
  4010                              <1> 	; 04/12/2021
  4011 00004092 7205                <1> 	jc	short sysclose_1
  4012 00004094 E931FBFFFF          <1> 	jmp	sysret
  4013                              <1> sysclose_1:
  4014 00004099 C705[4C750000]0A00- <1> 	mov	dword [u.error], ERR_FILE_NOT_OPEN ; file not open !
  4014 000040A1 0000                <1>
  4015 000040A3 E902FBFFFF          <1> 	jmp	error
  4016                              <1> 
  4017                              <1> 	; 19/12/2021
  4018                              <1> 	; 04/12/2021 - Retro UNIX 386 v2 fs compatibility code
  4019                              <1> sysemt:
  4020                              <1> 	; 04/12/2021 (Retro UNIX 386 v1.2)
  4021                              <1> 	; 14/05/2015 (Retro UNIX 386 v1 - Beginning)
  4022                              <1> 	; 10/12/2013 - 20/04/2014 (Retro UNIX 8086 v1)
  4023                              <1> 	;
  4024                              <1> 	; Retro UNIX 8086 v1 modification: 
  4025                              <1> 	;	'Enable Multi Tasking' system call instead 
  4026                              <1> 	;	of 'Emulator Trap' in original UNIX v1 for PDP-11.
  4027                              <1> 	;
  4028                              <1> 	; Retro UNIX 8086 v1 feature only!
  4029                              <1> 	;	Using purpose: Kernel will start without time-out
  4030                              <1> 	;	(internal clock/timer) functionality.
  4031                              <1> 	;	Then etc/init will enable clock/timer for
  4032                              <1> 	;	multi tasking. (Then it will not be disabled again
  4033                              <1> 	;	except hardware reset/restart.)
  4034                              <1> 	;
  4035                              <1> 	
  4036                              <1> 	;cmp	byte [u.uid], 0 ; root ?
  4037                              <1> 	;;ja	error
  4038                              <1> 	;ja	badsys ; 14/05/2015
  4039                              <1> 	; 04/12/2021
  4040 000040A8 66833D[2A750000]00  <1> 	cmp	word [u.uid], 0 ; root ?
  4041 000040B0 7605                <1> 	jna	short emt_0 
  4042 000040B2 E9A5FBFFFF          <1> 	jmp	badsys
  4043                              <1> emt_0:
  4044                              <1> 	; 19/12/2021
  4045                              <1> 	;cli
  4046 000040B7 21DB                <1> 	and	ebx, ebx
  4047 000040B9 740F                <1> 	jz	short emt_2
  4048                              <1> 	; Enable multi tasking -time sharing-
  4049 000040BB B8[29500000]        <1> 	mov	eax, clock
  4050                              <1> emt_1:
  4051 000040C0 A3[0F070000]        <1> 	mov	[x_timer], eax
  4052                              <1> 	;sti
  4053 000040C5 E900FBFFFF          <1> 	jmp	sysret
  4054                              <1> emt_2:
  4055                              <1> 	; Disable multi tasking -time sharing-
  4056 000040CA B8[17070000]        <1> 	mov	eax, u_timer
  4057 000040CF EBEF                <1> 	jmp	short emt_1
  4058                              <1> 
  4059                              <1> 	; Original UNIX v1 'sysemt' routine
  4060                              <1> ;sysemt:
  4061                              <1>         ;
  4062                              <1> 	;jsr    r0,arg; 30 / put the argument of the sysemt call 
  4063                              <1> 			 ; / in loc 30
  4064                              <1>         ;cmp    30,$core / was the argument a lower address 
  4065                              <1> 			; / than core
  4066                              <1>         ;blo    1f / yes, rtssym
  4067                              <1>         ;cmp    30,$ecore / no, was it higher than "core" 
  4068                              <1> 			; / and less than "ecore"
  4069                              <1>         ;blo    2f / yes, sysret2
  4070                              <1> ;1:
  4071                              <1>         ;mov    $rtssym,30
  4072                              <1> ;2:
  4073                              <1>         ;br     sysret2
  4074                              <1> 
  4075                              <1> sysilgins:
  4076                              <1> 	; 14/05/2015 (Retro UNIX 386 v1 - Beginning)
  4077                              <1> 	; 03/06/2013
  4078                              <1> 	; Retro UNIX 8086 v1 modification: 
  4079                              <1> 	;	not a valid system call ! (not in use)
  4080                              <1> 	;
  4081 000040D1 E986FBFFFF          <1> 	jmp	badsys
  4082                              <1> 	;jmp	error
  4083                              <1> 	;;jmp 	sysret
  4084                              <1> 
  4085                              <1> 	; Original UNIX v1 'sysemt' routine
  4086                              <1> ;sysilgins: / calculate proper illegal instruction trap address
  4087                              <1>         ;jsr    r0,arg; 10 / take address from sysilgins call
  4088                              <1> 			  ;/ put it in loc 8.,
  4089                              <1>         ;cmp    10,$core / making it the illegal instruction 
  4090                              <1> 		       ; / trap address
  4091                              <1>         ;blo    1f / is the address a user core address?  
  4092                              <1> 		; / yes, go to 2f
  4093                              <1>         ;cmp    10,$ecore
  4094                              <1>         ;blo    2f
  4095                              <1> ;1:
  4096                              <1>         ;mov    $fpsym,10 / no, make 'fpsum' the illegal 
  4097                              <1> 		    ; / instruction trap address for the system
  4098                              <1> ;2:
  4099                              <1>         ;br     sysret2 / return to the caller via 'sysret'
  4100                              <1> 
  4101                              <1> 	; 04/12/2021 - Retro UNIX 386 v2 fs compatibility code
  4102                              <1> sysmdate: ; < change the modification time of a file >
  4103                              <1> 	; 24/12/2021
  4104                              <1> 	; 04/12/2021 (Retro UNIX 386 v1.2)
  4105                              <1> 	; 16/05/2015 (Retro UNIX 386 v1 - Beginning)
  4106                              <1> 	; 03/06/2013 - 02/08/2013 (Retro UNIX 8086 v1)
  4107                              <1> 	;
  4108                              <1> 	; 'sysmdate' is given a file name. It gets inode of this 
  4109                              <1> 	; file into core. The user is checked if he is the owner 
  4110                              <1> 	; or super user. If he is neither an error occurs.
  4111                              <1> 	; 'setimod' is then called to set the i-node modification
  4112                              <1> 	; byte and the modification time, but the modification time
  4113                              <1> 	; is overwritten by whatever get put on the stack during
  4114                              <1> 	; a 'systime' system call. This calls are restricted to
  4115                              <1> 	; the super user.		
  4116                              <1> 	;
  4117                              <1> 	; Calling sequence:
  4118                              <1> 	;	sysmdate; name
  4119                              <1> 	; Arguments:
  4120                              <1> 	;	name - points to the name of file
  4121                              <1> 	; Inputs: (arguments)
  4122                              <1> 	; Outputs: -
  4123                              <1> 	; ...............................................................
  4124                              <1> 	;				
  4125                              <1> 	; Retro UNIX 8086 v1 modification: 
  4126                              <1> 	;	 The user/application program puts address 
  4127                              <1> 	;	 of the file name in BX register 
  4128                              <1> 	;	 as 'sysmdate' system call argument.
  4129                              <1> 	;
  4130                              <1> ; / change the modification time of a file
  4131                              <1> 		; jsr r0,arg; u.namep / point u.namep to the file name
  4132 000040D6 891D[F4740000]      <1>         mov	[u.namep], ebx
  4133 000040DC E80B090000          <1> 	call	namei
  4134                              <1> 		; jsr r0,namei / get its i-number
  4135                              <1> 	;;jc	error       
  4136                              <1> 	;	; br error2 / no, such file
  4137                              <1> 	;jc	fnotfound ; file not found !
  4138                              <1> 	; 24/12/2021
  4139 000040E1 7305                <1> 	jnc	short mdate_0
  4140 000040E3 E9C0FEFFFF          <1> 	jmp	fnotfound
  4141                              <1> mdate_0: 
  4142 000040E8 E89A130000          <1> 	call	iget
  4143                              <1> 		; jsr r0,iget / get i-node into core
  4144                              <1> 	;mov	al, [u.uid]
  4145                              <1> 	;cmp	al, [i.uid]
  4146                              <1>         ;	; cmpb u.uid,i.uid / is user same as owner
  4147                              <1> 	;je	short mdate_1
  4148                              <1>         ;	; beq 1f / yes
  4149                              <1> 	;and	al, al
  4150                              <1> 	;	; tstb u.uid / no, is user the super user
  4151                              <1> 	;;jnz	error
  4152                              <1> 	;	; bne error2 / no, error
  4153                              <1> 	;jz	short mdate_1
  4154                              <1> 	; 04/12/2021
  4155 000040ED 66A1[2A750000]      <1> 	mov	ax, [u.uid]
  4156 000040F3 663B05[78700000]    <1> 	cmp	ax, [i.uid]
  4157 000040FA 7414                <1> 	je	short mdate_1	
  4158 000040FC 6621C0              <1> 	and	ax, ax
  4159 000040FF 740F                <1> 	jz	short mdate_1
  4160                              <1> 
  4161 00004101 C705[4C750000]0B00- <1> 	mov	dword [u.error], ERR_FILE_ACCESS ; permission denied !
  4161 00004109 0000                <1>
  4162 0000410B E99AFAFFFF          <1> 	jmp	error
  4163                              <1> mdate_1: ;1:
  4164 00004110 E811150000          <1> 	call	setimod
  4165                              <1>         	; jsr r0,setimod / fill in modification data,
  4166                              <1> 		               ; / time etc.
  4167 00004115 BE[00700000]        <1> 	mov	esi, p_time
  4168 0000411A BF[AC700000]        <1> 	mov	edi, i.mtim
  4169 0000411F A5                  <1> 	movsd
  4170                              <1> 		; mov 4(sp),i.mtim / move present time to
  4171                              <1>         	; mov 2(sp),i.mtim+2 / modification time
  4172 00004120 E9A5FAFFFF          <1>         jmp	sysret
  4173                              <1> 		; br sysret2
  4174                              <1> 
  4175                              <1> sysstty: ; < set tty status and mode >
  4176                              <1> 	; 24/12/2021 (Retro UNIX 386 v1.1)
  4177                              <1> 	;	    ((32 bit reg push/pop))
  4178                              <1> 	; 17/11/2015
  4179                              <1> 	; 12/11/2015
  4180                              <1> 	; 29/10/2015
  4181                              <1> 	; 17/10/2015
  4182                              <1> 	; 13/10/2015
  4183                              <1> 	; 29/06/2015
  4184                              <1> 	; 27/06/2015 (Retro UNIX 386 v1 - Beginning)
  4185                              <1> 	; 02/06/2013 - 12/07/2014 (Retro UNIX 8086 v1)
  4186                              <1> 	;
  4187                              <1> 	; 'sysstty' sets the status and mode of the typewriter 
  4188                              <1> 	; whose file descriptor is in (u.r0).
  4189                              <1> 	;
  4190                              <1> 	; Calling sequence:
  4191                              <1> 	;	sysstty; arg
  4192                              <1> 	; Arguments:
  4193                              <1> 	;	arg - address of 3 consequitive words that contain
  4194                              <1> 	;	      the source of status data	
  4195                              <1> 	; Inputs: ((*u.r0 - file descriptor & argument))
  4196                              <1> 	; Outputs: ((status in address which is pointed to by arg))
  4197                              <1> 	; ...............................................................
  4198                              <1> 	;	
  4199                              <1> 	; Retro UNIX 8086 v1 modification: 
  4200                              <1> 	;	'sysstty' system call will set the tty
  4201                              <1> 	;	(clear keyboard buffer and set cursor position)
  4202                              <1> 	;	 in following manner:
  4203                              <1> 	;   NOTE: All of tty setting functions are here (16/01/2014)
  4204                              <1> 	;
  4205                              <1> 	; Inputs:
  4206                              <1> 	;	BX = 0 --> means
  4207                              <1> 	;	   If CL = FFh
  4208                              <1> 	;	      set cursor position for console tty, only 
  4209                              <1> 	;	      CH will be ignored (char. will not be written)	
  4210                              <1> 	;	   If CH = 0 (CL < FFh)
  4211                              <1> 	;	      set console tty for (current) process
  4212                              <1> 	;	      CL = tty number (0 to 9)
  4213                              <1> 	;	      (If CH = 0, character will not be written)			
  4214                              <1> 	;          If CH > 0 (CL < FFh)	
  4215                              <1> 	;             CL = tty number (0 to 9)
  4216                              <1> 	;	      CH = character will be written
  4217                              <1> 	;	        at requested cursor position (in DX)	
  4218                              <1> 	;	   DX = cursor position for tty number 0 to 7.	
  4219                              <1>   	;		(only tty number 0 to 7) 
  4220                              <1> 	;          DL = communication parameters (for serial ports) 
  4221                              <1> 	;	        (only for COM1 and COM2 serial ports)
  4222                              <1> 	;	   DH < 0FFh -> DL is valid, initialize serial port
  4223                              <1> 	;			or set cursor position	
  4224                              <1> 	;	   DH = 0FFh -> DL is not valid
  4225                              <1> 	;		do not set serial port parameters 
  4226                              <1> 	;		or do not set cursor position
  4227                              <1> 	;
  4228                              <1> 	;	BX > 0 --> points to name of tty
  4229                              <1> 	;    	   CH > 0 -->
  4230                              <1> 	;		CH = character will be written in current 
  4231                              <1> 	;            	cursor position (for tty number from 0 to 7)
  4232                              <1> 	;	     	or character will be sent to serial port
  4233                              <1> 	;	     	(for tty number 8 or 9)
  4234                              <1> 	;		CL = color of the character if tty number < 8.
  4235                              <1> 	;    	   CH = 0 --> Do not write a character, 
  4236                              <1> 	;		set mode (tty 8 to 9) or 
  4237                              <1> 	;		set current cursor positions (tty 0 to 7) only. 
  4238                              <1> 	;   	   DX = cursor position for tty number 0 to 7.
  4239                              <1> 	;    	   DH = FFh --> Do not set cursor pos (or comm. params.)
  4240                              <1> 	;		(DL is not valid)
  4241                              <1> 	;	   DL = communication parameters 
  4242                              <1> 	;		for tty number 8 or 9 (COM1 or COM2).
  4243                              <1> 	; Outputs:
  4244                              <1> 	;	cf = 0 -> OK
  4245                              <1> 	;	     AL = tty number (0 to 9)
  4246                              <1> 	;	     AH = line status if tty number is 8 or 9
  4247                              <1> 	;	     AH = process number (of the caller) 	
  4248                              <1> 	;	cf = 1 means error (requested tty is not ready)
  4249                              <1> 	;	     AH = FFh if the tty is locked 
  4250                              <1> 	;		  (owned by another process)
  4251                              <1> 	;	        = process number (of the caller) 
  4252                              <1> 	;		  (if < FFh and tty number < 8)
  4253                              <1> 	;	     AL = tty number (0FFh if it does not exist)
  4254                              <1> 	;	     AH = line status if tty number is 8 or 9
  4255                              <1> 	;	NOTE: Video page will be cleared if cf = 0.
  4256                              <1> 	;
  4257                              <1> 	; 27/06/2015 (32 bit modifications)
  4258                              <1> 	; 14/01/2014
  4259 00004125 31C0                <1> 	xor 	eax, eax
  4260 00004127 6648                <1> 	dec	ax ; 17/10/2015
  4261 00004129 A3[D8740000]        <1> 	mov	[u.r0], eax ; 0FFFFh
  4262 0000412E 21DB                <1> 	and	ebx, ebx
  4263                              <1>         ;jnz	sysstty_6
  4264                              <1> 	; 24/12/2021
  4265 00004130 7405                <1> 	jz	short sysstty_14
  4266 00004132 E9C7000000          <1> 	jmp	sysstty_6
  4267                              <1> sysstty_14:
  4268                              <1> 	; set console tty
  4269                              <1> 	; 29/10/2015
  4270                              <1> 	; 17/01/2014 
  4271 00004137 80F909              <1> 	cmp	cl, 9
  4272 0000413A 7613                <1> 	jna	short sysstty_0
  4273                              <1> 	; 17/11/2015
  4274 0000413C 80F9FF              <1> 	cmp	cl, 0FFh
  4275 0000413F 7202                <1> 	jb	short sysstty_13
  4276 00004141 88CD                <1> 	mov	ch, cl ; force CH value to FFh 
  4277                              <1> sysstty_13:
  4278 00004143 8A1D[29750000]      <1> 	mov	bl, [u.uno] ; process number
  4279 00004149 8A8B[F3700000]      <1> 	mov	cl, [ebx+p.ttyc-1] ; current/console tty
  4280                              <1> sysstty_0:
  4281                              <1> 	; 29/06/2015
  4282 0000414F 52                  <1> 	push	edx ; 24/12/2021
  4283 00004150 51                  <1> 	push	ecx
  4284 00004151 30D2                <1> 	xor 	dl, dl	; sysstty call sign
  4285 00004153 88C8                <1> 	mov	al, cl
  4286 00004155 A2[D8740000]        <1> 	mov	[u.r0], al ; tyy number (0 to 9)
  4287 0000415A E8311D0000          <1> 	call	ottyp
  4288 0000415F 59                  <1> 	pop	ecx
  4289 00004160 5A                  <1> 	pop	edx
  4290                              <1> 	;
  4291 00004161 7256                <1> 	jc	short sysstty_pd_err
  4292                              <1> 	;
  4293 00004163 80F908              <1> 	cmp	cl, 8
  4294 00004166 7220                <1> 	jb	short sysstty_2
  4295                              <1> 	;
  4296 00004168 80FEFF              <1> 	cmp	dh, 0FFh
  4297 0000416B 741B                <1> 	je	short sysstty_2
  4298                              <1> 	; set communication parameters for serial ports
  4299                              <1> 	; 29/10/2015
  4300 0000416D 88D4                <1> 	mov	ah, dl ; communication parameters
  4301                              <1> 		; ah = 0E3h = 11100011b = 115200 baud,
  4302                              <1> 		;			 THRE int + RDA int 
  4303                              <1> 		; ah = 23h = 00100011b = 9600 baud,
  4304                              <1> 		;			 THRE int + RDA int 
  4305 0000416F 28C0                <1> 	sub	al, al ; 0
  4306                              <1> 	; 12/07/2014
  4307 00004171 80F909              <1> 	cmp	cl, 9
  4308 00004174 7202                <1> 	jb	short sysstty_1
  4309 00004176 FEC0                <1> 	inc	al
  4310                              <1> sysstty_1:
  4311 00004178 51                  <1> 	push	ecx ; 24/12/2021
  4312                              <1> 	; 29/06/2015	
  4313 00004179 E8B6F4FFFF          <1> 	call 	sp_setp ; Set serial port communication parameters
  4314 0000417E 66890D[D9740000]    <1> 	mov	[u.r0+1], cx ; Line status (ah)
  4315                              <1> 			     ; Modem status (EAX bits 16 to 23)
  4316 00004185 59                  <1> 	pop	ecx
  4317 00004186 7267                <1>         jc      short sysstty_tmout_err ; 29/10/2015
  4318                              <1> sysstty_2:
  4319                              <1> 	; 17/01/2014
  4320 00004188 20ED                <1> 	and	ch, ch 	; set cursor position 
  4321                              <1> 			; or comm. parameters ONLY
  4322 0000418A 750D                <1> 	jnz	short sysstty_3
  4323 0000418C 0FB61D[29750000]    <1> 	movzx	ebx, byte [u.uno] ; process number
  4324 00004193 888B[F3700000]      <1> 	mov	[ebx+p.ttyc-1], cl ; console tty
  4325                              <1> sysstty_3:
  4326                              <1> 	; 16/01/2014
  4327 00004199 88E8                <1> 	mov	al, ch ; character  ; 0 to FFh
  4328                              <1> 	; 17/11/2015
  4329 0000419B B507                <1> 	mov 	ch, 7  ; Default color (light gray)
  4330 0000419D 38E9                <1> 	cmp	cl, ch ; 7 (tty number)
  4331                              <1>         ;jna	sysstty_9
  4332                              <1> 	; 24/12/2021
  4333 0000419F 7705                <1> 	ja	short sysstty_12
  4334 000041A1 E9BE000000          <1> 	jmp	sysstty_9
  4335                              <1> sysstty_12:
  4336                              <1> 	;; BX = 0, CL = 8 or CL = 9
  4337                              <1> 	; (Set specified serial port as console tty port)
  4338                              <1> 	; CH = character to be written
  4339                              <1> 	; 15/04/2014
  4340                              <1> 	; CH = 0 --> initialization only
  4341                              <1> 	; AL = character
  4342                              <1> 	; 26/06/2014
  4343 000041A6 880D[0D750000]      <1> 	mov	[u.ttyn], cl
  4344                              <1> 	; 12/07/2014
  4345 000041AC 88CC                <1> 	mov	ah, cl ; tty number (8 or 9)
  4346 000041AE 20C0                <1> 	and	al, al
  4347 000041B0 7416                <1> 	jz	short sysstty_4 ; al = ch = 0
  4348                              <1>  	; 04/07/2014
  4349 000041B2 E802230000          <1> 	call 	sndc
  4350                              <1> 	; 12/07/2014
  4351 000041B7 EB1B                <1> 	jmp	short sysstty_5
  4352                              <1> sysstty_pd_err: ; 29/06/2015
  4353                              <1> 	; 'permission denied !' error
  4354 000041B9 C705[4C750000]0B00- <1> 	mov	dword [u.error], ERR_NOT_OWNER
  4354 000041C1 0000                <1>
  4355 000041C3 E9E2F9FFFF          <1> 	jmp	error
  4356                              <1> sysstty_4:
  4357                              <1> 	; 12/07/2014
  4358                              <1> 	;xchg 	ah, al	; al = 0 -> al = ah, ah = 0
  4359 000041C8 88E0                <1> 	mov	al, ah ; 29/06/2015
  4360 000041CA 2C08                <1> 	sub	al, 8
  4361                              <1> 	; 27/06/2015
  4362 000041CC E85BF4FFFF          <1> 	call	sp_status ; get serial port status
  4363                              <1> 	; AL = Line status, AH = Modem status
  4364                              <1> 	; 12/11/2015
  4365 000041D1 3C80                <1> 	cmp	al, 80h
  4366 000041D3 F5                  <1> 	cmc
  4367                              <1> sysstty_5:
  4368 000041D4 66A3[D9740000]      <1> 	mov	[u.r0+1], ax ; ah = line status
  4369                              <1> 		     ; EAX bits 16-23 = modem status	
  4370 000041DA 9C                  <1> 	pushf
  4371 000041DB 30D2                <1> 	xor	dl, dl ; sysstty call sign
  4372 000041DD A0[0D750000]        <1> 	mov	al, [u.ttyn] ; 26/06/2014
  4373 000041E2 E8CB1D0000          <1> 	call	cttyp
  4374 000041E7 9D                  <1> 	popf
  4375                              <1> 	;jnc	sysret	
  4376                              <1> 	; 24/12/2021
  4377 000041E8 7205                <1> 	jc	short sysstty_tmout_err
  4378 000041EA E9DBF9FFFF          <1> 	jmp	sysret
  4379                              <1> 	; time out error 
  4380                              <1> sysstty_tmout_err:
  4381 000041EF C705[4C750000]1900- <1> 	mov	dword [u.error], ERR_TIME_OUT
  4381 000041F7 0000                <1>
  4382 000041F9 E9ACF9FFFF          <1> 	jmp	error
  4383                              <1> sysstty_6:
  4384 000041FE 52                  <1> 	push	edx ; 24/12/2021
  4385 000041FF 51                  <1> 	push	ecx
  4386 00004200 891D[F4740000]      <1> 	mov	[u.namep], ebx
  4387 00004206 E8E1070000          <1> 	call	namei
  4388 0000420B 59                  <1> 	pop	ecx
  4389 0000420C 5A                  <1> 	pop	edx
  4390 0000420D 720E                <1> 	jc	short sysstty_inv_dn
  4391                              <1> 	;
  4392 0000420F 6683F813            <1> 	cmp	ax, 19  ; inode number of /dev/COM2
  4393 00004213 7708                <1> 	ja	short sysstty_inv_dn ; 27/06/2015
  4394                              <1> 	;
  4395 00004215 3C0A                <1> 	cmp	al, 10 ; /dev/tty0 .. /dev/tty7
  4396                              <1> 		       ; /dev/COM1, /dev/COM2
  4397 00004217 7213                <1> 	jb	short sysstty_7
  4398 00004219 2C0A                <1> 	sub	al, 10
  4399 0000421B EB20                <1> 	jmp	short sysstty_8
  4400                              <1> sysstty_inv_dn: 
  4401                              <1> 	; 27/06/2015
  4402                              <1> 	; Invalid device name (not a tty) ! error
  4403                              <1> 	; (Device is not a tty or device name not found)
  4404 0000421D C705[4C750000]1800- <1> 	mov	dword [u.error], ERR_INV_DEV_NAME
  4404 00004225 0000                <1>
  4405 00004227 E97EF9FFFF          <1> 	jmp	error 
  4406                              <1> sysstty_7:
  4407 0000422C 3C01                <1> 	cmp	al, 1 ; /dev/tty
  4408 0000422E 75ED                <1> 	jne	short sysstty_inv_dn ; 27/06/2015
  4409 00004230 0FB61D[29750000]    <1> 	movzx	ebx, byte [u.uno] ; process number
  4410 00004237 8A83[F3700000]      <1> 	mov	al, [ebx+p.ttyc-1] ; console tty
  4411                              <1> sysstty_8:	
  4412 0000423D A2[D8740000]        <1> 	mov	[u.r0], al
  4413 00004242 52                  <1> 	push	edx ; 24/12/2021
  4414 00004243 50                  <1> 	push	eax
  4415 00004244 51                  <1> 	push	ecx	
  4416 00004245 E8461C0000          <1> 	call	ottyp
  4417 0000424A 59                  <1> 	pop	ecx
  4418 0000424B 58                  <1> 	pop	eax
  4419 0000424C 5A                  <1> 	pop	edx
  4420                              <1>         ;jc	sysstty_pd_err ; 'permission denied !'
  4421                              <1> 	; 24/12/2021
  4422 0000424D 7305                <1> 	jnc	short sysstty_15
  4423                              <1> 	; 'permission denied !'
  4424 0000424F E965FFFFFF          <1> 	jmp	sysstty_pd_err
  4425                              <1> sysstty_15:
  4426                              <1> 	; 29/10/2015
  4427 00004254 86E9                <1> 	xchg 	ch, cl
  4428                              <1> 		; cl = character, ch = color code
  4429 00004256 86C1                <1> 	xchg	al, cl
  4430                              <1> 		; al = character, cl = tty number
  4431 00004258 80F907              <1> 	cmp	cl, 7
  4432                              <1> 	;ja	sysstty_12
  4433                              <1> 	; 24/12/2021
  4434 0000425B 7605                <1> 	jna	short sysstty_16
  4435 0000425D E944FFFFFF          <1> 	jmp	sysstty_12
  4436                              <1> sysstty_16:
  4437                              <1> 	; 16/01/2014
  4438 00004262 30FF                <1> 	xor	bh, bh
  4439                              <1> 	;
  4440                              <1> sysstty_9: 	; tty 0 to tty 7
  4441                              <1> 	; al = character
  4442 00004264 80FEFF              <1> 	cmp	dh, 0FFh ; Do not set cursor position
  4443 00004267 740B                <1> 	je	short sysstty_10
  4444 00004269 51                  <1> 	push	ecx ; 24/12/2021
  4445 0000426A 50                  <1> 	push	eax	
  4446                              <1> 	;movzx, ebx, cl
  4447 0000426B 88CB                <1> 	mov	bl, cl ; (tty number = video page number)
  4448 0000426D E86AD2FFFF          <1> 	call	set_cpos
  4449 00004272 58                  <1> 	pop	eax
  4450 00004273 59                  <1> 	pop	ecx
  4451                              <1> sysstty_10: 
  4452                              <1> 	; 29/10/2015
  4453 00004274 08C0                <1> 	or	al, al ; character
  4454 00004276 740D                <1> 	jz      short sysstty_11 ; al = 0
  4455                              <1> 	; 17/11/2015
  4456 00004278 3CFF                <1> 	cmp	al, 0FFh
  4457 0000427A 7309                <1> 	jnb	short sysstty_11
  4458                              <1> 		; ch > 0 and ch < FFh
  4459                              <1> 	; write a character at current cursor position
  4460 0000427C 88EC                <1> 	mov	ah, ch ; color/attribute
  4461                              <1> 	; 12/07/2014
  4462 0000427E 51                  <1> 	push	ecx ; 24/12/2021
  4463 0000427F E896D3FFFF          <1> 	call	write_c_current
  4464 00004284 59                  <1> 	pop	ecx
  4465                              <1> sysstty_11:
  4466                              <1> 	; 14/01/2014
  4467 00004285 30D2                <1> 	xor	dl, dl ; sysstty call sign
  4468                              <1> 	; 18/01/2014
  4469                              <1> 	;movzx	eax, cl ; 27/06/2015
  4470 00004287 88C8                <1> 	mov	al, cl
  4471 00004289 E8241D0000          <1> 	call	cttyp
  4472 0000428E E937F9FFFF          <1> 	jmp	sysret
  4473                              <1> 
  4474                              <1> ; Original UNIX v1 'sysstty' routine:
  4475                              <1> ; gtty:
  4476                              <1> ;sysstty: / set mode of typewriter; 3 consequtive word arguments
  4477                              <1>         ;jsr    r0,gtty / r1 will have offset to tty block, 
  4478                              <1> 	; 		/ r2 has source
  4479                              <1>         ;mov    r2,-(sp)
  4480                              <1>         ;mov    r1,-(sp) / put r1 and r2 on the stack
  4481                              <1> ;1: / flush the clist wait till typewriter is quiescent
  4482                              <1>         ;mov    (sp),r1 / restore r1 to tty block offset
  4483                              <1>         ;movb   tty+3(r1),0f / put cc offset into getc argument
  4484                              <1>         ;mov    $240,*$ps / set processor priority to 5
  4485                              <1>         ;jsr    r0,getc; 0:../ put character from clist in r1
  4486                              <1>         ;       br .+4 / list empty, skip branch
  4487                              <1>         ;br     1b / get another character until list is empty
  4488                              <1>         ;mov    0b,r1 / move cc offset to r1
  4489                              <1>         ;inc    r1 / bump it for output clist
  4490                              <1>         ;tstb   cc(r1) / is it 0
  4491                              <1>         ;beq    1f / yes, no characters to output
  4492                              <1>  	;mov    r1,0f / no, put offset in sleep arg
  4493                              <1>         ;jsr    r0,sleep; 0:.. / put tty output process to sleep
  4494                              <1>         ;br     1b / try to calm it down again
  4495                              <1> ;1:
  4496                              <1>         ;mov    (sp)+,r1
  4497                              <1>         ;mov    (sp)+,r2 / restore registers
  4498                              <1> 	;mov    (r2)+,r3 / put reader control status in r3
  4499                              <1>         ;beq    1f / if 0, 1f
  4500                              <1>         ;mov    r3,rcsr(r1) / move r.c. status to reader
  4501                              <1>         ;                   / control status register
  4502                              <1> ;1:
  4503                              <1>         ;mov    (r2)+,r3 / move pointer control status to r3
  4504                              <1>         ;beq    1f / if 0 1f
  4505                              <1>         ;mov    r3,tcsr(r1) / move p.c. status to printer 
  4506                              <1> 	;		    / control status reg
  4507                              <1> ;1:
  4508                              <1>         ;mov    (r2)+,tty+4(r1) / move to flag byte of tty block
  4509                              <1>         ;jmp     sysret2 / return to user
  4510                              <1> 
  4511                              <1> sysgtty: ; < get tty status >
  4512                              <1> 	; 24/12/2021 (Retro UNIX 386 v1.1)
  4513                              <1> 	;	    ((32 bit reg push/pop))
  4514                              <1> 	; 23/11/2015
  4515                              <1> 	; 29/10/2015
  4516                              <1> 	; 17/10/2015
  4517                              <1> 	; 28/06/2015 (Retro UNIX 386 v1 - Beginning)
  4518                              <1> 	; 30/05/2013 - 12/07/2014 (Retro UNIX 8086 v1)
  4519                              <1> 	;
  4520                              <1> 	; 'sysgtty' gets the status of tty in question. 
  4521                              <1> 	; It stores in the three words addressed by it's argument
  4522                              <1> 	; the status of the typewriter whose file descriptor
  4523                              <1> 	; in (u.r0).
  4524                              <1> 	;
  4525                              <1> 	; Calling sequence:
  4526                              <1> 	;	sysgtty; arg
  4527                              <1> 	; Arguments:
  4528                              <1> 	;	arg - address of 3 words destination of the status
  4529                              <1> 	; Inputs: ((*u.r0 - file descriptor))
  4530                              <1> 	; Outputs: ((status in address which is pointed to by arg))
  4531                              <1> 	; ...............................................................
  4532                              <1> 	;	
  4533                              <1> 	; Retro UNIX 8086 v1 modification: 
  4534                              <1> 	;	'sysgtty' system call will return status of tty
  4535                              <1> 	;	(keyboard, serial port and video page status)
  4536                              <1> 	;	 in following manner:
  4537                              <1> 	;
  4538                              <1> 	; Inputs:
  4539                              <1> 	;	BX = 0 --> means 
  4540                              <1> 	;	     CH = 0 -->	'return status of the console tty' 
  4541                              <1> 	;	                 for (current) process
  4542                              <1> 	;	     CL = 0 --> return keyboard status (tty 0 to 9)
  4543                              <1> 	;	     CL = 1 --> return video page status (tty 0 to 7)
  4544                              <1> 	;	     CL = 1 --> return serial port status (tty 8 & 9)		
  4545                              <1> 	;	     CH > 0 -->	tty number + 1
  4546                              <1> 	;
  4547                              <1> 	;	BX > 0 --> points to name of tty
  4548                              <1> 	;	     CL = 0 --> return keyboard status
  4549                              <1> 	;	     CL = 1 --> return video page status
  4550                              <1> 	;	     CH = undefined		 
  4551                              <1> 	;
  4552                              <1> 	; Outputs:
  4553                              <1> 	;	cf = 0 ->
  4554                              <1> 	;
  4555                              <1> 	;	     AL = tty number from 0 to 9
  4556                              <1> 	;		  (0 to 7 is also the video page of the tty)	
  4557                              <1> 	;	     AH = 0 if the tty is free/unused
  4558                              <1> 	;	     AH = the process number of the caller 
  4559                              <1>  	;	     AH = FFh if the tty is locked by another process
  4560                              <1> 	;
  4561                              <1> 	;	  (if calling is for serial port status)
  4562                              <1> 	;	     BX = serial port status if tty number is 8 or 9
  4563                              <1> 	;		  (BH = modem status, BL = Line status)
  4564                              <1> 	;	     CX = 0FFFFh (if data is ready)
  4565                              <1> 	;	     CX = 0 (if data is not ready or undefined)		
  4566                              <1> 	;
  4567                              <1> 	;	  (if calling is for keyboard status)
  4568                              <1> 	;	     BX = current character in tty/keyboard buffer
  4569                              <1> 	;		  (BH = scan code, BL = ascii code)
  4570                              <1> 	;		  (BX=0 if there is not a waiting character)
  4571                              <1> 	;	     CX  is undefined
  4572                              <1> 	;
  4573                              <1> 	;	  (if calling is for video page status)	
  4574                              <1> 	;	     BX = cursor position on the video page
  4575                              <1> 	;		  if tty number < 8
  4576                              <1> 	;		  (BH = row, BL = column)
  4577                              <1> 	;	     CX = current character (in cursor position)
  4578                              <1> 	;		  on the video page of the tty 
  4579                              <1> 	;		  if tty number < 8
  4580                              <1> 	;		  (CH = color, CL = character)
  4581                              <1> 	;	
  4582                              <1> 	;	cf = 1 means error (requested tty is not ready)
  4583                              <1> 	;
  4584                              <1> 	;	     AH = FFh if the caller is not owner of
  4585                              <1> 	;		  specified tty or console tty
  4586                              <1> 	;	     AL = tty number (0FFh if it does not exist)
  4587                              <1> 	;	     BX, CX are undefined if cf = 1
  4588                              <1> 	;
  4589                              <1> 	;	  (If tty number is 8 or 9)
  4590                              <1> 	;	     AL = tty number 
  4591                              <1> 	;	     AH = the process number of the caller 
  4592                              <1> 	;	     BX = serial port status
  4593                              <1> 	;  		 (BH = modem status, BL = Line status)
  4594                              <1> 	;	     CX = 0
  4595                              <1> 	;
  4596                              <1> 		
  4597                              <1> gtty:   ; get (requested) tty number
  4598                              <1> 	; 17/10/2015
  4599                              <1> 	; 28/06/2015 (Retro UNIX 386 v1 - 32 bit modifications)
  4600                              <1> 	; 30/05/2013 - 12/07/2014
  4601                              <1> 	; Retro UNIX 8086 v1 modification ! 
  4602                              <1> 	;
  4603                              <1> 	; ((Modified regs: eAX, eBX, eCX, eDX, eSI, eDI, eBP))
  4604                              <1> 	;
  4605                              <1> 	; 28/06/2015 (32 bit modifications)
  4606                              <1> 	; 16/01/2014
  4607 00004293 31C0                <1> 	xor 	eax, eax
  4608 00004295 6648                <1> 	dec	ax ; 17/10/2015
  4609 00004297 A3[D8740000]        <1> 	mov 	[u.r0], eax ; 0FFFFh
  4610 0000429C 80F901              <1> 	cmp	cl, 1
  4611 0000429F 760F                <1> 	jna	short sysgtty_0
  4612                              <1> sysgtty_invp:
  4613                              <1> 	; 28/06/2015
  4614 000042A1 C705[4C750000]1700- <1>         mov     dword [u.error], ERR_INV_PARAMETER ; 'invalid parameter !' 
  4614 000042A9 0000                <1>
  4615 000042AB E9FAF8FFFF          <1> 	jmp	error
  4616                              <1> sysgtty_0:	
  4617 000042B0 21DB                <1> 	and	ebx, ebx
  4618 000042B2 742E                <1> 	jz	short sysgtty_1
  4619                              <1> 	;
  4620 000042B4 891D[F4740000]      <1> 	mov	[u.namep], ebx
  4621                              <1> 	;push	cx ; 23/11/2015
  4622 000042BA 51                  <1> 	push	ecx ; 24/12/2021
  4623 000042BB E82C070000          <1> 	call	namei
  4624 000042C0 59                  <1> 	pop	ecx
  4625                              <1> 	;pop	cx ; 23/11/2015
  4626 000042C1 7210                <1> 	jc 	short sysgtty_inv_dn ; 28/06/2015
  4627                              <1> 	;
  4628 000042C3 6683F801            <1> 	cmp	ax, 1
  4629 000042C7 7622                <1> 	jna	short sysgtty_2
  4630 000042C9 6683E80A            <1> 	sub	ax, 10
  4631 000042CD 6683F809            <1> 	cmp	ax, 9
  4632                              <1> 	;ja	short sysgtty_inv_dn
  4633                              <1> 	;mov	ch, al
  4634                              <1> 	;jmp	short sysgtty_4
  4635                              <1> 	; 23/11/2015
  4636 000042D1 7629                <1> 	jna	short sysgtty_4
  4637                              <1> sysgtty_inv_dn: 
  4638                              <1> 	; 28/06/2015
  4639                              <1> 	; Invalid device name (not a tty) ! error
  4640                              <1> 	; (Device is not a tty or device name not found)
  4641 000042D3 C705[4C750000]1800- <1> 	mov	dword [u.error], ERR_INV_DEV_NAME
  4641 000042DB 0000                <1>
  4642 000042DD E9C8F8FFFF          <1> 	jmp	error 
  4643                              <1> sysgtty_1:
  4644                              <1> 	; 16/01/2014
  4645 000042E2 80FD0A              <1> 	cmp	ch, 10
  4646 000042E5 77BA                <1> 	ja	short sysgtty_invp ; 28/06/2015
  4647 000042E7 FECD                <1> 	dec	ch ; 0 -> FFh (negative)
  4648 000042E9 790F                <1> 	jns	short sysgtty_3 ; not negative
  4649                              <1> 	;
  4650                              <1> sysgtty_2:
  4651                              <1> 	; get tty number of console tty
  4652 000042EB 8A25[29750000]      <1> 	mov	ah, [u.uno]
  4653                              <1>  	; 28/06/2015
  4654 000042F1 0FB6DC              <1> 	movzx 	ebx, ah
  4655 000042F4 8AAB[F3700000]      <1> 	mov	ch, [ebx+p.ttyc-1]
  4656                              <1> sysgtty_3:
  4657 000042FA 88E8                <1> 	mov	al, ch
  4658                              <1> sysgtty_4:
  4659 000042FC A2[D8740000]        <1> 	mov	[u.r0], al
  4660                              <1>  	; 28/06/2015
  4661                              <1> 	;cmp	al, 9
  4662                              <1> 	;ja	short sysgtty_invp
  4663 00004301 8B2D[D4740000]      <1> 	mov	ebp, [u.usp]
  4664                              <1> 	; 23/11/2015
  4665 00004307 20C9                <1> 	and	cl, cl
  4666 00004309 7432                <1> 	jz	short sysgtty_6 ; keyboard status
  4667 0000430B 3C08                <1> 	cmp	al, 8 ; cmp ch, 8
  4668 0000430D 722E                <1> 	jb	short sysgtty_6 ; video page status
  4669                              <1> 	; serial port status
  4670                              <1> 	; 12/07/2014
  4671                              <1> 	;mov	dx, 0
  4672                              <1> 	;je	short sysgtty_5
  4673                              <1> 	;inc	dl
  4674                              <1> ;sysgtty_5:
  4675                              <1> 	; 28/06/2015
  4676 0000430F 2C08                <1> 	sub	al, 8
  4677 00004311 E816F3FFFF          <1> 	call	sp_status ; serial (COM) port (line) status
  4678                              <1> 	; AL = Line status, AH = Modem status
  4679 00004316 66894510            <1> 	mov	[ebp+16], ax ; serial port status (in EBX)
  4680 0000431A 8A25[29750000]      <1> 	mov	ah, [u.uno]
  4681 00004320 8825[D9740000]      <1>         mov     [u.r0+1], ah
  4682                              <1> 	; 24/12/2021
  4683 00004326 66C745180000        <1> 	mov	word [ebp+24], 0 ; data status (0 = not ready)
  4684                              <1> 				; (in ECX)
  4685 0000432C A880                <1> 	test	al, 80h
  4686 0000432E 7561                <1> 	jnz	short sysgtty_dnr_err ; 29/06/2015
  4687 00004330 A801                <1> 	test	al, 1
  4688                              <1> 	;jz	sysret
  4689 00004332 7404                <1> 	jz	short sysgtty_10
  4690 00004334 66FF4D18            <1> 	dec	word [ebp+24] ; data status (FFFFh = ready)
  4691                              <1> sysgtty_10:
  4692 00004338 E98DF8FFFF          <1> 	jmp	sysret
  4693                              <1> sysgtty_6:
  4694 0000433D A2[0D750000]        <1> 	mov	[u.ttyn], al ; tty number
  4695                              <1> 	;movzx	ebx, al
  4696 00004342 88C3                <1> 	mov 	bl, al ; tty number (0 to 9)
  4697 00004344 D0E3                <1> 	shl 	bl, 1  ; aligned to word
  4698                              <1> 	; 22/04/2014 - 29/06/2015
  4699 00004346 81C3[04700000]      <1>         add     ebx, ttyl
  4700 0000434C 8A23                <1>  	mov	ah, [ebx]
  4701 0000434E 3A25[29750000]      <1> 	cmp	ah, [u.uno]
  4702 00004354 7404                <1> 	je	short sysgtty_7
  4703 00004356 20E4                <1> 	and	ah, ah
  4704                              <1> 	;jz	short sysgtty_7
  4705 00004358 7506                <1> 	jnz	short sysgtty_8
  4706                              <1> 	;mov	ah, 0FFh
  4707                              <1> sysgtty_7:
  4708 0000435A 8825[D9740000]      <1>         mov     [u.r0+1], ah
  4709                              <1> sysgtty_8:
  4710 00004360 08C9                <1> 	or	cl, cl
  4711 00004362 7510                <1> 	jnz	short sysgtty_9
  4712 00004364 B001                <1> 	mov	al, 1  ; test a key is available
  4713 00004366 E8D1200000          <1> 	call	getc
  4714 0000436B 66894510            <1> 	mov	[ebp+16], ax ; bx, character
  4715 0000436F E956F8FFFF          <1> 	jmp	sysret
  4716                              <1> sysgtty_9:
  4717 00004374 8A1D[0D750000]      <1> 	mov	bl, [u.ttyn]
  4718                              <1> 	; bl = video page number
  4719 0000437A E82E220000          <1> 	call 	get_cpos
  4720                              <1> 	; dx = cursor position
  4721 0000437F 66895510            <1> 	mov	[ebp+16], dx ; bx
  4722                              <1> 	;mov	bl, [u.ttyn]
  4723                              <1> 	; bl = video page number
  4724 00004383 E836220000          <1> 	call	read_ac_current
  4725                              <1> 	; ax = character and attribute/color
  4726 00004388 66894518            <1> 	mov	[ebp+24], ax ; cx
  4727 0000438C E939F8FFFF          <1> 	jmp	sysret
  4728                              <1> sysgtty_dnr_err:
  4729                              <1> 	; 'device not responding !' error	
  4730                              <1> 	;mov 	dword [u.error], ERR_TIME_OUT ; 25
  4731 00004391 C705[4C750000]1900- <1> 	mov 	dword [u.error], ERR_DEV_NOT_RESP ; 25
  4731 00004399 0000                <1>
  4732 0000439B E90AF8FFFF          <1> 	jmp	error	
  4733                              <1> 
  4734                              <1> ; Original UNIX v1 'sysgtty' routine:
  4735                              <1> ; sysgtty:
  4736                              <1>         ;jsr    r0,gtty / r1 will have offset to tty block,
  4737                              <1> 	;	       / r2 has destination
  4738                              <1>         ;mov    rcsr(r1),(r2)+ / put reader control status 
  4739                              <1> 	;                     / in 1st word of dest
  4740                              <1>         ;mov    tcsr(r1),(r2)+ / put printer control status
  4741                              <1> 	;                     / in 2nd word of dest
  4742                              <1>         ;mov    tty+4(r1),(r2)+ / put mode in 3rd word
  4743                              <1>         ;jmp    sysret2 / return to user
  4744                              <1> 	
  4745                              <1> ; Original UNIX v1 'gtty' routine:
  4746                              <1> ; gtty:
  4747                              <1>         ;jsr    r0,arg; u.off / put first arg in u.off
  4748                              <1>         ;mov    *u.r0,r1 / put file descriptor in r1
  4749                              <1>         ;jsr    r0,getf / get the i-number of the file
  4750                              <1>         ;tst    r1 / is it open for reading
  4751                              <1>         ;bgt    1f / yes
  4752                              <1>         ;neg    r1 / no, i-number is negative, 
  4753                              <1> 	;          / so make it positive
  4754                              <1> ;1:
  4755                              <1>         ;sub    $14.,r1 / get i-number of tty0
  4756                              <1>         ;cmp    r1,$ntty-1 / is there such a typewriter
  4757                              <1>         ;bhis   error9 / no, error
  4758                              <1>         ;asl    r1 / 0%2
  4759                              <1>         ;asl    r1 / 0%4 / yes
  4760                              <1>         ;asl    r1 / 0%8 / multiply by 8 so r1 points to 
  4761                              <1> 	;	       ; / tty block
  4762                              <1>         ;mov    u.off,r2 / put argument in r2
  4763                              <1>         ;rts    r0 / return
  2071                                  %include 'u2.s'      ; 11/05/2015
  2072                              <1> ; Temporary Runix kernel v2.0 file for debug - 22/11/2021
  2073                              <1> ; (re-write kernel for test by using previous version without a major defect)
  2074                              <1> ; ****************************************************************************
  2075                              <1> ; Retro UNIX 386 v1 Kernel (v0.2) - SYS2.INC
  2076                              <1> ; Last Modification: 21/01/2022
  2077                              <1> ; ----------------------------------------------------------------------------
  2078                              <1> ; Derived from 'Retro UNIX 8086 v1' source code by Erdogan Tan
  2079                              <1> ; (v0.1 - Beginning: 11/07/2012)
  2080                              <1> ;
  2081                              <1> ; Derived from UNIX Operating System (v1.0 for PDP-11) 
  2082                              <1> ; (Original) Source Code by Ken Thompson (1971-1972)
  2083                              <1> ; <Bell Laboratories (17/3/1972)>
  2084                              <1> ; <Preliminary Release of UNIX Implementation Document>
  2085                              <1> ;
  2086                              <1> ; Retro UNIX 8086 v1 - U2.ASM (24/03/2014) //// UNIX v1 -> u2.s
  2087                              <1> ;
  2088                              <1> ; ****************************************************************************
  2089                              <1> 
  2090                              <1> syslink:
  2091                              <1> 	; 12/01/2022
  2092                              <1> 	; 24/12/2021 (Retro UNIX 386 v1.2)
  2093                              <1> 	; 23/06/2015 (Retro UNIX 386 v1 - Beginning)
  2094                              <1> 	; 19/06/2013 (Retro UNIX 8086 v1)
  2095                              <1> 	;
  2096                              <1> 	; 'syslink' is given two arguments, name 1 and name 2.
  2097                              <1> 	; name 1 is a file that already exists. name 2 is the name
  2098                              <1> 	; given to the entry that will go in the current directory.
  2099                              <1> 	; name2 will then be a link to the name 1 file. The i-number
  2100                              <1> 	; in the name 2 entry of current directory is the same
  2101                              <1> 	; i-number for the name 1 file.
  2102                              <1> 	;
  2103                              <1> 	; Calling sequence:
  2104                              <1> 	;	syslink; name 1; name 2
  2105                              <1> 	; Arguments:
  2106                              <1> 	;	name 1 - file name to which link will be created.
  2107                              <1> 	;	name 2 - name of entry in current directory that
  2108                              <1> 	;		 links to name 1.
  2109                              <1> 	; Inputs: -
  2110                              <1> 	; Outputs: -
  2111                              <1> 	; ...............................................................
  2112                              <1> 	;	
  2113                              <1> 	; Retro UNIX 8086 v1 modification: 
  2114                              <1> 	;       'syslink' system call has two arguments; so,
  2115                              <1> 	;	* 1st argument, name 1 is pointed to by BX register
  2116                              <1> 	;	* 2nd argument, name 2 is pointed to by CX register
  2117                              <1> 	;
  2118                              <1> 		; / name1, name2
  2119                              <1> 		;jsr r0,arg2 / u.namep has 1st arg u.off has 2nd
  2120 000043A0 891D[F4740000]      <1> 	mov	[u.namep], ebx
  2121 000043A6 51                  <1> 	push	ecx
  2122 000043A7 E840060000          <1> 	call	namei
  2123                              <1> 		; jsr r0,namei / find the i-number associated with
  2124                              <1> 			     ; / the 1st path name
  2125                              <1>      	;;and	ax, ax
  2126                              <1> 	;;jz	error ; File not found
  2127                              <1> 	;jc	error 
  2128                              <1> 		; br error9 / cannot be found
  2129 000043AC 730F                <1> 	jnc	short syslink0
  2130                              <1> 	;pop 	ecx
  2131                              <1> 	; 'file not found !' error
  2132 000043AE C705[4C750000]0C00- <1> 	mov	dword [u.error], ERR_FILE_NOT_FOUND ; 12
  2132 000043B6 0000                <1>
  2133 000043B8 E9EDF7FFFF          <1> 	jmp	error
  2134                              <1> syslink0:
  2135 000043BD E8C5100000          <1> 	call	iget
  2136                              <1> 		; jsr r0,iget / get the i-node into core
  2137 000043C2 8F05[F4740000]      <1> 	pop	dword [u.namep] ; ecx
  2138                              <1> 		; mov (sp)+,u.namep / u.namep points to 2nd name
  2139                              <1> 	; 24/12/2021
  2140 000043C8 50                  <1> 	push	eax ; *
  2141                              <1> 	;push	ax
  2142                              <1> 		; mov r1,-(sp) / put i-number of name1 on the stack
  2143                              <1> 			    ; / (a link to this file is to be created)
  2144                              <1> 	; 24/12/2021
  2145 000043C9 8A0D[B9740000]      <1> 	mov	cl, [cdev]
  2146 000043CF 51                  <1> 	push	ecx ; **
  2147                              <1> 	;push	word [cdev]
  2148                              <1> 		; mov cdev,-(sp) / put i-nodes device on the stack
  2149 000043D0 E8AC000000          <1> 	call	isdir
  2150                              <1> 		; jsr r0,isdir / is it a directory
  2151 000043D5 E812060000          <1> 	call	namei
  2152                              <1> 		; jsr r0,namei / no, get i-number of name2
  2153                              <1> 	;jnc	error
  2154                              <1> 		; br .+4   / not found 
  2155                              <1> 			 ; / so r1 = i-number of current directory
  2156                              <1> 			 ; / ii = i-number of current directory
  2157                              <1> 		; br error9 / file already exists., error
  2158 000043DA 720F                <1> 	jc	short syslink1
  2159                              <1> 	; pop eax ; 24/12/2021
  2160                              <1> 	; pop eax
  2161                              <1> 	; 'file exists !' error
  2162 000043DC C705[4C750000]0E00- <1> 	mov	dword [u.error], ERR_FILE_EXISTS ; 14
  2162 000043E4 0000                <1>
  2163 000043E6 E9BFF7FFFF          <1> 	jmp	error
  2164                              <1> syslink1:
  2165                              <1> 	;pop	cx
  2166                              <1> 	; 24/12/2021
  2167 000043EB 59                  <1> 	pop	ecx ; **
  2168                              <1> 	;cmp	cx, [cdev]
  2169 000043EC 3A0D[B9740000]      <1> 	cmp	cl, [cdev]
  2170                              <1> 	;jne	error
  2171                              <1> 		; cmp (sp)+,cdev / u.dirp now points to 
  2172                              <1> 			       ; / end of current directory
  2173                              <1> 	        ; bne error9
  2174 000043F2 740F                <1> 	je	short syslink2
  2175                              <1> 	; 'not same drive !' error
  2176 000043F4 C705[4C750000]1500- <1> 	mov	dword [u.error],  ERR_DRV_NOT_SAME ; 21
  2176 000043FC 0000                <1>
  2177 000043FE E9A7F7FFFF          <1> 	jmp	error
  2178                              <1> syslink2:
  2179                              <1> 	;pop	eax ; 24/12/2021
  2180                              <1> 	;push	eax
  2181                              <1> 	; 24/12/2021
  2182 00004403 8B0424              <1> 	mov	eax, [esp] ; *
  2183 00004406 66A3[10750000]      <1> 	mov	[u.dirbuf], ax
  2184                              <1> 		; mov (sp),u.dirbuf / i-number of name1 into u.dirbuf
  2185 0000440C E8AD000000          <1> 	call	mkdir
  2186                              <1> 		; jsr r0,mkdir / make directory entry for name2 
  2187                              <1> 		 	     ; / in current directory
  2188                              <1> 	; 24/12/2021
  2189 00004411 58                  <1> 	pop	eax ; *
  2190                              <1> 	;pop	ax
  2191                              <1> 		; mov (sp)+,r1 / r1 has i-number of name1
  2192 00004412 E870100000          <1> 	call	iget
  2193                              <1> 		; jsr r0,iget / get i-node into core
  2194 00004417 FE05[76700000]      <1> 	inc	byte [i.nlks]
  2195                              <1> 		; incb i.nlks / add 1 to its number of links
  2196                              <1> sysunlink_2:
  2197                              <1> 	; 12/01/2022 - Retro UNIX 386 v1.2
  2198 0000441D C605[CB740000]01    <1> 	mov	byte [imodx], 1	; (flag means file data is same
  2199                              <1> 				;  but inode's itself has been modified)
  2200 00004424 E8FD110000          <1> 	call	setimod
  2201                              <1> 		; jsr r0,setimod / set the i-node modified flag
  2202 00004429 E99CF7FFFF          <1> 	jmp	sysret
  2203                              <1> 
  2204                              <1> sysunlink:
  2205                              <1> 	; 12/01/2022
  2206                              <1> 	; 24/12/2021 (Retro UNIX 386 v1.2)
  2207                              <1> 	; 04/12/2015 (14 byte file names)
  2208                              <1> 	; 23/06/2015 (Retro UNIX 386 v1 - Beginning)
  2209                              <1> 	; 19/06/2013 (Retro UNIX 8086 v1)
  2210                              <1> 	;
  2211                              <1> 	; 'sysunlink' removes the entry for the file pointed to by
  2212                              <1> 	; name from its directory. If this entry was the last link
  2213                              <1> 	; to the file, the contents of the file are freed and the
  2214                              <1> 	; file is destroyed. If, however, the file was open in any
  2215                              <1> 	; process, the actual destruction is delayed until it is 
  2216                              <1> 	; closed, even though the directory entry has disappeared.
  2217                              <1> 	; 
  2218                              <1> 	; The error bit (e-bit) is set to indicate that the file	
  2219                              <1> 	; does not exist or that its directory can not be written.
  2220                              <1> 	; Write permission is not required on the file itself.
  2221                              <1> 	; It is also illegal to unlink a directory (except for
  2222                              <1> 	; the superuser).
  2223                              <1> 	;
  2224                              <1> 	; Calling sequence:
  2225                              <1> 	;	sysunlink; name
  2226                              <1> 	; Arguments:
  2227                              <1> 	;	name - name of directory entry to be removed 
  2228                              <1> 	; Inputs: -
  2229                              <1> 	; Outputs: -
  2230                              <1> 	; ...............................................................
  2231                              <1> 	;				
  2232                              <1> 	; Retro UNIX 8086 v1 modification:
  2233                              <1> 	;	 The user/application program puts address of the name
  2234                              <1> 	;        in BX register as 'sysunlink' system call argument.
  2235                              <1> 
  2236                              <1> 	; / name - remove link name
  2237 0000442E 891D[F4740000]      <1> 	mov	[u.namep], ebx
  2238                              <1> 		;jsr r0,arg; u.namep / u.namep points to name
  2239 00004434 E8B3050000          <1> 	call	namei
  2240                              <1> 		; jsr r0,namei / find the i-number associated 
  2241                              <1> 			     ; / with the path name
  2242                              <1> 	;jc	error
  2243                              <1> 		; br error9 / not found
  2244 00004439 730F                <1> 	jnc	short sysunlink1
  2245                              <1> 	; 'file not found !' error
  2246 0000443B C705[4C750000]0C00- <1> 	mov	dword [u.error], ERR_FILE_NOT_FOUND ; 12
  2246 00004443 0000                <1>
  2247 00004445 E960F7FFFF          <1> 	jmp	error
  2248                              <1> sysunlink1:
  2249 0000444A 50                  <1> 	push	eax ; 24/12/2021
  2250                              <1> 	;push	ax
  2251                              <1> 		; mov r1,-(sp) / put its i-number on the stack
  2252 0000444B E831000000          <1> 	call	isdir
  2253                              <1> 		; jsr r0,isdir / is it a directory
  2254                              <1> 	;xor 	ax, ax
  2255                              <1> 	; 24/12/2021
  2256 00004450 31C0                <1> 	xor	eax, eax
  2257 00004452 66A3[10750000]      <1> 	mov	[u.dirbuf], ax ; 0
  2258                              <1> 		; clr u.dirbuf / no, clear the location that will
  2259                              <1> 			   ; / get written into the i-number portion
  2260                              <1> 			 ; / of the entry
  2261 00004458 832D[F8740000]10    <1> 	sub	dword [u.off], 16 ; 04/12/2015 (10 -> 16) 
  2262                              <1> 		; sub $10.,u.off / move u.off back 1 directory entry
  2263 0000445F E8A5000000          <1> 	call	wdir
  2264                              <1> 		; jsr r0,wdir / free the directory entry
  2265 00004464 58                  <1> 	pop	eax ; 24/12/2021
  2266                              <1> 	;pop	ax
  2267                              <1> 		; mov (sp)+,r1 / get i-number back
  2268 00004465 E81D100000          <1> 	call	iget
  2269                              <1> 		; jsr r0,iget / get i-node
  2270                              <1> 	; 12/01/2022 - Retro UNIX 386 v1.2
  2271                              <1> 	;call	setimod
  2272                              <1> 	;	; jsr r0,setimod / set modified flag
  2273                              <1> 	;	
  2274 0000446A FE0D[76700000]      <1> 	dec	byte [i.nlks]
  2275                              <1> 		; decb i.nlks / decrement the number of links
  2276                              <1> 	; 24/12/2021
  2277 00004470 75AB                <1> 	jnz	short sysunlink_2
  2278                              <1> 	;jnz	sysret
  2279                              <1> 		; bgt sysret9 / if this was not the last link
  2280                              <1> 			    ; / to file return
  2281                              <1> 	; 12/01/2022 - Retro UNIX 386 v1.2
  2282 00004472 E8AF110000          <1> 	call	setimod
  2283                              <1> 		; jsr r0,setimod / set modified flag
  2284                              <1> 	; AX = r1 = i-number
  2285 00004477 E8560A0000          <1> 	call	anyi
  2286                              <1> 		; jsr r0,anyi / if it was, see if anyone has it open.
  2287                              <1> 			 ; / Then free contents of file and destroy it.
  2288 0000447C E949F7FFFF          <1> 	jmp	sysret
  2289                              <1> 		; br sysret9
  2290                              <1> ;sysunlink_2:
  2291                              <1> 	;; 12/01/2022 - Retro UNIX 386 v1.2
  2292                              <1> 	;mov	byte [imodx], 1	; (flag means file data is same
  2293                              <1> 	;			;  but inode's itself has been modified)
  2294                              <1> 	;call	setimod
  2295                              <1> 	;	; jsr r0,setimod / set the i-node modified flag
  2296                              <1> 	;jmp	sysret
  2297                              <1> 
  2298                              <1> isdir:
  2299                              <1> 	; 08/01/2022
  2300                              <1> 	; 01/01/2022 - Retro UNIX 386 v1.2 (runix v2 fs inode)
  2301                              <1> 	; 22/06/2015 (Retro UNIX 386 v1 - Beginning)
  2302                              <1> 	; 04/05/2013 - 02/08/2013 (Retro UNIX 8086 v1)
  2303                              <1> 	;
  2304                              <1> 	; 'isdir' check to see if the i-node whose i-number is in r1
  2305                              <1> 	;  is a directory. If it is, an error occurs, because 'isdir'
  2306                              <1> 	;  called by syslink and sysunlink to make sure directories
  2307                              <1> 	;  are not linked. If the user is the super user (u.uid=0),
  2308                              <1> 	; 'isdir' does not bother checking. The current i-node
  2309                              <1> 	;  is not disturbed.			
  2310                              <1> 	;		
  2311                              <1> 	; INPUTS ->
  2312                              <1> 	;    r1 - contains the i-number whose i-node is being checked.
  2313                              <1> 	;    u.uid - user id
  2314                              <1> 	; OUTPUTS ->
  2315                              <1> 	;    r1 - contains current i-number upon exit
  2316                              <1> 	;    	 (current i-node back in core) 
  2317                              <1> 	;	
  2318                              <1> 	; ((AX = R1))
  2319                              <1> 	;
  2320                              <1>         ; ((Modified registers: eAX, eDX, eBX, eCX, eSI, eDI, eBP))  
  2321                              <1> 	;
  2322                              <1> 
  2323                              <1> 	; / if the i-node whose i-number is in r1 is a directory 
  2324                              <1> 	; / there is an error unless super user made the call
  2325                              <1> 	
  2326                              <1> 	; 01/01/2022
  2327 00004481 66833D[2A750000]00  <1> 	cmp	word [u.uid], 0 ; 16 bit uid (runix v2 fs inode)
  2328                              <1> 	;cmp	byte [u.uid], 0 
  2329                              <1> 		; tstb u.uid / super user
  2330 00004489 7632                <1> 	jna	short isdir1
  2331                              <1> 		; beq 1f / yes, don't care
  2332                              <1> 	; 08/01/2022
  2333 0000448B FF35[B4740000]      <1> 	push	dword [ii]
  2334                              <1> 	;push	word [ii]
  2335                              <1> 		; mov ii,-(sp) / put current i-number on stack
  2336 00004491 E8F10F0000          <1> 	call	iget
  2337                              <1> 		; jsr r0,iget / get i-node into core (i-number in r1)
  2338                              <1> 	; 01/01/2022 (runix v2 fs inode flag)
  2339 00004496 F605[75700000]80    <1> 	test	byte [i.flgs+1], 80h ; regular file ?
  2340 0000449D 7409                <1> 	jz	short isdir2 ; no, error!
  2341 0000449F F605[75700000]40    <1> 	test	byte [i.flgs+1], 40h ; directory flag
  2342                              <1> 	;test 	word [i.flgs], 4000h ; Bit 14 : Directory flag
  2343                              <1> 		; bit $40000,i.flgs / is it a directory
  2344                              <1> 	;jnz	error
  2345                              <1> 		; bne error9 / yes, error
  2346 000044A6 740F                <1> 	jz	short isdir0
  2347                              <1> isdir2:
  2348 000044A8 C705[4C750000]0B00- <1> 	mov 	dword [u.error], ERR_NOT_FILE  ; 11 ; ERR_DIR_ACCESS 
  2348 000044B0 0000                <1>
  2349                              <1> 				; 'permission denied !' error
  2350                              <1> 	;pop	ax
  2351 000044B2 E9F3F6FFFF          <1> 	jmp	error	
  2352                              <1> isdir0:	
  2353                              <1> 	; 08/01/2022
  2354 000044B7 58                  <1> 	pop	eax
  2355                              <1> 	;pop	ax
  2356                              <1> 		; mov (sp)+,r1 / no, put current i-number in r1 (ii)
  2357 000044B8 E8CA0F0000          <1> 	call	iget
  2358                              <1> 		; jsr r0,iget / get it back in
  2359                              <1> isdir1: ; 1:
  2360 000044BD C3                  <1> 	retn
  2361                              <1> 		; rts r0
  2362                              <1> 
  2363                              <1> mkdir:
  2364                              <1> 	; 24/12/2021 (Retro UNIX 386 v1.2)
  2365                              <1> 	; 04/12/2015 (14 byte directory names)
  2366                              <1> 	; 12/10/2015
  2367                              <1> 	; 17/06/2015 (Retro UNIX 386 v1 - Beginning)
  2368                              <1> 	; 29/04/2013 - 01/08/2013 (Retro UNIX 8086 v1)
  2369                              <1> 	;
  2370                              <1> 	; 'mkdir' makes a directory entry from the name pointed to
  2371                              <1> 	; by u.namep into the current directory.
  2372                              <1> 	;
  2373                              <1> 	; INPUTS ->
  2374                              <1> 	;    u.namep - points to a file name 
  2375                              <1> 	;	           that is about to be a directory entry.
  2376                              <1> 	;    ii - current directory's i-number.	
  2377                              <1> 	; OUTPUTS ->
  2378                              <1> 	;    u.dirbuf+2 - u.dirbuf+10 - contains file name. 
  2379                              <1> 	;    u.off - points to entry to be filled 
  2380                              <1> 	;	     in the current directory		
  2381                              <1> 	;    u.base - points to start of u.dirbuf.
  2382                              <1> 	;    r1 - contains i-number of current directory 
  2383                              <1> 	;	
  2384                              <1> 	; ((AX = R1)) output
  2385                              <1> 	;
  2386                              <1> 	;    (Retro UNIX Prototype : 11/11/2012, UNIXCOPY.ASM)
  2387                              <1>         ;    ((Modified registers: eAX, eDX, eBX, eCX, eSI, eDI, eBP))  
  2388                              <1> 	;
  2389                              <1> 
  2390                              <1> 	; 17/06/2015 - 32 bit modifications (Retro UNIX 386 v1)
  2391 000044BE 31C0                <1> 	xor 	eax, eax
  2392 000044C0 BF[12750000]        <1> 	mov     edi, u.dirbuf+2
  2393 000044C5 89FE                <1> 	mov	esi, edi
  2394 000044C7 AB                  <1> 	stosd
  2395 000044C8 AB                  <1> 	stosd
  2396                              <1> 	; 04/12/2015 (14 byte directory names)
  2397 000044C9 AB                  <1> 	stosd
  2398 000044CA 66AB                <1> 	stosw
  2399                              <1> 		; jsr r0,copyz; u.dirbuf+2; u.dirbuf+10. / clear this
  2400 000044CC 89F7                <1> 	mov	edi, esi ; offset to u.dirbuf
  2401                              <1> 	; 12/10/2015 ([u.namep] -> ebp)
  2402                              <1> 	;mov 	ebp, [u.namep]
  2403 000044CE E837060000          <1> 	call	trans_addr_nmbp ; convert virtual address to physical
  2404                              <1> 		; esi = physical address (page start + offset)
  2405                              <1> 		; ecx = byte count in the page (1 - 4096)
  2406                              <1> 	; edi = offset to u.dirbuf (edi is not modified in trans_addr_nm)
  2407                              <1> 		; mov u.namep,r2 / r2 points to name of directory entry
  2408                              <1> 		; mov $u.dirbuf+2,r3 / r3 points to u.dirbuf+2
  2409                              <1> mkdir_1: ; 1: 
  2410 000044D3 45                  <1> 	inc	ebp ; 12/10/2015
  2411                              <1> 	;
  2412                              <1> 	; / put characters in the directory name in u.dirbuf+2 - u.dirbuf+10
  2413                              <1> 	 ; 01/08/2013
  2414 000044D4 AC                  <1> 	lodsb
  2415                              <1> 		; movb (r2)+,r1 / move character in name to r1
  2416 000044D5 20C0                <1> 	and 	al, al
  2417 000044D7 7426                <1> 	jz 	short mkdir_3 	  
  2418                              <1> 		; beq 1f / if null, done
  2419 000044D9 3C2F                <1> 	cmp	al, '/'
  2420                              <1> 		; cmp r1,$'/ / is it a "/"?
  2421 000044DB 7413                <1> 	je	short mkdir_err
  2422                              <1> 	;je	error
  2423                              <1> 		; beq error9 / yes, error
  2424                              <1> 	; 12/10/2015
  2425                              <1> 	;dec	cx
  2426 000044DD 49                  <1> 	dec	ecx ; 24/12/2021
  2427 000044DE 7505                <1> 	jnz	short mkdir_2
  2428                              <1> 	; 12/10/2015 ([u.namep] -> ebp)
  2429 000044E0 E82B060000          <1> 	call	trans_addr_nm ; convert virtual address to physical
  2430                              <1> 		; esi = physical address (page start + offset)
  2431                              <1> 		; ecx = byte count in the page
  2432                              <1> 	; edi = offset to u.dirbuf (edi is not modified in trans_addr_nm)
  2433                              <1> mkdir_2:
  2434 000044E5 81FF[20750000]      <1> 	cmp     edi, u.dirbuf+16 ; ; 04/12/2015 (10 -> 16) 
  2435                              <1> 		; cmp r3,$u.dirbuf+10. / have we reached the last slot for
  2436                              <1> 				     ; / a char?
  2437 000044EB 74E6                <1> 	je	short mkdir_1
  2438                              <1> 		; beq 1b / yes, go back
  2439 000044ED AA                  <1> 	stosb
  2440                              <1> 		; movb r1,(r3)+ / no, put the char in the u.dirbuf
  2441 000044EE EBE3                <1> 	jmp 	short mkdir_1
  2442                              <1> 		; br 1b / get next char
  2443                              <1> mkdir_err:
  2444                              <1> 	; 17/06/2015
  2445 000044F0 C705[4C750000]1300- <1> 	mov	dword [u.error], ERR_NOT_DIR ; 'not a valid directory !'
  2445 000044F8 0000                <1>
  2446 000044FA E9ABF6FFFF          <1> 	jmp	error
  2447                              <1> 
  2448                              <1> mkdir_3: ; 1:
  2449 000044FF A1[F0740000]        <1> 	mov	eax, [u.dirp]
  2450 00004504 A3[F8740000]        <1> 	mov	[u.off], eax
  2451                              <1> 		; mov u.dirp,u.off / pointer to empty current directory
  2452                              <1> 				 ; / slot to u.off
  2453                              <1> 	; 08/01/2022
  2454                              <1> wdir:	; 24/12/2021 (Retro UNIX 386 v1.2)
  2455                              <1> 	; 29/04/2013
  2456 00004509 C705[FC740000]-     <1>         mov     dword [u.base], u.dirbuf
  2456 0000450F [10750000]          <1>
  2457                              <1> 		; mov $u.dirbuf,u.base / u.base points to created file name
  2458 00004513 C705[00750000]1000- <1>         mov     dword [u.count], 16 ; 04/12/2015 (10 -> 16) 
  2458 0000451B 0000                <1>
  2459                              <1> 		; mov $10.,u.count / u.count = 10
  2460                              <1> 	; 08/01/2022
  2461 0000451D A1[B4740000]        <1> 	mov	eax, [ii]
  2462                              <1> 	;mov	ax, [ii] 
  2463                              <1> 		; mov ii,r1 / r1 has i-number of current directory
  2464                              <1> 	;mov	dl, 1 ; owner flag mask ; RETRO UNIX 8086 v1 modification !
  2465                              <1> 	; 08/01/2022 (Retro UNIX 386 v2 file system inode)
  2466 00004522 66BA8000            <1> 	mov	dx, 80h	; mov dx, IWRITE
  2467 00004526 E846100000          <1> 	call 	access
  2468                              <1> 		; jsr r0,access; 1 / get i-node and set its file up 
  2469                              <1> 				 ; / for writing
  2470                              <1> 	; AX = i-number of current directory
  2471                              <1> 	; 01/08/2013
  2472 0000452B FE05[4A750000]      <1> 	inc     byte [u.kcall] ; the caller is 'mkdir' sign	
  2473                              <1> 	;call	writei
  2474                              <1> 	;	; jsr r0,writei / write into directory
  2475                              <1> 	;retn	
  2476                              <1> 	;	; rts r0
  2477                              <1> 	; 24/12/2021
  2478 00004531 E9DB140000          <1> 	jmp	writei
  2479                              <1> 
  2480                              <1> 	; 12/01/2022
  2481                              <1> 	; 01/01/2022
  2482                              <1> 	; 24/12/2021
  2483                              <1> 	; 11/12/2021
  2484                              <1> 	; 04/12/2021
  2485                              <1> 	; 30/11/2021
  2486                              <1> 	; 28/11/2021 - Retro UNIX 386 v1.2
  2487                              <1> 	;	(Retro UNIX 386 v2 fs compatibility modification)
  2488                              <1> sysexec:
  2489                              <1> 	; 02/05/2021
  2490                              <1> 	; 27/03/2021
  2491                              <1> 	; 26/03/2021
  2492                              <1> 	; 25/03/2021 (Retro UNIX 386 v2 - Beginning)
  2493                              <1> 	; 23/10/2015
  2494                              <1> 	; 10/10/2015, 18/10/2015, 19/10/2015
  2495                              <1> 	; 29/07/2015, 05/08/2015, 05/08/2015
  2496                              <1> 	; 21/07/2015, 24/07/2015, 25/07/2015
  2497                              <1> 	; 01/07/2015, 02/07/2015, 20/07,2015
  2498                              <1> 	; 24/06/2015, 25/06/2021
  2499                              <1> 	; 23/06/2015 (Retro UNIX 386 v1 - Beginning)
  2500                              <1> 	; 03/06/2013 - 06/12/2013 (Retro UNIX 8086 v1)
  2501                              <1> 	;
  2502                              <1> 	; 'sysexec' initiates execution of a file whose path name if
  2503                              <1> 	; pointed to by 'name' in the sysexec call. 
  2504                              <1> 	; 'sysexec' performs the following operations:
  2505                              <1> 	;    1. obtains i-number of file to be executed via 'namei'.
  2506                              <1> 	;    2. obtains i-node of file to be executed via 'iget'.
  2507                              <1> 	;    3. sets trap vectors to system routines.
  2508                              <1> 	;    4. loads arguments to be passed to executing file into
  2509                              <1> 	;	highest locations of user's core
  2510                              <1> 	;    5. puts pointers to arguments in locations immediately
  2511                              <1> 	;	following arguments.
  2512                              <1> 	;    6.	saves number of arguments in next location.
  2513                              <1> 	;    7. initializes user's stack area so that all registers
  2514                              <1> 	;	will be zeroed and the PS is cleared and the PC set
  2515                              <1> 	;	to core when 'sysret' restores registers 
  2516                              <1> 	;	and does an rti.
  2517                              <1> 	;    8. inializes u.r0 and u.sp
  2518                              <1> 	;    9. zeros user's core down to u.r0
  2519                              <1> 	;   10.	reads executable file from storage device into core
  2520                              <1> 	;	starting at location 'core'.
  2521                              <1> 	;   11.	sets u.break to point to end of user's code with
  2522                              <1> 	;	data area appended.
  2523                              <1> 	;   12.	calls 'sysret' which returns control at location
  2524                              <1> 	;	'core' via 'rti' instruction.
  2525                              <1> 	;
  2526                              <1> 	; Calling sequence:
  2527                              <1> 	;	sysexec; namep; argp
  2528                              <1> 	; Arguments:
  2529                              <1> 	;	namep - points to pathname of file to be executed
  2530                              <1> 	;	argp  - address of table of argument pointers
  2531                              <1> 	;	argp1... argpn - table of argument pointers
  2532                              <1> 	;	argp1:<...0> ... argpn:<...0> - argument strings
  2533                              <1> 	; Inputs: (arguments)
  2534                              <1> 	; Outputs: -	
  2535                              <1> 	; ...............................................................
  2536                              <1> 	;
  2537                              <1> 	; Retro UNIX 386 v1 modification: 
  2538                              <1> 	;	User application runs in it's own virtual space 
  2539                              <1> 	;	which is izolated from kernel memory (and other
  2540                              <1> 	;	memory pages) via 80386	paging in ring 3 
  2541                              <1> 	;	privilige mode. Virtual start address is always 0.
  2542                              <1> 	;	User's core memory starts at linear address 400000h
  2543                              <1> 	;	(the end of the 1st 4MB).
  2544                              <1> 	;
  2545                              <1> 	; Retro UNIX 8086 v1 modification: 
  2546                              <1> 	;	user/application segment and system/kernel segment
  2547                              <1> 	;	are different and sysenter/sysret/sysrele routines
  2548                              <1> 	;	are different (user's registers are saved to 
  2549                              <1> 	;	and then restored from system's stack.)
  2550                              <1> 	;
  2551                              <1> 	;	NOTE: Retro UNIX 8086 v1 'arg2' routine gets these
  2552                              <1> 	;	      arguments which were in these registers;
  2553                              <1> 	;	      but, it returns by putting the 1st argument
  2554                              <1> 	;	      in 'u.namep' and the 2nd argument
  2555                              <1> 	;	      on top of stack. (1st argument is offset of the
  2556                              <1> 	;	      file/path name in the user's program segment.)
  2557                              <1> 	
  2558                              <1> 	;call	arg2
  2559                              <1> 	; * name - 'u.namep' points to address of file/path name
  2560                              <1> 	;          in the user's program segment ('u.segmnt')
  2561                              <1> 	;          with offset in BX register (as sysopen argument 1).
  2562                              <1> 	; * argp - sysexec argument 2 is in CX register 
  2563                              <1> 	;          which is on top of stack.
  2564                              <1> 	;
  2565                              <1> 		; jsr r0,arg2 / arg0 in u.namep,arg1 on top of stack
  2566                              <1> 
  2567                              <1> 	; 23/06/2015 (32 bit modifications)
  2568                              <1> 
  2569 00004536 891D[F4740000]      <1> 	mov	[u.namep], ebx ; argument 1
  2570                              <1>         ; 18/10/2015
  2571 0000453C 890D[68750000]      <1> 	mov     [argv], ecx  ; * ; argument 2
  2572 00004542 E8A5040000          <1> 	call	namei
  2573                              <1> 		; jsr r0,namei / namei returns i-number of file 
  2574                              <1> 			     ; / named in sysexec call in r1
  2575                              <1> 	;jc	error
  2576                              <1> 		; br error9
  2577 00004547 730F                <1> 	jnc	short sysexec_0
  2578                              <1> 	;
  2579                              <1> 	; 'file not found !' error
  2580 00004549 C705[4C750000]0C00- <1> 	mov	dword [u.error], ERR_FILE_NOT_FOUND
  2580 00004551 0000                <1>
  2581 00004553 E952F6FFFF          <1> 	jmp	error
  2582                              <1> 
  2583                              <1> 	; 26/03/2021 - Retro UNIX 386 v2
  2584                              <1> 	; (executable file flag is checked in 'access')
  2585                              <1> 	; (following error message is in 'access' subroutine)
  2586                              <1> ;sysexec_not_exf:
  2587                              <1> 	;; 'not executable file !' error
  2588                              <1> 	;mov	dword [u.error], ERR_NOT_EXECUTABLE
  2589                              <1> 	;jmp	error 
  2590                              <1> sysexec_0:
  2591                              <1> 	; 26/03/2021 ('iget' will be called in 'access')
  2592                              <1> 	;call	iget
  2593                              <1> 	;	; jsr r0,iget / get i-node for file to be executed
  2594                              <1> 	;
  2595                              <1> 	;test	byte [i.flgs], 10h
  2596                              <1> 	;;test	word [i.flgs], 10h
  2597                              <1> 	;	; bit $20,i.flgs / is file executable
  2598                              <1> 	;jz	short sysexec_not_exf
  2599                              <1> 	;;jz	error
  2600                              <1> 	;	; beq error9
  2601                              <1> 	; 26/03/2021
  2602 00004558 66BA4000            <1> 	mov	dx, 40h ; IEXEC - execute, owner
  2603 0000455C E810100000          <1> 	call	access
  2604                              <1> 	; ! we are here because there is execute permission !
  2605                              <1> 	; ('iopen' is not needed for regular files, from now on)
  2606                              <1> 
  2607                              <1> 	; 26/03/2021
  2608                              <1> 	;; 25/03/2021
  2609                              <1> 	;; (ref: Retro UNIX 386 v2, 'ux.s')
  2610                              <1> 	;mov	dx, 40h ; IEXEC - execute, owner
  2611                              <1> 	;;
  2612                              <1> 	;call	iopen
  2613                              <1> 	;	; jsr r0,iopen / gets i-node for file with i-number
  2614                              <1> 	;		     ; / given in r1 (opens file)
  2615                              <1> 	
  2616                              <1> 	; 26/03/2021
  2617                              <1> 	; AX = i-number of the file
  2618                              <1> 	;test	byte [i.flgs], 20h
  2619                              <1> 	;;test	word [i.flgs], 20h
  2620                              <1> 	;	; bit $40,i.flgs / test user id on execution bit
  2621                              <1> 	;jz	short sysexec_1
  2622                              <1> 	;	; beq 1f
  2623                              <1> 	;cmp 	byte [u.uid], 0 ; 02/08/2013
  2624                              <1> 	;	; tstb u.uid / test user id
  2625                              <1> 	;jna	short sysexec_1
  2626                              <1> 	;	; beq 1f / super user
  2627                              <1> 
  2628                              <1> 	;mov	cl, [i.uid]
  2629                              <1> 	;mov	[u.uid], cl ; 02/08/2013
  2630                              <1> 	;	; movb i.uid,u.uid / put user id of owner of file
  2631                              <1> 				 ; / as process user id
  2632                              <1> 	; 26/03/2021 - Retro UNIX 386 v2
  2633                              <1> 	;mov	al, [i.flgs+1]
  2634 00004561 66833D[2A750000]00  <1> 	cmp	word [u.uid], 0	; super user (root) ?
  2635                              <1> 	;jna	short sysexec_19 ; yes, super user
  2636                              <1> 	;; 02/05/2021
  2637 00004569 7625                <1> 	jna	short sysexec_1	; don't set UID or GID
  2638                              <1> 
  2639                              <1> 	; 01/01/2022 - Retro UNIX 386 v1.2
  2640 0000456B A0[75700000]        <1> 	mov	al, [i.flgs+1]
  2641                              <1> 
  2642                              <1> 	; test set user id on execution bit 
  2643 00004570 A808                <1> 	test	al, 08h ; ISUID flag (800h)
  2644                              <1> 	;jz	short sysexec_19
  2645                              <1> 	; 02/05/2021
  2646 00004572 741C                <1> 	jz	short sysexec_1 ; do not set group ID
  2647                              <1> 				; (if user ID will not be set)
  2648                              <1> 				; (02/05/2021)	
  2649                              <1> 	; set user id on exec (800h)
  2650                              <1> 	; 26/03/2021
  2651                              <1> sysexec_setuid:
  2652                              <1> 	;; 27/03/2021
  2653                              <1> 	;cmp	word [u.uid], 0
  2654                              <1> 	;;jna	short sysexec_19 ; super user
  2655                              <1> 	;; 02/05/2021
  2656                              <1> 	;jna	short sysexec_1 ; do not set group ID
  2657                              <1> 	;			; (if user ID will not be set)
  2658                              <1> 	;			; (02/05/2021)
  2659 00004574 668B0D[78700000]    <1> 	mov	cx, [i.uid]
  2660 0000457B 66890D[2A750000]    <1> 	mov	[u.uid], cx
  2661                              <1> sysexec_19:
  2662                              <1> 	; test set group id on execution bit 
  2663 00004582 A804                <1> 	test	al, 04h ; ISGID flag (400h)
  2664 00004584 740A                <1> 	jz	short sysexec_1
  2665                              <1> sysexec_setgid:
  2666                              <1> 	; set group id on exec (400h)
  2667 00004586 A0[7A700000]        <1> 	mov	al, [i.gid]
  2668 0000458B A2[2E750000]        <1> 	mov	[u.gid], al
  2669                              <1> 	;
  2670                              <1> sysexec_1:
  2671                              <1> 	; 12/01/2022
  2672                              <1> 	; 24/12/2021
  2673                              <1> 	; 11/12/2021
  2674                              <1> 	; 04/12/2021
  2675                              <1> 	; 18/10/2015
  2676                              <1> 	; 10/10/2015
  2677                              <1> 	; 24/07/2015
  2678                              <1> 	; 21/07/2015
  2679                              <1> 	; 25/06/2015
  2680                              <1> 	; 24/06/2015
  2681                              <1>         ; Moving arguments to the end of [u.upage]
  2682                              <1> 	; (by regarding page borders in user's memory space)
  2683                              <1> 	;
  2684                              <1> 	; 10/10/2015
  2685                              <1> 	; 21/07/2015
  2686 00004590 89E5                <1> 	mov	ebp, esp ; (**)
  2687                              <1> 	; 18/10/2015
  2688 00004592 89EF                <1> 	mov 	edi, ebp
  2689 00004594 B900010000          <1> 	mov 	ecx, MAX_ARG_LEN ; 256
  2690                              <1> 	;sub	edi, MAX_ARG_LEN ; 256
  2691 00004599 29CF                <1> 	sub	edi, ecx
  2692 0000459B 89FC                <1> 	mov	esp, edi
  2693 0000459D 31C0                <1> 	xor	eax, eax
  2694 0000459F A3[04750000]        <1> 	mov 	[u.nread], eax ; 0
  2695                              <1> 	; 12/01/2022
  2696                              <1> 	; ([argc] must be cleared because previous 'sysexec'
  2697                              <1> 	; may leave it with any value after an error))
  2698 000045A4 A3[64750000]        <1> 	mov	[argc], eax ; 0
  2699                              <1> 	;
  2700 000045A9 49                  <1> 	dec	ecx ; 256 - 1
  2701 000045AA 890D[00750000]      <1> 	mov 	[u.count], ecx ; MAX_ARG_LEN - 1 ; 255
  2702                              <1> 	;mov 	dword [u.count], MAX_ARG_LEN - 1 ; 255
  2703                              <1> 	; 24/12/2021
  2704                              <1> sysexec_2:
  2705 000045B0 8B35[68750000]      <1> 	mov	esi, [argv] ; 18/10/2015
  2706 000045B6 E85A020000          <1> 	call	get_argp
  2707 000045BB B904000000          <1> 	mov	ecx, 4 ; mov ecx, 4
  2708                              <1> sysexec_3:
  2709 000045C0 21C0                <1> 	and	eax, eax
  2710 000045C2 7454                <1> 	jz	short sysexec_6
  2711                              <1> 	; 18/10/2015
  2712 000045C4 010D[68750000]      <1> 	add	[argv], ecx ; 4
  2713                              <1> 	;;inc	word [argc]
  2714                              <1> 	; 11/12/2021
  2715                              <1> 	;inc	dword [argc]
  2716                              <1> 	; 12/01/2022
  2717 000045CA FE05[64750000]      <1> 	inc	byte [argc]
  2718                              <1> 	;
  2719 000045D0 A3[FC740000]        <1> 	mov	[u.base], eax
  2720                              <1>  	; 23/10/2015
  2721 000045D5 66C705[44750000]00- <1> 	mov	word [u.pcount], 0
  2721 000045DD 00                  <1>
  2722                              <1> sysexec_4:
  2723 000045DE E8E9150000          <1> 	call	cpass ; get a character from user's core memory
  2724 000045E3 750B                <1>         jnz	short sysexec_5
  2725                              <1> 		; (max. 255 chars + null)
  2726                              <1> 	; 18/10/2015
  2727 000045E5 28C0                <1> 	sub 	al, al
  2728 000045E7 AA                  <1> 	stosb
  2729 000045E8 FF05[04750000]      <1> 	inc	dword [u.nread]
  2730 000045EE EB28                <1> 	jmp	short sysexec_6
  2731                              <1> sysexec_5:
  2732 000045F0 AA                  <1> 	stosb
  2733 000045F1 20C0                <1> 	and 	al, al
  2734 000045F3 75E9                <1> 	jnz	short sysexec_4
  2735 000045F5 B904000000          <1> 	mov	ecx, 4
  2736 000045FA 390D[60750000]      <1> 	cmp	[ncount], ecx ; 4
  2737 00004600 72AE                <1> 	jb	short sysexec_2
  2738 00004602 8B35[5C750000]      <1> 	mov	esi, [nbase]
  2739 00004608 010D[5C750000]      <1> 	add	[nbase], ecx ; 4	
  2740                              <1> 	;sub	[ncount], cx 
  2741                              <1> 	; 11/12/2021
  2742 0000460E 290D[60750000]      <1> 	sub	[ncount], ecx
  2743 00004614 8B06                <1> 	mov	eax, [esi]
  2744 00004616 EBA8                <1> 	jmp	short sysexec_3
  2745                              <1> sysexec_6:
  2746                              <1> 	; 24/12/2021
  2747                              <1> 	; 18/10/2015
  2748                              <1> 	; argument list transfer from user's core memory to
  2749                              <1> 	; kernel stack frame is OK here.
  2750                              <1> 	; [u.nread] = argument list length
  2751                              <1> 	;mov	[argv], esp ; start address of argument list
  2752                              <1> 	;
  2753                              <1> 	; 18/10/2015
  2754                              <1> 	; 24/07/2015
  2755                              <1>         ; 21/07/2015
  2756                              <1> 	; 02/07/2015
  2757                              <1> 	; 25/06/2015
  2758                              <1> 	; 24/06/2015
  2759                              <1> 	; 23/06/2015
  2760                              <1> 	;
  2761 00004618 8B1D[3C750000]      <1> 	mov	ebx, [u.ppgdir] ; parent's page directory
  2762 0000461E 21DB                <1> 	and 	ebx, ebx  ; /etc/init ? (u.ppgdir = 0)	
  2763 00004620 740A                <1> 	jz	short sysexec_7
  2764 00004622 A1[38750000]        <1> 	mov	eax, [u.pgdir] ; physical address of page directory
  2765 00004627 E86AE9FFFF          <1> 	call	deallocate_page_dir
  2766                              <1> sysexec_7:
  2767 0000462C E89AE8FFFF          <1> 	call	make_page_dir
  2768                              <1> 	;jc	short sysexec_14
  2769                              <1> 	;jc	panic  ; allocation error 
  2770                              <1> 	;	       ; after a deallocation would be nonsence !?
  2771                              <1> 	; 26/03/2021
  2772 00004631 7243                <1> 	jc	short sysexec_panic
  2773                              <1> 	
  2774                              <1> 	; 24/07/2015
  2775                              <1> 	; map kernel pages (1st 4MB) to PDE 0
  2776                              <1> 	;     of the user's page directory
  2777                              <1> 	;     (It is needed for interrupts!)
  2778                              <1> 	; 18/10/2015
  2779 00004633 8B15[B86F0000]      <1> 	mov	edx, [k_page_dir] ; Kernel's page directory
  2780 00004639 8B02                <1> 	mov	eax, [edx] ; physical address of
  2781                              <1> 			   ; kernel's first page table (1st 4 MB)
  2782                              <1> 			   ; (PDE 0 of kernel's page directory)
  2783 0000463B 8B15[38750000]      <1> 	mov 	edx, [u.pgdir]
  2784 00004641 8902                <1> 	mov	[edx], eax ; PDE 0 (1st 4MB)
  2785                              <1> 	;
  2786                              <1> 	; 20/07/2015
  2787 00004643 BB00004000          <1> 	mov	ebx, CORE ; start address = 0 (virtual) + CORE
  2788                              <1> 	; 18/10/2015
  2789 00004648 BE[54750000]        <1> 	mov	esi, pcore ; physical start address
  2790                              <1> sysexec_8:	
  2791 0000464D B907000000          <1> 	mov	ecx, PDE_A_USER + PDE_A_WRITE + PDE_A_PRESENT
  2792 00004652 E892E8FFFF          <1> 	call	make_page_table
  2793                              <1> 	;jc	panic
  2794                              <1> 	; 26/03/2021
  2795 00004657 721D                <1> 	jc	short sysexec_panic
  2796                              <1> 	;
  2797                              <1> 	;mov	ecx, PTE_A_USER + PTE_A_WRITE + PTE_A_PRESENT
  2798 00004659 E899E8FFFF          <1> 	call	make_page ; make new page, clear and set the pte
  2799                              <1> 	;jc	panic
  2800                              <1> 	; 26/03/2021
  2801 0000465E 7216                <1> 	jc	short sysexec_panic
  2802                              <1> 	;
  2803 00004660 8906                <1> 	mov	[esi], eax ; 24/06/2015
  2804                              <1> 	; ebx = virtual address (24/07/2015)
  2805                              <1> 	; 30/11/2021
  2806                              <1> 	;call 	add_to_swap_queue
  2807                              <1> 	; 18/10/2015
  2808 00004662 81FE[58750000]      <1> 	cmp	esi, ecore ; user's stack (last) page ?
  2809 00004668 7411                <1> 	je	short sysexec_9 ; yes
  2810 0000466A BE[58750000]        <1> 	mov	esi, ecore  ; physical address of the last page
  2811                              <1> 	; 20/07/2015
  2812 0000466F BB00F0FFFF          <1> 	mov	ebx, (ECORE - PAGE_SIZE) + CORE
  2813                              <1> 	; ebx = virtual end address + segment base address - 4K
  2814 00004674 EBD7                <1>         jmp     short sysexec_8
  2815                              <1> sysexec_panic:
  2816                              <1> 	; 26/03/2021
  2817 00004676 E9C7EDFFFF          <1> 	jmp	panic
  2818                              <1> 
  2819                              <1> sysexec_9:
  2820                              <1> 	; 12/01/2022
  2821                              <1> 	; 11/12/2021
  2822                              <1> 	; 18/10/2015
  2823                              <1> 	; 26/08/2015
  2824                              <1> 	; 25/06/2015
  2825                              <1> 	; move arguments from kernel stack to [ecore]
  2826                              <1> 	; (argument list/line will be copied from kernel stack
  2827                              <1> 	; frame to the last (stack) page of user's core memory)
  2828                              <1> 	; 18/10/2015
  2829 0000467B 8B3D[58750000]      <1> 	mov	edi, [ecore]
  2830 00004681 81C700100000        <1> 	add	edi, PAGE_SIZE
  2831                              <1> 	;movzx	eax, word [argc]
  2832                              <1> 	; 12/01/2022
  2833                              <1> 	;xor	eax, eax
  2834                              <1> 	;mov	al, [argc]
  2835                              <1> 	; [argc] < 32
  2836                              <1> 	; 11/12/2021
  2837 00004687 A1[64750000]        <1> 	mov	eax, [argc]
  2838 0000468C 09C0                <1> 	or	eax, eax
  2839 0000468E 7509                <1> 	jnz	short sysexec_10
  2840 00004690 89FB                <1> 	mov 	ebx, edi
  2841 00004692 83EB04              <1> 	sub	ebx, 4 
  2842 00004695 8903                <1> 	mov	[ebx], eax ; 0
  2843 00004697 EB40                <1> 	jmp 	short sysexec_13
  2844                              <1> sysexec_10:
  2845 00004699 8B0D[04750000]      <1> 	mov	ecx, [u.nread]
  2846                              <1> 	;mov 	esi, [argv]
  2847 0000469F 89E6                <1> 	mov	esi, esp ; start address of argument list
  2848 000046A1 29CF                <1> 	sub	edi, ecx ; page end address - argument list length
  2849 000046A3 89C2                <1> 	mov	edx, eax ; [argc]
  2850 000046A5 FEC2                <1> 	inc	dl ; argument count + 1 for argc value  
  2851 000046A7 C0E202              <1> 	shl 	dl, 2  ; 4 * (argument count + 1)
  2852                              <1> 	; max. value of [argc] = 31 (32*4=128)  
  2853                              <1> 	; 12/01/2022
  2854                              <1> 	;inc	edx  ; [argc] <= 255
  2855                              <1> 	;shl	edx, 2 ; max. 256*4
  2856 000046AA 89FB                <1> 	mov	ebx, edi
  2857 000046AC 80E3FC              <1> 	and	bl, 0FCh ; 32 bit (dword) alignment
  2858 000046AF 29D3                <1> 	sub 	ebx, edx
  2859 000046B1 89FA                <1> 	mov	edx, edi
  2860 000046B3 F3A4                <1> 	rep	movsb
  2861 000046B5 89D6                <1> 	mov 	esi, edx
  2862 000046B7 89DF                <1> 	mov 	edi, ebx
  2863 000046B9 BA00F0BFFF          <1> 	mov	edx, ECORE - PAGE_SIZE ; virtual addr. of the last page
  2864 000046BE 2B15[58750000]      <1> 	sub 	edx, [ecore] ; difference (virtual - physical) 
  2865 000046C4 AB                  <1> 	stosd	; eax = argument count
  2866                              <1> sysexec_11:
  2867 000046C5 89F0                <1> 	mov	eax, esi
  2868 000046C7 01D0                <1> 	add	eax, edx
  2869 000046C9 AB                  <1> 	stosd  ; eax = virtual address
  2870 000046CA FE0D[64750000]      <1> 	dec	byte [argc]
  2871 000046D0 7407                <1> 	jz	short sysexec_13
  2872                              <1> sysexec_12:
  2873 000046D2 AC                  <1> 	lodsb
  2874 000046D3 20C0                <1> 	and	al, al
  2875 000046D5 75FB                <1> 	jnz	short sysexec_12
  2876 000046D7 EBEC                <1> 	jmp	short sysexec_11
  2877                              <1> 	;
  2878                              <1> 	; 1:
  2879                              <1> 		; mov (sp)+,r5 / r5 now contains address of list of 
  2880                              <1> 			     ; / pointers to arguments to be passed
  2881                              <1> 		; mov $1,u.quit / u.quit determines handling of quits;
  2882                              <1> 			      ; / u.quit = 1 take quit
  2883                              <1> 		; mov $1,u.intr / u.intr determines handling of 
  2884                              <1> 			     ; / interrupts; u.intr = 1 take interrupt
  2885                              <1> 		; mov $rtssym,30 / emt trap vector set to take 
  2886                              <1> 			       ; / system routine
  2887                              <1> 		; mov $fpsym,*10 / reserved instruction trap vector
  2888                              <1> 			       ; / set to take system routine
  2889                              <1> 		; mov $sstack,sp / stack space used during swapping
  2890                              <1> 		; mov r5,-(sp) / save arguments pointer on stack
  2891                              <1> 		; mov $ecore,r5 / r5 has end of core
  2892                              <1> 		; mov $core,r4 / r4 has start of users core
  2893                              <1> 		; mov r4,u.base / u.base has start of users core
  2894                              <1> 		; mov (sp),r2 / move arguments list pointer into r2
  2895                              <1> 	; 1:
  2896                              <1> 		; tst (r2)+ / argument char = "nul"
  2897                              <1> 		; bne 1b
  2898                              <1> 		; tst -(r2) / decrement r2 by 2; r2 has addr of
  2899                              <1> 			  ; / end of argument pointer list
  2900                              <1> 	; 1:
  2901                              <1> 	     ; / move arguments to bottom of users core
  2902                              <1> 		; mov -(r2),r3 / (r3) last non zero argument ptr
  2903                              <1> 		; cmp r2,(sp) / is r2 = beginning of argument
  2904                              <1> 			    ; / ptr list
  2905                              <1> 		; blo 1f / branch to 1f when all arguments
  2906                              <1> 		       ; / are moved
  2907                              <1> 		; mov -(r2),r3 / (r3) last non zero argument ptr
  2908                              <1> 	; 2:
  2909                              <1> 		; tstb (r3)+
  2910                              <1> 		; bne 2b / scan argument for \0 (nul)
  2911                              <1> 
  2912                              <1> 	; 2:
  2913                              <1> 		; movb -(r3),-(r5) / move argument char 
  2914                              <1> 				 ; / by char starting at "ecore"
  2915                              <1> 		; cmp r3,(r2) / moved all characters in 
  2916                              <1> 			    ; / this argument
  2917                              <1> 		; bhi 2b / branch 2b if not
  2918                              <1> 		; mov r5,(r4)+ / move r5 into top of users core;
  2919                              <1> 			     ; / r5 has pointer to nth arg
  2920                              <1> 		; br 1b / string
  2921                              <1> 	; 1:
  2922                              <1> 		; clrb -(r5)
  2923                              <1> 		; bic $1,r5 / make r5 even, r5 points to 
  2924                              <1> 			; / last word of argument strings
  2925                              <1> 		; mov $core,r2
  2926                              <1> 	
  2927                              <1> 	; 1: / move argument pointers into core following 
  2928                              <1> 	      ; / argument strings
  2929                              <1> 		; cmp r2,r4
  2930                              <1> 		; bhis 1f / branch to 1f when all pointers
  2931                              <1> 			; / are moved
  2932                              <1> 		; mov (r2)+,-(r5)
  2933                              <1> 		; br 1b
  2934                              <1> 	; 1:
  2935                              <1> 		; sub $core,r4 / gives number of arguments *2
  2936                              <1> 		; asr r4 / divide r4 by 2 to calculate 
  2937                              <1> 		       ; / the number of args stored
  2938                              <1> 		; mov r4,-(r5) / save number of arguments ahead
  2939                              <1> 			     ; / of the argument pointers
  2940                              <1> sysexec_13:
  2941                              <1> 	; 30/11/2021
  2942                              <1> 	; 28/11/2021
  2943                              <1> 	; 19/10/2015
  2944                              <1> 	; 18/10/2015
  2945                              <1> 	; 29/07/2015
  2946                              <1> 	; 25/07/2015
  2947                              <1> 	; 24/07/2015
  2948                              <1> 	; 20/07/2015
  2949                              <1> 	; 25/06/2015
  2950                              <1> 	; 24/06/2015
  2951                              <1> 	; 23/06/2015
  2952                              <1> 	;
  2953                              <1> 	; moving arguments to [ecore] is OK here..
  2954                              <1> 	; 18/10/2015
  2955 000046D9 89EC                <1> 	mov 	esp, ebp ; (**) restore kernel stack pointer
  2956                              <1> 	; ebx = beginning address of argument list pointers
  2957                              <1> 	;	in user's stack
  2958                              <1> 	; 19/10/2015
  2959 000046DB 2B1D[58750000]      <1> 	sub 	ebx, [ecore]
  2960 000046E1 81C300F0BFFF        <1> 	add     ebx, (ECORE - PAGE_SIZE)
  2961                              <1> 			; end of core - 4096 (last page)
  2962                              <1> 			; (virtual address)
  2963 000046E7 891D[68750000]      <1> 	mov	[argv], ebx
  2964 000046ED 891D[08750000]      <1> 	mov	[u.break], ebx ; available user memory
  2965                              <1> 	;
  2966 000046F3 29C0                <1> 	sub	eax, eax
  2967 000046F5 C705[00750000]2000- <1> 	mov	dword [u.count], 32 ; Executable file header size
  2967 000046FD 0000                <1>
  2968                              <1> 		; mov $14,u.count
  2969 000046FF C705[EC740000]-     <1> 	mov	dword [u.fofp], u.off
  2969 00004705 [F8740000]          <1>
  2970                              <1> 		; mov $u.off,u.fofp
  2971 00004709 A3[F8740000]        <1> 	mov	[u.off], eax ; 0
  2972                              <1> 		; clr u.off / set offset in file to be read to zero
  2973                              <1> 	; 25/07/2015
  2974 0000470E A3[FC740000]        <1> 	mov	[u.base], eax ; 0, start of user's core (virtual)
  2975                              <1> 	; 25/06/2015 
  2976 00004713 A1[B4740000]        <1> 	mov	eax, [ii] ; 28/11/2021 (32 bit inode number)
  2977                              <1> 	; AX = i-number of the executable file
  2978 00004718 E8E4100000          <1> 	call	readi
  2979                              <1> 		; jsr r0,readi / read in first six words of 
  2980                              <1> 			; / user's file, starting at $core
  2981                              <1> 		; mov sp,r5 / put users stack address in r5
  2982                              <1> 		; sub $core+40.,r5 / subtract $core +40, 
  2983                              <1> 				; / from r5 (leaves number of words
  2984                              <1> 				; / less 26 available for
  2985                              <1> 			     	; / program in user core
  2986                              <1> 		; mov r5,u.count /
  2987                              <1> 	; 25/06/2015
  2988 0000471D 8B0D[08750000]      <1> 	mov	ecx, [u.break] ; top of user's stack (physical addr.)
  2989 00004723 890D[00750000]      <1> 	mov	[u.count], ecx ; save for overrun check
  2990                              <1> 	;
  2991 00004729 8B0D[04750000]      <1> 	mov	ecx, [u.nread]
  2992 0000472F 890D[08750000]      <1> 	mov	[u.break], ecx ; virtual address (offset from start)
  2993 00004735 80F920              <1> 	cmp	cl, 32
  2994 00004738 7540                <1>         jne     short sysexec_15
  2995                              <1> 	;:
  2996                              <1> 	; 25/06/2015
  2997                              <1> 	; Retro UNIX 386 v1 (32 bit) executable file header format
  2998                              <1> 	; 18/10/2015
  2999 0000473A 8B35[54750000]      <1> 	mov	esi, [pcore] ; start address of user's core memory 
  3000                              <1> 		             ; (phys. start addr. of the exec. file)
  3001 00004740 AD                  <1> 	lodsd
  3002 00004741 663DEB1E            <1> 	cmp	ax, 1EEBh ; EBh, 1Eh -> jump to +32
  3003 00004745 7533                <1> 	jne	short sysexec_15
  3004                              <1> 		; cmp core,$405 / br .+14 is first instruction 
  3005                              <1> 			      ; / if file is standard a.out format
  3006                              <1> 		; bne 1f / branch, if not standard format
  3007 00004747 AD                  <1> 	lodsd
  3008 00004748 89C1                <1> 	mov	ecx, eax ; text (code) section size
  3009 0000474A AD                  <1> 	lodsd
  3010 0000474B 01C1                <1> 	add	ecx, eax ; + data section size (initialized data)
  3011                              <1> 		; mov core+2,r5 / put 2nd word of users program in r5;
  3012                              <1> 		              ; / number of bytes in program text
  3013                              <1> 		; sub $14,r5 / subtract 12
  3014 0000474D 89CB                <1> 	mov	ebx, ecx
  3015                              <1> 	;
  3016                              <1> 	; 25/06/2015
  3017                              <1> 	; NOTE: These are for next versions of Retro UNIX 386
  3018                              <1> 	;	and SINGLIX operating systems (as code template).
  3019                              <1> 	;	Current Retro UNIX 386 v1 files can be max. 64KB
  3020                              <1> 	;	due to RUFS (floppy disk file system) restriction...
  3021                              <1> 	;	Overrun is not possible for current version.
  3022                              <1> 	;
  3023 0000474F AD                  <1> 	lodsd	
  3024 00004750 01C3                <1> 	add	ebx, eax ; + bss section size (for overrun checking)
  3025 00004752 3B1D[00750000]      <1> 	cmp	ebx, [u.count]
  3026 00004758 7711                <1> 	ja	short sysexec_14  ; program overruns stack !
  3027                              <1> 	;
  3028                              <1> 	; 24/07/2015
  3029                              <1> 	; add bss section size to [u.break]
  3030 0000475A 0105[08750000]      <1> 	add 	[u.break], eax
  3031                              <1> 	;
  3032 00004760 83E920              <1> 	sub	ecx, 32 ; header size (already loaded)
  3033                              <1> 	;cmp	ecx, [u.count]
  3034                              <1> 	;jnb	short sysexec_16
  3035                              <1> 		; cmp r5,u.count /
  3036                              <1> 		; bgt 1f / branch if r5 greater than u.count
  3037 00004763 890D[00750000]      <1> 	mov	[u.count], ecx ; required read count
  3038                              <1> 		; mov r5,u.count
  3039                              <1> 	;
  3040 00004769 EB29                <1> 	jmp	short sysexec_16
  3041                              <1> 	;
  3042                              <1> sysexec_14:
  3043                              <1> 	; 23/06/2015
  3044                              <1> 	; insufficient (out of) memory
  3045 0000476B C705[4C750000]0400- <1> 	mov	dword [u.error], ERR_MINOR_IM ; 1
  3045 00004773 0000                <1>
  3046 00004775 E930F4FFFF          <1> 	jmp	error
  3047                              <1> 	;
  3048                              <1> sysexec_15:
  3049                              <1> 	; 25/06/2015
  3050                              <1>         ;movzx   edx, word [i.size] ; file size
  3051                              <1> 	; 30/11/2021
  3052 0000477A 8B15[7C700000]      <1> 	mov	edx, [i.size] ; file size
  3053 00004780 29CA                <1> 	sub	edx, ecx ; file size - loaded bytes
  3054 00004782 7626                <1> 	jna	short sysexec_17 ; no need to next read
  3055 00004784 01D1                <1> 	add	ecx, edx ; [i.size]
  3056 00004786 3B0D[00750000]      <1> 	cmp	ecx, [u.count] ; overrun check (!)
  3057 0000478C 77DD                <1> 	ja	short sysexec_14
  3058 0000478E 8915[00750000]      <1> 	mov	[u.count], edx
  3059                              <1> sysexec_16:
  3060                              <1> 	;mov	ax, [ii] ; i-number
  3061                              <1> 	; 28/11/2021
  3062 00004794 A1[B4740000]        <1> 	mov	eax, [ii] ; 32 bit inode number
  3063 00004799 E863100000          <1> 	call	readi
  3064                              <1> 		; add core+10,u.nread / add size of user data area
  3065                              <1> 		                    ; / to u.nread
  3066                              <1> 		; br 2f
  3067                              <1> 	; 1:
  3068                              <1> 		; jsr r0,readi / read in rest of file
  3069                              <1> 	; 2:
  3070 0000479E 8B0D[04750000]      <1> 	mov	ecx, [u.nread]
  3071 000047A4 010D[08750000]      <1> 	add	[u.break], ecx
  3072                              <1> 		; mov u.nread,u.break / set users program break to end of
  3073                              <1> 				    ; / user code
  3074                              <1> 		; add $core+14,u.break / plus data area
  3075                              <1> 	; 20/07/2015
  3076                              <1> sysexec_17:
  3077                              <1> 	; 26/03/2021 - Retro UNIX 386 v2
  3078                              <1> 	; ('iclose' is not needed for regular files, from now on)
  3079                              <1> 	;;mov	ax, [ii] ; i-number
  3080                              <1> 	;call	iclose
  3081                              <1> 	;	; jsr r0,iclose / does nothing
  3082 000047AA 31C0                <1>         xor     eax, eax
  3083 000047AC FEC0                <1> 	inc	al
  3084 000047AE 66A3[24750000]      <1> 	mov	[u.intr], ax ; 1 (interrupt/time-out is enabled)
  3085 000047B4 66A3[26750000]      <1> 	mov	[u.quit], ax ; 1 ('crtl+brk' signal is enabled) 
  3086                              <1> 	; 30/11/2021
  3087 000047BA FEC8                <1> 	dec	al  ; eax = 0
  3088                              <1> 	; 02/07/2015
  3089                              <1>         ;cmp	dword [u.ppgdir], 0 ; is the caller sys_init (kernel) ?
  3090 000047BC 3905[3C750000]      <1> 	cmp	[u.ppgdir], eax ; 0 ; is the caller sys_init (kernel) ?
  3091 000047C2 770C                <1> 	ja	short sysexec_18 ; no, the caller is user process
  3092                              <1> 	; If the caller is kernel (sys_init), 'sysexec' will come here
  3093 000047C4 8B15[B86F0000]      <1> 	mov	edx, [k_page_dir] ; kernel's page directory
  3094 000047CA 8915[3C750000]      <1> 	mov	[u.ppgdir], edx ; next time 'sysexec' must not come here
  3095                              <1> sysexec_18:
  3096                              <1> 	; 18/10/2015
  3097                              <1> 	; 05/08/2015
  3098                              <1> 	; 29/07/2015
  3099 000047D0 8B2D[68750000]      <1> 	mov	ebp, [argv] ; user's stack pointer must points to argument
  3100                              <1> 			    ; list pointers (argument count)
  3101 000047D6 FA                  <1> 	cli
  3102 000047D7 8B25[546F0000]      <1>         mov     esp, [tss.esp0]  ; ring 0 (kernel) stack pointer
  3103                              <1> 	;mov   	esp, [u.sp] ; Restore Kernel stack
  3104                              <1> 			    ; for this process	 
  3105                              <1> 	;add	esp, 20 ; --> EIP, CS, EFLAGS, ESP, SS
  3106                              <1> 	;;xor	eax, eax ; 0
  3107                              <1> 	;dec	al ; eax = 0
  3108                              <1> 	; eax = 0 ; 30/11/2021 
  3109 000047DD 66BA2300            <1> 	mov	dx, UDATA
  3110 000047E1 6652                <1> 	push	dx  ; user's stack segment
  3111 000047E3 55                  <1> 	push	ebp ; user's stack pointer
  3112                              <1> 		    ; (points to number of arguments)
  3113 000047E4 FB                  <1> 	sti
  3114 000047E5 9C                  <1> 	pushfd	; EFLAGS
  3115                              <1> 		; Set IF for enabling interrupts in user mode
  3116                              <1> 	;or	dword [esp], 200h 
  3117                              <1> 	;
  3118                              <1> 	;mov	bx, UCODE
  3119                              <1> 	;push	bx ; user's code segment
  3120 000047E6 6A1B                <1> 	push	UCODE
  3121                              <1> 	;push	0
  3122 000047E8 50                  <1> 	push	eax ; EIP (=0) - start address -
  3123                              <1> 		; clr -(r5) / popped into ps when rti in 
  3124                              <1> 			  ; / sysrele is executed
  3125                              <1> 		; mov $core,-(r5) / popped into pc when rti 
  3126                              <1> 		                ; / in sysrele is executed
  3127                              <1> 		;mov r5,0f / load second copyz argument
  3128                              <1> 		;tst -(r5) / decrement r5
  3129 000047E9 8925[D0740000]      <1> 	mov	[u.sp], esp ; 29/07/2015
  3130                              <1> 	; 05/08/2015
  3131                              <1> 	; Remedy of a General Protection Fault during 'iretd' is here !
  3132                              <1> 	; ('push dx' would cause to general protection fault, 
  3133                              <1> 	; after 'pop ds' etc.)
  3134                              <1> 	;
  3135                              <1> 	;; push dx ; ds (UDATA)
  3136                              <1> 	;; push dx ; es (UDATA)
  3137                              <1> 	;; push dx ; fs (UDATA)
  3138                              <1> 	;; push dx ; gs (UDATA)
  3139                              <1> 	;
  3140                              <1> 	; This is a trick to prevent general protection fault
  3141                              <1> 	; during 'iretd' intruction at the end of 'sysrele' (in u1.s):
  3142 000047EF 8EC2                <1> 	mov 	es, dx ; UDATA
  3143 000047F1 06                  <1> 	push 	es ; ds (UDATA)
  3144 000047F2 06                  <1> 	push 	es ; es (UDATA)
  3145 000047F3 06                  <1> 	push 	es ; fs (UDATA)
  3146 000047F4 06                  <1> 	push	es ; gs (UDATA)
  3147 000047F5 66BA1000            <1> 	mov	dx, KDATA
  3148 000047F9 8EC2                <1> 	mov	es, dx
  3149                              <1> 	;
  3150                              <1> 	;; pushad simulation
  3151 000047FB 89E5                <1> 	mov	ebp, esp ; esp before pushad
  3152 000047FD 50                  <1> 	push	eax ; eax (0)
  3153 000047FE 50                  <1> 	push	eax ; ecx (0)
  3154 000047FF 50                  <1> 	push	eax ; edx (0)
  3155 00004800 50                  <1> 	push	eax ; ebx (0)
  3156 00004801 55                  <1> 	push	ebp ; esp before pushad
  3157 00004802 50                  <1> 	push	eax ; ebp (0)
  3158 00004803 50                  <1> 	push	eax ; esi (0)		
  3159 00004804 50                  <1> 	push	eax ; edi (0)	
  3160                              <1> 	;
  3161 00004805 A3[D8740000]        <1> 	mov	[u.r0], eax ; eax = 0
  3162 0000480A 8925[D4740000]      <1> 	mov	[u.usp], esp
  3163                              <1> 		; mov r5,u.r0 /
  3164                              <1> 		; sub $16.,r5 / skip 8 words
  3165                              <1> 		; mov r5,u.sp / assign user stack pointer value,
  3166                              <1> 		;             / effectively zeroes all regs
  3167                              <1> 			    ; / when sysrele is executed
  3168                              <1> 		; jsr r0,copyz; core; 0:0 / zero user's core
  3169                              <1> 		; clr u.break
  3170                              <1> 		; mov r5,sp / point sp to user's stack
  3171                              <1> 	;
  3172 00004810 E9B7F3FFFF          <1> 	jmp	sysret0
  3173                              <1> 	;jmp	sysret
  3174                              <1> 		; br sysret3 / return to core image at $core
  3175                              <1> 
  3176                              <1> get_argp:
  3177                              <1> 	; 11/12/2021 - Retro UNIX 386 v1.2
  3178                              <1> 	; 18/10/2015 (nbase, ncount)
  3179                              <1> 	; 21/07/2015
  3180                              <1> 	; 24/06/2015 (Retro UNIX 386 v1)
  3181                              <1> 	; Get (virtual) address of argument from user's core memory
  3182                              <1> 	;
  3183                              <1> 	; INPUT:
  3184                              <1> 	;	esi = virtual address of argument pointer
  3185                              <1> 	; OUTPUT:
  3186                              <1> 	;	eax = virtual address of argument
  3187                              <1> 	;
  3188                              <1> 	; Modified registers: EAX, EBX, ECX, EDX, ESI 
  3189                              <1> 	;
  3190 00004815 833D[3C750000]00    <1>  	cmp     dword [u.ppgdir], 0 ; /etc/init ?
  3191                              <1> 				    ; (the caller is kernel)
  3192 0000481C 767C                <1>         jna     short get_argpk 
  3193                              <1> 	;
  3194 0000481E 89F3                <1>      	mov	ebx, esi
  3195 00004820 E82DEAFFFF          <1> 	call	get_physical_addr ; get physical address
  3196 00004825 7254                <1>         jc	short get_argp_err ; 11/12/2021
  3197 00004827 A3[5C750000]        <1> 	mov 	[nbase], eax ; physical address	
  3198                              <1> 	;mov	[ncount], cx ; remain byte count in page (1-4096)
  3199                              <1> 	; 11/12/2021
  3200 0000482C 890D[60750000]      <1> 	mov	[ncount], ecx
  3201 00004832 B804000000          <1> 	mov	eax, 4 ; 21/07/2015
  3202                              <1> 	;cmp	cx, ax ; 4
  3203                              <1> 	; 11/12/2021
  3204 00004837 39C1                <1> 	cmp	ecx, eax ; 4
  3205 00004839 734A                <1> 	jnb	short get_argp2
  3206 0000483B 89F3                <1> 	mov	ebx, esi
  3207 0000483D 01CB                <1> 	add	ebx, ecx
  3208 0000483F E80EEAFFFF          <1> 	call	get_physical_addr ; get physical address
  3209 00004844 7235                <1> 	jc	short get_argp_err
  3210                              <1> 	;push	esi
  3211 00004846 89C6                <1> 	mov	esi, eax
  3212                              <1> 	;xchg	cx, [ncount]
  3213                              <1> 	; 11/12/2021
  3214 00004848 870D[60750000]      <1> 	xchg	ecx, [ncount]
  3215 0000484E 8735[5C750000]      <1> 	xchg	esi, [nbase]
  3216 00004854 B504                <1> 	mov	ch, 4
  3217 00004856 28CD                <1> 	sub	ch, cl
  3218                              <1> get_argp0:
  3219 00004858 AC                  <1> 	lodsb
  3220                              <1> 	;push	ax
  3221                              <1> 	; 11/12/2021
  3222 00004859 50                  <1> 	push	eax
  3223 0000485A FEC9                <1> 	dec	cl
  3224 0000485C 75FA                <1>         jnz     short get_argp0
  3225 0000485E 8B35[5C750000]      <1> 	mov	esi, [nbase]
  3226                              <1> 	; 21/07/2015
  3227 00004864 0FB6C5              <1> 	movzx	eax, ch
  3228 00004867 0105[5C750000]      <1> 	add	[nbase], eax
  3229                              <1> 	;sub	[ncount], ax
  3230                              <1> 	; 11/12/2021
  3231 0000486D 2905[60750000]      <1> 	sub	[ncount], eax
  3232                              <1> get_argp1:
  3233 00004873 AC                  <1> 	lodsb
  3234 00004874 FECD                <1> 	dec	ch
  3235 00004876 743B                <1>         jz      short get_argp3
  3236                              <1>         ;push	ax
  3237                              <1> 	; 11/12/2021
  3238 00004878 50                  <1> 	push	eax
  3239 00004879 EBF8                <1> 	jmp     short get_argp1
  3240                              <1> get_argp_err:
  3241 0000487B A3[4C750000]        <1> 	mov	[u.error], eax
  3242 00004880 E925F3FFFF          <1> 	jmp	error
  3243                              <1> get_argp2:
  3244                              <1> 	; 21/07/2015
  3245                              <1> 	;mov	eax, 4
  3246 00004885 8B15[5C750000]      <1> 	mov 	edx, [nbase] ; 18/10/2015
  3247 0000488B 0105[5C750000]      <1> 	add	[nbase], eax
  3248                              <1> 	;sub	[ncount], ax
  3249                              <1> 	; 11/12/2021
  3250 00004891 2905[60750000]      <1> 	sub	[ncount], eax
  3251                              <1> 	;
  3252 00004897 8B02                <1> 	mov	eax, [edx]
  3253 00004899 C3                  <1> 	retn
  3254                              <1> get_argpk:
  3255                              <1> 	; Argument is in kernel's memory space
  3256 0000489A 66C705[60750000]00- <1> 	mov	word [ncount], PAGE_SIZE ; 4096
  3256 000048A2 10                  <1>
  3257 000048A3 8935[5C750000]      <1> 	mov	[nbase], esi
  3258 000048A9 8305[5C750000]04    <1> 	add	dword [nbase], 4
  3259 000048B0 8B06                <1> 	mov	eax, [esi] ; virtual addr. = physical addr.
  3260 000048B2 C3                  <1> 	retn
  3261                              <1> get_argp3:
  3262 000048B3 B103                <1> 	mov	cl, 3
  3263                              <1> get_argp4:
  3264 000048B5 C1E008              <1> 	shl	eax, 8
  3265                              <1> 	;pop	dx
  3266                              <1> 	; 11/12/2021
  3267 000048B8 5A                  <1> 	pop	edx
  3268 000048B9 88D0                <1> 	mov 	al, dl
  3269 000048BB E2F8                <1>         loop    get_argp4
  3270                              <1> 	;pop	esi
  3271 000048BD C3                  <1> 	retn	
  3272                              <1> 
  3273                              <1> sysfstat: 
  3274                              <1> 	; 08/01/2022
  3275                              <1> 	; 26/12/2021 (Retro UNIX 386 v1.2) 
  3276                              <1> 	; 23/06/2015 (Retro UNIX 386 v1 - Beginning)
  3277                              <1> 	; 19/06/2013 (Retro UNIX 8086 v1)
  3278                              <1> 	;
  3279                              <1> 	; 'sysfstat' is identical to 'sysstat' except that it operates
  3280                              <1> 	; on open files instead of files given by name. It puts the
  3281                              <1> 	; buffer address on the stack, gets the i-number and
  3282                              <1> 	; checks to see if the file is open for reading or writing.
  3283                              <1> 	; If the file is open for writing (i-number is negative)
  3284                              <1> 	; the i-number is set positive and a branch into 'sysstat'
  3285                              <1> 	; is made.	
  3286                              <1> 	;
  3287                              <1> 	; Calling sequence:
  3288                              <1> 	;	sysfstat; buf
  3289                              <1> 	; Arguments:
  3290                              <1> 	;	buf - buffer address
  3291                              <1> 	;
  3292                              <1> 	; Inputs: *u.r0 - file descriptor
  3293                              <1> 	; Outputs: buffer is loaded with file information
  3294                              <1> 	; ...............................................................
  3295                              <1> 	;				
  3296                              <1> 	; Retro UNIX 8086 v1 modification:
  3297                              <1> 	;       'sysfstat' system call has two arguments; so,
  3298                              <1> 	;	* 1st argument, file descriptor is in BX register
  3299                              <1> 	;	* 2nd argument, buf is pointed to by CX register
  3300                              <1> 
  3301                              <1> 	; / set status of open file
  3302                              <1> 		; jsr r0,arg; u.off / put buffer address in u.off
  3303 000048BE 51                  <1> 	push	ecx ; *
  3304                              <1> 		; mov u.off,-(sp) / put buffer address on the stack
  3305                              <1> 		; mov *u.r0,r1 / put file descriptor in r1
  3306                              <1> 		; jsr r0,getf / get the files i-number
  3307                              <1> 	; BX = file descriptor (file number)
  3308 000048BF E8F2000000          <1> 	call	getf1
  3309 000048C4 6621C0              <1> 	and	ax, ax ; i-number of the file
  3310                              <1> 		; tst	r1 / is it 0?
  3311                              <1> 	;jz	error
  3312                              <1> 		; beq error3 / yes, error
  3313                              <1> 	;jnz	short sysfstat1
  3314                              <1> 	; 26/12/2021 - Retro UNIX 386 v1.2 (runix v2 file system)
  3315 000048C7 752C                <1> 	jnz	short sysstat1
  3316 000048C9 C705[4C750000]0A00- <1> 	mov	dword [u.error], ERR_FILE_NOT_OPEN  ; 'file not open !'
  3316 000048D1 0000                <1>
  3317 000048D3 E9D2F2FFFF          <1> 	jmp	error
  3318                              <1> 	; 26/12/2021
  3319                              <1> ;sysfstat1:
  3320                              <1> 	;cmp	ah, 80h
  3321                              <1>         ;jb      short sysstat1
  3322                              <1> 	;	; bgt 1f / if i-number is negative (open for writing)
  3323                              <1> 	;neg	ax
  3324                              <1> 	;	; neg r1 / make it positive, then branch
  3325                              <1> 	; 08/01/2022
  3326                              <1> 	;jmp	short sysstat1
  3327                              <1> 		; br 1f / to 1f
  3328                              <1> sysstat:
  3329                              <1> 	; 26/12/2021 (Retro UNIX 386 v1.2) 
  3330                              <1> 	; 18/10/2015
  3331                              <1> 	; 07/10/2015
  3332                              <1> 	; 02/09/2015
  3333                              <1> 	; 23/06/2015 (Retro UNIX 386 v1 - Beginning)
  3334                              <1> 	; 19/06/2013 (Retro UNIX 8086 v1)
  3335                              <1> 	;
  3336                              <1> 	; 'sysstat' gets the status of a file. Its arguments are the
  3337                              <1> 	; name of the file and buffer address. The buffer is 34 bytes
  3338                              <1> 	; long and information about the file placed in it.	
  3339                              <1> 	; sysstat calls 'namei' to get the i-number of the file.
  3340                              <1> 	; Then 'iget' is called to get i-node in core. The buffer
  3341                              <1> 	; is then loaded and the results are given in the UNIX
  3342                              <1> 	; Programmers Manual sysstat (II).	
  3343                              <1> 	;
  3344                              <1> 	; Calling sequence:
  3345                              <1> 	;	sysstat; name; buf
  3346                              <1> 	; Arguments:
  3347                              <1> 	;	name - points to the name of the file
  3348                              <1> 	;	buf - address of a 34 bytes buffer
  3349                              <1> 	; Inputs: -
  3350                              <1> 	; Outputs: buffer is loaded with file information
  3351                              <1> 	; ...............................................................
  3352                              <1> 	;				
  3353                              <1> 	; Retro UNIX 8086 v1 modification: 
  3354                              <1> 	;       'sysstat' system call has two arguments; so,
  3355                              <1> 	;	Retro UNIX 8086 v1 argument transfer method 2 is used
  3356                              <1> 	;	to get sysstat system call arguments from the user;
  3357                              <1> 	;	* 1st argument, name is pointed to by BX register
  3358                              <1> 	;	* 2nd argument, buf is pointed to by CX register
  3359                              <1> 	;
  3360                              <1> 	;	NOTE: Retro UNIX 8086 v1 'arg2' routine gets these
  3361                              <1> 	;	      arguments which were in these registers;
  3362                              <1> 	;	      but, it returns by putting the 1st argument
  3363                              <1> 	;	      in 'u.namep' and the 2nd argument
  3364                              <1> 	;	      on top of stack. (1st argument is offset of the
  3365                              <1> 	;	      file/path name in the user's program segment.)		 	
  3366                              <1> 	
  3367                              <1> 	; / ; name of file; buffer - get files status
  3368                              <1> 		; jsr r0,arg2 / get the 2 arguments
  3369 000048D8 891D[F4740000]      <1> 	mov	[u.namep], ebx
  3370 000048DE 51                  <1> 	push	ecx ; *
  3371 000048DF E808010000          <1> 	call	namei
  3372                              <1> 		; jsr r0,namei / get the i-number for the file
  3373                              <1> 	;jc	error
  3374                              <1> 		; br error3 / no such file, error
  3375 000048E4 730F                <1> 	jnc	short sysstat1
  3376                              <1> 	; pop 	ecx
  3377                              <1> sysstat_err0:
  3378                              <1> 	; 'file not found !' error
  3379 000048E6 C705[4C750000]0C00- <1> 	mov	dword [u.error], ERR_FILE_NOT_FOUND ; 12
  3379 000048EE 0000                <1>
  3380 000048F0 E9B5F2FFFF          <1> 	jmp	error
  3381                              <1> 
  3382                              <1> sysstat1: ; 1:
  3383 000048F5 E88D0B0000          <1> 	call	iget
  3384                              <1> 		; jsr r0,iget / get the i-node into core
  3385                              <1> 	; 07/10/2015 (ax = [ii], inode number)
  3386                              <1> 	; 02/09/2015
  3387 000048FA 8F05[FC740000]      <1> 	pop	dword [u.base] ; *
  3388                              <1> 		; mov (sp)+,r3 / move u.off to r3 (points to buffer)
  3389 00004900 E855000000          <1> 	call	sysstat_gpa ; get physical address
  3390 00004905 730A                <1> 	jnc 	short sysstat2
  3391                              <1> sysstat_err1:
  3392 00004907 A3[4C750000]        <1> 	mov	dword [u.error], eax ; error code
  3393 0000490C E999F2FFFF          <1> 	jmp	error
  3394                              <1> sysstat2:
  3395 00004911 A0[B4740000]        <1> 	mov 	al, [ii] ; 07/10/2015 (result of 'iget' call, above)
  3396 00004916 AA                  <1> 	stosb
  3397 00004917 FF05[FC740000]      <1> 	inc 	dword [u.base]
  3398                              <1> 	;dec 	cx
  3399 0000491D 49                  <1> 	dec	ecx ; 26/12/2021
  3400 0000491E 7505                <1> 	jnz	short sysstat3
  3401 00004920 E835000000          <1> 	call	sysstat_gpa
  3402                              <1> 	;jc	short sysstat_err1
  3403                              <1> sysstat3:
  3404 00004925 A0[B5740000]        <1> 	mov 	al, [ii+1] ; 07/10/2015 (result of 'iget' call, above)
  3405 0000492A AA                  <1> 	stosb
  3406                              <1> 		; mov r1,(r3)+ / put i-number in 1st word of buffer
  3407 0000492B FF05[FC740000]      <1> 	inc 	dword [u.base]
  3408                              <1> 	;;dec 	word [u.pcount]
  3409                              <1> 	;dec	cx
  3410 00004931 49                  <1> 	dec	ecx ; 26/12/2021
  3411 00004932 7505                <1> 	jnz	short sysstat4
  3412 00004934 E821000000          <1> 	call	sysstat_gpa
  3413                              <1> 	;jc	short sysstat_err1	
  3414                              <1> sysstat4:
  3415 00004939 BE[74700000]        <1> 	mov	esi, inode
  3416                              <1> 		; mov $inode,r2 / r2 points to i-node
  3417                              <1> sysstat5: ; 1:
  3418 0000493E A4                  <1> 	movsb
  3419                              <1> 		; mov (r2)+,(r3)+ / move rest of i-node to buffer
  3420 0000493F FF05[FC740000]      <1> 	inc 	dword [u.base]
  3421                              <1> 	;;dec 	word [u.pcount]
  3422                              <1> 	;dec	cx
  3423 00004945 49                  <1> 	dec	ecx ; 26/12/2021
  3424 00004946 7505                <1> 	jnz	short sysstat6
  3425 00004948 E80D000000          <1> 	call	sysstat_gpa
  3426                              <1> 	;jc	short sysstat_err1
  3427                              <1> sysstat6:
  3428                              <1> 	; 26/12/2021 - Retro UNIX 386 v1.2
  3429 0000494D 81FE[B4700000]      <1> 	cmp	esi, inode + 64 ; Retro UNIX v2 inode		
  3430                              <1> 	;cmp	esi, inode + 32
  3431                              <1> 		; cmp r2,$inode+32 / done?
  3432 00004953 75E9                <1> 	jne	short sysstat5
  3433                              <1> 		; bne 1b / no, go back
  3434 00004955 E970F2FFFF          <1> 	jmp	sysret
  3435                              <1> 		; br sysret3 / return through sysret
  3436                              <1> 	;
  3437                              <1> sysstat_gpa: ; get physical address of file status buffer
  3438                              <1> 	; 02/09/2015
  3439 0000495A 8B1D[FC740000]      <1> 	mov 	ebx, [u.base]
  3440                              <1> 	; 07/10/2015
  3441 00004960 E8EDE8FFFF          <1> 	call	get_physical_addr ; get physical address
  3442                              <1> 	;jc	short sysstat_gpa1
  3443 00004965 72A0                <1> 	jc	short sysstat_err1
  3444                              <1> 	; 18/10/2015
  3445 00004967 89C7                <1> 	mov	edi, eax ; physical address
  3446                              <1> 	;mov	[u.pcount], cx ; remain bytes in page
  3447                              <1> ;sysstat_gpa1:
  3448 00004969 C3                  <1> 	retn
  3449                              <1> 
  3450                              <1> fclose:
  3451                              <1> 	; 08/01/2022
  3452                              <1> 	; 04/12/2021 - Retro UNIX 386 v1.2
  3453                              <1> 	; 08/04/2021
  3454                              <1> 	; 04/04/2021
  3455                              <1> 	; 18/08/2020 - Retro UNIX 386 v2
  3456                              <1> 	; 18/06/2015 (Retro UNIX 386 v1 - Beginning)
  3457                              <1> 	;            (32 bit offset pointer modification)
  3458                              <1> 	; 19/04/2013 - 12/01/2014 (Retro UNIX 8086 v1)
  3459                              <1> 	;
  3460                              <1> 	; Given the file descriptor (index to the u.fp list)
  3461                              <1> 	; 'fclose' first gets the i-number of the file via 'getf'.
  3462                              <1> 	; If i-node is active (i-number > 0) the entry in 
  3463                              <1> 	; u.fp list is cleared. If all the processes that opened
  3464                              <1> 	; that file close it, then fsp etry is freed and the file
  3465                              <1> 	; is closed. If not a return is taken. 
  3466                              <1> 	; If the file has been deleted while open, 'anyi' is called
  3467                              <1> 	; to see anyone else has it open, i.e., see if it is appears
  3468                              <1> 	; in another entry in the fsp table. Upon return from 'anyi'
  3469                              <1> 	; a check is made to see if the file is special.	
  3470                              <1> 	;
  3471                              <1> 	; INPUTS ->
  3472                              <1> 	;    r1 - contains the file descriptor (value=0,1,2...)
  3473                              <1> 	;    u.fp - list of entries in the fsp table
  3474                              <1> 	;    fsp - table of entries (4 words/entry) of open files.	 
  3475                              <1> 	; OUTPUTS ->
  3476                              <1> 	;    r1 - contains the same file descriptor
  3477                              <1> 	;    r2 - contains i-number
  3478                              <1> 	;
  3479                              <1> 	; ((AX = R1))
  3480                              <1> 	; ((Modified registers: edx, ebx, ecx, esi, edi, ebp))
  3481                              <1> 	;
  3482                              <1> 	; Retro UNIX 8086 v1 modification : CF = 1
  3483                              <1> 	;              if i-number of the file is 0. (error)
  3484                              <1> 
  3485                              <1> 	; 08/04/2021
  3486                              <1> 	; INPUT:
  3487                              <1> 	;	eax = file descriptor
  3488                              <1> 	; OUTPUT:
  3489                              <1> 	;	cf = 0 -> eax = file descriptor
  3490                              <1> 	;	cf = 1 -> eax = error code
  3491                              <1> 	;		  (al = 0 -> file not open)
  3492                              <1> 
  3493                              <1> 	; 18/08/2020
  3494                              <1> 	;movzx	edx, ax ; **
  3495 0000496A 89C3                <1> 	mov	ebx, eax
  3496                              <1> 
  3497                              <1> 	; 18/08/2020
  3498                              <1> _fclose:
  3499 0000496C 53                  <1> 	push	ebx ; ***  ; file descriptor (index to u.fp)
  3500                              <1> 
  3501                              <1> 	;push	ax ; ***
  3502                              <1> 		; mov r1,-(sp) / put r1 on the stack (it contains 
  3503                              <1> 			     ; / the index to u.fp list)
  3504                              <1> 	; ebx = file descriptor/number
  3505 0000496D E844000000          <1> 	call	getf
  3506                              <1> 		; jsr r0,getf / r1 contains i-number, 
  3507                              <1> 			    ; / cdev has device =, u.fofp 
  3508                              <1> 			    ; / points to 3rd word of fsp entry
  3509                              <1> 	;cmp	eax, 1 ; 18/08/2020
  3510 00004972 6683F801            <1> 	cmp	ax, 1 ; r1
  3511                              <1> 		; tst r1 / is i-number 0?
  3512 00004976 723C                <1> 	jb	short fclose_2
  3513                              <1> 		; beq 1f / yes, i-node not active so return
  3514                              <1> 		; tst (r0)+ / no, jump over error return
  3515                              <1> 	
  3516                              <1> 	;mov	ebx, edx ; **
  3517                              <1>  	; 18/08/2020
  3518 00004978 8B1C24              <1> 	mov	ebx, [esp]
  3519                              <1> 	;mov	edx, eax 
  3520                              <1> 	;;mov 	dx, ax ; *
  3521                              <1> 		; mov r1,r2 / move i-number to r2 ;*
  3522                              <1> 		; mov (sp),r1 / restore value of r1 from the stack
  3523                              <1> 			    ; / which is index to u.fp ; **
  3524 0000497B C683[E2740000]00    <1> 	mov	byte [ebx+u.fp], 0
  3525                              <1> 		; clrb u.fp(r1) / clear that entry in the u.fp list
  3526 00004982 8B1D[EC740000]      <1> 	mov	ebx, [u.fofp]
  3527                              <1> 		; mov u.fofp,r1 / r1 points to 3rd word in fsp entry
  3528                              <1> 
  3529                              <1> 	; 18/08/2020 - Retro UNIX 386 v2 (new fsp structure)
  3530                              <1> 	; ebx = 5th word of fsp entry (64 bit file offset)
  3531                              <1> fclose_0:
  3532                              <1> 	; 08/01/2022
  3533 00004988 FE4BFE              <1> 	dec	byte [ebx-2] ; 18/08/2020 - Retro UNIX 386 v2
  3534                              <1> 	;dec	byte [ebx+4] ; 18/06/2015
  3535                              <1> 		; decb 2(r1) / decrement the number of processes 
  3536                              <1> 			   ; / that have opened the file
  3537 0000498B 7927                <1> 	jns	short fclose_2 ; jump if not negative (jump if bit 7 is 0)	 
  3538                              <1> 		; bge 1f / if all processes haven't closed the file, return
  3539                              <1> 	;
  3540                              <1> 	;push	dx ; *
  3541                              <1> 		; mov r2,-(sp) / put r2 on the stack (i-number)
  3542                              <1> 	;;xor	ax, ax ; 0
  3543                              <1> 	;xor	eax, eax ; 18/08/2020
  3544                              <1> 		; clear 1st word of fsp entry
  3545                              <1> 	;mov	[ebx-8], ax ; 0 ; 18/08/2020
  3546                              <1> 	; 18/08/2020 - Retro UNIX 386 v2
  3547 0000498D 66C743F80000        <1> 	mov	word [ebx-8], 0 ; clear 1st word of fsp entry ; i-number
  3548                              <1> 			; Note: 32 bit i-node number field is ready but
  3549                              <1> 			;	it is not used in current runix version.	
  3550                              <1> 
  3551                              <1> 	;;mov	[ebx-4], ax ; 0
  3552                              <1> 		; clr -4(r1) / clear 1st word of fsp entry
  3553                              <1> 	;mov	al, [ebx+5] ; 18/06/2015
  3554                              <1> 	;	; tstb	3(r1) / has this file been deleted
  3555                              <1> 	;and	al, al
  3556                              <1> 	;jz	short fclose_1
  3557                              <1> 	;	; beq 2f / no, branch
  3558                              <1> 
  3559                              <1> 	;push	edx ; * ; 18/08/2020
  3560                              <1> 	;mov	eax, edx ; * ; 18/08/2020
  3561                              <1> 
  3562                              <1> 	;; 18/08/2020 - Retro UNIX 386 v2 (new fsp structure)
  3563                              <1> 	;;test	byte [ebx-3], 80h ; open mode & status flags
  3564                              <1> 	; 04/04/2021 - Retro UNIX 386 v2 (new fsp structure)
  3565                              <1> 	;test	byte [ebx-4], 80h ; open mode & status flags 
  3566                              <1> 	;jz	short fclose_1	; deleted file flag (bit 7)
  3567                              <1> 
  3568                              <1> 	; 04/04/2021
  3569 00004993 31D2                <1> 	xor	edx, edx
  3570                              <1> 	; 08/01/2022
  3571 00004995 8A53FD              <1> 	mov	dl, [ebx-3] ; open mode & status flags
  3572 00004998 F6C280              <1> 	test	dl, 80h	    ; deleted file flag (bit 7)
  3573 0000499B 7409                <1> 	jz	short fclose_1 ; (not deleted -while open-)
  3574 0000499D 52                  <1> 	push	edx
  3575                              <1> 
  3576                              <1> 	; deleted file !
  3577                              <1> 
  3578                              <1> 	;mov	ax, dx ; *
  3579                              <1> 		; mov r2,r1 / yes, put i-number back into r1
  3580                              <1> 	; AX = inode number
  3581 0000499E E82F050000          <1> 	call	anyi
  3582                              <1> 		; jsr r0,anyi / free all blocks related to i-number
  3583                              <1> 			    ; / check if file appears in fsp again
  3584                              <1> 	; 04/04/2021
  3585 000049A3 5A                  <1> 	pop	edx
  3586                              <1> 	; 08/04/2021
  3587 000049A4 720C                <1> 	jc	short fclose_3 ; error code in eax	
  3588                              <1> 	; 18/08/2020
  3589                              <1> 	; jmp short fclose_2
  3590                              <1> 
  3591                              <1> 	; ax = inode number
  3592                              <1> fclose_1: ; 2:
  3593                              <1> 	; 04/04/2021
  3594 000049A6 80E201              <1> 	and	dl, 1
  3595 000049A9 FEC2                <1> 	inc	dl ; 0 -> 1 (open for read), 1 -> 2 (open for write)
  3596                              <1> 	
  3597                              <1> 	;pop	eax ; * ; 18/08/2020 
  3598                              <1> 	;;pop	ax ; *
  3599                              <1> 		; mov (sp)+,r1 / put i-number back into r1
  3600 000049AB E872150000          <1> 	call	iclose ; close if it is special file 
  3601                              <1> 		; jsr r0,iclose / check to see if it is a special file
  3602                              <1> 	; 08/04/2021
  3603 000049B0 7302                <1> 	jnc	short fclose_2
  3604                              <1> fclose_3:
  3605 000049B2 5B                  <1> 	pop	ebx  ; ***  ; file descriptor
  3606                              <1> 	; eax = error code
  3607 000049B3 C3                  <1> 	retn
  3608                              <1> fclose_2: ; 1:
  3609 000049B4 58                  <1> 	pop	eax ; *** ; 18/08/2020
  3610                              <1> 	;pop	ax ; ***
  3611                              <1> 		; mov (sp)+,r1 / put index to u.fp back into r1
  3612 000049B5 C3                  <1> 	retn
  3613                              <1> 		; rts r0
  3614                              <1> 
  3615                              <1> 	; 08/01/2022
  3616                              <1> 	; 04/12/2021 - Retro UNIX 386 v2 fs compatibility code
  3617                              <1> getf:	; / get the device number and the i-number of an open file
  3618                              <1> 	; 13/05/2015
  3619                              <1> 	; 11/05/2015 (Retro UNIX 386 v1 - Beginning)
  3620                              <1> 	; 19/04/2013 - 18/11/2013 (Retro UNIX 8086 v1)
  3621                              <1> 
  3622                              <1> 	; 04/12/2021 - Retro UNIX 386 v1.2
  3623                              <1> 	;
  3624                              <1> 	; 16/05/2021
  3625                              <1> 	; 27/03/2020 - Retro UNIX 386 v2 
  3626                              <1> 	;	(new open files -fsp- structure)
  3627                              <1> 	; 13/05/2015
  3628                              <1> 	; 11/05/2015 (Retro UNIX 386 v1 - Beginning)
  3629                              <1> 	; 19/04/2013 - 18/11/2013 (Retro UNIX 8086 v1)
  3630                              <1> 	
  3631                              <1> 	; INPUT: 
  3632                              <1> 	;    ;eax = file descriptor/number
  3633                              <1> 	;     ebx = file descriptor/number ; 18/08/2020 	
  3634                              <1> 	; OUTPUT:
  3635                              <1> 	;     bl = open mode & status flag ; 27/03/2020
  3636                              <1> 	;        (bit 0 open mode flag, 0 = read, 1 = write)  
  3637                              <1> 	;    eax = inode number (in AX)
  3638                              <1> 	;          If eax = 0 -> file not open
  3639                              <1> 	;   byte [cdev] = logical drive number (of inode in eax)
  3640                              <1> 	;	
  3641                              <1> 	; Modified registers: eax, ebx
  3642                              <1> 
  3643                              <1> 	; 16/05/2021 ('getf' procedure, unix v7 x86 'fio.c')
  3644                              <1> 	; /*
  3645                              <1> 	;  * Convert a user supplied
  3646                              <1> 	;  * file descriptor into a pointer
  3647                              <1> 	;  * to a file structure.
  3648                              <1> 	;  * Only task is to check range
  3649                              <1> 	;  * of the descriptor.
  3650                              <1> 	;  */
  3651                              <1> 	; struct file *
  3652                              <1> 	; getf(f)
  3653                              <1> 	;
  3654                              <1> 	; 18/08/2020
  3655                              <1> 	; 	ebx = file descriptor/number
  3656                              <1> 
  3657                              <1> 	; 16/05/2021
  3658                              <1> 	;NOFILE	equ 10 ; 08/01/2022
  3659                              <1> 
  3660                              <1> 	; 18/08/2020
  3661                              <1> 	;mov	ebx, eax
  3662                              <1> getf1: ;; Calling point from 'rw1' (23/05/2013)
  3663                              <1> 
  3664                              <1> 	; ebx = file descriptor/number
  3665                              <1> 
  3666                              <1> 	; 18/08/2020
  3667 000049B6 29C0                <1> 	sub	eax, eax ; eax = 0
  3668                              <1>  
  3669 000049B8 83FB0A              <1> 	cmp	ebx, 10 ; cmp ebx, NOFILE ; 16/05/2021
  3670                              <1> 		; cmp r1,$10. / user limited to 10 open files
  3671 000049BB 732E                <1>         jnb	short getf2 ; 13/05/2015
  3672                              <1> 	;jnb	error
  3673                              <1> 		; bhis error3 / u.fp is table of users open files,
  3674                              <1> 			    ; / index in fsp table
  3675                              <1> 	; 08/01/2022
  3676 000049BD 8A83[E2740000]      <1> 	mov	al, [ebx+u.fp]
  3677                              <1> 	;mov	bl, [ebx+u.fp]
  3678                              <1> 		; movb u.fp(r1),r1 / r1 contains number of entry 
  3679                              <1> 		                 ; / in fsp table
  3680 000049C3 08C0                <1> 	or	al, al ; 08/01/2022
  3681                              <1> 	;or	bl, bl
  3682                              <1> 	;jnz	short getf3
  3683 000049C5 7424                <1> 	jz	short getf2 ; 18/08/2020
  3684                              <1> 	;;jz	short getf4
  3685                              <1> 		; beq 1f / if its zero return
  3686                              <1> ;getf2:
  3687                              <1> ;	; 'File not open !' error (ax=0)
  3688                              <1> ;	;sub	eax, eax ; 18/08/2020
  3689                              <1> ;	retn
  3690                              <1> ;getf3:	
  3691                              <1> 	; Retro UNIX 386 v1 modification ! (11/05/2015)
  3692                              <1> 	;
  3693                              <1> 	; 'fsp' table (10 bytes/entry)
  3694                              <1> 	; bit 15				   bit 0
  3695                              <1> 	; ---|-------------------------------------------
  3696                              <1> 	; r/w|		i-number of open file
  3697                              <1> 	; ---|-------------------------------------------
  3698                              <1> 	;		   device number
  3699                              <1> 	; -----------------------------------------------
  3700                              <1> 	; offset pointer, r/w pointer to file (bit 0-15)
  3701                              <1> 	; -----------------------------------------------
  3702                              <1> 	; offset pointer, r/w pointer to file (bit 16-31)
  3703                              <1> 	; ----------------------|------------------------
  3704                              <1> 	;  flag that says file 	| number of processes
  3705                              <1> 	;   has been deleted	| that have file open 
  3706                              <1> 	; ----------------------|------------------------
  3707                              <1> 
  3708                              <1> 	; Retro UNIX 386 v2 modification ! (27/03/2020)
  3709                              <1> 
  3710                              <1> 	; bit 15 				    bit 0
  3711                              <1> 	; ----------------------------------------------- byte 0
  3712                              <1> 	;    	       i-number of open file           
  3713                              <1> 	; ----------------------------------------------- byte 2
  3714                              <1> 	;          high word of 32 bit i-number       
  3715                              <1> 	; ----------------------------------------------- byte 4
  3716                              <1> 	;    open mode & status  |   device number     
  3717                              <1> 	; ----------------------------------------------- byte 6
  3718                              <1> 	;       reserved byte    |    open count  
  3719                              <1> 	; ----------------------------------------------- byte 8
  3720                              <1> 	;   offset pointer, i.e., r/w pointer to file 
  3721                              <1> 	; ----------------------------------------------- byte 10
  3722                              <1> 	;     64 bit file offset pointer (bit 16-31)  
  3723                              <1> 	; ----------------------------------------------- byte 12
  3724                              <1> 	;     64 bit file offset pointer (bit 32-47)   
  3725                              <1> 	; ----------------------------------------------- byte 14
  3726                              <1> 	;     64 bit file offset pointer (bit 48-63)  
  3727                              <1> 	; ----------------------------------------------- byte 16
  3728                              <1> 	
  3729                              <1> 	; 11/05/2015 - Retro UNIX 386 v1
  3730                              <1> 	;mov	eax, 10
  3731                              <1> 
  3732                              <1> 	; 27/03/2020 - Retro UNIX 386 v2	
  3733                              <1> 	;mov	eax, opfls.size ; mov eax, 16
  3734                              <1> 	
  3735                              <1> 	;sub	eax, eax ; 18/08/2020 ; eax = 0
  3736                              <1> 
  3737                              <1> 	;mov	al, opfls.size  ; mov al, 16
  3738                              <1> 	;
  3739                              <1> 	;mul	bl
  3740                              <1> 	;;mov	ebx, fsp-6 ; the 3rd word in the fsp entry
  3741                              <1> 	;; 27/03/2020
  3742                              <1> 	;mov	ebx, fsp-(opfls.size-opfls.offset) ; fsp-8
  3743                              <1> 	
  3744                              <1> 	;add	ebx, eax
  3745                              <1> 		; asl r1
  3746                              <1> 		; asl r1 / multiply by 8 to get index into 
  3747                              <1> 		       ; / fsp table entry
  3748                              <1> 		; asl r1
  3749                              <1> 		; add $fsp-4,r1 / r1 is pointing at the 3rd word 
  3750                              <1> 			      ; / in the fsp entry
  3751                              <1> 	; 08/01/2022
  3752 000049C7 88C3                <1> 	mov	bl, al
  3753                              <1> 	; 18/08/2020
  3754 000049C9 FECB                <1> 	dec	bl ; zero based fsp table entry number
  3755                              <1> 	;shl	bl, 4 ; * 16 ; opfls.size
  3756                              <1> 	; 08/01/2022
  3757 000049CB C1E304              <1> 	shl	ebx, 4 ; * 16 ; opfls.size
  3758 000049CE 81C3[7C710000]      <1> 	add	ebx, fsp+8 ; opfls.offset	
  3759                              <1> 	; ebx = address of the file offset pointer 
  3760                              <1> 	;	(not file offset! not fsp entry address !)
  3761                              <1> 
  3762 000049D4 891D[EC740000]      <1> 	mov	[u.fofp], ebx
  3763                              <1> 		; mov r1,u.fofp / save address of 3rd word 
  3764                              <1> 			     ; / in fsp entry in u.fofp
  3765                              <1> 	; 18/08/2020
  3766 000049DA 8A43FD              <1> 	mov	al, [ebx-3]  ; open mode & status flags
  3767                              <1> 
  3768 000049DD 50                  <1> 	push	eax
  3769                              <1> 
  3770                              <1> 	;mov	ax, [ebx]
  3771                              <1> 	;;mov	[cdev], al ; ;;Retro UNIX 8086 v1 ! 
  3772                              <1> 	;mov	[cdev], ax ; ;;in fact (!) 
  3773                              <1> 			     ;;dev number is in 1 byte
  3774                              <1> 		; mov -(r1),cdev / remove the device number cdev
  3775                              <1> 
  3776                              <1> 	; 18/08/2020
  3777 000049DE 8A43FC              <1> 	mov	al, [ebx-4]  ; logical drive number
  3778                              <1> 
  3779 000049E1 A2[B9740000]        <1> 	mov	[cdev], al
  3780                              <1> 
  3781                              <1> 	;dec	ebx
  3782                              <1> 	;dec	ebx
  3783                              <1> 	; 18/08/2020
  3784 000049E6 668B43F8            <1> 	mov	ax, [ebx-8]
  3785                              <1> 		; mov -(r1),r1 / and the i-number r1
  3786                              <1> 
  3787 000049EA 5B                  <1> 	pop	ebx ; ebx = bl = open mode and status flag
  3788                              <1> 		; (bit 0 is open mode flag, 0 = read, 1 = write)
  3789                              <1> 	; eax = inode number (in ax)	
  3790                              <1> 
  3791                              <1> getf2:	; 18/08/2020
  3792                              <1> getf4:	; 1: ; 08/01/2022
  3793 000049EB C3                  <1> 	retn
  3794                              <1> 		; rts r0
  3795                              <1> 
  3796                              <1> 	; 08/01/2022
  3797                              <1> 	; 19/12/2021
  3798                              <1> 	; 22/11/2021 - Retro UNIX 386 v2 compatibility modification
  3799                              <1> namei:
  3800                              <1> 	; 18/10/2021
  3801                              <1> 	; 12/06/2021
  3802                              <1> 	; 01/05/2021
  3803                              <1> 	; 26/03/2021
  3804                              <1> 	; 25/03/2021 (Retro UNIX 386 v2 - Beginning)
  3805                              <1> 	; 04/12/2015 (14 byte file names)
  3806                              <1> 	; 18/10/2015 (nbase, ncount)
  3807                              <1> 	; 12/10/2015
  3808                              <1> 	; 21/08/2015
  3809                              <1> 	; 18/07/2015
  3810                              <1> 	; 02/07/2015
  3811                              <1> 	; 17/06/2015
  3812                              <1> 	; 16/06/2015 (Retro UNIX 386 v1 - Beginning)
  3813                              <1> 	; 24/04/2013 - 31/07/2013 (Retro UNIX 8086 v1)
  3814                              <1> 	;
  3815                              <1> 	; 'namei' takes a file path name and returns i-number of
  3816                              <1> 	; the file in the current directory or the root directory
  3817                              <1> 	; (if the first character of the pathname is '/').	
  3818                              <1> 	;
  3819                              <1> 	; INPUTS ->
  3820                              <1> 	;    u.namep - points to a file path name
  3821                              <1> 	;    u.cdir - i-number of users directory
  3822                              <1> 	;    u.cdev - device number on which user directory resides	
  3823                              <1> 	; OUTPUTS ->
  3824                              <1> 	;    r1 - i-number of file
  3825                              <1> 	;    cdev
  3826                              <1> 	;    u.dirbuf - points to directory entry where a match 
  3827                              <1> 	;               occurs in the search for file path name.
  3828                              <1> 	;	        If no match u.dirb points to the end of 
  3829                              <1> 	;               the directory and r1 = i-number of the current
  3830                              <1> 	;	        directory.	
  3831                              <1> 	; ((AX = R1))
  3832                              <1> 	;
  3833                              <1> 	; (Retro UNIX Prototype : 07/10/2012 - 05/01/2013, UNIXCOPY.ASM)
  3834                              <1>         ; ((Modified registers: eDX, eBX, eCX, eSI, eDI, eBP))  
  3835                              <1> 	;
  3836                              <1> 
  3837                              <1> 	;mov	ax, [u.cdir]
  3838                              <1> 		; mov u.cdir,r1 / put the i-number of current directory
  3839                              <1> 			      ; / in r1
  3840                              <1> 	; 01/05/2021
  3841 000049EC A1[DC740000]        <1> 	mov	eax, [u.cdir]	
  3842 000049F1 8A15[E0740000]      <1> 	mov	dl, [u.cdrv]
  3843 000049F7 8815[B9740000]      <1> 	mov	[cdev], dl
  3844                              <1> 	;mov	dx, [u.cdrv]
  3845                              <1> 	;mov	[cdev], dx	; NOTE: Retro UNIX 8086 v1 
  3846                              <1> 				; device/drive number is in 1 byte, 
  3847                              <1> 				; not in 1 word!
  3848                              <1> 		; mov u.cdev,cdev / device number for users directory 
  3849                              <1> 				; / into cdev
  3850                              <1> 	; 12/10/2015
  3851                              <1> 	; 16/06/2015 - 32 bit modifications (Retro UNIX 386 v1)
  3852                              <1>       	; convert virtual (pathname) addr to physical address
  3853 000049FD E808010000          <1> 	call    trans_addr_nmbp ; 12/10/2015
  3854                              <1> 		; esi = physical address of [u.namep]
  3855                              <1> 		; ecx = byte count in the page
  3856 00004A02 803E2F              <1> 	cmp	byte [esi], '/'
  3857                              <1> 		; cmpb *u.namep,$'/ / is first char in file name a /
  3858 00004A05 751C                <1> 	jne	short namei_1
  3859                              <1> 		; bne 1f
  3860 00004A07 FF05[F4740000]      <1> 	inc	dword [u.namep]
  3861                              <1> 		; inc u.namep / go to next char
  3862                              <1> 	;dec	cx ; remain byte count in the page
  3863 00004A0D 49                  <1> 	dec	ecx ; 18/10/2021
  3864 00004A0E 7506                <1> 	jnz	short namei_0
  3865                              <1> 	; 12/10/2015
  3866 00004A10 E8F5000000          <1> 	call	trans_addr_nmbp ; convert virtual address to physical
  3867                              <1> 		; esi = physical address (page start + offset)
  3868                              <1> 		; ecx = byte count in the page
  3869 00004A15 4E                  <1> 	dec	esi
  3870                              <1> namei_0:
  3871 00004A16 46                  <1> 	inc 	esi  ; go to next char
  3872                              <1> 	;mov	ax, [rootdir] ; 09/07/2013
  3873                              <1> 	; 18/10/2021
  3874 00004A17 A1[C0740000]        <1> 	mov	eax, [rootdir]
  3875                              <1> 		; mov rootdir,r1 / put i-number of rootdirectory in r1
  3876 00004A1C C605[B9740000]00    <1> 	mov	byte [cdev], 0
  3877                              <1> 		; clr cdev / clear device number
  3878                              <1> namei_1: ; 1:
  3879 00004A23 F606FF              <1> 	test	byte [esi], 0FFh
  3880                              <1> 	;jz	short getf4
  3881                              <1> 	; 26/03/2021
  3882                              <1> 	;jz	short getf2 ; retn
  3883                              <1> 	;;jz	nig
  3884                              <1> 		; tstb *u.namep / is the character in file name a nul
  3885                              <1> 		; beq nig / yes, end of file name reached; 
  3886                              <1> 			; / branch to "nig"
  3887                              <1> 	; 19/12/2021
  3888                              <1> namei_9:
  3889                              <1> 	; 12/06/2021
  3890                              <1> 	;jnz	short namei_2
  3891                              <1> 	;retn
  3892                              <1> 	; 08/01/2022
  3893 00004A26 74C3                <1> 	jz	short getf4
  3894                              <1> namei_2: ; 1:
  3895                              <1> 	; 18/10/2015
  3896 00004A28 8935[5C750000]      <1> 	mov 	[nbase], esi
  3897                              <1> 	;mov 	[ncount], cx
  3898                              <1> 	; 18/10/2021
  3899 00004A2E 890D[60750000]      <1> 	mov	[ncount], ecx
  3900                              <1> 	;
  3901                              <1> 	;;mov	dx, 2
  3902                              <1> 	;mov	dl, 2 ; user flag (read, non-owner)
  3903                              <1> 	; 25/03/2021
  3904                              <1> 	;mov	dx, 100h ; IREAD - read, owner 
  3905                              <1> 	; 18/10/2021
  3906 00004A34 29D2                <1> 	sub	edx, edx
  3907 00004A36 FEC6                <1> 	inc	dh ; dx = 100h ; IREAD - read, owner 
  3908 00004A38 E8340B0000          <1> 	call	access
  3909                              <1> 		; jsr r0,access; 2 / get i-node with i-number r1
  3910                              <1> 	; 'access' will not return here if user has not "r" permission !
  3911                              <1> 
  3912                              <1> 	;test 	word [i.flgs], 4000h
  3913                              <1> 	;	; bit $40000,i.flgs / directory i-node?
  3914                              <1>         ;jz	short namei_err
  3915                              <1> 		; beq error3 / no, got an error
  3916                              <1> 	; 26/03/2021 
  3917                              <1> ;	mov	al, [i.flgs+1]
  3918                              <1> ;	;test	al, 80h ; IFREG ; 80h
  3919                              <1> ;	;jz	short namei_err ; not a regular file ! (device!?)
  3920                              <1> ;	;and	al, 40h ; IFDIR ; 40h
  3921                              <1> ;	;jz	short namei_err ; not a sub directory !
  3922                              <1> ;	shl	al, 1 ; 80h (IFREG flag) -> cf = 1
  3923                              <1> ;	jnc	short namei_err ; not a regular file ! (device!?)
  3924                              <1> ;	shl	al, 1 ; 80h (IFDIR flag) -> cf = 1
  3925                              <1> ;	jnc	short namei_err ; not a sub directory !
  3926                              <1> 
  3927                              <1> 	; 27/03/2021
  3928 00004A3D E8F9000000          <1> 	call	chk_dir ; check for directory, jump to 'error' if not
  3929                              <1> 	; CPU will be here if the inode is a (valid) directory inode
  3930                              <1> 
  3931                              <1> 	; 16/06/2015 - 32 bit modifications (Retro UNIX 386 v1)
  3932                              <1> 	;xor	eax, eax
  3933                              <1> 	;mov	[u.off], eax ; 0
  3934                              <1> 	; 01/05/2021 - Retro UNIX 386 v2 inode structure
  3935 00004A42 A1[7C700000]        <1> 	mov	eax, [i.size]
  3936                              <1> 	;mov	ax, [i.size]
  3937 00004A47 A3[F0740000]        <1> 	mov	[u.dirp], eax
  3938                              <1> 		; mov i.size,u.dirp / put size of directory in u.dirp
  3939                              <1> 		; clr u.off / u.off is file offset used by user
  3940                              <1> 	; 18/10/2021
  3941 00004A4C 31C0                <1> 	xor	eax, eax
  3942                              <1> 	;mov	[u.off], eax ; 0
  3943 00004A4E BB[F8740000]        <1> 	mov	ebx, u.off
  3944 00004A53 8903                <1> 	mov	[ebx], eax ; mov dword [u.off], 0
  3945                              <1> 	;
  3946 00004A55 891D[EC740000]      <1> 	mov	[u.fofp], ebx ; u.off ; 18/10/2021
  3947                              <1> 	;mov	dword [u.fofp], u.off
  3948                              <1> 			; mov $u.off,u.fofp / u.fofp is a pointer to 
  3949                              <1> 				  ; / the offset portion of fsp entry
  3950                              <1> namei_3: ; 2:
  3951 00004A5B C705[FC740000]-     <1> 	mov	dword [u.base], u.dirbuf
  3951 00004A61 [10750000]          <1>
  3952                              <1> 		; mov $u.dirbuf,u.base / u.dirbuf holds a file name 
  3953                              <1> 				    ; / copied from a directory
  3954 00004A65 C705[00750000]1000- <1> 	mov 	dword [u.count], 16 ; 04/12/2015 (10 -> 16) 	
  3954 00004A6D 0000                <1>
  3955                              <1>  		; mov $10.,u.count / u.count is byte count 
  3956                              <1> 				 ; / for reads and writes
  3957                              <1> 	;mov 	ax, [ii]
  3958                              <1> 	; 18/10/2021
  3959 00004A6F A1[B4740000]        <1> 	mov	eax, [ii] ; (32 bit inode number)
  3960                              <1> 	; 31/07/2013 ('namei_r') - 16/06/2015 ('u.kcall')
  3961 00004A74 FE05[4A750000]      <1>  	inc     byte [u.kcall] ; the caller is 'namei' sign	
  3962 00004A7A E8820D0000          <1>     	call	readi
  3963                              <1> 		; jsr r0,readi / read 10. bytes of file 
  3964                              <1> 		      ; with i-number (r1); i.e. read a directory entry
  3965                              <1> 
  3966 00004A7F 8B0D[04750000]      <1> 	mov 	ecx, [u.nread]
  3967 00004A85 09C9                <1> 	or 	ecx, ecx
  3968                              <1> 		; tst u.nread
  3969 00004A87 741A                <1> 	jz	short nib
  3970                              <1> 		; ble nib / gives error return
  3971                              <1> 	;
  3972                              <1> 	;mov 	bx, [u.dirbuf]
  3973                              <1> 	;and 	bx, bx       
  3974                              <1> 	; 18/10/2021
  3975 00004A89 66F705[10750000]FF- <1> 	test	word [u.dirbuf], 0FFFFh
  3975 00004A91 FF                  <1>
  3976                              <1> 		; tst u.dirbuf /
  3977 00004A92 7513                <1> 	jnz	short namei_4
  3978                              <1> 		; bne 3f / branch when active directory entry 
  3979                              <1> 		       ; / (i-node word in entry non zero)
  3980 00004A94 A1[F8740000]        <1> 	mov	eax, [u.off]
  3981 00004A99 83E810              <1> 	sub	eax, 16 ; 04/12/2015 (10 -> 16) 
  3982 00004A9C A3[F0740000]        <1> 	mov	[u.dirp], eax
  3983                              <1> 		; mov u.off,u.dirp
  3984                              <1> 		; sub $10.,u.dirp
  3985 00004AA1 EBB8                <1> 	jmp	short namei_3
  3986                              <1> 		; br 2b
  3987                              <1> 
  3988                              <1> 	; 18/07/2013
  3989                              <1> nib: 
  3990 00004AA3 31C0                <1> 	xor	eax, eax  ; xor ax, ax ; ax = 0 -> file not found 
  3991 00004AA5 F9                  <1> 	stc
  3992                              <1> nig:
  3993 00004AA6 C3                  <1> 	retn
  3994                              <1> 
  3995                              <1> 	; 27/03/2021
  3996                              <1> ;namei_err:
  3997                              <1> 	; 16/06/2015
  3998                              <1> ;	mov	dword [u.error], ERR_NOT_DIR ; 'not a directory !' error
  3999                              <1> ;	jmp	error
  4000                              <1> 
  4001                              <1> namei_4: ; 3:
  4002                              <1> 	; 18/10/2015
  4003                              <1> 	; 12/10/2015
  4004                              <1> 	; 21/08/2015
  4005                              <1> 	; 18/07/2015
  4006 00004AA7 8B2D[F4740000]      <1> 	mov	ebp, [u.namep]
  4007                              <1> 		; mov u.namep,r2 / u.namep points into a file name string
  4008 00004AAD BF[12750000]        <1> 	mov 	edi, u.dirbuf + 2
  4009                              <1> 		; mov $u.dirbuf+2,r3 / points to file name of directory entry
  4010                              <1> 	; 18/10/2015
  4011 00004AB2 8B35[5C750000]      <1> 	mov	esi, [nbase]
  4012                              <1> 	;mov	cx, [ncount]
  4013                              <1> 	;and	cx, cx
  4014                              <1> 	; 18/10/2021
  4015 00004AB8 8B0D[60750000]      <1> 	mov	ecx, [ncount]
  4016 00004ABE 21C9                <1> 	and	ecx, ecx
  4017 00004AC0 7505                <1> 	jnz	short namei_5	
  4018                              <1> 	;
  4019 00004AC2 E849000000          <1> 	call	trans_addr_nm ; convert virtual address to physical
  4020                              <1> 		; esi = physical address (page start + offset)
  4021                              <1> 		; ecx = byte count in the page
  4022                              <1> namei_5: ; 3:
  4023 00004AC7 45                  <1> 	inc	ebp ; 18/07/2015
  4024 00004AC8 AC                  <1> 	lodsb   ; mov al, [esi] ; inc esi (al = r4)
  4025                              <1> 		; movb (r2)+,r4 / move a character from u.namep string into r4
  4026 00004AC9 08C0                <1> 	or 	al, al
  4027 00004ACB 741C                <1> 	jz 	short namei_7
  4028                              <1> 		; beq 3f / if char is nul, then the last char in string
  4029                              <1> 			; / has been moved
  4030 00004ACD 3C2F                <1> 	cmp	al, '/'
  4031                              <1> 		; cmp r4,$'/ / is char a </>
  4032 00004ACF 7418                <1> 	je 	short namei_7
  4033                              <1> 		; beq 3f	
  4034                              <1> 	; 12/10/2015
  4035                              <1> 	;dec	cx ; remain byte count in the page
  4036 00004AD1 49                  <1> 	dec	ecx ; 18/10/2021
  4037 00004AD2 7505                <1> 	jnz	short namei_6
  4038 00004AD4 E837000000          <1> 	call	trans_addr_nm ; convert virtual address to physical
  4039                              <1> 		; esi = physical address (page start + offset)
  4040                              <1> 		; ecx = byte count in the page
  4041                              <1> namei_6:
  4042 00004AD9 81FF[20750000]      <1>         cmp     edi, u.dirbuf + 16 ; 04/12/2015 (10 -> 16) 
  4043                              <1> 		; cmp r3,$u.dirbuf+10. / have I checked
  4044                              <1> 				     ; / all 8 bytes of file name
  4045 00004ADF 74E6                <1> 	je	short namei_5
  4046                              <1> 		; beq 3b
  4047 00004AE1 AE                  <1> 	scasb	
  4048                              <1> 		; cmpb (r3)+,r4 / compare char in u.namep string to file name 
  4049                              <1> 			      ; / char read from directory
  4050 00004AE2 74E3                <1> 	je 	short namei_5
  4051                              <1> 		; beq 3b / branch if chars match
  4052                              <1> namei_10:
  4053 00004AE4 E972FFFFFF          <1>         jmp	namei_3 ; 2b
  4054                              <1> 		; br 2b / file names do not match go to next directory entry
  4055                              <1> namei_7: ; 3:
  4056 00004AE9 81FF[20750000]      <1> 	cmp	edi, u.dirbuf + 16 ; 04/12/2015 (10 -> 16) 
  4057                              <1> 		; cmp r3,$u.dirbuf+10. / if equal all 8 bytes were matched
  4058 00004AEF 7405                <1> 	je	short namei_8
  4059                              <1> 		; beq 3f
  4060                              <1> 	;mov 	ah, [edi]
  4061                              <1> 	;;inc 	edi 
  4062                              <1> 	;and 	ah, ah
  4063                              <1> 	;	; tstb (r3)+ /
  4064                              <1>         ; 18/10/2021
  4065 00004AF1 F607FF              <1> 	test	byte [edi], 0FFh
  4066                              <1> 	; 08/01/2021
  4067 00004AF4 75EE                <1> 	jnz	short namei_10 
  4068                              <1> 	;jnz	namei_3
  4069                              <1> 		; bne 2b
  4070                              <1> namei_8: ; 3
  4071 00004AF6 892D[F4740000]      <1> 	mov	[u.namep], ebp ; 18/07/2015
  4072                              <1> 		; mov r2,u.namep / u.namep points to char 
  4073                              <1> 			       ; / following a / or nul
  4074                              <1> 	;mov	bx, [u.dirbuf]
  4075                              <1> 		; mov u.dirbuf,r1 / move i-node number in directory 
  4076                              <1> 				; / entry to r1
  4077 00004AFC 20C0                <1> 	and 	al, al
  4078                              <1> 		; tst r4 / if r4 = 0 the end of file name reached,
  4079                              <1> 		      ;  / if r4 = </> then go to next directory
  4080                              <1> 	;mov	ax, bx
  4081                              <1> 	;mov 	ax, [u.dirbuf] ; 17/06/2015
  4082                              <1>         ; 18/10/2021 (16 bit inode number in 32 bit register)
  4083 00004AFE 0FB705[10750000]    <1> 	movzx	eax, word [u.dirbuf] 
  4084                              <1> 	; 19/12/2021
  4085 00004B05 E91CFFFFFF          <1> 	jmp	namei_9 ; eax = inode number
  4086                              <1> ;	jnz	namei_2 
  4087                              <1> ;		; bne 1b
  4088                              <1> ;	; AX = i-number of the file
  4089                              <1> ;;;nig:
  4090                              <1> ;	retn
  4091                              <1> 		; tst (r0)+ / gives non-error return
  4092                              <1> ;;nib:
  4093                              <1> ;;	xor	ax, ax ; Retro UNIX 8086 v1 modification !
  4094                              <1> 		       ; ax = 0 -> file not found 
  4095                              <1> ;;	stc	; 27/05/2013
  4096                              <1> ;;	retn
  4097                              <1> 		; rts r0
  4098                              <1> 
  4099                              <1> trans_addr_nmbp:
  4100                              <1> 	; 18/10/2021 - Retro UNIX 386 v2
  4101                              <1> 	; 18/10/2015
  4102                              <1> 	; 12/10/2015
  4103 00004B0A 8B2D[F4740000]      <1> 	mov 	ebp, [u.namep]
  4104                              <1> trans_addr_nm: 
  4105                              <1> 	; 18/10/2021 - Retro UNIX 386 v2
  4106                              <1> 	; Convert virtual (pathname) address to physical address
  4107                              <1> 	; (Retro UNIX 386 v1 feature only !)
  4108                              <1> 	; 18/10/2015
  4109                              <1> 	; 12/10/2015 (u.pnbase & u.pncount has been removed from code)
  4110                              <1> 	; 02/07/2015
  4111                              <1> 	; 17/06/2015
  4112                              <1> 	; 16/06/2015
  4113                              <1> 	;
  4114                              <1> 	; INPUTS: 
  4115                              <1> 	;	ebp = pathname address (virtual) ; [u.namep]
  4116                              <1> 	;	[u.pgdir] = user's page directory
  4117                              <1> 	; OUTPUT:
  4118                              <1> 	;       esi = physical address of the pathname
  4119                              <1> 	;	ecx = remain byte count in the page
  4120                              <1> 	;
  4121                              <1> 	; (Modified registers: EBX, ECX, EDX, ESI) ; 18/10/2021
  4122                              <1> 	;
  4123                              <1> 
  4124                              <1> 	; 18/10/2021
  4125 00004B10 29C9                <1> 	sub	ecx, ecx
  4126                              <1> 	;
  4127                              <1>         ;cmp	dword [u.ppgdir], 0  ; /etc/init ? (sysexec)
  4128 00004B12 390D[3C750000]      <1> 	cmp	[u.ppgdir], ecx ; 0 ; 18/10/2021
  4129 00004B18 7618                <1> 	jna	short trans_addr_nmk ; the caller is os kernel;
  4130                              <1> 				     ; it is already physical address
  4131 00004B1A 50                  <1>    	push	eax	
  4132 00004B1B 89EB                <1> 	mov	ebx, ebp ; [u.namep] ; pathname address (virtual)
  4133 00004B1D E830E7FFFF          <1>        	call	get_physical_addr ; get physical address
  4134 00004B22 7204                <1> 	jc	short tr_addr_nm_err
  4135                              <1> 	; 18/10/2015
  4136                              <1> 	; eax = physical address 
  4137                              <1> 	; ecx = remain byte count in page (1-4096) ; 18/10/2021
  4138                              <1> 		; 12/10/2015 (cx = [u.pncount])
  4139 00004B24 89C6                <1> 	mov	esi, eax ; 12/10/2015 (esi=[u.pnbase])
  4140 00004B26 58                  <1> 	pop	eax 
  4141 00004B27 C3                  <1> 	retn
  4142                              <1> 
  4143                              <1> tr_addr_nm_err:
  4144 00004B28 A3[4C750000]        <1> 	mov	[u.error], eax
  4145                              <1> 	;pop 	eax
  4146 00004B2D E978F0FFFF          <1> 	jmp	error
  4147                              <1> 
  4148                              <1> trans_addr_nmk:
  4149                              <1> 	; 12/10/2015
  4150                              <1> 	; 02/07/2015
  4151 00004B32 8B35[F4740000]      <1> 	mov	esi, [u.namep]  ; [u.pnbase]
  4152                              <1> 	;mov	cx, PAGE_SIZE ; 4096 ; [u.pncount]
  4153                              <1> 	; 18/10/2021
  4154 00004B38 B510                <1> 	mov	ch, PAGE_SIZE/256
  4155 00004B3A C3                  <1> 	retn
  4156                              <1> 
  4157                              <1> 	; 22/11/2021 - Retro UNIX 386 v2 compatibility modification
  4158                              <1> chk_dir:
  4159                              <1> 	; 12/06/2021
  4160                              <1> 	; 27/03/2021 (Retro UNIX 386 v2)
  4161                              <1> 	; Check for directory inode
  4162                              <1> 
  4163 00004B3B 8A0D[75700000]      <1> 	mov	cl, [i.flgs+1]
  4164                              <1> 	;test	cl, 80h ; IFREG ; 80h
  4165                              <1> 	;jz	short chk_dir_err ; not a regular file ! (device!?)
  4166                              <1> 	;and	cl, 40h ; IFDIR ; 40h
  4167                              <1> 	;jz	short chk_dir_err ; not a sub directory !
  4168 00004B41 D0E1                <1> 	shl	cl, 1 ; 80h (IFREG flag) -> cf = 1
  4169 00004B43 7305                <1> 	jnc	short chk_dir_err ; not a regular file ! (device!?)
  4170 00004B45 D0E1                <1> 	shl	cl, 1 ; 40h (IFDIR flag) -> cf = 1
  4171 00004B47 7301                <1> 	jnc	short chk_dir_err ; not a sub directory !
  4172                              <1> 	; 12/06/2021
  4173 00004B49 C3                  <1> 	retn
  4174                              <1> chk_dir_err:
  4175 00004B4A C705[4C750000]1300- <1> 	mov	dword [u.error], ERR_NOT_DIR ; 'not a valid directory !'
  4175 00004B52 0000                <1>
  4176 00004B54 E951F0FFFF          <1> 	jmp	error
  4177                              <1> 
  4178                              <1> 	; 08/01/2022
  4179                              <1> 	; 01/01/2022 - Retro UNIX 386 v1.2 (runix v2 fs inode)
  4180                              <1> syschdir:
  4181                              <1> 	; / makes the directory specified in the argument
  4182                              <1> 	; / the current directory
  4183                              <1> 	;
  4184                              <1> 	; 08/01/2022 (Retro UNIX 386 v1.2)
  4185                              <1> 	; 23/06/2015 (Retro UNIX 386 v1 - Beginning)
  4186                              <1> 	; 19/06/2013 (Retro UNIX 8086 v1)
  4187                              <1> 	;
  4188                              <1> 	; 'syschdir' makes the directory specified in its argument
  4189                              <1> 	; the current working directory.
  4190                              <1> 	;
  4191                              <1> 	; Calling sequence:
  4192                              <1> 	;	syschdir; name
  4193                              <1> 	; Arguments:
  4194                              <1> 	;	name - address of the path name of a directory
  4195                              <1> 	;	       terminated by nul byte.	
  4196                              <1> 	; Inputs: -
  4197                              <1> 	; Outputs: -
  4198                              <1> 	; ...............................................................
  4199                              <1> 	;				
  4200                              <1> 	; Retro UNIX 8086 v1 modification:
  4201                              <1> 	;	 The user/application program puts address of 
  4202                              <1> 	;	 the path name in BX register as 'syschdir' 
  4203                              <1> 	; 	 system call argument.
  4204                              <1> 
  4205 00004B59 891D[F4740000]      <1> 	mov	[u.namep], ebx
  4206                              <1> 		;jsr r0,arg; u.namep / u.namep points to path name
  4207 00004B5F E888FEFFFF          <1> 	call	namei
  4208                              <1> 		; jsr r0,namei / find its i-number
  4209                              <1> 	;jc	error
  4210                              <1> 		; br error3
  4211 00004B64 730F                <1> 	jnc	short syschdir0
  4212                              <1> 	; 'directory not found !' error
  4213 00004B66 C705[4C750000]0C00- <1> 	mov	dword [u.error], ERR_DIR_NOT_FOUND ; 12
  4213 00004B6E 0000                <1>
  4214 00004B70 E935F0FFFF          <1> 	jmp	error
  4215                              <1> syschdir0:
  4216                              <1> 	; 08/01/2022
  4217 00004B75 66BA0001            <1> 	mov	dx, 100h  ; read access ; IREAD (100h)
  4218 00004B79 E8F3090000          <1> 	call	access
  4219                              <1> 		; jsr r0,access; 2 / get i-node into core
  4220                              <1> 	
  4221                              <1> 	; 01/01/2022 (runix v2 fs inode flag)
  4222 00004B7E F605[75700000]80    <1> 	test	byte [i.flgs+1], 80h ; regular file ?
  4223 00004B85 7409                <1> 	jz	short syschdir2 ; no, error!
  4224 00004B87 F605[75700000]40    <1> 	test	byte [i.flgs+1], 40h ; directory flag ?
  4225                              <1> 	;test	word [i.flgs], 4000h
  4226                              <1> 		; bit $40000,i.flgs / is it a directory?
  4227                              <1> 	;jz	error 
  4228                              <1> 		; beq error3 / no error
  4229 00004B8E 750F                <1> 	jnz	short syschdir1
  4230                              <1> syschdir2:
  4231 00004B90 C705[4C750000]1300- <1> 	mov	dword [u.error], ERR_NOT_DIR ; 'not a valid directory !'
  4231 00004B98 0000                <1>
  4232 00004B9A E90BF0FFFF          <1> 	jmp	error
  4233                              <1> syschdir1:
  4234 00004B9F 66A3[DC740000]      <1> 	mov	[u.cdir], ax
  4235                              <1> 		; mov r1,u.cdir / move i-number to users 
  4236                              <1> 			      ; / current directory
  4237                              <1> 	;mov	ax, [cdev]
  4238                              <1> 	;mov	[u.cdrv], ax
  4239                              <1> 	; 08/01/2022
  4240 00004BA5 A0[B9740000]        <1> 	mov	al, [cdev]
  4241 00004BAA A2[E0740000]        <1> 	mov	[u.cdrv], al
  4242                              <1> 		; mov cdev,u.cdev / move its device to users 
  4243                              <1> 			        ; / current device
  4244 00004BAF E916F0FFFF          <1> 	jmp	sysret
  4245                              <1> 		; br sysret3
  4246                              <1> 
  4247                              <1> syschmod: ; < change mode of file >
  4248                              <1> 	; 09/01/2022 (Retro UNIX 386 v1.2)
  4249                              <1> 	; 01/05/2021
  4250                              <1> 	; 25/05/2020 (Retro UNIX 386 v2 - Beginning)
  4251                              <1> 	; 23/06/2015 (Retro UNIX 386 v1 - Beginning)
  4252                              <1> 	; 20/06/2013 - 07/07/2013 (Retro UNIX 8086 v1)
  4253                              <1> 	;
  4254                              <1> 	; 'syschmod' changes mode of the file whose name is given as
  4255                              <1> 	; null terminated string pointed to by 'name' has it's mode 
  4256                              <1> 	; changed to 'mode'.
  4257                              <1> 	;
  4258                              <1> 	; Calling sequence:
  4259                              <1> 	;	syschmod; name; mode
  4260                              <1> 	; Arguments:
  4261                              <1> 	;	name - address of the file name
  4262                              <1> 	;	       terminated by null byte.
  4263                              <1> 	;	mode - (new) mode/flags < attributes >
  4264                              <1> 	;	
  4265                              <1> 	; Inputs: -
  4266                              <1> 	; Outputs: -
  4267                              <1> 	; ...............................................................
  4268                              <1> 	;				
  4269                              <1> 	; Retro UNIX 8086 v1 modification: 
  4270                              <1> 	;       'syschmod' system call has two arguments; so,
  4271                              <1> 	;	* 1st argument, name is pointed to by BX register
  4272                              <1> 	;	* 2nd argument, mode is in CX register
  4273                              <1> 	;
  4274                              <1> 	; Mode bits (Flags):
  4275                              <1> 	;	bit 15 - 'i-node is allocated' flag (8000h)
  4276                              <1> 	;	bit 14 - directory flag (4000h)
  4277                              <1> 	;	bit 13 - file has modified flag (always on) (2000h)
  4278                              <1> 	;	bit 12 - large file flag (1000h)
  4279                              <1> 	;	bit 6,7,8,9,10,11 are not used (undefined)
  4280                              <1> 	;	bit 5 - set user ID on execution flag (20h) 
  4281                              <1> 	;	bit 4 - executable flag (10h)
  4282                              <1> 	;	bit 3 - read permission for owner (08h)
  4283                              <1> 	;	bit 2 - write permission for owner (04h)
  4284                              <1> 	;	bit 1 - read permission for non-owner (02h)
  4285                              <1> 	;	bit 0 - write permission for non-owner (01h)
  4286                              <1> 
  4287                              <1> 	; 01/05/2021 (07/02/2020)
  4288                              <1> 	; Retro UNIX 386 v2 I-node Flags: (di_mode) for files
  4289                              <1> 	; 	bit 15 - IFREG - regular file (8000h)
  4290                              <1> 	;	bit 14 - IFDIR - directory (4000h)
  4291                              <1> 	;	bit 13 - IRSVD - 0 - reserved/mounted bit (2000h)
  4292                              <1> 	; 	bit 12 - ILARG - large file addressing bit (1000h)
  4293                              <1> 	; 	bit 11 - ISUID - set user id on exec (800h)
  4294                              <1> 	;	bit 10 - ISGID - set group id on exec (400h)
  4295                              <1> 	; 	bit  9 - IEXTT - 0 - use extents (200h)
  4296                              <1> 	;	bit  8 - IREAD - read, owner (100h)
  4297                              <1> 	;	bit  7 - IWRITE - write, owner (80h)
  4298                              <1> 	;	bit  6 - IEXEC - execute, owner (40h)
  4299                              <1> 	; 	bit  5 - read, group (20h)
  4300                              <1> 	; 	bit  4 - write, group (10h)
  4301                              <1> 	; 	bit  3 - execute, group (08h)
  4302                              <1> 	;	bit  2 - read, others (04h)
  4303                              <1> 	; 	bit  1 - write, others (02h)
  4304                              <1> 	; 	bit  0 - execute, others (01h)
  4305                              <1> 	;
  4306                              <1> 	; Retro UNIX 386 v2 I-node Flags: (di_mode) for devices
  4307                              <1> 	;	bit 15 - IFREG - 0 - device file (8000h)
  4308                              <1> 	; 	bit 14 - IFBLK - block device (4000h)
  4309                              <1> 	; 	bit 13 - IFCHR - 1 - character special (2000h)
  4310                              <1> 	;	bit 12 - IFIFO - fifo special (1000h)
  4311                              <1> 	; 	bit 11 - IPIPE - pipe special (800h)
  4312                              <1> 	;	bit 10 - IREDIR - redirected (400h)
  4313                              <1> 	;	bit  9 - IEXTR - external device driver (200h)
  4314                              <1> 	;	bit  8 - IREAD - read, owner (100h)
  4315                              <1> 	;	bit  7 - IWRITE - write, owner (80h)
  4316                              <1> 	;	bit  6 - IEXEC - execute, owner (40h)
  4317                              <1> 	; 	bit  5 - read, group (20h)
  4318                              <1> 	; 	bit  4 - write, group (10h)
  4319                              <1> 	; 	bit  3 - execute, group (08h)
  4320                              <1> 	;	bit  2 - read, others (04h)
  4321                              <1> 	; 	bit  1 - write, others (02h)
  4322                              <1> 	; 	bit  0 - execute, others (01h)
  4323                              <1> 
  4324                              <1> 	; / name; mode
  4325 00004BB4 E870000000          <1> 	call	isown
  4326                              <1> 		;jsr r0,isown / get the i-node and check user status
  4327                              <1> 
  4328                              <1> 	; 25/05/2020
  4329                              <1> 	; AL = new mode (return form 'isown', ecx -> eax)
  4330                              <1> 
  4331                              <1> 	;test	word [i.flgs], 4000h
  4332                              <1> 	;	; bit $40000,i.flgs / directory?
  4333                              <1> 	;jz	short syschmod1
  4334                              <1> 	;	; beq 2f / no
  4335                              <1> 	;; AL = (new) mode
  4336                              <1> 	;and	al, 0CFh ; 11001111b (clears bit 4 & 5)
  4337                              <1> 	;	; bic $60,r2 / su & ex / yes, clear set user id and 
  4338                              <1> 	;		   ; / executable modes
  4339                              <1> 
  4340                              <1> 	; 01/05/2021
  4341                              <1> 	; AX = new mode (9 bits for devices or 12 bits files)
  4342 00004BB9 8A15[75700000]      <1> 	mov	dl, [i.flgs+1]
  4343                              <1> 	; 09/01/2022
  4344 00004BBF F6C280              <1> 	test	dl, 80h ; regular file
  4345 00004BC2 7416                <1> 	jz	short syschmod1 ; device file
  4346 00004BC4 F6C240              <1> 	test	dl, 40h ; directory
  4347 00004BC7 7511                <1> 	jnz	short syschmod1
  4348                              <1> 	; regular file
  4349 00004BC9 6625FF0D            <1> 	and	ax, 0DFFh ; clear bit 9, 12-15 (of new mode)
  4350                              <1> 	; clear bit 8,10,11 of current mode/flags
  4351 00004BCD 80E2F2              <1> 	and	dl, 0F2h ; and dl, 11110010b
  4352                              <1> syschmod0:
  4353 00004BD0 08D4                <1> 	or	ah, dl
  4354 00004BD2 66A3[74700000]      <1> 	mov	[i.flgs], ax	
  4355 00004BD8 EB3E                <1> 	jmp	short syschmod2
  4356                              <1> syschmod1: 
  4357                              <1> 	; device or directory	
  4358 00004BDA 6625B601            <1> 	and	ax, 1B6h ; clear bit 9-15 & bit 0,3,6 (EXEC bits)
  4359                              <1> 	; clear bit 8 (IREAD) of current mode/flags
  4360                              <1> 	;	 (dl contains flag bits 8-15)
  4361 00004BDE 80E2FE              <1> 	and	dl, ~1 ; and dl, 11111110b 
  4362 00004BE1 EBED                <1> 	jmp	short syschmod0
  4363                              <1> 
  4364                              <1> ;syschmod1: ; 2:
  4365                              <1> 	;mov	[i.flgs], al	
  4366                              <1> 	;	; movb r2,i.flgs / move remaining mode to i.flgs
  4367                              <1> 
  4368                              <1> 	; 25/05/2020 - Retro UNIX 386 v2
  4369                              <1> 	;mov	byte [imodx], 1 ; (flag means file data is same
  4370                              <1> 	;			;  but inode's itself has been modified)
  4371                              <1> 	;call	setimod
  4372                              <1> 	;jmp	sysret
  4373                              <1> 
  4374                              <1> 	; 25/05/2020
  4375                              <1> 	;jmp	short syschmod2
  4376                              <1> 
  4377                              <1> syschown: ; < change owner of file >
  4378                              <1> 	; 09/01/2022 (Retro UNIX 386 v1.2)
  4379                              <1> 	; 01/05/2021
  4380                              <1> 	;	(change owner and group ID of file)
  4381                              <1> 	; 02/04/2021
  4382                              <1> 	; 25/05/2020 (Retro UNIX 386 v2 - Beginning)
  4383                              <1> 	; 23/06/2015 (Retro UNIX 386 v1 - Beginning)
  4384                              <1> 	; 20/06/2013 - 02/08/2013 (Retro UNIX 8086 v1)
  4385                              <1> 	;
  4386                              <1> 	; 'syschown' changes the owner of the file whose name is given
  4387                              <1> 	; as null terminated string pointed to by 'name' has it's owner
  4388                              <1> 	; changed to 'owner'
  4389                              <1> 	;
  4390                              <1> 	; Calling sequence:
  4391                              <1> 	;	syschown; name; owner
  4392                              <1> 	; Arguments:
  4393                              <1> 	;	name - address of the file name
  4394                              <1> 	;	       terminated by null byte.
  4395                              <1> 	;	owner - (new) owner (number/ID)
  4396                              <1> 	;	
  4397                              <1> 	; Inputs: -
  4398                              <1> 	; Outputs: -
  4399                              <1> 	; ...............................................................
  4400                              <1> 	;				
  4401                              <1> 	; Retro UNIX 8086 v1 modification: 
  4402                              <1> 	;       'syschown' system call has two arguments; so,
  4403                              <1> 	;	* 1st argument, name is pointed to by BX register
  4404                              <1> 	;	* 2nd argument, owner number is in CX register
  4405                              <1> 	;
  4406                              <1> 	; / name; owner
  4407 00004BE3 E841000000          <1> 	call	isown
  4408                              <1> 		; jsr r0,isown / get the i-node and check user status
  4409                              <1> 	
  4410                              <1> 	; 25/05/2020
  4411                              <1> 	; AL = owner number (return form 'isown', ecx -> eax)
  4412                              <1> 	; 02/04/2021
  4413                              <1> 	; AX = owner number
  4414                              <1> 	; 01/05/2021
  4415                              <1> 	; byte 2 of EAX is group number
  4416                              <1> 
  4417                              <1> 	;cmp 	byte [u.uid], 0 ; 02/08/2013 
  4418                              <1> 	;	; tstb u.uid / super user
  4419                              <1> 	;jz	short syschown1
  4420                              <1> 	;	; beq 2f / yes, 2f
  4421 00004BE8 66833D[2A750000]00  <1> 	cmp	word [u.uid], 0
  4422 00004BF0 7418                <1> 	jz	short syschown1
  4423                              <1> 
  4424                              <1> 	;test	byte [i.flgs], 20h ; 32
  4425                              <1> 	;	; bit $40,i.flgs / no, set userid on execution?
  4426                              <1> 	;;jnz	error
  4427                              <1> 	;	; bne 3f / yes error, could create Trojan Horses
  4428                              <1> 	;jz	short syschown1
  4429                              <1> 	; 01/05/2021
  4430                              <1> 	; check set user id on execution flag
  4431                              <1> 	; (protection against Trojan Horses)
  4432                              <1> 
  4433                              <1> 	; ((Note: UNIX v7 x86 'chown' source code in 'sys4.c'
  4434                              <1> 	;        does not contain this protection)) 
  4435                              <1> 
  4436                              <1> 	; 09/01/2022
  4437 00004BF2 F605[75700000]08    <1> 	test	byte [i.flgs+1], 08h ; ISUID ; RUNIX v2 inode flags  	
  4438 00004BF9 740F                <1> 	jz	short syschown1
  4439                              <1> 
  4440                              <1> 	; 'permission denied !'
  4441 00004BFB C705[4C750000]0B00- <1> 	mov	dword [u.error], ERR_FILE_ACCESS  ; 11
  4441 00004C03 0000                <1>
  4442 00004C05 E9A0EFFFFF          <1> 	jmp	error
  4443                              <1> syschown1: ; 2:
  4444                              <1> 	;; AL = owner (number/ID)
  4445                              <1> 	;mov	[i.uid], al ; 23/06/2015
  4446                              <1> 	;	;  movb	r2,i.uid / no, put the new owners id 
  4447                              <1> 	;		       ; / in the i-node
  4448                              <1> 	; 02/04/2021 (Retro UNIX 386 v2)
  4449                              <1> 	; AX = owner number
  4450 00004C0A 66A3[78700000]      <1> 	mov	[i.uid], ax ; owner
  4451                              <1> 	; 01/05/2021 (Retro UNIX 386 v2)
  4452 00004C10 C1E810              <1> 	shr	eax, 16
  4453 00004C13 A2[7A700000]        <1> 	mov	[i.gid], al ; group
  4454                              <1> syschown2:
  4455                              <1> 	; 25/05/2020 - Retro UNIX 386 v2
  4456                              <1> syschmod2:
  4457                              <1> 	; 09/01/2022 (Retro UNIX 386 v1.2)
  4458 00004C18 C605[CB740000]01    <1> 	mov	byte [imodx], 1	; (flag means file data is same
  4459                              <1> 			      ;  but inode's itself has been modified)
  4460 00004C1F E8020A0000          <1> 	call	setimod ; 25/05/2020
  4461                              <1> 	
  4462 00004C24 E9A1EFFFFF          <1> 	jmp	sysret
  4463                              <1> 
  4464                              <1> 	; 1: 
  4465                              <1> 		; jmp sysret4
  4466                              <1> 	; 3:
  4467                              <1> 		; jmp	error
  4468                              <1> 
  4469                              <1> isown:
  4470                              <1> 	; 09/01/2022 (Retro UNIX 386 v1.2)
  4471                              <1> 	; 06/11/2021
  4472                              <1> 	; 12/06/2021
  4473                              <1> 	; 01/05/2021
  4474                              <1> 	; 25/05/2020 (Retro UNIX 386 v2 - Beginning)
  4475                              <1> 	; 22/06/2015 (Retro UNIX 386 v1 - Beginning)
  4476                              <1> 	; 04/05/2013 - 07/07/2013 (Retro UNIX 8086 v1)
  4477                              <1> 	;
  4478                              <1> 	; 'isown' is given a file name (the 1st argument).
  4479                              <1> 	;  It find the i-number of that file via 'namei' 
  4480                              <1> 	;  then gets the i-node into core via 'iget'.
  4481                              <1> 	;  It then tests to see if the user is super user. 
  4482                              <1> 	;  If not, it checks to see if the user is owner of 
  4483                              <1> 	;  the file. If he is not an error occurs.
  4484                              <1> 	;  If user is the owner 'setimod' is called to indicate
  4485                              <1> 	;  the inode has been modified and the 2nd argument of
  4486                              <1> 	;  the call is put in r2.
  4487                              <1> 	;
  4488                              <1> 	; INPUTS ->
  4489                              <1> 	;    arguments of syschmod and syschown calls
  4490                              <1> 	; OUTPUTS ->
  4491                              <1> 	;    u.uid - id of user
  4492                              <1> 	;    imod - set to a 1
  4493                              <1> 	;    r2 - contains second argument of the system call
  4494                              <1> 	;
  4495                              <1> 	;   ((AX=R2) output as 2nd argument)
  4496                              <1> 	;
  4497                              <1>         ; ((Modified registers: eAX, eDX, eBX, eCX, eSI, eDI, eBP))
  4498                              <1> 	;
  4499                              <1> 		; jsr r0,arg2 / u.namep points to file name
  4500                              <1> 	;; ! 2nd argument on top of stack !
  4501                              <1> 	;; 22/06/2015 - 32 bit modifications
  4502                              <1> 	;; 07/07/2013
  4503 00004C29 891D[F4740000]      <1> 	mov	[u.namep], ebx ;; 1st argument
  4504 00004C2F 51                  <1> 	push 	ecx ;* ; 2nd argument
  4505                              <1> 	;;
  4506 00004C30 E8B7FDFFFF          <1> 	call	namei
  4507                              <1> 		; jsr r0,namei / get its i-number
  4508                              <1>        ; Retro UNIX 8086 v1 modification !
  4509                              <1>        ; ax = 0 -> file not found 
  4510                              <1> 	;and	ax, ax
  4511                              <1> 	;jz	error
  4512                              <1> 	;jc	error ; 27/05/2013
  4513                              <1> 		; br error3
  4514 00004C35 730F                <1> 	jnc	short isown1  ; 25/05/2020 (isown0 -> isown1)
  4515                              <1> 	; 'file not found !' error
  4516 00004C37 C705[4C750000]0C00- <1> 	mov	dword [u.error], ERR_FILE_NOT_FOUND ; 12
  4516 00004C3F 0000                <1>
  4517 00004C41 E964EFFFFF          <1> 	jmp	error
  4518                              <1> isown1:
  4519 00004C46 E83C080000          <1> 	call	iget
  4520                              <1> 		; jsr r0,iget / get i-node into core
  4521                              <1> 	; 09/01/2022 - Retro UNIX 386 v1.2
  4522                              <1> 	;; 06/11/2021
  4523                              <1> 	;jnc	short isown3
  4524                              <1> 	;mov	[u.error], eax
  4525                              <1> 	;jmp	error
  4526                              <1> ;isown3:
  4527                              <1> 	; check if it is super user ID
  4528                              <1> 	; 09/01/2022 (Retro UNIX 386 v2 fs inode structure)
  4529 00004C4B 66A1[2A750000]      <1> 	mov	ax, [u.uid]
  4530 00004C51 6609C0              <1> 	or	ax, ax
  4531                              <1> 	;mov	al, [u.uid] ; 02/08/2013
  4532                              <1> 	;or	al, al
  4533                              <1> 		; tstb u.uid / super user?
  4534                              <1> 	; 12/06/2021
  4535 00004C54 7425                <1> 	jz	short isown2 ; 25/05/2020 (isown1 -> isown2)
  4536                              <1> 		; beq 1f / yes, branch
  4537                              <1> 	
  4538                              <1> 	; check user ID	(if same with file's owner)
  4539 00004C56 663B05[78700000]    <1> 	cmp	ax, [i.uid] ; 09/01/2022
  4540                              <1> 	;cmp	al, [i.uid]
  4541                              <1> 		; cmpb i.uid,u.uid / no, is this the owner of
  4542                              <1> 				 ; / the file
  4543                              <1> 	;jne	error
  4544                              <1> 		; beq 1f / yes
  4545                              <1> 		; jmp error3 / no, error
  4546                              <1> 	;je	short isown2 ; 25/05/2020
  4547                              <1> 	; 01/05/2021
  4548 00004C5D 750D                <1> 	jne	short isown_err
  4549                              <1> 
  4550                              <1> 	; 01/05/2021
  4551                              <1> 	;mov	ax, [u.uid]
  4552                              <1> 	;or	ax, ax
  4553                              <1> 	;jz	short isown2 
  4554                              <1> 	;cmp	ax, [i.uid]
  4555                              <1> 	;;je	short isown2
  4556                              <1> 	;jne	short isown_err
  4557                              <1> 
  4558                              <1> 	; 01/05/2021
  4559                              <1> 	; ((Note: UNIX v7 x86 'chown', 'chmod' procedures
  4560                              <1> 	;    and their sub procedures do not check group ID))
  4561                              <1> 	; (('sys4.c', 'fio.c'))
  4562                              <1> 
  4563                              <1> 	; 01/05/2021
  4564                              <1> 	; check group ID (if same with group of file's owner)
  4565 00004C5F A0[2E750000]        <1> 	mov	al, [u.gid]
  4566 00004C64 3A05[7A700000]      <1> 	cmp	al, [i.gid]
  4567 00004C6A 740F                <1> 	je	short isown2
  4568                              <1> isown_err:
  4569 00004C6C C705[4C750000]0B00- <1> 	mov	dword [u.error], ERR_NOT_OWNER ; 11
  4569 00004C74 0000                <1>
  4570                              <1> 			; 'permission denied !' error
  4571 00004C76 E92FEFFFFF          <1> 	jmp	error
  4572                              <1> 
  4573                              <1> 	; 25/05/2020
  4574                              <1> ;isown2: ; 1:
  4575                              <1> ;	call	setimod
  4576                              <1> ;		; jsr r0,setimod / indicates 
  4577                              <1> ;		;	       ; / i-node has been modified
  4578                              <1> isown2: 
  4579                              <1> 	; 25/05/2020
  4580 00004C7B 58                  <1> 	pop	eax ;* ; 2nd argument
  4581                              <1> 		; mov (sp)+,r2 / mode is put in r2 
  4582                              <1> 		       ; / (u.off put on stack with 2nd arg)
  4583 00004C7C C3                  <1> 	retn
  4584                              <1> 
  4585                              <1> ;;arg:  ; < get system call arguments >
  4586                              <1> 	; 'arg' extracts an argument for a routine whose call is 
  4587                              <1> 	; of form:
  4588                              <1> 	;	sys 'routine' ; arg1
  4589                              <1> 	;		or
  4590                              <1> 	;	sys 'routine' ; arg1 ; arg2
  4591                              <1> 	;		or
  4592                              <1> 	;	sys 'routine' ; arg1;...;arg10 (sys exec) 
  4593                              <1> 	;	
  4594                              <1> 	; INPUTS ->
  4595                              <1> 	;    u.sp+18 - contains a pointer to one of arg1..argn
  4596                              <1> 	;	This pointers's value is actually the value of
  4597                              <1> 	;	update pc at the the trap to sysent (unkni) is
  4598                              <1> 	;	made to process the sys instruction
  4599                              <1> 	;    r0 - contains the return address for the routine
  4600                              <1> 	;	that called arg. The data in the word pointer 
  4601                              <1> 	;	to by the return address is used as address
  4602                              <1> 	;	in which the extracted argument is stored
  4603                              <1> 	;    	
  4604                              <1> 	; OUTPUTS ->
  4605                              <1> 	;    'address' - contains the extracted argument 
  4606                              <1> 	;    u.sp+18 - is incremented by 2 
  4607                              <1> 	;    r1 - contains the extracted argument
  4608                              <1> 	;    r0 - points to the next instruction to be
  4609                              <1> 	;	 executed in the calling routine.
  4610                              <1> 	;
  4611                              <1>   
  4612                              <1> 	; mov u.sp,r1
  4613                              <1> 	; mov *18.(r1),*(r0)+ / put argument of system call
  4614                              <1> 			; / into argument of arg2
  4615                              <1> 	; add $2,18.(r1) / point pc on stack 
  4616                              <1> 			      ; / to next system argument
  4617                              <1> 	; rts r0
  4618                              <1> 
  4619                              <1> ;;arg2: ; < get system calls arguments - with file name pointer>
  4620                              <1> 	; 'arg2' takes first argument in system call
  4621                              <1> 	;  (pointer to name of the file) and puts it in location
  4622                              <1> 	;  u.namep; takes second argument and puts it in u.off
  4623                              <1> 	;  and on top of the stack
  4624                              <1> 	;	
  4625                              <1> 	; INPUTS ->
  4626                              <1> 	;    u.sp, r0
  4627                              <1> 	;    	
  4628                              <1> 	; OUTPUTS ->
  4629                              <1> 	;    u.namep
  4630                              <1> 	;    u.off 
  4631                              <1> 	;    u.off pushed on stack
  4632                              <1> 	;    r1
  4633                              <1> 	;
  4634                              <1> 
  4635                              <1> 	; jsr	r0,arg; u.namep / u.namep contains value of
  4636                              <1> 				; / first arg in sys call
  4637                              <1> 	; jsr r0,arg; u.off / u.off contains value of 
  4638                              <1> 				; / second arg in sys call
  4639                              <1> 	; mov r0,r1 / r0 points to calling routine
  4640                              <1> 	; mov (sp),r0 / put operation code back in r0
  4641                              <1> 	; mov u.off,(sp) / put pointer to second argument 
  4642                              <1> 			; / on stack
  4643                              <1> 	; jmp (r1) / return to calling routine
  4644                              <1> 
  4645                              <1> systime: ; / get time of year
  4646                              <1> 	; 23/06/2015 (Retro UNIX 386 v1 - Beginning)
  4647                              <1> 	; 20/06/2013 (Retro UNIX 8086 v1)
  4648                              <1> 	;
  4649                              <1> 	; 20/06/2013
  4650                              <1> 	; 'systime' gets the time of the year.
  4651                              <1> 	; The present time is put on the stack.
  4652                              <1> 	;
  4653                              <1> 	; Calling sequence:
  4654                              <1> 	;	systime
  4655                              <1> 	; Arguments: -
  4656                              <1> 	;	
  4657                              <1> 	; Inputs: -
  4658                              <1> 	; Outputs: sp+2, sp+4 - present time
  4659                              <1> 	; ...............................................................
  4660                              <1> 	;	
  4661                              <1> 	; Retro UNIX 8086 v1 modification: 
  4662                              <1> 	;       'systime' system call will return to the user
  4663                              <1> 	;	with unix time (epoch) in DX:AX register pair
  4664                              <1> 	;
  4665                              <1> 	; 	!! Major modification on original Unix v1 'systime' 
  4666                              <1> 	;	system call for PC compatibility !!		 	
  4667                              <1> 
  4668 00004C7D E845EAFFFF          <1> 	call 	epoch
  4669 00004C82 A3[D8740000]        <1> 	mov 	[u.r0], eax
  4670                              <1> 		; mov s.time,4(sp)
  4671                              <1> 		; mov s.time+2,2(sp) / put the present time 
  4672                              <1> 				   ; / on the stack
  4673                              <1> 		; br sysret4
  4674 00004C87 E93EEFFFFF          <1> 	jmp	sysret 
  4675                              <1> 
  4676                              <1> sysstime: ; / set time
  4677                              <1> 	; 09/01/2022 (Retro UNIX 386 v1.2)
  4678                              <1> 	; 23/06/2015 (Retro UNIX 386 v1 - Beginning)
  4679                              <1> 	; 20/06/2013 - 02/08/2013 (Retro UNIX 8086 v1)
  4680                              <1> 	;
  4681                              <1> 	; 'sysstime' sets the time. Only super user can use this call.
  4682                              <1> 	;
  4683                              <1> 	; Calling sequence:
  4684                              <1> 	;	sysstime
  4685                              <1> 	; Arguments: -
  4686                              <1> 	;	
  4687                              <1> 	; Inputs: sp+2, sp+4 - time system is to be set to.
  4688                              <1> 	; Outputs: -
  4689                              <1> 	; ...............................................................
  4690                              <1> 	;	
  4691                              <1> 	; Retro UNIX 8086 v1 modification: 
  4692                              <1> 	;	the user calls 'sysstime' with unix (epoch) time
  4693                              <1> 	;	(to be set) is in CX:BX register pair as two arguments.
  4694                              <1> 	; 
  4695                              <1> 	;	Retro UNIX 8086 v1 argument transfer method 2 is used
  4696                              <1> 	;	to get sysstime system call arguments from the user;
  4697                              <1> 	;	* 1st argument, lowword of unix time is in BX register
  4698                              <1> 	;	* 2nd argument, highword of unix time is in CX register		 	
  4699                              <1> 	;
  4700                              <1> 	; 	!! Major modification on original Unix v1 'sysstime' 
  4701                              <1> 	;	system call for PC compatibility !!	
  4702                              <1> 
  4703                              <1> 	; 09/01/2022 (Retro UNIX 386 v2 fs inode structure)
  4704 00004C8C 66833D[2A750000]00  <1> 	cmp	word [u.uid], 0 ; 16 bit user ID
  4705                              <1> 	;cmp	byte [u.uid], 0
  4706                              <1> 		; tstb u.uid / is user the super user
  4707                              <1> 	;ja	error
  4708                              <1> 		; bne error4 / no, error
  4709 00004C94 760F                <1> 	jna	short systime1
  4710                              <1> 	; 'permission denied !'
  4711 00004C96 C705[4C750000]0B00- <1> 	mov	dword [u.error], ERR_NOT_SUPERUSER  ; 11 
  4711 00004C9E 0000                <1>
  4712 00004CA0 E905EFFFFF          <1> 	jmp	error
  4713                              <1> systime1:
  4714                              <1> 	; 23/06/2015 (Retro UNIX 386 v1 - 32 bit version)
  4715                              <1> 	; EBX = unix (epoch) time (from user)
  4716 00004CA5 89D8                <1> 	mov	eax, ebx
  4717 00004CA7 E89BEBFFFF          <1> 	call 	set_date_time
  4718                              <1> 		; mov 4(sp),s.time
  4719                              <1> 		; mov 2(sp),s.time+2 / set the system time
  4720 00004CAC E919EFFFFF          <1> 	jmp	sysret
  4721                              <1> 		; br sysret4
  4722                              <1> 
  4723                              <1> sysbreak:
  4724                              <1> 	; 18/10/2015
  4725                              <1> 	; 07/10/2015
  4726                              <1> 	; 23/06/2015 (Retro UNIX 386 v1 - Beginning)
  4727                              <1> 	; 20/06/2013 - 24/03/2014 (Retro UNIX 8086 v1)
  4728                              <1> 	;
  4729                              <1> 	; 'sysbreak' sets the programs break points. 
  4730                              <1> 	; It checks the current break point (u.break) to see if it is
  4731                              <1> 	; between "core" and the stack (sp). If it is, it is made an
  4732                              <1> 	; even address (if it was odd) and the area between u.break
  4733                              <1> 	; and the stack is cleared. The new breakpoint is then put
  4734                              <1> 	; in u.break and control is passed to 'sysret'.
  4735                              <1> 	;
  4736                              <1> 	; Calling sequence:
  4737                              <1> 	;	sysbreak; addr
  4738                              <1> 	; Arguments: -
  4739                              <1> 	;	
  4740                              <1> 	; Inputs: u.break - current breakpoint
  4741                              <1> 	; Outputs: u.break - new breakpoint 
  4742                              <1> 	;	area between old u.break and the stack (sp) is cleared.
  4743                              <1> 	; ...............................................................
  4744                              <1> 	;	
  4745                              <1> 	; Retro UNIX 8086 v1 modification:
  4746                              <1> 	;	The user/application program puts breakpoint address
  4747                              <1> 	;       in BX register as 'sysbreak' system call argument.
  4748                              <1> 	; 	(argument transfer method 1)
  4749                              <1> 	;
  4750                              <1> 	;  NOTE: Beginning of core is 0 in Retro UNIX 8086 v1 !
  4751                              <1> 	; 	((!'sysbreak' is not needed in Retro UNIX 8086 v1!))
  4752                              <1> 	;  NOTE:
  4753                              <1> 	; 	'sysbreak' clears extended part (beyond of previous
  4754                              <1> 	;	'u.break' address) of user's memory for original unix's
  4755                              <1> 	;	'bss' compatibility with Retro UNIX 8086 v1 (19/11/2013)
  4756                              <1> 
  4757                              <1> 		; mov u.break,r1 / move users break point to r1
  4758                              <1> 		; cmp r1,$core / is it the same or lower than core?
  4759                              <1> 		; blos 1f / yes, 1f
  4760                              <1> 	; 23/06/2015
  4761 00004CB1 8B2D[08750000]      <1> 	mov	ebp, [u.break] ; virtual address (offset)
  4762                              <1> 	;and	ebp, ebp
  4763                              <1> 	;jz	short sysbreak_3 
  4764                              <1> 	; Retro UNIX 386 v1 NOTE: u.break points to virtual address !!!
  4765                              <1> 	; (Even break point address is not needed for Retro UNIX 386 v1)
  4766 00004CB7 8B15[D0740000]      <1> 	mov	edx, [u.sp] ; kernel stack at the beginning of sys call
  4767 00004CBD 83C20C              <1> 	add	edx, 12 ; EIP -4-> CS -4-> EFLAGS -4-> ESP (user) 
  4768                              <1> 	; 07/10/2015
  4769 00004CC0 891D[08750000]      <1> 	mov	[u.break], ebx ; virtual address !!!
  4770                              <1> 	;
  4771 00004CC6 3B1A                <1> 	cmp	ebx, [edx] ; compare new break point with 
  4772                              <1> 			   ; with top of user's stack (virtual!)
  4773 00004CC8 7327                <1> 	jnb	short sysbreak_3
  4774                              <1> 		; cmp r1,sp / is it the same or higher 
  4775                              <1> 			  ; / than the stack?
  4776                              <1> 		; bhis 1f / yes, 1f
  4777 00004CCA 89DE                <1> 	mov	esi, ebx
  4778 00004CCC 29EE                <1> 	sub	esi, ebp ; new break point - old break point
  4779 00004CCE 7621                <1> 	jna	short sysbreak_3 
  4780                              <1> 	;push	ebx
  4781                              <1> sysbreak_1:
  4782 00004CD0 89EB                <1> 	mov	ebx, ebp  
  4783 00004CD2 E87BE5FFFF          <1> 	call	get_physical_addr ; get physical address
  4784 00004CD7 0F824BFEFFFF        <1> 	jc	tr_addr_nm_err
  4785                              <1> 	; 18/10/2015
  4786 00004CDD 89C7                <1> 	mov	edi, eax 
  4787 00004CDF 29C0                <1> 	sub	eax, eax ; 0
  4788                              <1> 		 ; ECX = remain byte count in page (1-4096)
  4789 00004CE1 39CE                <1> 	cmp	esi, ecx
  4790 00004CE3 7302                <1> 	jnb	short sysbreak_2
  4791 00004CE5 89F1                <1> 	mov	ecx, esi
  4792                              <1> sysbreak_2:
  4793 00004CE7 29CE                <1> 	sub	esi, ecx
  4794 00004CE9 01CD                <1> 	add	ebp, ecx
  4795 00004CEB F3AA                <1> 	rep 	stosb
  4796 00004CED 09F6                <1> 	or	esi, esi
  4797 00004CEF 75DF                <1> 	jnz	short sysbreak_1
  4798                              <1> 	;
  4799                              <1> 		; bit $1,r1 / is it an odd address
  4800                              <1> 		; beq 2f / no, its even
  4801                              <1> 		; clrb (r1)+ / yes, make it even
  4802                              <1> 	; 2: / clear area between the break point and the stack
  4803                              <1> 		; cmp r1,sp / is it higher or same than the stack
  4804                              <1> 		; bhis 1f / yes, quit
  4805                              <1> 		; clr (r1)+ / clear word
  4806                              <1> 		; br 2b / go back
  4807                              <1> 	;pop	ebx
  4808                              <1> sysbreak_3: ; 1:
  4809                              <1> 	;mov	[u.break], ebx ; virtual address !!!
  4810                              <1> 		; jsr r0,arg; u.break / put the "address" 
  4811                              <1> 			; / in u.break (set new break point)
  4812                              <1> 		; br sysret4 / br sysret
  4813 00004CF1 E9D4EEFFFF          <1> 	jmp	sysret
  4814                              <1> 
  4815                              <1> maknod: 
  4816                              <1> 	; 10/01/2022
  4817                              <1> 	; 09/01/2022 (Retro UNIX 386 v1.2)
  4818                              <1> 	; 06/11/2021 (Retro UNIX 386 v2)
  4819                              <1> 	; 15/08/2021
  4820                              <1> 	; 06/05/2021
  4821                              <1> 	; 02/05/2021
  4822                              <1> 	; 01/04/2021
  4823                              <1> 	; 27/03/2021
  4824                              <1> 	; 25/03/2021 (Retro UNIX 386 v2 - Beginning)
  4825                              <1> 	; 22/06/2015 (Retro UNIX 386 v1 - Beginning)
  4826                              <1> 	; 02/05/2013 - 02/08/2013 (Retro UNIX 8086 v1)
  4827                              <1> 	;
  4828                              <1> 	; 'maknod' creates an i-node and makes a directory entry
  4829                              <1> 	; for this i-node in the current directory.
  4830                              <1> 	;
  4831                              <1> 	; INPUTS ->
  4832                              <1> 	;    r1 - contains mode
  4833                              <1> 	;    ii - current directory's i-number	
  4834                              <1> 	;    	
  4835                              <1> 	; OUTPUTS ->
  4836                              <1> 	;    u.dirbuf - contains i-number of free i-node 
  4837                              <1> 	;    i.flgs - flags in new i-node 
  4838                              <1> 	;    i.uid - filled with u.uid
  4839                              <1> 	;    i.nlks - 1 is put in the number of links
  4840                              <1> 	;    i.ctim - creation time
  4841                              <1> 	;    i.ctim+2 - modification time
  4842                              <1> 	;    imod - set via call to setimod
  4843                              <1> 	;	
  4844                              <1> 	; ((AX = R1)) input
  4845                              <1> 	;
  4846                              <1> 	; (Retro UNIX Prototype : 
  4847                              <1> 	;	30/10/2012 - 01/03/2013, UNIXCOPY.ASM)
  4848                              <1>         ; ((Modified registers: eAX, eDX, eBX, eCX, eSI, eDI, eBP))
  4849                              <1> 
  4850                              <1> 	; 27/03/2021
  4851                              <1> 	; INPUT:
  4852                              <1>  	;	AX = mode
  4853                              <1> 	;
  4854                              <1> 	; ('maknod' is called by 'syscreat' and 'sysmkdir')
  4855                              <1> 	; (('syscreat' and 'sysmkdir' will set AX input))
  4856                              <1> 
  4857                              <1> 	; 27/03/2021
  4858                              <1> 	; Retro UNIX 386 v2 inode mode flags (ref: 'ux.s')
  4859                              <1> 	; for File Inode: (high byte)
  4860                              <1> 	;   IFREG - 1 = regular file (8000h)
  4861                              <1> 	;   IFDIR - 1 = directory (4000h)
  4862                              <1> 	;   IRSVD - 0 = reserved bit (2000h) ; Mounted flag for dirs
  4863                              <1> 	;   ILARG - large file addressing bit (1000h)
  4864                              <1> 	;   ISUID - set user id on exec (800h)
  4865                              <1> 	;   ISGID - set group id on exec (400h)
  4866                              <1> 	;   IEXTT - 1 = use extents (200h)
  4867                              <1> 	;   IREAD - read, owner (100h)
  4868                              <1> 	; for Device Inode: (high byte)
  4869                              <1> 	;   IFREG - 0 = device file (8000h)
  4870                              <1> 	;   IFBLK - 1 = block device (4000h)
  4871                              <1> 	;   IFCHR - character special (2000h) -always 1-
  4872                              <1> 	;   IFIFO - fifo special (1000h)
  4873                              <1> 	;   IPIPE - pipe special (800h) ; 07/02/2020
  4874                              <1> 	;   IREDIR - redirected (400h)  ; 07/02/2020
  4875                              <1> 	;   IEXTR - 1 = external device driver (200h)
  4876                              <1> 	;   IREAD - read, owner (100h)
  4877                              <1> 
  4878                              <1> 	;; / r1 contains the mode
  4879                              <1> 	;or 	ah, 80h	; 10000000b
  4880                              <1> 	;	; bis	$100000,r1 / allocate flag set
  4881                              <1> 
  4882                              <1> 	; high 3 bit will be checked here
  4883                              <1> 	; 	(ref: unix v7 x86 source, iget.c)
  4884                              <1> 
  4885                              <1> ;	; 27/03/2021
  4886                              <1> ;	test	ah, 0E0h ; 80h+40h+20h
  4887                              <1> ;	jnz	short maknod0	
  4888                              <1> ;
  4889                              <1> ;	; ('syscreat' clears ILARG & IEXTT bits)
  4890                              <1> ;	;and	ah, ~0Ch ; clear ILARG & IEXTT bits
  4891                              <1> ;	;	; user can set ISUID & ISGID bits
  4892                              <1> ;
  4893                              <1> ;	or	ah, 80h ; IFREG
  4894                              <1> ;maknod0:
  4895 00004CF6 50                  <1> 	push	eax ; ***** ; 27/03/2021 (32 bit push, pop)
  4896                              <1> 		; mov r1,-(sp) / put mode on stack
  4897                              <1> 	; 31/07/2013
  4898                              <1> 	;mov	ax, [ii] ; move current i-number to AX/r1
  4899                              <1> 	;	; mov ii,r1 / move current i-number to r1
  4900                              <1> 	;mov	dl, 1 ; owner flag mask
  4901                              <1> 	; 15/08/2021
  4902                              <1> 	;movzx	eax, word [ii] ; move current i-number to EAX
  4903                              <1> 	; 09/01/2022
  4904 00004CF7 A1[B4740000]        <1> 	mov	eax, [ii] ; move current i-number to EAX
  4905                              <1> 	; 25/03/2021
  4906 00004CFC 66BA8000            <1> 	mov	dx, 80h	; IWRITE - write,owner
  4907 00004D00 E86C080000          <1> 	call	access	
  4908                              <1> 		; jsr r0,access; 1 / get its i-node into core
  4909                              <1> 	; 15/08/2021
  4910                              <1> 	; NOTE: cpu will not return here if there is a permission error
  4911                              <1> 	;	(it will jump to 'error' address from/in 'access')
  4912                              <1> 	
  4913 00004D05 50                  <1> 	push	eax ; **** ; 27/03/2021 (32 bit push, pop)
  4914                              <1> 		; mov r1,-(sp) / put i-number on stack
  4915                              <1> 	;mov	ax, 40
  4916                              <1> 	;	; mov $40.,r1 / r1 = 40
  4917                              <1> 	; 27/03/2021
  4918 00004D06 31C0                <1> 	xor	eax, eax
  4919                              <1> 	;;dec	ax ; 0FFFFh
  4920                              <1> 	; 15/08/2021
  4921                              <1> 	;dec	eax ; 0FFFFFFFFh
  4922                              <1> 	; 01/04/2021
  4923                              <1> ;maknod1: ; 1: / scan for a free i-node (next 4 instructions)
  4924                              <1> 	;;inc	ax
  4925                              <1> 	;	; inc r1 / r1 = r1 + 1
  4926                              <1> 	; 15/08/2021
  4927                              <1> 	;inc	eax
  4928                              <1> 	; 27/03/2021 - Retro UNIX 386 v2
  4929                              <1> 	; eax = 0 -> start from first free inode
  4930                              <1> 	; eax > 0 -> locate this inode on the inode map buffer
  4931                              <1> 	; NOTE:
  4932                              <1> 	; Retro UNIX 386 v2 'imap' is mostly different than v1 'imap'
  4933                              <1> 	; ('imap' will not check inode table)
  4934 00004D08 E8650A0000          <1> 	call	imap
  4935                              <1> 		; jsr r0,imap / get byte address and bit position in 
  4936                              <1> 			    ; /	inode map in r2 & m
  4937                              <1> 	; 09/01/2022
  4938                              <1> 	; (cpu will not return here if there would be an eror in imap)
  4939                              <1> 	; 15/08/2021
  4940                              <1> 	;jc	short maknod_err
  4941                              <1> 
  4942                              <1> 	; DX (MQ) has a 1 in the calculated bit position
  4943                              <1>         ; eBX (R2) has byte address of the byte with allocation bit
  4944                              <1> 
  4945                              <1> 	; 06/11/2021
  4946                              <1> 	; ebp = superblock buffer address
  4947                              <1> 	; eax = inode number
  4948                              <1> 	; 10/01/2022
  4949                              <1> 	; [ebp+SB.IMapBuffer] = physical block/sector number
  4950                              <1> 	;		of current IMAP sector	
  4951                              <1> 
  4952                              <1> 	; 27/03/2021
  4953                              <1> 	; If cpu is here, there is not an error
  4954                              <1> 	; and EBX points to inode map buffer
  4955                              <1> 	; DL has 1 at bit position which is for free inode
  4956                              <1> 	; (E)AX = inode number
  4957                              <1> 	; 10/01/2022 (Retro UNIX 386 v1.2)
  4958                              <1> 	; ECX = byte offset from the (start of) inode map buffer
  4959                              <1> 
  4960                              <1> 	; 15/08/2021
  4961                              <1> maknod1:
  4962                              <1> 	; 22/06/2015 - NOTE for next Retro UNIX version: 
  4963                              <1> 	;	       Inode count must be checked here
  4964                              <1> 	; (Original UNIX v1 did not check inode count here !?)
  4965 00004D0D 8413                <1> 	test	[ebx], dl
  4966                              <1> 		; bitb mq,(r2) / is the i-node active
  4967                              <1> 	;jnz	short maknod1
  4968                              <1> 	;	; bne 1b / yes, try the next one
  4969                              <1> 	; 15/08/2021
  4970 00004D0F 7412                <1> 	jz	short maknod3 ; free inode (inactive inode)
  4971                              <1> maknod2:
  4972 00004D11 40                  <1> 	inc	eax
  4973 00004D12 E87F0A0000          <1> 	call	imap_x ; next call to imap (bypass 1st call code)
  4974                              <1> 	; 10/01/2022
  4975                              <1> 	; (cpu will/would not return here 
  4976                              <1> 	;  if there is/was an error in 'imap_x')
  4977                              <1> 	;jnc	short maknod1
  4978 00004D17 EBF4                <1> 	jmp	short maknod1
  4979                              <1> maknod_err:
  4980                              <1> 	; 15/08/2021
  4981                              <1> 	;pop	edx  ; two pops for stack alignment
  4982                              <1> 	;pop	edx  ; (may not be needed while jumping to 'error')
  4983 00004D19 A3[4C750000]        <1> 	mov	[u.error], eax
  4984 00004D1E E987EEFFFF          <1> 	jmp	error
  4985                              <1> maknod3:
  4986                              <1> 	; 10/01/2022
  4987                              <1> 	; ebp = superblock buffer address
  4988                              <1> 	; eax = (free) inode number
  4989                              <1> 	; [ebp+SB.IMapBuffer] = physical block/sector number
  4990                              <1> 	;		of current IMAP sector
  4991                              <1> 	; EBX = buffer address/offset for relevant alloc byte
  4992                              <1> 	;  DL has 1 at bit position which is for free inode
  4993                              <1> 	;
  4994                              <1> 	; ECX = byte offset from the (start of) inode map buffer
  4995                              <1> 
  4996                              <1> 	;mov	[ebp+SB.LastInode], eax ; (free) inode number
  4997                              <1> 
  4998 00004D23 50                  <1> 	push	eax ; ***
  4999 00004D24 51                  <1> 	push	ecx ; **
  5000 00004D25 52                  <1> 	push	edx ; *
  5001                              <1> 
  5002 00004D26 8B457C              <1> 	mov	eax, [ebp+SB.ImapBuffer]
  5003                              <1> 
  5004 00004D29 E883140000          <1> 	call	wslot
  5005                              <1> 	; ebx = buffer data address (write operation bit is set)
  5006                              <1> 	; eax = physical sector number
  5007                              <1> 	; Note: ebx contains addr of the 1st buffer in the buffer
  5008                              <1> 	; (the last allocated buffer becomes the 1st in buffer chain)
  5009                              <1> 
  5010                              <1> 	;mov	[ebp+SB.ImapBuffer], ebx
  5011                              <1> 			; save inode map buffer address
  5012 00004D2E 5A                  <1> 	pop	edx ; *
  5013 00004D2F 59                  <1> 	pop	ecx ; **
  5014 00004D30 01CB                <1> 	add	ebx, ecx ; + byte offset (for allocation bit pos)	
  5015                              <1> 
  5016                              <1> 	; set allocation bit (for this new inode) ; 10/01/2022 
  5017 00004D32 0813                <1> 	or	[ebx], dl
  5018                              <1> 		; bisb mq,(r2) / no, make it active 
  5019                              <1> 			     ; / (put a 1 in the bit map)
  5020                              <1> 	;push	ebp ; @
  5021 00004D34 E894140000          <1> 	call	dskwr ; writes the 1st buffer to sector
  5022                              <1> 		      ; (at the beginning/head of the buffer chain)
  5023                              <1> 	;pop	ebp ; @
  5024                              <1> 
  5025                              <1> 	; if we are here, buffer has been written to disk successfully 
  5026                              <1> 	; (otherwise cpu would jump to 'error' address)
  5027                              <1> 
  5028                              <1> 	; set superblock modified flag
  5029 00004D39 BA[CC740000]        <1> 	mov	edx, smod
  5030 00004D3E F605[B9740000]01    <1> 	test	byte [cdev], 1 ; mounted device ?
  5031 00004D45 7401                <1> 	jz	short maknod4 ; no, root device
  5032                              <1> 	; yes, mounted device
  5033                              <1> 	;mov	edx, mmod
  5034 00004D47 42                  <1> 	inc	edx ; edx = offset mmod
  5035                              <1> maknod4: 
  5036 00004D48 FE02                <1> 	inc	byte [edx] ; superblock modified !
  5037                              <1> 
  5038                              <1> 	; 09/01/2022 (Retro UNIX 386 v1.2)
  5039                              <1> 	;;
  5040                              <1> 	;; 27/03/2021
  5041                              <1> 	;; (ref: 'UNIXHDCP.ASM', 'mak_nod', 18/01/2020)
  5042                              <1> 	;or	byte [smod], 2 ; inode map modified
  5043                              <1> 	;; 01/04/2021
  5044                              <1> 	;;or	byte [imapbuf_hdr+bufhdr.status], 2
  5045                              <1> 	;or	byte [imapbuf_hdr], 2 ; set modified flag bit
  5046                              <1> 	; 02/05/2021
  5047                              <1> 	;or	byte [esi+ldrv.b_status], 2 ; inode map modified
  5048                              <1> 	; 15/08/2021
  5049                              <1> 	; 06/05/2021
  5050                              <1> 	;or	byte [esi+ldrv.status], 2 ; inode map modified
  5051                              <1> 	;;or	byte [sysbuf_hdr], 2 ; set modified flag bit
  5052                              <1> 	
  5053                              <1> 	;mov	eax, [ebp+SB.LastInode] ; (free) inode number
  5054                              <1> 	;push	eax ; ***
  5055 00004D4A 8B0424              <1> 	mov	eax, [esp] ; ***
  5056 00004D4D 40                  <1> 	inc	eax ; next inode number 
  5057                              <1> 		    ; (free inode search start value)
  5058 00004D4E 894534              <1> 	mov	[ebp+SB.FirstFreeIno], eax
  5059 00004D51 E82E150000          <1> 	call	get_system_time
  5060                              <1> 		; eax = current time (as unix epoch time)
  5061 00004D56 89455C              <1> 	mov	[ebp+SB.ModifTime], eax
  5062 00004D59 58                  <1> 	pop	eax ; *** ; inode number
  5063                              <1> 	;;
  5064 00004D5A E828070000          <1> 	call	iget
  5065                              <1> 		; jsr r0,iget / get i-node into core
  5066                              <1> 	; 09/01/2022
  5067                              <1> 	;  (If cpu is here, there was/is not an error!)
  5068                              <1> 	; 06/11/2021
  5069                              <1> 	;jc	short maknod_err
  5070                              <1> 	
  5071                              <1> 	;test	word [i.flgs], 8000h 
  5072                              <1> 	;	; tst i.flgs / is i-node already allocated
  5073                              <1> 	;jnz	short maknod1	
  5074                              <1> 	;	; blt 1b / yes, look for another one
  5075                              <1> 	; 27/03/2021
  5076                              <1> 	; (Retro UNIX 386 v2 inode flags) 
  5077 00004D5F F605[75700000]E0    <1> 	test	byte [i.flgs+1], 0E0h ; 80h+40h+20h
  5078                              <1> 	;jnz	short maknod1 ; i-node already allocated
  5079                              <1> 	;		      ; (as it is in inode table)
  5080                              <1> 	; 15/08/2021
  5081 00004D66 75A9                <1> 	jnz	short maknod2 ; defective inode map !?
  5082                              <1> 
  5083                              <1> 	; AX = free inode number
  5084 00004D68 66A3[10750000]      <1> 	mov	[u.dirbuf], ax
  5085                              <1> 		; mov r1,u.dirbuf / no, put i-number in u.dirbuf
  5086 00004D6E 58                  <1> 	pop	eax ; **** ; 27/03/2021 (32 bit push, pop)
  5087                              <1> 		; mov (sp)+,r1 / get current i-number back
  5088 00004D6F E813070000          <1> 	call	iget
  5089                              <1> 		; jsr r0,iget / get i-node in core
  5090                              <1> 	; 09/01/2022
  5091                              <1> 	; 06/11/2021
  5092                              <1> 	;jc	short maknod_err
  5093                              <1> 
  5094 00004D74 E845F7FFFF          <1> 	call	mkdir
  5095                              <1> 		; jsr r0,mkdir / make a directory entry 
  5096                              <1> 			     ; / in current directory
  5097                              <1> 	; 09/01/2022
  5098                              <1> 	;  (If cpu is here, there was/is not an error in 'mkdir'!)
  5099                              <1> 	; 06/11/2021
  5100                              <1> 	;jc	short maknod_err
  5101                              <1> 	
  5102                              <1> 	; (eax = 0)
  5103                              <1> 	;movzx	eax, [u.difbuf] ; 06/11/2021
  5104 00004D79 66A1[10750000]      <1> 	mov	ax, [u.dirbuf]
  5105                              <1> 		; mov u.dirbuf,r1 / r1 = new inode number
  5106 00004D7F E803070000          <1> 	call	iget
  5107                              <1> 		; jsr r0,iget / get it into core
  5108                              <1> 		; jsr r0,copyz; inode; inode+32. / 0 it out
  5109                              <1> 	; 09/01/2022
  5110                              <1> 	; 06/11/2021
  5111                              <1> 	;jc	short maknod_err
  5112                              <1> 
  5113                              <1> 	;mov	ecx, 8
  5114                              <1> 	; 27/03/2021
  5115 00004D84 31C9                <1> 	xor	ecx, ecx 
  5116 00004D86 B110                <1> 	mov	cl, 16 ; 64 bit inodes
  5117 00004D88 31C0                <1> 	xor	eax, eax ; 0
  5118 00004D8A BF[74700000]        <1> 	mov	edi, inode 
  5119 00004D8F F3AB                <1> 	rep	stosd
  5120                              <1> 	;
  5121                              <1> 	;pop	word [i.flgs]
  5122                              <1> 	;	; mov (sp)+,i.flgs / fill flags
  5123                              <1> 	; 27/03/2021 (32 bit push, pop)
  5124 00004D91 58                  <1> 	pop	eax ; *****
  5125 00004D92 66A3[74700000]      <1> 	mov	[i.flgs], ax
  5126                              <1> 
  5127                              <1> 	;mov 	cl, [u.uid] ; 02/08/2013
  5128                              <1> 	;mov 	[i.uid], cl
  5129                              <1> 	;	; movb u.uid,i.uid / user id
  5130                              <1> 	; 15/08/2021
  5131                              <1> 	; 27/03/2021
  5132 00004D98 8A0D[2E750000]      <1> 	mov	cl, [u.gid] ; 8 bit group ID ; 15/08/2021
  5133 00004D9E 880D[7A700000]      <1> 	mov	[i.gid], cl 	
  5134 00004DA4 668B0D[2A750000]    <1> 	mov	cx, [u.uid] ; 16 bit user ID
  5135 00004DAB 66890D[78700000]    <1> 	mov	[i.uid], cx	
  5136                              <1> 
  5137 00004DB2 C605[76700000]01    <1> 	mov     byte [i.nlks], 1
  5138                              <1> 		; movb $1,i.nlks / 1 link
  5139                              <1> 	;call	epoch ; Retro UNIX 8086 v1 modification !
  5140                              <1> 	;mov	eax, [s.time]
  5141                              <1> 	;mov 	[i.ctim], eax
  5142                              <1> 	 	; mov s.time,i.ctim / time created
  5143                              <1> 	 	; mov s.time+2,i.ctim+2 / time modified
  5144                              <1> 	; Retro UNIX 8086 v1 modification !
  5145                              <1> 	; i.ctime=0, i.ctime+2=0 and
  5146                              <1>         ; 'setimod' will set ctime of file via 'epoch'
  5147                              <1> 	;call	setimod
  5148                              <1> 	;	; jsr r0,setimod / set modified flag
  5149                              <1> 	;retn
  5150                              <1> 	;	; rts r0 / return
  5151                              <1> 	; 27/03/2021
  5152 00004DB9 E968080000          <1> 	jmp	setimod
  5153                              <1> 
  5154                              <1> sysseek: ; / moves read write pointer in an fsp entry
  5155                              <1> 	; 22/06/2015 (Retro UNIX 386 v1 - Beginning)
  5156                              <1> 	; 07/07/2013 - 05/08/2013 (Retro UNIX 8086 v1)
  5157                              <1> 	;
  5158                              <1> 	; 'sysseek' changes the r/w pointer of (3rd word of in an
  5159                              <1> 	; fsp entry) of an open file whose file descriptor is in u.r0.
  5160                              <1> 	; The file descriptor refers to a file open for reading or
  5161                              <1> 	; writing. The read (or write) pointer is set as follows:
  5162                              <1> 	;	* if 'ptrname' is 0, the pointer is set to offset.
  5163                              <1> 	;	* if 'ptrname' is 1, the pointer is set to its
  5164                              <1> 	;	  current location plus offset.
  5165                              <1> 	;	* if 'ptrname' is 2, the pointer is set to the
  5166                              <1> 	;	  size of file plus offset.
  5167                              <1> 	; The error bit (e-bit) is set for an undefined descriptor.
  5168                              <1> 	;
  5169                              <1> 	; Calling sequence:
  5170                              <1> 	;	sysseek; offset; ptrname
  5171                              <1> 	; Arguments:
  5172                              <1> 	;	offset - number of bytes desired to move 
  5173                              <1> 	;		 the r/w pointer
  5174                              <1> 	;	ptrname - a switch indicated above
  5175                              <1> 	;
  5176                              <1> 	; Inputs: r0 - file descriptor 
  5177                              <1> 	; Outputs: -
  5178                              <1> 	; ...............................................................
  5179                              <1> 	;	
  5180                              <1> 	; Retro UNIX 8086 v1 modification: 
  5181                              <1> 	;       'sysseek' system call has three arguments; so,
  5182                              <1> 	;	* 1st argument, file descriptor is in BX (BL) register
  5183                              <1> 	;	* 2nd argument, offset is in CX register
  5184                              <1> 	;	* 3rd argument, ptrname/switch is in DX (DL) register	
  5185                              <1> 	;	
  5186                              <1> 
  5187 00004DBE E822000000          <1> 	call	seektell
  5188                              <1> 	; AX = u.count
  5189                              <1> 	; BX = *u.fofp
  5190                              <1> 		; jsr r0,seektell / get proper value in u.count
  5191                              <1> 		; add u.base,u.count / add u.base to it
  5192 00004DC3 0305[FC740000]      <1> 	add	eax, [u.base] ; add offset (u.base) to base
  5193 00004DC9 8903                <1> 	mov	[ebx], eax
  5194                              <1> 		; mov u.count,*u.fofp / put result into r/w pointer
  5195 00004DCB E9FAEDFFFF          <1> 	jmp	sysret
  5196                              <1> 		; br sysret4
  5197                              <1> 
  5198                              <1> systell: ; / get the r/w pointer
  5199                              <1> 	; 09/01/2022 (Retro UNIX 386 v1.2)
  5200                              <1> 	; 22/06/2015 (Retro UNIX 386 v1 - Beginning)
  5201                              <1> 	; 07/07/2013 - 05/08/2013 (Retro UNIX 8086 v1)
  5202                              <1> 	;
  5203                              <1> 	; Retro UNIX 8086 v1 modification:
  5204                              <1> 	; ! 'systell' does not work in original UNIX v1,
  5205                              <1> 	; 	    it returns with error !
  5206                              <1> 	; Inputs: r0 - file descriptor 
  5207                              <1> 	; Outputs: r0 - file r/w pointer
  5208                              <1> 
  5209                              <1> 	;xor	ecx, ecx ; 0
  5210                              <1> 	;mov	edx, 1 ; 05/08/2013
  5211                              <1> 	; 09/01/2022
  5212 00004DD0 29D2                <1> 	sub	edx, edx
  5213 00004DD2 FEC2                <1> 	inc	dl
  5214                              <1> 	; edx = 1
  5215                              <1> 	;call 	seektell
  5216 00004DD4 E812000000          <1> 	call 	seektell0 ; 05/08/2013
  5217                              <1> 	;mov	ebx, [u.fofp]
  5218 00004DD9 8B03                <1> 	mov	eax, [ebx]
  5219 00004DDB A3[D8740000]        <1> 	mov	[u.r0], eax
  5220 00004DE0 E9E5EDFFFF          <1> 	jmp	sysret
  5221                              <1> 
  5222                              <1> ; Original unix v1 'systell' system call:
  5223                              <1> 		; jsr r0,seektell
  5224                              <1> 		; br error4
  5225                              <1> 
  5226                              <1> seektell:
  5227                              <1> 	; 09/01/2022 (Retro UNIX 386 v1.2)
  5228                              <1> 	; 06/11/2021
  5229                              <1> 	; 12/06/2021
  5230                              <1> 	; 06/05/2021 (Retro UNIX 386 v2)
  5231                              <1> 	; 03/01/2016
  5232                              <1> 	; 22/06/2015 (Retro UNIX 386 v1 - Beginning)
  5233                              <1> 	; 07/07/2013 - 05/08/2013 (Retro UNIX 8086 v1)
  5234                              <1> 	;
  5235                              <1> 	; 'seektell' puts the arguments from sysseek and systell
  5236                              <1> 	; call in u.base and u.count. It then gets the i-number of
  5237                              <1> 	; the file from the file descriptor in u.r0 and by calling
  5238                              <1> 	; getf. The i-node is brought into core and then u.count
  5239                              <1> 	; is checked to see it is a 0, 1, or 2.
  5240                              <1> 	; If it is 0 - u.count stays the same
  5241                              <1> 	;          1 - u.count = offset (u.fofp)
  5242                              <1> 	;	   2 - u.count = i.size (size of file)
  5243                              <1> 	; 	 		
  5244                              <1> 	; !! Retro UNIX 8086 v1 modification:
  5245                              <1> 	;	Argument 1, file descriptor is in BX;
  5246                              <1> 	;	Argument 2, offset is in CX;
  5247                              <1> 	;	Argument 3, ptrname/switch is in DX register.	
  5248                              <1> 	;
  5249                              <1> 	; mov 	ax, 3 ; Argument transfer method 3 (three arguments)	
  5250                              <1> 	; call 	arg
  5251                              <1> 	;
  5252                              <1> 	; ((Return -> ax = base for offset (position= base+offset))
  5253                              <1> 	;
  5254 00004DE5 890D[FC740000]      <1> 	mov 	[u.base], ecx ; offset
  5255                              <1> 		; jsr r0,arg; u.base / puts offset in u.base
  5256                              <1> 	; 09/01/2022
  5257                              <1> 	; ebx = file descriptor (0 to 9)
  5258                              <1> seektell0:
  5259 00004DEB 8915[00750000]      <1> 	mov 	[u.count], edx
  5260                              <1> 		; jsr r0,arg; u.count / put ptr name in u.count
  5261                              <1> 	;mov	ax, bx
  5262                              <1> 		; mov *u.r0,r1 / file descriptor in r1 
  5263                              <1> 			     ; / (index in u.fp list)
  5264                              <1> 	; 12/06/2021
  5265                              <1> 	; BX = file descriptor (file number)
  5266 00004DF1 E8C0FBFFFF          <1> 	call	getf
  5267                              <1> 		; jsr r0,getf / u.fofp points to 3rd word in fsp entry
  5268                              <1> 	; 09/01/2022
  5269 00004DF6 09C0                <1> 	or	eax, eax
  5270                              <1> 	;or	ax, ax ; i-number of the file
  5271                              <1> 		; mov r1,-(sp) / r1 has i-number of file, 
  5272                              <1> 		             ; / put it on the stack
  5273                              <1> 	;jz	error
  5274                              <1> 		; beq error4 / if i-number is 0, not active so error
  5275 00004DF8 750F                <1> 	jnz	short seektell1
  5276 00004DFA C705[4C750000]0A00- <1> 	mov	dword [u.error], ERR_FILE_NOT_OPEN  ; 'file not open !'
  5276 00004E02 0000                <1>
  5277 00004E04 E9A1EDFFFF          <1> 	jmp	error
  5278                              <1> seektell1:
  5279                              <1> 	; 06/05/2021
  5280                              <1> 	;push	eax
  5281                              <1> 	;cmp	ah, 80h
  5282                              <1> 	;jb	short seektell2
  5283                              <1> 	;	; bgt .+4 / if its positive jump
  5284                              <1> 	;neg	ax
  5285                              <1> 	;	; neg r1 / if not make it positive
  5286                              <1> seektell2:
  5287 00004E09 E879060000          <1> 	call	iget
  5288                              <1> 		; jsr r0,iget / get its i-node into core
  5289                              <1> 	; 09/01/2022
  5290                              <1> 	; (if there is/was an error in 'iget', cpu will not come here)
  5291                              <1> 	;; 06/11/2021
  5292                              <1> 	;jnc	short seektell6
  5293                              <1> 	;mov	[u.error], eax
  5294                              <1> 	;jmp	error
  5295                              <1> seektell6:
  5296 00004E0E 8B1D[EC740000]      <1>         mov     ebx, [u.fofp] ; 05/08/2013
  5297 00004E14 803D[00750000]01    <1> 	cmp	byte [u.count], 1
  5298                              <1> 		; cmp u.count,$1 / is ptr name =1
  5299 00004E1B 7705                <1> 	ja	short seektell3
  5300                              <1> 		; blt 2f / no its zero
  5301 00004E1D 7409                <1> 	je	short seektell4
  5302                              <1> 		; beq 1f / yes its 1
  5303 00004E1F 31C0                <1> 	xor	eax, eax
  5304                              <1> 	;jmp	short seektell5
  5305 00004E21 C3                  <1> 	retn
  5306                              <1> seektell3:
  5307                              <1> 	; 03/01/2016
  5308                              <1> 	;;movzx	eax, word [i.size]
  5309                              <1> 	;mov   	ax, [i.size]
  5310                              <1> 	;	; mov i.size,u.count /  put number of bytes 
  5311                              <1> 	;			; / in file in u.count
  5312                              <1> 	;;jmp	short seektell5
  5313                              <1> 	;	; br 2f
  5314                              <1> 	; 06/05/2021 - Retro UNIX 386 v2
  5315 00004E22 A1[7C700000]        <1> 	mov	eax, [i.size]
  5316 00004E27 C3                  <1> 	retn
  5317                              <1> seektell4: ; 1: / ptrname =1
  5318                              <1> 	;mov	ebx, [u.fofp]
  5319 00004E28 8B03                <1> 	mov	eax, [ebx]
  5320                              <1> 		; mov *u.fofp,u.count / put offset in u.count
  5321                              <1> ;seektell5: ; 2: / ptrname =0
  5322                              <1> 	;mov	[u.count], eax
  5323                              <1> 	;pop	eax 
  5324                              <1> 		; mov (sp)+,r1 / i-number on stack  r1
  5325 00004E2A C3                  <1> 	retn
  5326                              <1> 		; rts r0
  5327                              <1> 
  5328                              <1> sysintr: ; / set interrupt handling
  5329                              <1> 	; 22/06/2015 (Retro UNIX 386 v1 - Beginning)
  5330                              <1> 	; 07/07/2013 (Retro UNIX 8086 v1)
  5331                              <1> 	;
  5332                              <1> 	; 'sysintr' sets the interrupt handling value. It puts
  5333                              <1> 	; argument of its call in u.intr then branches into 'sysquit'
  5334                              <1> 	; routine. u.tty is checked if to see if a control tty exists.
  5335                              <1> 	; If one does the interrupt character in the tty buffer is
  5336                              <1> 	; cleared and 'sysret'is called. If one does not exits
  5337                              <1> 	; 'sysret' is just called.	
  5338                              <1> 	;
  5339                              <1> 	; Calling sequence:
  5340                              <1> 	;	sysintr; arg
  5341                              <1> 	; Argument:
  5342                              <1> 	;	arg - if 0, interrupts (ASCII DELETE) are ignored.
  5343                              <1> 	;	    - if 1, intterupts cause their normal result
  5344                              <1> 	;		 i.e force an exit.
  5345                              <1> 	;	    - if arg is a location within the program,
  5346                              <1> 	;		control is passed to that location when
  5347                              <1> 	;		an interrupt occurs.	
  5348                              <1> 	; Inputs: -
  5349                              <1> 	; Outputs: -
  5350                              <1> 	; ...............................................................
  5351                              <1> 	;	
  5352                              <1> 	; Retro UNIX 8086 v1 modification: 
  5353                              <1> 	;       'sysintr' system call sets u.intr to value of BX
  5354                              <1> 	;	then branches into sysquit.
  5355                              <1> 	;
  5356 00004E2B 66891D[24750000]    <1> 	mov	[u.intr], bx
  5357                              <1> 		; jsr r0,arg; u.intr / put the argument in u.intr
  5358                              <1> 		; br 1f / go into quit routine
  5359 00004E32 E993EDFFFF          <1> 	jmp	sysret
  5360                              <1> 
  5361                              <1> sysquit:
  5362                              <1> 	; 22/06/2015 (Retro UNIX 386 v1 - Beginning)
  5363                              <1> 	; 07/07/2013 (Retro UNIX 8086 v1)
  5364                              <1> 	;
  5365                              <1> 	; 'sysquit' turns off the quit signal. it puts the argument of
  5366                              <1> 	; the call in u.quit. u.tty is checked if to see if a control 
  5367                              <1> 	; tty exists. If one does the interrupt character in the tty
  5368                              <1> 	; buffer is cleared and 'sysret'is called. If one does not exits
  5369                              <1> 	; 'sysret' is just called.	
  5370                              <1> 	;
  5371                              <1> 	; Calling sequence:
  5372                              <1> 	;	sysquit; arg
  5373                              <1> 	; Argument:
  5374                              <1> 	;	arg - if 0, this call diables quit signals from the
  5375                              <1> 	;		typewriter (ASCII FS)
  5376                              <1> 	;	    - if 1, quits are re-enabled and cause execution to
  5377                              <1> 	;		cease and a core image to be produced.
  5378                              <1> 	;		 i.e force an exit.
  5379                              <1> 	;	    - if arg is an addres in the program,
  5380                              <1> 	;		a quit causes control to sent to that
  5381                              <1> 	;		location.	
  5382                              <1> 	; Inputs: -
  5383                              <1> 	; Outputs: -
  5384                              <1> 	; ...............................................................
  5385                              <1> 	;	
  5386                              <1> 	; Retro UNIX 8086 v1 modification: 
  5387                              <1> 	;       'sysquit' system call sets u.quit to value of BX
  5388                              <1> 	;	then branches into 'sysret'.
  5389                              <1> 	;
  5390 00004E37 66891D[26750000]    <1> 	mov	[u.quit], bx
  5391 00004E3E E987EDFFFF          <1> 	jmp	sysret
  5392                              <1> 		; jsr r0,arg; u.quit / put argument in u.quit
  5393                              <1> 	;1:
  5394                              <1> 		; mov u.ttyp,r1 / move pointer to control tty buffer
  5395                              <1> 			      ; / to r1
  5396                              <1> 		; beq sysret4 / return to user
  5397                              <1> 		; clrb 6(r1) / clear the interrupt character 
  5398                              <1> 			   ; / in the tty buffer
  5399                              <1> 		; br sysret4 / return to user
  5400                              <1> 
  5401                              <1> syssetuid: ; / set process id
  5402                              <1> 	; 09/01/2022 - Retro UNIX 386 v1.2
  5403                              <1> 	; 27/03/2021 - Retro UNIX 386 v2
  5404                              <1> 	;		(16 bit uid)
  5405                              <1> 	; 22/06/2015 (Retro UNIX 386 v1 - Beginning)
  5406                              <1> 	; 07/07/2013 - 02/08/2013 (Retro UNIX 8086 v1)
  5407                              <1> 	;
  5408                              <1> 	; 'syssetuid' sets the user id (u.uid) of the current process
  5409                              <1> 	; to the process id in (u.r0). Both the effective user u.uid
  5410                              <1> 	; and the real user u.ruid are set to this. 
  5411                              <1> 	; Only the super user can make this call.	
  5412                              <1> 	;
  5413                              <1> 	; Calling sequence:
  5414                              <1> 	;	syssetuid
  5415                              <1> 	; Arguments: -
  5416                              <1> 	;
  5417                              <1> 	; Inputs: (u.r0) - contains the process id.
  5418                              <1> 	; Outputs: -
  5419                              <1> 	; ...............................................................
  5420                              <1> 	;	
  5421                              <1> 	; Retro UNIX 8086 v1 modification: 
  5422                              <1> 	;       BL contains the (new) user ID of the current process
  5423                              <1> 
  5424                              <1> 	; 27/03/2021
  5425                              <1> 	; INPUT:
  5426                              <1> 	;    BX = (new) user ID
  5427                              <1> 
  5428                              <1> 		; movb *u.r0,r1 / move process id (number) to r1
  5429                              <1> 	;cmp	bl, [u.ruid] 
  5430                              <1> 	;	; cmpb r1,u.ruid / is it equal to the real user 
  5431                              <1> 			       ; / id number
  5432                              <1> 	; 27/03/2021
  5433 00004E43 663B1D[2C750000]    <1> 	cmp	bx, [u.ruid]
  5434 00004E4A 7420                <1> 	je	short setuid1
  5435                              <1> 		; beq 1f / yes
  5436                              <1> 	;cmp	byte [u.uid], 0 ; 02/08/2013
  5437                              <1> 	;	; tstb u.uid / no, is current user the super user?
  5438                              <1> 	;;ja	error
  5439                              <1> 	;	; bne error4 / no, error
  5440                              <1> 	; 27/03/2021
  5441 00004E4C 66833D[2A750000]00  <1> 	cmp	word [u.uid], 0 
  5442 00004E54 760F                <1> 	jna	short setuid0
  5443                              <1> setuid_err:
  5444                              <1> setgid_err:
  5445 00004E56 C705[4C750000]0B00- <1> 	mov	dword [u.error], ERR_NOT_SUPERUSER  ; 11
  5445 00004E5E 0000                <1>
  5446                              <1> 				;  'permission denied !' error
  5447 00004E60 E945EDFFFF          <1> 	jmp	error
  5448                              <1> setuid0:
  5449                              <1> 	;mov	[u.ruid], bl
  5450                              <1> 	; 27/03/2021
  5451 00004E65 66891D[2C750000]    <1> 	mov	[u.ruid], bx
  5452                              <1> setuid1: ; 1:
  5453                              <1> 	;mov	[u.uid], bl ; 02/08/2013
  5454                              <1> 	;	; movb r1,u.uid / put process id in u.uid
  5455                              <1> 	;	; movb r1,u.ruid / put process id in u.ruid
  5456                              <1> 	; 27/03/2021
  5457 00004E6C 66891D[2A750000]    <1> 	mov	[u.uid], bx
  5458 00004E73 E952EDFFFF          <1> 	jmp	sysret
  5459                              <1> 		; br sysret4 / system return
  5460                              <1> 
  5461                              <1> sysgetuid: ; < get user id >
  5462                              <1> 	; 09/01/2022 - Retro UNIX 386 v1.2
  5463                              <1> 	; 27/03/2021 - Retro UNIX 386 v2
  5464                              <1> 	;		(16 bit uid)
  5465                              <1> 	; 22/06/2015 (Retro UNIX 386 v1 - Beginning)
  5466                              <1> 	; 07/07/2013 (Retro UNIX 8086 v1)
  5467                              <1> 	;
  5468                              <1> 	; 'sysgetuid' returns the real user ID of the current process.
  5469                              <1> 	; The real user ID identifies the person who is logged in,
  5470                              <1> 	; in contradistinction to the effective user ID, which
  5471                              <1> 	; determines his access permission at each moment. It is thus
  5472                              <1> 	; useful to programs which operate using the 'set user ID'
  5473                              <1> 	; mode, to find out who invoked them.	
  5474                              <1> 	;
  5475                              <1> 	; Calling sequence:
  5476                              <1> 	;	syssetuid
  5477                              <1> 	; Arguments: -
  5478                              <1> 	;
  5479                              <1> 	; Inputs: -
  5480                              <1> 	; Outputs: (u.r0) - contains the real user's id.
  5481                              <1> 	; ...............................................................
  5482                              <1> 	;	
  5483                              <1> 	; Retro UNIX 8086 v1 modification: 
  5484                              <1> 	;       AL contains the real user ID at return.
  5485                              <1> 	;
  5486                              <1> 	;;movzx eax, byte [u.ruid]
  5487                              <1> 	; 27/03/2021
  5488                              <1> 	;movzx	eax, word [u.ruid]
  5489                              <1> 	; 09/01/2022
  5490 00004E78 66A1[2C750000]      <1> 	mov	ax, [u.ruid]
  5491 00004E7E A3[D8740000]        <1> 	mov	[u.r0], eax
  5492                              <1> 		; movb	u.ruid,*u.r0 / move the real user id to (u.r0)
  5493 00004E83 E942EDFFFF          <1> 	jmp	sysret
  5494                              <1> 		; br sysret4 / systerm return, sysret
  5495                              <1> 
  5496                              <1> syssetgid: ; set group id
  5497                              <1> 	; 09/01/2022 - Retro UNIX 386 v1.2
  5498                              <1> 	; 02/04/2021
  5499                              <1> 	; 27/03/2021 - Retro UNIX 386 v2
  5500                              <1> 	;
  5501                              <1> 	; 'syssetgid' sets the user's group id (u.gid) 
  5502                              <1> 	;  of the current process to the process id in (u.r0).
  5503                              <1> 	;  Both the effective user u.gid and the real user
  5504                              <1> 	;  u.rgid are set to this. 
  5505                              <1> 	;  Only the super user can make this call.	
  5506                              <1> 	
  5507                              <1> 	; INPUT:
  5508                              <1> 	;	BL = (new) group ID
  5509                              <1> 	; OUTPUT:
  5510                              <1> 	;	-
  5511                              <1> 
  5512 00004E88 3A1D[2F750000]      <1> 	cmp	bl, [u.rgid] 
  5513 00004E8E 7410                <1> 	je	short setgid1
  5514                              <1> 
  5515                              <1> 	;cmp	byte [u.uid], 0
  5516                              <1> 	; 02/04/2021
  5517 00004E90 66833D[2A750000]00  <1> 	cmp	word [u.uid], 0
  5518 00004E98 77BC                <1> 	ja	short setgid_err
  5519                              <1> setgid0:
  5520 00004E9A 881D[2F750000]      <1> 	mov	[u.rgid], bl
  5521                              <1> setgid1: ; 1:
  5522 00004EA0 881D[2E750000]      <1> 	mov	[u.gid], bl
  5523 00004EA6 E91FEDFFFF          <1> 	jmp	sysret
  5524                              <1> 
  5525                              <1> sysgetgid: ; < get group id > (& user ID)
  5526                              <1> 	; 09/01/2022 - Retro UNIX 386 v1.2
  5527                              <1> 	; 27/03/2021 - Retro UNIX 386 v2
  5528                              <1> 	;
  5529                              <1> 	; 'sysgetgid' returns the real group ID of the current process.
  5530                              <1> 	; The real group ID identifies the group of the person
  5531                              <1> 	; who is logged in, in contradistinction to the effective
  5532                              <1> 	; group ID, which determines his access permission at each moment.
  5533                              <1> 	; It is thus useful to programs which operate using
  5534                              <1> 	; the 'set group ID' mode, to find out the group of the user
  5535                              <1> 	; who invoked them.
  5536                              <1> 
  5537                              <1> 	;movzx	eax, word [u.ruid]
  5538                              <1> 	; 09/01/2022
  5539 00004EAB 66A1[2C750000]      <1> 	mov	ax, [u.ruid]
  5540 00004EB1 C1E008              <1> 	shl	eax, 8
  5541                              <1> 	; user ID in byte 1 and byte 2
  5542 00004EB4 A0[2F750000]        <1> 	mov	al, [u.rgid] ; group ID in byte 0
  5543 00004EB9 A3[D8740000]        <1> 	mov	[u.r0], eax
  5544 00004EBE E907EDFFFF          <1> 	jmp	sysret
  5545                              <1> 
  5546                              <1> sysver: ; get operating system version
  5547                              <1> 	; 09/01/2022 - Retro UNIX 386 v1.2
  5548                              <1> 	; 18/06/2021 - Retro UNIX 386 v2
  5549                              <1> 
  5550                              <1> 	;VMAJOR equ 2 ; Retro UNIX 386 v2
  5551                              <1> 	;VMINOR equ 0 ; Retro UNIX 386 v2.0 (v2.0.0)
  5552                              <1> 	;CURRENT_VERSION equ (VMAJOR*256)+VMINOR
  5553                              <1> 
  5554                              <1> 	; 09/01/2022
  5555                              <1> 	VMAJOR equ 1 ; Retro UNIX 386 v1
  5556                              <1> 	VMINOR equ 2 ; Retro UNIX 386 v1.2 (v1.2.0)
  5557                              <1> 	CURRENT_VERSION equ (VMAJOR*256)+VMINOR 		  
  5558                              <1> 
  5559                              <1> 	; 29/04/2016 - TRDOS 386 v2.0
  5560                              <1> 	;mov	dword [u.r0], 200h ; AH = major version, AL = minor version 
  5561                              <1> 	; 18/06/2021
  5562 00004EC3 C705[D8740000]0201- <1> 	mov	dword [u.r0], CURRENT_VERSION ;	(ah = 2, al = 0)
  5562 00004ECB 0000                <1>
  5563 00004ECD E9F8ECFFFF          <1> 	jmp	sysret
  5564                              <1> 
  5565                              <1> anyi: 
  5566                              <1> 	; 09/01/2022
  5567                              <1> 	; 02/01/2022
  5568                              <1> 	; 01/01/2022
  5569                              <1> 	; 27/12/2021 (Retro UNIX 386 v1.2)
  5570                              <1> 	; 22/11/2021 - Retro UNIX 386 v2 compatibility modification
  5571                              <1> 	; 22/06/2015 (Retro UNIX 386 v1 - Beginning)
  5572                              <1> 	; 25/04/2013 (Retro UNIX 8086 v1)
  5573                              <1> 	;
  5574                              <1> 	; 'anyi' is called if a file deleted while open.
  5575                              <1> 	; "anyi" checks to see if someone else has opened this file.
  5576                              <1> 	;
  5577                              <1> 	; INPUTS ->
  5578                              <1> 	;    r1 - contains an i-number
  5579                              <1> 	;    fsp - start of table containing open files
  5580                              <1> 	;
  5581                              <1> 	; OUTPUTS ->
  5582                              <1> 	;    "deleted" flag set in fsp entry of another occurrence of
  5583                              <1> 	;	   this file and r2 points 1st word of this fsp entry.
  5584                              <1> 	;    if file not found - bit in i-node map is cleared
  5585                              <1> 	;    			 (i-node is freed)
  5586                              <1> 	;               all blocks related to i-node are freed
  5587                              <1> 	;	        all flags in i-node are cleared
  5588                              <1> 	; ((AX = R1)) input
  5589                              <1> 	;
  5590                              <1> 	;    (Retro UNIX Prototype: 02/12/2012, UNIXCOPY.ASM)
  5591                              <1>         ;    ((Modified registers: eDX, eCX, eBX, eSI, eDI, eBP))  
  5592                              <1> 	;
  5593                              <1> 		; / r1 contains an i-number
  5594 00004ED2 BB[74710000]        <1> 	mov	ebx, fsp
  5595                              <1> 		; mov $fsp,r2 / move start of fsp table to r2
  5596                              <1> anyi_1: ; 1:
  5597                              <1> 	;cmp	eax, [ebx] ; 09/01/2022 (32 bit inode number)
  5598 00004ED7 663B03              <1> 	cmp	ax, [ebx]
  5599                              <1> 		; cmp r1,(r2) / do i-numbers match?
  5600 00004EDA 7423                <1> 	je	short anyi_3
  5601                              <1> 		; beq 1f / yes, 1f
  5602                              <1> 	; 27/12/2021 - Retro UNIX 386 v1.2 (runix v2 fs)
  5603                              <1> 	;neg	ax
  5604                              <1> 	;	; neg r1 / no complement r1
  5605                              <1> 	;cmp	ax, [ebx]
  5606                              <1> 	;	; cmp r1,(r2) / do they match now?
  5607                              <1> 	;je	short anyi_3
  5608                              <1> 		; beq 1f / yes, transfer
  5609                              <1> 		; / i-numbers do not match
  5610                              <1> anyi_0:
  5611                              <1> 	; 27/12/2021 - Retro UNIX 386 v1.2 (runix v2 fs)
  5612                              <1> 	;add	ebx, fp.size ; runix v2 fsp table size is 16 bytes
  5613                              <1> 	; 01/01/2022
  5614 00004EDC 83C310              <1> 	add	ebx, 16 ; runix v2 fsp table size is 16 bytes
  5615                              <1> 	;;add	ebx, 10 ; fsp table size is 10 bytes
  5616                              <1> 			; in Retro UNIX 386 v1 (22/06/2015)
  5617                              <1> 		; add $8,r2 / no, bump to next entry in fsp table
  5618                              <1> 	; 22/11/2021
  5619                              <1> 	;cmp	ebx, fsp + (NFILES*fp.size) ; fsp+(NFILES*16)
  5620                              <1> 	; 02/01/2022
  5621 00004EDF 81FB[94740000]      <1> 	cmp	ebx, fsp + (nfiles*16)
  5622                              <1> 	; 01/01/2022
  5623                              <1> 	;cmp	ebx, fsp + (NFILES*16) ; fsp+(NFILES*fp.size)
  5624                              <1> 	;;cmp	ebx, fsp + (nfiles*10) ; 22/06/2015 
  5625                              <1> 		; cmp r2,$fsp+[nfiles*8] 
  5626                              <1> 			; / are we at last entry in the table
  5627 00004EE5 72F0                <1> 	jb	short anyi_1
  5628                              <1> 		; blt 1b / no, check next entries i-number
  5629                              <1> 	; 27/12/2021
  5630                              <1> 	;;cmp	ax, 32768
  5631                              <1> 	;cmp	ah, 80h ; negative number check
  5632                              <1> 	;	; tst r1 / yes, no match
  5633                              <1> 	;	; bge .+4
  5634                              <1> 	;jb	short anyi_2
  5635                              <1> 	;neg	ax
  5636                              <1> 	;	; neg r1 / make i-number positive
  5637                              <1> ;anyi_2:	
  5638 00004EE7 E886080000          <1> 	call	imap
  5639                              <1> 		; jsr r0,imap / get address of allocation bit 
  5640                              <1> 			    ; / in the i-map in r2
  5641                              <1> 	;; DL/DX (MQ) has a 1 in the calculated bit position
  5642                              <1>         ;; eBX (R2) has address of the byte with allocation bit
  5643                              <1>  	; not	dx
  5644 00004EEC F6D2                <1> 	not 	dl ;; 0 at calculated bit position, other bits are 1
  5645                              <1>         ;and	[ebx], dx
  5646 00004EEE 2013                <1> 	and 	[ebx], dl 
  5647                              <1> 		; bicb mq,(r2) / clear bit for i-node in the imap
  5648 00004EF0 E87B070000          <1> 	call	itrunc
  5649                              <1> 		; jsr r0,itrunc / free all blocks related to i-node
  5650 00004EF5 66C705[74700000]00- <1>  	mov 	word [i.flgs], 0
  5650 00004EFD 00                  <1>
  5651                              <1> 		; clr i.flgs / clear all flags in the i-node
  5652 00004EFE C3                  <1> 	retn
  5653                              <1> 		;rts	r0 / return
  5654                              <1> anyi_3: ; 1: / i-numbers match
  5655                              <1> 	; 09/01/2022
  5656 00004EFF F6430501            <1> 	test	byte [ebx+5], 1 ; open for writing flag
  5657 00004F03 74D7                <1> 	jz	short anyi_0	
  5658                              <1> 
  5659                              <1> 	; 27/12/2021 - Retro UNIX 386 v1.2 (runix v2 fs)
  5660 00004F05 804B0580            <1> 	or	byte [ebx+5], 80h ; set deleted file flag (bit 7)
  5661                              <1> 	; 22/06/2015
  5662                              <1> 	;inc 	byte [ebx+9]
  5663                              <1> 		;incb 7(r2) / increment upper byte of the 4th word
  5664                              <1> 		   ; / in that fsp entry (deleted flag of fsp entry)
  5665 00004F09 C3                  <1> 	retn
  5666                              <1> 		; rts r0
  2072                                  %include 'u3.s'      ; 10/05/2015
  2073                              <1> ; Temporary Runix kernel v2.0 file for debug - 22/11/2021
  2074                              <1> ; (re-write kernel for test by using previous version without a major defect)
  2075                              <1> ; ****************************************************************************
  2076                              <1> ; Retro UNIX 386 v1 Kernel (v0.2) - SYS3.INC
  2077                              <1> ; Last Modification: 24/12/2021
  2078                              <1> ; ----------------------------------------------------------------------------
  2079                              <1> ; Derived from 'Retro UNIX 8086 v1' source code by Erdogan Tan
  2080                              <1> ; (v0.1 - Beginning: 11/07/2012)
  2081                              <1> ;
  2082                              <1> ; Derived from UNIX Operating System (v1.0 for PDP-11) 
  2083                              <1> ; (Original) Source Code by Ken Thompson (1971-1972)
  2084                              <1> ; <Bell Laboratories (17/3/1972)>
  2085                              <1> ; <Preliminary Release of UNIX Implementation Document>
  2086                              <1> ;
  2087                              <1> ; Retro UNIX 8086 v1 - U3.ASM (08/03/2014) //// UNIX v1 -> u3.s
  2088                              <1> ;
  2089                              <1> ; ****************************************************************************
  2090                              <1> 
  2091                              <1> tswitch: ; Retro UNIX 386 v1
  2092                              <1> tswap:
  2093                              <1> 	; 01/09/2015
  2094                              <1> 	; 10/05/2015 (Retro UNIX 386 v1 - Beginning)
  2095                              <1> 	; 14/04/2013 - 14/02/2014 (Retro UNIX 8086 v1)
  2096                              <1> 	; time out swap, called when a user times out.
  2097                              <1> 	; the user is put on the low priority queue.
  2098                              <1> 	; This is done by making a link from the last user
  2099                              <1> 	; on the low priority queue to him via a call to 'putlu'.
  2100                              <1> 	; then he is swapped out.
  2101                              <1> 	;
  2102                              <1> 	; Retro UNIX 386 v1 modification ->
  2103                              <1> 	;       swap (software task switch) is performed by changing
  2104                              <1> 	;	user's page directory (u.pgdir) instead of segment change
  2105                              <1> 	;	as in Retro UNIX 8086 v1.
  2106                              <1> 	;
  2107                              <1> 	; RETRO UNIX 8086 v1 modification ->
  2108                              <1> 	;       'swap to disk' is replaced with 'change running segment'
  2109                              <1> 	;	according to 8086 cpu (x86 real mode) architecture.
  2110                              <1> 	;	pdp-11 was using 64KB uniform memory while IBM PC
  2111                              <1> 	;	compatibles was using 1MB segmented memory 
  2112                              <1> 	;	in 8086/8088 times.
  2113                              <1> 	;
  2114                              <1> 	; INPUTS ->
  2115                              <1> 	;    u.uno - users process number
  2116                              <1> 	;    runq+4 - lowest priority queue
  2117                              <1> 	; OUTPUTS ->
  2118                              <1> 	;    r0 - users process number
  2119                              <1> 	;    r2 - lowest priority queue address
  2120                              <1> 	;
  2121                              <1> 	; ((AX = R0, BX = R2)) output
  2122                              <1> 	; ((Modified registers: EDX, EBX, ECX, ESI, EDI))  	
  2123                              <1> 	;
  2124 00004F0A A0[29750000]        <1> 	mov 	al, [u.uno]
  2125                              <1> 	       	; movb u.uno,r1 / move users process number to r1
  2126                              <1> 		; mov  $runq+4,r2 
  2127                              <1> 			; / move lowest priority queue address to r2
  2128 00004F0F E8CD000000          <1>         call 	putlu
  2129                              <1> 		; jsr r0,putlu / create link from last user on Q to 
  2130                              <1> 		             ; / u.uno's user
  2131                              <1> 
  2132                              <1> switch: ; Retro UNIX 386 v1
  2133                              <1> swap:
  2134                              <1> 	; 24/12/2021 (Retro UNIX 386 v1.2)
  2135                              <1> 	; 02/09/2015
  2136                              <1> 	; 01/09/2015
  2137                              <1> 	; 31/08/2015
  2138                              <1> 	; 10/05/2015 (Retro UNIX 386 v1 - Beginning)
  2139                              <1> 	; 14/04/2013 - 08/03/2014 (Retro UNIX 8086 v1)
  2140                              <1> 	; 'swap' is routine that controls the swapping of processes
  2141                              <1> 	; in and out of core.
  2142                              <1> 	;
  2143                              <1> 	; Retro UNIX 386 v1 modification ->
  2144                              <1> 	;       swap (software task switch) is performed by changing
  2145                              <1> 	;	user's page directory (u.pgdir) instead of segment change
  2146                              <1> 	;	as in Retro UNIX 8086 v1.
  2147                              <1> 	;
  2148                              <1> 	; RETRO UNIX 8086 v1 modification ->
  2149                              <1> 	;       'swap to disk' is replaced with 'change running segment'
  2150                              <1> 	;	according to 8086 cpu (x86 real mode) architecture.
  2151                              <1> 	;	pdp-11 was using 64KB uniform memory while IBM PC
  2152                              <1> 	;	compatibles was using 1MB segmented memory 
  2153                              <1> 	;	in 8086/8088 times.
  2154                              <1> 	;
  2155                              <1> 	; INPUTS ->
  2156                              <1> 	;    runq table - contains processes to run.
  2157                              <1> 	;    p.link - contains next process in line to be run.
  2158                              <1> 	;    u.uno - process number of process in core	
  2159                              <1> 	;    s.stack - swap stack used as an internal stack for swapping.	
  2160                              <1> 	; OUTPUTS ->
  2161                              <1> 	;    (original unix v1 -> present process to its disk block)
  2162                              <1> 	;    (original unix v1 -> new process into core -> 
  2163                              <1> 	;	   Retro Unix 8086 v1 -> segment registers changed 
  2164                              <1> 	;	   for new process)
  2165                              <1> 	;    u.quant = 3 (Time quantum for a process)
  2166                              <1> 	; 	((INT 1Ch count down speed -> 18.2 times per second)	 	
  2167                              <1> 	;    RETRO UNIX 8086 v1 will use INT 1Ch (18.2 times per second)
  2168                              <1> 	;	 for now, it will swap the process if there is not
  2169                              <1> 	;	 a keyboard event (keystroke) (Int 15h, function 4Fh)
  2170                              <1> 	;	 or will count down from 3 to 0 even if there is a
  2171                              <1> 	;        keyboard event locking due to repetitive key strokes.
  2172                              <1> 	;	 u.quant will be reset to 3 for RETRO UNIX 8086 v1.
  2173                              <1> 	;
  2174                              <1> 	;    u.pri -points to highest priority run Q.
  2175                              <1> 	;    r2 - points to the run queue.
  2176                              <1> 	;    r1 - contains new process number
  2177                              <1> 	;    r0 - points to place in routine or process that called
  2178                              <1> 	;	  swap all user parameters
  2179                              <1> 	;				
  2180                              <1> 	; ((Modified registers: EAX, EDX, EBX, ECX, ESI, EDI))  	
  2181                              <1> 	;
  2182                              <1> swap_0:
  2183                              <1> 		;mov $300,*$ps / processor priority = 6
  2184 00004F14 BE[C8740000]        <1> 	mov	esi, runq
  2185                              <1> 		; mov $runq,r2 / r2 points to runq table
  2186                              <1> swap_1: ; 1: / search runq table for highest priority process
  2187 00004F19 668B06              <1> 	mov	ax, [esi]
  2188 00004F1C 6621C0              <1> 	and 	ax, ax
  2189                              <1>        		; tst (r2)+ / are there any processes to run 
  2190                              <1> 			  ; / in this Q entry
  2191 00004F1F 7507                <1> 	jnz	short swap_2
  2192                              <1>        		; bne 1f / yes, process 1f
  2193                              <1> 		; cmp r2,$runq+6 / if zero compare address 
  2194                              <1> 			       ; / to end of table
  2195                              <1> 		; bne 1b / if not at end, go back
  2196 00004F21 E8E0000000          <1> 	call	idle
  2197                              <1> 		; jsr r0,idle; s.idlet+2 / wait for interrupt; 
  2198                              <1> 				       ; / all queues are empty
  2199 00004F26 EBF1                <1> 	jmp	short swap_1
  2200                              <1> 		; br swap
  2201                              <1> swap_2: ; 1:
  2202 00004F28 0FB6D8              <1> 	movzx	ebx, al ; 02/09/2015
  2203                              <1> 		; tst -(r2) / restore pointer to right Q entry
  2204                              <1>  		; mov r2,u.pri / set present user to this run queue
  2205                              <1> 	        ; movb (r2)+,r1 / move 1st process in queue to r1
  2206 00004F2B 38E0                <1> 	cmp	al, ah
  2207                              <1> 		; cmpb r1,(r2)+ / is there only 1 process 
  2208                              <1> 			      ; / in this Q to be run
  2209 00004F2D 740A                <1> 	je	short swap_3
  2210                              <1>        		; beq 1f / yes
  2211                              <1> 		; tst -(r2) / no, pt r2 back to this Q entry
  2212                              <1> 	;movzx	ebx, al
  2213 00004F2F 8AA3[13710000]      <1> 	mov	ah, [ebx+p.link-1] 
  2214 00004F35 8826                <1>        	mov	[esi], ah
  2215                              <1> 		; movb p.link-1(r1),(r2) / move next process 
  2216                              <1> 				       ; / in line into run queue
  2217 00004F37 EB05                <1> 	jmp	short swap_4
  2218                              <1>        		; br 2f
  2219                              <1> swap_3: ; 1:
  2220                              <1> 	;xor	dx, dx
  2221                              <1> 	; 24/12/2021
  2222 00004F39 31D2                <1> 	xor	edx, edx
  2223 00004F3B 668916              <1> 	mov	[esi], dx
  2224                              <1> 		; clr -(r2) / zero the entry; no processes on the Q
  2225                              <1> swap_4: ; / write out core to appropriate disk area and read 
  2226                              <1>       ; / in new process if required
  2227                              <1>        		; clr *$ps / clear processor status
  2228 00004F3E 8A25[29750000]      <1> 	mov 	ah, [u.uno]
  2229 00004F44 38C4                <1> 	cmp	ah, al
  2230                              <1> 		; cmpb r1,u.uno / is this process the same as 
  2231                              <1> 			      ; / the process in core?
  2232 00004F46 743B                <1>        	je	short swap_8
  2233                              <1>        		; beq 2f / yes, don't have to swap
  2234                              <1>        		; mov r0,-(sp) / no, write out core; save r0 
  2235                              <1> 			   ; / (address in routine that called swap)
  2236                              <1> 		; mov r1,-(sp) / put r1 (new process #) on the stack
  2237                              <1> 	; 01/09/2015
  2238                              <1> 	;mov	[u.usp], esp
  2239                              <1>        		; mov sp,u.usp / save stack pointer
  2240                              <1> 		; mov $sstack,sp / move swap stack pointer 
  2241                              <1> 			       ; / to the stack pointer
  2242 00004F48 08E4                <1> 	or	ah, ah
  2243                              <1>        		; tstb u.uno / is the process # = 0
  2244 00004F4A 740D                <1>        	jz	short swap_6 ; 'sysexit'
  2245                              <1> 		; beq  1f / yes, kill process by overwriting
  2246                              <1> 	; 02/09/2015
  2247 00004F4C 8925[D4740000]      <1> 	mov	[u.usp], esp ; return  address for 'syswait' & 'sleep'
  2248                              <1> 	;
  2249 00004F52 E834000000          <1> 	call	wswap
  2250                              <1> 		;jsr r0,wswap / write out core to disk
  2251                              <1> 	 ; 31/08/2015
  2252                              <1> 	;movzx	ebx, al ; New (running) process number
  2253 00004F57 EB1C                <1> 	jmp 	short swap_7
  2254                              <1> swap_6:
  2255                              <1> 	; 31/08/2015
  2256                              <1> 	; Deallocate memory pages belong to the process
  2257                              <1> 	; which is being terminated
  2258                              <1> 	; 14/05/2015 ('sysexit')
  2259                              <1>  	; Deallocate memory pages of the process
  2260                              <1> 	; (Retro UNIX 386 v1 modification !)
  2261                              <1> 	;
  2262                              <1> 	; movzx ebx, al
  2263 00004F59 53                  <1> 	push	ebx
  2264 00004F5A A1[38750000]        <1> 	mov 	eax, [u.pgdir]  ; page directory of the process
  2265 00004F5F 8B1D[3C750000]      <1> 	mov	ebx, [u.ppgdir] ; page directory of the parent process
  2266 00004F65 E82CE0FFFF          <1> 	call	deallocate_page_dir
  2267 00004F6A A1[34750000]        <1> 	mov	eax, [u.upage] ; 'user' structure page of the process
  2268 00004F6F E8BAE0FFFF          <1> 	call	deallocate_page
  2269 00004F74 5B                  <1> 	pop	ebx
  2270                              <1> swap_7: ;1: 
  2271                              <1> 	; 02/09/2015
  2272                              <1> 	; 31/08/2015
  2273                              <1> 	; 14/05/2015
  2274 00004F75 C0E302              <1> 	shl	bl, 2 ; * 4 
  2275 00004F78 8B83[30710000]      <1> 	mov	eax, [ebx+p.upage-4] ; the 'u' page of the new process
  2276                              <1> 	;cli
  2277 00004F7E E831000000          <1> 	call	rswap
  2278                              <1>  		; mov (sp)+,r1 / restore r1 to new process number
  2279                              <1> 		; jsr r0,rswap / read new process into core
  2280                              <1>        		; jsr r0,unpack / unpack the users stack from next
  2281                              <1> 			      ; / to his program to its normal
  2282                              <1> 	; 01/09/2015
  2283                              <1> 	;mov	esp, [u.usp]	
  2284                              <1> 		; mov u.usp,sp / location; restore stack pointer to
  2285                              <1> 			     ; / new process stack
  2286                              <1> 		; mov (sp)+,r0 / put address of where the process 
  2287                              <1> 			     ; / that just got swapped in, left off.,
  2288                              <1> 			     ; / i.e., transfer control to new process
  2289                              <1> 	;sti
  2290                              <1> swap_8: ;2:
  2291                              <1> 	; RETRO UNIX 8086 v1 modification !
  2292 00004F83 C605[20750000]04    <1> 	mov	byte [u.quant], time_count 
  2293                              <1> 		; movb $30.,uquant / initialize process time quantum
  2294 00004F8A C3                  <1> 	retn
  2295                              <1> 		; rts r0 / return
  2296                              <1> 
  2297                              <1> wswap:  ; < swap out, swap to disk >
  2298                              <1> 	; 09/05/2015 (Retro UNIX 386 v1 - Beginning)
  2299                              <1> 	; 26/05/2013 - 08/03/2014 (Retro UNIX 8086 v1)
  2300                              <1> 	; 'wswap' writes out the process that is in core onto its 
  2301                              <1> 	; appropriate disk area.
  2302                              <1> 	;
  2303                              <1> 	; Retro UNIX 386 v1 modification ->
  2304                              <1> 	;       User (u) structure content and the user's register content
  2305                              <1> 	;	will be copied to the process's/user's UPAGE (a page for
  2306                              <1> 	;	saving 'u' structure and user registers for task switching).
  2307                              <1> 	;	u.usp - points to kernel stack address which contains
  2308                              <1> 	;		user's registers while entering system call.  
  2309                              <1> 	;	u.sp  - points to kernel stack address 
  2310                              <1> 	;		to return from system call -for IRET-.
  2311                              <1> 	;	[u.usp]+32+16 = [u.sp] 
  2312                              <1> 	;	[u.usp] -> edi, esi, ebp, esp (= [u.usp]+32), ebx, 
  2313                              <1> 	;		edx, ecx, eax, gs, fs, es, ds, -> [u.sp].
  2314                              <1> 	;
  2315                              <1> 	; Retro UNIX 8086 v1 modification ->
  2316                              <1> 	;       'swap to disk' is replaced with 'change running segment'
  2317                              <1> 	;	according to 8086 cpu (x86 real mode) architecture.
  2318                              <1> 	;	pdp-11 was using 64KB uniform memory while IBM PC
  2319                              <1> 	;	compatibles was using 1MB segmented memory 
  2320                              <1> 	;	in 8086/8088 times.
  2321                              <1> 	;
  2322                              <1> 	; INPUTS ->
  2323                              <1> 	;    u.break - points to end of program
  2324                              <1> 	;    u.usp - stack pointer at the moment of swap
  2325                              <1> 	;    core - beginning of process program		
  2326                              <1> 	;    ecore - end of core 	
  2327                              <1> 	;    user - start of user parameter area		
  2328                              <1> 	;    u.uno - user process number	
  2329                              <1> 	;    p.dska - holds block number of process	
  2330                              <1> 	; OUTPUTS ->
  2331                              <1> 	;    swp I/O queue
  2332                              <1> 	;    p.break - negative word count of process 
  2333                              <1> 	;    r1 - process disk address	
  2334                              <1> 	;    r2 - negative word count
  2335                              <1> 	;
  2336                              <1> 	; RETRO UNIX 8086 v1 input/output:
  2337                              <1> 	;
  2338                              <1> 	; INPUTS ->
  2339                              <1> 	;    u.uno - process number (to be swapped out)
  2340                              <1> 	; OUTPUTS ->
  2341                              <1> 	;    none
  2342                              <1> 	;
  2343                              <1> 	;   ((Modified registers: ECX, ESI, EDI))  
  2344                              <1> 	;
  2345 00004F8B 8B3D[34750000]      <1> 	mov	edi, [u.upage] ; process's user (u) structure page addr
  2346 00004F91 B921000000          <1> 	mov	ecx, (U_SIZE + 3) / 4
  2347 00004F96 BE[D0740000]        <1> 	mov	esi, user ; active user (u) structure	
  2348 00004F9B F3A5                <1> 	rep	movsd
  2349                              <1> 	;
  2350 00004F9D 8B35[D4740000]      <1> 	mov	esi, [u.usp] ; esp (system stack pointer, 
  2351                              <1> 			     ;      points to user registers)
  2352 00004FA3 8B0D[D0740000]      <1> 	mov	ecx, [u.sp]  ; return address from the system call
  2353                              <1> 			     ; (for IRET)
  2354                              <1> 			     ; [u.sp] -> EIP (user)
  2355                              <1> 			     ; [u.sp+4]-> CS (user)
  2356                              <1> 			     ; [u.sp+8] -> EFLAGS (user)
  2357                              <1> 			     ; [u.sp+12] -> ESP (user)
  2358                              <1> 			     ; [u.sp+16] -> SS (user)	
  2359 00004FA9 29F1                <1> 	sub	ecx, esi     ; required space for user registers
  2360 00004FAB 83C114              <1> 	add	ecx, 20	     ; +5 dwords to return from system call
  2361                              <1> 			     ; (for IRET) 	
  2362 00004FAE C1E902              <1> 	shr	ecx, 2	     		
  2363 00004FB1 F3A5                <1> 	rep	movsd
  2364 00004FB3 C3                  <1> 	retn
  2365                              <1> 
  2366                              <1> 	; Original UNIX v1 'wswap' routine:
  2367                              <1> 	; wswap:
  2368                              <1> 		; mov *$30,u.emt / determines handling of emts
  2369                              <1>         	; mov *$10,u.ilgins / determines handling of 
  2370                              <1> 				; / illegal instructions
  2371                              <1> 		; mov u.break,r2 / put process program break address in r2
  2372                              <1> 		; inc r2 / add 1 to it 
  2373                              <1> 		; bic $1,r2 / make it even
  2374                              <1> 		; mov r2,u.break / set break to an even location
  2375                              <1> 		; mov u.usp,r3 / put users stack pointer 
  2376                              <1> 			     ; / at moment of swap in r3
  2377                              <1> 		; cmp r2,$core / is u.break less than $core
  2378                              <1> 		; blos 2f / yes
  2379                              <1> 		; cmp r2,r3 / no, is (u.break) greater than stack ptr.
  2380                              <1>        		; bhis 2f / yes
  2381                              <1> 	; 1:
  2382                              <1>        		; mov (r3)+,(r2)+ / no, pack stack next to users program
  2383                              <1> 		; cmp r3,$ecore / has stack reached end of core
  2384                              <1> 		; bne 1b / no, keep packing
  2385                              <1> 	 	; br 1f / yes
  2386                              <1> 	; 2:
  2387                              <1>        		; mov $ecore,r2 / put end of core in r2 
  2388                              <1> 	; 1:
  2389                              <1>        		; sub  $user,r2 / get number of bytes to write out 
  2390                              <1> 			   ; / (user up to end of stack gets written out)
  2391                              <1> 		; neg r2 / make it negative
  2392                              <1> 		; asr r2 / change bytes to words (divide by 2)
  2393                              <1> 		; mov r2,swp+4 / word count
  2394                              <1> 		; movb u.uno,r1 / move user process number to r1
  2395                              <1> 		; asl r1 / x2 for index
  2396                              <1>       		; mov r2,p.break-2(r1) / put negative of word count 
  2397                              <1> 				     ; / into the p.break table
  2398                              <1>        		; mov p.dska-2(r1),r1 / move disk address of swap area 
  2399                              <1> 				    ; /	for process to r1
  2400                              <1>        		; mov r1,swp+2 / put processes dska address in swp+2 
  2401                              <1> 			     ; / (block number)
  2402                              <1> 		; bis $1000,swp / set it up to write (set bit 9)
  2403                              <1>        		; jsr r0,ppoke / write process out on swap area of disk
  2404                              <1> 	; 1:
  2405                              <1>        		; tstb swp+1 / is lt done writing?
  2406                              <1>        		; bne 1b / no, wait
  2407                              <1> 		; rts r0 / yes, return to swap
  2408                              <1> 
  2409                              <1> rswap:  ; < swap in, swap from disk >
  2410                              <1> 	; 15/09/2015
  2411                              <1> 	; 28/08/2015
  2412                              <1> 	; 14/05/2015
  2413                              <1> 	; 09/05/2015 (Retro UNIX 386 v1 - Beginning)
  2414                              <1> 	; 26/05/2013 - 08/03/2014 (Retro UNIX 8086 v1)
  2415                              <1> 	; 'rswap' reads a process whose number is in r1, 
  2416                              <1> 	; from disk into core.
  2417                              <1> 	;
  2418                              <1> 	; Retro UNIX 386 v1 modification ->
  2419                              <1> 	;       User (u) structure content and the user's register content
  2420                              <1> 	;	will be restored from process's/user's UPAGE (a page for
  2421                              <1> 	;	saving 'u' structure and user registers for task switching).
  2422                              <1> 	;	u.usp - points to kernel stack address which contains
  2423                              <1> 	;		user's registers while entering system call.  
  2424                              <1> 	;	u.sp  - points to kernel stack address 
  2425                              <1> 	;		to return from system call -for IRET-.
  2426                              <1> 	;	[u.usp]+32+16 = [u.sp] 
  2427                              <1> 	;	[u.usp] -> edi, esi, ebp, esp (= [u.usp]+32), ebx, 
  2428                              <1> 	;		edx, ecx, eax, gs, fs, es, ds, -> [u.sp].
  2429                              <1> 	;
  2430                              <1> 	; RETRO UNIX 8086 v1 modification ->
  2431                              <1> 	;       'swap to disk' is replaced with 'change running segment'
  2432                              <1> 	;	according to 8086 cpu (x86 real mode) architecture.
  2433                              <1> 	;	pdp-11 was using 64KB uniform memory while IBM PC
  2434                              <1> 	;	compatibles was using 1MB segmented memory 
  2435                              <1> 	;	in 8086/8088 times.
  2436                              <1> 	;
  2437                              <1> 	; INPUTS ->
  2438                              <1> 	;    r1 - process number of process to be read in
  2439                              <1> 	;    p.break - negative of word count of process 
  2440                              <1> 	;    p.dska - disk address of the process		
  2441                              <1> 	;    u.emt - determines handling of emt's 	
  2442                              <1> 	;    u.ilgins - determines handling of illegal instructions		
  2443                              <1> 	; OUTPUTS ->
  2444                              <1> 	;    8 = (u.ilgins)
  2445                              <1> 	;    24 = (u.emt)
  2446                              <1> 	;    swp - bit 10 is set to indicate read 
  2447                              <1> 	;		(bit 15=0 when reading is done)	
  2448                              <1> 	;    swp+2 - disk block address
  2449                              <1> 	;    swp+4 - negative word count 	
  2450                              <1> 	;      ((swp+6 - address of user structure)) 
  2451                              <1> 	;
  2452                              <1> 	; RETRO UNIX 8086 v1 input/output:
  2453                              <1> 	;
  2454                              <1> 	; INPUTS ->
  2455                              <1> 	;    AL	- new process number (to be swapped in)	 
  2456                              <1> 	; OUTPUTS ->
  2457                              <1> 	;    none
  2458                              <1> 	;
  2459                              <1> 	;   ((Modified registers: EAX, ECX, ESI, EDI, ESP)) 
  2460                              <1> 	;
  2461                              <1> 	; Retro UNIX 386 v1 - modification ! 14/05/2015
  2462 00004FB4 89C6                <1> 	mov	esi, eax  ; process's user (u) structure page addr
  2463 00004FB6 B921000000          <1> 	mov	ecx, (U_SIZE + 3) / 4
  2464 00004FBB BF[D0740000]        <1> 	mov	edi, user ; active user (u) structure	
  2465 00004FC0 F3A5                <1> 	rep	movsd
  2466 00004FC2 58                  <1> 	pop	eax ; 15/09/2015, 'rswap' return address 
  2467 00004FC3 8B3D[D4740000]      <1> 	mov	edi, [u.usp] ; esp (system stack pointer, 
  2468                              <1> 			     ;      points to user registers)
  2469 00004FC9 8B0D[D0740000]      <1> 	mov	ecx, [u.sp]  ; return address from the system call
  2470                              <1> 			     ; (for IRET)
  2471                              <1> 			     ; [u.sp] -> EIP (user)
  2472                              <1> 			     ; [u.sp+4]-> CS (user)
  2473                              <1> 			     ; [u.sp+8] -> EFLAGS (user)
  2474                              <1> 			     ; [u.sp+12] -> ESP (user)
  2475                              <1> 			     ; [u.sp+16] -> SS (user)		
  2476                              <1> 	; 28/08/2015
  2477 00004FCF 29F9                <1> 	sub	ecx, edi     ; required space for user registers
  2478 00004FD1 83C114              <1> 	add	ecx, 20	     ; +5 dwords to return from system call
  2479                              <1> 			     ; (for IRET) 	
  2480 00004FD4 C1E902              <1> 	shr	ecx, 2	       		
  2481 00004FD7 F3A5                <1> 	rep	movsd
  2482 00004FD9 8B25[D4740000]      <1> 	mov	esp, [u.usp] ; 15/09/2015
  2483 00004FDF 50                  <1> 	push	eax ; 15/09/2015 'rswap' return address
  2484 00004FE0 C3                  <1> 	retn
  2485                              <1> 
  2486                              <1> 	; Original UNIX v1 'rswap'  and 'unpack' routines:
  2487                              <1> 	;rswap:
  2488                              <1>        		; asl r1 / process number x2 for index
  2489                              <1>        		; mov p.break-2(r1), swp+4 / word count
  2490                              <1>        		; mov p.dska-2(r1),swp+2 / disk address
  2491                              <1>        		; bis $2000,swp / read
  2492                              <1>        		; jsr r0,ppoke / read it in 
  2493                              <1> 	; 1:
  2494                              <1>        		; tstb swp+1 / done
  2495                              <1>        		; bne 1b / no, wait for bit 15 to clear (inhibit bit)
  2496                              <1>        		; mov u.emt,*$30 / yes move these
  2497                              <1>        		; mov u.ilgins,*$10 / back
  2498                              <1>        		; rts r0 / return
  2499                              <1> 
  2500                              <1> 	;unpack: ; / move stack back to its normal place
  2501                              <1> 		; mov u.break,r2 / r2 points to end of user program
  2502                              <1>        		; cmp r2,$core / at beginning of user program yet?
  2503                              <1> 		; blos 2f / yes, return
  2504                              <1> 		; cmp r2,u.usp / is break_above the stack pointer 
  2505                              <1> 			     ; / before swapping
  2506                              <1> 		; bhis 2f / yes, return
  2507                              <1> 		; mov $ecore,r3 / r3 points to end of core
  2508                              <1> 		; add r3,r2
  2509                              <1> 		; sub u.usp,r2 / end of users stack is in r2
  2510                              <1> 	; 1:
  2511                              <1> 		; mov -(r2),-(r3) / move stack back to its normal place
  2512                              <1> 		; cmp r2,u.break / in core
  2513                              <1> 		; bne 1b
  2514                              <1> 	; 2:
  2515                              <1>        		; rts r0
  2516                              <1> 
  2517                              <1> putlu: 
  2518                              <1> 	; 12/09/2015
  2519                              <1> 	; 02/09/2015
  2520                              <1> 	; 10/05/2015 (Retro UNIX 386 v1 - Beginning)
  2521                              <1> 	; 15/04/2013 - 23/02/2014 (Retro UNIX 8086 v1)
  2522                              <1> 	; 'putlu' is called with a process number in r1 and a pointer
  2523                              <1> 	; to lowest priority Q (runq+4) in r2. A link is created from
  2524                              <1> 	; the last process on the queue to process in r1 by putting
  2525                              <1> 	; the process number in r1 into the last process's link.
  2526                              <1> 	;
  2527                              <1> 	; INPUTS ->
  2528                              <1> 	;    r1 - user process number
  2529                              <1> 	;    r2 - points to lowest priority queue 
  2530                              <1> 	;    p.dska - disk address of the process		
  2531                              <1> 	;    u.emt - determines handling of emt's 	
  2532                              <1> 	;    u.ilgins - determines handling of illegal instructions		
  2533                              <1> 	; OUTPUTS ->
  2534                              <1> 	;    r3 - process number of last process on the queue upon
  2535                              <1> 	;	  entering putlu
  2536                              <1> 	;    p.link-1 + r3 - process number in r1
  2537                              <1> 	;    r2 - points to lowest priority queue
  2538                              <1> 	;
  2539                              <1> 	; ((Modified registers: EDX, EBX)) 
  2540                              <1> 	;
  2541                              <1> 	; / r1 = user process no.; r2 points to lowest priority queue
  2542                              <1> 
  2543                              <1> 	; eBX = r2
  2544                              <1> 	; eAX = r1 (AL=r1b)
  2545                              <1> 
  2546 00004FE1 BB[C8740000]        <1> 	mov	ebx, runq
  2547 00004FE6 0FB613              <1> 	movzx  	edx, byte [ebx]
  2548 00004FE9 43                  <1> 	inc	ebx
  2549 00004FEA 20D2                <1> 	and	dl, dl
  2550                              <1> 		; tstb (r2)+ / is queue empty?
  2551 00004FEC 740A                <1>        	jz	short putlu_1
  2552                              <1> 		; beq 1f / yes, branch
  2553 00004FEE 8A13                <1> 	mov 	dl, [ebx] ; 12/09/2015
  2554                              <1> 		; movb (r2),r3 / no, save the "last user" process number
  2555                              <1> 			     ; / in r3
  2556 00004FF0 8882[13710000]      <1>        	mov	[edx+p.link-1], al
  2557                              <1> 		; movb r1,p.link-1(r3) / put pointer to user on 
  2558                              <1> 			     ; / "last users" link
  2559 00004FF6 EB03                <1> 	jmp	short putlu_2
  2560                              <1> 		; br 2f /
  2561                              <1> putlu_1: ; 1:
  2562 00004FF8 8843FF              <1> 	mov	[ebx-1], al
  2563                              <1>        		; movb r1,-1(r2) / user is only user; 
  2564                              <1> 			    ; / put process no. at beginning and at end
  2565                              <1> putlu_2: ; 2: 
  2566 00004FFB 8803                <1> 	mov	[ebx], al
  2567                              <1>        		; movb r1,(r2) / user process in r1 is now the last entry
  2568                              <1> 			     ; / on the queue
  2569 00004FFD 88C2                <1> 	mov	dl, al
  2570 00004FFF 88B2[13710000]      <1>         mov     [edx+p.link-1], dh ; 0
  2571                              <1> 		; dec r2 / restore r2
  2572 00005005 C3                  <1>         retn
  2573                              <1> 		; rts r0
  2574                              <1> 
  2575                              <1> ;copyz:
  2576                              <1> ;       mov     r1,-(sp) / put r1 on stack
  2577                              <1> ;       mov     r2,-(sp) / put r2 on stack
  2578                              <1> ;       mov     (r0)+,r1
  2579                              <1> ;       mov     (r0)+,r2
  2580                              <1> ;1:
  2581                              <1> ;       clr     (r1)+ / clear all locations between r1 and r2
  2582                              <1> ;       cmp     r1,r2 
  2583                              <1> ;       blo     1b
  2584                              <1> ;       mov     (sp)+,r2 / restore r2
  2585                              <1> ;       mov     (sp)+,r1 / restore r1
  2586                              <1> ;       rts     r0 
  2587                              <1> 
  2588                              <1> idle:
  2589                              <1> 	; 01/09/2015
  2590                              <1> 	; 10/05/2015 (Retro UNIX 386 v1 - Beginning)
  2591                              <1> 	; 10/04/2013 - 23/10/2013 (Retro UNIX 8086 v1)
  2592                              <1> 	; (idle & wait loop)
  2593                              <1> 	; Retro Unix 8086 v1 modification on original UNIX v1
  2594                              <1> 	; idle procedure!
  2595                              <1>       	;
  2596                              <1>   	; 01/09/2015
  2597 00005006 FB                  <1> 	sti
  2598                              <1>       	; 29/07/2013
  2599 00005007 F4                  <1>       	hlt
  2600 00005008 90                  <1>       	nop ; 10/10/2013
  2601 00005009 90                  <1>       	nop
  2602 0000500A 90                  <1>       	nop
  2603                              <1>       	; 23/10/2013
  2604 0000500B 90                  <1>       	nop
  2605 0000500C 90                  <1>       	nop
  2606 0000500D 90                  <1>       	nop
  2607 0000500E 90                  <1>       	nop
  2608 0000500F C3                  <1>       	retn      
  2609                              <1> 
  2610                              <1> 	;mov *$ps,-(sp) / save ps on stack
  2611                              <1> 	;clr *$ps / clear ps
  2612                              <1> 	;mov clockp,-(sp) / save clockp on stack
  2613                              <1> 	;mov (r0)+,clockp / arg to idle in clockp
  2614                              <1> 	;1 / wait for interrupt
  2615                              <1> 	;mov (sp)+,clockp / restore clockp, ps
  2616                              <1> 	;mov (sp)+,*$ps
  2617                              <1> 	;rts r0
  2618                              <1> 
  2619                              <1> clear:
  2620                              <1> 	; 24/12/2021 (Retro UNIX 386 v1.1)
  2621                              <1> 	; 10/05/2015 (Retro UNIX 386 v1 - Beginning)
  2622                              <1> 	; 09/04/2013 - 03/08/2013 (Retro UNIX 8086 v1)
  2623                              <1> 	; 'clear' zero's out of a block (whose block number is in r1)
  2624                              <1> 	; on the current device (cdev)
  2625                              <1> 	;	
  2626                              <1> 	; INPUTS ->
  2627                              <1> 	;    r1 - block number of block to be zeroed
  2628                              <1> 	;    cdev - current device number 
  2629                              <1> 	; OUTPUTS ->
  2630                              <1> 	;    a zeroed I/O buffer onto the current device
  2631                              <1> 	;    r1 - points to last entry in the I/O buffer
  2632                              <1> 	;
  2633                              <1> 	; ((AX = R1)) input/output
  2634                              <1> 	;    (Retro UNIX Prototype : 18/11/2012 - 14/11/2012, UNIXCOPY.ASM)
  2635                              <1>         ;    ((Modified registers: EDX, ECX, EBX, ESI, EDI, EBP))  
  2636                              <1> 
  2637 00005010 E89C110000          <1> 	call 	wslot
  2638                              <1> 		; jsr r0,wslot / get an I/O buffer set bits 9 and 15 in first
  2639                              <1>                    ; / word of I/O queue r5 points to first data word in buffer
  2640 00005015 89DF                <1> 	mov	edi, ebx ; r5
  2641 00005017 89C2                <1> 	mov	edx, eax
  2642 00005019 B980000000          <1> 	mov	ecx, 128
  2643                              <1> 		; mov $256.,r3
  2644 0000501E 31C0                <1> 	xor	eax, eax
  2645 00005020 F3AB                <1> 	rep	stosd
  2646 00005022 89D0                <1> 	mov	eax, edx
  2647                              <1> ; 1: 
  2648                              <1>        		; clr (r5)+ / zero data word in buffer
  2649                              <1>        		; dec r3
  2650                              <1>        		; bgt 1b / branch until all data words in buffer are zero
  2651                              <1> 	;call	dskwr
  2652                              <1> 		; jsr r0,dskwr / write zeroed buffer area out onto physical
  2653                              <1>                              ; / block specified in r1
  2654                              <1> 	; eAX (r1) = block number
  2655                              <1> 	;retn
  2656                              <1> 		; rts r0
  2657                              <1> 	; 24/12/2021
  2658 00005024 E9A4110000          <1> 	jmp	dskwr
  2073                                  %include 'u4.s'      ; 15/04/2015
  2074                              <1> ; Temporary Runix kernel v2.0 file for debug - 22/11/2021
  2075                              <1> ; (re-write kernel for test by using previous version without a major defect)
  2076                              <1> ; ****************************************************************************
  2077                              <1> ; Retro UNIX 386 v1 Kernel (v0.2) - SYS4.INC
  2078                              <1> ; Last Modification: 05/12/2021
  2079                              <1> ; ----------------------------------------------------------------------------
  2080                              <1> ; Derived from 'Retro UNIX 8086 v1' source code by Erdogan Tan
  2081                              <1> ; (v0.1 - Beginning: 11/07/2012)
  2082                              <1> ;
  2083                              <1> ; Derived from UNIX Operating System (v1.0 for PDP-11) 
  2084                              <1> ; (Original) Source Code by Ken Thompson (1971-1972)
  2085                              <1> ; <Bell Laboratories (17/3/1972)>
  2086                              <1> ; <Preliminary Release of UNIX Implementation Document>
  2087                              <1> ;
  2088                              <1> ; Retro UNIX 8086 v1 - U4.ASM (04/07/2014) //// UNIX v1 -> u4.s
  2089                              <1> ;
  2090                              <1> ; ****************************************************************************
  2091                              <1> 
  2092                              <1> ;setisp:
  2093                              <1>        ;mov     r1,-(sp)
  2094                              <1>        ;mov     r2,-(sp)
  2095                              <1>        ;mov     r3,-(sp)
  2096                              <1>        ;mov     clockp,-(sp)
  2097                              <1>        ;mov     $s.syst+2,clockp
  2098                              <1>        ;jmp     (r0)
  2099                              <1> 
  2100                              <1> clock: ; / interrupt from 60 cycle clock
  2101                              <1> 	
  2102                              <1> 	; 14/10/2015
  2103                              <1> 	; 14/05/2015 (Retro UNIX 386 v1 - Beginning)
  2104                              <1> 	; 07/12/2013 - 10/04/2014 (Retro UNIX 8086 v1)
  2105                              <1> 
  2106                              <1>        ;mov     r0,-(sp) / save r0
  2107                              <1>        ;tst     *$lks / restart clock?
  2108                              <1>        ;mov     $s.time+2,r0 / increment the time of day
  2109                              <1>        ;inc     (r0)
  2110                              <1>        ;bne     1f
  2111                              <1>        ;inc     -(r0)
  2112                              <1> ;1:
  2113                              <1>        ;mov     clockp,r0 / increment appropriate time category
  2114                              <1>        ;inc     (r0)
  2115                              <1>        ;bne     1f
  2116                              <1>        ;inc     -(r0)
  2117                              <1> ;1:
  2118                              <1> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2119                              <1> 
  2120 00005029 803D[20750000]00    <1> 	cmp	byte [u.quant], 0
  2121 00005030 772C                <1> 	ja	short clk_1
  2122                              <1> 	;
  2123 00005032 803D[CE740000]FF    <1>         cmp     byte [sysflg], 0FFh ; user or system space ?
  2124 00005039 7529                <1> 	jne	short clk_2 ; system space (sysflg <> 0FFh)
  2125 0000503B 803D[29750000]01    <1> 	cmp     byte [u.uno], 1 ; /etc/init ?
  2126 00005042 761A                <1> 	jna	short clk_1 ; yes, do not swap out
  2127 00005044 66833D[24750000]00  <1> 	cmp	word [u.intr], 0
  2128 0000504C 7616                <1> 	jna	short clk_2
  2129                              <1> clk_0:
  2130                              <1> 	; 14/10/2015
  2131 0000504E FE05[CE740000]      <1> 	inc	byte [sysflg] ; Now, we are in system space
  2132 00005054 58                  <1> 	pop	eax ; return address to the timer interrupt
  2133                              <1> 	;
  2134 00005055 B020                <1> 	MOV	AL,EOI			; GET END OF INTERRUPT MASK
  2135                              <1> 	;CLI				; DISABLE INTERRUPTS TILL STACK CLEARED
  2136 00005057 E620                <1> 	OUT	INTA00,AL		; END OF INTERRUPT TO 8259 - 1	
  2137                              <1> 	;
  2138 00005059 E9D1EBFFFF          <1> 	jmp     sysrelease ; 'sys release' by clock/timer
  2139                              <1> clk_1:
  2140 0000505E FE0D[20750000]      <1> 	dec	byte [u.quant]
  2141                              <1> clk_2:
  2142 00005064 C3                  <1> 	retn   ; return to (hardware) timer interrupt routine
  2143                              <1> 
  2144                              <1> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
  2145                              <1> 
  2146                              <1>        ;mov     $uquant,r0 / decrement user time quantum
  2147                              <1>        ;decb    (r0)
  2148                              <1>        ;bge     1f / if less than 0
  2149                              <1>        ;clrb    (r0) / make it 0
  2150                              <1> ;1: / decrement time out counts return now if priority was not 0
  2151                              <1>        ;cmp     4(sp),$200 / ps greater than or equal to 200
  2152                              <1>        ;bge     2f / yes, check time outs
  2153                              <1>        ;tstb    (r0) / no, user timed out?
  2154                              <1>        ;bne     1f / no
  2155                              <1>        ;cmpb    sysflg,$-1 / yes, are we outside the system?
  2156                              <1>        ;bne     1f / no, 1f
  2157                              <1>        ;mov     (sp)+,r0 / yes, put users r0 in r0
  2158                              <1>        ;sys     0 / sysrele
  2159                              <1>        ;rti
  2160                              <1> ;2: / priority is high so just decrement time out counts
  2161                              <1>        ;mov     $toutt,r0 / r0 points to beginning of time out table
  2162                              <1> ;2:
  2163                              <1>        ;tstb    (r0) / is the time out?
  2164                              <1>        ;beq     3f / yes, 3f (get next entry)
  2165                              <1>        ;decb    (r0) / no, decrement the time
  2166                              <1>        ;bne     3f / isit zero now?
  2167                              <1>        ;incb    (r0) / yes, increment the time
  2168                              <1> ;3:
  2169                              <1>        ;inc     r0 / next entry
  2170                              <1>        ;cmp     r0,$touts / end of toutt table?
  2171                              <1>        ;blo     2b / no, check this entry
  2172                              <1>        ;mov     (sp)+,r0 / yes, restore r0
  2173                              <1>        ;rti / return from interrupt
  2174                              <1> ;1: / decrement time out counts; if 0 call subroutine
  2175                              <1>        ;mov     (sp)+,r0 / restore r0
  2176                              <1>        ;mov     $240,*$ps / set processor priority to 5
  2177                              <1>        ;jsr     r0,setisp / save registers
  2178                              <1>        ;mov     $touts-toutt-1,r0 / set up r0 as index to decrement thru
  2179                              <1>                                ;  / the table
  2180                              <1> ;1:
  2181                              <1>        ;tstb    toutt(r0) / is the time out for this entry
  2182                              <1>        ;beq     2f / yes
  2183                              <1>        ;decb    toutt(r0) / no, decrement the time
  2184                              <1>        ;bne     2f / is the time 0, now
  2185                              <1>        ;asl     r0 / yes, 2 x r0 to get word index for tout entry
  2186                              <1>        ;jsr     r0,*touts(r0) / go to appropriate routine specified in this
  2187                              <1>        ;asr     r0 / touts entry; set r0 back to toutt index
  2188                              <1> ;2:
  2189                              <1>        ;dec     r0 / set up r0 for next entry
  2190                              <1>        ;bge     1b / finished? , no, go back
  2191                              <1>        ;br      retisp / yes, restore registers and do a rti
  2192                              <1> 
  2193                              <1> ;retisp:
  2194                              <1>        ;mov     (sp)+,clockp / pop values before interrupt off the stack
  2195                              <1>        ;mov     (sp)+,r3
  2196                              <1>        ;mov     (sp)+,r2
  2197                              <1>        ;mov     (sp)+,r1
  2198                              <1>        ;mov     (sp)+,r0
  2199                              <1>        ;rti     / return from interrupt
  2200                              <1> 
  2201                              <1> 
  2202                              <1> wakeup: ; / wakeup processes waiting for an event 
  2203                              <1> 	; / by linking them to the queue
  2204                              <1> 	;
  2205                              <1> 	; 15/09/2015
  2206                              <1> 	; 29/06/2015
  2207                              <1> 	; 15/04/2015 (Retro UNIX 386 v1 - Beginning)
  2208                              <1> 	;
  2209                              <1> 	; 15/05/2013 - 02/06/2014
  2210                              <1> 	; Retro UNIX 8086 v1 modification !
  2211                              <1> 	; (Process/task switching routine by using
  2212                              <1> 	; Retro UNIX 8086 v1 keyboard interrupt output.)
  2213                              <1> 	;
  2214                              <1> 	; In original UNIX v1, 'wakeup' is called to wake the process
  2215                              <1> 	; sleeping in the specified wait channel by creating a link 
  2216                              <1> 	; to it from the last user process on the run queue.
  2217                              <1> 	; If there is no process to wake up, nothing happens.
  2218                              <1> 	;
  2219                              <1> 	; In Retro UNIX 8086 v1, Int 09h keyboard interrupt will set
  2220                              <1> 	; 'switching' status of the current process (owns current tty)
  2221                              <1> 	; (via alt + function keys) to a process which has highest
  2222                              <1> 	; priority (on run queue) on the requested tty (0 to 7, except
  2223                              <1> 	; 8 and 9 which are tty identifiers of COM1, COM2 serial ports)
  2224                              <1> 	; as it's console tty. (NOTE: 'p.ttyc' is used to set console
  2225                              <1> 	; tty for tty switching by keyboard.)	 
  2226                              <1> 	; 
  2227                              <1> 	; INPUT -> 
  2228                              <1> 	;	   AL = wait channel (r3) ('tty number' for now)
  2229                              <1> 	;	   ;;EBX = Run queue (r2) offset
  2230                              <1> 	;
  2231                              <1> 	; ((modified registers: EAX, EBX))
  2232                              <1> 	;
  2233 00005065 0FB6D8              <1> 	movzx	ebx, al ; 29/06/2015
  2234 00005068 81C3[18700000]      <1> 	add	ebx, wlist
  2235 0000506E 8A03                <1> 	mov	al, [ebx] ; waiting list (waiting process number)
  2236 00005070 20C0                <1> 	and	al, al
  2237 00005072 7424                <1> 	jz	short wa0 ; nothing to wakeup
  2238                              <1> 	;
  2239 00005074 30E4                <1> 	xor	ah, ah
  2240 00005076 8825[20750000]      <1> 	mov 	[u.quant], ah ; 0 ; time quantum = 0	
  2241 0000507C 8823                <1> 	mov	[ebx], ah ; 0 ; zero wait channel entry
  2242                              <1> 	; 15/09/2015
  2243 0000507E 0FB6D8              <1> 	movzx	ebx, al
  2244 00005081 88A3[03710000]      <1> 	mov	[ebx+p.waitc-1], ah ; 0
  2245 00005087 FEC4                <1> 	inc	ah
  2246 00005089 88A3[23710000]      <1> 	mov	byte [ebx+p.stat-1], ah ; 1 ; SRUN
  2247                              <1> 	;
  2248 0000508F 57                  <1> 	push	edi
  2249 00005090 52                  <1> 	push	edx
  2250 00005091 E84BFFFFFF          <1> 	call	putlu
  2251 00005096 5A                  <1> 	pop	edx
  2252 00005097 5F                  <1> 	pop	edi
  2253                              <1> wa0:
  2254 00005098 C3                  <1> 	retn
  2255                              <1> 
  2256                              <1> 	; 05/12/2021
  2257                              <1> 	; 30/11/2021 - Retro UNIX 386 v1.2
  2258                              <1> sleep: 
  2259                              <1> 	; 15/09/2015
  2260                              <1> 	; 30/06/2015 (Retro UNIX 386 v1 - Beginning)
  2261                              <1> 	;
  2262                              <1> 	; 09/05/2013 - 20/03/2014
  2263                              <1> 	;
  2264                              <1> 	; Retro UNIX 8086 v1 modification !
  2265                              <1> 	; (Process/task switching and quit routine by using
  2266                              <1> 	; Retro UNIX 8086 v1 keyboard interrupt output.))
  2267                              <1> 	;
  2268                              <1> 	; In original UNIX v1, 'sleep' is called to wait for
  2269                              <1> 	; tty and tape output or input becomes available
  2270                              <1> 	; and process is put on waiting channel and swapped out,
  2271                              <1> 	; then -when the tty or tape is ready to write or read-
  2272                              <1> 	; 'wakeup' gets process back to active swapped-in status.)
  2273                              <1> 	;
  2274                              <1> 	; In Retro UNIX 8086 v1, Int 1Bh ctrl+brk interrupt and
  2275                              <1> 	; Int 09h keyboard interrupt will set 'quit' or 'switching'		
  2276                              <1> 	; status of the current process also INT 1Ch will count down
  2277                              <1> 	; 'uquant' value and INT 09h will redirect scancode of keystroke
  2278                              <1> 	; to tty buffer of the current process and kernel will get
  2279                              <1> 	; user input by using tty buffer of the current process
  2280                              <1> 	; (instead of standard INT 16h interrupt).
  2281                              <1> 	; TTY output will be redirected to related video page of text mode
  2282                              <1> 	; (INT 10h will be called with different video page depending
  2283                              <1> 	; on tty assignment of the active process: 0 to 7 for
  2284                              <1> 	; pseudo screens.)
  2285                              <1> 	;
  2286                              <1> 	; In Retro UNIX 8086 v1, 'sleep' will be called to wait for
  2287                              <1> 	; a keystroke from keyboard or wait for reading or writing
  2288                              <1> 	; characters/data on serial port(s).
  2289                              <1> 	;
  2290                              <1> 	; Character/Terminal input/output through COM1 and COM2 will be
  2291                              <1> 	; performed by related routines in addition to pseudo TTY routines.
  2292                              <1> 	; 
  2293                              <1> 	; R1 = AH = wait channel (0-9 for TTYs) ; 05/10/2013 (22/09/2013)
  2294                              <1> 	;
  2295                              <1> 	;; 05/10/2013
  2296                              <1>         ;10/12/2013
  2297                              <1> 	;cmp   byte [u.uno], 1
  2298                              <1>         ;ja    short sleep0
  2299                              <1> 	;retn
  2300                              <1> 
  2301                              <1> 	; 20/03/2014
  2302                              <1> 	;mov	bx, [runq]
  2303                              <1> 	;cmp	bl, bh
  2304                              <1> 	;jne	short sleep0	
  2305                              <1> 	; 25/02/2014
  2306                              <1> 	;cmp	word ptr [runq], 0
  2307                              <1> 	;ja	short sleep0	
  2308                              <1> 	;retn
  2309                              <1> sleep0:
  2310                              <1> 	;
  2311 00005099 E851000000          <1> 	call	isintr
  2312                              <1> 	;jnz	sysret
  2313                              <1> 		; / wait for event
  2314                              <1>        		; jsr r0,isintr / check to see if interrupt 
  2315                              <1> 			      ; / or quit from user
  2316                              <1>                		; br 2f / something happened
  2317                              <1> 			      ; / yes, his interrupt so return
  2318                              <1>                      	      ;	/ to user
  2319                              <1> 	; 05/12/2021
  2320 0000509E 7405                <1> 	jz	short sleep_2
  2321                              <1> sleep_3:
  2322 000050A0 E925EBFFFF          <1> 	jmp	sysret
  2323                              <1> sleep_2:
  2324                              <1> 	; 30/06/2015
  2325 000050A5 0FB6DC              <1>     	movzx	ebx, ah ; 30/06/2015
  2326 000050A8 81C3[18700000]      <1> 	add	ebx, wlist
  2327 000050AE 8A03                <1> 	mov	al, [ebx]
  2328 000050B0 20C0                <1> 	and	al, al
  2329 000050B2 7407                <1> 	jz	short sleep1
  2330 000050B4 53                  <1> 	push	ebx
  2331 000050B5 E827FFFFFF          <1> 	call	putlu
  2332 000050BA 5B                  <1> 	pop	ebx
  2333                              <1> sleep1:
  2334 000050BB A0[29750000]        <1> 	mov	al, [u.uno]    
  2335 000050C0 8803                <1>   	mov	[ebx], al 	; put the process number
  2336                              <1> 				; in the wait channel
  2337                              <1> 		; mov (r0)+,r1 / put number of wait channel in r1
  2338                              <1> 		; movb wlist(r1),-(sp) / put old process number in there,
  2339                              <1> 				     ; / on the stack
  2340                              <1>        		; movb u.uno,wlist(r1) / put process number of process
  2341                              <1> 				     ; / to put to sleep in there
  2342                              <1> 	; 30/11/2021
  2343 000050C2 31DB                <1> 	xor	ebx, ebx
  2344 000050C4 8A1D[B9740000]      <1> 	mov	bl, [cdev]
  2345 000050CA 53                  <1> 	push	ebx
  2346                              <1>         ; 15/09/2015
  2347                              <1> 	;movzx	ebx, al
  2348 000050CB 88C3                <1> 	mov	bl, al
  2349 000050CD C683[23710000]04    <1>         mov     byte [ebx+p.stat-1], 4 ; SSLEEP
  2350 000050D4 FEC4                <1> 	inc	ah
  2351 000050D6 88A3[03710000]      <1> 	mov	[ebx+p.waitc-1], ah ; wait channel + 1
  2352                              <1> 	;
  2353                              <1> 	;push	word [cdev]
  2354                              <1> 	;	; mov cdev,-(sp) / nothing happened in isintr so
  2355 000050DC E833FEFFFF          <1> 	call	swap
  2356                              <1>        		; jsr r0,swap / swap out process that needs to sleep
  2357                              <1>         ;pop	word [cdev]
  2358                              <1> 	;	; mov (sp)+,cdev / restore device
  2359                              <1> 	; 30/11/2021
  2360 000050E1 58                  <1> 	pop	eax
  2361 000050E2 A2[B9740000]        <1> 	mov	[cdev], al
  2362 000050E7 E803000000          <1> 	call	isintr
  2363                              <1> 	; 22/09/2013
  2364                              <1> 	;jnz	sysret         
  2365                              <1> 		; jsr r0,isintr / check for interrupt of new process
  2366                              <1>                		; br 2f / yes, return to new user
  2367                              <1> 		; movb (sp)+,r1 / no, r1 = old process number that was 
  2368                              <1> 				; / originally on the wait channel
  2369                              <1>        		; beq 1f / if 0 branch
  2370                              <1>   		; mov $runq+4,r2 / r2 points to lowest priority queue
  2371                              <1>        		; mov $300,*$ps / processor priority = 6
  2372                              <1> 		; jsr r0,putlu / create link to old process number
  2373                              <1>        		; clr *$ps / clear the status; process priority = 0
  2374                              <1> 	; 05/12/2021
  2375 000050EC 75B2                <1> 	jnz	short sleep_3 ; jump to sysret
  2376                              <1>      ;1:
  2377 000050EE C3                  <1> 	retn
  2378                              <1> 		; rts r0 / return
  2379                              <1>      ;2:
  2380                              <1>         ;;jmp	sysret
  2381                              <1> 		; jmp sysret / return to user
  2382                              <1> 
  2383                              <1> isintr:
  2384                              <1> 	; 30/11/2021	
  2385                              <1> 	; 30/06/2015 (Retro UNIX 386 v1 - Beginning)
  2386                              <1> 	;
  2387                              <1> 	; 09/05/2013 - 30/05/2014
  2388                              <1> 	;
  2389                              <1> 	; Retro UNIX 8086 v1 modification !
  2390                              <1> 	; (Process/task switching and quit routine by using
  2391                              <1> 	; Retro UNIX 8086 v1 keyboard interrupt output.))
  2392                              <1> 	;
  2393                              <1> 	; Retro UNIX 8086 v1 modification:
  2394                              <1> 	; 'isintr' checks if user interrupt request is enabled
  2395                              <1> 	;  and there is a 'quit' request by user;
  2396                              <1> 	;  otherwise, 'isintr' will return with zf=1 that means
  2397                              <1> 	;  "nothing to do". (20/10/2013)
  2398                              <1> 	;
  2399                              <1> 	; 20/10/2013
  2400 000050EF 66833D[0E750000]00  <1> 	cmp 	word [u.ttyp], 0 ; has process got a tty ?
  2401 000050F7 761F                <1> 	jna	short isintr2 ; retn
  2402                              <1> 	; 03/09/2013
  2403                              <1> 	; (nothing to do)
  2404                              <1> 	;retn
  2405                              <1> 	; 22/09/2013
  2406 000050F9 66833D[24750000]00  <1> 	cmp	word [u.intr], 0
  2407 00005101 7615                <1> 	jna	short isintr2 ; retn
  2408                              <1> 	; 30/05/2014
  2409                              <1> 	;push	ax
  2410 00005103 50                  <1> 	push	eax ; 30/11/2021 
  2411 00005104 66A1[26750000]      <1> 	mov	ax, [u.quit]
  2412 0000510A 6609C0              <1> 	or	ax, ax ; 0 ?
  2413 0000510D 7408                <1> 	jz	short isintr1 ; zf = 1
  2414 0000510F 6683F8FE            <1> 	cmp	ax, 0FFFEh  ; 'ctrl + brk' check
  2415 00005113 7702                <1> 	ja	short isintr1 ; 0FFFFh, zf = 0
  2416                              <1> 	;xor	ax, ax ; zf = 1
  2417                              <1> 	; 30/11/2021
  2418 00005115 31C0                <1> 	xor	eax, eax
  2419                              <1> isintr1:
  2420                              <1> 	;pop	ax
  2421                              <1> 	; 30/11/2021
  2422 00005117 58                  <1> 	pop	eax
  2423                              <1> isintr2: ; 22/09/2013
  2424                              <1> 	; zf=1 -> nothing to do
  2425 00005118 C3                  <1> 	retn
  2426                              <1> 
  2427                              <1> 	; UNIX v1 original 'isintr' routine... 
  2428                              <1>        	;mov     r1,-(sp) / put number of wait channel on the stack
  2429                              <1>        	;mov     r2,-(sp) / save r2
  2430                              <1>        	;mov     u.ttyp,r1 / r1 = pointer to buffer of process control
  2431                              <1>         ;                 / typewriter
  2432                              <1>        	;beq     1f / if 0, do nothing except skip return
  2433                              <1>        	;movb    6(r1),r1 / put interrupt char in the tty buffer in r1
  2434                              <1>        	;beq     1f / if its 0 do nothing except skip return
  2435                              <1>        	;cmp     r1,$177 / is interrupt char = delete?
  2436                              <1>        	;bne     3f / no, so it must be a quit (fs)
  2437                              <1>        	;tst     u.intr / yes, value of u.intr determines handling
  2438                              <1>         ;              / of interrupts
  2439                              <1>        	;bne     2f / if not 0, 2f. If zero do nothing.
  2440                              <1>      ;1:
  2441                              <1>        	;tst     (r0)+ / bump r0 past system return (skip)
  2442                              <1>      ;4:
  2443                              <1>        	;mov     (sp)+,r2 / restore r1 and r2
  2444                              <1>        	;mov     (sp)+,r1
  2445                              <1>        	;rts     r0
  2446                              <1>      ;3: / interrupt char = quit (fs)
  2447                              <1>        	;tst     u.quit / value of u.quit determines handling of quits
  2448                              <1>        	;beq     1b / u.quit = 0 means do nothing
  2449                              <1>      ;2: / get here because either u.intr <> 0 or u.qult <> O
  2450                              <1>        	;mov     $tty+6,r1 / move pointer to tty block into r1
  2451                              <1>      ;1: / find process control tty entry in tty block
  2452                              <1>        	;cmp     (r1),u.ttyp / is this the process control tty buffer?
  2453                              <1>        	;beq     1f / block found go to 1f
  2454                              <1>        	;add     $8,r1 / look at next tty block
  2455                              <1>        	;cmp     r1,$tty+[ntty*8]+6 / are we at end of tty blocks
  2456                              <1>        	;blo     1b / no
  2457                              <1>        	;br      4b / no process control tty found so go to 4b
  2458                              <1>      ;1:
  2459                              <1>        	;mov     $240,*$ps / set processor priority to 5
  2460                              <1>        	;movb    -3(r1),0f / load getc call argument; character llst
  2461                              <1>         ;                  / identifier
  2462                              <1>        	;inc     0f / increment
  2463                              <1>      ;1:
  2464                              <1>        	;jsr     r0,getc; 0:.. / erase output char list for control
  2465                              <1>         ;        br 4b / process tty. This prevents a line of stuff
  2466                              <1>         ;             / being typed out after you hit the interrupt
  2467                              <1>         ;             / key
  2468                              <1>        	;br      1b
  2074                                  %include 'u5.s'      ; 03/06/2015
  2075                              <1> ; Temporary Runix kernel v2.0 file for debug - 22/11/2021
  2076                              <1> ; (re-write kernel for test by using previous version without a major defect)
  2077                              <1> ; ****************************************************************************
  2078                              <1> ; Retro UNIX 386 v1 Kernel (v0.2) - SYS5.INC
  2079                              <1> ; Last Modification: 12/01/2022
  2080                              <1> ; ----------------------------------------------------------------------------
  2081                              <1> ; Derived from 'Retro UNIX 8086 v1' source code by Erdogan Tan
  2082                              <1> ; (v0.1 - Beginning: 11/07/2012)
  2083                              <1> ;
  2084                              <1> ; Derived from UNIX Operating System (v1.0 for PDP-11) 
  2085                              <1> ; (Original) Source Code by Ken Thompson (1971-1972)
  2086                              <1> ; <Bell Laboratories (17/3/1972)>
  2087                              <1> ; <Preliminary Release of UNIX Implementation Document>
  2088                              <1> ;
  2089                              <1> ; Retro UNIX 8086 v1 - U5.AS M (07/08/2013) //// UNIX v1 -> u5.s
  2090                              <1> ;
  2091                              <1> ; ****************************************************************************
  2092                              <1> 
  2093                              <1> 	; 09/01/2022
  2094                              <1> 	; 22/11/2021 - Retro UNIX 386 v2 fs compatibility modification
  2095                              <1> mget_w:
  2096                              <1> 	; 28/10/2021
  2097                              <1> 	; 22/08/2021
  2098                              <1> 	; 20/08/2021
  2099                              <1> 	; 25/05/2020
  2100                              <1> 	; 24/05/2020 - Retro UNIX 386 v2	
  2101 00005119 C605[6E750000]01    <1> 	mov	byte [mget_rw], 1 ; write access
  2102 00005120 EB07                <1> 	jmp	short mget
  2103                              <1> mget_r:
  2104                              <1> 	; 28/10/2021
  2105                              <1> 	; 22/08/2021
  2106                              <1> 	; 25/05/2020
  2107                              <1> 	; 24/05/2020 - Retro UNIX 386 v2
  2108 00005122 C605[6E750000]00    <1> 	mov	byte [mget_rw], 0 ; read access	
  2109                              <1> mget:
  2110                              <1> 	; 09/01/2022 (Retro UNIX 386 v1.2)
  2111                              <1> 	; 28/11/2021
  2112                              <1> 	; 22/11/2021
  2113                              <1> 	; 28/10/2021 - temporary (simplified code)
  2114                              <1> 	; 22/08/2021
  2115                              <1> 	; 20/08/2021
  2116                              <1> 	; 25/05/2020
  2117                              <1> 	; 24/05/2020
  2118                              <1> 	; 05/05/2020
  2119                              <1> 	; 02/05/2020, 03/05/2020
  2120                              <1> 	; 29/04/2020 - Retro UNIX 386 v2
  2121                              <1> 	; New inode model/format (Modified UNIX v7 inode model)
  2122                              <1> 	;	
  2123                              <1> 	; 03/06/2015 (Retro UNIX 386 v1 - Beginning)
  2124                              <1> 	; 22/03/2013 - 31/07/2013 (Retro UNIX 8086 v1)
  2125                              <1> 	;
  2126                              <1> 	; Get existing or (allocate) a new disk block for file
  2127                              <1> 	; 
  2128                              <1> 	; INPUTS ->
  2129                              <1> 	;    u.fofp (file offset pointer)
  2130                              <1> 	;    inode 
  2131                              <1> 	;    u.off (file offset)
  2132                              <1> 	; OUTPUTS ->
  2133                              <1> 	;    r1 (physical block number)
  2134                              <1> 	;    r2, r3, r5 (internal)
  2135                              <1> 	;
  2136                              <1> 	; ((AX = R1)) output
  2137                              <1> 	;    (Retro UNIX Prototype : 05/03/2013 - 14/11/2012, UNIXCOPY.ASM)
  2138                              <1> 	;
  2139                              <1> 	; Modified registers: eax, edx, ebx, ecx, esi, edi, ebp
  2140                              <1> 
  2141                              <1> 	; Retro UNIX 386 v2 'mget' procedure
  2142                              <1> 	; source code reference:
  2143                              <1> 	; 'mget' procedure in UNIXHDCP.ASM (25/01/2020) by Erdogan Tan
  2144                              <1> 
  2145                              <1> 	; Get sector/block address for current file pointer
  2146                              <1> 	; (If file pointer points to a number greater than current file
  2147                              <1> 	; size, a new -empty- sector/block will be created/written.)
  2148                              <1> 
  2149                              <1> 	; Note: 'mget' procedure will be used for only RUNIX v2 FS.
  2150                              <1> 	;	(installable file system drivers will not use 'mget')
  2151                              <1> 	;	 !!! 512 bytes per sector !!!  ; 02/05/2020
  2152                              <1> 
  2153                              <1> 	; ! 64 bit fofp ! but Retro UNIX 386 v2 kernel will use 32 bit
  2154                              <1> 	; file offset for regular files and directories.
  2155                              <1> 	; ('mget' procedure will be called for regular files & directories)
  2156                              <1> 
  2157                              <1> 	; 28/10/2021
  2158                              <1> 	; Note: 'mget' uses only current inode ([ii], 'inode:')
  2159                              <1> 	; (inode must be loaded at 'inode:' addr by 'iget' before 'mget')  
  2160                              <1> 
  2161                              <1> 		; mov *u.fofp,mq / file offset in mq
  2162                              <1> 		; clr ac / later to be high sig
  2163                              <1> 		; mov $-8,lsh   / divide ac/mq by 256.
  2164                              <1> 		; mov mq,r2
  2165                              <1> 		; bit $10000,i.flgs / lg/sm is this a large or small file
  2166                              <1> 		; bne 4f / branch for large file
  2167                              <1> 
  2168                              <1> 	; 09/01/2022
  2169                              <1> 	; Return: eax = physical block/sector number
  2170                              <1> mget_0:	
  2171 00005129 8B35[EC740000]      <1>         mov     esi, [u.fofp]
  2172 0000512F 8B1E                <1> 	mov	ebx, [esi]
  2173                              <1>         ; ebx = file offset
  2174                              <1> 
  2175                              <1> 	; 20/08/2021
  2176 00005131 C1EB09              <1> 	shr	ebx, 9 ; / 512 (to convert byte offset to sector offset)
  2177                              <1> 
  2178 00005134 F605[75700000]10    <1> 	test	byte [i.flgs+1], 10h ; is this a large or small file addr?
  2179 0000513B 7405                <1> 	jz	short mget_1 ; small file addressing
  2180 0000513D E986000000          <1> 	jmp	mget_5	; large file addressing
  2181                              <1> mget_1:
  2182                              <1> 	; If large file flag is clear -not set- RUNIX v2 FS inode
  2183                              <1> 	; will contain 10 direct disk block/sector dword pointers.
  2184                              <1> 
  2185                              <1> 	; 05/05/2020
  2186                              <1> 	;shr	ebx, 9 ; / 512 (to convert byte offset to sector offset)
  2187                              <1> 
  2188 00005142 83FB0A              <1> 	cmp	ebx, 10	; block 10
  2189 00005145 733A                <1> 	jnb	short mget_3 ; file offset >= 5120
  2190                              <1> 
  2191                              <1> 	; 05/05/2020
  2192                              <1> 	;cmp	ebx, 5120
  2193                              <1> 	;jnb	short mget_3 ; file offset >= 5120
  2194                              <1> 
  2195                              <1> 	;mov	bl, bh
  2196                              <1> 	;and	bl, 1Eh ; clear all bits but bits 1,2,3,4 ; 0,2,..,18
  2197                              <1> 	;	; max. value = 12h = 10010b = 18 (because ebx <= 5119) 
  2198                              <1> 	;shl	bl, 1	; * 2 ; max. value = 24h = 100100b = 36
  2199                              <1> 	;xor	bh, bh  ; clear bh
  2200                              <1> 		; RUNIX v2 (RUFS 2) file system bytes per sector value
  2201                              <1> 		; is always 512.
  2202                              <1> 		; ebx = 4*(ebx/512)  = 0 to 36
  2203                              <1> 
  2204                              <1> 	; 05/05/2020
  2205 00005147 C0E302              <1> 	shl	bl, 2 ; * 4
  2206                              <1> 		 ; ebx = 0 to 36
  2207                              <1> 
  2208 0000514A 8B83[80700000]      <1> 	mov	eax, [ebx+i.dskp] ; disk sector addr ptr 0 to 9 for file
  2209                              <1> 	; 22/08/2021
  2210 00005150 89DE                <1> 	mov	esi, ebx
  2211                              <1> 	;xor	ebx, ebx ; 0
  2212                              <1> 	; 09/01/2022
  2213 00005152 30DB                <1> 	xor	bl, bl
  2214                              <1> 
  2215 00005154 09C0                <1> 	or	eax, eax
  2216 00005156 7512                <1> 	jnz	short mget_2 ; existing block/sector (logical)
  2217                              <1> 
  2218 00005158 56                  <1> 	push	esi ; * ; 22/08/2021
  2219 00005159 E877010000          <1> 	call	alloc_m ; 24/05/2020	
  2220                              <1> 	;call	alloc	; allocate a new sector/block for this file	
  2221                              <1> 			; eax = sector/block number
  2222                              <1> 			; ebx = buffer header address
  2223 0000515E 5E                  <1> 	pop	esi ; * ; 22/08/2021
  2224                              <1> 	; 28/11/2021
  2225                              <1> 	;jc	short mget_2 
  2226                              <1> 			; cf = 1 -> eax = 'no free block' error
  2227                              <1> 	; 22/08/2021
  2228 0000515F 8986[80700000]      <1> 	mov	[esi+i.dskp], eax  ; logical sector/block address
  2229                              <1> 
  2230                              <1> 	; 09/01/2022
  2231 00005165 E8BC040000          <1> 	call	setimod	; set inode modification flag
  2232                              <1> 			; and set modification time
  2233                              <1> 
  2234                              <1> 	; eax = logical sector/block number
  2235                              <1> 	; ([idev] = logical drive number)
  2236                              <1> 	; 28/10/2021
  2237                              <1> 	; [cdev] = logical drive number (0 or 1)
  2238                              <1> 	;	(0 = root fs, 1 = mounted fs)
  2239                              <1> mget_2:
  2240                              <1> 	; 09/01/2022
  2241                              <1> 	; eax = logical sector/block number
  2242 0000516A F605[B9740000]01    <1> 	test	byte [cdev], 1
  2243 00005171 7507                <1> 	jnz	short mget_14
  2244                              <1> 	; convert to physical sector/block number
  2245 00005173 0305[88750000]      <1> 	add	eax, [systm+SB.BootSectAddr]
  2246 00005179 C3                  <1> 	retn
  2247                              <1> 	; 09/01/2022
  2248                              <1> mget_14:
  2249                              <1> 	; convert to physical sector/block number
  2250 0000517A 0305[90770000]      <1> 	add	eax, [mount+SB.BootSectAddr]
  2251 00005180 C3                  <1> 	retn
  2252                              <1> 	
  2253                              <1> 		; rts r0
  2254                              <1> mget_3: ; 3: / adding on block which changes small file to a large file
  2255                              <1> 	; 22/11/2021
  2256 00005181 E84F010000          <1> 	call	alloc_m ; 24/05/2020
  2257                              <1> 	;call	alloc	; allocate a new sector/block for this file
  2258                              <1> 			; eax = sector/block number
  2259                              <1> 			; ebx = buffer header  ; 24/05/2020
  2260                              <1> 	; 28/11/2021
  2261                              <1> 	;jc	short mget_2 ; error code in eax
  2262                              <1> 
  2263                              <1> 	; 09/01/2022
  2264                              <1> 	; convert to physical block/sector number
  2265 00005186 50                  <1> 	push	eax ; *!* ; logical sector/block number 
  2266 00005187 E8DEFFFFFF          <1> 	call	mget_2
  2267                              <1> 
  2268                              <1>         ; EAX (r1) = Physical block (sector) number
  2269 0000518C E820100000          <1> 	call 	wslot
  2270                              <1> 		; jsr r0,wslot / set up I/O buffer for write, r5 points to 
  2271                              <1> 			     ; / first data word in buffer
  2272                              <1>         ; EAX (r1) = Physical block number
  2273                              <1> 	; 28/11/2021
  2274                              <1> 	; ebx = buffer (data) address
  2275                              <1> 	; 22/11/2021
  2276 00005191 31C9                <1> 	xor	ecx, ecx
  2277 00005193 B10A                <1> 	mov 	cl, 10	; r3, transfer old physical block pointers
  2278                              <1> 			; into new indirect block area for the new
  2279                              <1> 			; large file		
  2280 00005195 89DF                <1> 	mov 	edi, ebx ; r5
  2281 00005197 BE[80700000]        <1> 	mov	esi, i.dskp  ; current inode's direct disk addr ptrs
  2282 0000519C 89C2                <1> 	mov	edx, eax
  2283 0000519E 31C0                <1> 	xor	eax, eax
  2284                              <1> mget_4:
  2285 000051A0 A5                  <1> 	movsd 	
  2286 000051A1 8946FC              <1> 	mov	[esi-4], eax ; 0
  2287 000051A4 E2FA                <1> 	loop	mget_4
  2288                              <1> 
  2289                              <1> 	; 22/11/2021
  2290 000051A6 B176                <1> 	mov 	cl, 128-10 ; clear rest of data buffer
  2291                              <1> ;mget_4: ; 1
  2292 000051A8 F3AB                <1> 	rep 	stosd
  2293                              <1> 		; clr (r5)+
  2294                              <1> 		; dec r3
  2295                              <1> 		; bgt 1b
  2296 000051AA 89D0                <1> 	mov	eax, edx
  2297                              <1>         ; EAX (r1) = Physical block number
  2298 000051AC E81C100000          <1> 	call	dskwr
  2299                              <1> 		; jsr r0,dskwr / write new indirect block on disk
  2300                              <1> 
  2301                              <1> 	;; EAX (r1) = Physical block number
  2302                              <1> 
  2303                              <1> 	; 09/01/2022        
  2304 000051B1 8F05[80700000]      <1> 	pop	dword [i.dskp] ; *!* ; logical sector/block number
  2305                              <1>  
  2306                              <1> 	; eax = logical disk sector/block number/addr ; 09/01/2022
  2307                              <1> 	;mov 	[i.dskp], eax ; 22/11/2021 
  2308                              <1> 		; mov r1,i.dskp / put pointer to indirect block in i-node
  2309                              <1> 
  2310 000051B7 800D[75700000]10    <1> 	or	byte [i.flgs+1], 10000b ; 10h ; 16  ; large file flag
  2311                              <1> 		; bis $10000,i.flgs / set large file bit 
  2312                              <1> 				  ; / in i.flgs word of i-node
  2313 000051BE E863040000          <1> 	call	setimod
  2314                              <1> 		; jsr r0,setimod / set i-node modified flag
  2315                              <1> 
  2316 000051C3 E961FFFFFF          <1> 	jmp	mget_0 ; 09/01/2022
  2317                              <1> 		; br mget
  2318                              <1> 
  2319                              <1> mget_5:  ; 4 ; large file
  2320                              <1> 	; 13/11/2019 (UNIXHDCP.ASM)
  2321                              <1> 	;
  2322                              <1> 	; Retro UNIX 386 v2 disk inode contains..
  2323                              <1> 	; (if large file flag is set)
  2324                              <1> 	; 8 indirect disk block/sector dword pointers
  2325                              <1> 	; +1 double indirect disk block/sector dword pointers
  2326                              <1> 	; +1 triple indirect disk block/sector dword pointers
  2327                              <1> 
  2328                              <1> 	; check indirect pointers limit (as file offset)
  2329                              <1> 	; 8*128 = 1024 blocks (or sectors) or 512 KB
  2330                              <1> 	; check dx (file offset hw) value
  2331                              <1> 	
  2332                              <1> 	; 03/05/2020
  2333                              <1> 	;cmp	ebx, 524288  ; is file offset >= 524288 ?
  2334                              <1> 	;jnb	short mget_6 ; yes, check double indirect limit
  2335                              <1> 
  2336                              <1> 	; 05/05/2020
  2337 000051C8 81FB00040000        <1> 	cmp	ebx, 1024    ; block 1024 (byte 524288)	
  2338 000051CE 7321                <1> 	jnb	short mget_6 ; check double indirect limit
  2339                              <1> 
  2340 000051D0 C605[70750000]01    <1> 	mov	byte [level], 1 ; levels, 1 = indirect blocks
  2341                              <1> 
  2342                              <1> 	; 05/05/2020
  2343                              <1> 	;shr	ebx, 9	; ebx = sector offset (flat)
  2344                              <1> 
  2345 000051D7 88D9                <1> 	mov	cl, bl
  2346 000051D9 80E17F              <1> 	and	cl, 127
  2347 000051DC 880D[71750000]      <1> 	mov	[level+1], cl ; level 1, direct block ptr index (0 to 127)
  2348 000051E2 66C1EB07            <1> 	shr	bx, 7 ; bl = level 0, indirect block pointer index (0 to 7)
  2349                              <1> 	;mov	[level+2], bl
  2350                              <1> 
  2351 000051E6 C0E302              <1> 	shl	bl, 2 ; * 4 to convert index number to offset
  2352                              <1> 
  2353                              <1> 	;mov	esi, i.dskp
  2354                              <1> 	;add	esi, ebx
  2355                              <1> 
  2356 000051E9 8DB3[80700000]      <1> 	lea	esi, [ebx+i.dskp]
  2357                              <1> 
  2358 000051EF EB66                <1> 	jmp	short mget_8
  2359                              <1> 
  2360                              <1> mget_6:
  2361                              <1> 	; 22/11/2021 - Retro UNIX 386 v2 compatibility modification
  2362                              <1> 	; 03/05/2020
  2363                              <1> 	; 13/11/2019 (UNIXHDCP.ASM)
  2364                              <1> 	; check double indirect pointer limit (as file offset)
  2365                              <1> 	; (128*128)+1024 = 16384+1024 blocks or 8 MB + 512 KB
  2366                              <1> 	; check dx (file offset hw) value
  2367                              <1> 	
  2368                              <1> 	;cmp	ebx, 8912896 ; >= 17408 sectors (16384+1024) ?	
  2369                              <1> 	;jnb	short mget_7 ; yes, use triple indirect pointer (block)
  2370                              <1> 
  2371                              <1> 	; 05/05/2020
  2372 000051F1 81FB00440000        <1> 	cmp	ebx, 17408   ; block 17408 (byte 8912896)	
  2373 000051F7 7329                <1> 	jnb	short mget_7 ; use triple indirect pointer (block)
  2374                              <1> 
  2375 000051F9 C605[70750000]02    <1> 	mov	byte [level], 2  ; levels, 2 = double indirect blocks
  2376                              <1> 	
  2377                              <1> 	;sub	ebx, 524288 ; 1024 sectors
  2378                              <1> 	;shr	ebx, 9	; ebx = sector offset (flat)
  2379                              <1> 			; (from sector 1024)
  2380                              <1> 	; 05/05/2020
  2381 00005200 81EB00040000        <1> 	sub	ebx, 1024 ; offset from sector 1024
  2382                              <1> 	
  2383 00005206 88D9                <1> 	mov	cl, bl
  2384 00005208 80E17F              <1> 	and	cl, 127
  2385 0000520B 880D[71750000]      <1> 	mov	[level+1], cl
  2386                              <1> 			; level 2, direct block ptr index (0 to 127)	
  2387 00005211 66C1EB07            <1> 	shr	bx, 7
  2388 00005215 881D[72750000]      <1> 	mov	[level+2], bl
  2389                              <1> 			; level 1, indirect block ptr index (0 to 127)
  2390                              <1> 
  2391 0000521B BE[A0700000]        <1> 	mov	esi, i.dskp + 32 ; 8*4
  2392                              <1> 			; level 0, double indirect block pointer
  2393 00005220 EB35                <1> 	jmp	short mget_8
  2394                              <1> 
  2395                              <1> mget_7:
  2396                              <1> 	; 22/11/2021 - Retro UNIX 386 v2 compatibility modification
  2397                              <1> 	; 03/05/2020
  2398                              <1> 	; 13/11/2019 (UNIXHDCP.ASM)
  2399                              <1> 	; triple indirect pointer ; 8912896 to 1082654720 bytes
  2400                              <1> 	;mov	esi, i.dskp + 36 ; 9*4
  2401                              <1> 
  2402 00005222 C605[70750000]03    <1> 	mov	byte [level], 3  ; levels, 3 = triple indirect blocks
  2403                              <1> 
  2404                              <1> 	;sub	ebx, 8912896 ; 17408 sectors (16384+1024)
  2405                              <1> 	;shr	ebx, 9	; ebx = sector offset (flat)
  2406                              <1> 			; (from sector 17408)
  2407                              <1> 	; 05/05/2020
  2408 00005229 81EB00440000        <1> 	sub	ebx, 17408 ; offset from sector 17408	
  2409                              <1> 
  2410 0000522F 88D9                <1> 	mov	cl, bl
  2411 00005231 80E17F              <1> 	and	cl, 127
  2412 00005234 880D[71750000]      <1> 	mov	[level+1], cl  
  2413                              <1> 			; level 3, direct block ptr index (0 to 127)	
  2414 0000523A C1EB07              <1> 	shr	ebx, 7	
  2415 0000523D 88D9                <1> 	mov	cl, bl
  2416 0000523F 80E17F              <1> 	and	cl, 127
  2417 00005242 880D[72750000]      <1> 	mov	[level+2], cl
  2418                              <1> 			; level 2, indirect block ptr index (0 to 127)
  2419 00005248 66C1EB07            <1> 	shr	bx, 7
  2420 0000524C 881D[73750000]      <1> 	mov	[level+3], bl
  2421                              <1> 			; level 1, double indir blk ptr index (0 to 127)
  2422                              <1> 
  2423 00005252 BE[A4700000]        <1> 	mov	esi, i.dskp + 36 ; 9*4
  2424                              <1> 			; level 0, triple indirect block pointer
  2425                              <1> mget_8:
  2426                              <1> 	; 22/11/2021 - Retro UNIX 386 v2 compatibility modification
  2427 00005257 8B06                <1> 	mov	eax, [esi]
  2428 00005259 09C0                <1> 	or 	eax, eax ; R1
  2429 0000525B 751A                <1> 	jnz 	short mget_10 ; 2f
  2430                              <1> 		; bne 2f / if no indirect block exists
  2431 0000525D 56                  <1> 	push	esi	; * ; 24/05/2020
  2432                              <1> 
  2433 0000525E E872000000          <1> 	call	alloc_m ; 24/05/2020
  2434                              <1> 	;call	alloc	; allocate a new block for this file	
  2435                              <1> 			; eax = block number
  2436                              <1> 			; ebx = buffer header address
  2437                              <1> 			; [pdn] = physical drive number ; 25/05/2020
  2438                              <1> 	;jc	mget_2	; cf -> 1 & eax = 0 -> no free block
  2439                              <1> 
  2440 00005263 5E                  <1> 	pop	esi	; * ; 24/05/2020
  2441                              <1> 	;jc	short mget_9
  2442                              <1> 
  2443                              <1> 	; 09/01/2022
  2444                              <1> 	; eax = logical sector/block number
  2445                              <1> 
  2446 00005264 8906                <1> 	mov	[esi], eax ; record sector/block address on i.dskp area
  2447                              <1> 
  2448 00005266 E8BB030000          <1> 	call 	setimod
  2449                              <1> 		; jsr r0,setimod / set i-node modified byte
  2450                              <1> 	; EAX = new block number (logical)
  2451                              <1> 
  2452                              <1> 	; 09/01/2022
  2453                              <1> 	; convert to physical block/sector number
  2454 0000526B E8FAFEFFFF          <1> 	call	mget_2
  2455                              <1> 	;
  2456 00005270 E89BFDFFFF          <1> 	call 	clear
  2457                              <1> 		; jsr r0,clear / clear new block
  2458                              <1> 	; 22/11/2021
  2459 00005275 EB0A                <1> 	jmp	short mget_11
  2460                              <1> ;mget_9:
  2461                              <1> ;	pop	edx ; *  ; 09/01/2022
  2462                              <1> ;	retn
  2463                              <1> mget_10: ;2
  2464                              <1> 	; 09/01/2022
  2465                              <1> 	; eax = logical sector/block number/address
  2466                              <1> 	; convert to physical sector/block number/address
  2467 00005277 E8EEFEFFFF          <1> 	call	mget_2	
  2468                              <1> 	; 05/03/2013
  2469                              <1> 	; EAX = r1, physical block number (of indirect block)
  2470 0000527C E8D00E0000          <1> 	call 	dskrd ; read indirect block
  2471                              <1> 		; jsr r0,dskrd / read in indirect block
  2472                              <1> 	;jc	short mget_9
  2473                              <1> mget_11:
  2474                              <1> 	; 22/11/2021
  2475                              <1> 	; eax = physical block/sector address
  2476                              <1> 	; ebx = buffer address
  2477                              <1> 
  2478 00005281 89C2                <1> 	mov	edx, eax ; *  ; save physical sector number in edx
  2479                              <1> 
  2480 00005283 0FB605[70750000]    <1> 	movzx	eax, byte [level]
  2481 0000528A 8A80[70750000]      <1> 	mov	al, [eax+level] ; get sector pointer offset
  2482 00005290 C1E002              <1> 	shl	eax, 2 ; * 4 ; 09/01/2022
  2483                              <1> 	;shl	ax, 2 ; * 4
  2484                              <1> 
  2485 00005293 01C3                <1> 	add	ebx, eax
  2486                              <1> 
  2487 00005295 8B03                <1> 	mov 	eax, [ebx] ; put logical block no of block
  2488                              <1> 			   ; in file sought in R1 (EAX)
  2489                              <1> 		; mov (r2),r1 / put physical block no of block in file
  2490                              <1> 	               	    ; / sought in r1
  2491                              <1> 
  2492 00005297 21C0                <1> 	and	eax, eax  ; if logical sector/block number is zero
  2493                              <1> 			  ; then we need a new block for file
  2494 00005299 740F                <1> 	jz	short mget_12
  2495                              <1> 
  2496 0000529B FE0D[70750000]      <1> 	dec	byte [level]
  2497 000052A1 75D4                <1> 	jnz	short mget_10
  2498                              <1> 
  2499 000052A3 29DB                <1> 	sub	ebx, ebx ; ebx = 0 ; existing sector
  2500                              <1> 
  2501                              <1> 	;retn
  2502                              <1> 	; 09/01/2022
  2503                              <1> 	; eax = logical block/sector number
  2504 000052A5 E9C0FEFFFF          <1> 	jmp	mget_2
  2505                              <1> 
  2506                              <1> mget_12:
  2507                              <1> 	; 22/11/2021
  2508 000052AA 52                  <1> 	push	edx ; *
  2509 000052AB 53                  <1> 	push	ebx ; ** ; buffer data position
  2510 000052AC E824000000          <1> 	call	alloc_m
  2511                              <1> 	;call	alloc	 ; allocate a new block for this file	
  2512                              <1> 			 ; eax = block number (logical)
  2513                              <1> 			 ; ebx = buffer header address
  2514 000052B1 5E                  <1> 	pop	esi ; **
  2515                              <1> 	; 09/01/2022
  2516                              <1> 	;pop	edx ; *
  2517                              <1> 	;jc	short mget_9 ; cf -> 1 & eax = 0 -> no free block
  2518                              <1> 
  2519 000052B2 8906                <1> 	mov	[esi], eax ; record/save block number (logical)
  2520                              <1> 
  2521                              <1> 	; 22/11/2021
  2522 000052B4 870424              <1> 	xchg	eax, [esp] ; *
  2523                              <1> 
  2524                              <1> 	; eax (r1) = physical block number (of indirect block)
  2525 000052B7 E8F50E0000          <1> 	call 	wslot
  2526                              <1> 		; jsr r0,wslot
  2527                              <1>         ; eax (r1) = physical block number
  2528                              <1> 	; ebx (r5) = pointer to buffer (indirect block)
  2529 000052BC E80C0F0000          <1> 	call 	dskwr
  2530                              <1> 	; eax = r1 = physical block number (of indirect block)
  2531                              <1> 		; jsr r0,dskwr / write newly modified indirect block 
  2532                              <1> 			     ; / back out on disk
  2533 000052C1 58                  <1> 	pop	eax ; *  ; 31/07/2013
  2534                              <1> 		; mov (sp),r1 / restore block number of new block	
  2535                              <1> 
  2536                              <1> 	; 09/01/2022
  2537                              <1> 	; eax = logical sector/block number/address of new block
  2538                              <1> 	; convert to physical sector/block number/address
  2539 000052C2 E8A3FEFFFF          <1> 	call	mget_2	
  2540                              <1> 
  2541 000052C7 E844FDFFFF          <1> 	call 	clear
  2542                              <1> 		; jsr r0,clear / clear new block
  2543                              <1> 
  2544 000052CC FE0D[70750000]      <1> 	dec	byte [level]
  2545 000052D2 75AD                <1> 	jnz	short mget_11
  2546                              <1> 
  2547                              <1> 	; 09/01/2022
  2548                              <1> 	; eax = physical sector/block number/address of new block
  2549                              <1> 	
  2550                              <1> 	; 22/11/2021
  2551                              <1> 	; ebx = buffer address
  2552                              <1> mget_13: ; 2
  2553                              <1> 	; eax (r1) = Block number of new block
  2554 000052D4 C3                  <1> 	retn
  2555                              <1> 		; rts r0
  2556                              <1> 
  2557                              <1> 	; 09/01/2022
  2558                              <1> 	; 26/11/2021
  2559                              <1> 	; 22/11/2021 - Retro UNIX 386 v2 fs compatibility modification
  2560                              <1> alloc_m:
  2561                              <1> 	; 27/11/2021
  2562                              <1> 	; 28/10/2021
  2563                              <1> 	; 07/05/2021
  2564                              <1> 	; 26/05/2020
  2565                              <1> 	; 25/05/2020
  2566                              <1> 	; 24/05/2020 - Retro UNIX 386 v2
  2567                              <1> 	; 'alloc' call in 'mget'
  2568                              <1> 	;
  2569                              <1> 	; (('mget_r' will be returned with error))
  2570                              <1> 	; (('mget_w' will continue to 'alloc'))
  2571                              <1> 
  2572                              <1> 	; Note: 'sysread' will return with cf = 0, eax = 0
  2573                              <1> 	; if [fofp] >= file size (eax = 0 --> EOF)
  2574                              <1> 	; So, if we are here, that means [fofp] < file size
  2575                              <1> 	;     but disk address is 0. 
  2576                              <1> 	
  2577 000052D5 803D[6E750000]01    <1> 	cmp	byte [mget_rw], 1 ; write access ? (mget_w) 
  2578 000052DC 730F                <1> 	jnb	short alloc ; yes (this is a call from 'mget_w')
  2579                              <1> 	; ((cf = 1))	
  2580                              <1> 	; no (this is a call from 'mget_r') !
  2581                              <1> 	;mov	eax, ERR_FILE_SIZE ; file size error (inode error!)
  2582                              <1> 		; [User may change this empty/invalid inode (disk) sector
  2583                              <1> 		; pointer with a new valid inode (disk) sector pointer 
  2584                              <1> 		; by writing new clear (zero) sector to same file position
  2585                              <1> 		; in order to correct file size -inode parm/content- error.
  2586                              <1> 		; It may be useful for recovering lost data if other sector
  2587                              <1> 		; pointers are not empty.]	
  2588                              <1> 	;retn
  2589                              <1> 	; 27/11/2021
  2590 000052DE C705[4C750000]1400- <1> 	mov	dword [u.error], ERR_FILE_SIZE 
  2590 000052E6 0000                <1>
  2591                              <1> 			; file size error (inode error!)
  2592 000052E8 E9BDE8FFFF          <1> 	jmp	error
  2593                              <1> alloc:
  2594                              <1> 	; 09/01/2022 (Retro UNIX 386 v1.2)
  2595                              <1> 	; 27/11/2021
  2596                              <1> 	; 26/11/2021 - Major modification for Runix v2 file system
  2597                              <1> 	;	(I have called this kernel version as v1.2, it is a
  2598                              <1> 	;	 debug/test version just before Retro UNIX 386 v2 kernel)
  2599                              <1> 	;	((there were running problems on new version, 
  2600                              <1> 	;	  so i am developing an intermediate version with minimum
  2601                              <1> 	;	  modification on v1.1 code which is successfuly running))
  2602                              <1> 	;	 ; /// Erdogan Tan - Istanbul, 26/11/2021 /// 
  2603                              <1> 	;	 
  2604                              <1> 	; 03/06/2015 (Retro UNIX 386 v1 - Beginning)
  2605                              <1> 	; 01/04/2013 - 01/08/2013 (Retro UNIX 8086 v1)
  2606                              <1> 	;
  2607                              <1> 	; get a free block and 
  2608                              <1> 	; set the corresponding bit in the free storage map
  2609                              <1> 	; 
  2610                              <1> 	; INPUTS ->
  2611                              <1> 	;    cdev (current device)
  2612                              <1> 	;    r2 
  2613                              <1> 	;    r3
  2614                              <1> 	; OUTPUTS ->
  2615                              <1> 	;    r1 (physical block number of block assigned)
  2616                              <1> 	;    smod, mmod, systm (super block), mount (mountable super block)	
  2617                              <1> 	;
  2618                              <1> 	; ((AX = R1)) output
  2619                              <1> 	;    (Retro UNIX Prototype : 14/11/2012 - 21/07/2012, UNIXCOPY.ASM)
  2620                              <1>         ;    ((Modified registers: edx, ecx)) 
  2621                              <1> 
  2622                              <1> 	; 26/11/2021
  2623                              <1> 	; INPUT:
  2624                              <1> 	;	[cdev] = current device (root = 0, mounted = 1) 
  2625                              <1> 	; OUTPUT:
  2626                              <1> 	;	eax = disk block/sector number/address (physical)
  2627                              <1> 	;	(ebx = disk buffer address for sector/block in eax)
  2628                              <1> 	;
  2629                              <1> 	;    Note:	
  2630                              <1> 	;	Free blocks map will be modified and written to it's disk.
  2631                              <1> 	;	Superblock (buffer) of [cdev] will be modified and it's 
  2632                              <1> 	;	modification flag will be set; but the superblock 
  2633                              <1> 	;	will not be written to it's disk in this 'alloc' procedure)
  2634                              <1> 	;
  2635                              <1> 	; Modified registers: eax, ebx, ecx, edx, esi, edi, ebp
  2636                              <1> 
  2637                              <1> 	; 09/01/2022
  2638                              <1> 	; Return: eax = logical block/sector number 
  2639                              <1> 
  2640                              <1> 	; 26/11/2021
  2641 000052ED BB[84750000]        <1> 	mov 	ebx, systm ; (SuperBlock of root file system)
  2642                              <1> 		; mov $systm,r2 / start of inode and free storage map for drum
  2643 000052F2 F605[B9740000]01    <1> 	test	byte [cdev], 1
  2644                              <1> 		; tst cdev
  2645 000052F9 7405                <1> 	jz	short alloc_1
  2646                              <1> 		; beq 1f / drum is device
  2647 000052FB BB[8C770000]        <1> 	mov	ebx, mount ; (SuperBlock of mounted file system)
  2648                              <1> 		; mov $mount,r2 / disk or tape is device, start of inode and
  2649                              <1> 			      ; / free storage map
  2650                              <1> alloc_1: ; 1
  2651                              <1> 	; 26/11/2021 - Retro UNIX 386 v2 file system compatibility code
  2652                              <1> 	;test	byte [ebx+SB.ReadOnly], 1 ; if bit 0 is 1, it is RO fs	
  2653                              <1> 	;jnz	short alloc_2  ; 'read only file system' error
  2654                              <1> 	;
  2655 00005300 8B4B38              <1> 	mov	ecx, [ebx+SB.FreeBlocks] ; count of free blocks/sectors
  2656 00005303 21C9                <1> 	and	ecx, ecx
  2657 00005305 750F                <1> 	jnz	short alloc_3
  2658                              <1> alloc_2:
  2659                              <1> 	; 'no free blocks on disk !' error
  2660 00005307 C705[4C750000]2100- <1> 	mov	dword [u.error], ERR_ALLOC
  2660 0000530F 0000                <1>
  2661 00005311 E994E8FFFF          <1> 	jmp	error
  2662                              <1> alloc_3:
  2663                              <1> 	; 26/11/2021 - Retro UNIX 386 v2 file system compatibility code
  2664 00005316 31C0                <1> 	xor	eax, eax
  2665                              <1> 	; 23/07/2021
  2666 00005318 8B4B3C              <1> 	mov	ecx, [ebx+SB.FirstFreeBlk] ; 1st free sector number
  2667                              <1> 					 ; (as logical sector number)
  2668 0000531B 09C9                <1> 	or	ecx, ecx
  2669 0000531D 7415                <1> 	jz	short alloc_5 ; 0 = initial/reset value or 'not valid'
  2670 0000531F 41                  <1> 	inc	ecx ; 0FFFFFFFFh -> 0
  2671 00005320 7412                <1> 	jz	short alloc_5 ; 'not valid' or 'not calculated'
  2672 00005322 49                  <1> 	dec	ecx ; restore first free block value
  2673 00005323 89C8                <1> 	mov	eax, ecx
  2674                              <1> 	; 1 allocation byte (in fbm) is for 8 sectors
  2675 00005325 C1E903              <1> 	shr	ecx, 3	; first free block number / 8
  2676 00005328 80E1FC              <1> 	and	cl, ~3	; dword alignment (to backward)
  2677                              <1> 	; 27/11/2021
  2678 0000532B 890D[70750000]      <1> 	mov	[free_map_offset], ecx ; byte position on fbm
  2679                              <1> alloc_4:
  2680 00005331 C1E80C              <1> 	shr	eax, 12 ; 1 fbm sector for 4096 sectors
  2681                              <1> alloc_5:
  2682                              <1> 	; 26/11/2021
  2683 00005334 53                  <1> 	push	ebx ; ** ; superblock buffer address 
  2684                              <1> 	; eax = fbm sector index
  2685 00005335 A3[74750000]        <1> 	mov	[free_map_index], eax
  2686 0000533A 034318              <1> 	add	eax, [ebx+SB.FreeMapAddr] 
  2687                              <1> 			; free blocks map start sector addr
  2688                              <1> 	; 09/01/2022
  2689 0000533D 034304              <1> 	add	eax, [ebx+SB.BootSectAddr] 
  2690                              <1> 			; + Hidden Sectors
  2691                              <1> 	;
  2692                              <1> 	; eax = physical sector number
  2693 00005340 E80C0E0000          <1> 	call	dskrd
  2694                              <1> 	; cpu returns here if there is/was not an error in 'dskrd'
  2695                              <1> 	; eax = physical sector number
  2696                              <1> 	; ebx = buffer data address
  2697                              <1> 	; 27/11/2021
  2698 00005345 8B15[70750000]      <1> 	mov	edx, [free_map_offset] ; byte position on fbm
  2699 0000534B A3[78750000]        <1> 	mov	[free_map_sector], eax ; physical sector number
  2700 00005350 B9FF010000          <1> 	mov	ecx, 511
  2701                              <1> 	;and	edx, 511
  2702 00005355 21CA                <1> 	and 	edx, ecx ; fbm byte offset in fbm buffer
  2703 00005357 41                  <1> 	inc	ecx ; 512
  2704 00005358 01D9                <1> 	add	ecx, ebx
  2705 0000535A 01DA                <1> 	add	edx, ebx ; + buffer address
  2706 0000535C 5B                  <1> 	pop	ebx ; ** ; superblock buffer address 
  2707                              <1> 	;
  2708                              <1> 	; ebx = superblock buffer address
  2709                              <1> 	; edx = superblock buffer address + byte offset (dword aligned)
  2710                              <1> 	; ecx = superblock buffer address + 512
  2711                              <1> alloc_6:
  2712                              <1> 	; 26/11/2021 - Retro UNIX 386 v2 file system compatibility code
  2713                              <1> 	;		for Retro UNIX 386 v1.2
  2714                              <1> 	; 25/05/2020 - Retro UNIX 386 v2 code
  2715                              <1> 	; (dword scan) ((1 dword = 32 sectors, 1 byte = 8 sectors))
  2716                              <1> 	; ((Note: Logical drive size must be multiplies of 32 sectors))
  2717                              <1> 	; 19/05/2020
  2718                              <1> 	; edx = free blocks map byte address 
  2719 0000535D 0FBC02              <1> 	bsf	eax, [edx] ; Scans source operand for first bit set (1).
  2720                              <1> 			  ; Clear ZF if a bit is found set (1) and 
  2721                              <1> 			  ; loads the destination with an index to
  2722                              <1> 			  ; first set bit. (0 -> 31) 
  2723                              <1> 			  ; Sets ZF to 1 if no bits are found set.
  2724 00005360 752A                <1> 	jnz	short alloc_8 ; ZF = 0 -> a free block has been found
  2725                              <1> 
  2726 00005362 A1[70750000]        <1> 	mov	eax, [free_map_offset] ; byte offset from start of fbm
  2727 00005367 83C004              <1> 	add	eax, 4 ; next dword
  2728 0000536A 3B431C              <1> 	cmp	eax, [ebx+SB.FreeMapSize] ; fbm size in bytes
  2729 0000536D 7209                <1> 	jb	short alloc_7
  2730                              <1> 	; invalidate superblock's first free block field
  2731 0000536F C7433CFFFFFFFF      <1> 	mov	dword [ebx+SB.FirstFreeBlk], 0FFFFFFFFh
  2732                              <1> 	; 'no free blocks on disk !' error
  2733 00005376 EB8F                <1> 	jmp	alloc_2
  2734                              <1> alloc_7:
  2735                              <1> 		; 26/05/2020
  2736                              <1> 		; NOTE: If ldrv size is not multiplies of 32 sectors,
  2737                              <1> 		; mod (ldrv size / 32) sectors (at the end of ldrv)
  2738                              <1> 		; can not be allocated (for regular files or dirs)! 
  2739                              <1> 		; (Kernel or runix fs installation program can use
  2740                              <1> 		;  this fact to save/duplicate critical sectors/data 
  2741                              <1> 		;  onto end sectors of that logical drive's runix fs.)
  2742                              <1> 
  2743                              <1> 	; 26/11/2021
  2744 00005378 A3[70750000]        <1> 	mov	[free_map_offset], eax
  2745                              <1> 	; 28/07/2021
  2746                              <1> 	; set next first free block value for search
  2747 0000537D C1E003              <1> 	shl	eax, 3 ; * 8 ; first free block
  2748                              <1>  	; 26/11/2021
  2749 00005380 89433C              <1> 	mov	[ebx+SB.FirstFreeBlk], eax
  2750                              <1> 	; search (scan) for next free block map dword (in buffer)
  2751 00005383 83C204              <1> 	add	edx, 4 ; next dword
  2752 00005386 39CA                <1> 	cmp	edx, ecx
  2753 00005388 72D3                <1> 	jb	short alloc_6
  2754                              <1> 	; 27/11/2021
  2755 0000538A EBA5                <1> 	jmp	short alloc_4
  2756                              <1> 
  2757                              <1> alloc_8:
  2758                              <1> 	; 26/11/2021
  2759                              <1> 	; convert bit index to xor value
  2760                              <1> 	; and then set allocated flag for corresponding fbm bit 
  2761 0000538C 89C1                <1> 	mov	ecx, eax
  2762                              <1> 	;mov	cl, al 
  2763                              <1> 	;sub	eax, eax
  2764                              <1> 	;inc	al ; eax = 1
  2765 0000538E B001                <1> 	mov	al, 1
  2766 00005390 D3E0                <1> 	shl	eax, cl
  2767 00005392 3102                <1> 	xor	[edx], eax  ; clear allocated block's bit in the fbm
  2768                              <1> 	; zero bit means allocated disk block (1 means free disk block) 
  2769                              <1> 
  2770 00005394 A1[70750000]        <1> 	mov	eax, [free_map_offset] ; byte offset from start of fbm
  2771                              <1> 		; note: eax is dword aligned (0,4,8,12..) !
  2772 00005399 C1E003              <1> 	shl	eax, 3 ; * 8 ; free block number base
  2773 0000539C 01C8                <1> 	add	eax, ecx ; add allocation bit index (0 to 31)	
  2774 0000539E 89433C              <1> 	mov	[ebx+SB.FirstFreeBlk], eax ; current free block
  2775                              <1> 				; (which is being allocated here)
  2776                              <1> 
  2777                              <1> 	;mov	eax, [ebx+SB.FreeMapAddr] 
  2778                              <1> 	;		; free blocks map start sector addr
  2779                              <1> 	;add	eax, [free_map_index]
  2780                              <1> 	
  2781                              <1> 	;;add	eax, [ebx+SB.BootSectAddr] 
  2782                              <1> 	;		; + Hidden Sectors
  2783                              <1> 	; eax = physical sector number
  2784                              <1> 	
  2785                              <1> 	; 27/11/2021
  2786 000053A1 A1[78750000]        <1> 	mov	eax, [free_map_sector] ; physical sector number
  2787                              <1> 	
  2788 000053A6 E8060E0000          <1> 	call	wslot
  2789                              <1> 	; ebx = buffer data address (write operation bit is set)
  2790                              <1> 	; eax = physical sector number
  2791                              <1> 	; Note: ebx contains addr of the 1st buffer in the buffer
  2792                              <1> 	; (the last allocated buffer becomes the 1st in buffer chain)
  2793                              <1> 
  2794                              <1> 	; 27/11/2021
  2795                              <1> 	;mov	[free_map_buffer], ebx ; save free map buffer address
  2796                              <1> 
  2797 000053AB E81D0E0000          <1> 	call	dskwr ; writes the 1st buffer to sector
  2798                              <1> 		      ; (at the beginning/head of the buffer chain)
  2799                              <1> 
  2800                              <1> 	; if we are here, buffer has been written to disk successfully 
  2801                              <1> 	; (otherwise cpu would jump to 'error' address)
  2802                              <1> 
  2803                              <1> 	; set superblock modified flag
  2804 000053B0 BA[CC740000]        <1> 	mov	edx, smod
  2805 000053B5 BB[84750000]        <1> 	mov	ebx, systm
  2806 000053BA F605[B9740000]01    <1> 	test	byte [cdev], 1 ; mounted device ?
  2807 000053C1 7406                <1> 	jz	short alloc_9  ; no, root device
  2808                              <1> 	; yes, mounted device
  2809 000053C3 BB[8C770000]        <1> 	mov	ebx, mount
  2810                              <1> 	;mov	edx, mmod
  2811 000053C8 42                  <1> 	inc	edx ; edx = offset mmod
  2812                              <1> alloc_9: 
  2813 000053C9 FE02                <1> 	inc	byte [edx] ; superblock modified !
  2814                              <1> 	
  2815                              <1> 	; Allocating a new sector/block (for file) has been completed here.
  2816 000053CB 8B4338              <1> 	mov	eax, [ebx+SB.FreeBlocks]
  2817 000053CE 40                  <1> 	inc	eax ; 0FFFFFFFFh -> 0 
  2818 000053CF 7405                <1> 	jz	short alloc_10 ; not valid (not set)
  2819 000053D1 48                  <1> 	dec	eax ; Free Blocks
  2820 000053D2 48                  <1> 	dec	eax ; Free Blocks = Free Blocks - 1
  2821 000053D3 894338              <1> 	mov	[ebx+SB.FreeBlocks], eax
  2822                              <1> alloc_10:
  2823 000053D6 E8A90E0000          <1> 	call	get_system_time
  2824                              <1> 		; eax = current time (as unix epoch time)
  2825                              <1> 	; 30/07/2021
  2826 000053DB 89435C              <1> 	mov	[ebx+SB.ModifTime], eax
  2827                              <1> 
  2828 000053DE 8B433C              <1> 	mov	eax, [ebx+SB.FirstFreeBlk] ; allocated block address
  2829 000053E1 FF433C              <1> 	inc	dword [ebx+SB.FirstFreeBlk] 
  2830                              <1> 				; +1, new value of first free block
  2831                              <1> 				; (new search will be started from here)
  2832                              <1> 				; ((it may not be free, no problem))
  2833                              <1> 
  2834                              <1> 	; put free map buffer address in ebx
  2835                              <1> 	; 27/11/2021
  2836                              <1> 	;mov	ebx, [free_map_buffer]
  2837                              <1> 
  2838 000053E4 C3                  <1> 	retn	
  2839                              <1> 
  2840                              <1> 	; 09/01/2022
  2841                              <1> 	; 27/11/2021 - Retro UNIX 386 v2 fs compatibility modification
  2842                              <1> free:
  2843                              <1> 	; 09/01/2022 (Retro UNIX 386 v1.2)
  2844                              <1> 	; 05/11/2021
  2845                              <1> 	; 29/10/2021 - temporary (simplified code)
  2846                              <1> 	; 14/08/2021
  2847                              <1> 	; 12/06/2021
  2848                              <1> 	; 07/06/2020
  2849                              <1> 	; 25/05/2020 (Retro UNIX 386 v2 - Beginning)
  2850                              <1> 	; 03/06/2015 (Retro UNIX 386 v1 - Beginning)
  2851                              <1> 	; 07/04/2013 - 01/08/2013 (Retro UNIX 8086 v1)
  2852                              <1> 	;
  2853                              <1> 	; calculates byte address and bit position for given block number
  2854                              <1> 	; then sets the corresponding bit in the free storage map
  2855                              <1> 	; 
  2856                              <1> 	; INPUTS ->
  2857                              <1> 	;    r1 - block number for a block structured device
  2858                              <1> 	;    cdev - current device 
  2859                              <1> 	; OUTPUTS ->
  2860                              <1> 	;    free storage map is updated
  2861                              <1> 	;    smod is incremented if cdev is root device (fixed disk)
  2862                              <1> 	;    mmod is incremented if cdev is a removable disk 	
  2863                              <1> 	;
  2864                              <1> 	;  (Retro UNIX Prototype : 01/12/2012, UNIXCOPY.ASM)
  2865                              <1>         ;  ((Modified registers: DX, CX))  
  2866                              <1> 
  2867                              <1> 	; 27/11/2021
  2868                              <1> 	; INPUT:
  2869                              <1> 	;	[cdev] = current device (root = 0, mounted = 1) 
  2870                              <1> 	;	eax = disk block/sector number/address (logical)
  2871                              <1> 	; OUTPUT:
  2872                              <1> 	;	ebx = superblock buffer address
  2873                              <1> 	;
  2874                              <1> 	;    Note:	
  2875                              <1> 	;	Free blocks map will be modified and written to it's disk.
  2876                              <1> 	;	Superblock (buffer) of [cdev] will be modified and it's 
  2877                              <1> 	;	modification flag will be set; but the superblock 
  2878                              <1> 	;	will not be written to it's disk in this 'free' procedure)
  2879                              <1> 	;
  2880                              <1> 	; Modified registers: ebx, ecx, edx, esi, edi, ebp 
  2881                              <1> 
  2882                              <1> 	; 27/11/2021
  2883 000053E5 BB[84750000]        <1> 	mov 	ebx, systm ; (SuperBlock of root file system)
  2884 000053EA F605[B9740000]01    <1> 	test	byte [cdev], 1
  2885 000053F1 7405                <1> 	jz	short free_1
  2886 000053F3 BB[8C770000]        <1> 	mov	ebx, mount ; (SuperBlock of mounted file system)
  2887                              <1> free_1: ; 1
  2888                              <1> 	; 27/11/2021 - Retro UNIX 386 v2 file system compatibility code
  2889                              <1> 	;test	byte [ebx+SB.ReadOnly], 1 ; if bit 0 is 1, it is RO fs	
  2890                              <1> 	;jz	short free_2 
  2891                              <1> 	; 'read only file system' error
  2892 000053F8 C705[4C750000]1E00- <1> 	mov	dword [u.error], ERR_READ_ONLY_FS
  2892 00005400 0000                <1>
  2893 00005402 E9A3E7FFFF          <1> 	jmp	error
  2894                              <1> free_2:
  2895 00005407 89C1                <1> 	mov	ecx, eax ; logical sector/block number
  2896 00005409 A3[78750000]        <1> 	mov	[free_map_sector], eax
  2897 0000540E C1E903              <1> 	shr	ecx, 3	; convert to fbmap byte offset
  2898 00005411 C1E80C              <1> 	shr	eax, 12 ; convert to fbmap sector index 
  2899                              <1> 			; (1 fb sector for 4096 sectors)
  2900                              <1> 	;and	cl, ~3	; dword alignment (to backward)
  2901 00005414 890D[70750000]      <1> 	mov	[free_map_offset], ecx ; byte position on fbm
  2902                              <1> 	; 27/11/2021
  2903                              <1> 	; eax = fbm sector index
  2904 0000541A 034318              <1> 	add	eax, [ebx+SB.FreeMapAddr] 
  2905                              <1> 			; free blocks map start sector address
  2906                              <1> 	; 09/01/2022
  2907 0000541D 034304              <1> 	add	eax, [ebx+SB.BootSectAddr]
  2908                              <1> 			; + Hidden Sectors
  2909                              <1> 	; ebx = superblock address
  2910                              <1> 	; eax = physical sector number
  2911 00005420 E82C0D0000          <1> 	call	dskrd
  2912                              <1> 	; cpu returns here if there is/was not an error in 'dskrd'
  2913                              <1> 	; eax = physical sector number
  2914                              <1> 	; ebx = buffer data address
  2915                              <1> 	; 27/11/2021
  2916 00005425 A3[74750000]        <1> 	mov	[free_map_index], eax  ; save physical sector address
  2917 0000542A 8B15[70750000]      <1> 	mov	edx, [free_map_offset] ; byte position on fbm
  2918                              <1> 	;
  2919 00005430 29C0                <1> 	sub	eax, eax
  2920 00005432 A0[78750000]        <1> 	mov	al, [free_map_sector] ; logical sector number
  2921 00005437 2407                <1> 	and	al, 7
  2922                              <1> 
  2923                              <1> 	; al = bit offset in fbm allocation byte
  2924                              <1> 	; edx = buffer address (start+offset)
  2925                              <1> 	; [free_map_offset] = byte position on fbm
  2926                              <1> 	; 27/11/2021
  2927 00005439 0FAB02              <1> 	bts	[edx], eax  ; copy value of bit position in eax to cf
  2928                              <1> 			    ; then set same bit position at [edx]
  2929 0000543C 7305                <1> 	jnc	short free_3 ; it was allocated sector
  2930                              <1> 
  2931                              <1> 	; already free sector !? 
  2932                              <1> 	; (nonsence or defective fs)
  2933                              <1> 
  2934 0000543E 30C0                <1> 	xor	al, al ; 0  ; eax = 0
  2935 00005440 48                  <1> 	dec	eax ; 0FFFFFFFFh
  2936                              <1> 	;mov	[ebx+SB.FreeBlocks], eax 
  2937                              <1> 		; invalidate free blocks count
  2938 00005441 EB18                <1> 	jmp	short free_5
  2939                              <1> free_3:
  2940                              <1> 	; 27/11/2021
  2941 00005443 8B4338              <1> 	mov	eax, [ebx+SB.FreeBlocks]
  2942 00005446 40                  <1> 	inc	eax
  2943 00005447 7401                <1> 	jz	short free_4 ; 0FFFFFFFFh -> 0 (invalid!)
  2944 00005449 48                  <1> 	dec	eax  ; 0 -> 0FFFFFFFFh (invalid!)
  2945                              <1> free_4: 
  2946 0000544A 50                  <1> 	push	eax ; * ; number of free blocks 
  2947                              <1> 	;mov	eax, [free_map_index] ; fbm sector index
  2948                              <1> 	;add	eax, [ebx+SB.FreeMapAddr] 
  2949                              <1> 			; free blocks map start sector address
  2950                              <1> 	;add	eax, [ebx+SB.BootSectAddr]
  2951                              <1> 	;		; + Hidden Sectors
  2952 0000544B A1[74750000]        <1> 	mov	eax, [free_map_index] ; restore physical sector address
  2953                              <1> 	; eax = physical sector number
  2954 00005450 E85C0D0000          <1> 	call	wslot
  2955                              <1> 	; ebx = buffer data address (write operation bit is set)
  2956                              <1> 	; eax = physical sector number
  2957                              <1> 	; Note: ebx contains addr of the 1st buffer in the buffer
  2958                              <1> 	; (the last allocated buffer becomes the 1st in buffer chain)
  2959                              <1> 
  2960 00005455 E8730D0000          <1> 	call	dskwr ; writes the 1st buffer to sector
  2961                              <1> 		      ; (at the beginning/head of the buffer chain)
  2962                              <1> 
  2963                              <1> 	; if we are here, buffer has been written to disk successfully 
  2964                              <1> 	; (otherwise cpu would jump to 'error' address)
  2965 0000545A 58                  <1> 	pop	eax ; *  ; number of free blocks 
  2966                              <1> free_5:
  2967                              <1> 	; eax = number of free blocks 
  2968                              <1> 	;
  2969                              <1> 	; set superblock modified flag
  2970 0000545B BA[CC740000]        <1> 	mov	edx, smod
  2971 00005460 BB[84750000]        <1> 	mov	ebx, systm
  2972 00005465 F605[B9740000]01    <1> 	test	byte [cdev], 1 ; mounted device ?
  2973 0000546C 7406                <1> 	jz	short free_6 ; no, root device
  2974                              <1> 	; yes, mounted device
  2975 0000546E BB[8C770000]        <1> 	mov	ebx, mount
  2976                              <1> 	;mov	edx, mmod
  2977 00005473 42                  <1> 	inc	edx ; edx = offset mmod
  2978                              <1> free_6: 
  2979 00005474 894338              <1> 	mov	[ebx+SB.FreeBlocks], eax 
  2980                              <1> 
  2981 00005477 A1[78750000]        <1> 	mov	eax, [free_map_sector] ; logical sector number
  2982 0000547C 3B433C              <1> 	cmp	eax, [ebx+SB.FirstFreeBlk]
  2983 0000547F 7303                <1> 	jnb	short free_7
  2984 00005481 89433C              <1> 	mov	[ebx+SB.FirstFreeBlk], eax
  2985                              <1> free_7:
  2986 00005484 FE02                <1> 	inc	byte [edx] ; superblock modified !
  2987 00005486 C3                  <1> 	retn
  2988                              <1> 	
  2989                              <1> iget:
  2990                              <1> 	; 09/01/2022 (Retro UNIX 386 v1.2)
  2991                              <1> 	; 30/11/2021
  2992                              <1> 	; 22/11/2021 - Retro UNIX 386 v2 compatibility modification
  2993                              <1> 	; 03/06/2015 (Retro UNIX 386 v1 - Beginning)
  2994                              <1> 	; 07/04/2013 - 07/08/2013 (Retro UNIX 8086 v1)
  2995                              <1> 	;
  2996                              <1> 	; get a new i-node whose i-number in r1 and whose device is in cdev
  2997                              <1> 	;
  2998                              <1> 	; ('iget' returns current i-number in r1, if input value of r1 is 0)
  2999                              <1> 	; 
  3000                              <1> 	; INPUTS ->
  3001                              <1> 	;    ii - current i-number, rootdir
  3002                              <1> 	;    cdev - new i-node device
  3003                              <1> 	;    idev - current i-node device
  3004                              <1> 	;    imod - current i-node modified flag
  3005                              <1> 	;    mnti - cross device file i-number
  3006                              <1> 	;    r1 - i-numbe rof new i-node
  3007                              <1> 	;    mntd - mountable device number		
  3008                              <1> 	; 	 
  3009                              <1> 	; OUTPUTS ->
  3010                              <1> 	;    cdev, idev, imod, ii, r1
  3011                              <1> 	;
  3012                              <1> 	; ((AX = R1)) input/output
  3013                              <1> 	;
  3014                              <1> 	;  (Retro UNIX Prototype : 14/07/2012 - 18/11/2012, UNIXCOPY.ASM)
  3015                              <1>         ;  ((Modified registers: eDX, eCX, eBX, eSI, eDI, eBP))  
  3016                              <1> 
  3017                              <1> 	; 22/11/2021
  3018                              <1> 	;;mov	dl, [cdev] ; 18/07/2013
  3019                              <1> 	;;mov	dh, [idev] ; 07/08/2013
  3020                              <1> 	; 26/05/2020 - Retro UNIX 386 v2
  3021                              <1> 	;mov	dh, [cdev]
  3022                              <1> 	;mov	dl, [idev]
  3023 00005487 668B15[B8740000]    <1> 	mov	dx, [idev] ; [idev] in dl, [cdev] in dh
  3024                              <1> 	;
  3025                              <1> 	; 22/11/2021
  3026 0000548E 25FFFF0000          <1> 	and	eax, 0FFFFh
  3027 00005493 3B05[B4740000]      <1> 	cmp 	eax, [ii]
  3028                              <1> 		; cmp r1,ii / r1 = i-number of current file
  3029 00005499 7504                <1> 	jne 	short iget_1
  3030                              <1> 		; bne 1f
  3031 0000549B 38F2                <1> 	cmp	dl, dh
  3032                              <1> 		; cmp idev,cdev
  3033                              <1> 			  ; / is device number of i-node = current device
  3034 0000549D 746C                <1>         je	short iget_5
  3035                              <1> 	;	; beq 2f
  3036                              <1> 	; 14/08/2021
  3037                              <1> iget_1: ; 1:
  3038 0000549F 30DB                <1> 	xor	bl, bl
  3039 000054A1 381D[CA740000]      <1> 	cmp	[imod], bl ; 0	
  3040                              <1> 		; tstb imod / has i-node of current file
  3041                              <1> 			  ; / been modified i.e., imod set
  3042 000054A7 7628                <1> 	jna	short iget_2
  3043                              <1> 		; beq 1f
  3044 000054A9 881D[CA740000]      <1> 	mov	[imod], bl ; 0
  3045                              <1> 		;  clrb	imod / if it has, 
  3046                              <1> 			   ; / we must write the new i-node out on disk
  3047                              <1> 	; 22/11/2021 (32 bit push-pop)
  3048 000054AF 50                  <1> 	push	eax ; *
  3049                              <1> 		; mov r1,-(sp)
  3050 000054B0 52                  <1> 	push	edx ; **
  3051                              <1> 		; mov cdev,-(sp)
  3052 000054B1 A1[B4740000]        <1> 	mov	eax, [ii]
  3053                              <1> 		; mov ii,r1
  3054                              <1> 	;mov	dh, [idev]
  3055                              <1> 	;mov	[cdev], dh
  3056                              <1> 	; 09/01/2022
  3057 000054B6 8815[B9740000]      <1> 	mov	[cdev], dl  ; dl = [idev]
  3058                              <1> 		; mov idev,cdev
  3059 000054BC FEC3                <1> 	inc	bl ; 1
  3060                              <1> 	; 31/07/2013
  3061 000054BE 881D[6C750000]      <1> 	mov     [rw], bl ; 1 == write 
  3062                              <1> 	;;28/07/2013 rw -> u.rw
  3063                              <1>         ;;mov   [u.rw], bl ; 1 == write
  3064 000054C4 E843000000          <1> 	call	icalc
  3065                              <1> 		; jsr r0,icalc; 1
  3066 000054C9 5A                  <1> 	pop	edx ; **
  3067 000054CA 8835[B9740000]      <1> 	mov	[cdev], dh ; 22/11/2021
  3068                              <1> 		; mov (sp)+,cdev
  3069 000054D0 58                  <1> 	pop	eax ; *
  3070                              <1> 		; mov (sp)+,r1
  3071                              <1> iget_2: ; 1:
  3072                              <1> 	;and	ax, ax
  3073 000054D1 21C0                <1> 	and	eax, eax ; 22/11/2021 (32 bit inode number)
  3074                              <1> 		; tst r1 / is new i-number non zero
  3075 000054D3 7431                <1> 	jz	short iget_4 ; 2f
  3076                              <1> 		; beq 2f / branch if r1=0
  3077                              <1> 
  3078                              <1> 	;mov 	dh, [cdev]
  3079 000054D5 08F6                <1> 	or	dh, dh ; 22/11/2021
  3080                              <1> 		; tst cdev / is the current device number non zero
  3081                              <1> 			 ; / (i.e., device =/ drum)
  3082 000054D7 7515                <1> 	jnz	short iget_3 ;  1f
  3083                              <1> 		; bne 1f / branch 1f cdev =/ 0  ;; (cdev != 0)
  3084                              <1> 	; 22/11/2021
  3085 000054D9 3B05[C4740000]      <1> 	cmp	eax, [mnti]
  3086                              <1> 	;cmp	ax, [mnti]			
  3087                              <1> 		; cmp r1,mnti / mnti is the i-number of the cross device
  3088                              <1> 			    ; / file (root directory of mounted device)
  3089 000054DF 750D                <1> 	jne	short iget_3 ; 1f
  3090                              <1> 		; bne 1f
  3091                              <1>         ;mov    bl, [mntd]
  3092 000054E1 FEC6                <1> 	inc	dh ; mov dh, 1 ; 22/11/2021
  3093 000054E3 8835[B9740000]      <1>         mov	[cdev], dh ; 22/11/2021
  3094                              <1> 		; mov mntd,cdev / make mounted device the current device
  3095                              <1> 	; 22/11/2021
  3096 000054E9 A1[C0740000]        <1> 	mov	eax, [rootdir] ; rootdir = 1 (for runix v2 file system)
  3097                              <1> 	;mov	ax, [rootdir]
  3098                              <1> 		; mov rootdir,r1
  3099                              <1> iget_3: ; 1:
  3100                              <1> 	; 22/11/2021
  3101 000054EE A3[B4740000]        <1> 	mov	[ii], eax ; 32 bit inode number
  3102                              <1> 	;mov	[ii], ax
  3103                              <1> 		; mov r1,ii
  3104                              <1> 	; 30/11/2021
  3105 000054F3 8835[B8740000]      <1> 	mov	[idev], dh ; cdev 
  3106                              <1> 	;mov	[idev], dl ; cdev
  3107                              <1> 		; mov cdev,idev
  3108 000054F9 30DB                <1> 	xor	bl, bl
  3109                              <1>         ; 31/07/2013
  3110 000054FB 881D[6C750000]      <1> 	mov     [rw], bl ; 0 == read 
  3111                              <1> 	;;28/07/2013 rw -> u.rw       
  3112                              <1>         ;;mov   [u.rw], bl ; 0 = read
  3113 00005501 E806000000          <1> 	call	icalc
  3114                              <1> 		; jsr r0,icalc; 0 / read in i-node ii
  3115                              <1> iget_4: ; 2:
  3116                              <1> 	; 22/11/2021
  3117 00005506 A1[B4740000]        <1> 	mov	eax, [ii]
  3118                              <1> 	;mov	ax, [ii]
  3119                              <1> 		; mov ii,r1
  3120                              <1> iget_5:
  3121 0000550B C3                  <1> 	retn
  3122                              <1> 		; rts r0
  3123                              <1> 
  3124                              <1> icalc:
  3125                              <1> 	; 09/01/2022 (Retro UNIX 386 v1.2)
  3126                              <1> 	; 28/11/2021
  3127                              <1> 	; 22/11/2021 - Retro UNIX 386 v2 compatibility modification 
  3128                              <1> 	; 02/07/2015
  3129                              <1> 	; 03/06/2015 (Retro UNIX 386 v1 - Beginning)
  3130                              <1> 	; 07/04/2013 - 31/07/2013 (Retro UNIX 8086 v1)
  3131                              <1> 	;
  3132                              <1> 	; calculate physical block number from i-number then
  3133                              <1> 	; read or write that block
  3134                              <1> 	;
  3135                              <1> 	; 'icalc' is called from 'iget'
  3136                              <1> 	;
  3137                              <1> 	; for original unix v1:
  3138                              <1> 	; / i-node i is located in block (i+31.)/16. and begins 32.*
  3139                              <1>        	; / (i+31.) mod 16. bytes from its start
  3140                              <1> 	;
  3141                              <1> 	; for retro unix 8086 v1:
  3142                              <1> 	;  i-node is located in block (i+47)/16 and
  3143                              <1> 	;  begins 32*(i+47) mod 16 bytes from its start
  3144                              <1> 	;
  3145                              <1> 	; INPUTS ->
  3146                              <1> 	;    r1 - i-number of i-node
  3147                              <1> 	; 	 
  3148                              <1> 	; OUTPUTS ->
  3149                              <1> 	;    inode r/w
  3150                              <1> 	;
  3151                              <1> 	; ((AX = R1)) input
  3152                              <1> 	;
  3153                              <1> 	;  (Retro UNIX Prototype : 14/07/2012 - 18/11/2012, UNIXCOPY.ASM)
  3154                              <1>         ;  ((Modified registers: eAX, eDX, eCX, eBX, eSI, eDI, eBP))  
  3155                              <1> 	;
  3156                              <1> 
  3157                              <1> 	; 28/11/2021
  3158                              <1> 	;cmp	byte [idev], 0 ; [cdev] = [idev]
  3159 0000550C 803D[B9740000]00    <1> 	cmp	byte [cdev], 0
  3160 00005513 7607                <1> 	jna	short icalc_0r
  3161 00005515 BD[8C770000]        <1> 	mov	ebp, mount  ; mounted file system's superblock 
  3162 0000551A EB05                <1> 	jmp	short icalc_0m
  3163                              <1> icalc_0r:
  3164                              <1> 	; 28/11/2021
  3165 0000551C BD[84750000]        <1> 	mov	ebp, systm  ; root file system's superblock 
  3166                              <1> icalc_0m:
  3167                              <1> 	; 22/11/2021
  3168                              <1> 	;mov	edx, eax
  3169 00005521 8B15[B4740000]      <1> 	mov	edx, [ii] ; 32 bit inode number
  3170 00005527 8B4528              <1> 	mov	eax, [ebp+SB.InodeTblAddr] ; inode table base address
  3171                              <1> 	;
  3172 0000552A 4A                  <1> 	dec	edx ; 0 based inode number 
  3173                              <1> 		    ; (inode index in inode table)
  3174 0000552B 52                  <1> 	push	edx ; *
  3175 0000552C C1EA03              <1> 	shr	edx, 3
  3176 0000552F 7402                <1> 	jz	short icalc_0t ; 0 for inodes 1 to 8
  3177 00005531 01D0                <1> 	add	eax, edx
  3178                              <1> icalc_0t:
  3179                              <1> 	; 09/01/2022
  3180                              <1> 	; eax = locical block/sector number 
  3181 00005533 034504              <1> 	add	eax, [ebp+SB.BootSectAddr]
  3182                              <1> 	; eax = physical block/sector number 
  3183 00005536 E8160C0000          <1> 	call	dskrd
  3184                              <1> 		; jsr r0,dskrd / read in block containing i-node i.
  3185                              <1> 	; 31/07/2013
  3186 0000553B 803D[6C750000]00    <1>         cmp     byte [rw], 0 ; Retro Unix 8086 v1 feature !
  3187                              <1> 	;; 28/07/2013 rw -> u.rw
  3188                              <1>         ;;cmp	byte [u.rw], 0 ; Retro Unix 8086 v1 feature !
  3189                              <1> 		; tst (r0)
  3190 00005542 7605                <1> 	jna	short icalc_1
  3191                              <1> 		; beq 1f / branch to wslot when argument
  3192                              <1> 		       ; / in icalc call = 1
  3193                              <1> 	; eAX = r1 = block number
  3194 00005544 E8680C0000          <1> 	call	wslot
  3195                              <1> 		; jsr r0,wslot / set up data buffer for write
  3196                              <1> 			     ; / (will be same buffer as dskrd got)
  3197                              <1> 	; EBX = r5 points to first word in data area for this block
  3198                              <1> icalc_1: ; 1:
  3199 00005549 5A                  <1> 	pop	edx ; *
  3200                              <1> 	; 22/11/2021
  3201 0000554A 83E207              <1> 	and 	edx, 07h ; 8 inodes per inode table sector
  3202 0000554D C1E206              <1> 	shl 	edx, 6 ; * 64 (inode size)
  3203                              <1> 	; edx = 64 * (mod(inodenumber-1)/8)
  3204 00005550 89DE                <1> 	mov	esi, ebx  ; ebx points to 1st word of the buffer
  3205 00005552 01D6                <1> 	add	esi, edx  ; edx is inode offset in the buffer
  3206                              <1>           	; eSI (r5) points to first word in i-node i.	
  3207                              <1> 		; mov (sp)+,mq / calculate offset in data buffer; 
  3208                              <1> 			     ; / 32.*(i+31.)mod16
  3209                              <1> 		; mov $5,lsh / for i-node i.
  3210                              <1> 		; add mq,r5 / r5 points to first word in i-node i.
  3211 00005554 BF[74700000]        <1> 	mov	edi, inode
  3212                              <1> 		; mov $inode,r1 / inode is address of first word 
  3213                              <1> 			      ; / of current i-node
  3214                              <1> 	;mov 	ecx, 8 ; 02/07/2015 (32 bit modification)
  3215                              <1> 	;	; mov $16.,r3
  3216                              <1> 	; 28/11/2021
  3217 00005559 29C9                <1> 	sub	ecx, ecx
  3218 0000555B B110                <1> 	mov	cl, 16 ; Retro UNIX 386 v2 inode size = 64 bytes
  3219                              <1> 	; 31/07/2013
  3220 0000555D 382D[6C750000]      <1>   	cmp     [rw], ch ; 0  ;; Retro Unix 8086 v1 feature !
  3221                              <1>        ;;28/07/2013 rw -> u.rw                 
  3222                              <1>        ;;cmp    [u.rw], ch ; 0  ;; Retro Unix 8086 v1 feature !
  3223                              <1> 		; tst (r0)+ / branch to 2f when argument in icalc call = 0
  3224 00005563 7609                <1> 	jna	short icalc_3
  3225                              <1> 		; beq 2f / r0 now contains proper return address 
  3226                              <1> 		       ; / for rts r0
  3227                              <1> icalc_2: ; 1:
  3228 00005565 87F7                <1> 	xchg 	esi, edi
  3229                              <1> 	; overwrite old i-node (in buffer to be written)
  3230 00005567 F3A5                <1> 	rep 	movsd
  3231                              <1> 		; mov (r1)+,(r5)+ / over write old i-node
  3232                              <1> 		; dec r3
  3233                              <1> 		; bgt 1b
  3234                              <1> 	;call	dskwr
  3235                              <1> 	;	; jsr r0,dskwr / write inode out on device
  3236                              <1> 	;retn
  3237                              <1> 	;	; rts r0
  3238                              <1> 	; 28/11/2021
  3239 00005569 E95F0C0000          <1> 	jmp	dskwr
  3240                              <1> icalc_3: ; 2:
  3241                              <1> 	; copy new i-node into inode area of (core) memory
  3242 0000556E F3A5                <1> 	rep 	movsd
  3243                              <1> 		; mov (r5)+,(r1)+ / read new i-node into 
  3244                              <1> 		                ; / "inode" area of core
  3245                              <1> 		; dec r3
  3246                              <1> 		; bgt 2b
  3247 00005570 C3                  <1> 	retn
  3248                              <1> 		; rts r0
  3249                              <1> 
  3250                              <1> access:
  3251                              <1> 	; 25/12/2021
  3252                              <1> 	; 19/12/2021
  3253                              <1> 	; 28/11/2021
  3254                              <1> 	; 22/11/2021 - Retro UNIX 386 v2 compatibility modification
  3255                              <1> 	; 31/10/2021 - temporary (simplified code)
  3256                              <1> 	; 14/06/2021
  3257                              <1> 	; 02/05/2021
  3258                              <1> 	; 27/03/2021
  3259                              <1> 	; 26/03/2021
  3260                              <1> 	; 25/03/2021
  3261                              <1> 	; 23/03/2021 (Retro UNIX 386 v2 - Beginning)
  3262                              <1> 	;	((ref: unix v7 source - fio.c))
  3263                              <1> 	; ref: INODE STRUCTURE of Retro UNIX v2 file system
  3264                              <1> 	;	07/02/2020, 'RETRO UNIX v2 Inodes.pdf'
  3265                              <1> 	;	 
  3266                              <1> 	; 03/06/2015 (Retro UNIX 386 v1 - Beginning)
  3267                              <1> 	; 24/04/2013 - 29/04/2013 (Retro UNIX 8086 v1)
  3268                              <1> 	;
  3269                              <1> 	; check whether user is owner of file or user has read or write
  3270                              <1> 	; permission (based on i.flgs).
  3271                              <1> 	;
  3272                              <1> 	; INPUTS ->
  3273                              <1> 	;    r1 - i-number of file
  3274                              <1> 	;    u.uid
  3275                              <1> 	; arg0 -> (owner flag mask)	 		
  3276                              <1> 	;     Retro UNIX 8086 v1 feature -> owner flag mask in DL (DX) 	 
  3277                              <1> 	; OUTPUTS ->
  3278                              <1> 	;    inode (or jump to error)
  3279                              <1> 	;
  3280                              <1> 	; ((AX = R1)) input/output
  3281                              <1> 	;
  3282                              <1> 	;  ((Modified registers: eCX, eBX, eDX, eSI, eDI, eBP))
  3283                              <1> 
  3284                              <1> 	; 26/03/2021
  3285                              <1> 	; INPUT:
  3286                              <1> 	;	eax = inode number (ax)
  3287                              <1> 	;	 dx = mode (IEXEC, IREAD or IWRITE) 
  3288                              <1> 	;	[cdev] = logical drive number ; 31/10/2021 
  3289                              <1> 	;	
  3290                              <1> 	; OUTPUT:
  3291                              <1> 	;	inode (or jump to error)
  3292                              <1> 	;	eax (ax) = [ii] = inode number
  3293                              <1> 	;	[cdev] = logical drive number ; 31/10/2021 
  3294                              <1> 	;
  3295                              <1> 	; Modified registers: (eax), ebx, ecx, edx, esi, edi, ebp
  3296                              <1> 	;
  3297                              <1> 	; IREAD	 equ 100h ; read permission flag, owner
  3298                              <1> 	; IWRITE equ 80h  ; write permission flag, owner
  3299                              <1> 	; IEXEC	 equ 40h  ; execute permission flag, owner	 
  3300                              <1> 		
  3301                              <1> 	; 23/03/2021
  3302 00005571 52                  <1> 	push	edx ; save flag (DX)
  3303                              <1> 	;push	dx  ; save flag (DL)
  3304 00005572 E810FFFFFF          <1> 	call	iget
  3305                              <1> 		; jsr r0,iget / read in i-node for current directory
  3306                              <1> 			    ; / (i-number passed in r1)
  3307                              <1> 	;;mov	cl, [i.flgs]
  3308                              <1> 	;;	; mov i.flgs,r2
  3309                              <1> 	; 23/03/2021
  3310                              <1> 	;mov	cx, [i.iflgs]	
  3311                              <1> 
  3312                              <1> 	;;pop	dx  ; restore flag (DL)
  3313                              <1> 	; 23/03/2021
  3314 00005577 5A                  <1> 	pop	edx ; restore flag (DX)
  3315                              <1> 
  3316                              <1> 	; 28/11/2021 (iget will not return here if there is an error)
  3317                              <1> 	; 31/10/2021
  3318                              <1> 	;jc	short access_5
  3319                              <1> 
  3320                              <1> 	; 31/10/2021
  3321                              <1> 	; Note: If eax input is same with [mnti], 'iget' will change
  3322                              <1> 	; [cdev] to 1 and eax will be 1 (root dir of mounted fs) 
  3323                              <1> 
  3324                              <1> 	; 27/03/2021
  3325                              <1> 	; 23/03/2021
  3326                              <1> 	; (check if it is write permission flag)
  3327 00005578 6681FA8000          <1> 	cmp	dx, 80h ; IWRITE
  3328 0000557D 775E                <1> 	ja	short access_r	; IREAD = 100h (owner)
  3329 0000557F 722B                <1> 	jb	short access_x	; IEXEC = 40h (owner)
  3330                              <1> 	; IWRITE (80h)
  3331                              <1> access_w:
  3332                              <1> 	; 23/03/2021
  3333                              <1> 	; ((ref: Retro UNIX 386 v2, 'ux.s', 'ldrv' structure))
  3334                              <1> 	;mov	bl, [idev]
  3335                              <1> 	;call	getfs
  3336                              <1>  	;movzx	ebx, byte [idev]  ; logical drive number
  3337                              <1> 	;shl	bx, 6 ; * 64	  ; of current inode	
  3338                              <1> 	;add	ebx, ldrvtable	
  3339                              <1> 	;;test	byte [ebx+ldrv.pflags], 01h ; read only fs flag
  3340                              <1> 	;;jz	short access_0
  3341                              <1> 	;; 14/06/2021
  3342                              <1> 	; 14/06/2021
  3343                              <1> 	; check block device (fs) if it is read only fs or not ? 
  3344                              <1> 	;mov	edi, [ebx+ldrv.superblk]
  3345                              <1> 	;test	byte [edi+SB.ReadOnly], 1 ; bit 0 = 1 ?
  3346                              <1> 	;jz	short access_0
  3347                              <1> 	; 31/10/2021
  3348 00005581 803D[B9740000]01    <1> 	cmp	byte [cdev], 1
  3349 00005588 7207                <1> 	jb	short access_7 ; [cdev] = 0
  3350                              <1> 	; [cdev] = 1
  3351                              <1> 	; 28/11/2021
  3352 0000558A BF[8C770000]        <1> 	mov	edi, mount ; mounted file system's superblock buffer
  3353 0000558F EB05                <1> 	jmp	short access_8
  3354                              <1> access_7:
  3355                              <1> 	; 28/11/2021
  3356 00005591 BF[84750000]        <1> 	mov	edi, systm ; root file system's superblock buffer
  3357                              <1> access_8:
  3358 00005596 F6476A01            <1> 	test	byte [edi+SB.ReadOnly], 1 ; bit 0 = 1 ?
  3359 0000559A 7441                <1> 	jz	short access_0
  3360                              <1> 
  3361                              <1> 	; 23/03/2021
  3362                              <1> 	;mov	dword [u.error], ERR_READ_ONLY_FS
  3363                              <1> 	;		; 'read only file system !' error
  3364                              <1> 	;jmp	error
  3365                              <1> 
  3366                              <1> 	; 19/12/2021
  3367 0000559C F605[75700000]80    <1> 	test	byte [i.flgs+1], 80h ; regular file ?
  3368 000055A3 7438                <1> 	jz	short access_0  ; no, device file
  3369                              <1> 
  3370                              <1> 	; 31/10/2021
  3371 000055A5 B81E000000          <1> 	mov	eax, ERR_READ_ONLY_FS
  3372                              <1> 			; 'read only file system !' error
  3373                              <1> 	; 27/03/2021
  3374 000055AA EB27                <1> 	jmp	short access_5
  3375                              <1> 
  3376                              <1> 	; check block device (fs) if it is read only fs or not ? 
  3377                              <1> 
  3378                              <1> 	;mov	dh, [u.uid]
  3379                              <1> 	;cmp	dh, [i.uid]
  3380                              <1> 	;	; cmpb i.uid,u.uid / is user same as owner of file
  3381                              <1> 	;jne	short access_1
  3382                              <1> 		; bne 1f / no, then branch
  3383                              <1> access_x:
  3384                              <1> 	; IEXEC (40h)
  3385                              <1> 	; 27/03/2021
  3386 000055AC 668B1D[74700000]    <1> 	mov	bx, [i.flgs]
  3387 000055B3 F6C780              <1> 	test	bh, 80h ; regular file ?
  3388 000055B6 7416                <1> 	jz	short access_4 ; not executable file !
  3389 000055B8 F6C740              <1> 	test	bh, 40h	; directory ?
  3390 000055BB 7511                <1> 	jnz	short access_4 ; not executable file !
  3391 000055BD 668B0D[2A750000]    <1> 	mov	cx, [u.uid]
  3392 000055C4 6609C9              <1> 	or	cx, cx
  3393 000055C7 7520                <1> 	jnz	short access_3 ; (restricted user)
  3394                              <1> 	; (super user)
  3395                              <1> 	;test	[i.flgs], dl ; execute permission for owner
  3396                              <1> 	;test	byte [i.flgs], 49h ; for owner, group, others
  3397 000055C9 F6C349              <1> 	test	bl, 49h ; exec perm for owner, group, others
  3398 000055CC 7557                <1> 	jnz	short access_2
  3399                              <1> access_4:
  3400                              <1> 	; 31/10/2021
  3401 000055CE B816000000          <1> 	mov	eax, ERR_NOT_EXECUTABLE
  3402                              <1> 			; 'not executable file !' error
  3403                              <1> 	; 26/03/2021
  3404                              <1> ;;sysexec_not_exf:
  3405                              <1> ;	mov	dword [u.error], ERR_NOT_EXECUTABLE
  3406                              <1> ;			; 'not executable file !' error
  3407                              <1> ;access_5:
  3408                              <1> ;	jmp	error
  3409                              <1> 	
  3410                              <1> 	; 31/10/2021
  3411                              <1> access_5:
  3412 000055D3 A3[4C750000]        <1> 	mov	[u.error], eax
  3413 000055D8 E9CDE5FFFF          <1> 	jmp	error
  3414                              <1> 
  3415                              <1> access_r:
  3416                              <1> 	; 27/03/2021
  3417                              <1> access_0:
  3418                              <1> 	; 23/03/2021
  3419 000055DD 668B0D[2A750000]    <1> 	mov	cx, [u.uid]
  3420 000055E4 6609C9              <1> 	or	cx, cx ; 0 ; root ?
  3421                              <1> 	;jz	short access_2 ; yes
  3422                              <1> 	; 25/12/2021
  3423 000055E7 742C                <1> 	jz	short access_1 ; yes
  3424                              <1> access_3:
  3425                              <1> 	; owner ?
  3426 000055E9 8A1D[7A700000]      <1> 	mov	bl, [i.gid]
  3427 000055EF 663B0D[78700000]    <1> 	cmp	cx, [i.uid]
  3428                              <1> 	;je	short access_1
  3429                              <1> 	; 02/05/2021
  3430 000055F6 750E                <1> 	jne	short access_6
  3431                              <1> 	; check owner's group number/ID
  3432 000055F8 3A1D[2E750000]      <1> 	cmp	bl, [u.gid] ; same group number/ID ?
  3433 000055FE 7415                <1> 	je	short access_1 ; yes, owner
  3434                              <1> 	; one of others
  3435 00005600 66C1EA06            <1> 	shr	dx, 6
  3436 00005604 EB0F                <1> 	jmp	short access_1 ; others
  3437                              <1> access_6:
  3438                              <1> 	;shr	cl, 2
  3439                              <1> 	;	; asrb r2 / shift owner read write bits into non owner
  3440                              <1> 	;	;       ; / read/write bits
  3441                              <1> 	;	; asrb r2
  3442                              <1> 
  3443                              <1> 	; 23/03/2021
  3444                              <1> 	; same group ?
  3445 00005606 66C1EA03            <1> 	shr	dx, 3
  3446                              <1> 	;mov	cl, [u.gid]
  3447                              <1> 	;cmp	cl, [i.gid]
  3448                              <1> 	;je	short access_1
  3449                              <1> 	; 02/05/2021
  3450 0000560A 3A1D[2E750000]      <1> 	cmp	bl, [u.gid]
  3451 00005610 7403                <1> 	je	short access_1 ; same group, different owner
  3452                              <1> 
  3453                              <1> 	; others
  3454                              <1> 	;shr	dx, 3
  3455 00005612 C0EA03              <1> 	shr	dl, 3
  3456                              <1> 	;jmp	short access_1	
  3457                              <1> 
  3458                              <1> ;access_1: ; 1:
  3459                              <1> 	;and	cl, dl
  3460                              <1> 	;	; bit r2,(r0)+ / test read-write flags against argument
  3461                              <1> 	;		     ; / in access call
  3462                              <1> 	;jnz	short access_2
  3463                              <1> 	;	; bne 1f
  3464                              <1> 	
  3465                              <1> 	;or	dh, dh	; super user (root) ?
  3466                              <1> 	;	; tstb u.uid
  3467                              <1> 	;jz	short access_2 ; yes, super user
  3468                              <1> 	;;jnz	error
  3469                              <1> 	;	; beq 1f
  3470                              <1> 	;	; jmp error
  3471                              <1> 	
  3472                              <1> 	;mov	dword [u.error], ERR_FILE_ACCESS 
  3473                              <1> 	;		; 'permission denied !' error
  3474                              <1> 	;jmp	error
  3475                              <1> 
  3476                              <1> access_1:
  3477                              <1> 	; 23/03/2021
  3478 00005615 668515[74700000]    <1> 	test	dx, [i.flgs]
  3479 0000561C 7507                <1> 	jnz	short access_2
  3480                              <1> 
  3481                              <1> 	;; r/w permission error
  3482                              <1> 	;mov	dword [u.error], ERR_FILE_ACCESS 
  3483                              <1> 	;		; 'permission denied !' error
  3484                              <1> 	;;jmp	error
  3485                              <1> 
  3486                              <1> 	; 31/10/2021
  3487 0000561E B80B000000          <1> 	mov	eax, ERR_FILE_ACCESS 
  3488                              <1> 			; 'permission denied !' error
  3489                              <1> 	; 27/03/2021
  3490 00005623 EBAE                <1> 	jmp	short access_5
  3491                              <1> 
  3492                              <1> access_2: ; 1:
  3493                              <1> 	;; DL = flags
  3494                              <1> 	;retn
  3495                              <1> 	;	; rts r0
  3496                              <1> 	; 27/03/2021
  3497                              <1> 	; DX = flag (100h-20h-04h or 80h-10h-02h or 40h-08h-01h)
  3498 00005625 C3                  <1> 	retn
  3499                              <1> 
  3500                              <1> 	; 12/01/2022 - Retro UNIX 386 v1.2
  3501                              <1> %if 0
  3502                              <1> 
  3503                              <1> setimod:
  3504                              <1> 	; 03/06/2015 (Retro UNIX 386 v1 - Beginning)
  3505                              <1> 	; 09/04/2013 - 31/07/2013 (Retro UNIX 8086 v1)
  3506                              <1> 	;
  3507                              <1> 	; 'setimod' sets byte at location 'imod' to 1; thus indicating that 
  3508                              <1> 	; the inode has been modified. Also puts the time of modification
  3509                              <1> 	; into the inode.
  3510                              <1> 	;
  3511                              <1> 	;  (Retro UNIX Prototype : 14/07/2012 - 23/02/2013, UNIXCOPY.ASM)
  3512                              <1>         ;  ((Modified registers: eDX, eCX, eBX)) 
  3513                              <1> 	;
  3514                              <1> 	
  3515                              <1> 	; push 	edx
  3516                              <1> 	push	eax
  3517                              <1> 
  3518                              <1> 	mov 	byte [imod], 1
  3519                              <1> 		; movb $1,imod / set current i-node modified bytes
  3520                              <1> 	; Erdogan Tan 14-7-2012
  3521                              <1> 	call 	epoch
  3522                              <1> 		 ; mov s.time,i.mtim 
  3523                              <1> 			    ; / put present time into file modified time
  3524                              <1> 		 ; mov s.time+2,i.mtim+2
  3525                              <1> 
  3526                              <1> 	mov 	[i.mtim], eax
  3527                              <1> 	
  3528                              <1> 	; Retro UNIX 386 v1 modification ! (cmp)
  3529                              <1> 	; Retro UNIX 8086 v1 modification ! (test)
  3530                              <1> 	cmp	dword [i.ctim], 0
  3531                              <1> 	jnz	short setimod_ok
  3532                              <1> 
  3533                              <1> 	mov 	[i.ctim], eax
  3534                              <1> 
  3535                              <1> setimod_ok: ; 31/07/2013
  3536                              <1> 	pop	eax
  3537                              <1> 	;pop	edx
  3538                              <1> 	
  3539                              <1> 	retn
  3540                              <1> 		; rts r0
  3541                              <1> %endif
  3542                              <1> 
  3543                              <1> setimod:
  3544                              <1> 	; 12/01/2022 - Retro UNIX 386 v1.2
  3545                              <1> 	; 25/05/2020 - Retro UNIX 386 v2
  3546                              <1> 	; 03/06/2015 (Retro UNIX 386 v1 - Beginning)
  3547                              <1> 	; 09/04/2013 - 31/07/2013 (Retro UNIX 8086 v1)
  3548                              <1> 	;
  3549                              <1> 	; 'setimod' sets byte at location 'imod' to 1; thus indicating that 
  3550                              <1> 	; the inode has been modified. Also puts the time of modification
  3551                              <1> 	; into the inode.
  3552                              <1> 	;
  3553                              <1> 	;  (Retro UNIX Prototype : 14/07/2012 - 23/02/2013, UNIXCOPY.ASM)
  3554                              <1>         ;  ((Modified registers: eDX, eCX, eBX)) 
  3555                              <1> 	;
  3556                              <1> 
  3557                              <1> 	; Reference:
  3558                              <1> 	; 25/01/2020 - 'UNIXHDCP.ASM', 'setimod' procedure
  3559                              <1> 	;	(Retro UNIX 386 v2 file system installation utility)
  3560                              <1> 
  3561                              <1> 	; 25/05/2020
  3562                              <1> 	;
  3563                              <1> 	; INPUT:
  3564                              <1> 	;	none
  3565                              <1> 	; OUTPUT:
  3566                              <1> 	;	inode times and [imod] will be set
  3567                              <1> 	;	      ([imodx] will be reset)
  3568                              <1> 	;	cf = 0 	
  3569                              <1> 
  3570                              <1> 	; Modified registers: ecx, (edx)?
  3571                              <1> 	
  3572                              <1> 	;push 	edx
  3573 00005626 50                  <1> 	push	eax
  3574                              <1> 
  3575 00005627 C605[CA740000]01    <1> 	mov 	byte [imod], 1
  3576                              <1> 		; movb $1,imod / set current i-node modified bytes
  3577                              <1> 
  3578                              <1> 	; 25/05/2020 - Retro UNIX 386 v2
  3579 0000562E A1[B0700000]        <1> 	mov	eax, [i.ctim] ; inode (file) creation time
  3580 00005633 21C0                <1> 	and	eax, eax
  3581 00005635 7518                <1> 	jnz	short setimod_4
  3582                              <1> 
  3583                              <1> 	; New file
  3584                              <1> setimod_1:
  3585                              <1> 	; 25/05/2020 - Retro UNIX 386 v2 by Erdogan Tan
  3586                              <1> 	;	 (Modified UNIX v7 inode and new FS construction) 	
  3587                              <1> 	;
  3588 00005637 E8480C0000          <1> 	call	get_system_time ; get current (unix epoch) time
  3589                              <1> 
  3590                              <1> 	;; Erdogan Tan 14-7-2012
  3591                              <1> 	;call 	epoch
  3592                              <1> 	;	 ; mov s.time,i.mtim 
  3593                              <1> 	;		    ; / put present time into file modified time
  3594                              <1> 	;	 ; mov s.time+2,i.mtim+2
  3595                              <1> 	;
  3596                              <1> 	;mov 	[i.mtim], eax
  3597                              <1> 
  3598                              <1> 	; Retro UNIX 386 v1 modification ! (cmp)
  3599                              <1> 	; Retro UNIX 8086 v1 modification ! (test)
  3600                              <1> 	;cmp	dword [i.ctim], 0
  3601                              <1> 	;jnz	short setimod_ok
  3602                              <1> 		
  3603 0000563C A3[B0700000]        <1> 	mov 	[i.ctim], eax
  3604                              <1> 	
  3605                              <1> 	; 25/05/2020
  3606 00005641 EB05                <1> 	jmp	short setimod_3
  3607                              <1> 
  3608                              <1> setimod_2:
  3609                              <1> 	; File/Directory data (file size or content) is changed
  3610                              <1> 	; This is last modification date&time
  3611 00005643 A3[AC700000]        <1> 	mov	[i.mtim], eax
  3612                              <1> setimod_3:
  3613                              <1> 	; 19/09/2019 ('UNIXHDCP.ASM', 'setimod' by Erdogan Tan)
  3614                              <1> 	; This is last access or last changing date&time
  3615                              <1> 	; of inode (chmod,chown,chgrp,link)
  3616                              <1> 	; parameters (without file/dir data changing)
  3617 00005648 A3[A8700000]        <1> 	mov	[i.atim], eax
  3618                              <1> 
  3619                              <1> setimod_ok: ; 31/07/2013
  3620 0000564D 58                  <1> 	pop	eax
  3621                              <1> 	;pop	edx
  3622                              <1> 	
  3623 0000564E C3                  <1> 	retn
  3624                              <1> 		; rts r0
  3625                              <1> setimod_4:
  3626                              <1> 	; 25/05/2020 - Retro UNIX 386 v2
  3627 0000564F 31C9                <1> 	xor	ecx, ecx
  3628 00005651 390D[AC700000]      <1> 	cmp	[i.mtim], ecx ; 0
  3629 00005657 76EA                <1> 	jna	short setimod_2
  3630                              <1> 
  3631 00005659 E8260C0000          <1> 	call	get_system_time ; get current (unix epoch) time
  3632                              <1> 	; ((system time will be updated directly by timer interrupt))
  3633                              <1> 
  3634 0000565E 803D[CB740000]00    <1> 	cmp	byte [imodx], 0 ; flag means "file/dir data is same but
  3635 00005665 76DC                <1> 	jna	short setimod_2 ; inode has been changed"
  3636                              <1> 
  3637                              <1> 	; File/Dir data (File size or content) is same but
  3638                              <1> 	; inode's mode, link count, owner or group id has been changed
  3639                              <1> 	; (so, we do not change last modification date&time)
  3640 00005667 C605[CB740000]00    <1> 	mov	byte [imodx], 0 ; reset inode modified (extended) flag
  3641 0000566E EBD8                <1> 	jmp	short setimod_3
  3642                              <1> 
  3643                              <1> ; 10/01/2022 - Retro UNIX 386 v1.2
  3644                              <1> %if 0
  3645                              <1> 	; Retro UNIX 386 v1.1 'itrunc' code
  3646                              <1> itrunc:
  3647                              <1> 	; 03/06/2015 (Retro UNIX 386 v1 - Beginning)
  3648                              <1> 	; 23/04/2013 - 01/08/2013 (Retro UNIX 8086 v1)
  3649                              <1> 	;
  3650                              <1> 	; 'itrunc' truncates a file whose i-number is given in r1
  3651                              <1> 	;  to zero length.
  3652                              <1> 	;
  3653                              <1> 	; INPUTS ->
  3654                              <1> 	;    r1 - i-number of i-node
  3655                              <1> 	;    i.dskp - pointer to contents or indirect block in an i-node
  3656                              <1> 	;    i.flgs - large file flag		
  3657                              <1> 	;    i.size - size of file	
  3658                              <1> 	; 	 
  3659                              <1> 	; OUTPUTS ->
  3660                              <1> 	;    i.flgs - large file flag is cleared
  3661                              <1> 	;    i.size - set to 0	
  3662                              <1> 	;    i.dskp .. i.dskp+16 - entire list is cleared
  3663                              <1> 	;    setimod - set to indicate i-node has been modified
  3664                              <1> 	;    r1 - i-number of i-node  					
  3665                              <1> 	;
  3666                              <1> 	; ((AX = R1)) input/output
  3667                              <1> 	;
  3668                              <1> 	;  (Retro UNIX Prototype : 01/12/2012 - 10/03/2013, UNIXCOPY.ASM)
  3669                              <1>         ;  ((Modified registers: eDX, eCX, eBX, eSI, eDI, eBP))  
  3670                              <1> 
  3671                              <1> 	call	iget
  3672                              <1> 		; jsr r0,iget
  3673                              <1> 	mov	esi, i.dskp
  3674                              <1> 		; mov $i.dskp,r2 / address of block pointers in r2
  3675                              <1> 	xor	eax, eax
  3676                              <1> itrunc_1: ; 1:
  3677                              <1> 	lodsw
  3678                              <1> 		; mov (r2)+,r1 / move physical block number into r1
  3679                              <1> 	or 	ax, ax
  3680                              <1> 	jz	short itrunc_5
  3681                              <1> 		; beq 5f
  3682                              <1> 	push	esi
  3683                              <1> 		; mov r2,-(sp)
  3684                              <1> 	test    word [i.flgs], 1000h    
  3685                              <1> 		; bit $10000,i.flgs / test large file bit?
  3686                              <1> 	jz	short itrunc_4
  3687                              <1> 		; beq 4f / if clear, branch
  3688                              <1> 	push	eax
  3689                              <1> 		; mov r1,-(sp) / save block number of indirect block
  3690                              <1> 	call	dskrd
  3691                              <1> 		; jsr r0,dskrd / read in block, 1st data word 
  3692                              <1> 			     ; / pointed to by r5
  3693                              <1> 	; eBX = r5 = Buffer data address (the 1st word)
  3694                              <1> 	mov	ecx, 256
  3695                              <1> 		; mov $256.,r3 / move word count into r3
  3696                              <1> 	mov	esi, ebx
  3697                              <1> itrunc_2: ; 2:
  3698                              <1> 	lodsw
  3699                              <1> 		; mov (r5)+,r1 / put 1st data word in r1; 
  3700                              <1> 			     ; / physical block number
  3701                              <1> 	and	ax, ax
  3702                              <1> 	jz	short itrunc_3
  3703                              <1> 		; beq 3f / branch if zero
  3704                              <1> 	;push	ecx
  3705                              <1> 	push	cx
  3706                              <1> 		; mov r3,-(sp) / save r3, r5 on stack
  3707                              <1> 	;push	esi
  3708                              <1> 		; mov r5,-(sp)
  3709                              <1> 	call	free
  3710                              <1> 		; jsr r0,free / free block in free storage map
  3711                              <1> 	;pop	esi
  3712                              <1> 		; mov(sp)+,r5
  3713                              <1> 	pop	cx
  3714                              <1> 	;pop	ecx
  3715                              <1> 		; mov (sp)+,r3
  3716                              <1> itrunc_3: ; 3:
  3717                              <1> 	loop	itrunc_2
  3718                              <1> 		; dec r3 / decrement word count
  3719                              <1> 		; bgt 2b / branch if positive
  3720                              <1> 	pop	eax
  3721                              <1> 		; mov (sp)+,r1 / put physical block number of 
  3722                              <1> 			     ; / indirect block
  3723                              <1> 	; 01/08/2013
  3724                              <1>         and     word [i.flgs], 0EFFFh ; 1110111111111111b
  3725                              <1> itrunc_4: ; 4:
  3726                              <1> 	call	free
  3727                              <1> 		; jsr r0,free / free indirect block
  3728                              <1> 	pop	esi
  3729                              <1> 		; mov (sp)+,r2
  3730                              <1> itrunc_5: ; 5:
  3731                              <1> 	cmp	esi, i.dskp+16
  3732                              <1> 		; cmp r2,$i.dskp+16.
  3733                              <1> 	jb	short itrunc_1	
  3734                              <1> 		; bne 1b / branch until all i.dskp entries check
  3735                              <1> 	; 01/08/2013
  3736                              <1> 	;and     word [i.flgs], 0EFFFh ; 1110111111111111b
  3737                              <1> 		; bic $10000,i.flgs / clear large file bit
  3738                              <1> 	mov	edi, i.dskp
  3739                              <1> 	mov	cx, 8
  3740                              <1> 	xor 	ax, ax
  3741                              <1> 	mov	[i.size], ax ; 0
  3742                              <1> 		; clr i.size / zero file size
  3743                              <1> 	rep	stosw
  3744                              <1> 		; jsr r0,copyz; i.dskp; i.dskp+16. 
  3745                              <1> 			   ; / zero block pointers
  3746                              <1> 	call	setimod
  3747                              <1> 		; jsr r0,setimod / set i-node modified flag
  3748                              <1> 	mov	ax, [ii]
  3749                              <1> 		; mov ii,r1
  3750                              <1> 	retn
  3751                              <1> 		; rts r0
  3752                              <1> 
  3753                              <1> 	; Retro UNIX 386 v1.1 'imap' code
  3754                              <1> imap:
  3755                              <1> 	; 03/06/2015 (Retro UNIX 386 v1 - Beginning)
  3756                              <1> 	; 26/04/2013 (Retro UNIX 8086 v1)
  3757                              <1> 	;
  3758                              <1> 	; 'imap' finds the byte in core (superblock) containing
  3759                              <1> 	; allocation bit for an i-node whose number in r1.
  3760                              <1> 	;
  3761                              <1> 	; INPUTS ->
  3762                              <1> 	;    r1 - contains an i-number
  3763                              <1> 	;    fsp - start of table containing open files
  3764                              <1> 	;
  3765                              <1> 	; OUTPUTS ->
  3766                              <1> 	;    r2 - byte address of byte with the allocation bit
  3767                              <1> 	;    mq - a mask to locate the bit position.	
  3768                              <1> 	;	  (a 1 is in calculated bit posisiton)
  3769                              <1> 	;
  3770                              <1> 	; ((AX = R1)) input/output
  3771                              <1> 	; ((DL/DX = MQ)) output
  3772                              <1> 	; ((BX = R2)) output
  3773                              <1> 	;
  3774                              <1> 	;    (Retro UNIX Prototype : 02/12/2012, UNIXCOPY.ASM)
  3775                              <1>         ;    ((Modified registers: eDX, eCX, eBX, eSI))  
  3776                              <1> 	;
  3777                              <1> 		; / get the byte that has the allocation bit for 
  3778                              <1> 		; / the i-number contained in r1
  3779                              <1> 	;mov	dx, 1
  3780                              <1> 	mov	dl, 1
  3781                              <1> 		; mov $1,mq / put 1 in the mq
  3782                              <1> 	movzx	ebx, ax
  3783                              <1> 		; mov r1,r2 / r2 now has i-number whose byte
  3784                              <1>  		          ; / in the map we must find
  3785                              <1> 	sub	bx, 41
  3786                              <1> 		; sub $41.,r2 / r2 has i-41
  3787                              <1> 	mov	cl, bl
  3788                              <1> 		; mov r2,r3 / r3 has i-41
  3789                              <1> 	and	cl, 7
  3790                              <1> 		; bic $!7,r3 / r3 has (i-41) mod 8 to get 
  3791                              <1> 			   ; / the bit position
  3792                              <1> 	jz	short imap1
  3793                              <1> 	;shl	dx, cl
  3794                              <1> 	shl	dl, cl
  3795                              <1> 		; mov r3,lsh / move the 1 over (i-41) mod 8 positions
  3796                              <1> imap1:			   ; / to the left to mask the correct bit
  3797                              <1> 	shr	bx, 3
  3798                              <1> 		; asr r2
  3799                              <1> 		; asr r2
  3800                              <1> 		; asr r2 / r2 has (i-41) base 8 of the byte number
  3801                              <1> 		       ; / from the start of the map
  3802                              <1> 		; mov r2,-(sp) / put (i-41) base 8 on the stack
  3803                              <1> 	mov	esi, systm
  3804                              <1> 		; mov $systm,r2 / r2 points to the in-core image of
  3805                              <1> 				; / the super block for drum
  3806                              <1> 	;cmp	word [cdev], 0
  3807                              <1> 	cmp	byte [cdev], 0
  3808                              <1> 		; tst cdev / is the device the disk
  3809                              <1> 	jna	short imap2
  3810                              <1> 		; beq 1f / yes
  3811                              <1> 	add	esi, mount - systm
  3812                              <1> 		; add $mount-systm,r2 / for mounted device,
  3813                              <1> 			; / r2 points to 1st word of its super block
  3814                              <1> imap2: ; 1:
  3815                              <1> 	add	bx, [esi] ;; add free map size to si
  3816                              <1> 		; add (r2)+,(sp) / get byte address of allocation bit
  3817                              <1> 	add	bx, 4
  3818                              <1> 	add	ebx, esi
  3819                              <1>         	; add (sp)+,r2 / ?
  3820                              <1> 	;add	ebx, 4 ;; inode map offset in superblock
  3821                              <1> 		      ;; (2 + free map size + 2)
  3822                              <1> 		; add $2,r2 / ?
  3823                              <1>  	; DL/DX (MQ) has a 1 in the calculated bit position
  3824                              <1>         ; BX (R2) has byte address of the byte with allocation bit
  3825                              <1> 	retn
  3826                              <1> 		; rts r0
  3827                              <1> %endif
  3828                              <1> 
  3829                              <1> 	; Retro UNIX 386 v2.0 'itrunc' code
  3830                              <1> itrunc:
  3831                              <1> 	; 10/01/2022 - Retro UNIX 386 v1.2
  3832                              <1> 	; 07/06/2020
  3833                              <1> 	; 02/06/2020
  3834                              <1> 	; 26/05/2020 - Retro UNIX 386 v2
  3835                              <1> 	;
  3836                              <1> 	; 'itrunc' truncates a file whose i-number is given in r1
  3837                              <1> 	;  to zero length.
  3838                              <1> 	; 07/06/2020
  3839                              <1> 	; 02/06/2020
  3840                              <1> 	; 26/05/2020 - Retro UNIX 386 v2
  3841                              <1> 	;
  3842                              <1> 	; 'itrunc' truncates a file whose i-number is given
  3843                              <1> 	; in ax to zero length.
  3844                              <1> 	;
  3845                              <1> 
  3846                              <1> 	; Reference: 
  3847                              <1> 	;	Retro UNIX 386 v2 FS installation utility
  3848                              <1> 	; 17/01/2020 - 'UNIXHDCP.ASM', 'itrunc' procedure
  3849                              <1> 
  3850                              <1> 	; * Free all the disk blocks associated
  3851                              <1> 	; * with the specified inode structure.
  3852                              <1> 	
  3853                              <1> 	; INPUT: 
  3854                              <1> 	;	ax = inode number
  3855                              <1> 	; OUTPUT: 
  3856                              <1> 	;	inode will be modified (file size = 0)
  3857                              <1> 
  3858                              <1> 	; (Modified registers: eax, edx, ecx, ebx, esi, edi, ebp) 
  3859                              <1> 
  3860                              <1> 	; 07/06/2020
  3861                              <1> 
  3862                              <1> 	; [cdev] = current logical drive number
  3863                              <1> 	;    eax = inode number (for [cdev])
  3864                              <1> 
  3865 00005670 E812FEFFFF          <1> 	call 	iget
  3866                              <1> 	; 10/01/2022
  3867                              <1> 	;jc	short itrunc_0 ; error code in eax (al)
  3868                              <1> 
  3869                              <1> 	;    eax = inode number, [ii]
  3870                              <1> 	; [cdev] = current logical drive number (same with input)
  3871                              <1> 	; [idev] = logical drive number of current inode, [ii]
  3872                              <1> 	;   [ii] = current inode ([ii] = ax input, if ax was not zero)
  3873                              <1> 	; [imod] = 0		
  3874                              <1> 	; ([imodx] = 0)
  3875                              <1> 
  3876 00005675 F605[75700000]80    <1> 	test	byte [i.flgs+1], 80h ; regular file or directory	
  3877 0000567C 750F                <1> 	jnz	short itrunc_1
  3878                              <1> 
  3879                              <1> 	; 10/01/2022
  3880 0000567E C705[4C750000]FF00- <1> 	mov	dword [u.error], ERR_NOT_REGULAR ; 255 
  3880 00005686 0000                <1>
  3881                              <1> 			; 'not a regular file (or directory) !' error
  3882                              <1> itrunc_0:
  3883 00005688 E91DE5FFFF          <1> 	jmp	error
  3884                              <1> itrunc_1:
  3885 0000568D F605[75700000]10    <1> 	test	byte [i.flgs+1], 10h ; large file (indirect blocks)
  3886 00005694 7528                <1> 	jnz	short itrunc_5 ; large file
  3887 00005696 BE[80700000]        <1> 	mov	esi, i.dskp ; disk address pointer 0
  3888                              <1> itrunc_2:
  3889 0000569B AD                  <1> 	lodsd
  3890 0000569C 21C0                <1> 	and	eax, eax
  3891 0000569E 740C                <1> 	jz	short itrunc_3 ; empty ! (no more sectors/blocks)
  3892                              <1> 	; eax = block address to be released
  3893 000056A0 56                  <1> 	push	esi ; * ; 10/01/2022
  3894 000056A1 E83FFDFFFF          <1> 	call	free
  3895                              <1> 	; modified registers: ebx, ecx, edx, esi, edi, ebp 
  3896                              <1> 	; 10/01/2022
  3897                              <1> 	;jc	short itrunc_0 ; error code in eax
  3898 000056A6 5E                  <1> 	pop	esi ; * ; 10/01/2022
  3899 000056A7 31C0                <1> 	xor	eax, eax ; 0
  3900 000056A9 8946FC              <1> 	mov	[esi-4], eax ; 0
  3901                              <1> itrunc_3:
  3902 000056AC 81FE[A8700000]      <1> 	cmp	esi, i.dskp+40
  3903 000056B2 72E7                <1> 	jb	short itrunc_2 ; next disk address ptr
  3904                              <1> itrunc_4:
  3905 000056B4 A3[7C700000]        <1> 	mov	[i.size], eax ; 0
  3906                              <1> 	;mov	[i.size_h], al ; 0
  3907                              <1> 	
  3908                              <1> 	; clear large file flag
  3909                              <1> 	;and     byte [i.flgs+1], 0EFh ; 11101111b ;  not 10h
  3910                              <1> 		
  3911                              <1> 	;call	setimod
  3912                              <1> 	;retn
  3913 000056B9 E968FFFFFF          <1> 	jmp	setimod
  3914                              <1> 
  3915                              <1> itrunc_5:
  3916                              <1> 	; free disk blocks by using triple indirect blocks at first
  3917 000056BE BE[A4700000]        <1> 	mov	esi, i.dskp+36
  3918 000056C3 8B06                <1> 	mov	eax, [esi]
  3919 000056C5 09C0                <1> 	or	eax, eax
  3920 000056C7 740C                <1> 	jz	short itrunc_6
  3921                              <1> 
  3922 000056C9 BA02000000          <1> 	mov	edx, 2 ; Triple indirect sign (level = 2)
  3923 000056CE E835000000          <1> 	call	tloop
  3924                              <1> 	; 10/01/2022
  3925                              <1> 	;jc	short itrunc_0
  3926                              <1> 		; eax = 0	
  3927 000056D3 8906                <1> 	mov	[esi], eax ; 0
  3928                              <1> itrunc_6:
  3929                              <1> 	; free disk blocks by using double indirect blocks at second
  3930 000056D5 BE[A0700000]        <1> 	mov	esi, i.dskp+32
  3931 000056DA 8B06                <1> 	mov	eax, [esi]
  3932 000056DC 21C0                <1> 	and	eax, eax 
  3933 000056DE 740C                <1> 	jz	short itrunc_7
  3934                              <1> 
  3935 000056E0 BA01000000          <1> 	mov	edx, 1 ; Double indirect sign (level = 1)
  3936 000056E5 E81E000000          <1> 	call	tloop
  3937                              <1> 	; 10/01/2022
  3938                              <1> 	;jc	short itrunc_0
  3939                              <1> 		; eax = 0	
  3940 000056EA 8906                <1> 	mov	[esi], eax ; 0
  3941                              <1> 	;esi = i.dskp+32
  3942                              <1> itrunc_7:
  3943                              <1> 	; free disk blocks by using single indirect blocks at third
  3944 000056EC 83EE04              <1> 	sub	esi, 4
  3945 000056EF 8B06                <1> 	mov	eax, [esi]
  3946 000056F1 09C0                <1> 	or	eax, eax
  3947 000056F3 7409                <1> 	jz	short itrunc_8
  3948                              <1> 
  3949 000056F5 29D2                <1> 	sub	edx, edx ; 0  ; Single indirect sign (level = 0)
  3950 000056F7 E80C000000          <1> 	call	tloop
  3951                              <1> 	; 10/01/2022
  3952                              <1> 	;jc	short itrunc_0
  3953                              <1> 		; eax = 0
  3954 000056FC 8906                <1> 	mov	[esi], eax ; 0
  3955                              <1> itrunc_8:
  3956 000056FE 81FE[80700000]      <1> 	cmp	esi, i.dskp
  3957 00005704 77E6                <1> 	ja	short itrunc_7
  3958 00005706 EBAC                <1> 	jmp	short itrunc_4
  3959                              <1> 
  3960                              <1> tloop:
  3961                              <1> 	; 10/01/2022 - Retro UNIX 386 v1.2
  3962                              <1> 	; 07/06/2020
  3963                              <1> 	; 03/06/2020
  3964                              <1> 	; 02/06/2020
  3965                              <1> 	; 26/05/2020 - Retro UNIX 386 v2 by Erdogan Tan
  3966                              <1> 	; 
  3967                              <1> 	; * Free all the disk blocks associated
  3968                              <1> 	; * with the specified inode structure.
  3969                              <1> 
  3970                              <1> 	; Reference: 
  3971                              <1> 	;	Retro UNIX 386 v2 FS installation utility
  3972                              <1> 	; 17/01/2020 - 'UNIXHDCP.ASM', 'tloop' procedure
  3973                              <1> 
  3974                              <1> 	; INPUT: 
  3975                              <1> 	;	03/06/2020
  3976                              <1> 	;	eax = indirect block number/address (logical)
  3977                              <1> 	;	edx = level (in dl) 
  3978                              <1> 	;	(0 = indirect, 1 = double indr. 2 = triple indr.) 	
  3979                              <1> 	;
  3980                              <1> 	; OUTPUT: 
  3981                              <1> 	;	indirect blocks will be released
  3982                              <1> 	;
  3983                              <1> 	; Modified registers: eax, ebx, ecx, edx
  3984                              <1> 
  3985                              <1> 	; 03/06/2020
  3986                              <1> 
  3987 00005708 57                  <1> 	push	edi ; *
  3988 00005709 56                  <1> 	push	esi ; **
  3989                              <1> 
  3990 0000570A 50                  <1> 	push	eax ; ***
  3991                              <1> 
  3992 0000570B 52                  <1> 	push	edx ; **** level
  3993                              <1> 
  3994                              <1> 	; [ii] = current inode number
  3995                              <1> 	; [idev] = logical drive number of current inode, [ii]
  3996                              <1> 	; eax = logical sector/block number
  3997                              <1> 
  3998                              <1> 	; 10/01/2022
  3999                              <1> 	; convert to physical sector/block number/address
  4000 0000570C E859FAFFFF          <1> 	call	mget_2	
  4001                              <1> 
  4002 00005711 E83B0A0000          <1> 	call	dskrd ; read disk sector
  4003                              <1> 
  4004                              <1> 		;; ebx = buffer header address
  4005                              <1> 		;; eax = physical sector/block number 
  4006                              <1> 		;; [pdn] = phsysical drive (index) number
  4007                              <1> 		
  4008                              <1> 		;; Modified registers: eax, edx, ecx, esi, edi
  4009                              <1> 		;; If cf = 1 --> eax = error code (al)
  4010                              <1> 
  4011                              <1> 	; 10/01/2022
  4012                              <1> 	; Return from 'dskrd': (Retro UNIX 386 v1.2)
  4013                              <1> 	; 	eax = physical block/sector number
  4014                              <1> 	; 	ebx = buffer data address
  4015                              <1>  	; Modified registers: edx, ecx, ebx, esi, edi, ebp
  4016                              <1>  
  4017 00005716 5A                  <1> 	pop	edx ; **** ; level in dl
  4018                              <1> 	; 10/01/2022
  4019                              <1> 	;jc	short tloop_5
  4020                              <1> 
  4021                              <1> 	;mov	esi, [ebx+bufhdr.address] ; buffer address
  4022                              <1> 	; 10/01/2022
  4023 00005717 89DE                <1> 	mov	esi, ebx ; buffer data address	
  4024                              <1> 
  4025                              <1> 	; 07/06/2020
  4026 00005719 B980000000          <1> 	mov	ecx, 512/4 ; 128 dwords	
  4027                              <1> 
  4028 0000571E 80FA01              <1> 	cmp	dl, 1 ; -
  4029 00005721 722A                <1> 	jb	short tloop_7
  4030 00005723 7407                <1> 	je	short tloop_1
  4031 00005725 BF[8C7B0000]        <1> 	mov	edi, trpi_buf ; triple indirect buffer address
  4032 0000572A EB05                <1> 	jmp	short tloop_2
  4033                              <1> tloop_1:
  4034 0000572C BF[8C790000]        <1> 	mov	edi, dbli_buf ; double indirect buffer address
  4035                              <1> tloop_2:
  4036                              <1> 	; copy disk r/w buffer content to indirect block buffer
  4037                              <1> 	;mov	ecx, 512/4 ; 128 dwords	
  4038 00005731 F3A5                <1> 	rep	movsd
  4039                              <1> 
  4040 00005733 FECA                <1> 	dec	dl ; -
  4041                              <1> 		   ; next indirect block level, 2 -> 1, 1 -> 0
  4042 00005735 89FE                <1> 	mov	esi, edi  ; (dbli, trpi) indirect buffer + 512
  4043 00005737 B180                <1> 	mov	cl, 128 ; 128 dwords
  4044                              <1> tloop_3:
  4045 00005739 83EE04              <1> 	sub	esi, 4  ; previous pointer
  4046                              <1> 
  4047 0000573C 8B06                <1> 	mov	eax, [esi]
  4048                              <1> 
  4049 0000573E 09C0                <1> 	or	eax, eax ; 0
  4050 00005740 7407                <1> 	jz	short tloop_4
  4051                              <1> 
  4052                              <1> 	; 03/06/2020
  4053 00005742 51                  <1> 	push	ecx ; @
  4054 00005743 E8C0FFFFFF          <1> 	call	tloop
  4055 00005748 59                  <1> 	pop	ecx ; @
  4056                              <1> 	; 10/01/2022
  4057                              <1> 	;jc	short tloop_5
  4058                              <1> tloop_4:
  4059 00005749 E2EE                <1> 	loop	tloop_3
  4060                              <1> 
  4061 0000574B EB1A                <1> 	jmp	short tloop_10
  4062                              <1> 
  4063                              <1> 	; 10/01/2022
  4064                              <1> ;tloop_5:
  4065                              <1> ;	pop	edx ; ***  ; discard eax
  4066                              <1> ;	; error code in eax
  4067                              <1> ;tloop_6:
  4068                              <1> ;	pop	esi ; **
  4069                              <1> ;	pop	edi ; *
  4070                              <1> ;	retn
  4071                              <1> 	
  4072                              <1> 	; free blocks in current indirect block
  4073                              <1> tloop_7:
  4074                              <1> 	;mov	ecx, 512/4  ; 128 dwords
  4075 0000574D 81C6FC010000        <1> 	add	esi, 508
  4076                              <1> tloop_8:
  4077 00005753 8B06                <1> 	mov	eax, [esi]
  4078 00005755 21C0                <1> 	and	eax, eax ; 0 ?
  4079 00005757 7409                <1> 	jz	short tloop_9
  4080                              <1> 	
  4081 00005759 56                  <1> 	push	esi ; **** ; 10/01/2022
  4082 0000575A 51                  <1> 	push	ecx ; *****
  4083 0000575B E885FCFFFF          <1> 	call	free
  4084                              <1> 		; if cf = 0 -> eax = 0
  4085                              <1> 		; if cf = 1 -> eax = error code (in al)
  4086 00005760 59                  <1> 	pop	ecx ; *****
  4087 00005761 5E                  <1> 	pop	esi ; **** ; 10/01/2022
  4088                              <1> 	; 10/01/2022
  4089                              <1> 	;; 07/06/2020
  4090                              <1> 	;jc	short tloop_6
  4091                              <1> tloop_9:
  4092 00005762 83EE04              <1> 	sub	esi, 4
  4093 00005765 E2EC                <1> 	loop	tloop_8
  4094                              <1> tloop_10:
  4095 00005767 58                  <1> 	pop	eax ; *** ; free indirect block's itself
  4096 00005768 E878FCFFFF          <1> 	call	free
  4097                              <1> 		;; if cf = 0 -> eax = 0
  4098                              <1> 		;; if cf = 1 -> eax = error code (in al)
  4099                              <1> 	; 10/01/2022
  4100                              <1> 	; (if we are here, there is not an error, cf=0) 
  4101                              <1> 	;jmp	short tloop_6
  4102                              <1> 
  4103                              <1> 	; 10/02/2022
  4104 0000576D 31C0                <1> 	xor	eax, eax ; 0
  4105 0000576F 5E                  <1> 	pop	esi ; **
  4106 00005770 5F                  <1> 	pop	edi ; *
  4107 00005771 C3                  <1> 	retn
  4108                              <1> 
  4109                              <1> 	; Retro UNIX 386 v2.0 'imap' code
  4110                              <1> 	;	(runix v2 fs inode map)
  4111                              <1> imap:
  4112                              <1> 	; 10/01/2022 - Retro UNIX 386 v1.2
  4113                              <1> 	;	(major modification)
  4114                              <1> 	; 05/11/2021 - temporary (simplified code)
  4115                              <1> 	; 21/08/2021
  4116                              <1> 	; 15/08/2021
  4117                              <1> 	; 14/06/2021
  4118                              <1> 	; 02/05/2021
  4119                              <1> 	; 01/04/2021, 08/04/2021, 24/04/2021
  4120                              <1> 	; 29/03/2021
  4121                              <1> 	; 28/03/2021 - Retro UNIX 386 v2 (beginning)
  4122                              <1> 	;
  4123                              <1> 	; 'imap' finds the byte in inode map containing
  4124                              <1> 	; allocation bit for an i-node whose number in (E)AX
  4125                              <1> 	;
  4126                              <1> 	; (ref: 'UNIXHDCP.ASM', imap', 22/01/2020) 
  4127                              <1> 	;
  4128                              <1> 	; ((Retro UNIX 386 v2 'imap' code is mostly different
  4129                              <1> 	; than Retro UNIX 386 v1.1 'imap' code.))
  4130                              <1> 	;
  4131                              <1> 	; INPUTS ->
  4132                              <1> 	;   EAX = inode number
  4133                              <1> 	;        0 = first free inode
  4134                              <1> 	;       >0 = requested inode
  4135                              <1> 	;   [cdev] = current (logical) drive/device
  4136                              <1> 	;	
  4137                              <1> 	; OUTPUTS ->
  4138                              <1> 	;    EBX = address of the byte contains allocation bit
  4139                              <1> 	;     DX has 1 at calculated bit position
  4140                              <1> 	;   ; 05/11/2021	
  4141                              <1> 	;    EBP = superblock buffer address  
  4142                              <1> 	;    (EAX = inode number)	  	
  4143                              <1> 	;   ; 08/04/2021
  4144                              <1> 	;     If cf=1 -> error code in eax (al) 	
  4145                              <1> 	;
  4146                              <1> 	; Modified registers: ebx, ecx, edx, esi, edi, ebp
  4147                              <1> 
  4148                              <1> 	; 05/11/2021
  4149 00005772 803D[B9740000]00    <1> 	cmp	byte [cdev], 0
  4150 00005779 7616                <1> 	jna	short imap_0
  4151                              <1> 	; 10/01/2022
  4152 0000577B BD[8C770000]        <1> 	mov	ebp, mount ; mounted fs superblock buffer
  4153 00005780 EB14                <1> 	jmp	short imap_1
  4154                              <1> 
  4155                              <1> imap_ialloc_err:
  4156                              <1> 	; 10/01/2022
  4157 00005782 C705[4C750000]2100- <1> 	mov	dword [u.error], ERR_INO_ALLOC ; 33
  4157 0000578A 0000                <1>
  4158                              <1> 			; 'inode allocation error !'
  4159 0000578C E919E4FFFF          <1> 	jmp	error
  4160                              <1>  
  4161                              <1> imap_0:
  4162                              <1> 	; 10/01/2022
  4163 00005791 BD[84750000]        <1> 	mov	ebp, systm ; root fs superblock buffer
  4164                              <1> imap_1:
  4165                              <1> 	; 10/01/2022 (Retro UNIX 386 v1.2)
  4166                              <1> imap_x:
  4167                              <1> 	; 05/11/2021
  4168                              <1> 	; 21/08/2021
  4169                              <1> 	; 15/08/2021 (Retro UNIX 386 v2)
  4170                              <1> 	; call from 'maknod2' ; 10/01/2022
  4171                              <1> 	;
  4172                              <1> 	; eax = inode number
  4173                              <1> 	; [cdev] = device number for inode in eax
  4174                              <1> 	; ebp = superblock buffer address
  4175                              <1> 
  4176                              <1> 	; 10/01/2022 (Retro UNIX 386 v1.2)
  4177                              <1> 	; 'imap' and 'imap_x' will return with..
  4178                              <1> 	;    ECX = byte offset from inode map buff (imap_x)
  4179                              <1> 	;    EBX = byte addr of the byte with allocation bit
  4180                              <1> 	;    EDX (DL) has a 1 in the calculated bit position
  4181                              <1> 
  4182                              <1> 	; 02/05/2021
  4183                              <1> 	; convert inode number to first free inode if it is 0
  4184 00005796 E83D000000          <1> 	call	imap_3 ; 10/01/2022
  4185 0000579B 72E5                <1> 	jc	short imap_ialloc_err
  4186                              <1> 	; eax = inode number > 1 (1 = root directory inode)
  4187                              <1> 
  4188 0000579D 898584000000        <1> 	mov	[ebp+SB.LastInode], eax
  4189                              <1> 	
  4190 000057A3 50                  <1> 	push	eax ; * ; save inode number
  4191                              <1> 
  4192                              <1> 	; convert inode number to inode map sector index
  4193 000057A4 E847000000          <1> 	call	imap_8 ; 10/01/2022
  4194 000057A9 50                  <1> 	push	eax ; ** ; byte offset in inode map buffer
  4195                              <1> 
  4196                              <1> 	; edx = inode map buffer sector index number (< 16)
  4197                              <1> 	; eax = byte offset in inode map buffer (< 512)
  4198                              <1> 
  4199                              <1> 	; 15/08/2021
  4200 000057AA 89958C000000        <1> 	mov	[ebp+SB.ImapIndex], edx
  4201 000057B0 8B4520              <1> 	mov	eax, [ebp+SB.InodeMapAddr]
  4202 000057B3 01D0                <1> 	add	eax, edx
  4203 000057B5 034504              <1> 	add	eax, [ebp+SB.BootSectAddr]
  4204                              <1> 	; eax = physical sector number
  4205                              <1> 	; [cdev] = current device/disk number
  4206                              <1> 	
  4207                              <1> 	; 10/01/2022 (Retro UNIX 386 v1.2)
  4208 000057B8 55                  <1> 	push	ebp ; ***
  4209 000057B9 E893090000          <1> 	call	dskrd
  4210 000057BE 5D                  <1> 	pop	ebp ; ***
  4211                              <1> 	; cpu returns here if there is/was not an error in 'dskrd'
  4212                              <1> 	; eax = physical sector number
  4213                              <1> 	; ebx = buffer data address
  4214                              <1> 
  4215                              <1> 	; 10/01/2022
  4216 000057BF 8A4C2404            <1> 	mov	cl, [esp+4] ; * ; inode number
  4217                              <1> 	;dec	cl
  4218 000057C3 31D2                <1> 	xor	edx, edx
  4219                              <1> 	;mov	dl, 1
  4220 000057C5 FEC2                <1> 	inc	dl ; dl = 1
  4221                              <1> 	;and	ecx, 7
  4222                              <1> 	; 02/05/2021
  4223 000057C7 FEC9                <1> 	dec	cl
  4224 000057C9 7405                <1> 	jz	short imap_2 ; 15/08/2021
  4225 000057CB 80E107              <1> 	and	cl, 7	; 1 imap bytes is for 8 inodes 
  4226 000057CE D2E2                <1> 	shl	dl, cl
  4227                              <1> imap_2:
  4228                              <1> 	; 10/01/2022
  4229                              <1> 	; save physical sector number of imap sector in the SB
  4230 000057D0 89457C              <1> 	mov	[ebp+SB.ImapBuffer], eax ; ! sector addr !
  4231                              <1> 	; 
  4232 000057D3 59                  <1> 	pop	ecx ; ** ; byte offset in inode map buffer
  4233 000057D4 01CB                <1> 	add	ebx, ecx ; + byte position
  4234 000057D6 58                  <1> 	pop	eax ; * ; inode number
  4235                              <1> 
  4236                              <1> 	; 10/01/2022
  4237                              <1> 	; ECX contains byte offset from inode map buff (imap_x)
  4238                              <1> 	; EDX (DL) has a 1 in the calculated bit position
  4239                              <1> 	; EBX has byte address of the byte with allocation bit
  4240                              <1> 
  4241 000057D7 C3                  <1> 	retn
  4242                              <1> 
  4243                              <1> imap_3:
  4244                              <1> 	; 10/01/2022
  4245                              <1> 	; 15/08/2021
  4246                              <1> 	; 02/05/2021
  4247                              <1> 	; 28/03/2021
  4248                              <1> 	; get first free inode number (from superblock)
  4249                              <1> 	; and check inode number against inode count
  4250                              <1> 	;
  4251                              <1> 	; INPUT:
  4252                              <1> 	;    eax = 0 -> get first free inode number
  4253                              <1> 	;    (eax > 0 -> check inode number)
  4254                              <1> 	;    ebp = superblock address ; 15/08/2021
  4255                              <1> 	; OUTPUT:
  4256                              <1> 	;    eax = inode number 
  4257                              <1> 	;    (eax will be 2 when SB.FirstFreeIno is invalid)
  4258                              <1> 	;    cf = 1 -> inode allocation error/problem
  4259                              <1> 	
  4260 000057D8 21C0                <1> 	and	eax, eax	
  4261 000057DA 750E                <1> 	jnz	short imap_5
  4262                              <1> 
  4263                              <1> 	; get first free inode
  4264                              <1> 	;mov	eax, [ebp+SB.FreeInodes]
  4265                              <1> 	;and	eax, eax
  4266                              <1> 	;jz	short imap_6
  4267                              <1> 	; 10/01/2022
  4268 000057DC 394530              <1> 	cmp	[ebp+SB.FreeInodes], eax ; 0
  4269 000057DF 760D                <1> 	jna	short imap_6	
  4270 000057E1 8B4534              <1> 	mov	eax, [ebp+SB.FirstFreeIno] ; 1 based inode num
  4271 000057E4 40                  <1> 	inc	eax
  4272 000057E5 7502                <1> 	jnz	short imap_4 ; 0FFFFFFFFh -> 0
  4273                              <1> 	; invalid first free inode value (not initialized)
  4274 000057E7 B003                <1> 	mov	al, 3 ; first free inode to be searched + 1
  4275                              <1> imap_4:
  4276 000057E9 48                  <1> 	dec	eax
  4277                              <1> imap_5:
  4278                              <1> 	; 15/08/2021
  4279                              <1> 	; check inode number against inode count of the fs
  4280 000057EA 394514              <1> 	cmp	[ebp+SB.InodeCount], eax
  4281 000057ED C3                  <1> 	retn	; if cf = 1 --> inode number > inode count
  4282                              <1> imap_6:
  4283                              <1> 	; 'imap' inode allocation error !
  4284 000057EE F9                  <1> 	stc
  4285                              <1> imap_7:
  4286 000057EF C3                  <1> 	retn
  4287                              <1> 
  4288                              <1> imap_8:
  4289                              <1> 	; 10/01/2022
  4290                              <1> 	; 15/08/2021
  4291                              <1> 	; 02/05/2021
  4292                              <1> 	; 28/03/2021
  4293                              <1> 	; convert inode count to inode map sector index
  4294                              <1> 	;
  4295                              <1> 	; INPUT: 
  4296                              <1> 	;    eax = inode number
  4297                              <1> 	; OUTPUT:
  4298                              <1> 	;    edx = inode map sector offset/index number
  4299                              <1> 	;    eax = byte offset from start of imapbuf	 
  4300                              <1> 
  4301 000057F0 31D2                <1> 	xor	edx, edx
  4302 000057F2 48                  <1> 	dec	eax ; zero based inode number
  4303 000057F3 74FA                <1> 	jz	short imap_7
  4304                              <1> 
  4305 000057F5 C1E803              <1> 	shr	eax, 3 ; 8 inodes per inode map byte
  4306                              <1> 		; eax = byte offset from start of inode map
  4307 000057F8 88E2                <1> 	mov	dl, ah ; ah <= 31 (1Fh) for Retro UNIX 386 v2
  4308 000057FA D0EA                <1> 	shr	dl, 1
  4309                              <1> 		; edx = inode map sector offset/index number
  4310                              <1> 	;and	eax, 511
  4311 000057FC 6625FF01            <1> 	and	ax, 511
  4312                              <1> 		; eax = byte offset from start of imapbuf
  4313 00005800 C3                  <1> 	retn
  2075                                  %include 'u6.s'      ; 31/05/2015
  2076                              <1> ; Temporary Runix kernel v2.0 file for debug - 22/11/2021
  2077                              <1> ; (re-write kernel for test by using previous version without a major defect)
  2078                              <1> ; ****************************************************************************
  2079                              <1> ; Retro UNIX 386 v1 Kernel (v0.2) - SYS6.INC
  2080                              <1> ; Last Modification: 11/01/2022
  2081                              <1> ; ----------------------------------------------------------------------------
  2082                              <1> ; Derived from 'Retro UNIX 8086 v1' source code by Erdogan Tan
  2083                              <1> ; (v0.1 - Beginning: 11/07/2012)
  2084                              <1> ;
  2085                              <1> ; Derived from UNIX Operating System (v1.0 for PDP-11) 
  2086                              <1> ; (Original) Source Code by Ken Thompson (1971-1972)
  2087                              <1> ; <Bell Laboratories (17/3/1972)>
  2088                              <1> ; <Preliminary Release of UNIX Implementation Document>
  2089                              <1> ;
  2090                              <1> ; Retro UNIX 8086 v1 - U6.ASM (23/07/2014) //// UNIX v1 -> u6.s
  2091                              <1> ;
  2092                              <1> ; ****************************************************************************
  2093                              <1> 
  2094                              <1> 	; 25/12/2021
  2095                              <1> 	; 28/11/2021 - Retro UNIX 386 v2 fs compatibility modification
  2096                              <1> readi:
  2097                              <1> 	; 20/05/2015
  2098                              <1> 	; 19/05/2015 (Retro UNIX 386 v1 - Beginning)
  2099                              <1> 	; 11/03/2013 - 31/07/2013 (Retro UNIX 8086 v1)
  2100                              <1> 	;
  2101                              <1> 	; Reads from an inode whose number in R1
  2102                              <1> 	; 
  2103                              <1> 	; INPUTS ->
  2104                              <1> 	;    r1 - inode number
  2105                              <1> 	;    u.count - byte count user desires
  2106                              <1> 	;    u.base - points to user buffer
  2107                              <1> 	;    u.fofp - points to word with current file offset
  2108                              <1> 	; OUTPUTS ->
  2109                              <1> 	;    u.count - cleared
  2110                              <1> 	;    u.nread - accumulates total bytes passed back
  2111                              <1> 	;
  2112                              <1> 	; ((AX = R1)) input/output
  2113                              <1> 	;    (Retro UNIX Prototype : 01/03/2013 - 14/12/2012, UNIXCOPY.ASM)
  2114                              <1>         ;    ((Modified registers: edx, ebx, ecx, esi, esi, ebp))  
  2115                              <1> 
  2116 00005801 31D2                <1> 	xor	edx, edx ; 0
  2117 00005803 8915[04750000]      <1> 	mov 	[u.nread], edx ; 0
  2118                              <1> 		; clr u.nread / accumulates number of bytes transmitted
  2119 00005809 668915[44750000]    <1> 	mov	[u.pcount], dx ; 19/05/2015
  2120 00005810 3915[00750000]      <1> 	cmp 	[u.count], edx ; 0
  2121                              <1> 		; tst u.count / is number of bytes to be read greater than 0
  2122 00005816 7701                <1> 	ja 	short readi_1 ; 1f
  2123                              <1> 		; bgt 1f / yes, branch
  2124 00005818 C3                  <1> 	retn
  2125                              <1> 		; rts r0 / no, nothing to read; return to caller
  2126                              <1> readi_1: ; 1:
  2127                              <1> 	;	; mov r1,-(sp) / save i-number on stack
  2128                              <1> 	;cmp	ax, 40
  2129                              <1> 	;	; cmp r1,$40. / want to read a special file 
  2130                              <1> 	;	;             / (i-nodes 1,...,40 are for special files)
  2131                              <1>         ;ja	dskr 
  2132                              <1> 	;	; ble 1f / yes, branch
  2133                              <1> 	;	; jmp dskr / no, jmp to dskr; 
  2134                              <1> 	;	;         / read file with i-node number (r1)
  2135                              <1> 	;	;    / starting at byte ((u.fofp)), read in u.count bytes
  2136                              <1> 	
  2137                              <1> 	; 28/11/2021
  2138 00005819 F605[4A750000]FF    <1> 	test	byte [u.kcall], 0FFh
  2139 00005820 7505                <1> 	jnz	short readi_2  ; 'readi' called by 'namei'
  2140                              <1> 
  2141                              <1> 	; eax = inode number
  2142                              <1> 	; [cdev] = device number (0 = root fs, 1 = mounted fs)
  2143 00005822 E860FCFFFF          <1> 	call	iget
  2144                              <1> readi_2:
  2145                              <1> 	; 28/11/2021
  2146                              <1> 	; eax = inode number
  2147                              <1> 	; [cdev] = device number (0 = root fs, 1 = mounted fs)
  2148 00005827 E881040000          <1> 	call	is_regular_file
  2149                              <1> 	;jz	dskr ; regular file 
  2150                              <1> 	; 25/12/2021
  2151 0000582C 7505                <1> 	jnz	short readi_5 ; device file 
  2152                              <1> 	; regular file
  2153 0000582E E9E3000000          <1> 	jmp	dskr
  2154                              <1> readi_5:
  2155                              <1> 	; (20/05/2015)
  2156 00005833 50                  <1> 	push	eax ; because subroutines will jump to 'ret_'
  2157                              <1> 
  2158                              <1> 	; 28/11/2021	
  2159                              <1> 	; device file
  2160 00005834 BB[4E580000]        <1> 	mov	ebx, readi_4
  2161 00005839 83F808              <1> 	cmp	eax, 8	; /dev/tty inode number is 8
  2162 0000583C 720E                <1> 	jb	short readi_3
  2163 0000583E 83F81A              <1> 	cmp	eax, 26	; /dev/tty9 (/dev/com2) inode number is 26
  2164 00005841 7709                <1> 	ja	short readi_3	
  2165                              <1> 	
  2166                              <1> 	; convert v2 inode number to v1 device inode number
  2167 00005843 2C07                <1> 	sub	al, 7 ; 8 -> 1, 26 -> 19
  2168 00005845 89C1                <1> 	mov	ecx, eax
  2169 00005847 C0E102              <1> 	shl	cl, 2 ; * 4
  2170 0000584A 01CB                <1> 	add	ebx, ecx
  2171                              <1> readi_3:
  2172 0000584C FF23                <1> 	jmp	dword [ebx]	
  2173                              <1> 		 ; jmp *1f-2(r1)
  2174                              <1> readi_4: ; 1:
  2175 0000584E [9E580000]          <1> 	dd	null ; 28/11/2021
  2176 00005852 [A2580000]          <1> 	dd	rtty ; tty, AX = 1 (runix)
  2177                              <1> 		 ;rtty / tty; r1=2
  2178                              <1> 		 ;rppt / ppt; r1=4
  2179 00005856 [F2580000]          <1> 	dd	rmem ; mem, AX = 2 (runix)
  2180                              <1> 		 ;rmem / mem; r1=6
  2181                              <1> 		 ;rrf0 / rf0
  2182                              <1> 		 ;rrk0 / rk0
  2183                              <1> 		 ;rtap / tap0
  2184                              <1> 		 ;rtap / tap1
  2185                              <1> 		 ;rtap / tap2
  2186                              <1> 		 ;rtap / tap3
  2187                              <1> 		 ;rtap / tap4
  2188                              <1> 		 ;rtap / tap5
  2189                              <1> 		 ;rtap / tap6
  2190                              <1> 		 ;rtap / tap7
  2191 0000585A [F75F0000]          <1> 	dd	rfd ; fd0, AX = 3 (runix only)
  2192 0000585E [F75F0000]          <1> 	dd	rfd ; fd1, AX = 4 (runix only)
  2193 00005862 [F75F0000]          <1> 	dd	rhd ; hd0, AX = 5 (runix only)
  2194 00005866 [F75F0000]          <1> 	dd	rhd ; hd1, AX = 6 (runix only)	
  2195 0000586A [F75F0000]          <1> 	dd	rhd ; hd2, AX = 7 (runix only)
  2196 0000586E [F75F0000]          <1> 	dd	rhd ; hd3, AX = 8 (runix only)	
  2197 00005872 [07590000]          <1> 	dd	rlpr ; lpr, AX = 9 (invalid, write only device !?)
  2198 00005876 [EE580000]          <1> 	dd	rcvt ; tty0, AX = 10 (runix)	  
  2199                              <1> 		 ;rcvt / tty0
  2200 0000587A [EE580000]          <1> 	dd	rcvt ; tty1, AX = 11 (runix)	  
  2201                              <1> 		 ;rcvt / tty1
  2202 0000587E [EE580000]          <1> 	dd	rcvt ; tty2, AX = 12 (runix)	  
  2203                              <1> 		 ;rcvt / tty2
  2204 00005882 [EE580000]          <1> 	dd	rcvt ; tty3, AX = 13 (runix)	  
  2205                              <1> 		 ;rcvt / tty3
  2206 00005886 [EE580000]          <1> 	dd	rcvt ; tty4, AX = 14 (runix)	  
  2207                              <1> 		 ;rcvt / tty4
  2208 0000588A [EE580000]          <1> 	dd	rcvt ; tty5, AX = 15 (runix)	  
  2209                              <1> 		 ;rcvt / tty5
  2210 0000588E [EE580000]          <1> 	dd	rcvt ; tty6, AX = 16 (runix)	  
  2211                              <1> 		 ;rcvt / tty6
  2212 00005892 [EE580000]          <1> 	dd	rcvt ; tty7, AX = 17 (runix)	  
  2213                              <1> 		 ;rcvt / tty7
  2214 00005896 [EE580000]          <1> 	dd	rcvt ; COM1, AX = 18 (runix only)	  
  2215                              <1> 		 ;rcrd / crd
  2216 0000589A [EE580000]          <1> 	dd	rcvt ; COM2, AX = 19 (runix only)
  2217                              <1> 
  2218                              <1> 	; 28/11/2021 - Retro UNIX 386 v2 fs compatibility modification
  2219                              <1> null:
  2220 0000589E 31C0                <1> 	xor	eax, eax
  2221 000058A0 58                  <1> 	pop	eax
  2222 000058A1 C3                  <1> 	retn
  2223                              <1> 
  2224                              <1> 	; 11/01/2022
  2225                              <1> 	; 11/12/2021 - Retro UNIX 386 v1.2
  2226                              <1> rtty: ; / read from console tty
  2227                              <1> 	; 17/10/2015 - 16/07/2015 (Retro UNIX 8086 v1)
  2228                              <1> 	; 	     (Only 1 byte is read, by ignoring byte count!)
  2229                              <1> 	;  	     WHAT FOR: Every character from Keyboard input 
  2230                              <1> 	;	     must be written immediate on video page (screen)
  2231                              <1> 	;	     when it is required.	
  2232                              <1> 	; 19/05/2015 (Retro UNIX 386 v1 - Beginning)
  2233                              <1> 	; 11/03/2013 - 19/06/2014 (Retro UNIX 8086 v1)
  2234                              <1> 	;
  2235                              <1> 	; Console tty buffer is PC keyboard buffer
  2236                              <1> 	; and keyboard-keystroke handling is different than original
  2237                              <1> 	; unix (PDP-11) here. TTY/Keyboard procedures here are changed
  2238                              <1> 	; according to IBM PC compatible ROM BIOS keyboard functions. 
  2239                              <1> 	;
  2240                              <1> 	; 06/12/2013
  2241 000058A2 0FB61D[29750000]    <1> 	movzx	ebx, byte [u.uno] ; process number
  2242 000058A9 8A83[F3700000]      <1> 	mov	al, [ebx+p.ttyc-1] ; current/console tty
  2243                              <1> rttys:
  2244                              <1> 		; mov tty+[8*ntty]-8+6,r5 / r5 is the address of the 4th word of
  2245                              <1> 	               ; / of the control and status block
  2246                              <1> 		; tst 2(r5) / for the console tty; this word points to the console
  2247                              <1> 		       ; / tty buffer
  2248                              <1> 	; 28/07/2013
  2249 000058AF A2[0D750000]        <1> 	mov 	[u.ttyn], al
  2250                              <1> 	; 13/01/2014
  2251 000058B4 FEC0                <1> 	inc	al
  2252 000058B6 A2[0E750000]        <1> 	mov	[u.ttyp], al ; tty number + 1
  2253                              <1> rtty_nc: ; 01/02/2014
  2254                              <1> 	; 29/09/2013
  2255                              <1> 	;mov	ecx, 10
  2256                              <1> 	; 11/01/2022
  2257 000058BB 29C9                <1> 	sub	ecx, ecx
  2258 000058BD B10A                <1> 	mov	cl, 10
  2259                              <1> rtty_1: 	; 01/02/2014
  2260                              <1> 	;push 	cx ; 29/09/2013
  2261                              <1> 	; 11/12/2021
  2262 000058BF 51                  <1> 	push	ecx
  2263                              <1> 	; byte [u.ttyn] = tty number (0 to 9) 
  2264 000058C0 B001                <1> 	mov 	al, 1
  2265 000058C2 E8750B0000          <1> 	call 	getc
  2266                              <1> 	;pop 	cx ; 29/09/2013	
  2267                              <1> 	; 11/12/2021
  2268 000058C7 59                  <1> 	pop	ecx
  2269 000058C8 7516                <1> 	jnz	short rtty_2
  2270                              <1> 		; bne 1f / 2nd word of console tty buffer contains number
  2271                              <1> 	               ; / of chars. Is this number non-zero?
  2272 000058CA E20D                <1> 	loop	rtty_idle ; 01/02/2014
  2273                              <1> 	; 05/10/2013
  2274 000058CC 8A25[0D750000]      <1> 	mov	ah, [u.ttyn]
  2275                              <1> 	; 29/09/2013
  2276 000058D2 E8C2F7FFFF          <1> 	call	sleep
  2277                              <1> 		; jsr r0,canon; ttych / if 0, call 'canon' to get a line
  2278                              <1>                 ;           / (120 chars.)
  2279                              <1> 	; byte [u.ttyn] = tty number (0 to 9) 
  2280 000058D7 EBE2                <1> 	jmp	short rtty_nc ; 01/02/2014
  2281                              <1> 
  2282                              <1> rtty_idle:
  2283                              <1> 	; 29/07/2013
  2284 000058D9 E828F7FFFF          <1> 	call 	idle
  2285 000058DE EBDF                <1> 	jmp	short rtty_1 ; 01/02/2014
  2286                              <1> 	;1:
  2287                              <1> 		; tst 2(r5) / is the number of characters zero
  2288                              <1> 		; beq ret1 / yes, return to caller via 'ret1'
  2289                              <1> 		; movb *4(r5),r1 / no, put character in r1
  2290                              <1> 		; inc 4(r5) / 3rd word of console tty buffer points to byte which
  2291                              <1> 		          ; / contains the next char.
  2292                              <1> 		; dec 2(r5) / decrement the character count
  2293                              <1> rtty_2:
  2294 000058E0 30C0                <1> 	xor 	al, al
  2295 000058E2 E8550B0000          <1> 	call 	getc
  2296 000058E7 E891000000          <1> 	call	passc
  2297                              <1> 		; jsr r0,passc / move the character to core (user)
  2298                              <1> 	;; 17/10/2015 - 16/07/2015
  2299                              <1> 	; 19/06/2014
  2300                              <1> 	;;jnz	short rtty_nc
  2301 000058EC 58                  <1> 	pop	eax  ; (20/05/2015)
  2302 000058ED C3                  <1> 	retn 
  2303                              <1> ;ret1:
  2304                              <1> 		; jmp ret / return to caller via 'ret'
  2305                              <1> 
  2306                              <1> rcvt:   ; < receive/read character from tty >
  2307                              <1> 	; 19/05/2015 (Retro UNIX 386 v1 - Beginning)
  2308                              <1> 	; 15/05/2013 - 06/12/2013 (Retro UNIX 8086 v1)
  2309                              <1> 	;
  2310                              <1> 	; Retro UNIX 8086 v1 modification !
  2311                              <1> 	; 
  2312                              <1> 	; In original UNIX v1, 'rcvt' routine 
  2313                              <1> 	;		(exactly different than this one)
  2314                              <1> 	;	was in 'u9.s' file.
  2315                              <1> 	;
  2316 000058EE 2C0A                <1> 	sub 	al, 10
  2317                              <1> 	; AL = tty number (0 to 9), (COM1=8, COM2=9)
  2318                              <1> 	; 16/07/2013
  2319                              <1> 	; 21/05/2013
  2320 000058F0 EBBD                <1>         jmp     short rttys
  2321                              <1>       
  2322                              <1> ;rppt: / read paper tape
  2323                              <1> ;	jsr	r0,pptic / gets next character in clist for ppt input and
  2324                              <1> ;			 / places
  2325                              <1> ;		br ret / it in r1; if there 1s no problem with reader, it
  2326                              <1> ;		       / also enables read bit in prs
  2327                              <1> ;	jsr	r0,passc / place character in users buffer area
  2328                              <1> ;	br	rppt
  2329                              <1> 
  2330                              <1> rmem: ; / transfer characters from memory to a user area of core
  2331                              <1> 	; 17/10/2015
  2332                              <1> 	; 11/06/2015
  2333                              <1> 	; 24/05/2015
  2334                              <1> 	; 19/05/2015 (Retro UNIX 386 v1 - Beginning)
  2335                              <1> 	;
  2336 000058F2 8B35[EC740000]      <1> 	mov     esi, [u.fofp]
  2337                              <1> rmem_1:
  2338 000058F8 8B1E                <1>         mov     ebx, [esi]        
  2339                              <1> 	        ; mov *u.fofp,r1 / save file offset which points to the char
  2340                              <1> 		               ; / to be transferred to user
  2341 000058FA FF06                <1>         inc     dword [esi] ; 17/10/2015
  2342                              <1> 		; inc *u.fofp / increment file offset to point to 'next' 
  2343                              <1> 			    ; / char in memory file
  2344 000058FC 8A03                <1> 	mov	al, [ebx]
  2345                              <1> 		; movb (r1),r1 / get character from memory file, 
  2346                              <1> 		             ; / put it in r1
  2347 000058FE E87A000000          <1> 	call	passc        ; jsr r0,passc / move this character to 
  2348                              <1> 			     ;  / the next byte of the users core area
  2349                              <1> 		; br rmem / continue
  2350 00005903 75F3                <1> 	jnz	short rmem_1
  2351                              <1> ret_:
  2352 00005905 58                  <1> 	pop	eax ; 09/06/2015
  2353 00005906 C3                  <1> 	retn
  2354                              <1> 
  2355                              <1> rlpr:
  2356                              <1> ;1:
  2357                              <1> ;rcrd:
  2358 00005907 C705[4C750000]0F00- <1>         mov     dword [u.error], ERR_DEV_NOT_RDY ; 19/05/2015
  2358 0000590F 0000                <1>
  2359 00005911 E994E2FFFF          <1> 	jmp	error
  2360                              <1> 		;jmp	error / see 'error' routine
  2361                              <1> 
  2362                              <1> 	; 28/11/2021 - Retro UNIX 386 v2 fs compatibility modification
  2363                              <1> dskr:
  2364                              <1> 	; 12/10/2015
  2365                              <1> 	; 21/08/2015
  2366                              <1> 	; 25/07/2015
  2367                              <1> 	; 10/07/2015
  2368                              <1> 	; 16/06/2015
  2369                              <1> 	; 31/05/2015
  2370                              <1> 	; 24/05/2015 (Retro UNIX 386 v1 - Beginning)
  2371                              <1> 	; 26/04/2013 - 03/08/2013 (Retro UNIX 8086 v1)
  2372                              <1> dskr_0:
  2373 00005916 50                  <1> 	push	eax
  2374                              <1> 		; mov (sp),r1 / i-number in r1
  2375                              <1> 	; AX = i-number
  2376 00005917 E86BFBFFFF          <1> 	call	iget
  2377                              <1> 		; jsr r0,iget / get i-node (r1) into i-node section of core
  2378                              <1>         ;movzx	edx, word [i.size] ; 16/06/2015
  2379                              <1> 	;	; mov i.size,r2 / file size in bytes in r2
  2380                              <1> 	; 28/11/2021
  2381 0000591C 8B15[7C700000]      <1> 	mov	edx, [i.size] ; 32 bit file size (runix v2 inode)
  2382                              <1> 	;
  2383 00005922 8B1D[EC740000]      <1> 	mov	ebx, [u.fofp]
  2384 00005928 2B13                <1> 	sub	edx, [ebx]
  2385                              <1> 		; sub *u.fofp,r2 / subtract file offset
  2386                              <1>         ; 12/10/2015
  2387                              <1> 	; jna	short ret_ 
  2388                              <1> 		; blos ret
  2389 0000592A 7709                <1> 	ja	short dskr_1
  2390                              <1> 	;
  2391                              <1> dskr_retn: ; 12/10/2015
  2392 0000592C 58                  <1> 	pop	eax
  2393 0000592D C605[4A750000]00    <1> 	mov	byte [u.kcall], 0
  2394 00005934 C3                  <1> 	retn	
  2395                              <1> dskr_1: 
  2396 00005935 3B15[00750000]      <1> 	cmp     edx, [u.count] 
  2397                              <1> 		; cmp r2,u.count / are enough bytes left in file 
  2398                              <1> 			       ; / to carry out read
  2399 0000593B 7306                <1> 	jnb	short dskr_2
  2400                              <1> 		; bhis 1f
  2401 0000593D 8915[00750000]      <1> 	mov	[u.count], edx
  2402                              <1> 		; mov r2,u.count / no, just read to end of file
  2403                              <1> dskr_2: ; 1:
  2404                              <1> 	; (E)AX = i-number ; 28/11/2021 (32 bit inode number)
  2405                              <1> 	;call	mget
  2406                              <1> 		; jsr r0,mget / returns physical block number of block 
  2407                              <1> 		    ; / in file where offset points
  2408                              <1> 	; 28/11/2021
  2409 00005943 E8DAF7FFFF          <1> 	call	mget_r
  2410                              <1> 
  2411                              <1> 	; eax = physical block number
  2412                              <1> 	; [cdev] = current device number 
  2413 00005948 E804080000          <1> 	call	dskrd
  2414                              <1> 		; jsr r0,dskrd / read in block, r5 points to 
  2415                              <1> 			     ; / 1st word of data in buffer
  2416                              <1> 	; 09/06/2015
  2417 0000594D 803D[4A750000]00    <1> 	cmp	byte [u.kcall], 0 ; the caller is 'namei' sign (=1)
  2418 00005954 770F                <1> 	ja	short dskr_4	  ; zf=0 -> the caller is 'namei'
  2419 00005956 66833D[44750000]00  <1> 	cmp	word [u.pcount], 0
  2420 0000595E 7705                <1> 	ja	short dskr_4
  2421                              <1> dskr_3:
  2422                              <1> 	; [u.base] = virtual address to transfer (as destination address)
  2423 00005960 E853000000          <1> 	call	trans_addr_w ; translate virtual address to physical (w)
  2424                              <1> dskr_4:
  2425                              <1> 	; EBX (r5) = system (I/O) buffer address -physical-
  2426 00005965 E8C2020000          <1> 	call	sioreg
  2427                              <1> 		; jsr r0,sioreg
  2428 0000596A 87F7                <1> 	xchg	esi, edi
  2429                              <1> 	; edi = file (user data) offset
  2430                              <1> 	; edi = sector (I/O) buffer offset
  2431                              <1> 	; ecx = byte count
  2432 0000596C F3A4                <1> 	rep	movsb
  2433                              <1> 		; movb (r2)+,(r1)+ / move data from buffer into working core
  2434                              <1> 		                 ; / starting at u.base
  2435                              <1> 		; dec r3
  2436                              <1> 		; bne 2b / branch until proper number of bytes are transferred
  2437                              <1> 	; 25/07/2015
  2438                              <1> 	; eax = remain bytes in buffer
  2439                              <1>         ;       (check if remain bytes in the buffer > [u.pcount])
  2440 0000596E 09C0                <1> 	or	eax, eax
  2441 00005970 75EE                <1> 	jnz	short dskr_3 ; (page end before system buffer end!)		
  2442                              <1> 	; 03/08/2013
  2443                              <1> 	;pop	eax
  2444 00005972 390D[00750000]      <1> 	cmp	[u.count], ecx ; 0
  2445                              <1> 		; tst u.count / all bytes read off disk
  2446                              <1> 		; bne dskr
  2447                              <1> 		; br ret
  2448                              <1>         ;ja	short dskr_0
  2449                              <1> 	;mov	[u.kcall], cl ; 0 ; 09/06/2015
  2450                              <1> 	;retn
  2451                              <1> 	; 12/10/2015
  2452 00005978 76B2                <1> 	jna	short dskr_retn
  2453 0000597A 58                  <1> 	pop	eax  ; (i-node number)
  2454 0000597B EB99                <1> 	jmp	short dskr_0
  2455                              <1> 	
  2456                              <1> passc:
  2457                              <1> 	; 18/10/2015
  2458                              <1> 	; 10/07/2015
  2459                              <1> 	; 01/07/2015
  2460                              <1> 	; 08/06/2015
  2461                              <1> 	; 04/06/2015
  2462                              <1> 	; 20/05/2015
  2463                              <1> 	; 19/05/2015 (Retro UNIX 386 v1 - Beginning)
  2464                              <1> 	;
  2465                              <1>    	;(Retro UNIX 386 v1 - translation from user's virtual address
  2466                              <1> 	;		      to physical address
  2467 0000597D 66833D[44750000]00  <1> 	cmp	word [u.pcount], 0 ; byte count in page = 0 (initial value)
  2468                              <1> 			     ; 1-4095 --> use previous physical base address
  2469                              <1> 			     ; in [u.pbase]
  2470 00005985 7705                <1> 	ja	short passc_3
  2471                              <1> 	; 08/06/2015 - 10/07/2015
  2472 00005987 E82C000000          <1> 	call	trans_addr_w
  2473                              <1> passc_3:
  2474                              <1> 	; 19/05/2015
  2475 0000598C 66FF0D[44750000]    <1> 	dec	word [u.pcount]
  2476                              <1> 	;
  2477 00005993 8B1D[40750000]      <1> 	mov	ebx, [u.pbase]
  2478 00005999 8803                <1> 	mov	[ebx], al
  2479                              <1> 		; movb r1,*u.base / move a character to the next byte of the
  2480                              <1> 		               ; / users buffer
  2481 0000599B FF05[FC740000]      <1> 	inc	dword [u.base]
  2482                              <1> 		; inc u.base / increment the pointer to point to 
  2483                              <1> 			  ; / the next byte in users buffer
  2484 000059A1 FF05[40750000]      <1> 	inc	dword [u.pbase] ; 04/06/2015
  2485 000059A7 FF05[04750000]      <1> 	inc	dword [u.nread]
  2486                              <1> 		; inc u.nread / increment the number of bytes read
  2487 000059AD FF0D[00750000]      <1> 	dec	dword [u.count]
  2488                              <1> 		; dec u.count / decrement the number of bytes to be read
  2489                              <1> 		; bne 1f / any more bytes to read? ; yes, branch
  2490 000059B3 C3                  <1> 	retn
  2491                              <1> 		; mov (sp)+,r0 / no, do a non-local return to the caller of
  2492                              <1> 		             ; / 'readi' by:
  2493                              <1> 		;/ (1) pop the return address off the stack into r0
  2494                              <1> 		; mov (sp)+,r1 / (2) pop the i-number off the stack into r1
  2495                              <1> 	;1:
  2496                              <1> 		; clr	*$ps / clear processor status
  2497                              <1> 		; rts r0 / return to address currently on top of stack
  2498                              <1> 
  2499                              <1> trans_addr_r:
  2500                              <1> 	; 30/11/2021 (Retro UNIX 386 v2, Retro UNIX 386 v1.2)
  2501                              <1> 	;	-'add_to_swap_queue' call is diasbled as temporary-
  2502                              <1> 	; Translate virtual address to physical address 
  2503                              <1> 	; for reading from user's memory space
  2504                              <1> 	; (Retro UNIX 386 v1 feature only !)
  2505                              <1> 	; 18/10/2015
  2506                              <1> 	; 10/07/2015
  2507                              <1> 	; 09/06/2015
  2508                              <1> 	; 08/06/2015 
  2509                              <1> 	; 04/06/2015
  2510                              <1> 	;
  2511                              <1> 	; 18/10/2015
  2512 000059B4 31D2                <1> 	xor	edx, edx ; 0 (read access sign)
  2513 000059B6 EB04                <1> 	jmp 	short trans_addr_rw
  2514                              <1> 
  2515                              <1> 	;push	eax
  2516                              <1> 	;push	ebx
  2517                              <1> 	;mov	ebx, [u.base]
  2518                              <1> 	;call	get_physical_addr ; get physical address
  2519                              <1> 	;;jnc	short cpass_0
  2520                              <1> 	;jnc	short passc_1
  2521                              <1> 	;mov	[u.error], eax
  2522                              <1> 	;;pop	ebx
  2523                              <1> 	;;pop	eax
  2524                              <1> 	;jmp	error
  2525                              <1> ;cpass_0:
  2526                              <1> 	; 18/10/2015
  2527                              <1> 	; 20/05/2015
  2528                              <1> 	;mov 	[u.pbase], eax ; physical address	
  2529                              <1> 	;mov	[u.pcount], cx ; remain byte count in page (1-4096)
  2530                              <1> 	;pop	ebx
  2531                              <1> 	;pop	eax
  2532                              <1> 	;retn	; 08/06/2015
  2533                              <1> 
  2534                              <1> trans_addr_w:
  2535                              <1> 	; 31/12/2021
  2536                              <1> 	; 30/11/2021 (Retro UNIX 386 v2, Retro UNIX 386 v1.2)
  2537                              <1> 	;	-'add_to_swap_queue' call is disabled as temporary-
  2538                              <1> 	; Translate virtual address to physical address
  2539                              <1> 	; for writing to user's memory space
  2540                              <1> 	; (Retro UNIX 386 v1 feature only !)
  2541                              <1> 	; 18/10/2015
  2542                              <1> 	; 29/07/2015
  2543                              <1> 	; 10/07/2015
  2544                              <1> 	; 09/06/2015
  2545                              <1> 	; 08/06/2015
  2546                              <1> 	; 04/06/2015 (passc)
  2547                              <1> 	;
  2548                              <1> 	; 18/10/2015
  2549 000059B8 29D2                <1> 	sub	edx, edx
  2550 000059BA FEC2                <1> 	inc	dl ; 1 (write access sign)
  2551                              <1> trans_addr_rw:
  2552 000059BC 50                  <1> 	push	eax
  2553 000059BD 53                  <1> 	push	ebx
  2554                              <1> 	; 18/10/2015
  2555 000059BE 52                  <1> 	push 	edx ; r/w sign (in DL)
  2556                              <1> 	;
  2557 000059BF 8B1D[FC740000]      <1> 	mov	ebx, [u.base]
  2558 000059C5 E888D8FFFF          <1> 	call	get_physical_addr ; get physical address
  2559 000059CA 730A                <1> 	jnc	short passc_0
  2560 000059CC A3[4C750000]        <1> 	mov	[u.error], eax
  2561                              <1> 	;pop	edx
  2562                              <1> 	;pop 	ebx
  2563                              <1> 	;pop	eax
  2564 000059D1 E9D4E1FFFF          <1> 	jmp	error
  2565                              <1> passc_0:
  2566 000059D6 F6C202              <1> 	test	dl, PTE_A_WRITE ; writable page ; 18/10/2015
  2567 000059D9 5A                  <1> 	pop	edx ; 18/10/2015
  2568 000059DA 7517                <1> 	jnz	short passc_1
  2569                              <1> 	; 18/10/2015
  2570 000059DC 20D2                <1> 	and 	dl, dl
  2571 000059DE 7413                <1> 	jz	short passc_1
  2572                              <1> 	; 20/05/2015
  2573                              <1> 	; read only (duplicated) page -must be copied to a new page-
  2574                              <1> 	; EBX = linear address
  2575 000059E0 51                  <1> 	push 	ecx
  2576                              <1> 	; 31/12/2021 - Retro UNIX 386 v1.2 (BugFix)
  2577 000059E1 53                  <1> 	push	ebx ;* ; BugFix  ; 31/12/2021
  2578 000059E2 E8DAD7FFFF          <1> 	call 	copy_page
  2579 000059E7 5B                  <1> 	pop	ebx ;* ; BugFix  ; 31/12/2021
  2580 000059E8 59                  <1> 	pop	ecx
  2581 000059E9 7217                <1> 	jc	short passc_2
  2582                              <1> 	; 30/11/2021
  2583                              <1> 	;push	eax ; physical address of the new/allocated page
  2584                              <1> 	;call	add_to_swap_queue
  2585                              <1> 	;pop	eax
  2586                              <1> 	; 18/10/2015
  2587 000059EB 81E3FF0F0000        <1> 	and 	ebx, PAGE_OFF ; 0FFFh
  2588                              <1> 	;mov 	ecx, PAGE_SIZE
  2589                              <1> 	;sub	ecx, ebx 
  2590 000059F1 01D8                <1> 	add	eax, ebx  
  2591                              <1> passc_1: 
  2592                              <1> 	; 18/10/2015
  2593                              <1> 	; 20/05/2015
  2594 000059F3 A3[40750000]        <1> 	mov 	[u.pbase], eax ; physical address	
  2595 000059F8 66890D[44750000]    <1> 	mov	[u.pcount], cx ; remain byte count in page (1-4096)
  2596 000059FF 5B                  <1> 	pop	ebx
  2597 00005A00 58                  <1> 	pop	eax
  2598 00005A01 C3                  <1> 	retn	; 08/06/2015
  2599                              <1> passc_2:
  2600 00005A02 C705[4C750000]0400- <1> 	mov	dword [u.error], ERR_MINOR_IM ; "Insufficient memory !" error
  2600 00005A0A 0000                <1>
  2601                              <1> 	;pop 	ebx
  2602                              <1> 	;pop	eax
  2603 00005A0C E999E1FFFF          <1> 	jmp	error
  2604                              <1> 
  2605                              <1> 	; 25/12/2021
  2606                              <1> 	; 11/12/2021
  2607                              <1> 	; 28/11/2021 - Retro UNIX 386 v2 fs compatibility modification
  2608                              <1> writei:
  2609                              <1> 	; 20/05/2015
  2610                              <1> 	; 19/05/2015 (Retro UNIX 386 v1 - Beginning)
  2611                              <1> 	; 12/03/2013 - 31/07/2013 (Retro UNIX 8086 v1)
  2612                              <1> 	;
  2613                              <1> 	; Write data to file with inode number in R1
  2614                              <1> 	; 
  2615                              <1> 	; INPUTS ->
  2616                              <1> 	;    r1 - inode number
  2617                              <1> 	;    u.count - byte count to be written
  2618                              <1> 	;    u.base - points to user buffer
  2619                              <1> 	;    u.fofp - points to word with current file offset
  2620                              <1> 	; OUTPUTS ->
  2621                              <1> 	;    u.count - cleared
  2622                              <1> 	;    u.nread - accumulates total bytes passed back	
  2623                              <1> 	; ((AX = R1))
  2624                              <1> 	;    (Retro UNIX Prototype : 18/11/2012 - 11/11/2012, UNIXCOPY.ASM)
  2625                              <1> 	;    ((Modified registers: DX, BX, CX, SI, DI, BP)) 	
  2626                              <1> 
  2627 00005A11 31C9                <1> 	xor	ecx, ecx
  2628 00005A13 890D[04750000]      <1> 	mov 	[u.nread], ecx  ; 0
  2629                              <1> 		; clr u.nread / clear the number of bytes transmitted during
  2630                              <1> 		            ; / read or write calls
  2631 00005A19 66890D[44750000]    <1> 	mov	[u.pcount], cx ; 19/05/2015
  2632 00005A20 390D[00750000]      <1> 	cmp 	[u.count], ecx
  2633                              <1> 	;	; tst u.count / test the byte count specified by the user
  2634 00005A26 7701                <1> 	ja 	short writei_1 ; 1f
  2635                              <1> 		; bgt 1f / any bytes to output; yes, branch
  2636 00005A28 C3                  <1> 	retn
  2637                              <1> 	;	; rts r0 / no, return - no writing to do
  2638                              <1> writei_1: ;1:
  2639                              <1> 	;	; mov r1 ,-(sp) / save the i-node number on the stack
  2640                              <1> 	;cmp 	ax, 40
  2641                              <1> 	;	; cmp r1,$40.
  2642                              <1> 	;	; / does the i-node number indicate a special file?
  2643                              <1>         ;ja	dskw 
  2644                              <1> 	;	; bgt dskw / no, branch to standard file output
  2645                              <1> 
  2646                              <1> 	; 28/11/2021
  2647 00005A29 F605[4A750000]FF    <1> 	test	byte [u.kcall], 0FFh
  2648 00005A30 7505                <1> 	jnz	short writei_2  ; 'writei' called by 'mkdir'
  2649                              <1> 
  2650                              <1> 	; eax = inode number
  2651                              <1> 	; [cdev] = device number (0 = root fs, 1 = mounted fs)
  2652 00005A32 E850FAFFFF          <1> 	call	iget
  2653                              <1> writei_2:
  2654                              <1> 	; 28/11/2021
  2655                              <1> 	; eax = inode number
  2656                              <1> 	; [cdev] = device number (0 = root fs, 1 = mounted fs)
  2657 00005A37 E871020000          <1> 	call	is_regular_file
  2658                              <1> 	;jz	dskw ; regular file 
  2659                              <1> 	; 25/12/2021
  2660 00005A3C 7505                <1> 	jnz	short writei_5 ; device file 
  2661                              <1> 	; regular file
  2662 00005A3E E9FD000000          <1> 	jmp	dskw
  2663                              <1> writei_5:
  2664                              <1> 	; (20/05/2015)
  2665 00005A43 50                  <1> 	push	eax ; because subroutines will jump to 'ret_'
  2666                              <1> 	
  2667                              <1> 	; 28/11/2021	
  2668                              <1> 	; device file
  2669 00005A44 BB[5E5A0000]        <1> 	mov	ebx, writei_4
  2670 00005A49 83F808              <1> 	cmp	eax, 8	; /dev/tty inode number is 8
  2671 00005A4C 720E                <1> 	jb	short writei_3
  2672 00005A4E 83F81A              <1> 	cmp	eax, 26	; /dev/tty9 (/dev/com2) inode number is 26
  2673 00005A51 7709                <1> 	ja	short writei_3 ; 11/12/2021	
  2674                              <1> 	
  2675                              <1> 	; convert v2 inode number to v1 device inode number
  2676 00005A53 2C07                <1> 	sub	al, 7 ; 8 -> 1, 26 -> 19
  2677 00005A55 89C1                <1> 	mov	ecx, eax
  2678 00005A57 C0E102              <1> 	shl	cl, 2 ; * 4
  2679 00005A5A 01CB                <1> 	add	ebx, ecx
  2680                              <1> writei_3:
  2681 00005A5C FF23                <1> 	jmp	dword [ebx]	
  2682                              <1> 		; jmp *1f-2(r1)
  2683                              <1> 		; / jump table and jump to the appropriate routine
  2684                              <1> writei_4: ;1:
  2685 00005A5E [9E580000]          <1> 	dd	null ; 28/11/2021
  2686 00005A62 [AE5A0000]          <1> 	dd	wtty ; tty, AX = 1 (runix)
  2687                              <1> 		 ;wtty / tty; r1=2
  2688                              <1> 		 ;wppt / ppt; r1=4
  2689 00005A66 [105B0000]          <1> 	dd	wmem ; mem, AX = 2 (runix)
  2690                              <1> 		 ;wmem / mem; r1=6
  2691                              <1> 		 ;wrf0 / rf0
  2692                              <1> 		 ;wrk0 / rk0
  2693                              <1> 		 ;wtap / tap0
  2694                              <1> 		 ;wtap / tap1
  2695                              <1> 		 ;wtap / tap2
  2696                              <1> 		 ;wtap / tap3
  2697                              <1> 		 ;wtap / tap4
  2698                              <1> 		 ;wtap / tap5
  2699                              <1> 		 ;wtap / tap6
  2700                              <1> 		 ;wtap / tap7
  2701 00005A6A [79600000]          <1> 	dd	wfd ; fd0, AX = 3 (runix only)
  2702 00005A6E [79600000]          <1> 	dd	wfd ; fd1, AX = 4 (runix only)
  2703 00005A72 [79600000]          <1> 	dd	whd ; hd0, AX = 5 (runix only)
  2704 00005A76 [79600000]          <1> 	dd	whd ; hd1, AX = 6 (runix only)	
  2705 00005A7A [79600000]          <1> 	dd	whd ; hd2, AX = 7 (runix only)
  2706 00005A7E [79600000]          <1> 	dd	whd ; hd3, AX = 8 (runix only)	
  2707 00005A82 [015B0000]          <1> 	dd	wlpr ; lpr, AX = 9   (runix)
  2708 00005A86 [FB5A0000]          <1> 	dd	xmtt ; tty0, AX = 10 (runix)	  
  2709                              <1> 		 ;xmtt / tty0
  2710 00005A8A [FB5A0000]          <1> 	dd	xmtt ; tty1, AX = 11 (runix)	  
  2711                              <1> 		 ;xmtt / tty1
  2712 00005A8E [FB5A0000]          <1> 	dd	xmtt ; tty2, AX = 12 (runix)	  
  2713                              <1> 		 ;xmtt / tty2
  2714 00005A92 [FB5A0000]          <1> 	dd	xmtt ; tty3, AX = 13 (runix)	  
  2715                              <1> 		 ;xmtt / tty3
  2716 00005A96 [FB5A0000]          <1> 	dd	xmtt ; tty4, AX = 14 (runix)	  
  2717                              <1> 		 ;xmtt / tty4
  2718 00005A9A [FB5A0000]          <1> 	dd	xmtt ; tty5, AX = 15 (runix)	  
  2719                              <1> 		 ;xmtt / tty5
  2720 00005A9E [FB5A0000]          <1> 	dd	xmtt ; tty6, AX = 16 (runix)	  
  2721                              <1> 		 ;xmtt / tty6
  2722 00005AA2 [FB5A0000]          <1> 	dd	xmtt ; tty7, AX = 17 (runix)	  
  2723                              <1> 		 ;xmtt / tty7
  2724 00005AA6 [FB5A0000]          <1> 	dd	xmtt ; COM1, AX = 18 (runix only)	  
  2725                              <1> 		; / wlpr / lpr
  2726 00005AAA [FB5A0000]          <1> 	dd	xmtt ; COM2, AX = 19 (runix only)
  2727                              <1> 
  2728                              <1> ;	; 28/11/2021 - Retro UNIX 386 v2 fs compatibility modification
  2729                              <1> ;null:
  2730                              <1> ;	xor	eax, eax
  2731                              <1> ;	pop	eax
  2732                              <1> ;	retn	
  2733                              <1> 
  2734                              <1> 	; 11/12/2021 - Retro UNIX 386 v1.2
  2735                              <1> wtty: ; write to console tty (write to screen)
  2736                              <1> 	; 18/11/2015
  2737                              <1> 	; 19/05/2015 (Retro UNIX 386 v1 - Beginning)
  2738                              <1> 	; 12/03/2013 - 07/07/2014 (Retro UNIX 8086 v1)
  2739                              <1> 	;
  2740                              <1> 	; Console tty output is on current video page
  2741                              <1> 	; Console tty character output procedure is changed here
  2742                              <1> 	; acconding to IBM PC compatible ROM BIOS video (text mode) functions.
  2743                              <1> 	;
  2744 00005AAE 0FB61D[29750000]    <1> 	movzx	ebx, byte [u.uno] ; process number
  2745 00005AB5 8AA3[F3700000]      <1> 	mov	ah, [ebx+p.ttyc-1] ; current/console tty
  2746 00005ABB 88E0                <1> 	mov	al, ah ; 07/07/2014
  2747                              <1> wttys:	
  2748                              <1> 	; 10/10/2013
  2749 00005ABD 8825[0D750000]      <1> 	mov 	[u.ttyn], ah
  2750                              <1> 	; 13/01/2014
  2751 00005AC3 FEC0                <1> 	inc	al
  2752 00005AC5 A2[0F750000]        <1> 	mov	[u.ttyp+1], al ; tty number + 1
  2753                              <1> wtty_nc: ; 15/05/2013
  2754                              <1> 	; AH = [u.ttyn] = tty number ; 28/07/2013
  2755 00005ACA E8FD000000          <1> 	call	cpass
  2756                              <1> 		; jsr r0,cpass / get next character from user buffer area; if
  2757                              <1> 		             ; / none go to return address in syswrite
  2758                              <1> 		; tst r1 / is character = null
  2759                              <1> 		; beq wtty / yes, get next character
  2760                              <1> 	; 10/10/2013
  2761 00005ACF 7428                <1> 	jz	short wret
  2762                              <1> 	;1 :
  2763                              <1> 		;mov 	$240,*$ps / no, set processor priority to five
  2764                              <1> 		;cmpb	cc+1,$20. / is character count for console tty greater
  2765                              <1> 		;	          / than 20
  2766                              <1> 		;bhis	2f / yes; branch to put process to sleep
  2767                              <1> 	; 27/06/2014
  2768                              <1> wtty_1:
  2769                              <1> 	; AH = tty number
  2770                              <1> 	; AL = ASCII code of the character
  2771                              <1> 	; 15/04/2014
  2772                              <1> 	;push	ax
  2773                              <1> 	; 11/12/2021
  2774 00005AD1 50                  <1> 	push	eax
  2775 00005AD2 E8D3090000          <1> 	call	putc ; 14/05/2013
  2776 00005AD7 731D                <1> 	jnc	short wtty_2
  2777                              <1> 	; 18/11/2015
  2778 00005AD9 E828F5FFFF          <1> 	call	idle
  2779                              <1> 	;mov	ax, [esp]
  2780                              <1> 	; 11/12/2021
  2781 00005ADE 8B0424              <1> 	mov	eax, [esp]
  2782 00005AE1 E8C4090000          <1> 	call	putc
  2783 00005AE6 730E                <1> 	jnc	short wtty_2 
  2784                              <1> 	; 02/06/2014
  2785 00005AE8 8A25[0D750000]      <1> 	mov	ah, [u.ttyn]
  2786 00005AEE E8A6F5FFFF          <1> 	call	sleep
  2787                              <1> 	;pop	ax
  2788                              <1> 	; 11/12/2021
  2789 00005AF3 58                  <1> 	pop	eax
  2790 00005AF4 EBDB                <1> 	jmp 	short wtty_1
  2791                              <1> 		; jc 	error ; 15/05/2013 (COM1 or COM2 serial port error)
  2792                              <1> 		; jsr 	r0,putc; 1 / find place in freelist to assign to 
  2793                              <1> 			      ; / console tty and
  2794                              <1> 		; br 	2f / place character in list; if none available
  2795                              <1> 		   	  ; / branch to put process to sleep
  2796                              <1> 		; jsr	r0,startty / attempt to output character on tty
  2797                              <1> wtty_2:
  2798                              <1> 	; 15/04/2014
  2799                              <1> 	;pop	ax
  2800                              <1> 	; 11/12/2021
  2801 00005AF6 58                  <1> 	pop	eax
  2802 00005AF7 EBD1                <1> 	jmp	short wtty_nc
  2803                              <1> 		; br wtty
  2804                              <1> wret:	; 10/10/2013 (20/05/2015)
  2805 00005AF9 58                  <1> 	pop	eax
  2806 00005AFA C3                  <1> 	retn
  2807                              <1> 	;2:
  2808                              <1> 		;mov	r1,-(sp) / place character on stack
  2809                              <1> 		;jsr	r0,sleep; 1 / put process to sleep
  2810                              <1> 		;mov	(sp)+,r1 / remove character from stack
  2811                              <1> 		;br	1b / try again to place character in clist and output
  2812                              <1> 
  2813                              <1> xmtt:   ; < send/write character to tty >
  2814                              <1> 	; 19/05/2015 (Retro UNIX 386 v1 - Beginning)
  2815                              <1> 	; 15/05/2013 - 06/12/2013 (Retro UNIX 8086 v1)
  2816                              <1> 	;
  2817                              <1> 	; Retro UNIX 8086 v1 modification !
  2818                              <1> 	; 
  2819                              <1> 	; In original UNIX v1, 'xmtt' routine 
  2820                              <1> 	;		(exactly different than this one)
  2821                              <1> 	;	was in 'u9.s' file.
  2822                              <1> 	;
  2823 00005AFB 2C0A                <1> 	sub 	al, 10
  2824                              <1> 	; AL = tty number (0 to 9), (COM1=8, COM2=9)
  2825                              <1> 	; 10/10/2013
  2826 00005AFD 88C4                <1> 	mov	ah, al
  2827                              <1> 	; 28/07/2013
  2828 00005AFF EBBC                <1> 	jmp	short wttys
  2829                              <1> 
  2830                              <1> ;wppt:
  2831                              <1> ;	jsr	r0,cpass / get next character from user buffer area,
  2832                              <1> ;		         / if none return to writei's calling routine
  2833                              <1> ;	jsr	r0,pptoc / output character on ppt
  2834                              <1> ;	br	wppt
  2835                              <1> wlpr:
  2836 00005B01 C705[4C750000]0F00- <1>         mov     dword [u.error], ERR_DEV_NOT_RDY ; 19/05/2015
  2836 00005B09 0000                <1>
  2837 00005B0B E99AE0FFFF          <1> 	jmp 	error   ; ... Printing procedure will be located here ...
  2838                              <1> 		;/	jsr	r0,cpass
  2839                              <1> 		;/	cmp	r0,$'a
  2840                              <1> 		;/	blo	1f
  2841                              <1> 		;/	cmp	r1,$'z
  2842                              <1> 		;/	bhi	1f
  2843                              <1> 		;/	sub	$40,r1
  2844                              <1> 		;/1:
  2845                              <1> 		;/	jsr	r0,lptoc
  2846                              <1> 		;/	br	wlpr
  2847                              <1> 		; br rmem / continue
  2848                              <1> 
  2849                              <1> wmem: ; / transfer characters from a user area of core to memory file
  2850                              <1> 	; 17/10/2015
  2851                              <1> 	; 11/06/2015
  2852                              <1> 	; 24/05/2015
  2853                              <1> 	; 19/05/2015 (Retro UNIX 386 v1 - Beginning)
  2854                              <1> 	;
  2855 00005B10 813D[0F070000]-     <1> 	cmp	dword [x_timer], clock ; multi tasking clock/timer
  2855 00005B16 [29500000]          <1>
  2856 00005B1A 7415                <1>         je      short wmem_acc_err
  2857                              <1> 	;
  2858 00005B1C 8B35[EC740000]      <1>         mov     esi, [u.fofp] 
  2859                              <1> wmem_1:
  2860 00005B22 E8A5000000          <1> 	call	cpass
  2861                              <1> 		; jsr r0,cpass / get next character from users area of
  2862                              <1> 			     ; / core and put it in r1
  2863                              <1>         	; mov r1,-(sp) / put character on the stack
  2864                              <1> 	; 20/09/2013
  2865 00005B27 74D0                <1> 	jz	short wret ; wmem_2  
  2866 00005B29 8B1E                <1>         mov     ebx, [esi]
  2867                              <1> 		; mov *u.fofp,r1 / save file offset in r1
  2868 00005B2B FF06                <1>         inc     dword [esi] ; 17/10/2015
  2869                              <1> 		; inc *u.fofp / increment file offset to point to next
  2870                              <1> 			    ; / available location in file
  2871 00005B2D 8803                <1> 	mov	[ebx], al	
  2872                              <1> 		; movb (sp)+,(r1) / pop char off stack, put in memory loc 
  2873                              <1> 			        ; / assigned to it
  2874 00005B2F EBF1                <1> 	jmp	short wmem_1
  2875                              <1> 		; br wmem / continue
  2876                              <1> 	;1:
  2877                              <1> 	;jmp	error / ?
  2878                              <1> ;wmem_2:	
  2879                              <1> ;	; 20/09/2013
  2880                              <1> ;	pop	ax
  2881                              <1> ;	retn
  2882                              <1> 
  2883                              <1> wmem_acc_err:
  2884 00005B31 C705[4C750000]0B00- <1> 	mov	dword [u.error], ERR_FILE_ACCESS ; permission denied !
  2884 00005B39 0000                <1>
  2885 00005B3B E96AE0FFFF          <1> 	jmp	error
  2886                              <1> 
  2887                              <1> 	; 28/11/2021 - Retro UNIX 386 v2 fs compatibility modification
  2888                              <1> 
  2889                              <1> dskw: ; / write routine for non-special files
  2890                              <1> 	;
  2891                              <1> 	; 28/11/2021
  2892                              <1> 	; 25/07/2015
  2893                              <1> 	; 16/06/2015
  2894                              <1> 	; 09/06/2015
  2895                              <1> 	; 31/05/2015 (Retro UNIX 386 v1 - Beginning)
  2896                              <1> 	; 26/04/2013 - 20/09/2013 (Retro UNIX 8086 v1)
  2897                              <1> 
  2898                              <1> 	; 01/08/2013 (mkdir_w check)
  2899                              <1> 	;push	ax ; 26/04/2013
  2900                              <1> 	;	; mov (sp),r1 / get an i-node number from the stack into r1
  2901                              <1> 	; 28/11/2021
  2902 00005B40 50                  <1> 	push	eax
  2903                              <1> 	; AX = inode number
  2904 00005B41 E841F9FFFF          <1> 	call	iget
  2905                              <1> 		; jsr r0,iget / write i-node out (if modified), 
  2906                              <1> 		            ; / read i-node 'r1' into i-node area of core
  2907 00005B46 8B1D[EC740000]      <1>         mov     ebx, [u.fofp] 
  2908 00005B4C 8B13                <1> 	mov 	edx, [ebx]
  2909                              <1> 		; mov *u.fofp,r2 / put the file offset [(u.off) or the offset
  2910                              <1> 			       ; / in the fsp entry for this file] in r2
  2911 00005B4E 0315[00750000]      <1> 	add 	edx, [u.count]	
  2912                              <1> 		; add u.count,r2 / no. of bytes to be written
  2913                              <1> 			       ; / + file offset is put in r2
  2914                              <1> 	;; 16/06/2015        
  2915                              <1> 	;cmp	edx, 65535 ; file size limit (for UNIX v1 file system)
  2916                              <1> 	;jna	short dskw_0
  2917                              <1> 	;mov	dword [u.error], ERR_FILE_SIZE ; 'file size error !'
  2918                              <1> 	;jmp	error
  2919                              <1> dskw_0:	
  2920                              <1> 	; 28/11/2021
  2921 00005B54 3B15[7C700000]      <1> 	cmp	edx, [i.size] ; 32 bit file size (runix v2 inode)
  2922                              <1> 	;cmp	dx, [i.size]
  2923                              <1> 	;	; cmp r2,i.size / is this greater than the present size of
  2924                              <1> 		              ; / the file?
  2925 00005B5A 760B                <1> 	jna	short dskw_1
  2926                              <1> 		; blos 1f / no, branch
  2927 00005B5C 8915[7C700000]      <1> 	mov	[i.size], edx ; 32 bit file size (runix v2 inode)
  2928                              <1> 	;mov	[i.size], dx
  2929                              <1> 	 	; mov r2,i.size / yes, increase the file size to 
  2930                              <1> 			      ; / file offset + no. of data bytes
  2931 00005B62 E8BFFAFFFF          <1> 	call	setimod
  2932                              <1> 	 	; jsr r0,setimod / set imod=1 (i.e., core inode has been
  2933                              <1> 		          ; / modified), stuff time of modification into
  2934                              <1> 	          	  ; / core image of i-node
  2935                              <1> dskw_1: ; 1:
  2936                              <1> 	; 28/11/2021
  2937                              <1> 	;call	mget
  2938 00005B67 E8ADF5FFFF          <1> 	call	mget_w
  2939                              <1> 		; jsr r0,mget / get the block no. in which to write 
  2940                              <1> 			    ; /	the next data byte
  2941                              <1> 	; eax = physical block/sector number
  2942 00005B6C 8B1D[EC740000]      <1> 	mov     ebx, [u.fofp]
  2943 00005B72 8B13                <1> 	mov	edx, [ebx]
  2944 00005B74 81E2FF010000        <1> 	and	edx, 1FFh  
  2945                              <1> 		; bit *u.fofp,$777 / test the lower 9 bits of the file offset
  2946 00005B7A 750C                <1> 	jnz	short dskw_2
  2947                              <1> 		; bne 2f / if its non-zero, branch; if zero, file offset = 0,
  2948                              <1> 		       ; / 512, 1024,...(i.e., start of new block)
  2949 00005B7C 813D[00750000]0002- <1> 	cmp	dword [u.count], 512
  2949 00005B84 0000                <1>
  2950                              <1> 		; cmp u.count,$512. / if zero, is there enough data to fill
  2951                              <1> 				  ; / an entire block? (i.e., no. of
  2952 00005B86 7305                <1> 	jnb	short dskw_3
  2953                              <1> 		; bhis 3f / bytes to be written greater than 512.? 
  2954                              <1> 			; / Yes, branch. Don't have to read block
  2955                              <1> dskw_2: ; 2: / in as no past info. is to be saved (the entire block will be
  2956                              <1>    		; / overwritten).
  2957 00005B88 E8C4050000          <1> 	call	dskrd
  2958                              <1> 		; jsr r0,dskrd / no, must retain old info.. 
  2959                              <1> 			     ; / Hence, read block 'r1' into an I/O buffer
  2960                              <1> dskw_3: ; 3:
  2961                              <1> 	; EAX (r1) = block/sector number
  2962 00005B8D E81F060000          <1> 	call	wslot
  2963                              <1> 		; jsr r0,wslot / set write and inhibit bits in I/O queue, 
  2964                              <1> 			   ; / proc. status=0, r5 points to 1st word of data
  2965 00005B92 803D[4A750000]00    <1> 	cmp	byte [u.kcall], 0
  2966 00005B99 770F                <1> 	ja	short dskw_5 ; zf=0 -> the caller is 'mkdir'
  2967                              <1> 	;
  2968 00005B9B 66833D[44750000]00  <1> 	cmp	word [u.pcount], 0
  2969 00005BA3 7705                <1> 	ja	short dskw_5
  2970                              <1> dskw_4:
  2971                              <1> 	; [u.base] = virtual address to transfer (as source address)
  2972 00005BA5 E80AFEFFFF          <1> 	call	trans_addr_r ; translate virtual address to physical (r)
  2973                              <1> dskw_5:
  2974                              <1> 	; eBX (r5) = system (I/O) buffer address
  2975 00005BAA E87D000000          <1> 	call	sioreg
  2976                              <1> 		; jsr r0,sioreg / r3 = no. of bytes of data, 
  2977                              <1> 			     ; / r1 = address of data, r2 points to location
  2978                              <1> 			     ; / in buffer in which to start writing data
  2979                              <1> 	; eSI = file (user data) offset
  2980                              <1> 	; eDI = sector (I/O) buffer offset
  2981                              <1> 	; eCX = byte count
  2982                              <1> 	;
  2983 00005BAF F3A4                <1>   	rep	movsb
  2984                              <1> 		; movb (r1 )+,(r2)+ 
  2985                              <1> 		         ; / transfer a byte of data to the I/O buffer
  2986                              <1> 		; dec r3 / decrement no. of bytes to be written
  2987                              <1> 		; bne 2b / have all bytes been transferred? No, branch
  2988                              <1> 	; 25/07/2015
  2989                              <1> 	; eax = remain bytes in buffer
  2990                              <1>         ;       (check if remain bytes in the buffer > [u.pcount])
  2991 00005BB1 09C0                <1> 	or	eax, eax
  2992 00005BB3 75F0                <1> 	jnz	short dskw_4 ; (page end before system buffer end!)	
  2993                              <1> dskw_6:
  2994 00005BB5 E813060000          <1> 	call	dskwr
  2995                              <1> 		; jsr r0,dskwr / yes, write the block and the i-node
  2996 00005BBA 833D[00750000]00    <1>         cmp     dword [u.count], 0
  2997                              <1> 		; tst u.count / any more data to write?
  2998 00005BC1 77A4                <1> 	ja	short dskw_1
  2999                              <1> 		; bne 1b / yes, branch
  3000                              <1> 	; 03/08/2013
  3001 00005BC3 C605[4A750000]00    <1> 	mov	byte [u.kcall], 0
  3002                              <1> 	; 20/09/2013 (;;)
  3003                              <1> 	;pop	ax
  3004                              <1> 	; 28/11/2021
  3005 00005BCA 58                  <1> 	pop	eax  
  3006 00005BCB C3                  <1> 	retn
  3007                              <1> 	;;jmp 	short dskw_ret 
  3008                              <1> 	        ; jmp ret / no, return to the caller via 'ret'
  3009                              <1> 
  3010                              <1> 	; 24/12/2021
  3011                              <1> 	; 04/12/2021 - Retro UNIX 386 v1.2
  3012                              <1> cpass: ; / get next character from user area of core and put it in r1
  3013                              <1> 	; 18/10/2015
  3014                              <1> 	; 10/10/2015
  3015                              <1> 	; 10/07/2015
  3016                              <1> 	; 02/07/2015
  3017                              <1> 	; 01/07/2015
  3018                              <1> 	; 24/06/2015
  3019                              <1> 	; 08/06/2015
  3020                              <1> 	; 04/06/2015
  3021                              <1> 	; 20/05/2015
  3022                              <1> 	; 19/05/2015 (Retro UNIX 386 v1 - Beginning)
  3023                              <1> 	;
  3024                              <1> 	; INPUTS -> 
  3025                              <1> 	;     [u.base] = virtual address in user area
  3026                              <1> 	;     [u.count] = byte count (max.)
  3027                              <1> 	;     [u.pcount] = byte count in page (0 = reset)		
  3028                              <1> 	; OUTPUTS -> 
  3029                              <1> 	;     AL = the character which is pointed by [u.base]
  3030                              <1> 	;     zf = 1 -> transfer count has been completed	
  3031                              <1>         ;
  3032                              <1> 	; ((Modified registers: EAX, EDX, ECX))
  3033                              <1> 	;
  3034                              <1> 	;
  3035 00005BCC 833D[00750000]00    <1> 	cmp 	dword [u.count], 0  ; 14/08/2013
  3036                              <1> 		; tst u.count / have all the characters been transferred
  3037                              <1> 			    ; / (i.e., u.count, # of chars. left
  3038 00005BD3 763F                <1> 	jna	short cpass_3
  3039                              <1> 		; beq 1f / to be transferred = 0?) yes, branch
  3040 00005BD5 FF0D[00750000]      <1> 	dec	dword [u.count]
  3041                              <1> 		; dec u.count / no, decrement u.count
  3042                              <1>         ; 19/05/2015 
  3043                              <1> 	;(Retro UNIX 386 v1 - translation from user's virtual address
  3044                              <1> 	;		      to physical address
  3045 00005BDB 66833D[44750000]00  <1> 	cmp	word [u.pcount], 0 ; byte count in page = 0 (initial value)
  3046                              <1> 			     ; 1-4095 --> use previous physical base address
  3047                              <1> 			     ; in [u.pbase]
  3048 00005BE3 770E                <1> 	ja	short cpass_1
  3049                              <1> 	; 02/07/2015
  3050 00005BE5 833D[3C750000]00    <1> 	cmp	dword [u.ppgdir], 0  ; is the caller os kernel
  3051 00005BEC 7427                <1> 	je	short cpass_k 	; (sysexec, '/etc/init') ? 
  3052                              <1> 	; 08/06/2015 - 10/07/2015
  3053 00005BEE E8C1FDFFFF          <1> 	call	trans_addr_r
  3054                              <1> cpass_1:
  3055                              <1> 	; 02/07/2015
  3056                              <1> 	; 24/06/2015
  3057 00005BF3 66FF0D[44750000]    <1> 	dec	word [u.pcount]
  3058                              <1> cpass_2: 
  3059                              <1> 	; 10/10/2015
  3060                              <1> 	; 02/07/2015
  3061 00005BFA 8B15[40750000]      <1> 	mov	edx, [u.pbase]
  3062 00005C00 8A02                <1> 	mov	al, [edx] ; 10/10/2015
  3063                              <1> 		; movb *u.base,r1 / take the character pointed to 
  3064                              <1> 				; / by u.base and put it in r1
  3065 00005C02 FF05[04750000]      <1> 	inc	dword [u.nread]
  3066                              <1> 		; inc u.nread / increment no. of bytes transferred
  3067 00005C08 FF05[FC740000]      <1> 	inc	dword [u.base]
  3068                              <1> 		; inc u.base / increment the buffer address to point to the
  3069                              <1> 			   ; / next byte
  3070 00005C0E FF05[40750000]      <1> 	inc	dword [u.pbase] ; 04/06/2015
  3071                              <1> cpass_3:
  3072 00005C14 C3                  <1> 	retn
  3073                              <1> 		; rts	r0 / next byte
  3074                              <1> 	; 1: 
  3075                              <1> 		; mov (sp)+,r0 
  3076                              <1> 		         ; / put return address of calling routine into r0
  3077                              <1> 		; mov (sp)+,r1 / i-number in r1
  3078                              <1> 		; rts r0 / non-local return
  3079                              <1> cpass_k:
  3080                              <1> 	; 02/07/2015
  3081                              <1> 	; The caller is os kernel 
  3082                              <1> 	; (get sysexec arguments from kernel's memory space)
  3083                              <1> 	;
  3084 00005C15 8B1D[FC740000]      <1> 	mov	ebx, [u.base]
  3085 00005C1B 66C705[44750000]00- <1>         mov     word [u.pcount], PAGE_SIZE ; 4096
  3085 00005C23 10                  <1>
  3086 00005C24 891D[40750000]      <1> 	mov	[u.pbase], ebx
  3087 00005C2A EBCE                <1> 	jmp	short cpass_2
  3088                              <1> 	
  3089                              <1> sioreg: 
  3090                              <1> 	; 25/07/2015
  3091                              <1> 	; 18/07/2015
  3092                              <1> 	; 02/07/2015
  3093                              <1> 	; 17/06/2015
  3094                              <1> 	; 09/06/2015
  3095                              <1> 	; 19/05/2015 (Retro UNIX 386 v1 - Beginning)
  3096                              <1> 	; 12/03/2013 - 22/07/2013 (Retro UNIX 8086 v1)
  3097                              <1> 	;
  3098                              <1> 	; INPUTS -> 
  3099                              <1> 	;     eBX = system buffer (data) address (r5)
  3100                              <1> 	;     [u.fofp] = pointer to file offset pointer
  3101                              <1> 	;     [u.base] = virtual address of the user buffer
  3102                              <1> 	;     [u.pbase] = physical address of the user buffer
  3103                              <1> 	;     [u.count] = byte count
  3104                              <1> 	;     [u.pcount] = byte count within page frame 			
  3105                              <1> 	; OUTPUTS -> 
  3106                              <1> 	;     eSI = user data offset (r1)
  3107                              <1> 	;     eDI = system (I/O) buffer offset (r2)
  3108                              <1> 	;     eCX = byte count (r3)
  3109                              <1> 	;     EAX = remain bytes after byte count within page frame
  3110                              <1> 	;	(If EAX > 0, transfer will continue from the next page)
  3111                              <1>         ;
  3112                              <1> 	; ((Modified registers:  EDX))
  3113                              <1>  
  3114 00005C2C 8B35[EC740000]      <1>         mov     esi, [u.fofp]
  3115 00005C32 8B3E                <1>         mov     edi, [esi]
  3116                              <1> 		; mov *u.fofp,r2 / file offset (in bytes) is moved to r2
  3117 00005C34 89F9                <1> 	mov	ecx, edi
  3118                              <1> 		; mov r2,r3 / and also to r3
  3119 00005C36 81C900FEFFFF        <1> 	or	ecx, 0FFFFFE00h
  3120                              <1> 		; bis $177000,r3 / set bits 9,...,15 of file offset in r3
  3121 00005C3C 81E7FF010000        <1> 	and	edi, 1FFh
  3122                              <1> 		; bic $!777,r2 / calculate file offset mod 512.
  3123 00005C42 01DF                <1> 	add	edi, ebx ; EBX = system buffer (data) address
  3124                              <1> 		; add r5,r2 / r2 now points to 1st byte in system buffer
  3125                              <1> 			  ; / where data is to be placed
  3126                              <1>                 ; mov u.base,r1 / address of data is in r1
  3127 00005C44 F7D9                <1> 	neg	ecx
  3128                              <1> 		; neg r3 / 512 - file offset (mod512.) in r3 
  3129                              <1> 		       ; / (i.e., the no. of free bytes in the file block)
  3130 00005C46 3B0D[00750000]      <1> 	cmp	ecx, [u.count]
  3131                              <1> 		; cmp r3,u.count / compare this with the no. of data bytes
  3132                              <1> 			       ; / to be written to the file
  3133 00005C4C 7606                <1> 	jna	short sioreg_0
  3134                              <1> 		; blos	2f / if less than branch. Use the no. of free bytes
  3135                              <1> 			 ; / in the file block as the number to be written
  3136 00005C4E 8B0D[00750000]      <1> 	mov	ecx, [u.count]
  3137                              <1> 		; mov u.count,r3 / if greater than, use the no. of data 
  3138                              <1> 			       ; / bytes as the number to be written
  3139                              <1> sioreg_0:
  3140                              <1> 	; 17/06/2015
  3141 00005C54 803D[4A750000]00    <1> 	cmp	byte [u.kcall], 0 
  3142 00005C5B 7613                <1> 	jna	short sioreg_1
  3143                              <1> 	; 25/07/2015
  3144                              <1> 	 ; the caller is 'mkdir' or 'namei'
  3145 00005C5D A1[FC740000]        <1> 	mov	eax, [u.base] ; 25/07/2015
  3146 00005C62 A3[40750000]        <1> 	mov 	[u.pbase], eax ; physical address = virtual address
  3147 00005C67 66890D[44750000]    <1> 	mov	word [u.pcount], cx ; remain bytes in buffer (1 sector)
  3148 00005C6E EB0B                <1> 	jmp	short sioreg_2
  3149                              <1> sioreg_1:
  3150                              <1> 	; 25/07/2015
  3151                              <1> 	; 18/07/2015
  3152                              <1> 	; 09/06/2015 
  3153 00005C70 0FB715[44750000]    <1> 	movzx	edx, word [u.pcount]
  3154                              <1> 		; ecx and [u.pcount] are always > 0, here
  3155 00005C77 39D1                <1> 	cmp	ecx, edx	
  3156 00005C79 772A                <1> 	ja	short sioreg_4 ; transfer count > [u.pcount]
  3157                              <1> sioreg_2: ; 2:
  3158 00005C7B 31C0                <1> 	xor 	eax, eax ; 25/07/2015
  3159                              <1> sioreg_3:
  3160 00005C7D 010D[04750000]      <1> 	add 	[u.nread], ecx
  3161                              <1> 		; add r3,u.nread / r3 + number of bytes xmitted 
  3162                              <1> 			         ; / during write is put into u.nread
  3163 00005C83 290D[00750000]      <1> 	sub 	[u.count], ecx
  3164                              <1> 		; sub r3,u.count / u.count = no. of bytes that still 
  3165                              <1> 			       ; / must be written or read
  3166 00005C89 010D[FC740000]      <1> 	add 	[u.base], ecx
  3167                              <1> 		; add r3,u.base / u.base points to the 1st of the remaining
  3168                              <1> 			      ; / data bytes
  3169 00005C8F 010E                <1>         add 	[esi], ecx 
  3170                              <1> 		; add r3,*u.fofp / new file offset = number of bytes done
  3171                              <1> 			       ; / + old file offset
  3172                              <1> 	; 25/07/2015
  3173 00005C91 8B35[40750000]      <1> 	mov	esi, [u.pbase]
  3174 00005C97 66290D[44750000]    <1> 	sub	[u.pcount], cx
  3175 00005C9E 010D[40750000]      <1> 	add	[u.pbase], ecx
  3176 00005CA4 C3                  <1>         retn
  3177                              <1> 		; rts r0
  3178                              <1> 		; transfer count > [u.pcount]
  3179                              <1> sioreg_4:
  3180                              <1> 	; 25/07/2015
  3181                              <1> 	; transfer count > [u.pcount] 
  3182                              <1> 	; (ecx > edx)
  3183 00005CA5 89C8                <1> 	mov	eax, ecx
  3184 00005CA7 29D0                <1> 	sub	eax, edx ; remain bytes for 1 sector (block) transfer 
  3185 00005CA9 89D1                <1> 	mov	ecx, edx ; current transfer count = [u.pcount]
  3186 00005CAB EBD0                <1> 	jmp	short sioreg_3
  3187                              <1> 
  3188                              <1> 	; 28/11/2021 - Retro UNIX 386 v2 fs compatibility modification
  3189                              <1> is_regular_file:
  3190                              <1> 	; check if it is a regular file or device file inode
  3191                              <1> 	;
  3192                              <1> 	; INPUT:
  3193                              <1> 	;	current inode
  3194                              <1> 	; OUTPUT:
  3195                              <1> 	;	cf = 0 and zf = 1 -> regular file
  3196                              <1> 	;	cf = 0 and zf = 0 -> device file
  3197                              <1> 	;	cf = 1 -> invalid file (jump to 'error')
  3198                              <1> 	;
  3199                              <1> 	; Modified registers: ecx (cl)
  3200                              <1> 
  3201                              <1> 	; 28/11/2021
  3202 00005CAD 8A0D[75700000]      <1> 	mov	cl, [i.flgs+1]
  3203 00005CB3 F6C180              <1> 	test	cl, 80h		; regular file ?
  3204 00005CB6 7514                <1> 	jnz	short isregf_1  ; yes 
  3205 00005CB8 80E120              <1> 	and	cl, 20h 	; device file ?
  3206 00005CBB 7511                <1> 	jnz	short isregf_2  ; yes 
  3207                              <1> 
  3208                              <1> 	; 28/11/2021
  3209                              <1> 	; invalid inode/file type !
  3210 00005CBD C705[4C750000]FF00- <1> 	mov	dword [u.error], ERR_INV_FILE ; 0FFh ; invalid file
  3210 00005CC5 0000                <1>
  3211                              <1> 	;stc
  3212                              <1> 	;retn
  3213 00005CC7 E9DEDEFFFF          <1> 	jmp	error
  3214                              <1> isregf_1:
  3215 00005CCC 30C9                <1> 	xor	cl, cl  ; cl = 0
  3216                              <1> 	; zf = 1
  3217                              <1> 	;retn
  3218                              <1> isregf_2:
  3219                              <1> 	; zf = 0
  3220                              <1> 	; cl = 20h
  3221 00005CCE C3                  <1> 	retn	
  2076                                  %include 'u7.s'      ; 18/04/2015
  2077                              <1> ; Temporary Runix kernel v2.0 file for debug - 22/11/2021
  2078                              <1> ; (re-write kernel for test by using previous version without a major defect)
  2079                              <1> ; ****************************************************************************
  2080                              <1> ; Retro UNIX 386 v1 Kernel (v0.2) - SYS7.INC
  2081                              <1> ; Last Modification: 19/01/2022
  2082                              <1> ; ----------------------------------------------------------------------------
  2083                              <1> ; Derived from 'Retro UNIX 8086 v1' source code by Erdogan Tan
  2084                              <1> ; (v0.1 - Beginning: 11/07/2012)
  2085                              <1> ;
  2086                              <1> ; Derived from UNIX Operating System (v1.0 for PDP-11) 
  2087                              <1> ; (Original) Source Code by Ken Thompson (1971-1972)
  2088                              <1> ; <Bell Laboratories (17/3/1972)>
  2089                              <1> ; <Preliminary Release of UNIX Implementation Document>
  2090                              <1> ;
  2091                              <1> ; Retro UNIX 8086 v1 - U7.ASM (13/07/2014) //// UNIX v1 -> u7.s
  2092                              <1> ;
  2093                              <1> ; ****************************************************************************
  2094                              <1> 
  2095                              <1> sysmount: ; / mount file system; args special; name
  2096                              <1> 	; 11/01/2022 (Retro UNIX 386 v1.2)
  2097                              <1> 	; 14/11/2015
  2098                              <1> 	; 24/10/2015
  2099                              <1> 	; 13/10/2015
  2100                              <1> 	; 10/07/2015
  2101                              <1> 	; 16/05/2015 (Retro UNIX 386 v1 - Beginning)
  2102                              <1> 	; 09/07/2013 - 04/11/2013 (Retro UNIX 8086 v1)
  2103                              <1> 	;
  2104                              <1> 	; 'sysmount' anounces to the system that a removable 
  2105                              <1> 	; file system has been mounted on a special file.
  2106                              <1> 	; The device number of the special file is obtained via
  2107                              <1> 	; a call to 'getspl'. It is put in the I/O queue entry for
  2108                              <1> 	; dismountable file system (sb1) and the I/O queue entry is
  2109                              <1> 	; set up to read (bit 10 is set). 'ppoke' is then called to
  2110                              <1> 	; to read file system into core, i.e. the first block on the
  2111                              <1> 	; mountable file system is read in. This block is super block
  2112                              <1> 	; for the file system. This call is super user restricted.	
  2113                              <1> 	;
  2114                              <1> 	; Calling sequence:
  2115                              <1> 	;	sysmount; special; name
  2116                              <1> 	; Arguments:
  2117                              <1> 	;	special - pointer to name of special file (device)
  2118                              <1> 	;	name -  pointer to name of the root directory of the
  2119                              <1> 	;		newly mounted file system. 'name' should 
  2120                              <1> 	;		always be a directory.
  2121                              <1> 	; Inputs: - 
  2122                              <1> 	; Outputs: -
  2123                              <1> 	; ...............................................................
  2124                              <1> 	;				
  2125                              <1> 	; Retro UNIX 8086 v1 modification: 
  2126                              <1> 	;       'sysmount' system call has two arguments; so,
  2127                              <1> 	;	* 1st argument, special is pointed to by BX register
  2128                              <1> 	;	* 2nd argument, name is in CX register
  2129                              <1> 	;
  2130                              <1> 	;	NOTE: Device numbers, names and related procedures are 
  2131                              <1> 	;	       already modified for IBM PC compatibility and 
  2132                              <1> 	;	       Retro UNIX 8086 v1 device configuration.	
  2133                              <1> 	
  2134                              <1> 	;call	arg2
  2135                              <1> 		; jsr r0,arg2 / get arguments special and name
  2136 00005CCF 891D[F4740000]      <1> 	mov	[u.namep], ebx
  2137 00005CD5 51                  <1> 	push	ecx ; directory name
  2138                              <1> 	;cmp	dword [mnti], 0 ; 11/01/2022 (32 bit inode number)
  2139 00005CD6 66833D[C4740000]00  <1> 	cmp	word [mnti], 0
  2140                              <1> 		; tst mnti / is the i-number of the cross device file
  2141                              <1> 			 ; / zero?
  2142                              <1> 	;;ja	error
  2143                              <1>         	; bne errora / no, error
  2144                              <1> 	;ja	sysmnt_err0
  2145                              <1> 	; 11/01/2022
  2146 00005CDE 7605                <1> 	jna	short sysmnt_0 ; 12/01/2022
  2147 00005CE0 E9D6000000          <1> 	jmp	sysmnt_err0
  2148                              <1> sysmnt_0:
  2149 00005CE5 E8B9000000          <1> 	call	getspl
  2150                              <1> 		; jsr r0,getspl / get special files device number in r1
  2151                              <1> 	; 13/10/2015
  2152                              <1> 	;movzx	ebx, ax ; Retro UNIX 8086 v1 device number (0 to 5)
  2153                              <1>         ; 11/01/2022
  2154 00005CEA 29DB                <1> 	sub	ebx, ebx
  2155 00005CEC 88C3                <1> 	mov	bl, al
  2156 00005CEE F683[FA6A0000]80    <1> 	test    byte [ebx+drv.status], 80h ; 24/10/2015 
  2157 00005CF5 750F                <1> 	jnz	short sysmnt_1
  2158                              <1> sysmnt_err1:
  2159 00005CF7 C705[4C750000]0F00- <1>         mov     dword [u.error], ERR_DRV_NOT_RDY ; drive not ready !
  2159 00005CFF 0000                <1>
  2160 00005D01 E9A4DEFFFF          <1> 	jmp	error
  2161                              <1> sysmnt_1:
  2162 00005D06 8F05[F4740000]      <1> 	pop	dword [u.namep]
  2163                              <1>         	; mov (sp)+,u.namep / put the name of file to be placed
  2164                              <1> 				  ; / on the device
  2165                              <1> 	; 14/11/2015
  2166 00005D0C 53                  <1> 	push	ebx ; 13/10/2015
  2167                              <1> 		; mov r1,-(sp) / save the device number
  2168                              <1>         ;
  2169 00005D0D E8DAECFFFF          <1> 	call	namei
  2170                              <1> 	;or	ax, ax ; Retro UNIX 8086 v1 modification !
  2171                              <1> 		       ; ax = 0 -> file not found 	
  2172                              <1> 	;jz	error
  2173                              <1> 	;jc	error
  2174                              <1> 		; jsr r0,namei / get the i-number of the file
  2175                              <1>                	; br errora
  2176 00005D12 730F                <1> 	jnc	short sysmnt_2
  2177                              <1> sysmnt_err2:
  2178 00005D14 C705[4C750000]0C00- <1>         mov     dword [u.error], ERR_FILE_NOT_FOUND ; drive not ready !
  2178 00005D1C 0000                <1>
  2179 00005D1E E987DEFFFF          <1> 	jmp	error
  2180                              <1> sysmnt_2:
  2181                              <1> 	; 11/01/2022
  2182 00005D23 A3[C4740000]        <1> 	mov	[mnti], eax ; 32 bit inode number (<= 65535)	
  2183                              <1> 	;mov	[mnti], ax
  2184                              <1>         	; mov r1,mnti / put it in mnti
  2185                              <1> 	; 11/01/2022
  2186 00005D28 BB[84770000]        <1> 	mov	ebx, sb1 ; super block buffer header (of mounted disk)
  2187                              <1> sysmnt_3: ;1:
  2188                              <1>         ;cmp	byte [ebx+1], 0
  2189                              <1> 		; tstb sb1+1 / is 15th bit of I/O queue entry for
  2190                              <1> 			   ; / dismountable device set?
  2191                              <1>         ;jna	short sysmnt_4		
  2192                              <1> 		; bne 1b / (inhibit bit) yes, skip writing
  2193                              <1> 	;call	idle 	; (wait for hardware interrupt)
  2194                              <1> 	;jmp	short sysmnt_3
  2195                              <1> sysmnt_4:   
  2196 00005D2D 58                  <1> 	pop	eax ; Retro UNIX 8086 v1 device number/ID (0 to 5)     
  2197 00005D2E A2[BB740000]        <1> 	mov	[mdev], al
  2198                              <1> 		; mov (sp),mntd / no, put the device number in mntd
  2199 00005D33 8803                <1> 	mov	[ebx], al
  2200                              <1>         	; movb (sp),sb1 / put the device number in the lower byte
  2201                              <1> 			      ; / of the I/O queue entry
  2202                              <1> 	;mov	byte [cdev], 1 ; mounted device/drive
  2203                              <1>         	; mov (sp)+,cdev / put device number in cdev
  2204 00005D35 66810B0004          <1>         or	word [ebx], 400h ; Bit 10, 'read' flag/bit
  2205                              <1> 		; bis $2000,sb1 / set the read bit
  2206                              <1> 	; Retro UNIX 386 v1 modification : 
  2207                              <1> 	;	32 bit block number at buffer header offset 4
  2208 00005D3A C7430401000000      <1> 	mov	dword [ebx+4], 1 ; physical block number = 1
  2209 00005D41 E8C9050000          <1> 	call 	diskio
  2210 00005D46 731B                <1> 	jnc	short sysmnt_5
  2211 00005D48 31C0                <1> 	xor 	eax, eax
  2212                              <1> 	;mov	[mnti], ax ; 0
  2213 00005D4A A3[C4740000]        <1> 	mov	[mnti], eax ; 11/01/2022
  2214 00005D4F A2[BB740000]        <1> 	mov	[mdev], al ; 0
  2215                              <1> 	;mov	[cdev], al ; 0
  2216                              <1> sysmnt_invd:
  2217                              <1> 	; 14/11/2015
  2218 00005D54 FEC8                <1> 	dec 	al
  2219 00005D56 8903                <1> 	mov	[ebx], eax ; 000000FFh
  2220 00005D58 FEC0                <1> 	inc	al
  2221 00005D5A 48                  <1> 	dec	eax
  2222 00005D5B 894304              <1> 	mov	[ebx+4], eax ; 0FFFFFFFFh
  2223 00005D5E E947DEFFFF          <1> 	jmp	error
  2224                              <1> sysmnt_5:
  2225                              <1> 	; 11/01/2022 (Retro UNIX 386 v1.1 BugFix)
  2226                              <1> 	;  v1.2 Note: This code will be changed for hard disk partitions
  2227                              <1> 	;	    ! (for now, rufs v2 floppy disks can be mounted) !		
  2228                              <1> 	; 14/11/2015 (Retro UNIX 386 v1 modification)
  2229                              <1> 	; (Following check is needed to prevent mounting an
  2230                              <1> 	; invalid file system (invalid super block).
  2231                              <1> 	; 
  2232 00005D63 0FB603              <1> 	movzx	eax, byte [ebx] ; device number
  2233 00005D66 C0E002              <1> 	shl	al, 2 ; 4*index
  2234 00005D69 8B88[DE6A0000]      <1> 	mov	ecx, [eax+drv.size] ; volume (fs) size
  2235                              <1> 	;shr 	ecx, 3 ; 11/01/2021 (8 sectors per 1 fbm byte)
  2236                              <1> 	; ecx = number of free map bytes (required)
  2237                              <1> 	;;movzx	edx, word [sb1+8] ; the 1st data word ('mount:')
  2238                              <1> 	;movzx	edx, word [ebx+8] ; the 1st data word (of the buffer)	
  2239                              <1> 	; edx = number of free blocks map bytes
  2240                              <1> 	;;shl	edx, 3 ; convert free map bytes to free map bits
  2241                              <1> 	;cmp	ecx, edx ; compare free map bits and volume size
  2242                              <1> 	;		 ; (in sectors), if they are not equal
  2243                              <1> 	;		 ; the disk to be mounted is an...
  2244                              <1> 	;jne	short sysmnt_invd ; invalid disk !
  2245                              <1> 	;		 ; (which has not got a valid super bloc	
  2246                              <1> 	; 11/01/2022
  2247                              <1> 	; Retro UNIX 386 v2 superblock (offset 8, volume size)
  2248 00005D6F 8B5310              <1> 	mov	edx, [ebx+16] ; SB.VolumeSize (in sectors)
  2249 00005D72 39D1                <1> 	cmp	ecx, edx ; compare SB.VolumeSize and Disk volume size
  2250                              <1> 			 ; (in sectors), if they are not equal
  2251                              <1> 			 ; the disk to be mounted is an...	
  2252 00005D74 75DE                <1> 	jne	short sysmnt_invd ; invalid disk !
  2253                              <1> 			 ; (which has not got a valid super block)
  2254                              <1> 	;
  2255 00005D76 C6430100            <1> 	mov	byte [ebx+1], 0
  2256                              <1> 	       	; jsr r0,ppoke / read in entire file system
  2257                              <1> ;sysmnt_6: ;1:
  2258                              <1> 	;;cmp	byte [sb1+1], 0
  2259                              <1> 		; tstb sb1+1 / done reading?
  2260                              <1>    	;;jna	sysret
  2261                              <1> 	;;call	idle ; (wait for hardware interrupt)
  2262                              <1> 	;;jmp	short sysmnt_6
  2263                              <1> 		;bne 1b / no, wait
  2264                              <1>         	;br sysreta / yes
  2265 00005D7A E94BDEFFFF          <1> 	jmp	sysret
  2266                              <1> 
  2267                              <1> sysumount: ; / special dismount file system
  2268                              <1> 	; 11/01/2022 (Retro UNIX 386 v1.2)
  2269                              <1> 	; 16/05/2015 (Retro UNIX 386 v1 - Beginning)
  2270                              <1> 	; 09/07/2013 - 04/11/2013 (Retro UNIX 8086 v1)
  2271                              <1> 	;
  2272                              <1> 	; 04/11/2013
  2273                              <1> 	; 09/07/2013
  2274                              <1> 	; 'sysumount' anounces to the system that the special file, 
  2275                              <1> 	; indicated as an argument is no longer contain a removable
  2276                              <1> 	; file system. 'getspl' gets the device number of the special
  2277                              <1> 	; file. If no file system was mounted on that device an error
  2278                              <1> 	; occurs. 'mntd' and 'mnti' are cleared and control is passed
  2279                              <1> 	; to 'sysret'.
  2280                              <1> 	;
  2281                              <1> 	; Calling sequence:
  2282                              <1> 	;	sysmount; special
  2283                              <1> 	; Arguments:
  2284                              <1> 	;	special - special file to dismount (device)
  2285                              <1> 	;
  2286                              <1> 	; Inputs: - 
  2287                              <1> 	; Outputs: -
  2288                              <1> 	; ...............................................................
  2289                              <1> 	;				
  2290                              <1> 	; Retro UNIX 8086 v1 modification: 
  2291                              <1> 	;       'sysumount' system call has one argument; so,
  2292                              <1> 	;	* Single argument, special is pointed to by BX register
  2293                              <1> 	;
  2294                              <1> 	
  2295                              <1> 	;mov 	ax, 1 ; one/single argument, put argument in BX	
  2296                              <1> 	;call	arg
  2297                              <1> 		; jsr r0,arg; u.namep / point u.namep to special
  2298 00005D7F 891D[F4740000]      <1>         mov	[u.namep], ebx
  2299 00005D85 E819000000          <1> 	call	getspl
  2300                              <1> 		; jsr r0,getspl / get the device number in r1
  2301 00005D8A 3A05[BB740000]      <1> 	cmp	al, [mdev]
  2302                              <1> 		; cmp r1,mntd / is it equal to the last device mounted?
  2303 00005D90 7529                <1> 	jne	short sysmnt_err0 ; 'permission denied !' error
  2304                              <1> 	;jne	error
  2305                              <1>         	; bne errora / no error
  2306 00005D92 30C0                <1> 	xor	al, al ; ah = 0
  2307                              <1> 	; (eax = 0) ; 11/01/2022
  2308                              <1> sysumnt_0: ;1:
  2309                              <1>      	; 11/01/2022
  2310                              <1> 	;cmp 	[sb1+1], al ; 0
  2311                              <1> 	;	; tstb sb1+1 / yes, is the device still doing I/O 
  2312                              <1> 	;		   ; / (inhibit bit set)?
  2313                              <1> 	;jna	short sysumnt_1		
  2314                              <1> 	;	; bne 1b / yes, wait
  2315                              <1> 	;call	idle ; (wait for hardware interrupt)
  2316                              <1> 	;jmp	short sysumnt_0
  2317                              <1> sysumnt_1:        
  2318 00005D94 A2[BB740000]        <1> 	mov	[mdev], al ; 0
  2319                              <1> 	     	; clr mntd / no, clear these
  2320 00005D99 A3[C4740000]        <1>    	mov	[mnti], eax ; 11/01/2022 (eax = 0)
  2321                              <1> 	;mov	[mnti], ax ; 0
  2322                              <1>         	; clr mnti
  2323 00005D9E E927DEFFFF          <1>         jmp	sysret
  2324                              <1> 		; br sysreta / return
  2325                              <1> 
  2326                              <1> 	; 11/01/2022 - Retro UNIX 386 v1.2
  2327                              <1> 	;	(runix v2 fs inode numbers)
  2328                              <1> getspl: ; / get device number from a special file name
  2329 00005DA3 E844ECFFFF          <1> 	call	namei
  2330                              <1> 	;or	ax, ax ; Retro UNIX 8086 v1 modification !
  2331                              <1> 		       ; ax = 0 -> file not found 	
  2332                              <1> 	;jc	sysmnt_err2 ; 'file not found !' error
  2333                              <1> 	; 11/01/2022 (Retro UNIX 386 v1.2)
  2334 00005DA8 7305                <1> 	jnc	short getspl_0
  2335 00005DAA E965FFFFFF          <1> 	jmp	sysmnt_err2
  2336                              <1> getspl_0:
  2337                              <1> 	;jz	error
  2338                              <1> 	;jc	error
  2339                              <1> 		; jsr r0,namei / get the i-number of the special file
  2340                              <1>                 ; br errora / no such file
  2341                              <1>         ; 11/01/2022 - Retro UNIX 386 v1.2 (runix v2 fs inode numbers)
  2342 00005DAF 6683E80A            <1> 	sub	ax, 10 ; fd0 inode number = 10, hd3 inode number = 15
  2343                              <1> 		      ;	i-number-10, 0 = fd0, 5 = hd3 
  2344                              <1> 	;sub	ax, 3 ; Retro UNIX 8086 v1 modification !
  2345                              <1> 		      ;	i-number-3, 0 = fd0, 5 = hd3 
  2346                              <1> 		; sub $4,r1 / i-number-4 rk=1,tap=2+n
  2347 00005DB3 7206                <1>         jc	short sysmnt_err0 ; 'permission denied !' error
  2348                              <1> 	;jc	error
  2349                              <1> 		; ble errora / less than 0?  yes, error
  2350 00005DB5 6683F805            <1>         cmp	ax, 5 ; 5 = hd3
  2351                              <1> 		; cmp  r1,$9. / greater than 9  tap 7
  2352                              <1> 	;ja	short sysmnt_err0 ; 'permission denied !' error
  2353                              <1> 	;;ja	error
  2354                              <1> 		; bgt errora / yes, error
  2355                              <1> 	; 11/01/2022
  2356 00005DB9 760F                <1> 	jna	short getspl_retn
  2357                              <1> 	; AX = Retro UNIX 8086 v1 Device Number (0 to 5)
  2358                              <1> ;iopen_retn:
  2359                              <1> ;	retn
  2360                              <1> 		; rts    r0 / return with device number in r1
  2361                              <1> sysmnt_err0:
  2362 00005DBB C705[4C750000]0B00- <1> 	mov	dword [u.error], ERR_FILE_ACCESS ; permission denied !
  2362 00005DC3 0000                <1>
  2363 00005DC5 E9E0DDFFFF          <1> 	jmp	error
  2364                              <1> 
  2365                              <1> getspl_retn:
  2366                              <1> 	; 11/01/2022
  2367                              <1> 	; eax = Retro UNIX 386 v1 device number (0 to 5)
  2368                              <1> iopen_retn:
  2369 00005DCA C3                  <1> 	retn
  2370                              <1> 
  2371                              <1> 	; 05/12/2021 - Retro UNIX 386 v2 fs compatibility modification
  2372                              <1> iopen:
  2373                              <1> 	; 05/12/2021 - Retro UNIX 386 v1.2
  2374                              <1> 	; 27/03/2021 (Retro UNIX 386 v2)
  2375                              <1> 	; 19/05/2015
  2376                              <1> 	; 18/05/2015 (Retro UNIX 386 v1 - Beginning)
  2377                              <1> 	; 21/05/2013 - 27/08/2013 (Retro UNIX 8086 v1)
  2378                              <1> 	;
  2379                              <1> 	; open file whose i-number is in r1
  2380                              <1> 	; 
  2381                              <1> 	; INPUTS ->
  2382                              <1> 	;    r1 - inode number
  2383                              <1> 	; OUTPUTS ->
  2384                              <1> 	;    file's inode in core	
  2385                              <1> 	;    r1 - inode number (positive)
  2386                              <1> 	;
  2387                              <1> 	; ((AX = R1))
  2388                              <1>         ; ((Modified registers: edx, ebx, ecx, esi, edi, ebp)) 
  2389                              <1> 	;        
  2390                              <1> ; / open file whose i-number is in r1
  2391                              <1> ;	test	ah, 80h ; Bit 15 of AX
  2392                              <1> ;		;tst r1 / write or read access?
  2393                              <1> ;       jnz	short iopen_2
  2394                              <1> ;		;blt 2f / write, go to 2f
  2395                              <1> ;	mov	dl, 2 ; read access
  2396                              <1> ;	call	access
  2397                              <1> ;       	;jsr r0,access; 2 
  2398                              <1> ;	; / get inode into core with read access
  2399                              <1> ;	; DL=2
  2400                              <1> ;iopen_0:
  2401                              <1> ;       cmp	ax, 40
  2402                              <1> ;		; cmp r1,$40. / is it a special file
  2403                              <1> ;       ja	short iopen_retn
  2404                              <1> ;		;bgt  3f / no. 3f
  2405                              <1> 
  2406                              <1> 	; 05/12/2021
  2407                              <1> 	; DL = 0 -> open for read
  2408                              <1> 	; DL = 1 -> open for write
  2409                              <1> 
  2410 00005DCB 52                  <1> 	push	edx ; *
  2411                              <1> 
  2412                              <1> 	; set permission/mode flag for 'access' 
  2413                              <1> 	;	(DX = 100h -> IREAD, DX = 80h -> IWRITE)
  2414                              <1> 
  2415 00005DCC B601                <1> 	mov	dh, 1
  2416 00005DCE 08D2                <1> 	or	dl, dl ; dl = 0, open for read	
  2417 00005DD0 7404                <1> 	jz	short iopen_0 ; DX = IREAD (100h)
  2418                              <1> 	; dl = 1, open for write 
  2419 00005DD2 FECE                <1> 	dec	dh
  2420 00005DD4 B280                <1> 	mov	dl, 80h
  2421                              <1> 	; DX = IWRITE (80h)
  2422                              <1> iopen_0:
  2423                              <1> 	; 27/03/2021
  2424                              <1> 	;   EAX = inode number (in AX)
  2425                              <1> 	;    DX = access mode, 100h = read, 80h = write
  2426                              <1> 
  2427                              <1> 	; 24/06/2020 - Retro UNIX 386 v2	
  2428 00005DD6 E896F7FFFF          <1> 	call	access
  2429                              <1> 		;jsr r0,access; 1 / get inode in core
  2430                              <1> 	
  2431                              <1> 	; 27/03/2021
  2432                              <1> 	; DX return value from 'access' subroutine
  2433                              <1> 	;    may be 100h, 20h, 04h for DX = 100h (IREAD) input
  2434                              <1> 	;	     80h, 10h, 02h for DX = 80h (IWRITE) input
  2435                              <1> 
  2436                              <1> 	; 05/12/2021
  2437 00005DDB 5A                  <1> 	pop	edx ; * ; dl = 0 -> open for read
  2438                              <1> 			; dl = 1 -> open for write
  2439                              <1> 	; 24/06/2020
  2440 00005DDC 8A0D[75700000]      <1> 	mov	cl, [i.flgs+1]
  2441                              <1> 
  2442 00005DE2 F6C180              <1> 	test	cl, 80h		; regular file ?
  2443 00005DE5 7514                <1> 	jnz	short iopen_1	; yes ; 05/12/2021
  2444 00005DE7 F6C120              <1> 	test	cl, 20h 	; device file ?
  2445 00005DEA 7524                <1> 	jnz	short iopen_2	; yes
  2446                              <1> 
  2447                              <1> 	; no !?, error.
  2448                              <1> 
  2449 00005DEC C705[4C750000]FF00- <1> 	mov	dword [u.error], ERR_INV_FILE ; 0FFh ; invalid file
  2449 00005DF4 0000                <1>
  2450                              <1> iopen_err:
  2451 00005DF6 E9AFDDFFFF          <1> 	jmp	error
  2452                              <1> 
  2453                              <1> iopen_1:
  2454                              <1> 	; 05/12/2021
  2455                              <1> 	; (If open mode is 'write' and the file/inode is a directory,
  2456                              <1> 	;  open request will/must be rejected.)
  2457                              <1> 	;
  2458                              <1> 	; (If inode is a regular file and it is not a directory,
  2459                              <1> 	; there is nothing to do here, we need to return to 'sysopen'
  2460                              <1> 	; for completing the rest of file opening procedure.) 
  2461                              <1> 	
  2462 00005DFB F6C140              <1> 	test	cl, 40h	; directory ?
  2463 00005DFE 74CA                <1> 	jz	short iopen_retn ; no
  2464                              <1> 	
  2465 00005E00 20D2                <1> 	and	dl, dl ; open mode is 1 (write) ?
  2466 00005E02 74C6                <1> 	jz	short iopen_retn ; no
  2467                              <1> 
  2468                              <1> 	; !!!	
  2469                              <1> 	; 'sysopen' for writing
  2470                              <1> 	;	 is not applicable for directories
  2471                              <1> 	; (Directory entries must be created or deleted by
  2472                              <1> 	; relevant system calls.)
  2473                              <1> 
  2474                              <1> 	;mov	dword [u.error], ERR_DIR_ACCESS ; 11
  2475 00005E04 C705[4C750000]0B00- <1> 	mov	dword [u.error], ERR_PERM_DENIED ; 11
  2475 00005E0C 0000                <1>
  2476                              <1> 				; 'permission denied !' error  
  2477                              <1> 	;jmp	error
  2478 00005E0E EBE6                <1> 	jmp	short iopen_err
  2479                              <1> 
  2480                              <1> ;iopen_retn:
  2481                              <1> ;	retn
  2482                              <1> 
  2483                              <1> iopen_2:
  2484                              <1> 	;push	ax
  2485                              <1> 	;	; mov r1,-(sp) / yes, figure out
  2486                              <1> 	;movzx	ebx, al
  2487                              <1> 	;shl	bx, 2
  2488                              <1> 	;	; asl r1
  2489                              <1>         ;add	ebx, iopen_1 - 4
  2490                              <1> 	;jmp	dword [ebx]
  2491                              <1>         ;	; jmp *1f-2(r1) / which one and transfer to it
  2492                              <1> 	
  2493                              <1> 	; 05/12/2021
  2494 00005E10 50                  <1> 	push	eax ; save inode number
  2495                              <1> 	; device file
  2496 00005E11 BB[2D5E0000]        <1> 	mov	ebx, iopen_4
  2497 00005E16 83F808              <1> 	cmp	eax, 8	; /dev/tty inode number is 8
  2498 00005E19 720E                <1> 	jb	short iopen_3
  2499 00005E1B 83F81A              <1> 	cmp	eax, 26	; /dev/tty9 (/dev/com2) inode number is 26
  2500 00005E1E 7709                <1> 	ja	short iopen_3	
  2501                              <1> 	; convert v2 inode number to v1 device inode number
  2502 00005E20 2C07                <1> 	sub	al, 7 ; 8 -> 1, 26 -> 19
  2503 00005E22 89C1                <1> 	mov	ecx, eax
  2504 00005E24 C0E102              <1> 	shl	cl, 2 ; * 4
  2505 00005E27 01CB                <1> 	add	ebx, ecx
  2506                              <1> iopen_3:
  2507 00005E29 FEC2                <1> 	inc	dl ; 1 = read, 2 = write
  2508 00005E2B FF23                <1> 	jmp	dword [ebx]	
  2509                              <1> 		 ; jmp *1f-2(r1)
  2510                              <1> ;iopen_1: ; 1:
  2511                              <1> iopen_4:
  2512 00005E2D [9E580000]          <1> 	dd	null ; 05/12/2021
  2513 00005E31 [7D5E0000]          <1> 	dd	otty ; tty, AX = 1 (runix)
  2514                              <1>  		 ;otty / tty ; r1=2
  2515                              <1>         	 ;oppt / ppt ; r1=4
  2516 00005E35 [1C5F0000]          <1> 	dd	sret ; mem, AX = 2 (runix)
  2517                              <1> 		 ;sret / mem ; r1=6
  2518                              <1> 		 ;sret / rf0
  2519                              <1>         	 ;sret / rk0
  2520                              <1>         	 ;sret / tap0
  2521                              <1>         	 ;sret / tap1
  2522                              <1>         	 ;sret / tap2
  2523                              <1>         	 ;sret / tap3
  2524                              <1>         	 ;sret / tap4
  2525                              <1>         	 ;sret / tap5
  2526                              <1>         	 ;sret / tap6
  2527                              <1>         	 ;sret / tap7
  2528 00005E39 [1C5F0000]          <1>         dd	sret ; fd0, AX = 3 (runix only)
  2529 00005E3D [1C5F0000]          <1>         dd	sret ; fd1, AX = 4 (runix only)
  2530 00005E41 [1C5F0000]          <1>         dd	sret ; hd0, AX = 5 (runix only)
  2531 00005E45 [1C5F0000]          <1>         dd	sret ; hd1, AX = 6 (runix only) 
  2532 00005E49 [1C5F0000]          <1>         dd	sret ; hd2, AX = 7 (runix only)
  2533 00005E4D [1C5F0000]          <1>         dd	sret ; hd3, AX = 8 (runix only) 
  2534                              <1> 	;dd	error ; lpr, AX = 9 (error !)
  2535 00005E51 [1C5F0000]          <1>         dd      sret ; lpr, AX = 9 (runix)
  2536 00005E55 [8C5E0000]          <1> 	dd	ocvt ; tty0, AX = 10 (runix)	  
  2537                              <1> 		 ;ocvt / tty0
  2538 00005E59 [8C5E0000]          <1> 	dd	ocvt ; tty1, AX = 11 (runix)	  
  2539                              <1> 		 ;ocvt / tty1
  2540 00005E5D [8C5E0000]          <1> 	dd	ocvt ; tty2, AX = 12 (runix)	  
  2541                              <1> 		 ;ocvt / tty2
  2542 00005E61 [8C5E0000]          <1> 	dd	ocvt ; tty3, AX = 13 (runix)	  
  2543                              <1> 		 ;ocvt / tty3
  2544 00005E65 [8C5E0000]          <1> 	dd	ocvt ; tty4, AX = 14 (runix)	  
  2545                              <1> 		 ;ocvt / tty4
  2546 00005E69 [8C5E0000]          <1> 	dd	ocvt ; tty5, AX = 15 (runix)	  
  2547                              <1> 		 ;ocvt / tty5
  2548 00005E6D [8C5E0000]          <1> 	dd	ocvt ; tty6, AX = 16 (runix)	  
  2549                              <1> 		 ;ocvt / tty6
  2550 00005E71 [8C5E0000]          <1> 	dd	ocvt ; tty7, AX = 17 (runix)	  
  2551                              <1> 		 ;ocvt / tty7
  2552 00005E75 [8C5E0000]          <1> 	dd	ocvt ; COM1, AX = 18 (runix only)	  
  2553                              <1> 		 ;error / crd
  2554 00005E79 [8C5E0000]          <1> 	dd	ocvt ; COM2, AX = 19 (runix only)
  2555                              <1> 
  2556                              <1> ;iopen_2: ; 2: / check open write access
  2557                              <1> ;	neg	ax
  2558                              <1> ;		;neg r1 / make inode number positive
  2559                              <1> ;	mov	dl, 1 ; write access
  2560                              <1> ;	call	access
  2561                              <1> ;		;jsr r0,access; 1 / get inode in core
  2562                              <1> ;	; DL=1
  2563                              <1> ;	test	word [i.flgs], 4000h ; Bit 14 : Directory flag
  2564                              <1> ;		;bit $40000,i.flgs / is it a directory?
  2565                              <1> ;	jz	short iopen_0
  2566                              <1> ;	;mov	[u.error], ERR_DIR_ACCESS
  2567                              <1> ;	;jmp	error ; permission denied !
  2568                              <1> ;	jmp	sysmnt_err0
  2569                              <1> ;	;;jnz	error		
  2570                              <1> ;      		; bne 2f / yes, transfer (error)
  2571                              <1> ;       ;;jmp     short iopen_0
  2572                              <1> ;	;cmp	ax, 40
  2573                              <1> ;		; cmp r1,$40. / no, is it a special file?
  2574                              <1> ;        ;ja	short iopen_2
  2575                              <1> ;		 ;bgt 3f / no, return
  2576                              <1> ;	;push	ax
  2577                              <1> ;		;mov r1,-(sp) / yes
  2578                              <1> ;	;movzx	ebx, al
  2579                              <1> ;	;shl	bx, 1
  2580                              <1> ;		; asl r1
  2581                              <1> ;	;add	ebx, ipen_3 - 2
  2582                              <1> ;	;jmp	dword [ebx]
  2583                              <1> ;		; jmp *1f-2(r1) / figure out 
  2584                              <1> ;			; / which special file it is and transfer
  2585                              <1> ;iopen_3: ; 1:
  2586                              <1> ;	dd 	otty ; tty, AX = 1 (runix)
  2587                              <1> ; 		 ;otty / tty ; r1=2
  2588                              <1> ;        	 ;leadr / ppt ; r1=4
  2589                              <1> ;	dd	sret ; mem, AX = 2 (runix)
  2590                              <1> ;		 ;sret / mem ; r1=6
  2591                              <1> ;		 ;sret / rf0
  2592                              <1> ;        	 ;sret / rk0
  2593                              <1> ;        	 ;sret / tap0
  2594                              <1> ;        	 ;sret / tap1
  2595                              <1> ;        	 ;sret / tap2
  2596                              <1> ;        	 ;sret / tap3
  2597                              <1> ;        	 ;sret / tap4
  2598                              <1> ;        	 ;sret / tap5
  2599                              <1> ;        	 ;sret / tap6
  2600                              <1> ;        	 ;sret / tap7
  2601                              <1> ;	dd 	sret ; fd0, AX = 3 (runix only)
  2602                              <1> ;	dd 	sret ; fd1, AX = 4 (runix only)
  2603                              <1> ;	dd 	sret ; hd0, AX = 5 (runix only)
  2604                              <1> ;	dd 	sret ; hd1, AX = 6 (runix only)	
  2605                              <1> ;	dd 	sret ; hd2, AX = 7 (runix only)
  2606                              <1> ;	dd 	sret ; hd3, AX = 8 (runix only)	
  2607                              <1> ;	dd	sret ; lpr, AX = 9  (runix)
  2608                              <1> ;	;dd	ejec ; lpr, AX = 9  (runix)
  2609                              <1> ;	dd	sret ; tty0, AX = 10 (runix)	  
  2610                              <1> ;		 ;ocvt / tty0
  2611                              <1> ;	dd	sret ; tty1, AX = 11 (runix)	  
  2612                              <1> ;		 ;ocvt / tty1
  2613                              <1> ;	dd	sret ; tty2, AX = 12 (runix)	  
  2614                              <1> ;		 ;ocvt / tty2
  2615                              <1> ;	dd	sret ; tty3, AX = 13 (runix)	  
  2616                              <1> ;		 ;ocvt / tty3
  2617                              <1> ;	dd	sret ; tty4, AX = 14 (runix)	  
  2618                              <1> ;		 ;ocvt / tty4
  2619                              <1> ;	dd	sret ; tty5, AX = 15 (runix)	  
  2620                              <1> ;		 ;ocvt / tty5
  2621                              <1> ;	dd	sret ; tty6, AX = 16 (runix)	  
  2622                              <1> ;		 ;ocvt / tty6
  2623                              <1> ;	dd	sret ; tty7, AX = 17 (runix)	  
  2624                              <1> ;		 ;ocvt / tty7
  2625                              <1> ;	dd	ocvt ; COM1, AX = 18 (runix only)	  
  2626                              <1> ;		 ;/ ejec / lpr
  2627                              <1> ;	dd	ocvt ; COM2, AX = 19 (runix only)
  2628                              <1> 
  2629                              <1> otty: ;/ open console tty for reading or writing
  2630                              <1> 	; 08/01/2022
  2631                              <1> 	; 01/01/2022
  2632                              <1> 	; 05/12/2021 (Retro UNIX 386 v1.2)
  2633                              <1> 	; 16/11/2015
  2634                              <1> 	; 12/11/2015
  2635                              <1> 	; 18/05/2015 (Retro UNIX 386 v1 - Beginning)
  2636                              <1> 	; 21/05/2013 - 13/07/2014 (Retro UNIX 8086 v1)
  2637                              <1> 	; 16/07/2013
  2638                              <1> 	; Retro UNIX 8086 v1 modification:
  2639                              <1> 	;  If a tty is open for read or write by
  2640                              <1> 	;     a process (u.uno), only same process can open
  2641                              <1> 	;     same tty to write or read (R->R&W or W->W&R).	
  2642                              <1> 	;
  2643                              <1> 	; (INPUT: DL=2 for Read, DL=1 for Write, DL=0 for sysstty)
  2644                              <1> 	;
  2645 00005E7D 0FB61D[29750000]    <1> 	movzx	ebx, byte [u.uno] ; process number
  2646 00005E84 8A83[F3700000]      <1> 	mov	al, [ebx+p.ttyc-1] ; current/console tty
  2647                              <1> 	; 13/01/2014
  2648 00005E8A EB04                <1> 	jmp	short ottyp
  2649                              <1> ocvt:
  2650 00005E8C 2C0A                <1> 	sub	al, 10
  2651                              <1> 	; 08/01/2022
  2652 00005E8E 31DB                <1> 	xor	ebx, ebx
  2653                              <1> ottyp:
  2654                              <1> 	; 08/01/2022
  2655                              <1> 	; 01/01/2022
  2656                              <1> 	; 05/12/2021 (Retro UNIX 386 v1.2)
  2657                              <1> 	; 16/11/2015
  2658                              <1> 	; 12/11/2015
  2659                              <1> 	; 18/05/2015 (32 bit modifications)
  2660                              <1> 	; 06/12/2013 - 13/07/2014
  2661 00005E90 88C6                <1> 	mov	dh, al ; tty number
  2662                              <1> 	;movzx 	ebx, al ; AL = tty number (0 to 9), AH = 0
  2663                              <1> 	; 08/01/2022
  2664 00005E92 88C3                <1> 	mov	bl, al
  2665 00005E94 D0E3                <1> 	shl 	bl, 1  ; aligned to word
  2666                              <1> 	; 26/01/2014	
  2667 00005E96 81C3[04700000]      <1> 	add 	ebx, ttyl
  2668 00005E9C 668B0B              <1> 	mov 	cx, [ebx]
  2669                              <1> 		   ; CL = lock value (0 or process number)
  2670                              <1> 		   ; CH = open count 
  2671 00005E9F 20C9                <1> 	and 	cl, cl
  2672                              <1> 	; 13/01/2014
  2673                              <1> 	;jz 	short otty_ret
  2674                              <1> 	; 05/12/2021
  2675 00005EA1 743A                <1> 	jz 	short ottys_0
  2676                              <1> 	;
  2677                              <1> 	; 16/11/2015
  2678 00005EA3 3A0D[29750000]      <1> 	cmp 	cl, [u.uno]
  2679 00005EA9 745D                <1> 	je	short ottys_3
  2680                              <1> 	;
  2681 00005EAB 0FB6D9              <1> 	movzx 	ebx, cl ; the process which has locked the tty
  2682 00005EAE D0E3                <1> 	shl 	bl, 1
  2683 00005EB0 668B83[B2700000]    <1> 	mov 	ax, [ebx+p.pid-2]
  2684                              <1> 	;movzx 	ebx, byte [u.uno]
  2685 00005EB7 8A1D[29750000]      <1> 	mov	bl, [u.uno]
  2686 00005EBD D0E3                <1> 	shl 	bl, 1
  2687 00005EBF 663B83[D2700000]    <1> 	cmp 	ax, [ebx+p.ppid-2]
  2688 00005EC6 7440                <1> 	je 	short ottys_3  ; 16/11/2015
  2689                              <1> 	;
  2690                              <1> 	; the tty is locked by another process
  2691                              <1> 	; except the parent process (p.ppid)
  2692                              <1>         ;
  2693 00005EC8 C705[4C750000]0B00- <1> 	mov     dword [u.error], ERR_DEV_ACCESS
  2693 00005ED0 0000                <1>
  2694                              <1> 			; permission denied ! error
  2695                              <1> otty_err: ; 13/01/2014
  2696 00005ED2 08D2                <1> 	or 	dl, dl ; DL = 0 -> called by sysstty
  2697                              <1> 	;jnz	error
  2698                              <1> 	; 05/12/2021
  2699 00005ED4 7405                <1> 	jz	short otty_stc_retn
  2700 00005ED6 E9CFDCFFFF          <1> 	jmp	error
  2701                              <1> otty_stc_retn:
  2702 00005EDB F9                  <1> 	stc
  2703 00005EDC C3                  <1> 	retn
  2704                              <1> ottys_0:
  2705                              <1> 	; 05/12/2021
  2706                              <1> otty_ret:
  2707                              <1> 	; 13/01/2014
  2708 00005EDD 80FE07              <1> 	cmp 	dh, 7
  2709 00005EE0 761C                <1> 	jna	short ottys_2
  2710                              <1> 	; 16/11/2015
  2711                              <1> com_port_check:
  2712 00005EE2 BE[22700000]        <1> 	mov	esi, com1p
  2713 00005EE7 80FE08              <1> 	cmp	dh, 8	; COM1 (tty8) ?
  2714 00005EEA 7601                <1> 	jna	short ottys_1 ; yes, it is COM1
  2715 00005EEC 46                  <1> 	inc	esi	; no, it is COM2 (tty9)
  2716                              <1> ottys_1:
  2717                              <1> 	; 12/11/2015
  2718 00005EED 803E00              <1> 	cmp	byte [esi], 0 ; E3h (or 23h)
  2719 00005EF0 770C                <1> 	ja	short com_port_ready
  2720                              <1> 	;
  2721 00005EF2 C705[4C750000]0F00- <1>         mov     dword [u.error], ERR_DEV_NOT_RDY
  2721 00005EFA 0000                <1>
  2722                              <1> 			   ; device not ready ! error
  2723 00005EFC EBD4                <1> 	jmp	short otty_err	 
  2724                              <1> com_port_ready:
  2725                              <1> ottys_2:
  2726 00005EFE 08C9                <1> 	or	cl, cl  ; cl = lock/owner, ch = open count
  2727 00005F00 7506                <1> 	jnz	short ottys_3
  2728 00005F02 8A0D[29750000]      <1> 	mov	cl, [u.uno]
  2729                              <1> ottys_3:
  2730 00005F08 FEC5                <1> 	inc 	ch
  2731 00005F0A 66890B              <1> 	mov 	[ebx], cx ; set tty lock again
  2732                              <1> 	; 06/12/2013
  2733 00005F0D FEC6                <1> 	inc	dh ; tty number + 1
  2734 00005F0F BB[0E750000]        <1> 	mov	ebx, u.ttyp
  2735                              <1> 	; 13/01/2014
  2736                              <1> 	;test	dl, 2 ; open for read sign
  2737                              <1> 	;jnz	short ottys_4
  2738                              <1> 	; 05/12/2021
  2739                              <1> 	;test	dl, 2
  2740                              <1> 	;jz	short ottys_4 ; open for read
  2741                              <1> 	; 01/01/2022
  2742 00005F14 F6C201              <1> 	test	dl, 1
  2743 00005F17 7501                <1> 	jnz	short ottys_4 ; open for read
  2744                              <1> 	; dl = 2 (open for write) or dl = 0 (sysstty)  
  2745 00005F19 43                  <1> 	inc	ebx
  2746                              <1> ottys_4:
  2747                              <1> 	; Set 'u.ttyp' ('the recent TTY') value
  2748 00005F1A 8833                <1> 	mov 	[ebx], dh ; tty number + 1
  2749                              <1> sret:
  2750 00005F1C 08D2                <1> 	or	dl, dl ; sysstty system call check (DL=0)
  2751 00005F1E 7401                <1> 	jz	short iclose_retn
  2752                              <1> 	;pop 	ax
  2753                              <1> 	; 05/12/2021
  2754 00005F20 58                  <1> 	pop	eax
  2755                              <1> iclose_retn:	
  2756 00005F21 C3                  <1> 	retn
  2757                              <1> 
  2758                              <1> 	;
  2759                              <1> 	; Original UNIX v1 'otty' routine:
  2760                              <1> 	;	
  2761                              <1> 	;mov    $100,*$tks / set interrupt enable bit (zero others) in
  2762                              <1>         ;                 / reader status reg
  2763                              <1>         ;mov    $100,*$tps / set interrupt enable bit (zero others) in
  2764                              <1>         ;                 / punch status reg
  2765                              <1>         ;mov    tty+[ntty*8]-8+6,r5 / r5 points to the header of the
  2766                              <1>         ;                          / console tty buffer
  2767                              <1>         ;incb   (r5) / increment the count of processes that opened the
  2768                              <1>         ;            / console tty
  2769                              <1>         ;tst    u.ttyp / is there a process control tty (i.e., has a tty
  2770                              <1>         ;             / buffer header
  2771                              <1>         ;bne    sret / address been loaded into u.ttyp yet)?  yes, branch
  2772                              <1>         ;mov    r5,u.ttyp / no, make the console tty the process control
  2773                              <1>         ;                 / tty
  2774                              <1>         ;br     sret / ?
  2775                              <1> ;sret:
  2776                              <1> 		;clr *$ps / set processor priority to zero
  2777                              <1> ;	pop	ax
  2778                              <1>         	;mov (sp)+,r1 / pop stack to r1
  2779                              <1> ;3:
  2780                              <1> ;	retn
  2781                              <1>         	;rts r0
  2782                              <1> 	
  2783                              <1> ;ocvt:	; < open tty >
  2784                              <1> 	; 13/01/2014
  2785                              <1> 	; 06/12/2013 (major modification: p.ttyc, u.ttyp)
  2786                              <1> 	; 24/09/2013 consistency check -> ok
  2787                              <1> 	; 16/09/2013
  2788                              <1> 	; 03/09/2013
  2789                              <1> 	; 27/08/2013
  2790                              <1> 	; 16/08/2013
  2791                              <1> 	; 16/07/2013
  2792                              <1> 	; 27/05/2013
  2793                              <1> 	; 21/05/2013
  2794                              <1> 	;
  2795                              <1> 	; Retro UNIX 8086 v1 modification !
  2796                              <1> 	; 
  2797                              <1> 	; In original UNIX v1, 'ocvt' routine 
  2798                              <1> 	;		(exactly different than this one)
  2799                              <1> 	;	was in 'u9.s' file.
  2800                              <1> 	;
  2801                              <1> 	; 16/07/2013
  2802                              <1> 	; Retro UNIX 8086 v1 modification:
  2803                              <1> 	;  If a tty is open for read or write by
  2804                              <1> 	;     a process (u.uno), only same process can open
  2805                              <1> 	;     same tty to write or read (R->R&W or W->W&R).	
  2806                              <1> 	;
  2807                              <1> 	; INPUT: DL=2 for Read DL=1 for Write
  2808                              <1> 
  2809                              <1> 	; 16/09/2013
  2810                              <1> 	; sub 	al, 10
  2811                              <1> 	
  2812                              <1> 	; 06/12/2013
  2813                              <1> 	;cmp	al, 7
  2814                              <1>         ;jna	short ottyp
  2815                              <1> 	; 13/01/2014
  2816                              <1> 	;jmp	short ottyp
  2817                              <1> 
  2818                              <1> ;oppt: / open paper tape for reading or writing
  2819                              <1> ;        mov    $100,*$prs / set reader interrupt enable bit
  2820                              <1> ;        tstb   pptiflg / is file already open
  2821                              <1> ;        bne    2f / yes, branch
  2822                              <1> ;1:
  2823                              <1> ;        mov    $240,*$ps / no, set processor priority to 5
  2824                              <1> ;        jsr    r0,getc; 2 / remove all entries in clist
  2825                              <1> ;               br .+4 / for paper tape input and place in free list
  2826                              <1> ;        br     1b
  2827                              <1> ;        movb   $2,pptiflg / set pptiflg to indicate file just open
  2828                              <1> ;        movb   $10.,toutt+1 / place 10 in paper tape input tout entry
  2829                              <1> ;        br     sret
  2830                              <1> ;2:
  2831                              <1> ;        jmp    error / file already open
  2832                              <1> 
  2833                              <1> 	; 05/12/2021 - Retro UNIX 386 v2 fs compatibility modification
  2834                              <1> iclose:
  2835                              <1> 	; 05/12/2021 - Retro UNIX 386 v1.2
  2836                              <1> 	; 10/04/2021
  2837                              <1> 	; 08/04/2021
  2838                              <1> 	; 04/04/2021 - Retro UNIX 386 v2
  2839                              <1> 	; 19/05/2015
  2840                              <1> 	; 18/05/2015 (Retro UNIX 386 v1 - Beginning)
  2841                              <1> 	; 21/05/2013 - 13/01/2014 (Retro UNIX 8086 v1)
  2842                              <1> 	;
  2843                              <1> 	; close file whose i-number is in r1
  2844                              <1> 	; 
  2845                              <1> 	; INPUTS ->
  2846                              <1> 	;    r1 - inode number
  2847                              <1> 	; OUTPUTS ->
  2848                              <1> 	;    file's inode in core	
  2849                              <1> 	;    r1 - inode number (positive)
  2850                              <1> 	;
  2851                              <1> 	; ((AX = R1))
  2852                              <1>         ;    ((Modified registers: -ebx-, edx)) 
  2853                              <1> 	;        
  2854                              <1> ;;/ close file whose i-number is in r1
  2855                              <1> ;	mov	dl, 2 ; 12/01/2014
  2856                              <1> ;	test	ah, 80h ; Bit 15 of AX
  2857                              <1> ;		;tst r1 / test i-number
  2858                              <1> ;	;jnz	short iclose_2
  2859                              <1> ;		;blt 2f / if neg., branch
  2860                              <1> ;	jz	short iclose_0 ; 30/07/2013
  2861                              <1> ;	; 16/07/2013 
  2862                              <1> ;	neg	ax ; make it positive
  2863                              <1> ;	; 12/01/2014
  2864                              <1> ;	dec	dl ; dl = 1 (open for write)
  2865                              <1> ;iclose_0:
  2866                              <1> ;	cmp	ax, 40
  2867                              <1> ;		;cmp r1,$40. / is it a special file
  2868                              <1> ;       ja	short iclose_retn  ; 13/01/2014
  2869                              <1> ;		;bgt 3b / no, return
  2870                              <1> ;	; 12/01/2014
  2871                              <1> ;	; DL=2 -> special file was opened for reading
  2872                              <1> ;	; DL=1 -> special file was opened for writing
  2873                              <1> 
  2874                              <1> 	; 04/04/2021
  2875                              <1> 	; INPUT:
  2876                              <1> 	;	(e)ax = inode number
  2877                              <1> 	;	   dl = open mode
  2878                              <1> 	;		0 = control mode
  2879                              <1> 	;		1 = read
  2880                              <1> 	;		2 = write
  2881                              <1> 	; OUTPUT:
  2882                              <1> 	;	none
  2883                              <1> 	;	(if cf=1 -> eax = error code)
  2884                              <1> 	;	(if cf=0 -> ax = inode number)
  2885                              <1> 
  2886                              <1> 	; 10/04/2021
  2887                              <1>         ; Modified registers: ebx, ecx, edx, esi, edi, (ebp) 
  2888                              <1> 
  2889                              <1> 	; 05/12/2021
  2890                              <1> 	; 08/04/2021
  2891                              <1> 
  2892 00005F22 52                  <1> 	push	edx
  2893 00005F23 E85FF5FFFF          <1> 	call	iget
  2894 00005F28 5A                  <1> 	pop	edx 
  2895                              <1> 
  2896 00005F29 8A0D[75700000]      <1> 	mov	cl, [i.flgs+1]
  2897                              <1> 
  2898                              <1> 	; if i.flgs bit 15 is 1, it is regular file 
  2899                              <1> 	;test	byte [i.flgs+1], 80h ; is it a special file?
  2900 00005F2F F6C180              <1> 	test	cl, 80h ; is it a special file?
  2901 00005F32 75ED                <1> 	jnz	short iclose_retn ; no
  2902                              <1> 
  2903                              <1> 	;; inode flags - bit 13 must be 1 
  2904                              <1> 	;test	cl, 20h ; is it a valid device inode
  2905                              <1> 	;jz	short iclose_retn ; no ; 19/01/2022
  2906                              <1> 	
  2907                              <1> 	;push	ax
  2908                              <1> 	;	;mov r1,-(sp) / yes, save r1 on stack
  2909                              <1> 	;movzx	ebx, al
  2910                              <1> 	;shl	bx, 2
  2911                              <1> 	;	; asl r1
  2912                              <1> 	;add	ebx, iclose_1 - 4
  2913                              <1> 	;jmp	dword [ebx]
  2914                              <1> 	;	; jmp *1f-2(r1) / compute jump address and transfer
  2915                              <1> 
  2916                              <1> 	; 05/12/2021
  2917 00005F34 50                  <1> 	push	eax ; save inode number
  2918                              <1> 	; device file
  2919 00005F35 BB[4F5F0000]        <1> 	mov	ebx, iclose_1
  2920 00005F3A 83F808              <1> 	cmp	eax, 8	; /dev/tty inode number is 8
  2921 00005F3D 720E                <1> 	jb	short iclose_0
  2922 00005F3F 83F81A              <1> 	cmp	eax, 26	; /dev/tty9 (/dev/com2) inode number is 26
  2923 00005F42 7709                <1> 	ja	short iclose_0
  2924                              <1> 	; convert v2 inode number to v1 device inode number
  2925 00005F44 2C07                <1> 	sub	al, 7 ; 8 -> 1, 26 -> 19
  2926 00005F46 89C1                <1> 	mov	ecx, eax
  2927 00005F48 C0E102              <1> 	shl	cl, 2 ; * 4
  2928 00005F4B 01CB                <1> 	add	ebx, ecx
  2929                              <1> iclose_0:
  2930 00005F4D FF23                <1> 	jmp	dword [ebx]	
  2931                              <1> 		 ; jmp *1f-2(r1)
  2932                              <1> iclose_1: ; 1:
  2933 00005F4F [9E580000]          <1> 	dd	null ; 05/12/2021
  2934 00005F53 [9F5F0000]          <1> 	dd	ctty ; tty, AX = 1 (runix)
  2935 00005F57 [F15F0000]          <1> 	dd	cret ; mem, AX = 2 (runix)
  2936 00005F5B [F15F0000]          <1> 	dd	cret ; fd0, AX = 3 (runix only)
  2937 00005F5F [F15F0000]          <1> 	dd	cret ; fd1, AX = 4 (runix only)
  2938 00005F63 [F15F0000]          <1> 	dd	cret ; hd0, AX = 5 (runix only)
  2939 00005F67 [F15F0000]          <1> 	dd	cret ; hd1, AX = 6 (runix only)	
  2940 00005F6B [F15F0000]          <1> 	dd	cret ; hd2, AX = 7 (runix only)
  2941 00005F6F [F15F0000]          <1> 	dd	cret ; hd3, AX = 8 (runix only)	
  2942 00005F73 [F15F0000]          <1> 	dd	cret ; lpr, AX = 9 (runix)
  2943                              <1> 	;dd	error; lpr, AX = 9 (error!)
  2944                              <1> 	;;dd	offset ejec ;;lpr, AX = 9  
  2945 00005F77 [AE5F0000]          <1> 	dd	ccvt ; tty0, AX = 10 (runix)	  
  2946 00005F7B [AE5F0000]          <1> 	dd	ccvt ; tty1, AX = 11 (runix)	  
  2947 00005F7F [AE5F0000]          <1> 	dd	ccvt ; tty2, AX = 12 (runix)	  
  2948 00005F83 [AE5F0000]          <1> 	dd	ccvt ; tty3, AX = 13 (runix)	  
  2949 00005F87 [AE5F0000]          <1> 	dd	ccvt ; tty4, AX = 14 (runix)	  
  2950 00005F8B [AE5F0000]          <1> 	dd	ccvt ; tty5, AX = 15 (runix)	  
  2951 00005F8F [AE5F0000]          <1> 	dd	ccvt ; tty6, AX = 16 (runix)	  
  2952 00005F93 [AE5F0000]          <1> 	dd	ccvt ; tty7, AX = 17 (runix)	  
  2953 00005F97 [AE5F0000]          <1> 	dd	ccvt ; COM1, AX = 18 (runix only)	  
  2954 00005F9B [AE5F0000]          <1> 	dd	ccvt ; COM2, AX = 19 (runix only)
  2955                              <1> 
  2956                              <1> 	; 1:
  2957                              <1> 	;        ctty   / tty
  2958                              <1> 	;        cppt   / ppt
  2959                              <1> 	;        sret   / mem
  2960                              <1> 	;        sret   / rf0
  2961                              <1> 	;        sret   / rk0
  2962                              <1> 	;        sret   / tap0
  2963                              <1> 	;        sret   / tap1
  2964                              <1> 	;        sret   / tap2
  2965                              <1> 	;        sret   / tap3
  2966                              <1> 	;        sret   / tap4
  2967                              <1> 	;        sret   / tap5
  2968                              <1> 	;        sret   / tap6
  2969                              <1> 	;        sret   / tap7
  2970                              <1> 	;        ccvt   / tty0
  2971                              <1> 	;        ccvt   / tty1
  2972                              <1> 	;        ccvt   / tty2
  2973                              <1> 	;        ccvt   / tty3
  2974                              <1> 	;        ccvt   / tty4
  2975                              <1> 	;        ccvt   / tty5
  2976                              <1> 	;        ccvt   / tty6
  2977                              <1> 	;        ccvt   / tty7
  2978                              <1> 	;        error / crd
  2979                              <1> 
  2980                              <1> ;iclose_2: ; 2: / negative i-number
  2981                              <1> 	;neg	ax
  2982                              <1> 		;neg r1 / make it positive
  2983                              <1> 	;cmp	ax, 40
  2984                              <1> 		;cmp r1,$40. / is it a special file?
  2985                              <1>         ;ja	short @b
  2986                              <1> 		;bgt    3b / no. return
  2987                              <1> 	;push	ax
  2988                              <1> 		;mov r1,-(sp)
  2989                              <1> 	;movzx	ebx, al
  2990                              <1> 	;shl	bx, 1
  2991                              <1> 		;asl r1 / yes. compute jump address and transfer
  2992                              <1> 	;add	ebx, iclose_3 - 2
  2993                              <1> 	;jmp	dword [ebx]
  2994                              <1> 		;jmp *1f-2(r1) / figure out 
  2995                              <1> ;iclose_3:
  2996                              <1> 	;dd	ctty ; tty, AX = 1 (runix)
  2997                              <1> 	;dd	sret ; mem, AX = 2 (runix)
  2998                              <1> 	;dd	sret ; fd0, AX = 3 (runix only)
  2999                              <1> 	;dd	sret ; fd1, AX = 4 (runix only)
  3000                              <1> 	;dd	sret ; hd0, AX = 5 (runix only)
  3001                              <1> 	;dd	sret ; hd1, AX = 6 (runix only)	
  3002                              <1> 	;dd	sret ; hd2, AX = 7 (runix only)
  3003                              <1> 	;dd	sret ; hd3, AX = 8 (runix only)
  3004                              <1> 	;;dd	sret ; lpr, AX = 9	
  3005                              <1> 	;dd	ejec ; lpr, AX = 9  (runix)
  3006                              <1> 	;dd	ccvt ; tty0, AX = 10 (runix)	  
  3007                              <1> 	;dd	ccvt ; tty1, AX = 11 (runix)	  
  3008                              <1> 	;dd	ccvt ; tty2, AX = 12 (runix)	  
  3009                              <1> 	;dd	ccvt ; tty3, AX = 13 (runix)	  
  3010                              <1> 	;dd	ccvt ; tty4, AX = 14 (runix)	  
  3011                              <1> 	;dd	ccvt ; tty5, AX = 15 (runix)	  
  3012                              <1> 	;dd	ccvt ; tty6, AX = 16 (runix)	  
  3013                              <1> 	;dd	ccvt ; tty7, AX = 17 (runix)	  
  3014                              <1> 	;dd	ccvt ; COM1, AX = 18 (runix only)	  
  3015                              <1> 	;dd	ccvt ; COM2, AX = 19 (runix only) 
  3016                              <1> 	
  3017                              <1> 	;1:
  3018                              <1> 	;      	ctty   / tty
  3019                              <1> 	;       leadr  / ppt
  3020                              <1> 	;       sret   / mem
  3021                              <1> 	;       sret   / rf0
  3022                              <1> 	;       sret   / rk0
  3023                              <1> 	;       sret   / tap0
  3024                              <1> 	;       sret   / tap1
  3025                              <1> 	;       sret   / tap2
  3026                              <1> 	;       sret   / tap3
  3027                              <1> 	;       sret   / tap4
  3028                              <1> 	;       sret   / tap5
  3029                              <1> 	;       sret   / tap6
  3030                              <1> 	;       sret   / tap7
  3031                              <1> 	;       ccvt   / tty0
  3032                              <1> 	;       ccvt   / tty1
  3033                              <1> 	;       ccvt   / tty2
  3034                              <1> 	;       ccvt   / tty3
  3035                              <1> 	;       ccvt   / tty4
  3036                              <1> 	;       ccvt   / tty5
  3037                              <1> 	;       ccvt   / tty6
  3038                              <1> 	;       ccvt   / tty7
  3039                              <1> 	;/       ejec / lpr
  3040                              <1> 
  3041                              <1> ctty: ; / close console tty
  3042                              <1> 	; 08/01/2022
  3043                              <1> 	; 05/12/2021 (Retro UNIX 386 v1.2)
  3044                              <1> 	; 18/05/2015 (Retro UNIX 386 v1 - Beginning)
  3045                              <1> 	; 21/05/2013 - 26/01/2014 (Retro UNIX 8086 v1)
  3046                              <1> 	;
  3047                              <1> 	; Retro UNIX 8086 v1 modification !
  3048                              <1> 	; (DL = 2 -> it is open for reading)
  3049                              <1> 	; (DL = 1 -> it is open for writing)
  3050                              <1> 	; (DL = 0 -> it is open for sysstty system call)
  3051                              <1> 	;
  3052                              <1> 	; 06/12/2013
  3053 00005F9F 0FB61D[29750000]    <1>         movzx   ebx, byte [u.uno] ; process number
  3054 00005FA6 8A83[F3700000]      <1>         mov     al, [ebx+p.ttyc-1]
  3055                              <1> 	; 13/01/2014
  3056 00005FAC EB04                <1> 	jmp	short cttyp
  3057                              <1> ccvt:
  3058 00005FAE 2C0A                <1> 	sub 	al, 10
  3059                              <1> 	; 08/01/2022
  3060 00005FB0 31DB                <1> 	xor	ebx, ebx
  3061                              <1> cttyp:	
  3062                              <1> 	; 08/01/2022
  3063                              <1> 	; 05/12/2021 (Retro UNIX 386 v1.2)
  3064                              <1> 	; 18/05/2015 (32 bit modifications)
  3065                              <1> 	; 16/08/2013 - 26/01/2014
  3066                              <1> 	;movzx 	ebx, al ; tty number (0 to 9)
  3067                              <1> 	; 08/01/2022
  3068 00005FB2 88C3                <1> 	mov	bl, al
  3069 00005FB4 D0E3                <1> 	shl 	bl, 1  ; aligned to word	
  3070                              <1> 	; 26/01/2014
  3071 00005FB6 81C3[04700000]      <1> 	add 	ebx, ttyl
  3072 00005FBC 88C6                <1> 	mov 	dh, al ; tty number
  3073 00005FBE 668B03              <1> 	mov 	ax, [ebx]
  3074                              <1> 		   ; AL = lock value (0 or process number)
  3075                              <1> 		   ; AH = open count 
  3076 00005FC1 20E4                <1> 	and 	ah, ah
  3077 00005FC3 750F                <1> 	jnz	short ctty_ret
  3078 00005FC5 C705[4C750000]0A00- <1>         mov     dword [u.error], ERR_DEV_NOT_OPEN
  3078 00005FCD 0000                <1>
  3079                              <1> 			; device not open ! error
  3080                              <1> 	;jmp 	short ctty_err ; open count = 0, it is not open !
  3081 00005FCF E9D6DBFFFF          <1> 	jmp	error
  3082                              <1> 	; 26/01/2014
  3083                              <1> ctty_ret:
  3084 00005FD4 FECC                <1> 	dec 	ah ; decrease open count
  3085 00005FD6 7502                <1> 	jnz	short ctty_1
  3086 00005FD8 30C0                <1> 	xor	al, al ; unlock/free tty
  3087                              <1> ctty_1:
  3088 00005FDA 668903              <1> 	mov 	[ebx], ax ; close tty instance
  3089                              <1> 	;
  3090 00005FDD BB[0E750000]        <1> 	mov	ebx, u.ttyp
  3091                              <1> 	;test	dl, 1 ; open for write sign
  3092                              <1> 	;jz	short ctty_2
  3093                              <1> 	; 05/12/2021
  3094 00005FE2 F6C202              <1> 	test	dl, 2 ; open for write sign
  3095 00005FE5 7401                <1> 	jz	short ctty_2	
  3096 00005FE7 43                  <1> 	inc	ebx
  3097                              <1> ctty_2:
  3098 00005FE8 FEC6                <1> 	inc	dh ; tty number + 1
  3099 00005FEA 3A33                <1> 	cmp	dh, [ebx]
  3100 00005FEC 7503                <1> 	jne	short cret
  3101                              <1> 	; Reset/Clear 'u.ttyp' ('the recent TTY') value
  3102 00005FEE C60300              <1> 	mov	byte [ebx], 0
  3103                              <1> cret:
  3104 00005FF1 08D2                <1> 	or	dl, dl ; sysstty system call check (DL=0)
  3105 00005FF3 7401                <1> 	jz	short ctty_3
  3106                              <1> 	;pop	ax
  3107                              <1> 	; 05/12/2021
  3108 00005FF5 58                  <1> 	pop	eax
  3109                              <1> ctty_3:
  3110 00005FF6 C3                  <1> 	retn
  3111                              <1> 
  3112                              <1> ;ctty_err: ; 13/01/2014
  3113                              <1> ;	or 	dl, dl ; DL = 0 -> called by sysstty
  3114                              <1> ;	jnz	error
  3115                              <1> ;	stc
  3116                              <1> ;	retn
  3117                              <1> 
  3118                              <1> 	; Original UNIX v1 'ctty' routine:
  3119                              <1> 	;	
  3120                              <1>         ;mov    tty+[ntty*8]-8+6,r5 
  3121                              <1> 	;		;/ point r5 to the console tty buffer
  3122                              <1>         ;decb   (r5) / dec number of processes using console tty
  3123                              <1>         ;br     sret / return via sret
  3124                              <1> 
  3125                              <1> ;ccvt:	; < close tty >
  3126                              <1> 	; 21/05/2013 - 13/01/2014 (Retro UNIX 8086 v1)
  3127                              <1> 	;
  3128                              <1> 	; Retro UNIX 8086 v1 modification !
  3129                              <1> 	; 
  3130                              <1> 	; In original UNIX v1, 'ccvt' routine 
  3131                              <1> 	;		(exactly different than this one)
  3132                              <1> 	;	was in 'u9.s' file.
  3133                              <1> 	;
  3134                              <1> 	; DL = 2 -> it is open for reading
  3135                              <1> 	; DL = 1 -> it is open for writing
  3136                              <1> 	;
  3137                              <1> 	; 17/09/2013
  3138                              <1> 	;sub 	al, 10
  3139                              <1> 	;cmp	al, 7
  3140                              <1> 	;jna	short cttyp
  3141                              <1> 	; 13/01/2014
  3142                              <1> 	;jmp	short cttyp
  3143                              <1> 
  3144                              <1> ;cppt: / close paper tape
  3145                              <1> ;        clrb   pptiflg / set pptiflg to indicate file not open
  3146                              <1> ;1:
  3147                              <1> ;        mov    $240,*$ps /set process or priority to 5
  3148                              <1> ;        jsr    r0,getc; 2 / remove all ppt input entries from clist
  3149                              <1> ;                          / and assign to free list
  3150                              <1> ;               br sret
  3151                              <1> ;        br     1b
  3152                              <1> 
  3153                              <1> ;ejec:	
  3154                              <1> ;	jmp	error
  3155                              <1> ;/ejec:
  3156                              <1> ;/       mov    $100,*$lps / set line printer interrupt enable bit
  3157                              <1> ;/       mov    $14,r1 / 'form feed' character in r1 (new page).
  3158                              <1> ;/       jsr    r0,lptoc / space the printer to a new page
  3159                              <1> ;/       br     sret / return to caller via 'sret'
  2077                                  %include 'u8.s'      ; 11/06/2015
  2078                              <1> ; Temporary Runix kernel v2.0 file for debug - 22/11/2021
  2079                              <1> ; (re-write kernel for test by using previous version without a major defect)
  2080                              <1> ; ****************************************************************************
  2081                              <1> ; Retro UNIX 386 v1 Kernel (v0.2) - SYS8.INC
  2082                              <1> ; Last Modification: 12/01/2022
  2083                              <1> ; ----------------------------------------------------------------------------
  2084                              <1> ; Derived from 'Retro UNIX 8086 v1' source code by Erdogan Tan
  2085                              <1> ; (v0.1 - Beginning: 11/07/2012)
  2086                              <1> ;
  2087                              <1> ; Derived from UNIX Operating System (v1.0 for PDP-11) 
  2088                              <1> ; (Original) Source Code by Ken Thompson (1971-1972)
  2089                              <1> ; <Bell Laboratories (17/3/1972)>
  2090                              <1> ; <Preliminary Release of UNIX Implementation Document>
  2091                              <1> ;
  2092                              <1> ; Retro UNIX 8086 v1 - U8.ASM (18/01/2014) //// UNIX v1 -> u8.s
  2093                              <1> ;
  2094                              <1> ; ****************************************************************************
  2095                              <1> 
  2096                              <1> ;; I/O Buffer - Retro UNIX 386 v1 modification
  2097                              <1> ;;     (8+512 bytes, 8 bytes header, 512 bytes data)
  2098                              <1> ;; Word 1, byte 0 = device id
  2099                              <1> ;; Word 1, byte 1 = status bits (bits 8 to 15)
  2100                              <1> ;;          bit 9 = write bit
  2101                              <1> ;;	    bit 10 = read bit	  
  2102                              <1> ;;	    bit 12 = waiting to write bit	
  2103                              <1> ;;	    bit 13 = waiting to read bit
  2104                              <1> ;;	    bit 15 = inhibit bit
  2105                              <1> ;; Word 2 (byte 2 & byte 3) = reserved (for now - 07/06/2015)
  2106                              <1> ;; Word 3 + Word 4 (byte 4,5,6,7) = physical block number 
  2107                              <1> ;;		   (In fact, it is 32 bit LBA for Retro UNIX 386 v1)
  2108                              <1> ;;
  2109                              <1> ;; I/O Buffer ((8+512 bytes in original Unix v1))
  2110                              <1> ;;	      ((4+512 bytes in Retro UNIX 8086 v1))
  2111                              <1> ;;
  2112                              <1> ;; I/O Queue Entry (of original UNIX operating system v1)
  2113                              <1> ;; Word 1, Byte 0 = device id
  2114                              <1> ;; Word 1, Byte 1 = (bits 8 to 15)
  2115                              <1> ;;          bit 9 = write bit
  2116                              <1> ;;	    bit 10 = read bit	  
  2117                              <1> ;;	    bit 12 = waiting to write bit	
  2118                              <1> ;;	    bit 13 = waiting to read bit
  2119                              <1> ;;	    bit 15 = inhibit bit
  2120                              <1> ;; Word 2 = physical block number (In fact, it is LBA for Retro UNIX 8086 v1)
  2121                              <1> ;;
  2122                              <1> ;; Original UNIX v1 ->
  2123                              <1> ;;		Word 3 = number of words in buffer (=256) 		
  2124                              <1> ;; Original UNIX v1 -> 
  2125                              <1> ;;		Word 4 = bus address (addr of first word of data buffer)
  2126                              <1> ;;
  2127                              <1> ;; Retro UNIX 8086 v1 -> Buffer Header (I/O Queue Entry) size is 4 bytes !
  2128                              <1> ;;
  2129                              <1> ;; Device IDs (of Retro Unix 8086 v1)
  2130                              <1> ;;          0 = fd0
  2131                              <1> ;;	    1 = fd1
  2132                              <1> ;;	    2 = hd0
  2133                              <1> ;;	    3 = hd1
  2134                              <1> ;;	    4 = hd2
  2135                              <1> ;;	    5 = hd3
  2136                              <1> 
  2137                              <1> ; Retro UNIX 386 v1 - 32 bit modifications (rfd, wfd, rhd, whd) - 09/06/2015
  2138                              <1> 
  2139                              <1> 	; 12/01/2022 (Retro UNIX 386 v1.2)
  2140                              <1> rfd:	; 09/06/2015 (Retro UNIX 386 v1 - Beginning)
  2141                              <1> 	; 26/04/2013
  2142                              <1>    	; 13/03/2013 Retro UNIX 8086 v1 device (not an original unix v1 device)	
  2143                              <1>    	;sub 	ax, 3 ; zero based device number (Floppy disk)
  2144                              <1>       	;jmp 	short bread ; **** returns to routine that called readi			
  2145                              <1> 
  2146                              <1> 	; 12/01/2022 (Retro UNIX 386 v1.2)
  2147                              <1> rhd:	; 09/06/2015 (Retro UNIX 386 v1 - Beginning)
  2148                              <1> 	; 26/04/2013
  2149                              <1>    	; 14/03/2013 Retro UNIX 8086 v1 device (not an original unix v1 device)	
  2150                              <1>    	;sub 	ax, 3 ; zero based device number (Hard disk)
  2151                              <1>    	;jmp	short bread ; **** returns to routine that called readi	
  2152                              <1> 
  2153                              <1> bread: 
  2154                              <1> 	; 12/01/2022 (Retro UNIX 386 v1.2)
  2155                              <1> 	; 14/07/2015
  2156                              <1> 	; 10/07/2015
  2157                              <1> 	; 09/06/2015
  2158                              <1> 	; 07/06/2015 (Retro UNIX 386 v1 - Beginning)
  2159                              <1> 	; 13/03/2013 - 29/07/2013 (Retro UNIX 8086 v1)
  2160                              <1> 	;	
  2161                              <1> 	; / read a block from a block structured device
  2162                              <1> 	;
  2163                              <1> 	; INPUTS ->
  2164                              <1> 	;    [u.fopf] points to the block number
  2165                              <1> 	;    CX = maximum block number allowed on device
  2166                              <1> 	;	 ; that was an arg to bread, in original Unix v1, but
  2167                              <1> 	;	 ; CX register is used instead of arg in Retro Unix 8086 v1 		
  2168                              <1> 	;    [u.count]	number of bytes to read in
  2169                              <1> 	; OUTPUTS ->
  2170                              <1> 	;    [u.base] starting address of data block or blocks in user area  	
  2171                              <1> 	;    [u.fopf] points to next consecutive block to be read
  2172                              <1> 	;
  2173                              <1> 	; ((Modified registers: eAX, eDX, eCX, eBX, eSI, eDI, eBP))
  2174                              <1> 	;
  2175                              <1> 	; NOTE: Original UNIX v1 has/had a defect/bug here, even if read
  2176                              <1> 	;       byte count is less than 512, block number in *u.fofp (u.off)
  2177                              <1> 	;	is increased by 1. For example: If user/program request 
  2178                              <1> 	;       to read 16 bytes in current block, 'sys read' increases
  2179                              <1> 	;  	the next block number just as 512 byte reading is done.
  2180                              <1> 	;       This wrong is done in 'bread'. So, in Retro UNIX 8086 v1, 
  2181                              <1> 	;       for user (u) structure compatibility (because 16 bit is not
  2182                              <1> 	;       enough to keep byte position/offset of the disk), this
  2183                              <1> 	;	defect will not be corrected, user/program must request
  2184                              <1> 	;	512 byte read per every 'sys read' call to block devices
  2185                              <1> 	;       for achieving correct result. In future version(s), 
  2186                              <1> 	;	this defect will be corrected by using different 
  2187                              <1> 	;       user (u) structure. 26/07/2013 - Erdogan Tan 	
  2188                              <1> 
  2189                              <1> 	   	; jsr r0,tstdeve / error on special file I/O 
  2190                              <1> 			       ; / (only works on tape)
  2191                              <1> 		; mov *u.fofp,r1 / move block number to r1
  2192                              <1> 		; mov $2.-cold,-(sp) / "2-cold" to stack
  2193                              <1> ;1:
  2194                              <1> 		; cmp r1,(r0) / is this block # greater than or equal to
  2195                              <1> 			    ; / maximum block # allowed on device
  2196                              <1> 		; jnb short @f
  2197                              <1> 		; bhis	1f / yes, 1f (error)
  2198                              <1> 		; mov r1,-(sp) / no, put block # on stack
  2199                              <1> 		; jsr r0,preread / read in the block into an I/O buffer
  2200                              <1> 		; mov (sp)+,r1 / return block # to r1
  2201                              <1> 		; inc r1 / bump block # to next consecutive block
  2202                              <1> 		; dec (sp) / "2-1-cold" on stack
  2203                              <1> 		; bgt 1b / 2-1-cold = 0?  No, go back and read in next block
  2204                              <1> ;1:
  2205                              <1> 		; tst (sp)+ / yes, pop stack to clear off cold calculation
  2206                              <1> 	;push	ecx ; **
  2207                              <1> 	;26/04/2013
  2208                              <1> 	;sub	ax, 3 ; 3 to 8 -> 0 to 5
  2209 00005FF7 2C03                <1> 	sub	al, 3
  2210                              <1> 		; AL = Retro Unix 8086 v1 disk (block device) number
  2211 00005FF9 A2[4B750000]        <1> 	mov	[u.brwdev], al
  2212                              <1> 	; 09/06/2015
  2213                              <1> 	;movzx	ebx, al
  2214                              <1> 	;mov	ecx, [ebx+drv.size] ; disk size (in sectors)
  2215                              <1> 	; 12/01/2022 (BugFix)
  2216 00005FFE C0E002              <1> 	shl	al, 2 ; * 4
  2217 00006001 8B88[DE6A0000]      <1> 	mov	ecx, [eax+drv.size] ; disk size (in sectors)
  2218                              <1> bread_0:
  2219 00006007 51                  <1> 	push	ecx ; ** ; 09/06/2015 
  2220                              <1> 	; 10/07/2015 (Retro UNIX 386 v1 modification!)
  2221                              <1> 	; [u.fopf] points to byte position in disk, not sector/block !
  2222 00006008 8B1D[EC740000]      <1> 	mov	ebx, [u.fofp]
  2223 0000600E 8B03                <1> 	mov	eax, [ebx]
  2224 00006010 C1E809              <1> 	shr	eax, 9 ; convert byte position to block/sector number
  2225                              <1> 		; mov *u.fofp,r1 / restore r1 to initial value of the
  2226                              <1> 			       ; / block #
  2227 00006013 39C8                <1> 	cmp	eax, ecx
  2228                              <1> 		; cmp r1,(r0)+ / block # greater than or equal to maximum
  2229                              <1>        	                     ; / block number allowed
  2230                              <1> 	;jnb	error 	     ; 18/04/2013
  2231                              <1> 		; bhis error10 / yes, error
  2232 00006015 720F                <1> 	jb	short bread_1
  2233 00006017 C705[4C750000]1000- <1> 	mov 	dword [u.error], ERR_DEV_VOL_SIZE  ; 'out of volume' error	
  2233 0000601F 0000                <1>
  2234 00006021 E984DBFFFF          <1> 	jmp	error
  2235                              <1> bread_1:
  2236                              <1> 	; inc 	dword [ebx] ; 10/07/2015 (Retro UNIX 386 v1 - modification!)
  2237                              <1> 		; inc *u.fofp / no, *u.fofp has next block number
  2238                              <1> 	; eAX = Block number (zero based)
  2239                              <1> 		;;jsr r0,preread / read in the block whose number is in r1
  2240                              <1> preread: ;; call preread
  2241 00006026 BF[4B750000]        <1> 	mov	edi, u.brwdev ; block device number for direct I/O
  2242 0000602B E869020000          <1> 	call	bufaloc_0 ; 26/04/2013
  2243                              <1> 	;; jc 	error
  2244                              <1> 	; eBX = Buffer (Header) Address -Physical-
  2245                              <1>         ; eAX = Block/Sector number (r1)
  2246                              <1> 	       ; jsr r0,bufaloc / get a free I/O buffer (r1 has block number)
  2247                              <1> 	; 14/03/2013
  2248 00006030 740A                <1>         jz	short bread_2 ; Retro UNIX 8086 v1 modification
  2249                              <1>        		; br 1f / branch if block already in a I/O buffer
  2250 00006032 66810B0004          <1> 	or	word [ebx], 400h ; set read bit (10) in I/O Buffer
  2251                              <1>         	; bis $2000,(r5) / set read bit (bit 10 in I/O buffer)
  2252 00006037 E8B3010000          <1> 	call	poke
  2253                              <1>         	; jsr r0,poke / perform the read
  2254                              <1> 	;;jc	error ;2 0/07/2013
  2255                              <1> ; 1:
  2256                              <1>  		; clr *$ps / ps = 0
  2257                              <1>         	; rts r0
  2258                              <1> ;; return from preread
  2259                              <1> bread_2:
  2260 0000603C 66810B0040          <1> 	or	word [ebx], 4000h 
  2261                              <1> 		; bis $40000,(r5) 
  2262                              <1> 			; / set bit 14 of the 1st word of the I/O buffer
  2263                              <1> bread_3: ; 1:
  2264 00006041 66F7030024          <1> 	test	word [ebx], 2400h
  2265                              <1> 		; bit $22000,(r5) / are 10th and 13th bits set (read bits)
  2266 00006046 7407                <1> 	jz	short bread_4
  2267                              <1> 		; beq 1f / no
  2268                              <1> 		; cmp cdev,$1 / disk or drum?
  2269                              <1> 		; ble 2f / yes
  2270                              <1> 		; tstb uquant / is the time quantum = 0?
  2271                              <1> 		; bne 2f / no, 2f
  2272                              <1> 		; mov r5,-(sp) / yes, save r5 (buffer address)
  2273                              <1> 		; jsr r0,sleep; 31. 
  2274                              <1> 			; / put process to sleep in channel 31 (tape)
  2275                              <1> 		; mov (sp)+,r5 / restore r5
  2276                              <1> 		; br 1b / go back
  2277                              <1> ; 2: / drum or disk
  2278                              <1>         ;; mov	cx, [s.wait_]+2 ;; 29/07/2013
  2279 00006048 E8B9EFFFFF          <1> 	call	idle
  2280                              <1> 		; jsr r0,idle; s.wait+2 / wait
  2281 0000604D EBF2                <1> 	jmp	short bread_3
  2282                              <1>        		; br 1b
  2283                              <1> bread_4: ; 1: / 10th and 13th bits not set
  2284 0000604F 668123FFBF          <1> 	and	word [ebx], 0BFFFh ; 1011111111111111b
  2285                              <1> 		; bic $40000,(r5) / clear bit 14
  2286                              <1>        		; jsr r0,tstdeve / test device for error (tape)
  2287 00006054 83C308              <1> 	add	ebx, 8
  2288                              <1> 		; add $8,r5 / r5 points to data in I/O buffer
  2289                              <1> 	; 09/06/2015
  2290 00006057 66833D[44750000]00  <1> 	cmp	word [u.pcount], 0
  2291 0000605F 7705                <1> 	ja	short bread_5
  2292 00006061 E852F9FFFF          <1> 	call	trans_addr_w ; translate virtual address to physical (w)
  2293                              <1> bread_5:
  2294                              <1> 	; eBX = system (I/O) buffer address
  2295 00006066 E870000000          <1> 	call	dioreg
  2296                              <1>        		; jsr r0,dioreg / do bookkeeping on u.count etc.
  2297                              <1> 	; esi = start address of the transfer (in the buffer)
  2298                              <1> 	; edi = [u.pbase], destination address in user's memory space
  2299                              <1> 	; ecx = transfer count (in bytes)
  2300                              <1> 	;
  2301                              <1> ;1: / r5 points to beginning of data in I/O buffer, r2 points to beginning
  2302                              <1> ;   / of users data
  2303 0000606B F3A4                <1> 	rep	movsb
  2304                              <1> 		; movb (r5)+,(r2)+ / move data from the I/O buffer
  2305                              <1>        		; dec r3 / to the user's area in core starting at u.base
  2306                              <1>        		; bne 1b
  2307 0000606D 59                  <1> 	pop	ecx ; **
  2308 0000606E 833D[00750000]00    <1> 	cmp	dword [u.count], 0
  2309                              <1> 		; tst u.count / done
  2310 00006075 7790                <1> 	ja	short bread_0 ; 09/06/2015
  2311                              <1>        		; beq 1f / yes, return
  2312                              <1> 		; tst -(r0) / no, point r0 to the argument again
  2313                              <1>        		; br bread / read some more
  2314                              <1> ; 1:
  2315 00006077 58                  <1> 	pop	eax ; ****
  2316                              <1>        		; mov (sp)+,r0
  2317 00006078 C3                  <1>         retn		; 09/06/2015
  2318                              <1> 	;jmp	ret_ 
  2319                              <1> 		;jmp ret  / jump to routine that called readi
  2320                              <1> 
  2321                              <1> 	; 12/01/2022 (Retro UNIX 386 v1.2)
  2322                              <1> wfd:    ; 09/06/2015 (Retro UNIX 386 v1 - Beginning)
  2323                              <1> 	; 26/04/2013
  2324                              <1>    	; 14/03/2013 Retro UNIX 8086 v1 device (not an original unix v1 device)	
  2325                              <1>    	;sub 	ax, 3 ; zero based device number (Hard disk)
  2326                              <1>    	;jmp	short bwrite ; **** returns to routine that called writei
  2327                              <1> 
  2328                              <1> 	; 12/01/2022 (Retro UNIX 386 v1.2)				
  2329                              <1> whd:	; 09/06/2015 (Retro UNIX 386 v1 - Beginning)
  2330                              <1>    	; 14/03/2013 Retro UNIX 8086 v1 device (not an original unix v1 device)	
  2331                              <1>    	;sub 	ax, 3 ; zero based device number (Hard disk)
  2332                              <1>    	;jmp 	short bwrite ; **** returns to routine that called writei ('jmp ret')
  2333                              <1> 
  2334                              <1> bwrite: 
  2335                              <1> 	; 12/01/2022 (Retro UNIX 386 v1.2)
  2336                              <1> 	; 14/07/2015
  2337                              <1> 	; 10/07/2015
  2338                              <1> 	; 09/06/2015 (Retro UNIX 386 v1 - Beginning)
  2339                              <1> 	; 14/03/2013 - 20/07/2013 (Retro UNIX 8086 v1)
  2340                              <1> 	;	
  2341                              <1> 	;; / write on block structured device
  2342                              <1> 	;
  2343                              <1> 	; INPUTS ->
  2344                              <1> 	;    [u.fopf] points to the block number
  2345                              <1> 	;    CX = maximum block number allowed on device
  2346                              <1> 	;	 ; that was an arg to bwrite, in original Unix v1, but
  2347                              <1> 	;	 ; CX register is used instead of arg in Retro Unix 8086 v1 		
  2348                              <1> 	;    [u.count]	number of bytes to user desires to write
  2349                              <1> 	; OUTPUTS ->
  2350                              <1> 	;    [u.fopf] points to next consecutive block to be written into
  2351                              <1> 	;
  2352                              <1> 	; ((Modified registers: eDX, eCX, eBX, eSI, eDI, eBP))
  2353                              <1> 	;
  2354                              <1> 	; NOTE: Original UNIX v1 has/had a defect/bug here, even if write
  2355                              <1> 	;       byte count is less than 512, block number in *u.fofp (u.off)
  2356                              <1> 	;	is increased by 1. For example: If user/program request 
  2357                              <1> 	;       to write 16 bytes in current block, 'sys write' increases
  2358                              <1> 	;  	the next block number just as 512 byte writing is done.
  2359                              <1> 	;       This wrong is done in 'bwrite'. So, in Retro UNIX 8086 v1, 
  2360                              <1> 	;       for user (u) structure compatibility (because 16 bit is not
  2361                              <1> 	;       enough to keep byte position/offset of the disk), this
  2362                              <1> 	;	defect will not be corrected, user/program must request
  2363                              <1> 	;	512 byte write per every 'sys write' call to block devices
  2364                              <1> 	;       for achieving correct result. In future version(s), 
  2365                              <1> 	;	this defect will be corrected by using different 
  2366                              <1> 	;       user (u) structure. 26/07/2013 - Erdogan Tan 	
  2367                              <1> 
  2368                              <1>        		; jsr r0,tstdeve / test the device for an error
  2369                              <1> 	;push	ecx ; **
  2370                              <1> 	;26/04/2013
  2371                              <1> 	;sub	ax, 3 ; 3 to 8 -> 0 to 5
  2372 00006079 2C03                <1> 	sub	al, 3
  2373                              <1> 		; AL = Retro Unix 8086 v1 disk (block device) number
  2374 0000607B A2[4B750000]        <1> 	mov	[u.brwdev], al
  2375                              <1> 	; 09/06/2015
  2376                              <1> 	;movzx	ebx, al
  2377                              <1> 	;mov	ecx, [ebx+drv.size] ; disk size (in sectors)
  2378                              <1> 	; 12/01/2022 (BugFix)
  2379 00006080 C0E002              <1> 	shl	al, 2 ; * 4
  2380 00006083 8B88[DE6A0000]      <1> 	mov	ecx, [eax+drv.size] ; disk size (in sectors)
  2381                              <1> bwrite_0:
  2382 00006089 51                  <1> 	push	ecx ; ** ; 09/06/2015
  2383                              <1> 	; 10/07/2015 (Retro UNIX 386 v1 modification!)
  2384                              <1> 	; [u.fopf] points to byte position in disk, not sector/block !
  2385 0000608A 8B1D[EC740000]      <1> 	mov	ebx, [u.fofp]
  2386 00006090 8B03                <1> 	mov	eax, [ebx]       
  2387 00006092 C1E809              <1> 	shr	eax, 9 ; convert byte position to block/sector number
  2388                              <1> 		; mov *u.fofp,r1 / put the block number in r1
  2389 00006095 39C8                <1> 	cmp	eax, ecx
  2390                              <1> 		; cmp r1,(r0)+ / does block number exceed maximum allowable #
  2391                              <1>        	                     ; / block number allowed
  2392                              <1> 	;jnb	error	     ; 18/04/2013
  2393                              <1> 		; bhis error10 / yes, error
  2394 00006097 720F                <1>      	jb	short bwrite_1
  2395 00006099 C705[4C750000]1000- <1> 	mov 	dword [u.error], ERR_DEV_VOL_SIZE  ; 'out of volume' error	
  2395 000060A1 0000                <1>
  2396 000060A3 E902DBFFFF          <1> 	jmp	error
  2397                              <1> bwrite_1:
  2398                              <1> 	; inc 	dword [ebx] ; 10/07/2015 (Retro UNIX 386 v1 - modification!)
  2399                              <1> 		; inc *u.fofp / no, increment block number
  2400                              <1> 	; 09/06/2015 - 10/07/2015
  2401 000060A8 66833D[44750000]00  <1> 	cmp	word [u.pcount], 0
  2402 000060B0 7705                <1> 	ja	short bwrite_2
  2403 000060B2 E8FDF8FFFF          <1> 	call	trans_addr_r ; translate virtual address to physical (r)
  2404                              <1> bwrite_2:
  2405 000060B7 BF[4B750000]        <1> 	mov	edi, u.brwdev  ; block device number for direct I/O
  2406 000060BC E8C4000000          <1>        	call	bwslot ; 26/04/2013 (wslot -> bwslot)	 				
  2407                              <1> 		; jsr r0,wslot / get an I/O buffer to write into
  2408                              <1> 		; add $8,r5 / r5 points to data in I/O buffer
  2409 000060C1 E815000000          <1>         call	dioreg
  2410                              <1> 		; jsr r0,dioreg / do the necessary bookkeeping
  2411                              <1> 	; esi = destination address (in the buffer)
  2412                              <1> 	; edi = [u.pbase], start address of transfer in user's memory space
  2413                              <1> 	; ecx = transfer count (in bytes)
  2414                              <1> ; 1: / r2 points to the users data; r5 points to the I/O buffers data area
  2415 000060C6 87F7                <1> 	xchg 	esi, edi ; 14/07/2015
  2416 000060C8 F3A4                <1> 	rep	movsb
  2417                              <1> 		; movb (r2)+,(r5)+ / ; r3, has the byte count
  2418                              <1>        		; dec r3 / area to the I/O buffer
  2419                              <1>        		; bne 1b
  2420 000060CA E8FE000000          <1> 	call	dskwr
  2421                              <1> 		; jsr r0,dskwr / write it out on the device
  2422 000060CF 59                  <1> 	pop	ecx ; **
  2423 000060D0 833D[00750000]00    <1>         cmp     dword [u.count], 0
  2424                              <1> 		; tst u.count / done
  2425 000060D7 77B0                <1> 	ja	short bwrite_0 ; 09/06/2015
  2426                              <1> 		; beq 1f / yes, 1f
  2427                              <1> 		; tst -(r0) / no, point r0 to the argument of the call
  2428                              <1>        		; br bwrite / go back and write next block
  2429                              <1> ; 1:
  2430 000060D9 58                  <1> 	pop	eax ; ****
  2431                              <1>        		; mov (sp)+,r0
  2432 000060DA C3                  <1> 	retn		; 09/06/2015
  2433                              <1>         ;jmp	ret_ 
  2434                              <1> 		; jmp ret / return to routine that called writei
  2435                              <1> ;error10:
  2436                              <1> ;       jmp     error  ; / see 'error' routine
  2437                              <1> 
  2438                              <1> dioreg:
  2439                              <1> 	; 14/07/2015
  2440                              <1> 	; 10/07/2015 (UNIX v1 bugfix - [u.fofp]: byte pos., not block)
  2441                              <1> 	; 09/06/2015 (Retro UNIX 386 v1 - Beginning)
  2442                              <1> 	; 14/03/2013 (Retro UNIX 8086 v1)
  2443                              <1> 	;	
  2444                              <1> 	; bookkeeping on block transfers of data
  2445                              <1> 	;
  2446                              <1> 	; * returns value of u.pbase before it gets updated, in EDI
  2447                              <1> 	; * returns byte count (to transfer) in ECX (<=512)
  2448                              <1> 	; 10/07/2015
  2449                              <1> 	; * returns byte offset from beginning of current sector buffer
  2450                              <1> 	; (beginning of data) in ESI
  2451                              <1> 	;
  2452 000060DB 8B0D[00750000]      <1> 	mov	ecx, [u.count]
  2453                              <1> 		; mov u.count,r3 / move char count to r3
  2454 000060E1 81F900020000        <1>        	cmp 	ecx, 512
  2455                              <1> 		; cmp r3,$512. / more than 512. char?
  2456 000060E7 7605                <1> 	jna	short dioreg_0
  2457                              <1> 		; blos 1f / no, branch
  2458 000060E9 B900020000          <1> 	mov	ecx, 512
  2459                              <1> 		; mov $512.,r3 / yes, just take 512.
  2460                              <1> dioreg_0:
  2461                              <1> 	; 09/06/2015
  2462 000060EE 663B0D[44750000]    <1> 	cmp	cx, [u.pcount]
  2463 000060F5 7607                <1> 	jna	short dioreg_1
  2464 000060F7 668B0D[44750000]    <1> 	mov	cx, [u.pcount]
  2465                              <1> dioreg_1:
  2466                              <1> ; 1:
  2467 000060FE 8B15[FC740000]      <1> 	mov	edx, [u.base] ; 09/06/2015 (eax -> edx)
  2468                              <1> 	        ; mov u.base,r2 / put users base in r2
  2469 00006104 010D[04750000]      <1> 	add	[u.nread], ecx
  2470                              <1> 		; add r3,u.nread / add the number to be read to u.nread
  2471 0000610A 290D[00750000]      <1> 	sub	[u.count], ecx
  2472                              <1> 		; sub r3,u.count / update count
  2473 00006110 010D[FC740000]      <1> 	add	[u.base], ecx
  2474                              <1> 		; add r3,u.base / update base
  2475                              <1> 	; 10/07/2015
  2476                              <1> 	; Retro UNIX 386 v1 - modification !
  2477                              <1> 	; (File pointer points to byte position, not block/sector no.)
  2478                              <1> 	; (It will point to next byte position instead of next block no.)
  2479 00006116 8B35[EC740000]      <1> 	mov	esi, [u.fofp] ; u.fopf points to byte position pointer  
  2480 0000611C 8B06                <1> 	mov	eax, [esi] ; esi points to current byte pos. on the disk
  2481 0000611E 010E                <1> 	add	[esi], ecx ; ecx is added to set the next byte position
  2482 00006120 25FF010000          <1> 	and	eax, 1FFh  ; get offset from beginning of current block	
  2483 00006125 89DE                <1> 	mov	esi, ebx   ; beginning of data in sector/block buffer
  2484 00006127 01C6                <1> 	add	esi, eax   ; esi contains start address of the transfer
  2485                              <1> 	; 09/06/2015 - 10/07/2015
  2486 00006129 66290D[44750000]    <1> 	sub	[u.pcount], cx
  2487 00006130 81E2FF0F0000        <1> 	and	edx, PAGE_OFF ; 0FFFh
  2488 00006136 8B3D[40750000]      <1> 	mov	edi, [u.pbase]
  2489 0000613C 81E700F0FFFF        <1> 	and	edi, ~PAGE_OFF
  2490 00006142 01D7                <1> 	add	edi, edx
  2491 00006144 893D[40750000]      <1> 	mov	[u.pbase], edi
  2492 0000614A 010D[40750000]      <1> 	add	[u.pbase], ecx ; 14/07/2015
  2493 00006150 C3                  <1> 	retn
  2494                              <1> 		; rts r0 / return
  2495                              <1> 
  2496                              <1> dskrd:
  2497                              <1> 	; 18/08/2015
  2498                              <1> 	; 02/07/2015
  2499                              <1> 	; 09/06/2015 (Retro UNIX 386 v1 - Beginning)
  2500                              <1> 	; 14/03/2013 - 29/07/2013 (Retro UNIX 8086 v1)
  2501                              <1> 	;
  2502                              <1> 	; 'dskrd' acquires an I/O buffer, puts in the proper
  2503                              <1> 	; I/O queue entries (via bufaloc) then reads a block
  2504                              <1> 	; (number specified in r1) in the acquired buffer.)
  2505                              <1> 	; If the device is busy at the time dskrd is called,	
  2506                              <1> 	; dskrd calls idle.
  2507                              <1> 	; 
  2508                              <1> 	; INPUTS ->
  2509                              <1> 	;    r1 - block number
  2510                              <1> 	;    cdev - current device number 
  2511                              <1> 	; OUTPUTS ->
  2512                              <1> 	;    r5 - points to first data word in I/O buffer
  2513                              <1> 	;
  2514                              <1> 	; ((AX = R1)) input/output
  2515                              <1> 	; ((BX = R5)) output 
  2516                              <1> 	;
  2517                              <1>         ; ((Modified registers: eDX, eCX, eBX, eSI, eDI, eBP))  
  2518                              <1> 	;
  2519 00006151 E836010000          <1> 	call 	bufaloc
  2520                              <1> 		; jsr r0,bufaloc / shuffle off to bufaloc; 
  2521                              <1> 			       ; / get a free I/O buffer
  2522                              <1> 	;;jc	error ; 20/07/2013
  2523 00006156 741B                <1> 	jz	short dskrd_1 ; Retro UNIX 8086 v1 modification
  2524                              <1>        		; br 1f / branch if block already in a I/O buffer
  2525                              <1> dskrd_0: ; 10/07/2015 (wslot)
  2526 00006158 66810B0004          <1> 	or	word [ebx], 400h ; set read bit (10) in I/O Buffer
  2527                              <1>         	; bis $2000,(r5) / set bit 10 of word 1 of 
  2528                              <1> 		               ; / I/O queue entry for buffer
  2529 0000615D E88D000000          <1> 	call	poke
  2530                              <1> 		; jsr r0,poke / just assigned in bufaloc, 
  2531                              <1> 			    ; /	bit 10=1 says read
  2532                              <1> 	; 09/06/2015
  2533 00006162 730F                <1> 	jnc	short dskrd_1
  2534                              <1> 	;
  2535 00006164 C705[4C750000]1100- <1> 	mov	dword [u.error], ERR_DRV_READ ; disk read error !
  2535 0000616C 0000                <1>
  2536 0000616E E937DAFFFF          <1> 	jmp	error
  2537                              <1> dskrd_1: ; 1:
  2538                              <1>        		;clr *$ps
  2539 00006173 66F7030024          <1>        	test	word [ebx], 2400h
  2540                              <1> 		; bit $22000,(r5) / if either bits 10, or 13 are 1; 
  2541                              <1> 				; / jump to idle
  2542 00006178 7407                <1>        	jz	short dskrd_2
  2543                              <1> 		; beq 1f
  2544                              <1>         ;;mov   ecx, [s.wait_]
  2545 0000617A E887EEFFFF          <1>        	call	idle
  2546                              <1> 		; jsr r0,idle; s.wait+2
  2547 0000617F EBF2                <1> 	jmp 	short dskrd_1
  2548                              <1>        		; br 1b
  2549                              <1> dskrd_2: ; 1:
  2550 00006181 83C308              <1>         add	ebx, 8
  2551                              <1> 		; add $8,r5 / r5 points to first word of data in block 
  2552                              <1> 			  ; / just read in
  2553 00006184 C3                  <1>        	retn
  2554                              <1> 		; rts r0
  2555                              <1> 
  2556                              <1> bwslot:
  2557                              <1> 	; 12/01/2022 (Retro UNIX 386 v1.2)
  2558                              <1> 	; 10/07/2015
  2559                              <1> 	;	If the block/sector is not placed in a buffer
  2560                              <1> 	;	before 'wslot', it must be read before
  2561                              <1> 	;	it is written! (Otherwise transfer counts less
  2562                              <1> 	;	than 512 bytes will be able to destroy existing 
  2563                              <1> 	;	data on disk.)
  2564                              <1> 	;
  2565                              <1> 	; 11/06/2015 (Retro UNIX 386 v1 - Beginning)
  2566                              <1> 	; 26/04/2013(Retro UNIX 8086 v1)
  2567                              <1> 	; Retro UNIX 8086 v1 modification !
  2568                              <1> 	; ('bwslot' will be called from 'bwrite' only!)
  2569                              <1> 	; INPUT -> eDI - points to device id (in u.brwdev)	
  2570                              <1> 	;	-> eAX = block number
  2571                              <1> 	;
  2572 00006185 E80F010000          <1> 	call	bufaloc_0
  2573 0000618A 742A                <1> 	jz	short wslot_0 ; block/sector already is in the buffer
  2574                              <1> 	; 12/01/2022
  2575                              <1> 	; ebx = buffer header address
  2576                              <1> bwslot_0:
  2577                              <1> 	; 10/07/2015
  2578 0000618C 8B35[EC740000]      <1> 	mov	esi, [u.fofp]
  2579 00006192 8B06                <1> 	mov	eax, [esi]
  2580 00006194 25FF010000          <1> 	and	eax, 1FFh ; offset from beginning of the sector/block
  2581 00006199 750C                <1> 	jnz 	short bwslot_1 ; it is not a full sector write
  2582                              <1> 		       ; recent disk data must be placed in the buffer
  2583 0000619B 813D[00750000]0002- <1> 	cmp	dword [u.count], 512
  2583 000061A3 0000                <1>
  2584 000061A5 730F                <1> 	jnb	short wslot_0	
  2585                              <1> bwslot_1:
  2586 000061A7 E8ACFFFFFF          <1> 	call	dskrd_0
  2587                              <1> 	; 12/01/2022
  2588                              <1> 	; ebx = buffer data address = buffer header addr + 8
  2589 000061AC 83EB08              <1> 	sub	ebx, 8 ; set ebx to the buffer header address again	
  2590 000061AF EB05                <1> 	jmp 	short wslot_0
  2591                              <1> 
  2592                              <1> wslot:
  2593                              <1> 	; 11/06/2015 (Retro UNIX 386 v1 - Beginning)
  2594                              <1> 	; 		(32 bit modifications)
  2595                              <1> 	; 14/03/2013 - 29/07/2013(Retro UNIX 8086 v1)
  2596                              <1> 	;
  2597                              <1> 	; 'wslot' calls 'bufaloc' and obtains as a result, a pointer
  2598                              <1> 	; to the I/O queue of an I/O buffer for a block structured
  2599                              <1> 	; device. It then checks the first word of I/O queue entry.	
  2600                              <1> 	; If bits 10 and/or 13 (read bit, waiting to read bit) are set,
  2601                              <1> 	; wslot calls 'idle'. When 'idle' returns, or if bits 10 
  2602                              <1> 	; and/or 13 are not set, 'wslot' sets bits 9 and 15 of the first
  2603                              <1> 	; word of the I/O queue entry (write bit, inhibit bit).
  2604                              <1> 	;
  2605                              <1> 	; INPUTS ->
  2606                              <1>  	;    r1 - block number
  2607                              <1> 	;    cdev - current (block/disk) device number
  2608                              <1>  	;
  2609                              <1> 	; OUTPUTS ->
  2610                              <1> 	;    bufp - bits 9 and 15 are set, 
  2611                              <1> 	;           the remainder of the word left unchanged
  2612                              <1> 	;    r5 - points to first data word in I/O buffer
  2613                              <1> 	;
  2614                              <1> 	; ((AX = R1)) input/output
  2615                              <1> 	; ((BX = R5)) output 
  2616                              <1> 	;
  2617                              <1>         ; ((Modified registers: eDX, eCX, eBX, eSI, eDI, eBP)) 
  2618                              <1> 
  2619 000061B1 E8D6000000          <1> 	call	bufaloc
  2620                              <1> 	; 10/07/2015
  2621                              <1> 		; jsr r0,bufaloc / get a free I/O buffer; pointer to first
  2622                              <1>         	; br 1f / word in buffer in r5
  2623                              <1> 	; eBX = Buffer (Header) Address (r5) (ES=CS=DS, system/kernel segment)
  2624                              <1>         ; eAX = Block/Sector number (r1)
  2625                              <1> wslot_0: ;1:
  2626 000061B6 66F7030024          <1>      	test	word [ebx], 2400h
  2627                              <1> 		; bit $22000,(r5) / check bits 10, 13 (read, waiting to read)
  2628                              <1> 				; / of I/O queue entry
  2629 000061BB 7407                <1> 	jz	short wslot_1
  2630                              <1>                 ; beq 1f  / branch if 10, 13 zero (i.e., not reading, 
  2631                              <1> 		     ; / or not waiting to read)
  2632                              <1> 
  2633                              <1>         ;; mov	ecx, [s.wait_] ; 29/07/2013
  2634 000061BD E844EEFFFF          <1> 	call	idle
  2635                              <1> 		; jsr r0,idle; / if buffer is reading or writing to read,
  2636                              <1>        	                     ; / idle
  2637 000061C2 EBF2                <1> 	jmp	short wslot_0
  2638                              <1> 		; br 1b / till finished
  2639                              <1> wslot_1: ;1:
  2640 000061C4 66810B0082          <1>         or      word [ebx], 8200h
  2641                              <1>        		; bis $101000,(r5) / set bits 9, 15 in 1st word of I/O queue
  2642                              <1>                             	 ; / (write, inhibit bits)
  2643                              <1>        		; clr     *$ps / clear processor status
  2644 000061C9 83C308              <1>         add	ebx, 8 ; 11/06/2015
  2645                              <1> 		; add $8,r5 / r5 points to first word in data area 
  2646                              <1> 			  ; / for this block
  2647 000061CC C3                  <1>        	retn
  2648                              <1> 		; rts r0
  2649                              <1> dskwr:
  2650                              <1> 	; 09/06/2015 (Retro UNIX 386 v1 - Beginning)
  2651                              <1> 	; 14/03/2013 - 03/08/2013 (Retro UNIX 8086 v1)
  2652                              <1> 	;
  2653                              <1> 	; 'dskwr' writes a block out on disk, via ppoke. The only
  2654                              <1> 	; thing dskwr does is clear bit 15 in the first word of I/O queue
  2655                              <1> 	; entry pointed by 'bufp'. 'wslot' which must have been called
  2656                              <1> 	; previously has supplied all the information required in the
  2657                              <1> 	; I/O queue entry.
  2658                              <1> 	;
  2659                              <1> 	; (Modified registers: eCX, eDX, eBX, eSI, eDI)
  2660                              <1> 	;
  2661                              <1> 	;
  2662 000061CD 8B1D[94740000]      <1> 	mov	ebx, [bufp]
  2663 000061D3 668123FF7F          <1> 	and	word [ebx], 7FFFh ; 0111111111111111b
  2664                              <1> 		; bic $100000,*bufp / clear bit 15 of I/O queue entry at
  2665                              <1>                                   ; / bottom of queue
  2666 000061D8 E812000000          <1> 	call	poke
  2667                              <1> 	; 09/06/2015
  2668 000061DD 730F                <1> 	jnc	short dskwr_1
  2669 000061DF C705[4C750000]1200- <1> 	mov	dword [u.error], ERR_DRV_WRITE ; disk write error !
  2669 000061E7 0000                <1>
  2670 000061E9 E9BCD9FFFF          <1> 	jmp	error
  2671                              <1> dskwr_1:
  2672 000061EE C3                  <1> 	retn
  2673                              <1> 
  2674                              <1> ;ppoke:
  2675                              <1>        		; mov $340,*$ps
  2676                              <1>        		; jsr r0,poke
  2677                              <1>        		; clr *$ps
  2678                              <1> 		; rts r0
  2679                              <1> poke:
  2680                              <1> 	; 29/11/2021 (32 bit reg push-pop)
  2681                              <1> 	; 24/10/2015
  2682                              <1> 	; 20/08/2015
  2683                              <1> 	; 18/08/2015
  2684                              <1> 	; 02/07/2015
  2685                              <1> 	; 09/06/2015 (Retro UNIX 386 v1 - Beginning)
  2686                              <1> 	; 15/03/2013 - 18/01/2014 (Retro UNIX 8086 v1)
  2687                              <1> 	;
  2688                              <1> 	; (NOTE: There are some disk I/O code modifications & extensions
  2689                              <1> 	; & exclusions on original 'poke' & other device I/O procedures of 
  2690                              <1> 	; UNIX v1 OS for performing disk I/O functions by using IBM PC 
  2691                              <1> 	; compatible rombios calls in Retro UNIX 8086 v1 kernel.)
  2692                              <1> 	;
  2693                              <1> 	; Basic I/O functions for all block structured devices
  2694                              <1> 	;
  2695                              <1>         ; (Modified registers: eCX, eDX, eSI, eDI)
  2696                              <1> 	;
  2697                              <1> 	; 20/07/2013 modifications
  2698                              <1> 	;            (Retro UNIX 8086 v1 features only !)
  2699                              <1> 	; INPUTS -> 
  2700                              <1> 	;        (EBX = buffer header address)
  2701                              <1> 	; OUTPUTS ->
  2702                              <1> 	;	 cf=0 -> successed r/w (at least, for the caller's buffer) 
  2703                              <1> 	;	 cf=1 -> error, word [eBX] = 0FFFFh
  2704                              <1> 	;		(drive not ready or r/w error!)
  2705                              <1> 	;	 (dword [EBX+4] <> 0FFFFFFFFh indicates r/w success)	
  2706                              <1> 	;	 (dword [EBx+4] = 0FFFFFFFFh means RW/IO error)
  2707                              <1> 	;        (also it indicates invalid buffer data)
  2708                              <1> 	;
  2709 000061EF 53                  <1> 	push	ebx
  2710                              <1>        		; mov r1,-(sp)
  2711                              <1>        		; mov r2,-(sp)
  2712                              <1>        		; mov r3,-(sp)
  2713 000061F0 50                  <1> 	push 	eax ; Physical Block Number (r1) (mget)
  2714                              <1> 	;
  2715                              <1> 	; 09/06/2015
  2716                              <1> 	; (permit read/write after a disk R/W error)
  2717 000061F1 8A0B                <1> 	mov	cl, [ebx] ; device id (0 to 5)
  2718 000061F3 B001                <1> 	mov	al, 1
  2719 000061F5 D2E0                <1> 	shl	al, cl
  2720 000061F7 8405[BC740000]      <1> 	test 	al, [active] ; busy ? (error)
  2721 000061FD 7408                <1> 	jz	short poke_0
  2722 000061FF F6D0                <1> 	not	al
  2723 00006201 2005[BC740000]      <1> 	and	[active], al ; reset busy bit for this device only
  2724                              <1> poke_0:
  2725 00006207 BE[B4740000]        <1>         mov     esi, bufp + (4*(nbuf+2)) 
  2726                              <1> 		; mov $bufp+nbuf+nbuf+6,r2 / r2 points to highest priority
  2727                              <1> 					 ; / I/O queue pointer
  2728                              <1> poke_1: ; 1:
  2729 0000620C 83EE04              <1>         sub	esi, 4
  2730 0000620F 8B1E                <1> 	mov	ebx, [esi]
  2731                              <1> 		; mov -(r2),r1 / r1 points to an I/O queue entry
  2732 00006211 668B03              <1> 	mov	ax, [ebx] ; 17/07/2013
  2733 00006214 F6C406              <1>        	test	ah, 06h
  2734                              <1> 	;test	word [ebx], 600h ; 0000011000000000b
  2735                              <1> 		; bit $3000,(r1) / test bits 9 and 10 of word 1 of I/O 
  2736                              <1> 			       ; / queue entry
  2737 00006217 745C                <1>         jz      short poke_5
  2738                              <1> 		; beq 2f / branch to 2f if both are clear
  2739                              <1> 	; 31/07/2013
  2740                              <1> 	;test	ah, 0B0h ; (*)
  2741                              <1> 	;;test	word [ebx], 0B000h ; 1011000000000000b
  2742                              <1> 		; bit $130000,(r1) / test bits 12, 13, and 15
  2743                              <1>         ;jnz     short poke_5 ; 31/07/2013 (*)
  2744                              <1> 		; bne 2f / branch if any are set
  2745                              <1> 	;movzx	ecx, byte [ebx] ; 09/06/2015 ; Device Id
  2746                              <1>     		; movb (r1),r3 / get device id
  2747 00006219 0FB6C8              <1> 	movzx	ecx, al ; 18/08/2015
  2748                              <1> 	;mov	edi, ecx ; 26/04/2013
  2749 0000621C 31C0                <1> 	xor 	eax, eax ; 0
  2750                              <1> 	;cmp 	[edi+drv.error], al ; 0
  2751                              <1> 		; tstb deverr(r3) / test for errors on this device
  2752                              <1>        	;jna	short poke_2 
  2753                              <1> 		; beq 3f / branch if no errors
  2754                              <1> 	; 02/07/2015
  2755                              <1> 	;dec	eax
  2756                              <1> 	;mov	[ebx+4], ax ; 0FFFFFFFFh ; -1 
  2757                              <1>        		; mov $-1,2(r1) / destroy associativity
  2758                              <1> 	;shr	eax, 24
  2759                              <1> 	;mov	[ebx], eax ; 000000FFh, reset
  2760                              <1> 		; clrb 1(r1) / do not do I/O
  2761                              <1> 	;jmp     short poke_5
  2762                              <1>         ;       ; br 2f
  2763                              <1>                 ; rts r0
  2764                              <1> poke_2: ; 3:
  2765                              <1> 	; 02/07/2015
  2766 0000621E FEC1                <1> 	inc	cl ; 0FFh -> 0
  2767 00006220 7453                <1> 	jz	short poke_5
  2768 00006222 FEC0                <1> 	inc	al ; mov ax, 1
  2769 00006224 FEC9                <1> 	dec	cl
  2770 00006226 7402                <1> 	jz	short poke_3
  2771                              <1> 	; 26/04/2013 Modification
  2772                              <1> 	;inc	al ; mov ax, 1
  2773                              <1> 	;or	cl, cl ; Retro UNIX 8086 v1 device id.
  2774                              <1> 	;jz	short poke_3 ; cl = 0
  2775 00006228 D2E0                <1> 	shl	al, cl ; shl ax, cl
  2776                              <1> poke_3:
  2777                              <1> 	;test	[active], ax
  2778 0000622A 8405[BC740000]      <1> 	test	[active], al
  2779                              <1> 		; bit $2,active / test disk busy bit
  2780 00006230 7543                <1> 	jnz     short poke_5
  2781                              <1> 		; bne 2f / branch if bit is set
  2782                              <1> 	;or	[active], ax
  2783 00006232 0805[BC740000]      <1> 	or	[active], al
  2784                              <1> 		; bis $2,active / set disk busy bit
  2785 00006238 50                  <1> 	push	eax ; 29/11/2021
  2786 00006239 E8D1000000          <1> 	call	diskio ; Retro UNIX 8086 v1 Only !
  2787                              <1> 	;mov    [edi+drv.error], ah
  2788 0000623E 58                  <1> 	pop	eax
  2789 0000623F 730E                <1> 	jnc	short poke_4 ; 20/07/2013
  2790                              <1> 	;cmp 	[edi+drv.error], al ; 0	
  2791                              <1> 	;jna	short poke_4
  2792                              <1> 		; tstb deverr(r3) / test for errors on this device
  2793                              <1>        		; beq 3f / branch if no errors
  2794                              <1> 	; 02/07/2015 (32 bit modification)
  2795                              <1> 	; 20/07/2013
  2796 00006241 C74304FFFFFFFF      <1> 	mov	dword [ebx+4], 0FFFFFFFFh ; -1 
  2797                              <1>        		; mov $-1,2(r1) / destroy associativity
  2798 00006248 66C703FF00          <1> 	mov	word [ebx], 0FFh ; 20/08/2015 
  2799                              <1> 		; clrb 1(r1) / do not do I/O
  2800 0000624D EB26                <1> 	jmp     short poke_5
  2801                              <1> poke_4:	; 20/07/2013
  2802                              <1> 	; 17/07/2013
  2803 0000624F F6D0                <1> 	not 	al 
  2804 00006251 2005[BC740000]      <1> 	and	[active], al ; reset, not busy
  2805                              <1> 	; eBX = system I/O buffer header (queue entry) address
  2806                              <1> seta: ; / I/O queue bookkeeping; set read/write waiting bits.
  2807 00006257 668B03              <1> 	mov	ax, [ebx]
  2808                              <1>        		; mov (r1),r3 / move word 1 of I/O queue entry into r3
  2809 0000625A 66250006            <1>         and	ax, 600h
  2810                              <1> 		; bic $!3000,r3 / clear all bits except 9 and 10
  2811 0000625E 668123FFF9          <1> 	and 	word [ebx], 0F9FFh
  2812                              <1>        		; bic $3000,(r1) / clear only bits 9 and 10
  2813 00006263 C0E403              <1> 	shl	ah, 3
  2814                              <1>        		; rol r3
  2815                              <1>                 ; rol r3
  2816                              <1>                 ; rol r3	
  2817 00006266 660903              <1> 	or	[ebx], ax
  2818                              <1> 		; bis r3,(r1) / or old value of bits 9 and 10 with 
  2819                              <1> 			   ; bits 12 and 13
  2820 00006269 E898EDFFFF          <1> 	call	idle ; 18/01/2014
  2821                              <1> 	;; sti
  2822                              <1> 	;hlt 	; wait for a hardware interrupt
  2823                              <1> 	;; cli
  2824                              <1> 	; NOTE: In fact, disk controller's 'disk I/O completed' 
  2825                              <1>         ; interrupt would be used to reset busy bits, but INT 13h
  2826                              <1> 	; returns when disk I/O is completed. So, here, as temporary
  2827                              <1> 	; method, this procedure will wait for a time according to
  2828                              <1> 	; multi tasking and time sharing concept.
  2829                              <1> 	;
  2830                              <1> 	; 24/10/2015
  2831                              <1> 	;not	ax 
  2832 0000626E 66B8FF00            <1> 	mov 	ax, 0FFh ; 24/10/2015 (temporary)
  2833 00006272 662103              <1> 	and	[ebx], ax ; clear bits 12 and 13
  2834                              <1> poke_5: ;2:
  2835 00006275 81FE[94740000]      <1>         cmp     esi, bufp
  2836                              <1> 		; cmp r2,$bufp / test to see if entire I/O queue 
  2837                              <1>                             ; / has been scanned
  2838 0000627B 778F                <1> 	ja      short poke_1
  2839                              <1> 		; bhi 1b
  2840                              <1> 	; 24/03/2013
  2841                              <1>        		; mov (sp)+,r3
  2842                              <1>        		; mov (sp)+,r2
  2843                              <1>        		; mov (sp)+,r1
  2844 0000627D 58                  <1>         pop 	eax  ; Physical Block Number (r1) (mget)
  2845 0000627E 5B                  <1> 	pop 	ebx
  2846                              <1> 	; 02/07/2015 (32 bit modification)
  2847                              <1> 	; 20/07/2013
  2848                              <1> 	;cmp 	dword [ebx+4], 0FFFFFFFFh
  2849 0000627F 803BFF              <1> 	cmp	byte [ebx], 0FFh ; 20/08/2015
  2850                              <1> 	;	
  2851                              <1> 	; 'poke' returns with cf=0 if the requested buffer is read 
  2852                              <1> 	; or written succesfully; even if an error occurs while
  2853                              <1> 	; reading to or writing from other buffers. 20/07/2013
  2854                              <1> 	;
  2855                              <1> 	; 09/06/2015
  2856 00006282 F5                  <1> 	cmc
  2857 00006283 C3                  <1> 	retn
  2858                              <1>                 ; rts r0
  2859                              <1> 
  2860                              <1> 	; 27/11/2021 - Retro UNIX 386 v2 compatibility code
  2861                              <1> get_system_time:
  2862                              <1> 	; 27/10/2021
  2863                              <1> 	; 21/02/2020 - Retro UNIX 386 v2
  2864                              <1> 	; get system time
  2865                              <1> 	;  (from recent value which is saved in RTC interrupt)
  2866                              <1> 	; Input:
  2867                              <1> 	; 	none
  2868                              <1> 	; Return:
  2869                              <1> 	;   	eax = system time in unix epoch format 
  2870                              <1> 	;
  2871                              <1> 	; Modified registers: eax, ecx, edx
  2872                              <1> 	
  2873                              <1> 	; 27/11/2021
  2874 00006284 53                  <1> 	push	ebx
  2875 00006285 E83DD4FFFF          <1> 	call	epoch
  2876 0000628A 5B                  <1> 	pop	ebx
  2877 0000628B C3                  <1> 	retn
  2878                              <1> 	
  2879                              <1> 	; 12/01/2022 (Retro UNIX 386 v1.2)
  2880                              <1> 	;  ('get_serial_number' is not used in v1.2 kernel)
  2881                              <1> ;get_serial_number:
  2882                              <1> 	; temporary - 21/02/2020
  2883                              <1> 	; 26/04/2020 (esi)
  2884                              <1> 	; 25/04/2020
  2885                              <1> 	; (Possible) Modified registers: eax, ecx, edx
  2886                              <1> 	; (edi, ebx & esi registers must be preserved)
  2887                              <1> 	; Return:
  2888                              <1> 	; eax = 32 bit serial number  
  2889                              <1> 	; (edx = high 32 bit of 64 bit serial number)
  2890                              <1> 	; -64 bit serial numbers will not be used in RUNIX 386 v2-
  2891                              <1> 	
  2892                              <1> 	;retn
  2893                              <1> 
  2894                              <1> bufaloc:
  2895                              <1> 	; 09/01/2022 (Retro UNIX 386 v1.2)
  2896                              <1> 	; 20/08/2015
  2897                              <1> 	; 19/08/2015
  2898                              <1> 	; 02/07/2015
  2899                              <1> 	; 11/06/2015 (Retro UNIX 386 v1 - Beginning)
  2900                              <1> 	;	     (32 bit modifications)	
  2901                              <1> 	; 13/03/2013 - 29/07/2013 (Retro UNIX 8086 v1)
  2902                              <1> 	;
  2903                              <1> 	; bufaloc - Block device I/O buffer allocation
  2904                              <1> 	; 
  2905                              <1> 	; INPUTS ->
  2906                              <1> 	;    r1 - block number
  2907                              <1> 	;    cdev - current (block/disk) device number
  2908                              <1> 	;    bufp+(2*n)-2 --- n = 1 ... nbuff
  2909                              <1> 	; OUTPUTS ->
  2910                              <1> 	;    r5 - pointer to buffer allocated
  2911                              <1> 	;    bufp ... bufp+12 --- (bufp), (bufp)+2
  2912                              <1> 	;
  2913                              <1> 	; ((AX = R1)) input/output
  2914                              <1> 	; ((BX = R5)) output
  2915                              <1>         ;    ((Modified registers: DX, CX, BX, SI, DI, BP))
  2916                              <1> 	;    zf=1 -> block already in a I/O buffer
  2917                              <1> 	;    zf=0 -> a new I/O buffer has been allocated
  2918                              <1> 	;    ((DL = Device ID))
  2919                              <1> 	;    (((DH = 0 or 1)))
  2920                              <1> 	;    (((CX = previous value of word ptr [bufp])))
  2921                              <1> 	;    ((CX and DH will not be used after return)))
  2922                              <1> 
  2923                              <1> 	;;push 	esi ; ***
  2924                              <1> 		; mov r2,-(sp) / save r2 on stack
  2925                              <1>        		; mov $340,*$ps / set processor priority to 7
  2926                              <1> 	; 20/07/2013
  2927                              <1> 	; 26/04/2013
  2928                              <1> 	;movzx	ebx, byte [cdev] ; 0 or 1
  2929                              <1> 	;mov	edi, rdev  ; offset mdev = offset rdev + 1
  2930                              <1> 	;add	edi, ebx
  2931                              <1> 	; 09/01/2022
  2932 0000628C 0FB63D[B9740000]    <1> 	movzx	edi, byte [cdev] ; 0 or 1
  2933 00006293 81C7[BA740000]      <1> 	add	edi, rdev
  2934                              <1> bufaloc_0: ; 26/04/2013 !! here is called from bread or bwrite !!
  2935                              <1> 			;; eDI points to device id.
  2936 00006299 0FB61F              <1> 	movzx	ebx, byte [edi] ; [EDI] -> rdev/mdev or brwdev
  2937                              <1> 	; 11/06/2021
  2938 0000629C 80BB[FA6A0000]F0    <1> 	cmp 	byte [ebx+drv.status], 0F0h ; Drive not ready !
  2939 000062A3 720F                <1> 	jb	short bufaloc_9
  2940 000062A5 C705[4C750000]0F00- <1> 	mov	dword [u.error], ERR_DRV_NOT_RDY
  2940 000062AD 0000                <1>
  2941 000062AF E9F6D8FFFF          <1> 	jmp	error
  2942                              <1> bufaloc_9:
  2943 000062B4 89DA                <1> 	mov	edx, ebx ; dh = 0, dl = device number (0 to 5)
  2944                              <1> bufaloc_10: ; 02/07/2015
  2945 000062B6 31ED                <1> 	xor 	ebp, ebp ; 0
  2946 000062B8 55                  <1> 	push	ebp ; 0
  2947 000062B9 89E5                <1>         mov     ebp, esp	
  2948                              <1> 	;
  2949                              <1> bufaloc_1: ;1:
  2950                              <1> 		; clr -(sp) / vacant buffer
  2951 000062BB BE[94740000]        <1>         mov 	esi, bufp
  2952                              <1> 		; mov $bufp,r2 / bufp contains pointers to I/O queue 
  2953                              <1> 			     ; / entrys in buffer area
  2954                              <1> bufaloc_2: ;2:
  2955 000062C0 8B1E                <1> 	mov	ebx, [esi]
  2956                              <1>        		; mov (r2)+,r5 / move pointer to word 1 of an I/O 
  2957                              <1> 			    ; queue entry into r5
  2958 000062C2 66F70300F6          <1> 	test	word [ebx], 0F600h
  2959                              <1> 		; bit $173000,(r5) / lock+keep+active+outstanding
  2960 000062C7 7503                <1>         jnz	short bufaloc_3
  2961                              <1> 		; bne 3f / branch when 
  2962                              <1> 		       ; / any of bits 9,10,12,13,14,15 are set
  2963                              <1>                        ; / (i.e., buffer busy)
  2964 000062C9 897500              <1>         mov     [ebp], esi ; pointer to I/0 queue entry
  2965                              <1>                 ; mov  r2,(sp) ;/ save pointer to last non-busy buffer
  2966                              <1> 			; / found points to word 2 of I/O queue entry)
  2967                              <1> bufaloc_3: ;3:
  2968                              <1> 	;mov	dl, [edi] ; 26/04/2013
  2969                              <1> 	;
  2970 000062CC 3813                <1> 	cmp	[ebx], dl	
  2971                              <1> 		; cmpb (r5),cdev / is device in I/O queue entry same 
  2972                              <1> 			       ; / as current device
  2973 000062CE 7508                <1> 	jne	short bufaloc_4
  2974                              <1>        		; bne 3f
  2975 000062D0 394304              <1> 	cmp	[ebx+4], eax
  2976                              <1>        		; cmp 2(r5),r1 / is block number in I/O queue entry, 
  2977                              <1> 			     ; / same as current block number
  2978 000062D3 7503                <1>        	jne	short bufaloc_4
  2979                              <1> 		; bne 3f
  2980                              <1> 	;add	esp, 4
  2981 000062D5 59                  <1> 	pop	ecx
  2982                              <1>        		; tst (sp)+ / bump stack pointer
  2983 000062D6 EB20                <1> 	jmp	short bufaloc_7 ; Retro Unix 8086 v1 modification
  2984                              <1> 				; jump to bufaloc_6 in original Unix v1
  2985                              <1>        		; br 1f / use this buffer
  2986                              <1> bufaloc_4: ;3:
  2987 000062D8 83C604              <1> 	add	esi, 4 ; 20/08/2015
  2988                              <1> 	;
  2989 000062DB 81FE[AC740000]      <1> 	cmp	esi, bufp + (nbuf*4)
  2990                              <1> 		; cmp r2,$bufp+nbuf+nbuf
  2991 000062E1 72DD                <1> 	jb	short bufaloc_2
  2992                              <1> 		; blo 2b / go to 2b if r2 less than bufp+nbuf+nbuf (all
  2993                              <1>                        ; / buffers not checked)
  2994 000062E3 5E                  <1>         pop	esi
  2995                              <1> 		; mov (sp)+,r2 / once all bufs are examined move pointer
  2996                              <1> 			     ; / to last free block
  2997 000062E4 09F6                <1>        	or	esi, esi 
  2998 000062E6 7507                <1> 	jnz	short bufaloc_5
  2999                              <1> 		; bne 2f / if (sp) is non zero, i.e., 
  3000                              <1> 	        ; / if a free buffer is found branch to 2f
  3001                              <1>         ;; mov  ecx, [s.wait_]
  3002 000062E8 E819EDFFFF          <1> 	call	idle
  3003                              <1> 		; jsr r0,idle; s.wait+2 / idle if no free buffers
  3004 000062ED EBC7                <1> 	jmp 	short bufaloc_10 ; 02/07/2015
  3005                              <1>        		; br 1b
  3006                              <1> bufaloc_5: ;2:
  3007                              <1> 		; tst (r0)+ / skip if warmed over buffer
  3008 000062EF FEC6                <1> 	inc	dh ; Retro UNIX 8086 v1 modification
  3009                              <1> bufaloc_6: ;1:
  3010 000062F1 8B1E                <1>         mov    	ebx, [esi] 
  3011                              <1> 		; mov -(r2),r5 / put pointer to word 1 of I/O queue 
  3012                              <1> 			     ; / entry in r5
  3013                              <1> 	;; 26/04/2013
  3014                              <1>         ;mov	dl, [edi] ; byte [rdev] or byte [mdev]
  3015 000062F3 8813                <1> 	mov 	[ebx], dl
  3016                              <1> 		; movb cdev,(r5) / put current device number 
  3017                              <1> 				 ; / in I/O queue entry
  3018 000062F5 894304              <1> 	mov 	[ebx+4], eax
  3019                              <1> 		; mov r1,2(r5) / move block number into word 2 
  3020                              <1> 			     ; / of I/O queue entry
  3021                              <1> bufaloc_7: ;1:
  3022 000062F8 81FE[94740000]      <1>         cmp	esi, bufp
  3023                              <1> 		; cmp r2,$bufp / bump all entrys in bufp 
  3024                              <1> 			     ; / and put latest assigned
  3025 000062FE 760A                <1> 	jna	short bufaloc_8	
  3026                              <1>        		; blos 1f / buffer on the top 
  3027                              <1> 			; / (this makes if the lowest priority)
  3028 00006300 83EE04              <1> 	sub	esi, 4
  3029 00006303 8B0E                <1> 	mov	ecx, [esi]
  3030 00006305 894E04              <1> 	mov	[esi+4], ecx
  3031                              <1> 		; mov -(r2),2(r2) / job for a particular device
  3032 00006308 EBEE                <1> 	jmp 	short bufaloc_7        
  3033                              <1> 		; br 1b
  3034                              <1> bufaloc_8: ;1:
  3035 0000630A 891E                <1>         mov	[esi], ebx
  3036                              <1> 		; mov r5,(r2)
  3037                              <1> 	;;pop	esi ; ***
  3038                              <1>        		; mov (sp)+,r2 / restore r2
  3039 0000630C 08F6                <1>        	or 	dh, dh ; 0 or 1 ?
  3040                              <1> 		; Retro UNIX 8086 v1 modification
  3041                              <1> 		; zf=1 --> block already is in an I/O buffer
  3042                              <1> 		; zf=0 --> a new I/O buffer has been allocated
  3043 0000630E C3                  <1> 	retn
  3044                              <1> 		; rts r0
  3045                              <1> 
  3046                              <1> diskio:
  3047                              <1> 	; 09/01/2022 (Retro UNIX 386 v1.2)
  3048                              <1> 	; 28/11/2021
  3049                              <1> 	; 10/07/2015
  3050                              <1> 	; 02/07/2015
  3051                              <1> 	; 16/06/2015
  3052                              <1> 	; 11/06/2015 (Retro UNIX 386 v1 - Beginning)
  3053                              <1> 	;	     (80386 protected mode modifications)	
  3054                              <1> 	; 15/03/2013 - 29/04/2013 (Retro UNIX 8086 v1)
  3055                              <1> 	;
  3056                              <1> 	; Retro UNIX 8086 v1 feature only !
  3057                              <1> 	;
  3058                              <1> 	; Derived from proc_chs_read procedure of TRDOS DISKIO.ASM (2011)
  3059                              <1> 	; 04/07/2009 - 20/07/2011
  3060                              <1> 	;
  3061                              <1> 	; NOTE: Reads only 1 block/sector (sector/block size is 512 bytes)
  3062                              <1> 	;
  3063                              <1>         ; INPUTS ->
  3064                              <1> 	; 	   ebx = System I/O Buffer header address
  3065                              <1> 	;
  3066                              <1>         ; OUTPUTS -> cf=0 --> done 
  3067                              <1> 	; 	     cf=1 ---> error code in AH
  3068                              <1> 	;
  3069                              <1> 	; (Modified registers: eax, ecx, edx)
  3070                              <1> 	
  3071                              <1> ;rw_disk_sector:
  3072                              <1> 	; 28/11/2021
  3073                              <1> 	; 10/07/2015
  3074                              <1> 	; 02/07/2015
  3075                              <1> 	; 11/06/2015 - Retro UNIX 386 v1 - 'u8.s' 
  3076                              <1> 	; 21/02/2015 ('dsectpm.s', 'read_disk_sector')
  3077                              <1> 	; 16/02/2015 (Retro UNIX 386 v1 test - 'unix386.s')
  3078                              <1> 	; 01/12/2014 - 18/01/2015 ('dsectrm2.s')
  3079                              <1> 	;
  3080                              <1> 	;mov	dx, 0201h ; Read 1 sector/block
  3081 0000630F B602                <1> 	mov	dh, 2
  3082                              <1> 	;mov	ax, [ebx]
  3083                              <1> 	; 28/11/2021
  3084 00006311 8B03                <1> 	mov	eax, [ebx]
  3085                              <1> 	;
  3086 00006313 56                  <1> 	push	esi ; ****
  3087 00006314 53                  <1> 	push	ebx ; ***
  3088                              <1> 	;
  3089 00006315 0FB6C8              <1> 	movzx	ecx, al
  3090                              <1> 	; 28/11/2021
  3091                              <1> 	;mov	ecx, eax
  3092 00006318 89CE                <1> 	mov	esi, ecx
  3093                              <1> 	;
  3094 0000631A 38F1                <1> 	cmp	cl, dh ; 2
  3095 0000631C 7202                <1> 	jb	short rwdsk0
  3096 0000631E 047E                <1> 	add	al, 7Eh  ; 80h, 81h, 82h, 83h
  3097                              <1> rwdsk0:
  3098 00006320 A2[AB6A0000]        <1> 	mov	[drv], al
  3099 00006325 81C6[FA6A0000]      <1> 	add	esi, drv.status
  3100                              <1> 	; 11/06/2015
  3101 0000632B 803EF0              <1> 	cmp	byte [esi], 0F0h 
  3102 0000632E 720F                <1> 	jb      short rwdsk1
  3103                              <1> 	; 'drive not ready' error
  3104 00006330 C705[4C750000]0F00- <1> 	mov	dword [u.error], ERR_DRV_NOT_RDY
  3104 00006338 0000                <1>
  3105 0000633A E96BD8FFFF          <1> 	jmp	error
  3106                              <1> rwdsk1:
  3107 0000633F F6C402              <1> 	test	ah, 2
  3108                              <1> 	;test	ax, 200h ; Bit 9 of word 0 (status word)
  3109                              <1> 			 ; write bit
  3110 00006342 7402                <1> 	jz	short rwdsk2
  3111                              <1> 	;test	ah, 4
  3112                              <1> 	;;test	ax, 400h ; Bit 10 of word 0 (status word)
  3113                              <1> 	;		 ; read bit
  3114                              <1> 	;jz	short diskio_ret
  3115 00006344 FEC6                <1> 	inc	dh ; 03h = write
  3116                              <1> rwdsk2:
  3117 00006346 88C2                <1> 	mov	dl, al
  3118 00006348 83C304              <1> 	add	ebx, 4 ; sector/block address/number pointer
  3119 0000634B 8B03                <1> 	mov	eax, [ebx] ; sector/block number (LBA)
  3120 0000634D C0E102              <1> 	shl	cl, 2
  3121 00006350 81C1[DE6A0000]      <1> 	add	ecx, drv.size ; disk size
  3122 00006356 3B01                <1> 	cmp	eax, [ecx] ; Last sector + 1 (number of secs.)
  3123 00006358 720F                <1> 	jb      short rwdsk3
  3124                              <1>  	; 'out of volume' error
  3125 0000635A C705[4C750000]1000- <1> 	mov 	dword [u.error], ERR_DEV_VOL_SIZE
  3125 00006362 0000                <1>
  3126 00006364 E941D8FFFF          <1> 	jmp	error
  3127                              <1> rwdsk3:
  3128                              <1> 	; 11/06/2015
  3129 00006369 83C304              <1> 	add	ebx, 4 ; buffer address
  3130 0000636C C605[6F750000]04    <1> 	mov	byte [retry_count], 4
  3131 00006373 F60601              <1> 	test	byte [esi], 1 ; LBA ready ?
  3132 00006376 7432                <1>         jz      short rwdsk_chs
  3133                              <1> rwdsk_lba:
  3134                              <1> 	; LBA read/write (with private LBA function) 
  3135                              <1> 	;((Retro UNIX 386 v1 - DISK I/O code by Erdogan Tan))
  3136 00006378 83C607              <1>         add     esi, drv.error - drv.status ; 10/07/2015
  3137 0000637B 89C1                <1> 	mov	ecx, eax ; sector number
  3138                              <1> 	; ebx = buffer (data) address
  3139                              <1> 	; dl = physical drive number (0,1, 80h, 81h, 82h, 83h)
  3140                              <1> rwdsk_lba_retry:
  3141                              <1> 	;mov	dl, [drv]
  3142                              <1> 		; Function 1Bh = LBA read, 1Ch = LBA write
  3143 0000637D B419                <1> 	mov	ah, 1Ch - 03h ; LBA write function number - 3
  3144 0000637F 00F4                <1> 	add	ah, dh		
  3145 00006381 B001                <1> 	mov	al, 1
  3146                              <1> 	;int	13h
  3147 00006383 E8C4C2FFFF          <1> 	call	int13h
  3148 00006388 8826                <1> 	mov	[esi], ah ; error code ; 10/07/2015
  3149 0000638A 730E                <1> 	jnc	short rwdsk_lba_ok
  3150 0000638C 80FC80              <1> 	cmp	ah, 80h ; time out ?
  3151 0000638F 7408                <1>         je      short rwdsk_lba_fails
  3152 00006391 FE0D[6F750000]      <1> 	dec	byte [retry_count]
  3153 00006397 7504                <1>         jnz     short rwdsk_lba_reset ; 10/07/2015
  3154                              <1> rwdsk_lba_fails:
  3155 00006399 F9                  <1> 	stc
  3156                              <1> rwdsk_lba_ok:
  3157 0000639A 5B                  <1> 	pop	ebx ; ***
  3158 0000639B 5E                  <1> 	pop	esi ; ****
  3159 0000639C C3                  <1> 	retn
  3160                              <1> rwdsk_lba_reset:
  3161 0000639D B40D                <1> 	mov	ah, 0Dh ; Alternate reset
  3162                              <1> 	;int	13h
  3163 0000639F E8A8C2FFFF          <1>         call	int13h
  3164 000063A4 73D7                <1> 	jnc     short rwdsk_lba_retry
  3165 000063A6 8826                <1> 	mov	[esi], ah ; error code ; 10/07/2015
  3166 000063A8 EBF0                <1> 	jmp	short rwdsk_lba_ok
  3167                              <1> 	;
  3168                              <1> 	; CHS read (convert LBA address to CHS values)	
  3169                              <1> rwdsk_chs:
  3170                              <1> 	; 10/07/2015
  3171 000063AA 81EE[FA6A0000]      <1> 	sub	esi, drv.status
  3172 000063B0 89F1                <1> 	mov	ecx, esi
  3173 000063B2 81C6[016B0000]      <1> 	add 	esi, drv.error
  3174                              <1> 	; 02/07/2015
  3175                              <1> 	; 16/06/2015
  3176                              <1>  	; 11/06/2015 
  3177 000063B8 53                  <1> 	push	ebx ; ** ; buffer
  3178 000063B9 D1E1                <1> 	shl	ecx, 1
  3179 000063BB 51                  <1> 	push	ecx ; * 
  3180                              <1> 	;
  3181 000063BC 89CB                <1> 	mov	ebx, ecx
  3182 000063BE 8835[6D750000]      <1> 	mov	[rwdsk], dh ; 02/07/2015
  3183 000063C4 31D2                <1> 	xor	edx, edx ; 0
  3184                              <1> 	; 09/01/2022
  3185                              <1> 	;sub	ecx, ecx 
  3186 000063C6 81C3[D06A0000]      <1>         add     ebx, drv.spt
  3187 000063CC 668B0B              <1> 	mov	cx, [ebx] ; sector per track
  3188                              <1> 		; EDX:EAX = LBA
  3189 000063CF F7F1                <1> 	div	ecx
  3190 000063D1 88D1                <1> 	mov	cl, dl	; sector number - 1
  3191 000063D3 FEC1                <1> 	inc	cl	; sector number (1 based)
  3192 000063D5 5B                  <1> 	pop	ebx ; * ; 11/06/2015
  3193                              <1> 	;push	cx
  3194 000063D6 51                  <1> 	push	ecx ; 28/11/2021
  3195 000063D7 81C3[C26A0000]      <1>         add     ebx, drv.heads
  3196 000063DD 668B0B              <1> 	mov	cx, [ebx] ; heads
  3197 000063E0 31D2                <1> 	xor	edx, edx
  3198                              <1> 		; EAX = cylinders * heads + head
  3199 000063E2 F7F1                <1> 	div	ecx
  3200                              <1> 	;pop	cx     ; sector number
  3201 000063E4 59                  <1> 	pop	ecx ; 28/11/2021
  3202 000063E5 88D6                <1> 	mov	dh, dl ; head number
  3203 000063E7 8A15[AB6A0000]      <1> 	mov	dl, [drv]
  3204 000063ED 88C5                <1> 	mov	ch, al ; cylinder (bits 0-7)
  3205 000063EF C0E406              <1> 	shl	ah, 6
  3206 000063F2 08E1                <1> 	or	cl, ah ; cylinder (bits 8-9)
  3207                              <1> 		       ; sector (bits 0-7)
  3208 000063F4 5B                  <1> 	pop	ebx ; ** ; buffer ; 11/06/2015
  3209                              <1> 		; CL = sector (bits 0-5)
  3210                              <1> 		;      cylinder (bits 8-9 -> bits 6-7)
  3211                              <1> 		; CH = cylinder (bits 0-7)
  3212                              <1> 		; DH = head
  3213                              <1> 		; DL = drive
  3214                              <1> 	;
  3215 000063F5 C605[6F750000]04    <1> 	mov	byte [retry_count], 4
  3216                              <1> rwdsk_retry:	
  3217 000063FC 8A25[6D750000]      <1> 	mov	ah, [rwdsk] ; 02h = read, 03h = write
  3218 00006402 B001                <1> 	mov	al, 1 ; sector count	
  3219                              <1> 	;int	13h
  3220 00006404 E843C2FFFF          <1> 	call	int13h
  3221 00006409 8826                <1> 	mov	[esi], ah ; error code ; 10/07/2015
  3222 0000640B 730E                <1> 	jnc	short rwdsk_ok ; ah = 0
  3223 0000640D 80FC80              <1> 	cmp	ah, 80h ; time out ?
  3224 00006410 7408                <1> 	je	short rwdsk_fails
  3225 00006412 FE0D[6F750000]      <1> 	dec	byte [retry_count]
  3226 00006418 7504                <1> 	jnz	short rwdsk_reset
  3227                              <1> rwdsk_fails:
  3228 0000641A F9                  <1> 	stc
  3229                              <1> rwdsk_ok:
  3230 0000641B 5B                  <1> 	pop	ebx ; ***
  3231 0000641C 5E                  <1> 	pop	esi ; ****
  3232 0000641D C3                  <1> 	retn
  3233                              <1> rwdsk_reset:
  3234                              <1> 	; 02/02/2015
  3235 0000641E 28E4                <1> 	sub	ah, ah
  3236 00006420 80FA80              <1> 	cmp	dl, 80h
  3237 00006423 7202                <1> 	jb	short rwdsk_fd_reset
  3238 00006425 B40D                <1> 	mov	ah, 0Dh ; Alternate reset
  3239                              <1> rwdsk_fd_reset:
  3240                              <1> 	;int	13h
  3241 00006427 E820C2FFFF          <1>         call	int13h
  3242 0000642C 73CE                <1> 	jnc	short rwdsk_retry
  3243 0000642E 8826                <1> 	mov	[esi], ah ; error code ; 10/07/2015
  3244 00006430 EBE9                <1> 	jmp 	short rwdsk_ok
  3245                              <1> 
  3246                              <1> ; Original UNIX v1 - drum (& disk) interrupt routine
  3247                              <1> ;	(Equivalent to IRQ 14 & IRQ 15 disk/hardware interrupts)
  3248                              <1> ;
  3249                              <1> ; This feature is not used in Retro UNIX 386 (& 8086) for now.
  3250                              <1> ; Because, current Retro UNIX 386 disk I/O -INT13H- routine is 
  3251                              <1> ; derived from IBM PC AT -infact: XT286- BIOS source code, int 13h
  3252                              <1> ; that uses hardware -transfer has been completed-  interrupt inside it. 
  3253                              <1> ; In a next Retro UNIX 386 version, these interrupts
  3254                              <1> ; (fdc_int, hdc1_int, hdc2_int) will be handled by a separate routine
  3255                              <1> ; as in original unix v1.
  3256                              <1> ; I am not removing IBM BIOS source code derivatives -compatible code-
  3257                              <1> ; for now, regarding the new/next 32 bit TRDOS project by me
  3258                              <1> ; (to keep source code files easy adaptable to 32 bit TRDOS.)
  3259                              <1> ;
  3260                              <1> ; Erdogan tan (10/07/2015) 
  3261                              <1> 
  3262                              <1> ;drum: / interrupt handler
  3263                              <1> ;       jsr     r0,setisp / save r1,r2,r3, and clockp on the stack
  3264                              <1> ;       jsr     r0,trapt; dcs; rfap; 1 / check for stray interrupt or
  3265                              <1> ;                                      / error
  3266                              <1> ;               br 3f / no, error
  3267                              <1> ;       br      2f / error
  3268                              <1> ;
  3269                              <1> ;disk:
  3270                              <1> ;       jsr     r0,setisp / save r1,r2,r3, and clockp on the stack
  3271                              <1> ;       jmp     *$0f
  3272                              <1> ;0:
  3273                              <1> ;       jsr     r0,trapt; rkcs; rkap; 2
  3274                              <1> ;      	        br 3f / no, errors
  3275                              <1> ;       mov     $115,(r2) / drive reset, errbit was set
  3276                              <1> ;       mov     $1f,0b-2 / next time jmp *$0f is executed jmp will be
  3277                              <1> ;                        / to 1f
  3278                              <1> ;       br      4f
  3279                              <1> ;1:
  3280                              <1> ;       bit     $20000,rkcs
  3281                              <1> ;       beq     4f / wait for seek complete
  3282                              <1> ;       mov     $0b,0b-2
  3283                              <1> ;       mov     rkap,r1
  3284                              <1> ;2:
  3285                              <1> ;       bit     $3000,(r1) / are bits 9 or 10 set in the 1st word of
  3286                              <1> ;                          / the disk buffer
  3287                              <1> ;       bne     3f / no, branch ignore error if outstanding
  3288                              <1> ;       inc     r1
  3289                              <1> ;       asr     (r1)
  3290                              <1> ;       asr     (r1)
  3291                              <1> ;       asr     (r1) / reissue request
  3292                              <1> ;       dec     r1
  3293                              <1> ;3:
  3294                              <1> ;       bic     $30000,(r1) / clear bits 12 and 13 in 1st word of buffer
  3295                              <1> ;       mov     ac,-(sp)
  3296                              <1> ;       mov     mq,-(sp) / put these on the stack
  3297                              <1> ;       mov     sc,-(sp)
  3298                              <1> ;       jsr     r0,poke
  3299                              <1> ;       mov     (sp)+,sc
  3300                              <1> ;       mov     (sp)+,mq / pop them off stack
  3301                              <1> ;       mov     (sp)+,ac
  3302                              <1> ;4:
  3303                              <1> ;       jmp     retisp / u4-3
  3304                              <1> ;
  3305                              <1> ;trapt:                  / r2 points to the
  3306                              <1> ;       mov     (r0)+,r2 / device control register
  3307                              <1> ;       mov     *(r0)+,r1 / transaction pointer points to buffer
  3308                              <1> ;       tst     (sp)+
  3309                              <1> ;       tstb    (r2) / is ready bit of dcs set?
  3310                              <1> ;       bge     4b / device still active so branch
  3311                              <1> ;       bit     (r0),active / was device busy?
  3312                              <1> ;       beq     4b / no, stray interrupt
  3313                              <1> ;       bic     (r0)+,active / yes, set active to zero
  3314                              <1> ;       tst     (r2) / test the err(bit is) of dcs
  3315                              <1> ;       bge     2f / if no error jump to 2f
  3316                              <1> ;       tst     (r0)+ / skip on error
  3317                              <1> ; 2:
  3318                              <1> ;       jmp     (r0)
  2078                                  %include 'u9.s'      ; 29/06/2015
  2079                              <1> ; Temporary Runix kernel v2.0 file for debug - 22/11/2021
  2080                              <1> ; (re-write kernel for test by using previous version without a major defect)
  2081                              <1> ; ****************************************************************************
  2082                              <1> ; Retro UNIX 386 v1 Kernel (v0.2) - SYS9.INC
  2083                              <1> ; Last Modification: 12/01/2022
  2084                              <1> ; ----------------------------------------------------------------------------
  2085                              <1> ; Derived from 'Retro UNIX 8086 v1' source code by Erdogan Tan
  2086                              <1> ; (v0.1 - Beginning: 11/07/2012)
  2087                              <1> ;
  2088                              <1> ; Derived from UNIX Operating System (v1.0 for PDP-11) 
  2089                              <1> ; (Original) Source Code by Ken Thompson (1971-1972)
  2090                              <1> ; <Bell Laboratories (17/3/1972)>
  2091                              <1> ; <Preliminary Release of UNIX Implementation Document>
  2092                              <1> ;
  2093                              <1> ; Retro UNIX 8086 v1 - U9.ASM (01/09/2014) //// UNIX v1 -> u9.s
  2094                              <1> ;
  2095                              <1> ; ****************************************************************************
  2096                              <1> 
  2097                              <1> getch:
  2098                              <1> 	; 30/06/2015
  2099                              <1> 	; 18/02/2015 - Retro UNIX 386 v1 - feature only!
  2100 00006432 28C0                <1> 	sub	al, al ; 0
  2101                              <1> getch_q: ; 06/08/2015
  2102 00006434 8A25[E66F0000]      <1> 	mov 	ah, [ptty] ; active (current) video page
  2103 0000643A EB06                <1>         jmp     short getc_n
  2104                              <1> 
  2105                              <1> getc: 
  2106                              <1> 	; 12/11/2015
  2107                              <1> 	; 15/09/2015
  2108                              <1> 	; 01/07/2015
  2109                              <1> 	; 30/06/2015
  2110                              <1> 	; 18/02/2015 (Retro UNIX 386 v1 - Beginning)
  2111                              <1> 	; 13/05/2013 - 04/07/2014 (Retro UNIX 8086 v1)
  2112                              <1> 	;
  2113                              <1> 	; Retro UNIX 8086 v1 modification !
  2114                              <1> 	; 
  2115                              <1> 	; 'getc' gets (next) character 
  2116                              <1> 	;	 from requested TTY (keyboard) buffer 
  2117                              <1> 	; INPUTS ->
  2118                              <1> 	;     [u.ttyn] = tty number (0 to 7) (8 is COM1, 9 is COM2)	
  2119                              <1> 	;     AL=0 -> Get (next) character from requested TTY buffer
  2120                              <1> 	;	(Keyboard buffer will point to 
  2121                              <1> 	;			next character at next call)
  2122                              <1> 	;     AL=1 -> Test a key is available in requested TTY buffer
  2123                              <1> 	;	(Keyboard buffer will point to 
  2124                              <1> 	;			current character at next call)
  2125                              <1> 	; OUTPUTS ->
  2126                              <1> 	;     (If AL input is 1) ZF=1 -> 'empty buffer' (no chars)
  2127                              <1> 	;     			 ZF=0 -> AX has (current) character
  2128                              <1> 	;      AL = ascii code
  2129                              <1> 	;      AH = scan code	(AH = line status for COM1 or COM2)	 			
  2130                              <1> 	; 		        (cf=1 -> error code/flags in AH)
  2131                              <1> 	; Original UNIX V1 'getc': 
  2132                              <1> 	;		get a character off character list
  2133                              <1> 	;
  2134                              <1> 	; ((Modified registers: eAX, eBX, eCX, eDX, eSI, eDI))	
  2135                              <1> 	;
  2136                              <1> 	; 30/06/2015 (32 bit modifications)
  2137                              <1> 	; 16/07/2013
  2138                              <1> 	; mov 	[getctty], ah
  2139                              <1> 	;
  2140                              <1> 
  2141 0000643C 8A25[0D750000]      <1> 	mov	ah, [u.ttyn] 	; 28/07/2013
  2142                              <1> getc_n:
  2143                              <1> 	; 30/06/2015
  2144 00006442 08E4                <1> 	or	ah, ah
  2145 00006444 740D                <1> 	jz	short getc0 
  2146 00006446 D0E4                <1> 	shl	ah, 1
  2147 00006448 0FB6DC              <1> 	movzx	ebx, ah
  2148 0000644B 81C3[E86F0000]      <1> 	add	ebx, ttychr
  2149 00006451 EB05                <1> 	jmp	short getc1
  2150                              <1> getc0:
  2151 00006453 BB[E86F0000]        <1> 	mov	ebx, ttychr
  2152                              <1> getc1:
  2153 00006458 668B0B              <1> 	mov	cx, [ebx] 	; ascii & scan code
  2154                              <1> 				; (by kb_int)	
  2155 0000645B 6609C9              <1> 	or	cx, cx
  2156 0000645E 7507                <1> 	jnz	short getc2
  2157 00006460 20C0                <1> 	and 	al, al
  2158 00006462 7415                <1> 	jz	short getc_s
  2159                              <1> 	;xor	ax, ax
  2160                              <1> 	; 30/11/2021
  2161 00006464 31C0                <1> 	xor	eax, eax
  2162 00006466 C3                  <1> 	retn
  2163                              <1> getc2:	
  2164 00006467 20C0                <1> 	and	al, al
  2165 00006469 6689C8              <1> 	mov	ax, cx
  2166 0000646C 66B90000            <1> 	mov	cx, 0
  2167 00006470 7506                <1> 	jnz	short getc3
  2168                              <1> getc_sn:
  2169 00006472 66890B              <1> 	mov	[ebx], cx ; 0, reset
  2170 00006475 6639C8              <1> 	cmp	ax, cx  ; zf = 0
  2171                              <1> getc3:
  2172 00006478 C3                  <1> 	retn
  2173                              <1> getc_s:
  2174                              <1> 	; 12/11/2015
  2175                              <1> 	; 15/09/2015
  2176                              <1> 	; 01/07/2015
  2177                              <1> 	; 30/06/2015 (Retro UNIX 386 v1 - Beginning)
  2178                              <1> 	; 16/07/2013 - 14/02/2014 (Retro UNIX 8086 v1)
  2179                              <1> 	;
  2180                              <1> 	; tty  of the current process is not 
  2181                              <1> 	; current tty (ptty); so, current process only 
  2182                              <1> 	; can use keyboard input when its tty becomes 
  2183                              <1> 	; current tty (ptty).
  2184                              <1> 	; 'sleep' is for preventing an endless lock
  2185                              <1> 	; during this tty input request.
  2186                              <1> 	; (Because, the user is not looking at the video page
  2187                              <1> 	; of the process to undersand there is a keyboard
  2188                              <1> 	; input request.)
  2189                              <1> 	;
  2190                              <1> 	;((Modified registers: eAX, eBX, eCX, eDX, eSI, eDI))
  2191                              <1> 	;
  2192                              <1> 	; 05/10/2013
  2193                              <1> 	; ah = byte ptr [u.ttyn] ; (tty number)
  2194                              <1> 	;
  2195                              <1> 	; 10/10/2013
  2196                              <1> gcw0:
  2197 00006479 B10A                <1> 	mov	cl, 10 ; ch = 0
  2198                              <1> gcw1:	
  2199                              <1> 	; 12/11/2015
  2200 0000647B E837D8FFFF          <1> 	call intract ; jumps to 'sysexit' if [u.quit] = FFFFh
  2201                              <1> 	; 10/10/2013
  2202 00006480 E881EBFFFF          <1> 	call	idle
  2203 00006485 668B03              <1> 	mov	ax, [ebx] 	; ascii & scan code
  2204                              <1> 				; (by kb_int)
  2205 00006488 6609C0              <1> 	or	ax, ax
  2206                              <1> ;	jnz	short gcw3
  2207 0000648B 7519                <1> 	jnz	short gcw2 ; 15/09/2015
  2208                              <1> 	; 30/06/2015
  2209 0000648D FEC9                <1> 	dec	cl
  2210 0000648F 75EA                <1> 	jnz	short gcw1
  2211                              <1> 	;
  2212 00006491 8A25[0D750000]      <1> 	mov	ah, [u.ttyn] 	; 20/10/2013
  2213                              <1> ;	; 10/12/2013
  2214                              <1> ;	cmp 	ah, [ptty]
  2215                              <1> ;	jne	short gcw2
  2216                              <1> ;	; 14/02/2014
  2217                              <1> ;	cmp	byte [u.uno], 1
  2218                              <1> ;	jna	short gcw0		
  2219                              <1> ;gcw2:
  2220 00006497 E8FDEBFFFF          <1> 	call	sleep
  2221                              <1> 	;
  2222                              <1> 	; 20/09/2013
  2223 0000649C 8A25[0D750000]      <1> 	mov	ah, [u.ttyn]
  2224 000064A2 30C0                <1> 	xor 	al, al
  2225 000064A4 EB9C                <1> 	jmp	short getc_n
  2226                              <1> ;gcw3:
  2227                              <1> gcw2: 	; 15/09/2015
  2228                              <1> 	; 10/10/2013
  2229 000064A6 30C9                <1> 	xor	cl, cl
  2230 000064A8 EBC8                <1> 	jmp	short getc_sn
  2231                              <1> 
  2232                              <1> putc:	
  2233                              <1> 	; 05/12/2021 (Retro UNIX 386 v1.2)
  2234                              <1> 	; 13/08/2015
  2235                              <1> 	; 30/06/2015 (Retro UNIX 386 v1 - Beginning)
  2236                              <1> 	; 15/05/2013 - 27/07/2014 (Retro UNIX 8086 v1)
  2237                              <1> 	;
  2238                              <1> 	; Retro UNIX 8086 v1 modification !
  2239                              <1> 	; 
  2240                              <1> 	; 'putc' puts a character 
  2241                              <1> 	;	 onto requested (tty) video page or
  2242                              <1> 	;	 serial port
  2243                              <1> 	; INPUTS ->
  2244                              <1> 	;     AL = ascii code of the character
  2245                              <1> 	;     AH = video page (tty) number (0 to 7)
  2246                              <1> 	;			  (8 is COM1, 9 is COM2)	
  2247                              <1> 	; OUTPUTS ->
  2248                              <1> 	;    (If AL input is 1) ZF=1 -> 'empty buffer' (no chars)
  2249                              <1> 	;      			ZF=0 -> AX has (current) character
  2250                              <1> 	;     cf=0 and AH = 0 -> no error
  2251                              <1> 	;     cf=1 and AH > 0 -> error (only for COM1 and COM2)
  2252                              <1> 	; 
  2253                              <1> 	; Original UNIX V1 'putc': 
  2254                              <1> 	;     put a character at the end of character list
  2255                              <1> 	;
  2256                              <1> 	; ((Modified registers: eAX, eBX, eCX, eDX, eSI, eDI))
  2257                              <1> 	;
  2258 000064AA 80FC07              <1> 	cmp	ah, 7
  2259 000064AD 770A                <1>         ja      short sndc ; 05/12/2021
  2260                              <1> 	; 30/06/2015
  2261 000064AF 0FB6DC              <1> 	movzx	ebx, ah
  2262                              <1> 	; 13/08/2015
  2263 000064B2 B407                <1> 	mov	ah, 07h ; black background, light gray character color
  2264 000064B4 E901AFFFFF          <1> 	jmp	write_tty ; 'video.inc'
  2265                              <1> 
  2266                              <1> sndc:   ; <Send character>
  2267                              <1> 	;
  2268                              <1> 	; 12/01/2022
  2269                              <1> 	; 24/12/2021 (Retro UNIX 386 v1.2)
  2270                              <1> 	; 17/11/2015
  2271                              <1> 	; 16/11/2015
  2272                              <1> 	; 11/11/2015
  2273                              <1> 	; 10/11/2015
  2274                              <1> 	; 09/11/2015
  2275                              <1> 	; 08/11/2015
  2276                              <1> 	; 07/11/2015
  2277                              <1> 	; 06/11/2015 (serial4.asm, 'sendchr')	
  2278                              <1> 	; 29/10/2015
  2279                              <1> 	; 30/06/2015 (Retro UNIX 386 v1 - Beginning)
  2280                              <1> 	; 14/05/2013 - 28/07/2014 (Retro UNIX 8086 v1)
  2281                              <1> 	;
  2282                              <1> 	; Retro UNIX 8086 v1 feature only !
  2283                              <1> 	;
  2284                              <1> 	; ah = [u.ttyn]
  2285                              <1> 	;
  2286                              <1> 	; 30/06/2015
  2287 000064B9 80EC08              <1> 	sub	ah, 8 ; ; 0 = tty8 or 1 = tty9
  2288                              <1> 	; 07/11/2015
  2289 000064BC 0FB6DC              <1> 	movzx	ebx, ah ; serial port index (0 or 1)
  2290                              <1> sndc0:
  2291                              <1> 	; 07/11/2015
  2292 000064BF E82BECFFFF          <1> 	call	isintr ; quit (ctrl+break) check
  2293 000064C4 7405                <1> 	jz	short sndc1
  2294 000064C6 E8ECD7FFFF          <1> 	call	intract ; quit (ctrl+break) check
  2295                              <1> 	; CPU will jump to 'sysexit' if 'u.quit' = 0FFFFh (yes)
  2296                              <1> sndc1:
  2297                              <1> 	; 16/11/2015
  2298                              <1> 	;mov	cx, ax	; *** al = character (to be sent)
  2299                              <1> 	; 24/12/2021
  2300 000064CB 89C1                <1> 	mov	ecx, eax ; *** al = character (to be sent)
  2301                              <1> sndcx:
  2302 000064CD 8A83[2A700000]      <1> 	mov	al, [ebx+schar] ; last sent character
  2303 000064D3 8AA3[28700000]      <1> 	mov	ah, [ebx+rchar] ; last received character
  2304                              <1> 	;
  2305                              <1> 	; 17/11/2015
  2306                              <1> 	; check 'request for response' status
  2307 000064D9 80BB[24700000]00    <1> 	cmp	byte [ebx+req_resp], 0
  2308 000064E0 740A                <1> 	jz	short query
  2309                              <1> response:
  2310 000064E2 FE05[27700000]      <1> 	inc 	byte [comqr] ; query or response status
  2311 000064E8 B0FF                <1> 	mov	al, 0FFh	 
  2312 000064EA EB14                <1> 	jmp	short sndc3
  2313                              <1> query:
  2314 000064EC 08C0                <1> 	or 	al, al  ; 0 = query (also end of text)
  2315 000064EE 750E                <1> 	jnz 	short sndc2 ; normal character
  2316                              <1> 	;cmp 	ah, 0FFh     ; is it responded by terminal ?
  2317                              <1> 	;je	short sndc2  ; yes, already responded
  2318                              <1> 	; 16/11/2015
  2319                              <1> 	; query: request for response (again)
  2320 000064F0 8883[28700000]      <1> 	mov	[ebx+rchar], al ; 0 ; reset
  2321 000064F6 FE05[27700000]      <1> 	inc 	byte [comqr] ; query or response status
  2322 000064FC EB02                <1> 	jmp	short sndc3
  2323                              <1> sndc2:
  2324 000064FE 88C8                <1> 	mov	al, cl 	; *** character (to be sent)
  2325                              <1> sndc3:
  2326 00006500 8883[2A700000]      <1> 	mov	[ebx+schar], al ; current character (to be sent)
  2327 00006506 88D8                <1> 	mov	al, bl ; 0 or 1 (serial port index)
  2328                              <1> 	; 30/06/2015
  2329 00006508 E81FD1FFFF          <1> 	call	sp_status ; get serial port status
  2330                              <1> 	; AL = Line status, AH = Modem status
  2331                              <1> 	; 07/11/2015
  2332 0000650D A880                <1> 	test	al, 80h
  2333 0000650F 7504                <1> 	jnz	short sndc4
  2334 00006511 A820                <1> 	test	al, 20h	; Transmitter holding register empty ?
  2335 00006513 751A                <1> 	jnz	short sndc5
  2336                              <1> sndc4: 	; Check line status again
  2337                              <1> 	; 16/11/2015
  2338                              <1> 	;push	cx
  2339                              <1> 	; 24/12/2021
  2340 00006515 51                  <1> 	push	ecx
  2341                              <1> 	;mov	ecx, 6 ; 6*30 micro seconds (~5556 chars/second)
  2342                              <1> 	; 12/01/2022
  2343 00006516 31C9                <1> 	xor	ecx, ecx
  2344 00006518 B106                <1> 	mov	cl, 6
  2345 0000651A E8AAAFFFFF          <1> 	call	WAITF
  2346                              <1> 	;pop	cx
  2347                              <1> 	; 24/12/1021
  2348 0000651F 59                  <1> 	pop	ecx
  2349                              <1> 	;
  2350 00006520 88D8                <1> 	mov	al, bl ; 0 or 1 (serial port index)
  2351 00006522 E805D1FFFF          <1> 	call	sp_status ; get serial port status
  2352                              <1> 	; 16/11/2015
  2353                              <1> 	; 09/11/2015
  2354                              <1> 	; 08/11/2015
  2355 00006527 A880                <1> 	test	al, 80h	; time out error
  2356 00006529 7565                <1>         jnz     short sndc7
  2357 0000652B A820                <1> 	test	al, 20h	; Transmitter holding register empty ?
  2358 0000652D 7461                <1>         jz	short sndc7
  2359                              <1> sndc5:  
  2360 0000652F 8A83[2A700000]      <1> 	mov	al, [ebx+schar] ; character (to be sent)
  2361 00006535 66BAF803            <1> 	mov	dx, 3F8h   ; data port (COM2)
  2362 00006539 28DE                <1> 	sub	dh, bl
  2363 0000653B EE                  <1> 	out	dx, al	   ; send on serial port
  2364                              <1> 	; 10/11/2015
  2365                              <1> 	; delay for 3*30 (3*(15..80)) micro seconds
  2366                              <1> 	; (to improve text flow to the terminal)
  2367                              <1> 	; ('diskette.inc': 'WAITF')
  2368                              <1> 	; Uses port 61h, bit 4 to have CPU speed independent waiting.
  2369                              <1> 	; (refresh periods = 1 per 30 microseconds on most machines)
  2370                              <1> 	;push	cx
  2371                              <1> 	; 24/12/2021
  2372 0000653C 51                  <1> 	push	ecx
  2373                              <1> 	;mov	ecx, 6 ; 6*30 micro seconds (~5556 chars/second)
  2374                              <1> 	; 12/01/2022
  2375 0000653D 29C9                <1> 	sub	ecx, ecx
  2376 0000653F B106                <1> 	mov	cl, 6
  2377 00006541 E883AFFFFF          <1> 	call	WAITF
  2378                              <1> 	;pop	cx
  2379                              <1> 	; 24/12/1021
  2380 00006546 59                  <1> 	pop	ecx
  2381                              <1>     	;
  2382                              <1> 	; 07/11/2015
  2383 00006547 88D8                <1> 	mov	al, bl ; al = 0 (tty8) or 1 (tty9)
  2384                              <1> 	;
  2385 00006549 E8DED0FFFF          <1> 	call	sp_status ; get serial port status
  2386                              <1> 	; AL = Line status, AH = Modem status
  2387                              <1> 	;
  2388 0000654E E89CEBFFFF          <1> 	call	isintr ; quit (ctrl+break) check
  2389 00006553 7405                <1> 	jz	short sndc6
  2390 00006555 E85DD7FFFF          <1> 	call	intract ; quit (ctrl+break) check
  2391                              <1> 	; CPU will jump to 'sysexit' if 'u.quit' = 0FFFFh (yes)
  2392                              <1> sndc6:
  2393 0000655A 3C80                <1> 	cmp	al, 80h
  2394 0000655C 7332                <1> 	jnb	short sndc7		
  2395                              <1> 	;
  2396 0000655E 803D[27700000]01    <1> 	cmp	byte [comqr], 1 ; 'query or response' ?
  2397 00006565 7244                <1> 	jb	short sndc8 	; no, normal character
  2398 00006567 883D[27700000]      <1> 	mov 	byte [comqr], bh ; 0 ; reset
  2399                              <1> 	; 17/11/2015
  2400 0000656D E894EAFFFF          <1> 	call	idle
  2401                              <1> 	;
  2402 00006572 38BB[2A700000]      <1> 	cmp	[ebx+schar], bh ; 0 ; query ?
  2403                              <1>         ;ja	sndc2       ; response (will be followed by
  2404                              <1> 			    ; a normal character)
  2405                              <1> 	; 24/12/2021
  2406 00006578 7602                <1> 	jna	short sndc_10
  2407 0000657A EB82                <1> 	jmp	sndc2
  2408                              <1> sndc_10:
  2409                              <1> 	; Query request must be responded by the terminal
  2410                              <1> 	; before sending a normal character !
  2411 0000657C 53                  <1> 	push	ebx
  2412                              <1> 	;push	cx ; *** cl = character (to be sent)
  2413                              <1> 	; 24/12/2021
  2414 0000657D 51                  <1> 	push	ecx ; *** cl = character (to be sent)
  2415 0000657E 8A25[0D750000]      <1> 	mov	ah, [u.ttyn]
  2416 00006584 E810EBFFFF          <1> 	call	sleep ; this process will be awakened by
  2417                              <1> 		      ; received data available interrupt
  2418                              <1> 	;pop	cx ; *** cl = character (to be sent)
  2419                              <1> 	; 24/12/2021
  2420 00006589 59                  <1> 	pop	ecx ; *** cl = character (to be sent) 
  2421 0000658A 5B                  <1> 	pop	ebx
  2422 0000658B E93DFFFFFF          <1>         jmp	sndcx
  2423                              <1> sndc7:
  2424                              <1> 	 ; 16/11/2015
  2425 00006590 803D[27700000]01    <1> 	cmp	byte [comqr], 1 ; 'query or response' ?
  2426 00006597 7213                <1> 	jb	short sndc9 	; no
  2427                              <1> 	;
  2428 00006599 88BB[28700000]      <1> 	mov	[ebx+rchar], bh ; 0 ; reset
  2429 0000659F 88BB[2A700000]      <1> 	mov	[ebx+schar], bh ; 0 ; reset
  2430                              <1> 	;
  2431 000065A5 883D[27700000]      <1> 	mov	byte [comqr], bh ; 0 ; reset  
  2432                              <1> sndc8:
  2433 000065AB F5                  <1> 	cmc  ; jnc -> jc, jb -> jnb
  2434                              <1> sndc9:
  2435                              <1> 	; AL = Line status, AH = Modem status
  2436 000065AC C3                  <1> 	retn
  2437                              <1> 
  2438                              <1> get_cpos:
  2439                              <1> 	; 29/06/2015 (Retro UNIX 386 v1)
  2440                              <1> 	; 04/12/2013 (Retro UNIX 8086 v1 - 'sysgtty')
  2441                              <1> 	;
  2442                              <1> 	; INPUT -> bl = video page number
  2443                              <1> 	; RETURN -> dx = cursor position
  2444                              <1> 
  2445 000065AD 53                  <1> 	push	ebx
  2446 000065AE 83E30F              <1> 	and	ebx, 0Fh ; 07h ; tty0 to tty7
  2447 000065B1 D0E3                <1> 	shl	bl, 1
  2448 000065B3 81C3[D66F0000]      <1> 	add	ebx, cursor_posn
  2449 000065B9 668B13              <1> 	mov	dx, [ebx]
  2450 000065BC 5B                  <1> 	pop	ebx
  2451 000065BD C3                  <1> 	retn
  2452                              <1> 
  2453                              <1> read_ac_current:
  2454                              <1> 	; 29/06/2015 (Retro UNIX 386 v1)
  2455                              <1> 	; 04/12/2013 (Retro UNIX 8086 v1 - 'sysgtty')
  2456                              <1> 	;
  2457                              <1> 	; INPUT -> bl = video page number
  2458                              <1> 	; RETURN -> ax = character (al) and attribute (ah)
  2459                              <1> 
  2460 000065BE E875AFFFFF          <1> 	call 	find_position ; 'video.inc'
  2461                              <1> 	; dx = status port
  2462                              <1> 	; esi = cursor location/address
  2463 000065C3 81C600800B00        <1> 	add	esi, 0B8000h	; 30/08/2014 (Retro UNIX 386 v1)
  2464 000065C9 668B06              <1> 	mov 	ax, [esi]	; get the character and attribute
  2465 000065CC C3                  <1> 	retn
  2466                              <1> 
  2467                              <1> syssleep:
  2468                              <1> 	; 29/06/2015 - (Retro UNIX 386 v1)
  2469                              <1> 	; 11/06/2014 - (Retro UNIX 8086 v1)
  2470                              <1> 	;
  2471                              <1> 	; Retro UNIX 8086 v1 feature only
  2472                              <1> 	; (INPUT -> none)
  2473                              <1> 	;
  2474 000065CD 0FB61D[29750000]    <1> 	movzx	ebx, byte [u.uno] ; process number
  2475 000065D4 8AA3[F3700000]      <1> 	mov	ah, [ebx+p.ttyc-1] ; current/console tty
  2476 000065DA E8BAEAFFFF          <1> 	call	sleep
  2477 000065DF E9E6D5FFFF          <1> 	jmp	sysret
  2478                              <1> 
  2479                              <1> vp_clr:
  2480                              <1> 	; Reset/Clear Video Page
  2481                              <1> 	;
  2482                              <1> 	; 05/12/2021 - (Retro UNIX 386 v1.2)
  2483                              <1> 	; 30/06/2015 - (Retro UNIX 386 v1)
  2484                              <1> 	; 21/05/2013 - 30/10/2013(Retro UNIX 8086 v1) (U0.ASM)
  2485                              <1> 	;
  2486                              <1> 	; Retro UNIX 8086 v1 feature only !
  2487                              <1> 	;
  2488                              <1> 	; INPUTS -> 
  2489                              <1> 	;   BL = video page number	 
  2490                              <1> 	;
  2491                              <1> 	; OUTPUT ->
  2492                              <1> 	;   none
  2493                              <1> 	; ((Modified registers: eAX, BH, eCX, eDX, eSI, eDI))
  2494                              <1> 	;
  2495                              <1> 	; 04/12/2013
  2496 000065E4 28C0                <1> 	sub	al, al
  2497                              <1> 	; al = 0 (clear video page)
  2498                              <1> 	; bl = video page
  2499 000065E6 B407                <1> 	mov	ah, 07h
  2500                              <1> 	; ah = 7 (attribute/color)
  2501                              <1> 	;xor 	cx, cx ; 0, left upper column (cl) & row (cl)
  2502                              <1> 	; 05/12/2021
  2503 000065E8 31C9                <1> 	xor	ecx, ecx
  2504 000065EA 66BA4F18            <1> 	mov	dx, 184Fh ; right lower column & row (dl=24, dh=79)
  2505 000065EE E86FAFFFFF          <1> 	call	scroll_up
  2506                              <1> 	; bl = video page
  2507                              <1> 	;xor	dx, dx ; 0 (cursor position) 
  2508                              <1> 	; 05/12/2021
  2509 000065F3 31D2                <1> 	xor	edx, edx
  2510 000065F5 E9E2AEFFFF          <1> 	jmp 	set_cpos
  2511                              <1> 
  2512                              <1> sysmsg:
  2513                              <1> 	; 11/12/2021
  2514                              <1> 	; 04/12/2021 (Retro UNIX 386 v1.2)
  2515                              <1> 	; 11/11/2015
  2516                              <1> 	; 01/07/2015 - (Retro UNIX 386 v1 feature only!)
  2517                              <1> 	; Print user-application message on user's console tty
  2518                              <1> 	;
  2519                              <1> 	; Input -> EBX = Message address
  2520                              <1> 	;	   ECX = Message length (max. 255)
  2521                              <1> 	;	   DL = Color (IBM PC Rombios color attributes)
  2522                              <1> 	;
  2523 000065FA 81F9FF000000        <1> 	cmp	ecx, MAX_MSG_LEN ; 255
  2524                              <1> 	;ja	sysret ; nothing to do with big message size
  2525 00006600 7779                <1> 	ja	short sysmsg8 ; 11/12/2021
  2526 00006602 08C9                <1> 	or	cl, cl
  2527                              <1> 	;jz	sysret
  2528 00006604 7475                <1> 	jz	short sysmsg8 ; 11/12/2021
  2529 00006606 20D2                <1> 	and	dl, dl
  2530 00006608 7502                <1> 	jnz	short sysmsg0
  2531 0000660A B207                <1> 	mov	dl, 07h ; default color
  2532                              <1> 		; (black background, light gray character)
  2533                              <1> sysmsg0:
  2534 0000660C 891D[FC740000]      <1> 	mov	[u.base], ebx
  2535 00006612 8815[E76F0000]      <1> 	mov	[ccolor], dl ; color attributes
  2536 00006618 89E5                <1> 	mov	ebp, esp
  2537 0000661A 31DB                <1> 	xor	ebx, ebx ; 0
  2538 0000661C 891D[04750000]      <1> 	mov	[u.nread], ebx ; 0
  2539                              <1> 	;
  2540 00006622 381D[4A750000]      <1> 	cmp	[u.kcall], bl ; 0
  2541 00006628 7772                <1> 	ja	short sysmsgk ; Temporary (01/07/2015)
  2542                              <1> 	;
  2543 0000662A 890D[00750000]      <1> 	mov	[u.count], ecx
  2544 00006630 41                  <1> 	inc	ecx ; + 00h ; ASCIIZ ; 04/12/2021
  2545                              <1> 	; 04/12/2021
  2546                              <1> 	; (dword alignment for esp)
  2547 00006631 F6C103              <1> 	test	cl, 3
  2548 00006634 7404                <1> 	jz	short sysmsg_7
  2549 00006636 80C903              <1> 	or	cl, 3
  2550 00006639 41                  <1> 	inc	ecx
  2551                              <1> sysmsg_7:
  2552 0000663A 29CC                <1> 	sub	esp, ecx
  2553 0000663C 89E7                <1> 	mov	edi, esp
  2554 0000663E 89E6                <1> 	mov	esi, esp
  2555 00006640 66891D[44750000]    <1> 	mov	[u.pcount], bx ; reset page (phy. addr.) counter
  2556                              <1> 	; 11/11/2015 
  2557 00006647 8A25[0E750000]      <1> 	mov 	ah, [u.ttyp] ; recent open tty
  2558                              <1> 	; 0 = none
  2559 0000664D FECC                <1> 	dec	ah
  2560 0000664F 790C                <1> 	jns	short sysmsg1 
  2561 00006651 8A1D[29750000]      <1> 	mov	bl, [u.uno] ; process number	
  2562 00006657 8AA3[F3700000]      <1> 	mov	ah, [ebx+p.ttyc-1] ; user's (process's) console tty
  2563                              <1> sysmsg1:
  2564 0000665D 8825[0D750000]      <1> 	mov	[u.ttyn], ah
  2565                              <1> sysmsg2:
  2566 00006663 E864F5FFFF          <1> 	call	cpass
  2567 00006668 7416                <1> 	jz	short sysmsg5
  2568 0000666A AA                  <1> 	stosb
  2569 0000666B 20C0                <1> 	and	al, al
  2570 0000666D 75F4                <1> 	jnz	short sysmsg2
  2571                              <1> sysmsg3:
  2572 0000666F 80FC07              <1> 	cmp	ah, 7 ; tty number
  2573 00006672 7711                <1> 	ja	short sysmsg6 ; serial port
  2574 00006674 E83E000000          <1> 	call	print_cmsg
  2575                              <1> sysmsg4:
  2576 00006679 89EC                <1> 	mov	esp, ebp
  2577                              <1> sysmsg8: ; 11/12/2021	
  2578 0000667B E94AD5FFFF          <1> 	jmp	sysret
  2579                              <1> sysmsg5:
  2580 00006680 C60700              <1> 	mov	byte [edi], 0
  2581 00006683 EBEA                <1> 	jmp	short sysmsg3
  2582                              <1> sysmsg6:
  2583 00006685 8A06                <1> 	mov	al, [esi]
  2584 00006687 E82DFEFFFF          <1> 	call	sndc
  2585 0000668C 72EB                <1> 	jc	short sysmsg4
  2586 0000668E 803E00              <1> 	cmp	byte [esi], 0  ; 0 is stop character
  2587 00006691 76E6                <1> 	jna	short sysmsg4
  2588 00006693 46                  <1> 	inc 	esi
  2589 00006694 8A25[0D750000]      <1> 	mov	ah, [u.ttyn]
  2590 0000669A EBE9                <1> 	jmp	short sysmsg6
  2591                              <1> 
  2592                              <1> sysmsgk: ; Temporary (01/07/2015)
  2593                              <1> 	; The message has been sent by Kernel (ASCIIZ string)
  2594                              <1> 	; (ECX -character count- will not be considered)
  2595 0000669C 8B35[FC740000]      <1> 	mov	esi, [u.base]
  2596 000066A2 8A25[E66F0000]      <1> 	mov	ah, [ptty] ; present/current screen (video page)
  2597 000066A8 8825[0D750000]      <1> 	mov	[u.ttyn], ah
  2598 000066AE C605[4A750000]00    <1> 	mov	byte [u.kcall], 0
  2599 000066B5 EBB8                <1> 	jmp	short sysmsg3
  2600                              <1> 	
  2601                              <1> print_cmsg: 
  2602                              <1> 	; 01/07/2015 (retro UNIX 386 v1 feature only !)
  2603                              <1> 	;
  2604                              <1> 	; print message (on user's console tty) 
  2605                              <1> 	;	with requested color
  2606                              <1> 	;
  2607                              <1> 	; INPUTS:
  2608                              <1> 	;	esi = message address
  2609                              <1> 	;	[u.ttyn] = tty number (0 to 7)
  2610                              <1> 	;	[ccolor] = color attributes (IBM PC BIOS colors)
  2611                              <1> 	;
  2612 000066B7 AC                  <1> 	lodsb
  2613                              <1> pcmsg1:
  2614 000066B8 56                  <1> 	push 	esi
  2615 000066B9 0FB61D[0D750000]    <1>         movzx   ebx, byte [u.ttyn]
  2616 000066C0 8A25[E76F0000]      <1> 	mov	ah, [ccolor]
  2617 000066C6 E8EFACFFFF          <1> 	call 	write_tty
  2618 000066CB 5E                  <1> 	pop	esi
  2619 000066CC AC                  <1> 	lodsb
  2620 000066CD 20C0                <1> 	and 	al, al  ; 0
  2621 000066CF 75E7                <1> 	jnz 	short pcmsg1
  2622 000066D1 C3                  <1> 	retn
  2623                              <1> 
  2624                              <1> sysgeterr:
  2625                              <1> 	; 09/12/2015
  2626                              <1> 	; 21/09/2015 - (Retro UNIX 386 v1 feature only!)
  2627                              <1> 	; Get last error number or page fault count
  2628                              <1> 	; (for debugging)
  2629                              <1> 	;
  2630                              <1> 	; Input -> EBX = return type
  2631                              <1> 	;	   0 = last error code (which is in 'u.error')	
  2632                              <1> 	;	   FFFFFFFFh = page fault count for running process
  2633                              <1> 	;	   FFFFFFFEh = total page fault count
  2634                              <1> 	;	   1 .. FFFFFFFDh = undefined 
  2635                              <1> 	;
  2636                              <1> 	; Output -> EAX = last error number or page fault count
  2637                              <1> 	;	   (depending on EBX input)
  2638                              <1> 	; 	
  2639 000066D2 21DB                <1> 	and 	ebx, ebx
  2640 000066D4 750B                <1> 	jnz	short glerr_2
  2641                              <1> glerr_0:
  2642 000066D6 A1[4C750000]        <1> 	mov	eax, [u.error]
  2643                              <1> glerr_1:
  2644 000066DB A3[D8740000]        <1> 	mov	[u.r0], eax
  2645 000066E0 C3                  <1>  	retn
  2646                              <1> glerr_2:
  2647 000066E1 43                  <1> 	inc	ebx ; FFFFFFFFh -> 0, FFFFFFFEh -> FFFFFFFFh
  2648 000066E2 74FD                <1> 	jz	short glerr_2 ; page fault count for process
  2649 000066E4 43                  <1> 	inc	ebx ; FFFFFFFFh -> 0	
  2650 000066E5 75EF                <1> 	jnz	short glerr_0
  2651 000066E7 A1[70700000]        <1> 	mov	eax, [PF_Count] ; total page fault count
  2652 000066EC EBED                <1>         jmp     short glerr_1
  2653                              <1> glerr_3:
  2654 000066EE A1[50750000]        <1> 	mov 	eax, [u.pfcount]
  2655 000066F3 EBE6                <1> 	jmp	short glerr_1
  2079                                  
  2080                                  ; 07/03/2015
  2081                                  ; Temporary Code
  2082                                  display_disks:
  2083 000066F5 803D[AE6A0000]00        	cmp 	byte [fd0_type], 0
  2084 000066FC 7605                    	jna 	short ddsks1
  2085 000066FE E87D000000              	call	pdskm
  2086                                  ddsks1:
  2087 00006703 803D[AF6A0000]00        	cmp	byte [fd1_type], 0
  2088 0000670A 760C                    	jna	short ddsks2
  2089 0000670C C605[9F6C0000]31        	mov	byte [dskx], '1'
  2090 00006713 E868000000              	call	pdskm
  2091                                  ddsks2:
  2092 00006718 803D[B06A0000]00        	cmp	byte [hd0_type], 0
  2093 0000671F 7654                    	jna	short ddsk6
  2094 00006721 66C705[9D6C0000]68-     	mov	word [dsktype], 'hd'
  2094 00006729 64                 
  2095 0000672A C605[9F6C0000]30        	mov	byte [dskx], '0'
  2096 00006731 E84A000000              	call	pdskm
  2097                                  ddsks3:
  2098 00006736 803D[B16A0000]00        	cmp	byte [hd1_type], 0
  2099 0000673D 7636                    	jna	short ddsk6
  2100 0000673F C605[9F6C0000]31        	mov	byte [dskx], '1'
  2101 00006746 E835000000              	call	pdskm
  2102                                  ddsks4:
  2103 0000674B 803D[B26A0000]00        	cmp	byte [hd2_type], 0
  2104 00006752 7621                    	jna	short ddsk6
  2105 00006754 C605[9F6C0000]32        	mov	byte [dskx], '2'
  2106 0000675B E820000000              	call	pdskm
  2107                                  ddsks5:
  2108 00006760 803D[B36A0000]00        	cmp	byte [hd3_type], 0
  2109 00006767 760C                    	jna	short ddsk6
  2110 00006769 C605[9F6C0000]33        	mov	byte [dskx], '3'
  2111 00006770 E80B000000              	call	pdskm
  2112                                  ddsk6:
  2113 00006775 BE[AE6C0000]            	mov	esi, nextline
  2114 0000677A E806000000              	call	pdskml
  2115                                  pdskm_ok:
  2116 0000677F C3                      	retn
  2117                                  pdskm:
  2118 00006780 BE[9B6C0000]            	mov	esi, dsk_ready_msg
  2119                                  pdskml:	
  2120 00006785 AC                      	lodsb
  2121 00006786 08C0                    	or	al, al
  2122 00006788 74F5                    	jz	short pdskm_ok
  2123 0000678A 56                      	push	esi
  2124 0000678B 31DB                    	xor	ebx, ebx ; 0
  2125                                  			; Video page 0 (bl=0)
  2126 0000678D B407                    	mov	ah, 07h ; Black background, 
  2127                                  			; light gray forecolor
  2128 0000678F E826ACFFFF              	call	write_tty
  2129 00006794 5E                      	pop	esi
  2130 00006795 EBEE                    	jmp	short pdskml
  2131                                  
  2132 00006797 90<rep 9h>              align 16
  2133                                  
  2134                                  gdt:	; Global Descriptor Table
  2135                                  	; (30/07/2015, conforming cs)
  2136                                  	; (26/03/2015)
  2137                                  	; (24/03/2015, tss)
  2138                                  	; (19/03/2015)
  2139                                  	; (29/12/2013)
  2140                                  	;
  2141 000067A0 0000000000000000        	dw 0, 0, 0, 0	; NULL descriptor
  2142                                  	; 18/08/2014
  2143                                  			; 8h kernel code segment, base = 00000000h		
  2144 000067A8 FFFF0000009ACF00        	dw 0FFFFh, 0, 9A00h, 00CFh	; KCODE
  2145                                  			; 10h kernel data segment, base = 00000000h	
  2146 000067B0 FFFF00000092CF00        	dw 0FFFFh, 0, 9200h, 00CFh	; KDATA
  2147                                  			; 1Bh user code segment, base address = 400000h ; CORE
  2148 000067B8 FFFB000040FACF00        	dw 0FBFFh, 0, 0FA40h, 00CFh	; UCODE 
  2149                                  			; 23h user data segment, base address = 400000h ; CORE
  2150 000067C0 FFFB000040F2CF00        	dw 0FBFFh, 0, 0F240h, 00CFh	; UDATA
  2151                                  			; Task State Segment
  2152 000067C8 6700                    	dw 0067h ; Limit = 103 ; (104-1, tss size = 104 byte, 
  2153                                  			       ;  no IO permission in ring 3)
  2154                                  gdt_tss0:
  2155 000067CA 0000                    	dw 0  ; TSS base address, bits 0-15 
  2156                                  gdt_tss1:
  2157 000067CC 00                      	db 0  ; TSS base address, bits 16-23 
  2158                                  	      		; 49h	
  2159 000067CD E9                      	db 11101001b ; E9h => P=1/DPL=11/0/1/0/B/1 --> B = Task is busy (1)
  2160 000067CE 00                      	db 0 ; G/0/0/AVL/LIMIT=0000 ; (Limit bits 16-19 = 0000) (G=0, 1 byte)
  2161                                  gdt_tss2:
  2162 000067CF 00                      	db 0  ; TSS base address, bits 24-31 
  2163                                  
  2164                                  gdt_end:
  2165                                  	;; 9Ah = 1001 1010b (GDT byte 5) P=1/DPL=00/1/TYPE=1010, 
  2166                                  					;; Type= 1 (code)/C=0/R=1/A=0
  2167                                  		; P= Present, DPL=0=ring 0,  1= user (0= system)
  2168                                  		; 1= Code C= non-Conforming, R= Readable, A = Accessed
  2169                                  
  2170                                  	;; 92h = 1001 0010b (GDT byte 5) P=1/DPL=00/1/TYPE=1010, 
  2171                                  					;; Type= 0 (data)/E=0/W=1/A=0
  2172                                  		; P= Present, DPL=0=ring 0,  1= user (0= system)
  2173                                  		; 0= Data E= Expansion direction (1= down, 0= up)
  2174                                  		; W= Writeable, A= Accessed
  2175                                  	
  2176                                  	;; FAh = 1111 1010b (GDT byte 5) P=1/DPL=11/1/TYPE=1010, 
  2177                                  					;; Type= 1 (code)/C=0/R=1/A=0
  2178                                  		; P= Present, DPL=3=ring 3,  1= user (0= system)
  2179                                  		; 1= Code C= non-Conforming, R= Readable, A = Accessed
  2180                                  
  2181                                  	;; F2h = 1111 0010b (GDT byte 5) P=1/DPL=11/1/TYPE=0010, 
  2182                                  					;; Type= 0 (data)/E=0/W=1/A=0
  2183                                  		; P= Present, DPL=3=ring 3,  1= user (0= system)
  2184                                  		; 0= Data E= Expansion direction (1= down, 0= up)
  2185                                  	
  2186                                  	;; CFh = 1100 1111b (GDT byte 6) G=1/B=1/0/AVL=0, Limit=1111b (3)
  2187                                  
  2188                                  		;; Limit = FFFFFh (=> FFFFFh+1= 100000h) // bits 0-15, 48-51 //
  2189                                  		;	 = 100000h * 1000h (G=1) = 4GB
  2190                                  		;; Limit = FFBFFh (=> FFBFFh+1= FFC00h) // bits 0-15, 48-51 //
  2191                                  		;	 = FFC00h * 1000h (G=1) = 4GB - 4MB
  2192                                  		; G= Granularity (1= 4KB), B= Big (32 bit), 
  2193                                  		; AVL= Available to programmers	
  2194                                  
  2195                                  gdtd:
  2196 000067D0 2F00                            dw gdt_end - gdt - 1    ; Limit (size)
  2197 000067D2 [A0670000]                      dd gdt			; Address of the GDT
  2198                                  
  2199                                  	; 20/08/2014
  2200                                  idtd:
  2201 000067D6 FF01                            dw idt_end - idt - 1    ; Limit (size)
  2202 000067D8 [506D0000]                      dd idt			; Address of the IDT
  2203                                  
  2204                                  Align 4
  2205                                  
  2206                                  	; 21/08/2014
  2207                                  ilist:
  2208                                  	;times 	32 dd cpu_except ; INT 0 to INT 1Fh
  2209                                  	;
  2210                                  	; Exception list
  2211                                  	; 25/08/2014	
  2212 000067DC [1F080000]              	dd	exc0	; 0h,  Divide-by-zero Error
  2213 000067E0 [26080000]              	dd	exc1	
  2214 000067E4 [2D080000]              	dd 	exc2	
  2215 000067E8 [34080000]              	dd	exc3	
  2216 000067EC [38080000]              	dd	exc4	
  2217 000067F0 [3C080000]              	dd	exc5	
  2218 000067F4 [40080000]              	dd 	exc6	; 06h,  Invalid Opcode
  2219 000067F8 [44080000]              	dd	exc7	
  2220 000067FC [48080000]              	dd	exc8	
  2221 00006800 [4C080000]              	dd	exc9	
  2222 00006804 [50080000]              	dd 	exc10	
  2223 00006808 [54080000]              	dd	exc11
  2224 0000680C [58080000]              	dd	exc12
  2225 00006810 [5C080000]              	dd	exc13	; 0Dh, General Protection Fault
  2226 00006814 [60080000]              	dd 	exc14	; 0Eh, Page Fault
  2227 00006818 [64080000]              	dd	exc15
  2228 0000681C [68080000]              	dd	exc16
  2229 00006820 [6C080000]              	dd	exc17
  2230 00006824 [70080000]              	dd 	exc18
  2231 00006828 [74080000]              	dd	exc19
  2232 0000682C [78080000]              	dd 	exc20
  2233 00006830 [7C080000]              	dd	exc21
  2234 00006834 [80080000]              	dd	exc22
  2235 00006838 [84080000]              	dd	exc23
  2236 0000683C [88080000]              	dd 	exc24
  2237 00006840 [8C080000]              	dd	exc25
  2238 00006844 [90080000]              	dd	exc26
  2239 00006848 [94080000]              	dd	exc27
  2240 0000684C [98080000]              	dd 	exc28
  2241 00006850 [9C080000]              	dd	exc29
  2242 00006854 [A0080000]              	dd 	exc30
  2243 00006858 [A4080000]              	dd	exc31
  2244                                  	; Interrupt list
  2245 0000685C [5E060000]              	dd	timer_int	; INT 20h
  2246                                  		;dd	irq0	
  2247 00006860 [560B0000]              	dd	keyb_int	; 27/08/2014
  2248                                  		;dd	irq1
  2249 00006864 [79070000]              	dd	irq2
  2250                                  		; COM2 int
  2251 00006868 [7D070000]              	dd	irq3
  2252                                  		; COM1 int
  2253 0000686C [88070000]              	dd	irq4
  2254 00006870 [93070000]              	dd	irq5
  2255                                  ;DISKETTE_INT: ;06/02/2015
  2256 00006874 [F9250000]              	dd	fdc_int		; 16/02/2015, IRQ 6 handler	
  2257                                  		;dd	irq6
  2258                                  ; Default IRQ 7 handler against spurious IRQs (from master PIC)
  2259                                  ; 25/02/2015 (source: http://wiki.osdev.org/8259_PIC)
  2260 00006878 [070B0000]              	dd	default_irq7	; 25/02/2015
  2261                                  		;dd	irq7
  2262                                  ; Real Time Clock Interrupt
  2263 0000687C [AD090000]              	dd	rtc_int		; 23/02/2015, IRQ 8 handler
  2264                                  		;dd	irq8	; INT 28h
  2265 00006880 [A3070000]              	dd	irq9
  2266 00006884 [A7070000]              	dd	irq10
  2267 00006888 [AB070000]              	dd	irq11
  2268 0000688C [AF070000]              	dd	irq12
  2269 00006890 [B3070000]              	dd	irq13
  2270                                  ;HDISK_INT1:  ;06/02/2015 	
  2271 00006894 [1F2E0000]              	dd	hdc1_int 	; 21/02/2015, IRQ 14 handler		
  2272                                  		;dd	irq14
  2273                                  ;HDISK_INT2:  ;06/02/2015
  2274 00006898 [422E0000]              	dd	hdc2_int 	; 21/02/2015, IRQ 15 handler		
  2275                                  		;dd	irq15	; INT 2Fh
  2276                                  		; 14/08/2015
  2277 0000689C [A63A0000]              	dd	sysent		; INT 30h (system calls)
  2278                                  	
  2279                                  	;dd	ignore_int
  2280 000068A0 00000000                	dd	0
  2281                                  
  2282                                  ;;;
  2283                                  ;;; 11/03/2015
  2284                                  %include 'kybdata.s'	; KEYBOARD (BIOS) DATA
  2285                              <1> ; Retro UNIX 386 v1 Kernel - KYBDATA.INC
  2286                              <1> ; Last Modification: 11/03/2015
  2287                              <1> ;		 (Data Section for 'KEYBOARD.INC')	
  2288                              <1> ;
  2289                              <1> ; ///////// KEYBOARD DATA ///////////////
  2290                              <1> 
  2291                              <1> ; 05/12/2014
  2292                              <1> ; 04/12/2014 (derived from pc-xt-286 bios source code -1986-) 
  2293                              <1> ; 03/06/86  KEYBOARD BIOS
  2294                              <1> 
  2295                              <1> ;---------------------------------------------------------------------------------
  2296                              <1> ;	KEY IDENTIFICATION SCAN TABLES
  2297                              <1> ;---------------------------------------------------------------------------------
  2298                              <1> 
  2299                              <1> ;-----	TABLES FOR ALT CASE ------------
  2300                              <1> ;-----	ALT-INPUT-TABLE 
  2301 000068A4 524F50514B          <1> K30:	db	82,79,80,81,75
  2302 000068A9 4C4D474849          <1> 	db	76,77,71,72,73		; 10 NUMBER ON KEYPAD
  2303                              <1> ;-----	SUPER-SHIFT-TABLE 
  2304 000068AE 101112131415        <1> 	db	16,17,18,19,20,21	; A-Z TYPEWRITER CHARS
  2305 000068B4 161718191E1F        <1> 	db	22,23,24,25,30,31
  2306 000068BA 202122232425        <1> 	db	32,33,34,35,36,37
  2307 000068C0 262C2D2E2F30        <1> 	db	38,44,45,46,47,48
  2308 000068C6 3132                <1> 	db	49,50
  2309                              <1> 
  2310                              <1> ;-----	TABLE OF SHIFT KEYS AND MASK VALUES
  2311                              <1> ;-----	KEY_TABLE 
  2312 000068C8 52                  <1> _K6:    db      INS_KEY                 ; INSERT KEY
  2313 000068C9 3A4546381D          <1> 	db	CAPS_KEY,NUM_KEY,SCROLL_KEY,ALT_KEY,CTL_KEY
  2314 000068CE 2A36                <1>         db      LEFT_KEY,RIGHT_KEY
  2315                              <1> _K6L    equ     $-_K6
  2316                              <1> 
  2317                              <1> ;-----	MASK_TABLE
  2318 000068D0 80                  <1> _K7:    db      INS_SHIFT               ; INSERT MODE SHIFT
  2319 000068D1 4020100804          <1> 	db	CAPS_SHIFT,NUM_SHIFT,SCROLL_SHIFT,ALT_SHIFT,CTL_SHIFT
  2320 000068D6 0201                <1> 	db	LEFT_SHIFT,RIGHT_SHIFT
  2321                              <1> 
  2322                              <1> ;-----	TABLES FOR CTRL CASE		;---- CHARACTERS ------
  2323 000068D8 1BFF00FFFFFF        <1> _K8:	db	27,-1,0,-1,-1,-1	; Esc, 1, 2, 3, 4, 5
  2324 000068DE 1EFFFFFFFF1F        <1> 	db 	30,-1,-1,-1,-1,31	; 6, 7, 8, 9, 0, -
  2325 000068E4 FF7FFF111705        <1> 	db	-1,127,-1,17,23,5	; =, Bksp, Tab, Q, W, E
  2326 000068EA 12141915090F        <1> 	db	18,20,25,21,9,15	; R, T, Y, U, I, O
  2327 000068F0 101B1D0AFF01        <1> 	db	16,27,29,10,-1,1	; P, [, ], Enter, Ctrl, A
  2328 000068F6 13040607080A        <1> 	db	19,4,6,7,8,10		; S, D, F, G, H, J
  2329 000068FC 0B0CFFFFFFFF        <1> 	db	11,12,-1,-1,-1,-1	; K, L, :, ', `, LShift
  2330 00006902 1C1A18031602        <1> 	db	28,26,24,3,22,2		; Bkslash, Z, X, C, V, B
  2331 00006908 0E0DFFFFFFFF        <1> 	db	14,13,-1,-1,-1,-1	; N, M, ,, ., /, RShift
  2332 0000690E 96FF20FF            <1> 	db	150,-1,' ',-1		; *, ALT, Spc, CL
  2333                              <1> 	;				;----- FUNCTIONS ------		
  2334 00006912 5E5F60616263        <1> 	db 	94,95,96,97,98,99	; F1 - F6
  2335 00006918 64656667FFFF        <1> 	db	100,101,102,103,-1,-1	; F7 - F10, NL, SL
  2336 0000691E 778D848E738F        <1> 	db	119,141,132,142,115,143	; Home, Up, PgUp, -, Left, Pad5
  2337 00006924 749075917692        <1> 	db 	116,144,117,145,118,146 ; Right, +, End, Down, PgDn, Ins
  2338 0000692A 93FFFFFF898A        <1> 	db	147,-1,-1,-1,137,138	; Del, SysReq, Undef, WT, F11, F12
  2339                              <1> 
  2340                              <1> ;-----	TABLES FOR LOWER CASE ----------
  2341 00006930 1B3132333435363738- <1> K10:	db 	27,'1234567890-=',8,9
  2341 00006939 39302D3D0809        <1>
  2342 0000693F 71776572747975696F- <1> 	db 	'qwertyuiop[]',13,-1,'asdfghjkl;',39
  2342 00006948 705B5D0DFF61736466- <1>
  2342 00006951 67686A6B6C3B27      <1>
  2343 00006958 60FF5C7A786376626E- <1> 	db	96,-1,92,'zxcvbnm,./',-1,'*',-1,' ',-1
  2343 00006961 6D2C2E2FFF2AFF20FF  <1>
  2344                              <1> ;-----	LC TABLE SCAN
  2345 0000696A 3B3C3D3E3F          <1> 	db	59,60,61,62,63		; BASE STATE OF F1 - F10
  2346 0000696F 4041424344          <1> 	db	64,65,66,67,68
  2347 00006974 FFFF                <1> 	db	-1,-1			; NL, SL
  2348                              <1> 
  2349                              <1> ;-----	KEYPAD TABLE
  2350 00006976 474849FF4BFF        <1> K15:	db	71,72,73,-1,75,-1	; BASE STATE OF KEYPAD KEYS
  2351 0000697C 4DFF4F50515253      <1> 	db	77,-1,79,80,81,82,83
  2352 00006983 FFFF5C8586          <1> 	db	-1,-1,92,133,134	; SysRq, Undef, WT, F11, F12
  2353                              <1> 
  2354                              <1> ;-----	TABLES FOR UPPER CASE ----------
  2355 00006988 1B21402324255E262A- <1> K11:	db 	27,'!@#$%',94,'&*()_+',8,0
  2355 00006991 28295F2B0800        <1>
  2356 00006997 51574552545955494F- <1> 	db 	'QWERTYUIOP{}',13,-1,'ASDFGHJKL:"'
  2356 000069A0 507B7D0DFF41534446- <1>
  2356 000069A9 47484A4B4C3A22      <1>
  2357 000069B0 7EFF7C5A584356424E- <1> 	db	126,-1,'|ZXCVBNM<>?',-1,'*',-1,' ',-1
  2357 000069B9 4D3C3E3FFF2AFF20FF  <1>
  2358                              <1> ;-----	UC TABLE SCAN
  2359 000069C2 5455565758          <1> K12:	db	84,85,86,87,88		; SHIFTED STATE OF F1 - F10
  2360 000069C7 595A5B5C5D          <1> 	db	89,90,91,92,93
  2361 000069CC FFFF                <1> 	db	-1,-1			; NL, SL
  2362                              <1> 
  2363                              <1> ;-----	NUM STATE TABLE
  2364 000069CE 3738392D3435362B31- <1> K14:	db 	'789-456+1230.'		; NUMLOCK STATE OF KEYPAD KEYS
  2364 000069D7 3233302E            <1>
  2365                              <1> 	;
  2366 000069DB FFFF7C8788          <1> 	db	-1,-1,124,135,136	; SysRq, Undef, WT, F11, F12
  2367                              <1> 
  2368                              <1> Align	4
  2369                              <1> ;----------------------------------------
  2370                              <1> ;	VIDEO DISPLAY DATA AREA		;
  2371                              <1> ;----------------------------------------
  2372 000069E0 03                  <1> CRT_MODE	db	3	; CURRENT DISPLAY MODE (TYPE)
  2373 000069E1 29                  <1> CRT_MODE_SET	db	29h	; CURRENT SETTING OF THE 3X8 REGISTER
  2374                              <1> 				; (29h default setting for video mode 3)
  2375                              <1> 				; Mode Select register Bits
  2376                              <1> 				;   BIT 0 - 80x25 (1), 40x25 (0)
  2377                              <1> 				;   BIT 1 - ALPHA (0), 320x200 GRAPHICS (1)
  2378                              <1> 				;   BIT 2 - COLOR (0), BW (1)
  2379                              <1> 				;   BIT 3 - Video Sig. ENABLE (1), DISABLE (0)
  2380                              <1> 				;   BIT 4 - 640x200 B&W Graphics Mode (1)
  2381                              <1> 				;   BIT 5 - ALPHA mode BLINKING (1)
  2382                              <1> 				;   BIT 6, 7 - Not Used
  2383                              <1> 
  2384                              <1> ; Mode 0 - 2Ch = 101100b	; 40x25 text, 16 gray colors
  2385                              <1> ; Mode 1 - 28h = 101000b	; 40x25 text, 16 fore colors, 8 back colors
  2386                              <1> ; Mode 2 - 2Dh = 101101b	; 80x25 text, 16 gray colors	
  2387                              <1> ; MODE 3 - 29h = 101001b	; 80x25 text, 16 fore color, 8 back color
  2388                              <1> ; Mode 4 - 2Ah = 101010b	; 320x200 graphics, 4 colors
  2389                              <1> ; Mode 5 - 2Eh = 101110b	; 320x200 graphics, 4 gray colors
  2390                              <1> ; Mode 6 - 1Eh = 011110b	; 640x200 graphics, 2 colors
  2391                              <1> ; Mode 7 - 29h = 101001b	; 80x25 text, black & white colors
  2392                              <1> ; Mode & 37h = Video signal OFF
  2393                              <1> 			
  2394                              <1> 
  2395                              <1> ; 26/08/2014
  2396                              <1> ; Retro UNIX 8086 v1 - UNIX.ASM (03/03/2014)
  2397                              <1> ; Derived from IBM "pc-at" 
  2398                              <1> ; rombios source code (06/10/1985)
  2399                              <1> ; 'dseg.inc'
  2400                              <1> 
  2401                              <1> ;---------------------------------------;
  2402                              <1> ;	SYSTEM DATA AREA		;
  2403                              <1> ;----------------------------------------
  2404 000069E2 00                  <1> BIOS_BREAK	db	0		; BIT 7=1 IF BREAK KEY HAS BEEN PRESSED
  2405                              <1> 
  2406                              <1> ;----------------------------------------
  2407                              <1> ;	KEYBOARD DATA AREAS		;
  2408                              <1> ;----------------------------------------
  2409                              <1> 
  2410 000069E3 00                  <1> KB_FLAG		db	0		; KEYBOARD SHIFT STATE AND STATUS FLAGS
  2411 000069E4 00                  <1> KB_FLAG_1	db	0		; SECOND BYTE OF KEYBOARD STATUS
  2412 000069E5 00                  <1> KB_FLAG_2	db	0		; KEYBOARD LED FLAGS
  2413 000069E6 00                  <1> KB_FLAG_3	db	0		; KEYBOARD MODE STATE AND TYPE FLAGS
  2414 000069E7 00                  <1> ALT_INPUT	db	0		; STORAGE FOR ALTERNATE KEY PAD ENTRY
  2415 000069E8 [F8690000]          <1> BUFFER_START	dd	KB_BUFFER 	; OFFSET OF KEYBOARD BUFFER START
  2416 000069EC [186A0000]          <1> BUFFER_END	dd	KB_BUFFER + 32	; OFFSET OF END OF BUFFER
  2417 000069F0 [F8690000]          <1> BUFFER_HEAD	dd	KB_BUFFER 	; POINTER TO HEAD OF KEYBOARD BUFFER
  2418 000069F4 [F8690000]          <1> BUFFER_TAIL	dd	KB_BUFFER 	; POINTER TO TAIL OF KEYBOARD BUFFER
  2419                              <1> ; ------	HEAD = TAIL	INDICATES THAT THE BUFFER IS EMPTY
  2420 000069F8 0000<rep 10h>       <1> KB_BUFFER	times	16 dw 0		; ROOM FOR 16 SCAN CODE ENTRIES
  2421                              <1> 
  2422                              <1> ; /// End Of KEYBOARD DATA ///
  2285                                  %include 'vidata.s'	; VIDEO (BIOS) DATA
  2286                              <1> ; Retro UNIX 386 v1 Kernel - VIDATA.INC
  2287                              <1> ; Last Modification: 11/03/2015
  2288                              <1> ;		    (Data section for 'VIDEO.INC')	
  2289                              <1> ;
  2290                              <1> ; ///////// VIDEO DATA ///////////////
  2291                              <1> 
  2292                              <1> video_params:
  2293                              <1> 	; 02/09/2014 (Retro UNIX 386 v1)
  2294                              <1> 	;ORGS.ASM ----- 06/10/85   COMPATIBILITY MODULE
  2295                              <1> 	; VIDEO MODE 3
  2296 00006A18 71505A0A1F0619      <1> 	db	71h,50h,5Ah,0Ah,1Fh,6,19h	; SET UP FOR 80X25
  2297 00006A1F 1C02070607          <1> 	db	1Ch,2,7,6,7	; cursor start = 6, cursor stop = 7
  2298 00006A24 00000000            <1> 	db	0,0,0,0
  2299                              <1> 
  2300                              <1> ; /// End Of VIDEO DATA ///
  2286                                  %include 'diskdata.s'	; DISK (BIOS) DATA (initialized)
  2287                              <1> ; Retro UNIX 386 v1 Kernel - DISKDATA.INC
  2288                              <1> ; Last Modification: 11/03/2015
  2289                              <1> ;	(Initialized Disk Parameters Data section for 'DISKIO.INC') 
  2290                              <1> ;
  2291                              <1> ; *****************************************************************************
  2292                              <1> 
  2293                              <1> ;----------------------------------------
  2294                              <1> ;	80286 INTERRUPT LOCATIONS	:
  2295                              <1> ;	REFERENCED BY POST & BIOS	:
  2296                              <1> ;----------------------------------------
  2297                              <1> 
  2298 00006A28 [8B6A0000]          <1> DISK_POINTER:	dd	MD_TBL6		; Pointer to Diskette Parameter Table
  2299                              <1> 
  2300                              <1> ; IBM PC-XT Model 286 source code ORGS.ASM (06/10/85) - 14/12/2014
  2301                              <1> ;----------------------------------------------------------------
  2302                              <1> ; DISK_BASE							:
  2303                              <1> ;	THIS IS THE SET OF PARAMETERS REQUIRED FOR		:
  2304                              <1> ;	DISKETTE OPERATION. THEY ARE POINTED AT BY THE		:
  2305                              <1> ;	DATA VARIABLE @DISK_POINTER. TO MODIFY THE PARAMETERS,	:
  2306                              <1> ;	BUILD ANOTHER PARAMETER BLOCK AND POINT AT IT		:
  2307                              <1> ;----------------------------------------------------------------
  2308                              <1> 
  2309                              <1> ;DISK_BASE:	
  2310                              <1> ;	DB	11011111B	; SRT=D, HD UNLOAD=0F - 1ST SPECIFY BYTE
  2311                              <1> ;	DB	2		; HD LOAD=1, MODE=DMA - 2ND SPECIFY BYTE
  2312                              <1> ;	DB	MOTOR_WAIT	; WAIT TIME AFTER OPERATION TILL MOTOR OFF
  2313                              <1> ;	DB	2		; 512 BYTES/SECTOR
  2314                              <1> ;	;DB	15		; EOT (LAST SECTOR ON TRACK)
  2315                              <1> ;	db	18		; (EOT for 1.44MB diskette)
  2316                              <1> ;	DB	01BH		; GAP LENGTH
  2317                              <1> ;	DB	0FFH		; DTL
  2318                              <1> ;	;DB	054H		; GAP LENGTH FOR FORMAT
  2319                              <1> ;	db	06ch		; (for 1.44MB dsikette)
  2320                              <1> ;	DB	0F6H		; FILL BYTE FOR FORMAT
  2321                              <1> ;	DB	15		; HEAD SETTLE TIME (MILLISECONDS)
  2322                              <1> ;	DB	8		; MOTOR START TIME (1/8 SECONDS)
  2323                              <1> 
  2324                              <1> ;----------------------------------------
  2325                              <1> ;	ROM BIOS DATA AREAS		:
  2326                              <1> ;----------------------------------------
  2327                              <1> 
  2328                              <1> ;DATA		SEGMENT AT 40H		; ADDRESS= 0040:0000
  2329                              <1> 
  2330                              <1> ;@EQUIP_FLAG	DW	?		; INSTALLED HARDWARE FLAGS
  2331                              <1> 
  2332                              <1> ;----------------------------------------
  2333                              <1> ;	DISKETTE DATA AREAS		:
  2334                              <1> ;----------------------------------------
  2335                              <1> 
  2336                              <1> ;@SEEK_STATUS	DB	?		; DRIVE RECALIBRATION STATUS
  2337                              <1> ;					; BIT 3-0 = DRIVE 3-0 RECALIBRATION
  2338                              <1> ;					; BEFORE NEXT SEEK IF BIT IS = 0
  2339                              <1> ;@MOTOR_STATUS	DB	?		; MOTOR STATUS
  2340                              <1> ;					; BIT 3-0 = DRIVE 3-0 CURRENTLY RUNNING
  2341                              <1> ;					; BIT 7 = CURRENT OPERATION IS A WRITE
  2342                              <1> ;@MOTOR_COUNT	DB	?		; TIME OUT COUNTER FOR MOTOR(S) TURN OFF
  2343                              <1> ;@DSKETTE_STATUS DB	?		; RETURN CODE STATUS BYTE
  2344                              <1> ;					; CMD_BLOCK  IN STACK FOR DISK OPERATION
  2345                              <1> ;@NEC_STATUS	DB	7 DUP(?)	; STATUS BYTES FROM DISKETTE OPERATION
  2346                              <1> 
  2347                              <1> ;----------------------------------------
  2348                              <1> ;	POST AND BIOS WORK DATA AREA	:
  2349                              <1> ;----------------------------------------
  2350                              <1> 
  2351                              <1> ;@INTR_FLAG	DB	?		; FLAG INDICATING AN INTERRUPT HAPPENED
  2352                              <1> 
  2353                              <1> ;----------------------------------------
  2354                              <1> ;	TIMER DATA AREA 		:
  2355                              <1> ;----------------------------------------
  2356                              <1> 
  2357                              <1> ; 17/12/2014  (IRQ 0 - INT 08H)
  2358                              <1> ;TIMER_LOW	equ	46Ch		; Timer ticks (counter)  @ 40h:006Ch
  2359                              <1> ;TIMER_HIGH	equ	46Eh		; (18.2 timer ticks per second)
  2360                              <1> ;TIMER_OFL	equ	470h		; Timer - 24 hours flag  @ 40h:0070h
  2361                              <1> 
  2362                              <1> ;----------------------------------------
  2363                              <1> ;	ADDITIONAL MEDIA DATA		:
  2364                              <1> ;----------------------------------------
  2365                              <1> 
  2366                              <1> ;@LASTRATE	DB	?		; LAST DISKETTE DATA RATE SELECTED
  2367                              <1> ;@DSK_STATE	DB	?		; DRIVE 0 MEDIA STATE
  2368                              <1> ;		DB	?		; DRIVE 1 MEDIA STATE
  2369                              <1> ;		DB	?		; DRIVE 0 OPERATION START STATE
  2370                              <1> ;		DB	?		; DRIVE 1 OPERATION START STATE
  2371                              <1> ;@DSK_TRK	DB	?		; DRIVE 0 PRESENT CYLINDER
  2372                              <1> ;		DB	?		; DRIVE 1 PRESENT CYLINDER
  2373                              <1> 
  2374                              <1> ;DATA		ENDS			; END OF BIOS DATA SEGMENT
  2375                              <1> 
  2376                              <1> ;--------------------------------------------------------
  2377                              <1> ;	DRIVE TYPE TABLE				:
  2378                              <1> ;--------------------------------------------------------
  2379                              <1> 		; 16/02/2015 (unix386.s, 32 bit modifications)
  2380                              <1> DR_TYPE:
  2381 00006A2C 01                  <1> 		DB	01		;DRIVE TYPE, MEDIA TABLE
  2382                              <1>                 ;DW	MD_TBL1
  2383 00006A2D [4A6A0000]          <1> 		dd	MD_TBL1
  2384 00006A31 82                  <1> 		DB	02+BIT7ON
  2385                              <1> 		;DW	MD_TBL2
  2386 00006A32 [576A0000]          <1>                 dd      MD_TBL2
  2387 00006A36 02                  <1> DR_DEFAULT:	DB	02
  2388                              <1>                 ;DW	MD_TBL3
  2389 00006A37 [646A0000]          <1> 		dd      MD_TBL3
  2390 00006A3B 03                  <1> 		DB	03
  2391                              <1>                 ;DW	MD_TBL4
  2392 00006A3C [716A0000]          <1> 		dd      MD_TBL4
  2393 00006A40 84                  <1> 		DB	04+BIT7ON
  2394                              <1>                 ;DW	MD_TBL5
  2395 00006A41 [7E6A0000]          <1> 		dd      MD_TBL5
  2396 00006A45 04                  <1> 		DB	04
  2397                              <1>                 ;DW	MD_TBL6
  2398 00006A46 [8B6A0000]          <1> 		dd      MD_TBL6
  2399                              <1> DR_TYPE_E       equ $			; END OF TABLE
  2400                              <1> ;DR_CNT		EQU	(DR_TYPE_E-DR_TYPE)/3
  2401                              <1> DR_CNT		equ	(DR_TYPE_E-DR_TYPE)/5
  2402                              <1> ;--------------------------------------------------------
  2403                              <1> ;	MEDIA/DRIVE PARAMETER TABLES			:
  2404                              <1> ;--------------------------------------------------------
  2405                              <1> ;--------------------------------------------------------
  2406                              <1> ;	360 KB MEDIA IN 360 KB DRIVE			:
  2407                              <1> ;--------------------------------------------------------
  2408                              <1> MD_TBL1:        
  2409 00006A4A DF                  <1> 	DB	11011111B	; SRT=D, HD UNLOAD=0F - 1ST SPECIFY BYTE
  2410 00006A4B 02                  <1> 	DB	2		; HD LOAD=1, MODE=DMA - 2ND SPECIFY BYTE
  2411 00006A4C 25                  <1> 	DB	MOTOR_WAIT	; WAIT TIME AFTER OPERATION TILL MOTOR OFF
  2412 00006A4D 02                  <1> 	DB	2		; 512 BYTES/SECTOR
  2413 00006A4E 09                  <1> 	DB	09		; EOT (LAST SECTOR ON TRACK)
  2414 00006A4F 2A                  <1> 	DB	02AH		; GAP LENGTH
  2415 00006A50 FF                  <1> 	DB	0FFH		; DTL
  2416 00006A51 50                  <1> 	DB	050H		; GAP LENGTH FOR FORMAT
  2417 00006A52 F6                  <1> 	DB	0F6H		; FILL BYTE FOR FORMAT
  2418 00006A53 0F                  <1> 	DB	15		; HEAD SETTLE TIME (MILLISECONDS)
  2419 00006A54 08                  <1> 	DB	8		; MOTOR START TIME (1/8 SECONDS)
  2420 00006A55 27                  <1> 	DB	39		; MAX. TRACK NUMBER
  2421 00006A56 80                  <1> 	DB	RATE_250	; DATA TRANSFER RATE
  2422                              <1> ;--------------------------------------------------------
  2423                              <1> ;	360 KB MEDIA IN 1.2 MB DRIVE			:
  2424                              <1> ;--------------------------------------------------------
  2425                              <1> MD_TBL2:        
  2426 00006A57 DF                  <1> 	DB	11011111B	; SRT=D, HD UNLOAD=0F - 1ST SPECIFY BYTE
  2427 00006A58 02                  <1> 	DB	2		; HD LOAD=1, MODE=DMA - 2ND SPECIFY BYTE
  2428 00006A59 25                  <1> 	DB	MOTOR_WAIT	; WAIT TIME AFTER OPERATION TILL MOTOR OFF
  2429 00006A5A 02                  <1> 	DB	2		; 512 BYTES/SECTOR
  2430 00006A5B 09                  <1> 	DB	09		; EOT (LAST SECTOR ON TRACK)
  2431 00006A5C 2A                  <1> 	DB	02AH		; GAP LENGTH
  2432 00006A5D FF                  <1> 	DB	0FFH		; DTL
  2433 00006A5E 50                  <1> 	DB	050H		; GAP LENGTH FOR FORMAT
  2434 00006A5F F6                  <1> 	DB	0F6H		; FILL BYTE FOR FORMAT
  2435 00006A60 0F                  <1> 	DB	15		; HEAD SETTLE TIME (MILLISECONDS)
  2436 00006A61 08                  <1> 	DB	8		; MOTOR START TIME (1/8 SECONDS)
  2437 00006A62 27                  <1> 	DB	39		; MAX. TRACK NUMBER
  2438 00006A63 40                  <1> 	DB	RATE_300	; DATA TRANSFER RATE
  2439                              <1> ;--------------------------------------------------------
  2440                              <1> ;	1.2 MB MEDIA IN 1.2 MB DRIVE			:
  2441                              <1> ;--------------------------------------------------------
  2442                              <1> MD_TBL3:
  2443 00006A64 DF                  <1> 	DB	11011111B	; SRT=D, HD UNLOAD=0F - 1ST SPECIFY BYTE
  2444 00006A65 02                  <1> 	DB	2		; HD LOAD=1, MODE=DMA - 2ND SPECIFY BYTE
  2445 00006A66 25                  <1> 	DB	MOTOR_WAIT	; WAIT TIME AFTER OPERATION TILL MOTOR OFF
  2446 00006A67 02                  <1> 	DB	2		; 512 BYTES/SECTOR
  2447 00006A68 0F                  <1> 	DB	15		; EOT (LAST SECTOR ON TRACK)
  2448 00006A69 1B                  <1> 	DB	01BH		; GAP LENGTH
  2449 00006A6A FF                  <1> 	DB	0FFH		; DTL
  2450 00006A6B 54                  <1> 	DB	054H		; GAP LENGTH FOR FORMAT
  2451 00006A6C F6                  <1> 	DB	0F6H		; FILL BYTE FOR FORMAT
  2452 00006A6D 0F                  <1> 	DB	15		; HEAD SETTLE TIME (MILLISECONDS)
  2453 00006A6E 08                  <1> 	DB	8		; MOTOR START TIME (1/8 SECONDS)
  2454 00006A6F 4F                  <1> 	DB	79		; MAX. TRACK NUMBER
  2455 00006A70 00                  <1> 	DB	RATE_500	; DATA TRANSFER RATE
  2456                              <1> ;--------------------------------------------------------
  2457                              <1> ;	720 KB MEDIA IN 720 KB DRIVE			:
  2458                              <1> ;--------------------------------------------------------
  2459                              <1> MD_TBL4:
  2460 00006A71 DF                  <1> 	DB	11011111B	; SRT=D, HD UNLOAD=0F - 1ST SPECIFY BYTE
  2461 00006A72 02                  <1> 	DB	2		; HD LOAD=1, MODE=DMA - 2ND SPECIFY BYTE
  2462 00006A73 25                  <1> 	DB	MOTOR_WAIT	; WAIT TIME AFTER OPERATION TILL MOTOR OFF
  2463 00006A74 02                  <1> 	DB	2		; 512 BYTES/SECTOR
  2464 00006A75 09                  <1> 	DB	09		; EOT (LAST SECTOR ON TRACK)
  2465 00006A76 2A                  <1> 	DB	02AH		; GAP LENGTH
  2466 00006A77 FF                  <1> 	DB	0FFH		; DTL
  2467 00006A78 50                  <1> 	DB	050H		; GAP LENGTH FOR FORMAT
  2468 00006A79 F6                  <1> 	DB	0F6H		; FILL BYTE FOR FORMAT
  2469 00006A7A 0F                  <1> 	DB	15		; HEAD SETTLE TIME (MILLISECONDS)
  2470 00006A7B 08                  <1> 	DB	8		; MOTOR START TIME (1/8 SECONDS)
  2471 00006A7C 4F                  <1> 	DB	79		; MAX. TRACK NUMBER
  2472 00006A7D 80                  <1> 	DB	RATE_250	; DATA TRANSFER RATE
  2473                              <1> ;--------------------------------------------------------
  2474                              <1> ;	720 KB MEDIA IN 1.44 MB DRIVE			:
  2475                              <1> ;--------------------------------------------------------
  2476                              <1> MD_TBL5:
  2477 00006A7E DF                  <1> 	DB	11011111B	; SRT=D, HD UNLOAD=0F - 1ST SPECIFY BYTE
  2478 00006A7F 02                  <1> 	DB	2		; HD LOAD=1, MODE=DMA - 2ND SPECIFY BYTE
  2479 00006A80 25                  <1> 	DB	MOTOR_WAIT	; WAIT TIME AFTER OPERATION TILL MOTOR OFF
  2480 00006A81 02                  <1> 	DB	2		; 512 BYTES/SECTOR
  2481 00006A82 09                  <1> 	DB	09		; EOT (LAST SECTOR ON TRACK)
  2482 00006A83 2A                  <1> 	DB	02AH		; GAP LENGTH
  2483 00006A84 FF                  <1> 	DB	0FFH		; DTL
  2484 00006A85 50                  <1> 	DB	050H		; GAP LENGTH FOR FORMAT
  2485 00006A86 F6                  <1> 	DB	0F6H		; FILL BYTE FOR FORMAT
  2486 00006A87 0F                  <1> 	DB	15		; HEAD SETTLE TIME (MILLISECONDS)
  2487 00006A88 08                  <1> 	DB	8		; MOTOR START TIME (1/8 SECONDS)
  2488 00006A89 4F                  <1> 	DB	79		; MAX. TRACK NUMBER
  2489 00006A8A 80                  <1> 	DB	RATE_250	; DATA TRANSFER RATE
  2490                              <1> ;--------------------------------------------------------
  2491                              <1> ;	1.44 MB MEDIA IN 1.44 MB DRIVE			:
  2492                              <1> ;--------------------------------------------------------
  2493                              <1> MD_TBL6:
  2494 00006A8B AF                  <1> 	DB	10101111B	; SRT=A, HD UNLOAD=0F - 1ST SPECIFY BYTE
  2495 00006A8C 02                  <1> 	DB	2		; HD LOAD=1, MODE=DMA - 2ND SPECIFY BYTE
  2496 00006A8D 25                  <1> 	DB	MOTOR_WAIT	; WAIT TIME AFTER OPERATION TILL MOTOR OFF
  2497 00006A8E 02                  <1> 	DB	2		; 512 BYTES/SECTOR
  2498 00006A8F 12                  <1> 	DB	18		; EOT (LAST SECTOR ON TRACK)
  2499 00006A90 1B                  <1> 	DB	01BH		; GAP LENGTH
  2500 00006A91 FF                  <1> 	DB	0FFH		; DTL
  2501 00006A92 6C                  <1> 	DB	06CH		; GAP LENGTH FOR FORMAT
  2502 00006A93 F6                  <1> 	DB	0F6H		; FILL BYTE FOR FORMAT
  2503 00006A94 0F                  <1> 	DB	15		; HEAD SETTLE TIME (MILLISECONDS)
  2504 00006A95 08                  <1> 	DB	8		; MOTOR START TIME (1/8 SECONDS)
  2505 00006A96 4F                  <1> 	DB	79		; MAX. TRACK NUMBER
  2506 00006A97 00                  <1> 	DB	RATE_500	; DATA TRANSFER RATE
  2507                              <1> 
  2508                              <1> 
  2509                              <1> ; << diskette.inc >>
  2510                              <1> ; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2511                              <1> ;
  2512                              <1> ;----------------------------------------
  2513                              <1> ;	ROM BIOS DATA AREAS		:
  2514                              <1> ;----------------------------------------
  2515                              <1> 
  2516                              <1> ;DATA		SEGMENT AT 40H		; ADDRESS= 0040:0000
  2517                              <1> 
  2518                              <1> ;----------------------------------------
  2519                              <1> ;	FIXED DISK DATA AREAS		:
  2520                              <1> ;----------------------------------------
  2521                              <1> 
  2522                              <1> ;DISK_STATUS1:	DB	0		; FIXED DISK STATUS
  2523                              <1> ;HF_NUM:		DB	0		; COUNT OF FIXED DISK DRIVES
  2524                              <1> ;CONTROL_BYTE:	DB	0		; HEAD CONTROL BYTE
  2525                              <1> ;@PORT_OFF	DB	?		;  RESERVED (PORT OFFSET)
  2526                              <1> 
  2527                              <1> ;----------------------------------------
  2528                              <1> ;	ADDITIONAL MEDIA DATA		:
  2529                              <1> ;----------------------------------------
  2530                              <1> 
  2531                              <1> ;@LASTRATE	DB	?		; LAST DISKETTE DATA RATE SELECTED
  2532                              <1> ;HF_STATUS	DB	0		; STATUS REGISTER
  2533                              <1> ;HF_ERROR	DB	0		; ERROR REGISTER
  2534                              <1> ;HF_INT_FLAG	DB	0		; FIXED DISK INTERRUPT FLAG
  2535                              <1> ;HF_CNTRL	DB	0		; COMBO FIXED DISK/DISKETTE CARD BIT 0=1
  2536                              <1> ;@DSK_STATE	DB	?		; DRIVE 0 MEDIA STATE
  2537                              <1> ;		DB	?		; DRIVE 1 MEDIA STATE
  2538                              <1> ;		DB	?		; DRIVE 0 OPERATION START STATE
  2539                              <1> ;		DB	?		; DRIVE 1 OPERATION START STATE
  2540                              <1> ;@DSK_TRK	DB	?		; DRIVE 0 PRESENT CYLINDER
  2541                              <1> ;		DB	?		; DRIVE 1 PRESENT CYLINDER
  2542                              <1> 
  2543                              <1> ;DATA		ENDS			; END OF BIOS DATA SEGMENT
  2544                              <1> ;
  2545                              <1> ; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2546                              <1> 
  2547                              <1> ERR_TBL:
  2548 00006A98 E0                  <1> 	db	NO_ERR
  2549 00006A99 024001BB            <1> 	db	BAD_ADDR_MARK,BAD_SEEK,BAD_CMD,UNDEF_ERR
  2550 00006A9D 04BB100A            <1> 	db	RECORD_NOT_FND,UNDEF_ERR,BAD_ECC,BAD_SECTOR
  2551                              <1> 
  2552                              <1> ; 17/12/2014 (mov ax, [cfd])
  2553                              <1> ; 11/12/2014
  2554 00006AA1 00                  <1> cfd:		db	0		; current floppy drive (for GET_PARM)
  2555                              <1> ; 17/12/2014				; instead of 'DISK_POINTER'
  2556 00006AA2 01                  <1> pfd:		db	1		; previous floppy drive (for GET_PARM)
  2557                              <1> 					; (initial value of 'pfd 
  2558                              <1> 					; must be different then 'cfd' value
  2559                              <1> 					; to force updating/initializing
  2560                              <1> 					; current drive parameters) 
  2561 00006AA3 90                  <1> align 2
  2562                              <1> 
  2563 00006AA4 F001                <1> HF_PORT:	dw 	1F0h  ; Default = 1F0h
  2564                              <1> 			      ; (170h)
  2565 00006AA6 F603                <1> HF_REG_PORT:	dw	3F6h  ; HF_PORT + 206h
  2566                              <1> 
  2567                              <1> ; 05/01/2015 
  2568 00006AA8 00                  <1> hf_m_s:         db      0     ; (0 = Master, 1 = Slave)
  2569                              <1> 
  2570                              <1> ; *****************************************************************************
  2287                                  ;;;
  2288                                  
  2289 00006AA9 90                      Align 2
  2290                                  
  2291                                  ; 12/11/2014 (Retro UNIX 386 v1)
  2292 00006AAA 00                      boot_drv:    db 0  ; boot drive number (physical)
  2293                                  ; 24/11/2014
  2294 00006AAB 00                      drv:	     db 0 
  2295 00006AAC 00                      last_drv:    db 0  ; last hdd
  2296 00006AAD 00                      hdc:         db 0  ; number of hard disk drives
  2297                                  		     ; (present/detected)
  2298                                  ;
  2299                                  ; 24/11/2014 (Retro UNIX 386 v1)
  2300                                  ; Physical drive type & flags
  2301 00006AAE 00                      fd0_type:    db 0  ; floppy drive type
  2302 00006AAF 00                      fd1_type:    db 0    ; 4 = 1.44 Mb, 80 track, 3.5" (18 spt)
  2303                                  		     ; 6 = 2.88 Mb, 80 track, 3.5" (36 spt)
  2304                                  		     ; 3 = 720 Kb, 80 track, 3.5" (9 spt)
  2305                                  		     ; 2 = 1.2 Mb, 80 track, 5.25" (15 spt)
  2306                                  		     ; 1 = 360 Kb, 40 track, 5.25" (9 spt)		
  2307 00006AB0 00                      hd0_type:    db 0  ; EDD status for hd0 (bit 7 = present flag)
  2308 00006AB1 00                      hd1_type:    db 0  ; EDD status for hd1 (bit 7 = present flag)
  2309 00006AB2 00                      hd2_type:    db 0  ; EDD status for hd2 (bit 7 = present flag)
  2310 00006AB3 00                      hd3_type:    db 0  ; EDD status for hd3 (bit 7 = present flag)
  2311                                  		     ; bit 0 - Fixed disk access subset supported
  2312                                  		     ; bit 1 - Drive locking and ejecting
  2313                                  		     ; bit 2 - Enhanced disk drive support
  2314                                  		     ; bit 3 = Reserved (64 bit EDD support)
  2315                                  		     ; (If bit 0 is '1' Retro UNIX 386 v1
  2316                                  		     ; will interpret it as 'LBA ready'!)		
  2317                                  
  2318                                  ; 11/03/2015 - 10/07/2015
  2319 00006AB4 000000000000000000-     drv.cylinders: dw 0,0,0,0,0,0,0
  2319 00006ABD 0000000000         
  2320 00006AC2 000000000000000000-     drv.heads:     dw 0,0,0,0,0,0,0
  2320 00006ACB 0000000000         
  2321 00006AD0 000000000000000000-     drv.spt:       dw 0,0,0,0,0,0,0
  2321 00006AD9 0000000000         
  2322 00006ADE 000000000000000000-     drv.size:      dd 0,0,0,0,0,0,0
  2322 00006AE7 000000000000000000-
  2322 00006AF0 000000000000000000-
  2322 00006AF9 00                 
  2323 00006AFA 00000000000000          drv.status:    db 0,0,0,0,0,0,0
  2324 00006B01 00000000000000          drv.error:     db 0,0,0,0,0,0,0		
  2325                                  ;
  2326                                  
  2327                                  ; 27/08/2014
  2328                                  scr_row:
  2329 00006B08 E0810B00                	dd 0B8000h + 0A0h + 0A0h + 0A0h ; Row 3
  2330                                  scr_col:
  2331 00006B0C 00000000                	dd 0
  2332                                  
  2333                                  ;; 14/08/2015
  2334                                  ;;msgPM:
  2335                                  ;;      db "Protected mode and paging are ENABLED ... ", 0
  2336                                  msgKVER:
  2337 00006B10 526574726F20554E49-     	db "Retro UNIX 386 v1.2 - Kernel v0.2.2.0 [21/01/2022]", 0
  2337 00006B19 58203338362076312E-
  2337 00006B22 32202D204B65726E65-
  2337 00006B2B 6C2076302E322E322E-
  2337 00006B34 30205B32312F30312F-
  2337 00006B3D 323032325D00       
  2338                                  
  2339 00006B43 90                      Align 2
  2340                                  
  2341                                  ; 20/08/2014
  2342                                    ; /* This is the default interrupt "handler" :-) */ 
  2343                                    ; Linux v0.12 (head.s)
  2344                                  int_msg:
  2345 00006B44 556E6B6E6F776E2069-     	db "Unknown interrupt ! ", 0
  2345 00006B4D 6E7465727275707420-
  2345 00006B56 212000             
  2346                                  
  2347 00006B59 90                      Align 2  
  2348                                  
  2349                                  ; 21/08/2014
  2350                                  timer_msg:
  2351 00006B5A 49525120302028494E-     	db "IRQ 0 (INT 20h) ! Timer Interrupt : "
  2351 00006B63 542032306829202120-
  2351 00006B6C 54696D657220496E74-
  2351 00006B75 657272757074203A20 
  2352                                  tcountstr:
  2353 00006B7E 303030303020            	db "00000 "
  2354 00006B84 00                      	db 0
  2355                                  
  2356 00006B85 90                      Align 2
  2357                                  	; 21/08/2014
  2358                                  exc_msg:
  2359 00006B86 435055206578636570-     	db "CPU exception ! "
  2359 00006B8F 74696F6E202120     
  2360                                  excnstr: 		; 25/08/2014
  2361 00006B96 3F3F68202045495020-     	db "??h", "  EIP : "
  2361 00006B9F 3A20               
  2362                                  EIPstr: ; 29/08/2014
  2363 00006BA1 00<rep Ch>              	times 12 db 0
  2364                                  rtc_msg:
  2365 00006BAD 5265616C2054696D65-     	db "Real Time Clock - "
  2365 00006BB6 20436C6F636B202D20 
  2366                                  datestr:
  2367 00006BBF 30302F30302F303030-     	db "00/00/0000"
  2367 00006BC8 30                 
  2368 00006BC9 20                      	db " "
  2369                                  daystr:
  2370 00006BCA 44415920                	db "DAY "
  2371                                  timestr:	
  2372 00006BCE 30303A30303A3030                db "00:00:00"
  2373 00006BD6 20                      	db " "
  2374 00006BD7 00                      	db 0 
  2375                                  
  2376                                  daytmp:
  2377                                  	; 28/02/2015
  2378 00006BD8 3F3F3F2053554E204D-     	db "??? SUN MON TUE WED THU FRI SAT "
  2378 00006BE1 4F4E20545545205745-
  2378 00006BEA 442054485520465249-
  2378 00006BF3 2053415420         
  2379                                  
  2380 00006BF8 FF                      ptime_seconds: db 0FFh
  2381                                  
  2382                                  	; 23/02/2015
  2383                                  	; 25/08/2014
  2384                                  ;scounter:
  2385                                  ;	db 5
  2386                                  ;	db 19
  2387                                  
  2388                                  ; 28/11/2021
  2389                                  ;; 05/11/2014
  2390                                  ;msg_out_of_memory:
  2391                                  ;	db 	07h, 0Dh, 0Ah
  2392                                  ;	db	'Insufficient memory ! (Minimum 2 MB memory is needed.)'
  2393                                  ; 	db	0Dh, 0Ah, 0
  2394                                  	;
  2395                                  setup_error_msg:
  2396 00006BF9 0D0A                    	db 0Dh, 0Ah
  2397 00006BFB 4469736B2053657475-     	db 'Disk Setup Error!' 
  2397 00006C04 70204572726F7221   
  2398 00006C0C 0D0A00                  	db 0Dh, 0Ah,0
  2399                                  
  2400                                  ; 02/09/2014 (Retro UNIX 386 v1)
  2401                                  ;crt_ulc: db 0 ; upper left column (for scroll) 
  2402                                  ;	  db 0 ; upper left row (for scroll)	
  2403                                  
  2404                                  ;crt_lrc: db 79 ; lower right column (for scroll) 
  2405                                  ;	  db 24 ; lower right row (for scroll)
  2406                                  
  2407                                  ; 06/11/2014 (Temporary Data)
  2408                                  ; Memory Information message
  2409                                  ; 14/08/2015
  2410                                  msg_memory_info:
  2411 00006C0F 07                      	db	07h
  2412 00006C10 0D0A                    	db	0Dh, 0Ah
  2413                                  	;db 	"MEMORY ALLOCATION INFO", 0Dh, 0Ah, 0Dh, 0Ah
  2414 00006C12 546F74616C206D656D-     	db	"Total memory : "
  2414 00006C1B 6F7279203A20       
  2415                                  mem_total_b_str: ; 10 digits
  2416 00006C21 303030303030303030-     	db	"0000000000 bytes", 0Dh, 0Ah
  2416 00006C2A 302062797465730D0A 
  2417 00006C33 202020202020202020-     	db	"               ", 20h, 20h, 20h
  2417 00006C3C 202020202020202020 
  2418                                  mem_total_p_str: ; 7 digits
  2419 00006C45 303030303030302070-     	db	"0000000 pages", 0Dh, 0Ah
  2419 00006C4E 616765730D0A       
  2420 00006C54 0D0A                    	db 	0Dh, 0Ah
  2421 00006C56 46726565206D656D6F-     	db	"Free memory  : "
  2421 00006C5F 727920203A20       
  2422                                  free_mem_b_str:  ; 10 digits
  2423 00006C65 3F3F3F3F3F3F3F3F3F-     	db	"?????????? bytes", 0Dh, 0Ah
  2423 00006C6E 3F2062797465730D0A 
  2424 00006C77 202020202020202020-     	db	"               ", 20h, 20h, 20h
  2424 00006C80 202020202020202020 
  2425                                  free_mem_p_str:  ; 7 digits
  2426 00006C89 3F3F3F3F3F3F3F2070-     	db	"??????? pages", 0Dh, 0Ah
  2426 00006C92 616765730D0A       
  2427 00006C98 0D0A00                  	db	0Dh, 0Ah, 0
  2428                                  
  2429                                  dsk_ready_msg:
  2430 00006C9B 0D0A                    	db 	0Dh, 0Ah
  2431                                  dsktype:
  2432 00006C9D 6664                    	db	'fd'
  2433                                  dskx:
  2434 00006C9F 30                      	db	'0'
  2435 00006CA0 20                      	db	20h
  2436 00006CA1 697320524541445920-     	db 	'is READY ...'
  2436 00006CAA 2E2E2E             
  2437 00006CAD 00                      	db 	0
  2438                                  nextline:
  2439 00006CAE 0D0A00                  	db 	0Dh, 0Ah, 0
  2440                                  
  2441                                  ; KERNEL - SYSINIT Messages
  2442                                  ; 24/08/2015
  2443                                  ; 13/04/2015 - (Retro UNIX 386 v1 Beginning)
  2444                                  ; 14/07/2013
  2445                                  ;kernel_init_err_msg:
  2446                                  ;	db 0Dh, 0Ah
  2447                                  ;	db 07h
  2448                                  ;	db 'Kernel initialization ERROR !'
  2449                                  ;	db 0Dh, 0Ah, 0 
  2450                                  ; 24/08/2015
  2451                                  ;;; (temporary kernel init message has been removed
  2452                                  ;;;  from 'sys_init' code)
  2453                                  ;kernel_init_ok_msg: 
  2454                                  ;	db 0Dh, 0Ah
  2455                                  ;	db 07h
  2456                                  ;	db 'Welcome to Retro UNIX 386 v1.1 Operating System !'
  2457                                  ;	db 0Dh, 0Ah
  2458                                  ;       db 'by Erdogan Tan - 04/02/2016 (v0.2.1.0)'
  2459                                  ;	db 0Dh, 0Ah, 0
  2460                                  panic_msg:
  2461 00006CB1 0D0A07                  	db 0Dh, 0Ah, 07h
  2462 00006CB4 4552524F523A204B65-     	db 'ERROR: Kernel Panic !'
  2462 00006CBD 726E656C2050616E69-
  2462 00006CC6 632021             
  2463 00006CC9 0D0A00                  	db 0Dh, 0Ah, 0
  2464                                  etc_init_err_msg:
  2465 00006CCC 0D0A                    	db 0Dh, 0Ah
  2466 00006CCE 07                      	db 07h
  2467 00006CCF 4552524F523A202F65-     	db 'ERROR: /etc/init !?'
  2467 00006CD8 74632F696E69742021-
  2467 00006CE1 3F                 
  2468 00006CE2 0D0A00                  	db 0Dh, 0Ah, 0
  2469                                  
  2470                                  ; 10/05/2015
  2471                                  badsys_msg:
  2472 00006CE5 0D0A                    	db 0Dh, 0Ah
  2473 00006CE7 07                      	db 07h
  2474 00006CE8 496E76616C69642053-     	db 'Invalid System Call !'
  2474 00006CF1 797374656D2043616C-
  2474 00006CFA 6C2021             
  2475 00006CFD 0D0A                    	db 0Dh, 0Ah
  2476 00006CFF 4541583A20              	db 'EAX: '
  2477                                  bsys_msg_eax:
  2478 00006D04 303030303030303068      	db '00000000h'
  2479 00006D0D 0D0A                    	db 0Dh, 0Ah
  2480 00006D0F 4549503A20              	db 'EIP: '
  2481                                  bsys_msg_eip:
  2482 00006D14 303030303030303068      	db '00000000h' 
  2483 00006D1D 0D0A00                  	db 0Dh, 0Ah, 0
  2484                                  
  2485                                  BSYS_M_SIZE equ $ - badsys_msg
  2486                                  
  2487                                  align 2
  2488                                  
  2489                                  ; EPOCH Variables
  2490                                  ; 13/04/2015 - Retro UNIX 386 v1 Beginning
  2491                                  ; 09/04/2013 epoch variables
  2492                                  ; Retro UNIX 8086 v1 Prototype: UNIXCOPY.ASM, 10/03/2013
  2493                                  ;
  2494 00006D20 B207                    year: 	dw 1970
  2495 00006D22 0100                    month: 	dw 1
  2496 00006D24 0100                    day: 	dw 1
  2497 00006D26 0000                    hour: 	dw 0
  2498 00006D28 0000                    minute: dw 0
  2499 00006D2A 0000                    second: dw 0
  2500                                  
  2501                                  DMonth:
  2502 00006D2C 0000                    	dw 0
  2503 00006D2E 1F00                    	dw 31
  2504 00006D30 3B00                    	dw 59
  2505 00006D32 5A00                    	dw 90
  2506 00006D34 7800                    	dw 120
  2507 00006D36 9700                    	dw 151
  2508 00006D38 B500                    	dw 181
  2509 00006D3A D400                    	dw 212
  2510 00006D3C F300                    	dw 243
  2511 00006D3E 1101                    	dw 273
  2512 00006D40 3001                    	dw 304
  2513 00006D42 4E01                    	dw 334
  2514                                  
  2515                                  ; 02/01/2022 (Retro UNIX 386 v1.2)
  2516                                  ; 04/11/2014 (Retro UNIX 386 v1)
  2517 00006D44 0000                    mem_1m_1k:   dw 0  ; Number of contiguous KB between
  2518                                  		   ;   1 and 16 MB, max. 3C00h = 15 MB.
  2519 00006D46 0000                    	     dw 0  ; 02/01/2022 (Retro UNIX 386 v1.2)
  2520 00006D48 0000                    mem_16m_64k: dw 0  ; Number of contiguous 64 KB blocks
  2521                                  		   ;   between 16 MB and 4 GB.
  2522 00006D4A 0000                    	     dw 0  ; 02/01/2022 (Retro UNIX 386 v1.2)
  2523                                  
  2524                                  ; 01/01/2022
  2525                                  KEND:
  2526                                  
  2527 00006D4C 90<rep 4h>              align 16
  2528                                  
  2529                                  bss_start:
  2530                                  
  2531                                  ABSOLUTE bss_start
  2532                                  
  2533                                  	; 11/03/2015
  2534                                  	; Interrupt Descriptor Table (20/08/2014)
  2535                                  idt:
  2536 00006D50 <res 200h>              	resb	64*8 ; INT 0 to INT 3Fh
  2537                                  idt_end:
  2538                                  
  2539                                  ;alignb 4
  2540                                  
  2541                                  task_state_segment:
  2542                                  	; 24/03/2015
  2543 00006F50 ????                    tss.link:   resw 1
  2544 00006F52 ????                    	    resw 1
  2545                                  ; tss offset 4	
  2546 00006F54 ????????                tss.esp0:   resd 1
  2547 00006F58 ????                    tss.ss0:    resw 1
  2548 00006F5A ????                    	    resw 1	
  2549 00006F5C ????????                tss.esp1:   resd 1
  2550 00006F60 ????                    tss.ss1:    resw 1
  2551 00006F62 ????                    	    resw 1 	
  2552 00006F64 ????????                tss.esp2:   resd 1
  2553 00006F68 ????                    tss.ss2:    resw 1
  2554 00006F6A ????                    	    resw 1
  2555                                  ; tss offset 28
  2556 00006F6C ????????                tss.CR3:    resd 1
  2557 00006F70 ????????                tss.eip:    resd 1
  2558 00006F74 ????????                tss.eflags: resd 1
  2559                                  ; tss offset 40
  2560 00006F78 ????????                tss.eax:    resd 1		 		
  2561 00006F7C ????????                tss.ecx:    resd 1
  2562 00006F80 ????????                tss.edx:    resd 1
  2563 00006F84 ????????                tss.ebx:    resd 1
  2564 00006F88 ????????                tss.esp:    resd 1
  2565 00006F8C ????????                tss.ebp:    resd 1
  2566 00006F90 ????????                tss.esi:    resd 1
  2567 00006F94 ????????                tss.edi:    resd 1
  2568                                  ; tss offset 72
  2569 00006F98 ????                    tss.ES:     resw 1
  2570 00006F9A ????                    	    resw 1	
  2571 00006F9C ????                    tss.CS:	    resw 1
  2572 00006F9E ????                    	    resw 1
  2573 00006FA0 ????                    tss.SS:	    resw 1
  2574 00006FA2 ????                    	    resw 1
  2575 00006FA4 ????                    tss.DS:	    resw 1
  2576 00006FA6 ????                    	    resw 1
  2577 00006FA8 ????                    tss.FS:	    resw 1
  2578 00006FAA ????                    	    resw 1
  2579 00006FAC ????                    tss.GS:	    resw 1
  2580 00006FAE ????                    	    resw 1		
  2581 00006FB0 ????                    tss.LDTR:   resw 1
  2582 00006FB2 ????                    	    resw 1
  2583                                  ; tss offset 100		
  2584 00006FB4 ????                    	    resw 1		
  2585 00006FB6 ????                    tss.IOPB:   resw 1
  2586                                  ; tss offset 104 
  2587                                  tss_end:
  2588                                  
  2589 00006FB8 ????????                k_page_dir:  resd 1 ; Kernel's (System) Page Directory address
  2590                                  		    ;  (Physical address = Virtual address)	 	
  2591 00006FBC ????????                memory_size: resd 1 ; memory size in pages
  2592 00006FC0 ????????                free_pages:  resd 1 ; number of free pages		
  2593 00006FC4 ????????                next_page:   resd 1 ; offset value in M.A.T. for
  2594                                  		    ;   first free page search
  2595 00006FC8 ????????                last_page:   resd 1 ; offset value in M.A.T. which
  2596                                  		    ;   next free page search will be
  2597                                  		    ;   stopped after it. (end of M.A.T.)
  2598 00006FCC ????????                first_page:  resd 1 ;   offset value in M.A.T. which
  2599                                  		    ; first free page search
  2600                                  		    ;   will be started on it. (for user)
  2601 00006FD0 ????????                mat_size:    resd 1 ; Memory Allocation Table size in pages		
  2602                                  
  2603                                  ;;;
  2604                                  ; 02/09/2014 (Retro UNIX 386 v1)
  2605                                  ; 04/12/2013 (Retro UNIX 8086 v1)
  2606 00006FD4 ????                    CRT_START:   resw 1 	  ; starting address in regen buffer
  2607                                  			  ; NOTE: active page only
  2608 00006FD6 <res 10h>               cursor_posn: resw 8 	  ; cursor positions for video pages
  2609                                  active_page: 
  2610 00006FE6 ??                      ptty: 	     resb 1 	  ; current tty
  2611                                  ; 01/07/2015
  2612 00006FE7 ??                      ccolor:	     resb 1	  ; current color attributes ('sysmsg')	
  2613                                  ; 26/10/2015
  2614                                  ; 07/09/2014
  2615 00006FE8 <res 14h>               ttychr:      resw ntty+2  ; Character buffer (multiscreen)
  2616                                  
  2617                                  ; 21/08/2014
  2618 00006FFC ????????                tcount:	     resd 1
  2619                                  
  2620                                  ; 18/05/2015 (03/06/2013 - Retro UNIX 8086 v1 feature only!)
  2621 00007000 ????????                p_time:      resd 1     ; present time (for systime & sysmdate)
  2622                                  
  2623                                  ; 18/05/2015 (16/08/2013 - Retro UNIX 8086 v1 feature only !)
  2624                                  ; (open mode locks for pseudo TTYs)
  2625                                  ; [ major tty locks (return error in any conflicts) ]
  2626 00007004 <res 14h>               ttyl:        resw ntty+2 ; opening locks for TTYs.
  2627                                  
  2628                                  ; 15/04/2015 (Retro UNIX 386 v1)
  2629                                  ; 22/09/2013 (Retro UNIX 8086 v1)
  2630 00007018 <res Ah>                wlist:       resb ntty+2 ; wait channel list (0 to 9 for TTYs)
  2631                                  ; 15/04/2015 (Retro UNIX 386 v1)
  2632                                  ;; 12/07/2014 -> sp_init set comm. parameters as 0E3h
  2633                                  ;; 0 means serial port is not available 
  2634                                  ;;comprm: ; 25/06/2014
  2635 00007022 ??                      com1p:       resb 1  ;;0E3h
  2636 00007023 ??                      com2p:       resb 1  ;;0E3h
  2637                                  
  2638                                  ; 17/11/2015
  2639                                  ; request for response (from the terminal)	
  2640 00007024 ????                    req_resp:    resw 1 			
  2641                                  ; 07/11/2015
  2642 00007026 ??                      ccomport:    resb 1 ; current COM (serial) port
  2643                                  		    ; (0= COM1, 1= COM2)
  2644                                  ; 09/11/2015
  2645 00007027 ??                      comqr:	     resb 1 ; 'query or response' sign (u9.s, 'sndc')
  2646                                  ; 07/11/2015
  2647 00007028 ????                    rchar:	     resw 1 ; last received char for COM 1 and COM 2		
  2648 0000702A ????                    schar:	     resw 1 ; last sent char for COM 1 and COM 2
  2649                                  
  2650                                  ; 23/10/2015
  2651                                  ; SERIAL PORTS - COMMUNICATION MODES
  2652                                  ; (Retro UNIX 386 v1 feature only!)
  2653                                  ; 0 - command mode (default/initial mode)
  2654                                  ; 1 - terminal mode (Retro UNIX 386 v1 terminal, ascii chars)
  2655                                  ;;; communication modes for futre versions:  
  2656                                  ; // 2 - keyboard mode (ascii+scancode input)
  2657                                  ; // 3 - mouse mode
  2658                                  ; // 4 - device control (output) mode
  2659                                  ; VALID COMMANDS for current version:
  2660                                  ; 	'LOGIN'
  2661                                  ;  Login request: db 0FFh, 'LOGIN', 0 
  2662                                  ;	 ("Retro UNIX 386 v1 terminal requests login")
  2663                                  ;  Login response: db 0FFh, 'login', 0
  2664                                  ;	 ("login request accepted, wait for login prompt") 
  2665                                  ; When a login requests is received and acknowledged (by
  2666                                  ; serial port interrupt handler (communication procedure),
  2667                                  ; Retro UNIX 386 v1 operating system will start terminal mode
  2668                                  ; (login procedure) by changing comm. mode to 1 (terminal mode)
  2669                                  ; and then running 'etc/getty' for tty8 (COM1) or tty9 (COM2)
  2670                                  ; 
  2671                                  ; 'sys connect' system call is used to change communication mode
  2672                                  ; except 'LOGIN' command which is used to start terminal mode
  2673                                  ; by using (COM port) terminal.
  2674                                  
  2675                                  ;com1own:     resb 1 ; COM1 owner (u.uno)
  2676                                  ;com2own:     resb 1 ; COM2 owner (u.uno)
  2677                                  ;com1mode:    resb 1 ; communication mode for COM1
  2678                                  ;com1com:     resb 1 ; communication command for COM1
  2679                                  ;com2mode:    resb 1 ; communication mode for COM1
  2680                                  ;com2com      resb 1 ; communication command for COM1
  2681                                  ;com1cbufp:   resb 8 ; COM1 command buffer char pointer	
  2682                                  ;com2cbufp:   resb 8 ; COM2 command buffer char pointer	
  2683                                  ;com1cbuf:    resb 8 ; COM2 command buffer
  2684                                  ;com2cbuf:    resb 8 ; COM2 command buffer
  2685                                  
  2686                                  ; 22/08/2014 (RTC)
  2687                                  ; (Packed BCD)
  2688 0000702C ??                      time_seconds: resb 1
  2689 0000702D ??                      time_minutes: resb 1
  2690 0000702E ??                      time_hours:   resb 1
  2691 0000702F ??                      date_wday:    resb 1
  2692 00007030 ??                      date_day:     resb 1
  2693 00007031 ??                      date_month:   resb 1			
  2694 00007032 ??                      date_year:    resb 1
  2695 00007033 ??                      date_century: resb 1
  2696                                  
  2697                                  %include 'diskbss.s'	; UNINITIALIZED DISK (BIOS) DATA
  2698                              <1> ; Retro UNIX 386 v1 Kernel - DISKBSS.INC
  2699                              <1> ; Last Modification: 10/07/2015
  2700                              <1> ;	(Unitialized Disk Parameters Data section for 'DISKIO.INC') 
  2701                              <1> ;
  2702                              <1> ; *****************************************************************************
  2703                              <1> 
  2704                              <1> alignb 2
  2705                              <1> 
  2706                              <1> ;----------------------------------------
  2707                              <1> ;	TIMER DATA AREA 		:
  2708                              <1> ;----------------------------------------
  2709                              <1> 
  2710                              <1> TIMER_LH:	; 16/02/2015
  2711 00007034 ????                <1> TIMER_LOW:      resw	1               ; LOW WORD OF TIMER COUNT
  2712 00007036 ????                <1> TIMER_HIGH:     resw	1               ; HIGH WORD OF TIMER COUNT
  2713 00007038 ??                  <1> TIMER_OFL:      resb 	1               ; TIMER HAS ROLLED OVER SINCE LAST READ
  2714                              <1> 
  2715                              <1> ;----------------------------------------
  2716                              <1> ;	DISKETTE DATA AREAS		:
  2717                              <1> ;----------------------------------------
  2718                              <1> 
  2719 00007039 ??                  <1> SEEK_STATUS:	resb	1
  2720 0000703A ??                  <1> MOTOR_STATUS:	resb	1
  2721 0000703B ??                  <1> MOTOR_COUNT:	resb	1
  2722 0000703C ??                  <1> DSKETTE_STATUS:	resb	1
  2723 0000703D ??????????????      <1> NEC_STATUS:	resb	7
  2724                              <1> 
  2725                              <1> ;----------------------------------------
  2726                              <1> ;	ADDITIONAL MEDIA DATA		:
  2727                              <1> ;----------------------------------------
  2728                              <1> 
  2729 00007044 ??                  <1> LASTRATE:	resb 	1
  2730 00007045 ??                  <1> HF_STATUS:	resb 	1
  2731 00007046 ??                  <1> HF_ERROR:	resb 	1
  2732 00007047 ??                  <1> HF_INT_FLAG:	resb 	1
  2733 00007048 ??                  <1> HF_CNTRL:	resb 	1
  2734 00007049 ????????            <1> DSK_STATE:	resb 	4
  2735 0000704D ????                <1> DSK_TRK:	resb 	2
  2736                              <1> 
  2737                              <1> ;----------------------------------------
  2738                              <1> ;	FIXED DISK DATA AREAS		:
  2739                              <1> ;----------------------------------------
  2740                              <1> 
  2741 0000704F ??                  <1> DISK_STATUS1:	resb 	1		; FIXED DISK STATUS
  2742 00007050 ??                  <1> HF_NUM:		resb 	1		; COUNT OF FIXED DISK DRIVES
  2743 00007051 ??                  <1> CONTROL_BYTE:	resb 	1		; HEAD CONTROL BYTE
  2744                              <1> ;@PORT_OFF	resb	1		; RESERVED (PORT OFFSET)
  2745                              <1> ;port1_off	resb	1		; Hard disk controller 1 - port offset
  2746                              <1> ;port2_off	resb	1		; Hard idsk controller 2 - port offset
  2747                              <1> 
  2748 00007052 ????                <1> alignb 4
  2749                              <1> 
  2750                              <1> ;HF_TBL_VEC:	resd	1 		; Primary master disk param. tbl. pointer
  2751                              <1> ;HF1_TBL_VEC:	resd	1		; Primary slave disk param. tbl. pointer
  2752                              <1> HF_TBL_VEC: ; 22/12/2014	
  2753 00007054 ????????            <1> HDPM_TBL_VEC:	resd	1 		; Primary master disk param. tbl. pointer
  2754 00007058 ????????            <1> HDPS_TBL_VEC:	resd	1		; Primary slave disk param. tbl. pointer
  2755 0000705C ????????            <1> HDSM_TBL_VEC:	resd	1 		; Secondary master disk param. tbl. pointer
  2756 00007060 ????????            <1> HDSS_TBL_VEC:	resd	1		; Secondary slave disk param. tbl. pointer
  2757                              <1> 
  2758                              <1> ; 03/01/2015
  2759 00007064 ??                  <1> LBAMode:     	resb	1
  2760                              <1> 
  2761                              <1> ; *****************************************************************************
  2698                                  
  2699                                  ;;; Real Mode Data (10/07/2015 - BSS)
  2700                                  
  2701                                  ;alignb 2
  2702                                  
  2703                                  ; 02/01/2022
  2704                                  ;%include 'ux.s' ; 12/04/2015 (unix system/user/process data)
  2705                                  
  2706                                  ; 17/04/2021
  2707                                  ; (memory page swap parameters are disabled as temporary)
  2708                                  ;
  2709                                  ;; Memory (swap) Data (11/03/2015)
  2710                                  ; 09/03/2015
  2711                                  ;swpq_count: resw 1 ; count of pages on the swap que
  2712                                  ;swp_drv:    resd 1 ; logical drive description table address of the swap drive/disk
  2713                                  ;swpd_size:  resd 1 ; size of swap drive/disk (volume) in sectors (512 bytes).	
  2714                                  ;swpd_free:  resd 1 ; free page blocks (4096 bytes) on swap disk/drive (logical)
  2715                                  ;swpd_next:  resd 1 ; next free page block
  2716                                  ;swpd_last:  resd 1 ; last swap page block	
  2717                                  
  2718 00007065 ??????                  alignb 4
  2719                                  
  2720                                  ; 10/07/2015
  2721                                  ; 28/08/2014
  2722 00007068 ????????                error_code:	resd 1
  2723                                  ; 29/08/2014
  2724 0000706C ????????                FaultOffset:	resd 1
  2725                                  ; 21/09/2015
  2726 00007070 ????????                PF_Count:	resd 1	; total page fault count
  2727                                  		       	; (for debugging - page fault analyze)
  2728                                  		 	; 'page _fault_handler' (memory.s)
  2729                                  			; 'sysgeterr' (u9.s)
  2730                                  ;; 21/08/2015
  2731                                  ;;buffer: resb (nbuf*520) ;; sysdefs.s, ux.s
  2732                                  
  2733                                  ; 02/01/2022
  2734                                  %include 'ux.s' ; 12/04/2015 (unix system/user/process data)
  2735                              <1> ; Temporary Runix kernel v2.0 file for debug - 22/11/2021
  2736                              <1> ; (re-write kernel for test by using previous version without a major defect)
  2737                              <1> ; ****************************************************************************
  2738                              <1> ; Retro UNIX 386 v1.2 Kernel - ux.s
  2739                              <1> ; Last Modification: 12/01/2022
  2740                              <1> ;
  2741                              <1> ; ///////// RETRO UNIX 386 V1 SYSTEM DEFINITIONS ///////////////
  2742                              <1> ; (Modified from 
  2743                              <1> ;	Retro UNIX 8086 v1 system definitions in 'UNIX.ASM', 01/09/2014)
  2744                              <1> ; ((UNIX.ASM (RETRO UNIX 8086 V1 Kernel), 11/03/2013 - 01/09/2014))
  2745                              <1> ; ----------------------------------------------------------------------------
  2746                              <1> ; Derived from UNIX Operating System (v1.0 for PDP-11) 
  2747                              <1> ; (Original) Source Code by Ken Thompson (1971-1972)
  2748                              <1> ; <Bell Laboratories (17/3/1972)>
  2749                              <1> ; <Preliminary Release of UNIX Implementation Document>
  2750                              <1> ; (Section E10 (17/3/1972) - ux.s)
  2751                              <1> ; ****************************************************************************
  2752                              <1> 
  2753                              <1> ; 22/11/2021 (Modification in Runix v1.1 'ux.s' for runix v2 fs compatibility)
  2754                              <1> ; 18/07/2021
  2755                              <1> ; 15/07/2021
  2756                              <1> ; 12/05/2021
  2757                              <1> ; 09/05/2021
  2758                              <1> ; 06/05/2021
  2759                              <1> ; 02/05/2021
  2760                              <1> ; 26/01/2020 (NASM version of SuperBlock structure in UNIXHDCP.ASM)
  2761                              <1> 
  2762                              <1> ; 21/12/2019 (UNIXCOPY.COM, UNIXCOPY.ASM, UNIXHDCP.COM, UNIXHDCP.ASM)
  2763                              <1> ; 19/12/2019 (UNIXHDFS.COM, RUFSHDI.ASM)
  2764                              <1> ; 01/09/2019 - Retro UNIX 386 v2 SuperBlock
  2765                              <1> 
  2766                              <1> ; 14/01/2020 - Super Block modification:
  2767                              <1> ;	     - Extended sections/divisions (consequental sectors)
  2768                              <1> ;	     - (for swapping, configuration, boot space etc.)	
  2769                              <1> 
  2770                              <1> ; 26/01/2020 (unix386.s, ux.s)
  2771                              <1> ; 14/01/2020
  2772                              <1> SB.HiddenSects	equ SB.BootSectAddr
  2773                              <1> SB.TotalSects	equ SB.VolumeSize
  2774                              <1> 
  2775                              <1> ; 11/05/2021 (Retro UNIX 386 v2)
  2776                              <1> ; Super block status byte (byte 0 of SB.status dword)
  2777                              <1> ; 	bit 0 - superblock modified flag
  2778                              <1> ; 	bit 1 - inode map modified flag
  2779                              <1> ; 	bit 2 - free blocks map modified flag
  2780                              <1> ; 	bit 3 - inode table modified flag
  2781                              <1> ;; 	bit 4 - boot sector modified flag ; 17/08/2021
  2782                              <1> ; 17/08/2021
  2783                              <1> ;	bit 4 - file data write error (for SB.LastInode)
  2784                              <1> ;	bit 5 - inode table write error (for SB.LastInode)
  2785                              <1> ;	bit 6 - inode map write error (for SB.LastInode)
  2786                              <1> ;	bit 7 - free blocks map write error (for SB.LastInode)
  2787                              <1> 	
  2788                              <1> ; 21/12/2019
  2789                              <1> ; 19/12/2019 - Retro UNIX 386 v2 HD (071h) partition boot sector 
  2790                              <1> ;	       (UNIXHDFS.ASM)
  2791                              <1> ; 04/12/2015 (14 byte file names - Retro UNIX 386 v1.1)
  2792                              <1> ; 14/07/2015 (8 byte file names - Retro UNIX 8086 v1 & Retro UNIX 386 v1.0)
  2793                              <1> 
  2794                              <1> bsFSystemID 	equ 2  ; db 'RUFS'	
  2795                              <1> bsVolumeSerial 	equ 6  ; dd 0 ; (4 bytes)
  2796                              <1> bsFDSign	equ 10 ; db 'fd'
  2797                              <1> bsDriveNumber 	equ 12 ; db 0 ; fd0 or fd1 (0 or 1)
  2798                              <1> bsReserved 	equ 13 ; db 0 ; (512 bytes per sector)	
  2799                              <1> bsSecPerTrack	equ 14 ; db 18 ; (9 or 15)	
  2800                              <1> bsHeads		equ 15 ; db  ; 2
  2801                              <1> bsTracks	equ 16 ; dw 80 ; bsCylinders
  2802                              <1> bs_bf_inode_number equ 18 ; dw 0 ; 0 or Boot/Startup File I-Number
  2803                              <1> bsInfoEndsign	equ 20 ; db '@'
  2804                              <1> ; 21/12/2019
  2805                              <1> bsMagic		equ 20 ; db '@'
  2806                              <1> bsPartitionID	equ 21 ; db 0 ; db 71h
  2807                              <1> bsHiddenSects	equ 22 ; dd 0 ; Hidden sectors (Boot Sector LBA)
  2808                              <1> 
  2809                              <1> ; 22/11/2021 - Retro UNIX v2 I-node Flags
  2810                              <1> ; ------------------------------------------------------------------
  2811                              <1> ; UINSTALL.ASM (included in UNIXFDFS.ASM) - 23/01/2020
  2812                              <1> ; ------------------------------------------------------------------
  2813                              <1> 
  2814                              <1> ; Retro UNIX 386 v2 I-node Flags: (di_mode) for files
  2815                              <1> ; 1000000000000000b 	IFREG - 1 = regular file (8000h)
  2816                              <1> ; 0100000000000000b	IFDIR - 1 = directory (4000h)
  2817                              <1> ; 0010000000000000b	IRSVD - 0 = reserved bit (2000h) ; Mounted flag
  2818                              <1> ; 0001000000000000b	ILARG - large file addressing bit (1000h)
  2819                              <1> ; 0000100000000000b	ISUID - set user id on exec (800h)
  2820                              <1> ; 0000010000000000b	ISGID - set group id on exec (400h)
  2821                              <1> ; 0000001000000000b	IEXTT - 1 = use extents (200h)
  2822                              <1> ; 0000000100000000b	IREAD - read, owner (100h)
  2823                              <1> ; 0000000010000000b	IWRITE - write, owner (80h)
  2824                              <1> ; 0000000001000000b	IEXEC - execute, owner (40h)
  2825                              <1> ; 0000000000100000b	read, group (20h)
  2826                              <1> ; 0000000000010000b	write, group (10h)
  2827                              <1> ; 0000000000001000b	execute, group (08h)
  2828                              <1> ; 0000000000000100b	read, others (04h)
  2829                              <1> ; 0000000000000010b	write, others (02h)
  2830                              <1> ; 0000000000000001b	execute, others (01h)
  2831                              <1> 
  2832                              <1> ; Retro UNIX 386 v2 I-node Flags: (di_mode) for devices
  2833                              <1> ; 1000000000000000b 	IFREG - 0 = device file (8000h)
  2834                              <1> ; 0100000000000000b	IFBLK - 1 = block device (4000h)
  2835                              <1> ; 0010000000000000b	IFCHR - character special (2000h) -always 1-
  2836                              <1> ; 0001000000000000b	IFIFO - fifo special (1000h)
  2837                              <1> ; 0000100000000000b	IPIPE - pipe special (800h) ; 07/02/2020
  2838                              <1> ; 0000010000000000b	IREDIR - redirected (400h)  ; 07/02/2020
  2839                              <1> ; 0000001000000000b	IEXTR - 1 = external device driver (200h)
  2840                              <1> ; 0000000100000000b	IREAD - read, owner (100h)
  2841                              <1> ; 0000000010000000b	IWRITE - write, owner (80h)
  2842                              <1> ; 0000000001000000b	IEXEC - execute, owner (40h)
  2843                              <1> ; 0000000000100000b	read, group (20h)
  2844                              <1> ; 0000000000010000b	write, group (10h)
  2845                              <1> ; 0000000000001000b	execute, group (08h)
  2846                              <1> ; 0000000000000100b	read, others (04h)
  2847                              <1> ; 0000000000000010b	write, others (02h)
  2848                              <1> ; 0000000000000001b	execute, others (01h)
  2849                              <1> 
  2850                              <1> ; 10/01/2022 - Retro UNIX 386 v1.2
  2851                              <1> ; (SB structure has been moved from here to 'sysdefs.s'
  2852                              <1> ;	to overcome NASM's bss addressing bug !!!)
  2853                              <1> ; (('ux.s' bss section addresses are being overlapped
  2854                              <1> ;    when SB structure is defined in 'ux.s'))
  2855                              <1> 
  2856                              <1> %if 0
  2857                              <1> 
  2858                              <1> struc SB ; SuperBlock
  2859                              <1> 
  2860                              <1> .Header:	resd 1
  2861                              <1> ;.HiddenSects:
  2862                              <1> .BootSectAddr:	resd 1	; Hidden Sectors
  2863                              <1> ;.TotalSects:
  2864                              <1> .VolumeSize:	resd 1	; Entire Volume/Partition Size (includes ext. volume)
  2865                              <1> .Version:	resd 1	
  2866                              <1> .BlockSize:	resd 1	
  2867                              <1> .InodeCount:	resd 1	
  2868                              <1> .FreeMapAddr:	resd 1	
  2869                              <1> .FreeMapSize:	resd 1	
  2870                              <1> .InodeMapAddr:	resd 1	
  2871                              <1> .InodeMapSize:	resd 1	
  2872                              <1> .InodeTblAddr:	resd 1	
  2873                              <1> .InodeTblSize:	resd 1	
  2874                              <1> .FreeInodes:	resd 1	
  2875                              <1> .FirstFreeIno:	resd 1	
  2876                              <1> .FreeBlocks:	resd 1	
  2877                              <1> .FirstFreeBlk:	resd 1	
  2878                              <1> .BootSecParms:	resb 19	; v1
  2879                              <1> .BSExtension:	resb 5	; v2 HDFS
  2880                              <1> .Status:	resb 1	; 12/05/2021 (system modification status) (*)
  2881                              <1> .Pdrv:		resb 1  ; Physical disk number (index) ; 12/05/2021 (*) 
  2882                              <1> .Uno:		resw 1	; user/process number ; 12/05/2021 (*)
  2883                              <1> .ModifTime:	resd 1	; (last) modification time (*)
  2884                              <1> .ExtdVolTbl:	resd 1	; Extended Volume Start/Table Address
  2885                              <1> .ExtdVolSize:	resd 1	; Extended Volume (swap section etc.) Size	
  2886                              <1> .LBA_rw:	resb 1
  2887                              <1> .ClusterSize:	resb 1
  2888                              <1> .ReadOnly:	resb 1	; (SB will not be written to disk if bit 0 is 1)
  2889                              <1> .Mounted:	resb 1
  2890                              <1> .MountInode:	resd 1  ; double word
  2891                              <1> .DevMajor:	resb 1
  2892                              <1> .DevMinor:	resb 1
  2893                              <1> .LongName:	resb 1
  2894                              <1> .Direntry32:	resb 1
  2895                              <1> ; 18/07/2021
  2896                              <1> .FileBuffer:	resd 1
  2897                              <1> .ItabBuffer:	resd 1
  2898                              <1> .ImapBuffer:	resd 1
  2899                              <1> .FmapBuffer:	resd 1
  2900                              <1>  ; 15/07/2021
  2901                              <1> .LastInode:	resd 1
  2902                              <1> ; 02/05/2021
  2903                              <1> .FmapIndex:	resd 1
  2904                              <1> .ImapIndex:	resd 1
  2905                              <1> .ItableIndex:	resd 1
  2906                              <1> .Reserved:	resb 508-148 ; 18/07/2021
  2907                              <1> .Footer:	resd 1
  2908                              <1> 
  2909                              <1> endstruc
  2910                              <1> 
  2911                              <1> %endif
  2912                              <1> 
  2913                              <1> alignb 2
  2914                              <1> 
  2915                              <1> inode:
  2916                              <1> 	;; 11/03/2013. 
  2917                              <1> 	;;Derived from UNIX v1 source code 'inode' structure (ux).
  2918                              <1> 	;;i.
  2919                              <1> 	;
  2920                              <1> 	;i.flgs: resw 1
  2921                              <1> 	;i.nlks: resb 1
  2922                              <1> 	;i.uid:	 resb 1
  2923                              <1>         ;i.size: resw 1 ; size
  2924                              <1> 	;i.dskp: resw 8 ; 16 bytes
  2925                              <1> 	;i.ctim: resd 1
  2926                              <1> 	;i.mtim: resd 1
  2927                              <1> 	;i.rsvd: resw 1 ; Reserved (ZERO/Undefined word for UNIX v1)
  2928                              <1> 
  2929                              <1> 	; 26/01/2020
  2930                              <1> 	; Retro UNIX 386 v2.0 - Modified UNIX v7 inode model
  2931                              <1> 	;	(15/09/2029 .. 18/12/2019)
  2932                              <1> 
  2933 00007074 ????                <1> 	i.flgs:   resw 1	; /* mode and type of file */
  2934 00007076 ????                <1> 	i.nlks:	  resw 1	; /* number of links to file */
  2935 00007078 ????                <1> 	i.uid:	  resw 1	; /* owner's user id */  - 0 to 655535 -
  2936 0000707A ??                  <1> 	i.gid:	  resb 1	; /* owner's group id */ - o to 255 -
  2937 0000707B ??                  <1> 	i.size_h: resb 1	; /* number of bytes in file */ ; byte 5
  2938 0000707C ????????            <1> 	i.size:	  resd 1 ; size	; /* number of bytes in file */
  2939 00007080 <res 28h>           <1> 	i.dskp:	  resd 10 ; 40 bytes ; /* disk block addresses */
  2940 000070A8 ????????            <1> 	i.atim:	  resd 1	; /* time last accessed */
  2941 000070AC ????????            <1> 	i.mtim:	  resd 1	; /* time last modified */
  2942 000070B0 ????????            <1> 	i.ctim:	  resd 1	; /* time created */
  2943                              <1> 
  2944                              <1> I_SIZE	equ $ - inode
  2945                              <1> 
  2946                              <1> process:
  2947                              <1> 	; 12/01/2022 (Retro UNIX 386 v1.2) 
  2948                              <1> 	; 06/05/2015
  2949                              <1> 	; 11/03/2013 - 05/02/2014
  2950                              <1> 	;Derived from UNIX v1 source code 'proc' structure (ux).
  2951                              <1> 	;p.
  2952                              <1> 	
  2953 000070B4 <res 20h>           <1>         p.pid:   resw nproc
  2954 000070D4 <res 20h>           <1>         p.ppid:  resw nproc
  2955                              <1> 	;p.break: resw nproc ; 12/01/2022 (p.break is not used)
  2956 000070F4 <res 10h>           <1>         p.ttyc:  resb nproc ; console tty in Retro UNIX 8086 v1.
  2957 00007104 <res 10h>           <1> 	p.waitc: resb nproc ; waiting channel in Retro UNIX 8086 v1.
  2958 00007114 <res 10h>           <1> 	p.link:	 resb nproc
  2959 00007124 <res 10h>           <1> 	p.stat:	 resb nproc
  2960                              <1> 
  2961                              <1> 	; 06/05/2015 (Retro UNIX 386 v1 feature only !) 
  2962 00007134 <res 40h>           <1> 	p.upage: resd nproc ; Physical address of the process's
  2963                              <1> 			    ; 'user' structure	
  2964                              <1> 
  2965                              <1> P_SIZE	equ $ - process
  2966                              <1> 
  2967                              <1> ; fsp table (original UNIX v1)
  2968                              <1> ;
  2969                              <1> ;Entry
  2970                              <1> ;          15                                      0
  2971                              <1> ;  1     |---|---------------------------------------|
  2972                              <1> ;        |r/w|       i-number of open file           |
  2973                              <1> ;        |---|---------------------------------------| 
  2974                              <1> ;        |               device number               |
  2975                              <1> ;        |-------------------------------------------|
  2976                              <1> ;    (*) | offset pointer, i.e., r/w pointer to file |
  2977                              <1> ;        |-------------------------------------------| 
  2978                              <1> ;        |  flag that says    | number of processes  |
  2979                              <1> ;        |   file deleted     | that have file open  |
  2980                              <1> ;        |-------------------------------------------| 
  2981                              <1> ;  2     |                                           |
  2982                              <1> ;        |-------------------------------------------| 
  2983                              <1> ;        |                                           |
  2984                              <1> ;        |-------------------------------------------|
  2985                              <1> ;        |                                           |
  2986                              <1> ;        |-------------------------------------------|
  2987                              <1> ;        |                                           |
  2988                              <1> ;        |-------------------------------------------| 
  2989                              <1> ;  3     |                                           | 
  2990                              <1> ;        |                                           |  
  2991                              <1> ;
  2992                              <1> ; (*) Retro UNIX 386 v1 modification: 32 bit offset pointer 
  2993                              <1> 
  2994                              <1> ; 27/03/2020 - Retro UNIX 386 v2 - FSP (OPEN FILES) TABLE 
  2995                              <1> 
  2996                              <1> ;Entry
  2997                              <1> ;         15                    7                   0
  2998                              <1> ;  1     |-------------------------------------------|
  2999                              <1> ;        |   	     i-number of open file           |
  3000                              <1> ;        |-------------------------------------------| 
  3001                              <1> ;        |        high word of 32 bit i-number       |
  3002                              <1> ;        |-------------------------------------------|
  3003                              <1> ;        | open mode & status  |   device number     |
  3004                              <1> ;        |-------------------------------------------|
  3005                              <1> ;        |    reserved byte    |     open count      |
  3006                              <1> ;        |-------------------------------------------| 
  3007                              <1> ;        | offset pointer, i.e., r/w pointer to file |
  3008                              <1> ;        |-------------------------------------------|
  3009                              <1> ;        |   64 bit file offset pointer (bit 16-31)  | 
  3010                              <1> ;        |-------------------------------------------|
  3011                              <1> ;        |   64 bit file offset pointer (bit 32-47)  | 
  3012                              <1> ;        |-------------------------------------------|
  3013                              <1> ;        |   64 bit file offset pointer (bit 48-63)  | 
  3014                              <1> ;        |-------------------------------------------|
  3015                              <1> ;  2     |                                           |
  3016                              <1> ;        |-------------------------------------------| 
  3017                              <1> ;        |                                           |
  3018                              <1> ;        |-------------------------------------------|
  3019                              <1> ;        |                                           |
  3020                              <1> ;        |-------------------------------------------|
  3021                              <1> ;        |                                           |
  3022                              <1> ;        |-------------------------------------------| 
  3023                              <1> ;        |                                           | 
  3024                              <1> 
  3025                              <1> ; 10/01/2022 - Retro UNIX 386 v1.2
  3026                              <1> ; (file structure has been moved from here to 'sysdefs.s'
  3027                              <1> ;	to overcome NASM's bss addressing bug !!!)
  3028                              <1> ; (('ux.s' bss section addresses are being overlapped
  3029                              <1> ;    when file file structure is defined in 'ux.s'))
  3030                              <1> 
  3031                              <1> %if 0
  3032                              <1> 
  3033                              <1> ; 22/11/2021
  3034                              <1> ; 21/07/2021 - Retro UNIX 386 v2 open file structure revision
  3035                              <1> 
  3036                              <1> struc file	; open files (fsp) structure
  3037                              <1>   .inode:  resw 1  ; inode number of open file (32 bit)
  3038                              <1>   .i32:	   resw 1  ; higher word of inode number (reserved)
  3039                              <1>   .drive:  resb 1  ; logical drive (disk) number
  3040                              <1>   .flags:  resb 1  ; open mode and status
  3041                              <1>   .count:  resb 1  ; number of processes that have file open
  3042                              <1>   ;.rsvd:  resb 1  ; reserved byte (for next versions)
  3043                              <1>   .mnt:    resb 1  ; mnttab index+1 (0 = not mounted)
  3044                              <1>   .offset: resd 1  ; file offset/pointer (64 bit) 
  3045                              <1>   .o64:	   resd 1  ; higher 32 bit of file offset
  3046                              <1>  .size:  ; = 16		
  3047                              <1> endstruc
  3048                              <1> 
  3049                              <1> %endif
  3050                              <1> 
  3051                              <1> ; 01/01/2022
  3052                              <1> ; 22/11/2021
  3053                              <1> ;fp.size equ file.size
  3054                              <1> 
  3055                              <1> ; 02/01/2022
  3056 00007174 <res 320h>          <1> fsp:	resb nfiles*16 ; (NFILES*fp.size)
  3057                              <1> ; 01/01/2022
  3058                              <1> ; 22/11/2021 (16/05/2021)
  3059                              <1> ;fsp:	resb NFILES*16 ; (NFILES*fp.size)
  3060                              <1> ; 15/04/2015
  3061                              <1> ;fsp:	resb nfiles*10 ; 11/05/2015 (8 -> 10)
  3062                              <1> 
  3063 00007494 <res 20h>           <1> bufp:	resd (nbuf+2) ; will be initialized 
  3064 000074B4 ????????            <1> ii:	resd 1 ; 22/11/2021 ; 32 bit inode number (high word is 0)
  3065                              <1> ; 22/11/2021
  3066 000074B8 ??                  <1> idev:	resb 1 ; logical drive number of current inode, [ii]
  3067 000074B9 ??                  <1> cdev:	resb 1 ; current logical drive number for current user
  3068                              <1> 
  3069                              <1> ; 18/05/2015
  3070                              <1> ; 26/04/2013 device/drive parameters (Retro UNIX 8086 v1 feature only!)
  3071                              <1> ; 'UNIX' device numbers (as in 'cdev' and 'u.cdrv')
  3072                              <1> ;	0 -> root device (which has Retro UNIX 8086 v1 file system)
  3073                              <1> ; 	1 -> mounted device (which has Retro UNIX 8086 v1 file system)
  3074                              <1> ; 'Retro UNIX 8086 v1' device numbers: (for disk I/O procedures)
  3075                              <1> ;	0 -> fd0 (physical drive, floppy disk 1), physical drive number = 0
  3076                              <1> ;	1 -> fd1 (physical drive, floppy disk 2), physical drive number = 1
  3077                              <1> ;	2 -> hd0 (physical drive, hard disk 1), physical drive number = 80h
  3078                              <1> ;	3 -> hd1 (physical drive, hard disk 2), physical drive number = 81h
  3079                              <1> ;	4 -> hd2 (physical drive, hard disk 3), physical drive number = 82h
  3080                              <1> ;	5 -> hd3 (physical drive, hard disk 4), physical drive number = 83h
  3081 000074BA ??                  <1> rdev:	 resb 1 ; root device number ; Retro UNIX 8086 v1 feature only!
  3082                              <1> 	        ; as above, for physical drives numbers in following table
  3083 000074BB ??                  <1> mdev:	 resb 1 ; mounted device number ; Retro UNIX 8086 v1 feature only!
  3084                              <1> ; 15/04/2015
  3085 000074BC ??                  <1> active:	 resb 1 
  3086 000074BD ??                  <1> 	 resb 1 ; 09/06/2015
  3087 000074BE ????                <1> mpid:	 resw 1
  3088                              <1> ; 22/11/2021 (32 bit inode numbers)
  3089 000074C0 ????????            <1> rootdir: resd 1
  3090 000074C4 ????????            <1> mnti:	 resd 1
  3091                              <1> 
  3092                              <1> ; 14/02/2014
  3093                              <1> ; Major Modification: Retro UNIX 8086 v1 feature only!
  3094                              <1> ;		      Single level run queue
  3095                              <1> ;		      (in order to solve sleep/wakeup lock)
  3096 000074C8 ????                <1> runq:	resw 1
  3097 000074CA ??                  <1> imod:	resb 1
  3098 000074CB ??                  <1> imodx:	resb 1 ; 09/01/2022 - Retro UNIX 386 v1.2
  3099 000074CC ??                  <1> smod:	resb 1
  3100 000074CD ??                  <1> mmod:	resb 1
  3101                              <1> ;	resb 1 ; 09/01/2022 
  3102 000074CE ??                  <1> sysflg:	resb 1
  3103                              <1> 
  3104 000074CF ??                  <1> alignb 4
  3105                              <1> 
  3106                              <1> user:
  3107                              <1> 	; 01/01/2022
  3108                              <1> 	; 04/12/2021 - Retro UNIX 386 v1.2
  3109                              <1> 	; 24/10/2021
  3110                              <1> 	; 18/10/2021
  3111                              <1> 	; 10/06/2021
  3112                              <1> 	; 30/05/2021
  3113                              <1> 	; 29/05/2021
  3114                              <1> 	; 21/05/2021
  3115                              <1> 	; 20/05/2021
  3116                              <1> 	; 16/05/2021
  3117                              <1> 	; 01/05/2021
  3118                              <1> 	; 27/03/2021
  3119                              <1> 	; 17/04/2020, 28/04/2020
  3120                              <1> 	; 25/03/2020, 28/03/2020
  3121                              <1> 	; 20/03/2020, 22/03/2020, 23/03/2020
  3122                              <1> 	; 05/03/2020, 08/03/2020, 14/03/2020
  3123                              <1> 	; 07/02/2020 - Retro UNIX 386 v2
  3124                              <1> 	;
  3125                              <1> 	; 27/02/2017 - TRDOS 386
  3126                              <1> 	; 13/01/2017 - TRDOS 386
  3127                              <1> 	; 10/01/2017 - TRDOS 386
  3128                              <1> 	; 19/12/2016 - TRDOS 386	
  3129                              <1> 	; 21/05/2016 - TRDOS 386 (TRDOS v2.0) 
  3130                              <1> 	; 	       [u.pri] usage method modification
  3131                              <1> 	;
  3132                              <1> 	; 04/12/2015 - Retro UNIX 386 v1.1 (14 byte file/directory names)
  3133                              <1> 	; 18/10/2015
  3134                              <1> 	; 12/10/2015
  3135                              <1> 	; 21/09/2015
  3136                              <1> 	; 24/07/2015
  3137                              <1> 	; 16/06/2015
  3138                              <1> 	; 09/06/2015
  3139                              <1> 	; 11/05/2015
  3140                              <1> 	; 16/04/2015 (Retro UNIX 386 v1 - 32 bit modifications)
  3141                              <1> 	; 10/10/2013
  3142                              <1> 	; 11/03/2013. 
  3143                              <1> 	;Derived from UNIX v1 source code 'user' structure (ux).
  3144                              <1> 	;u.
  3145                              <1> 
  3146 000074D0 ????????            <1> 	u.sp:	  resd 1 ; esp (kernel stack at the beginning of 'sysent')
  3147 000074D4 ????????            <1> 	u.usp:	  resd 1 ; esp (kernel stack points to user's registers)
  3148 000074D8 ????????            <1> 	u.r0:	  resd 1 ; eax
  3149 000074DC ????                <1> 	u.cdir:	  resw 1
  3150 000074DE ????                <1> 		  resw 1 ; 28/03/2020 - reserved for 32 bit inode number
  3151                              <1> 	;u.cdrv:  resw 1 ; 17/04/2020 (dword alignment) 
  3152 000074E0 ??                  <1> 	u.cdrv:   resb 1 ; 01/05/2021
  3153 000074E1 ??                  <1> 		  resb 1 ; 01/05/2021 (dword alignment)
  3154 000074E2 <res Ah>            <1> 	u.fp:	  resb 10 ; Retro UNIX 386 v1
  3155                              <1> 	;u.fp:	  resb OPENFILES ; Retro UNIX 386 v2 ; 28/03/2020 
  3156                              <1> 	u.fsp:	  ; 16/05/2021
  3157 000074EC ????????            <1> 	u.fofp:	  resd 1 ; 16/05/2021 (pointer to fsp entry)
  3158 000074F0 ????????            <1> 	u.dirp:	  resd 1
  3159 000074F4 ????????            <1> 	u.namep:  resd 1
  3160 000074F8 ????????            <1> 	u.off:	  resd 1
  3161                              <1> 	;	  resd 1 ; 08/03/2020 - Retro UNIX 386 v2 - 64 bit fptr
  3162 000074FC ????????            <1> 	u.base:	  resd 1
  3163 00007500 ????????            <1> 	u.count:  resd 1
  3164 00007504 ????????            <1> 	u.nread:  resd 1
  3165 00007508 ????????            <1> 	u.break:  resd 1 ; break
  3166                              <1> 	; 16/05/2021 (Retro UNIX 386 v2)
  3167 0000750C ??                  <1> 	u.mode:   resb 1 ; 16/05/2021 (sysread, syswrite, 'rdwr' file mode)
  3168                              <1> 	; 10/01/2017 (TRDOS 386, relocation and dword alignment)
  3169                              <1> 	; tty number (rtty, rcvt, wtty)
  3170 0000750D ??                  <1> 	u.ttyn:	  resb 1 ; 28/07/2013 - Retro Unix 8086 v1 feature only !
  3171 0000750E ????                <1> 	u.ttyp:	  resw 1 
  3172 00007510 <res 10h>           <1> 	u.dirbuf: resb 16 ; 04/12/2015 (10 -> 16) 
  3173                              <1> 	;u.pri:	  resw 1 ; 14/02/2014
  3174 00007520 ??                  <1> 	u.quant:  resb 1 ; Retro UNIX 8086 v1 Feature only ! (uquant)
  3175 00007521 ??                  <1> 		  resb 1 ; 17/04/2020
  3176 00007522 ??                  <1> 	u.pri:	  resb 1 ; Modification: 21/05/2016 (priority levels: 0, 1, 2)
  3177 00007523 ??                  <1> 		  resb 1 ; 17/04/2020
  3178                              <1> 	; 10/06/2021
  3179                              <1> 	;u.signal: resw 1 ; 21/05/2021 - Retro UNIX 386 v2
  3180                              <1> 	;	  resw 1 ; reserved ; 21/05/2021	
  3181 00007524 ????                <1> 	u.intr:	  resw 1
  3182 00007526 ????                <1> 	u.quit:	  resw 1
  3183                              <1> 	;u.emt:	  resw 1 ; 10/10/2013
  3184                              <1> 	;u.ilgins: resw 1 ; 10/01/2017
  3185                              <1> 	;u.cdrv:  resw 1 ; cdev ; 17/04/2020 (moved to up)
  3186                              <1> 	;u.uid:	  resb 1 ; uid
  3187                              <1> 	;u.ruid:  resb 1
  3188 00007528 ??                  <1> 	u.bsys:	  resb 1
  3189 00007529 ??                  <1> 	u.uno:	  resb 1
  3190 0000752A ????                <1>         u.uid:	  resw 1 ; uid	; 27/03/2021 - Retro UNIX 386 v2
  3191 0000752C ????                <1> 	u.ruid:	  resw 1	; 16 bit uid
  3192 0000752E ??                  <1> 	u.gid:	  resb 1 ; gid 	; 27/03/2021 - Retro UNIX 386 v2
  3193 0000752F ??                  <1> 	u.rgid:	  resb 1
  3194                              <1> 	; 20/05/2021 - Retro UNIX 386 v2
  3195 00007530 ????????            <1> 	u.procp:  resd 1 ; /* pointer to proc structure */		
  3196 00007534 ????????            <1> 	u.upage:  resd 1 ; 16/04/2015 - Retro Unix 386 v1 feature only !
  3197 00007538 ????????            <1> 	u.pgdir:  resd 1 ; 09/03/2015 (page dir addr of process)
  3198 0000753C ????????            <1> 	u.ppgdir: resd 1 ; 06/05/2015 (page dir addr of the parent process)
  3199 00007540 ????????            <1> 	u.pbase:  resd 1 ; 20/05/2015 (physical base/transfer address)
  3200                              <1> 		; 24/10/2021 (32 bit value for Retro UNIX 386 v2)
  3201 00007544 ????????            <1> 	u.pcount: resd 1 ; 20/05/2015 (byte -transfer- count for page)
  3202                              <1> 	;u.pncount: resw 1 
  3203                              <1> 		; 16/06/2015 (byte -transfer- count for page, 'namei', 'mkdir')
  3204                              <1> 	;u.pnbase:  resd 1 
  3205                              <1> 		; 16/06/2015 (physical base/transfer address, 'namei', 'mkdir')
  3206 00007548 ????                <1> 	u.rsvd:	  resw 1 ; 04/12/2021 (dword alignment)
  3207                              <1> 			 ; 09/06/2015
  3208 0000754A ??                  <1> 	u.kcall:  resb 1 ; The caller is 'namei' (dskr) or 'mkdir' (dskw) sign
  3209                              <1> 		; 08/03/2020 (block device read/write flag for 'sioreg')		
  3210 0000754B ??                  <1> 	u.brwdev: resb 1 ; Block device number for direct I/O (bread & bwrite)
  3211                              <1> 			 ; 24/07/2015 - 24/06/2015
  3212                              <1> 	;u.args:  resd 1 ; arguments list (line) offset from start of [u.upage]
  3213                              <1> 			 ; (arg list/line is from offset [u.args] to 4096 in [u.upage])
  3214                              <1> 			 ; ([u.args] points to argument count -argc- address offset)
  3215                              <1>  			 ; 24/06/2015	  	
  3216                              <1> 	;u.core:  resd 1 ; physical start address of user's memory space (for sys exec)
  3217                              <1> 	;u.ecore: resd 1 ; physical end address of user's memory space (for sys exec)
  3218                              <1> 	; last error number
  3219 0000754C ????????            <1> 	u.error:  resd 1 ; 28/07/2013 - 09/03/2015 
  3220                              <1> 			; Retro UNIX 8086/386 v1 feature only!
  3221                              <1> 			; 21/09/2015 (debugging - page fault analyze)
  3222 00007550 ????????            <1> 	u.pfcount: resd 1 ; page fault count for (this) process (for sys geterr)
  3223                              <1> 		; 29/05/2021 - Retro UNIX 386 v2, 2021 (sleep, psig)
  3224                              <1> ;	u.signal: resd 1 ; unix signal recognition (enabling, accepting) bits	
  3225                              <1> ;	u.srb:	  resb 1 ; signal handling/responding method
  3226                              <1> ;	u.snum:	  resb 1 ; signal number (last signal number)
  3227                              <1> ;	u.exit:	  resb 1 ; exit code ; 30/05/2021 - Retro UNIX 386 v2
  3228                              <1> ;	u.s_lock: resb 1 ; signal handling (phase) lock	
  3229                              <1> ;	u.s_addr: resd 1 ; Signal Response Byte or signal handler (callback) address
  3230                              <1> ;	u.s_time: resd 1 ; signal time in unix epoch format		
  3231                              <1> 		; 19/12/2016 (TRDOS 386)	
  3232                              <1> ;	u.tcb:	  resd 1 ; Timer callback address/flag which will be used by timer int
  3233                              <1> 		; 13/01/2017 (TRDOS 386)
  3234                              <1> ;	u.t_lock: resb 1 ; Timer interrupt (callback) lock (unlocked by 'sysrele')
  3235                              <1> ;	u.t_mode: resb 1 ; running mode during timer interrupt (0= system, 0FFh= user)
  3236                              <1> 		; 26/02/2017 (TRDOS 386)
  3237                              <1> ;	u.irqc:	  resb 1  ; Count of IRQ callback services (IRQs in use)
  3238                              <1> 		; 28/02/2017 (TRDOS 386) 
  3239                              <1> ;	u.irqwait: resb 1 ; IRQ waiting for callback service flag (IRQ number, If > 0)
  3240                              <1> ;	u.r_lock:  resb 1 ; 'IRQ callback service is in progress' flag (IRQ lock)
  3241                              <1> ;	u.r_mode:  resb 1 ; running mode during hardware interrupt
  3242                              <1> 		; 07/02/2020 - Retro UNIX 386 v2
  3243                              <1> ;	u.redir:  resb 1 ; device func redirection permitted by user (if u.redir > 0)
  3244                              <1> 		; 26/02/2020 - Retro UNIX 386 v2
  3245                              <1> ;	u.rwm:	  resb 1  ; read & write mode for devices/drives, 0 = direct, 1 = fs r/w 
  3246                              <1> 		; 24/02/2020 - Retro UNIX 386 v2
  3247                              <1> 		; 24/10/2021 (32 bit value)
  3248                              <1> ;	u.scount: resd 1 ; sub byte count ; 22/03/2020 
  3249                              <1> 		; 07/03/2020 - Retro UNIX 386 v2
  3250                              <1> ;	u.bps:	  resw 1 ; u.bps = bytes per sector, for readi and writei
  3251                              <1> ;		  resw 1 ; 24/10/2021 (32 bit value for using with 32 bit registers)	
  3252                              <1> 		; 23/03/2020 - Retro UNIX 386 v2
  3253                              <1> ;	u.timeout: resw 1 ; timeout setting (seconds) -for 'sleep'-
  3254                              <1> 		; 28/04/2020 - Retro UNIX 386 v2
  3255                              <1> ;	u.lock:	  resb 1 ; Device lock flag (bit 0 is for device/disk read/write)
  3256                              <1> 		; 24/04/2020
  3257                              <1> 		; 17/04/2020 - Retro UNIX 386 v2
  3258                              <1> ;	u.ifs:	  resb 1 ; (current) installable file system driver index number
  3259                              <1> 		; 25/03/2020
  3260                              <1> 		; 20/03/2020
  3261                              <1> 	;u.lcount: resd 1 ; last byte count for 'writei' (for restoring write count)	
  3262                              <1> 
  3263                              <1> 		; 08/03/2020 - Retro UNIX 386 v2 ('sioreg, 'readi')
  3264                              <1> 	;u.limit: resd 1  ; disk/file size limit for block device read/write ('sioreg')			
  3265                              <1> 	;	  resd 1  ; 08/03/2020 - 64 bit limit (disk size in bytes)
  3266                              <1> 	;	; 26/02/2020 - Retro UNIX 386 v2
  3267                              <1> 	;u.sector: resd 1  ; current sector (for readi and writei, device r/w)  		
  3268                              <1> 	;u.buffer: resd 1  ; current buffer (for readi and writei, device r/w) 
  3269                              <1> 		; 14/03/2020 - Retro UNIX 386 v2 (sleep, wakeup)
  3270                              <1> 	;u.devmm: resw 1 ; device major (hb) and minor (lb) numbers
  3271                              <1> 	;u.zero:  resw 1 ; 14/03/2020 - Must be ZERO for current version
  3272                              <1> 		; 22/03/2020 - Retro UNIX 386 v2 (sleep, wakeup)
  3273                              <1> ;	u.device: resd 1 ; (blk, chr) device description table (entry) address 	
  3274                              <1> 		; 14/03/2020 - Retro UNIX 386 v2 (sleep, wakeup)
  3275                              <1> ;	u.function: resd 1 ; device function (read, write)
  3276                              <1> 		; 22/03/2020 - Retro UNIX 386 v2, 2020 (readi, writei)
  3277                              <1> ;	u.buffer: resd 1  ; for saving buffer header address for 'poke'
  3278                              <1> ;	u.inode:  resw 1  ; for saving inode number of current (device) inode
  3279                              <1> ;		  resw 1  ; 18/10/2021 Retro UNIX 386 v2 - 32 bit inode number			
  3280                              <1> ;	u.idev:	  resb 1  ; for saving logical drv number of current (dev) inode
  3281                              <1> 		; 04/12/2021
  3282                              <1> ;	u.rsvd2:  resw 1  ; (for dword alignment)	
  3283                              <1> 		; 27/02/2017 (TRDOS 386) 
  3284                              <1> ;; 31/12/2021 - temporary !
  3285                              <1> ;	u.fpsave: resb 1  ; TRDOS 386, 'save/restore FPU registers' flag
  3286                              <1> alignb 4
  3287                              <1> 	; !! wrong sizing in TRDOS 386 v2.0.4 (in 'ubss.s', 28/02/2017) !! 
  3288                              <1> 	;u.fpregs: resb 94 ; 94 byte area for saving and restoring FPU registers
  3289                              <1> 	; 30/05/2021 - Retro UNIX 386 v2
  3290                              <1> ;	u.fpregs: resb 108 ; 108 byte area for saving and restoring FPU registers	
  3291                              <1> alignb 4
  3292                              <1> 
  3293                              <1> U_SIZE	equ $ - user
  3294                              <1> 
  3295                              <1> ; 18/10/2015 - Retro UNIX 386 v1 (local variables for 'namei' and 'sysexec')
  3296 00007554 ????????            <1> pcore:  resd 1 ; physical start address of user's memory space (for sys exec)
  3297 00007558 ????????            <1> ecore:  resd 1 ; physical start address of user's memory space (for sys exec)
  3298 0000755C ????????            <1> nbase:	resd 1	; physical base address for 'namei' & 'sysexec'
  3299                              <1> ;ncount: resw 1 ; remain byte count in page for 'namei' & 'sysexec'
  3300                              <1> ; 11/12/2021 - Retro UNIX 386 v1.2 (32 bit 'ncount')
  3301 00007560 ????????            <1> ncount: resd 1	; remain byte count in page for 'namei' & 'sysexec'
  3302                              <1> ;argc:	resw 1	; argument count for 'sysexec'
  3303                              <1> ; 11/12/2021 - Retro UNIX 386 v1.2 (32 bit 'argc')
  3304 00007564 ????????            <1> argc:	resd 1	; argument count for 'sysexec'
  3305 00007568 ????????            <1> argv:	resd 1	; argument list (recent) address for 'sysexec'
  3306                              <1> 
  3307                              <1> ; 03/06/2015 - Retro UNIX 386 v1 Beginning
  3308                              <1> ; 07/04/2013 - 31/07/2013 - Retro UNIX 8086 v1
  3309 0000756C ??                  <1> rw: 	 resb 1 ;; Read/Write sign (iget)
  3310 0000756D ??                  <1> rwdsk:	 resb 1 ;; Read/Write function number (diskio) - 16/06/2015
  3311 0000756E ??                  <1> mget_rw: resb 1 ; 22/11/2021 
  3312 0000756F ??                  <1> retry_count: resb 1 ; Disk I/O retry count - 11/06/2015
  3313                              <1> 
  3314                              <1> ; 22/11/2021 - Retro UNIX 386 v2 compatibility
  3315                              <1> level:	resd 0  ; level, level+1, level+2, level+3  ; for 'mget' procedure
  3316                              <1> 
  3317                              <1> ; (02/01/2022)
  3318                              <1> ; 27/11/2021
  3319                              <1> ; these are will be used for disk block allocation ('alloc' proc)
  3320                              <1> free_map_offset: 
  3321 00007570 ????????            <1> 		resd 1
  3322 00007574 ????????            <1> free_map_index:	resd 1
  3323                              <1> free_map_sector:
  3324 00007578 ????????            <1> 		resd 1
  3325                              <1> ;free_map_buffer:
  3326                              <1> ;		resd 1
  3327                              <1> 
  3328                              <1> ; 12/01/2022
  3329                              <1> ; 27/11/2021 - temporary !
  3330                              <1> ;s.time:	resd 1
  3331                              <1> 
  3332                              <1> ;alignb 4
  3333                              <1> 
  3334                              <1> ; (02/01/2022)
  3335                              <1> ; 22/08/2015
  3336                              <1> ;buffer: resb nbuf * 520
  3337                              <1> 
  3338 0000757C ????????????????    <1> sb0:	resd 2
  3339                              <1> ;s:
  3340                              <1> ; (root disk) super block buffer
  3341                              <1> systm:
  3342                              <1> 	; 27/11/2021 - Retro UNIX 386 v2 compatible super block
  3343                              <1> 	; 13/11/2015 (Retro UNIX 386 v1)	
  3344                              <1> 	; 11/03/2013. 
  3345                              <1> 	;Derived from UNIX v1 source code 'systm' structure (ux).
  3346                              <1> 	;s.
  3347                              <1> 
  3348                              <1> 	;resw 1
  3349                              <1> 	;resb 360 ; 2880 sectors ; original UNIX v1 value: 128
  3350                              <1> 	;resw 1
  3351                              <1> 	;resb 32 ; 256+40 inodes ; original UNIX v1 value: 64
  3352                              <1> 	;s.time: resd 1
  3353                              <1> 	;s.syst: resd 1
  3354                              <1>         ;s.wait_: resd 1 ; wait
  3355                              <1> 	;s.idlet: resd 1
  3356                              <1> 	;s.chrgt: resd 1
  3357                              <1> 	;s.drerr: resw 1
  3358                              <1> 
  3359                              <1> 	; 27/11/2021
  3360 00007584 <res 200h>          <1> 	resb 512	
  3361                              <1> 
  3362                              <1> ;S_SIZE	equ $ - systm
  3363                              <1> 
  3364                              <1> 	;resb 512-S_SIZE ; 03/06/2015
  3365                              <1> 
  3366 00007784 ????????????????    <1> sb1:	resd 2
  3367                              <1> ; (mounted disk) super block buffer
  3368                              <1> mount:	
  3369 0000778C <res 200h>          <1> 	resb 512  ; 03/06/2015
  3370                              <1> 
  3371                              <1> ; 10/01/2022
  3372 0000798C <res 200h>          <1> dbli_buf: resb 512  ; double indir ptr buff for 'itrunc', 'tloop'
  3373 00007B8C <res 200h>          <1> trpi_buf: resb 512  ; triple indir ptr buff for 'itrunc', 'tloop'
  3374                              <1> 
  3375                              <1> ;/ ux -- unix
  3376                              <1> ;
  3377                              <1> ;systm:
  3378                              <1> ;
  3379                              <1> ;	.=.+2
  3380                              <1> ;	.=.+128.
  3381                              <1> ;	.=.+2
  3382                              <1> ;	.=.+64.
  3383                              <1> ;	s.time: .=.+4
  3384                              <1> ;	s.syst: .=.+4
  3385                              <1> ;	s.wait: .=.+4
  3386                              <1> ;	s.idlet:.=.+4
  3387                              <1> ;	s.chrgt:.=.+4
  3388                              <1> ;	s.drerr:.=.+2
  3389                              <1> ;inode:
  3390                              <1> ;	i.flgs: .=.+2
  3391                              <1> ;	i.nlks: .=.+1
  3392                              <1> ;	i.uid:  .=.+1
  3393                              <1> ;	i.size: .=.+2
  3394                              <1> ;	i.dskp: .=.+16.
  3395                              <1> ;	i.ctim: .=.+4
  3396                              <1> ;	i.mtim: .=.+4
  3397                              <1> ;	. = inode+32.
  3398                              <1> ;mount:	.=.+1024.
  3399                              <1> ;proc:
  3400                              <1> ;	p.pid:  .=.+[2*nproc]
  3401                              <1> ;	p.dska: .=.+[2*nproc]
  3402                              <1> ;	p.ppid: .=.+[2*nproc]
  3403                              <1> ;	p.break:.=.+[2*nproc]
  3404                              <1> ;	p.link: .=.+nproc
  3405                              <1> ;	p.stat: .=.+nproc
  3406                              <1> ;tty:
  3407                              <1> ;	. = .+[ntty*8.]
  3408                              <1> ;fsp:	.=.+[nfiles*8.]
  3409                              <1> ;bufp:	.=.+[nbuf*2]+6
  3410                              <1> ;sb0:	.=.+8
  3411                              <1> ;sb1:	.=.+8
  3412                              <1> ;swp:	.=.+8
  3413                              <1> ;ii:	.=.+2
  3414                              <1> ;idev:	.=.+2
  3415                              <1> ;cdev:	.=.+2
  3416                              <1> ;deverr: .=.+12.
  3417                              <1> ;active: .=.+2
  3418                              <1> ;rfap:	.=.+2
  3419                              <1> ;rkap:	.=.+2
  3420                              <1> ;tcap:	.=.+2
  3421                              <1> ;tcstate:.=.+2
  3422                              <1> ;tcerrc: .=.+2
  3423                              <1> ;mnti:	.=.+2
  3424                              <1> ;mntd:	.=.+2
  3425                              <1> ;mpid:	.=.+2
  3426                              <1> ;clockp: .=.+2
  3427                              <1> ;rootdir:.=.+2
  3428                              <1> ;toutt:	.=.+16.
  3429                              <1> ;touts: .=.+32.
  3430                              <1> ;runq:	.=.+6
  3431                              <1> ;
  3432                              <1> ;wlist:	.=.+40.
  3433                              <1> ;cc:	.=.+30.
  3434                              <1> ;cf:	.=.+31.
  3435                              <1> ;cl:	.=.+31.
  3436                              <1> ;clist:	.=.+510.
  3437                              <1> ;imod:	.=.+1
  3438                              <1> ;smod:	.=.+1
  3439                              <1> ;mmod:	.=.+1
  3440                              <1> ;uquant: .=.+1
  3441                              <1> ;sysflg: .=.+1
  3442                              <1> ;pptiflg:.=.+1
  3443                              <1> ;ttyoch: .=.+1
  3444                              <1> ; .even
  3445                              <1> ; .=.+100.; sstack:
  3446                              <1> ;buffer: .=.+[ntty*140.]
  3447                              <1> ;	.=.+[nbuf*520.]
  3448                              <1> ;
  3449                              <1> ; . = core-64.
  3450                              <1> ;user:
  3451                              <1> ;	u.sp:    .=.+2
  3452                              <1> ;	u.usp:   .=.+2
  3453                              <1> ;	u.r0:    .=.+2
  3454                              <1> ;	u.cdir:  .=.+2
  3455                              <1> ;	u.fp:    .=.+10.
  3456                              <1> ;	u.fofp:  .=.+2
  3457                              <1> ;	u.dirp:  .=.+2
  3458                              <1> ;	u.namep: .=.+2
  3459                              <1> ;	u.off:   .=.+2
  3460                              <1> ;	u.base:  .=.+2
  3461                              <1> ;	u.count: .=.+2
  3462                              <1> ;	u.nread: .=.+2
  3463                              <1> ;	u.break: .=.+2
  3464                              <1> ;	u.ttyp:  .=.+2
  3465                              <1> ;	u.dirbuf:.=.+10.
  3466                              <1> ;	u.pri:   .=.+2
  3467                              <1> ;	u.intr:  .=.+2
  3468                              <1> ;	u.quit:  .=.+2
  3469                              <1> ;	u.emt:   .=.+2
  3470                              <1> ;	u.ilgins:.=.+2
  3471                              <1> ;	u.cdev:  .=.+2
  3472                              <1> ;	u.uid:   .=.+1
  3473                              <1> ;	u.ruid:  .=.+1
  3474                              <1> ;	u.bsys:  .=.+1
  3475                              <1> ;	u.uno:   .=.+1
  3476                              <1> ;. = core
  2735                                  
  2736                                  ; 27/12/2021
  2737 00007D8C <res C30h>              buffer: resb (nbuf*520)
  2738                                  
  2739                                  bss_end:
  2740                                  
  2741                                  ; 12/12/2021
  2742                                  BSS_SIZE equ bss_end - bss_start
  2743                                  
  2744                                  ; 27/12/2013
  2745                                  _end:  ; end of kernel code (and read only data, just before bss)
