Pong Game - use buttons instead of mouse

Pluto/-II/-3/-P boards

Pong Game - use buttons instead of mouse

Postby lonesync » Tue Sep 23, 2008 1:52 am

Hi,

This little experimental project i did is to light the LED on whenever the button on the tact switch is press. however, the LED did not seems to response to tat.

So i thought maybe is the asynchronous problem as in the Debouncer. So i follow the similar coding as in the debouncer but cancelled out the PB_down and PB_up output since only 1 output is needed. But the results is the same.

I'm not sure whenever my code is correct either, just started to experimenting around with it for a few months.

Codes:
module testsw (clk, sw, LED1, LED2);
input clk;
input sw;
output LED1;
output LED2;

reg [22:0] cnt;
always@ (posedge clk) cnt <= cnt+1;

wire state;
debouncer swmod(.clk(clk), .PB(sw), .PB_state(state));

assign LED1 = ~cnt[22] & ~cnt[20];
assign LED2 = state;

endmodule

module debouncer(clk, PB, PB_state);
input clk;
input PB;

output PB_state;

reg PB_sync_0; always @(posedge clk) PB_sync_0 <= ~PB;
reg PB_sync_1; always @(posedge clk) PB_sync_1 <= PB_sync_0;

reg [15:0] PB_cnt;

reg PB_state;
wire PB_idle = (PB_state==PB_sync_1);
wire PB_cnt_max = &PB_cnt;

always @(posedge clk)
if(PB_idle)
PB_cnt <= 0;
else
begin
PB_cnt <= PB_cnt + 1;
if(PB_cnt_max) PB_state <= ~PB_state;
end

endmodule

Thanks in advance!

Regards,
LoneSync
Last edited by lonesync on Mon Nov 17, 2008 12:58 am, edited 1 time in total.
lonesync
 
Posts: 8
Joined: Wed Apr 23, 2008 8:41 am

Postby lonesync » Tue Sep 23, 2008 1:56 am

Another question, is the "posedge" and "negedge" can only be use with "always@". I seems to get error if i use those 2 keywords with "if..else".

I'm thinking of assigning decimal to the posedge and negedge of PB_down and PB_up. Then based on the decimal, to light up the LED.

Thanks!

Regards,
LoneSync
lonesync
 
Posts: 8
Joined: Wed Apr 23, 2008 8:41 am

Postby NickH » Wed Sep 24, 2008 11:02 am

Hi,

You don't need a debouncer for this purpose (but it doesn't hurt). So I think you need to take a step back... Did your earlier experiments work? Can you flash both LEDs?

If so, try some very simple code to synchronize button and connect it to LEDs: one LED should be on when pressed, the other when released:
Code: Select all
reg r1, r2;
always @(posedge clk) begin
  r1 <= sw;
  r2 <= r1;
end
assign LED1 = r2;
assign LED2 = ~r2;


If that doesn't work, then most likely you've set it up with the wrong pins (sorry I don't know how to fix that on Altera, I only know about Xilinx).


About @(posedge clk): Verilog can simulate many things that it can't implement on FPGA. In simulations, you can put @(...) before any statement in an always/initial block -- it means "wait until this change before executing the next instruction".

But for FPGAs, you have to write always @(...). The @ doesn't work anywhere else.

And you can never put @ inside the brackets of if (...), because it's not a condition.

Regards,

Nick
NickH
 
Posts: 88
Joined: Tue Sep 02, 2008 1:53 pm

Postby lonesync » Mon Oct 06, 2008 4:18 am

Currently my codes do work, when the switch is pressed, the LED is on.
The code r as below:

Code: Select all
module testsw (clk, sw, LED);
input clk;
input sw;
output LED;

wire swstate;
debouncer swmod(.clk(clk), .PB(sw), .PB_state(swstate));

reg LED;
always@ (posedge clk) if (swstate == 1) LED <= ~LED; else LED <= 0;

endmodule

module debouncer(clk, PB, PB_state);
input clk;
input PB;

output PB_state;
//output PB_down;
//output PB_up;

reg PB_sync_0;  always @(posedge clk) PB_sync_0 <= ~PB;
reg PB_sync_1;  always @(posedge clk) PB_sync_1 <= PB_sync_0;

reg [15:0] PB_cnt;

reg PB_state;
wire PB_idle = (PB_state==PB_sync_1);
wire PB_cnt_max = &PB_cnt;

always @(posedge clk)
if(PB_idle)
    PB_cnt <= 0;
else
begin
    PB_cnt <= PB_cnt + 1;
    if(PB_cnt_max) PB_state <= ~PB_state;
end

//wire PB_down = ~PB_state & ~PB_idle & PB_cnt_max;
//wire PB_up   =  PB_state & ~PB_idle & PB_cnt_max;

endmodule


will try ur code and see how it works out...

thanks anyway!!
:lol:

I think my second post is a bit misleading.
Actually what i'm trying there is something like below:

Code: Select all
 always@ (posedge clk)  if (posedge PB_down) LED <= ~LED; else LED <= 0;


but it seems like posedge / negedge cant be used with if..else in this arrangement. how come? :?

sorry for the misleading post. :(
lonesync
 
Posts: 8
Joined: Wed Apr 23, 2008 8:41 am

Postby NickH » Wed Oct 08, 2008 10:44 am

"posedge clk" is an event, not a condition. You can only use it in "@(...)". And if you want your code to be synthesizable, you have to write "always @(posedge clk)" and no other edges (except maybe a reset).

Luckily, Jean's website example already has a signal that pulses for one clock cycle, every time the switch is pressed. It's "PB_down". So if you want to toggle the LED every time the switch is pressed, try:
Code: Select all
always @(posedge clk) if (PB_down) LED <= ~LED;


You don't want the "else LED <= 0" part! as this would make the LED go out immediately on the next clock cycle. You'd probably never see it lit.

Regards,

Nick
NickH
 
Posts: 88
Joined: Tue Sep 02, 2008 1:53 pm

Postby lonesync » Mon Nov 17, 2008 12:57 am

Thanks NickH for ur help...
now LED do lid on when buttons are pressed...
either ur filpflop method or Jean's method...

from buttons, now i proceed on...
so i'm now trying the pong game from Jean's web...
with all the tutorials, display on the VGA monitor is not a problem...

problem occurs when i want to use the button instead of the mouse to control the paddle...

Code: Select all
reg startpos = 319;
reg [8:0] position;

reg [15:0] cnt1;
wire cnt1max = & cnt1;

reg [15:0] cnt2;
wire cnt2max = & cnt2;

always@ (posedge clk) if (sw3_state) position = startpos;

always@ (posedge clk) begin
   if (sw1_state == 0)
      cnt1 <= 0;
   else if (sw2_state == 0)
      cnt2 <= 0;
   else
      if (sw1_state)
         cnt1 <= cnt1 + 1;
         if (cnt1max) position <= position + 1;
      if (sw2_state)
         cnt2 <= cnt2 + 1;
         if (cnt2max) position <= position - 1;
end


my intention is to let the paddle to start at the initial position...
i'm not so sure how to do that, so i add another switch to reset it to the position...

Regards,
LoneSync
lonesync
 
Posts: 8
Joined: Wed Apr 23, 2008 8:41 am


Return to Pluto FPGA boards

Who is online

Users browsing this forum: No registered users and 1 guest

cron