utilities.c 11 KB

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