Advertisement
Guest User

TI-83+ Raw Flash control

a guest
Sep 8th, 2010
510
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; ==============================================================================
  2. ; Raw flash commands tester.
  3.  
  4. ; ===== Equates ================================================================
  5. .nolist
  6. #include "ti83plus.txt"
  7. .list
  8.  
  9. ;spaddr     .equ    appBackUpScreen
  10. inputerFlags    .equ    Asm_Flag1
  11. secondNibble    .equ    0
  12.  
  13. ;====== Header =================================================================
  14.     .org    $9D93
  15.     .db t2ByteTok, tAsmCmp
  16.  
  17.  
  18. ;====== Init ===================================================================
  19.     call    UnlockFlash
  20.     ;ld (spaddr), sp
  21.     pop hl
  22.     push    hl
  23.     ld  (spaddr), hl
  24.    
  25.     ld  hl, mSplash
  26.     jp  menu
  27.  
  28. ;====== Flash commands =========================================================
  29. ;------ Flash read -------------------------------------------------------------
  30. readFlashByte:
  31.     B_CALL(_ClrScrn)
  32.     B_CALL(_HomeUp)
  33.     ld  hl, readFlashByteMsg
  34.     b_call(_PutS)
  35.     call    getHexByte
  36.     out (6), a
  37.     b_call(_NewLine)
  38.     ld  hl, readFlashByteAddrMsg
  39.     b_call(_PutS)
  40.     call    getHexByte
  41.     ld  h, a
  42.     call    getHexByte
  43.     ld  l, a
  44.     push    hl
  45.     b_call(_NewLine)
  46.     ld  hl, readFlashByteOutputMsg
  47.     b_call(_PutS)
  48.     pop hl
  49.     ld  l, (hl)
  50.     ld  de, OP1
  51.     call    LToHexString
  52.     ld  hl, OP1
  53.     b_call(_PutS)
  54.     jp  waitAndReturn
  55.  
  56. ;------ Flash write ------------------------------------------------------------
  57. writeFlashByte:
  58.     B_CALL(_ClrScrn)
  59.     B_CALL(_HomeUp)
  60.     ld  hl, writeFlashByteMsg
  61.     b_call(_PutS)
  62.     call    getHexByte
  63.     ;out    (6), a
  64.     push    af
  65.     b_call(_NewLine)
  66.     ld  hl, writeFlashByteAddrMsg
  67.     b_call(_PutS)
  68.     call    getHexByte
  69.     ld  h, a
  70.     call    getHexByte
  71.     ld  l, a
  72.     push    hl
  73.     b_call(_NewLine)
  74.     ld  hl, writeFlashBytePageMsg
  75.     b_call(_PutS)
  76.     call    getHexByte
  77.     push    af
  78.     b_call(_NewLine)
  79. ; Flash program sequence
  80. ; This is being done according to how the datasheet says it should be done.
  81. ; The standard interrupt must not trigger during this sequence or the flash chip
  82. ; will not get the write command.
  83. ; At 6MHz, one clock cycle will be 167ns
  84. ; At 15MHz, one clock cycle will be 67ns
  85.     di
  86.     xor a
  87.     out (6), a
  88.     ; reset bus (not needed by the books)
  89.     ;ld a, 0F0h
  90.     ;ld (4000h), a
  91.     ; First bus cycle---unlock
  92.     ld  a, 0AAh
  93.     ld  (4AAAh), a
  94.     ; Second bus cycle---unlock
  95.     ld  a, 55h
  96.     ld  (4555h), a
  97.     ; Third bus cycle---write command
  98.     ld  a, 0A0h
  99.     ld  (4AAAh), a
  100.     ; Fourth bus cycle---program data
  101.     pop af
  102.     out (6), a
  103.     pop hl
  104.     pop af
  105.     ld  (hl), a
  106.     ; Wait for data to be good
  107. ; "During the Embedded Program Algorithm, an attempt to read the devices will
  108. ; produce the complement of the data last written to DQ7. Upon completion of the
  109. ; Embedded Program Algorithm, an attempt to read the device will produce the
  110. ; true data last written to DQ7"
  111. ; "DQ5 will indicate if the program or erase time has exceeded the specified
  112. ; limits (internal pulse count).  If this occurs, reset the device with command
  113. ; sequence."---Fujitsu documentation
  114. programWaitLoop:
  115.     ld  b, a
  116.     ld  a, 0FDh     ; This checks for the CLEAR key.
  117.     out (1), a      ; If pressed, it aborts.
  118.     in  a, (1)
  119.     cp  0BFh
  120.     jr  z, abortProgram
  121.     xor (hl)
  122.     bit 7, a
  123.     ld  a, b
  124.     jr  z, programDone
  125.     bit 5, (hl)
  126.     jr  z, programWaitLoop
  127. abortProgram:
  128.     ld  a, 0F0h
  129.     ld  (4000h), a
  130.     ld  hl, errTimeoutMsg
  131.     b_call(_PutS)
  132.     jp  waitAndReturn
  133. programDone:
  134.     ei
  135.     ld  hl, mMainMenu
  136.     jp  menu
  137.  
  138.  
  139.  
  140. ;------ Erase sector -----------------------------------------------------------
  141. eraseSector:
  142.     B_CALL(_ClrScrn)
  143.     B_CALL(_HomeUp)
  144.     ld  hl, eraseSectorMsg
  145.     b_call(_PutS)
  146.     call    getHexByte
  147.     ;out    (6), a
  148.     push    af
  149.     b_call(_NewLine)
  150.     ld  hl, erasingSectorMsg
  151.     b_call(_PutS)
  152.     ld  hl, 4000h
  153. ; Flash program sequence
  154. ; This is being done according to how the datasheet says it should be done.
  155. ; The standard interrupt must not trigger during this sequence or the flash chip
  156. ; will not get the write command.
  157. ; At 6MHz, one clock cycle will be 167ns
  158. ; At 15MHz, one clock cycle will be 67ns
  159.     di
  160.     xor a
  161.     out (6), a
  162.     ; reset bus (not needed by books)
  163.     ;ld a, 0F0h
  164.     ;ld (4000h), a
  165.     ; First bus cycle---unlock
  166.     ld  a, 0AAh
  167.     ld  (4AAAh), a
  168.     ; Second bus cycle---unlock
  169.     ld  a, 55h
  170.     ld  (4555h), a
  171.     ; Third bus cycle---write command
  172.     ld  a, 080h
  173.     ld  (4AAAh), a
  174.     ; Fourth bus cycle---unlock (again)
  175.     ld  a, 0AAh
  176.     ld  (4AAAh), a
  177.     ; Fifth bus cycle---unlock (again)
  178.     ld  a, 55h
  179.     ld  (4555h), a
  180.     ; Sixth bus cycle---select sector
  181.     pop af
  182.     out (6), a
  183.     ld  a, 30h
  184.     ld  (hl), a
  185.     ; Wait for data to be good
  186. ; "During the Embedded Erase Algorithm, an attempt to read the device will
  187. ; produce a '0' at the DQ7 output. Upon completion of the Embedded Erase
  188. ; Algorithm an attempt to read the device will produce a '1' at the DQ7 output."
  189. ; "DQ5 will indicate if the program or erase time has exceeded the specified
  190. ; limits (internal pulse count).  If this occurs, reset the device with command
  191. ; sequence."---Fujitsu documentation
  192. eraseWaitLoop:
  193.     ld  a, 0FDh
  194.     out (1), a
  195.     in  a, (1)
  196.     cp  0BFh
  197.     jr  z, abortErase
  198.     cp  0FDh
  199.     jr  nz, eraseWaitLoopCont
  200.     ld  a, 0B0h
  201.     ld  (4000h), a
  202.     ld  a, "+"
  203.     b_call(_PutMap)
  204.     ld  a, 30h
  205.     ld  (4000h), a
  206. eraseWaitLoopCont:
  207.     ld  a, (4000h)
  208.     bit 7, a
  209.     jr  nz, eraseDone
  210.     bit 5, a
  211.     jr  z, eraseWaitLoop
  212. abortErase:
  213.     ld  a, 0F0h
  214.     ld  (4000h), a
  215.     ld  hl, errTimeoutMsg
  216.     b_call(_PutS)
  217.     jp  waitAndReturn
  218. eraseDone:
  219.     ei
  220.     ld  hl, mMainMenu
  221.     jp  menu
  222.  
  223.  
  224.  
  225. ;====== Autoselect commands ====================================================
  226. ; "The system must write the reset command to exit the
  227. ; autoselect mode and return to reading array data."---AMD data sheet
  228. ; AMD also says you uses different autoselect codes for byte and word mode.
  229. ;------ Manufacture's Code -----------------------------------------------------
  230. getManufacturesCode:
  231.     B_CALL(_ClrScrn)
  232.     B_CALL(_HomeUp)
  233.     ld  hl, manCodeMsg
  234.     b_call(_PutS)
  235. ; Autoselect sequence
  236. ; This code tries it TI's way.
  237.     di
  238.     ; First bus cycle---unlock
  239.     ld  a, 2
  240.     out (6), a
  241.     ld  a, 0AAh
  242.     ld  (6AAAh), a
  243.     ; Second bus cycle---unlock
  244.     ld  a, 1
  245.     out (6), a
  246.     ld  a, 55h
  247.     ld  (5555h), a
  248.     ; Third bus cycle---write command
  249.     ld  a, 2
  250.     out (6), a
  251.     ld  a, 090h
  252.     ld  (6AAAh), a
  253.     ; Read autoselect code
  254.     ld  a, 1
  255.     out (6), a
  256.     ld  l, (5000h)
  257.     ld  a, 0F0h
  258.     ld  (4000h), a
  259.     push    hl
  260.     pop hl
  261.     ei
  262.     ld  de, OP1
  263.     call    LToHexString
  264.     ld  hl, OP1
  265.     b_call(_PutS)
  266.     jp  waitAndReturn
  267.  
  268.  
  269. ;------ Device Code ------------------------------------------------------------   
  270. getDeviceCode:
  271.     B_CALL(_ClrScrn)
  272.     B_CALL(_HomeUp)
  273.     ld  hl, deviceCodeMsg
  274.     b_call(_PutS)
  275. ; Autoselect sequence
  276. ; This code tries it my way.
  277.     di
  278.     xor a
  279.     out (6), a
  280.     ; First bus cycle---unlock
  281. ;   ld  a, 0AAh
  282. ;   ld  (4AAAh), a
  283.     ; Second bus cycle---unlock
  284. ;   ld  a, 55h
  285. ;   ld  (4555h), a
  286.     ; Third bus cycle---write command
  287. ;   ld  a, 090h
  288. ;   ld  (4AAAh), a
  289.     ; Read autoselect code
  290.     ld  l, (4047h) ;4001h
  291.     ld  a, 0F0h
  292.     ld  (4000h), a
  293.     push    hl
  294.     pop hl
  295.     ei
  296.     ld  de, OP1
  297.     call    LToHexString
  298.     ld  hl, OP1
  299.     b_call(_PutS)
  300.     jp  waitAndReturn
  301.  
  302.  
  303. ;------ Sector Protection ------------------------------------------------------
  304. getSectorProtection:
  305.     B_CALL(_ClrScrn)
  306.     B_CALL(_HomeUp)
  307.     ld  hl, sectorProtCodeMsg
  308.     b_call(_PutS)
  309.     call    getHexByte
  310.     push    af
  311.     ld  hl, sectorProtOutputMsg
  312.     b_call(_PutS)
  313. ; Autoselect sequence  
  314.     di
  315.     xor a
  316.     out (6), a
  317.     ; First bus cycle---unlock
  318.     ld  a, 0AAh
  319.     ld  (4AAAh), a
  320.     ; Second bus cycle---unlock
  321.     ld  a, 55h
  322.     ld  (4555h), a
  323.     ; Third bus cycle---write command
  324.     ld  a, 090h
  325.     ld  (4AAAh), a
  326.     ; Read autoselect code
  327.     pop af
  328.     out (6), a
  329.     ld  l, (4004h) ;4002h
  330.     ld  a, 0F0h
  331.     ld  (4000h), a
  332.     push    hl
  333.     pop hl
  334.     ei
  335.     ld  de, OP1
  336.     call    LToHexString
  337.     ld  hl, OP1
  338.     jp  waitAndReturn
  339.    
  340.  
  341. ;====== ========================================================================
  342. waitAndReturn:
  343. ;   im  1
  344. ;   ei
  345. ;   ld  hl, 8000h
  346. ;   ld  de, 8000h
  347. ;   ld  bc, 8000h
  348. ;   ldir    ; waste clock cycles
  349.     b_call(_GetKey)
  350.     ld  hl, mMainMenu
  351.     jp  menu
  352.  
  353.  
  354.  
  355. ;====== Term ===================================================================
  356. quit:
  357.     b_call(_NewLine)
  358. quitNoNewLine:
  359.     B_CALL(_FlashWriteDisable)
  360.     set indicRun, (iy+indicFlags)
  361.     res DonePrgm, (iy+DoneFlags)
  362.     res OnInterrupt, (iy+OnFlags)
  363.     ;ld sp, (spaddr)
  364.     im  1
  365.     ei
  366.     ld  hl, (spaddr)
  367.     jp  (hl)
  368.     ;ld hl, (spaddr)
  369.     ;ret
  370.  
  371.  
  372.    
  373. ;====== Routines ===============================================================
  374.  
  375. ldHLindirect:
  376.     ld  a, (hl)
  377.     inc hl
  378.     ld  h, (hl)
  379.     ld  l, a   
  380.     ret
  381.    
  382. ;------ Menu -------------------------------------------------------------------
  383. menu:
  384. ; Simple menu routine.
  385. ; HL points to a data structure that describes the menu.
  386. ; The first item in the menu is a zero-termed string to be displayed.
  387. ; After that, there is a list of acceptable keys and jump addresses.
  388. ; This is a JUMP not a CALL!
  389.     im  1
  390.     ei
  391.     push    hl
  392.     ;set    appAutoScroll, (iy+appFlags)
  393.     push    hl
  394.     B_CALL(_ClrScrn)
  395.     ld  hl, 0
  396.     ld  (curRow), hl
  397.     pop hl
  398.     b_call(_PutS)
  399.     push    hl
  400. menuKeyLoop:
  401.     b_call(_GetKey);_GetCSC)
  402.     pop hl
  403.     push    hl
  404.     ld  b, a
  405.     ld  a, (hl) ; sanity check
  406.     or  a ;
  407.     jp  z, quit
  408. mkla:   ld  a, (hl)
  409.     or  a
  410.     jr  z, menuKeyLoop
  411.     cp  b
  412.     jr  z, mklb
  413.     inc hl
  414.     inc hl
  415.     inc hl
  416.     jr  mkla
  417. mklb:   pop de
  418.     inc hl
  419.     call    ldHLindirect
  420.     push    af
  421.     ld  a, (hl)
  422.     or  a
  423.     jr  z, mklc
  424.     pop af
  425.     jp  (hl)
  426. mklc:   pop af
  427.     inc hl
  428.     jp  menu
  429.  
  430. ;------ ------------------------------------------------------------------------
  431. ; These routines are by Milos "baze" Bazelides, from the web page
  432. ; http://baze.au.com/misc/z80bits.html
  433. HLToHexString:
  434. ;Input: HL = number to convert, DE = location of ASCII string
  435. ;Output: ASCII string at (DE)
  436.     ld  a,h
  437.     call    LNum1
  438.     ld  a,h
  439.     call    LNum2
  440. LToHexString:   ld  a,l
  441.     call    LNum1
  442.     ld  a,l
  443.     jr  LNum2
  444.  
  445. LNum1rra
  446.     rra
  447.     rra
  448.     rra
  449. LNum2:  or  0F0h
  450.     daa
  451.     add a,0A0h
  452.     adc a,40h
  453.  
  454.     ld  (de),a
  455.     inc de
  456.     xor a
  457.     ld  (de), a
  458.     ret
  459.  
  460.  
  461. ;------ Hex input --------------------------------------------------------------
  462. getHexByte:
  463. ; Do not attempt to get a byte across a line break.
  464. ; Gets byte at current location.  Returns byte in A
  465. ; Registers:
  466. ; b: returns nibble from getNibble
  467. ; d: byte
  468.     push    bc
  469.     push    de
  470.     push    hl
  471.     B_CALL(_CursorOn)
  472. getHexByteFirstNibble:
  473.     res secondNibble, (iy+inputerFlags)
  474.     call    getNibble
  475.     sla b
  476.     sla b
  477.     sla b
  478.     sla b
  479.     push    bc  ;   ld  d, b
  480.     set secondNibble, (iy+inputerFlags)
  481.     call    getNibble
  482.     ld  a, b
  483.     pop bc
  484.     add a, b    ;add    a, d
  485.     push    af
  486.     B_CALL(_CursorOff)
  487.     pop af
  488.     pop hl
  489.     pop de
  490.     pop bc
  491.     ret
  492. getNibble:
  493. ; Gets a single nibble.  Set secondNibble in IY+inputerFlags to allow backspace.
  494. ; Returns nibble in B.
  495.     B_CALL(_GetCSC)
  496.     ld  b, 0
  497.     cp  sk0
  498.     jr  z, nibble0
  499.     cp  sk1
  500.     jr  z, nibble1
  501.     cp  sk2
  502.     jr  z, nibble2
  503.     cp  sk3
  504.     jr  z, nibble3
  505.     cp  sk4
  506.     jr  z, nibble4
  507.     cp  sk5
  508.     jr  z, nibble5
  509.     cp  sk6
  510.     jr  z, nibble6
  511.     cp  sk7
  512.     jr  z, nibble7
  513.     cp  sk8
  514.     jr  z, nibble8
  515.     cp  sk9
  516.     jr  z, nibble9
  517.     cp  skMath
  518.     jr  z, nibbleA
  519.     cp  skMatrix
  520.     jr  z, nibbleB
  521.     cp  skPrgm
  522.     jr  z, nibbleC
  523.     cp  skRecip
  524.     jr  z, nibbleD
  525.     cp  skSin
  526.     jr  z, nibbleE
  527.     cp  skCos
  528.     jr  z, nibbleF
  529.     cp  skDel
  530.     jr  z, getNibbleBackspace
  531.     cp  skLeft
  532.     jr  z, getNibbleBackspace
  533. ;   cp  skClear
  534. ;   jp  z, quit
  535. ;   cp  skMode
  536. ;   jp  z, quit
  537.     jr  getNibble
  538. nibbleF: inc    b
  539. nibbleE: inc    b
  540. nibbleD: inc    b
  541. nibbleC: inc    b
  542. nibbleB: inc    b
  543. nibbleA: inc    b
  544. nibble9: inc    b
  545. nibble8: inc    b
  546. nibble7: inc    b
  547. nibble6: inc    b
  548. nibble5: inc    b
  549. nibble4: inc    b
  550. nibble3: inc    b
  551. nibble2: inc    b
  552. nibble1: inc    b
  553. nibble0:
  554.     ld  a, b
  555.     ld  c, '0'
  556.     ld  a, 9
  557.     cp  b
  558.     jr  nc, number
  559.     ld  c, 'A'-0Ah
  560. number:
  561.     ld  a, b
  562.     add a, c
  563.     B_CALL(_PutC)
  564.     ret
  565. getNibbleBackspace:
  566.     bit secondNibble, (iy+inputerFlags)
  567.     jr  z, getNibble
  568.     ld  a, (curCol)
  569.     dec a
  570.     ld  (curCol), a
  571.     ld  a, ' '
  572.     B_CALL(_PutMap)
  573.     pop bc              ; stack
  574.     jp  getHexByteFirstNibble
  575.  
  576. ;------ ------------------------------------------------------------------------
  577.  
  578. #include "unlockflash.asm"
  579.  
  580. ;====== Data ===================================================================
  581. ;        1234567890123456
  582. mSplash:
  583.     .db "Raw Flash Tools "
  584.     .db "26 Auguest 2009 "
  585.     .db "Flash is unlockd"
  586.     .db "Press enter. . ."
  587.     .db 0
  588.     .db kEnter
  589.     .dw mMainMenu-1
  590.     .db 0
  591. mMainMenu:
  592.     .db "Raw Flash Tools:"
  593.     .db "1. Read         "
  594.     .db "2. Program      "
  595.     .db "3. Sector erase "
  596.     .db "4. Autoselect   "
  597.     .db "5. Quit"
  598.     .db 0
  599.     .db k1
  600.     .dw readFlashByte
  601.     .db k2
  602.     .dw writeFlashByte
  603.     .db k3
  604.     .dw eraseSector
  605.     .db k4
  606.     .dw mAutoselect-1
  607.     .db k5
  608.     .dw quit
  609.     .db 0
  610. mAutoselect:
  611.     .db "Autoselect:     "
  612.     .db "1. Manufact.Code"
  613.     .db "2. Device Code  "
  614.     .db "3. Sector prot. "
  615.     .db "4. Return"
  616.     .db 0
  617.     .db k1
  618.     .dw getManufacturesCode
  619.     .db k2
  620.     .dw getDeviceCode
  621.     .db k3
  622.     .dw getSectorProtection
  623.     .db k4
  624.     .dw mMainMenu-1
  625.     .db 0
  626. readFlashByteMsg:
  627. ;        1234567890123456
  628.     .db "Read flash byte:"
  629.     .db "Enter page: "
  630.     .db 0
  631. writeFlashByteAddrMsg:
  632. readFlashByteAddrMsg:
  633.     .db "Enter addr: ", 0
  634. readFlashByteOutputMsg:
  635.     .db "Result: ", 0
  636. writeFlashByteMsg:
  637.     .db "Write flash byte"
  638.     .db "Enter data: "
  639.     .db 0
  640. writeFlashBytePageMsg:
  641.     .db "Enter page: ", 0
  642. eraseSectorMsg:
  643.     .db "Erase sector:   "
  644.     .db "Enter SA:"
  645.     .db 0
  646. erasingSectorMsg:
  647.     .db "Erasing sector! "
  648.     .db "Press ENTER to  "
  649.     .db "test erase      "
  650.     .db "suspend, or     "
  651.     .db "CLEAR to abort."
  652.     .db 0
  653. manCodeMsg:
  654.     .db "Manufacture's   "
  655.     .db "code: "
  656.     .db 0
  657. deviceCodeMsg:
  658.     .db "Device code: ", 0
  659. sectorProtCodeMsg:
  660.     .db "Sector Prot Test"
  661.     .db "Enter sector: "
  662.     .db 0
  663. sectorProtOutputMsg:
  664.     .db "Result: ", 0
  665. errTimeoutMsg:
  666. ;        1234567890123456
  667.     .db "The operation   "
  668.     .db "has failed."
  669.     .db 0
  670. ;panicText:
  671. ;   .db "Panic:", 0
  672. spaddr:
  673.     .dw 0
  674. .end
  675. .end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement