atpg-ls/src/circuit.h
2023-06-30 03:26:15 +00:00

128 lines
2.6 KiB
C++

#pragma once
#include <string>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <algorithm>
#include <assert.h>
using ll = long long;
class Gate {
public:
// gate basic info
int id;
int level;
int rtopo;
std::string name;
enum Type { AND, NAND, OR, NOR, XOR, XNOR, NOT, BUF, INPUT } type;
int value;
bool isPI;
bool isPO;
std::vector<Gate*> fanouts;
std::vector<Gate*> fanins;
std::vector<Gate*> reigon;
// circuit-ls info
int value_satisfied;
int value_unsatisfied_cost;
int fault_propagated_len[2];
int fault_propagated_weight[2];
int fault_detected[2];
int fault_detected_weight[2];
int fault_need_update;
int fault_need_update_cost;
int CC;
bool is_propagated();
int cal_value();
bool cal_fault_detected(bool x);
bool is_detected(Gate* one_of_input);
int cal_propagate_len(bool x);
void update_gate_property();
void update_gate_statistics();
// ( partical ) score
// score(x) = for y in neibor(x) { ~V_x,V_y make - ~V_x,V_y break }
// [ cal_FPL(~Vx, fanouts) - cal_FPL(Vx, fanouts) ] * FLP_WEIGHT(x)
// [ cal_FD(~Vx, fanouts) - cal_FD(Vx, fanouts) ] * FD_WEIGHT(x)
// [ cal_FPLS(~Vx, fanouts) - cal_FPLS(Vx, fanouts) ] * - FPLS_COST(x)
// [ cal_FDS(~Vx, fanouts) - cal_FDS(Vx, fanouts) ] * - FDS_COST(x)
int score;
int score_value_unsatisfied_cost;
int score_fault_propagated_weight[2];
int score_fault_detected_weight[2];
int score_fault_update_cost;
// score calculation function
int cal_score(int debug=0);
int cal_value_unsatisfied_cost();
int cal_score_fault_propagated_weight(int sa);
int cal_score_fault_detected_weight(int sa);
int cal_score_fault_update_cost();
};
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*> rtopo_gates;
std::unordered_map<std::string, Gate*> name2gate;
// std::queue<Gate*> tmp;
// std::unordered_map<Gate*, bool> tmp_used;
void init_topo_index();
int MAX_GATE_LEVEL;
void init_gate_level();
void init_reigons();
void parse_from_file(const char *filename);
void print_gates();
// local search
bool local_search(std::unordered_set<Fault*> &faults);
void ls_init_circuit(std::unordered_set<Fault*> &faults);
void ls_update_weight();
Gate* ls_pick();
void ls_flip(Gate* stem);
void ls_update(Gate* stem);
// checker
int check_circuit();
};