Title:Conwaylife

Conway’s Game of Life is a two-dimensional cellular automaton.

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:

  1. 0-1 neighbour: Cell becomes 0.
  2. 2 neighbours: Cell state does not change.
  3. 3 neighbours: Cell becomes 1.
  4. 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.

题目为康威的生命游戏

其实总的大体是个D触发器,只不过这个D是“二维”的,题目中用一维256位宽数据代替16x16二维数据,下一个状态由上一个状态转变而来,而“二维”数据的边界为环形(既被填充),具体填充规则如下图所示(下图蓝色框中为原始数据,周围既为填充数):

每个元素的下一个状态都由当前状态的邻域1的个数所决定,如下图所示,(0,0)的邻域有8个,当这其中8个含1的有0~1个时:Q* = 0; 有2个时:Q* = Q; 有三个时:Q* = 1; 有4个及以上时:Q* = 0。

//模块定义
Module Declaration
module top_module(
input clk,
input load,
input [255:0] data,
output [255:0] q
);

废话不多少,直接上代码

module top_module(input clk,input load,input [255:0] data,output reg [255:0] q
); wire  [15:0] d [15:0];wire  [17:0] d_loop [17:0];//extend qassign d_loop[0][17:0] = {q[240],q[255:240],q[255]};assign d_loop[17][17:0] = {q[0],q[15:0],q[15]};genvar m;generate for(m=0;m<16;m=m+1)begin:loopd_loop_extend u_d_loop_extend({q[(16*m)],q[(16*m+15):(16*m)],q[(16*m+15)]},d_loop[(m+1)][17:0]);endendgenerate //next q stategenvar i,j;generatefor(i=1;i<17;i=i+1)begin:loop0for(j=1;j<17;j=j+1)begin:loop1neighbor_num u_neighbor_num({d_loop[i-1][j-1],d_loop[i-1][j],d_loop[i-1][j+1],d_loop[i][j-1],d_loop[i][j+1],d_loop[i+1][j-1],d_loop[i+1][j],d_loop[i+1][j+1]},d_loop[i][j],d[i-1][j-1]);endendendgenerate  always@(posedge clk)if(load)q <= data;elseq <= {d[15][15:0],d[14][15:0],d[13][15:0],d[12][15:0],d[11][15:0],d[10][15:0],d[9][15:0],d[8][15:0],d[7][15:0],d[6][15:0],d[5][15:0],d[4][15:0],d[3][15:0],d[2][15:0],d[1][15:0],d[0][15:0]};endmodulemodule d_loop_extend(   //供generate模块调用填充数据邻域input    [17:0] data_in  ,output [17:0]  data_out
);assign data_out = data_in;
endmodulemodule neighbor_num(  //计算领域中1的个数input [7:0]  nums,input          d0    ,output  reg q0
);
wire [3:0] num;
assign num = nums[0]+nums[1]+nums[2]+nums[3]+nums[4]+nums[5]+nums[6]+nums[7];always@(*)case(num)4'd2:q0 = d0;4'd3: q0 = 1'b1;default: q0 = 1'b0;endcase
endmodule

仿真图:

用Verilog实现Conway‘s Game of Life 16x16(HDLbits的Conwaylife题)相关推荐

  1. Conway‘s Game of Life 16x16(Conwaylife)

    项目场景: Conway's Game of Life is a two-dimensional cellular automaton. The "game" is played ...

  2. verilog for循环_HDLBits:在线学习 Verilog (二十四 · Problem 115-119)

    本系列文章将和读者一起巡礼数字逻辑在线学习网站 HDLBits 的教程与习题,并附上解答和一些作者个人的理解,相信无论是想 7 分钟精通 Verilog,还是对 Verilog 和数电知识查漏补缺的同 ...

  3. verilog练习:hdlbits网站上的做题笔记(5)

    前言 之前的文章<如何学习verilog,如何快速入门?>中提到了verilog学习,推荐了一个可以练习的网站:hdlbits网站,那自己也玩玩这个网站. 这篇文章,是接着<veri ...

  4. 边沿检测 Verilog

    1.用verilog实现1bit信号边沿检测功能,输出一个周期宽度的脉冲信号 定义了一个2位的寄存器变量data_r,data_r[0]用来存储当前的状态,data_r[1]用来存储上一拍的状态 每个 ...

  5. 爆肝4万字❤️零基础掌握Verilog HDL

    文章目录 0.前言 1.Verilog HDL简介 1.1 什么是Verilog HDL 1.2 verilog发展历史ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ 1.3 为什么要使用verilog ...

  6. FPGA的设计艺术(13)使用generate语句构建可重用的逻辑设计

    前言 本文首发:https://www.ebaina.com/articles/140000010059 我们在verilog中使用generate语句在我们的设计中有条件地或迭代地生成代码块. 这使 ...

  7. 【HDLBits 刷题】所有答案直达链接汇总

    写在前面 以下为HDLBits全部答案,有些题的解法不唯一,我的也许不是最优解,欢迎提出更好的想法,HDLBits总的来说比较适合初学者. HDLBits 答案汇总 Language [HDLBits ...

  8. HDLBits 系列(7)——Sequential Logic(Counters、Shift Registers、More Circuits)

    目录 3.2 Sequential Logic 3.2.2 Counters 1. Four-bit binary counter​ 2.Decade counter 3. Decade counte ...

  9. 对象 php 输出用字符串连接,在PHP中使用 在使用echo或print输出对象时将对象转化为字符串。_学小易找答案...

    [单选题]激光洗眉常出现下列哪种情况? [单选题]绪论1 你的青春就是一场远行,一场离自己的童年,离自己的少年,越来越远的一场远行.在我和你们一样大的时候,18岁,我正在一列,从上海到北京的火车上,我 ...

最新文章

  1. Java Day02-2(字符串)
  2. mp4v2再学习 -- H264视频编码成MP4文件
  3. 【深度学习】使用transformer进行图像分类
  4. 【51nod】1934 受限制的排列
  5. 超清世界地图可放大_3D高清世界地图
  6. Swin Transformer(W-MSA详解)代码+图解
  7. MDT2012部署问题,USMT5.0 迁移哪些内容
  8. r mysql utf8_R读取MySQL数据出现乱码,解决该问题的方法总结
  9. Linux PHY几个状态的跟踪
  10. Linux下安装ActiveMQ(CentOS7)
  11. 控制用户创建课程权限
  12. 虎牙直播电影一天收入_电影收入
  13. 电脑鼠标右键菜单太多了怎么办?Windows右键菜单设置删除方法介绍
  14. ThinkPHP3.1.3 { Fast Simple OOP PHP Framework } — [ WE CAN DO IT JUST THINK ] 报错解决办法。...
  15. html5中插入样式表方法,如何插入css样式?
  16. 小米扫地机器人换了边刷很响_米家/石头/小瓦扫扫地机器人为什么不使用双边刷而使用单边刷?...
  17. 【Unity VR开发基础】Player视角设置调整与地面的相对高度
  18. 阿里技术专家的编程方法论:如何写出一手漂亮的代码?
  19. 推荐3个Windows电脑上的epub小说阅读器
  20. Android 多国家语言适配

热门文章

  1. pdf文件去掉广告,水印,背景和删除密码方法收藏
  2. .NET Elasticsearch教程:使用NEST插入、查询数据
  3. 组策略给全公司统一桌面壁纸
  4. 工艺品娃娃和塑料娃娃的区别?
  5. 党员管理系统毕业设计
  6. Hexo+Gitee搭建个人博客 —— 零基础Gitee部署
  7. Java实例项目之自动售票机原理(待完善)
  8. 前端面试之---link 引入css文件和@import引入css文件的区别
  9. RFID模块+WIFI模块+振动传感器+有源蜂鸣器+舵机+Arduino UNO R3所构成的门禁系统模块
  10. 数据资产为王,如何解析企业数字化转型与数据资产管理的关系?