-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_of_life_interface.v
70 lines (60 loc) · 1.66 KB
/
game_of_life_interface.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
module GameOfLifeInterface (
run,
write_read_not,
serial_in,
clk,
serial_out
);
parameter ROW = 6;
parameter COL = 6;
input run, write_read_not, serial_in, clk;
output reg serial_out;
reg [ROW*COL-1:0] game_board_initial;
wire [ROW*COL-1:0] game_board;
reg [3:1] read_write_state; // 1:init, 2: write, 3: read
reg game_start;
GameOfLife game(
.init_board(game_board_initial),
.start(game_start),
.clk(clk),
.game_board(game_board)
);
defparam game.ROW = ROW;
defparam game.COL = COL;
integer write_index;
integer read_index;
reg [ROW*COL-1:0] temp_game_board;
initial begin
temp_game_board = {ROW*COL{1'b0}};
write_index = ROW*COL-1;
read_index = 0;
read_write_state = 3'b001;
game_start = 0;
end
always @(posedge clk) begin
if (~run) begin
game_start = 0;
if (read_write_state[1] || (read_write_state[2] && ~write_read_not) || (read_write_state[3] && write_read_not)) begin
if (read_write_state[1]) begin
temp_game_board = game_board;
end
write_index = ROW*COL-1;
read_index = 0;
read_write_state[1] = 0;
read_write_state[2] = write_read_not;
read_write_state[3] = ~write_read_not;
end
if (read_write_state[2]) begin
temp_game_board = temp_game_board >> 1;
temp_game_board[ROW*COL-1] = serial_in;
end else begin
serial_out = temp_game_board[0];
temp_game_board = temp_game_board >> 1;
end
end else begin
game_board_initial = temp_game_board;
read_write_state = 3'b001;
game_start = 1;
end
end
endmodule