#### Bobsons

Joined Aug 21, 2014
9
Hi all.
I really need help with Verilog.
Our lecturer has restricted to use if/else and etc.. We are allowed to use just AND OR NOT and etc logic gates.
We are also restricted to use counters like n = n + 1 and supposed to make it with flip-flops

The Prime Number
A prime number (or a prime) is a natural number greater than 1 that has
no positive divisors other than 1 and itself. A natural number greater than
1 that is not a prime number is called a composite number. For example, 5
is prime because 1 and 5 are its only positive integer factors, whereas 6 is
composite because it has the divisors 2 and 3 in addition to 1 and 6.
In this assignment, on the Verilog environment, you will design a prime
counter counting between 1-63 and repeating 5 times by returning to the
beginning. Your each code line must include an explanation line, and another
something important is that your codes must be synthesizable.

Im stuck at making counters because dont know Verilog well
My code is:
Rich (BB code):
module dff (Q,D, CK);
input CK,D;
output Q;

wire NM,NCK;
wire NQ,M;

nand DN1 (NM,D,CK);
nand DN2 (M,NM,CK);
nand DN3 (Q,NQ,NM);
nand ND4 (QN,Q,M);
endmodule

module tb;
reg D, CK;
wire Q;
dff p0(Q, D, CK);
//jkstruct m0(q, qn, t, cp);
initial begin
D = 0;
CK = 1;
#100;
$display(Q); D = 0; CK = 1; #50$display(Q);
D = 1;
CK = 0;
#50
$display(Q); D = 1; CK = 1;$display(Q);
\$finish;
end
endmodule

I cant even test my flip flop...

#### tshuck

Joined Oct 18, 2012
3,534
This is why you shouldn't wait until the project is nearly due before you start asking questions - ask them often and as soon as there is any confusion.

Why can't you test your flip flop?

#### Bobsons

Joined Aug 21, 2014
9
Well I have done all minor modules. just counter left. The problem is that I actually dont understand how to represent it in code. But I have build it on chips with wires and LEDs.

I need a counter with reset on verilog. The modules must be done with logic gates. This is the problem for me
I always get X from Q output... Whatever I do I get x

Last edited:

#### tshuck

Joined Oct 18, 2012
3,534
Code:
module dff (Q,D, CK);
input CK,D;
output Q;

wire NM,NCK;
wire NQ,M;

nand DN1 (NM,D,CK);
nand DN2 (M,NM,CK);
nand DN3 (Q,NQ,NM);
nand ND4 (QN,Q,M);
endmodule
The X means you have undefined signals at some point. Initializing values can help...

Use an always block that makes Q <= D on each rising edge of the clock.

#### Bobsons

Joined Aug 21, 2014
9
Code:
module dff (Q,D, CK);
input CK,D;
output Q;

wire NM,NCK;
wire NQ,M;

nand DN1 (NM,D,CK);
nand DN2 (M,NM,CK);
nand DN3 (Q,NQ,NM);
nand ND4 (QN,Q,M);
endmodule
The X means you have undefined signals at some point. Initializing values can help...

Use an always block that makes Q <= D on each rising edge of the clock.
That is the problem. We are restricted to use always, <= , and etc in the modules.

#### tshuck

Joined Oct 18, 2012
3,534
That is the problem. We are restricted to use always, <= , and etc in the modules.
Restricted to use means you can only use them - you seem to mean restricted from use.

Are you sure this is the case in regards to the flip flop design? I can understand it at the counter level, but not the flip flop level.

Why don't you list everything you can't use instead of telling us after the fact?

#### Bobsons

Joined Aug 21, 2014
9
Restricted to use means you can only use them - you seem to mean restricted from use.

Are you sure this is the case in regards to the flip flop design? I can understand it at the counter level, but not the flip flop level.

Why don't you list everything you can't use instead of telling us after the fact?
Our lecturer said that we are restricted to design the cirquit with if/else <= loops, and all the other high level programming ways.
When I asked him the list of restrictions - he said "estricted to design the cirquit with if/else <= loops, and all the other high level programming ways." and left the class. When I went his office - he said same without any details...
He just said to use logic gates otherwise we get 0 points...

#### Bobsons

Joined Aug 21, 2014
9
I just need to make this flip flop to work. Im getting X as output. But how to set default value to reg without <=?

#### tshuck

Joined Oct 18, 2012
3,534
In your testbench, make sure you put
Rich (BB code):
initial
begin
D =0;
end

#### Bobsons

Joined Aug 21, 2014
9
In your testbench, make sure you put
Rich (BB code):
initial
begin
D =0;
end
Done already. Not helped. Output is always X

#### tshuck

Joined Oct 18, 2012
3,534
Done already. Not helped. Output is always X
Okay, have a look at your flip flop code. Draw it out from the Verilog. You'll find that NQ is not defined, yet is an input to a NAND gate... Your output cannot be determined because you have an indeterminate input.

Last edited:

#### tshuck

Joined Oct 18, 2012
3,534
Take a look at this, it shows how you can implement a D flip flop...

Last edited:

#### Brownout

Joined Jan 10, 2012
2,390
Try this:

NOT N1(NQ, D)
...
NAND DN2(M, NQ, CK)
NAND DN3(Q, QN, NM)
NAND DN4(QN, Q, M)

#### Bobsons

Joined Aug 21, 2014
9
Try this:

NOT N1(NQ, D)
...
NAND DN2(M, NQ, CK)
NAND DN3(Q, QN, NM)
NAND DN4(QN, Q, M)

Thying to implement a JK flip-flop in Verilog but always get X output. My head is going to explode...
Code:
Rich (BB code):
module jkff (q, j, k, clk);
input j, k, clk;
output q;
wire clkn, g1o, g2o, g5o, g6o, qn;

not n1(clkn, clk);

nand g1(g1o, j, clk, qn);
nand g2(g2o, k, clk, q);

nand g3(s, r, g1o);
nand g4(r, s, g2o);

nand g5(g5o, s, clkn);
nand g6(g6o, r, clkn);

nand g7(q, qn, g5o);
nand g8(qn, q, g6o);
endmodule
And a TestBench:
Rich (BB code):
module tb;
reg j, k, clk;
wire q;
jkff jk0(q, j, k, clk);
initial begin
j = 1;
k = 0;
#100
clk = 1;
#20
clk = 0;
#20
clk = 1;

end

endmodule
Used this picture to write code: http://www.indiabix.com/_files/images/electronics-circuits/jk-flip-flop.png

The problem is in
Rich (BB code):
nand g7(q, qn, g5o);
nand g8(qn, q, g6o);

Last edited:

#### Brownout

Joined Jan 10, 2012
2,390
Try initializing clk to zero and make sure you declare r and s.

#### Brownout

Joined Jan 10, 2012
2,390
Oh, one more thing. Your flip-flop will start in an unknown state. You need a way to set or reset it to a known state.

Add a reset wire to g4 and g8.

#### Bobsons

Joined Aug 21, 2014
9
Oh, one more thing. Your flip-flop will start in an unknown state. You need a way to set or reset it to a known state.

Add a reset wire to g4 and g8.
How to? Im newbie in Verilog....

#### Brownout

Joined Jan 10, 2012
2,390
Just make new ports for reset like you have for clk and d. Connect to the inputs like the other signals.

#### Bobsons

Joined Aug 21, 2014
9
The problem is that I dont know why it is not working. New data is not assigning to FF at all

#### Brownout

Joined Jan 10, 2012
2,390
I can only suggest ways to get it to work. You have to try to implement them. Or not if you want to continue to flounder.