shell.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /*
  2. * =====================================================================================
  3. *
  4. * ________ .__ __ ________ ____ ________
  5. * \_____ \ __ __|__| ____ | | __\______ \ _______ _/_ |/ _____/
  6. * / / \ \| | \ |/ ___\| |/ / | | \_/ __ \ \/ /| / __ \
  7. * / \_/. \ | / \ \___| < | ` \ ___/\ / | \ |__\ \
  8. * \_____\ \_/____/|__|\___ >__|_ \/_______ /\___ >\_/ |___|\_____ /
  9. * \__> \/ \/ \/ \/ \/
  10. *
  11. * www.optixx.org
  12. *
  13. *
  14. * Version: 1.0
  15. * Created: 07/21/2009 03:32:16 PM
  16. * Author: david@optixx.org
  17. *
  18. * =====================================================================================
  19. */
  20. #include <stdint.h>
  21. #include <string.h>
  22. #include <avr/io.h>
  23. #include <stdlib.h>
  24. #include <avr/interrupt.h>
  25. #include <util/delay.h>
  26. #include <avr/pgmspace.h>
  27. #include <avr/eeprom.h>
  28. #include "pwm.h"
  29. #include "debug.h"
  30. #include "info.h"
  31. #include "sram.h"
  32. #include "util.h"
  33. #include "uart.h"
  34. #include "dump.h"
  35. #include "irq.h"
  36. #include "config.h"
  37. #include "crc.h"
  38. #include "command.h"
  39. #include "shared_memory.h"
  40. #include "system.h"
  41. extern system_t system;
  42. uint8_t command_buf[RECEIVE_BUF_LEN];
  43. uint8_t recv_buf[RECEIVE_BUF_LEN];
  44. volatile uint8_t recv_counter = 0;
  45. volatile uint8_t cr = 0;
  46. uint8_t *token_ptr;
  47. uint8_t *get_token(void)
  48. {
  49. uint8_t *p = token_ptr;
  50. while (*p == ' ')
  51. p++;
  52. if (*p == '\0')
  53. return NULL;
  54. token_ptr = p;
  55. do {
  56. token_ptr++;
  57. if (*token_ptr == ' ' || *token_ptr == '\n' || *token_ptr == '\r') {
  58. *token_ptr++ = '\0';
  59. break;
  60. }
  61. } while (*token_ptr != ' ' && *token_ptr != '\n' && *token_ptr != '\r');
  62. return p;
  63. }
  64. uint8_t get_dec(uint32_t *decval)
  65. {
  66. const uint8_t *t;
  67. t = get_token();
  68. if (t != NULL) {
  69. int x = util_sscandec(t);
  70. if (x < 0)
  71. return 0;
  72. *decval = x;
  73. return 1;
  74. }
  75. return 0;
  76. }
  77. uint8_t parse_hex(const uint8_t *s, uint32_t *hexval)
  78. {
  79. uint32_t x = util_sscanhex(s);
  80. *hexval = (uint32_t) x;
  81. return 1;
  82. }
  83. uint8_t get_hex(uint32_t *hexval)
  84. {
  85. const uint8_t *t;
  86. t = get_token();
  87. if (t != NULL)
  88. return parse_hex(t, hexval);
  89. return 0;
  90. }
  91. uint8_t get_hex_arg2(uint32_t *hexval1, uint32_t *hexval2)
  92. {
  93. return get_hex(hexval1) && get_hex(hexval2);
  94. }
  95. uint8_t get_hex_arg3(uint32_t *hexval1, uint32_t *hexval2, uint32_t *hexval3)
  96. {
  97. return get_hex(hexval1) && get_hex(hexval2) && get_hex(hexval3);
  98. }
  99. static uint8_t get_int32(uint32_t *val)
  100. {
  101. if (!get_hex(val)){
  102. info_P(PSTR("Invalid argument!\n"));
  103. return 0;
  104. } else {
  105. return 1;
  106. }
  107. }
  108. static uint8_t get_int8(uint8_t *val)
  109. {
  110. uint32_t ret;
  111. if (!get_hex(&ret) ||ret > 0xff){
  112. info_P(PSTR("Invalid argument!\n"));
  113. return 0;
  114. }else{
  115. *val = (uint8_t)ret;
  116. return 1;
  117. }
  118. }
  119. static int get_bool(void)
  120. {
  121. const uint8_t *t;
  122. t = get_token();
  123. if (t != NULL) {
  124. int result = util_sscanbool(t);
  125. if (result >= 0)
  126. return result;
  127. }
  128. info_P(PSTR("Invalid argument (should be 0 or 1)!\n"));
  129. return -1;
  130. }
  131. void prompt(void){
  132. uart_putc('\r');
  133. uart_putc('\n');
  134. uart_putc('>');
  135. }
  136. ISR(USART0_RX_vect) // Interrupt for UART Byte received
  137. {
  138. UCSR0B &= (255 - (1<<RXCIE0));// Interrupts disable for RxD
  139. sei();
  140. if(recv_counter == (sizeof(recv_buf)-1)) {
  141. cr=1;
  142. recv_buf[recv_counter]='\0';
  143. recv_counter=0;
  144. prompt();
  145. }
  146. recv_buf[recv_counter] = UDR0;
  147. uart_putc(recv_buf[recv_counter]); /* do a echo, maybe should reside not in interrupt */
  148. if (recv_buf[recv_counter] == 0x0d) {
  149. /* recv_buf[recv_counter] = 0; */
  150. cr = 1; // found a CR, so the application should do something
  151. recv_buf[++recv_counter]='\0'; // terminate string
  152. recv_counter = 0;
  153. prompt();
  154. } else {
  155. // we accept backspace or delete
  156. if ((recv_buf[recv_counter] == 0x08 || recv_buf[recv_counter] == 0x7f) && recv_counter > 0) {
  157. recv_counter--;
  158. } else {
  159. recv_counter++;
  160. }
  161. }
  162. UCSR0B |= (1<<RXCIE0);// Interrupts enable for RxD
  163. }
  164. enum cmds {
  165. CMD_DUMP,
  166. CMD_DUMPVEC,
  167. CMD_DUMPHEADER,
  168. CMD_CRC,
  169. CMD_EXIT,
  170. CMD_RESET,
  171. CMD_RESETSNIFF,
  172. CMD_IRQ,
  173. CMD_AVR,
  174. CMD_SNES,
  175. CMD_LOROM,
  176. CMD_HIROM,
  177. CMD_WR,
  178. CMD_SHMWR,
  179. CMD_SHMSAVE,
  180. CMD_SHMRESTORE,
  181. CMD_LOADER,
  182. CMD_RECONNECT,
  183. CMD_STATUS,
  184. CMD_SYS,
  185. CMD_HELP
  186. };
  187. uint8_t cmdlist[][CMD_HELP] PROGMEM = {
  188. {"DUMP"},
  189. {"DUMPVEC"},
  190. {"DUMPHEADER"},
  191. {"CRC"},
  192. {"EXIT"},
  193. {"RESET"},
  194. {"RESETSNIFF"},
  195. {"IRQ"},
  196. {"AVR"},
  197. {"SNES"},
  198. {"LOROM"},
  199. {"HIROM"},
  200. {"WR"},
  201. {"SHMWR"},
  202. {"SHMSAVE"},
  203. {"SHMRESTORE"},
  204. {"LOADER"},
  205. {"RECONNECT"},
  206. {"STATUS"},
  207. {"SYS"},
  208. {"HELP"},
  209. };
  210. void shell_help(void){
  211. uint8_t i;
  212. info_P(PSTR("\n"));
  213. for (i=CMD_DUMP; i<CMD_HELP; i++){
  214. info_P((PGM_P)cmdlist[i]);
  215. info_P(PSTR("\n"));
  216. }
  217. }
  218. void shell_run(void)
  219. {
  220. uint8_t *t;
  221. uint32_t arg1;
  222. uint32_t arg2;
  223. uint32_t arg3;
  224. uint16_t crc;
  225. uint16_t offset;
  226. uint16_t i;
  227. uint8_t c;
  228. if (!cr)
  229. return;
  230. cr=0;
  231. strcpy((char*)command_buf, (char*)recv_buf);
  232. token_ptr = command_buf;
  233. t = get_token();
  234. if (t == NULL)
  235. shell_help();
  236. util_strupper(t);
  237. if (strcmp_P((const char*)t,(PGM_P)cmdlist[CMD_DUMP]) == 0) {
  238. if (get_hex_arg2(&arg1,&arg2))
  239. dump_memory(arg1,arg2);
  240. else
  241. info_P(PSTR("DUMP <start addr> <end addr>\n"));
  242. }else if (strcmp_P((char*)t, (PGM_P)cmdlist[CMD_CRC]) == 0) {
  243. if (get_hex_arg2(&arg1,&arg2)){
  244. crc = crc_check_bulk_memory(arg1,arg2,0x8000);
  245. info_P(PSTR("0x%06lx - 0x%06lx crc=0x%04x\n"),arg1,arg2,crc);
  246. } else
  247. info_P(PSTR("CRC <start addr> <end addr>\n"));
  248. }else if (strcmp_P((char*)t, (PGM_P)cmdlist[CMD_EXIT]) == 0) {
  249. leave_application();
  250. }else if (strcmp_P((char*)t, (PGM_P)cmdlist[CMD_RESET]) == 0) {
  251. system_send_snes_reset();
  252. }else if (strcmp_P((char*)t, (PGM_P)cmdlist[CMD_IRQ]) == 0) {
  253. info_P(PSTR("Send IRQ\n"));
  254. snes_irq_on();
  255. snes_irq_lo();
  256. _delay_us(20);
  257. snes_irq_hi();
  258. snes_irq_off();
  259. }else if (strcmp_P((char*)t, (PGM_P)cmdlist[CMD_AVR]) == 0) {
  260. //info_P(PSTR("Activate AVR bus\n"));
  261. //avr_bus_active();
  262. //snes_irq_lo();
  263. //snes_irq_off();
  264. system_set_bus_avr();
  265. snes_irq_lo();
  266. system_snes_irq_off();
  267. }else if (strcmp_P((char*)t, (PGM_P)cmdlist[CMD_SNES]) == 0) {
  268. //info_P(PSTR("Activate SNES bus\n"));
  269. //snes_irq_lo();
  270. //snes_irq_off();
  271. //snes_wr_disable();
  272. //snes_bus_active();
  273. snes_irq_lo();
  274. system_snes_irq_off();
  275. system_set_wr_disable();
  276. system_set_bus_snes();
  277. }else if (strcmp_P((char*)t, (PGM_P)cmdlist[CMD_LOROM]) == 0) {
  278. //info_P(PSTR("Set LOROM\n"));
  279. //snes_lorom();
  280. //snes_wr_disable();
  281. system_set_rom_lorom();
  282. system_set_wr_disable();
  283. }else if (strcmp_P((char*)t, (PGM_P)cmdlist[CMD_HIROM]) == 0) {
  284. //info_P(PSTR("Set HIROM\n"));
  285. //snes_hirom();
  286. //snes_wr_disable();
  287. system_set_rom_hirom();
  288. system_set_wr_disable();
  289. }else if (strcmp_P((char*)t, (PGM_P)cmdlist[CMD_WR]) == 0) {
  290. arg1 = get_bool();
  291. if(arg1==1){
  292. info_P(PSTR("Set WR enable"));
  293. snes_wr_enable();
  294. }else if (arg1==0){
  295. info_P(PSTR("Set WR disable"));
  296. snes_wr_disable();
  297. }
  298. }else if (strcmp_P((char*)t, (PGM_P)cmdlist[CMD_RESETSNIFF]) == 0) {
  299. arg1 = get_bool();
  300. if(arg1==1){
  301. info_P(PSTR("Start Reset sniffer"));
  302. irq_init();
  303. }else if (arg1==0){
  304. info_P(PSTR("Stop Reset sniffer"));
  305. irq_stop();
  306. }
  307. }else if (strcmp_P((char*)t, (PGM_P)cmdlist[CMD_DUMPVEC]) == 0) {
  308. uint16_t offset;
  309. if (system.rom_mode==LOROM)
  310. offset = 0x8000;
  311. else
  312. offset = 0x0000;
  313. info_P(PSTR("ABORT 0x%04x 0x%04x\n"), (0xFFE8 - offset),sram_read16_be(0xFFE8 - offset));
  314. info_P(PSTR("BRK 0x%04x 0x%04x\n"), (0xFFE6 - offset),sram_read16_be(0xFFE6 - offset));
  315. info_P(PSTR("COP 0x%04x 0x%04x\n"), (0xFFE4 - offset),sram_read16_be(0xFFE4 - offset));
  316. info_P(PSTR("IRQ 0x%04x 0x%04x\n"), (0xFFEE - offset),sram_read16_be(0xFFEE - offset));
  317. info_P(PSTR("NMI 0x%04x 0x%04x\n"), (0xFFEA - offset),sram_read16_be(0xFFEA - offset));
  318. info_P(PSTR("RES 0x%04x 0x%04x\n"), (0xFFFC - offset),sram_read16_be(0xFFFC - offset));
  319. }else if (strcmp_P((char*)t, (PGM_P)cmdlist[CMD_DUMPHEADER]) == 0) {
  320. if (system.rom_mode==LOROM)
  321. offset = 0x8000;
  322. else
  323. offset = 0x0000;
  324. /*
  325. # $ffc0..$ffd4 => Name of the ROM, typically in ASCII, using spaces to pad the name to 21 bytes.
  326. # $ffd5 => ROM layout, typically $20 for LoROM, or $21 for HiROM. Add $10 for FastROM.
  327. # $ffd6 => Cartridge type, typically $00 for ROM only, or $02 for ROM with save-RAM.
  328. # $ffd7 => ROM size byte.
  329. # $ffd8 => RAM size byte.
  330. # $ffd9 => Country code, which selects the video in the emulator. Values $00, $01, $0d use NTSC. Values in range $02..$0c use PAL. Other values are invalid.
  331. # $ffda => Licensee code. If this value is $33, then the ROM has an extended header with ID at $ffb2..$ffb5.
  332. # $ffdb => Version number, typically $00.
  333. # $ffdc..$ffdd => Checksum complement, which is the bitwise-xor of the checksum and $ffff.
  334. # $ffde..$ffdf => SNES checksum, an unsigned 16-bit checksum of bytes.
  335. # $ffe0..$ffe3 => Unknown.
  336. */
  337. info_P(PSTR("NAME 0x%04x "), (0xffc0 - offset));
  338. for(arg1=(0xffc0 - offset); arg1<(0xffc0 - offset + 21);arg1++){
  339. c = sram_read(arg1);
  340. if (c>0x1f && c<0x7f)
  341. printf("%c",c);
  342. }
  343. printf("\n");
  344. info_P(PSTR("LAYOUT 0x%04x 0x%02x\n"), (0xffd5 - offset),sram_read(0xffd5 - offset));
  345. info_P(PSTR("TYPE 0x%04x 0x%02x\n"), (0xffd6 - offset),sram_read(0xffd6 - offset));
  346. info_P(PSTR("ROM 0x%04x 0x%02x\n"), (0xffd7 - offset),sram_read(0xffd7 - offset));
  347. info_P(PSTR("RAM 0x%04x 0x%02x\n"), (0xffd8 - offset),sram_read(0xffd8 - offset));
  348. info_P(PSTR("CCODE 0x%04x 0x%02x\n"), (0xffd9 - offset),sram_read(0xffd9 - offset));
  349. info_P(PSTR("LIC 0x%04x 0x%02x\n"), (0xffda - offset),sram_read(0xffda - offset));
  350. info_P(PSTR("VER 0x%04x 0x%02x\n"), (0xffdb - offset),sram_read(0xffdb - offset));
  351. info_P(PSTR("SUM1 0x%04x 0x%04x\n"), (0xffdc - offset),sram_read16_be(0xffdc - offset));
  352. info_P(PSTR("SUM2 0x%04x 0x%04x\n"), (0xffde - offset),sram_read16_be(0xffde - offset));
  353. }else if (strcmp_P((char*)t, (PGM_P)cmdlist[CMD_SHMWR]) == 0) {
  354. if (get_hex_arg2(&arg1,&arg2))
  355. shared_memory_write((uint8_t)arg1, (uint8_t)arg1);
  356. else
  357. info_P(PSTR("SHMWR <command> <value>\n"));
  358. }else if (strcmp_P((char*)t, (PGM_P)cmdlist[CMD_SHMSAVE]) == 0) {
  359. shared_memory_scratchpad_region_tx_save();
  360. shared_memory_scratchpad_region_rx_save();
  361. info_P(PSTR("Save scratchpad\n"));
  362. }else if (strcmp_P((char*)t, (PGM_P)cmdlist[CMD_SHMRESTORE]) == 0) {
  363. shared_memory_scratchpad_region_tx_restore();
  364. shared_memory_scratchpad_region_rx_restore();
  365. info_P(PSTR("Restore scratchpad\n"));
  366. }else if (strcmp_P((char*)t, (PGM_P)cmdlist[CMD_LOADER]) == 0) {
  367. boot_startup_rom(500);
  368. }else if (strcmp_P((char*)t, (PGM_P)cmdlist[CMD_RECONNECT]) == 0) {
  369. usb_connect();
  370. }else if (strcmp_P((char*)t, (PGM_P)cmdlist[CMD_STATUS]) == 0) {
  371. transaction_status();
  372. }else if (strcmp_P((char*)t, (PGM_P)cmdlist[CMD_SYS]) == 0) {
  373. system_status();
  374. }else if (strcmp_P((char*)t, (PGM_P)cmdlist[CMD_HELP]) == 0) {
  375. shell_help();
  376. }
  377. prompt();
  378. }