module pinballmachine (input clk, reset_bar, output reg [0:2] pstate, output reg [0:2] nstate, output reg [0:4] games, output reg [0:15] points, output reg [0:4] pgames, output reg [0:15] ppoints, input h100, h200, bop_hit, ball_lost, output reg bop_en, green, yellow, red, gold); parameter init = 0, start = 1, h200one = 2, h200two = 3, h200three = 4; // State flipflops always @(posedge clk, negedge reset_bar) begin if (reset_bar == 0) pstate <= init; else pstate <= nstate; end // Next state and output decoder always @(*) begin case (pstate) init: begin nstate <= start; points <= 0; games <= 0; green <= 0; yellow <= 0; red <= 0; gold <= 0; bop_en <= 0; end start:begin bop_en <= 0; if (! ball_lost) begin if (h100 == 1) begin nstate <= start; points <= ppoints + 100; games <= 0; green <= 1; yellow <= 0; red <= 0; gold <= 0; end else if (h200 == 1) begin nstate <= h200one; points <= ppoints + 200; games <= 0; green <= 0; yellow <= 1; red <= 0; gold <= 0; end else begin nstate <= start; points <= 0; games <= 0; end end else begin nstate <= start; points <= 0; games <= 0; green <= 0; yellow <= 0; red <= 0; gold <= 0; end end h200one: begin bop_en <= 0; if (! ball_lost) begin if (h100 == 1) begin nstate <= start; points <= ppoints + 100; games <= 0; green <= 1; yellow <= 0; red <= 0; gold <= 0; end else if (h200 == 1) begin nstate <= h200two; points <= ppoints + 200; games <= 0; green <= 0; yellow <= 1; red <= 0; gold <= 0; end else begin nstate <= h200one; points <= 0; games <= 0; end end else begin nstate <= start; points <= 0; games <= 0; green <= 0; yellow <= 0; red <= 0; gold <= 0; end end h200two: begin if (! ball_lost) begin if (h100 == 1) begin nstate <= start; points <= ppoints + 100; games <= 0; green <= 1; yellow <= 0; red <= 0; gold <= 0; bop_en <= 0; end else if (h200 == 1) begin nstate <= h200three; points <= ppoints + 200; games <= 0; green <= 0; yellow <= 0; red <= 1; gold <= 0; bop_en <= 1; end else begin nstate <= h200two; points <= 0; games <= 0; end end else begin nstate <= start; points <= 0; games <= 0; green <= 0; yellow <= 0; red <= 0; gold <= 0; bop_en <= 0; end end h200three: begin if (! ball_lost) begin if (h100 == 1) begin nstate <= start; points <= ppoints + 100; games <= 0; green <= 1; yellow <= 0; red <= 0; gold <= 0; bop_en <= 0; end else if (h200 == 1) begin nstate <= start; points <= ppoints + 200; games <= 0; green <= 0; yellow <= 1; red <= 0; gold <= 0; bop_en <= 0; end else if (bop_hit == 1) begin nstate <= start; points <= ppoints + 600; games <= pgames + 1; green <= 0; yellow <= 0; red <= 0; gold <= 1; bop_en <= 0; end else begin nstate <= h200three; points <= 0; games <= 0; end end else begin nstate <= start; points <= 0; games <= 0; green <= 0; yellow <= 0; red <= 0; gold <= 0; bop_en <= 0; end end endcase end // Point counter always @(posedge clk, negedge reset_bar) begin if (reset_bar == 0) ppoints <= 0; else if (points) ppoints <= points; end // Game counter always @(posedge clk, negedge reset_bar) begin if (reset_bar == 0) pgames <= 0; else if (games) pgames <= games; end endmodule module testbench (output reg clk, output reg reset_bar, input [0:2] pstate, input [0:2] nstate, input [0:4] games, input [0:15] points, input [0:4] pgames, input [0:15] ppoints, output reg h100, h200, bop_hit, ball_lost, input bop_en, green, yellow, red, gold); // Test Bench initial begin $dumpvars; $dumpfile ("pinballmachine2.vcd"); clk <= 0; reset_bar <= 1; h100 <= 0; h200 <= 0; bop_hit <= 0; ball_lost <= 0; #10 reset_bar <= 0; #10 reset_bar <= 1; clk <= 1; #10 clk <= 0; #10 clk <= 1; h100 <= 1; h200 <= 0; #10 clk <= 0; #10 clk <= 1; #10 clk <= 0; #10 clk <= 1; #10 clk <= 0; #10 clk <= 1; h100 <= 0; #10 clk <= 0; #10 clk <= 1; #10 clk <= 0; #10 h200 <= 1; clk <= 1; #10 clk <= 0; #10 clk <= 1; #10 clk <= 0; #10 h200 <= 0; clk <= 1; #10 clk <= 0; #10 h200 <= 1; clk <= 1; #10 clk <= 0; #10 bop_hit <= 1; h200 <= 0; clk <= 1; #10 clk <= 0; #10 bop_hit <= 0; clk <= 1; #10 clk <= 0; #10 clk <= 1; #10 clk <= 0; #10 clk <= 1; #10 clk <= 0; #350 $finish; end endmodule module system (); wire clk; wire reset_bar; wire [0:2] pstate; wire [0:2] nstate; wire [0:4] games; wire [0:15] points; wire [0:4] pgames; wire [0:15] ppoints; wire h100, h200, bop_hit, ball_lost; wire bop_en; wire green, yellow, red, gold; pinballmachine (clk, reset_bar, pstate, nstate, games, points, pgames, ppoints, h100, h200, bop_hit, ball_lost, bop_en, green, yellow, red, gold); testbench (clk, reset_bar, pstate, nstate, games, points, pgames, ppoints, h100, h200, bop_hit, ball_lost, bop_en, green, yellow, red, gold); endmodule