A C++ FAT32 implementation

Programming the 6502 microprocessor and its relatives in assembly and other languages.
Post Reply
User avatar
AndrewP
Posts: 368
Joined: 30 Aug 2021
Location: South Africa

A C++ FAT32 implementation

Post by AndrewP »

Sometimes have a second implementation of something is handy so as there is a bit of chatter about FAT32 I'm posting a C++ implementation I've done below (that includes reading, writing, seeking, formatting):

It was initially based on (stolen from) Fernando Rodriguez's implementation and is pretty thoroughly tested. The two big changes I made were to rewrite it in C++ with a focus on shorter more readable methods and adding a cache behind the file system. I've used it for reading SD cards on a Raspberry Pi Pico but it's ultimately supposed to be the file system for a 65C816 based computer.

A good starting point is:
FatFile.h
FatFile.cpp
that includes the Open, Close, Write, Read, Seek, Flush, Truncate, Tell and Size methods.

and from there backing up to the:
FatCommon.h
file that defines all of the FAT file structures.

then if you're brave take a look in:
FatVolume.h
FatVolume.cpp
that finally do the work to deal with the awful mangled cluster system that is FAT.

a FAT Volume sits on top of an implementation of a:
FileDrive.h
FileDrive.cpp
that is some form of block device. In my case an SD card whose implementation is in another library.

Tests can be found in:
TestFat32.cpp
TestFatCache.cpp

If you want to get this compiling and running you'll need Windows with Visual Studio 2019 and then clone the two repositories:

Code: Select all

git@github.com:andrewpaterson/Codaphela.Library.git

Code: Select all

git@github.com:andrewpaterson/Codaphela.Test.git
and then message me because I think there is one project level variable that needs to be setup and I cannot remember what it is off of the of my head. And I'm assuming people are only going to read this implementation for reference, not try and run it.
Attachments
VisualStudioFAT.PNG
Post Reply