I am attempting to create a 16-bit factorial calculator for an unsigned binary number. In doing so I have created a data path and a state machine that
a.) outputs a final value of 1 if the value input is 0
b.) displays and overflow flag if the input is over 8!
c.) Calculates the value for the factorial if the value input is less then 8 and not zero.
The system seems to fall apart if the value I input is anything over 3 yet still not an overflow. I think the issue is somewhere in Test3, Update 1, update 2 in my state machine. Could anyone help explain what is going wrong?
State Machine:
library IEEE;
use IEEE.STD_LOGIC_1164.all;
entity fact_16_ctrlv2 is
port(
go : in STD_LOGIC;
clk : in STD_LOGIC;
clr : in STD_LOGIC;
overflow : in STD_LOGIC;
numeqcnt : in STD_LOGIC;
factzero : in STD_LOGIC;
flag: out STD_logic;
xload : out STD_LOGIC;
cntmux : out STD_LOGIC;
cntload : out STD_LOGIC;
factload : out STD_LOGIC;
factmux : out STD_LOGIC;
finalload : out STD_LOGIC;
finalmuxselect: out STD_logic;
decimalpoint: out std_logic
);
end fact_16_ctrlv2;
--}} End of automatically maintained section
architecture fact_16_ctrlv2 of fact_16_ctrlv2 is
type state_type is (start, input, test1, test2, test3, update1, update2, update3, done);
signal present_state, next_state: state_type;
begin
sreg: process(clk, clr)
begin
if clr='1' then
present_state <= start;
elsif clk'event and clk='1' then
present_state <= next_state;
end if;
end process;
c1: process(present_state, go, overflow, factzero, numeqcnt)
begin
case present_state is
when start =>
if go = '1' then
next_state <= input;
else
next_state <= start;
end if;
when input =>
next_state <= test1;
when test1 =>
if factzero= '1' then
next_state <= done;
else
next_state <= test2;
end if;
when test2 =>
if overflow = '1' then
next_state <= done;
else
next_state <= update3;
end if;
when update3 =>
next_state <= test3;
when test3 =>
if numeqcnt = '1' then
next_state <= done;
else
next_state <= update1;
end if;
when update1 =>
next_state <= update2;
when update2 =>
next_state <= test3;
when done =>
next_state <= done;
when others =>
null;
end case;
end process;
C2: process(present_state, overflow, numeqcnt, factzero)
begin
xload<= '0';
flag <= '0';
cntmux <= '0';
cntload<= '0';
factload<='0';
factmux<='0';
finalload <='0';
finalmuxselect<='0';
decimalpoint<='0';
case present_state is
when input =>
xload<='1';
--cntmux<='1';
--cntload<='1';
when test2 =>
if overflow ='1' then
flag <= '1';
finalmuxselect<='0';
finalload<='1';
factload <='1';
decimalpoint <='1';
end if;
when test1 =>
if factzero ='1' then
flag <= '1';
finalmuxselect <='1';
finalload <='1';
end if;
when update3 =>
cntmux<='1';
cntload<='1';
factmux<= '1';
factload<= '1';
when test3 =>
if numeqcnt ='1' then
finalmuxselect<='0';
--finalload<='1';
factload <='1';
cntload<='1';
factmux<='0';
else
end if;
when update1 =>
factmux<='0';
factload<='1';
when update2 =>
cntmux<='0';
cntload<='1';
--factload<='1';
when done =>
finalload<='1';
when others =>
null;
end case;
end process;
end fact_16_ctrlv2;
Data Path
library IEEE;
use IEEE.STD_LOGIC_1164.all;
entity fact_16_dp is
port(
clr : in STD_LOGIC;
clk : in STD_LOGIC;
X : in STD_LOGIC_VECTOR(3 downto 0);
overflow : out STD_LOGIC;
finalfact : out STD_LOGIC_VECTOR(15 downto 0);
cntload, cntmult, factload, factmult, xload, finalload :in Std_logic;
equalone, numcnt : out Std_logic;
finalmuxselect, flagin: in STD_logic
);
end fact_16_dp;
--}} End of automatically maintained section
architecture fact_16_dp of fact_16_dp is
-- Component declaration of the "mult16b(mult16b)" unit defined in
-- file: "./../src/multiplier.vhd"
component mult16b
port(
a : in STD_LOGIC_VECTOR(15 downto 0);
b : in STD_LOGIC_VECTOR(15 downto 0);
p : out STD_LOGIC_VECTOR(15 downto 0));
end component;
for all: mult16b use entity work.mult16b(mult16b);
-- Component declaration of the "comp(comp)" unit defined in
-- file: "./../src/comparitor.vhd"
component comp
generic(
N : INTEGER := 8);
port(
x : in STD_LOGIC_VECTOR(N-1 downto 0);
y : in STD_LOGIC_VECTOR(N-1 downto 0);
gt : out STD_LOGIC;
eq : out STD_LOGIC;
lt : out STD_LOGIC);
end component;
for all: comp use entity work.comp(comp);
-- Component declaration of the "adder(adder)" unit defined in
-- file: "./../src/adder.vhd"
component adder
generic(
N : INTEGER := 8);
port(
a : in STD_LOGIC_VECTOR(N-1 downto 0);
b : in STD_LOGIC_VECTOR(N-1 downto 0);
y : out STD_LOGIC_VECTOR(N-1 downto 0));
end component;
for all: adder use entity work.adder(adder);
-- Component declaration of the "reg(reg)" unit defined in
-- file: "./../src/reg.vhd"
component reg
generic(
N : INTEGER := 8);
port(
load : in STD_LOGIC;
clk : in STD_LOGIC;
clr : in STD_LOGIC;
d : in STD_LOGIC_VECTOR(N-1 downto 0);
q : out STD_LOGIC_VECTOR(N-1 downto 0));
end component;
for all: reg use entity work.reg(reg);
-- Component declaration of the "mux2g(mux2g)" unit defined in
-- file: "./../src/mux21.vhd"
component mux2g
generic(
N : INTEGER);
port(
a : in STD_LOGIC_VECTOR(N-1 downto 0);
b : in STD_LOGIC_VECTOR(N-1 downto 0);
s : in STD_LOGIC;
y : out STD_LOGIC_VECTOR(N-1 downto 0));
end component;
for all: mux2g use entity work.mux2g(mux2g);
-- Component declaration of the "mux4g(mux4g)" unit defined in
-- file: "./../src/mux4to1.vhd"
component mux4g
generic(
N : INTEGER);
port(
a : in STD_LOGIC_VECTOR(N-1 downto 0);
b : in STD_LOGIC_VECTOR(N-1 downto 0);
c : in STD_LOGIC_VECTOR(N-1 downto 0);
s : in STD_LOGIC_VECTOR(1 downto 0);
y : out STD_LOGIC_VECTOR(N-1 downto 0));
end component;
for all: mux4g use entity work.mux4g(mux4g);
--signal cntload, cntmult, numcnt, factload, factmult, xload, numload, equalone, finalload :Std_logic;
signal adderout, cntregout, cntregin, x2 :Std_logic_vector(3 downto 0);
signal xin, factregin, factregout, overin, factmout, checkzero, numregout, flag, finalmuxout, cntregoutb :Std_logic_vector(15 downto 0);
begin
x2 <= x;
xin <= "000000000000" & x2; --Change 4 bit number input into a 16 bit number
cntregoutb <= "000000000000" & cntregout; --change count from 4 bit value to a 16 bit value
flag <= "000000000000000" & flagin;
cntreg : reg --4-bit counter register
generic map(
N => 4
)
port map(
load => cntload,
clk => clk,
clr => clr,
d => cntregin, --not in drawing
q => cntregout
);
factreg : reg --16-bit fact register
generic map(
N => 16
)
port map(
load => factload,
clk => clk,
clr => clr,
d => factregin,
q => factregout
);
--numreg : reg --16-bit num register
--generic map(
--N => 16
--)
--port map(
--load => numload,
--clk => clk,
--clr => clr,
--d => xin,
--q => numregout
--);
xreg : reg --4-bit initial x vlaue register
generic map(
N => 4
)
port map(
load => xload,
clk => clk,
clr => clr,
d => x, --x(3:0)
q => x2
);
finalreg : reg --16-bit final fact register
generic map(
N => 16
)
port map(
load => finalload,
clk => clk,
clr => clr,
d => finalmuxout,
q => finalfact
);
cntmux : mux2g --4-bit cnt mux
generic map(
N => 4
)
port map(
b => "0001", --initial value set
a => adderout, --value after initial run through
s => cntmult,
y => cntregin --cntregin not in drawing
);
factmux : mux2g --16-bit fact mux
generic map(
N => 16
)
port map(
b => "0000000000000001", --initial value set
a => factmout, --value after initial run through
s => factmult,
y => factregin
);
add1 : adder --Increment counter 4-bit
generic map(
N => 4
)
port map(
a => cntregout, --add 1
b => "0001", --1
y => adderout --out of the adder
);
multiplier : mult16b --multiply cnt and fact 16-bit
port map(
a => factregout,
b => cntregoutb, --cnt plus 12 zeros to 16-bit
p => factmout --Multiplier out
);
greater8 : comp --16-bit check x overflow if greater then 8
generic map(
N => 16
)
port map(
x => xin, --check value to 8
y => "0000000000001000",
gt => overflow --send overflow flag
--eq => eq,
--lt => lt
);
check0 : comp --16-bit check x not equal to zero
generic map(
N => 16
)
port map(
x => xin,
y => "0000000000000000",
--gt => gt,
eq => equalone
--lt => lt
);
numeqcnt : comp --16-bit check if num equals cnt
generic map(
N => 16
)
port map(
x => xin,
y => cntregoutb, --need 16 bit
--gt => gt,
eq => numcnt --not in drawing
--lt => lt
);
finalmux: mux2g
generic map(
N => 16
)
port map(
a => factregout,
b => flag,
s => finalmuxselect,
y => finalmuxout
);
end fact_16_dp;
Connected together
-------------------------------------------------------------------------------
--
-- Title : fact_16
-- Design : lab4
-- Author :
-- Company :
--
-------------------------------------------------------------------------------
--
-- File : c:\My_Designs\lab4\lab4\src\fact_16.vhd
-- Generated : Tue Oct 14 16:40:38 2014
-- From : interface description file
-- By : Itf2Vhdl ver. 1.22
--
-------------------------------------------------------------------------------
--
-- Description :
--
-------------------------------------------------------------------------------
--{{ Section below this comment is automatically maintained
-- and may be overwritten
--{entity {fact_16} architecture {fact_16}}
library IEEE;
use IEEE.STD_LOGIC_1164.all;
entity fact_16 is
port(
go : in STD_LOGIC;
clk : in STD_LOGIC;
clr : in STD_LOGIC;
x : in STD_LOGIC_VECTOR(3 downto 0);
overflowB : out STD_LOGIC;
factout : out STD_LOGIC_VECTOR(15 downto 0)
);
end fact_16;
architecture fact_16 of fact_16 is
-- Component declaration of the "fact_16_dp(fact_16_dp)" unit defined in
-- file: "./../src/fact_16_dp.vhd"
component fact_16_dp
port(
clr : in STD_LOGIC;
clk : in STD_LOGIC;
X : in STD_LOGIC_VECTOR(3 downto 0);
overflow : out STD_LOGIC;
finalfact : out STD_LOGIC_VECTOR(15 downto 0);
cntload : in STD_LOGIC;
cntmult : in STD_LOGIC;
factload : in STD_LOGIC;
factmult : in STD_LOGIC;
xload : in STD_LOGIC;
finalload : in STD_LOGIC;
equalone : out STD_LOGIC;
numcnt : out STD_LOGIC;
finalmuxselect : in STD_LOGIC;
flagin : in STD_LOGIC);
end component;
for all: fact_16_dp use entity work.fact_16_dp(fact_16_dp);
-- Component declaration of the "fact_16_ctrlv2(fact_16_ctrlv2)" unit defined in
-- file: "./../src/fact_16_ctrlv2.vhd"
component fact_16_ctrlv2
port(
go : in STD_LOGIC;
clk : in STD_LOGIC;
clr : in STD_LOGIC;
overflow : in STD_LOGIC;
numeqcnt : in STD_LOGIC;
factzero : in STD_LOGIC;
flag : out STD_LOGIC;
xload : out STD_LOGIC;
cntmux : out STD_LOGIC;
cntload : out STD_LOGIC;
factload : out STD_LOGIC;
factmux : out STD_LOGIC;
finalload : out STD_LOGIC;
finalmuxselect : out STD_LOGIC;
decimalpoint : out STD_LOGIC);
end component;
for all: fact_16_ctrlv2 use entity work.fact_16_ctrlv2(fact_16_ctrlv2);
signal overflow, numeqcnt, factzero, xload, cntmux, cntload, factload, flag, factmux, numload, finalload: STD_logic;
signal finalmuxselect: std_logic;
begin
Dpath : fact_16_dp
port map(
clr => clr,
clk => clk,
X => X,
overflow => overflow,
finalfact => factout,
cntload => cntload,
cntmult => cntmux,
factload => factload,
factmult => factmux,
xload => xload,
finalload => finalload,
equalone => factzero,
numcnt => numeqcnt,
finalmuxselect => finalmuxselect,
flagin => flag
);
Cunit : fact_16_ctrlv2
port map(
go => go,
clk => clk,
clr => clr,
overflow => overflow,
numeqcnt => numeqcnt,
factzero => factzero,
flag => flag,
xload => xload,
cntmux => cntmux,
cntload => cntload,
factload => factload,
factmux => factmux,
finalload => finalload,
finalmuxselect => finalmuxselect,
decimalpoint => overflowB
);
end fact_16;
Related
While all the code is perfectly compiled by ModelSim, I can't simulate it because of "Error loading design"
This is for a CRC encode and decode using a Linear Feedback Shift Register that uses D-FlipFlop as components. So the project is actually composed by the CRC box that contains the LFSR made by DFF.
library IEEE;
use IEEE.std_logic_1164.all;
entity CRC is
generic (
NBit : positive := 64;
poly : positive := 8
);
port(
clk :in std_logic;
reset :in std_logic;
md :in std_logic; --1 per sender, 0 per receiver
input :in std_logic_vector(Nbit-1 downto 0);
dout_s :out std_logic_vector(Nbit-1 downto 0);
dout_r :out std_logic_vector(Nbit-poly-1 downto 0)
);
end CRC;
architecture rtl of CRC is
component LFSR
generic (N : positive := 8);
port(
clk :in std_logic;
reset :in std_logic;
din :in std_logic;
dout :out std_logic_vector(N-1 downto 0);
ready :out std_logic
);
end component LFSR;
signal input_temp :std_logic_vector(Nbit-1 downto 0);
signal input_LFSR :std_logic;
signal output_LFSR :std_logic_vector(poly-1 downto 0);
signal ready_LFSR :std_logic;
constant crc_check :std_logic_vector(poly-1 downto 0):= (others => '0');
begin
LFSR_o: LFSR generic map (N => poly)
port map(
clk => clk,
reset => reset,
din => input_LFSR,
dout => output_LFSR,
ready => ready_LFSR
);
process (clk)
variable i : natural := 0;
begin
if (md = '1') then
input_temp(Nbit-1 downto poly)<=input(Nbit-1 downto poly);
input_temp(7 downto 0) <=(others =>'0');
if(rising_edge(clk)) then
input_LFSR <= input_temp(i);
i:= i+1;
if(ready_LFSR = '1') then
dout_s <= input(Nbit-1 downto 0) & output_LFSR;
end if;
end if;
elsif(md = '0') then
input_temp(Nbit-1 downto 0)<=input(Nbit-1 downto 0);
if(rising_edge(clk)) then
input_LFSR <= input_temp(i);
i:= i+1;
if(ready_LFSR = '1') then
--codice per il controllo
for t in 0 to poly-1 loop
if (output_LFSR(t)='1') then
dout_r <=(others =>'0');
exit;
elsif(t=poly-1 and output_LFSR(t)='0') then
dout_r <= input(Nbit-1 downto poly);
end if;
end loop;
end if;
end if;
end if;
end process;
end rtl;
here the LFSR
library IEEE;
use IEEE.std_logic_1164.all;
entity LFSR is
generic (NBit : positive := 8);
port(
clk :in std_logic;
reset :in std_logic;
din :in std_logic;
dout :out std_logic_vector(Nbit-1 downto 0);
ready :out std_logic
);
end LFSR;
architecture rtl of LFSR is
component DFC
port(
clk :in std_logic;
reset :in std_logic;
d :in std_logic;
crc :out std_logic;
q :out std_logic
);
end component DFC;
signal q_s : std_logic_vector (NBit-1 downto 0):= (others => '0');
signal crc_t : std_logic_vector (NBit-1 downto 0):= (others => '0'); --registro temporaneo su cui fare le operazioni
signal int_0 :std_logic := '0';
signal int_2 :std_logic := '0';
signal int_4 :std_logic := '0';
signal int_8 :std_logic := '0';
begin
int_0<= din xor q_s(7);
int_2<= q_s(1) xor q_s(7);
int_4<= q_s(3) xor q_s(7);
GEN: for i in 0 to Nbit-1 generate
FIRST: if i=0 generate
FF1: DFC port map (
clk => clk,
reset => reset,
d => int_0,
crc => crc_t(i), --funziona benissimo se metto dout(i)
q => q_s(i)
);
end generate FIRST;
THIRD: if i=2 generate
FF2: DFC port map (
clk => clk,
reset => reset,
d => int_2,
crc => crc_t(i),
q => q_s(i)
);
end generate THIRD;
FIFTH: if i=4 generate
FF4: DFC port map (
clk => clk,
reset => reset,
d => int_4,
crc => crc_t(i),
q => q_s(i)
);
end generate FIFTH;
INTERNAL: if i>0 and i<Nbit-1 and i/= 2 and i/=4 generate
FFI: DFC port map (
clk => clk,
reset => reset,
d => q_s(i-1),
crc => crc_t(i),
q => q_s(i)
);
end generate INTERNAL;
LAST: if i=Nbit-1 generate
FFN: DFC port map (
clk => clk,
reset => reset,
d => q_s(i-1),
crc => crc_t(i),
q => q_s(i)
);
end generate LAST;
end generate GEN;
process(clk)
variable t : natural := 0;
begin
--ready <= '0';
if(rising_edge(clk)) then
t:= t+1;
if t=24 then --per qualche ragione ho bisogno di 3 cicli di clock in più per arrivare al risultato ricercato
dout <= crc_t;
ready <='1';
end if;
end if;
end process;
end rtl;
here the DFF
library IEEE;
use IEEE.std_logic_1164.all;
entity DFC is
port(
clk :in std_logic;
reset :in std_logic;
d :in std_logic;
crc :out std_logic;
q :out std_logic
);
end DFC;
architecture rtl of DFC is
begin
process(clk, reset, d)
begin
if(reset = '1')then
q <= '0';
crc<= '0';
elsif (clk'event and clk='1') then
q <= d;
crc <= d;
end if;
end process;
end rtl;
and finally here the testbench for the CRC
library IEEE;
use IEEE.std_logic_1164.all;
entity CRC_tb is
end CRC_tb;
architecture testbench of CRC_tb is
component CRC is
generic (
NBit : positive := 20; --da rimettere a 64
poly : positive := 8
);
port(
clk :in std_logic;
reset :in std_logic;
md :in std_logic; --1 per sender, 0 per receiver
input :in std_logic_vector(Nbit-1 downto 0);
dout_s :out std_logic_vector(Nbit-1 downto 0);
dout_r :out std_logic_vector(Nbit-poly-1 downto 0)
);
end component;
constant T_CLK :time := 25 ns;
constant T_sim :time := 2000 ns;
signal sim_time :std_logic :='1';
constant M :integer :=20; --da rimettere a 64, Nbit
constant N :integer :=8; -- poly
signal clk_tb :std_logic :='0';
signal reset_tb :std_logic :='1';
signal md_tb :std_logic;
signal input_tb :std_logic_vector(M-1 downto 0);
signal dout_s_tb :std_logic_vector(M-1 downto 0);
signal dout_r_tb :std_logic_vector(M-N-1 downto 0);
begin
clk_tb <= (not(clk_tb)and sim_time) after T_CLK/2;
sim_time <= '0' after T_sim;
DUT_i : CRC
generic map (
Nbit => M,
poly => N
)
port map (
clk => clk_tb,
reset => reset_tb,
md => md_tb,
input => input_tb,
dout_s => dout_s_tb,
dout_r => dout_r_tb
);
input_process : process(clk_tb)
begin
if(rising_edge(clk_tb)) then
md_tb <= '1';
input_tb <= "10100111010000000000";
end if;
end process input_process;
end testbench;
I was expecting that everything went fine given that the CRC code doesn't do much if not creating connections. I'm very new to VHDL so I can't understand very well what
** Error: (vsim-3733) C:/Modeltech_pe_edu_10.4a/CRC2.0/CRC.vhd(50):
No default binding for component instance 'LFSR_o'.
The following component generic is not on the entity: N Time: 0 ns
Iteration: 0 Instance: /crc_tb/DUT_i/LFSR_o File:
C:/Modeltech_pe_edu_10.4a/CRC2.0/LFSR.vhd
Loading work.dfc(rtl)
Error loading design
means.
Thanks all for the answers.
I am confused about sequential logic with components (I am new).
I have these components, but I get confused how to use them within a process. I need help understanding how sequential logic works with components, also I am unsure if my input/output vectors are correct. I'm having trouble with the inputs and outputs for the shift registers, like if x(0) <= sin is the right call.
I have to design this:
(https://i.stack.imgur.com/mwVdw.jpg)
This is my main file
use IEEE.STD_LOGIC_1164.ALL;
entity sa_top is
port(
x: in STD_LOGIC_VECTOR(7 downto 0);
y: in STD_LOGIC_VECTOR(7 downto 0);
clk: in STD_LOGIC;
rst: in STD_LOGIC;
s: out STD_LOGIC_VECTOR(7 downto 0)
);
end sa_top;
architecture Behavioral of sa_top is
-- shift register
component sr is
port(
sin: in STD_LOGIC;
sout: out STD_LOGIC;
clk: in STD_LOGIC;
rst: in STD_LOGIC
);
end component sr;
-- d flip/flop
component dff is
port(
d: in STD_LOGIC;
q: in STD_LOGIC;
clk: in STD_LOGIC;
rst: in STD_LOGIC
);
end component dff;
-- full adder
component fa is
port(
a: in STD_LOGIC;
b: in STD_LOGIC;
cin: in STD_LOGIC;
sum: out STD_LOGIC;
cout: out STD_LOGIC
);
end component fa;
signal xi, yi, si: std_logic;
signal xo, yo, so: std_logic;
signal s_temp: std_logic;
signal carry: std_logic;
begin
xi <= x(0);
yi <= y(0);
inp_x_instance: sr port map(sin => xi, sout => xo, clk => clk, rst => rst);
inp_y_instance: sr port map(sin => yi, sout => yo, clk => clk, rst => rst);
adder_instace: fa port map(a => xo, b=> yo, cin => carry, sum => si, cout => carry);
op_s_instance: sr port map(sin => si, sout => so, clk => clk, rst => rst);
--df_instance: dff port map(d => s_temp, q => s_temp, clk => clk, rst => rst);
process(clk, s_temp) is
begin
if rst = '1' then
s <= (others=>'0');
elsif rising_edge(clk) then
s(0) <= so;
end if;
end process;
end Behavioral;```
I think what you are doing is correct, the only thing that you maybe got confused about is the actual shifting itself, you need to shift it the bits by 1 to the left into the other bits. Which can look something like this:
s(7 downto 1) <= s(6 downto 0);
So the final piece of code should look like this: (note that I have removed s_temp from the sensitivity list of the process)
process(clk) is
begin
if rst = '1' then
s <= (others=>'0');
elsif rising_edge(clk) then
s(7 downto 1) <= s(6 downto 0);
s(0) <= so;
end if;
end process;
My VHDL-Code is functionaly correct, in simulation it does what it's thought for. I tested in many variations and the code works correct.
But when i program the fpga (Nexyx 4 ddr) everything works well except the preload of the counter.
I don't know if the load enable (load_e) output from the fsm doesn't reach the counter or if the output signal that sais the counter is loaded (counter_loaded) doesn't reach the fsm but when i program the fpga it never pases from state C or D (waiting for counter loaded) to state E or F (where it makes a countdown).
I tested the other parts of the code in the target and it works properly, so the only problema so far is that one and i can't find the error, i'm thinking about timming, but i have no idea of how to solve it.
I leave here the counter and fsm code, as well as the TOP code, i`m new in VHDL and it might be lots of bad practice mistakes.
I'm spanish, that's the reason of my bad English and also the spanish name of some signal, but i add a comment next to them.
--------COUNTER---------------------------------------
entity counter is
Generic (NBITS : positive := 15
);
Port (clk : in STD_LOGIC;
rst : in STD_LOGIC;
ce : in STD_LOGIC;
load : in STD_LOGIC_VECTOR (NBITS-1 downto 0);
load_e : in STD_LOGIC;
unit : out STD_LOGIC_VECTOR(3 downto 0);
dec : out STD_LOGIC_VECTOR(3 downto 0);
zero_n : out STD_LOGIC; --true si cuenta = 0
loaded : out STD_LOGIC);
end counter;
architecture Behavioral of counter is
signal q_i : unsigned (NBITS-1 downto 0) := (others => '1');
begin
process(clk,rst)
begin
if rst = '1' then
q_i <= (OTHERS => '1');
loaded <= '0';
elsif rising_edge(clk) then
if CE = '1' then
if load_e = '1' then --ONE OF MY GUESSES OF THE PROBLEM
q_i <= unsigned(load);
loaded <= '1';
else
q_i <= q_i - 1;
loaded <= '0';
end if;
end if;
end if;
end process;
dec <= std_logic_vector(to_unsigned((to_integer(q_i(14 downto 10)) / 10),dec'length)); --first 5 bits are the tens
unit <= std_logic_vector(to_unsigned((to_integer(q_i(14 downto 10)) rem 10),unit'length)); --fist 5 bits are the unit
zero_n <= '1' WHEN q_i < "000010000000000" ELSE '0'; --cout is zero if the first 5 bits are less tan 1 in binary
end Behavioral;
------FINITE STATE MACHINE--------------------------------
entity maquina_estados is
Port (
clk : in STD_LOGIC;
rst : in STD_LOGIC;
corto : in STD_LOGIC;
largo : in STD_LOGIC;
b_on : in STD_LOGIC;
zero_n : in STD_LOGIC;
counter_loaded : in STD_LOGIC;
load_e : out STD_LOGIC;
load : out STD_LOGIC_VECTOR(14 downto 0);
bomba_led : out STD_LOGIC;
indica_on : out STD_LOGIC);
end maquina_estados;
architecture Behavioral of maquina_estados is
type state_type is (A, B, C, D, E, F); --define state(A = powered off, B = powered on, C = short coffee preload, D = large coffee preload, E = short coffee, F = large coffee)
signal state, next_state : state_type; --type state signal
begin
process(clk,rst)
begin
if rst = '1' then
state <= A;
elsif rising_edge(clk) then
state <= next_state;
end if;
end process;
process(state, b_on, corto, largo, zero_n, counter_loaded)
begin
CASE state IS
WHEN A => if b_on = '1' then
next_state <= B;
else
next_state <= A;
end if;
WHEN B => if b_on = '0' then
next_state <= A;
elsif corto = '1' then
next_state <= C;
elsif largo = '1' then
next_state <= D;
else
next_state <= B;
end if;
WHEN C => if counter_loaded = '1' then
next_state <= E;
else
next_state <= C;
end if;
WHEN D => if counter_loaded = '1' then
next_state <= F;
else
next_state <= D;
end if;
WHEN E => if zero_n = '1' then
next_state <= B;
else
next_state <= E;
end if;
WHEN F => if zero_n = '1' then
next_state <= B;
else
next_state <= F;
end if;
WHEN OTHERS => next_state <= A;
end case;
end process;
process(state)
begin
CASE state IS
WHEN A => load <= "111111111111111"; --default value of the count
load_e <= '0';
bomba_led <= '0';
indica_on <= '0';
WHEN B => load <= "111111111111111";
load_e <= '0';
bomba_led <= '0';
indica_on <= '1';
WHEN C => load <= "010101111111111"; --10 second, this in addition to a 1024 hz clock made posible to use the first 5 bits as the number
load_e <= '1';
bomba_led <= '0';
indica_on <= '1';
WHEN D => load <= "101001111111111"; --20 seconds
load_e <= '1';
bomba_led <= '0';
indica_on <= '1';
WHEN E => load <= "111111111111111";
load_e <= '0';
bomba_led <= '1';
indica_on <= '1';
WHEN F => load <= "111111111111111";
load_e <= '0';
bomba_led <= '1';
indica_on <= '1';
end case;
end process;
end behavioral;
------TOP-----------------------
entity TOP is
Generic(
FIN : positive := 100000000;
FOUT : positive := 1024);
Port ( clk : in STD_LOGIC;
rst : in STD_LOGIC;
corto : in STD_LOGIC;
largo : in STD_LOGIC;
b_on : in STD_LOGIC;
display_number : out STD_LOGIC_VECTOR (6 downto 0);
display_selection : out STD_LOGIC_VECTOR (7 downto 0);
bomba_led : out STD_LOGIC;
indica_on : out STD_LOGIC);
end TOP;
architecture Behavioral of TOP is
--instancies
component clk_divider is
-- Port ( );
generic(
FIN : positive;
FOUT : positive
);
port (
Clk : in STD_LOGIC;
Reset : in STD_LOGIC;
Clk_out : out STD_LOGIC
);
end component;
component maquina_estados is
Port ( clk : in STD_LOGIC;
rst : in STD_LOGIC;
corto : in STD_LOGIC;
largo : in STD_LOGIC;
b_on : in STD_LOGIC;
zero_n : in STD_LOGIC;
counter_loaded : in STD_LOGIC;
load_e : out STD_LOGIC;
load : out STD_LOGIC_VECTOR(14 downto 0);
bomba_led : out STD_LOGIC;
indica_on : out STD_LOGIC);
end component;
component counter is
Generic (NBITS : positive
);
Port (clk : in STD_LOGIC;
rst : in STD_LOGIC;
ce : in STD_LOGIC;
load : in STD_LOGIC_VECTOR (NBITS-1 downto 0);
load_e : in STD_LOGIC;
unit : out STD_LOGIC_VECTOR(3 downto 0);
dec : out STD_LOGIC_VECTOR(3 downto 0);
zero_n : out STD_LOGIC;
loaded : out STD_LOGIC);
end component;
component clk_manager is
generic(
CLK_FREQ : positive
);
Port (
clk : in STD_LOGIC;
rst : in STD_LOGIC;
strobe_1024Hz : out STD_LOGIC;
strobe_128Hz : out STD_LOGIC
);
end component;
component decoder is
Port ( code : in STD_LOGIC_VECTOR(3 downto 0);
led : out STD_LOGIC_vector(6 downto 0)
);
end component;
component display_refresh is
Port ( clk : in STD_LOGIC;
ce : in STD_LOGIC;
segment_unit : in STD_LOGIC_VECTOR (6 downto 0);
segment_dec : in STD_LOGIC_VECTOR (6 downto 0);
display_number : out STD_LOGIC_VECTOR (6 downto 0);
display_selection : out STD_LOGIC_VECTOR (1 downto 0)); --cada elemento del vector corresponde a un 7 seg, true se ve false no
end component;
-- prescaler signals
signal prescaler_clk_out : STD_LOGIC;
--maquina estados signals
signal zero_n_fsm : STD_LOGIC;
signal load_e_fsm : STD_LOGIC;
signal load_fsm : STD_LOGIC_VECTOR(14 downto 0);
signal bomba_led_fsm: STD_LOGIC;
--counter signals
signal unit : STD_LOGIC_VECTOR(3 downto 0);
signal dec : STD_LOGIC_VECTOR(3 downto 0);
signal zero_n_cntr : STD_LOGIC;
signal load_e_cntr : STD_LOGIC;
signal load_cntr : STD_LOGIC_VECTOR(14 downto 0);
signal counter_loaded : STD_LOGIC;
--clk_manager signals
signal strobe_1024Hz : STD_LOGIC;
signal strobe_128Hz : STD_LOGIC;
signal ce_clkm : STD_LOGIC;
signal rst_clkm : STD_LOGIC;
--decoders signals
signal unit_code : STD_LOGIC_VECTOR(6 downto 0);
signal dec_code : STD_LOGIC_VECTOR(6 downto 0);
--display refresh signals
signal display_refresh_number : STD_LOGIC_VECTOR(6 downto 0);
signal display_refresh_selection : STD_LOGIC_VECTOR(1 downto 0);
begin
prescaler: clk_divider
generic map(
FIN => FIN,
FOUT => FOUT
)
port map(
Clk => clk,
Reset => rst,
Clk_out => prescaler_clk_out
);
sm: maquina_estados
Port map( clk => prescaler_clk_out,
rst => rst,
corto => corto,
largo => largo,
b_on => b_on,
zero_n => zero_n_fsm,
counter_loaded => counter_loaded,
load_e => load_e_fsm,
load => load_fsm,
bomba_led => bomba_led_fsm,
indica_on => indica_on);
cntr: counter
Generic map(NBITS => 15
)
Port map(clk => clk,
rst => rst,
ce => strobe_1024Hz,
load => load_cntr,
load_e => load_e_fsm,
unit => unit,
dec => dec,
zero_n => zero_n_cntr,
loaded => counter_loaded);
clk_m: clk_manager
generic map(
CLK_FREQ => FIN
)
Port map(
clk => clk,
rst => rst,
strobe_1024Hz => strobe_1024Hz,
strobe_128Hz => strobe_128Hz
);
unit_dcd: decoder
Port map(
code => unit,
led => unit_code
);
dec_dcd: decoder
Port map(
code => dec,
led => dec_code
);
dr: display_refresh
Port map(
clk => clk,
ce => strobe_128Hz,
segment_unit => unit_code,
segment_dec => dec_code,
display_number => display_refresh_number,
display_selection => display_refresh_selection);
display_number <= display_refresh_number WHEN bomba_led_fsm = '1' ELSE "1111111";
display_selection <= ("111111" & display_refresh_selection) WHEN bomba_led_fsm = '1' ELSE "11111111";
zero_n_fsm <= zero_n_cntr;
bomba_led <= bomba_led_fsm;
load_cntr <= load_fsm;
end Behavioral;
Here are all the reports that the implementation ans sythesis gave me:
Synthesis reports
implementation reports 1/6
implementation reports 2/6
implementation reports 3/6
implementation reports 4/6
implementation reports 5/6
implementation reports 6/6
I hope someone could find the problema and give me a solution or a way of how to debug this problem.
Thanks.
Your FSM is clocked on prescaler_clk_out, and your counter is clocked on clk, which is a red flag. This could easily lead to an implementation failure.
Draw a timing diagram showing all your clocks and resets, and your lower-frequency enables (in particular, strobe_1024Hz)
Try to clock all the logic on the same clock, presumably clk, and make sure that everything is synchronous to this clock (in other words, inputs have sufficient setup and hold times relative to this clock)
Make sure you are actually resetting the chip
Once you've done the timing diagram, write a constraints file that tells the synthesiser what your clocks are. clk_manager and clk_divider may be an issue here, but hopefully everything will be clocked on just 'clk', and the contstraints file will contain only the clock name and frequency. If you still can't get it to work, ask a new question, showing your timing diagram, and your attempt at a constraints file.
I am trying to do a circuit that get the MOD of two numbers, my problem is in the output of that circuit, the output shows 0 and then the correct value of MOD, i have to integrate this circuito to another circuit to do a circuito that shows if a number is prime or not. When i integrate the MOD circuit to the PRIME number circuit, the output of the PRIME circuit goes always wrong because the situation above, the output of the MOD circuit shows always two values. The output form below:
In this image the circuit get always 0 and 3 because my state machine stays in loop, this is not the problem, the problem is: i need that output stay always with the value of the MOD and no zeros. In this example should stay always 3.
My control code.
LIBRARY IEEE;
USE IEEE.std_logic_1164.ALL;
USE IEEE.std_logic_unsigned.ALL;
use IEEE.std_logic_arith.ALL;
ENTITY operacao_modulo_control IS
PORT (
i_CLK : IN STD_ULOGIC;
i_RST : IN STD_LOGIC;
i_S : IN INTEGER;
i_COMP : IN STD_LOGIC;
o_LD_CLR : OUT STD_LOGIC;
o_LD_K : OUT STD_LOGIC;
o_DOUT : OUT INTEGER
);
END operacao_modulo_control;
ARCHITECTURE arch_1 OF operacao_modulo_control IS
TYPE state_type IS (s0, s1, s2,s3);
SIGNAL stateT : state_type;
BEGIN
PROCESS(i_CLK)
BEGIN
IF rising_edge(i_CLK) THEN
IF (i_RST = '1') THEN
stateT <= s0;
ELSE
CASE stateT IS
when s0 => stateT <= s1;
when s1 => if (i_COMP = '1') then
stateT <= s2;
else
stateT <= s3;
end if;
when s2 => stateT <= s1;
when s3 => stateT <= s0;
END CASE;
END IF;
END IF;
END PROCESS;
o_LD_CLR <= '1' WHEN (stateT = s0) ELSE '0';
o_LD_K <= '1' WHEN (stateT = s2) ELSE '0';
o_DOUT <= i_S WHEN (stateT = s3) ELSE 0;
END arch_1;
How can i change this to make the output shows only the correct value of the MOD and no zeros?
If the other codes is necessary, they are below:
Datapath:
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
USE ieee.numeric_std.ALL;
entity operacao_modulo_datapath is
port (
i_RESET : IN STD_LOGIC; -- Sinal para resetar a maquina de estados do contador
i_CLOCK : IN STD_ULOGIC; -- Clock
i_LOAD_K : IN STD_LOGIC; -- Sinal para carregar K
i_LOAD_CLEAR : IN STD_LOGIC; -- Sinal para limpar K
i_A : IN UNSIGNED (7 downto 0); -- Entrada A
i_B : IN UNSIGNED (7 downto 0); -- Entrada B
o_OUTS : OUT INTEGER; -- Saida da operação aritmética
o_COMP : OUT STD_LOGIC -- Saída do comparador
);
end operacao_modulo_datapath;
architecture arch_1 of operacao_modulo_datapath is
component tot is
port (
i_RST : IN STD_LOGIC;
i_CLR : IN STD_LOGIC; -- Sinal vindo do controle para limpar o valor de K
i_CLK : IN STD_ULOGIC; -- Sinal de clock para sincronizar com o controle
i_DINL : IN STD_LOGIC ; -- Sinal de load para carregar K
i_DINK : IN INTEGER; -- Valor antigo de K
o_DOUTK : OUT INTEGER
); -- data output
end component;
component array_multiplier is
port (
i_MULTIPLICAND : in unsigned(7 downto 0); -- data input
i_MULTIPLIER : in integer; -- data input
o_DOUT : out std_logic_vector(15 downto 0)); -- data output
end component;
component alu_mod is
port (
i_DINA : IN UNSIGNED (7 downto 0); -- Entrada A
i_DINM : IN STD_LOGIC_VECTOR(15 downto 0); -- entrada do multiplicador
o_DOUTS : OUT INTEGER -- saída S da operação de mod
);
end component;
component comparator is
port (
i_DINS : IN INTEGER; -- Entrada S (saída S da alu_mod)
i_DINB : IN UNSIGNED (7 downto 0); -- Entrada B (entrada do usuário)
o_DOUT : OUT STD_LOGIC); -- Saída para informar o resultado da comparação
end component;
signal w_OUT : STD_LOGIC;
signal w_Ko : INTEGER;
signal w_Ki : INTEGER;
signal w_OUT0 : INTEGER;
signal w_OUT1 : std_logic_vector(15 downto 0);
begin
u_0: tot port map (
i_RST => i_RESET,
i_CLR => i_LOAD_CLEAR,
i_CLK => i_CLOCK,
i_DINL => i_LOAD_K,
i_DINK => w_Ko,
o_DOUTK => w_Ko
);
u_1: array_multiplier port map (
i_MULTIPLICAND => i_B,
i_MULTIPLIER => w_Ko,
o_DOUT => w_OUT1
);
u_2: alu_mod port map (
i_DINA => i_A,
i_DINM => w_OUT1,
o_DOUTS => w_OUT0
);
u_3: comparator port map (
i_DINS => w_OUT0,
i_DINB => i_B,
o_DOUT => w_OUT
);
o_OUTS <= w_OUT0;
o_COMP <= w_OUT;
end arch_1;
Top level entity:
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
USE ieee.numeric_std.ALL;
entity operacao_modulo_top is
port (
i_RESETAR : IN STD_LOGIC; -- Sinal para resetar a maquina de estados do contador
i_CLCK : IN STD_ULOGIC; -- Clock
i_INA : IN UNSIGNED (7 downto 0); -- Entrada A
i_INB : IN UNSIGNED (7 downto 0); -- Entrada B
o_SOMA : OUT INTEGER -- Saida da operação aritmética
);
end operacao_modulo_top;
architecture arch_1 of operacao_modulo_top is
component operacao_modulo_datapath is
port(
i_RESET : IN STD_LOGIC; -- Sinal para resetar a maquina de estados do contador
i_CLOCK : IN STD_ULOGIC; -- Clock
i_LOAD_K : IN STD_LOGIC; -- Sinal para carregar K
i_LOAD_CLEAR : IN STD_LOGIC; -- Sinal para limpar K
i_A : IN UNSIGNED (7 downto 0); -- Entrada A
i_B : IN UNSIGNED (7 downto 0); -- Entrada B
o_OUTS : OUT INTEGER; -- Saida da operação aritmética
o_COMP : OUT STD_LOGIC -- Saída do comparador
);
end component;
component operacao_modulo_control is
port (
i_CLK : IN STD_ULOGIC;
i_RST : IN STD_LOGIC;
i_S : IN INTEGER;
i_COMP : IN STD_LOGIC;
o_LD_CLR : OUT STD_LOGIC;
o_LD_K : OUT STD_LOGIC;
o_DOUT : OUT INTEGER
);
end component;
signal w_OUT0 : INTEGER;
signal w_OUT3 : INTEGER;
signal w_OUT1 : STD_LOGIC;
signal w_OUT2 : STD_LOGIC;
signal w_OUT : STD_LOGIC;
begin
u_0: operacao_modulo_datapath port map (
i_RESET => i_RESETAR,
i_CLOCK => i_CLCK,
i_LOAD_K => w_OUT2,
i_LOAD_CLEAR => w_OUT1,
i_A => i_INA,
i_B => i_INB,
o_OUTS => w_OUT0,
o_COMP => w_OUT
);
u_1: operacao_modulo_control port map (
i_CLK => i_CLCK,
i_RST => i_RESETAR,
i_S => w_OUT0,
i_COMP => w_OUT,
o_LD_CLR => w_OUT1,
o_LD_K => w_OUT2,
o_DOUT => w_OUT3
);
o_SOMA <= w_OUT3;
end arch_1;
Your problem is this line:
o_DOUT <= i_S WHEN (stateT = s3) ELSE 0;
stateT only equals s3 one clock in three, hence you output. If you don't want the zeros, you need to store the value of i_S in some flip-flops, which are updated when stateT = s3:
PROCESS(i_CLK)
BEGIN
IF rising_edge(i_CLK) THEN
IF i_RST = '1' THEN
o_DOUT <= 0;
ELSIF stateT = s3 THEN
o_DOUT <= i_S;
END IF;
END IF;
END PROCESS;
http://www.edaplayground.com/x/25QN
My waveform does not change:
I am working on my 32-bit comparator project. I already have an 1 bit one. I do not know where is the issue. Anyone can help me find that?
Thanks so much
Code:
1bit:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
ENTITY comp1 is
port (a : IN std_logic ;
b : IN std_logic ;
g : IN std_logic ;
l : IN std_logic ;
e : IN std_logic ;
great : OUT std_logic ;
less : OUT std_logic ;
equal : OUT std_logic );
END ;
ARCHITECTURE comp1_arch OF comp1 IS
signal s1,s2,s3: std_logic;
begin
s1 <= (a and (not b));
s2 <= (not ((a and (not b)) or (b and (not a))));
s3 <= (b and (not a));
equal <= (e and s2) after 30 ns;
great <= (g or(e and s1)) after 27 ns;
less <= (l or(e and s3)) after 27 ns;
end comp1_arch;
32 bit:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
ENTITY comp32 is
GENERIC (BW : INTEGER :=32);
PORT ( a_32 : IN STD_LOGIC_VECTOR (BW -1 DOWNTO 0);
b_32 : IN STD_LOGIC_VECTOR (BW -1 DOWNTO 0);
g_32 : OUT STD_LOGIC ;
l_32 : OUT STD_LOGIC ;
e_32 : OUT STD_LOGIC );
END comp32;
ARCHITECTURE comp32_arch OF comp32 IS
COMPONENT comp1
PORT (a,b,g,l,e : IN std_logic ;
great,less,equal : OUT std_logic);
END COMPONENT comp1;
signal gre : std_logic_vector(BW downto 0);
signal les : std_logic_vector(BW downto 0);
signal equ : std_logic_vector(BW downto 0);
begin
gre(0)<='0';les(0)<='0';equ(0)<='0';
gen: for i in 0 to BW-1 generate
biti: comp1 port map( a => a_32(i),b => b_32(i), g => gre(i), l => les(i), e =>equ(i),
great => gre(i+1), less => les(i+1), equal => equ(i+1));
end generate;
g_32 <= gre(BW-1);
l_32 <= les(BW-1);
e_32 <= equ(BW-1);
end comp32_arch;
Test Bench:
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
ENTITY comp32_TB IS
END comp32_TB;
ARCHITECTURE behavior OF comp32_TB IS
COMPONENT comp32
PORT(
a_32 : IN std_logic_vector(31 downto 0);
b_32 : IN std_logic_vector(31 downto 0);
g_32 : OUT std_logic;
l_32 : OUT std_logic;
e_32 : OUT std_logic
);
END COMPONENT;
signal a_32 : std_logic_vector(31 downto 0) := (others => '0');
signal b_32 : std_logic_vector(31 downto 0) := (others => '0');
signal g_32 : std_logic;
signal l_32 : std_logic;
signal e_32 : std_logic;
BEGIN
uut: comp32 PORT MAP (
a_32 => a_32,
b_32 => b_32,
g_32 => g_32,
l_32 => l_32,
e_32 => e_32
);
stim_proc: process
begin
a_32 <="00000000000000000000000000000000";b_32<="00000000000000000000000000000000";wait for 1500 ns;
a_32 <="00000000000000000000000000000001";b_32<="00000000000000000000000000000000";wait for 1500 ns;
a_32 <="00000000000000000000000000000000";b_32<="10000000000000000000000000000000";wait for 1500 ns;
wait;
end process;
END;
You had your chained signals backward, and the first inputs want to show equal:
architecture comp32_arch of comp32 is
component comp1
port (a,b,g,l,e : in std_logic ;
great,less,equal : out std_logic);
end component comp1;
signal gre : std_logic_vector(BW downto 0);
signal les : std_logic_vector(BW downto 0);
signal equ : std_logic_vector(BW downto 0);
begin
gre(BW) <= '0'; -- gre(0) <= '0';
les(BW) <= '0'; -- les(0) <= '0';
equ(BW) <= '1'; -- equ(0) <= '0';
gen:
for i in 0 to BW-1 generate
biti:
comp1
port map (
a => a_32(i),
b => b_32(i),
g => gre(i+1), -- gre(i),
l => les(i+1), -- les(i),
e => equ(i+1), -- equ(i),
great => gre(i), -- gre(i+1),
less => les(i), -- les(i+1),
equal => equ(i) -- equ(i+1)
);
end generate;
g_32 <= gre(0); -- gre(BW);-- (BW-1);
l_32 <= les(0); -- les(BW); -- (BW-1);
e_32 <= equ(0); -- equ(BW); -- (BW-1);
end architecture comp32_arch;
And that gives:
The most significant bit without an equals defines either less than or greater than. If they're all equal that propagates all the way through.