     1                                  ; ****************************************************************************
     2                                  ; counter.s (TRDOS 386, TRDOS v2.0 - sample binary file, 'counter.s'
     3                                  ; ----------------------------------------------------------------------------
     4                                  ; COUNTER.PRG ! TEST program !
     5                                  ; TRDOS 386 timer interrupt & callback function test program (by Erdogan Tan)
     6                                  ;
     7                                  ; 19/12/2016
     8                                  ;
     9                                  ; [ Last Modification: 31/12/2016 ]
    10                                  ;
    11                                  ; ****************************************************************************
    12                                  
    13                                  [BITS 32]
    14 00000000 BE[16010000]                    mov     esi, prg_msg
    15 00000005 E8F9000000              	call	print_msg
    16                                  
    17 0000000A E8EF000000              	call	getch
    18                                  
    19 0000000F 3C1B                    	cmp	al, 1Bh  ; ESC key
    20 00000011 7479                    	je	terminate ; short olacak !	
    21                                  
    22                                  		; DIRECT VGA MEMORY ACCESS
    23 00000013 31DB                    		xor	ebx, ebx
    24 00000015 B704                    		mov	bh, 4 ; Direct access/map to VGA memory (0B8000h)
    25                                  		;mov	eax, _video ; 1Fh
    26 00000017 B81F000000              		mov	eax, 1Fh ; sys _video ; TRDOS 386 Video functions
    27 0000001C CD40                    		int	40h   ; TRDOS 386 system call
    28 0000001E C70500800B00200720-     		mov 	dword [0B8000h], 07200720h
    28 00000027 07                 
    29 00000028 C70504800B00200720-     		mov 	dword [0B8004h], 07200720h
    29 00000031 07                 
    30                                  
    31                                  	;mov	word [color], 22h
    32                                  
    33                                  	; start (Real Time Clock) timer function
    34                                  	;;mov	bl, 0
    35                                          ;;mov	bh, 84h	 ; Current Timer setup, Callback method 	
    36 00000032 66BB0084                        mov	bx, 8400h
    37                                  	;mov	ecx, 1	 ; 1 tick 
    38                                  	;mov	bl, 0FFh
    39                                  	;mov	bh, 4
    40 00000036 B101                    	mov	cl, 1
    41 00000038 BA[98000000]            	mov	edx, t_callback ; timer callback service address
    42                                  
    43                                  	;mov	edx, t_event
    44 0000003D B821000000              	mov	eax, 33	; 'systimer'
    45 00000042 CD40                    	int	40h	; TRDOS 386 system call
    46 00000044 7246                    	jc	terminate
    47                                  
    48 00000046 A2[83010000]            	mov	[timer_event_number], al 
    49                                  
    50                                  ;	mov 	ah, 0   ; set video mode
    51                                  ;	mov 	al, 13h ; 320x200 graphics mode (256 colors)
    52                                  ;	int	31h     ; TRDOS 386 video interrupt
    53                                  			; (IBM PC/AT ROMBIOS, INT 10h) 
    54                                  pc_1:
    55 0000004B 30FF                    	xor	bh, bh
    56 0000004D 383D[84010000]          	cmp	[timer_event_status], bh ; 0
    57                                  	;cmp	byte [t_event], bh ; 0
    58 00000053 7617                    	jna	short pc_2
    59                                  
    60 00000055 883D[84010000]          	mov	[timer_event_status], bh ; 0
    61                                  	;mov	byte [t_event], bh ; 0
    62                                  
    63                                  	;call	t_callback
    64                                  
    65                                  	; Set cursor position
    66 0000005B 6631D2                  	xor	dx, dx  ; row 0, column 0
    67                                  	;xor	bh, bh  ; Video page 0
    68 0000005E B402                    	mov	ah, 2	; set cursor position
    69 00000060 CD31                    	int	31h 	; TRDOS 386 video interrupt
    70                                  
    71                                  	; Print Count
    72 00000062 BE[87010000]            	mov	esi, counter_str
    73 00000067 E897000000              	call	print_msg
    74                                  pc_2:
    75                                  	; Check keyboard buffer
    76 0000006C B411                    	mov	ah, 11h
    77 0000006E CD32                    	int 	32h ; TRDOS 386 keyboard interrupt
    78                                  		    ; (IBM PC/AT ROMBIOS, INT 16h)			
    79 00000070 74D9                    	jz	short pc_1 ; keyboard buffer empty
    80                                  
    81 00000072 E887000000              	call	getch
    82 00000077 3C1B                    	cmp	al, 1Bh  ; ESC key
    83 00000079 77D0                    	ja	short pc_1
    84 0000007B 740F                    	je	short terminate
    85                                  
    86                                  	; Stop timer event
    87 0000007D 8A1D[83010000]          	mov	bl, [timer_event_number]
    88 00000083 B780                    	mov	bh, 80h	; stop timer event
    89                                  			; and cancel callback service	
    90 00000085 B821000000              	mov	eax, 33	; 'systimer'
    91 0000008A CD40                    	int	40h	; TRDOS 386 system call
    92                                  
    93                                  terminate:
    94 0000008C B801000000              	mov	eax, 1	; 'sysexit'
    95 00000091 CD40                    	int	40h	; TRDOS 386 system call
    96                                  hang:
    97 00000093 90                      	nop
    98 00000094 90                      	nop
    99 00000095 90                      	nop
   100 00000096 EBFB                    	jmp	short hang
   101                                  
   102                                  ;t_event:
   103                                  ;	db	0
   104                                  
   105                                  t_callback:
   106 00000098 9C                      	pushfd
   107                                  
   108                                  	; save registers
   109 00000099 57                      	push	edi
   110 0000009A 52                      	push	edx
   111 0000009B 50                      	push	eax
   112 0000009C 51                      	push	ecx
   113                                  	
   114 0000009D FE05[84010000]          	inc	byte [timer_event_status]
   115                                  
   116 000000A3 B900010000              	mov	ecx, 256
   117                                  _tcbx:
   118 000000A8 51                      	push	ecx
   119 000000A9 29D2                    	sub	edx, edx
   120 000000AB 0FB705[85010000]        	movzx	eax, word [tcount]
   121 000000B2 BF[8C010000]            	mov	edi, counter_str+5
   122 000000B7 B90A000000              	mov	ecx, 10
   123                                  _tcb1:
   124 000000BC F7F1                    	div	ecx
   125 000000BE 80C230                  	add	dl, '0'
   126 000000C1 4F                      	dec 	edi
   127 000000C2 8817                    	mov	[edi], dl
   128 000000C4 28D2                    	sub	dl, dl 
   129 000000C6 21C0                    	and	eax, eax
   130 000000C8 75F2                    	jnz	short _tcb1
   131                                  	
   132 000000CA 89F9                    	mov	ecx, edi
   133 000000CC BF[87010000]            	mov	edi, counter_str
   134 000000D1 29F9                    	sub	ecx, edi
   135 000000D3 7604                    	jna	short _tcb3
   136                                  _tcb2:
   137 000000D5 B030                    	mov	al, '0'
   138 000000D7 F3AA                    	rep	stosb	
   139                                  _tcb3:
   140 000000D9 66FF05[85010000]        	inc	word [tcount]
   141 000000E0 56                      	push	esi
   142 000000E1 BE[87010000]            	mov	esi, counter_str
   143 000000E6 BF00800B00              	mov	edi, 0B8000h
   144 000000EB B40F                    	mov	ah, 0Fh
   145 000000ED B105                    	mov	cl, 5
   146                                  _tcby:
   147 000000EF AC                      	lodsb
   148 000000F0 66AB                    	stosw
   149 000000F2 E2FB                    	loop	_tcby 	
   150 000000F4 5E                      	pop	esi
   151 000000F5 59                      	pop	ecx
   152 000000F6 E2B0                    	loop 	_tcbx
   153                                  
   154                                  	; restore registers
   155 000000F8 59                      	pop	ecx
   156 000000F9 58                      	pop	eax
   157 000000FA 5A                      	pop	edx
   158 000000FB 5F                      	pop	edi
   159                                  
   160 000000FC 9D                      	popfd
   161                                  
   162 000000FD C3                      	retn
   163                                  
   164                                  	;iret	; restore EIP, CS, E-FLAGS	
   165                                  		; return to normal running code
   166                                  
   167                                  getch:
   168                                  	; Getchar by using keyboard interrupt
   169 000000FE B410                    	mov	ah, 10h
   170 00000100 CD32                    	int 	32h ; TRDOS 386 keyboard interrupt
   171                                  		    ; (IBM PC/AT ROMBIOS, INT 16h)			
   172 00000102 C3                      	retn
   173                                  
   174                                  print_msg:
   175 00000103 B40E                    	mov	ah, 0Eh
   176 00000105 668B1D[81010000]        	mov	bx, [color]
   177                                  pmsg_loop:
   178 0000010C AC                      	lodsb
   179 0000010D 20C0                    	and	al, al
   180 0000010F 7404                    	jz	short pmsg_ok
   181 00000111 CD31                    	int	31h	; TRDOS 386 video interrupt
   182 00000113 EBF7                    	jmp	short pmsg_loop	
   183                                  pmsg_ok:
   184 00000115 C3                      	retn
   185                                  
   186                                  ; /// MESSAGE - DATA ///
   187                                  
   188                                  prg_msg:
   189 00000116 0D0A07                  	db 0Dh, 0Ah, 07h
   190 00000119 5452444F5320333836-     	db 'TRDOS 386 timer callback test program by Erdogan Tan [31/12/2016]'
   190 00000122 2074696D6572206361-
   190 0000012B 6C6C6261636B207465-
   190 00000134 73742070726F677261-
   190 0000013D 6D206279204572646F-
   190 00000146 67616E2054616E205B-
   190 0000014F 33312F31322F323031-
   190 00000158 365D               
   191 0000015A 0D0A0D0A                	db 0Dh, 0Ah, 0Dh, 0Ah
   192 0000015E 28507265737320616E-             db '(Press any key to continue...)'
   192 00000167 79206B657920746F20-
   192 00000170 636F6E74696E75652E-
   192 00000179 2E2E29             
   193 0000017C 0D0A                    	db 0Dh, 0Ah
   194 0000017E 0D0A00                  	db 0Dh, 0Ah, 0
   195                                  color:
   196 00000181 0F00                    	dw 0Fh
   197                                  timer_event_number:
   198 00000183 00                      	db 0
   199                                  timer_event_status:
   200 00000184 00                      	db 0
   201                                  tcount:
   202 00000185 0000                    	dw 0
   203                                  
   204                                  counter_str:
   205 00000187 3939393939              	db '99999'
   206 0000018C 00                      	db 0
