! ***********************************************
! File: speed.S -> speed.com
! Program for Linux Dosemu
! Written by Tim Van der Linden <timvdl@innet.be>
! Changes the HogThreshold value during execution
! ***********************************************

.text
.data
.bss

.align 0

cr	= 0xd
lf	= 0xa
eom	= '$'

.globl _main
_main:
        .org 0x100

        push cs         ! Display string
        pop ds
        mov dx, #string
        add dx, #0x100
        mov ah, #0x09
        int 0x21

        xor dx,dx       ! Clean registers
        xor cx,cx
        xor bx,bx
        xor ax,ax

read_first:
        mov ah, #0x08   ! Read the first character
        int 0x21
        mov cl,al

        mov bl, #0x0d   ! Is it a carriage return?
        cmp al,bl
        je not_changed

        mov bl, #0x3a   ! Is it >9?
        cmp al,bl
        jge read_first

        mov bl, #0x2f   ! Is it <0?
        cmp bl,al
        jge read_first

        mov dl,al       ! Display the first character
        mov ah, #0x02
        int 0x21

read_second:
        xor bx,bx
        mov ah, #0x08   ! Read the second character
        int 0x21
        mov bl,al

        mov dl, #0x0d   ! Is it a CR?
        cmp al,dl
        je is_carriage_return

back:
        mov dl, #0x3a   ! Is it >9?
        cmp bl,dl
        jge read_second

        mov dl, #0x2f   ! Is it <0?
        cmp dl,bl
        jge read_second

        mov dl,al       ! Display second character
        mov ah, #0x02
        int 0x21

value:
        sub cl, #0x30   ! Calculate the HogThreshold value
        sub bl, #0x30
        mov al,cl
        mov cl, #0xa
        mul cl
        add bx,ax


HogThreshold:
	push	bx			! Save the value
	mov	ah, #0xC0		! Get ROM BIOS information
	int	0x15
	mov	cx, #7
	mov	di, bx			! ES:DI = BIOS name
	add	di, #10
	mov	si, #VerCheck		! DS:SI = Version check
	add	si, #0x100
	.byte   0xf3, 0xa6		! repz	cmpsb
	jcxz	version_ok

	mov	ax, #0x35E6		! Get the Int 0xE6 vector
	int	0x21

	mov	ax, es			! Sanity check -- if 0xE6 -> 0000:0000
	or	ax, bx			!   don't call it!
	pop	bx			! Restore HogThreshold
	jz	not_dosemu

        xor	ax,ax       ! Alter the HogThreshold value
        mov	al, #0x12
        int	0xe6
	jmp	back_to_dos

version_ok:				! Version 0.61+
	pop	bx			! Restore HogThreshold
	call	#0xFC00:#0x0002		! Dos helper

back_to_dos:
        xor ax,ax       ! Leave the program
        mov ah, #0x4c
        int 0x21

is_carriage_return:
        mov bl,cl
        mov cl, #0x30
        jmp back

not_changed:
        mov dx, #string_not_changed
        add dx, #0x100
        mov ah, #0x09
        int 0x21
        jmp back_to_dos

not_dosemu:
        mov	dx, #Warning		! Not DOSEMU -- print warning
	add	dx, #0x100		! [magic offset for .COM files]
        mov	ah, #0x09
        int	0x21
	jmp	back_to_dos

VerCheck:
	.ascii	"DOSEMU "

string:
	.ascii "Enter the new HogThreshold value (0-99): $"
string_not_changed:
	.byte cr,lf
	.ascii "HogThreshold value left unchanged.$"

Warning:
	.byte cr,lf
	.ascii "Speed can not be run under DOS"
	.byte cr,lf,eom
