utilities.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 "utilities.h"
  16. extern uint8_t* MEMSPACE;
  17. /**
  18. * @brief This function loads the default TI bootloader code into virtual mem
  19. * @param virt_addr The location in virtual memory to load the bootloader
  20. */
  21. void load_bootloader(uint16_t virt_addr)
  22. {
  23. uint16_t *real_addr = get_addr_ptr(virt_addr);
  24. memmove(real_addr, blc, sizeof blc);
  25. printf("Loaded booloader code into address 0x%04X\n", virt_addr);
  26. }
  27. /**
  28. * @brief This function loads firmware from a binary file on disk into the
  29. * virtual memory of the emulated device at base virt_loc
  30. * @param file_name The file name of the binary to load into virtual memory
  31. * @param virt_loc The location in virtual memory to load the firmware
  32. */
  33. int load_firmware(Emulator *emu, char *file_name, uint16_t virt_addr)
  34. {
  35. uint32_t size, result;
  36. char str[100] = {0};
  37. sprintf(str, "Loading firmware: ( %s )\n", file_name);
  38. printf("%s", str);
  39. print_console(emu, str);
  40. FILE *fd = fopen(file_name, "rb+");
  41. if (fd == NULL)
  42. {
  43. printf("Could not open %s, exiting.\n", file_name);
  44. return 1;
  45. }
  46. /* obtain file size */
  47. fseek(fd, 0, SEEK_END);
  48. size = ftell(fd);
  49. rewind(fd);
  50. // check size
  51. if (size > (0x10000 - 0x0C000))
  52. {
  53. printf("SizeTooBig\n");
  54. print_console(emu, "Flash Size too small to fit your binary. Quitting, please refresh to try again. Ensure you are compiling for the right MSP version.\n");
  55. usleep(20000);
  56. return 1;
  57. }
  58. uint16_t *real_addr = get_addr_ptr(virt_addr);
  59. result = fread(real_addr, 1, size, fd);
  60. sprintf(str, "Placed %d bytes into flash\n\n", result);
  61. printf("%s", str);
  62. print_console(emu, str);
  63. fclose(fd);
  64. }
  65. uint16_t *get_stack_ptr(Emulator *emu)
  66. {
  67. Cpu *cpu = emu->cpu;
  68. return (uint16_t *) (MEMSPACE + cpu->sp);
  69. }
  70. /**
  71. * @brief Get the host's pointer to the virtual address of the guest
  72. * @param virt_addr The virtual address of the guest to translate to a useable
  73. * one in context of the host
  74. * @return Pointer to the host's location of the guest's memory address
  75. */
  76. uint16_t *get_addr_ptr(uint16_t virt_addr)
  77. {
  78. return (uint16_t *) (MEMSPACE + virt_addr);
  79. }
  80. /**
  81. * @brief Get a pointer to the register specified by the numeric register value
  82. * @param cpu A pointer to the CPU structure
  83. * @param reg The numeric value of the register
  84. * @return Pointer to the register in question, NULL if register doesn't exist
  85. */
  86. int16_t *get_reg_ptr(Emulator *emu, uint8_t reg)
  87. {
  88. Cpu *cpu = emu->cpu;
  89. static int16_t r2 = 0;
  90. switch (reg)
  91. {
  92. case 0x0: return (int16_t *) &cpu->pc;
  93. case 0x1: return (int16_t *) &cpu->sp;
  94. case 0x2:
  95. {
  96. r2 = sr_to_value(emu);
  97. return &r2;
  98. }
  99. case 0x3: return &cpu->cg2;
  100. case 0x4: return &cpu->r4;
  101. case 0x5: return &cpu->r5;
  102. case 0x6: return &cpu->r6;
  103. case 0x7: return &cpu->r7;
  104. case 0x8: return &cpu->r8;
  105. case 0x9: return &cpu->r9;
  106. case 0xA: return &cpu->r10;
  107. case 0xB: return &cpu->r11;
  108. case 0xC: return &cpu->r12;
  109. case 0xD: return &cpu->r13;
  110. case 0xE: return &cpu->r14;
  111. case 0xF: return &cpu->r15;
  112. default:
  113. {
  114. puts("Invalid Register Number");
  115. return 0;
  116. }
  117. }
  118. }
  119. /**
  120. * @brief Convert register ASCII name to it's respective numeric value
  121. * @param name The register's ASCII name
  122. * @return The numeric equivalent for the register on success, -1 if an
  123. * invalid name was supplied
  124. */
  125. int8_t reg_name_to_num(char *name)
  126. {
  127. if ( !strncasecmp("%r0", name, sizeof "%r0") ||
  128. !strncasecmp("r0", name, sizeof "r0") ||
  129. !strncasecmp("%pc", name, sizeof "%pc") ||
  130. !strncasecmp("pc", name, sizeof "pc") )
  131. {
  132. return 0;
  133. }
  134. else if ( !strncasecmp("%r1", name, sizeof "%r1") ||
  135. !strncasecmp("r1", name, sizeof "r1") ||
  136. !strncasecmp("%sp", name, sizeof "%sp") ||
  137. !strncasecmp("sp", name, sizeof "sp") )
  138. {
  139. return 1;
  140. }
  141. else if ( !strncasecmp("%r2", name, sizeof "%r2") ||
  142. !strncasecmp("r2", name, sizeof "r2") ||
  143. !strncasecmp("%sr", name, sizeof "%sr") ||
  144. !strncasecmp("sr", name, sizeof "sr") ) {
  145. return 2;
  146. }
  147. else if ( !strncasecmp("%r3", name, sizeof "%r3") ||
  148. !strncasecmp("r3", name, sizeof "r3") ||
  149. !strncasecmp("%cg2", name, sizeof "%cg2") ||
  150. !strncasecmp("cg2", name, sizeof "cg2") ) {
  151. return 3;
  152. }
  153. else if ( !strncasecmp("%r4", name, sizeof "%r4") ||
  154. !strncasecmp("r4", name, sizeof "r4") ) {
  155. return 4;
  156. }
  157. else if ( !strncasecmp("%r5", name, sizeof "%r5") ||
  158. !strncasecmp("r5", name, sizeof "r5") ) {
  159. return 5;
  160. }
  161. else if ( !strncasecmp("%r6", name, sizeof "%r6") ||
  162. !strncasecmp("r6", name, sizeof "r6") ) {
  163. return 6;
  164. }
  165. else if ( !strncasecmp("%r7", name, sizeof "%r7") ||
  166. !strncasecmp("r7", name, sizeof "r7") ) {
  167. return 7;
  168. }
  169. else if ( !strncasecmp("%r8", name, sizeof "%r8") ||
  170. !strncasecmp("r8", name, sizeof "r8") ) {
  171. return 8;
  172. }
  173. else if ( !strncasecmp("%r9", name, sizeof "%r9") ||
  174. !strncasecmp("r9", name, sizeof "r9") ) {
  175. return 9;
  176. }
  177. else if ( !strncasecmp("%r10", name, sizeof "%r10") ||
  178. !strncasecmp("r10", name, sizeof "r10") ) {
  179. return 10;
  180. }
  181. else if ( !strncasecmp("%r11", name, sizeof "%r11") ||
  182. !strncasecmp("r11", name, sizeof "r11") ) {
  183. return 11;
  184. }
  185. else if ( !strncasecmp("%r12", name, sizeof "%r12") ||
  186. !strncasecmp("r12", name, sizeof "r12") ) {
  187. return 12;
  188. }
  189. else if ( !strncasecmp("%r13", name, sizeof "%r13") ||
  190. !strncasecmp("r13", name, sizeof "r13") ) {
  191. return 13;
  192. }
  193. else if ( !strncasecmp("%r14", name, sizeof "%r14") ||
  194. !strncasecmp("r14", name, sizeof "r14") ) {
  195. return 14;
  196. }
  197. else if ( !strncasecmp("%r15", name, sizeof "%r15") ||
  198. !strncasecmp("r15", name, sizeof "r15") ) {
  199. return 15;
  200. }
  201. return -1;
  202. }
  203. /**
  204. * @brief Convert register number into its ASCII name
  205. * @param number The register number (0, 1, 2, ...) associated with a
  206. * register's name like (R0, R1, R2, ...)
  207. * @param name A pointer to an allocated character array to fill up with
  208. * the register's ASCII name
  209. */
  210. void reg_num_to_name(uint8_t number, char *name)
  211. {
  212. switch (number)
  213. {
  214. case 0x0:
  215. {
  216. strncpy(name, "PC\0", 3);
  217. return;
  218. }
  219. case 0x1:
  220. {
  221. strncpy(name, "SP\0", 3);
  222. return;
  223. }
  224. case 0x2:
  225. {
  226. strncpy(name, "SR\0", 3);
  227. return;
  228. }
  229. case 0x3:
  230. {
  231. strncpy(name, "R3\0", 3);
  232. return;
  233. }
  234. case 0x4:
  235. {
  236. strncpy(name, "R4\0", 3);
  237. return;
  238. }
  239. case 0x5:
  240. {
  241. strncpy(name, "R5\0", 3);
  242. return;
  243. }
  244. case 0x6:
  245. {
  246. strncpy(name, "R6\0", 3);
  247. return;
  248. }
  249. case 0x7:
  250. {
  251. strncpy(name, "R7\0", 3);
  252. return;
  253. }
  254. case 0x8:
  255. {
  256. strncpy(name, "R8\0", 3);
  257. return;
  258. }
  259. case 0x9:
  260. {
  261. strncpy(name, "R9\0", 3);
  262. return;
  263. }
  264. case 0xA:
  265. {
  266. strncpy(name, "R10\0", 4);
  267. return;
  268. }
  269. case 0xB:
  270. {
  271. strncpy(name, "R11\0", 4);
  272. return;
  273. }
  274. case 0xC:
  275. {
  276. strncpy(name, "R12\0", 4);
  277. return;
  278. }
  279. case 0xD:
  280. {
  281. strncpy(name, "R13\0", 4);
  282. return;
  283. }
  284. case 0xE:
  285. {
  286. strncpy(name, "R14\0", 4);
  287. return;
  288. }
  289. case 0xF:
  290. {
  291. strncpy(name, "R15\0", 4);
  292. return;
  293. }
  294. default:
  295. {
  296. strncpy(name, "???\0", 4);
  297. return;
  298. }
  299. }
  300. }
  301. /**
  302. * @brief This function displays the help menu to the user if he (or - but in
  303. * practice all too seldom - she) enters incorrect parameters or prompts the
  304. * help menu with "help" or "h"
  305. */
  306. const char* LocalHelpStr =
  307. "**************************************************\n"\
  308. "*\t\tMSP430-Emulator\n*\n*\tUsage: ./msp430 BINARY_FIRMWARE\n*\n"\
  309. "* run\t\t\t[Run Program Until Breakpoint is Hit]\n"\
  310. "* step [N]\t\t[Step Into Instruction]\n"\
  311. "* dump [HEX_ADDR|Rn]\t[Dump Memory direct or at register value]\n"\
  312. "* set [HEX_ADDR|Rn]\t[Set Memory or Register Location]\n"\
  313. "* dis [N][HEX_ADDR]\t[Disassemble Instructions]\n"\
  314. "* break ADDR\t\t[Set a Breakpoint]\n"\
  315. "* bps\t\t\t[Display Breakpoints]\n"\
  316. "* regs\t\t\t[Display Registers]\n"\
  317. "* CTRL+C\t\t[Pause Execution]\n"\
  318. "* reset\t\t\t[Reset Machine]\n"\
  319. "* quit\t\t\t[Exit program]\n"\
  320. "**************************************************\n";
  321. const char* WebHelpStr =
  322. "--------------------------------------------------\n"\
  323. " \t\t[MSP430-Emulator]\n"\
  324. " run\t\t\t[Run Program Until Breakpoint is Hit]\n"\
  325. " step [N]\t\t[Step Into Instruction]\n"\
  326. " dump [HEX_ADDR|Rn]\t[Dump Memory direct or at register value]\n"\
  327. " set [HEX_ADDR|Rn]\t[Set Memory or Register Location]\n"\
  328. " dis [N][HEX_ADDR]\t[Disassemble Instructions]\n"\
  329. " break ADDR\t\t[Set a Breakpoint]\n"\
  330. " bps\t\t\t[Display Breakpoints]\n"\
  331. " regs\t\t\t[Display Registers]\n"\
  332. " reset\t\t\t[Reset Machine]\n"\
  333. " quit\t\t\t[Exit program]\n"\
  334. "--------------------------------------------------\n";
  335. void display_help(Emulator *emu)
  336. {
  337. Debugger *deb = emu->debugger;
  338. if (deb->web_interface)
  339. {
  340. print_console(emu, WebHelpStr);
  341. }
  342. else
  343. {
  344. puts(LocalHelpStr);
  345. }
  346. }