HDLbits刷题-康威生命游戏Conway‘s Game of Life

文章描述了一个利用SystemVerilog语言在HDLbits平台上实现康威生命游戏的过程,包括16x16网格的设定、状态更新规则和使用循环结构进行邻域计数的代码片段。作者试图通过函数实现分块操作但未成功。
摘要由CSDN通过智能技术生成

HDLbits刷题-康威生命游戏Conway's Game of Life

这是题目

The “game” is played on a two-dimensional grid of cells, where each cell is either 1 (alive) or 0 (dead). At each time step, each cell changes state depending on how many neighbours it has:

0-1 neighbour: Cell becomes 0.
2 neighbours: Cell state does not change.
3 neighbours: Cell becomes 1.
4+ neighbours: Cell becomes 0.
The game is formulated for an infinite grid. In this circuit, we will use a 16x16 grid. To make things more interesting, we will use a 16x16 toroid, where the sides wrap around to the other side of the grid. For example, the corner cell (0,0) has 8 neighbours: (15,1), (15,0), (15,15), (0,1), (0,15), (1,1), (1,0), and (1,15). The 16x16 grid is represented by a length 256 vector, where each row of 16 cells is represented by a sub-vector: q[15:0] is row 0, q[31:16] is row 1, etc. (This tool accepts SystemVerilog, so you may use 2D vectors if you wish.)

load: Loads data into q at the next clock edge, for loading initial state.
q: The 16x16 current state of the game, updated every clock cycle.
The game state should advance by one timestep every clock cycle.

我想通过函数来写,直接分块操作,但是没有运行成功,不知道怎么回事,能力有限

我的代码

module top_module(
    input clk,
    input load,
    input [255:0] data,
    output [255:0] q ); 
    
    integer i , j;//行号和列号,0-15
                          
    always @(posedge clk) begin
        if(load) q <= data;
        else begin
            for(i = 0; i<16; i++) begin
                for(j = 0; j<16; j++) begin                 
                    q[x(i,j)] <= state_new(q_new(i,j,q), q[x(i,j)]);
                end
            end
        end
    end
                    
           
//x代表q的序列号,第a行第b列的序号为x   
    function [31:0] x(input a, b); 
        x = 16*a + b;
    endfunction
    
// 取得一个元素周围的8个元素,并把这八个元素作为q_new输出    
   function [7:0] q_new(input i,j,
                        input [255:0] q);
        begin
            //前四个为表格的四个角
            if(i == 0 && j == 0) //右下角
                q_new = {q[x(i+1,j+1)], q[x(i+1,j)],   q[x(i+1,j+15)], q[x(i,j+1)], q[x(i,j+15)],
                         q[x(i+15,j+1)], q[x(i+15,j)], q[x(i+15,j+15)]};
            else if(i == 0 && j == 15)  //左下角
                q_new = {q[x(i+1,j-15)],  q[x(i+1,j)],  q[x(i+1,j-1)], q[x(i,j-15)], q[x(i,j-1)],
                         q[x(i+15,j-15)], q[x(i+15,j)], q[x(i+15,j-1)]};
            else if(i == 15 && j == 0)  //右上角
                q_new = {q[x(i-15,j+1)],  q[x(i-15,j)], q[x(i-15,j+15)], q[x(i,j+1)], q[x(i,j+15)],
                         q[x(i-1,j+1)], q[x(i-1,j)], q[x(i-1,j+15)]};
            else if(i == 15 && j == 15)  //左上角
                q_new = {q[x(i-15,j-15)],  q[x(i-15,j)], q[x(i-15,j-1)], q[x(i,j-15)], q[x(i,j-1)],
                         q[x(i-1,j-15)], q[x(i-1,j)], q[x(i-1,j-1)]};
            
            //中间四个为表格的四条边,除去四个角
            else if(i == 0)  // 下边
                q_new = {q[x(i+1,j+1)], q[x(i+1,j)],   q[x(i+1,j-1)], q[x(i,j+1)], q[x(i,j-1)],
                         q[x(i+15,j+1)], q[x(i+15,j)], q[x(i+15,j-1)]};
            else if(i == 15)  //上边
                q_new = {q[x(i-15,j+1)],q[x(i-15,j)], q[x(i-15,j-1)], q[x(i,j+1)], q[x(i,j-1)],
                         q[x(i-1,j+1)], q[x(i-1,j)], q[x(i-1,j-1)]};
            else if(j == 0)  //右边
                q_new = {q[x(i+1,j+1)], q[x(i+1,j)], q[x(i+1,j+15)], q[x(i,j+1)], q[x(i,j+15)],
                         q[x(i-1,j+1)], q[x(i-1,j)], q[x(i-1,j+15)]};
            else if(j == 15)  //左边
                q_new = {q[x(i+1,j-15)], q[x(i+1,j)], q[x(i+1,j-1)], q[x(i,j-15)], q[x(i,j-1)],
                         q[x(i-1,j-15)], q[x(i-1,j)], q[x(i-1,j-1)]};
            
            //中间部分
            else 
                q_new = {q[x(i+1,j+1)], q[x(i+1,j)], q[x(i+1,j-1)], q[x(i,j+1)], q[x(i,j-1)],
                         q[x(i-1,j+1)], q[x(i-1,j)], q[x(i-1,j-1)]};
        end
    endfunction
            
//根据周围的八个元素计算新状态 ,返回一个元素的一次游戏之后的状态   
    function state_new(input [7:0] q,
                       input state_old);
        integer a;
        reg [3:0] count;
        begin
            count = 0;
            for(a = 0 ; a < 8 ; a++) begin
                count = count + q[a];
            end
            case(count)
                4'd0: state_new = 1'b0;
                4'd1: state_new = 1'b0;
                4'd2: state_new = state_old;
                4'd3: state_new = 1'b1;
                default : state_new = 1'b0;
            endcase
        end
    endfunction
                        
        
endmodule

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值