/* life1 The first simple version of Conway's Life on Mefisto 8x8 chess board. Copyright (C) 2005, Antti Karttunen, Antti.Karttunen@iki.fi */ module life1(CLK,PB_IN,SW_IN,BCOL_IN,BCOL_OUT,BROW,LED_OUT); input CLK; input [3:0] PB_IN; input [7:0] SW_IN; input [7:0] BCOL_IN; output [7:0] BCOL_OUT; output [7:0] BROW; output [7:0] LED_OUT; // wire RST = PB_IN[3]; reg [7:0] BOARD_ROW [7:0]; wire [7:0] PIECES_ROW [7:0]; function fredkin; input n; input s; input w; input e; fredkin = (n^s^w^e); endfunction // The cell will be alife if it has either 3 neighbours // or if it has 2 neighbours and it itself is alive. function life; input c; input n; input ne; input s; input se; input w; input sw; input e; input nw; //life = (4'b0011 == ((n+ne+e+se+s+sw+w+nw)|c)); life = (4'b0011 == (({3'b000,n}+{3'b000,ne}+{3'b000,s}+{3'b000,se}+{3'b000,w}+{3'b000,sw}+{3'b000,e}+{3'b000,nw})|{3'b000,c})); endfunction mefi8x8a BOARD_IO(.CLK(CLK), .BCOL_IN(BCOL_IN), .BCOL_OUT(BCOL_OUT), .BROW(BROW), .LEDS2LIT0(BOARD_ROW[0]), .LEDS2LIT1(BOARD_ROW[1]), .LEDS2LIT2(BOARD_ROW[2]), .LEDS2LIT3(BOARD_ROW[3]), .LEDS2LIT4(BOARD_ROW[4]), .LEDS2LIT5(BOARD_ROW[5]), .LEDS2LIT6(BOARD_ROW[6]), .LEDS2LIT7(BOARD_ROW[7]), .PIECES0(PIECES_ROW[0]), .PIECES1(PIECES_ROW[1]), .PIECES2(PIECES_ROW[2]), .PIECES3(PIECES_ROW[3]), .PIECES4(PIECES_ROW[4]), .PIECES5(PIECES_ROW[5]), .PIECES6(PIECES_ROW[6]), .PIECES7(PIECES_ROW[7]) ); wire [2:0] shrow = {SW_IN[2],SW_IN[1],SW_IN[0]}; wire input_mode = SW_IN[7]; wire PB_NEXT; // State of the debounced push button for incrementing. reg pb_next_prev = 0; // is saved here also. wire next_gen = (PB_NEXT && ~pb_next_prev); debounced_button DBB0(CLK,PB_IN[0],PB_NEXT); assign LED_OUT = {PIECES_ROW[shrow][0],PIECES_ROW[shrow][1],PIECES_ROW[shrow][2],PIECES_ROW[shrow][3],PIECES_ROW[shrow][4],PIECES_ROW[shrow][5],PIECES_ROW[shrow][6],PIECES_ROW[shrow][7]}; integer i; // A "compile-time" index to board rows. (for the for loop). integer j; always @(posedge CLK) begin // if(RST) // begin // end // else begin pb_next_prev <= PB_NEXT; if(input_mode) begin for(i=0; i<8; i=i+1) BOARD_ROW[i] <= PIECES_ROW[i]; end else if(next_gen) begin for(i=0; i<8; i=i+1) for(j=0; j<8; j=j+1) BOARD_ROW[i][j] <= life(BOARD_ROW[i][j], // The cell itself. BOARD_ROW[i][(7==j ? 0 : j+1)], // n BOARD_ROW[(7==i ? 0 : i+1)][(7==j ? 0 : j+1)], // ne BOARD_ROW[(7==i ? 0 : i+1)][j], // e BOARD_ROW[(7==i ? 0 : i+1)][(0==j ? 7 : j-1)], // se BOARD_ROW[i][(0==j ? 7 : j-1)], // s BOARD_ROW[(0==i ? 7 : i-1)][(0==j ? 7 : j-1)], // sw BOARD_ROW[(0==i ? 7 : i-1)][j], // w BOARD_ROW[(0==i ? 7 : i-1)][(7==j ? 0 : j+1)] // nw ); end end end endmodule