flash.4th 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. \ flash.4th
  2. base @ decimal
  3. : get-image-file ( b u addr -- flag )
  4. >r
  5. r/o bin open-file ?dup
  6. if
  7. ." open error = " dec. cr
  8. r> 2drop
  9. false exit
  10. then
  11. r> \ file-id buffer
  12. swap >r \ buffer
  13. flash-rom-size
  14. begin
  15. \ b u1
  16. 2dup
  17. r@ read-file ?dup \ b u1 u2 ior ior?
  18. if
  19. ." read error = " dec. cr
  20. 2drop drop
  21. r> close-file drop
  22. false exit
  23. then
  24. \ b u1 u2
  25. swap over - \ b u2 (u1-u2)
  26. >r + r> \ (b+u2) (u1-u2)
  27. dup 0=
  28. until
  29. 2drop
  30. r> close-file drop
  31. true
  32. ;
  33. : program-rom ( b u -- )
  34. cr ." Loading: " 2dup type cr
  35. here get-image-file 0=
  36. if
  37. ." Read file failed" cr
  38. exit
  39. then
  40. cr ." Serial Number: "
  41. \ save serial number
  42. here flash-serial-number-offset +
  43. flash-serial-number-length
  44. flash-serial-number-offset flash-read
  45. if
  46. flash-serial-number-length 0
  47. ?do
  48. here flash-serial-number-offset + i + c@
  49. dup bl 127 within
  50. if
  51. emit
  52. else
  53. drop
  54. then
  55. loop
  56. else
  57. ." FAIL" cr
  58. exit
  59. then
  60. cr ." Erase: "
  61. flash-write-enable
  62. if
  63. flash-chip-erase
  64. if
  65. ." Done"
  66. else
  67. ." FAIL"
  68. cr exit
  69. then
  70. else
  71. ." FAIL"
  72. cr exit
  73. then
  74. cr ." Program: "
  75. flash-rom-size 0 ?do
  76. flash-write-enable 0=
  77. if
  78. ." FAIL" cr
  79. unloop
  80. exit
  81. then
  82. here i + flash-page-size i flash-write 0=
  83. if
  84. ." FAIL" cr
  85. unloop
  86. exit
  87. then
  88. i flash-sector-size 1- and 0=
  89. if
  90. ." ."
  91. then
  92. flash-page-size +loop
  93. cr ." Verify: "
  94. flash-rom-size 0 ?do
  95. here i + flash-sector-size i flash-verify
  96. if
  97. ." ."
  98. else
  99. ." E"
  100. then
  101. flash-sector-size +loop
  102. cr
  103. ;
  104. : save-rom ( b u -- )
  105. cr ." Read ROM: "
  106. here flash-rom-size 0 flash-read
  107. if
  108. ." PASS"
  109. else
  110. ." FAIL" cr
  111. 2drop
  112. exit
  113. then
  114. cr ." Write to: " 2dup type cr
  115. w/o create-file ?dup
  116. if
  117. ." create file error = " dec. cr
  118. drop
  119. exit
  120. then
  121. >r \ save fileid
  122. here flash-rom-size r@ write-file ?dup
  123. if
  124. ." write file error = " dec. cr
  125. then
  126. r> close-file drop
  127. ;
  128. : burn ( "<spaces><filename>" -- )
  129. bl parse program-rom
  130. ;
  131. : save ( "<spaces><filename>" -- )
  132. bl parse save-rom
  133. ;
  134. base !