vpi_pty.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. // Configure raw mode (no echo, no translation)
  31. cfmakeraw(&tio);
  32. tcsetattr(master_fd, TCSANOW, &tio);
  33. tcsetattr(slave_fd, TCSANOW, &tio);
  34. // Set non-blocking on master_fd
  35. int flags = fcntl(master_fd, F_GETFL, 0);
  36. fcntl(master_fd, F_SETFL, flags | O_NONBLOCK);
  37. char *sname = ttyname(slave_fd);
  38. vpi_printf("[VPI_PTY] Opened PTY: slave=%s\n", sname);
  39. FILE *fp = fopen(slave_path_file, "w");
  40. if (fp) {
  41. fprintf(fp, "%s\n", sname);
  42. fclose(fp);
  43. }
  44. return 0;
  45. }
  46. static int pty_poll_read_calltf(char *user_data) {
  47. vpiHandle systfref = vpi_handle(vpiSysTfCall, NULL);
  48. vpiHandle args_iter = vpi_iterate(vpiArgument, systfref);
  49. if (!args_iter) return 0;
  50. vpiHandle byte_arg = vpi_scan(args_iter);
  51. vpiHandle valid_arg = vpi_scan(args_iter);
  52. vpi_free_object(args_iter);
  53. unsigned char buf = 0;
  54. int valid = 0;
  55. if (master_fd >= 0) {
  56. ssize_t n = read(master_fd, &buf, 1);
  57. if (n == 1) {
  58. valid = 1;
  59. }
  60. }
  61. s_vpi_value val_byte;
  62. val_byte.format = vpiIntVal;
  63. val_byte.value.integer = buf;
  64. vpi_put_value(byte_arg, &val_byte, NULL, vpiNoDelay);
  65. s_vpi_value val_valid;
  66. val_valid.format = vpiIntVal;
  67. val_valid.value.integer = valid;
  68. vpi_put_value(valid_arg, &val_valid, NULL, vpiNoDelay);
  69. return 0;
  70. }
  71. static int pty_send_byte_calltf(char *user_data) {
  72. vpiHandle systfref = vpi_handle(vpiSysTfCall, NULL);
  73. vpiHandle args_iter = vpi_iterate(vpiArgument, systfref);
  74. if (!args_iter) return 0;
  75. vpiHandle byte_arg = vpi_scan(args_iter);
  76. vpi_free_object(args_iter);
  77. s_vpi_value val;
  78. val.format = vpiIntVal;
  79. vpi_get_value(byte_arg, &val);
  80. unsigned char b = (unsigned char)val.value.integer;
  81. if (master_fd >= 0) {
  82. write(master_fd, &b, 1);
  83. }
  84. return 0;
  85. }
  86. void register_pty_vpi(void) {
  87. s_vpi_systf_data tf_init = {
  88. vpiSysTask, 0, "$pty_init", pty_init_calltf, 0, 0, 0
  89. };
  90. vpi_register_systf(&tf_init);
  91. s_vpi_systf_data tf_read = {
  92. vpiSysTask, 0, "$pty_poll_read", pty_poll_read_calltf, 0, 0, 0
  93. };
  94. vpi_register_systf(&tf_read);
  95. s_vpi_systf_data tf_send = {
  96. vpiSysTask, 0, "$pty_send_byte", pty_send_byte_calltf, 0, 0, 0
  97. };
  98. vpi_register_systf(&tf_send);
  99. }
  100. void (*vlog_startup_routines[])(void) = {
  101. register_pty_vpi,
  102. 0
  103. };