Skip to content

Commit

Permalink
Add external ram save capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
mateoconlechuga committed Dec 15, 2017
1 parent 3fcdb34 commit 46dba47
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 33 deletions.
12 changes: 12 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ Available actions:

You can change the password from the settings menu (Accessed using [mode]), and pressing the [sto->] button. This will prompt for a new password which will be automatically saved.

### External Backup
Cesium also offers the ability to externally back up the RAM from within the OS. These are also tied to the shortcuts:

**PLEASE DO NOT USE THESE FOR TRIVIAL ISSUES. THEY MAY CAUSE UNINTENDED WEAR ON THE FLASH CHIP, SO BE SURE TO MODERATE USEAGE.**
**THE FLASH CHIP IS ONLY DESIGNED TO SUPPORT 100,000+ ERASE CYCLES**

| Combination | Action |
|-------------|------------------------------------------------------------|
| [on]+[8] | Backup RAM from TI-OS |
| [on]+[5] | Remove latest RAM backup |
| [on]+[2] | Restore RAM from latest backup |

### Running Programs
Cesium can run programs written in ASM, C, ICE, or BASIC, either from the archive or not. It is prefered that you place programs in the archive, as it will protect them against RAM clears.
To run a program, simply press [2ND] or [Enter]. After a program is finished running, it will return to Cesium.
Expand Down
162 changes: 135 additions & 27 deletions src/routines/hooks.asm
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ GetKeyHook:
jr z,Good
cp a,skStat
jr z,Good
cp a,sk8
jr z,Good
cp a,sk5
jr z,Good
cp a,sk2
jr z,Good
ret

Good:
Expand All @@ -105,22 +111,140 @@ Good:
bit 0,(hl)
jr z,NoOnKey
cp a,skPrgm
jr z,StartCesium
jr z,RelocateHook
cp a,skStat
jr z,StartPassword
cp a,skEnter
jr z,LaunchProgram
jr z,RelocateHook
cp a,sk8
jr z,RelocateHook
cp a,sk5
jr z,RelocateHook
cp a,sk2
jr z,RelocateHook
ret

NoOnKey:
dec a
inc a
ret

LaunchProgram:
ld hl,progCurrent
call _Mov9ToOP1
jr NoOnKey
RelocateHook:
push af
ld a,$E1
ld ($E30800),a
ld a,$E9
ld ($E30801),a
pop af
ld bc,HookEnd-HookStart
ld de,Hook
push bc
call $E30800
ld bc,12
add hl,bc
pop bc
ldir
jp Hook
HookStart:
relocate(pixelshadow2)
Hook:
cp a,skPrgm
jp z,StartCesium
cp a,skStat
jp z,StartPassword
cp a,sk8
jp z,BackupRAM
cp a,sk5
jp z,ClearOldRAM
cp a,sk2
jp z,RestoreRAM
ret

ClearOldRAM:
ld hl,$D00001
xor a,a
ld (hl),a
inc hl
ld (hl),a
ld a,($3C0000)
or a,a
ret z
di
ld.sis sp,$ea1f
call.is unlocks & $ffff
ld b,0
ld de,$3C0000
call _WriteFlashByte
call.is locks & $ffff
ret

RestoreRAM:
di
ld hl,$3C0001
ld a,$A5
cp a,(hl)
ret nz
dec hl
ld a,$5a
cp a,(hl)
ret nz
ld hl,$D00002
ld a,$A5
cp a,(hl)
ret nz
dec hl
cp a,(hl)
ret nz
jp 0

BackupRAM:
call _os_ClearStatusBarLow
di
ld de,$e71c
ld.sis (drawFGColor & $ffff),de
ld.sis de,(statusBarBGColor & $ffff)
ld.sis (drawBGColor & $ffff),de
ld a,14
ld (penRow),a
ld de,2
ld.sis (penCol & $ffff), de
ld hl,savingstringhook
call _VPutS
di ; let's do some crazy flash things so that way we can save the RAM state...
ld.sis sp,$ea1f
call.is unlocks & $ffff
ld a,$3F
call EraseSectorhook ; clean out the flash sectors
ld a,$3E
call EraseSectorhook
ld a,$3D
call EraseSectorhook
ld a,$3C
call EraseSectorhook ; this is so we can store the new RAM data \o/
ld hl,$D00002
ld (hl),$A5
dec hl
ld (hl),$A5
dec hl
ld (hl),$5A ; write some magical bytes
ld de,$3C0000 ; we could just write all of ram
ld bc,$40000
call _WriteFlash
call.is locks & $ffff
call _DrawStatusBar
dec a
inc a
ret
EraseSectorhook:
ld bc,$f8
push bc
jp _EraseFlashSector
savingstringhook:
#ifdef ENGLISH
.db "Backing up...",0
#else
.db "Sauvegarde en cours...",0
#endif

#include "routines/ramhook.asm"

StartCesium:
xor a,a
Expand Down Expand Up @@ -150,31 +274,14 @@ StartCesium:
jp _NewContext

StartPassword:
ld bc,reloacted_code_password_end-reloacted_code_password_start
ld de,pixelshadow2
push bc
ld a,$E1
ld ($E30800),a
ld a,$E9
ld ($E30801),a
call $E30800
ld bc,12
add hl,bc
pop bc
ldir
jp FindSettings

reloacted_code_password_start:
relocate(pixelshadow2)
FindSettings:
ld hl,CesiumAppvarNameRelocated
call _Mov9ToOP1
call _ChkFindSym
call _ChkInRam
push af
call z,_Arc_Unarc ; archive it
pop af
jr z,FindSettings ; now lookup the settings appvar
jr z,StartPassword ; now lookup the settings appvar
ex de,hl
ld de,9
push de
Expand Down Expand Up @@ -248,4 +355,5 @@ PasswordStrRelocated:
PasswordTemp:
.dl 0
endrelocate()
reloacted_code_password_end:
HookEnd:
.echo "Hook Size:\t",HookEnd-HookStart
13 changes: 9 additions & 4 deletions src/routines/loader.asm
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
.assume adl = 1

; inputs:
; shortcutKeys - Check if shortcut keys are enabled
; prgmNamePtr - Pointer to program name
;
; flags:
; bootEnter - Was enter used to launch?
CesiumLoader:
bit bootEnter,(iy+cesiumFlags)
jr nz,SkipSave
Expand Down Expand Up @@ -130,16 +136,13 @@ SaveRAMState:
call.is lock & $ffff

ret

EraseSector:
ld bc,$0000F8 ; apparently we can't erase sectors unless we call this routine from flash... Well, I called it from flash now :) (lol, what a secuity flaw)
push bc
jp _EraseFlashSector

#include "routines/ramsave.asm"

savingstring:

#ifdef ENGLISH
.db "Backing up...",0
#else
Expand All @@ -149,3 +152,5 @@ savingstring:
StopError:
.db "Stop",0
StopErrorEnd:

#include "routines/ramsave.asm"
4 changes: 2 additions & 2 deletions src/routines/main.asm
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CESIUM_OS_BEGIN:
cp a,$aa
cp a,$aa ; executed program reload
jr z,LoadSettings

xor a,a
sbc hl,hl
ld (currSelAbs),hl
Expand Down
23 changes: 23 additions & 0 deletions src/routines/ramhook.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

.assume adl = 0

unlocks:
ld a, $8c
out0 ($24), a
ld c, 4
in0 a, (6)
or c
out0 (6), a
out0 ($28), c
ret.l
locks:
xor a, a
out0 ($28), a
in0 a, (6)
res 2, a
out0 (6), a
ld a, $88
out0 ($24), a
ret.l

.assume adl = 1

0 comments on commit 46dba47

Please sign in to comment.