Logic synthesis和仿真

假设我们要做个电路,输入为0到3的数,输出为4盏灯ABCD。输入为0时只有A灯亮,为3时只有D灯亮。它是个2-4 Decoder,输入XY两根线,输出ABCD4根线。输入不叫High和Low的原因后面会提到。

下面是用Verilog写的Encoder,懒得写Decoder了,大同小异:

module encoder(out, in, enable);
output[1:0]    out;
reg[1:0]        out;
input[3:0]     in;
input                enable;
always @ (enable or in) begin
if(enable) begin
    if(in == 1) begin out = 0; end
    if(in == 2) begin out = 1; end
    if(in == 4) begin out = 2; end
    if(in == 8) begin out = 3;    end
end
end endmodule
module test_encoder(); // 部分代码
wire[1:0]    out;
reg[3:0]    in;
initial begin
    #5 enable = 1;
    #1 in = 1;
end
encoder testee(out, in, enable);

Verilog里除了if语句,还有switch和for语句,所以我一直很困惑:
1. 先不说switch和for,与或非三个门怎么实现if语句?
2. 为啥不用C++:

#define module class
#define begin {
#define end }
// reg(ister)是成员变量,wire是函数参数,转瞬即逝? testee是test_encoder的成员变量,如果对应的C++程序能编译通过,不用操心电会乱流? 
// 据说有时候test比testee还难写,比如testee高速判断一个数是不是素数,test要从网上下载素数表,Verilog写不了socket程序吧?
// 能不能Verilog to C,然后python调用C?

我们不需要(A,B,C,D)=f(X,Y), f这个函数太高档了,A=f1(X,Y),...,D=f4(X,Y)即可。也许真值表不能告诉我们怎么干,但肯定可以说清楚干什么。

tt = [ # truth table # 傻了,真值表应为4行,只写了2行!不过原理基本上是对的。:-) 傻了傻了,再一看好像又没错。:--)
# x 0  1      y
  [ 1, 0 ], # 0
  [ 0, 0 ]  # 1
]
def ok(e):
    for y in (0,1):
        for x in (0,1):
            if eval(e) != tt[y][x]: return False
    return True
print(ok('not(x or y)'))

《数字电路基础》讲了如何系统地、高效地从真值表生成逻辑表达式。A=f1(X,Y)我们能凑出来。我们还能一个个地试:

q = ['(x)', '(y)']; h = 0
for i in range(10):
    if h + 2 > len(q): break
    a = q[h]; b = q[h+1]; h += 2
    q.append('(not' + a + ')')
    q.append('(not' + b + ')')
    q.append('('+ a + 'and' + b + ')')
    q.append('('+ a + 'or' + b + ')')
for e in q:
    if ok(e): print('***', e)
*** ((not(x))and(not(y)))
*** (not((x)or(y)))
*** (((not(x))and(not(y)))and((not(x))or(not(y))))
*** ((not((x)and(y)))and(not((x)or(y))))
*** (not(((x)and(y))or((x)or(y))))
*** ((not(not(not(x))))and(not(not(not(y)))))
*** (not((not(not(x)))or(not(not(y)))))
*** (not(not((not(x))and(not(y)))))

上面的程序有bug. xor和任何逻辑表达式都可以用not, and, or not and来表示: 非门和与门俩门,或与非门一个吗? nand可生成所有逻辑表达式

logic - Is XOR a combination of AND and NOT operators? - Mathematics Stack Exchange

There is a story about certain military devices being designed using only NAND gates, so that only one part needs to be certified, stocked as spares, etc. I don't know whether it's actually true.

能做和能高效地做差着很远很远呢。据说科学家们在研究solver,好像是:判断是否存在一组变量的值,使得某个用它们组合出的逻辑表达式的值为True. 听起来很简单,可如果有32个变量,就要把2**32个不同的值带进式子里去算,所以必须找算法优化,这就难了。

可是Logic synthesis好像更难:我没表达式啊,请你给我找个表达式出来,要短的不要长的。好像关键是要短表达式。不优化的长表达式好像比solver简单。

为啥把输入叫X和Y? 当然不是为了找表达式时在屏幕上亮点玩。深度学习处理图像,搞人脸识别啥的是强项啊。视频也许可看作"3D"的图像,多了个时间轴,和VR的3D意思不一样。科学家们在研究用深度学习做Logic synthesis.

也许用Verilog和用C++/Python的人应该互相学习和帮助,不带用Jxxx和Cx的人玩。:-) 最好能有个v2cpp - Verilog to C++. Icarus的iverilog输出汇编伪码,也许可以把它转换成x86 asm. 不现实。opcode.txt: %abs/wr This instruction calculates the absolute value of a real value. 不过"汇编"变C,又不要求转换后代码的可读性,应该是可行的。Thread objects in vvp are created by .thread statements in the input source file. A thread object includes a program counter and private bit registers. 不过vvp好像没有用pthread_create.

和4张牌算24有一点点像啊。 挺强的算24程序,支持2重() 博客园 关键是变量的个数啊: 在线卡诺图@博客园

布丰投针和蒙特卡洛法可以算pi。也许可以从一堆门中随机挑两个、组合下、丢回去,如此不断重复:发疯的蒙娜丽莎。太拼人品了:

  • (not((not(not((y)and(x))and(not(y)and(x))))and((((y)and(x))and not(not(x)))and not(not(not((not(y))and not(not((y)and(x))and(not(y)and(x))))))))and(not((not(y))and not(not((y)and(x))and(not(y)and(x))))))
  • (not((not(not(x)and(y)))and(not((x)and not(y))))and(not(not(not(((x)and not(y))and(y))))))

但这个方法适合用机群计算,相互之间不必通信啊。:-) 而且可以用C++来写,就算不用树,用&a!b这样的前缀表达式也好啊,运算符和opnd都单字符(a-zA-Z 52个了)。不知道是if(c=='&‘)效率高呢,还是函数指针表高。C++不支持inline函数表吧,也许可以用Verilog来写,发疯的蒙娜丽莎板。

posted @ 2021-12-22 10:38  Fun_with_Words  阅读(83)  评论(0)    收藏  举报









 张牌。