mirror of
https://github.com/Polprzewodnikowy/N64FlashcartMenu.git
synced 2024-11-22 02:29:19 +01:00
Use libcart to detect flashcart model instead of USB subsystem
This commit is contained in:
parent
bc280c7fc6
commit
f6b5a4081c
1
Makefile
1
Makefile
@ -17,6 +17,7 @@ SRCS = \
|
|||||||
boot/boot.c \
|
boot/boot.c \
|
||||||
boot/crc32.c \
|
boot/crc32.c \
|
||||||
boot/ipl2.S \
|
boot/ipl2.S \
|
||||||
|
flashcart/flashcart_utils.c \
|
||||||
flashcart/flashcart.c \
|
flashcart/flashcart.c \
|
||||||
flashcart/sc64/sc64_internal.c \
|
flashcart/sc64/sc64_internal.c \
|
||||||
flashcart/sc64/sc64.c \
|
flashcart/sc64/sc64.c \
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
|
#include <libcart/cart.h>
|
||||||
#include <libdragon.h>
|
#include <libdragon.h>
|
||||||
#include <usb.h>
|
#include <usb.h>
|
||||||
|
|
||||||
@ -40,16 +41,27 @@ static flashcart_t *flashcart = &((flashcart_t) {
|
|||||||
flashcart_error_t flashcart_init (void) {
|
flashcart_error_t flashcart_init (void) {
|
||||||
flashcart_error_t error;
|
flashcart_error_t error;
|
||||||
|
|
||||||
if (usb_initialize() == CART_NONE) {
|
// HACK: Because libcart reads PI config from address 0x10000000 when initializing
|
||||||
|
// we need to write safe value before running any libcart function.
|
||||||
|
// Data in SDRAM can be undefined on some flashcarts at this point
|
||||||
|
// and might result in setting incorrect PI config.
|
||||||
|
extern uint32_t cart_dom1;
|
||||||
|
cart_dom1 = 0x80371240;
|
||||||
|
|
||||||
|
if (!debug_init_sdfs("sd:/", -1)) {
|
||||||
return FLASHCART_ERROR_NOT_DETECTED;
|
return FLASHCART_ERROR_NOT_DETECTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (usb_getcart()) {
|
// NOTE: Flashcart model is extracted from libcart after debug_init_sdfs call is made
|
||||||
case CART_64DRIVE:
|
extern int cart_type;
|
||||||
case CART_EVERDRIVE:
|
|
||||||
|
switch (cart_type) {
|
||||||
|
case CART_CI: // 64drive
|
||||||
|
case CART_ED: // Original EverDrive-64
|
||||||
|
case CART_EDX: // Series X EverDrive-64
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case CART_SC64:
|
case CART_SC: // SC64
|
||||||
flashcart = sc64_get_flashcart();
|
flashcart = sc64_get_flashcart();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -61,12 +73,9 @@ flashcart_error_t flashcart_init (void) {
|
|||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!debug_init_sdfs("sd:/", -1)) {
|
#ifndef MENU_NO_USB_LOG
|
||||||
return FLASHCART_ERROR_SD_CARD_ERROR;
|
// NOTE: Some flashcarts doesn't have USB port, can't throw error here
|
||||||
}
|
debug_init_usblog();
|
||||||
|
|
||||||
#ifndef NDEBUG
|
|
||||||
assertf(debug_init_usblog(), "Couldn't initialize USB debugging");
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return FLASHCART_OK;
|
return FLASHCART_OK;
|
||||||
@ -80,10 +89,23 @@ flashcart_error_t flashcart_deinit (void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
flashcart_error_t flashcart_load_rom (char *rom_path, bool byte_swap) {
|
flashcart_error_t flashcart_load_rom (char *rom_path, bool byte_swap) {
|
||||||
|
flashcart_error_t error;
|
||||||
|
|
||||||
if ((rom_path == NULL) || (!file_exists(rom_path)) || (file_get_size(rom_path) < KiB(4))) {
|
if ((rom_path == NULL) || (!file_exists(rom_path)) || (file_get_size(rom_path) < KiB(4))) {
|
||||||
return FLASHCART_ERROR_ARGS;
|
return FLASHCART_ERROR_ARGS;
|
||||||
}
|
}
|
||||||
return flashcart->load_rom(rom_path, byte_swap);
|
|
||||||
|
if (cart_card_byteswap(byte_swap)) {
|
||||||
|
return FLASHCART_ERROR_INT;
|
||||||
|
}
|
||||||
|
|
||||||
|
error = flashcart->load_rom(rom_path);
|
||||||
|
|
||||||
|
if (cart_card_byteswap(false)) {
|
||||||
|
return FLASHCART_ERROR_INT;
|
||||||
|
}
|
||||||
|
|
||||||
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
flashcart_error_t flashcart_load_save (char *save_path, flashcart_save_type_t save_type) {
|
flashcart_error_t flashcart_load_save (char *save_path, flashcart_save_type_t save_type) {
|
||||||
|
@ -16,7 +16,6 @@
|
|||||||
typedef enum {
|
typedef enum {
|
||||||
FLASHCART_OK,
|
FLASHCART_OK,
|
||||||
FLASHCART_ERROR_NOT_DETECTED,
|
FLASHCART_ERROR_NOT_DETECTED,
|
||||||
FLASHCART_ERROR_SD_CARD_ERROR,
|
|
||||||
FLASHCART_ERROR_UNSUPPORTED,
|
FLASHCART_ERROR_UNSUPPORTED,
|
||||||
FLASHCART_ERROR_OUTDATED,
|
FLASHCART_ERROR_OUTDATED,
|
||||||
FLASHCART_ERROR_ARGS,
|
FLASHCART_ERROR_ARGS,
|
||||||
@ -40,7 +39,7 @@ typedef enum {
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
flashcart_error_t (*init) (void);
|
flashcart_error_t (*init) (void);
|
||||||
flashcart_error_t (*deinit) (void);
|
flashcart_error_t (*deinit) (void);
|
||||||
flashcart_error_t (*load_rom) (char *rom_path, bool byte_swap);
|
flashcart_error_t (*load_rom) (char *rom_path);
|
||||||
flashcart_error_t (*load_save) (char *save_path);
|
flashcart_error_t (*load_save) (char *save_path);
|
||||||
flashcart_error_t (*set_save_type) (flashcart_save_type_t save_type);
|
flashcart_error_t (*set_save_type) (flashcart_save_type_t save_type);
|
||||||
flashcart_error_t (*set_save_writeback) (uint32_t *sectors);
|
flashcart_error_t (*set_save_writeback) (uint32_t *sectors);
|
||||||
|
11
src/flashcart/flashcart_utils.c
Normal file
11
src/flashcart/flashcart_utils.c
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#include "flashcart_utils.h"
|
||||||
|
#include "utils/fs.h"
|
||||||
|
#include "utils/utils.h"
|
||||||
|
|
||||||
|
|
||||||
|
void fix_file_size (FIL *fil) {
|
||||||
|
// HACK: Align file size to the SD sector size to prevent FatFs from doing partial sector load.
|
||||||
|
// We are relying on direct transfer from SD to SDRAM without CPU intervention.
|
||||||
|
// Sending some extra bytes isn't an issue here.
|
||||||
|
fil->obj.objsize = ALIGN(f_size(fil), FS_SECTOR_SIZE);
|
||||||
|
}
|
17
src/flashcart/flashcart_utils.h
Normal file
17
src/flashcart/flashcart_utils.h
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
/**
|
||||||
|
* @file flashcart_utils.h
|
||||||
|
* @brief Flashcart utilities
|
||||||
|
* @ingroup flashcart
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef FLASHCART_UTILS_H__
|
||||||
|
#define FLASHCART_UTILS_H__
|
||||||
|
|
||||||
|
|
||||||
|
#include <fatfs/ff.h>
|
||||||
|
|
||||||
|
|
||||||
|
void fix_file_size (FIL *fil);
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
@ -8,6 +8,7 @@
|
|||||||
#include "utils/fs.h"
|
#include "utils/fs.h"
|
||||||
#include "utils/utils.h"
|
#include "utils/utils.h"
|
||||||
|
|
||||||
|
#include "../flashcart_utils.h"
|
||||||
#include "sc64_internal.h"
|
#include "sc64_internal.h"
|
||||||
#include "sc64.h"
|
#include "sc64.h"
|
||||||
|
|
||||||
@ -51,22 +52,10 @@ static flashcart_error_t load_to_flash (FIL *fil, void *address, size_t size, UI
|
|||||||
return FLASHCART_OK;
|
return FLASHCART_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void load_cleanup (FIL *fil) {
|
|
||||||
sc64_sd_set_byte_swap(false);
|
|
||||||
f_close(fil);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static flashcart_error_t sc64_init (void) {
|
static flashcart_error_t sc64_init (void) {
|
||||||
uint16_t major;
|
uint16_t major;
|
||||||
uint16_t minor;
|
uint16_t minor;
|
||||||
|
|
||||||
// HACK: Because libcart reads PI config from address 0x10000000 when initializing
|
|
||||||
// we need to write safe value before running any libcart function.
|
|
||||||
// Data in SDRAM can be undefined at this point and result in setting incorrect PI config.
|
|
||||||
extern uint32_t cart_dom1;
|
|
||||||
cart_dom1 = 0x80371240;
|
|
||||||
|
|
||||||
sc64_unlock();
|
sc64_unlock();
|
||||||
|
|
||||||
if (!sc64_check_presence()) {
|
if (!sc64_check_presence()) {
|
||||||
@ -127,7 +116,7 @@ static flashcart_error_t sc64_deinit (void) {
|
|||||||
return FLASHCART_OK;
|
return FLASHCART_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static flashcart_error_t sc64_load_rom (char *rom_path, bool byte_swap) {
|
static flashcart_error_t sc64_load_rom (char *rom_path) {
|
||||||
FIL fil;
|
FIL fil;
|
||||||
UINT br;
|
UINT br;
|
||||||
|
|
||||||
@ -135,10 +124,7 @@ static flashcart_error_t sc64_load_rom (char *rom_path, bool byte_swap) {
|
|||||||
return FLASHCART_ERROR_LOAD;
|
return FLASHCART_ERROR_LOAD;
|
||||||
}
|
}
|
||||||
|
|
||||||
// HACK: Align file size to the SD sector size to prevent FatFs from doing partial sector load.
|
fix_file_size(&fil);
|
||||||
// We are relying on direct transfer from SD to SDRAM without CPU intervention.
|
|
||||||
// Sending some extra bytes isn't an issue here.
|
|
||||||
fil.obj.objsize = ALIGN(f_size(&fil), FS_SECTOR_SIZE);
|
|
||||||
|
|
||||||
size_t rom_size = f_size(&fil);
|
size_t rom_size = f_size(&fil);
|
||||||
|
|
||||||
@ -147,11 +133,6 @@ static flashcart_error_t sc64_load_rom (char *rom_path, bool byte_swap) {
|
|||||||
return FLASHCART_ERROR_LOAD;
|
return FLASHCART_ERROR_LOAD;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sc64_sd_set_byte_swap(byte_swap) != SC64_OK) {
|
|
||||||
load_cleanup(&fil);
|
|
||||||
return FLASHCART_ERROR_INT;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool shadow_enabled = (rom_size > (MiB(64) - KiB(128)));
|
bool shadow_enabled = (rom_size > (MiB(64) - KiB(128)));
|
||||||
bool extended_enabled = (rom_size > MiB(64));
|
bool extended_enabled = (rom_size > MiB(64));
|
||||||
|
|
||||||
@ -160,53 +141,48 @@ static flashcart_error_t sc64_load_rom (char *rom_path, bool byte_swap) {
|
|||||||
size_t extended_size = extended_enabled ? rom_size - MiB(64) : 0;
|
size_t extended_size = extended_enabled ? rom_size - MiB(64) : 0;
|
||||||
|
|
||||||
if (f_read(&fil, (void *) (ROM_ADDRESS), sdram_size, &br) != FR_OK) {
|
if (f_read(&fil, (void *) (ROM_ADDRESS), sdram_size, &br) != FR_OK) {
|
||||||
load_cleanup(&fil);
|
f_close(&fil);
|
||||||
return FLASHCART_ERROR_LOAD;
|
return FLASHCART_ERROR_LOAD;
|
||||||
}
|
}
|
||||||
if (br != sdram_size) {
|
if (br != sdram_size) {
|
||||||
load_cleanup(&fil);
|
f_close(&fil);
|
||||||
return FLASHCART_ERROR_LOAD;
|
return FLASHCART_ERROR_LOAD;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sc64_set_config(CFG_ROM_SHADOW_ENABLE, shadow_enabled) != SC64_OK) {
|
if (sc64_set_config(CFG_ROM_SHADOW_ENABLE, shadow_enabled) != SC64_OK) {
|
||||||
load_cleanup(&fil);
|
f_close(&fil);
|
||||||
return FLASHCART_ERROR_INT;
|
return FLASHCART_ERROR_INT;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (shadow_enabled) {
|
if (shadow_enabled) {
|
||||||
flashcart_error_t error = load_to_flash(&fil, (void *) (SHADOW_ADDRESS), shadow_size, &br);
|
flashcart_error_t error = load_to_flash(&fil, (void *) (SHADOW_ADDRESS), shadow_size, &br);
|
||||||
if (error != FLASHCART_OK) {
|
if (error != FLASHCART_OK) {
|
||||||
load_cleanup(&fil);
|
f_close(&fil);
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
if (br != shadow_size) {
|
if (br != shadow_size) {
|
||||||
load_cleanup(&fil);
|
f_close(&fil);
|
||||||
return FLASHCART_ERROR_LOAD;
|
return FLASHCART_ERROR_LOAD;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sc64_set_config(CFG_ROM_EXTENDED_ENABLE, extended_enabled) != SC64_OK) {
|
if (sc64_set_config(CFG_ROM_EXTENDED_ENABLE, extended_enabled) != SC64_OK) {
|
||||||
load_cleanup(&fil);
|
f_close(&fil);
|
||||||
return FLASHCART_ERROR_INT;
|
return FLASHCART_ERROR_INT;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (extended_enabled) {
|
if (extended_enabled) {
|
||||||
flashcart_error_t error = load_to_flash(&fil, (void *) (EXTENDED_ADDRESS), extended_size, &br);
|
flashcart_error_t error = load_to_flash(&fil, (void *) (EXTENDED_ADDRESS), extended_size, &br);
|
||||||
if (error != FLASHCART_OK) {
|
if (error != FLASHCART_OK) {
|
||||||
load_cleanup(&fil);
|
f_close(&fil);
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
if (br != extended_size) {
|
if (br != extended_size) {
|
||||||
load_cleanup(&fil);
|
f_close(&fil);
|
||||||
return FLASHCART_ERROR_LOAD;
|
return FLASHCART_ERROR_LOAD;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sc64_sd_set_byte_swap(false) != SC64_OK) {
|
|
||||||
load_cleanup(&fil);
|
|
||||||
return FLASHCART_ERROR_INT;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (f_close(&fil) != FR_OK) {
|
if (f_close(&fil) != FR_OK) {
|
||||||
return FLASHCART_ERROR_LOAD;
|
return FLASHCART_ERROR_LOAD;
|
||||||
}
|
}
|
||||||
|
@ -11,8 +11,6 @@ static char *format_flashcart_error (flashcart_error_t error) {
|
|||||||
return "No error";
|
return "No error";
|
||||||
case FLASHCART_ERROR_NOT_DETECTED:
|
case FLASHCART_ERROR_NOT_DETECTED:
|
||||||
return "No flashcart hardware was detected";
|
return "No flashcart hardware was detected";
|
||||||
case FLASHCART_ERROR_SD_CARD_ERROR:
|
|
||||||
return "Error during SD card initialization";
|
|
||||||
case FLASHCART_ERROR_UNSUPPORTED:
|
case FLASHCART_ERROR_UNSUPPORTED:
|
||||||
return "Unsupported flashcart";
|
return "Unsupported flashcart";
|
||||||
case FLASHCART_ERROR_OUTDATED:
|
case FLASHCART_ERROR_OUTDATED:
|
||||||
|
Loading…
Reference in New Issue
Block a user