sim_console_c.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. #include <string.h>
  6. #include <termios.h>
  7. #include <unistd.h>
  8. #include <poll.h>
  9. #include "sim_vhpi_c.h"
  10. /* Should we exit simulation on ctrl-c or pass it through? */
  11. #define EXIT_ON_CTRL_C
  12. static struct termios oldt;
  13. static void disable_raw_mode(void)
  14. {
  15. tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  16. }
  17. static void enable_raw_mode(void)
  18. {
  19. static bool initialized = false;
  20. if (!initialized) {
  21. static struct termios newt;
  22. tcgetattr(STDIN_FILENO, &oldt);
  23. newt = oldt;
  24. cfmakeraw(&newt);
  25. #ifdef EXIT_ON_CTRL_C
  26. newt.c_lflag |= ISIG;
  27. #endif
  28. tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  29. initialized = true;
  30. atexit(disable_raw_mode);
  31. }
  32. }
  33. void sim_console_read(unsigned char *__rt)
  34. {
  35. int ret;
  36. unsigned long val = 0;
  37. enable_raw_mode();
  38. ret = read(STDIN_FILENO, &val, 1);
  39. if (ret != 1) {
  40. fprintf(stderr, "%s: read of stdin returns %d\n", __func__, ret);
  41. exit(1);
  42. }
  43. //fprintf(stderr, "read returns %c\n", val);
  44. to_std_logic_vector(val, __rt, 64);
  45. }
  46. void sim_console_poll(unsigned char *__rt)
  47. {
  48. int ret;
  49. struct pollfd fdset[1];
  50. uint8_t val = 0;
  51. enable_raw_mode();
  52. memset(fdset, 0, sizeof(fdset));
  53. fdset[0].fd = STDIN_FILENO;
  54. fdset[0].events = POLLIN;
  55. ret = poll(fdset, 1, 0);
  56. //fprintf(stderr, "poll returns %d\n", ret);
  57. if (ret == 1) {
  58. if (fdset[0].revents & POLLIN)
  59. val = 1;
  60. // fprintf(stderr, "poll revents: 0x%x\n", fdset[0].revents);
  61. }
  62. to_std_logic_vector(val, __rt, 64);
  63. }
  64. void sim_console_write(unsigned char *__rs)
  65. {
  66. uint8_t val;
  67. val = from_std_logic_vector(__rs, 64);
  68. fprintf(stderr, "%c", val);
  69. }