8051dumper.asm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;; 8051dumper.asm: Dump internal ROM contents of 8051 to serial port.
  3. ;;
  4. ;;
  5. PROCESSOR 8051
  6. INCLUDE "SFR.asm"
  7. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  8. ;; GPIOs
  9. ;;
  10. OUT_EA ALIAS "P1.0" ; Inverted and connected to EAn pin
  11. OUT_REDLEDn ALIAS "P1.1" ; Red LED in RESET button
  12. OUT_GRNLEDn ALIAS "P1.2" ; Green LED in START button
  13. IN_GRNBTNn ALIAS "P1.3" ; Green START button
  14. IN_R32Kn ALIAS "P1.4" ; Dump size knob: 8058 (32k)
  15. IN_R16Kn ALIAS "P1.5" ; Dump size knob: 8054 (16k)
  16. IN_R8Kn ALIAS "P1.6" ; Dump size knob: 8052 (8k)
  17. IN_R4Kn ALIAS "P1.7" ; Dump size knob: 8051 (4k)
  18. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  19. ;; Internal RAM (uninitialized data segment)
  20. ;;
  21. SEGU "IRAM"
  22. ; 0x00..0x1F contains the four register banks.
  23. ; 0x20..0x2F is the bit addressable area.
  24. ; 0x30..0x7F is the scratch pad area.
  25. ORG 0x0030
  26. IROMSIZE: DS 1 ; High byte of internal ROM size
  27. CKSUM: DS 1 ; Checksum accumulator for line
  28. HINYBBLE: DS 1 ; Temp buffer for HEXBYTE
  29. LONYBBLE: DS 1 ; Temp buffer for HEXBYTE
  30. ; Intel Hex line buffer for 16 byte record
  31. BUF_START: DS 1 ; Start code ":"
  32. BUF_BYTECOUNT: DS 2 ; Byte count "10"
  33. BUF_ADDR: DS 4 ; Start address of record
  34. BUF_RECTYPE: DS 1 ; Record type
  35. BUF_DATA: DS 32 ; Record data
  36. BUF_CKSUM: DS 2 ; Checksum
  37. BUF_EOL: DS 3 ; CRLF and terminating NUL
  38. ; Rest of RAM is stack. Initial stack pointer should be 1 byte before
  39. ; beginning of stack because PUSH operations pre-increment the stack pointer.
  40. STACK: DS 0x0080-$
  41. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  42. ;; Code segment starting at base of external program memory EEPROM
  43. ;;
  44. SEG "code"
  45. ; Reset vector (3 bytes)
  46. ORG 0x0000
  47. RESET: LJMP MAIN
  48. ; External Request 0 Interrupt Service Routine (8 bytes)
  49. ORG 0x0003
  50. ER0_ISR: LJMP ERROR
  51. ; Internal Timer/Counter 0 Interrupt Service Routine (8 bytes)
  52. ORG 0x000B
  53. ITC0_ISR: LJMP ERROR
  54. ; External Request 1 Interrupt Service Routine (8 bytes)
  55. ORG 0x0013
  56. ER1_ISR: LJMP ERROR
  57. ; Internal Timer/Counter 1 Interrupt Service Routine (8 bytes)
  58. ORG 0x001B
  59. ITC1_ISR: LJMP ERROR
  60. ; Internal Serial Port Interrupt Service Routine (8 bytes)
  61. ORG 0x0023
  62. ISP_ISR: LJMP ERROR
  63. ; 8052 TF2 and EXF2 Interrupt Service Routine (8 bytes)
  64. ORG 0x002B
  65. TF2_ISR: LJMP ERROR
  66. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  67. ;; Following code/data will be accessed in a mirror image of the 32k EEPROM
  68. ;; at 0x8000. We will raise the EAn pin with a GPIO in order to overlay
  69. ;; internal ROM at address 0x0000. We will not use any interrupts because
  70. ;; interrupt vectors will contain unpredictable code from internal ROM.
  71. ;;
  72. ORG 0x0032
  73. RORG 0x8032
  74. IDSTR: DB "8051dumper v1.0 by NF6X", 0x0D, 0x0A, 0x00
  75. KNOBSTR: DB "ERROR: Could not read ROM size knob!", 0x0D, 0x0A, 0x00
  76. ENDSTR: DB ":00000001FF", 0x0D, 0x0A, 0x00
  77. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  78. ;; Main entry point
  79. ;;
  80. MAIN:
  81. MOV IE, #0x00 ; Disable all interrupts
  82. MOV P1, #0xFF ; GPIOs all input/high
  83. MOV SP, #STACK-1 ; Initialize stack pointer
  84. CLR OUT_EA ; Select INTERNAL ROM at 0x0000
  85. ; Configure serial port for Mode 1, 9600 8n1
  86. ; SMOD = 0 ==> K = 1
  87. ; Oscillator frequency = 11.0592 MHz
  88. ; TH1 = 256 - (K * Fosc)/(32 * 12 * Baud)
  89. ; TH1 = 256 - (1 * 11059200)/(32 * 12 * 9600) = 253 = 0xFD
  90. MOV PCON, #0x00 ; SMOD = 0
  91. MOV SCON, #0x50 ; Mode 1
  92. MOV TMOD, #0x21 ; T1: Mode 2, T0: Mode 1
  93. MOV TH1, #0xFD ; 9600 baud
  94. MOV TL1, #0xFD ; 9600 baud
  95. SETB TCON_TR1 ; Enable timer
  96. ; Send ID string
  97. MOV DPTR, #IDSTR
  98. ACALL SENDROM
  99. ; Blink green LED until green START button pressed.
  100. ; Flash LED 2x per second at 20% duty cycle, checking
  101. ; button every 100ms.
  102. WAITSTART: CLR OUT_GRNLEDn ; On for 100ms
  103. ACALL DELAY100ms
  104. JNB IN_GRNBTNn, READKNOB ; Button pressed?
  105. SETB OUT_GRNLEDn ; Off for 400ms
  106. ACALL DELAY100ms
  107. JNB IN_GRNBTNn, READKNOB ; Button pressed?
  108. ACALL DELAY100ms
  109. JNB IN_GRNBTNn, READKNOB ; Button pressed?
  110. ACALL DELAY100ms
  111. JNB IN_GRNBTNn, READKNOB ; Button pressed?
  112. ACALL DELAY100ms
  113. JNB IN_GRNBTNn, READKNOB ; Button pressed?
  114. SJMP WAITSTART
  115. ; Read dump size knob and begin dumping code.
  116. READKNOB: SETB OUT_GRNLEDn ; Green LED off
  117. .TST32K: JB IN_R32Kn, .TST16K ; Knob set to 32k?
  118. MOV IROMSIZE, #0x80 ; Yes, 32k
  119. SJMP INITBUF
  120. .TST16K: JB IN_R16Kn, .TST8K ; Knob set to 16k?
  121. MOV IROMSIZE, #0x40 ; Yes, 16k
  122. SJMP INITBUF
  123. .TST8K: JB IN_R8Kn, .TST4K ; Knob set to 8k?
  124. MOV IROMSIZE, #0x20 ; Yes, 8k
  125. SJMP INITBUF
  126. .TST4K: JB IN_R4Kn, .TSTFAIL ; Knob set to 4k?
  127. MOV IROMSIZE, #0x10 ; Yes, 4k
  128. SJMP INITBUF
  129. .TSTFAIL: MOV DPTR, #KNOBSTR ; Cannot read knob!
  130. ACALL SENDROM
  131. LJMP ERROR
  132. ; Initialize the line buffer and ROM data pointer
  133. INITBUF: MOV BUF_START, #':'
  134. MOV BUF_BYTECOUNT, #'1'
  135. MOV BUF_BYTECOUNT+1, #'0'
  136. MOV BUF_EOL, #0x0D
  137. MOV BUF_EOL+1, #0x0A
  138. MOV BUF_EOL+2, #0x00
  139. MOV DPTR, #0000
  140. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  141. ;; Dump one Intel Hex record of 16 bytes at a time until done.
  142. ;; Toggle red LED for each line to show activity.
  143. ;;
  144. DUMPLOOP: MOV A, DPH ; Has DPTR hit end of IROM?
  145. CJNE A, IROMSIZE, DUMPRECORD
  146. SJMP EOF ; Finished with IROM
  147. ; Dump one 16-byte record
  148. DUMPRECORD:
  149. CPL OUT_REDLEDn
  150. MOV CKSUM, #0x10 ; Init checksum w/ record length
  151. MOV R0, #BUF_ADDR ; Point to address field
  152. MOV A, DPH ; High address byte
  153. ACALL HEXBYTE
  154. MOV A, DPL ; Low address byte
  155. ACALL HEXBYTE
  156. CLR A ; Record type
  157. ACALL HEXBYTE
  158. DUMPBYTE: CLR A ; Read byte from IROM
  159. MOVC A, @A+DPTR
  160. ACALL HEXBYTE ; Add it to line buffer
  161. MOV A, DPL ; End of record?
  162. ANL A, #0x0F
  163. CLR C
  164. SUBB A, #0x0F
  165. JZ EOR ; Yes, at end of 16-byte record
  166. INC DPTR ; No, increment IROM pointer
  167. SJMP DUMPBYTE ; Add next byte
  168. EOR: MOV A, CKSUM ; Add checksum to record
  169. CPL A
  170. ADD A, #1
  171. ACALL HEXBYTE
  172. ACALL SENDRECORD ; Send out this record
  173. INC DPTR
  174. SJMP DUMPLOOP
  175. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  176. ;; Send out end of file record, turn green LED on and red LED off,
  177. ;; and loop forever.
  178. ;;
  179. EOF: MOV DPTR, #ENDSTR
  180. ACALL SENDROM
  181. CLR OUT_GRNLEDn ; Green LED on
  182. SETB OUT_REDLEDn ; Red LED off
  183. SJMP $
  184. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  185. ;; Add hex representation of A to buffer at R0 and accumulate checksum.
  186. ;;
  187. HEXBYTE: MOV HINYBBLE, A ; Add byte to checksum
  188. MOV LONYBBLE, A
  189. ADD A, CKSUM
  190. MOV CKSUM, A
  191. MOV A, HINYBBLE ; Isolate high nybble
  192. SWAP A
  193. ANL A, #0x0F
  194. ADD A, #'0'
  195. MOV HINYBBLE, A
  196. CLR C
  197. SUBB A, #':'
  198. JC .LO
  199. MOV A, HINYBBLE
  200. ADD A, #7
  201. MOV HINYBBLE, A
  202. .LO: MOV A, LONYBBLE ; Isolate low nybble
  203. ANL A, #0x0F
  204. ADD A, #'0'
  205. MOV LONYBBLE, A
  206. CLR C
  207. SUBB A, #':'
  208. JC .STHI
  209. MOV A, LONYBBLE
  210. ADD A, #7
  211. MOV LONYBBLE, A
  212. .STHI: MOV A, HINYBBLE ; Store high nybble hex char
  213. MOV @R0, A
  214. INC R0
  215. MOV A, LONYBBLE ; Store low nybble hex char
  216. MOV @R0, A
  217. INC R0
  218. RET
  219. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  220. ;; Send out the current record.
  221. ;;
  222. SENDRECORD: MOV R0, #BUF_START ; Init pointer
  223. .SENDCHAR: MOV A, @R0 ; Get next char
  224. JZ .DONE ; Done if it is NUL
  225. CLR SCON_TI ; Send byte
  226. MOV SBUF, A
  227. JNB SCON_TI, $ ; Loop until character sent
  228. INC R0
  229. SJMP .SENDCHAR
  230. .DONE: RET
  231. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  232. ;; Error handler
  233. ;; Turn on red LED and loop forever.
  234. ;;
  235. ERROR: SETB OUT_GRNLEDn ; Green LED off
  236. CLR OUT_REDLEDn ; Red LED on
  237. SJMP $
  238. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  239. ;; Delay 100ms
  240. ;; Uses Timer 0, delaying 50ms twice
  241. ;; Oscillator frequency = 11.0592 MHz
  242. ;; ==> 50ms = 11059200 / (12 * 20) = 46080 = 0xB400 counts
  243. ;; ==> (TH0,TL0) = 0x10000 - 0xB400 = 0x4C00
  244. ;; This all ignores subroutine call overhead and so forth.
  245. ;;
  246. DELAY100ms: CLR TCON_TR0 ; Timer off
  247. MOV TH0, #0x4C ; 50ms delay
  248. MOV TL0, #0x00
  249. CLR TCON_TF0 ; Clear overflow
  250. SETB TCON_TR0 ; Timer on
  251. JNB TCON_TF0, $ ; Wait for overflow
  252. CLR TCON_TR0 ; Timer off
  253. MOV TH0, #0x4C ; 50ms delay
  254. MOV TL0, #0x00
  255. CLR TCON_TF0 ; Clear overflow
  256. SETB TCON_TR0 ; Timer on
  257. JNB TCON_TF0, $ ; Wait for overflow
  258. CLR TCON_TR0 ; Timer off
  259. RET
  260. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  261. ;; Send NUL-terminated string in ROM pointed to by DPTR.
  262. ;; Trashes A, DPTR
  263. ;;
  264. SENDROM: CLR A ; Get byte from ROM
  265. MOVC A, @A+DPTR
  266. JZ .DONE ; Done if it is NUL
  267. CLR SCON_TI ; Send byte
  268. MOV SBUF, A
  269. JNB SCON_TI, $ ; Loop until character sent
  270. INC DPTR ; Send next byte
  271. SJMP SENDROM
  272. .DONE: RET
  273. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  274. ;;
  275. END
  276. ;;
  277. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;