port1.c 10 KB

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