vpi_pty.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include <vpi_user.h>
  2. #include <pty.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <termios.h>
  8. static int master_fd = -1;
  9. static int slave_fd = -1;
  10. static int pty_init_calltf(char *user_data) {
  11. vpiHandle systfref = vpi_handle(vpiSysTfCall, NULL);
  12. vpiHandle args_iter = vpi_iterate(vpiArgument, systfref);
  13. vpiHandle arg;
  14. char slave_path_file[256] = "/workspace/tmp/mrdimm_pty_slave";
  15. if (args_iter) {
  16. arg = vpi_scan(args_iter);
  17. if (arg) {
  18. s_vpi_value val;
  19. val.format = vpiStringVal;
  20. vpi_get_value(arg, &val);
  21. strncpy(slave_path_file, val.value.str, sizeof(slave_path_file) - 1);
  22. }
  23. vpi_free_object(args_iter);
  24. }
  25. struct termios tio;
  26. if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) < 0) {
  27. vpi_printf("FATAL: openpty failed\n");
  28. return -1;
  29. }
  30. // Start from the PTY settings before changing its raw-mode flags.
  31. if (tcgetattr(slave_fd, &tio) < 0) {
  32. vpi_printf("FATAL: tcgetattr failed\n");
  33. close(master_fd);
  34. close(slave_fd);
  35. master_fd = slave_fd = -1;
  36. return -1;
  37. }
  38. // Configure raw mode (no echo, no translation)
  39. cfmakeraw(&tio);
  40. tcsetattr(master_fd, TCSANOW, &tio);
  41. tcsetattr(slave_fd, TCSANOW, &tio);
  42. // Set non-blocking on master_fd
  43. int flags = fcntl(master_fd, F_GETFL, 0);
  44. fcntl(master_fd, F_SETFL, flags | O_NONBLOCK);
  45. char *sname = ttyname(slave_fd);
  46. vpi_printf("[VPI_PTY] Opened PTY: slave=%s\n", sname);
  47. FILE *fp = fopen(slave_path_file, "w");
  48. if (fp) {
  49. fprintf(fp, "%s\n", sname);
  50. fclose(fp);
  51. }
  52. return 0;
  53. }
  54. static int pty_poll_read_calltf(char *user_data) {
  55. vpiHandle systfref = vpi_handle(vpiSysTfCall, NULL);
  56. vpiHandle args_iter = vpi_iterate(vpiArgument, systfref);
  57. if (!args_iter) return 0;
  58. vpiHandle byte_arg = vpi_scan(args_iter);
  59. vpiHandle valid_arg = vpi_scan(args_iter);
  60. vpi_free_object(args_iter);
  61. unsigned char buf = 0;
  62. int valid = 0;
  63. if (master_fd >= 0) {
  64. ssize_t n = read(master_fd, &buf, 1);
  65. if (n == 1) {
  66. valid = 1;
  67. }
  68. }
  69. s_vpi_value val_byte;
  70. val_byte.format = vpiIntVal;
  71. val_byte.value.integer = buf;
  72. vpi_put_value(byte_arg, &val_byte, NULL, vpiNoDelay);
  73. s_vpi_value val_valid;
  74. val_valid.format = vpiIntVal;
  75. val_valid.value.integer = valid;
  76. vpi_put_value(valid_arg, &val_valid, NULL, vpiNoDelay);
  77. return 0;
  78. }
  79. static int pty_send_byte_calltf(char *user_data) {
  80. vpiHandle systfref = vpi_handle(vpiSysTfCall, NULL);
  81. vpiHandle args_iter = vpi_iterate(vpiArgument, systfref);
  82. if (!args_iter) return 0;
  83. vpiHandle byte_arg = vpi_scan(args_iter);
  84. vpi_free_object(args_iter);
  85. s_vpi_value val;
  86. val.format = vpiIntVal;
  87. vpi_get_value(byte_arg, &val);
  88. unsigned char b = (unsigned char)val.value.integer;
  89. if (master_fd >= 0) {
  90. write(master_fd, &b, 1);
  91. }
  92. return 0;
  93. }
  94. void register_pty_vpi(void) {
  95. s_vpi_systf_data tf_init = {
  96. vpiSysTask, 0, "$pty_init", pty_init_calltf, 0, 0, 0
  97. };
  98. vpi_register_systf(&tf_init);
  99. s_vpi_systf_data tf_read = {
  100. vpiSysTask, 0, "$pty_poll_read", pty_poll_read_calltf, 0, 0, 0
  101. };
  102. vpi_register_systf(&tf_read);
  103. s_vpi_systf_data tf_send = {
  104. vpiSysTask, 0, "$pty_send_byte", pty_send_byte_calltf, 0, 0, 0
  105. };
  106. vpi_register_systf(&tf_send);
  107. }
  108. void (*vlog_startup_routines[])(void) = {
  109. register_pty_vpi,
  110. 0
  111. };