HDLbits Conwaylife

2022-07-24 14:18:17 浏览数 (1)

这道题好像没有什么巧妙的办法,只有用for循环

代码语言:javascript复制
module top_module(
    input clk,
    input load,
    input [255:0] data,
    output [255:0] q ); 
    
    integer i,j;
    integer m,n;
    integer x,y;

    // 18 * 18
    reg [304:-19] bq;

    // 16 * 16 -> 18 * 18
    always@(*) begin
        for(i = -1; i < 17; i  = 1) begin
            for(j = -1; j < 17; j  = 1) begin
                if(i == -1)m = 15;
                else if(i == 16)m = 0;
                else m = i;

                if(j == -1)n = 15;
                else if(j == 16)n = 0;
                else n = j;
                
                bq[i*18   j] = q[m*16   n];
            end
        end
    end
    
    reg [2:0] sum;
    always@(posedge clk) begin
        if(load)q <= data;
        else begin
            for(x = 0; x < 16; x  = 1) begin
                for(y = 0; y < 16; y  = 1) begin
                    sum = bq[x*18   y   1]   bq[x*18   y - 1]
                      bq[(x 1)*18   y]   bq[(x 1)*18   y   1]   bq[(x 1)*18   y - 1]
                      bq[(x-1)*18   y]   bq[(x-1)*18   y   1]   bq[(x-1)*18   y - 1];
                    case(sum)
                        3'b010: q[x*16   y] <= q[x*16   y];
                        3'b011: q[x*16   y] <= 1'b1;
                        default: q[x*16   y] <= 1'b0;
                    endcase                    
                end
            end
        end
    end    

endmodule

0 人点赞