File size: 6,574 Bytes
f61bac1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2023-2025, The OpenROAD Authors

#include <filesystem>
#include <fstream>
#include <ios>
#include <string>
#include <type_traits>
#include <variant>
#include <vector>

#include "gtest/gtest.h"
#include "helper.h"
#include "odb/db.h"
#include "odb/dbSet.h"

namespace odb {
namespace {

constexpr char kPartition1[] = "CHAIN_1_FALLING_1";
constexpr char kPartition2[] = "CHAIN_1_RISING_1";

template <class>
inline constexpr bool always_false_v = false;

std::string GetName(const std::variant<dbBTerm*, dbITerm*>& pin)
{
  return std::visit(
      [](auto&& pin) {
        using T = std::decay_t<decltype(pin)>;
        if constexpr (std::is_same_v<T, dbBTerm*>) {
          return pin->getName();
        } else if constexpr (std::is_same_v<T, dbITerm*>) {
          return pin->getMTerm()->getName();
        } else {
          static_assert(always_false_v<T>, "non-exhaustive visitor");
        }
      },
      pin);
}

class TestScanChain : public SimpleDbFixture
{
 protected:
  TestScanChain()
  {
    create2LevetDbWithBTerms();
    block_ = db_->getChip()->getBlock();
    dft_ = block_->getDft();

    std::vector<std::string> instances_names = {"i1", "i2", "i3"};

    for (const auto& name : instances_names) {
      instances_.push_back(block_->findInst(name.c_str()));
    }
  }

  void SetUpTmpPath(const std::string& name)
  {
    std::filesystem::create_directory("results");
    tmp_path_ = "results/" + name;
  }

  // Writes a temporal DB and then tries to read the contents back to check if
  // the serialization is working as expected
  dbDatabase* writeReadDb()
  {
    writeDb();
    return readDb();
  }

  void writeDb()
  {
    std::ofstream write;
    write.exceptions(std::ifstream::failbit | std::ifstream::badbit
                     | std::ios::eofbit);
    write.open(tmp_path_, std::ios::binary);
    db_->write(write);
  }

  dbDatabase* readDb()
  {
    dbDatabase* db = dbDatabase::create();
    std::ifstream read;
    read.exceptions(std::ifstream::failbit | std::ifstream::badbit
                    | std::ios::eofbit);
    read.open(tmp_path_, std::ios::binary);
    db->read(read);
    return db;
  }

  dbBlock* block_;
  std::string tmp_path_;
  dbDft* dft_;
  std::vector<dbInst*> instances_;
};

TEST_F(TestScanChain, CreateScanChain)
{
  SetUpTmpPath("CreateScanChain");
  dbInst* inst = block_->findInst("i1");
  dbITerm* iterm = inst->findITerm("a");
  dbBTerm* bterm = block_->findBTerm("IN1");

  dbScanChain* scan_chain = dbScanChain::create(dft_);

  dbScanPartition* scan_partition = dbScanPartition::create(scan_chain);
  scan_partition->setName(kPartition1);
  dbScanList* scan_list = dbScanList::create(scan_partition);

  dbScanInst* scan_inst = scan_list->add(inst);
  scan_inst->setBits(1234);
  scan_inst->setAccessPins({.scan_in = iterm, .scan_out = bterm});

  //*****************************
  dbDatabase* db2 = writeReadDb();
  dbBlock* block2 = db2->getChip()->getBlock();
  dbDft* dft2 = block2->getDft();

  odb::dbSet<dbScanChain> scan_chains2 = dft2->getScanChains();
  EXPECT_EQ(scan_chains2.size(), 1);

  dbScanChain* scan_chain2 = *scan_chains2.begin();

  odb::dbSet<dbScanPartition> scan_partition2
      = scan_chain2->getScanPartitions();
  EXPECT_EQ(scan_partition2.size(), 1);
  EXPECT_EQ(scan_partition2.begin()->getName(), kPartition1);

  odb::dbSet<dbScanList> scan_lists2 = scan_partition2.begin()->getScanLists();
  EXPECT_EQ(scan_lists2.size(), 1);

  odb::dbSet<dbScanInst> scan_insts2 = scan_lists2.begin()->getScanInsts();
  EXPECT_EQ(scan_insts2.size(), 1);
}

TEST_F(TestScanChain, CreateScanChainWithPartition)
{
  SetUpTmpPath("CreateScanChainWithPartition");
  dbScanChain* scan_chain = dbScanChain::create(dft_);

  std::vector<dbInst*> instances_partition1 = {instances_[0], instances_[1]};
  std::vector<dbInst*> instances_partition2 = {instances_[2]};

  dbScanPartition* partition1 = dbScanPartition::create(scan_chain);
  dbScanPartition* partition2 = dbScanPartition::create(scan_chain);

  partition1->setName(kPartition1);
  partition2->setName(kPartition2);

  for (dbInst* inst : instances_partition1) {
    dbScanList* scan_list = dbScanList::create(partition1);
    dbScanInst* scan_inst = scan_list->add(inst);
    scan_inst->setBits(1);
    dbITerm* iterm = inst->findITerm("a");
    dbITerm* iterm2 = inst->findITerm("o");
    scan_inst->setAccessPins({.scan_in = iterm, .scan_out = iterm2});
  }

  for (dbInst* inst : instances_partition2) {
    dbScanList* scan_list = dbScanList::create(partition2);
    dbScanInst* scan_inst = scan_list->add(inst);
    scan_inst->setBits(1);
    dbITerm* iterm = inst->findITerm("a");
    dbITerm* iterm2 = inst->findITerm("o");
    scan_inst->setAccessPins({.scan_in = iterm, .scan_out = iterm2});
  }

  // 2 partitions, one for the first two instances and one for the last one
  //
  // Partition 1: [i1, i2]
  // Partition 2: [i3]

  //*****************************
  dbDatabase* db2 = writeReadDb();

  dbBlock* block2 = db2->getChip()->getBlock();
  dbDft* dft2 = block2->getDft();

  dbSet<dbScanChain> scan_chains2 = dft2->getScanChains();
  EXPECT_EQ(scan_chains2.size(), 1);

  dbSet<dbScanPartition> scan_partitions2
      = scan_chains2.begin()->getScanPartitions();
  EXPECT_EQ(scan_partitions2.size(), 2);

  auto iterator = scan_partitions2.begin();

  dbScanPartition* partition12 = *iterator;
  ++iterator;
  dbScanPartition* partition22 = *iterator;

  EXPECT_EQ(partition12->getName(), kPartition1);
  EXPECT_EQ(partition22->getName(), kPartition2);

  // check the created instances

  dbSet<dbScanList> scan_lists12 = partition12->getScanLists();
  dbSet<dbScanList> scan_lists22 = partition22->getScanLists();

  int i = 0;
  for (dbScanList* scan_list : scan_lists12) {
    for (dbScanInst* scan_inst : scan_list->getScanInsts()) {
      const dbScanInst::AccessPins& access_pins = scan_inst->getAccessPins();
      EXPECT_EQ(GetName(access_pins.scan_in), "a");
      EXPECT_EQ(GetName(access_pins.scan_out), "o");
      EXPECT_EQ(instances_[i]->getName(), scan_inst->getInst()->getName());
      ++i;
    }
  }

  for (dbScanList* scan_list : scan_lists22) {
    for (dbScanInst* scan_inst : scan_list->getScanInsts()) {
      const dbScanInst::AccessPins& access_pins = scan_inst->getAccessPins();
      EXPECT_EQ(GetName(access_pins.scan_in), "a");
      EXPECT_EQ(GetName(access_pins.scan_out), "o");
      EXPECT_EQ(instances_[i]->getName(), scan_inst->getInst()->getName());
      ++i;
    }
  }
}

}  // namespace
}  // namespace odb