Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1514

Here's some code to convert between LBA and CHS forms of drive sector addressing

$
0
0
Put this code in a module, and set the first 3 constants to accurately describe the drive you are working with (these are the drive geometry parameters). In the code here, I already have these set for the maximum allowed values (describes a harddrive that is just under 8GB in size, which is the largest you can get with CHS addressing). Once those drive geometry constants are set for the drive you want to do conversions for, just call these conversion functions from anywhere in your VB6 code, to convert between CHS and LBA addressing modes.

Code:

Const SectorsPerTrack As Long = 63
Const NumberOfHeads As Long = 255
Const NumberOfCylindars As Long = 1024

Const TotalSectorCount As Long = SectorsPerTrack * NumberOfHeads * NumberOfCylindars



Public Sub CHStoLBA(ByVal C As Long, ByVal H As Long, ByVal S As Long, ByRef LBA As Long)
    If S > SectorsPerTrack Then Exit Sub
    If H >= NumberOfHeads Then Exit Sub
    If C >= NumberOfCylindars Then Exit Sub
    LBA = (C * NumberOfHeads + H) * SectorsPerTrack + S - 1
End Sub

Public Sub LBAtoCHS(ByVal LBA As Long, ByRef C As Long, ByRef H As Long, ByRef S As Long)
    If LBA >= TotalSectorCount Then Exit Sub
    S = (LBA Mod SectorsPerTrack) + 1
    H = (LBA \ SectorsPerTrack) Mod NumberOfHeads
    C = (LBA \ SectorsPerTrack) \ NumberOfHeads
End Sub


Viewing all articles
Browse latest Browse all 1514

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>