summaryrefslogtreecommitdiff
path: root/arm9/lib
diff options
context:
space:
mode:
Diffstat (limited to 'arm9/lib')
-rw-r--r--arm9/lib/include/CTRDG_backup.h41
-rw-r--r--arm9/lib/include/CTRDG_flash.h25
-rw-r--r--arm9/lib/include/CTRDG_task.h25
-rw-r--r--arm9/lib/include/MI_exMemory.h18
-rw-r--r--arm9/lib/src/CTRDG_backup.c94
5 files changed, 203 insertions, 0 deletions
diff --git a/arm9/lib/include/CTRDG_backup.h b/arm9/lib/include/CTRDG_backup.h
new file mode 100644
index 00000000..d248b7a1
--- /dev/null
+++ b/arm9/lib/include/CTRDG_backup.h
@@ -0,0 +1,41 @@
+#ifndef POKEDIAMOND_CTRDG_BACKUP_H
+#define POKEDIAMOND_CTRDG_BACKUP_H
+
+#include "nitro/types.h"
+#include "CTRDG_flash.h"
+#include "CTRDG_task.h"
+
+typedef struct CTRDGiFlashTypePlusTag
+{
+ u16 (*CTRDGi_WriteAgbFlashSector)(u16 secNo, u8 *src);
+ u16 (*CTRDGi_EraseAgbFlashChip)(void);
+ u16 (*CTRDGi_EraseAgbFlashSector)(u16 secNo);
+ void (*CTRDGi_WriteAgbFlashSectorAsync)(u16 secNo, u8 *src, CTRDG_TASK_FUNC callback);
+ void (*CTRDGi_EraseAgbFlashChipAsync)(CTRDG_TASK_FUNC callback);
+ void (*CTRDGi_EraseAgbFlashSectorAsync)(u16 secNo, CTRDG_TASK_FUNC callback);
+ u16 (*CTRDGi_PollingSR)(u16 phase, u8 *adr, u16 lastData);
+ const u16 (*maxtime);
+ CTRDGFlashType type;
+} CTRDGiFlashTypePlus;
+
+typedef enum
+{
+ CTRDG_BACKUP_TYPE_FLASH_512K,
+ CTRDG_BACKUP_TYPE_FLASH_1M,
+ CTRDG_BACKUP_TYPE_SRAM
+} CTRDGBackupType;
+
+extern const u16 (*ctrdgi_fl_maxtime);
+
+extern u16 (*CTRDGi_PollingSR)(u16 phase, u8 *adr, u16 lastData);
+extern const CTRDGFlashType *AgbFlash;
+extern u16 (*CTRDGi_WriteAgbFlashSector)(u16 secNo, u8 *src);
+extern u16 (*CTRDGi_EraseAgbFlashChip)(void);
+extern u16 (*CTRDGi_EraseAgbFlashSector)(u16 secNo);
+extern void (*CTRDGi_WriteAgbFlashSectorAsync)(u16 secNo, u8 *src, CTRDG_TASK_FUNC callback);
+extern void (*CTRDGi_EraseAgbFlashChipAsync)(CTRDG_TASK_FUNC callback);
+extern void (*CTRDGi_EraseAgbFlashSectorAsync)(u16 secNo, CTRDG_TASK_FUNC callback);
+
+u16 CTRDG_IdentifyAgbBackup(CTRDGBackupType type);
+
+#endif //POKEDIAMOND_CTRDG_BACKUP_H
diff --git a/arm9/lib/include/CTRDG_flash.h b/arm9/lib/include/CTRDG_flash.h
new file mode 100644
index 00000000..9a4aff41
--- /dev/null
+++ b/arm9/lib/include/CTRDG_flash.h
@@ -0,0 +1,25 @@
+#ifndef POKEDIAMOND_CTRDG_FLASH_H
+#define POKEDIAMOND_CTRDG_FLASH_H
+
+#include "nitro/types.h"
+#include "MI_exMemory.h"
+
+typedef struct CTRDGiFlashSectorTag
+{
+ u32 size;
+ u16 shift;
+ u16 count;
+ u16 top;
+ u8 reserved[2];
+} CTRDGiFlashSector;
+
+typedef struct CTRDGFlashTypeTag
+{
+ u32 romSize;
+ CTRDGiFlashSector sector;
+ MICartridgeRamCycle agbWait[2];
+ u16 makerID;
+ u16 deviceID;
+} CTRDGFlashType;
+
+#endif //POKEDIAMOND_CTRDG_FLASH_H
diff --git a/arm9/lib/include/CTRDG_task.h b/arm9/lib/include/CTRDG_task.h
new file mode 100644
index 00000000..2cf7a233
--- /dev/null
+++ b/arm9/lib/include/CTRDG_task.h
@@ -0,0 +1,25 @@
+#ifndef POKEDIAMOND_CTRDG_TASK_H
+#define POKEDIAMOND_CTRDG_TASK_H
+
+#include "nitro/types.h"
+
+struct CTRDGTaskInfo_tag;
+
+typedef u32 (*CTRDG_TASK_FUNC) (struct CTRDGTaskInfo_tag *);
+
+typedef struct CTRDGTaskInfo_tag
+{
+ CTRDG_TASK_FUNC task;
+ CTRDG_TASK_FUNC callback;
+ u32 result;
+ u8 *data;
+ u8 *adr;
+ u32 offset;
+ u32 size;
+ u8 *dst;
+ u16 sec_num;
+ u8 busy;
+ u8 param[1];
+} CTRDGTaskInfo;
+
+#endif //POKEDIAMOND_CTRDG_TASK_H
diff --git a/arm9/lib/include/MI_exMemory.h b/arm9/lib/include/MI_exMemory.h
index df46b008..aa90a73c 100644
--- a/arm9/lib/include/MI_exMemory.h
+++ b/arm9/lib/include/MI_exMemory.h
@@ -22,6 +22,14 @@ typedef enum
MI_CTRDG_ROMCYCLE2_4 = 1
} MICartridgeRomCycle2nd;
+typedef enum
+{
+ MI_CTRDG_RAMCYCLE_10 = 0,
+ MI_CTRDG_RAMCYCLE_8 = 1,
+ MI_CTRDG_RAMCYCLE_6 = 2,
+ MI_CTRDG_RAMCYCLE_18 = 3
+} MICartridgeRamCycle;
+
static inline void MIi_SetCardProcessor(MIProcessor proc)
{
reg_MI_EXMEMCNT =
@@ -54,4 +62,14 @@ static inline void MI_SetCartridgeRomCycle2nd(MICartridgeRomCycle2nd c2)
reg_MI_EXMEMCNT = (u16)((reg_MI_EXMEMCNT & ~0x10) | (c2 << 4));
}
+static inline void MI_SetCartridgeRamCycle(MICartridgeRamCycle c)
+{
+ reg_MI_EXMEMCNT = (u16)((reg_MI_EXMEMCNT & ~3) | (c << 0));
+}
+
+static inline MICartridgeRamCycle MI_GetCartridgeRamCycle(void)
+{
+ return (MICartridgeRamCycle)((reg_MI_EXMEMCNT & 3) >> 0);
+}
+
#endif //POKEDIAMOND_MI_EXMEMORY_H
diff --git a/arm9/lib/src/CTRDG_backup.c b/arm9/lib/src/CTRDG_backup.c
new file mode 100644
index 00000000..dc1a3f4b
--- /dev/null
+++ b/arm9/lib/src/CTRDG_backup.c
@@ -0,0 +1,94 @@
+#include "CTRDG_backup.h"
+#include "function_target.h"
+#include "OS_spinLock.h"
+
+extern u16 ctrdgi_flash_lock_id;
+extern u16 ctrdgi_sram_lock_id;
+
+extern u16 CTRDGi_ReadFlashID(void);
+
+extern const CTRDGiFlashTypePlus defaultFlash1M;
+extern const CTRDGiFlashTypePlus MX29L010;
+extern const CTRDGiFlashTypePlus LE26FV10N1TS_10;
+
+extern const CTRDGiFlashTypePlus defaultFlash512;
+extern const CTRDGiFlashTypePlus LE39FW512;
+extern const CTRDGiFlashTypePlus AT29LV512_lib;
+extern const CTRDGiFlashTypePlus MN63F805MNP;
+
+static const CTRDGiFlashTypePlus *const flash1M_list[] = {
+ &MX29L010,
+ &LE26FV10N1TS_10,
+ &defaultFlash1M
+};
+
+static const CTRDGiFlashTypePlus *const flash512_list[] = {
+ &LE39FW512,
+ &AT29LV512_lib,
+ &MN63F805MNP,
+ &defaultFlash512
+};
+
+static const u16 readidtime[] = {
+ 20
+};
+
+ARM_FUNC u16 CTRDG_IdentifyAgbBackup(CTRDGBackupType type)
+{
+ u16 result = 1;
+ u16 flashID;
+ const CTRDGiFlashTypePlus *const *flp;
+ MICartridgeRamCycle ram_cycle;
+
+ if (type == CTRDG_BACKUP_TYPE_FLASH_512K || type == CTRDG_BACKUP_TYPE_FLASH_1M)
+ {
+ ctrdgi_flash_lock_id = (u16)OS_GetLockID();
+
+ (void)OS_LockCartridge(ctrdgi_flash_lock_id);
+
+ ram_cycle = MI_GetCartridgeRamCycle();
+ MI_SetCartridgeRamCycle(MI_CTRDG_RAMCYCLE_18);
+
+ ctrdgi_fl_maxtime = readidtime;
+ flashID = CTRDGi_ReadFlashID();
+
+ if (type == CTRDG_BACKUP_TYPE_FLASH_512K)
+ {
+ flp = flash512_list;
+ }
+
+ if (type == CTRDG_BACKUP_TYPE_FLASH_1M)
+ {
+ flp = flash1M_list;
+ }
+
+ MI_SetCartridgeRamCycle(ram_cycle);
+ (void)OS_UnlockCartridge(ctrdgi_flash_lock_id);
+
+ result = 1;
+ while ((*flp)->type.makerID != 0)
+ {
+ if ((flashID & 0xff) == *(u16 *)&((*flp)->type.makerID))
+ {
+ result = 0;
+ break;
+ }
+ flp++;
+ }
+ CTRDGi_WriteAgbFlashSector = (*flp)->CTRDGi_WriteAgbFlashSector;
+ CTRDGi_EraseAgbFlashChip = (*flp)->CTRDGi_EraseAgbFlashChip;
+ CTRDGi_EraseAgbFlashSector = (*flp)->CTRDGi_EraseAgbFlashSector;
+ CTRDGi_WriteAgbFlashSectorAsync = (*flp)->CTRDGi_WriteAgbFlashSectorAsync;
+ CTRDGi_EraseAgbFlashChipAsync = (*flp)->CTRDGi_EraseAgbFlashChipAsync;
+ CTRDGi_EraseAgbFlashSectorAsync = (*flp)->CTRDGi_EraseAgbFlashSectorAsync;
+ CTRDGi_PollingSR = (*flp)->CTRDGi_PollingSR;
+ ctrdgi_fl_maxtime = (*flp)->maxtime;
+ AgbFlash = &(*flp)->type;
+ }
+ else if (type == CTRDG_BACKUP_TYPE_SRAM)
+ {
+ ctrdgi_sram_lock_id = (u16)OS_GetLockID();
+ result = 0;
+ }
+ return result;
+}