add peripherals
[shakti-peripherals.git] / src / uncore / debug / RBB_Shakti.c
1 /*
2 Copyright (c) 2013, IIT Madras
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6
7 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9 * Neither the name of IIT Madras nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
11 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
13 */
14 #include <stdio.h>
15 #include <arpa/inet.h>
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <stdbool.h>
22 #include "RBB_Shakti.h"
23
24 int socket_fd;
25 int client_fd;
26 int port = 10000;
27 char frame = 0; // frame = {NA,NA,NA,reset,request_tdo,tck,tms,tdi}
28
29 char accept_cxn()
30 {
31 client_fd = accept(socket_fd, NULL, NULL);
32 if (client_fd == -1) {
33 if (errno == EAGAIN) {
34 return -1;
35 // No client waiting to connect right now.
36 } else {
37 fprintf(stderr, "failed to accept on socket: %s (%d)\n", strerror(errno),
38 errno);
39 }
40 } else {
41 fcntl(client_fd, F_SETFL, O_NONBLOCK);
42 return 1;
43 }
44 return -1;
45 }
46
47
48 char init1(){
49 socket_fd = socket(AF_INET, SOCK_STREAM, 0);
50 if (socket_fd == -1) {
51 fprintf(stderr, "remote_bitbang failed to make socket: %s (%d)\n",
52 strerror(errno), errno);
53 return -1;
54 }
55
56 fcntl(socket_fd, F_SETFL, O_NONBLOCK);
57 int reuseaddr = 1;
58 if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr,
59 sizeof(int)) == -1) {
60 fprintf(stderr, "remote_bitbang failed setsockopt: %s (%d)\n",
61 strerror(errno), errno);
62 return -1;
63 }
64
65 struct sockaddr_in addr;
66 memset(&addr, 0, sizeof(addr));
67 addr.sin_family = AF_INET;
68 addr.sin_addr.s_addr = INADDR_ANY;
69 addr.sin_port = htons(port);
70
71 if (bind(socket_fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
72 fprintf(stderr, "remote_bitbang failed to bind socket: %s (%d)\n",
73 strerror(errno), errno);
74 return -1;
75 }
76
77 if (listen(socket_fd, 1) == -1) {
78 fprintf(stderr, "remote_bitbang failed to listen on socket: %s (%d)\n",
79 strerror(errno), errno);
80 return -1;
81 }
82
83 socklen_t addrlen = sizeof(addr);
84 if (getsockname(socket_fd, (struct sockaddr *) &addr, &addrlen) == -1) {
85 fprintf(stderr, "remote_bitbang getsockname failed: %s (%d)\n",
86 strerror(errno), errno);
87 return -1;
88 }
89
90 printf("Listening for remote bitbang connection on port %d.\n",
91 ntohs(addr.sin_port));
92 fflush(stdout);
93 printf("Waiting for OpenOCD .... \n");
94 while (accept_cxn() != 1);
95 return 0;
96 }
97
98
99 char decode_frame(char command){
100 // frame = {NA,NA,NA,reset,request_tdo,tck,tms,tdi}
101 switch (command) {
102 case 'B': /* fprintf(stderr, "*BLINK*\n"); */ break; // not supported in spike
103 case 'b': /* fprintf(stderr, "_______\n"); */ break; // not supported in spike
104 case 'r': frame &= ~((char)24); frame |= 16 ; break;
105 case '0': frame = 0; break;
106 case '1': frame = 1; break;
107 case '2': frame = 2; break;
108 case '3': frame = 3; break;
109 case '4': frame = 4; break;
110 case '5': frame = 5; break;
111 case '6': frame = 6; break;
112 case '7': frame = 7; break;
113 case 'R': frame &= ~((char)24); frame |= 8 ; break; // push out a word with the previous state held with the read bit enabled maintain previous state and just push enable the read bit
114 case 'Q': break; // Not Supporting Q right now
115 default:
116 frame &= ~((char)24); //fprintf(stderr, "remote_bitbang got unsupported command '%d'\n",
117 // command); // essentially de assert the read bit if it was ever up;
118 }
119 return frame;
120 }
121
122 // frame = {NA,NA,NA,reset,request_tdo,tck,tms,tdi}
123 char get_frame(){
124 char packet;
125 read(client_fd,&packet, 1);
126 //printf("%d\n",decode_frame(packet));
127 return decode_frame(packet);
128 }
129
130 void send_tdo(bool tdo){
131 char a = (tdo)? '1' :'0' ;
132 write(client_fd,&a,1);
133 };
134
135
136 void main(){ // Test Bench of sorts
137 init1();
138 /* while(1){
139 char fr = get_frame();
140 if((fr & 8) == 8)send_tdo(1);
141 if((fr & 16) == 16) printf("Reset Requested \n");
142 }
143 */
144 }