;search for 32g-bug in orig BK vyvod

;search for 32g-bug in orig

BK
vyvod   msg_3
assume  ds, [cs:orig_0]

        mov     si, 0
        mov     bp, 0
.next_32g_orig:
        cmp     si, 0fff0h
        jb      @F

.not_32g:
        jmp     .skip_32g_orig
@@:
        inc     si

        cmp     dword [ds:si], 066207453h       ;'push bx'/'je xxxx'
        jne     .next_32g_orig          ;'push eax'
;...
        cmp     dword [ds:si+36], 0024f8b26h    ;'mov cx,es:[bx][00002]'
        jne     .next_32g_orig

;store 32g-orig
        mov     bp, si
.skip_32g_orig:

push    bp
        
;open "0d_i13h.tmp" to [cs:orig_1]
open_file       name_0d_i13h_tmp, [cs:id_0d_i13h_tmp], msg_open_1_bin
size_file       name_0d_i13h_tmp        ;result in EAX
        mov     [cs:length_0d_i13h_tmp], ax
read_file       [cs:id_0d_i13h_tmp], [cs:orig_1], 0, [cs:length_0d_i13h_tmp]

pop     bp

;search for 32g-bug
assume  ds, [cs:orig_1]
        mov     si, 0
        mov     di, 0
.next_32g:
        cmp     si, [cs:length_0d_i13h_tmp]     ;0fff0h
        jb      @F

        jmp     .skip_32g
@@:
        inc     si

        cmp     dword [ds:si], 066207453h       ;'push bx'/'je xxxx'
        jne     .next_32g                       ;'push eax'
;...
        cmp     dword [ds:si+36], 0024f8b26h    ;'mov cx,es:[bx][00002]'
        jne     .next_32g

        cmp     bp, 0
        je      @F

        mov     di, si

.skip_32g:

;;poka - SKIP!!!! - neskolko vyzovov bl=0d.... ->???
;       mov     di, 0

;check - found 32g-problem in any area? (orig or i13h)
        mov     ax, 0
        or      ax, bp
        or      ax, di
        cmp     ax, 0
        je      .not32g_bug     ;0 = not found

;store 32g-sub
mov_bottom_data _AMI32G, length__AMI32G

;current [cs:bottom_data_codes] = enter point for 'jmp'
        mov     si, _call__AMI32G
assume  [cs:si+1], [cs:bottom_data_codes]


        cmp     bp, 0           ;found in orig?
        je      .not_32g_in_orig

off_tbl 50h, bp, _call__AMI32G, length__call__AMI32G
.not_32g_in_orig:

        cmp     di, 0           ;found in int13h?
        je      .not_32g_in_int13h

        mov     bp, di
off_tbl_6000_ami 0dh, bp, _call__AMI32G, length__call__AMI32G
.not_32g_in_int13h:

vyvod   msg_fixed
        jmp     .kon_32g

.not32g_bug:

vyvod   msg_not_found
.kon_32g:

_AMI32G:
;put on place "push bx" - in new i13h BX not changed -> havn`t to do "push"
;old 13h-bug = 40bytes

;Word (50:49): Capabilities

;Word (61:60): Total number of user addressable sectors

;Words (84:82): Features/command sets supported
;Words (84:82) shall indicate features/command sets supported. If a defined bit is cleared to zero, the indicated
;features/command set is not supported. If bit 14 of word 83 is set to one and bit 15 of word 83 is cleared to
;zero, the contents of words (83:82) contain valid support information. If not, support information is not valid in
;these words. If bit 14 of word 84 is set to one and bit 15 of word 84 is cleared to zero, the contents of word 84
;contains valid support information. If not, support information is not valid in this word.

;If bit 10 of word 83 is set to one, the 48-bit Address feature set is supported.

;Words (87:85): Features/command sets enabled
;If bit 10 of word 86 is set to one, the 48-bit Address feature set is supported.

;Words (103:100): Maximum user LBA for 48-bit Address feature set
        jz      .skip

push    eax
        cmp     eax, 66059280           ;32Gb = 03EFFC10h
        jbe     .small

        mov     byte [es:bx+0Ch], 0FFh  ;Heads

        cmp     eax, 267382800                  ;128Gb = 0FEFF010h
        jbe     .small

        mov     byte [es:bx+6], 16      ;FFh    ;Sectors
        mov     cx, 65535               ;Cylinders
pop     eax
        jmp     .exit

.small:
        mov     al, [es:bx+0Ch]         ;Heads
        mul     byte [es:bx+6]          ;Sectors
        mov     cx, ax                  ;CX=H*S

        pop     ax                      ;low count sectors
        pop     dx                      ;high count sectors
        div     cx                      ;/(H*S)

        shr     cx, 1                   ;(H*S)/2
        cmp     dx, cx
        jb      @F
        inc     ax
@@:
        mov     cx, ax                  ;=Cylinders
        jmp     .exit

.skip:
        mov     cx, [es:bx+2]           ;=Cylinders
.exit:
        retf

length__AMI32G = $ - _AMI32G


_call__AMI32G:
        call    0F000h:3232h
        jmp     $+35
length__call__AMI32G = $ - _call__AMI32G