     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: 15/01/2017 ]
    10                                  ;
    11                                  ; ****************************************************************************
    12                                  
    13                                  [BITS 32]
    14 00000000 BE[17010000]                    mov     esi, prg_msg
    15 00000005 E8FA000000              	call	print_msg
    16                                  
    17 0000000A E8F0000000              	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[84010000]            	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[85010000]          	cmp	[timer_event_status], bh ; 0
    57                                  	;cmp	byte [t_event], bh ; 0
    58 00000053 7617                    	jna	short pc_2
    59                                  
    60 00000055 883D[85010000]          	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[88010000]            	mov	esi, counter_str
    73 00000067 E898000000              	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 E888000000              	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[84010000]          	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 FE05[85010000]          	inc	byte [timer_event_status]
   107                                  
   108 0000009E B900010000              	mov	ecx, 256
   109                                  _tcbx:
   110 000000A3 51                      	push	ecx
   111 000000A4 29D2                    	sub	edx, edx
   112 000000A6 0FB705[86010000]        	movzx	eax, word [tcount]
   113 000000AD BF[8D010000]            	mov	edi, counter_str+5
   114 000000B2 B90A000000              	mov	ecx, 10
   115                                  _tcb1:
   116 000000B7 F7F1                    	div	ecx
   117 000000B9 80C230                  	add	dl, '0'
   118 000000BC 4F                      	dec 	edi
   119 000000BD 8817                    	mov	[edi], dl
   120 000000BF 28D2                    	sub	dl, dl 
   121 000000C1 21C0                    	and	eax, eax
   122 000000C3 75F2                    	jnz	short _tcb1
   123                                  	
   124 000000C5 89F9                    	mov	ecx, edi
   125 000000C7 BF[88010000]            	mov	edi, counter_str
   126 000000CC 29F9                    	sub	ecx, edi
   127 000000CE 7604                    	jna	short _tcb3
   128                                  _tcb2:
   129 000000D0 B030                    	mov	al, '0'
   130 000000D2 F3AA                    	rep	stosb	
   131                                  _tcb3:
   132 000000D4 66FF05[86010000]        	inc	word [tcount]
   133 000000DB 56                      	push	esi
   134 000000DC BE[88010000]            	mov	esi, counter_str
   135 000000E1 BF00800B00              	mov	edi, 0B8000h
   136 000000E6 B40F                    	mov	ah, 0Fh
   137 000000E8 B105                    	mov	cl, 5
   138                                  _tcby:
   139 000000EA AC                      	lodsb
   140 000000EB 66AB                    	stosw
   141 000000ED E2FB                    	loop	_tcby 	
   142 000000EF 5E                      	pop	esi
   143 000000F0 59                      	pop	ecx
   144 000000F1 E2B0                    	loop 	_tcbx
   145                                  
   146 000000F3 B827000000              	mov	eax, 39 ; 'sysrele'
   147 000000F8 CD40                    	int	40h 	; TRDOS 386 system call
   148                                  here:
   149 000000FA 90                      	nop
   150 000000FB 90                      	nop
   151 000000FC 90                      	nop
   152 000000FD EBFB                    	jmp	 short here
   153                                  
   154                                  getch:
   155                                  	; Getchar by using keyboard interrupt
   156 000000FF B410                    	mov	ah, 10h
   157 00000101 CD32                    	int 	32h ; TRDOS 386 keyboard interrupt
   158                                  		    ; (IBM PC/AT ROMBIOS, INT 16h)			
   159 00000103 C3                      	retn
   160                                  
   161                                  print_msg:
   162 00000104 B40E                    	mov	ah, 0Eh
   163 00000106 668B1D[82010000]        	mov	bx, [color]
   164                                  pmsg_loop:
   165 0000010D AC                      	lodsb
   166 0000010E 20C0                    	and	al, al
   167 00000110 7404                    	jz	short pmsg_ok
   168 00000112 CD31                    	int	31h	; TRDOS 386 video interrupt
   169 00000114 EBF7                    	jmp	short pmsg_loop	
   170                                  pmsg_ok:
   171 00000116 C3                      	retn
   172                                  
   173                                  ; /// MESSAGE - DATA ///
   174                                  
   175                                  prg_msg:
   176 00000117 0D0A07                  	db 0Dh, 0Ah, 07h
   177 0000011A 5452444F5320333836-     	db 'TRDOS 386 timer callback test program by Erdogan Tan [15/01/2017]'
   177 00000123 2074696D6572206361-
   177 0000012C 6C6C6261636B207465-
   177 00000135 73742070726F677261-
   177 0000013E 6D206279204572646F-
   177 00000147 67616E2054616E205B-
   177 00000150 31352F30312F323031-
   177 00000159 375D               
   178 0000015B 0D0A0D0A                	db 0Dh, 0Ah, 0Dh, 0Ah
   179 0000015F 28507265737320616E-             db '(Press any key to continue...)'
   179 00000168 79206B657920746F20-
   179 00000171 636F6E74696E75652E-
   179 0000017A 2E2E29             
   180 0000017D 0D0A                    	db 0Dh, 0Ah
   181 0000017F 0D0A00                  	db 0Dh, 0Ah, 0
   182                                  color:
   183 00000182 0F00                    	dw 0Fh
   184                                  timer_event_number:
   185 00000184 00                      	db 0
   186                                  timer_event_status:
   187 00000185 00                      	db 0
   188                                  tcount:
   189 00000186 0000                    	dw 0
   190                                  
   191                                  counter_str:
   192 00000188 3939393939              	db '99999'
   193 0000018D 00                      	db 0
