rdos/kernel/fat.asm

126 lines
1.7 KiB
NASM

section .bss
clus resw 1
section .text
; Set cluster for next operations
; IN dx cluster number
setclus mov [clus], dx
ret
; get ptr to single byte in fat table
; IN dx position byte in fat table
; OUT bx ptr to byte in dskbuf
_fat1 push dx
mov ax, dx
xor dx, dx
mov cl, 9
shr ax, cl
add ax, [bpb+BPBRSC]
adc dx, 0
call map
pop dx
mov bx, dx
and bx, 0x1FF
add bx, dskbuf
ret
; get ptr to word in fat table
_fat2 push dx
mov ax, dx
xor dx, dx
mov cl, 8
shr ax, cl
add ax, [bpb+BPBRSC]
adc dx, 0
call map
pop dx
mov bl, dl
mov bh, 0
add bx, bx
add bx, dskbuf
ret
; read value from fat table
; index given with setclus
; OUT bx value
; carry set if EOF marker
rdfat mov dx, [clus]
mov ax, dx
shr ax, 1
add dx, ax
call _fat1
push word [bx]
inc dx
call _fat1
mov si, bx
pop bx
mov bh, [si]
test word [clus], 1
jz l001
mov cl, 4
shr bx, cl
l001 and bx, 0xFFF
cmp bx, 0xFF0
cmc
ret
; set value in FAT table
; IN dx value
wrfat and dh, 0x0F
push dx
; DX = index * 1.5
mov dx, [clus]
mov ax, dx
shr dx, 1
add dx, ax
call _fat1
pop ax
test [clus], 1
jnz l004
; even, first byte full
mov [bx], al
push ax
call dirty
; DX = index * 1.5
mov dx, [clus]
mov ax, dx
shr dx, 1
add dx, ax
inc dx
call _fat1
pop ax
mov al, [bx]
and al, 0xF0
or al, ah
mov [bx], al
call dirty
jmp l005
; odd, second byte full
l004: mov ch, [bx]
; concat existing and our nib
and ch, 0x0F
mov cl, 4
shl ax, cl
or al, ch
; write back lower 4 bits
mov [bx], al
call dirty
push ax
; re-calculate offset
mov ax, dx
shr dx, 1
add dx, ax
inc dx
; map second byte
call _fat1
pop ax
; write higher 8 bits
mov [bx], ah
call dirty
l005: ret
; wrfat from above, FAT16 flavour
wf16: ret