/
wiring.c
393 lines (321 loc) · 9.03 KB
/
wiring.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
* Copyright (C) 2006, 2007 Jean-Baptiste Note <jean-baptiste.note@m4x.org>
*
* This file is part of debit.
*
* Debit is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Debit is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with debit. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* New, simple wiring db implementation
*/
#include <stdio.h>
#include <glib.h>
#include <string.h>
#include "debitlog.h"
#include "wiring.h"
#include "design.h"
#ifdef __COMPILED_WIREDB
/*
* Using a compiled DB is really a breeze.
*/
#if defined(VIRTEX2)
#define WIREDB "data/virtex2/wires.m4"
#elif defined(VIRTEX4)
#define WIREDB "data/virtex4/wires.m4"
#elif defined(VIRTEX5)
#define WIREDB "data/virtex5/wires.m4"
#elif defined(SPARTAN3)
#define WIREDB "data/spartan3/wires.m4"
#else
#error "Unable to compile wiredb in"
#endif
#include "data/wiring_compiled.h"
wire_db_t *get_wiredb(const gchar *datadir) {
wire_db_t *wiredb = g_new0(wire_db_t, 1);
(void) datadir;
wiredb->details = details;
wiredb->wires = wires;
wiredb->wirenames = wirestr.str;
wiredb->wireidx = wireidx;
wiredb->dblen = G_N_ELEMENTS(wires);
return wiredb;
}
void free_wiredb(wire_db_t *wires) {
g_free(wires);
}
#else /* __COMPILED_WIREDB */
#define STRINGCHUNK_DEFAULT_SIZE 16
static gint
read_wiredb(GKeyFile **fill, const gchar *filename) {
GKeyFile *db;
GError *error = NULL;
debit_log(L_WIRES, "Loading data from %s", filename);
db = g_key_file_new();
if (!db)
goto out_err;
g_key_file_load_from_file(db,filename,G_KEY_FILE_NONE,&error);
if (error != NULL) {
g_warning("could not read db %s: %s",filename,error->message);
g_error_free (error);
goto out_err_free;
}
*fill = db;
return 0;
out_err_free:
g_key_file_free (db);
out_err:
return -1;
}
/*
* Allocate a wire db
*/
static inline int
load_wire_atom(const wire_db_t *db, GKeyFile *keyfile,
const gchar *wirename)
{
GError *err = NULL;
int retval = 0;
gint id = g_key_file_get_integer(keyfile, wirename, "ID", &err);
wire_simple_t *wire = (void *) &db->wires[id];
wire_t *detail = (void *) &db->details[id];
if (err)
goto out_err;
/* Insert the wirename */
debit_log(L_WIRES, "Inserting wire %s, id %i", wirename, id);
db->names[id] = g_string_chunk_insert_const(db->wirenames, wirename);
#define GET_STRUCT_MEMBER(structname, structmem, strname) \
do { structname->structmem = g_key_file_get_integer(keyfile, wirename, #strname, &err);\
if (err)\
goto out_err;\
} while (0)
#define GET_STRUCT_MEMBER_LIST(structname, structmem, structmemlen, strname) \
do { \
gsize listsize = 0; \
structname->structmem = g_key_file_get_integer_list(keyfile, wirename, #strname, \
&listsize, &err);\
structname->structmemlen = (typeof(structname->structmemlen)) listsize; \
if (err)\
goto out_err;\
} while (0)
GET_STRUCT_MEMBER(wire, dx, DX);
GET_STRUCT_MEMBER(wire, dy, DY);
GET_STRUCT_MEMBER(wire, ep, EP);
GET_STRUCT_MEMBER_LIST(wire, fut, fut_len, FUT);
GET_STRUCT_MEMBER(detail, type, TYPE);
GET_STRUCT_MEMBER(detail, direction, DIR);
GET_STRUCT_MEMBER(detail, situation, SIT);
return retval;
out_err:
g_warning("%s", err->message);
retval = err->code;
g_error_free (err);
return retval;
}
static inline void
free_wire_atom(const wire_db_t *db, gint id)
{
wire_simple_t *wire = (void *) &db->wires[id];
int *fut = wire->fut;
if (fut) {
wire->fut = NULL;
g_free(fut);
}
}
static inline int
fill_db_from_file(const wire_db_t *wires, GKeyFile *db,
const gsize nwires, gchar **wirenames) {
gint err = 0;
gsize i;
for(i = 0; i < nwires; i++) {
err = load_wire_atom(wires, db, wirenames[i]);
if (err)
break;
}
return err;
}
static inline void
empty_db(wire_db_t *wires, const gsize nwires) {
gsize i;
for(i = 0; i < nwires; i++)
free_wire_atom(wires, i);
}
/** Fill in a wire db with data from a file
*
* This function load from the db argument the wires argument
*
* @param db KeyFile containing data
* @param wires wire database to fill
* @return an error indication
*/
static int
load_db_from_file(GKeyFile* db, wire_db_t *wires) {
gint err;
gsize nwires;
gchar** wirenames;
wirenames = g_key_file_get_groups(db, &nwires);
debit_log(L_WIRES, "Wiring database contains %zd wires", nwires);
/* Allocate the array */
wires->dblen = nwires;
wires->wires = g_new(wire_simple_t, nwires);
wires->names = g_new(const gchar *, nwires);
wires->details = g_new(wire_t, nwires);
wires->wirenames = g_string_chunk_new(STRINGCHUNK_DEFAULT_SIZE);
/* Iterate over groups */
err = fill_db_from_file(wires, db, nwires, wirenames);
/* Cleanup */
g_strfreev(wirenames);
return err;
}
/*
* High-level function
*/
wire_db_t *get_wiredb(const gchar *datadir) {
wire_db_t *wiredb = g_new0(wire_db_t, 1);
GKeyFile *db = NULL;
gchar *dbname;
gint err;
dbname = g_build_filename(datadir,CHIP,"wires.db",NULL);
err = read_wiredb(&db, dbname);
g_free(dbname);
if (err)
goto out_err;
err = load_db_from_file(db, wiredb);
if (err)
goto out_err;
g_key_file_free(db);
return wiredb;
out_err:
g_warning("failed to readback wire db");
if (db)
g_key_file_free(db);
g_free(wiredb);
return NULL;
}
/*
* Free a wire db
*/
void free_wiredb(wire_db_t *wires) {
GStringChunk *wirenames = wires->wirenames;
if (wirenames)
g_string_chunk_free(wirenames);
g_free((void *)wires->details);
g_free(wires->names);
empty_db(wires, wires->dblen);
g_free((void *)wires->wires);
g_free(wires);
}
#endif /* __COMPILED_WIREDB */
/*
* Interface functions needed by wiring.h
*/
static inline gint cmp(const gchar *s1, const gchar *s2) {
return strcmp(s1,s2);
}
/* This one is a get_by_name. The only complicated function, which
is a dichotomy */
gint parse_wire_simple(const wire_db_t *db, wire_atom_t* res,
const gchar *wire) {
int low = 0, high = db->dblen-1;
// g_assert(high <= db->dblen);
do {
int middle = (high + low) / 2;
const char *middlename = wire_name(db, middle);
int comp = cmp(wire,middlename);
if (!comp) {
*res = middle;
return 0;
}
if (comp > 0)
low = middle + 1;
else
/* high can underflow here, so we want it to be signed */
high = middle - 1;
} while(low <= high);
return -1;
}
/** \brief Query the wiring database to get the copper startpoint of
* the orig wire
*
* Ask the wiring database so as to get back to the wire startpoint
*
* @param pipdb the pip database
* @param wire the wire to fill in
* @param orig the source wire
*
* @return error return
*/
gboolean
get_wire_startpoint(const wire_db_t *wiredb,
const chip_descr_t *chipdb,
site_ref_t *starget,
wire_atom_t *wtarget,
const site_ref_t sorig,
const wire_atom_t worig) {
const wire_simple_t *wo = &wiredb->wires[worig];
wire_atom_t target, ep = wo->ep;
unsigned dxy = 0;
site_ref_t ep_site;
debit_log(L_WIRES, "getting startpoint of wire %s",
wire_name(wiredb, worig));
/* This is how we detect unknown wires in the db */
if (ep == worig)
return FALSE;
ep_site = translate_global_site(chipdb, sorig, -wo->dx, -wo->dy);
if (ep_site == SITE_NULL) {
/* If the endpoint accepts projections, which should be the case */
if (!wiredb->wires[ep].fut) {
g_warning("no projection for wire %s",
wire_name(wiredb, worig));
return FALSE;
}
ep_site = project_global_site(chipdb, sorig, -wo->dx, -wo->dy, &dxy);
g_assert( dxy < wiredb->wires[ep].fut_len );
target = wiredb->wires[ep].fut[dxy];
/* This should be removed once the implicit databases are
complete */
if (target == WIRE_EP_END) {
g_warning("undefined projection for wire %s",
wire_name(wiredb, worig));
return FALSE;
}
g_warning("found projection for wire %s, %s",
wire_name(wiredb, worig),
wire_name(wiredb, target));
*wtarget = target;
*starget = ep_site;
return TRUE;
}
*wtarget = ep;
*starget = ep_site;
return TRUE;
}
/* \brief Print a sited_pip_t
*
* @param buf the string buffer where to do the print
* @param wdb the wire database
* @param spip the sited pip to print
*/
int
snprint_spip(gchar *buf, size_t bufs,
const wire_db_t *wdb,
const chip_descr_t *chip,
const sited_pip_t *spip) {
const gchar *start = wire_name(wdb, spip->pip.source);
const gchar *end = wire_name(wdb, spip->pip.target);
gchar site_buf[MAX_SITE_NLEN];
snprint_switch(site_buf, ARRAY_SIZE(site_buf),
chip, spip->site);
return snprintf(buf, bufs, "pip %s %s -> %s",
site_buf, start, end);
}