import slipway (fpga-fleet) code
[fleet.git] / src / edu / berkeley / fleet / slipway / test.c
1 #include<stdio.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <fcntl.h>
5 #include <stdint.h>
6
7 #define uint32 uint32_t
8 #define uint64 uint64_t
9 static uint32 fd;
10
11 void sendc(char c) {
12   uint32 rv = write(fd, &c, 1);
13   if (rv != 1) {
14     printf("write ret: %d\n", rv);
15   }
16   printf("send: %x\n", c);
17 }
18
19 void send(int dest, uint64 i) {
20   sendc((dest >> 8) & 0xff);
21   sendc((dest >> 0) & 0xff);
22   sendc((i >>32) & 0xff);
23   sendc((i >>24) & 0xff);
24   sendc((i >>16) & 0xff);
25   sendc((i >> 8) & 0xff);
26   sendc((i >> 0) & 0xff);
27 }
28
29 char recvc() {
30   char c;
31   uint32 rv = read(fd, &c, 1);
32   if (rv != 1) {
33     printf("read ret: %d\n", rv);
34   }
35   return c;
36 }
37
38 uint64 recv() {
39   uint64 ret = 0
40     | (((uint64)recvc()) << 32)
41     | (((uint64)recvc()) << 24)
42     | (((uint64)recvc()) << 16)
43     | (((uint64)recvc()) <<  8)
44     | (((uint64)recvc()) <<  0)
45     ;
46   return ret;
47 }
48
49 void sendbits(int num, int dat) {
50   sendc(num);
51   while(num > 0) {
52     sendc((dat>>(num>=8 ? (num-8):0)) & 0xff);
53     num -= 8;
54   }
55 }
56
57 uint64_t inst(int boxname, int tokenin, int datain, int latch, int dataout, int tokenout, int count, int dest) {
58   uint64_t ret = 0;
59   ret |= dest;     ret <<= 7;
60   ret |= count;    ret <<= 1;
61   ret |= tokenout; ret <<= 1;
62   ret |= dataout;  ret <<= 1;
63   ret |= latch;    ret <<= 1;
64   ret |= datain;   ret <<= 1;
65   ret |= tokenin;  ret <<= 11;
66   ret |= boxname;
67   return ret;
68 }
69
70 int main(int argc, char** argv) {
71   char c;
72   uint32 i,j;
73   printf("hello.\n");
74   fd = open(argv[1], O_RDWR);
75   printf("open %s = %d\n", argv[1], fd);
76
77   if (argc > 2) {
78     int fd2 = open(argv[2], O_RDWR);
79     printf("open %s = %d\n", argv[2], fd2);
80     while(1) {
81       i = read(fd2, &c, 1);
82       if (i!=1) break;
83       sendc(c);
84     }
85   }
86
87   //send(0x03, 0x101010);
88   //send(0x00, 0x101010);
89   //send(0x01, 0x010101);
90   /*
91   send(0x06, 0x22);
92   send(0x02, 0x22);
93   send(0x04,
94        inst(0x01, 0, 1, 1, 1, 0, 1, 0x00)
95        );
96   */
97   /*
98   if (argc > 3)
99     send(27, atoi(argv[3]));
100   if (argc > 4)
101     send(7, atoi(argv[4]));
102   */
103   printf("\n===================================================================\n\n");
104   /*
105   sendc(0x00);
106   sendc(12);
107   sendc(12);
108   sendc(0x06);
109   */
110
111   //for( j=0; j<2; j++) {
112   i = recvc();
113   printf("result: %d / %x \n", i, i);
114   i = recvc();
115   printf("result: %d / %x \n", i, i);
116   i = recvc();
117   printf("result: %d / %x \n", i, i);
118   i = recvc();
119   printf("result: %d / %x \n", i, i);
120   i = recvc();
121   printf("result: %d / %x \n", i, i);
122   i = recvc();
123   printf("result: %d / %x \n", i, i);
124   //}
125   return 0;
126 }