EmbDev.net

Forum: FPGA, VHDL & Verilog D Flip-Flop VHDL code


von Josh (Guest)


Rate this post
useful
not useful
Hi!

Can anybody help me with D-Flip Flop with synchronous set and reset and 
clock enable? I was trying write this code, but actually i can't

von -gb- (Guest)


Rate this post
useful
not useful
Greetings!

Untested:
1
library IEEE;
2
use IEEE.STD_LOGIC_1164.ALL;
3
use IEEE.NUMERIC_STD.ALL;
4
5
entity dff is port(
6
  nCLR : in std_logic;
7
  nPR : in std_logic;
8
  D : in std_logic;
9
  CP : in std_logic;
10
  Q : out std_logic;
11
  nQ : out std_logic);
12
end entity dff;
13
 
14
architecture behav of dff is
15
16
begin
17
18
process (nCLR, nPR, CP) begin
19
  if nCLR = '0' then
20
    Q <= '0';
21
    nQ <= '1';
22
  elsif nPR = '0' then
23
    Q <= '1';
24
    nQ <= '0';
25
  elsif rising_edge(CP) then
26
    Q <= D;
27
    nQ <= not D;
28
  end if;
29
end process;
30
31
32
33
end architecture behav;

von ktorkkelson (Guest)


Rate this post
useful
not useful
Hi, all.

Not completely correct, unfortunately. The description above has an 
asynchnonous(!) set and reset, and is also missing a clock enable.
1
...
2
process (clk) begin
3
  if rising_edge(clk) then
4
    if (set = '1') then
5
      Q  <= '1';
6
    elsif (clear = '1') then
7
      Q  <= '0';
8
    elsif (clk_en = '1') then
9
      Q  <= D;
10
    end if;  
11
  end if;
12
end process;
13
...

Cheers

von -gb- (Guest)


Rate this post
useful
not useful
Sorry ...

von Lothar M. (Company: Titel) (lkmiller) (Moderator)


Rate this post
useful
not useful
Josh wrote:
> Can anybody help me with D-Flip Flop with synchronous set and reset and
> clock enable?
That is nearby the most simple exercise after "assign an output to an 
input". You should get the trick on that before going on...

> I was trying write this code, but actually i can't
Show what you have and tell us what problems you encounter. Then we 
can help you.

Otherwise we can continue posting several solutions and you will not be 
able to check out which one ist ok.

ktorkkelson wrote:
> The description above has an asynchnonous(!) set and reset
Getting a synchronous description for sure can be achived this way:
1
library IEEE;
2
use IEEE.STD_LOGIC_1164.ALL;
3
use IEEE.NUMERIC_STD.ALL;
4
5
entity dff is port(
6
  R   : in std_logic;
7
  S   : in std_logic;
8
  CE  : in std_logic;
9
  CLK : in std_logic;
10
  D   : in std_logic;
11
  Q   : out std_logic);
12
end entity dff;
13
 
14
architecture behav of dff is
15
16
begin
17
18
  process begin
19
    wait until rising_edge(CLK);
20
    if    R = '1' then -- reset has priority over set
21
      Q <= '0';
22
    elsif S = '1' then
23
      Q <= '1';
24
    elsif CE = '1' then
25
      Q <= D;
26
    end if;
27
  end process;
28
29
end architecture behav;

: Edited by Moderator
von Josh (Guest)


Rate this post
useful
not useful
Ok, so it is D Flip-Flop with Synchronous Reset, Synchronous Set and 
Clock Enable?

von Lothar M. (Company: Titel) (lkmiller) (Moderator)


Rate this post
useful
not useful
Josh wrote:
> Ok, so it is D Flip-Flop with Synchronous Reset, Synchronous Set and
> Clock Enable?
Check it out with the simulator. Its just 7 lines you have to 
understand.

von Josh (Guest)


Rate this post
useful
not useful
Isn't it asynchronous reset in this code?

von Christophz (Guest)


Rate this post
useful
not useful
Josh wrote:
> Isn't it asynchronous reset in this code?

Which one? Pick one, you have a 50:50 chance :-)

von Lothar M. (Company: Titel) (lkmiller) (Moderator)


Rate this post
useful
not useful
Josh wrote:
> Isn't it asynchronous reset in this code?
You forgot to attach "this code" of yours.

Christophz wrote:
> Which one? Pick one, you have a 50:50 chance :-)
Based on the 3 code samples I calculate a fairly good 66% chance to get 
a completely synchronous flipflop.

von Alexander S. (Company: Home) (alex_isr)


Rate this post
useful
not useful
Josh wrote:
> Hi!
>
> Can anybody help me with D-Flip Flop with synchronous set and reset and
> clock enable? I was trying write this code, but actually i can't


I think it's may be :
1
use IEEE.STD_LOGIC_1164.ALL;
2
use IEEE.NUMERIC_STD.ALL;
3
4
entity dff is port(
5
  nCLR     : in std_logic;
6
  nPR      : in std_logic;
7
  D        : in std_logic;
8
  ClockIn  : in std_logic;
9
  ClockEn  : in std_logic;
10
  Q_Out    : out std_logic;
11
  nQ_Out   : out std_logic);
12
end entity dff;
13
14
architecture behav of dff is
15
signal Q,nQ : std_logic;
16
17
begin
18
19
  Q_Out   <= Q;
20
  nQ_Out   <= nQ;
21
22
23
 DFF_Process:
24
process (ClockIn)
25
 variable SetReset : std_logic_vector(1 downto 0);
26
begin
27
SetReset(0) := nCLR;
28
SetReset(1) := nPR;
29
 if(ClockIn'event and ClockIn = '1') then
30
  case SetReset is
31
   when "00" =>                                                                 -- No operation
32
     Q <= Q;
33
    nQ <= nQ;
34
   when "01"  =>                                                                -- nPR active
35
     Q <= '1';
36
    nQ <= '0';
37
   when "10"  =>                                                                -- nCLR active
38
    Q  <= '0';
39
    nQ <= '1';
40
   when others =>
41
    if(ClockEn = '1') then
42
     Q  <= D;
43
     nQ <= not D;
44
    else
45
     Q  <= Q;
46
     nQ <= nQ;
47
    end if;
48
  end case;
49
 end if;
50
end process  DFF_Process;
51
52
end architecture behav;

: Edited by User
von Lothar M. (Company: Titel) (lkmiller) (Moderator)


Rate this post
useful
not useful
Alexander S. wrote:
> nCLR     : in std_logic;
> nPR      : in std_logic;
That means for me that CLEAR and PRESET are active, when they are '0'. 
So the "forbidden" input combination is "00". But in your incredible 
spaciuos written code that is the only combination where the input are 
stored.

But in your code "00" is the "normal" state, in which the data input is 
stored in the flipflop with each clock.

And also usually CLEAR and PRESET are inputs for an asynchronous 
flipflop. Synchronous flipflops have SET and RESET inputs (but beware: 
RS-Flipflops also have SET and RESET, but no clock)

>   when "00" =>                -- No operation
Usually a synchronous flipflop with SET and RESET has a priotity on 
those two inputs: the RESET input his the most important. When RESET is 
active, then its not relevant what level the SET and D inputs have.

von Alexander S. (Company: Home) (alex_isr)


Rate this post
useful
not useful
Lothar M. wrote:

> And also usually CLEAR and PRESET are inputs for an asynchronous flipflop.

 But

Josh wrote:
> Hi!
>
> Can anybody help me with D-Flip Flop with synchronous set and reset and
> clock enable?

 Regards Alex.

: Edited by User
von Alexander S. (Company: Home) (alex_isr)


Rate this post
useful
not useful
Lothar M. wrote:
> Alexander S. wrote:
>> nCLR     : in std_logic;
>> nPR      : in std_logic;
> That means for me that CLEAR and PRESET are active, when they are '0'.
> So the "forbidden" input combination is "00". But in your incredible
> spaciuos written code that is the only combination where the input are
> stored.
>
> But in your code "00" is the "normal" state, in which the data input is
> stored in the flipflop with each clock.

  State "00" is wrong state .
 Designer must lock and prevent this state.

 In design we can't leave this state not defined.

 FlipFlop always have state '0' or '1'.

 In my design : if designer not prevented wrong input state "00" , Flip 
Flop
 save it last state and do not save/received input state.

 Regards Alex.

: Edited by User
Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.