]> Joshua Wise's Git repositories - fpgaboy.git/blame_incremental - binwire.c
Latest shit, I can't remember
[fpgaboy.git] / binwire.c
... / ...
CommitLineData
1#include <stdio.h>
2#include <sys/types.h>
3#include <sys/stat.h>
4#include <fcntl.h>
5#include <stdlib.h>
6#include <poll.h>
7
8void dowrite(char *s, int len)
9{
10 int i;
11 for (i=0; i<len; i++)
12 {
13 write(1, s+i, 1);
14 }
15}
16
17int waitchar(int timeout)
18{
19 struct pollfd pfd;
20
21 pfd.fd = 0;
22 pfd.events = POLLIN;
23 return poll(&pfd, 1, timeout) == 1;
24}
25
26int expect(char *s, int len)
27{
28 int i;
29 char c;
30 for (i=0; i < len; i++)
31 {
32 if (waitchar(1000) == 0)
33 {
34 fprintf(stderr, "Timeout reached in expect (expected %c)\n", s[i]);
35 return 1;
36 }
37 while (read(0, &c, 1) == 0)
38 fprintf(stderr, "Short read...\n");
39 if (c != s[i])
40 {
41 fprintf(stderr, "Expect failed: expected %d, got %d (pos %d)\n", s[i], c, i);
42 return 1;
43 }
44 }
45 return 0;
46}
47
48void expect_no_chars()
49{
50 int cs = 0;
51
52 while (waitchar(100) == 1)
53 {
54 char c;
55 if (read(0, &c, 1) == 0)
56 fprintf(stderr, "enc Short read...\n");
57 cs++;
58 fprintf(stderr, "Warning: expected no chars, got %d\n", c);
59 }
60 if (cs)
61 fprintf(stderr, "Expect no chars failed: got %d chars\n", cs);
62}
63
64void main(int argc, char **argv)
65{
66 unsigned char buf[259];
67 int sz;
68 int rfd;
69 int tc = 0;
70
71 if (argc < 2)
72 {
73 fprintf(stderr, "Usage: %s [filename]\n", argv[0]);
74 exit(1);
75 }
76 rfd = open(argv[1], O_RDONLY);
77 if (rfd < 0)
78 {
79 perror("open");
80 exit(1);
81 }
82
83 dowrite("\x1B" "A\x00\x00\x00", 5);
84 fprintf(stderr, "Address sent\n");
85 expect("A", 1);
86 expect_no_chars();
87 while ((sz = read(rfd, buf+3, 255)) > 0)
88 {
89 int rv;
90 buf[0] = 0x1B;
91 buf[1] = 'D';
92 buf[2] = sz+1;
93 tc += sz;
94 retry:
95 dowrite(buf, sz + 3);
96 fprintf(stderr, "Data sent (%d)\n", tc);
97 rv = expect("D", 1);
98 expect_no_chars();
99 if (rv)
100 {
101 printf("Failure to ack... retrying\n");
102 dowrite("...", 3);
103 rv = expect("...", 3);
104 expect_no_chars();
105 goto retry;
106 }
107 }
108 exit(0);
109}
This page took 0.020533 seconds and 4 git commands to generate.