FPGA Altera Cyclone IV上的游戏“ Life”

游戏“生活”是约翰·康威(John Conway)于1970年发明的著名的细胞自动机。 游戏的本质是模拟“宇宙”,在我们的例子中,是在具有封闭边缘的8x8正方形矩阵上实现的。

游戏玩法


在我们的案例中,该游戏使用内置按钮和开关在Altera Cyclone IV FPGA上实现。 整个过程有条件地分为两种操作模式-第一代配置的选择和实际仿真。

实作


该游戏以Verilog设计语言实现,包括四个基本模块:输入模块,输出,算法和基本模块,其余模块相互连接。

过渡算法

代码中的竞争环境以64个元素的寄存器的形式表示。 使用顺序逻辑执行到下一代的过渡。

过渡功能
function [63:0]step; input [63:0]field; reg [63:0]new_field; reg [7:0]position; reg [7:0]count; integer x; integer y; begin new_field = field; for(x = 0; x < 8; x = x + 1 ) begin: iloop for(y = 0; y < 8; y = y + 1) begin: jloop count = neighbour_count(field,x,y); position = to_1d(x,y); if (count == 3) new_field[position] = 1; else if ((count < 2) || (count > 3)) new_field[position] = 0; end end step = new_field; end endfunction function [7:0]neighbour_count; input [63:0]field; input [7:0]x; input [7:0]y; reg [7:0]count; reg [7:0]position; begin count = 0; position = to_1d(x-1,y-1); count = count + field[position]; position = to_1d(x,y-1); count = count + field[position]; position = to_1d(x + 1, y - 1); count = count + field[position]; position = to_1d(x - 1, y); count = count + field[position]; position = to_1d(x + 1, y); count = count + field[position]; position = to_1d(x - 1, y + 1); count = count + field[position]; position = to_1d(x, y + 1); count = count + field[position]; position = to_1d(x + 1, y + 1); count = count + field[position]; neighbour_count = count; end endfunction function [7:0]to_1d; input [7:0]x; input [7:0]y; begin if (x >= 8'b11111111) x = x + 8'd8; else if (x >= 8'd8) x = x - 8'd8; if (y >= 8'b11111111) y = y + 8'd8; else if (y >= 8'd8) y = y - 8'd8; to_1d = x + y * 8'd8; end endfunction 


输入/输出模块

输出模块与带有16个控制引脚的标准8x8 LED矩阵配合使用。 由于显示限制,图片以行显示,显示行连续变化。

输入模块由三个按钮组成-屏幕更新和模式开关。 在设置模式下,所有三个按钮均处于活动状态-选择活动单元格,更改活动单元格的状态并更新屏幕。 在模拟模式下,只有屏幕刷新按钮处于活动状态。

工作实例

图片

源代码

附言:本文是学生研究的要求之一,强烈要求您不要扔拖鞋,也不要怪。

Source: https://habr.com/ru/post/zh-CN429764/


All Articles