Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ed64 vseries #15

Draft
wants to merge 68 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
a66a0b3
Checkin initial bios files
networkfusion Jul 17, 2023
1b375e0
Rename functions to use `ed64_bios`
networkfusion Jul 17, 2023
42441fc
Improve comments
networkfusion Jul 17, 2023
4f5433e
Switch to libdragon `wait_ms`
networkfusion Jul 17, 2023
cec3b0f
Minor comment improvements
networkfusion Jul 17, 2023
a3720db
Correct some function names
networkfusion Jul 17, 2023
ff1411f
Switch to `dma_wait`
networkfusion Jul 17, 2023
a0b9ec7
Improve function names
networkfusion Jul 17, 2023
ca2f74c
Add lock register
networkfusion Jul 17, 2023
b99e30f
Improve some names
networkfusion Jul 17, 2023
c07354c
Convert some defines to enums
networkfusion Jul 17, 2023
f8f4d03
Improve FPGA firmware variables
networkfusion Jul 17, 2023
e6be62d
Improve names
networkfusion Jul 17, 2023
aa0c80a
Further renames
networkfusion Jul 17, 2023
9689e92
Name and comment improvements
networkfusion Jul 19, 2023
64e761d
Improvements to function names
networkfusion Jul 19, 2023
45e9a3d
Add ED64.c base implementation
networkfusion Jul 19, 2023
cba010e
Update README.md
networkfusion Jul 19, 2023
72c2574
Add set save type
networkfusion Jul 19, 2023
a752026
Save type improvements
networkfusion Jul 19, 2023
c408198
Re-enable save writeback
networkfusion Jul 19, 2023
1834917
Improve cart metadata
networkfusion Jul 19, 2023
c67967e
Add note on flashram
networkfusion Jul 19, 2023
96b6fa2
Add implementation of ED64 load ROM
networkfusion Jul 19, 2023
b947fe2
Part enable loading a save.
networkfusion Jul 19, 2023
206ce62
Update README.md
networkfusion Jul 19, 2023
aa51f67
Update README.md
networkfusion Jul 19, 2023
cc23473
PR feedback improvements
networkfusion Jul 21, 2023
2e6e8ba
Merge remote-tracking branch 'upstream/main' into ed64-vseries
networkfusion Aug 21, 2023
96eef11
Re-align source.
networkfusion Aug 21, 2023
d26e082
Update libdragon
networkfusion Aug 21, 2023
c0850f8
ED64 can now load ROM's
networkfusion Aug 21, 2023
b17579e
Rename ED64 bios functions
networkfusion Aug 21, 2023
54ae9da
Enable save type set
networkfusion Aug 21, 2023
a2576e2
Move ed64 ll functions
networkfusion Aug 21, 2023
ef682dd
Further fixups
networkfusion Aug 21, 2023
3aa12fb
Fix for V2.5 and below
networkfusion Aug 21, 2023
5490efb
Improve comments.
networkfusion Aug 21, 2023
769f754
Fix commercial ROM boots.
networkfusion Aug 21, 2023
a3f7863
Improve notes.
networkfusion Aug 21, 2023
117c7a4
Fix saves
networkfusion Aug 21, 2023
a117a3e
Merge remote-tracking branch 'upstream/main' into ed64-vseries
networkfusion Dec 27, 2023
68293be
Fix merge
networkfusion Dec 27, 2023
49cf33a
Fix issues with ED64 flashcart functions
networkfusion Dec 28, 2023
4e764ae
Fix some comments
networkfusion Dec 28, 2023
983ecd9
Minor improvements
networkfusion Dec 29, 2023
25f1f56
Save improvements
networkfusion Dec 29, 2023
5d50f90
Correct EEPROM size
networkfusion Dec 29, 2023
ad739ed
Readme improvements
networkfusion Dec 29, 2023
d1ec7b5
Update .gitignore
networkfusion Dec 29, 2023
0b348ac
Fix ability to deploy and debug
networkfusion Dec 29, 2023
df207a3
Fix loading eeprom
networkfusion Dec 29, 2023
20619a4
Add device information function
networkfusion Dec 29, 2023
af742a2
Add RTC feature
networkfusion Dec 29, 2023
6c3e09a
Add flashcart info menu
networkfusion Dec 29, 2023
4ef726f
Add device info struct
networkfusion Dec 30, 2023
c943a8d
commented out changes
networkfusion Dec 30, 2023
f8cbbb2
Changes towards libcart
networkfusion Dec 31, 2023
4365def
Uncomment some functions
networkfusion Jan 1, 2024
f6bdd90
Uncomment USB
networkfusion Jan 1, 2024
84c4aef
Add USB feature
networkfusion Jan 1, 2024
4167533
Add some more comments
networkfusion Jan 1, 2024
99c3d39
Comment out USB feature for the moment
networkfusion Jan 10, 2024
4a6d471
Merge branch 'main' into ed64-vseries
networkfusion Jan 10, 2024
ccf643b
keep name order
networkfusion Jan 11, 2024
6054cbe
Merge branch 'main' into ed64-vseries
networkfusion Jan 17, 2024
0635d44
Update ed64.c
networkfusion Jan 17, 2024
c2aab0e
Update views.h
networkfusion Jan 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ SRCS = \
boot/ipl2.S \
flashcart/flashcart.c \
flashcart/sc64/sc64_internal.c \
flashcart/ed64/ed64_internal.c \
flashcart/sc64/sc64.c \
flashcart/ed64/ed64.c \
libs/mini.c/src/mini.c \
menu/actions.c \
menu/assets.c \
Expand Down
15 changes: 15 additions & 0 deletions src/flashcart/ed64/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
This folder allows support for the ED64 "Official" flashcart when using V series hardware.
For the moment it is unlikely to support clones.

It is based on OS 2.12.x as used in the "Unofficial Menu" (https://github.com/n64-tools/ED64-UnofficialOS-binaries/releases/tag/V2.12.9.3-Release)

It may require further cleansing and updating to use the latest libdragon before it will probably work based on the fact that using the latest libdragon with `libcart` seems to have a detremental effect on the ability to load ROM's.


Initially, this aims to work when loading the official ED64 OS menu, selecting the N64FlashcartMenu and then loading a ROM from there. This works around needing to load the correct firmware. To automate this, it is possible to set this menu to autoload (at least in the unofficial menu). i.e. You can set this ROM to auto load by placing a file called `autoexec.v64` in the `ED64` folder with the content of the file pointing to the path of this ROM.

Notes:

* `libcart` seems to use a different base register address `0x08040000`, but official implementation uses `0xA8040000`
* The ability to load/save `FLASHRAM` may not yet be supported.

288 changes: 288 additions & 0 deletions src/flashcart/ed64/ed64.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,288 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

#include <fatfs/ff.h>
#include <libdragon.h>

#include "utils/fs.h"
#include "utils/utils.h"

#include "ed64_internal.h"
#include "ed64.h"


// #define SRAM_FLASHRAM_ADDRESS (0x08000000)
// #define ROM_ADDRESS (0x10000000)

// #define SUPPORTED_MAJOR_VERSION (2)
// #define SUPPORTED_MINOR_VERSION (12)


// static flashcart_error_t load_to_flash (FIL *fil, void *address, size_t size, UINT *br) {
// size_t erase_block_size;
// UINT bp;

// *br = 0;

// if (ed64_flash_get_erase_block_size(&erase_block_size) != ED64_OK) {
// return FLASHCART_ERROR_INT;
// }

// while (size > 0) {
// size_t program_size = MIN(size, erase_block_size);
// if (ed64_flash_erase_block(address) != ED64_OK) {
// return FLASHCART_ERROR_INT;
// }
// if (f_read(fil, address, program_size, &bp) != FR_OK) {
// return FLASHCART_ERROR_LOAD;
// }
// if (ed64_flash_wait_busy() != ED64_OK) {
// return FLASHCART_ERROR_INT;
// }
// address += program_size;
// size -= program_size;
// *br += bp;
// }

// return FLASHCART_OK;
// }

static void load_cleanup (FIL *fil) {
ed64_bios_byteswap_off();
f_close(fil);
}


static flashcart_error_t ed64_init (void) {
// uint16_t major;
// 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;
networkfusion marked this conversation as resolved.
Show resolved Hide resolved

// ed64_bios_unlock_regs();

ed64_bios_init();
// ed64_bios_reset_spx(); // only required if V2+ FW is updating!

ed64_bios_set_ram_bank(1); // Seemingly bank 0 (for SRAM 128k) is only supported on V3 and it is not the default.
ed64_bios_gpio_mode_io();

// if (!ed64_check_presence()) {
// return FLASHCART_ERROR_UNSUPPORTED;
// }
// if (ed64_get_version(&major, &minor) != ED64_OK) {
// return FLASHCART_ERROR_OUTDATED;
// }
// if (major != SUPPORTED_MAJOR_VERSION) {
// return FLASHCART_ERROR_OUTDATED;
// }
// if (minor < SUPPORTED_MINOR_VERSION) {
// return FLASHCART_ERROR_OUTDATED;
// }

// bool writeback_pending;
// do {
// if (ed64_writeback_pending(&writeback_pending) != ED64_OK) {
// return FLASHCART_ERROR_INT;
// }
// } while (writeback_pending);

// const struct {
// ed64_cfg_t id;
// uint32_t value;
// } default_config[] = {
// { CFG_BOOTLOADER_SWITCH, false },
// { CFG_ROM_WRITE_ENABLE, false },
// { CFG_ROM_SHADOW_ENABLE, false },
// { CFG_DD_MODE, DD_MODE_DISABLED },
// { CFG_ISV_ADDRESS, 0x00000000 },
// { CFG_BOOT_MODE, BOOT_MODE_MENU },
// { CFG_SAVE_TYPE, SAVE_TYPE_NONE },
// { CFG_CIC_SEED, CIC_SEED_AUTO },
// { CFG_TV_TYPE, TV_TYPE_PASSTHROUGH },
// { CFG_DD_SD_ENABLE, false },
// { CFG_DD_DRIVE_TYPE, DRIVE_TYPE_RETAIL },
// { CFG_DD_DISK_STATE, DISK_STATE_EJECTED },
// { CFG_BUTTON_MODE, BUTTON_MODE_NONE },
// { CFG_ROM_EXTENDED_ENABLE, false },
// };

// for (int i = 0; i < sizeof(default_config) / sizeof(default_config[0]); i++) {
// if (ed64_set_config(default_config[i].id, default_config[i].value) != ED64_OK) {
// return FLASHCART_ERROR_INT;
// }
// }

return FLASHCART_OK;
}

static flashcart_error_t ed64_deinit (void) {
// // NOTE: Necessary because libcart enables ROM write by default
// ed64_set_config(CFG_ROM_WRITE_ENABLE, false);

ed64_bios_lock_regs();
networkfusion marked this conversation as resolved.
Show resolved Hide resolved
ed64_bios_gpio_mode_off();

return FLASHCART_OK;
}

static flashcart_error_t ed64_load_rom (char *rom_path, bool byte_swap) {
FIL fil;
UINT br;

if (f_open(&fil, rom_path, FA_READ) != FR_OK) {
return FLASHCART_ERROR_LOAD;
}

// 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);

size_t rom_size = f_size(&fil);

if (rom_size > MiB(78)) {
networkfusion marked this conversation as resolved.
Show resolved Hide resolved
f_close(&fil);
return FLASHCART_ERROR_LOAD;
}

if (byte_swap) {
ed64_bios_byteswap_on();
}


size_t sdram_size = rom_size;

if (f_read(&fil, (void *) (ROM_ADDRESS), sdram_size, &br) != FR_OK) {
load_cleanup(&fil);
return FLASHCART_ERROR_LOAD;
}
if (br != sdram_size) {
load_cleanup(&fil);
return FLASHCART_ERROR_LOAD;
}


ed64_bios_byteswap_off();

if (f_close(&fil) != FR_OK) {
return FLASHCART_ERROR_LOAD;
}

return FLASHCART_OK;
}

static flashcart_error_t ed64_load_save (char *save_path) {
void *address = NULL;
networkfusion marked this conversation as resolved.
Show resolved Hide resolved
// ed64_save_type_t type = ed64_bios_get_save_type();

// // if (ed64_get_config(CFG_SAVE_TYPE, &type) != ED64_OK) {
// // return FLASHCART_ERROR_INT;
// // }

// // switch (type) {
// // case SAVE_TYPE_EEPROM_4K:
// // case SAVE_TYPE_EEPROM_16K:
// // address = (void *) (EEPROM_ADDRESS);
// // break;
// // case SAVE_TYPE_SRAM:
// // case SAVE_TYPE_FLASHRAM:
// // case SAVE_TYPE_SRAM_BANKED:
// // address = (void *) (SRAM_FLASHRAM_ADDRESS);
// // break;
// // case SAVE_TYPE_NONE:
// // default:
// // return FLASHCART_ERROR_ARGS;
// // }

FIL fil;
UINT br;

if (f_open(&fil, save_path, FA_READ) != FR_OK) {
return FLASHCART_ERROR_LOAD;
}

size_t save_size = f_size(&fil);

if (f_read(&fil, address, save_size, &br) != FR_OK) {
f_close(&fil);
return FLASHCART_ERROR_LOAD;
}

if (f_close(&fil) != FR_OK) {
return FLASHCART_ERROR_LOAD;
}

if (br != save_size) {
return FLASHCART_ERROR_LOAD;
}

return FLASHCART_OK;
}

static flashcart_error_t ed64_set_save_type (flashcart_save_type_t save_type) {

ed64_save_type_t type = SAVE_TYPE_OFF;

switch (save_type) {
case FLASHCART_SAVE_TYPE_NONE:
type = SAVE_TYPE_OFF;
break;
case FLASHCART_SAVE_TYPE_EEPROM_4K:
type = SAVE_TYPE_EEP4k;
break;
case FLASHCART_SAVE_TYPE_EEPROM_16K:
type = SAVE_TYPE_EEP16k;
break;
case FLASHCART_SAVE_TYPE_SRAM:
type = SAVE_TYPE_SRAM;
break;
case FLASHCART_SAVE_TYPE_SRAM_BANKED:
case FLASHCART_SAVE_TYPE_SRAM_128K:
type = SAVE_TYPE_SRAM128;
break;
case FLASHCART_SAVE_TYPE_FLASHRAM:
type = SAVE_TYPE_FLASH;
break;
default:
return FLASHCART_ERROR_ARGS;
}

// if (ed64_set_config(CFG_SAVE_TYPE, type) != ED64_OK) {
// return FLASHCART_ERROR_INT;
// }

ed64_bios_set_save_type(type);

return FLASHCART_OK;
}

static flashcart_error_t ed64_set_save_writeback (uint32_t *sectors) {
// ed64_write_data(sectors, ED64_BUFFERS->BUFFER, 1024);

// if (ed64_writeback_enable(ED64_BUFFERS->BUFFER) != ED64_OK) {
// return FLASHCART_ERROR_INT;
// }

return FLASHCART_OK;
}
networkfusion marked this conversation as resolved.
Show resolved Hide resolved


static flashcart_t flashcart_ed64 = {
.init = ed64_init,
.deinit = ed64_deinit,
.load_rom = ed64_load_rom,
.load_save = ed64_load_save,
.set_save_type = ed64_set_save_type,
.set_save_writeback = ed64_set_save_writeback,
networkfusion marked this conversation as resolved.
Show resolved Hide resolved
};


flashcart_t *ed64_get_flashcart (void) {
return &flashcart_ed64;
}
23 changes: 23 additions & 0 deletions src/flashcart/ed64/ed64.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @file flashcart.h
* @brief ED64 Flashcart Utilities
* @ingroup flashcart
*/

#ifndef FLASHCART_ED64_H__
#define FLASHCART_ED64_H__


#include "../flashcart.h"

/**
* @addtogroup ed64
* @{
*/


flashcart_t *ed64_get_flashcart (void);

/** @} */ /* ed64 */

#endif
Loading