FPGA, SoC, And CPLD Boards And Kits
FPGA Evaluation and Development Kits
5922 Discussions

Simple game using Max II starter kit

Altera_Forum
Honored Contributor II
2,705 Views

Hi!  

I really need help to the expert since I am not really familiar with Max II and verilog programming. This would be our final project for this semester. 

 

The game is called "catch the L.E.D". :) so here's the scenario of the game. We are task to program the kit so that the 4 led will lit randomly and the player should press the buttons accordingly as the led "ON" state. If the user/player presses with timing OR able to catch the flashing LED, we will display his/her score on 7-segment display. The highest score will be 99. it has also a time limit of 1minute. 

 

since Max II dev kit has 4-digit 7-segment display, we will display the time limit and score 2-digits each. 

 

Max II development kit: 

http://admucoe.tripod.com/max2.bmp
0 Kudos
12 Replies
Altera_Forum
Honored Contributor II
598 Views

hello... it seems nobody can help me out there.. :) thank you..  

 

by the way, I wanted to post my code that I started. I just um.. anybody who can help me.  

 

or it maybe I'm in a wrong category where I posted this topic. :)
0 Kudos
Altera_Forum
Honored Contributor II
598 Views

 

--- Quote Start ---  

it seems nobody can help me out there. 

 

--- Quote End ---  

Well, it is your homework, and you have shown absolutely no code, so you are basically asking us to do your homework for you. 

 

Try and write some code. Try and compile it. Describe a plan for what you are trying to do. Come back and ask specific questions. 

 

At least show you are trying first. 

 

Cheers, 

Dave
0 Kudos
Altera_Forum
Honored Contributor II
598 Views

this is the running code: :D 

 

module segmentTrial(clk,tick,seg,seg_sel,mouse,button1,button2,button3,button4); input clk,button1,button2,button3,button4; output seg; output seg_sel; output tick; output mouse; reg tick; reg mouse; reg seg; reg seg_sel; integer i,r,minute1,second,minute2,hour1,hour2,counter,mouseCom,catch,Buttontiming; /* 7 segement patern */ parameter one = 8'b100_1111_1; //159 parameter zero = 8'b000_0001_1; // 3 parameter two =8'b001_0010_1; // 37 parameter three=8'b000_0110_1; // 13 parameter four =8'b100_1100_1; //153 parameter five =8'b010_0100_1; // 73 parameter six =8'b010_0000_1; // 65 parameter seven=8'b000_1111_1; // 31 parameter eight=8'b000_0000_1; // 1 parameter nine =8'b000_0100_1; // 9 parameter blank=8'b111_1111_1; //255 */ /* End of 7 segement patern */ /* mouse pattern ;) */ parameter com0 =8'b010_0000_1; parameter com1 =8'b000_1000_0; parameter com2 =8'b001_0000_0; parameter com3 =8'b010_0000_0; parameter com4 =8'b100_000_0; parameter com5 =8'b001_0010_0; parameter com6 =8'b00_0100_1; parameter com7 =8'b100_0100_1; parameter com8 =8'b100_0100_0; parameter off = 8'b000_0000_0; /* End of mouse pattern ;) */ initial minute2 = 0; initial minute1 = 0; initial hour2 = 5; initial hour1 = 9; initial counter = 60; initial seg_sel = 4'b0001; //intialize seleector digit 1 initial mouse = ~off; /* 1 minute countdown counter */ always@(posedge clk) begin if(i==16000000) //1Hz begin i=1; counter = counter-1'b1; if(counter == 0) //reset everything after count begin mouse = ~off; hour1 = 0; hour2 = 0; end if(counter != 0); //to prevent displaying something after count zero begin hour1 = counter % 10; end if(counter == 49 ||counter == 39 || counter == 29 || counter == 19 || counter == 9) begin hour2 = hour2-1'b1; end end else begin i=i+1'b1; //increament i end if(Buttontiming==4000000) //button detection delay begin mouseCom = hour1; Buttontiming = 1; if(mouseCom==3) begin mouse = ~com1; if(button1==0) begin minute1 = minute1+1'b1; end end else if(mouseCom == 1) begin mouse = ~com2; if(button2 == 0) begin minute1 = minute1+1'b1; end end else if(mouseCom == 6) begin mouse = ~com6; if(button4 == 0 && button1 == 0) begin minute1 = minute1+2; end else if(button4 == 0 || button1 == 0) begin minute1 = minute1+1'b1; end end else if(mouseCom == 8) begin mouse = ~com4; if(button3 == 0) begin minute1 = minute1+1'b1; end end else if(minute1 == 10) begin minute1 = 0; minute2 = minute2+1'b1; end else begin mouse = ~off; end end else begin Buttontiming=Buttontiming+1'b1; end end /* End of 1 minute countdown counter */ /* Segment Selector */ always@(posedge clk) begin if(r==800) begin tick=~tick; r=1; end else begin r=r+1'b1; end end /* End of Segment Selector */ /* 7 segment Display Library */ always@(posedge tick) begin seg_sel={seg_sel,seg_sel}; if(seg_sel==4'b1000) begin case(hour2) 0:seg=zero; 1:seg=one; 2:seg=two; 3:seg=three; 4:seg=four; 5:seg=five; 6:seg=six; 7:seg=seven; 8:seg=eight; 9:seg=nine; endcase end else if(seg_sel==4'b0100) begin case(hour1) 0:seg=zero; 1:seg=one; 2:seg=two; 3:seg=three; 4:seg=four; 5:seg=five; 6:seg=six; 7:seg=seven; 8:seg=eight; 9:seg=nine; endcase end else if(seg_sel==4'b0010) begin case(minute2) 0:seg=zero; 1:seg=one; 2:seg=two; 3:seg=three; 4:seg=four; 5:seg=five; 6:seg=six; 7:seg=seven; 8:seg=eight; 9:seg=nine; endcase end else if(seg_sel==4'b0001) begin case(minute1) 0:seg=zero; 1:seg=one; 2:seg=two; 3:seg=three; 4:seg=four; 5:seg=five; 6:seg=six; 7:seg=seven; 8:seg=eight; 9:seg=nine; endcase end end /* End of 7 segment Display Library*/ endmodule  

 

sir, how do i improve this something like adding a several combination of lighting the bulb? let's say 100 combinations or more? how am i gonna do this in an array form or in case? 

 

thank you! :) 

 

 

 

0 Kudos
Altera_Forum
Honored Contributor II
598 Views

 

--- Quote Start ---  

Sir, how do I improve this something like adding a SEVERAL a combination of lighting the bulb? let's say 100 combinations or more? how am I gonna do this in an array form or in CASE? 

 

--- Quote End ---  

You will find it easier if you divide your logic into components, that way you can have multiple instances of each component, eg., multiple 7-segment decoders. 

 

I have attached two examples; 

 

* hex_display_sv.txt (rename to hex_display.sv) 

* hex_display_tb_sv.txt (rename to hex_display_tb.sv) 

 

The .sv extension stands for SystemVerilog (the new and improved Verilog). 

 

Now learn how to use the Modelsim simulator to look at your logic. Download and install Modelsim Altera Starter edition, and start the program. Change to a work folder and then compile the files. You can do this through the GUI, or via the command line, i.e., 

# cd c:/temp# vlib mwork# vmap work mwork# vlog -sv hex_display.sv# vlog -sv hex_display_tb.sv# vsim -novopt hex_display_tb# add wave *# run -a 

 

The '#' is the command prompt, you do not type that in. 

 

This sequence of commands compiles the hex display module and then the (very basic) testbench that uses the module. 

 

Read and understand the code.  

 

Create a testbench for your design. Look at the signals in the testbench and see if things work like you expect, and then test them in hardware. 

 

Cheers, 

Dave
0 Kudos
Altera_Forum
Honored Contributor II
598 Views

Thank for your immediate reply sir.. I do have "ModelSIn ALtera Starter Edition v6.4a" installed. 

 

I'm going to test it now. I'll just have test it now. New environment. :) 

 

I will post later result. :) 

 

 

 

EDIT: 

 

HELLO ,SIR' 

 

I'm having a trouble running your sample code in ModelSim Altera. 

here is the logs. 

# Reading C:/altera/90/modelsim_ase/tcl/vsim/pref.tcl cd c:/temp# reading modelsim.ini dir# Volume in drive C is JLwinXP# Volume Serial Number is 6435-3543# # Directory of C:\temp# # 09/11/2011 07:01 PM <DIR> .# 09/11/2011 07:01 PM <DIR> ..# 09/11/2011 06:52 PM 4,119 catch_mouse.sv# 09/11/2011 01:55 PM 0 hex_display.sv# 09/11/2011 01:56 PM 0 hex_display_tb.sv# 09/11/2011 06:57 PM 6,267 modelsim.ini# 09/11/2011 07:01 PM <DIR> mwork# 4 File(s) 10,386 bytes# 3 Dir(s) 50,575,568,896 bytes free vlib mwork vmap work mwork# Modifying modelsim.ini vlog -sv hex_display.sv# Model Technology ModelSim ALTERA vlog 6.4a Compiler 2008.08 Oct 22 2008 vlog -sv hex_display_tb.sv# Model Technology ModelSim ALTERA vlog 6.4a Compiler 2008.08 Oct 22 2008 vsim -novopt hex_display_tb# vsim -novopt hex_display_tb # ** Error: (vsim-3170) Could not find 'C:\temp\mwork.hex_display_tb'.# Error loading design vsim -novopt hex_display_tb.sv# OpenFile hex_display_tb.sv add wave * run -a# No Design Loaded!
0 Kudos
Altera_Forum
Honored Contributor II
598 Views

 

--- Quote Start ---  

 

I'm having a trouble running your sample code in ModelSim Altera. 

# 09/11/2011 01:55 PM 0 hex_display.sv# 09/11/2011 01:56 PM 0 hex_display_tb.sv 

 

--- Quote End ---  

 

 

I'd be a little concerned about files with a length of zero ... 

 

Cheers, 

Dave
0 Kudos
Altera_Forum
Honored Contributor II
598 Views

 

--- Quote Start ---  

I'd be a little concerned about files with a length of zero ... 

 

--- Quote End ---  

 

ah see.. I think that is the size of file in byte. I just noticed it. 

I wonder why it is zero. ??? hmm..  

 

sir, what can you suggest? 

 

anyway I'll be looking ways to find out, thank you! :)
0 Kudos
Altera_Forum
Honored Contributor II
598 Views

 

--- Quote Start ---  

 

what can you suggest? 

 

--- Quote End ---  

 

 

Open the files, are they empty? If so, download the files again from the links in this thread. 

 

Cheers, 

Dave
0 Kudos
Altera_Forum
Honored Contributor II
598 Views

 

--- Quote Start ---  

Open the files, are they empty? If so, download the files again from the links in this thread. 

--- Quote End ---  

I have double checked the file, sir. it's not empty. :confused:  

the GUI compilation doesn't work too. :)
0 Kudos
Altera_Forum
Honored Contributor II
598 Views

 

--- Quote Start ---  

I have double checked the file, sir. it's not empty. :confused:  

the GUI compilation doesn't work too. :) 

--- Quote End ---  

Ok, so lets see what is wrong with the tools setup. I'll step through exactly the same procedure I'd expect you to use, and we'll see where we differ. 

 

1) Download the files from the link above and rename them. 

 

2) Start Modelsim. In my case, I tested using Modelsim Altera Starter Edition from Quartus 10.1 which is version 6.6c. 

 

3) Change directory to where you downloaded the files to. 

 

4) Setup the work library 

 

vlib mwork 

vmap work mwork 

 

5) Build the components 

 

vlog -sv hex_display.sv 

vlog -sv hex_display_tb.sv 

 

6) Run the testbench 

 

vsim -novopt hex_display_tb 

add wave * 

run -a 

 

Ok, this all worked fine for me. 

 

Check the following Modelsim setting; Compile->Compile Options, click on the Verilog and SystemVerilog tab, and make sure the checkbutton for 'Use SystemVerilog' is checked. 

 

Cheers, 

Dave
0 Kudos
Altera_Forum
Honored Contributor II
598 Views

sir, ovro. 

 

I have quick question for you. i have running code below which I used switch as input. but i don't have any idea how to implement button debouncing. 

 

 

code: 

 

--- Quote Start ---  

 

/*game pattern(LED | BUTTON)*/ 

if(ButtonT==11000000) //button detection delay 

begin 

ledBlink = timer1; 

ButtonT = 1; 

 

if(ledBlink == 1)  

begin 

LED = D1; 

if(sw1==0 && sw4==0) 

begin 

score1 = score1+2;  

end 

 

 

else if (sw1==0 || sw4==0) 

begin 

score1 = score1+1'b1; 

end 

end 

 

 

else if(ledBlink == 2)  

begin  

LED = D2; 

if(sw2==0 && sw3==0) 

begin 

score1 = score1+2; 

end 

else if (sw2==0 || sw3==0) 

begin  

 

score1=score1+1'b1; 

 

end  

end 

 

else if(ledBlink == 3) 

begin  

LED = D3; 

if(sw3 == 0) 

begin  

 

score1 = score1+1'b1; 

 

 

end 

 

end 

 

else if(ledBlink == 4) 

begin 

LED = D4; 

if(sw4 == 0) 

begin 

score1 = score1+1'b1; 

end 

end 

 

else if(ledBlink == 5) 

begin  

LED = D8; 

if(sw4 == 0 ) 

begin 

 

score1 = score1+1'b1; 

end 

end 

 

else if(ledBlink == 6) 

begin 

LED = D7; 

if(sw3 == 0) 

begin 

score1 = score1+1'b1; 

end 

end 

 

else if(ledBlink == 7) 

begin 

LED = D6; 

if(sw2 == 0) 

begin 

score1 = score1+1'b1; 

end 

end  

else if(ledBlink == 8) 

begin 

LED = D5; 

if(sw1 == 0) 

begin 

score1 = score1+1'b1; 

end 

end 

 

 

else 

begin 

 

LED= off; 

end 

 

if(score1 == 10) 

begin 

score1 = 0; 

score2 = score2+1; 

end 

 

 

end 

else 

begin 

ButtonT=ButtonT+1'b1;  

 

end 

 

end 

 

--- Quote End ---  

I tested debouncing like on embdded C which I think cannot be implemented here in verilog programming, like this. 

 

 

 

 

--- Quote Start ---  

else if(ledBlink == 3) 

begin  

LED = D3; 

if(sw3 == 0) 

begin  

 

score1 = score1+1'b1; 

while (sw3 == 0); // I think it can't be implenmented here. :( 

 

end 

 

end 

--- Quote End ---  

THANK YOU!
0 Kudos
Altera_Forum
Honored Contributor II
598 Views

 

--- Quote Start ---  

 

i don't have any idea how to implement button debouncing. 

 

--- Quote End ---  

When I debounce signals, I use a small statemachine and registers. I have the code in VHDL, not Verilog, so I'll just describe it. 

 

1) Synchronize the button to the local clock using dual-DFF synchronizer. 

 

Lets call this signal 'button_sync' 

 

2) Delay the synchronized signal using a single DFF 

 

Lets call this signal 'button_delay' 

 

3) Define a signal that is the XOR of these two signals 

 

assign button_change = button_delay ^ button_sync; 

 

This signal will be high when there is a change in logic level. 

 

4) Use a state machine and a counter to control a 'button_debounce' register. 

 

When the 'button_change' signal asserts, the state machine transitions from IDLE to the CHANGE state. On the transition load a counter with the debounce time (use a down-counter, so that carry-out can be used to detect termination). While in the CHANGE state, enable the counter. 

 

If the change signal asserts while in the CHANGE state, then reload the counter. Only when the timer terminates do you update the 'button_debounce' output. This logic ensures that the signal is in a valid asserted or deasserted state for at least the debounce time. 

 

Create a testbench for your debounce component, and edit/debug until you can debounce a signal. 

 

The above description should get you most of the way there. 

 

If you have trouble, I'll post the VHDL code and its testbench. You can use that as the reference. 

 

Cheers, 

Dave
0 Kudos
Reply