[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Some Verilog

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 7
Thread images: 1

File: 1257542324414.png (6KB, 400x400px) Image search: [Google]
1257542324414.png
6KB, 400x400px
Hello, I need some advice on how to design a special state machine in verilog. Basically what I want to have it do is the following:

If btn0=0 & btn1=0, then selector=0.

If btn0=1 & btn1=0, then selector=0 and start counting to 5.

If count=5 and no btn has been pressed during this time then reset count to 0 and selector=0.

If count <5 & (btn0=1 or btn1=1), then selector=1 and start counting to 12. (selector needs to stay =1 while counting to 12).

If count=12 &(btn0=0 and btn1=0), then selector=0.

If count<12 &(btn0=1 or btn1=1), then selector=1 and restart counting from 0 to 12.

All of this should happen to the rhythm of a posedge clock. :)

I need help to get from this pseudo-code, to the actual verilog code.
>>
So, you want to draw out the state diagram and all the transitions, then at each posedge of the clock, you simply switch states, according to the logic in your state table. Here's an example from a past lab I did:

//SW[0] reset when 0
//SW[1] input signal

//KEY[0] clock signal

//LEDR[3:0] displays current state
//LEDR[9] displays output

module Lab5Part1(SW, KEY, LEDR);
input [9:0] SW;
input [3:0] KEY;
output [9:0] LEDR;

wire w, clock, resetn, out_light;

reg [3:0] y_Q, Y_D; // y_Q represents current state, Y_D represents next state

localparam A = 4'b0000, B = 4'b0001, C = 4'b0010, D = 4'b0011, E = 4'b0100, F = 4'b0101, G = 4'b0110;

assign w = SW[1];
assign clock = ~KEY[0];
assign resetn = SW[0];

//State table
//The state table should only contain the logic for state transitions
//Do not mix in any output logic. The output logic should be handled separately.
//This will make it easier to read, modify and debug the code.
always@(*)
begin: state_table
case (y_Q)
A: begin
if (!w) Y_D <= A;
else Y_D <= B;
end
B: begin
if(!w) Y_D <= A;
else Y_D <= C;
end
C: Y_D = (!w) ? E : D;
D: Y_D = (!w) ? E : F;
E: Y_D = (!w) ? A : G;
F: Y_D = (!w) ? E : F;
G: Y_D = (!w) ? A : C;
default: Y_D = A;
endcase
end // state_table

// State Registers
always @(posedge clock)
begin: state_FFs
if(resetn == 1'b0)
y_Q <= A; // Should set reset state to state A
else
y_Q <= Y_D;
end // state_FFS

// Output logic
// Set out_light to 1 to turn on LED when in relevant states
assign out_light = ((y_Q == F ) | (y_Q == G));

assign LEDR[9] = out_light;
assign LEDR[3:0] = y_Q;
endmodule
>>
I did do a state diagram but I'm not going to use it because it's way too big for this type of problem. It would be much easier to use behavioral logic to describe the system in a few lines as opposed to writing code for each state switch. I just don't understand how to have two different counters in the same always block that get triggered based on some inputs.
>>
This is how I started but now I'm stuck

always @ (posedge test or posedge reset)
begin
if (btn0 == 0 & btn1 == 0)
selector <= 0;
count <= 0;
else if (btn0 == 1 & btn1 == 0)
selector <= 0;
count <= count +1;


else if (count < 5 & btn0 ==1 or btn1 == 1)
selector <= 1;
count <= count +1;
else if
>>
>>228228
I think that you are running into the issue of trying to find a "nice programming" way of solving the problem, which I don't think there is. This is low level programming - its going to be ugly. My suggestion would be to go with the state diagram and translate that directly into verilog. It's going to be long but not that long (maybe like - 30 states?) I think that's the correct approach to these sorts of problems. Got me through the course fine.
>>
>>228235
on second thought there should not be that many states if you create a very efficient FSM
>>
Its not about getting through the course. It's about learning how to code properly and not like a 5 year old. If I got a job as an FPGA programmer and someone saw me code like this, they would laugh my ass out of the building.
Thread posts: 7
Thread images: 1


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.