1
0
mirror of https://github.com/parasyte/alt64 synced 2024-08-13 15:53:52 -04:00
alt64/utils.c

1619 lines
53 KiB
C
Raw Normal View History

2014-06-29 01:10:11 -04:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <malloc.h>
#include <libdragon.h>
#include <n64sys.h>
#include "everdrive.h"
#include "sys.h"
#include "types.h"
#include "utils.h"
#include "sram.h"
#define STBI_HEADER_FILE_ONLY
#include "stb_image.c"
extern short int gCheats; /* 0 = off, 1 = select, 2 = all */
extern short int force_tv;
extern short int boot_country;
struct gscEntry {
char *description;
char *gscodes;
u16 count;
u16 state;
u16 mask;
u16 value;
};
typedef struct gscEntry gscEntry_t;
extern gscEntry_t gGSCodes[];
static u8 __attribute__((aligned(16))) dmaBuf[128*1024];
static volatile struct _PI_regs_s * const _PI_regs = (struct _PI_regs_s *)0xa4600000;
2014-06-30 03:49:02 -04:00
int is_valid_rom(unsigned char *buffer) {
2014-06-29 01:10:11 -04:00
/* Test if rom is a native .z64 image with header 0x80371240. [ABCD] */
if((buffer[0]==0x80)&&(buffer[1]==0x37)&&(buffer[2]==0x12)&&(buffer[3]==0x40))
return 0;
/* Test if rom is a byteswapped .v64 image with header 0x37804012. [BADC] */
else if((buffer[0]==0x37)&&(buffer[1]==0x80)&&(buffer[2]==0x40)&&(buffer[3]==0x12))
return 1;
/* Test if rom is a wordswapped .n64 image with header 0x40123780. [DCBA] */
else if((buffer[0]==0x40)&&(buffer[1]==0x12)&&(buffer[2]==0x37)&&(buffer[3]==0x80))
return 2;
else
return 0;
}
2014-06-30 03:49:02 -04:00
void swap_header(unsigned char* header, int loadlength) {
2014-06-29 01:10:11 -04:00
unsigned char temp;
int i;
/* Btyeswap if .v64 image. */
2014-06-30 03:49:02 -04:00
if( header[0]==0x37) {
for (i = 0; i < loadlength; i+=2) {
2014-06-29 01:10:11 -04:00
temp= header[i];
2014-06-30 03:49:02 -04:00
header[i]= header[i+1];
header[i+1]=temp;
2014-06-29 01:10:11 -04:00
}
}
/* Wordswap if .n64 image. */
2014-06-30 03:49:02 -04:00
else if( header[0]==0x40) {
for (i = 0; i < loadlength; i+=4) {
2014-06-29 01:10:11 -04:00
temp= header[i];
2014-06-30 03:49:02 -04:00
header[i]= header[i+3];
header[i+3]=temp;
2014-06-29 01:10:11 -04:00
temp= header[i+1];
2014-06-30 03:49:02 -04:00
header[i+1]= header[i+2];
header[i+2]=temp;
2014-06-29 01:10:11 -04:00
}
2014-06-30 03:49:02 -04:00
}
2014-06-29 01:10:11 -04:00
}
u8 getCicType(u8 bios_cic) {
u8 cic_buff[2048];
volatile u8 cic_chip;
2014-06-30 03:49:02 -04:00
volatile u32 val;
2014-06-29 01:10:11 -04:00
if (bios_cic) {
evd_setCfgBit(ED_CFG_SDRAM_ON, 0);
sleep(10);
val = *(u32 *) 0xB0000170;
dma_read_s(cic_buff, 0xB0000040, 1024);
cic_chip = get_cic(cic_buff);
evd_setCfgBit(ED_CFG_SDRAM_ON, 1);
sleep(10);
2014-06-30 03:49:02 -04:00
}
else {
2014-06-29 01:10:11 -04:00
val = *(u32 *) 0xB0000170;
dma_read_s(cic_buff, 0xB0000040, 1024);
cic_chip = get_cic(cic_buff);
}
2014-06-30 03:49:02 -04:00
2014-06-29 01:10:11 -04:00
return cic_chip;
}
2014-06-30 03:49:02 -04:00
int get_cic(unsigned char *buffer) {
2014-06-29 01:10:11 -04:00
unsigned int crc;
// figure out the CIC
crc = CRC_Calculate(0, buffer, 1000);
2014-06-30 03:49:02 -04:00
switch(crc) {
case 0x303faac9:
case 0xf0da3d50:
return 1;
case 0xf3106101:
return 2;
case 0xe7cd9d51:
return 3;
case 0x7ae65c9:
return 5;
case 0x86015f8f:
return 6;
2014-06-29 01:10:11 -04:00
}
2014-06-30 03:49:02 -04:00
2014-06-29 01:10:11 -04:00
return 2;
}
2014-06-30 03:49:02 -04:00
int get_cic_save(char *cartid, int *cic, int *save) {
2014-06-29 01:10:11 -04:00
// variables
int NUM_CARTS = 137;
int i;
//data arrays
2014-06-30 03:49:02 -04:00
/*
char *names[] = {
"gnuboy64lite", "FRAMTestRom", "SRAMTestRom", "Worms Armageddon",
"Super Smash Bros.", "Banjo-Tooie", "Blast Corps", "Bomberman Hero",
"Body Harvest", "Banjo-Kazooie", "Bomberman 64",
"Bomberman 64: Second Attack", "Command & Conquer", "Chopper Attack",
"NBA Courtside 2 featuring Kobe Bryant", "Penny Racers",
"Chameleon Twist", "Cruis'n USA", "Cruis'n World",
"Legend of Zelda: Majora's Mask, The", "Donkey Kong 64",
"Donkey Kong 64", "Donald Duck: Goin' Quackers",
"Loony Toons: Duck Dodgers", "Diddy Kong Racing", "PGA European Tour",
"Star Wars Episode 1 Racer", "AeroFighters Assault", "Bass Hunter 64",
"Conker's Bad Fur Day", "F-1 World Grand Prix", "Star Fox 64",
"F-Zero X", "GT64 Championship Edition", "GoldenEye 007", "Glover",
"Bomberman 64", "Indy Racing 2000",
"Indiana Jones and the Infernal Machine", "Jet Force Gemini",
"Jet Force Gemini", "Earthworm Jim 3D", "Snowboard Kids 2",
"Kirby 64: The Crystal Shards", "Fighters Destiny",
"Major League Baseball featuring Ken Griffey Jr.",
"Killer Instinct Gold", "Ken Griffey Jr's Slugfest", "Mario Kart 64",
"Mario Party", "Lode Runner 3D", "Megaman 64", "Mario Tennis",
"Mario Golf", "Mission: Impossible", "Mickey's Speedway USA",
"Monopoly", "Paper Mario", "Multi-Racing Championship",
"Big Mountain 2000", "Mario Party 3", "Mario Party 2", "Excitebike 64",
"Dr. Mario 64", "Star Wars Episode 1: Battle for Naboo",
"Kobe Bryant in NBA Courtside", "Excitebike 64",
"Ogre Battle 64: Person of Lordly Caliber", "Pokémon Stadium 2",
"Pokémon Stadium 2", "Perfect Dark", "Pokémon Snap",
"Hey you, Pikachu!", "Pokémon Snap", "Pokémon Puzzle League",
"Pokémon Stadium", "Pokémon Stadium", "Pilotwings 64",
"Top Gear Overdrive", "Resident Evil 2", "New Tetris, The",
"Star Wars: Rogue Squadron", "Ridge Racer 64",
"Star Soldier: Vanishing Earth", "AeroFighters Assault",
"Starshot Space Circus", "Super Mario 64", "Starcraft 64",
"Rocket: Robot on Wheels", "Space Station Silicon Valley",
"Star Wars: Shadows of the Empire", "Tigger's Honey Hunt",
"1080º Snowboarding", "Tom & Jerry in Fists of Furry",
"Mischief Makers", "All-Star Tennis '99", "Tetrisphere",
"V-Rally Edition '99", "V-Rally Edition '99", "WCW/NWO Revenge",
"WWF: No Mercy", "Waialae Country Club: True Golf Classics",
"Wave Race 64", "Worms Armageddon", "WWF: Wrestlemania 2000",
"Cruis'n Exotica", "Yoshi's Story", "Harvest Moon 64",
"Legend of Zelda: Ocarina of Time, The",
"Legend of Zelda: Majora's Mask, The", "Airboarder 64",
"Bakuretsu Muteki Bangaioh", "Choro-Q 64 II", "Custom Robo",
"Custom Robo V2", "Densha de Go! 64", "Doraemon: Mittsu no Seireiseki",
"Dezaemon 3D", "Transformers Beast Wars",
"Transformers Beast Wars Metals", "64 Trump Collection", "Bass Rush",
"ECW Hardcore Revolution", "40 Winks", "Aero Gauge",
"Aidyn Chronicles The First Mage", "Derby Stallion 64",
"Doraemon 2 - Hikari no Shinden", "Doraemon 3 - Nobi Dai No Machi SOS",
"F-1 World Grand Prix II", "Fushigi no Dungeon - Furai no Shiren 2",
"Heiwa Pachinko World 64", "Neon Genesis Evangelion",
"Racing Simulation", "Tsumi to Batsu", "Sonic Wings Assault",
"Virtual Pro Wrestling", "Virtual Pro Wrestling 2", "Wild Choppers"
};
*/
char *cartIDs[] = {
"DZ", "B6", "ZY", "ZZ", "AD", "AL", "B7", "BC", "BD", "BH", "BK", "BM",
"BV", "CC", "CH", "CK", "CR", "CT", "CU", "CW", "DL", "DO", "DP", "DQ",
"DU", "DY", "EA", "EP", "ER", "FH", "FU", "FW", "FX", "FZ", "GC", "GE",
"GV", "HA", "IC", "IJ", "JD", "JF", "JM", "K2", "K4", "KA", "KG", "KI",
"KJ", "KT", "LB", "LR", "M6", "M8", "MF", "MI", "ML", "MO", "MQ", "MR",
"MU", "MV", "MW", "MX", "N6", "NA", "NB", "NX", "OB", "P2", "P3", "PD",
"PF", "PG", "PH", "PN", "PO", "PS", "PW", "RC", "RE", "RI", "RS", "RZ",
"S6", "SA", "SC", "SM", "SQ", "SU", "SV", "SW", "T9", "TE", "TJ", "TM",
"TN", "TP", "VL", "VY", "W2", "W4", "WL", "WR", "WU", "WX", "XO", "YS",
"YW", "ZL", "ZS", "AB", "BN", "CG", "CX", "CZ", "D6", "DR", "DZ", "OH",
"TB", "TC", "VB", "WI", "4W", "AG", "AY", "DA", "D2", "3D", "F2", "SI",
"HP", "EV", "MG", "GU", "SA", "VP", "A2", "WC"
};
/*
int saveTypes[] = {
5, 1, 6, 5, 5, 5, 5, 5, 5, 4, 5, 4, 5, 5, 5, 6, 4, 6, 6, 5, 5, 5, 5, 6,
5, 5, 6, 5, 5, 1, 5, 5, 5, 5, 5, 5, 4, 4, 5, 5, 5, 5, 1, 5, 4, 5, 5, 5,
4, 6, 1, 5, 5, 5, 4, 5, 5, 6, 5, 6, 5, 5, 6, 6, 1, 4, 4, 6, 4, 5, 4, 4,
4, 4, 5, 5, 1, 1, 5, 6, 5, 5, 5, 5, 4, 5, 5, 5, 4, 1, 5, 5, 5, 5, 5, 5,
1, 4, 5, 5, 5, 1, 5, 6, 1, 1, 4, 5, 5, 5, 5, 6, 1, 5, 1, 5, 5, 5, 1, 1,
5, 5, 1, 1, 6, 6, 6, 4, 5, 6, 5, 5, 5, 1, 1, 5
};
*/
// Banjo-Tooie B7 -> set to sram 'cause crk converts ek16->sram
int saveTypes[] = {
2, 1, 5, 1, 3, 1, 1, 3, 3, 3, 3, 3, 3, 5, 3, 5, 3, 3, 3, 4, 5, 4, 4, 3,
3, 3, 3, 4, 3, 3, 4, 3, 3, 1, 3, 3, 3, 3, 3, 3, 5, 5, 3, 3, 3, 3, 1, 3,
5, 3, 3, 3, 5, 4, 1, 3, 3, 3, 5, 3, 3, 4, 3, 4, 3, 3, 4, 4, 1, 5, 5, 4,
5, 3, 5, 5, 5, 5, 3, 3, 1, 1, 3, 4, 3, 3, 3, 3, 5, 3, 3, 3, 5, 1, 3, 3,
3, 3, 3, 3, 1, 5, 3, 3, 3, 1, 3, 4, 1, 1, 5, 3, 3, 3, 3, 4, 1, 3, 1, 3,
3, 3, 1, 1, 3, 3, 1, 1, 4, 4, 4, 5, 3, 4, 3, 3, 3, 1, 1, 3
};
//bt cic to 2 pos6 was 5
int cicTypes[] = {
2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 2, 6, 5, 5, 5, 2,
2, 3, 2, 2, 2, 2, 5, 2, 1, 6, 2, 2, 2, 2, 2, 2, 5, 5, 2, 2, 3, 2, 3, 2,
3, 2, 2, 2, 2, 2, 2, 2, 5, 2, 3, 2, 2, 2, 2, 3, 2, 2, 3, 3, 2, 3, 3, 5,
3, 2, 3, 2, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 2, 5, 5, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
};
2014-06-29 01:10:11 -04:00
// search for cartid
for (i=0; i<NUM_CARTS; i++)
if (strcmp(cartid, cartIDs[i]) == 0)
break;
2014-06-30 03:49:02 -04:00
if (i == NUM_CARTS) {
2014-06-29 01:10:11 -04:00
// cart not in list
*cic = 2;
*save = 0;
return 0; // not found
}
// cart found
*cic = cicTypes[i];
*save = saveTypes[i];
2014-06-30 03:49:02 -04:00
2014-06-29 01:10:11 -04:00
return 1; // found
}
2014-06-30 03:49:02 -04:00
const char* saveTypeToExtension(int type, int etype) {
static char* str = "SAV";
if(etype==0){
switch(type) {
case 0: str = "OFF"; break;
case 1: str = "SRM"; break;
case 2: str = "128"; break;
case 3: str = "E4K"; break;
case 4: str = "E16"; break;
case 5: str = "FLA"; break;
default: str = "SAV";
}
}
else {
switch(type) {
case 0: str = "OFF"; break;
case 1: str = "SRA"; break;
case 2: str = "SRA"; break;
case 3: str = "EEP"; break;
case 4: str = "EEP"; break;
case 5: str = "FLA"; break;
default: str = "SAV";
}
}
return str;
2014-06-29 01:10:11 -04:00
}
2014-06-30 03:49:02 -04:00
int saveTypeToSize(int type) {
switch(type) {
2014-06-29 01:10:11 -04:00
case 0: return 0; break;
case 1: return SAVE_SIZE_SRAM; break;
case 2: return SAVE_SIZE_SRAM128; break;
case 3: return SAVE_SIZE_EEP4k; break;
case 4: return SAVE_SIZE_EEP16k; break;
case 5: return SAVE_SIZE_FLASH; break;
default: return 0;
}
}
/*
#define SAVE_TYPE_OFF 0
#define SAVE_TYPE_SRAM 1
#define SAVE_TYPE_SRAM128 2
#define SAVE_TYPE_EEP4k 3
#define SAVE_TYPE_EEP16k 4
#define SAVE_TYPE_FLASH 5
*/
//switch to the correct dump function
2014-06-30 03:49:02 -04:00
int getSaveFromCart(int stype, uint8_t *buffer) {
int ret=0;
switch(stype) {
case 0: return 0;
2014-06-29 01:10:11 -04:00
case 1: ret = getSRAM32(buffer); break;
case 2: ret = getSRAM128(buffer); break;
case 3: ret = getEeprom4k(buffer); break;
case 4: ret = getEeprom16k(buffer); break;
case 5: ret = getFlashRAM(buffer); break;
default: return 0;
}
2014-06-30 03:49:02 -04:00
return ret;
2014-06-29 01:10:11 -04:00
}
//switch to the correct upload function
int pushSaveToCart(int stype, uint8_t *buffer){
2014-06-30 03:49:02 -04:00
int ret=0;
switch(stype) {
case 0: return 0;
2014-06-29 01:10:11 -04:00
case 1: ret = setSRAM32(buffer); break;
case 2: ret = setSRAM128(buffer); break;
case 3: ret = setEeprom4k(buffer); break;
case 4: ret = setEeprom16k(buffer); break;
case 5: ret = setFlashRAM(buffer); break;
default: return 0;
}
2014-06-30 03:49:02 -04:00
return ret;
2014-06-29 01:10:11 -04:00
}
2014-06-30 03:49:02 -04:00
int getSRAM( uint8_t *buffer, int size){
while (dma_busy()) ;
2014-06-29 01:10:11 -04:00
2014-06-30 03:49:02 -04:00
IO_WRITE(PI_BSD_DOM2_LAT_REG, 0x05);
2014-06-29 01:10:11 -04:00
IO_WRITE(PI_BSD_DOM2_PWD_REG, 0x0C);
IO_WRITE(PI_BSD_DOM2_PGS_REG, 0x0D);
IO_WRITE(PI_BSD_DOM2_RLS_REG, 0x02);
2014-06-30 03:49:02 -04:00
while (dma_busy()) ;
2014-06-29 01:10:11 -04:00
2014-06-30 03:49:02 -04:00
PI_Init();
2014-06-29 01:10:11 -04:00
2014-06-30 03:49:02 -04:00
sleep(1000);
2014-06-29 01:10:11 -04:00
2014-06-30 03:49:02 -04:00
while (dma_busy()) ;
2014-06-29 01:10:11 -04:00
2014-06-30 03:49:02 -04:00
PI_DMAFromSRAM(buffer, 0, size) ;
2014-06-29 01:10:11 -04:00
2014-06-30 03:49:02 -04:00
while (dma_busy()) ;
2014-06-29 01:10:11 -04:00
2014-06-30 03:49:02 -04:00
IO_WRITE(PI_BSD_DOM2_LAT_REG, 0x40);
IO_WRITE(PI_BSD_DOM2_PWD_REG, 0x12);
IO_WRITE(PI_BSD_DOM2_PGS_REG, 0x07);
IO_WRITE(PI_BSD_DOM2_RLS_REG, 0x03);
2014-06-29 01:10:11 -04:00
2014-06-30 03:49:02 -04:00
return 1;
}
2014-06-29 01:10:11 -04:00
2014-06-30 03:49:02 -04:00
int getSRAM32( uint8_t *buffer) {
return getSRAM(buffer, SAVE_SIZE_SRAM);
}
2014-06-29 01:10:11 -04:00
2014-06-30 03:49:02 -04:00
int getSRAM128( uint8_t *buffer) {
return getSRAM(buffer, SAVE_SIZE_SRAM128);
}
2014-06-29 01:10:11 -04:00
2014-06-30 03:49:02 -04:00
//working hurray :D
int getEeprom4k( uint8_t *buffer) {
if(eeprom_present()){
int blocks=SAVE_SIZE_EEP4k/8;
for( int b = 0; b < blocks; b++ ) {
eeprom_read( b, &buffer[b * 8] );
}
return 1;
}
2014-06-29 01:10:11 -04:00
2014-06-30 03:49:02 -04:00
return 0;
}
2014-06-29 01:10:11 -04:00
2014-06-30 03:49:02 -04:00
int getEeprom16k( uint8_t *buffer){
int blocks=SAVE_SIZE_EEP16k/8;
for( int b = 0; b < blocks; b++ ) {
eeprom_read( b, &buffer[b * 8] );
}
2014-06-29 01:10:11 -04:00
2014-06-30 03:49:02 -04:00
return 1;
}
2014-06-29 01:10:11 -04:00
2014-06-30 03:49:02 -04:00
int getFlashRAM( uint8_t *buffer){
evd_setSaveType(SAVE_TYPE_SRAM128); //2
sleep(10);
2014-06-29 01:10:11 -04:00
2014-06-30 03:49:02 -04:00
int s = getSRAM(buffer, SAVE_SIZE_SRAM128);
data_cache_hit_writeback_invalidate(buffer,SAVE_SIZE_SRAM128);
2014-06-29 01:10:11 -04:00
2014-06-30 03:49:02 -04:00
sleep(10);
evd_setSaveType(SAVE_TYPE_FLASH); //5
return 1;
2014-06-29 01:10:11 -04:00
}
2014-06-30 03:49:02 -04:00
2014-06-29 01:10:11 -04:00
/*
sram upload
2014-06-30 03:49:02 -04:00
*/
int setSRAM( uint8_t *buffer,int size){
//half working
PI_DMAWait();
//Timing
PI_Init_SRAM();
//Readmode
PI_Init();
data_cache_hit_writeback_invalidate(buffer,size);
while (dma_busy());
PI_DMAToSRAM(buffer, 0, size);
data_cache_hit_writeback_invalidate(buffer,size);
//Wait
PI_DMAWait();
//Restore evd Timing
setSDTiming();
return 1;
}
2014-06-29 01:10:11 -04:00
2014-06-30 03:49:02 -04:00
int setSRAM32( uint8_t *buffer){
return setSRAM(buffer, SAVE_SIZE_SRAM);
}
2014-06-29 01:10:11 -04:00
2014-06-30 03:49:02 -04:00
int setSRAM128( uint8_t *buffer){
return setSRAM(buffer, SAVE_SIZE_SRAM128);
}
2014-06-29 01:10:11 -04:00
2014-06-30 03:49:02 -04:00
//working hurray :D
int setEeprom4k( uint8_t *buffer){
if(eeprom_present()){
int blocks=SAVE_SIZE_EEP4k/8;
for( int b = 0; b < blocks; b++ ) {
eeprom_write( b, &buffer[b * 8] );
}
return 1;
}
2014-06-29 01:10:11 -04:00
2014-06-30 03:49:02 -04:00
return 0;
}
2014-06-29 01:10:11 -04:00
2014-06-30 03:49:02 -04:00
int setEeprom16k(uint8_t *buffer){
int blocks=SAVE_SIZE_EEP16k/8;
for( int b = 0; b < blocks; b++ ) {
eeprom_write( b, &buffer[b * 8] );
}
2014-06-29 01:10:11 -04:00
2014-06-30 03:49:02 -04:00
return 1;
}
2014-06-29 01:10:11 -04:00
//isn't working nor finished
2014-06-30 03:49:02 -04:00
int setFlashRAM(uint8_t *buffer){
evd_setSaveType(SAVE_TYPE_SRAM128); //2
sleep(10);
2014-06-29 01:10:11 -04:00
2014-06-30 03:49:02 -04:00
int s = setSRAM(buffer, SAVE_SIZE_SRAM128);
2014-06-29 01:10:11 -04:00
2014-06-30 03:49:02 -04:00
evd_setSaveType(SAVE_TYPE_FLASH); //5
2014-06-29 01:10:11 -04:00
2014-06-30 03:49:02 -04:00
return 1;
2014-06-29 01:10:11 -04:00
}
void setSDTiming(void){
/*
PI_BSD_DOM1_LAT_REG (0x04600014) write word 0x000000FF
PI_BSD_DOM1_PWD_REG (0x04600018) write word 0x000000FF
PI_BSD_DOM1_PGS_REG (0x0460001C) write word 0x0000000F
PI_BSD_DOM1_RLS_REG (0x04600020) write word 0x00000003
2014-06-30 03:49:02 -04:00
*
2014-06-29 01:10:11 -04:00
PI_BSD_DOM1_LAT_REG (0x04600014) write word 0x00000040
PI_BSD_DOM1_PWD_REG (0x04600018) write word 0x00803712
PI_BSD_DOM1_PGS_REG (0x0460001C) write word 0x00008037
PI_BSD_DOM1_RLS_REG (0x04600020) write word 0x00000803
*/
2014-06-30 03:49:02 -04:00
// PI_DMAWait();
IO_WRITE(PI_BSD_DOM1_LAT_REG, 0x40);
2014-06-29 01:10:11 -04:00
IO_WRITE(PI_BSD_DOM1_PWD_REG, 0x12);
IO_WRITE(PI_BSD_DOM1_PGS_REG, 0x07);
IO_WRITE(PI_BSD_DOM1_RLS_REG, 0x03);
IO_WRITE(PI_BSD_DOM2_LAT_REG, 0x40);
IO_WRITE(PI_BSD_DOM2_PWD_REG, 0x12);
IO_WRITE(PI_BSD_DOM2_PGS_REG, 0x07);
IO_WRITE(PI_BSD_DOM2_RLS_REG, 0x03);
}
void _data_cache_invalidate_all(void) {
2014-06-30 03:49:02 -04:00
asm(
"li $8,0x80000000\n"
"li $9,0x80000000\n"
"addu $9,$9,0x1FF0\n"
"cacheloop:\n"
"cache 1,0($8)\n"
"cache 1,16($8)\n"
"cache 1,32($8)\n"
"cache 1,48($8)\n"
"cache 1,64($8)\n"
"cache 1,80($8)\n"
"cache 1,96($8)\n"
"addu $8,$8,112\n"
"bne $8,$9,cacheloop\n"
"cache 1,0($8)\n"
2014-06-29 01:10:11 -04:00
: // no outputs
: // no inputs
: "$8", "$9" // trashed registers
2014-06-30 03:49:02 -04:00
);
2014-06-29 01:10:11 -04:00
}
2014-06-30 03:49:02 -04:00
void restoreTiming(void) {
//n64 timing restore :>
IO_WRITE(PI_BSD_DOM1_LAT_REG, 0x40);
2014-06-29 01:10:11 -04:00
IO_WRITE(PI_BSD_DOM1_PWD_REG, 0x12);
IO_WRITE(PI_BSD_DOM1_PGS_REG, 0x07);
IO_WRITE(PI_BSD_DOM1_RLS_REG, 0x03);
IO_WRITE(PI_BSD_DOM2_LAT_REG, 0x40);
IO_WRITE(PI_BSD_DOM2_PWD_REG, 0x12);
IO_WRITE(PI_BSD_DOM2_PGS_REG, 0x07);
IO_WRITE(PI_BSD_DOM2_RLS_REG, 0x03);
}
/*
* Load an image from the rom filesystem, returning a pointer to the
* sprite that hold the image.
*/
2014-06-30 03:49:02 -04:00
sprite_t *loadImageDFS(char *fname) {
2014-06-29 01:10:11 -04:00
int size, x, y, n, fd;
u8 *tbuf;
u8 *ibuf;
sprite_t *sbuf;
fd = dfs_open(fname);
if (fd < 0)
return 0; // couldn't open image
size = dfs_size(fd);
tbuf = malloc(size);
2014-06-30 03:49:02 -04:00
if (!tbuf) {
2014-06-29 01:10:11 -04:00
dfs_close(fd);
return 0; // out of memory
}
dfs_read(tbuf, 1, size, fd);
dfs_close(fd);
ibuf = stbi_load_from_memory(tbuf, size, &x, &y, &n, 4);
free(tbuf);
if (!ibuf)
return 0; // couldn't decode image
sbuf = (sprite_t*)malloc(sizeof(sprite_t) + x * y * 2);
2014-06-30 03:49:02 -04:00
if (!sbuf) {
2014-06-29 01:10:11 -04:00
stbi_image_free(ibuf);
return 0; // out of memory
}
sbuf->width = x;
sbuf->height = y;
sbuf->bitdepth = 2;
sbuf->format = 0;
sbuf->hslices = x / 32;
sbuf->vslices = y / 16;
color_t *src = (color_t*)ibuf;
u16 *dst = (u16*)((u32)sbuf + sizeof(sprite_t));
for (int j=0; j<y; j++)
for (int i=0; i<x; i++)
dst[i + j*x] = graphics_convert_color(src[i + j*x]) & 0x0000FFFF;
2014-06-30 03:49:02 -04:00
2014-06-29 01:10:11 -04:00
/* Invalidate data associated with sprite in cache */
data_cache_hit_writeback_invalidate( sbuf->data, sbuf->width * sbuf->height * sbuf->bitdepth );
stbi_image_free(ibuf);
return sbuf;
}
2014-06-30 03:49:02 -04:00
sprite_t *loadImage32(u8 *png, int size) {
2014-06-29 01:10:11 -04:00
int x, y, n, fd;
u8 *tbuf;
u32 *ibuf;
sprite_t *sbuf;
2014-06-30 03:49:02 -04:00
tbuf = malloc(size);
memcpy(tbuf,png,size);
2014-06-29 01:10:11 -04:00
ibuf = stbi_load_from_memory(tbuf, size, &x, &y, &n, 4);
free(tbuf);
if (!ibuf)
return 0; // couldn't decode image
sbuf = (sprite_t*)malloc(sizeof(sprite_t) + x * y * 4);
2014-06-30 03:49:02 -04:00
if (!sbuf) {
2014-06-29 01:10:11 -04:00
stbi_image_free(ibuf);
return 0; // out of memory
}
2014-06-30 03:49:02 -04:00
2014-06-29 01:10:11 -04:00
sbuf->width = x;
sbuf->height = y;
sbuf->bitdepth = 4;
sbuf->format = 0;
sbuf->hslices = x / 32;
sbuf->vslices = y / 32;
// color_t *src = (color_t*)ibuf;
u32 *dst = (u32*)((u32)sbuf + sizeof(sprite_t));
for (int j=0; j<y; j++)
for (int i=0; i<x; i++)
dst[i + j*x] = ibuf[i + j*x];
2014-06-30 03:49:02 -04:00
2014-06-29 01:10:11 -04:00
/* Invalidate data associated with sprite in cache */
data_cache_hit_writeback_invalidate( sbuf->data, sbuf->width * sbuf->height * sbuf->bitdepth );
stbi_image_free(ibuf);
return sbuf;
}
2014-06-30 03:49:02 -04:00
sprite_t *loadImage32DFS(char *fname) {
2014-06-29 01:10:11 -04:00
int size, x, y, n, fd;
u8 *tbuf;
u32 *ibuf;
sprite_t *sbuf;
fd = dfs_open(fname);
if (fd < 0)
return 0; // couldn't open image
size = dfs_size(fd);
tbuf = malloc(size);
2014-06-30 03:49:02 -04:00
if (!tbuf) {
2014-06-29 01:10:11 -04:00
dfs_close(fd);
return 0; // out of memory
}
dfs_read(tbuf, 1, size, fd);
dfs_close(fd);
ibuf = stbi_load_from_memory(tbuf, size, &x, &y, &n, 4);
free(tbuf);
if (!ibuf)
return 0; // couldn't decode image
sbuf = (sprite_t*)malloc(sizeof(sprite_t) + x * y * 4);
2014-06-30 03:49:02 -04:00
if (!sbuf) {
2014-06-29 01:10:11 -04:00
stbi_image_free(ibuf);
return 0; // out of memory
}
2014-06-30 03:49:02 -04:00
2014-06-29 01:10:11 -04:00
sbuf->width = x;
sbuf->height = y;
sbuf->bitdepth = 4;
sbuf->format = 0;
sbuf->hslices = x / 32;
sbuf->vslices = y / 32;
// color_t *src = (color_t*)ibuf;
u32 *dst = (u32*)((u32)sbuf + sizeof(sprite_t));
for (int j=0; j<y; j++)
for (int i=0; i<x; i++)
dst[i + j*x] = ibuf[i + j*x];
2014-06-30 03:49:02 -04:00
2014-06-29 01:10:11 -04:00
/* Invalidate data associated with sprite in cache */
data_cache_hit_writeback_invalidate( sbuf->data, sbuf->width * sbuf->height * sbuf->bitdepth );
stbi_image_free(ibuf);
return sbuf;
}
/*
* Draw an image to the screen using the sprite passed.
*/
2014-06-30 03:49:02 -04:00
void drawImage(display_context_t dcon, sprite_t *sprite) {
2014-06-29 01:10:11 -04:00
int x, y = 0;
rdp_sync(SYNC_PIPE);
rdp_set_default_clipping();
rdp_enable_texture_copy();
rdp_attach_display(dcon);
// Draw image
2014-06-30 03:49:02 -04:00
for (int j=0; j<sprite->vslices; j++) {
2014-06-29 01:10:11 -04:00
x = 0;
2014-06-30 03:49:02 -04:00
for (int i=0; i<sprite->hslices; i++) {
2014-06-29 01:10:11 -04:00
rdp_sync(SYNC_PIPE);
rdp_load_texture_stride(0, 0, MIRROR_DISABLED, sprite, j*sprite->hslices + i);
rdp_draw_sprite(0, x, y);
x += 32;
}
y += 16;
}
rdp_detach_display();
}
#define CIC_6101 1
#define CIC_6102 2
#define CIC_6103 3
#define CIC_6104 4
#define CIC_6105 5
#define CIC_6106 6
2014-06-30 03:49:02 -04:00
void globalTest(void) {
gCheats=1;
2014-06-29 01:10:11 -04:00
}
2014-06-30 03:49:02 -04:00
void simulate_boot(u32 cic_chip, u8 gBootCic) {
if (cic_chip == CIC_6104)
cic_chip = CIC_6102;
2014-06-29 01:10:11 -04:00
u32 ix, sz, cart, country;
vu32 *src, *dst;
u32 info = *(vu32 *)0xB000003C;
vu64 *gGPR = (vu64 *)0xA03E0000;
vu32 *codes = (vu32 *)0xA0000180;
u64 bootAddr = 0xFFFFFFFFA4000040LL;
char *cp, *vp, *tp;
char temp[8];
int i, type, val;
int curr_cheat = 0;
cart = info >> 16;
country = (info >> 8) & 0xFF;
2014-06-30 03:49:02 -04:00
if(boot_country!=0){
switch(boot_country){
case 1: country = 0x45; break; //ntsc region
case 2: country = 0x50; break; //pal region
default: break;
}
}
2014-06-29 01:10:11 -04:00
// clear XBUS/Flush/Freeze
((vu32 *)0xA4100000)[3] = 0x15;
2014-06-30 03:49:02 -04:00
2014-06-29 01:10:11 -04:00
sz = (gBootCic != CIC_6105) ? *(vu32 *)0xA0000318 : *(vu32 *)0xA00003F0;
if (cic_chip == CIC_6105)
*(vu32 *)0xA00003F0 = sz;
else
*(vu32 *)0xA0000318 = sz;
// clear some OS globals for cleaner boot
*(vu32 *)0xA000030C = 0; // cold boot
memset((void *)0xA000031C, 0, 64); // clear app nmi buffer
2014-06-30 03:49:02 -04:00
if (gCheats) {
2014-06-29 01:10:11 -04:00
u16 xv, yv, zv;
u32 xx;
vu32 *sp, *dp;
// get rom os boot segment - note, memcpy won't work for copying rom
sp = (vu32 *)0xB0001000;
dp = (vu32 *)0xA02A0000;
for (ix=0; ix<0x100000; ix++)
*dp++ = *sp++;
// default boot address with cheats
sp = (vu32 *)0xB0000008;
bootAddr = 0xFFFFFFFF00000000LL | *sp;
// move general int handler
sp = (vu32 *)0xA0000180;
dp = (vu32 *)0xA0000120;
for (ix=0; ix<0x60; ix+=4)
*dp++ = *sp++;
// insert new general int handler prologue
*codes++ = 0x401a6800; // mfc0 k0,c0_cause
*codes++ = 0x241b005c; // li k1,23*4
*codes++ = 0x335a007c; // andi k0,k0,0x7c
*codes++ = 0x175b0012; // bne k0,k1,0x1d8
*codes++ = 0x00000000; // nop
*codes++ = 0x40809000; // mtc0 zero,c0_watchlo
*codes++ = 0x401b7000; // mfc0 k1,c0_epc
*codes++ = 0x8f7a0000; // lw k0,0(k1)
*codes++ = 0x3c1b03e0; // lui k1,0x3e0
*codes++ = 0x035bd024; // and k0,k0,k1
*codes++ = 0x001ad142; // srl k0,k0,0x5
*codes++ = 0x3c1ba000; // lui k1,0xa000
*codes++ = 0x8f7b01cc; // lw k1,0x01cc(k1)
*codes++ = 0x035bd025; // or k0,k0,k1
*codes++ = 0x3c1ba000; // lui k1,0xa000
*codes++ = 0xaf7a01cc; // sw k0,0x01cc(k1)
*codes++ = 0x3c1b8000; // lui k1,0x8000
*codes++ = 0xbf7001cc; // cache 0x10,0x01cc(k1)
*codes++ = 0x3c1aa000; // lui k0,0xa000
*codes++ = 0x37400120; // ori zero,k0,0x120
*codes++ = 0x42000018; // eret
*codes++ = 0x00000000; // nop
// process cheats
2014-06-30 03:49:02 -04:00
while (gGSCodes[curr_cheat].count != 0xFFFF) {
if (!gGSCodes[curr_cheat].state || !gGSCodes[curr_cheat].count) {
2014-06-29 01:10:11 -04:00
// cheat not enabled or no codes, skip
curr_cheat++;
continue;
}
2014-06-30 03:49:02 -04:00
for (i=0; i<gGSCodes[curr_cheat].count; i++) {
2014-06-29 01:10:11 -04:00
cp = &gGSCodes[curr_cheat].gscodes[i*16 + 0];
vp = &gGSCodes[curr_cheat].gscodes[i*16 + 9];
temp[0] = cp[0];
temp[1] = cp[1];
temp[2] = 0;
type = strtol(temp, (char **)NULL, 16);
2014-06-30 03:49:02 -04:00
switch(type) {
2014-06-29 01:10:11 -04:00
case 0x80:
// write 8-bit value to (cached) ram continuously
// 80XXYYYY 00ZZ
temp[0] = cp[2];
temp[1] = cp[3];
temp[2] = 0;
xv = strtol(temp, (char **)NULL, 16);
temp[0] = cp[4];
temp[1] = cp[5];
temp[2] = cp[6];
temp[3] = cp[7];
temp[4] = 0;
yv = strtol(temp, (char **)NULL, 16);
if (yv & 0x8000) xv++; // adjust for sign extension of yv
2014-06-30 03:49:02 -04:00
if (gGSCodes[curr_cheat].mask) {
2014-06-29 01:10:11 -04:00
zv = gGSCodes[curr_cheat].value & gGSCodes[curr_cheat].mask;
}
2014-06-30 03:49:02 -04:00
else {
2014-06-29 01:10:11 -04:00
temp[0] = vp[2];
temp[1] = vp[3];
temp[2] = 0;
zv = strtol(temp, (char **)NULL, 16);
}
*codes++ = 0x3c1a8000 | xv; // lui k0,80xx
*codes++ = 0x241b0000 | zv; // li k1,00zz
*codes++ = 0xa35b0000 | yv; // sb k1,yyyy(k0)
break;
2014-06-30 03:49:02 -04:00
2014-06-29 01:10:11 -04:00
case 0x81:
// write 16-bit value to (cached) ram continuously
// 81XXYYYY 00ZZ
temp[0] = cp[2];
temp[1] = cp[3];
temp[2] = 0;
xv = strtol(temp, (char **)NULL, 16);
temp[0] = cp[4];
temp[1] = cp[5];
temp[2] = cp[6];
temp[3] = cp[7];
temp[4] = 0;
yv = strtol(temp, (char **)NULL, 16);
if (yv & 0x8000) xv++; // adjust for sign extension of yv
2014-06-30 03:49:02 -04:00
if (gGSCodes[curr_cheat].mask) {
2014-06-29 01:10:11 -04:00
zv = gGSCodes[curr_cheat].value & gGSCodes[curr_cheat].mask;
}
else
{
temp[0] = vp[0];
temp[1] = vp[1];
temp[2] = vp[2];
temp[3] = vp[3];
temp[4] = 0;
zv = strtol(temp, (char **)NULL, 16);
}
*codes++ = 0x3c1a8000 | xv; // lui k0,80xx
*codes++ = 0x241b0000 | zv; // li k1,zzzz
*codes++ = 0xa75b0000 | yv; // sh k1,yyyy(k0)
break;
2014-06-30 03:49:02 -04:00
2014-06-29 01:10:11 -04:00
case 0x88:
// write 8-bit value to (cached) ram on GS button pressed - unimplemented
// 88XXYYYY 00ZZ
break;
2014-06-30 03:49:02 -04:00
2014-06-29 01:10:11 -04:00
case 0x89:
// write 16-bit value to (cached) ram on GS button pressed - unimplemented
// 89XXYYYY ZZZZ
break;
2014-06-30 03:49:02 -04:00
2014-06-29 01:10:11 -04:00
case 0xA0:
// write 8-bit value to (uncached) ram continuously
// A0XXYYYY 00ZZ
temp[0] = cp[2];
temp[1] = cp[3];
temp[2] = 0;