OpenCores
URL https://opencores.org/ocsvn/adv_debug_sys/adv_debug_sys/trunk

Subversion Repositories adv_debug_sys

[/] [adv_debug_sys/] [tags/] [ADS_RELEASE_1_1_0/] [Software/] [adv_jtag_bridge/] [cable_sim.c] - Blame information for rev 19

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 nyawn
/* cable_sim.c - Simulation connection drivers for the Advanced JTAG Bridge
2
   Copyright (C) 2001 Marko Mlinar, markom@opencores.org
3
   Copyright (C) 2004 György Jeney, nog@sdf.lonestar.org
4
 
5
 
6
   This program is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License as published by
8
   the Free Software Foundation; either version 2 of the License, or
9
   (at your option) any later version.
10
 
11
   This program is distributed in the hope that it will be useful,
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
   GNU General Public License for more details.
15
 
16
   You should have received a copy of the GNU General Public License
17
   along with this program; if not, write to the Free Software
18
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
 
20
 
21
 
22
#include <stdio.h>
23
#include <string.h>
24
#include <sys/types.h>
25
#include <unistd.h>
26
#include <errno.h>
27
#include <stdlib.h>
28
 
29
#include <sys/socket.h>
30
#include <netinet/in.h>
31
#include <netdb.h>
32
 
33
 
34
#include "cable_common.h"
35
#include "errcodes.h"
36
 
37
#define debug(...) //fprintf(stderr, __VA_ARGS__ )
38
 
39
/* Only used in the vpi */
40
static int vpi_comm;
41
static int vpi_port = 4567;
42
static char *vpi_server = "localhost";
43
 
44
/* Only used for the rtl_sim */
45
static char *gdb_in = "gdb_in.dat";
46
static char *gdb_out = "gdb_out.dat";
47
 
48
 
49
 
50
/*-------------------------------------------[ rtl_sim specific functions ]---*/
51
int cable_rtl_sim_init()
52
{
53
  FILE *fin = fopen (gdb_in, "wt+");
54
  if(!fin) {
55
    fprintf(stderr, "Can not open %s\n", gdb_in);
56
    return APP_ERR_INIT_FAILED;
57
  }
58
  fclose(fin);
59
  return APP_ERR_NONE;
60
}
61
 
62
int cable_rtl_sim_out(uint8_t value)
63
{
64
  FILE *fout;
65
  int num_read;
66
  int r;
67
  debug("O (%x)\n", value);
68
  fout = fopen(gdb_in, "wt+");
69
  fprintf(fout, "F\n");
70
  fflush(fout);
71
  fclose(fout);
72
  fout = fopen(gdb_out, "wt+");
73
  fprintf(fout, "%02X\n", value);
74
  fflush(fout);
75
  fclose(fout);
76
  do {
77
    fout = fopen(gdb_out, "rt");
78
    r = fscanf(fout,"%x", &num_read);
79
    fclose(fout);
80
    usleep(1000);
81
    debug(" (Ack %x) ", num_read);
82
  } while(!r || (num_read != (0x10 | value)));
83
  debug("\n");
84
  return APP_ERR_NONE;
85
}
86
 
87
uint8_t cable_rtl_sim_inout(uint8_t value, uint8_t *inval)
88
{
89
  FILE *fin = 0;
90
  char ch;
91
  uint8_t data;
92
  debug("IO (");
93
 
94
  while(1) {
95
    fin = fopen(gdb_in, "rt");
96
    if(!fin) {
97
      usleep(1000);
98
      continue;
99
    }
100
    ch = fgetc(fin);
101
    fclose(fin);
102
    if((ch != '0') && (ch != '1')) {
103
      usleep(1000);
104
      continue;
105
    }
106
    else
107
      break;
108
  }
109
  data = ch == '1' ? 1 : 0;
110
 
111
  debug("%x,", data);
112
 
113
  cable_rtl_sim_out(value);
114
 
115
  debug("%x)\n", value);
116
 
117
  *inval = data;
118
  return APP_ERR_NONE;
119
}
120
 
121
 
122
int cable_rtl_sim_opt(int c, char *str)
123
{
124
  switch(c) {
125
  case 'd':
126
    if(!(gdb_in = malloc(strlen(str) + 12))) { /* 12 == strlen("gdb_in.dat") + 2 */
127
      fprintf(stderr, "Unable to allocate enough memory\n");
128
      return APP_ERR_MALLOC;
129
    }
130
    if(!(gdb_out = malloc(strlen(str) + 13))) { /* 13 == strlen("gdb_out.dat") + 2 */
131
      fprintf(stderr, "Unable to allocate enough memory\n");
132
      free(gdb_in);
133
      return APP_ERR_MALLOC;
134
    }
135
 
136
    sprintf(gdb_in, "%s/gdb_in.dat", str);
137
    sprintf(gdb_out, "%s/gdb_out.dat", str);
138
    break;
139
  default:
140
    fprintf(stderr, "Unknown parameter '%c'\n", c);
141
    return APP_ERR_BAD_PARAM;
142
  }
143
  return APP_ERR_NONE;
144
}
145
 
146
/*-----------------------------------------------[ VPI specific functions ]---*/
147
int cable_vpi_init()
148
{
149
  struct sockaddr_in addr;
150
  struct hostent *he;
151
 
152
  if((vpi_comm = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
153
    fprintf(stderr, "Unable to create socket (%s)\n", strerror(errno));
154
    return APP_ERR_CONNECT;
155
  }
156
 
157
 
158
  if((he = gethostbyname(vpi_server)) == NULL) {
159
    perror("gethostbyname");
160
    return APP_ERR_CONNECT;
161
  }
162
 
163
  addr.sin_family = AF_INET;
164
  addr.sin_port = vpi_port;
165
  addr.sin_addr = *((struct in_addr *)he->h_addr);
166
  memset(addr.sin_zero, '\0', sizeof(addr.sin_zero));
167
 
168
  if(connect(vpi_comm, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
169
    fprintf(stderr, "Unable to connect to %s port %d (%s)\n", vpi_server, vpi_port,
170
            strerror(errno));
171
    return APP_ERR_CONNECT;
172
  }
173
 
174
  debug("VPI connected!");
175
 
176
  return APP_ERR_NONE;
177
}
178
 
179
int cable_vpi_out(uint8_t value)
180
{
181
  uint8_t ack;
182
  int ret;
183
 
184
  /* Send the data to the socket */
185
  ret = send(vpi_comm, &value, 1, 0);
186
  debug("Sent %d, ret %d\n", value, ret);
187
 
188
  do {
189
    /* Ok, read the data */
190
    ret = recv(vpi_comm, &ack, 1, 0);
191
    if(ret < 0) {
192
      printf("Error during receive (%s)\n", strerror(errno));
193
      return APP_ERR_CONNECT;
194
    }
195
  } while(ack != (value | 0x10));
196
  return APP_ERR_NONE;
197
}
198
 
199
int cable_vpi_inout(uint8_t value, uint8_t *inval)
200
{
201
  uint8_t dat;
202
 
203
  /* ask vpi to send us the out-bit */
204
  dat = 0x80;
205
  send(vpi_comm, &dat, 1, 0);
206
 
207
  /* Wait and read the data */
208
  recv(vpi_comm, &dat, 1, 0);
209
 
210
  if(dat > 1)
211
    fprintf(stderr, "Unexpected value: %i\n", dat);
212
 
213
  cable_vpi_out(value);
214
 
215
  *inval = dat;
216
  return APP_ERR_NONE;
217
}
218
 
219
void cable_vpi_wait()
220
{
221
  uint8_t dat = 0x81;
222
 
223
  /* Get the sim to reply when the timeout has been reached */
224
  if(send(vpi_comm, &dat, 1, 0) < 1) {
225
    fprintf(stderr, "Failed to send pkt in cable_vpi_wait(): %s\n", strerror(errno));
226
  }
227
 
228
  /* block, waiting for the data */
229
  if(recv(vpi_comm, &dat, 1, 0) < 1) {
230
    fprintf(stderr, "Recv failed in cable_vpi_wait(): %s\n", strerror(errno));
231
  }
232
 
233
  if(dat != 0xFF)
234
    fprintf(stderr, "Warning: got wrong byte waiting for timeout: 0x%X\n", dat);
235
 
236
}
237
 
238
int cable_vpi_opt(int c, char *str)
239
{
240
  switch(c) {
241
  case 'p':
242
    if((vpi_port = atoi(str)) == 0) {
243
      fprintf(stderr, "Bad port value for VPI sim: %s\n", str);
244
      return APP_ERR_BAD_PARAM;
245
    }
246
    break;
247
  case 's':
248
    vpi_server = strdup(str);
249
    if(vpi_server == NULL) {
250
      fprintf(stderr, "Unable to allocate enough memory for server string\n");
251
      return APP_ERR_MALLOC;
252
    }
253
    break;
254
  default:
255
    fprintf(stderr, "Unknown parameter '%c'\n", c);
256
    return APP_ERR_BAD_PARAM;
257
  }
258
  return APP_ERR_NONE;
259
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.