Skip to content

Commit

Permalink
- Fix broken byte addressing SD in-game reads
Browse files Browse the repository at this point in the history
- Disable CMD18 resume logic until proper partial sector reads are implemented - Add debug to in-game read code
- Tidy unused vars in patcher.c
  • Loading branch information
emukidid committed Dec 12, 2018
1 parent 609178e commit 4bf1d1a
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 41 deletions.
23 changes: 21 additions & 2 deletions cube/patches/base/frag.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ u32 read_frag(void *dst, u32 len, u32 offset) {
u32 fragSize = fragList[fragTableIdx+1] & 0x7FFFFFFF;
u32 fragSector = fragList[fragTableIdx+2];
u32 fragOffsetEnd = fragOffset + fragSize;

#ifdef DEBUG_VERBOSE
usb_sendbuffer_safe("READ: dst: ",11);
print_int_hex(dst);
usb_sendbuffer_safe(" len: ",6);
print_int_hex(len);
usb_sendbuffer_safe(" ofs: ",6);
print_int_hex(offset);
#endif
// Find where our read starts and read as much as we can in this frag before returning
if(offset >= fragOffset && offset < fragOffsetEnd) {
// Does our read get cut off early?
Expand All @@ -37,14 +44,26 @@ u32 read_frag(void *dst, u32 len, u32 offset) {
adjustedOffset = offset - fragOffset;
}
do_read(dst, amountToRead, adjustedOffset, fragSector);
#ifdef DEBUG_VERBOSE
u32 sz = amountToRead;
u8* ptr = (u8*)dst;
u32 hash = 5381;
s32 c;
while (c = *ptr++)
hash = ((hash << 5) + hash) + c;

usb_sendbuffer_safe(" checksum: ",11);
print_int_hex(hash);
usb_sendbuffer_safe("\r\n",2);
#endif
return amountToRead;
}
}
return 0;
}

void device_frag_read(void *dst, u32 len, u32 offset)
{
{
while(len != 0) {
u32 amountRead = read_frag(dst, len, offset);
len-=amountRead;
Expand Down
5 changes: 5 additions & 0 deletions cube/patches/base/igr.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ typedef struct {
unsigned int unused[MAXTEXTSECTION];
} DOLHEADER;

#ifndef DEBUG
void *memcpy(void *dest, const void *src, u32 size)
{
char *d = dest;
Expand Down Expand Up @@ -115,3 +116,7 @@ void exit_to_pref(void) {

}
}
#else
void exit_to_pref(void) {
}
#endif
59 changes: 36 additions & 23 deletions cube/patches/sdgecko/sd.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,23 @@ void send_cmd(u32 cmd, u32 sector) {
void rcvr_datablock(void *dest, u32 start_byte, u32 bytes_to_read) {
int bytesRead = start_byte+bytes_to_read;

while(rcvr_spi() != 0xFE);
#ifdef DEBUG_VERBOSE
usb_sendbuffer_safe(" dst: ",6);
print_int_hex((u32)dest);
usb_sendbuffer_safe(" start_byte: ",13);
print_int_hex(start_byte);
usb_sendbuffer_safe(" bytes_to_read: ",17);
print_int_hex(bytes_to_read);
usb_sendbuffer_safe(" resume: ",9);
print_int_hex(resume);
usb_sendbuffer_safe(" sectorpos: ",12);
print_int_hex(*(vu16*)VAR_SD_SECTORPOS);
usb_sendbuffer_safe("\r\n",2);

#endif

while(rcvr_spi() != 0xFE);

// Skip the start if it's a misaligned read
while(start_byte>4) {
exi_imm_read(4);
Expand All @@ -109,7 +124,7 @@ void rcvr_datablock(void *dest, u32 start_byte, u32 bytes_to_read) {
}

// Read out the rest from the SD as we've requested it anyway and can't have it hanging off the bus
int remainder = SECTOR_SIZE-bytesRead;
u32 remainder = SECTOR_SIZE-bytesRead;
while(remainder>=4) {
exi_imm_read(4);
remainder-=4;
Expand All @@ -118,56 +133,54 @@ void rcvr_datablock(void *dest, u32 start_byte, u32 bytes_to_read) {
rcvr_spi();
remainder--;
}
exi_imm_read(2); // discard CRC
exi_imm_read(2); // discard CRC
}

void do_read(void *dst, u32 len, u32 offset, u32 sectorLba) {
u32 lba = (offset>>9) + sectorLba;
u32 startByte = (offset%SECTOR_SIZE);
u32 numBytes = len;

int lbaShift = 9 * (*(vu32*)VAR_SD_TYPE); // SD Card Type (SDHC=0, SD=1)
u32 lbaShift = *(vu32*)VAR_SD_SHIFT;

// SDHC uses sector addressing, SD uses byte
lba <<= lbaShift;

// If we weren't just reading this sector
if(lba != *(vu32*)VAR_TMP1) {
if(*(vu32*)VAR_TMP2) {
// End the read by sending CMD12 + Deselect SD + Burn a cycle after it
send_cmd(CMD12, 0);
exi_deselect();
rcvr_spi();
}
// Send multiple block read command and the LBA we want to start reading at
send_cmd(CMD18, lba);
*(vu32*)VAR_TMP2 = 1;
}
// Send multiple block read command and the LBA we want to start reading at
send_cmd(CMD18, lba);

// Read block crossing a boundary
if(startByte) {
// amount to read in first block may vary if our read is small enough to fit in it
int amountInBlock = (len + startByte > SECTOR_SIZE) ? SECTOR_SIZE-startByte : len;
u32 amountInBlock = (len + startByte > SECTOR_SIZE) ? SECTOR_SIZE-startByte : len;
rcvr_datablock(dst,startByte, amountInBlock);
numBytes-=amountInBlock;
dst+=amountInBlock;
}

// Read any full, aligned blocks
int numFullBlocks = numBytes>>9;
u32 numFullBlocks = numBytes>>9;
numBytes -= numFullBlocks << 9;
while(numFullBlocks) {
rcvr_datablock(dst, 0,SECTOR_SIZE);
dst+=SECTOR_SIZE;
numBytes-=SECTOR_SIZE;
numFullBlocks--;
}

// Read any trailing half block
if(numBytes) {
rcvr_datablock(dst,0, numBytes);
dst+=numBytes;
}

*(vu32*)VAR_TMP1 = lba + ((len + SECTOR_SIZE-startByte) >> lbaShift);
// End the read by sending CMD12 + Deselect SD + Burn a cycle after it
send_cmd(CMD12, 0);
exi_deselect();
rcvr_spi();
#ifdef DEBUG_VERBOSE
usb_sendbuffer_safe(" u32: ",5);
print_int_hex(*(u32*)((u32)dst-len));
usb_sendbuffer_safe(" u32: ",5);
print_int_hex(*(u32*)(((u32)dst-len)+4));
usb_sendbuffer_safe("\r\n",2);
#endif
}

/* End of SD functions */
4 changes: 2 additions & 2 deletions cube/reservedarea.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
.set VAR_DISC_2_LBA, 0x2F04 # is the base file sector for disk 2
.set VAR_CUR_DISC_LBA, 0x2F08 # is the currently selected disk sector
.set VAR_EXI_BUS_SPD, 0x2F0C # is the EXI bus speed (192 = 16mhz vs 208 = 32mhz)
.set VAR_SD_TYPE, 0x2F10 # is the Card Type (SDHC=0, SD=1)
.set VAR_SD_SHIFT, 0x2F10 # is the SD Card shift amount when issueing read cmds
.set VAR_EXI_FREQ, 0x2F14 # is the EXI frequency (4 = 16mhz, 5 = 32mhz)
.set VAR_EXI_SLOT, 0x2F18 # is the EXI slot (0 = slot a, 1 = slot b)
.set VAR_TMP1, 0x2F1C # space for a variable if required
Expand Down Expand Up @@ -124,7 +124,7 @@
#define VAR_DISC_2_LBA (VAR_AREA+0x2F04) // is the base file sector for disk 2
#define VAR_CUR_DISC_LBA (VAR_AREA+0x2F08) // is the currently selected disk sector
#define VAR_EXI_BUS_SPD (VAR_AREA+0x2F0C) // is the EXI bus speed (192 = 16mhz vs 208 = 32mhz)
#define VAR_SD_TYPE (VAR_AREA+0x2F10) // is the Card Type (SDHC=0, SD=1)
#define VAR_SD_SHIFT (VAR_AREA+0x2F10) // is the SD Card shift amount when issueing read cmds
#define VAR_EXI_FREQ (VAR_AREA+0x2F14) // is the EXI frequency (4 = 16mhz, 5 = 32mhz)
#define VAR_EXI_SLOT (VAR_AREA+0x2F18) // is the EXI slot (0 = slot a, 1 = slot b)
#define VAR_TMP1 (VAR_AREA+0x2F1C) // space for a variable if required
Expand Down
2 changes: 1 addition & 1 deletion cube/swiss/source/devices/dvd/deviceHandler-DVD.c
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ s32 deviceHandler_DVD_setupFile(file_handle* file, file_handle* file2) {
// Copy the current speed
*(vu32*)VAR_EXI_BUS_SPD = 192;
// Card Type
*(vu32*)VAR_SD_TYPE = sdgecko_getAddressingType(((devices[DEVICE_PATCHES]->location == LOC_MEMCARD_SLOT_A) ? 0:1));
*(vu32*)VAR_SD_SHIFT = 9 * sdgecko_getAddressingType(((devices[DEVICE_PATCHES]->location == LOC_MEMCARD_SLOT_A) ? 0:1));
// Copy the actual freq
*(vu32*)VAR_EXI_FREQ = EXI_SPEED16MHZ; // play it safe
// Device slot (0 or 1) // This represents 0xCC0068xx in number of u32's so, slot A = 0xCC006800, B = 0xCC006814
Expand Down
2 changes: 1 addition & 1 deletion cube/swiss/source/devices/fat/deviceHandler-FAT.c
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ s32 deviceHandler_FAT_setupFile(file_handle* file, file_handle* file2) {
// Copy the current speed
*(vu32*)VAR_EXI_BUS_SPD = !swissSettings.exiSpeed ? 192:208;
// Card Type
*(vu32*)VAR_SD_TYPE = SDHCCard;
*(vu32*)VAR_SD_SHIFT = 9 * SDHCCard;
// Copy the actual freq
*(vu32*)VAR_EXI_FREQ = !swissSettings.exiSpeed ? EXI_SPEED16MHZ:EXI_SPEED32MHZ;
// Device slot (0 or 1) // This represents 0xCC0068xx in number of u32's so, slot A = 0xCC006800, B = 0xCC006814
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ s32 deviceHandler_WKF_setupFile(file_handle* file, file_handle* file2) {
// Copy the current speed
*(vu32*)VAR_EXI_BUS_SPD = 192;
// Card Type
*(vu32*)VAR_SD_TYPE = sdgecko_getAddressingType(((devices[DEVICE_PATCHES]->location == LOC_MEMCARD_SLOT_A) ? 0:1));
*(vu32*)VAR_SD_SHIFT = 9 * sdgecko_getAddressingType(((devices[DEVICE_PATCHES]->location == LOC_MEMCARD_SLOT_A) ? 0:1));
// Copy the actual freq
*(vu32*)VAR_EXI_FREQ = EXI_SPEED16MHZ; // play it safe
// Device slot (0 or 1) // This represents 0xCC0068xx in number of u32's so, slot A = 0xCC006800, B = 0xCC006814
Expand Down
11 changes: 0 additions & 11 deletions cube/swiss/source/patcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@

static u32 top_addr = VAR_PATCHES_BASE;

/*** externs ***/
extern GXRModeObj *vmode; /*** Graphics Mode Object ***/
extern u32 *xfb[2]; /*** Framebuffers ***/
extern u32 SDHCCard;

extern void animateBox(int x1,int y1, int x2, int y2, int color,char *msg);

// Read
FuncPattern ReadDebug = {55, 23, 18, 3, 2, 4, 0, 0, "Read (Debug)", 0};
FuncPattern ReadCommon = {67, 30, 18, 5, 2, 3, 0, 0, "Read (Common)", 0};
Expand All @@ -43,10 +36,6 @@ u16 _dvdinterrupthandler_part[3] = {
0x6000, 0x002A, 0x0054
};

u32 _osdispatch_part_a[2] = {
0x3C60CC00, 0x83E33000
};

int install_code()
{
void *location = (void*)LO_RESERVE;
Expand Down
4 changes: 4 additions & 0 deletions cube/swiss/source/swiss.c
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,10 @@ unsigned int load_app(int multiDol, ExecutableFile *filesToPatch)
wkfWriteOffset(*(vu32*)VAR_DISC_1_LBA);
}
print_gecko("libogc shutdown and boot game!\r\n");
if((devices[DEVICE_CUR] == &__device_sd_a) || (devices[DEVICE_CUR] == &__device_sd_b)) {
print_gecko("set size\r\n");
sdgecko_setPageSize(((devices[DEVICE_CUR]->location == LOC_MEMCARD_SLOT_A)? 0:1), 512);
}
DOLtoARAM(main_dol_buffer, 0, NULL);
return 0;
}
Expand Down

0 comments on commit 4bf1d1a

Please sign in to comment.