Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
12 years ago

Help with internal Clock and States

Hi, I am very knew to Verilog and coding in general. I have to build a state machine so that when I have an input, the internal clock doesnt register it more then once. Instead it will pulse an output for one clock cycle. In order to do this I need to implement Input Conditioning. So there will be what I believe to be 5 states.

A: input 0,clock 0 or 1, output 0

B: input 1, clock 0, output 0

C: input 1, clock 1, output 1

D: input 1 or 0, clock 0, output 1

E: input 1 or 0, clock 1, output 0

I wrote some code which I believe is correct. My main area of concern is when I start the second Always block, the clk isn't posedge in the brackets, because the compiler wouldnt let me do it otherwise. The code compiles, but in the simulator there seems to be no change in the output regardles of inputs.

module attempt3(clk, a, reset, out);

input clk, a, reset;

output out;

reg out;

reg [1:0] state;

parameter A=0, B=1, C=2, D=3, E=4;

always @(state)

begin

case (state)

A:

out = 0;

B:

out = 0;

C:

out = 1;

D:

out = 1;

E:

out = 0;

endcase

end

always @(a, clk, reset)

begin

if (reset)

state = A;

else

case (state)

A:

if (a==0)

state = A;

else

state = B;

B:

if (a==0)

state = A;

else if (clk==0)

state = B;

else

state = C;

C:

if (clk==0)

state = D;

else

state = C;

D:

if (a==0)

state = A;

else

state = E;

E:

if (a==0)

state = A;

else

state = E;

endcase

end

endmodule

Thanks for any advice!

3 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    I suspect your test bench is at fault. I have simulated your code and can see activity on your 'out' signal, except out[3]. However, your code doesn't change that value. So, that's as expected. I've attached my test bench code for reference.

    You suggest you're new to this and, perhaps, your code demonstrates that :cool:. I have to recommend you have a good read through some form of verilog training. There is plenty of help online. You'll not go far wrong having a good read through the following:

    http://www.asic-world.com/verilog/index.html (http://www.asic-world.com/verilog/index.html)

    Perfect for starters, perhaps lacks a little for the more advanced designer.

    Regards,

    Alex
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Thanks for the help Alex, but I realized I mad one vital mistake when making the thread, I posted the wrong code :(. Anyway I have update my OP with the right code. I'll start reading the link you posted.