Alternate Everdrive64 Menu fork for the ED64 Plus
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

4657 lines
126 KiB

//
// Copyright (c) 2017 The Altra64 project contributors
// Portions (c) 2013 saturnu (Alt64) based on libdragon, Neo64Menu, ED64IO, libn64-hkz, libmikmod
// See LICENSE file in the project root for full license information.
//
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <math.h>
//libdragon n64 lib
#include <libdragon.h>
//everdrive system libs
#include "types.h"
#include "sys.h"
#include "everdrive.h"
//filesystem
#include "sd.h"
#include "ff.h"
//utils
#include "utils.h"
//config file on sd
#include "ini.h"
#include "strlib.h"
//main header
#include "main.h"
//sound
#include "sound.h"
#include "mp3.h"
// YAML parser
#include <yaml.h>
#include "constants.h"
#include "debug.h"
#include "mem.h"
#include "chksum64.h"
#include "image.h"
#include "rom.h"
#include "memorypak.h"
#include "menu.h"
#include "cic.h"
#ifdef USE_TRUETYPE
#define STB_TRUETYPE_IMPLEMENTATION
#include "stb_truetype.h"
#define MAX_LIST 20
struct glyph
{
int xoff;
int yoff;
int adv;
int lsb;
float scale;
unsigned int color;
unsigned char *alpha;
sprite_t sprite;
};
typedef struct glyph glyph_t;
glyph_t sans[96];
glyph_t serif[96];
#endif
typedef struct
{
uint32_t type;
uint32_t color;
char filename[MAX_FILENAME_LEN + 1];
} direntry_t;
//ini file
typedef struct
{
int version;
const char *name;
char *background_image;
char *border_color_1;
char *border_color_2;
char *box_color;
char *selection_color;
char *list_font_color;
char *list_dir_font_color;
char *selection_font_color;
char *mempak_path;
char *save_path;
int sound_on;
int page_display;
int tv_mode;
int quick_boot;
int enable_colored_list;
int ext_type;
int cd_behaviour;
int scroll_behaviour;
int text_offset;
int hide_sysfolder;
int sd_speed;
} configuration;
volatile u32 *romaddr_ptr = (u32 *)ROM_ADDR;
unsigned int gBootCic = CIC_6102;
static void *bg_buffer;
void *__safe_bg_buffer;
#define __get_buffer(x) __safe_buffer[(x)-1]
extern uint32_t __bitdepth;
extern uint32_t __width;
extern uint32_t __height;
extern void *__safe_buffer[];
int firm_found = 0;
char rom_config[10];
display_context_t lockVideo(int wait);
int select_mode = 0;
int toplist_reload = 1;
int toplist_cursor = 0;
char toplist15[15][256];
int fat_initialized = 0;
int exit_ok = 0;
int boot_cic = 0;
int boot_save = 0;
int cursor_line = 0;
int cursor_lastline = 0;
u16 cursor_history[32];
u16 cursor_history_pos = 0;
u8 empty = 0;
int mp3playing = 0;
FATFS *fs;
//start with filebrowser menu key settings
enum InputMap
{
none,
file_manager,
mempak_menu,
char_input,
rom_loaded,
mpk_format,
mpk_restore,
rom_config_box,
toplist,
mpk_choice,
mpk_quick_backup,
mp3,
abort_screen,
};
enum InputMap input_mapping = file_manager;
//holds the string of the character input screen result
//int text_input_on = 0;
//char input vars
int x, y, position, set;
unsigned char input_text[32];
uint32_t chr_forecolor;
uint32_t chr_backcolor;
//save type still set - save to do after reboot
int save_after_reboot = 0;
//cart id from the rom header
unsigned char cartID[4];
char curr_dirname[64];
char pwd[64];
TCHAR rom_filename[MAX_SUPPORTED_PATH_LEN];
u32 rom_buff[128]; //rom buffer
u8 *rom_buff8; //rom buffer
u8 *firmware;
u8 gbload = 0;
int cheats_on = 0;
int checksum_fix_on = 0;
short int gCheats = 0; /* 0 = off, 1 = select, 2 = all */
short int force_tv = 0;
short int boot_country = 0;
static resolution_t res = RESOLUTION_320x240;
//background sprites
sprite_t *loadPng(u8 *png_filename);
sprite_t *background; //background
sprite_t *splashscreen; //splash screen
//config file theme settings
u32 border_color_1 = 0xFFFFFFFF; //hex 0xRRGGBBAA AA=transparenxy
u32 border_color_2 = 0x3F3F3FFF;
u32 box_color = 0x00000080;
u32 selection_color = 0x6495ED60;
u32 selection_font_color = 0xFFFFFFFF;
u32 list_font_color = 0xFFFFFFFF;
u32 list_dir_font_color = 0xFFFFFFFF;
char *border_color_1_s;
char *border_color_2_s;
char *box_color_s;
char *selection_color_s;
char *selection_font_color_s;
char *list_font_color_s;
char *list_dir_font_color_s;
char *save_path;
u8 sound_on = 0;
u8 page_display = 0;
u8 tv_mode = 0; // 1=ntsc 2=pal 3=mpal 0=default automatic
u8 quick_boot = 0;
u8 enable_colored_list = 0;
u8 cd_behaviour = 0; //0=first entry 1=last entry
u8 scroll_behaviour = 0; //1=classic 0=new page-system
u8 ext_type = 0; //0=classic 1=org os
u8 sd_speed = 1; // 1=25Mhz 2=50Mhz
u8 hide_sysfolder = 0;
char *background_image;
//mp3
int buf_size;
char *buf_ptr;
//toplist helper
int list_pos_backup[3];
char list_pwd_backup[256];
char dirz[512] = "rom://";
int count = 0;
int page = 0;
int cursor = 0;
direntry_t *list;
int filesize(FILE *pFile)
{
fseek(pFile, 0, SEEK_END);
int lSize = ftell(pFile);
rewind(pFile);
return lSize;
}
sprite_t *read_sprite(const char *const spritename)
{
FILE *fp = fopen(spritename, "r");
if (fp)
{
sprite_t *sp = malloc(filesize(fp));
fread(sp, 1, filesize(fp), fp);
fclose(fp);
return sp;
}
else
{
return 0;
}
}
void drawSelection(display_context_t disp, int p)
{
int s_x = 23 + (text_offset * 8);
if (scroll_behaviour)
{
if (select_mode)
{
if (cursor_lastline > cursor && cursor_line > 0)
{
cursor_line--;
}
if (cursor_lastline < cursor && cursor_line < 19)
{
cursor_line++;
}
p = cursor_line;
graphics_draw_box_trans(disp, s_x, (((p + 3) * 8) + 24), 272, 8, selection_color); //(p+3) diff
cursor_lastline = cursor;
}
}
else
{ //new page-system
//accept p
graphics_draw_box_trans(disp, s_x, (((p + 3) * 8) + 24), 272, 8, selection_color);
}
}
void drawConfigSelection(display_context_t disp, int l)
{
int s_x = 62 + (text_offset * 8);
l = l + 5;
graphics_draw_box_trans(disp, s_x, (((l + 3) * 8) + 24), 193, 8, 0x00000080); //(p+3) diff
}
void drawToplistSelection(display_context_t disp, int l)
{
int s_x = 30 + (text_offset * 8);
l = l + 2;
graphics_draw_box_trans(disp, s_x, (((l + 3) * 8) + 24), 256, 8, 0xFFFFFF70); //(p+3) diff
}
void chdir(const char *const dirent)
{
/* Ghetto implementation */
if (strcmp(dirent, "..") == 0)
{
/* Go up one */
int len = strlen(dirz) - 1;
/* Stop going past the min */
if (dirz[len] == '/' && dirz[len - 1] == '/' && dirz[len - 2] == ':')
{
//return if ://
return;
}
if (dirz[len] == '/')
{
dirz[len] = 0;
len--;
}
while (dirz[len] != '/')
{
dirz[len] = 0;
len--;
}
}
else
{
/* Add to end */
strcat(dirz, dirent);
strcat(dirz, "/");
}
}
int compare(const void *a, const void *b)
{
direntry_t *first = (direntry_t *)a;
direntry_t *second = (direntry_t *)b;
if (first->type == DT_DIR && second->type != DT_DIR)
{
/* First should be first */
return -1;
}
if (first->type != DT_DIR && second->type == DT_DIR)
{
/* First should be second */
return 1;
}
return strcmp(first->filename, second->filename);
}
int compare_int(const void *a, const void *b)
{
const int *ia = (const int *)a; // casting pointer types
const int *ib = (const int *)b;
return *ia - *ib;
}
int compare_int_reverse(const void *a, const void *b)
{
const int *ia = (const int *)a; // casting pointer types
const int *ib = (const int *)b;
return *ib - *ia;
}
void new_scroll_pos(int *cursor, int *page, int max, int count)
{
/* Make sure windows too small can be calculated right */
if (max > count)
{
max = count;
}
/* Bounds checking */
if (*cursor >= count)
{
*cursor = count - 1;
}
if (*cursor < 0)
{
*cursor = 0;
}
/* Scrolled up? */
if (*cursor < *page)
{
*page = *cursor;
return;
}
/* Scrolled down/ */
if (*cursor >= (*page + max))
{
*page = (*cursor - max) + 1;
return;
}
/* Nothing here, should be good! */
}
void display_dir(direntry_t *list, int cursor, int page, int max, int count, display_context_t disp)
{
//system color
uint32_t forecolor = 0;
uint32_t forecolor_menu = 0;
uint32_t backcolor;
backcolor = graphics_make_color(0x00, 0x00, 0x00, 0x00); //bg
forecolor_menu = graphics_make_color(0xFF, 0xFF, 0xFF, 0xFF); //fg
graphics_set_color(list_font_color, backcolor);
u8 c_pos[MAX_LIST + 1];
int c_pos_counter = 0;
c_pos[c_pos_counter++] = 0;
u8 c_dirname[64];
if (page_display)
{
u8 pi = page / 20;
u8 ci = 0;
if (count % 20 == 0)
ci = (count - 1) / 20;
else
ci = count / 20;
sprintf(c_dirname, "%i/%i SD:/%s", pi + 1, ci + 1, pwd);
}
else
{
sprintf(c_dirname, "SD:/%s", pwd);
}
char sel_str[128];
printText(c_dirname, 3, 4, disp);
int firstrun = 1;
/* Page bounds checking */
if (max > count)
{ //count = directories starting at 1
max = count;
}
/* Cursor bounds checking */
if (cursor >= (page + max))
{
cursor = (page + max) - 1;
}
if (cursor < page)
{
cursor = page;
}
if (max == 0)
{
printText("dir empty...", 3, 6, disp);
sprintf(sel_str, "dir empty...");
empty = 1;
}
else
{
empty = 0;
}
//last page anti ghosting entries
if (page == (count / 20) * 20)
max = count % 20;
for (int i = page; i < (page + max); i++)
{ //from page to page + max
if (list[i].type == DT_DIR)
{
char tmpdir[(CONSOLE_WIDTH - 5) + 1];
strncpy(tmpdir, list[i].filename, CONSOLE_WIDTH - 5);
tmpdir[CONSOLE_WIDTH - 5] = 0;
char *dir_str;
dir_str = malloc(slen(tmpdir) + 3);
if (i == cursor)
{
sprintf(dir_str, " [%s]", tmpdir);
sprintf(sel_str, " [%s]", tmpdir);
if (scroll_behaviour)
drawSelection(disp, i);
c_pos[c_pos_counter++] = 1;
}
else
{
sprintf(dir_str, "[%s]", tmpdir);
c_pos[c_pos_counter++] = 0;
}
graphics_set_color(list_dir_font_color, backcolor);
if (firstrun)
{
printText(dir_str, 3, 6, disp);
firstrun = 0;
}
else
{
printText(dir_str, 3, -1, disp);
}
free(dir_str);
}
else
{ //if(list[i].type == DT_REG)
int fcolor = list[i].color;
if (fcolor != 0)
{
switch (fcolor)
{
case 1:
forecolor = graphics_make_color(0xFF, 0xFF, 0xFF, 0xFF); //common (white)
break;
case 2:
forecolor = graphics_make_color(0x00, 0xFF, 0x00, 0xCF); //uncommon (green)
break;
case 3:
forecolor = graphics_make_color(0x1E, 0x90, 0xFF, 0xFF); //rare (blue)
break;
case 4:
forecolor = graphics_make_color(0x9B, 0x30, 0xFF, 0xFF); //epic (purple)
break;
case 5:
forecolor = graphics_make_color(0xFF, 0xA5, 0x00, 0xFF); //legendary (orange)
break;
default:
break;
}
}
else
forecolor = list_font_color;
char tmpdir[(CONSOLE_WIDTH - 3) + 1];
strncpy(tmpdir, list[i].filename, CONSOLE_WIDTH - 3);
tmpdir[CONSOLE_WIDTH - 3] = 0;
char *dir_str;
dir_str = malloc(slen(tmpdir) + 1);
if (i == cursor)
{
sprintf(dir_str, " %s", tmpdir);
sprintf(sel_str, " %s", tmpdir);
if (scroll_behaviour)
drawSelection(disp, i);
c_pos[c_pos_counter++] = 1;
}
else
{
sprintf(dir_str, "%s", tmpdir);
c_pos[c_pos_counter++] = 0;
}
graphics_set_color(list_font_color, backcolor);
if (firstrun)
{
if (enable_colored_list)
graphics_set_color(forecolor, backcolor);
printText(dir_str, 3, 6, disp); //3,6
firstrun = 0;
}
else
{
if (enable_colored_list)
graphics_set_color(forecolor, backcolor);
printText(dir_str, 3, -1, disp); //3,1
}
free(dir_str);
}
}
//for page-wise scrolling
if (scroll_behaviour == 0)
{
int c = 0;
for (c = 0; c < max + 1; c++)
{
if (c_pos[c] == 1)
{
drawSelection(disp, c - 1);
//todo: set selection color
graphics_set_color(selection_font_color, backcolor);
printText(sel_str, 3, 6 + c - 1, disp);
graphics_set_color(forecolor, backcolor);
}
}
}
graphics_set_color(forecolor_menu, backcolor);
}
//background sprite
void drawBg(display_context_t disp)
{
graphics_draw_sprite(disp, 0, 0, background);
}
void drawBox(short x, short y, short width, short height, display_context_t disp)
{
x = x + (text_offset * 8);
/*
* |Y
* | x0/y0
* |
* |
* | x1/y1
* |_______________X
*
*/
// X0 Y0 X1 Y1
graphics_draw_line(disp, x, y, width + x, y, border_color_1); //A top left tp bottom right ok
graphics_draw_line(disp, width + x, y, width + x, height + y, border_color_1); //B top right to bottom right
graphics_draw_line(disp, x, y, x, height + y, border_color_2); //C //152-20
graphics_draw_line(disp, x, height + y, width + x, height + y, border_color_2); //D
graphics_draw_box_trans(disp, x + 1, y + 1, width - 1, height - 1, box_color); //red light transparent
}
void drawBoxNumber(display_context_t disp, int box)
{
int old_color = box_color;
//backup color
switch (box)
{
case 1:
drawBox(20, 24, 277, 193, disp);
break; //filebrowser
case 2:
drawBox(60, 56, 200, 128, disp);
break; //info screen
case 3:
box_color = graphics_make_color(0x00, 0x00, 0x60, 0xC9);
drawBox(79, 29, 161, 180, disp);
break; //cover
case 4:
box_color = graphics_make_color(0x30, 0x00, 0x00, 0xA3);
drawBox(79, 29, 161, 180, disp);
break; //mempak content
case 5:
box_color = graphics_make_color(0x60, 0x00, 0x00, 0xD3);
drawBox(60, 64, 197, 114, disp); //red confirm screen
break; //mempak content
case 6:
box_color = graphics_make_color(0x60, 0x60, 0x00, 0xC3);
drawBox(60, 64, 197, 125, disp); //yellow screen
break; //rom config box
case 7:
box_color = graphics_make_color(0x00, 0x00, 0x60, 0xC3);
drawBox(60, 105, 197, 20, disp); //blue info screen
break; //info screen
case 8:
box_color = graphics_make_color(0x60, 0x00, 0x00, 0xD3);
drawBox(60, 105, 197, 20, disp); //red error screen
break; //info screen
case 9:
box_color = graphics_make_color(0x00, 0x00, 0x00, 0xB6);
drawBox(28, 49, 260, 150, disp);
break; //yellow toplist
case 10:
box_color = graphics_make_color(0x00, 0x60, 0x00, 0xC3);
drawBox(60, 105, 197, 20, disp); //green info screen
break; //info screen
case 11:
box_color = graphics_make_color(0x00, 0x60, 0x00, 0xC3);
drawBox(28, 105, 260, 30, disp);
break; //green full filename
default:
break;
}
//restore color
box_color = old_color;
}
//is setting the config file vars into the pconfig-structure
static int configHandler(void *user, const char *section, const char *name, const char *value)
{
configuration *pconfig = (configuration *)user;
#define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
if (MATCH("ed64", "border_color_1"))
{
pconfig->border_color_1 = strdup(value);
}
else if (MATCH("ed64", "border_color_2"))
{
pconfig->border_color_2 = strdup(value);
}
else if (MATCH("ed64", "box_color"))
{
pconfig->box_color = strdup(value);
}
else if (MATCH("ed64", "selection_color"))
{
pconfig->selection_color = strdup(value);
}
else if (MATCH("ed64", "list_font_color"))
{
pconfig->list_font_color = strdup(value);
}
else if (MATCH("ed64", "list_dir_font_color"))
{
pconfig->list_dir_font_color = strdup(value);
}
else if (MATCH("ed64", "selection_font_color"))
{
pconfig->selection_font_color = strdup(value);
}
else if (MATCH("ed64", "mempak_path"))
{
pconfig->mempak_path = strdup(value);
}
else if (MATCH("ed64", "save_path"))
{
pconfig->save_path = strdup(value);
}
else if (MATCH("ed64", "sound_on"))
{
pconfig->sound_on = atoi(value);
}
else if (MATCH("ed64", "page_display"))
{
pconfig->page_display = atoi(value);
}
else if (MATCH("ed64", "tv_mode"))
{
pconfig->tv_mode = atoi(value);
}
else if (MATCH("ed64", "quick_boot"))
{
pconfig->quick_boot = atoi(value);
}
else if (MATCH("ed64", "enable_colored_list"))
{
pconfig->enable_colored_list = atoi(value);
}
else if (MATCH("ed64", "ext_type"))
{
pconfig->ext_type = atoi(value);
}
else if (MATCH("ed64", "cd_behaviour"))
{
pconfig->cd_behaviour = atoi(value);
}
else if (MATCH("ed64", "scroll_behaviour"))
{
pconfig->scroll_behaviour = atoi(value);
}
else if (MATCH("ed64", "text_offset"))
{
pconfig->text_offset = atoi(value);
}
else if (MATCH("ed64", "sd_speed"))
{
pconfig->sd_speed = atoi(value);
}
else if (MATCH("ed64", "hide_sysfolder"))
{
pconfig->hide_sysfolder = atoi(value);
}
else if (MATCH("ed64", "background_image"))
{
pconfig->background_image = strdup(value);
}
else if (MATCH("user", "name"))
{
pconfig->name = strdup(value);
}
else
{
return 0; /* unknown section/name, error */
}
return 1;
}
void updateFirmware(char *filename)
{ //check that firmware exists on the disk? mainly because it has to be ripped from the official image and may not have been.
FRESULT fr;
FILINFO fno;
fr = f_stat(filename, &fno); //TODO: given this is on the ROM (not SD) does it even work??????
if (fr == FR_OK)
{
int fpf = dfs_open(filename);
firmware = malloc(dfs_size(fpf));
dfs_read(firmware, 1, dfs_size(fpf), fpf);
dfs_close(fpf);
bi_load_firmware(firmware);
}
}
//everdrive init functions
void configure()
{
u16 msg = 0;
u8 buff[16];
u16 firm;
evd_init();
//REG_MAX_MSG
evd_setCfgBit(ED_CFG_SDRAM_ON, 0);
dma_read_s(buff, ROM_ADDR + 0x20, 16);
asm_date = memRomRead32(0x38); //TODO: this should be displayed somewhere...
evd_setCfgBit(ED_CFG_SDRAM_ON, 1);
firm = evd_getFirmVersion();
if (firm >= 0x0200)
{
sleep(1);
evd_setCfgBit(ED_CFG_SDRAM_ON, 0);
sleep(1);
msg = evd_readReg(REG_MAX_MSG);
if (!(msg & (1 << 14)))
{
msg |= 1 << 14;
evd_writeReg(REG_MAX_MSG, msg);
switch(firm) //need to take into account different default firmware versions for each ED64 version
{
case 0x0214:
updateFirmware("/firmware/firmware_v2.bin");
break;
case 0x0250:
updateFirmware("/firmware/firmware_v2_5.bin");
break;
case 0x0300:
updateFirmware("/firmware/firmware_v3.bin");
break;
default:
break;
}
sleep(1);
evd_init();
}
sleep(1);
evd_setCfgBit(ED_CFG_SDRAM_ON, 1);
}
if (streql("ED64 SD boot", buff, 12) && firm >= 0x0116) //TODO: can this be moved before the firmware is loaded?
{
sdSetInterface(DISK_IFACE_SD);
}
else
{
sdSetInterface(DISK_IFACE_SPI);
}
memSpiSetDma(0);
}
//rewrites the background and the box to clear the screen
void clearScreen(display_context_t disp)
{
drawBg(disp); //background
drawBoxNumber(disp, 1); //filebrowser box
}
void romInfoScreen(display_context_t disp, u8 *buff, int silent)
{
TCHAR filename[MAX_SUPPORTED_PATH_LEN];
sprintf(filename, "%s", buff);
int swapped = 0;
FRESULT result;
int fsize = 512; //rom-headersize 4096 but the bootcode is not needed
unsigned char headerdata[fsize]; //1*512
FIL file;
UINT bytesread;
result = f_open(&file, filename, FA_READ);
if (result == FR_OK)
{
int fsizeMB = f_size(&file) / 1048576; //Bytes in a MB
result =
f_read (
&file, /* [IN] File object */
headerdata, /* [OUT] Buffer to store read data */
fsize, /* [IN] Number of bytes to read */
&bytesread /* [OUT] Number of bytes read */
);
f_close(&file);
int sw_type = is_valid_rom(headerdata);
if (sw_type != 0)
{
swapped = 1;
swap_header(headerdata, 512);
}
if (silent != 1)
{
//char 32-51 name
unsigned char rom_name[32];
for (int u = 0; u < 19; u++)
{
if (u != 0)
sprintf(rom_name, "%s%c", rom_name, headerdata[32 + u]);
else
sprintf(rom_name, "%c", headerdata[32 + u]);
}
//rom name
sprintf(rom_name, "%s", trim(rom_name));
printText(rom_name, 11, 19, disp);
//rom size
sprintf(rom_name, "Size: %iMB", fsizeMB);
printText(rom_name, 11, -1, disp);
//unique cart id for gametype
unsigned char cartID_str[12];
sprintf(cartID_str, "ID: %c%c%c%c", headerdata[0x3B], headerdata[0x3C], headerdata[0x3D], headerdata[0x3E]);
printText(cartID_str, 11, -1, disp);
}
int cic, save;
cic = get_cic(&headerdata[0x40]);
unsigned char cartID_short[4];
sprintf(cartID_short, "%c%c\0", headerdata[0x3C], headerdata[0x3D]);
if (get_cic_save(cartID_short, &cic, &save))
{
if (silent != 1)
{
printText("found in db", 11, -1, disp);
unsigned char save_type_str[12];
sprintf(save_type_str, "Save: %s", saveTypeToExtension(save, ext_type));
printText(save_type_str, 11, -1, disp);
unsigned char cic_type_str[12];
switch (cic)
{
case 4:
sprintf(cic_type_str, "CIC: CIC-5101", cic);
break;
case 7:
sprintf(cic_type_str, "CIC: CIC-5167", cic);
break;
default:
sprintf(cic_type_str, "CIC: CIC-610%i", cic);
break;
}
printText(cic_type_str, 11, -1, disp);
}
//thanks for the db :>
//cart was found, use CIC and SaveRAM type
}
if (silent != 1)
{
char box_path[32];
sprite_t *n64cover;
sprintf(box_path, "/"ED64_FIRMWARE_PATH"/boxart/lowres/%c%c.png", headerdata[0x3C], headerdata[0x3D]);
FILINFO fnoba;
result = f_stat (box_path, &fnoba);
if (result != FR_OK)
{
//not found
sprintf(box_path, "/"ED64_FIRMWARE_PATH"/boxart/lowres/00.png");
}
n64cover = loadPng(box_path);
graphics_draw_sprite(disp, 81, 32, n64cover);
display_show(disp);
}
else
{
rom_config[1] = cic - 1;
rom_config[2] = save;
rom_config[3] = 0; //tv force off
rom_config[4] = 0; //cheat off
rom_config[5] = 0; //chk_sum off
rom_config[6] = 0; //rating
rom_config[7] = 0; //country
rom_config[8] = 0; //reserved
rom_config[9] = 0; //reserved
}
}
}
sprite_t *loadPng(u8 *png_filename)
{
TCHAR *filename = (TCHAR *)malloc(slen(png_filename));
sprintf(filename, "%s", png_filename);
FRESULT result;
FIL file;
UINT bytesread;
result = f_open(&file, filename, FA_READ);
if (result == FR_OK)
{
int fsize = f_size(&file);
u8 png_rawdata[fsize];
result =
f_read (
&file, /* [IN] File object */
png_rawdata, /* [OUT] Buffer to store read data */
fsize, /* [IN] Number of bytes to read */
&bytesread /* [OUT] Number of bytes read */
);
f_close(&file);
free(filename);
return loadImage32(png_rawdata, fsize);
}
return 0;
}
void loadgbrom(display_context_t disp, TCHAR *rom_path)
{
drawShortInfoBox(disp, " loading please wait", 0);
FRESULT result;
FIL emufile;
UINT emubytesread;
result = f_open(&emufile, "/"ED64_FIRMWARE_PATH"/gb.v64", FA_READ);
if (result == FR_OK)
{
int emufsize = f_size(&emufile);
//load gb emulator
result =
f_read (
&emufile, /* [IN] File object */
(void *)0xb0000000, /* [OUT] Buffer to store read data */
emufsize, /* [IN] Number of bytes to read */
&emubytesread /* [OUT] Number of bytes read */
);
f_close(&emufile);
//load gb rom
FIL romfile;
UINT rombytesread;
result = f_open(&romfile, rom_path, FA_READ);
if (result == FR_OK)
{
int romfsize = f_size(&romfile);
result =
f_read (
&romfile, /* [IN] File object */
(void *)0xb0200000, /* [OUT] Buffer to store read data */
romfsize, /* [IN] Number of bytes to read */
&rombytesread /* [OUT] Number of bytes read */
);
f_close(&romfile);
boot_cic = CIC_6102;
boot_save = 5; //flash
force_tv = 0; //no force
cheats_on = 0; //cheats off
checksum_fix_on = 0;
bootRom(disp, 1);
}
}
}
void loadmsx2rom(display_context_t disp, TCHAR *rom_path)
{
FRESULT romresult;
FIL romfile;
UINT rombytesread;
romresult = f_open(&romfile, rom_path, FA_READ);
if (romresult == FR_OK)
{
int romfsize = f_size(&romfile);
//max 128KB rom
if (romfsize > 128 * 1024)
{
//error
drawShortInfoBox(disp, " error: rom > 128KB", 1);
input_mapping = abort_screen;
return;
}
else
{
drawShortInfoBox(disp, " loading please wait", 0);
FRESULT result;
FIL file;
UINT bytesread;
result = f_open(&file, "/"ED64_FIRMWARE_PATH"/ultraMSX2.z64", FA_READ);
if (result == FR_OK)
{
int fsize = f_size(&file);
result =
f_read (
&file, /* [IN] File object */
(void *)0xb0000000, /* [OUT] Buffer to store read data */
fsize, /* [IN] Number of bytes to read */
&bytesread /* [OUT] Number of bytes read */
);
f_close(&file);
romresult =
f_read (
&romfile, /* [IN] File object */
(void *)0xb0000000 + 0x2df48, /* [OUT] Buffer to store read data */ //TODO: why is the offset this particular number
romfsize, /* [IN] Number of bytes to read */
&rombytesread /* [OUT] Number of bytes read */
);
f_close(&romfile);
boot_cic = CIC_6102;
boot_save = 0; //save off/cpak
force_tv = 0; //no force
cheats_on = 0; //cheats off
checksum_fix_on = 0;
checksum_sdram();
bootRom(disp, 1);
}
}
}
}
void loadggrom(display_context_t disp, TCHAR *rom_path) //TODO: this could be merged with MSX
{
FRESULT romresult;
FIL romfile;
UINT rombytesread;
romresult = f_open(&romfile, rom_path, FA_READ);
if (romresult == FR_OK)
{
int romfsize = f_size(&romfile);
//max 512KB rom
if (romfsize > 512 * 1024)
{
//error
drawShortInfoBox(disp, " error: rom > 512KB", 1);
input_mapping = abort_screen;
return;
}
else
{
drawShortInfoBox(disp, " loading please wait", 0);
FRESULT result;
FIL file;
UINT bytesread;
result = f_open(&file, "/"ED64_FIRMWARE_PATH"/UltraSMS.z64", FA_READ);
if (result == FR_OK)
{
int fsize = f_size(&file);
result =
f_read (
&file, /* [IN] File object */
(void *)0xb0000000, /* [OUT] Buffer to store read data */
fsize, /* [IN] Number of bytes to read */
&bytesread /* [OUT] Number of bytes read */
);
f_close(&file);
romresult =
f_read (
&romfile, /* [IN] File object */
(void *)0xb0000000 + 0x1b410, /* [OUT] Buffer to store read data */ //TODO: why is the offset this particular number
romfsize, /* [IN] Number of bytes to read */
&rombytesread /* [OUT] Number of bytes read */
);
f_close(&romfile);
boot_cic = CIC_6102;
boot_save = 0; //save off/cpak
force_tv = 0; //no force
cheats_on = 0; //cheats off
checksum_fix_on = 0;
checksum_sdram();
bootRom(disp, 1);
}
}
}
}
void loadnesrom(display_context_t disp, TCHAR *rom_path)
{
FRESULT result;
FIL emufile;
UINT emubytesread;
result = f_open(&emufile, "/"ED64_FIRMWARE_PATH"/neon64bu.rom", FA_READ);
if (result == FR_OK)
{
int emufsize = f_size(&emufile);
//load nes emulator
result =
f_read (
&emufile, /* [IN] File object */
(void *)0xb0000000, /* [OUT] Buffer to store read data */
emufsize, /* [IN] Number of bytes to read */
&emubytesread /* [OUT] Number of bytes read */
);
f_close(&emufile);
//load nes rom
FIL romfile;
UINT rombytesread;
result = f_open(&romfile, rom_path, FA_READ);
if (result == FR_OK)
{
int romfsize = f_size(&romfile);
result =
f_read (
&romfile, /* [IN] File object */
(void *)0xb0200000, /* [OUT] Buffer to store read data */
romfsize, /* [IN] Number of bytes to read */
&rombytesread /* [OUT] Number of bytes read */
);
f_close(&romfile);
boot_cic = CIC_6102;
boot_save = 2; //SRAM
force_tv = 0; //no force
cheats_on = 0; //cheats off
checksum_fix_on = 0;
bootRom(disp, 1);
}
}
}
//load a z64/v64/n64 rom to the sdram
void loadrom(display_context_t disp, u8 *buff, int fast)
{
clearScreen(disp);
display_show(disp);
printText("Loading ROM, Please wait:", 3, 4, disp);
TCHAR filename[MAX_SUPPORTED_PATH_LEN];
sprintf(filename, "%s", buff);
FRESULT result;
FIL file;
UINT bytesread = 0;
result = f_open(&file, filename, FA_READ);
if (result == FR_OK)
{
int swapped = 0;
int headerfsize = 512; //rom-headersize 4096 but the bootcode is not needed
unsigned char headerdata[headerfsize]; //1*512
int fsize = f_size(&file);
int fsizeMB = fsize /1048576; //Bytes in a MB
result =
f_read (
&file, /* [IN] File object */
headerdata, /* [OUT] Buffer to store read data */
headerfsize, /* [IN] Number of bytes to read */
&bytesread /* [OUT] Number of bytes read */
);
f_close(&file);
int sw_type = is_valid_rom(headerdata);
if (sw_type != 0)
{
swapped = 1;
swap_header(headerdata, 512);
}
if (fast != 1)
{
//char 32-51 name
unsigned char rom_name[32];
for (int u = 0; u < 19; u++)
{
if (u != 0)
sprintf(rom_name, "%s%c", rom_name, headerdata[32 + u]);
else
sprintf(rom_name, "%c", headerdata[32 + u]);
}
//rom name
sprintf(rom_name, "%s", trim(rom_name));
printText(rom_name, 3, -1, disp);
//rom size
sprintf(rom_name, "Size: %iMB", fsizeMB);
printText(rom_name, 3, -1, disp);
//unique cart id for gametype
unsigned char cartID_str[12];
sprintf(cartID_str, "ID: %c%c%c%c", headerdata[0x3B], headerdata[0x3C], headerdata[0x3D], headerdata[0x3E]);
printText(cartID_str, 3, -1, disp);
}
int cic, save;
cic = get_cic(&headerdata[0x40]);
unsigned char cartID_short[4];
sprintf(cartID_short, "%c%c\0", headerdata[0x3C], headerdata[0x3D]);
if (get_cic_save(cartID_short, &cic, &save))
{
if (fast != 1)
{
printText("found in db", 3, -1, disp);
unsigned char save_type_str[12];
sprintf(save_type_str, "Save: %s", saveTypeToExtension(save, ext_type));
printText(save_type_str, 3, -1, disp);
unsigned char cic_type_str[12];
switch (cic)
{
case 4:
sprintf(cic_type_str, "CIC: CIC-5101", cic);
break;
case 7:
sprintf(cic_type_str, "CIC: CIC-5167", cic);
break;
default:
sprintf(cic_type_str, "CIC: CIC-610%i", cic);
break;
}
printText(cic_type_str, 3, -1, disp);
}
//thanks for the db :>
//cart was found, use CIC and SaveRAM type
}
//new rom_config
boot_cic = rom_config[1] + 1;
boot_save = rom_config[2];
force_tv = rom_config[3];
cheats_on = rom_config[4];
checksum_fix_on = rom_config[5];
boot_country = rom_config[7]; //boot_block
if (gbload == 1)
boot_save = 1;
if (swapped == 1)
{
while (evd_isDmaBusy())
;
evd_mmcSetDmaSwap(1);
TRACE(disp, "swapping on");
}
bytesread = 0;
result = f_open(&file, filename, FA_READ);
if (fsizeMB <= 32)
{
result =
f_read (
&file, /* [IN] File object */
(void *)0xb0000000, /* [OUT] Buffer to store read data */
fsize, /* [IN] Number of bytes to read */
&bytesread /* [OUT] Number of bytes read */
);
}
else
{
result =
f_read (
&file, /* [IN] File object */
(void *)0xb0000000, /* [OUT] Buffer to store read data */
32 * 1048576, /* [IN] Number of bytes to read */
&bytesread /* [OUT] Number of bytes read */
);
if(result == FR_OK)
{
result =
f_read (
&file, /* [IN] File object */
(void *)0xb2000000, /* [OUT] Buffer to store read data */
fsize - bytesread, /* [IN] Number of bytes to read */
&bytesread /* [OUT] Number of bytes read */
);
}
}
if(result == FR_OK)
{
printText("Rom loaded", 3, -1, disp);
if (!fast)
{
printText(" ", 3, -1, disp);
printText("(C-UP to activate cheats)", 3, -1, disp);
printText("(C-RIGHT to force menu tv mode)", 3, -1, disp);
printText("done: PRESS START", 3, -1, disp);
}
else
{
bootRom(disp, 1);
}
}
else
{
printText("file open error", 3, -1, disp);
}
}
}
int backupSaveData(display_context_t disp)
{
//backup cart-save on sd after reboot
TCHAR config_file_path[MAX_SUPPORTED_PATH_LEN];
sprintf(config_file_path, "/"ED64_FIRMWARE_PATH"/%s/LASTROM.CFG", save_path);
u8 save_format;
u8 cfg_data[2]; //TODO: this should be a strut?
FRESULT result;
FIL file;
UINT bytesread;
result = f_open(&file, config_file_path, FA_READ);
if (result == FR_OK)
{
printText("updating last played game record...", 3, 4, disp);
int fsize = f_size(&file);
result =
f_read (
&file, /* [IN] File object */
&cfg_data, /* [OUT] Buffer to store read data */
2, /* [IN] Number of bytes to read */
&bytesread /* [OUT] Number of bytes read */
);
//split in save type and cart-id
save_format = cfg_data[0];
f_gets(rom_filename, 256, &file);
f_close(&file);
//set savetype to 0 disable for next boot
if (save_format != 0)
{
result = f_open(&file, config_file_path, FA_WRITE | FA_OPEN_EXISTING);
//set savetype to off
cfg_data[0] = 0;
UINT bw;
result = f_write (
&file, /* [IN] Pointer to the file object structure */
&cfg_data, /* [IN] Pointer to the data to be written */
1, /* [IN] Number of bytes to write */
&bw /* [OUT] Pointer to the variable to return number of bytes written */
);
f_close(&file);
TRACE(disp, "Disabling save for subsequent system reboots");
volatile u8 save_config_state = 0;
int cfgreg = evd_readReg(REG_CFG);
save_config_state = evd_readReg(REG_SAV_CFG);
if (save_config_state != 0 || evd_getFirmVersion() >= 0x0300)
{ //save register set or the firmware is V3
if (save_config_state == 0)
{ //we are V3 and have had a hard reboot
evd_writeReg(REG_SAV_CFG, 1); //so we need to tell the save register it still has data.
}
save_after_reboot = 1;
}
else
{
TRACE(disp, "Save not required.");
printText("...ready", 3, -1, disp);
display_show(disp);
return 1;
}
}
}
else
{
TRACE(disp, "No previous ROM loaded!");
printText("...ready", 3, -1, disp);
display_show(disp);
return 0;
}
//reset with save request
if (save_after_reboot)
{
printText("Copying save RAM to SD card...", 3, -1, disp);
if (saveTypeToSd(disp, rom_filename, save_format))
{
printText("Operation completed sucessfully...", 3, -1, disp);
}
else
{
TRACE(disp, "ERROR: the RAM was not successfully saved!");
}
}
else
{
TRACE(disp, "no reset - save request");
printText("...done", 3, -1, disp);
}
display_show(disp);
return 1;
}
//before boot_simulation
//write a cart-save from a file to the fpga/cart
int saveTypeFromSd(display_context_t disp, char *rom_name, int stype)
{
TRACE(disp, rom_filename);
const char* save_type_extension = saveTypeToExtension(stype, ext_type);
TCHAR fname[MAX_SUPPORTED_PATH_LEN] = {0};
int save_count = 0; //TODO: once this crosses 9999 bad infinite-loop type things happen, look into that one day
FRESULT result;
FILINFO fnoba;
printText("Finding latest save slot...", 3, -1, disp);
display_show(disp);
while (true) {
sprintf(fname, "/"ED64_FIRMWARE_PATH"/%s/%s.%04d.%s", save_path, rom_name, save_count, save_type_extension);
result = f_stat (fname, &fnoba);
if (result != FR_OK) {
// we found our first missing save slot, break
break;
}
++save_count;
}
if (save_count > 0) {
// we've went 1 past the end, so back up
sprintf(fname, "Found latest save slot: %04d", --save_count);
printText(fname, 3, -1, disp);
sprintf(fname, "/"ED64_FIRMWARE_PATH"/%s/%s.%04d.%s", save_path, rom_name, save_count, save_type_extension);
} else {
// not even a 0000 was found, so look at the original name before numbering was implemented
printText("No save slot found!", 3, -1, disp);
printText("Looking for non-numbered file...", 3, -1, disp);
sprintf(fname, "/"ED64_FIRMWARE_PATH"/%s/%s.%s", save_path, rom_name, save_type_extension);
}
display_show(disp);
int size = saveTypeToSize(stype); // int byte
uint8_t cartsave_data[size];
FIL file;
UINT bytesread;
result = f_open(&file, fname, FA_READ);
if (result == FR_OK)
{
int fsize = f_size(&file);
result =
f_read (
&file, /* [IN] File object */
cartsave_data, /* [OUT] Buffer to store read data */
size, /* [IN] Number of bytes to read */
&bytesread /* [OUT] Number of bytes read */
);
f_close(&file);
}
else
{
switch(result)
{
case FR_NOT_READY:
printText("not ready error", 11, -1, disp);
break;
case FR_NO_FILE:
printText("no file error", 11, -1, disp);
break;
case FR_NO_PATH:
printText("no path error", 11, -1, disp);
break;
case FR_INVALID_NAME:
printText("invalid name error", 11, -1, disp);
break;
case FR_DENIED:
printText("denied error", 11, -1, disp);
break;
case FR_EXIST:
printText("exist error", 11, -1, disp);
break;
case FR_TIMEOUT:
printText("timeout error", 11, -1, disp);
break;
case FR_LOCKED:
printText("locked error", 11, -1, disp);
break;
default:
break;
}
printText("no save found", 3, -1, disp);
//todo: clear memory area
return 0;
}
if (pushSaveToCart(stype, cartsave_data))
{
printText("transferred save data...", 3, -1, disp);
}
else
{
printText("error transfering save data", 3, -1, disp);
}
return 1;
}
int saveTypeToSd(display_context_t disp, char *rom_name, int stype)
{
//after reset create new savefile
const char* save_type_extension = saveTypeToExtension(stype, ext_type);
TCHAR fname[MAX_SUPPORTED_PATH_LEN];
int save_count = 0; //TODO: once this crosses 9999 bad infinite-loop type things happen, look into that one day
FRESULT result;
FILINFO fnoba;
printText("Finding unused save slot...", 3, -1, disp);
display_show(disp);
while (true) {
sprintf(fname, "/"ED64_FIRMWARE_PATH"/%s/%s.%04d.%s", save_path, rom_name, save_count, save_type_extension);
result = f_stat (fname, &fnoba);
if (result != FR_OK) {
// we found our first missing save slot, break
break;
}
++save_count;
}
sprintf(fname, "Found unused save slot: %04d", save_count);
printText(fname, 3, -1, disp);
display_show(disp);
sprintf(fname, "/"ED64_FIRMWARE_PATH"/%s/%s.%04d.%s", save_path, rom_name, save_count, save_type_extension);
int size = saveTypeToSize(stype); // int byte
TRACEF(disp, "size for save=%i", size);
FIL file;
UINT bytesread;
result = f_open(&file, fname, FA_WRITE | FA_OPEN_ALWAYS); //Could use FA_CREATE_ALWAYS but this could lead to the posibility of the file being emptied
if (result == FR_OK)
{
//for savegame
uint8_t cartsave_data[size]; //TODO: bring back old initialisation if this doesn't work
TRACEF(disp, "cartsave_data=%p", &cartsave_data);
printText("Transfering save data...", 3, -1, disp);
if (getSaveFromCart(stype, cartsave_data))
{
UINT bw;
result = f_write (
&file, /* [IN] Pointer to the file object structure */
cartsave_data, /* [IN] Pointer to the data to be written */
size, /* [IN] Number of bytes to write */
&bw /* [OUT] Pointer to the variable to return number of bytes written */
);
f_close(&file);
printText("RAM area copied to SD card.", 3, -1, disp);
return 1;
}
else
{
f_close(&file);
printText("Error saving game to SD", 3, -1, disp);
return 0;
}
}
else
{
TRACE(disp, "COULDNT CREATE FILE :-(");
printText("Error saving game to SD, couldn't create file!", 3, -1, disp);
}
}
//check out the userfriendly ini file for config-information
int readConfigFile(void)
{
TCHAR filename[MAX_SUPPORTED_PATH_LEN];
sprintf(filename, "/"ED64_FIRMWARE_PATH"/ALT64.INI");
FRESULT result;
FIL file;
UINT bytesread;
result = f_open(&file, filename, FA_READ);
if (result == FR_OK)
{
int fsize = f_size(&file);
char config_rawdata[fsize];
result =
f_read (
&file, /* [IN] File object */
config_rawdata, /* [OUT] Buffer to store read data */
fsize, /* [IN] Number of bytes to read */
&bytesread /* [OUT] Number of bytes read */
);
f_close(&file);
configuration config;
if (ini_parse_str(config_rawdata, configHandler, &config) < 0)
{
return 0;
}
else
{
border_color_1_s = config.border_color_1;
border_color_2_s = config.border_color_2;
box_color_s = config.box_color;
selection_color_s = config.selection_color;
selection_font_color_s = config.selection_font_color;
list_font_color_s = config.list_font_color;
list_dir_font_color_s = config.list_dir_font_color;
mempak_path = config.mempak_path;
save_path = config.save_path;
sound_on = config.sound_on;
page_display = config.page_display;
tv_mode = config.tv_mode;
quick_boot = config.quick_boot;
enable_colored_list = config.enable_colored_list;
ext_type = config.ext_type;
cd_behaviour = config.cd_behaviour;
scroll_behaviour = config.scroll_behaviour;
text_offset = config.text_offset;
hide_sysfolder = config.hide_sysfolder;
sd_speed = config.sd_speed;
background_image = config.background_image;
return 1;
}
}
}
int str2int(char data)
{
data -= '0';
if (data > 9)
data -= 7;
return data;
}
uint32_t translate_color(char *hexstring)
{
int r = str2int(hexstring[0]) * 16 + str2int(hexstring[1]);
int g = str2int(hexstring[2]) * 16 + str2int(hexstring[3]);
int b = str2int(hexstring[4]) * 16 + str2int(hexstring[5]);
int a = str2int(hexstring[6]) * 16 + str2int(hexstring[7]);
return graphics_make_color(r, g, b, a);
}
//init fat filesystem after everdrive init and before sdcard access
void initFilesystem(void)
{
evd_ulockRegs();
sleep(10);
fs = malloc(sizeof (FATFS)); /* Get work area for the volume */
FRESULT result = f_mount(fs,"",1);
if(result != FR_OK)
{
//printText("mount error", 11, -1, disp);
}
else
{
fat_initialized = 1;
}
}
//prints the sdcard-filesystem content
void readSDcard(display_context_t disp, char *directory)
{ //TODO: readd coloured list? use a hash table...
// FatRecord *frec;
// u8 cresp = 0;
// //load the directory-entry
// cresp = fatLoadDirByName("/"ED64_FIRMWARE_PATH"/CFG");
// int dsize = dir->size;
// char colorlist[dsize][256];
// if (enable_colored_list)
// {
// for (int i = 0; i < dir->size; i++)
// {
// frec = dir->rec[i];
// u8 rom_cfg_file[MAX_SUPPORTED_PATH_LEN];
// //set rom_cfg
// sprintf(rom_cfg_file, "/"ED64_FIRMWARE_PATH"/CFG/%s", frec->name);
// static uint8_t cfg_file_data[512] = {0};
// FRESULT result;
// FIL file;
// UINT bytesread;
// result = f_open(&file, rom_cfg_file, FA_READ);
// if (result == FR_OK)
// {
// int fsize = f_size(&file);
// result =
// f_read (
// &file, /* [IN] File object */
// &cfg_file_data, /* [OUT] Buffer to store read data */
// fsize, /* [IN] Number of bytes to read */
// &bytesread /* [OUT] Number of bytes read */
// );
// f_close(&file);
// colorlist[i][0] = (char)cfg_file_data[5]; //row i column 0 = colour
// strcpy(colorlist[i] + 1, cfg_file_data + 32); //row i column 1+ = fullpath
// }
// }
// }
// u8 buff[32];
// //some trash buffer
// FatRecord *rec;
// u8 resp = 0;
count = 1;
//dir_t buf;
//clear screen and print the directory name
clearScreen(disp);
FRESULT res;
DIR dir;
UINT i;
static FILINFO fno;
res = f_opendir(&dir, directory); /* Open the directory */
if (res == FR_OK) {
for (;;) {
res = f_readdir(&dir, &fno); /* Read a directory item */
if (res != FR_OK || fno.fname[0] == 0) break; /* Break on error or end of dir */
if (!strcmp(fno.fname, "System Volume Information") == 0 || (!strcmp(fno.fname, "ED64") == 0 && hide_sysfolder == 0))
{
if (fno.fattrib & AM_DIR) { /* It is a directory */
list[count - 1].type = DT_DIR;
} else { /* It is a file. */
//printf("%s/%s\n", path, fno.fname);
list[count - 1].type = DT_REG;
}
strcpy(list[count - 1].filename, fno.fname);
list[count - 1].color = 0;
// if (enable_colored_list)
// {
// for (int c = 0; c < dsize; c++)
// {
// u8 short_name[256];
// sprintf(short_name, "%s", colorlist[c] + 1);
// u8 *pch_s; // point-offset
// pch_s = strrchr(short_name, '/');
// if (strcmp(list[count - 1].filename, pch_s + 1) == 0)
// {
// list[count - 1].color = colorlist[c][0];
// }
// }
// //new color test end
// }
count++;
list = realloc(list, sizeof(direntry_t) * count);
}
}
f_closedir(&dir);
count--;
}
else
{
char error_msg[32];
sprintf(error_msg, "CHDIR ERROR: %i", res);
printText(error_msg, 3, -1, disp);
sleep(3000);
}
page = 0;
cursor = 0;
select_mode = 1;
if (count > 0)
{
/* Should sort! */
qsort(list, count, sizeof(direntry_t), compare);
}
//print directory
display_dir(list, cursor, page, MAX_LIST, count, disp);
}
/*
* Returns two cheat lists:
* - One for the "at boot" cheats
* - Another for the "in-game" cheats
*/
int readCheatFile(TCHAR *filename, u32 *cheat_lists[2])
{
// YAML parser
yaml_parser_t parser;
yaml_event_t event;
// State for YAML parser
int is_code = 0;
int code_on = 1;
int done = 0;
u32 *list1;
u32 *list2;
char *next;
int repeater = 0;
u32 address;
u32 value;
yaml_parser_initialize(&parser);
FRESULT result;
FIL file;
UINT bytesread;
result = f_open(&file, filename, FA_READ);
if (result == FR_OK)
{
int fsize = f_size(&file);
char *cheatfile = malloc(fsize);
if (!cheatfile)
{
return -2; // Out of memory
}
/*
* Size of the cheat list can never be more than half the size of the YAML
* Minimum YAML example:
* A:-80001234 FFFF
* Which is exactly 16 bytes.
* The cheat list in this case fits into exactly 8 bytes (2 words):
* 0x80001234, 0x0000FFFF
*/
list1 = calloc(1, fsize + 2 * sizeof(u32)); // Plus 2 words to be safe
if (!list1)
{
// Free
free(cheatfile);
return -2; // Out of memory
}
list2 = &list1[fsize / sizeof(u32) / 2];
cheat_lists[0] = list1;
cheat_lists[1] = list2;
result =
f_read (
&file, /* [IN] File object */
cheatfile, /* [OUT] Buffer to store read data */
fsize, /* [IN] Number of bytes to read */
&bytesread /* [OUT] Number of bytes read */
);
f_close(&file);
yaml_parser_set_input_string(&parser, cheatfile, strlen(cheatfile));
do
{
if (!yaml_parser_parse(&parser, &event))
{
// Free
yaml_parser_delete(&parser);
yaml_event_delete(&event);
free(cheatfile);
free(cheat_lists[0]);
cheat_lists[0] = 0;
cheat_lists[1] = 0;
return -3; // Parse error
}
// Process YAML
switch (event.type)
{
case YAML_MAPPING_START_EVENT:
// Begin code block
is_code = 0;
break;
case YAML_SEQUENCE_START_EVENT:
// Begin code lines
is_code = 1;
code_on = (event.data.sequence_start.tag ? !!strcasecmp(event.data.sequence_start.tag, "!off") : 1);
break;
case YAML_SEQUENCE_END_EVENT:
// End code lines
is_code = 0;
code_on = 1;
repeater = 0;
break;
case YAML_SCALAR_EVENT:
// Code line
if (!is_code || !code_on)
{
break;
}
address = strtoul(event.data.scalar.value, &next, 16);
value = strtoul(next, NULL, 16);
// Do not check code types within "repeater data"
if (repeater)
{
repeater--;
*list2++ = address;
*list2++ = value;
break;
}
// Determine destination cheat_list for the code type
switch (address >> 24)
{
// Uncessary code types
case 0x20: // Clear code list
case 0xCC: // Exception Handler Selection
case 0xDE: // Entry Point
break;
// Boot-time cheats
case 0xEE: // Disable Expansion Pak
case 0xF0: // 8-bit Boot-Time Write
case 0xF1: // 16-bit Boot-Time Write
case 0xFF: // Cheat Engine Location
*list1++ = address;
*list1++ = value;
break;
// In-game cheats
case 0x50: // Repeater/Patch
// Validate repeater count
if (address & 0x0000FF00)
{
repeater = 1;
*list2++ = address;
*list2++ = value;
}
break;
// Everything else
default:
if (!address)
{
// TODO: Support special code types! :)
}
// Fall-through!
case 0xD0: // 8-bit Equal-To Conditional
case 0xD1: // 16-bit Equal-To Conditional
case 0xD2: // 8-bit Not-Equal-To Conditional
case 0xD3: // 16-bit Not-Equal-To Conditional
// Validate 16-bit codes
if ((address & 0x01000001) == 0x01000001)
{
break;
}
*list2++ = address;
*list2++ = value;
break;
}
break;
case YAML_STREAM_END_EVENT:
// And we're outta here!
done = 1;
break;
default:
break;
}
yaml_event_delete(&event);
} while (!done);
// Free
yaml_parser_delete(&parser);
free(cheatfile);
return repeater; // Ok or repeater error
}
else
{
return -1; //err file not found
}
}
void bootRom(display_context_t disp, int silent)
{
if (boot_cic != 0)
{
// first load cheats if enabled, and error out if we can't
u32 *cheat_lists[2] = {NULL, NULL};
if (cheats_on)
{
gCheats = 1;
printText("try to load cheat-file...", 3, -1, disp);
char cheat_filename[64];
sprintf(cheat_filename, "/"ED64_FIRMWARE_PATH"/CHEATS/%s.yml", rom_filename);
int ok = readCheatFile(cheat_filename, cheat_lists);
if (ok == 0)
{
printText("cheats found...", 3, -1, disp);
}
else
{
printText("cheats not found", 3, -1, disp);
printText("or parsing failed", 3, -1, disp);
printText("reset console...", 3, -1, disp);
gCheats = 0;
while(true) {
sleep(20000);
}
}
}
else
{
gCheats = 0;
}
if (boot_save != 0)
{
TCHAR cfg_file[MAX_SUPPORTED_PATH_LEN];
//set cfg file with last loaded cart info and save-type
sprintf(cfg_file, "/"ED64_FIRMWARE_PATH"/%s/LASTROM.CFG", save_path);
FRESULT result;
FIL file;
result = f_open(&file, cfg_file, FA_WRITE | FA_CREATE_ALWAYS);
if (result == FR_OK)
{
uint8_t cfg_data[2] = {boot_save, boot_cic};
UINT bw;
result = f_write (
&file, /* [IN] Pointer to the file object structure */
&cfg_data, /* [IN] Pointer to the data to be written */
2, /* [IN] Number of bytes to write */
&bw /* [OUT] Pointer to the variable to return number of bytes written */
);
f_puts(rom_filename, &file);
f_close(&file);
//set the fpga cart-save type
evd_setSaveType(boot_save);
saveTypeFromSd(disp, rom_filename, boot_save);
}
}
TRACE(disp, "Cartridge-Savetype set");
TRACE(disp, "information stored for reboot-save...");
u32 cart, country;
u32 info = *(vu32 *)0xB000003C;
cart = info >> 16;
country = (info >> 8) & 0xFF;
disable_interrupts();
int bios_cic = getCicType(1);
if (checksum_fix_on)
{
checksum_sdram();
}
evd_lockRegs();
sleep(10);
while (!(disp = display_lock()))
;
//blank screen to avoid glitches
graphics_fill_screen(disp, 0x000000FF);
display_show(disp);
f_mount(0, "", 0); /* Unmount the default drive */
free(fs); /* Here the work area can be discarded */
simulate_boot(boot_cic, bios_cic, cheat_lists); // boot_cic
}
}
void playSound(int snd)
{
//no thread support in libdragon yet, sounds pause the menu for a time :/
if (snd == 1)
sndPlaySFX("rom://sounds/ed64_mono.wav");
if (snd == 2)
sndPlaySFX("rom://sounds/bamboo.wav");
if (snd == 3)
sndPlaySFX("rom://sounds/warning.wav");
if (snd == 4)
sndPlaySFX("rom://sounds/done.wav");
}
//draws the next char at the text input screen
void drawInputAdd(display_context_t disp, char *msg)
{
graphics_draw_box_trans(disp, 23, 5, 272, 18, 0x00000090);
position++;
sprintf(input_text, "%s%s", input_text, msg);
drawTextInput(disp, input_text);
}
//del the last char at the text input screen
void drawInputDel(display_context_t disp)
{
graphics_draw_box_trans(disp, 23, 5, 272, 18, 0x00000090);
if (position)
{
input_text[position - 1] = '\0';
drawTextInput(disp, input_text);
position--;
}
}
void drawTextInput(display_context_t disp, char *msg)
{
graphics_draw_text(disp, 40, 15, msg);
}
void drawConfirmBox(display_context_t disp)
{ while (!(disp = display_lock()))
;
new_scroll_pos(&cursor, &page, MAX_LIST, count);
clearScreen(disp); //part clear?
display_dir(list, cursor, page, MAX_LIST, count, disp);
drawBoxNumber(disp, 5);
display_show(disp);
if (sound_on)
playSound(3);
printText(" ", 9, 9, disp);
printText("Confirmation required:", 9, -1, disp);
printText(" ", 9, -1, disp);
printText(" ", 9, -1, disp);
printText(" Are you sure?", 9, -1, disp);
printText(" ", 9, -1, disp);
printText(" C-UP Continue ", 9, -1, disp); //set mapping 3
printText(" ", 9, -1, disp);
printText(" B Cancel", 9, -1, disp);
sleep(300);
}
void drawShortInfoBox(display_context_t disp, char *text, u8 mode)
{ while (!(disp = display_lock()))
;
new_scroll_pos(&cursor, &page, MAX_LIST, count);
clearScreen(disp); //part clear?
display_dir(list, cursor, page, MAX_LIST, count, disp);
if (mode == 0)
drawBoxNumber(disp, 7);
else if (mode == 1)
drawBoxNumber(disp, 8);
else if (mode == 2)
drawBoxNumber(disp, 10);
printText(text, 9, 14, disp);
display_show(disp);
if (sound_on)
{
if (mode == 0)
playSound(4);
else if (mode == 1)
playSound(3);
else if (mode == 2)
playSound(4);
}
sleep(300);
}
void readRomConfig(display_context_t disp, char *short_filename, char *full_filename)
{
TCHAR cfg_filename[MAX_SUPPORTED_PATH_LEN];
sprintf(rom_filename, "%s", short_filename);
rom_filename[strlen(rom_filename) - 4] = '\0'; // cut extension
sprintf(cfg_filename, "/"ED64_FIRMWARE_PATH"/CFG/%s.CFG", rom_filename);
uint8_t rom_cfg_data[512];
FRESULT result;
FIL file;
UINT bytesread;
result = f_open(&file, cfg_filename, FA_READ);
if (result == FR_OK)
{
result =
f_read (
&file, /* [IN] File object */
rom_cfg_data, /* [OUT] Buffer to store read data */
512, /* [IN] Number of bytes to read */
&bytesread /* [OUT] Number of bytes read */
);
f_close(&file);
rom_config[0] = 1; //preload cursor position 1 cic
rom_config[1] = rom_cfg_data[0];
rom_config[2] = rom_cfg_data[1];
rom_config[3] = rom_cfg_data[2];
rom_config[4] = rom_cfg_data[3];
rom_config[5] = rom_cfg_data[4];
rom_config[6] = rom_cfg_data[5];
rom_config[7] = rom_cfg_data[6];
rom_config[8] = rom_cfg_data[7];
rom_config[9] = rom_cfg_data[8];
}
else
{
//preload with header data
romInfoScreen(disp, full_filename, 1); //silent info screen with readout
}
}
void alterRomConfig(int type, int mode)
{
//mode 1 = increae mode 2 = decrease
//cic
u8 min_cic = 0;
u8 max_cic = 6;
//save
u8 min_save = 0;
u8 max_save = 5;
//tv-type
u8 min_tv = 0;
u8 max_tv = 3;
//cheat
u8 min_cheat = 0;
u8 max_cheat = 1;
//chk fix
u8 min_chk_sum = 0;
u8 max_chk_sum = 1;
//quality
u8 min_quality = 0;
u8 max_quality = 5;
//country
u8 min_country = 0;
u8 max_country = 2;
switch (type)
{
case 1:
//start cic
if (mode == 1)
{
//down
if (rom_config[1] < max_cic)
rom_config[1]++;
}
else if (mode == 2)
{
//up
if (rom_config[1] > min_cic)
rom_config[1]--;
}
//end cic
break;
case 2:
//start save
if (mode == 1)
{
//down
if (rom_config[2] < max_save)
rom_config[2]++;
}
else if (mode == 2)
{
//up
if (rom_config[2] > min_save)
rom_config[2]--;
}
//end save
break;
case 3:
//start tv
if (mode == 1)
{
//down
if (rom_config[3] < max_tv)
rom_config[3]++;
}
else if (mode == 2)
{
//up
if (rom_config[3] > min_tv)
rom_config[3]--;
}
//end tv
break;
case 4:
//start cheat
if (mode == 1)
{
//down
if (rom_config[4] < max_cheat)
rom_config[4]++;
}
else if (mode == 2)
{
//up
if (rom_config[4] > min_cheat)
rom_config[4]--;
}
//end cheat
break;
case 5:
//start chk sum
if (mode == 1)
{
//down
if (rom_config[5] < max_chk_sum)
rom_config[5]++;
}
else if (mode == 2)
{
//up
if (rom_config[5] > min_chk_sum)
rom_config[5]--;
}
//end chk sum
break;
case 6:
//start quality
if (mode == 1)
{
//down
if (rom_config[6] < max_quality)
rom_config[6]++;
}
else if (mode == 2)
{
//up
if (rom_config[6] > min_quality)
rom_config[6]--;
}
break;
case 7:
//start country
if (mode == 1)
{
//down
if (rom_config[7] < max_country)
rom_config[7]++;
}
else if (mode == 2)
{
//up
if (rom_config[7] > min_country)
rom_config[7]--;
}
break;
default:
break;
}
}
void drawToplistBox(display_context_t disp, int line)
{
list_pos_backup[0] = cursor;
list_pos_backup[1] = page;
int dsize = 0;
u8 list_size = 0;
if (line == 0)
{
char* path = "/"ED64_FIRMWARE_PATH"/CFG";
FRESULT res;
DIR dir;
UINT i;
static FILINFO fno;
//TODO: is there a better way we can count the entries perhaps a hashtable?
res = f_opendir(&dir, path); /* Open the directory */
if (res == FR_OK) {
for (;;) {
res = f_readdir(&dir, &fno); /* Read a directory item */
if (res != FR_OK || fno.fname[0] == 0) break; /* Break on error or end of dir */
if (!fno.fattrib & !AM_DIR) {
dsize++;
}
}
f_closedir(&dir);
}
res = f_opendir(&dir, path); /* Open the directory */
if (res == FR_OK) {
char toplist[dsize][256];
for (;;) {
res = f_readdir(&dir, &fno); /* Read a directory item */
if (res != FR_OK || fno.fname[0] == 0) break; /* Break on error or end of dir */
if (fno.fattrib & AM_DIR) { /* It is a directory */
//i = strlen(path);
// sprintf(&path[i], "/%s", fno.fname);
// res = scan_files(path); /* Enter the directory */
// if (res != FR_OK) break;
// path[i] = 0;
} else { /* It is a file. */
TCHAR rom_cfg_file[MAX_SUPPORTED_PATH_LEN];
//set rom_cfg
sprintf(rom_cfg_file, path, fno.fname);
FRESULT result;
FIL file;
UINT bytesread;
result = f_open(&file, rom_cfg_file, FA_READ);
if (result == FR_OK)
{
static uint8_t cfg_file_data[512] = {0};
int fsize = f_size(&file) + 1;
result =
f_read (
&file, /* [IN] File object */
cfg_file_data, /* [OUT] Buffer to store read data */
fsize, /* [IN] Number of bytes to read */
&bytesread /* [OUT] Number of bytes read */
);
f_close(&file);
toplist[i][0] = (char)cfg_file_data[5]; //quality
strcpy(toplist[i] + 1, cfg_file_data + 32); //fullpath
i++;
}
}
}
f_closedir(&dir);
qsort(toplist, dsize, 256, compare_int_reverse);
if (dsize > 15)
list_size = 15;
else
list_size = dsize;
for (int c = 0; c < list_size; c++)
strcpy(toplist15[c], toplist[c]);
list_pos_backup[2] = list_size;
}
}
list_size = list_pos_backup[2];
u8 min = 1;
u8 max = 15;
u8 real_max = 0;
for (int c = 0; c < list_size; c++)
if (toplist15[c][0] != 0)
real_max++;
max = real_max;
//cursor line
if (line != 0)
{
if (line == 1)
{
//down
if (toplist_cursor < max)
toplist_cursor++;
}
else if (line == 2)
{
//up
if (toplist_cursor > min)
toplist_cursor--;
}
}
drawBoxNumber(disp, 9); //toplist
printText(" -Toplist 15-", 4, 7, disp);
printText(" ", 4, -1, disp);
uint32_t forecolor;
uint32_t backcolor;
backcolor = graphics_make_color(0x00, 0x00, 0x00, 0x00); //bg
forecolor = graphics_make_color(0xFF, 0xFF, 0xFF, 0xFF); //fg
for (int t = 0; t < 15; t++)
{
int quality_level = toplist15[t][0]; //quality
if (quality_level != 0)
{
switch (quality_level)
{
case 1:
forecolor = graphics_make_color(0xFF, 0xFF, 0xFF, 0xFF); //common (white)
break;
case 2:
forecolor = graphics_make_color(0x00, 0xFF, 0x00, 0xCF); //uncommon (green)
break;
case 3:
forecolor = graphics_make_color(0x1E, 0x90, 0xFF, 0xFF); //rare (blue)
break;
case 4:
forecolor = graphics_make_color(0x9B, 0x30, 0xFF, 0xFF); //epic (purple)
break;
case 5:
forecolor = graphics_make_color(0xFF, 0xA5, 0x00, 0xFF); //legendary (orange)
break;
default:
break;
}
graphics_set_color(forecolor, backcolor);
//max 32 chr
u8 short_name[256];
sprintf(short_name, "%s", toplist15[t] + 1);
u8 *pch_s; // point-offset
pch_s = strrchr(short_name, '/');
u8 *pch; // point-offset
pch = strrchr(pch_s, '.');
pch_s[30] = '\0'; //was 31
pch_s[pch - pch_s] = '\0';
if (t + 1 == toplist_cursor)
printText(pch_s + 1, 5, -1, disp);
else
printText(pch_s + 1, 4, -1, disp);
//restore color
graphics_set_color(graphics_make_color(0xFF, 0xFF, 0xFF, 0xFF), graphics_make_color(0x00, 0x00, 0x00, 0x00));
}
}
}
void drawRomConfigBox(display_context_t disp, int line)
{
u8 min = 1;
u8 max = 7;
//cursor line
if (line != 0)
{
if (line == 1)
{
//down
if (rom_config[0] < max)
rom_config[0]++;
}
else if (line == 2)
{
//up
if (rom_config[0] > min)
rom_config[0]--;
}
}
drawBoxNumber(disp, 6);
drawConfigSelection(disp, rom_config[0]);
printText(" ", 9, 9, disp);
printText("Rom configuration:", 9, -1, disp);
printText(" ", 9, -1, disp);
switch (rom_config[1])
{
case 0:
printText(" CIC: 6101", 9, -1, disp);
break;
case 1:
printText(" CIC: 6102", 9, -1, disp);
break;
case 2:
printText(" CIC: 6103", 9, -1, disp);
break;
case 3:
printText(" CIC: 5101", 9, -1, disp);
break;
case 4:
printText(" CIC: 6105", 9, -1, disp);
break;
case 5:
printText(" CIC: 6106", 9, -1, disp);
break;
case 6:
printText(" CIC: 5167", 9, -1, disp);
break;
default:
break;
}
switch (rom_config[2])
{
case 0:
printText(" Save: Off/Mempak", 9, -1, disp);
<