supercic-key.asm 15 KB

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