Add Tercel PHY reset synchronization
[microwatt.git] / sim_console_c.c
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
11 /* Should we exit simulation on ctrl-c or pass it through? */
12 #define EXIT_ON_CTRL_C
13
14 static struct termios oldt;
15
16 static void disable_raw_mode(void)
17 {
18 tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
19 }
20
21 static void enable_raw_mode(void)
22 {
23 static bool initialized = false;
24
25 if (!initialized) {
26 static struct termios newt;
27
28 tcgetattr(STDIN_FILENO, &oldt);
29 newt = oldt;
30 cfmakeraw(&newt);
31 #ifdef EXIT_ON_CTRL_C
32 newt.c_lflag |= ISIG;
33 #endif
34 tcsetattr(STDIN_FILENO, TCSANOW, &newt);
35 initialized = true;
36 atexit(disable_raw_mode);
37 }
38 }
39
40 void sim_console_read(unsigned char *__rt)
41 {
42 int ret;
43 unsigned long val = 0;
44
45 enable_raw_mode();
46
47 ret = read(STDIN_FILENO, &val, 1);
48 if (ret != 1) {
49 fprintf(stderr, "%s: read of stdin returns %d\n", __func__, ret);
50 exit(1);
51 }
52
53 //fprintf(stderr, "read returns %c\n", val);
54
55 to_std_logic_vector(val, __rt, 64);
56 }
57
58 void sim_console_poll(unsigned char *__rt)
59 {
60 int ret;
61 struct pollfd fdset[1];
62 uint8_t val = 0;
63
64 enable_raw_mode();
65
66 memset(fdset, 0, sizeof(fdset));
67
68 fdset[0].fd = STDIN_FILENO;
69 fdset[0].events = POLLIN;
70
71 ret = poll(fdset, 1, 0);
72 //fprintf(stderr, "poll returns %d\n", ret);
73
74 if (ret == 1) {
75 if (fdset[0].revents & POLLIN)
76 val = 1;
77 // fprintf(stderr, "poll revents: 0x%x\n", fdset[0].revents);
78 }
79
80 to_std_logic_vector(val, __rt, 64);
81 }
82
83 void sim_console_write(unsigned char *__rs)
84 {
85 uint8_t val;
86
87 val = from_std_logic_vector(__rs, 64);
88
89 fprintf(stderr, "%c", val);
90 }