Advertisement
UDXS

CE Flash Unlock Helpers

Nov 12th, 2019
617
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .def _doUnlock
  2. .def _doLock
  3. .def _doErase
  4. .def _doWrite
  5. .def _doReset
  6.  
  7. _doUnlock:
  8.     assume adl = 1
  9.     ld a, $D1
  10.     ld mb, a ;Set mbase to userMem
  11.     call.is .unlock and $FFFF ;call unlock in Z80 mode
  12.     ret
  13.     .unlock:
  14.         assume adl = 0
  15.         ld a, $FF
  16.         out0 ($24), a ;Sets the memory protection upper range's middle byte
  17.         ld c, 4 ;4 = 0x100. Unlocks protected ports (SHA256/Flash)
  18.         in0 a,(6) ;Get the value in port 6
  19.         or c ;Do the unlock without changing other bits in port 6
  20.         out0 (6), a
  21.         out0 (28), c ;Finally unlock flash. It just so happens that c (0x100) is the right value for unlocking flash.
  22.         ret.l ;Return back to ADL mode.
  23. _doLock:
  24.     assume adl = 1
  25.     ld a, $D1
  26.     ld mb, a ;Set mbase to userMem
  27.     call.is .lock and $FFFF ;call unlock in Z80 mode
  28.     ret
  29.     .lock:
  30.         assume adl = 0
  31.         in0 a, (6) ;Get value in port 6
  32.         res 2, a ;Locks protected ports. This also locks flash automatically.
  33.         out0 (6), a
  34.         ld  a, $88 ;Disables privilege
  35.         out0    ($24), a
  36.         ret.l ;Return back to ADL mode.
  37. _doErase:
  38.     assume adl = 1
  39.     ld iy, 0 ;Get 1st C arg into A
  40.     add iy, sp
  41.     ld a, (iy+3)
  42.     ld bc, $F8 ;0xF8: This is a jmp to a ret instruction
  43.     push bc
  44.     jp $2DC ;EraseFlashSector. We don't use call so we can override the return address to convince it to run
  45. _doWrite:
  46.     assume adl = 1
  47.     ld iy, 0 ;Get C args
  48.     add iy, sp
  49.     ld de, (iy+3)
  50.     ld b, (iy+6)
  51.     call $2D4 ;Write a byte
  52.     ret
  53. _doReset:
  54.     assume adl = 1
  55.     jp 0 ;Reset the calculator
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement