1 /*
2 * Copyright (c) 2013 Stephen Williams (steve@icarus.com)
3 *
4 * This source code is free software; you can redistribute it
5 * and/or modify it in source code form under the terms of the GNU
6 * General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20 # include "nex_data.h"
21 # include <iostream>
22 # include <cstdlib>
23 # include <cstdio>
24 # include <cstring>
25 # include <cassert>
26
27 using namespace std;
28
blif_nex_data_t(ivl_nexus_t nex)29 inline blif_nex_data_t::blif_nex_data_t(ivl_nexus_t nex)
30 : nex_(nex), name_(0)
31 {
32 }
33
~blif_nex_data_t()34 blif_nex_data_t::~blif_nex_data_t()
35 {
36 if (name_) free(name_);
37
38 for (size_t idx = 0 ; idx < name_index_.size() ; idx += 1)
39 if (name_index_[idx]) free(name_index_[idx]);
40 }
41
get_nex_data(ivl_nexus_t nex)42 blif_nex_data_t* blif_nex_data_t::get_nex_data(ivl_nexus_t nex)
43 {
44 void*tmp = ivl_nexus_get_private(nex);
45 if (tmp != 0) return reinterpret_cast<blif_nex_data_t*> (tmp);
46
47 blif_nex_data_t*data = new blif_nex_data_t(nex);
48 ivl_nexus_set_private(nex, data);
49 return data;
50 }
51
make_name_from_sig_(ivl_signal_t sig)52 void blif_nex_data_t::make_name_from_sig_(ivl_signal_t sig)
53 {
54 assert(name_ == 0);
55
56 string tmp = ivl_signal_basename(sig);
57 for (ivl_scope_t sscope = ivl_signal_scope(sig) ; ivl_scope_parent(sscope) ; sscope = ivl_scope_parent(sscope)) {
58 tmp = ivl_scope_basename(sscope) + string("/") + tmp;
59 }
60
61 name_ = strdup(tmp.c_str());
62
63 assert(name_index_.size()==0);
64 if (ivl_signal_width(sig) > 1) {
65 name_index_.resize(ivl_signal_width(sig));
66
67 assert(ivl_signal_packed_dimensions(sig) == 1);
68 int msb = ivl_signal_packed_msb(sig,0);
69 int lsb = ivl_signal_packed_lsb(sig,0);
70 int dir = (lsb <= msb)? 1 : -1;
71
72 int val = lsb;
73 for (unsigned idx = 0 ; idx < ivl_signal_width(sig) ; idx += 1) {
74
75 char buf[64];
76 snprintf(buf, sizeof buf, "[%d]", val);
77 name_index_[idx] = strdup(buf);
78 val += dir;
79 }
80 }
81 }
82
83 /*
84 * Given that there is not an explicit binding to a signal for naming,
85 * search for a signal and use that signal to derive the name of this
86 * nexus.
87 */
select_name_(void)88 void blif_nex_data_t::select_name_(void)
89 {
90 for (unsigned idx = 0 ; idx < ivl_nexus_ptrs(nex_) ; idx += 1) {
91 ivl_nexus_ptr_t ptr = ivl_nexus_ptr(nex_, idx);
92 ivl_signal_t sig = ivl_nexus_ptr_sig(ptr);
93 if (sig == 0)
94 continue;
95
96 make_name_from_sig_(sig);
97 break;
98 }
99
100 assert(name_);
101 }
102
set_name(ivl_signal_t sig)103 void blif_nex_data_t::set_name(ivl_signal_t sig)
104 {
105 assert(name_ == 0);
106 assert(ivl_signal_nex(sig,0) == nex_);
107
108 make_name_from_sig_(sig);
109 }
110
get_name(void)111 const char* blif_nex_data_t::get_name(void)
112 {
113 if (name_==0) select_name_();
114 return name_;
115 }
116
get_name_index(unsigned bit)117 const char* blif_nex_data_t::get_name_index(unsigned bit)
118 {
119 if (name_==0) select_name_();
120
121 if (name_index_.size()==0) {
122 assert(bit == 0);
123 return "";
124 }
125
126 assert(bit < name_index_.size());
127 assert(name_index_[bit]);
128 return name_index_[bit];
129 }
130
131 /*
132 * Get the width from any signal that is attached to the nexus.
133 */
get_width(void)134 size_t blif_nex_data_t::get_width(void)
135 {
136 if (name_==0) select_name_();
137
138 // Special case: If the nexus width is 1 bit, then there is no
139 // need for index_name maps, so the name_index_ will be empty.
140 if (name_index_.size()==0)
141 return 1;
142
143 return name_index_.size();
144 }
145