HDLBits Day15 Rule 90 and Rule 110

本文探讨了一维细胞自动机Rule90和Rule110的特性及Verilog实现。Rule90以其简单而有趣的性质,通过细胞的异或运算展现复杂模式。Rule110则更为复杂,需通过真值表化简,其Verilog代码实现了更复杂的逻辑更新规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.Rule 90
Rule 90 is a one-dimensional cellular automaton with interesting properties.

The rules are simple. There is a one-dimensional array of cells (on or off). At each time step, the next state of each cell is the XOR of the cell’s two current neighbours. A more verbose way of expressing this rule is the following table, where a cell’s next state is a function of itself and its two neighbours:
在这里插入图片描述
错误答案:

module top_module(
    input clk,
    input load,
    input [511:0] data,
    output [511:0] q ); 

    always@(posedge clk)begin
        if(load)
            q<= data;
        else
            q[510:1] <=q[511:2] ^ q[509:0];
            q[511] <=q[510];
    	    q[0] <= q[1];
    end
    
    
endmodule

在这里插入图片描述

错误处就是else中没有使用begin-end.应养成良好的代码习惯,每个层次用begin-end,哪怕该层次中只有一句话,因为你后面可能会扩写该层次,然后忘记加begin-end了,导致出错。还有在verilog一个代码块中对同一变量多次赋值,只保留最后一次赋值结果,如上代码中,q[0]在load时就load的q[1],即数据没有装载进去;不允许在不同块中对同一变量进行赋值。
改正时加上个begin-end就好了。
2.Rule110
在这里插入图片描述
Rule110就没有Rule90那样简单的异或关系了,可列真值表进行化简。
另,由于关系复杂,q[511]和q[0]的值也不便单独给出,可设一wire型变量q_left,q_right,便于统一处理。

module top_module(
    input clk,
    input load,
    input [511:0] data,
    output [511:0] q
); 
    wire [511:0] q_left, q_right;
    assign q_left = {1'b0,q[511:1]};
    assign q_right = {q[510:0],1'b0};
    
    
    always@(posedge clk) begin
        if(load)
            q <= data;
    else  begin
        q <= (q & ~q_right) | (~q_left & q_right) | (~q & q_right);
    end
        end
endmodule
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值