Files
ESP8266lib/ext/sd/main.cpp
2021-02-21 09:33:08 +01:00

153 lines
3.8 KiB
C++

#include <stdexcept>
#include <iostream>
//#define debugMod(module, str) {printf("i[%-10s] ", module); printf(str); printf("\n");}
//#define debugMod1(module, str, v1) {printf("i[%-10s] ", module); printf(str, v1); printf("\n");}
//#define debugMod2(module, str, v1, v2) {printf("i[%-10s] ", module); printf(str, v1, v2); printf("\n");}
//#define debugMod3(module, str, v1, v2, v3) {printf("i[%-10s] ", module); printf(str, v1, v2, v3); printf("\n");}
//#define debugMod4(module, str, v1, v2, v3, v4) {printf("i[%-10s] ", module); printf(str, v1, v2, v3, v4); printf("\n");}
//#define debugMod5(module, str, v1, v2, v3, v4, v5) {printf("i[%-10s] ", module); printf(str, v1, v2, v3, v4, v5); printf("\n");}
#define PLATFORM DESKTOP
#include "../../Debug.h"
#include "MBR.h"
#include "fat32/FS.h"
#include "AccessHelper.h"
#include <fstream>
#include <fmt/format.h>
//class Simu {
// FILE* f;
//public:
// Simu(const char* image) {
// f = fopen(image, "rw");
// if (!f) {throw std::runtime_error("failed to open");}
// }
// uint32_t readSingleBlock(LBA512 addr, uint8_t* dst) {
// Log::addInfo("SD", "read512(%d*512)", addr);
// fseek(f, addr*512, SEEK_SET);
// return fread(dst, 512, 1, f) * 512;
// }
// uint32_t writeSingleBlock(LBA512 addr, const uint8_t* src) {
// Log::addInfo("SD", "write512(%d*512)", addr);
// fseek(f, addr*512, SEEK_SET);
// return fwrite(src, 512, 1, f) * 512;
// }
//};
class Simu {
uint8_t* data;
public:
Simu(const char* image) {
FILE* f = fopen(image, "rw");
if (!f) {throw std::runtime_error("failed to open");}
fseek(f, 0L, SEEK_END);
size_t size = ftell(f);
fseek(f, 0L, SEEK_SET);
data = (uint8_t*) malloc(size);
fread(data, size, 1, f);
fclose(f);
}
uint32_t readBlock(LBA512 addr, uint8_t* dst) {
Log::addInfo("SD", "read512(%d*512)", addr);
memcpy(dst, data+addr*512, 512);
return 512;
}
uint32_t writeBlock(LBA512 addr, const uint8_t* src) {
Log::addInfo("SD", "write512(%d*512)", addr);
memcpy(data+addr*512, src, 512);
return 512;
}
};
#include <gtest/gtest.h>
//#define logInfo(fmt, ...) std::string s = fmt::format(FMT_STRING(fmt), __VA_ARGS__);
int main(int argc, char** argv) {
//logInfo("{:s}", "I am not a number")
//std::string s = fmt::format(FMT_STRING("{:s}"), "I am not a number");
//std::string s = fmt::format(FMT_STRING("{:s}"), "I am not a number");
::testing::InitGoogleTest(&argc, argv);
::testing::GTEST_FLAG(filter) = "*TestCreate*";
return RUN_ALL_TESTS();
// diff /tmp/ram/TETRIS.GB /apps/workspace/gbemu/tests/tetris.gb
Simu simu("/tmp/ram/1.dat");
AccessHelper<Simu> ah(simu);
MBR<AccessHelper<Simu>> mbr(ah);
if (mbr.isPresent() && mbr.isValid()) {
for (int i = 0; i < 4; ++i) {
std::cout << (int) mbr.getPartition(i).getType() << " - " << mbr.getPartition(i).getFirstSector() << " - " << mbr.getPartition(i).getNumSectors() << std::endl;
}
using FAT32FS = FAT32::FS<AccessHelper<Simu>>;
FAT32FS fat(ah, mbr.getPartition(0).getFirstSector() * 512);
FAT32FS::DirIterator dir = fat.getRoot();
while(false) {
FAT32::DirEntryAt dea = dir.nextUsable();
if (!dea.isValid()) {break;}
if (1==0) {
FAT32FS::File2 f = fat.open2(dea);
if (1==0) {
uint8_t* bufff = (uint8_t*) malloc(1024*1024);
uint32_t read = f.read(f.getSize(), bufff);
std::string name = f.getName();
std::ofstream out("/tmp/ram/" + name);
out.write((char*)bufff, read);
out.close();
free(bufff);
break;
}
}
}
//FAT32::DirEntryAt root = fat.getRoot().cur();
FAT32FS::File2 file = fat.getOrCreateFile("tmp1.txt");
uint8_t src[128];
file.write(128, src);
// diff /tmp/ram/TETRIS.GB /apps/workspace/gbemu/tests/tetris.gb
// diff /tmp/ram/KIRBY1.GB /apps/workspace/gbemu/tests/Kirby\'s\ Dream\ Land\ \(USA\,\ Europe\).gb
}
}