.model small

extrn _div_sign:word, _di:word, _ax:word, _cx:word
extrn _FL1:word, _FL2:word, _FL3:word, _FL4:word, _nInserted_Key:word, _speed

extrn _r:word, _ir:word, _src:word, _dst:word, _memory:word
extrn _getdev:near, _putdev:near, _longjmp:near, _intrr:near, _chkbus:near
extrn _kbhit:near, _gettty:near

.data
public  _dstoff
memseg  dw      seg _memory
lasttime db	0
_dstoff  dw      ?

.code

public _getsrc
_getsrc proc    near
        mov     es,memseg
        mov     bx,_ir
        mov     cl,5
        sar     bx,cl
        mov     si,bx
        and     si,1110b
        mov     cl,3
        sar     bx,cl
        and     bx,1110b
        jmp     word ptr getsrctab[bx]
getsrc0:
        mov     ax,_r[si]
        mov     _src,ax
        ret
getsrc1:
        mov     bx,_r[si]
        jmp     getsm
getsrc2:
        mov     bx,_r[si]
        test    di,1
        jz      get21
        cmp     si,1100b
        jl      get2
get21:  add     _r[si],2
        jmp     short getsm
get2:   inc     _r[si]
        jmp     short getsm
getsrc3:
        mov     bx,_r[si]
        add     _r[si],2
        mov     bx,es:_memory[bx]
        jmp     short getsm
getsrc4:
        test    di,1
        jz      get41
        cmp     si,1100b
        jl      get4
get41:  sub     _r[si],2
        mov     bx,_r[si]
        jmp     short getsm
get4:   dec     _r[si]
        mov     bx,_r[si]
        jmp     short getsm
getsrc5:
        sub     _r[si],2
        mov     bx,_r[si]
        mov     bx,es:_memory[bx]
        jmp     short getsm
getsrc6:
        mov     bx,_r+14
        add     _r+14,2
        mov     bx,es:_memory[bx]
        mov     ax,_r[si]
        add     bx,ax
        jmp     short getsm
getsrc7:
        mov     bx,_r+14
        add     _r+14,2
        mov     bx,es:_memory[bx]
        mov     ax,_r[si]
        add     bx,ax
        mov     bx,es:_memory[bx]
getsm:  test    di,1
        jnz     getsmok
;       test    bx,1
;       jz      getsmok
and bx,0fffeh
jmp getsmok
        mov     ax,4
        push    ax
	call    _intrr
getsmok:
        cmp     bx,1110000000000000b
        jb      getsm1
        push    bx
        call    _getdev
        pop     bx
getsmd: mov     _src,ax
        ret
getsm1: mov     ax,es:_memory[bx]
        mov     _src,ax
        ret
.data
getsrctab       dw      getsrc0
        dw      getsrc1
        dw      getsrc2
        dw      getsrc3
        dw      getsrc4
        dw      getsrc5
        dw      getsrc6
        dw      getsrc7
_getsrc endp
.code
public _putdst
_putdst proc    near
        mov     es,memseg
        mov     bx,_ir
        mov     si,bx
        and     si,111b
        sal     si,1
        sar     bx,1
        sar     bx,1
        and     bx,1110b
        jmp     word ptr putdsttab[bx]
putdst0:
        mov     ax,_dst
        test    di,1
        jz      put0
        cbw
put0:   mov     _r[si],ax
        ret
putdst1:
        mov     bx,_r[si]
        jmp     putsm
putdst2:
        mov     bx,_r[si]
        test    di,1
        jz      put21
        cmp     si,1100b
        jl      put2
put21:  add     _r[si],2
        jmp     short putsm
put2:   inc     _r[si]
        jmp     short putsm
putdst3:
        mov     bx,_r[si]
        add     _r[si],2
        mov     bx,es:_memory[bx]
        jmp     short putsm
putdst4:
        test    di,1
        jz      put41
        cmp     si,1100b
        jl      put4
put41:  sub     _r[si],2
        mov     bx,_r[si]
        jmp     short putsm
put4:   dec     _r[si]
        mov     bx,_r[si]
        jmp     short putsm
putdst5:
        sub     _r[si],2
        mov     bx,_r[si]
        mov     bx,es:_memory[bx]
        jmp     short putsm
putdst6:
        mov     bx,_r+14
        add     _r+14,2
        mov     bx,es:_memory[bx]
        mov     ax,_r[si]
        add     bx,ax
        jmp     short putsm
putdst7:
        mov     bx,_r+14
        add     _r+14,2
        mov     bx,es:_memory[bx]
        mov     ax,_r[si]
        add     bx,ax
        mov     bx,es:_memory[bx]
putsm:  test    di,1
        jnz     putsmok
;       test    bx,1
;       jz      putsmok
and bx,0fffeh
jmp putsmok
        mov     ax,4
        push    ax
	call    _intrr
putsmok:
        cmp     bx,1110000000000000b
        jb      putsm1
        push    bx
        call    _putdev
        pop     bx
        ret
putsm1: mov     ax,_dst
        test    di,1
        jnz     ptb
        mov     es:_memory[bx],ax
        ret
ptb:    mov     es:byte ptr _memory[bx],al
        ret
.data
putdsttab       dw      putdst0
        dw      putdst1
        dw      putdst2
        dw      putdst3
        dw      putdst4
        dw      putdst5
        dw      putdst6
        dw      putdst7
_putdst endp
.code
public _adst
_adst proc    near
        mov     es,memseg
        mov     bx,_ir
        mov     si,bx
        and     si,111b
        sal     si,1
        sar     bx,1
        sar     bx,1
        and     bx,1110b
        jmp     word ptr adsttab[bx]
adst0:
        mov     ax,ds
        mov     es,ax
        add     si,offset _r
        mov     bx,si
        ret
adst1:
        mov     bx,_r[si]
        jmp     asm
adst2:
        mov     bx,_r[si]
        test    di,1
        jz      a21
        cmp     si,1100b
        jl      a2
a21:    add     _r[si],2
        jmp     short asm
a2:     inc     _r[si]
        jmp     short asm
adst3:
        mov     bx,_r[si]
        add     _r[si],2
        mov     bx,es:_memory[bx]
        jmp     short asm
adst4:
        test    di,1
        jz      a41
        cmp     si,1100b
        jl      a4
a41:    sub     _r[si],2
        mov     bx,_r[si]
        jmp     short asm
a4:     dec     _r[si]
        mov     bx,_r[si]
        jmp     short asm
adst5:
        sub     _r[si],2
        mov     bx,_r[si]
        mov     bx,es:_memory[bx]
        jmp     short asm
adst6:
        mov     bx,_r+14
        add     _r+14,2
        mov     bx,es:_memory[bx]
        mov     ax,_r[si]
        add     bx,ax
        jmp     short asm
adst7:
        mov     bx,_r+14
        add     _r+14,2
        mov     bx,es:_memory[bx]
        mov     ax,_r[si]
        add     bx,ax
        mov     bx,es:_memory[bx]
asm:    test    di,1
        jnz     asmok
;       test    bx,1
;       jz      asmok
and bx,0fffeh
jmp asmok
        mov     ax,4
        push    ax
	call    _intrr
asmok:
        cmp     bx,1110000000000000b
        jb      asmok1
        push    bx
        call    _chkbus
        pop     bx
asmok1: ret
.data
adsttab       dw      adst0
        dw      adst1
        dw      adst2
        dw      adst3
        dw      adst4
        dw      adst5
        dw      adst6
        dw      adst7
_adst endp
.code

public _jadst
_jadst proc    near
        mov     es,memseg
        mov     bx,_ir
        mov     si,bx
        and     si,111b
        sal     si,1
        sar     bx,1
        sar     bx,1
        and     bx,1110b
        jmp     word ptr jadsttab[bx]
jadst0:
        mov     ax,4
        push    ax
	call    _intrr
jadst1:
        mov     bx,_r[si]
        jmp     short jasm
jadst2:
        mov     bx,_r[si]
        add     _r[si],2
        jmp     short jasm
jadst3:
        mov     bx,_r[si]
        add     _r[si],2
        mov     bx,es:_memory[bx]
        jmp     short jasm
jadst4:
        sub     _r[si],2
        mov     bx,_r[si]
        jmp     short jasm
jadst5:
        sub     _r[si],2
        mov     bx,_r[si]
        mov     bx,es:_memory[bx]
        jmp     short jasm
jadst6:
        mov     bx,_r+14
        add     _r+14,2
        mov     bx,es:_memory[bx]
        mov     ax,_r[si]
        add     bx,ax
        jmp     short jasm
jadst7:
        mov     bx,_r+14
        add     _r+14,2
        mov     bx,es:_memory[bx]
        mov     ax,_r[si]
        add     bx,ax
        mov     bx,es:_memory[bx]
jasm:   test    bx,1
        jz      jasmok
        mov     ax,4
        push    ax
	call    _intrr
jasmok:
        mov     _dstoff,bx
        ret
.data
jadsttab      dw      jadst0
        dw      jadst1
        dw      jadst2
        dw      jadst3
        dw      jadst4
        dw      jadst5
        dw      jadst6
        dw      jadst7
_jadst endp
.code

putw    proc    near
        pushf
        cmp     bx,1110000000000000b
        jb      putw1
        push    bx
        mov     _dst,ax
        call    _putdev
        pop     bx
        popf
        ret

putw1:  mov     es:[bx],ax
        popf
        ret
putw    endp

putb    proc    near
        pushf
        cmp     bx,1110000000000000b
        jb      putb1
        push    bx
        mov     _dst,ax
        call    _putdev
        pop     bx
        popf
        ret

putb1:  mov     es:[bx],al
        popf
        ret
putb    endp

public  _clr
_clr    proc near
        xor     ax,ax
        mov     di,ax
        mov     _dst,ax
        call    _putdst
        mov     al,0100b

public  _setfl
_setfl:
setfl:  mov     es,memseg
        mov     cx,es:_memory[0fffeh]
        and     cx,0fff0h
        or      cl,al
        mov     es:_memory[0fffeh],cx
        ret
_clr    endp

public  _clrb
_clrb   proc near
        mov     di,1
        call    _adst
        xor     al,al
        call    putb
        mov     al,0100b
        jmp     setfl
_clrb   endp

public  _com
_com   proc near
        xor     ax,ax
        mov     di,ax
        call    _adst
        mov     ax,es:[bx]
        not     ax
        call    putw
        mov     bl,0001b
        test    ax,ax
        jnz     com1
        or      bl,4
com1:   test    ax,ax
        jns     com2
        or      bl,8
com2:   mov     al,bl
        jmp     setfl
_com    endp

public  _comb
_comb   proc near
        mov     di,1
        call    _adst
        mov     al,es:[bx]
        not     al
        call    putb
        mov     bl,0001b
        test    al,al
        jnz     com1b
        or      bl,4
com1b:  test    al,al
        jns     com2b
        or      bl,8
com2b:  mov     al,bl
        jmp     setfl
_comb   endp

public  _inc
_inc    proc near
        mov     di,0
        call    _adst
        mov     ax,es:[bx]
        inc     ax
        call    putw
        jmp     savec

public  _getfl
_getfl:
getfl:  mov     al,0
        mov     cl,al
        jnc     getf1
        mov     al,1
getf1:  mov     ah,cl
        jno     getf2
        mov     ah,2
getf2:  mov     bl,cl
        jnz     getf3
        mov     bl,4
getf3:  mov     bh,cl
        jns     getf4
        mov     bh,8
getf4:  or      al,ah
        or      al,bl
        or      al,bh
        jmp     setfl
_inc    endp

public  _incb
_incb   proc near
        mov     di,1
        call    _adst
        mov     al,es:[bx]
        inc     al
        call    putb
        jmp     savec
_incb   endp

public  _dec
_dec    proc near
        mov     di,0
        call    _adst
        mov     ax,es:[bx]
        dec     ax
        call    putw
        jmp     savec
_dec    endp

public  _decb
_decb   proc near
        mov     di,1
        call    _adst
        mov     al,es:[bx]
        dec     al
        call    putb
        jmp     savec
_decb   endp

public  _neg
_neg    proc near
        mov     di,0
        call    _adst
        mov     ax,es:[bx]
        neg     ax
        call    putw
        jmp     getfl
_neg    endp

public  _negb
_negb   proc near
        mov     di,1
        call    _adst
        mov     al,es:[bx]
        neg     al
        call    putb
        jmp     getfl
_negb   endp

public  _tst
_tst    proc near
        mov     di,0
        call    _adst
        mov     ax,es:[bx]
        test    ax,ax
        jmp     getfl
_tst    endp

public  _tstb
_tstb   proc near
        mov     di,1
        call    _adst
        mov     al,es:[bx]
        test    al,al
        jmp     getfl
_tstb   endp

public  _asr
_asr    proc near
        mov     di,0
        call    _adst
        mov     ax,es:[bx]
        sar     ax,1
        call    putw
        jmp     setv
_asr    endp

public  _asrb
_asrb   proc near
        mov     di,1
        call    _adst
        mov     al,es:[bx]
        sar     al,1
        call    putb
        jmp     setv
_asrb   endp

public  _asl
_asl    proc near
        mov     di,0
        call    _adst
        mov     ax,es:[bx]
        sal     ax,1
        call    putw
        jmp     setv
_asl    endp

public  _aslb
_aslb   proc near
        mov     di,1
        call    _adst
        mov     al,es:[bx]
        sal     al,1
        call    putb
        jmp     setv
_aslb   endp

loadc   proc    near
        push    ax
        call    getpsw
        test    al,1
        jz      lex
        stc
lex:    pop     ax
        ret
loadc   endp

public  _ror
_ror    proc near
        mov     di,0
        call    _adst
        mov     ax,es:[bx]
        call    loadc
        rcr     ax,1
        call    putw
setnz:  pushf
        test    di,1
        jnz     setnz1
        or      ax,ax
        jmp     setnz2
setnz1: or      al,al
setnz2: pushf
        pop     ax
        pop     bx
        and     bx,not 0feh
        or      bx,ax
        push    bx
        popf
setv:   pushf
        pop     bx
        mov     cx,bx
        and     cx,10000001b
        jz      setv0
        cmp     cx,10000001b
        jz      setv0
        or      bh,8
        jmp     setvcom
setv0:  and     bh,not 8
setvcom:
        push    bx
        popf
        jmp     getfl
_ror    endp

public  _rorb
_rorb   proc near
        mov     di,1
        call    _adst
        mov     al,es:[bx]
        call    loadc
        rcr     al,1
        call    putb
        jmp     setnz
_rorb   endp

public  _rol
_rol    proc near
        mov     di,0
        call    _adst
        mov     ax,es:[bx]
        call    loadc
        rcl     ax,1
        call    putw
        jmp     setnz
_rol    endp

public  _rolb
_rolb   proc near
        mov     di,1
        call    _adst
        mov     al,es:[bx]
        call    loadc
        rcl     al,1
        call    putb
        jmp     setnz
_rolb   endp
getpsw  proc    near
        push    es
        mov     es,memseg
        mov     ax,es:_memory[0fffeh]
        pop     es
        ret
getpsw  endp

public  _adc
_adc    proc near
        mov     di,0
        call    _adst
        call    getpsw
        mov     cx,0
        test    ax,1
        jz      adc0
        mov     cx,1
adc0:   mov     ax,es:[bx]
        add     ax,cx
        call    putw
        jmp     getfl
_adc    endp

public  _adcb
_adcb   proc near
        mov     di,1
        call    _adst
        call    getpsw
        mov     cl,0
        test    ax,1
        jz      adcb0
        mov     cl,1
adcb0:  mov     al,es:[bx]
        add     al,cl
        call    putb
        jmp     getfl
_adcb   endp

public  _sbc
_sbc    proc near
        mov     di,0
        call    _adst
        call    getpsw
        mov     cx,0
        test    ax,1
        jz      sbc0
        mov     cx,1
sbc0:   mov     ax,es:[bx]
        sub     ax,cx
        call    putw
        jmp     getfl
_sbc    endp

public  _sbcb
_sbcb   proc near
        mov     di,1
        call    _adst
        call    getpsw
        mov     cl,0
        test    ax,1
        jz      sbcb0
        mov     cl,1
sbcb0:  mov     al,es:[bx]
        sub     al,cl
        call    putb
        jmp     getfl
_sbcb   endp

public  _sxt
_sxt    proc near
        mov     di,0
        call    getpsw
        or      al,4
        and     al,not 2
        mov     cx,0
        test    al,8
        jz      sxt0
        mov     cx,0ffffh
        and     al,not 4
sxt0:   mov     _dst,cx
        call    setfl
        jmp     _putdst
_sxt    endp

public  _swabb
_swabb   proc near
        mov     di,0
        call    _adst
        mov     ax,es:[bx]
        xchg    al,ah
        or      al,al
        call    putw
        jmp     getfl
_swabb   endp

public  _mfps
_mfps   proc near
        mov     di,1
        call    getpsw
        or      al,al
        mov     _dst,ax
        call    savec
        jmp     _putdst
_mfps   endp

public  _mtps
_mtps   proc near
        mov     di,1
        call    _adst
        mov     al,es:[bx]
        and     al,11101111b
        mov     es,memseg
        mov     es:byte ptr _memory[0fffeh],al
        ret
_mtps   endp

public  _mov
_mov    proc near
        mov     di,0
        call    _getsrc
        mov     ax,_src
        mov     _dst,ax
        call    getpsw
        and     ax,1
        jnz     mov1
        test    _dst,0ffffh
        jmp     short mov2
mov1:   test    _dst,0ffffh
        stc
mov2:   call    getfl
        jmp     _putdst
_mov    endp

public  _movb
_movb   proc near
        mov     di,1
        call    _getsrc
        mov     ax,_src
        mov     _dst,ax
        call    getpsw
        and     ax,1
        jnz     movb1
        test    byte ptr _dst,0ffh
        jmp     short movb2
movb1:  test    byte ptr _dst,0ffh
        stc
movb2:  call    getfl
        jmp     _putdst
_movb   endp

public  _cmp
_cmp    proc near
        mov     di,0
        call    _getsrc
        call    _adst
        mov     ax,es:[bx]
        cmp     _src,ax
        jmp     getfl
_cmp    endp

public  _cmpb
_cmpb   proc near
        mov     di,1
        call    _getsrc
        call    _adst
        mov     al,es:[bx]
        cmp     byte ptr _src,al
        jmp     getfl
_cmpb   endp

public  _add
_add    proc near
        mov     di,0
        call    _getsrc
        call    _adst
        mov     ax,es:[bx]
        add     ax,_src
        call    putw
        jmp     getfl
_add    endp

public  _sub
_sub    proc near
        mov     di,0
        call    _getsrc
        call    _adst
        mov     ax,es:[bx]
        sub     ax,_src
        call    putw
        jmp     getfl
_sub    endp

public  _bit
_bit    proc near
        mov     di,0
        call    _getsrc
        call    _adst
        mov     ax,es:[bx]
        test    _src,ax
savec:  clc
        pushf
        call    getpsw
        test    al,1
        jnz     savec1
        popf
        jmp     getfl
savec1: popf
        stc
        jmp     getfl
_bit    endp

public  _bitb
_bitb   proc near
        mov     di,1
        call    _getsrc
        call    _adst
        mov     al,es:[bx]
        test    byte ptr _src,al
        jmp     savec
_bitb   endp

public  _bic
_bic    proc near
        mov     di,0
        call    _getsrc
        call    _adst
        mov     ax,es:[bx]
        mov     cx,_src
        not     cx
        and     ax,cx
        call    putw
        jmp     savec
_bic    endp

public  _bicb
_bicb   proc near
        mov     di,1
        call    _getsrc
        call    _adst
        mov     al,es:[bx]
        mov     cl,byte ptr _src
        not     cl
        and     al,cl
        call    putb
        jmp     savec
_bicb   endp

public  _bis
_bis    proc near
        mov     di,0
        call    _getsrc
        call    _adst
        mov     ax,es:[bx]
        or      ax,_src
        call    putw
        jmp     savec
_bis    endp

public  _bisb
_bisb   proc near
        mov     di,1
        call    _getsrc
        call    _adst
        mov     al,es:[bx]
        or      al,byte ptr _src
        call    putb
        jmp     savec
_bisb   endp

public  _xor
_xor    proc near
        mov     di,0
        and     _ir,0111000111111111b
        call    _getsrc
        call    _adst
        mov     ax,es:[bx]
        xor     ax,_src
        call    putw
        jmp     savec
_xor    endp

public  _mul
_mul    proc near
        mov     di,0
        call    _adst
        mov     ax,es:[bx]
        mov     si,_ir
        mov     cx,5
        sar     si,cl
        and     si,1110b
        imul    _r[si]
        jc      m1
        pushf
        mov     cl,al
        or      cl,ah
        xor     ch,ch
        popf
        or      cx,dx
        jmp     m2
m1:     pushf
        mov     cl,al
        or      cl,ah
        xor     ch,ch
        popf
        or      cx,dx
        stc
m2:     pushf
        mov     _r[si],ax
        test    si,2
        jnz     m3
        mov     _r[si],dx
        add     si,2
        mov     _r[si],ax
m3:     popf
        jmp     getfl
_mul    endp

public  _divv
_divv    proc near

mov     _div_sign, 1

;mov     _di, 0
;mov     _ax, 0
;mov     _cx, 0
;mov     _FL1, 0
;mov     _FL2, 0
;mov     _FL3, 0
;mov     _FL4, 0


        mov     di,0
        call    _adst
        mov     si,_ir
        mov     cx,5
        sar     si,cl
        and     si,1110b
        test    si,2

jnz     set_V                    ; Приемник - нечетный регистр.

;        jz      d1
;        mov     ax,4
;        push    ax
;	call    _intrr
;d1:

test    word ptr es:[bx], 0FFFFh
jz      set_ZVC                  ; Деление на 0.

mov     cx, _r+2[si]
mov     ax, _r[si]
test    ax, ax
jns     d2
neg     _div_sign
neg     cx
adc     ax, 0
neg     ax
d2:

mov     di, word ptr es:[bx]
test    di, di
jns     d4
neg     _div_sign
neg     di
d4:

shl     ax, 1
jc      set_V
shl     cx, 1
pushf
adc     ax, 0
popf
jnc     d5
inc     cx
d5:
ror     cx, 1
test    _div_sign, 0FFFFh
jns      d8

;mov  _di, di
;mov  _ax, ax

cmp      di, ax
jb       set_V                    ; Делимое/делитель > 2**16
ja       d9

;inc  _FL1
;mov  _di, di
;mov  _cx, cx

cmp      di, cx
jbe      set_V
jmp      d9

d8:
cmp      di, ax
jbe       set_V                   ; Делимое/делитель > 2**15 (один бит - знак)

d9:

        mov     dx,_r[si]
        mov     ax,_r+2[si]
        idiv    word ptr es:[bx]
        mov     _r[si],ax
        mov     _r+2[si],dx
        test    ax,ax
        jmp     getfl

set_ZVC:
        mov     al,0111b
        jmp     setfl
set_V:
        test    _div_sign, 0FFFFh
        js      set_NV
        mov     al,0010b
        jmp     setfl

set_NV:
        mov     al,1010b
        jmp     setfl

_divv    endp

public  _initov
_initov proc    near
        mov     ax,2500h
        push    ds
        mov     dx,cs
        mov     ds,dx
        mov     dx,offset ovr
        int     21h
        pop     ds
        ret
_initov endp

ovr     proc    near
        pop     ax
        pop     ax
        pop     ax
        test    word ptr es:[bx],0ffffh
        jz      ovr1
        mov     al,2
        jmp     setfl
ovr1:   mov     al,3
        jmp     setfl
ovr     endp

public  _ash
_ash    proc near
        mov     di,0
        call    _adst
        mov     si,_ir
        mov     cx,5
        sar     si,cl
        and     si,1110b
        mov     cx,es:[bx]
        and     cx,03fh
        jnz     ash
        test    _r[si],0ffffh
        jmp     getfl
ash:    mov     ax,_r[si]
        test    cx,100000b
        jnz     ashr
ashl:   sal     ax,1
        loop    ashl
        mov     _r[si],ax
        jmp     setv
ashr:   or      cx,0ffc0h
        neg     cx
ashrl:  sar     ax,1
        loop    ashrl
        mov     _r[si],ax
        jmp     getfl
_ash    endp


public  _ashc
_ashc   proc near
        mov     di,0
        call    _adst
        mov     si,_ir
        mov     cx,5
        sar     si,cl
        and     si,1110b
        mov     di,si
        or      di,2
        mov     cx,es:[bx]
        and     cx,3fh
        jnz     ashc
        test    _r[si],0ffffh
        jnz     gf
        mov     ax,_r[di]
        or      al,ah
        xor     ah,ah
        or      ax,ax
gf:     jmp     getfl
ashc:   test    cx,100000b
        jnz     ashcr
ashcl:  shl     _r[di],1
        rcl     _r[si],1
        loop    ashcl
        dec     _r[si]
        inc     _r[si]
        jnz     gf1
        pushf
        mov     ax,_r[di]
        or      al,ah
        xor     ah,ah
        popf
        dec     ax
        inc     ax
gf1:    jmp     setv
ashcr:  or      cx,0ffc0h
        neg     cx
ashcrl: sar     _r[si],1
        rcr     _r[di],1
        loop    ashcrl
        dec     _r[si]
        inc     _r[si]
        jnz     gf2
        pushf
        mov     ax,_r[di]
        or      al,ah
        xor     ah,ah
        popf
        dec     ax
        inc     ax
gf2:    jmp     getfl
_ashc   endp

public _intrchk
_intrchk proc   near
        mov     es,memseg
        mov     si,es:_memory[0fffeh]   ;PSW

        test    _nInserted_Key, 0FFFFh
        jnz     insKey$

        dec     c
        jnz     testT
;        mov     c,2000
        push    _speed
        pop     c

        call    _kbhit
        mov     es,memseg
        or      ax,ax
        jz      testT
insKey$:
        test    byte ptr es:_memory[0ff70h],80h ;TTKS
        jnz     testT
        or      byte ptr es:_memory[0ff70h],80h
        call    _gettty
        mov     es,memseg
        mov     byte ptr es:_memory[0ff72h],al
testT:  test    si,10h
        jz      testD
        mov     ax,12
        push    ax
	call    _intrr
testD:	xor	ax,ax
	mov	es,ax
	mov	al,es:46ch
	cmp	lasttime,al
	jz	testD1
	mov	lasttime,al
	test	si,11100000b
	jnz	testD1
	mov	ax,64
	push	ax
	call	_intrr
testD1:	mov	es,memseg
	dec     dev
        jnz     intdone
        mov     dev,100
testI:  test    si,11100000b
        jz      testK
        ret
testK:  mov     al,byte ptr es:_memory[0ff70h]  ;TTKS
        and     al,0c0h
        jns     testP
        cmp     al,0c0h
        jnz     testP
        mov     ax,30h
        push    ax
	call    _intrr
testP:  test    byte ptr es:_memory[0ff74h],40h ;TTPS
        jz      testL
        mov     ax,34h
        push    ax
	call    _intrr
testL:  test    byte ptr es:_memory[0ff4ch],40h ;LPS
        jz      intdone
        mov     ax,80h
        push    ax
	call    _intrr
intdone:
        ret
.data
c       dw      800
dev     dw      100
.code
_intrchk endp
END
