[英]Data model for logic gates simulation
我正努力为逻辑门开发一个模拟器。 模拟器需要计算给定电路的真值表。
这是一个示例电路。 a
, b
, c
, d
, e
是输入,而z
是输出。
我是编程新手。 我找不到建模门的方法。 你能告诉我一种方法吗?
对于每个输入,您将需要为状态(真,假)生成真值表。 因此,如果您有5个输入=总组合= 2 ^ 5。
您没有指定您需要哪种语言。 因此,我将为您提供一种流程化Java
方法。
我假设您已经为所有不同的门定义了所有特定的函数,例如XNOR
, AND
OR
等。例如,您可以为XNOR
门定义一个函数,如下: boolean XNOR(boolean ip1, boolean ip2)
现在,该过程简化为生成输入的所有组合(2 ^ 5)。 这可以简化为简单的置换问题-您可以通过以下方式进行操作:(其想法是将值从数组的结尾更改为开头。由于只需要两个值,因此很容易实现)
//inputs - all initialized to FALSE; - ready for 1st case of (2^5)
//Let the inputs a,b,c,d,e correspond to values of this array
boolean inp[]=new boolean[5];
//need a pointer variable for the array
//first pointing to the last-1 element of the array
int main_col=inp.length-2;
//Generate the combinations for input from all FALSE to until you reach all inputs to TRUE values
boolean looptf=true;
while(looptf){
call_Appropriate_gates_from_inputs(inp);
inp[inp.length-1]=!inp[inp.length-1]; //last array element value changed
call_Appropriate_gates_from_inputs(inp);
inp[inp.length-1]=!inp[inp.length-1]; //reset
for(int i=inp.length-2;i>=0;i--){
if (inp[i]){
inp[i]=false;
if (main_col==i){
main_col--;
if (main_col<0){
looptf=false;
break;
}
}
}else{
inp[i]=true;
break;
}
}//for
}//while
现在,您可以定义方法call_Appropriate_gates_from_inputs(boolean[])
并执行门逻辑并获取结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.