File size: 9,240 Bytes
d1be154
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#include <ot/spef/spef.hpp>

/*namespace ot::spef {

// Function: is_keyword
bool is_keyword(const std::string& str) {
  return keywords.find(str) != keywords.end();
}

// Function: to_string
std::string to_string(ConnectionType t) {
  switch(t) {
    case ConnectionType::INTERNAL:
      return "*I";
    break;

    case ConnectionType::EXTERNAL:
      return "*P";
    break;

    default:
      assert(false); 
    break;
  }
}

// Function: to_string
std::string to_string(ConnectionDirection d) {
  switch(d) {
    case ConnectionDirection::INPUT:
      return "I";
    break;

    case ConnectionDirection::OUTPUT:
      return "O";
    break;

    case ConnectionDirection::INOUT:
      return "B";
    break;

    default:
      assert(false);
    break;
  }
}

// Function: unmap
// TODO
void unmap(const std::unordered_map<std::string, std::string>& map, std::string& name) {
  
  if(map.empty()) {
    return;
  }

}

// ------------------------------------------------------------------------------------------------

// Constructor
Connection::Connection(const std::string& n, ConnectionType t, ConnectionDirection d) :
  name {n},
  type {t},
  direction {d}  {
}

// ------------------------------------------------------------------------------------------------

// Procedure: scale_capacitance
void Net::scale_capacitance(float s) {
  lcap *= s;
  for(auto& c : caps) {
    std::get<1>(c) *= s;
  }
}

// Procedure: scale_resistance
void Net::scale_resistance(float s) {
  for(auto& r : ress) {
    std::get<2>(r) *= s;
  }
}

// Operator <<
std::ostream& operator << (std::ostream& os, const Net& net) {
    
  os << "*D_NET " << net.name << ' ' << net.lcap << '\n';
  
  os << "*CONN\n";
  for(const auto& c : net.connections) {
    os << to_string(c.type) << ' ' 
       << c.name << ' ' 
       << to_string(c.direction) << '\n';
  }
  
  auto cap_counter {0};
  os << "*CAP\n";
  for(const auto& [key, cap] : net.caps) {
    os << ++cap_counter << ' ' << key << ' ' << cap << '\n';
  }

  auto res_counter {0};
  os << "*RES\n";
  for(const auto& [n1, n2, res] : net.ress) {
    os << ++res_counter << ' ' << n1 << ' ' << n2 << ' ' << res << '\n';
  }
  os << "*END\n";

  return os;
}

// ------------------------------------------------------------------------------------------------

// Operator <<
std::ostream& operator << (std::ostream& os, const Spef& spef) {

  // header

  // Name map section
  if(!spef.name_map.empty())  {
    os << "*NAME_MAP\n";
    for(const auto& [k, v] : spef.name_map) {
      os << k << ' ' << v << '\n';
    }
    os << '\n';
  }

  // Internal section
  for(size_t i=0; i<spef.nets.size(); ++i) {
    if(i) os << '\n';
    os << spef.nets[i];
  }
  return os;
}


// Procedure: read_spef
// *T_UNIT 1 PS
// *C_UNIT 1 FF
// *R_UNIT 1 KOHM
// *L_UNIT 1 UH
void Spef::read(const std::filesystem::path& path) {
  
  OT_LOGE_RIF(
    path.empty() || !std::filesystem::exists(path),
    "spef ", path, " doesn't exist"
  );

  OT_LOGI("loading spef ", path, " ...");

  auto tokens = tokenize(path);

  for(size_t i=0; i<tokens.size(); ++i) {
    
    // divider
    if(tokens[i] == "*DIVIDER") {
      if(i+1 >= tokens.size() || tokens[i+1].size() != 1) {
        OT_LOGF("syntax error in *DIVIDER section");
      }
      divider = tokens[i+1][0];
      i += 1;
    }
    // delimiter
    else if(tokens[i] == "*DELIMITER") {
      if(i+1 >= tokens.size() || tokens[i+1].size() != 1) {
        OT_LOGF("syntax error in *DELIMITER section");
      }

      delimiter = tokens[i+1][0];
      i += 1;
    }
    // time unit section
    else if(tokens[i] == "*T_UNIT") {
      if(i+2 >= tokens.size()) {
        OT_LOGF("syntax error in *T_UNIT section");
      }
      time_unit = make_time_unit(tokens[i+1] + to_lower(tokens[i+2]));
      i += 2;
    }
    // capacitance unit
    else if(tokens[i] == "*C_UNIT") {
      if(i+2 >= tokens.size()) {
        OT_LOGF("syntax error in *C_UNIT section");
      }
      capacitance_unit = make_capacitance_unit(tokens[i+1] + to_lower(tokens[i+2]));
    }
    // resistance unit
    else if(tokens[i] == "*R_UNIT") {
      if(i+2 >= tokens.size()) {
        OT_LOGF("syntax error in *R_UNIT section");
      }
      resistance_unit = make_resistance_unit(tokens[i+1] + to_lower(tokens[i+2]));
    }
    // name mapping section
    else if(tokens[i] == "*NAME_MAP") {
      while(i+2 < tokens.size()) {
        if(is_keyword(tokens[i+1]) || is_keyword(tokens[i+2])) {
          break;
        }
        name_map.try_emplace(std::move(tokens[i+1]), std::move(tokens[i+2]));
        i += 2;
      }
    }
    // Net section
    else if(tokens[i] == "*D_NET") {

      if(i+2 >= tokens.size()) {
        OT_LOGF("syntax error in *D_NET section");
      }

      Net net {std::move(tokens[i+1]), std::stof(tokens[i+2])}; 

      i += 2;

      while(++i < tokens.size()) {
        // *CONN section
        // *P external_connection direction {conn_attr} | 
        // *I internal_connection direction {conn_attr}
        if(tokens[i] == "*CONN") {
          auto j = i;
          while(++i < tokens.size()) {

            if(tokens[i] != "*P" && tokens[i] != "*I") {
              i = j;
              break;
            }
            
            if(i+2 >= tokens.size()) {
              OT_LOGF("syntax error in *CONN section");
            }
            
            auto t = (tokens[i] == "*P") ? ConnectionType::EXTERNAL : 
                                           ConnectionType::INTERNAL ;

            if(auto& k = tokens[i+2]; k != "I" && k != "O" && k != "B") {
              OT_LOGF("syntax error in parsing direction ", k);
            }
            
            auto d = (tokens[i+2] == "I") ? ConnectionDirection::INPUT  :
                    ((tokens[i+2] == "O") ? ConnectionDirection::OUTPUT : 
                                            ConnectionDirection::INOUT);
            
            net.connections.emplace_back(tokens[i+1], t, d);

            i += 2;

            // tyr to read attribute if any
            if(i+1 < tokens.size()) {
              while(tokens[i+1] == "*C" || tokens[i+1] == "*L" || tokens[i+1] == "*D") {
                // coordinate
                if(tokens[i+1] == "*C") {
                  if(i+3 >= tokens.size()) {
                    OT_LOGF("syntax error in parsing attribute *C");
                  }
                  // TODO
                  i += 3;
                }
                // load capacitance
                else if(tokens[i+1] == "*L") {
                  if(i+2 >= tokens.size()) {
                    OT_LOGF("syntax error in parsing attribute *L");
                  }
                  // TODO
                  i += 2;
                }
                // driving cell
                else {
                  if(i+2 >= tokens.size()) {
                    OT_LOGF("syntax error in parsing attribute *D");
                  }
                  // TODO
                  i += 2;
                }
              }
            }

            j = i;
          }
        }
        // *CAP section.
        else if(tokens[i] == "*CAP") {
          auto j = i;
          while(++i < tokens.size()) {
            if(!::isdigit(tokens[i][0])) {
              i = j;
              break;
            }

            if(i+2 >= tokens.size()) {
              OT_LOGF("syntax error in *CAP section");
            }

            net.caps.emplace_back(std::forward_as_tuple(
              std::move(tokens[i+1]), 
              std::stof(tokens[i+2])
            ));
            i += 2;
            j = i;
          }
        }
        // *RES section.
        else if(tokens[i] == "*RES") {
          auto j = i;
          while(++i < tokens.size()) {
            if(!::isdigit(tokens[i][0])) {
              i = j;
              break;
            }

            if(i+3 >= tokens.size()) {
              OT_LOGF("syntax error in *RES section");
            }

            net.ress.emplace_back(std::forward_as_tuple(
              std::move(tokens[i+1]),
              std::move(tokens[i+2]),
              std::stof(tokens[i+3])
            ));
            i += 3;
            j = i;
          }
        }
        // *END section.
        else if(tokens[i] == "*END") {
          nets.push_back(std::move(net));
          break;
        }
        else {
          OT_LOGF("unexpected token ", tokens[i], " in *D_NET section");
        }
      }
    }
    else {
      //OT_LOGW("unexpected token ", tokens[i]);
    }
  }
}
  
// Procedure: to_capacitance_unit
void Spef::to_capacitance_unit(const CapacitanceUnit& unit) {
   
  float s = (capacitance_unit) ? divide_capacitance_unit(*capacitance_unit, unit) : 1.0f;

  if(capacitance_unit = unit; std::fabs(s - 1.0f) < 1e-6) {
    return;
  }

  for(auto& n : nets) {
    n.scale_capacitance(s);
  }
}

// Procedure: to_resistance_unit
void Spef::to_resistance_unit(const ResistanceUnit& unit) {
   
  float s = (resistance_unit) ? divide_resistance_unit(*resistance_unit, unit) : 1.0f;

  if(resistance_unit = unit; std::fabs(s - 1.0f) < 1e-6) {
    return;
  }

  for(auto& n : nets) {
    n.scale_resistance(s);
  }
}

};  // end of namespace ot. ----------------------------------------------------------------------- */