supercic-key.asm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. #include <p12f629.inc>
  2. processor p12f629
  3. ; ---------------------------------------------------------------------
  4. ; SNES CIC clone for PIC Microcontroller (key mode only)
  5. ;
  6. ; Copyright (C) 2010 by Maximilian Rehkopf (ikari_01) <otakon@gmx.net>
  7. ; This software is part of the sd2snes project.
  8. ;
  9. ; This program is free software; you can redistribute it and/or modify
  10. ; it under the terms of the GNU General Public License as published by
  11. ; the Free Software Foundation; version 2 of the License only.
  12. ;
  13. ; This program is distributed in the hope that it will be useful,
  14. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ; GNU General Public License for more details.
  17. ;
  18. ; You should have received a copy of the GNU General Public License
  19. ; along with this program; if not, write to the Free Software
  20. ; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. ;
  22. ; ---------------------------------------------------------------------
  23. ;
  24. ; pin configuration: (cartridge pin) [key CIC pin]
  25. ;
  26. ; ,---_---.
  27. ; +5V (27,58) [16] |1 8| GND (5,36) [8]
  28. ; CIC clk (56) [6] |2 7| CIC data i/o 0 (55) [2]
  29. ; status out |3 6| CIC data i/o 1 (24) [1]
  30. ; /PAIR |4 5| CIC slave reset (25) [7]
  31. ; `-------'
  32. ;
  33. ;
  34. ; Status out can be connected to a LED. It indicates:
  35. ;
  36. ; state | output
  37. ; -------------------------+----------------------------
  38. ; OK or no lock CIC | high
  39. ; error | low
  40. ; SuperCIC pair mode | 148.75kHz / 50% duty cycle
  41. ; SuperCIC host detected | scarce spikes (3.3us)
  42. ; but pair mode disabled |
  43. ;
  44. ; In case lockout fails, the region is switched automatically and
  45. ; will be used after the next reset.
  46. ;
  47. ; The /PAIR pin can be used to enable or disable SuperCIC pair mode.
  48. ; It can be tied low or high to statically enable or disable pair mode
  49. ; detection, or connected to a switch or MCU to dynamically enable pair
  50. ; mode detection (it should then be connected to a pull-up resistor to
  51. ; Vcc). Pair mode detection can be enabled during operation,
  52. ; but pair mode cannot be left until the next power cycle.
  53. ; See SuperCIC lock documentation for a more detailed description of
  54. ; pair mode.
  55. ;
  56. ; memory usage:
  57. ;
  58. ; 0x20 buffer for seed calc and transfer
  59. ; 0x21 - 0x2f seed area (lock seed)
  60. ; 0x30 buffer for seed calc
  61. ; 0x31 - 0x3f seed area (key seed; 0x31 filled in by lock)
  62. ; 0x40 - 0x41 buffer for seed calc
  63. ; 0x4d buffer for eeprom access
  64. ; 0x4e loop variable for longwait
  65. ; 0x4f loop variable for wait
  66. ; 0x5d 0: SuperCIC pair mode available flag
  67. ; 0x5e SuperCIC pair mode detect (phase 1)
  68. ; 0x5f SuperCIC pair mode detect (phase 2)
  69. ; ---------------------------------------------------------------------
  70. ; -----------------------------------------------------------------------
  71. __CONFIG _EC_OSC & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _CPD_OFF
  72. ; -----------------------------------------------------------------------
  73. ; code memory
  74. org 0x0000
  75. nop
  76. nop
  77. nop
  78. goto init
  79. isr
  80. org 0x0004
  81. bcf INTCON, 1 ; clear interrupt cause
  82. bcf GPIO, 0
  83. bcf GPIO, 1
  84. bsf GPIO, 4 ; LED on
  85. clrf 0x5e ; clear pair mode detect
  86. clrf 0x5f ; clear pair mode detect
  87. bsf 0x5f, 1 ;
  88. clrf 0x5d ; clear pair mode available
  89. nop
  90. nop
  91. bsf INTCON, 7 ; re-enable interrupts (ISR will continue as main)
  92. goto main
  93. init
  94. org 0x0010
  95. banksel GPIO
  96. clrf GPIO
  97. movlw 0x07 ; GPIO2..0 are digital I/O (not connected to comparator)
  98. movwf CMCON
  99. movlw 0x90 ; global enable interrupts + enable external interrupt
  100. movwf INTCON
  101. banksel TRISIO
  102. movlw 0x2d ; in out in in out in
  103. movwf TRISIO
  104. movlw 0x24 ; pullups for reset+clk to avoid errors when no CIC in host
  105. movwf WPU
  106. movlw 0x00 ; 0x80 for global pullup disable
  107. movwf OPTION_REG
  108. banksel GPIO
  109. bsf GPIO, 4 ; LED on
  110. idle
  111. goto idle ; wait for interrupt from lock
  112. main
  113. banksel TRISIO
  114. bsf TRISIO, 0
  115. bcf TRISIO, 1
  116. banksel GPIO
  117. ; --------INIT LOCK SEED (what the lock sends)--------
  118. movlw 0xb
  119. movwf 0x21
  120. movlw 0x1
  121. movwf 0x22
  122. movlw 0x4
  123. movwf 0x23
  124. movlw 0xf
  125. movwf 0x24
  126. movlw 0x4
  127. movwf 0x25
  128. movlw 0xb
  129. movwf 0x26
  130. movlw 0x5
  131. movwf 0x27
  132. movlw 0x7
  133. movwf 0x28
  134. movlw 0xf
  135. movwf 0x29
  136. movlw 0xd
  137. movwf 0x2a
  138. movlw 0x6
  139. movwf 0x2b
  140. movlw 0x1
  141. movwf 0x2c
  142. movlw 0xe
  143. movwf 0x2d
  144. movlw 0x9
  145. movwf 0x2e
  146. movlw 0x8
  147. movwf 0x2f
  148. ; --------INIT KEY SEED (what we must send)--------
  149. banksel EEADR ; D/F411 and D/F413
  150. clrf EEADR ; differ in 2nd seed nibble
  151. bsf EECON1, RD ; of key stream,
  152. movf EEDAT, w ; restore saved nibble from EEPROM
  153. banksel GPIO
  154. movwf 0x32
  155. movlw 0xa
  156. movwf 0x33
  157. movlw 0x1
  158. movwf 0x34
  159. movlw 0x8
  160. movwf 0x35
  161. movlw 0x5
  162. movwf 0x36
  163. movlw 0xf
  164. movwf 0x37
  165. movlw 0x1
  166. movwf 0x38
  167. movwf 0x39
  168. movlw 0xe
  169. movwf 0x3a
  170. movlw 0x1
  171. movwf 0x3b
  172. movlw 0x0
  173. movwf 0x3c
  174. movlw 0xd
  175. movwf 0x3d
  176. movlw 0xe
  177. movwf 0x3e
  178. movlw 0xc
  179. movwf 0x3f
  180. ; --------wait for stream ID--------
  181. movlw 0xb5
  182. call wait
  183. clrf 0x31 ; clear lock stream ID
  184. ; --------lock sends stream ID. 15 cycles per bit--------
  185. ; bsf GPIO, 0 ; (debug marker)
  186. ; bcf GPIO, 0 ;
  187. btfsc GPIO, 0 ; check stream ID bit
  188. bsf 0x31, 3 ; copy to lock seed
  189. movlw 0x2 ; wait=3*W+5
  190. call wait ; burn 11 cycles
  191. nop
  192. nop
  193. ; bsf GPIO, 0
  194. ; bcf GPIO, 0
  195. btfsc GPIO, 0 ; check stream ID bit
  196. bsf 0x31, 0 ; copy to lock seed
  197. movlw 0x2 ;
  198. call wait ; burn 11 cycles
  199. nop
  200. nop
  201. ; bsf GPIO, 0
  202. ; bcf GPIO, 0
  203. btfsc GPIO, 0 ; check stream ID bit
  204. bsf 0x31, 1 ; copy to lock seed
  205. movlw 0x2 ;
  206. call wait ; burn 11 cycles
  207. nop
  208. nop
  209. ; bsf GPIO, 0
  210. ; bcf GPIO, 0
  211. btfsc GPIO, 0 ; check stream ID bit
  212. bsf 0x31, 2 ; copy to lock seed
  213. banksel TRISIO
  214. bcf TRISIO, 0
  215. bsf TRISIO, 1
  216. banksel GPIO
  217. nop
  218. movlw 0x27 ; "wait" 1
  219. call wait ; wait 121
  220. ; --------main loop--------
  221. loop
  222. movlw 0x1
  223. loop0
  224. addlw 0x30 ; key stream
  225. movwf FSR ; store in index reg
  226. loop1
  227. nop
  228. nop
  229. movf INDF, w ; load seed value
  230. movwf 0x20
  231. bcf 0x20, 1 ; clear bit 1
  232. btfsc 0x20, 0 ; copy from bit 0
  233. bsf 0x20, 1 ; (if set)
  234. bsf 0x20, 4 ; LED on
  235. movf 0x20, w
  236. movwf GPIO
  237. nop
  238. movlw 0x10
  239. movwf GPIO ; reset GPIO
  240. movlw 0x14
  241. call wait
  242. btfsc 0x5d, 0 ; pair mode available signal
  243. bcf GPIO, 4 ;
  244. nop
  245. nop
  246. bsf GPIO, 4 ;
  247. btfsc GPIO, 0 ; both pins must be low...
  248. goto die
  249. btfsc GPIO, 1 ; ...when no bit transfer takes place
  250. goto die ; if not -> lock cic error state -> die
  251. incf FSR, f ; next one
  252. movlw 0xf
  253. andwf FSR, w
  254. btfss STATUS, Z
  255. goto loop1
  256. call mangle
  257. call mangle
  258. call mangle
  259. movlw 0x2 ; wait 10
  260. call wait ;
  261. nop
  262. nop
  263. btfsc 0x37, 0
  264. goto swap
  265. banksel TRISIO
  266. bcf TRISIO, 0
  267. bsf TRISIO, 1
  268. goto swapskip
  269. swap
  270. banksel TRISIO
  271. bsf TRISIO, 0
  272. bcf TRISIO, 1
  273. nop
  274. swapskip
  275. banksel GPIO
  276. movf 0x37, w
  277. andlw 0xf
  278. btfss STATUS, Z
  279. goto loop0
  280. goto loop
  281. ; --------calculate new seeds--------
  282. ; had to be unrolled because PIC has an inefficient way of handling
  283. ; indirect access, no post increment, etc.
  284. mangle
  285. call mangle_lock
  286. nop
  287. nop
  288. mangle_key
  289. movf 0x2f, w
  290. movwf 0x20
  291. mangle_key_loop
  292. addlw 0x1
  293. addwf 0x21, f
  294. movf 0x22, w
  295. movwf 0x40
  296. movf 0x21, w
  297. addwf 0x22, f
  298. incf 0x22, f
  299. comf 0x22, f
  300. movf 0x23, w
  301. movwf 0x41 ; store 23 to 41
  302. movlw 0xf
  303. andwf 0x23, f
  304. movf 0x40, w ; add 40(22 old)+23+#1 and skip if carry
  305. andlw 0xf
  306. addwf 0x23, f
  307. incf 0x23, f
  308. btfsc 0x23, 4
  309. goto mangle_key_withskip
  310. mangle_key_withoutskip
  311. movf 0x41, w ; restore 23
  312. addwf 0x24, f ; add to 24
  313. movf 0x25, w
  314. movwf 0x40 ; save 25 to 40
  315. movf 0x24, w
  316. addwf 0x25, f
  317. movf 0x26, w
  318. movwf 0x41 ; save 26 to 41
  319. movf 0x40, w ; restore 25
  320. andlw 0xf ; mask nibble
  321. addlw 0x8 ; add #8 to HIGH nibble
  322. movwf 0x40
  323. btfss 0x40, 4 ; skip if carry to 5th bit
  324. addwf 0x26, w
  325. movwf 0x26
  326. movf 0x41, w ; restore 26
  327. addlw 0x1 ; inc
  328. addwf 0x27, f ; add to 27
  329. movf 0x27, w ;
  330. addlw 0x1 ; inc
  331. addwf 0x28, f ; add to 28
  332. movf 0x28, w ;
  333. addlw 0x1 ; inc
  334. addwf 0x29, f ; add to 29
  335. movf 0x29, w ;
  336. addlw 0x1 ; inc
  337. addwf 0x2a, f ; add to 2a
  338. movf 0x2a, w ;
  339. addlw 0x1 ; inc
  340. addwf 0x2b, f ; add to 2b
  341. movf 0x2b, w ;
  342. addlw 0x1 ; inc
  343. addwf 0x2c, f ; add to 2c
  344. movf 0x2c, w ;
  345. addlw 0x1 ; inc
  346. addwf 0x2d, f ; add to 2d
  347. movf 0x2d, w ;
  348. addlw 0x1 ; inc
  349. addwf 0x2e, f ; add to 2e
  350. movf 0x2e, w ;
  351. addlw 0x1 ; inc
  352. addwf 0x2f, f ; add to 2f
  353. movf 0x20, w ; restore original 0xf
  354. andlw 0xf
  355. addlw 0xf
  356. movwf 0x20
  357. btfsc 0x5d, 0 ; pair mode available signal
  358. bcf GPIO, 4
  359. nop
  360. nop
  361. bsf GPIO, 4
  362. nop
  363. nop
  364. nop
  365. nop
  366. btfss 0x20, 4 ; skip if half-byte carry
  367. goto mangle_return ; +2 cycles in return
  368. nop
  369. goto mangle_key_loop
  370. ; 69 when goto, 69 when return
  371. ; CIC has 78 -> 9 nops
  372. mangle_key_withskip
  373. movf 0x41, w ; restore 23
  374. addwf 0x23, f ; add to 23
  375. movf 0x24, w
  376. movwf 0x40 ; save 24 to 40
  377. movf 0x23, w
  378. addwf 0x24, f
  379. movf 0x25, w
  380. movwf 0x41 ; save 25 to 41
  381. movf 0x40, w ; restore 24
  382. andlw 0xf ; mask nibble
  383. addlw 0x8 ; add #8 to HIGH nibble
  384. movwf 0x40
  385. btfss 0x40, 4 ; skip if carry to 5th bit
  386. addwf 0x25, w
  387. movwf 0x25
  388. movf 0x41, w ; restore 25
  389. addlw 0x1 ; inc
  390. addwf 0x26, f ; add to 26
  391. movf 0x26, w ;
  392. addlw 0x1 ; inc
  393. addwf 0x27, f ; add to 27
  394. movf 0x27, w ;
  395. addlw 0x1 ; inc
  396. addwf 0x28, f ; add to 28
  397. movf 0x28, w ;
  398. addlw 0x1 ; inc
  399. addwf 0x29, f ; add to 29
  400. movf 0x29, w ;
  401. addlw 0x1 ; inc
  402. addwf 0x2a, f ; add to 2a
  403. movf 0x2a, w ;
  404. addlw 0x1 ; inc
  405. addwf 0x2b, f ; add to 2b
  406. movf 0x2b, w ;
  407. addlw 0x1 ; inc
  408. addwf 0x2c, f ; add to 2c
  409. movf 0x2c, w ;
  410. addlw 0x1 ; inc
  411. addwf 0x2d, f ; add to 2d
  412. movf 0x2d, w ;
  413. addlw 0x1 ; inc
  414. addwf 0x2e, f ; add to 2e
  415. movf 0x2e, w ;
  416. addlw 0x1 ; inc
  417. addwf 0x2f, f ; add to 2f
  418. movf 0x20, w ; restore original 0xf
  419. andlw 0xf
  420. addlw 0xf
  421. movwf 0x20
  422. ;-------pair mode code-------
  423. bcf GPIO, 0
  424. movf GPIO, w
  425. btfss GPIO, 3
  426. bsf GPIO, 0
  427. movwf 0x5e
  428. movf GPIO, w
  429. movwf 0x5f
  430. bcf GPIO, 0
  431. btfsc 0x5d, 0 ; pair mode available signal
  432. bcf GPIO, 4
  433. bsf GPIO, 4
  434. ;-------end of pair mode code-------
  435. btfss 0x20, 4 ; skip if half-byte carry
  436. goto mangle_return ; +2 cycles in return
  437. movf 0x20, w ; restore w (previously destroyed)
  438. goto mangle_key_loop
  439. mangle_return
  440. return
  441. ; 73 when goto, 73 when return
  442. ; CIC has 84 -> 11 nops
  443. mangle_lock
  444. movf 0x3f, w
  445. movwf 0x30
  446. mangle_lock_loop
  447. addlw 0x1
  448. addwf 0x31, f
  449. movf 0x32, w
  450. movwf 0x40
  451. movf 0x31, w
  452. addwf 0x32, f
  453. incf 0x32, f
  454. comf 0x32, f
  455. movf 0x33, w
  456. movwf 0x41 ; store 33 to 41
  457. movlw 0xf
  458. andwf 0x33, f
  459. movf 0x40, w ; add 40(32 old)+33+#1 and skip if carry
  460. andlw 0xf
  461. addwf 0x33, f
  462. incf 0x33, f
  463. btfsc 0x33, 4
  464. goto mangle_lock_withskip
  465. mangle_lock_withoutskip
  466. movf 0x41, w ; restore 33
  467. addwf 0x34, f ; add to 34
  468. movf 0x35, w
  469. movwf 0x40 ; save 35 to 40
  470. movf 0x34, w
  471. addwf 0x35, f
  472. movf 0x36, w
  473. movwf 0x41 ; save 36 to 41
  474. movf 0x40, w ; restore 35
  475. andlw 0xf ; mask nibble
  476. addlw 0x8 ; add #8 to HIGH nibble
  477. movwf 0x40
  478. btfss 0x40, 4 ; skip if carry to 5th bit
  479. addwf 0x36, w
  480. movwf 0x36
  481. movf 0x41, w ; restore 36
  482. addlw 0x1 ; inc
  483. addwf 0x37, f ; add to 37
  484. movf 0x37, w ;
  485. addlw 0x1 ; inc
  486. addwf 0x38, f ; add to 38
  487. movf 0x38, w ;
  488. addlw 0x1 ; inc
  489. addwf 0x39, f ; add to 39
  490. movf 0x39, w ;
  491. addlw 0x1 ; inc
  492. addwf 0x3a, f ; add to 3a
  493. movf 0x3a, w ;
  494. addlw 0x1 ; inc
  495. addwf 0x3b, f ; add to 3b
  496. movf 0x3b, w ;
  497. addlw 0x1 ; inc
  498. addwf 0x3c, f ; add to 3c
  499. movf 0x3c, w ;
  500. addlw 0x1 ; inc
  501. addwf 0x3d, f ; add to 3d
  502. movf 0x3d, w ;
  503. addlw 0x1 ; inc
  504. addwf 0x3e, f ; add to 3e
  505. movf 0x3e, w ;
  506. addlw 0x1 ; inc
  507. addwf 0x3f, f ; add to 3f
  508. movf 0x30, w ; restore original 0xf
  509. andlw 0xf
  510. addlw 0xf
  511. movwf 0x30
  512. btfsc 0x5d, 0 ; pair mode available signal
  513. bcf GPIO, 4
  514. nop
  515. nop
  516. bsf GPIO, 4
  517. nop
  518. nop
  519. nop
  520. nop
  521. btfss 0x30, 4 ; skip if half-byte carry
  522. goto mangle_return
  523. nop
  524. goto mangle_lock_loop
  525. ; 69 when goto, 69 when return
  526. ; CIC has 78 -> 9 nops
  527. mangle_lock_withskip
  528. movf 0x41, w ; restore 33
  529. addwf 0x33, f ; add to 33
  530. movf 0x34, w
  531. movwf 0x40 ; save 34 to 40
  532. movf 0x33, w
  533. addwf 0x34, f
  534. movf 0x35, w
  535. movwf 0x41 ; save 35 to 41
  536. movf 0x40, w ; restore 34
  537. andlw 0xf ; mask nibble
  538. addlw 0x8 ; add #8 to HIGH nibble
  539. movwf 0x40
  540. btfss 0x40, 4 ; skip if carry to 5th bit
  541. addwf 0x35, w
  542. movwf 0x35
  543. movf 0x41, w ; restore 35
  544. addlw 0x1 ; inc
  545. addwf 0x36, f ; add to 36
  546. movf 0x36, w ;
  547. addlw 0x1 ; inc
  548. addwf 0x37, f ; add to 37
  549. movf 0x37, w ;
  550. addlw 0x1 ; inc
  551. addwf 0x38, f ; add to 38
  552. movf 0x38, w ;
  553. addlw 0x1 ; inc
  554. addwf 0x39, f ; add to 39
  555. movf 0x39, w ;
  556. addlw 0x1 ; inc
  557. addwf 0x3a, f ; add to 3a
  558. movf 0x3a, w ;
  559. addlw 0x1 ; inc
  560. addwf 0x3b, f ; add to 3b
  561. movf 0x3b, w ;
  562. addlw 0x1 ; inc
  563. addwf 0x3c, f ; add to 3c
  564. movf 0x3c, w ;
  565. addlw 0x1 ; inc
  566. addwf 0x3d, f ; add to 3d
  567. movf 0x3d, w ;
  568. addlw 0x1 ; inc
  569. addwf 0x3e, f ; add to 3e
  570. movf 0x3e, w ;
  571. addlw 0x1 ; inc
  572. addwf 0x3f, f ; add to 3f
  573. movf 0x30, w ; restore original 0xf
  574. andlw 0xf
  575. addlw 0xf
  576. movwf 0x30
  577. ;-------pair mode code-------
  578. btfss 0x5e, 1
  579. goto scic_pair_skip1
  580. btfsc 0x5f, 1
  581. goto scic_pair_skip2
  582. btfsc GPIO, 3
  583. goto scic_pair_skip3
  584. goto supercic_pairmode
  585. scic_pair_skip1
  586. nop
  587. nop
  588. scic_pair_skip2
  589. nop
  590. nop
  591. nop
  592. nop
  593. goto scic_pair_skip4
  594. scic_pair_skip3
  595. bcf GPIO, 4
  596. bsf 0x5d, 0 ; set pair mode available
  597. nop
  598. bsf GPIO, 4
  599. scic_pair_skip4
  600. ;-------end of pair mode code-------
  601. btfss 0x30, 4 ; skip if half-byte carry
  602. goto mangle_return
  603. nop
  604. goto mangle_lock_loop
  605. ; 73 when goto, 73 when return
  606. ; CIC has 84 -> 11 nops
  607. ; --------wait: 3*(W-1)+7 cycles (including call+return). W=0 -> 256!--------
  608. wait
  609. movwf 0x4f
  610. wait0 decfsz 0x4f, f
  611. goto wait0
  612. return
  613. ; --------wait long: 8+(3*(w-1))+(772*w). W=0 -> 256!--------
  614. longwait
  615. movwf 0x4e
  616. clrw
  617. longwait0
  618. call wait
  619. decfsz 0x4e, f
  620. goto longwait0
  621. return
  622. ; --------change region in eeprom and die--------
  623. die
  624. movlw 0x3a ;wait 50ms before writing
  625. call longwait ;("error" might be due to power loss)
  626. banksel EEADR
  627. clrw
  628. movwf EEADR
  629. bsf EECON1, RD
  630. movf EEDAT, w
  631. banksel GPIO
  632. movwf 0x4d
  633. btfsc 0x4d, 0
  634. goto die_reg_6
  635. die_reg_9
  636. movlw 0x9 ; died with PAL, fall back to NTSC
  637. goto die_reg_cont
  638. die_reg_6
  639. movlw 0x6 ; died with NTSC, fall back to PAL
  640. die_reg_cont
  641. banksel EEADR
  642. movwf EEDAT
  643. bsf EECON1, WREN
  644. die_intloop
  645. bcf INTCON, GIE
  646. btfsc INTCON, GIE
  647. goto die_intloop
  648. movlw 0x55
  649. movwf EECON2
  650. movlw 0xaa
  651. movwf EECON2
  652. bsf EECON1, WR
  653. bsf INTCON, GIE
  654. banksel GPIO
  655. bcf GPIO, 4
  656. ; --------get caught up--------
  657. die_trap
  658. goto die_trap
  659. ; -----------------------------------------------------------------------
  660. supercic_pairmode
  661. banksel TRISIO
  662. bsf TRISIO, 0
  663. bsf TRISIO, 1
  664. banksel GPIO
  665. bcf INTCON, GIE
  666. supercic_pairmode_loop
  667. bsf GPIO, 4
  668. nop
  669. nop
  670. bcf GPIO, 4
  671. goto supercic_pairmode_loop
  672. ; eeprom memory
  673. DEEPROM CODE
  674. de 0x09 ; D411 (NTSC)
  675. end