port1.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. p->IFG_3 = true;
  256. }
  257. else {
  258. p->IFG_3 = false;
  259. }
  260. }
  261. else {
  262. p->IE_3 = false;
  263. }
  264. ///////////////////////////////////////////////////////////////
  265. // Handler P1.4
  266. if (*p->_DIR & 0x10) {
  267. p->DIR_4 = true;
  268. if (*p->_OUT & 0x10) {
  269. p->OUT_4 = true;
  270. }
  271. else {
  272. p->OUT_4 = false;
  273. }
  274. }
  275. else {
  276. p->DIR_4 = false;
  277. }
  278. if (*p->_IE & 0x10) {
  279. p->IE_4 = true;
  280. if (*p->_IFG & 0x10) {
  281. p->IFG_4 = true;
  282. }
  283. else {
  284. p->IFG_4 = false;
  285. }
  286. }
  287. else {
  288. p->IE_4 = false;
  289. }
  290. /////////////////////////////////////////////////
  291. // Handler P1.5
  292. if (*p->_DIR & 0x20) {
  293. p->DIR_5 = true;
  294. if (*p->_OUT & 0x20) {
  295. p->OUT_5 = true;
  296. }
  297. else {
  298. p->OUT_5 = false;
  299. }
  300. }
  301. else {
  302. p->DIR_5 = false;
  303. }
  304. if (*p->_IE & 0x20) {
  305. p->IE_5 = true;
  306. if (*p->_IFG & 0x20) {
  307. p->IFG_5 = true;
  308. }
  309. else {
  310. p->IFG_5 = false;
  311. }
  312. }
  313. else {
  314. p->IE_5 = false;
  315. }
  316. ////////////////////////////////////////////////////
  317. // Handler P1.6
  318. if (*p->_DIR & 0x40)
  319. {
  320. if(*p->_OUT & 0x40) {
  321. if(p->OUT_6 == false) send_control(emu, P1_6_ON_PACKET, NULL, 0);
  322. p->OUT_6 = true; // Set P1OUT.6 flag
  323. } else {
  324. if(p->OUT_6 == true) send_control(emu, P1_6_OFF_PACKET, NULL, 0);
  325. p->OUT_6 = false;// Reset P1OUT.6 flag
  326. }
  327. p->DIR_6 = true;
  328. }
  329. else
  330. {
  331. p->DIR_6 = false;
  332. }
  333. if (*p->_IE & 0x40)
  334. {
  335. p->IE_6 = true;
  336. if (*p->_IFG & 0x40)
  337. {
  338. p->IFG_6 = true;
  339. }
  340. else
  341. {
  342. p->IFG_6 = false;
  343. }
  344. }
  345. else
  346. {
  347. p->IE_6 = false;
  348. }
  349. ////////////////////////////////////////////////////
  350. // Handler P1.7
  351. if (*p->_DIR & 0x80) {
  352. p->DIR_7 = true;
  353. if (*p->_OUT & 0x80) {
  354. p->OUT_7 = true;
  355. }
  356. else {
  357. p->OUT_7 = false;
  358. }
  359. }
  360. else {
  361. p->DIR_7 = false;
  362. }
  363. if (*p->_IE & 0x80) {
  364. p->IE_7 = true;
  365. if (*p->_IFG & 0x80) {
  366. p->IFG_7 = true;
  367. }
  368. else {
  369. p->IFG_7 = false;
  370. }
  371. }
  372. else {
  373. p->IE_7 = false;
  374. }
  375. }
  376. void setup_port_1 (Emulator *emu)
  377. {
  378. Cpu *cpu = emu->cpu;
  379. Port_1 *p = cpu->p1;
  380. static const uint16_t IN_VLOC = 0x20; // Input
  381. static const uint16_t OUT_VLOC = 0x21; // Output
  382. static const uint16_t DIR_VLOC = 0x22; // Direction
  383. static const uint16_t IFG_VLOC = 0x23; // Interrupt flag
  384. static const uint16_t IES_VLOC = 0x24; // Interrupt Edge Select
  385. static const uint16_t IE_VLOC = 0x25; // Interrupt Enable
  386. static const uint16_t SEL_VLOC = 0x26; // Select
  387. static const uint16_t SEL2_VLOC = 0x41; // Select 2
  388. static const uint16_t REN_VLOC = 0x27; // Resistor Enable
  389. *(p->_IN = (uint8_t *) get_addr_ptr(IN_VLOC)) = 0;
  390. *(p->_OUT = (uint8_t *) get_addr_ptr(OUT_VLOC)) = 0;
  391. *(p->_DIR = (uint8_t *) get_addr_ptr(DIR_VLOC)) = 0;
  392. *(p->_IFG = (uint8_t *) get_addr_ptr(IFG_VLOC)) = 0;
  393. *(p->_IES = (uint8_t *) get_addr_ptr(IES_VLOC)) = 0;
  394. *(p->_IE = (uint8_t *) get_addr_ptr(IE_VLOC)) = 0;
  395. *(p->_SEL = (uint8_t *) get_addr_ptr(SEL_VLOC)) = 0;
  396. *(p->_SEL2 = (uint8_t *) get_addr_ptr(SEL2_VLOC)) = 0;
  397. *(p->_REN = (uint8_t *) get_addr_ptr(REN_VLOC)) = 0;
  398. p->DIR_0 = false; p->OUT_0 = false; p->IFG_0 = false;
  399. p->IE_0 = false; p->SEL_0 = false; p->SEL2_0 = false;
  400. p->DIR_1 = false; p->OUT_1 = false; p->IFG_1 = false;
  401. p->IE_1 = false; p->SEL_1 = false; p->SEL2_1 = false;
  402. p->DIR_2 = false; p->OUT_2 = false; p->IFG_2 = false;
  403. p->IE_2 = false; p->SEL_2 = false; p->SEL2_2 = false;
  404. p->DIR_3 = false; p->OUT_3 = false; p->IFG_3 = false;
  405. p->IE_3 = false; p->SEL_3 = false; p->SEL2_3 = false;
  406. p->DIR_4 = false; p->OUT_4 = false; p->IFG_4 = false;
  407. p->IE_4 = false; p->SEL_4 = false; p->SEL2_4 = false;
  408. p->DIR_5 = false; p->OUT_5 = false; p->IFG_5 = false;
  409. p->IE_5 = false; p->SEL_5 = false; p->SEL2_5 = false;
  410. p->DIR_6 = false; p->OUT_6 = false; p->IFG_6 = false;
  411. p->IE_6 = false; p->SEL_6 = false; p->SEL2_6 = false;
  412. p->DIR_7 = false; p->OUT_7 = false; p->IFG_7 = false;
  413. p->IE_7 = false; p->SEL_7 = false; p->SEL2_7 = false;
  414. p->PIN0F = P1_0_OFF_PACKET;
  415. }
  416. /* POWER UP CLEAR (PUC)
  417. *
  418. * A PUC is always generated when a POR is generated, but a POR is not
  419. * generated by a PUC. The following events trigger a PUC:
  420. *
  421. * A POR signal
  422. * Watchdog timer expiration when in watchdog mode only
  423. * Watchdog timer security key violation
  424. * A Flash memory security key violation
  425. * A CPU instruct fetch from the peripheral address range 0h to 01FFh
  426. void power_up_clear () {
  427. *P1OUT = *P1DIR = *P1IFG = *P1IE = *P1SEL = *P1SEL2 = *P1REN = 0;
  428. }
  429. */