Skip to content

Commit

Permalink
fixed wii nand writing
Browse files Browse the repository at this point in the history
  • Loading branch information
kipcode66 committed Sep 27, 2024
1 parent 0760e64 commit c303ca4
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 24 deletions.
4 changes: 4 additions & 0 deletions external/gcn_c/include/nand.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
#define NAND_OPEN_WRITE 0x02
#define NAND_OPEN_RW (NAND_OPEN_READ | NAND_OPEN_WRITE)

#define NAND_SEEK_BEG 0
#define NAND_SEEK_CUR 1
#define NAND_SEEK_END 2

typedef struct NANDInfo {
uint8_t unk[0x90];
} NANDInfo;
Expand Down
48 changes: 34 additions & 14 deletions external/gcn_c/include/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@
#define OPEN_MODE_READ
#define OPEN_MODE_WRITE
#define OPEN_MODE_RW
#define STORAGE_SEEK_BEG 0
#define STORAGE_SEEK_CUR 1
#define STORAGE_SEEK_END 2
#else // WII_PLATFORM
#include "nand.h"
#define STORAGE_FILENAME_MAX NAND_FILENAME_MAX
#define FileInfo NANDInfo
#define OPEN_MODE_READ NAND_OPEN_READ
#define OPEN_MODE_WRITE NAND_OPEN_WRITE
#define OPEN_MODE_RW NAND_OPEN_RW
#define STORAGE_SEEK_BEG NAND_SEEK_BEG
#define STORAGE_SEEK_CUR NAND_SEEK_CUR
#define STORAGE_SEEK_END NAND_SEEK_END
#endif // WII_PLATFORM

enum StorageError {
Expand Down Expand Up @@ -45,17 +51,34 @@ typedef struct Storage {
int32_t result;
char file_name_buffer[STORAGE_FILENAME_MAX * 2];
uint32_t result_size;
uint32_t position;
} Storage;

#ifndef WII_PLATFORM
#define StorageCreate(ch, fileName, size, fileBuffer) CARDCreate(ch, fileName, size, fileBuffer)
#define StorageDelete(ch, fileName) CARDDelete(ch, fileName)
#define StorageOpen(ch, fileName, fileInfo, mode) CARDOpen(ch, fileName, fileInfo)
#define StorageClose(fileInfo) CARDClose(fileInfo)
#define StorageRead(storage, data, length, offset) \
({ (storage).result = CARDRead(&(storage).info, data, length, offset); })
#define StorageWrite(storage, data, length, offset) \
({ (storage).result = CARDWrite(&(storage).info, data, length, offset); })
#define StorageRead(storage, data, length) \
({ (storage).result = CARDRead(&(storage).info, data, length, (storage).position); (storage).position += length; (storage).result; })
#define StorageWrite(storage, data, length) \
({ (storage).result = CARDWrite(&(storage).info, data, length, (storage).position); (storage).position += length; (storage).result; })
inline int32_t StorageSeek(Storage* storage, int32_t offset, int32_t whence) {
switch (whence) {
case STORAGE_SEEK_BEG:
storage->position = (uint32_t)offset;
break;
case STORAGE_SEEK_CUR:
storage->position += offset;
break;
case STORAGE_SEEK_END:
storage->position = storage->info.length + offset;
break;
default:
return IoError;
}
return storage->position;
}
#else // WII_PLATFORM
#define StorageCreate(ch, fileName, size, fileBuffer) \
({ \
Expand All @@ -66,20 +89,17 @@ typedef struct Storage {
#define StorageDelete(ch, fileName) NANDDelete(fileName)
#define StorageOpen(ch, fileName, fileInfo, mode) NANDOpen(fileName, fileInfo, mode)
#define StorageClose(fileInfo) NANDClose(fileInfo)
#define StorageRead(storage, data, length, offset) \
#define StorageRead(storage, data, length) \
({ \
(storage).result = NANDSeek(&(storage).info, offset, 0); \
if ((storage).result == Ready) { \
(storage).result_size = NANDRead(&(storage).info, data, length); \
} \
(storage).result_size = NANDRead(&(storage).info, data, length); \
(storage).result = Ready; \
})
#define StorageWrite(storage, data, length, offset) \
#define StorageWrite(storage, data, length) \
({ \
(storage).result = NANDSeek(&(storage).info, offset, 0); \
if ((storage).result == Ready) { \
(storage).result_size = NANDWrite(&(storage).info, data, length); \
} \
(storage).result_size = NANDWrite(&(storage).info, data, length); \
(storage).result = Ready; \
})
#define StorageSeek(storage, offset, whence) NANDSeek(&(*(storage)).info, offset, whence)
#endif // WII_PLATFORM

#endif // __STORAGE_H__
39 changes: 31 additions & 8 deletions modules/boot/src/utils/card.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,27 @@ int32_t GZ_storageWrite(Storage* storage, void* data, int32_t size, int32_t offs
int32_t read_bytes = 0;

while (result == Ready && size > 0) {
StorageRead(*storage, buf, sector_size, (offset & ~(sector_size - 1)));
if (result != Ready) {
OSReport("GZ_storageWrite: Writing 0x%x bytes to offset 0x%x\n", MIN(size, sector_size), offset);
StorageSeek(storage, (offset & ~(sector_size - 1)), STORAGE_SEEK_BEG);
StorageRead(*storage, buf, sector_size);
if (storage->result != Ready) {
result = IoError;
break;
}
int32_t rem_size = sector_size - (offset & (sector_size - 1));
memcpy(buf + (offset & (sector_size - 1)), (void*)((uint32_t)data + read_bytes),
MIN(rem_size, size));
StorageWrite(*storage, buf, sector_size, (offset & ~(sector_size - 1)));
StorageSeek(storage, (offset & ~(sector_size - 1)), STORAGE_SEEK_BEG);
StorageWrite(*storage, buf, sector_size);
if (storage->result != Ready) {
result = IoError;
break;
}
read_bytes += MIN(rem_size, size);
size -= rem_size;
offset += rem_size;
}
OSReport("GZ_storageWrite: Done Writing\n");
delete[] buf;
return result;
}
Expand All @@ -62,7 +71,8 @@ int32_t GZ_storageRead(Storage* storage, void* data, int32_t size, int32_t offse
int32_t read_bytes = 0;

while (result == Ready && size > 0) {
StorageRead(*storage, buf, sector_size, (offset & ~(sector_size - 1)));
StorageSeek(storage, (offset & ~(sector_size - 1)), STORAGE_SEEK_BEG);
StorageRead(*storage, buf, sector_size);
if (result != Ready) {
break;
}
Expand Down Expand Up @@ -244,7 +254,11 @@ KEEP_FUNC void GZ_storeMemfile(Storage& storage) {
posData.cam.target = matrixInfo.matrix_info->target;
posData.cam.pos = matrixInfo.matrix_info->pos;
posData.angle = dComIfGp_getPlayer()->shape_angle.y;
uint32_t file_size = (uint32_t)(ceil((double)sizeof(dSv_info_c) / (double)storage.sector_size) *
OSReport("GZ_storeMemfile: position: {%f, %f, %f}\n", posData.link.x, posData.link.y, posData.link.z);
OSReport("GZ_storeMemfile: angle: %f\n", posData.angle);
OSReport("GZ_storeMemfile: cam target: {%f, %f, %f}\n", posData.cam.target.x, posData.cam.target.y, posData.cam.target.z);
OSReport("GZ_storeMemfile: cam pos: {%f, %f, %f}\n", posData.cam.pos.x, posData.cam.pos.y, posData.cam.pos.z);
uint32_t file_size = (uint32_t)(ceil((double)(sizeof(dSv_info_c) + 1 + sizeof(PositionData)) / (double)storage.sector_size) *
storage.sector_size);

storage.result = StorageDelete(0, storage.file_name_buffer);
Expand All @@ -258,10 +272,13 @@ KEEP_FUNC void GZ_storeMemfile(Storage& storage) {
setReturnPlace(g_dComIfG_gameInfo.play.mStartStage.mStage,
g_dComIfG_gameInfo.play.mEvent.field_0x12c, 0);

storage.result = GZ_storageWrite(&storage, &g_dComIfG_gameInfo, sizeof(dSv_info_c), 0,
uint8_t* data = new (-32) uint8_t[sizeof(dSv_info_c) + 1 + sizeof(PositionData)];
memcpy(data, &g_dComIfG_gameInfo, sizeof(dSv_info_c));
memcpy(&data[sizeof(dSv_info_c) + 1], &posData, sizeof(PositionData));
storage.result = GZ_storageWrite(&storage, data, sizeof(dSv_info_c) + 1 + sizeof(PositionData), 0,
storage.sector_size);
storage.result = GZ_storageWrite(&storage, &posData, sizeof(posData),
sizeof(dSv_info_c) + 1, storage.sector_size);
OSReport("GZ_storeMemfile: data write result: %d\n", storage.result);
delete[] data;
if (storage.result == Ready) {
FIFOQueue::push("saved memfile!", Queue);
} else {
Expand Down Expand Up @@ -332,7 +349,13 @@ KEEP_FUNC void GZ_loadMemfile(Storage& storage) {
storage.result = StorageOpen(0, storage.file_name_buffer, &storage.info, OPEN_MODE_RW);
if (storage.result == Ready) {
PositionData posData;
OSReport("GZ_loadMemfile: reading position data at 0x%x\n", sizeof(dSv_info_c) + 1);
storage.result = GZ_readMemfile(&storage, posData, storage.sector_size);
OSReport("GZ_loadMemfile: result: %d\n", storage.result);
OSReport("GZ_loadMemfile: position: {%f, %f, %f}\n", posData.link.x, posData.link.y, posData.link.z);
OSReport("GZ_loadMemfile: angle: %f\n", posData.angle);
OSReport("GZ_loadMemfile: cam target: {%f, %f, %f}\n", posData.cam.target.x, posData.cam.target.y, posData.cam.target.z);
OSReport("GZ_loadMemfile: cam pos: {%f, %f, %f}\n", posData.cam.pos.x, posData.cam.pos.y, posData.cam.pos.z);
if (storage.result == Ready) {
FIFOQueue::push("loaded memfile!", Queue);
SaveManager::injectDefault_before();
Expand Down
2 changes: 0 additions & 2 deletions modules/menus/menu_memfiles/src/memfiles_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

#define MAX_SAVE_SLOTS 20

PositionData memfile_posdata;

KEEP_FUNC MemfilesMenu::MemfilesMenu(MemfilesData& data)
: Menu(data.cursor), l_fileNo(data.l_fileNo), lines{
{"file slot:", MEMFILE_SLOT_INDEX, "Select memfile slot"},
Expand Down

0 comments on commit c303ca4

Please sign in to comment.