port1.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /*
  2. MSP430 Emulator
  3. Copyright (C) 2020 Rudolf Geosits (rgeosits@live.esu.edu)
  4. "MSP430 Emulator" is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. "MSP430 Emulator" is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "port1.h"
  16. /* Cheat Sheet:
  17. * PxIN : 0 = LOW input, 1 = HIGH input
  18. * PxOUT: 0 = LOW output, 1 = HIGH output
  19. * PxDIR: 0 = INPUT, 1 = OUTPUT
  20. * PxREN: 0 = Pull Up/Down DISABLED, 1 = Pull Up/Down ENABLED
  21. *
  22. * PxSEL2 | PxSEL | Explaination
  23. * 0 | 0 | I/O function selected
  24. * 0 | 1 | Primary Peripheral module function selected
  25. * 1 | 0 | Reserved ?
  26. * 1 | 1 | Secondary Peripheral module function selected
  27. * [NOTE: P1 and P2 port pin INTs are disabled when PxSEL = 1]
  28. *
  29. * PxIFG: 0 = No interrupt pending, 1 = Interrupt Pending
  30. * PxIES: PxIFG set with a [0 = LOW-HIGH, 1 = HIGH-LOW] transition
  31. * PxIE : 0 = interrupt disabled, 1 = interrupt enabled
  32. */
  33. void handle_port_1 (Emulator *emu)
  34. {
  35. Cpu *cpu = emu->cpu;
  36. Port_1 *p = cpu->p1;
  37. uint8_t flag = 0;
  38. //////////////////// P1.0 ////////////////////////
  39. // Check Direction
  40. if (*p->_DIR & 0x01) {
  41. p->DIR_0 = true; // Set P1DIR.0 flag
  42. if(*p->_OUT & 0x01) {
  43. if(p->OUT_0 == false) send_control(emu, P1_0_ON_PACKET, NULL, 0);
  44. p->OUT_0 = true; // Set P1OUT.0 flag
  45. } else {
  46. if(p->OUT_0 == true) send_control(emu, P1_0_OFF_PACKET, NULL, 0);
  47. p->OUT_0 = false;// Reset P1OUT.0 flag
  48. }
  49. // if(((p->DIR_0 == false) && (*p->_DIR & 0x01)) || ((*p->_OUT & 0x01) != p->OUT_0)) {
  50. // if (*p->_OUT & 0x01) send_control(emu, P1_0_ON_PACKET, NULL, 0);
  51. // else send_control(emu, P1_0_OFF_PACKET, NULL, 0);
  52. // }
  53. }
  54. // Check INPUT
  55. else {
  56. p->DIR_0 = false;
  57. }
  58. /// Check if Interrupt Enabled for pin
  59. if (*p->_IE & 0x01)
  60. {
  61. p->IE_0 = true;
  62. // Check For Interrupt Pending
  63. if (*p->_IFG & 0x01)
  64. {
  65. // Set p->IFG.0 flag indicating INT
  66. p->IFG_0 = true;
  67. }
  68. else
  69. {
  70. p->IFG_0 = false;
  71. }
  72. }
  73. else
  74. {
  75. p->IE_0 = false;
  76. }
  77. if (*p->_REN & 0x01) p->REN_0 = true;
  78. else p->REN_0 = false;
  79. // Check primary select
  80. if (*p->_SEL & 0x01) {
  81. if (p->SEL_0 == false) {
  82. // puts("P1_SEL_0 = 1");
  83. }
  84. p->SEL_0 = true;
  85. }
  86. else {
  87. if (p->SEL_0 == true) {
  88. // puts("P1_SEL_0 = 0");
  89. }
  90. p->SEL_0 = false;
  91. }
  92. // Check secondary select
  93. if (*p->_SEL2 & 0x01) {
  94. if (p->SEL2_0 == false) {
  95. // puts("P1_SEL2_0 = 1");
  96. }
  97. p->SEL2_0 = true;
  98. }
  99. else {
  100. if (p->SEL2_0 == true) {
  101. // puts("P1_SEL2_0 = 0");
  102. }
  103. p->SEL2_0 = false;
  104. }
  105. // if ((p->SEL2_0 == false) && (p->SEL_0 == false)) {
  106. // // Output
  107. // if(p->DIR_0 == false) {
  108. // if(p->OUT_0 == true) flag = P1_0_ON_PACKET;
  109. // else flag = P1_0_ON_PACKET;
  110. // // Pull up/down enabled
  111. // } else if(p->REN_0 == true) {
  112. // if(p->OUT_0 == true) flag = P1_0_PULLU_PACKET;
  113. // else flag = P1_0_PULLD_PACKET;
  114. // } else {
  115. // flag = P1_0_HIGHZ_PACKET;
  116. // }
  117. // if(flag != p->PIN0F) {
  118. // p->PIN0F = flag;
  119. // send_control(emu, flag, NULL, 0);
  120. // }
  121. // }
  122. //////////////////// P1.1 ////////////////////////
  123. // Check Direction and IN/OUT
  124. if (*p->_DIR & 0x02) {
  125. p->DIR_1 = true;
  126. if (*p->_OUT & 0x02) {
  127. p->OUT_1 = true;
  128. }
  129. else {
  130. p->OUT_1 = false;
  131. }
  132. }
  133. else {
  134. p->DIR_1 = false;
  135. }
  136. // Check Interrupts
  137. if (*p->_IE & 0x02) {
  138. p->IE_1 = true;
  139. if (*p->_IFG & 0x02) {
  140. p->IFG_1 = true;
  141. }
  142. else {
  143. p->IFG_1 = false;
  144. }
  145. }
  146. else {
  147. p->IE_1 = false;
  148. }
  149. // Check primary select
  150. if (*p->_SEL & 0x02) {
  151. if (p->SEL_1 == false) {
  152. // puts("P1_SEL_1 = 1");
  153. }
  154. p->SEL_1 = true;
  155. }
  156. else {
  157. if (p->SEL_1 == true) {
  158. // puts("P1_SEL_1 = 0");
  159. }
  160. p->SEL_1 = false;
  161. }
  162. // Check secondary select
  163. if (*p->_SEL2 & 0x02) {
  164. if (p->SEL2_1 == false) {
  165. p->SEL2_1 = true;
  166. // puts("P1_SEL2_1 = 1");
  167. }
  168. }
  169. else {
  170. if (p->SEL2_1 == true) {
  171. p->SEL2_1 = false;
  172. // puts("P1_SEL2_1 = 0");
  173. }
  174. }
  175. //////////////////// P1.2 ////////////////////////
  176. if (*p->_DIR & 0x04)
  177. {
  178. p->DIR_2 = true;
  179. if (*p->_OUT & 0x04)
  180. {
  181. p->OUT_2 = true;
  182. }
  183. else
  184. {
  185. p->OUT_2 = false;
  186. }
  187. }
  188. else
  189. {
  190. p->DIR_2 = false;
  191. }
  192. if (*p->_IE & 0x04)
  193. {
  194. p->IE_2 = true;
  195. if (*p->_IFG & 0x04)
  196. {
  197. p->IFG_2 = true;
  198. }
  199. else
  200. {
  201. p->IFG_2 = false;
  202. }
  203. }
  204. else
  205. {
  206. p->IE_2 = false;
  207. }
  208. // Check primary select
  209. if (*p->_SEL & 0x04)
  210. {
  211. if (p->SEL_2 == false)
  212. {
  213. // puts("P1_SEL_2 = 1");
  214. }
  215. p->SEL_2 = true;
  216. }
  217. else
  218. {
  219. if (p->SEL_2 == true)
  220. {
  221. // puts("P1_SEL_2 = 0");
  222. }
  223. p->SEL_2 = false;
  224. }
  225. // Check secondary select
  226. if (*p->_SEL2 & 0x04) {
  227. if (p->SEL2_2 == false) {
  228. // puts("P1_SEL2_2 = 1");
  229. }
  230. p->SEL2_2 = true;
  231. }
  232. else {
  233. if (p->SEL2_2 == true) {
  234. // puts("P1_SEL2_2 = 0");
  235. }
  236. p->SEL2_2 = false;
  237. }
  238. ////////////////////////////////////////////////
  239. // Handler P1.3
  240. if (*p->_DIR & 0x08) {
  241. p->DIR_3 = true;
  242. if (*p->_OUT & 0x08) {
  243. p->OUT_3 = true;
  244. }
  245. else {
  246. p->OUT_3 = false;
  247. }
  248. }
  249. else {
  250. p->DIR_3 = false;
  251. }
  252. if (*p->_IE & 0x08) {
  253. p->IE_3 = true;
  254. if (*p->_IFG & 0x08) {
  255. if(!p->IFG_3) service_interrupt(emu, PORT1_VECTOR);
  256. p->IFG_3 = true;
  257. }
  258. else {
  259. p->IFG_3 = false;
  260. }
  261. }
  262. else {
  263. p->IE_3 = false;
  264. }
  265. if (!p->DIR_3 & p->OUT_3 & p->REN_3) {
  266. }
  267. ///////////////////////////////////////////////////////////////
  268. // Handler P1.4
  269. if (*p->_DIR & 0x10) {
  270. p->DIR_4 = true;
  271. if (*p->_OUT & 0x10) {
  272. p->OUT_4 = true;
  273. }
  274. else {
  275. p->OUT_4 = false;
  276. }
  277. }
  278. else {
  279. p->DIR_4 = false;
  280. }
  281. if (*p->_IE & 0x10) {
  282. p->IE_4 = true;
  283. if (*p->_IFG & 0x10) {
  284. p->IFG_4 = true;
  285. }
  286. else {
  287. p->IFG_4 = false;
  288. }
  289. }
  290. else {
  291. p->IE_4 = false;
  292. }
  293. /////////////////////////////////////////////////
  294. // Handler P1.5
  295. if (*p->_DIR & 0x20) {
  296. p->DIR_5 = true;
  297. if (*p->_OUT & 0x20) {
  298. p->OUT_5 = true;
  299. }
  300. else {
  301. p->OUT_5 = false;
  302. }
  303. }
  304. else {
  305. p->DIR_5 = false;
  306. }
  307. if (*p->_IE & 0x20) {
  308. p->IE_5 = true;
  309. if (*p->_IFG & 0x20) {
  310. p->IFG_5 = true;
  311. }
  312. else {
  313. p->IFG_5 = false;
  314. }
  315. }
  316. else {
  317. p->IE_5 = false;
  318. }
  319. ////////////////////////////////////////////////////
  320. // Handler P1.6
  321. if (*p->_DIR & 0x40)
  322. {
  323. if(*p->_OUT & 0x40) {
  324. if(p->OUT_6 == false) send_control(emu, P1_6_ON_PACKET, NULL, 0);
  325. p->OUT_6 = true; // Set P1OUT.6 flag
  326. } else {
  327. if(p->OUT_6 == true) send_control(emu, P1_6_OFF_PACKET, NULL, 0);
  328. p->OUT_6 = false;// Reset P1OUT.6 flag
  329. }
  330. p->DIR_6 = true;
  331. }
  332. else
  333. {
  334. p->DIR_6 = false;
  335. }
  336. if (*p->_IE & 0x40)
  337. {
  338. p->IE_6 = true;
  339. if (*p->_IFG & 0x40)
  340. {
  341. p->IFG_6 = true;
  342. }
  343. else
  344. {
  345. p->IFG_6 = false;
  346. }
  347. }
  348. else
  349. {
  350. p->IE_6 = false;
  351. }
  352. ////////////////////////////////////////////////////
  353. // Handler P1.7
  354. if (*p->_DIR & 0x80) {
  355. p->DIR_7 = true;
  356. if (*p->_OUT & 0x80) {
  357. p->OUT_7 = true;
  358. }
  359. else {
  360. p->OUT_7 = false;
  361. }
  362. }
  363. else {
  364. p->DIR_7 = false;
  365. }
  366. if (*p->_IE & 0x80) {
  367. p->IE_7 = true;
  368. if (*p->_IFG & 0x80) {
  369. p->IFG_7 = true;
  370. }
  371. else {
  372. p->IFG_7 = false;
  373. }
  374. }
  375. else {
  376. p->IE_7 = false;
  377. }
  378. // New P1IN value
  379. // Natural pull-up configuration
  380. uint8_t value = (~*p->_DIR) & *p->_REN & *p->_OUT;
  381. // Pins raised due to external bias
  382. value |= (p->EXT_EN & p->EXT_DIR);
  383. // Pins lowered due to external bias
  384. value &= ~(p->EXT_EN & ~(p->EXT_DIR));
  385. if(*p->_IN != value) {
  386. // printf("Changing PIN 0x%02X -> 0x%02X\n", *p->_IN, value);
  387. // Compute with edge trigger
  388. uint8_t ifg = *p->_IE & (((~*p->_IN) & value & (~*p->_IES)) | (*p->_IN & (~value) & *p->_IES));
  389. *p->_IN = value;
  390. if(ifg > 0 && ifg != *p->_IFG) {
  391. //printf("Interrupt %02X^%02X %02X\n", ifg, *p->_IFG, (ifg ^ *p->_IFG));
  392. *p->_IFG |= ifg;
  393. service_interrupt(emu, PORT1_VECTOR);
  394. }
  395. }
  396. }
  397. void setup_port_1 (Emulator *emu)
  398. {
  399. Cpu *cpu = emu->cpu;
  400. Port_1 *p = cpu->p1;
  401. static const uint16_t IN_VLOC = 0x20; // Input
  402. static const uint16_t OUT_VLOC = 0x21; // Output
  403. static const uint16_t DIR_VLOC = 0x22; // Direction
  404. static const uint16_t IFG_VLOC = 0x23; // Interrupt flag
  405. static const uint16_t IES_VLOC = 0x24; // Interrupt Edge Select
  406. static const uint16_t IE_VLOC = 0x25; // Interrupt Enable
  407. static const uint16_t SEL_VLOC = 0x26; // Select
  408. static const uint16_t SEL2_VLOC = 0x41; // Select 2
  409. static const uint16_t REN_VLOC = 0x27; // Resistor Enable
  410. *(p->_IN = (uint8_t *) get_addr_ptr(IN_VLOC)) = 0;
  411. *(p->_OUT = (uint8_t *) get_addr_ptr(OUT_VLOC)) = 0;
  412. *(p->_DIR = (uint8_t *) get_addr_ptr(DIR_VLOC)) = 0;
  413. *(p->_IFG = (uint8_t *) get_addr_ptr(IFG_VLOC)) = 0;
  414. *(p->_IES = (uint8_t *) get_addr_ptr(IES_VLOC)) = 0;
  415. *(p->_IE = (uint8_t *) get_addr_ptr(IE_VLOC)) = 0;
  416. *(p->_SEL = (uint8_t *) get_addr_ptr(SEL_VLOC)) = 0;
  417. *(p->_SEL2 = (uint8_t *) get_addr_ptr(SEL2_VLOC)) = 0;
  418. *(p->_REN = (uint8_t *) get_addr_ptr(REN_VLOC)) = 0;
  419. p->EXT_EN = 0;
  420. p->EXT_DIR = 0;
  421. p->DIR_0 = false; p->OUT_0 = false; p->IFG_0 = false;
  422. p->IE_0 = false; p->SEL_0 = false; p->SEL2_0 = false;
  423. p->DIR_1 = false; p->OUT_1 = false; p->IFG_1 = false;
  424. p->IE_1 = false; p->SEL_1 = false; p->SEL2_1 = false;
  425. p->DIR_2 = false; p->OUT_2 = false; p->IFG_2 = false;
  426. p->IE_2 = false; p->SEL_2 = false; p->SEL2_2 = false;
  427. p->DIR_3 = false; p->OUT_3 = false; p->IFG_3 = false;
  428. p->IE_3 = false; p->SEL_3 = false; p->SEL2_3 = false;
  429. p->DIR_4 = false; p->OUT_4 = false; p->IFG_4 = false;
  430. p->IE_4 = false; p->SEL_4 = false; p->SEL2_4 = false;
  431. p->DIR_5 = false; p->OUT_5 = false; p->IFG_5 = false;
  432. p->IE_5 = false; p->SEL_5 = false; p->SEL2_5 = false;
  433. p->DIR_6 = false; p->OUT_6 = false; p->IFG_6 = false;
  434. p->IE_6 = false; p->SEL_6 = false; p->SEL2_6 = false;
  435. p->DIR_7 = false; p->OUT_7 = false; p->IFG_7 = false;
  436. p->IE_7 = false; p->SEL_7 = false; p->SEL2_7 = false;
  437. p->PIN0F = P1_0_OFF_PACKET;
  438. }
  439. /* POWER UP CLEAR (PUC)
  440. *
  441. * A PUC is always generated when a POR is generated, but a POR is not
  442. * generated by a PUC. The following events trigger a PUC:
  443. *
  444. * A POR signal
  445. * Watchdog timer expiration when in watchdog mode only
  446. * Watchdog timer security key violation
  447. * A Flash memory security key violation
  448. * A CPU instruct fetch from the peripheral address range 0h to 01FFh
  449. void power_up_clear () {
  450. *P1OUT = *P1DIR = *P1IFG = *P1IE = *P1SEL = *P1SEL2 = *P1REN = 0;
  451. }
  452. */