Das Spiel "Life" ist ein bekannter zellularer Automat, der 1970 von John Conway erfunden wurde. Die Essenz des Spiels besteht darin, das "Universum" zu simulieren, in unserem Fall implementiert auf einer 8x8-Quadratmatrix mit geschlossenen Kanten.
Gameplay
In unserem Fall wird das Spiel mithilfe der integrierten Tasten und Schalter auf dem Altera Cyclone IV FPGA implementiert. Der gesamte Prozess ist bedingt in zwei Betriebsarten unterteilt - die Wahl der Konfiguration der ersten Generation und die eigentliche Simulation.
Implementierung
Das Spiel ist in der Verilog-Designsprache implementiert und besteht aus vier Grundmodulen: Eingabemodule, Ausgabe, algorithmisch und grundlegend, die den Rest verbinden.
Übergangsalgorithmus
Das Spielfeld im Code wird in Form eines Registers mit 64 Elementen dargestellt. Der Übergang zur nächsten Generation erfolgt mit sequentieller Logik.
Übergangsfunktionfunction [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
Ein- / Ausgabemodule
Das Ausgangsmodul arbeitet mit einer Standard-8x8-LED-Matrix mit 16 Steuerpins. Aufgrund von Anzeigebeschränkungen wird das Bild in Zeilen mit kontinuierlicher Änderung der angezeigten Zeile angezeigt.
Das Eingabemodul besteht aus drei Tasten - Bildschirmaktualisierungen und einem Modusschalter. Im Setup-Modus sind alle drei Schaltflächen aktiv - Auswahl der aktiven Zelle, Änderung des Status der aktiven Zelle und Aktualisierung des Bildschirms. Im Simulationsmodus ist nur die Schaltfläche zum Aktualisieren des Bildschirms aktiv.
Arbeitsbeispiel
→ QuellcodePS Dieser Artikel ist eine der Voraussetzungen für die studentische Forschung. Wir bitten Sie dringend, keine Hausschuhe zu werfen, wir sind nicht schuld.