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

Subversion Repositories adv_debug_sys

[/] [adv_debug_sys/] [trunk/] [Software/] [adv_jtag_bridge/] [cable_sim.c] - Blame information for rev 59

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 55 nyawn
#include "cable_sim.h"
34 4 nyawn
#include "errcodes.h"
35
 
36
#define debug(...) //fprintf(stderr, __VA_ARGS__ )
37
 
38
/* Only used in the vpi */
39 55 nyawn
jtag_cable_t vpi_cable_driver = {
40
    .name = "vpi",
41
    .inout_func = cable_vpi_inout,
42
    .out_func = cable_vpi_out,
43
    .init_func = cable_vpi_init,
44
    .opt_func = cable_vpi_opt,
45
    .bit_out_func = cable_common_write_bit,
46
    .bit_inout_func = cable_common_read_write_bit,
47
    .stream_out_func = cable_common_write_stream,
48
    .stream_inout_func = cable_common_read_stream,
49
    .flush_func = NULL,
50
    .opts = "s:p:",
51
    .help = "-p [port] Port number that the VPI module is listening on\n\t-s [server] Server that the VPI module is running on\n",
52
};
53
 
54 4 nyawn
static int vpi_comm;
55
static int vpi_port = 4567;
56
static char *vpi_server = "localhost";
57
 
58
/* Only used for the rtl_sim */
59 55 nyawn
jtag_cable_t rtl_cable_driver = {
60
    .name ="rtl_sim",
61
    .inout_func = cable_rtl_sim_inout,
62
    .out_func = cable_rtl_sim_out,
63
    .init_func = cable_rtl_sim_init,
64
    .opt_func = cable_rtl_sim_opt,
65
    .bit_out_func = cable_common_write_bit,
66
    .bit_inout_func = cable_common_read_write_bit,
67
    .stream_out_func = cable_common_write_stream,
68
    .stream_inout_func = cable_common_read_stream,
69
    .flush_func = NULL,
70
    .opts = "d:",
71
    .help = "-d [directory] Directory in which gdb_in.dat/gdb_out.dat may be found\n"
72
};
73
 
74 4 nyawn
static char *gdb_in = "gdb_in.dat";
75
static char *gdb_out = "gdb_out.dat";
76
 
77
 
78
 
79
/*-------------------------------------------[ rtl_sim specific functions ]---*/
80 55 nyawn
jtag_cable_t *cable_rtl_get_driver(void)
81
{
82
  return &rtl_cable_driver;
83
}
84
 
85 4 nyawn
int cable_rtl_sim_init()
86
{
87
  FILE *fin = fopen (gdb_in, "wt+");
88
  if(!fin) {
89
    fprintf(stderr, "Can not open %s\n", gdb_in);
90
    return APP_ERR_INIT_FAILED;
91
  }
92
  fclose(fin);
93
  return APP_ERR_NONE;
94
}
95
 
96
int cable_rtl_sim_out(uint8_t value)
97
{
98
  FILE *fout;
99
  int num_read;
100
  int r;
101
  debug("O (%x)\n", value);
102
  fout = fopen(gdb_in, "wt+");
103
  fprintf(fout, "F\n");
104
  fflush(fout);
105
  fclose(fout);
106
  fout = fopen(gdb_out, "wt+");
107
  fprintf(fout, "%02X\n", value);
108
  fflush(fout);
109
  fclose(fout);
110
  do {
111
    fout = fopen(gdb_out, "rt");
112
    r = fscanf(fout,"%x", &num_read);
113
    fclose(fout);
114
    usleep(1000);
115
    debug(" (Ack %x) ", num_read);
116
  } while(!r || (num_read != (0x10 | value)));
117
  debug("\n");
118
  return APP_ERR_NONE;
119
}
120
 
121 55 nyawn
int cable_rtl_sim_inout(uint8_t value, uint8_t *inval)
122 4 nyawn
{
123
  FILE *fin = 0;
124
  char ch;
125
  uint8_t data;
126
  debug("IO (");
127
 
128
  while(1) {
129
    fin = fopen(gdb_in, "rt");
130
    if(!fin) {
131
      usleep(1000);
132
      continue;
133
    }
134
    ch = fgetc(fin);
135
    fclose(fin);
136
    if((ch != '0') && (ch != '1')) {
137
      usleep(1000);
138
      continue;
139
    }
140
    else
141
      break;
142
  }
143
  data = ch == '1' ? 1 : 0;
144
 
145
  debug("%x,", data);
146
 
147
  cable_rtl_sim_out(value);
148
 
149
  debug("%x)\n", value);
150
 
151
  *inval = data;
152
  return APP_ERR_NONE;
153
}
154
 
155
 
156
int cable_rtl_sim_opt(int c, char *str)
157
{
158
  switch(c) {
159
  case 'd':
160
    if(!(gdb_in = malloc(strlen(str) + 12))) { /* 12 == strlen("gdb_in.dat") + 2 */
161
      fprintf(stderr, "Unable to allocate enough memory\n");
162
      return APP_ERR_MALLOC;
163
    }
164
    if(!(gdb_out = malloc(strlen(str) + 13))) { /* 13 == strlen("gdb_out.dat") + 2 */
165
      fprintf(stderr, "Unable to allocate enough memory\n");
166
      free(gdb_in);
167
      return APP_ERR_MALLOC;
168
    }
169
 
170
    sprintf(gdb_in, "%s/gdb_in.dat", str);
171
    sprintf(gdb_out, "%s/gdb_out.dat", str);
172
    break;
173
  default:
174
    fprintf(stderr, "Unknown parameter '%c'\n", c);
175
    return APP_ERR_BAD_PARAM;
176
  }
177
  return APP_ERR_NONE;
178
}
179
 
180
/*-----------------------------------------------[ VPI specific functions ]---*/
181 55 nyawn
jtag_cable_t *cable_vpi_get_driver(void)
182
{
183
  return &vpi_cable_driver;
184
}
185
 
186
 
187 4 nyawn
int cable_vpi_init()
188
{
189
  struct sockaddr_in addr;
190
  struct hostent *he;
191
 
192
  if((vpi_comm = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
193
    fprintf(stderr, "Unable to create socket (%s)\n", strerror(errno));
194
    return APP_ERR_CONNECT;
195
  }
196
 
197
 
198
  if((he = gethostbyname(vpi_server)) == NULL) {
199
    perror("gethostbyname");
200
    return APP_ERR_CONNECT;
201
  }
202
 
203
  addr.sin_family = AF_INET;
204 59 nyawn
  addr.sin_port = htons(vpi_port);
205 4 nyawn
  addr.sin_addr = *((struct in_addr *)he->h_addr);
206
  memset(addr.sin_zero, '\0', sizeof(addr.sin_zero));
207
 
208
  if(connect(vpi_comm, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
209
    fprintf(stderr, "Unable to connect to %s port %d (%s)\n", vpi_server, vpi_port,
210
            strerror(errno));
211
    return APP_ERR_CONNECT;
212
  }
213
 
214
  debug("VPI connected!");
215
 
216
  return APP_ERR_NONE;
217
}
218
 
219
int cable_vpi_out(uint8_t value)
220
{
221
  uint8_t ack;
222
  int ret;
223
 
224
  /* Send the data to the socket */
225
  ret = send(vpi_comm, &value, 1, 0);
226
  debug("Sent %d, ret %d\n", value, ret);
227
 
228
  do {
229
    /* Ok, read the data */
230
    ret = recv(vpi_comm, &ack, 1, 0);
231
    if(ret < 0) {
232
      printf("Error during receive (%s)\n", strerror(errno));
233
      return APP_ERR_CONNECT;
234
    }
235
  } while(ack != (value | 0x10));
236 55 nyawn
 
237
  cable_vpi_wait();  // finish the transaction
238
 
239 4 nyawn
  return APP_ERR_NONE;
240
}
241
 
242
int cable_vpi_inout(uint8_t value, uint8_t *inval)
243
{
244
  uint8_t dat;
245
 
246
  /* ask vpi to send us the out-bit */
247
  dat = 0x80;
248
  send(vpi_comm, &dat, 1, 0);
249
 
250
  /* Wait and read the data */
251
  recv(vpi_comm, &dat, 1, 0);
252
 
253
  if(dat > 1)
254
    fprintf(stderr, "Unexpected value: %i\n", dat);
255
 
256
  cable_vpi_out(value);
257
 
258
  *inval = dat;
259 55 nyawn
 
260
  cable_vpi_wait();  // finish the transaction
261
 
262 4 nyawn
  return APP_ERR_NONE;
263
}
264
 
265
void cable_vpi_wait()
266
{
267
  uint8_t dat = 0x81;
268
 
269
  /* Get the sim to reply when the timeout has been reached */
270
  if(send(vpi_comm, &dat, 1, 0) < 1) {
271
    fprintf(stderr, "Failed to send pkt in cable_vpi_wait(): %s\n", strerror(errno));
272
  }
273
 
274
  /* block, waiting for the data */
275
  if(recv(vpi_comm, &dat, 1, 0) < 1) {
276
    fprintf(stderr, "Recv failed in cable_vpi_wait(): %s\n", strerror(errno));
277
  }
278
 
279
  if(dat != 0xFF)
280
    fprintf(stderr, "Warning: got wrong byte waiting for timeout: 0x%X\n", dat);
281
 
282
}
283
 
284
int cable_vpi_opt(int c, char *str)
285
{
286
  switch(c) {
287
  case 'p':
288
    if((vpi_port = atoi(str)) == 0) {
289
      fprintf(stderr, "Bad port value for VPI sim: %s\n", str);
290
      return APP_ERR_BAD_PARAM;
291
    }
292
    break;
293
  case 's':
294
    vpi_server = strdup(str);
295
    if(vpi_server == NULL) {
296
      fprintf(stderr, "Unable to allocate enough memory for server string\n");
297
      return APP_ERR_MALLOC;
298
    }
299
    break;
300
  default:
301
    fprintf(stderr, "Unknown parameter '%c'\n", c);
302
    return APP_ERR_BAD_PARAM;
303
  }
304
  return APP_ERR_NONE;
305
}

powered by: WebSVN 2.1.0

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