Skip to content

Commit

Permalink
More device stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
buu342 committed Mar 13, 2023
1 parent 6bba25a commit 4a8291e
Show file tree
Hide file tree
Showing 11 changed files with 608 additions and 34 deletions.
158 changes: 144 additions & 14 deletions UNFLoader/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ Passes flashcart communication to more specific functions
#include "device_everdrive.h"
#include "device_sc64.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#pragma comment(lib, "Include/FTD2XX.lib")


Expand All @@ -19,9 +19,11 @@ Passes flashcart communication to more specific functions
*********************************/

DeviceError (*funcPointer_open)(FTDIDevice*);
void (*funcPointer_sendrom)(FTDIDevice*, FILE *file, uint32_t size);
DeviceError (*funcPointer_sendrom)(FTDIDevice*, uint8_t* rom, uint32_t size);
bool (*funcPointer_testdebug)(FTDIDevice*);
void (*funcPointer_senddata)(FTDIDevice*, int datatype, char *data, uint32_t size);
bool (*funcPointer_shouldpadrom)();
uint32_t (*funcPointer_maxromsize)();
DeviceError (*funcPointer_senddata)(FTDIDevice*, int datatype, char *data, uint32_t size);
DeviceError (*funcPointer_close)(FTDIDevice*);

static void device_set_64drive1(FTDIDevice* cart, uint32_t index);
Expand All @@ -34,12 +36,11 @@ static void device_set_sc64(FTDIDevice* cart, uint32_t index);
Globals
*********************************/

// File
static char* local_rompath = NULL;
static bool local_isz64 = false;
static CartType local_carttype = CART_NONE;
static CICType local_cictype = CIC_NONE;
static SaveType local_savetype = SAVE_NONE;

// Cart
static CartType local_carttype = CART_NONE;
static FTDIDevice local_cart;


Expand Down Expand Up @@ -121,13 +122,15 @@ static void device_set_64drive1(FTDIDevice* cart, uint32_t index)
{
// Set cart settings
cart->device_index = index;
cart->synchronous = 0;
cart->synchronous = false;
cart->carttype = CART_64DRIVE1;

// Set function pointers
funcPointer_open = &device_open_64drive;
/*
funcPointer_maxromsize = &device_maxromsize_64drive;
funcPointer_sendrom = &device_sendrom_64drive;
funcPointer_shouldpadrom = &device_shouldpadrom_64drive;
/*
funcPointer_testdebug = &device_testdebug_64drive;
funcPointer_senddata = &device_senddata_64drive;
*/
Expand All @@ -148,7 +151,7 @@ static void device_set_64drive2(FTDIDevice* cart, uint32_t index)
device_set_64drive1(cart, index);

// But modify the important cart settings
cart->synchronous = 1;
cart->synchronous = true;
cart->carttype = CART_64DRIVE2;
}

Expand All @@ -168,6 +171,8 @@ static void device_set_everdrive(FTDIDevice* cart, uint32_t index)

// Set function pointers
funcPointer_open = &device_open_everdrive;
funcPointer_maxromsize = &device_maxromsize_everdrive;
funcPointer_shouldpadrom = &device_shouldpadrom_everdrive;
/*
funcPointer_sendrom = &device_sendrom_everdrive;
funcPointer_testdebug = &device_testdebug_everdrive;
Expand All @@ -192,6 +197,8 @@ static void device_set_sc64(FTDIDevice* cart, uint32_t index)

// Set function pointers
funcPointer_open = &device_open_sc64;
funcPointer_maxromsize = &device_maxromsize_sc64;
funcPointer_shouldpadrom = &device_shouldpadrom_sc64;
/*
funcPointer_sendrom = &device_sendrom_sc64;
funcPointer_testdebug = &device_testdebug_sc64;
Expand Down Expand Up @@ -225,6 +232,61 @@ bool device_isopen()
}


/*==============================
device_maxromsize
Gets the max ROM size that
the flashcart supports
@return The max ROM size
==============================*/

uint32_t device_getmaxromsize()
{
return funcPointer_maxromsize();
}


/*==============================
device_sendrom
Opens the ROM and calls the function to send it to the flashcart
@param The ROM FILE pointer
@param The size of the ROM in bytes
==============================*/

DeviceError device_sendrom(FILE* rom, uint32_t filesize)
{
bool is_z64 = false;
uint8_t* rom_buffer;
DeviceError err;

// Pad the ROM if necessary
if (funcPointer_shouldpadrom())
filesize = calc_padsize(filesize/(1024*1024))*1024*1024;

// Check we managed to malloc
rom_buffer = (uint8_t*) malloc(sizeof(uint8_t)*filesize);
if (rom_buffer == NULL)
return DEVICEERR_MALLOCFAIL;

// Read the ROM into a buffer
fread(rom_buffer, 1, filesize, rom);

// Check if we have a Z64 ROM
if (!(rom_buffer[0] == 0x80 && rom_buffer[1] == 0x37 && rom_buffer[2] == 0x12 && rom_buffer[3] == 0x40))
is_z64 = true;
fseek(rom, 0, SEEK_SET);

// Byteswap if it's a Z64 ROM
if (is_z64)
for (uint32_t i=0; i<filesize; i+=2)
SWAP(rom_buffer[i], rom_buffer[i+1]);

// Upload the ROM
err = funcPointer_sendrom(&local_cart, rom_buffer, filesize);
free(rom_buffer);
return err;
}


/*==============================
device_testdebug
Checks whether this cart can use debug mode
Expand Down Expand Up @@ -262,12 +324,18 @@ DeviceError device_close()
/*==============================
device_setrom
Sets the path of the ROM to load
@param The path to the ROM
@param The path to the ROM
@return True if successful, false otherwise
==============================*/

void device_setrom(char* path)
bool device_setrom(char* path)
{
struct stat path_stat;
stat(path, &path_stat);
if (!S_ISREG(path_stat.st_mode))
return false;
local_rompath = path;
return true;
}


Expand All @@ -291,7 +359,7 @@ void device_setcart(CartType cart)

void device_setcic(CICType cic)
{
local_cictype = cic;
local_cart.cictype = cic;
}


Expand All @@ -303,7 +371,7 @@ void device_setcic(CICType cic)

void device_setsave(SaveType save)
{
local_savetype = save;
local_cart.savetype = save;
}


Expand Down Expand Up @@ -345,4 +413,66 @@ uint32_t swap_endian(uint32_t val)
((val << 8) & 0x00ff0000) |
((val >> 8) & 0x0000ff00) |
((val >> 24));
}


/*==============================
calc_padsize
Returns the correct size a ROM should be. Code taken from:
https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
@param The current ROM filesize
@return The correct ROM filesize
==============================*/

uint32_t calc_padsize(uint32_t size)
{
size--;
size |= size >> 1;
size |= size >> 2;
size |= size >> 4;
size |= size >> 8;
size |= size >> 16;
size++;
return size;
}


/*==============================
romhash
Returns an int with a simple hash of the inputted data
@param The data to hash
@param The size of the data
@return The hash number
==============================*/

uint32_t romhash(uint8_t *buff, uint32_t len)
{
uint32_t i;
uint32_t hash=0;
for (i=0; i<len; i++)
hash += buff[i];
return hash;
}

/*==============================
cic_from_hash
Returns a CIC value from the hash number
@param The hash number
@return The global_cictype value
==============================*/

CICType cic_from_hash(uint32_t hash)
{
switch (hash)
{
case 0x033A27: return CIC_6101;
case 0x034044: return CIC_6102;
case 0x03421E: return CIC_7102;
case 0x0357D0: return CIC_X103;
case 0x047A81: return CIC_X105;
case 0x0371CC: return CIC_X106;
case 0x02ABB7: return CIC_5101;
case 0x04F90E: return CIC_8303;
}
return CIC_NONE;
}
23 changes: 19 additions & 4 deletions UNFLoader/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include "Include/ftd2xx.h"
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>


Expand All @@ -27,7 +29,8 @@
CIC_X103 = 4,
CIC_X105 = 5,
CIC_X106 = 6,
CIC_5101 = 7
CIC_5101 = 7,
CIC_8303 = 8
} CICType;

typedef enum {
Expand All @@ -52,6 +55,7 @@
DEVICEERR_PURGEFAIL,
DEVICEERR_READFAIL,
DEVICEERR_WRITEFAIL,
DEVICEERR_WRITEZERO,
DEVICEERR_CLOSEFAIL,
DEVICEERR_BITMODEFAIL_RESET,
DEVICEERR_BITMODEFAIL_SYNCFIFO,
Expand All @@ -63,6 +67,8 @@
DEVICEERR_NOCOMPSIG,
DEVICEERR_READPACKSIZEFAIL,
DEVICEERR_BADPACKSIZE,
DEVICEERR_MALLOCFAIL,
DEVICEERR_64D_8303USB,
DEVICEERR_SC64_CTRLRESETFAIL,
DEVICEERR_SC64_CTRLRELEASEFAIL,
DEVICEERR_SC64_FIRMWARECHECKFAIL,
Expand All @@ -77,13 +83,13 @@
typedef struct {
CartType carttype;
CICType cictype;
CICType savetype;
SaveType savetype;
DWORD device_count;
uint32_t device_index;
FT_DEVICE_LIST_INFO_NODE* device_info;
FT_STATUS status;
FT_HANDLE handle;
uint32_t synchronous; // For 64Drive
bool synchronous; // For 64Drive
DWORD bytes_written;
DWORD bytes_read;
} FTDIDevice;
Expand All @@ -93,18 +99,27 @@
Function Prototypes
*********************************/

// Main device functions
DeviceError device_find();
DeviceError device_open();
uint32_t device_getmaxromsize();
DeviceError device_sendrom(FILE* rom, uint32_t filesize);
bool device_isopen();
DeviceError device_close();

void device_setrom(char* path);
// Device configuration
bool device_setrom(char* path);
void device_setcart(CartType cart);
void device_setcic(CICType cic);
void device_setsave(SaveType save);
char* device_getrom();
CartType device_getcart();

// Helper functions
#define SWAP(a, b) (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b))) // From https://graphics.stanford.edu/~seander/bithacks.html#SwappingValuesXOR
uint32_t swap_endian(uint32_t val);
uint32_t calc_padsize(uint32_t size);
uint32_t romhash(uint8_t *buff, uint32_t len);
CICType cic_from_hash(uint32_t hash);

#endif
Loading

0 comments on commit 4a8291e

Please sign in to comment.