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