atpg-ls/circuit.h
2023-03-04 10:39:33 +00:00

123 lines
2.5 KiB
C++

#pragma once
#include <string>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <queue>
using ll = long long;
class Gate {
public:
// gate structral info in circuit
int id;
std::string name;
enum Type { AND, NAND, OR, NOR, XOR, XNOR, NOT, BUF, INPUT } type;
bool stem;
bool pi;
bool po;
std::vector<Gate*> pre_stems;
std::vector<Gate*> suc_stems;
std::vector<Gate*> outputs;
std::vector<Gate*> inputs;
// Status Define (Two var)
bool value;
bool propagate;
// local search
bool stem_satisified;
int stem_cost;
// 和求得的值是否一致
bool propagate_satisfied;
int propagate_cost;
// 这个由 value 和 propagate 决定
int fault_satisfied[2];
int fault_cost[2];
// configuration checking
bool CC;
std::unordered_map<Gate*, std::pair<int, int>> sa_by_out;
int cal_value();
bool cal_sa(bool x);
//bool sa(bool x);
};
class Fault {
public:
Gate* gate;
enum Type { SA0, SA1 } type;
Fault(Gate* gate, Type type):gate(gate),type(type) {}
};
class Circuit {
public:
std::vector<Gate*> PIs;
std::vector<Gate*> POs;
std::vector<Gate*> gates;
std::vector<Gate*> stems; // PI + stems
std::unordered_map<std::string, Gate*> name2gate;
std::queue<Gate*> tmp;
std::unordered_map<Gate*, bool> tmp_used;
void parse_from_file(const char *filename);
void print_gates();
bool is_valid_circuit();
void init_topo_index();
void init_stems();
// local search
// shared info
static const int STEM_INC = 2;
static const int STEM_COST_MAX = 1e9;
static ll stem_total_cost;
static int stem_falsified_cnt;
static const int PROPAGATE_INC = 2;
static const int PROPAGATE_COST_MAX = 1e9;
static ll propagate_total_cost;
static int propagate_falsified_cnt;
static const int FAULT_INC = 1;
static const int FAULT_COST_MAX = 20;
static ll fault_total_cost;
static int fault_falsified_cnt;
bool local_search(std::unordered_set<Fault*> &faults);
// incremental flip struct
const double SP = 0.01;
void ls_init_circuit(const std::unordered_set<Fault*> &faults);
void ls_update_weight();
void ls_init_data_structs();
void ls_block_recal(Gate* stem);
void ls_block_recal_value(Gate* stem);
void ls_block_recal_fault(Gate* stem);
Gate* ls_pick_good_var(bool &value, bool &propagate);
Gate* ls_pick_falsified_var(bool &value, bool &propagate);
void ls_update(Gate* stem);
ll ls_pick_score(Gate* stem, bool value, bool propagate);
ll ls_score();
// simulator
int** simulate();
};