From 5442139ff45182b9ebbda035dd44672771b39b88 Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Sat, 14 Oct 2017 19:59:49 +0100 Subject: [PATCH] FatFS working #9 some main functions converted to use it mp3 converted to use it --- inc/ffconf.h | 6 +- src/diskio.c | 39 +++++--- src/main.c | 278 +++++++++++++++++++++++++++++---------------------- src/mp3.c | 51 ++++++++-- src/sd.c | 40 ++++---- 5 files changed, 248 insertions(+), 166 deletions(-) diff --git a/inc/ffconf.h b/inc/ffconf.h index a851024..0807499 100644 --- a/inc/ffconf.h +++ b/inc/ffconf.h @@ -68,7 +68,7 @@ / Locale and Namespace Configurations /---------------------------------------------------------------------------*/ -#define FF_CODE_PAGE 932 +#define FF_CODE_PAGE 437 /* This option specifies the OEM code page to be used on the target system. / Incorrect code page setting can cause a file open failure. / @@ -97,7 +97,7 @@ */ -#define FF_USE_LFN 0 +#define FF_USE_LFN 1 #define FF_MAX_LFN 255 /* The FF_USE_LFN switches the support for LFN (long file name). / @@ -210,7 +210,7 @@ / buffer in the filesystem object (FATFS) is used for the file data transfer. */ -#define FF_FS_EXFAT 0 +#define FF_FS_EXFAT 1 /* This option switches support for exFAT filesystem. (0:Disable or 1:Enable) / When enable exFAT, also LFN needs to be enabled. / Note that enabling exFAT discards ANSI C (C89) compatibility. */ diff --git a/src/diskio.c b/src/diskio.c index 8702a30..9e2128d 100644 --- a/src/diskio.c +++ b/src/diskio.c @@ -253,33 +253,48 @@ DRESULT disk_ioctl ( ) { DRESULT res; - int result; + // int result; - switch (pdrv) { - case DEV_RAM : + // switch (pdrv) { + // case DEV_RAM : - // Process of the command for the RAM drive + // // Process of the command for the RAM drive - return res; + // return res; - case DEV_MMC : + // case DEV_MMC : - // Process of the command for the MMC/SD card + // // Process of the command for the MMC/SD card - return res; + // return res; - case DEV_USB : + // case DEV_USB : - // Process of the command the USB drive + // // Process of the command the USB drive - return res; - } + // return res; + // } + + switch (cmd) { + case CTRL_SYNC: + return RES_OK; + case GET_SECTOR_SIZE: + *(WORD*)buff = 512; + return RES_OK; + case GET_SECTOR_COUNT: + //*(DWORD*)buff = sdGetSectors(); + return RES_OK; + case GET_BLOCK_SIZE: + //*(DWORD*)buff = sdGetBlockSize(); + return RES_OK; + } return RES_PARERR; } DWORD get_fattime (void) { + //TODO: can we use the V3 RTC? return 0; } diff --git a/src/main.c b/src/main.c index e6d3955..d1de5ad 100644 --- a/src/main.c +++ b/src/main.c @@ -21,8 +21,8 @@ //filesystem #include "sd.h" +#include "ff.h" #include "fat_old.h" - //utils #include "utils.h" @@ -144,6 +144,8 @@ u8 empty = 0; u8 playing = 0; u8 gb_load_y = 0; +FATFS *fs; + //start with filebrowser menu key settings enum InputMap { @@ -899,155 +901,175 @@ void clearScreen(display_context_t disp) void romInfoScreen(display_context_t disp, u8 *buff, int silent) { - if (silent != 1) - sleep(10); - - u8 tmp[32]; u8 filename[64]; - u8 ok = 0; - sprintf(filename, "%s", buff); + int swapped = 0; - FatRecord rec_tmpf; - //not needed any longer :> - //file IS there, it's selected at this point - ok = fatFindRecord(filename, &rec_tmpf, 0); - - u8 resp = 0; - - resp = fatOpenFileByName(filename, 0); //err if not found ^^ - - int mb = file.sec_available / 2048; - int block_offset = 0; - u32 cart_buff_offset = 0; - u32 begin_sector = file.sector; - - //filesize -> readfile / 512 + FRESULT result; + int fsize = 512; //rom-headersize 4096 but the bootcode is not needed unsigned char headerdata[fsize]; //1*512 - resp = fatReadFile(&headerdata, fsize / 512); //1 cluster + FIL file; + UINT bytesread; + result = f_open(&file, filename, FA_READ); - int sw_type = is_valid_rom(headerdata); - - if (sw_type != 0) + if (result == FR_OK) { - swapped = 1; - swap_header(headerdata, 512); - } + int fsizeMB = f_size(&file) / 1048576; //Bytes in a MB - //char 32-51 name - unsigned char rom_name[32]; + result = + f_read ( + &file, /* [IN] File object */ + &headerdata, /* [OUT] Buffer to store read data */ + fsize, /* [IN] Number of bytes to read */ + &bytesread /* [OUT] Number of bytes read */ + ); - for (int u = 0; u < 19; u++) - { - if (u != 0) - sprintf(rom_name, "%s%c", rom_name, headerdata[32 + u]); - else - sprintf(rom_name, "%c", headerdata[32 + u]); - } - //trim right spaces - //romname=trimmed rom name for filename - sprintf(rom_name, "%s", trim(rom_name)); + result = f_close(&file); - if (silent != 1) - printText(rom_name, 11, 19, disp); + int sw_type = is_valid_rom(headerdata); - sprintf(rom_name, "Size: %iMb", mb); - - if (silent != 1) - printText(rom_name, 11, -1, disp); - - //unique cart id for gametype - unsigned char cartID_str[12]; - sprintf(cartID_str, "ID: %c%c%c%c", headerdata[0x3B], headerdata[0x3C], headerdata[0x3D], headerdata[0x3E]); - - if (silent != 1) - printText(cartID_str, 11, -1, disp); - - int cic, save; - - cic = get_cic(&headerdata[0x40]); - - unsigned char cartID_short[4]; - sprintf(cartID_short, "%c%c\0", headerdata[0x3C], headerdata[0x3D]); - - if (get_cic_save(cartID_short, &cic, &save)) - { - if (silent != 1) - printText("found in db", 11, -1, disp); - unsigned char save_type_str[12]; - sprintf(save_type_str, "Save: %s", saveTypeToExtension(save, ext_type)); - if (silent != 1) - printText(save_type_str, 11, -1, disp); - - unsigned char cic_type_str[12]; - sprintf(cic_type_str, "CIC: CIC-610%i", cic); //TODO: need to take into account DD and Aleck CIC - if (silent != 1) - printText(cic_type_str, 11, -1, disp); - - //thanks for the db :> - //cart was found, use CIC and SaveRAM type - } - - if (silent != 1) - { - char box_path[32]; - - sprite_t *n64cover; - - sprintf(box_path, "/ED64/boxart/lowres/%c%c.png", headerdata[0x3C], headerdata[0x3D]); - - if (fatFindRecord(box_path, &rec_tmpf, 0) != 0) + if (sw_type != 0) { - //not found - sprintf(box_path, "/ED64/boxart/lowres/00.png"); + swapped = 1; + swap_header(headerdata, 512); } - n64cover = loadPng(box_path); - graphics_draw_sprite(disp, 81, 32, n64cover); - display_show(disp); - } - else - { - rom_config[1] = cic - 1; - rom_config[2] = save; - rom_config[3] = 0; //tv force off - rom_config[4] = 0; //cheat off - rom_config[5] = 0; //chk_sum off - rom_config[6] = 0; //rating - rom_config[7] = 0; //country - rom_config[8] = 0; //reserved - rom_config[9] = 0; //reserved + if (silent != 1) + { + //char 32-51 name + unsigned char rom_name[32]; + + for (int u = 0; u < 19; u++) + { + if (u != 0) + sprintf(rom_name, "%s%c", rom_name, headerdata[32 + u]); + else + sprintf(rom_name, "%c", headerdata[32 + u]); + } + + //rom name + sprintf(rom_name, "%s", trim(rom_name)); + printText(rom_name, 11, 19, disp); + + //rom size + sprintf(rom_name, "Size: %iMB", fsizeMB); + printText(rom_name, 11, -1, disp); + + + //unique cart id for gametype + unsigned char cartID_str[12]; + sprintf(cartID_str, "ID: %c%c%c%c", headerdata[0x3B], headerdata[0x3C], headerdata[0x3D], headerdata[0x3E]); + printText(cartID_str, 11, -1, disp); + } + + int cic, save; + + cic = get_cic(&headerdata[0x40]); + + unsigned char cartID_short[4]; + sprintf(cartID_short, "%c%c\0", headerdata[0x3C], headerdata[0x3D]); + + if (get_cic_save(cartID_short, &cic, &save)) + { + if (silent != 1) + { + printText("found in db", 11, -1, disp); + unsigned char save_type_str[12]; + sprintf(save_type_str, "Save: %s", saveTypeToExtension(save, ext_type)); + printText(save_type_str, 11, -1, disp); + + unsigned char cic_type_str[12]; + + switch (cic) + { + case 4: + sprintf(cic_type_str, "CIC: CIC-5101", cic); + break; + case 7: + sprintf(cic_type_str, "CIC: CIC-5167", cic); + break; + default: + sprintf(cic_type_str, "CIC: CIC-610%i", cic); + break; + } + + printText(cic_type_str, 11, -1, disp); + } + //thanks for the db :> + //cart was found, use CIC and SaveRAM type + } + + if (silent != 1) + { + char box_path[32]; + + sprite_t *n64cover; + + sprintf(box_path, "/ED64/boxart/lowres/%c%c.png", headerdata[0x3C], headerdata[0x3D]); + + FILINFO fnoba; + result = f_stat (box_path, &fnoba); + + if (result != FR_OK) + { + //not found + sprintf(box_path, "/ED64/boxart/lowres/00.png"); + } + + n64cover = loadPng(box_path); + graphics_draw_sprite(disp, 81, 32, n64cover); + display_show(disp); + } + else + { + rom_config[1] = cic - 1; + rom_config[2] = save; + rom_config[3] = 0; //tv force off + rom_config[4] = 0; //cheat off + rom_config[5] = 0; //chk_sum off + rom_config[6] = 0; //rating + rom_config[7] = 0; //country + rom_config[8] = 0; //reserved + rom_config[9] = 0; //reserved + } } } sprite_t *loadPng(u8 *png_filename) { - u8 *filename; - u8 ok = 0; - - filename = (u8 *)malloc(slen(png_filename)); - //config filename - + u8 *filename = (u8 *)malloc(slen(png_filename)); sprintf(filename, "%s", png_filename); - FatRecord rec_tmpf; - ok = fatFindRecord(filename, &rec_tmpf, 0); - u8 resp = 0; - resp = fatOpenFileByName(filename, 0); + FRESULT result; + FIL file; + UINT bytesread; + result = f_open(&file, filename, FA_READ); - //filesize of the opend file -> is the readfile / 512 - int fsize = file.sec_available * 512; + if (result == FR_OK) + { + int fsize = f_size(&file); + u8 png_rawdata[fsize]; - u8 png_rawdata[fsize]; + result = + f_read ( + &file, /* [IN] File object */ + &png_rawdata, /* [OUT] Buffer to store read data */ + fsize, /* [IN] Number of bytes to read */ + &bytesread /* [OUT] Number of bytes read */ + ); - resp = fatReadFile(&png_rawdata, fsize / 512); //1 cluster + result = f_close(&file); + + free(filename); + return loadImage32(png_rawdata, fsize); + } + + return 0; - return loadImage32(png_rawdata, fsize); - free(filename); } void loadgbrom(display_context_t disp, u8 *buff) @@ -1723,6 +1745,13 @@ void initFilesystem(void) evd_ulockRegs(); sleep(1000); + fs = malloc(sizeof (FATFS)); /* Get work area for the volume */ + FRESULT result = f_mount(fs,"",1); + if(result != FR_OK) + { + //printText("mount error", 11, -1, disp); + } + fatInitRam(); fatInit(); fat_initialized = 1; @@ -2158,6 +2187,9 @@ void bootRom(display_context_t disp, int silent) graphics_fill_screen(disp, 0x000000FF); display_show(disp); + f_mount(0, "", 0); /* Unmount the default drive */ + free(fs); /* Here the work area can be discarded */ + simulate_boot(boot_cic, bios_cic, cheat_lists); // boot_cic } } @@ -4294,6 +4326,8 @@ int main(void) else { printf("Filesystem failed to start!\n"); + f_mount(0, "", 0); /* Unmount the default drive */ + free(fs); /* Here the work area can be discarded */ for ( ;; ) ; //never leave! } diff --git a/src/mp3.c b/src/mp3.c index f8b1f2e..f8e9794 100644 --- a/src/mp3.c +++ b/src/mp3.c @@ -7,11 +7,11 @@ #include #include #include -#include -#include "fat_old.h" -#include "mp3.h" - #include +#include +#include "mp3.h" +#include "ff.h" + static struct mad_stream Stream; static struct mad_header Header; @@ -68,13 +68,11 @@ static int mp3_seek(char* fd, int offset, int whence) { } static int mp3_size(char* fd) { - FatRecord rec_tmpf; - u8 resp=0; - resp = fatOpenFileByName(fd, 0); //err if not found ^^ - - int fsize = file.sec_available*512; //fsize in bytes - mp3File_fsize = fsize; - //todo filesize + FRESULT result; + FILINFO fno; + //TODO: error + result = f_stat (fd, &fno); + mp3File_fsize = fno.fsize; return mp3File_fsize; } @@ -96,6 +94,37 @@ static void _f_read(char* fname, unsigned char *readBuffer, int size){ } //dma_write_s(buffer, 0xb0000000, fsize); */ + + mp3_size(fname); + + FRESULT result; + FIL file; + UINT bytesread; + result = f_open(&file, fname, FA_READ); + + if (result == FR_OK) + { + int fsize = f_size(&file); + + if ( fsize > size) + { + //todo: set the read pointer + //readBuffer+mp3File_fptr + } + + result = + f_read ( + &file, /* [IN] File object */ + &readBuffer, /* [OUT] Buffer to store read data */ + size, /* [IN] Number of bytes to read */ + &bytesread /* [OUT] Number of bytes read */ + ); + + result = f_close(&file); + + mp3File_fptr+=size; + //dma_write_s(buffer, 0xb0000000, fsize); + } } static int mp3_read(char* fd, unsigned char *ptr, int size) diff --git a/src/sd.c b/src/sd.c index 0b67921..ccea5c8 100644 --- a/src/sd.c +++ b/src/sd.c @@ -12,28 +12,32 @@ #include "errors.h" #include "sys.h" -#define CMD0 0x40 // software reset -#define CMD1 0x41 // brings card out of idle state -#define CMD8 0x48 // Reserved -#define CMD12 0x4C // stop transmission on multiple block read -#define CMD17 0x51 // read single block -#define CMD18 0x52 // read multiple block -#define CMD58 0x7A // reads the OCR register -#define CMD55 0x77 -#define CMD41 0x69 -#define CMD24 0x58 // writes a single block -#define CMD25 0x59 // writes a multi block -#define ACMD41 0x69 -#define ACMD6 0x46 +#define CMD0 (0x40+0) // GO_IDLE_STATE, Software reset. +#define CMD1 (0x40+1) // SEND_OP_COND, Initiate initialization process. +#define CMD2 (0x40+2) //read cid +#define CMD3 (0x40+3) //read rca +#define CMD6 (0x40+6) +#define CMD7 (0x40+7) +#define CMD8 (0x40+8) // SEND_IF_COND, For only SDC V2. Check voltage range. +#define CMD9 (0x40+9) // SEND_CSD, Read CSD register. +#define CMD10 (0x40+10) // SEND_CID, Read CID register. +#define CMD12 (0x40+12) // STOP_TRANSMISSION, Stop to read data. +#define ACMD13 (0xC0+13) // SD_STATUS (SDC) +#define CMD16 (0x40+16) // SET_BLOCKLEN, Change R/W block size. +#define CMD17 (0x40+17) // READ_SINGLE_BLOCK, Read a block. +#define CMD18 (0x40+18) // READ_MULTIPLE_BLOCK, Read multiple blocks. +#define ACMD23 (0xC0+23) // SET_WR_BLK_ERASE_COUNT (SDC) +#define CMD24 (0x40+24) // WRITE_BLOCK, Write a block. +#define CMD25 (0x40+25) // WRITE_MULTIPLE_BLOCK, Write multiple blocks. +#define CMD41 (0x40+41) // SEND_OP_COND (ACMD) +#define ACMD41 (0xC0+41) // SEND_OP_COND (SDC) +#define CMD55 (0x40+55) // APP_CMD, Leading command of ACMD command. +#define CMD58 (0x40+58) // READ_OCR, Read OCR. + #define SD_V2 2 #define SD_HC 1 -#define CMD2 0x42 //read cid -#define CMD3 0x43 //read rca -#define CMD7 0x47 -#define CMD9 0x49 -#define CMD6 0x46 #define R1 1 #define R2 2