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