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:
规则 90 是一个具有有趣属性的一维元胞自动机。
规则很简单。有一个一维单元格数组(打开或关闭)。在每个时间步中,每个单元的下一个状态是单元的两个当前邻居的异或。下表是表达此规则的更详细的方式,其中单元格的下一个状态是其自身及其两个相邻单元的函数:
题目网站
module top_module(
input clk,
input load,
input [511:0] data,
output [511:0] q );
integer i;
always@(posedge clk)begin
if(load)begin
q<=data;
end
else begin
q<={1'b0,q[511:1]}^{q[510:0],1'b0};
end
end
endmodule
这个写法画一个图方便理解,是从整体出发的写法:
从这个图可以看出q<={1'b0,q[511:1]}^{q[510:0],1'b0};
中第一位0b就是原数列a的左右两位的异或,第二位ac就是原数列b的左右两位的异或……依此类推可得。
下面提供另一种写法:
module top_module(
input clk,
input load,
input [511:0] data,
output [511:0] q );
always @(posedge clk ) begin
if(load)begin
q <= data;
end
else begin
q <= (q<<1)^(q>>1);
end
end
endmodule
这个写法用q <= (q<<1)^(q>>1);
,写的更直观。