Beruflich Dokumente
Kultur Dokumente
end if;
end process;
qb<= not q;
end behavioral;
D-FF
module dataf(d,clk, q, qb);
input d,clk;
output q,qb;
reg q;
always@(posedge(clk))
begin
if (clk)
q=d;
end
assign qb=~q;
endmodule
D-FF
module dataf(d,clk, q, qb);
input d,clk;
output q,qb;
reg q,qb;
always@(posedge(clk))
begin
if (clk)
q=d;
qb=~q;
end
endmodule
D-FF
module dataf(d,clk,q,qb);
input d,clk;
output q,qb;
reg q,qb;
always@(posedge(clk))
begin
q=d;
qb=~q;
end
endmodule
T-FF
module tff(t,clk, q,qb);
input t,clk;
output q,qb;
reg q;
initial q=0;
always@(posedge clk)
begin
if (clk)
if (t==0)
q=q;
else if (t==1)
q=~q;
end
assign qb=~q;
endmodule
Simulation
T-FF
module tff(t,clk, q,qb);
input t,clk;
output q,qb;
reg q;
initial q=0;
always@(posedge clk)
begin
if (t==0)
q=q;
else if (t==1)
q=~q;
end
assign qb=~q;
endmodule
T-FF
module tff(t,clr,clk, q,qb);
input t,clr,clk;
output q,qb;
reg q;
always@(posedge clk,clr)
begin
if (clr==0)
q=0;
else if (clk)
if (t==0)
q=q;
else if (t==1)
q=~q;
end
assign qb=~q;
endmodule
T-FF
module tff(t,clr,clk, q,qb);
input t,clr,clk;
output q,qb;
reg q;
always@(posedge clk,clr)
begin
if (clr==0)
q=0;
else if (t==0)
q=q;
else if (t==1)
q=~q;
end
assign qb=~q;
endmodule
waveforms
Mod-5 counter
entity mod5cntr is
Port ( clk,clr : in std_logic;
q : out std_logic_vector(2 downto 0));
end mod5cntr;
architecture Behavioral of mod5cntr is
begin
process(clk,clr)
variable s:std_logic_vector(2 downto 0);
begin
if clr='0'then s:="000";
elsif clk'event and clk='0' then
if s<"100" then s:=s+1;
else s:="000";
end if;
end if;
q<=s;
end process;
end Behavioral;
Up-counter
module upcnt(clr,clk, q);
input clr,clk;
output [1:0] q;
reg [1:0]q;
always@(clr,posedge clk)
begin
if (clr==0)
q=00;
else if (clk)
q=q+1;
end
endmodule
CASE statement
case condition is
when choice1=>statements;
when choice2=>statements;
when choice3=>statements;
when others=>statements;
end case;
Case statement in verilog
case (condition)
choice1: begin
statements;
end
choice2: begin statements; end
choice3: begin statements; end
default: begin statements; end
endcase
Mux(4:1)
entity mux4to1 is
Port ( i : in std_logic_vector(3 downto 0);
s : in std_logic_vector(1 downto 0);
y : out std_logic);
end mux4to1;
architecture Behavioral of mux4to1 is
begin
process(s,i)
begin
case s is
when "00"=>y<=i(0);
when "01"=>y<=i(1);
when "10"=>y<=i(2);
when “11”=>y<=i(3);
end case;
end process;
end Behavioral;
Mux(4:1)
entity mux4to1 is
Port ( i : in std_logic_vector(3 downto 0);
s : in std_logic_vector(1 downto 0);
e : in std_logic;
y : out std_logic);
end mux4to1;
architecture Behavioral of mux4to1 is
begin
process(s,i,e)
begin
if e='0' then
case s is
when "00"=>y<=i(0);
when "01"=>y<=i(1);
when "10"=>y<=i(2);
when others=>y<=i(3);
end case;
else y<='Z';
end if;
end process;
end Behavioral;
Multiplexer(4:1)
module multi4to1(i, s, y);
input [3:0] i;
input [1:0] s;
output y;
reg y;
always@(i,s)
begin
case (s)
2'b00: y=i[0];
2'b01: y=i[1];
2'b10: y=i[2];
2’b11: y=i[3];
endcase
end
endmodule
Multiplexer(4 to1)
module multi4to1(i, s, e, y);
input [3:0] i;
input [1:0] s;
input e;
output y;
reg y;
always@(i,s,e)
begin
if (e==0)
case (s)
2'b00:y=i[0];
2'b01:y=i[1];
2'b10:y=i[2];
default:y=i[3];
endcase
else y=1'bz;
end
endmodule
entity jkf is
JKFF
Port ( j,k,clk,clr : in STD_LOGIC;
q,qb : out STD_LOGIC);
end jkf;
architecture Behavioral of jkf is
Begin
process(clk,clr)
variable p :std_logic_vector(1 downto 0);
variable y:std_logic;
begin
p:=j&k;
if clr='0' then y:='0';
elsif rising_edge (clk) then
case p is
when "00"=>y:=y;
when "01"=>y:='0';
when "10"=>y:='1';
when "11"=>y:=not y;
when others=>null;
end case;
end if;
q<=y;
qb<= not y;
end process;
end Behavioral;
entity usr is
Universal Shift Register
Port ( clk,clr,si : in STD_LOGIC;
d : in STD_LOGIC_VECTOR (3 downto 0);
sel : in STD_LOGIC_VECTOR (1 downto 0);
q : out STD_LOGIC_VECTOR (3 downto 0));
end usr;
architecture Behavioral of usr is
Begin
process(clk,clr)
variable s:std_logic_vector(3 downto 0);
begin
if clr='0' then s:=(others=>'0');
elsif clk'event and clk='1' then
case sel is
when "00"=> s:=s;
when "01"=> s:=d;
when "10"=> s:=si&s(3 downto 1);
when "11"=> s:=s(2 downto 0)&si;
when others=>null;
end case;
end if;
q<=s;
end process;
end Behavioral;
State Diagram
VHDL code for state diagram
entity statediagram is
Port ( clk,clr : in STD_LOGIC;
q : out STD_LOGIC_VECTOR (1 downto 0));
end statediagram;
architecture Behavioral of statediagram is
begin
process(clk,clr)
variable s:std_logic_vector(1 downto 0);
begin
if clr='0' then s:="00";
elsif clk'event and clk='1' then
case s is
when "00" => s:="01";
when "01" => s:="10";
when "10" => s:="11";
when "11" => s:="00";
when others=>null;
end case;
end if;
q<=s;
end process;
end Behavioral;
HDL code for the following state diagram
LOOPS
(a) FOR LOOP: Eg: Factorial of 5
SYNTAX:
PROCESS ( )
PROCESS ( ) BEGIN
BEGIN fact <=1;
FOR identifier IN range LOOP FOR i IN 1 to 5
LOOP
:
fact <=fact*i;
sequential statements; END LOOP;
:
END LOOP; END PROCESS;
END PROCESS;
(b) WHILE LOOP
Eg: Factorial of 5
SYNTAX: PROCESS ( )
PROCESS ( )
BEGIN
BEGIN fact <=1;
WHILE (condition) LOOP i<=1;
: WHILE(i<=5)
sequential statements; LOOP
:
fact <=fact*i;
END LOOP;
i<=i+1;
END PROCESS; END LOOP;
END PROCESS;
Loops in Verilog
FOR loop:
for(initial value, condition,
increment/decrement)
begin
statements;
end
While Loop
While (condition)
begin
statements;
end