     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: 02/01/2017 ]
    10                                  ;
    11                                  ; ****************************************************************************
    12                                  
    13                                  [BITS 32]
    14 00000000 BE[F8000000]                    mov     esi, prg_msg
    15 00000005 E8DB000000              	call	print_msg
    16                                  
    17 0000000A E8D1000000              	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[65010000]            	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[66010000]          	cmp	[timer_event_status], bh ; 0
    57                                  	;cmp	byte [t_event], bh ; 0
    58 00000053 7617                    	jna	short pc_2
    59                                  
    60 00000055 883D[66010000]          	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[69010000]            	mov	esi, counter_str
    73 00000067 E879000000              	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 E869000000              	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[65010000]          	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[66010000]          	inc	byte [timer_event_status]
   115                                  
   116 000000A3 29D2                    	sub	edx, edx
   117 000000A5 0FB705[67010000]        	movzx	eax, word [tcount]
   118 000000AC BF[6E010000]            	mov	edi, counter_str+5
   119 000000B1 B90A000000              	mov	ecx, 10
   120                                  _tcb1:
   121 000000B6 F7F1                    	div	ecx
   122 000000B8 80C230                  	add	dl, '0'
   123 000000BB 4F                      	dec 	edi
   124 000000BC 8817                    	mov	[edi], dl
   125 000000BE 28D2                    	sub	dl, dl 
   126 000000C0 21C0                    	and	eax, eax
   127 000000C2 75F2                    	jnz	short _tcb1
   128                                  	
   129 000000C4 89F9                    	mov	ecx, edi
   130 000000C6 BF[69010000]            	mov	edi, counter_str
   131 000000CB 29F9                    	sub	ecx, edi
   132 000000CD 7604                    	jna	short _tcb3
   133                                  _tcb2:
   134 000000CF B030                    	mov	al, '0'
   135 000000D1 F3AA                    	rep	stosb	
   136                                  _tcb3:
   137 000000D3 66FF05[67010000]        	inc	word [tcount]
   138                                  
   139                                  	; restore registers
   140 000000DA 59                      	pop	ecx
   141 000000DB 58                      	pop	eax
   142 000000DC 5A                      	pop	edx
   143 000000DD 5F                      	pop	edi
   144                                  
   145 000000DE 9D                      	popfd
   146                                  
   147 000000DF C3                      	retn
   148                                  
   149                                  	;iret	; restore EIP, CS, E-FLAGS	
   150                                  		; return to normal running code
   151                                  
   152                                  getch:
   153                                  	; Getchar by using keyboard interrupt
   154 000000E0 B410                    	mov	ah, 10h
   155 000000E2 CD32                    	int 	32h ; TRDOS 386 keyboard interrupt
   156                                  		    ; (IBM PC/AT ROMBIOS, INT 16h)			
   157 000000E4 C3                      	retn
   158                                  
   159                                  print_msg:
   160 000000E5 B40E                    	mov	ah, 0Eh
   161 000000E7 668B1D[63010000]        	mov	bx, [color]
   162                                  pmsg_loop:
   163 000000EE AC                      	lodsb
   164 000000EF 20C0                    	and	al, al
   165 000000F1 7404                    	jz	short pmsg_ok
   166 000000F3 CD31                    	int	31h	; TRDOS 386 video interrupt
   167 000000F5 EBF7                    	jmp	short pmsg_loop	
   168                                  pmsg_ok:
   169 000000F7 C3                      	retn
   170                                  
   171                                  ; /// MESSAGE - DATA ///
   172                                  
   173                                  prg_msg:
   174 000000F8 0D0A07                  	db 0Dh, 0Ah, 07h
   175 000000FB 5452444F5320333836-     	db 'TRDOS 386 timer callback test program by Erdogan Tan [02/01/2017]'
   175 00000104 2074696D6572206361-
   175 0000010D 6C6C6261636B207465-
   175 00000116 73742070726F677261-
   175 0000011F 6D206279204572646F-
   175 00000128 67616E2054616E205B-
   175 00000131 30322F30312F323031-
   175 0000013A 375D               
   176 0000013C 0D0A0D0A                	db 0Dh, 0Ah, 0Dh, 0Ah
   177 00000140 28507265737320616E-             db '(Press any key to continue...)'
   177 00000149 79206B657920746F20-
   177 00000152 636F6E74696E75652E-
   177 0000015B 2E2E29             
   178 0000015E 0D0A                    	db 0Dh, 0Ah
   179 00000160 0D0A00                  	db 0Dh, 0Ah, 0
   180                                  color:
   181 00000163 0F00                    	dw 0Fh
   182                                  timer_event_number:
   183 00000165 00                      	db 0
   184                                  timer_event_status:
   185 00000166 00                      	db 0
   186                                  tcount:
   187 00000167 0000                    	dw 0
   188                                  
   189                                  counter_str:
   190 00000169 3939393939              	db '99999'
   191 0000016E 00                      	db 0
