port1.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. //////////////////// P1.0 ////////////////////////
  38. // Check Direction
  39. if (*p->DIR & 0x01)
  40. {
  41. p->DIR_0 = true; // Set P1DIR.0 flag
  42. if (*p->OUT & 0x01) // Check OUTPUT
  43. p->OUT_0 = true; // Set P1OUT.0 flag
  44. else
  45. p->OUT_0 = false; // Reset P1OUT.0 flag
  46. }
  47. // Check INPUT
  48. else
  49. {
  50. p->DIR_0 = false;
  51. }
  52. /// Check if Interrupt Enabled for pin
  53. if (*p->IE & 0x01)
  54. {
  55. p->IE_0 = true;
  56. // Check For Interrupt Pending
  57. if (*p->IFG & 0x01)
  58. {
  59. // Set p->IFG.0 flag indicating INT
  60. p->IFG_0 = true;
  61. }
  62. else
  63. {
  64. p->IFG_0 = false;
  65. }
  66. }
  67. else
  68. {
  69. p->IE_0 = false;
  70. }
  71. // Check primary select
  72. if (*p->SEL & 0x01) {
  73. if (p->SEL_0 == false) {
  74. puts("P1_SEL_0 = 1");
  75. }
  76. p->SEL_0 = true;
  77. }
  78. else {
  79. if (p->SEL_0 == true) {
  80. puts("P1_SEL_0 = 0");
  81. }
  82. p->SEL_0 = false;
  83. }
  84. // Check secondary select
  85. if (*p->SEL2 & 0x01) {
  86. if (p->SEL2_0 == false) {
  87. puts("P1_SEL2_0 = 1");
  88. }
  89. p->SEL2_0 = true;
  90. }
  91. else {
  92. if (p->SEL2_0 == true) {
  93. puts("P1_SEL2_0 = 0");
  94. }
  95. p->SEL2_0 = false;
  96. }
  97. //////////////////// P1.1 ////////////////////////
  98. // Check Direction and IN/OUT
  99. if (*p->DIR & 0x02) {
  100. p->DIR_1 = true;
  101. if (*p->OUT & 0x02) {
  102. p->OUT_1 = true;
  103. }
  104. else {
  105. p->OUT_1 = false;
  106. }
  107. }
  108. else {
  109. p->DIR_1 = false;
  110. }
  111. // Check Interrupts
  112. if (*p->IE & 0x02) {
  113. p->IE_1 = true;
  114. if (*p->IFG & 0x02) {
  115. p->IFG_1 = true;
  116. }
  117. else {
  118. p->IFG_1 = false;
  119. }
  120. }
  121. else {
  122. p->IE_1 = false;
  123. }
  124. // Check primary select
  125. if (*p->SEL & 0x02) {
  126. if (p->SEL_1 == false) {
  127. puts("P1_SEL_1 = 1");
  128. }
  129. p->SEL_1 = true;
  130. }
  131. else {
  132. if (p->SEL_1 == true) {
  133. puts("P1_SEL_1 = 0");
  134. }
  135. p->SEL_1 = false;
  136. }
  137. // Check secondary select
  138. if (*p->SEL2 & 0x02) {
  139. if (p->SEL2_1 == false) {
  140. p->SEL2_1 = true;
  141. puts("P1_SEL2_1 = 1");
  142. }
  143. }
  144. else {
  145. if (p->SEL2_1 == true) {
  146. p->SEL2_1 = false;
  147. puts("P1_SEL2_1 = 0");
  148. }
  149. }
  150. //////////////////// P1.2 ////////////////////////
  151. if (*p->DIR & 0x04)
  152. {
  153. p->DIR_2 = true;
  154. if (*p->OUT & 0x04)
  155. {
  156. p->OUT_2 = true;
  157. }
  158. else
  159. {
  160. p->OUT_2 = false;
  161. }
  162. }
  163. else
  164. {
  165. p->DIR_2 = false;
  166. }
  167. if (*p->IE & 0x04)
  168. {
  169. p->IE_2 = true;
  170. if (*p->IFG & 0x04)
  171. {
  172. p->IFG_2 = true;
  173. }
  174. else
  175. {
  176. p->IFG_2 = false;
  177. }
  178. }
  179. else
  180. {
  181. p->IE_2 = false;
  182. }
  183. // Check primary select
  184. if (*p->SEL & 0x04)
  185. {
  186. if (p->SEL_2 == false)
  187. {
  188. puts("P1_SEL_2 = 1");
  189. }
  190. p->SEL_2 = true;
  191. }
  192. else
  193. {
  194. if (p->SEL_2 == true)
  195. {
  196. puts("P1_SEL_2 = 0");
  197. }
  198. p->SEL_2 = false;
  199. }
  200. // Check secondary select
  201. if (*p->SEL2 & 0x04) {
  202. if (p->SEL2_2 == false) {
  203. puts("P1_SEL2_2 = 1");
  204. }
  205. p->SEL2_2 = true;
  206. }
  207. else {
  208. if (p->SEL2_2 == true) {
  209. puts("P1_SEL2_2 = 0");
  210. }
  211. p->SEL2_2 = false;
  212. }
  213. ////////////////////////////////////////////////
  214. // Handler P1.3
  215. if (*p->DIR & 0x08) {
  216. p->DIR_3 = true;
  217. if (*p->OUT & 0x08) {
  218. p->OUT_3 = true;
  219. }
  220. else {
  221. p->OUT_3 = false;
  222. }
  223. }
  224. else {
  225. p->DIR_3 = false;
  226. }
  227. if (*p->IE & 0x08) {
  228. p->IE_3 = true;
  229. if (*p->IFG & 0x08) {
  230. p->IFG_3 = true;
  231. }
  232. else {
  233. p->IFG_3 = false;
  234. }
  235. }
  236. else {
  237. p->IE_3 = false;
  238. }
  239. ///////////////////////////////////////////////////////////////
  240. // Handler P1.4
  241. if (*p->DIR & 0x10) {
  242. p->DIR_4 = true;
  243. if (*p->OUT & 0x10) {
  244. p->OUT_4 = true;
  245. }
  246. else {
  247. p->OUT_4 = false;
  248. }
  249. }
  250. else {
  251. p->DIR_4 = false;
  252. }
  253. if (*p->IE & 0x10) {
  254. p->IE_4 = true;
  255. if (*p->IFG & 0x10) {
  256. p->IFG_4 = true;
  257. }
  258. else {
  259. p->IFG_4 = false;
  260. }
  261. }
  262. else {
  263. p->IE_4 = false;
  264. }
  265. /////////////////////////////////////////////////
  266. // Handler P1.5
  267. if (*p->DIR & 0x20) {
  268. p->DIR_5 = true;
  269. if (*p->OUT & 0x20) {
  270. p->OUT_5 = true;
  271. }
  272. else {
  273. p->OUT_5 = false;
  274. }
  275. }
  276. else {
  277. p->DIR_5 = false;
  278. }
  279. if (*p->IE & 0x20) {
  280. p->IE_5 = true;
  281. if (*p->IFG & 0x20) {
  282. p->IFG_5 = true;
  283. }
  284. else {
  285. p->IFG_5 = false;
  286. }
  287. }
  288. else {
  289. p->IE_5 = false;
  290. }
  291. ////////////////////////////////////////////////////
  292. // Handler P1.6
  293. if (*p->DIR & 0x40)
  294. {
  295. p->DIR_6 = true;
  296. if (*p->OUT & 0x40)
  297. {
  298. p->OUT_6 = true;
  299. }
  300. else
  301. {
  302. p->OUT_6 = false;
  303. }
  304. }
  305. else
  306. {
  307. p->DIR_6 = false;
  308. }
  309. if (*p->IE & 0x40)
  310. {
  311. p->IE_6 = true;
  312. if (*p->IFG & 0x40)
  313. {
  314. p->IFG_6 = true;
  315. }
  316. else
  317. {
  318. p->IFG_6 = false;
  319. }
  320. }
  321. else
  322. {
  323. p->IE_6 = false;
  324. }
  325. ////////////////////////////////////////////////////
  326. // Handler P1.7
  327. if (*p->DIR & 0x80) {
  328. p->DIR_7 = true;
  329. if (*p->OUT & 0x80) {
  330. p->OUT_7 = true;
  331. }
  332. else {
  333. p->OUT_7 = false;
  334. }
  335. }
  336. else {
  337. p->DIR_7 = false;
  338. }
  339. if (*p->IE & 0x80) {
  340. p->IE_7 = true;
  341. if (*p->IFG & 0x80) {
  342. p->IFG_7 = true;
  343. }
  344. else {
  345. p->IFG_7 = false;
  346. }
  347. }
  348. else {
  349. p->IE_7 = false;
  350. }
  351. }
  352. void setup_port_1 (Emulator *emu)
  353. {
  354. Cpu *cpu = emu->cpu;
  355. Port_1 *p = cpu->p1;
  356. static const uint16_t IN_VLOC = 0x20; // Input
  357. static const uint16_t OUT_VLOC = 0x21; // Output
  358. static const uint16_t DIR_VLOC = 0x22; // Direction
  359. static const uint16_t IFG_VLOC = 0x23; // Interrupt flag
  360. static const uint16_t IES_VLOC = 0x24; // Interrupt Edge Select
  361. static const uint16_t IE_VLOC = 0x25; // Interrupt Enable
  362. static const uint16_t SEL_VLOC = 0x26; // Select
  363. static const uint16_t SEL2_VLOC = 0x41; // Select 2
  364. static const uint16_t REN_VLOC = 0x27; // Resistor Enable
  365. *(p->IN = (uint8_t *) get_addr_ptr(IN_VLOC)) = 0;
  366. *(p->OUT = (uint8_t *) get_addr_ptr(OUT_VLOC)) = 0;
  367. *(p->DIR = (uint8_t *) get_addr_ptr(DIR_VLOC)) = 0;
  368. *(p->IFG = (uint8_t *) get_addr_ptr(IFG_VLOC)) = 0;
  369. *(p->IES = (uint8_t *) get_addr_ptr(IES_VLOC)) = 0;
  370. *(p->IE = (uint8_t *) get_addr_ptr(IE_VLOC)) = 0;
  371. *(p->SEL = (uint8_t *) get_addr_ptr(SEL_VLOC)) = 0;
  372. *(p->SEL2 = (uint8_t *) get_addr_ptr(SEL2_VLOC)) = 0;
  373. *(p->REN = (uint8_t *) get_addr_ptr(REN_VLOC)) = 0;
  374. p->DIR_0 = false; p->OUT_0 = false; p->IFG_0 = false;
  375. p->IE_0 = false; p->SEL_0 = false; p->SEL2_0 = false;
  376. p->DIR_1 = false; p->OUT_1 = false; p->IFG_1 = false;
  377. p->IE_1 = false; p->SEL_1 = false; p->SEL2_1 = false;
  378. p->DIR_2 = false; p->OUT_2 = false; p->IFG_2 = false;
  379. p->IE_2 = false; p->SEL_2 = false; p->SEL2_2 = false;
  380. p->DIR_3 = false; p->OUT_3 = false; p->IFG_3 = false;
  381. p->IE_3 = false; p->SEL_3 = false; p->SEL2_3 = false;
  382. p->DIR_4 = false; p->OUT_4 = false; p->IFG_4 = false;
  383. p->IE_4 = false; p->SEL_4 = false; p->SEL2_4 = false;
  384. p->DIR_5 = false; p->OUT_5 = false; p->IFG_5 = false;
  385. p->IE_5 = false; p->SEL_5 = false; p->SEL2_5 = false;
  386. p->DIR_6 = false; p->OUT_6 = false; p->IFG_6 = false;
  387. p->IE_6 = false; p->SEL_6 = false; p->SEL2_6 = false;
  388. p->DIR_7 = false; p->OUT_7 = false; p->IFG_7 = false;
  389. p->IE_7 = false; p->SEL_7 = false; p->SEL2_7 = false;
  390. }
  391. /* POWER UP CLEAR (PUC)
  392. *
  393. * A PUC is always generated when a POR is generated, but a POR is not
  394. * generated by a PUC. The following events trigger a PUC:
  395. *
  396. * A POR signal
  397. * Watchdog timer expiration when in watchdog mode only
  398. * Watchdog timer security key violation
  399. * A Flash memory security key violation
  400. * A CPU instruct fetch from the peripheral address range 0h to 01FFh
  401. void power_up_clear () {
  402. *P1OUT = *P1DIR = *P1IFG = *P1IE = *P1SEL = *P1SEL2 = *P1REN = 0;
  403. }
  404. */