-
Notifications
You must be signed in to change notification settings - Fork 0
/
xec.c
230 lines (191 loc) · 3.73 KB
/
xec.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/file.h>
#define _reset "\033[0m"
#define KBLK "\e[0;30m"
#define KRED "\033[31m"
#define KGRN "\033[32m"
#define KYEL "\033[33m"
#define KBLU "\033[34m"
#define KMAG "\033[35m"
#define KCYN "\033[36m"
#define KWHT "\033[37m"
#define KLRD "\033[1;31m"
#define KLGN "\033[1;32m"
#define _inverse "\033[1;7m"
#define _bold "\x1B[1m"
#define _underline "\x1B[4m"
#define xec_clear() printf("\033[H\033[J")
#define xec_gotoxy(x,y) printf("\033[%d;%dH", (y), (x))
#define PORT "/dev/port"
int xec_fd = -1;
typedef enum
{
output_buffer_full = 0x01,
input_buffer_full = 0x02,
command = 0x08,
bu_reset_mode = 0x10,
sci_event_pending = 0x20,
smi_event_pending = 0x40
} xec_status_t;
typedef enum
{
command_port = 0x66,
data_port = 0x62
} xec_port_t;
typedef enum
{
xec_read = 0x80,
xec_write = 0x81,
xec_bu_reset_enable = 0x82,
xec_bu_reset_disable = 0x83,
xec_query = 0x84
} xec_command_t;
uint8_t xec_port_read(xec_port_t port);
uint8_t xec_port_write(xec_port_t port, uint8_t data);
int xec_wait_status(xec_status_t status, bool set);
int xec_wait_read();
int xec_wait_write();
int xec_monitor();
int xec_init();
int xec_close();
uint8_t xec_port_read(xec_port_t port)
{
uint8_t ch = 0;
int ls = lseek(xec_fd, port, 0);
if ( 0 > ls )
{
printf("error: cannot lseek() \n");
exit(-1);
}
int rd = read(xec_fd, &ch, 1);
if ( 1 != rd )
{
printf("error: read != 1 \n");
exit(-1);
}
return ch;
}
uint8_t xec_port_write(xec_port_t port, uint8_t data)
{
int ls = lseek(xec_fd, port, 0);
if ( 0 > ls )
{
printf("error: cannot lseek() \n");
exit(-1);
}
int wd = write(xec_fd, &data, 1);
if ( 1 != wd )
{
printf("error: read != 1 \n");
exit(-1);
}
return 0;
}
int xec_wait_status(xec_status_t status, bool set)
{
int timeout = 400;
while ( timeout > 0 )
{
timeout--;
uint8_t val = xec_port_read(command_port);
val = set ? (uint8_t) ~val : val;
if ( 0 == (status & val) )
{
return 0;
}
}
return -1;
}
int max_waits = 20, wait_faults;
int xec_wait_read()
{
if ( wait_faults > max_waits )
{
return 0;
} else if ( xec_wait_status(output_buffer_full, true) )
{
wait_faults = 0;
} else
{
wait_faults++;
return 1;
}
}
int xec_wait_write()
{
return xec_wait_status(input_buffer_full, false);
}
char* xec_style(uint8_t data)
{
char *x = malloc(32);
data = (data < 50 ? 50 : data);
sprintf(x, "\033[38;2;%d;%d;%dm", data, data, data);
return x;
switch(data)
{
case 0xFF: { return KGRN; break; }
case 0x00: { return "\033[38;2;50;50;50m"; break; }
default: { return KWHT; break; }
}
}
int xec_monitor()
{
printf(_reset);
printf(" │ ");
printf(_inverse);
for (uint16_t x = 0; x < 16; x++)
{
printf("%02X%s", x, /* (x != 16-1 ? " " : "") */ " "); // FIX!
//printf(_inverse "%02X" _reset " ", x);
}
printf(_reset "\n");
printf("──┼");
for (uint16_t x = 0; x < 16*3 + 1; x++)
{
printf("─");
}
//printf("\n");
printf(_reset);
for (uint32_t i = 0; i < 256; i++)
{
xec_wait_write();
xec_port_write(command_port, xec_read);
xec_wait_write();
xec_port_write(data_port, (uint8_t) i);
xec_wait_read();
uint8_t data = xec_port_read(data_port);
if ( (i % 16) == 0 )
{
printf("\n" _reset _inverse "\033[48;2;0;0;0m" "%02X" _reset "│ ", i);
}
printf("\033[48;2;0;0;0m" "%s%02X " _reset, xec_style(data), data);
}
printf("\n");
// xec_gotoxy(0, 0);
// xec_monitor();
}
int xec_init()
{
int fd = open(PORT, O_RDWR);
if ( 0 > fd )
{
printf("error: port open() \n");
exit(-1);
}
xec_fd = fd;
return 0;
}
int xec_close()
{
int cl = close(xec_fd);
if ( 0 > cl )
{
printf("error: cannot close()");
exit(-1);
}
return 0;
}