这是一个由C语言写的组合逻辑仿真代码,大致过程为读入电路(电路格式如下所示)、读入各个输入(输入格式如下所示)、仿真三个步骤,前两个步骤完全正确,最后的仿真结果出错,实在找不到哪里错了,请求帮助吼。。
//gate.h 头文件
//gate related information
#ifndef _GATE_H
#define _GATE_H
#include <stdio.h>
#include <stdlib.h>
enum GateType{
T_UNUSED, /*0*/
T_INPUT, /*1*/
T_OUTPUT, /*2*/
T_XOR, /*3*/
T_XNOR, /*4*/
T_DFF, /*5*/
T_AND, /*6*/
T_NAND, /*7*/
T_OR, /*8*/
T_NOR, /*9*/
T_NOT, /*10*/
T_BUF, /*11*/
};
struct Gate{
int id; //gate id
int type; //gate type
int level; //gate level
int num_pred; //number of inputs/predecessors
int * pred_c0; //inputs in order of decreasing c0
int * pred_c1; //inputs in order of decreasing c1
int num_succ; //number of successors
int *succ; //successors in order of decreasing obs.value
int obs; //observability value
int c0; //controllability 0 value
int c1; //controllability 1 value
int val; //value during simulation
};
#endif
//circuit.h 头文件
//store circuit information
#ifndef _CIRCUIT_H
#define _CIRCUIT_H
#include "gate.h"
struct Circuit {
int num_of_gates;
int unused;
struct Gate * nodes;
};
//read
int read_circuit(FILE* ckt_fp,struct Circuit** ckt);
//dump
int dump_circuit(FILE* output_fp,struct Circuit* ckt);
//delete
int delete_circuit(struct Circuit** ckt);
#endif
//vector.h
#ifndef _VECTOR_H
#define _VECTOR_H
#include <stdio.h>
#include <stdlib.h>
typedef struct InputVectors{
int num_inputs;
int num_vectors;
char **vectors;
}TestVectors;
int read_vectors(FILE* vec_fp,TestVectors ** tvectors_pp);
int dump_vectors(FILE* out_fp,TestVectors * tp);
int delete_vectors(TestVectors **tp);
#endif
//simulate.h
#ifndef _SIMULATE_H
#define _SIMULATE_H
#include "circuit.h"
#include "vector.h"
typedef struct Simulate{
const struct Circuit * ckt;
const TestVectors * vec;
int * gmv;
}Simulate;
void init_simulator(Simulate ** sim_pp,
const struct Circuit * ckt,
const TestVectors * vec);
void simulate_vectors(Simulate* sim,FILE * fp_out);
#endif
//simulate.c
#include "simulate.h"
#include <assert.h>
void init_simulator(Simulate ** sim_pp,
const struct Circuit * ckt,
const TestVectors* vec)
{
assert(*sim_pp == NULL);
*sim_pp = malloc(sizeof(Simulate));
Simulate *sim = *sim_pp;
sim->ckt = ckt;
sim->vec = vec;
sim->gmv = malloc(sim->ckt->num_of_gates * sizeof(int));
}
void simulate_vectors(Simulate* sim,FILE * fp_out)
{
for(int v= 0;v<sim->vec->num_vectors;v++){
for(int i=0;i<(sim->vec->num_inputs);i++){
sim->gmv[i] = (int)sim->vec->vectors[v][i];
}
for(int i = sim->vec->num_inputs;i < sim->ckt->num_of_gates;i++){
struct Gate *gat = sim->ckt->nodes +i;
switch(gat->type){
case T_INPUT:{ //#1
break;
}
case T_OUTPUT:{ //#2
sim->gmv[i] = sim->gmv[gat->pred_c0[0]];
break;
}
case T_XOR:{ //#3
int val = 0;
for(int p = 0;p < (gat->num_pred)-1;p++){
if(sim->gmv[gat->pred_c0[p]] !=sim->gmv[gat->pred_c0 [p+1]]){
val++;
}
}
if((val == 0)){ //all inputs are the same
sim->gmv[i] = 0;
}else{
sim->gmv[i] = 1;
}
break;
}
case T_XNOR:{ //#4
int val = 0;
for(int p = 0;p < (gat->num_pred)-1;p++){
if(sim->gmv[gat->pred_c0[p]] !=sim->gmv[gat->pred_c0 [p+1]]){
val++;
}
}
if((val == 0)){ //all inputs are the same
sim->gmv[i] = 1;
}else{
sim->gmv[i] = 0;
}
break;
}
case T_AND:{ //#6
int val = 1;
for(int p=0;p < (gat->num_pred);p++){
val &= sim->gmv[gat->pred_c0[p]];
}
sim->gmv[i] = val;
break;
}
case T_NAND:{ //#7
int val = 0xffffffff;
for(int p=0;p < (gat->num_pred);p++){
val = val & (sim->gmv[gat->pred_c0[p]]);
}
sim->gmv[i] = ~ val;
break;
}
case T_OR:{ //#8
int val = 0;
for(int p=0 ;p<gat->num_pred;p++){
val |= sim->gmv[gat->pred_c0[p]];
}
sim->gmv[i] = val;
break;
}
case T_NOR:{ //#9
int val = 0;
for(int p=0 ;p<gat->num_pred;p++){
val |= sim->gmv[gat->pred_c0[p]];
}
sim->gmv[i] = ~val;
break;
}
case T_NOT:{ //#10
sim->gmv[i] = ~(sim->gmv[gat->pred_c0[0]]);
break;
}
case T_BUF:{ //#11
sim->gmv[i] = sim->gmv[gat->pred_c0[0]];
break;
}
case T_DFF:{ //#5
printf("unknown gate type:%d\n",gat->type);
break;
}
}
}
//look for errors
/* printf("test vector %d\n",v);
printf("sim values:\n");
for(int i=0;i<sim->ckt->num_of_gates;i++){
if(sim->gmv[i] & 0x1){
printf("1 ");
} else {
printf("0 ");
}
}
printf("\n");
*/
//output
for(int i = sim->vec->num_inputs;i<sim->ckt->num_of_gates;i++){
struct Gate *gat = sim->ckt->nodes + i;
if((T_OUTPUT == gat->type)){
if(sim->gmv[i] & 0x1){
fprintf(fp_out,"1");
} else {
fprintf(fp_out,"0");
}
}
}
fprintf(fp_out,"\n");
}
}
实际仿真结果(结果不对):
正确结果应是:
输入电路:
电路的输入: