MSAKARIM
Full Member level 3
- Joined
- Jun 2, 2015
- Messages
- 154
- Helped
- 1
- Reputation
- 2
- Reaction score
- 4
- Trophy points
- 1,298
- Activity points
- 2,528
Your not_found state has no exit
Your not_found state has no exit
Too coarse time scale. I guess there are multiple state transitions not visible in the wave. Zoom in to the first clocks after reset deassertion.
Based on the waveforms you are not accounting for the delay to read the RAM. You should be updating ram_out after you've done the compare.Here, the used Target value is the first element in the ram and it gives me a wrong location (First calculation of M and stops at it, it should repeat until it reaches the exact location), also I observe some unsynchronization
I can't catch where is the error exactly inside the code.
Well not according to the code snippet below. The comparison is done in the Compare state and the values from the waveforms in post #41 show that data_comp and Target are identical when in the Compare state.Using Ram_out instead of data_comp in compare state should fix the timing problem.
Code VHDL - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 When Compare => if(data_comp < Target) then -- this checks for data_comp < Target R<=R; L<= M+1; State <= Start; -- then go to Start state elsif (data_comp > Target) then -- now check for data_comp > Target L<=L; R<= M-1; State <= Start; -- then go to Start state else -- the default of data_comp = Target Location <=M; -- Gee, you don't specify the state to go to....of course you never leave the Compare state end if;
architecture Behavioral of BS_SM is
-------BRAM-------
component BRAM_1 is
Port ( clk,we,en: in STD_LOGIC;
addr : in STD_LOGIC_VECTOR (11 downto 0);
din : in STD_LOGIC_VECTOR (31 downto 0);
dout : out STD_LOGIC_VECTOR (31 downto 0));
end component;
signal en: std_logic;
signal Ram_Out,data_comp :STD_LOGIC_VECTOR (31 downto 0);
signal M_addr:STD_LOGIC_VECTOR (11 downto 0);
------------------
------States------------
TYPE State_type IS (Start,Compare_LR,Not_Found,Wait_memory,Read_Memory,M_calc,Compare, Found);
SIGNAL State : State_Type;
signal M: INTEGEr;
signal L: INTEGER RANGE 0 TO 3871 :=0;
signal R: INTEGER RANGE 0 TO 3871 :=3871;
begin
BRAM: BRAM_1 port map (clk,en,'0',M_addr,Target,Ram_out);
process (clk,rst,State)
begin
if (rst='1') then
State <= Start;
elsif (clk'event and clk ='1') then
case State is
when Start =>
L<=0;
R<= 3871;
State <= Compare_LR;
when Compare_LR =>
if (L<= R) then
State <= M_calc;
else
State <= Not_Found;
end If;
When Not_Found =>
Location <=-1;
Match <= '0';
Report "No Matching Found" ;
State <= Start;
When M_calc =>
M<= integer (L + R)/2;
State <= Read_memory;
when Read_Memory =>
M_addr <=std_logic_vector(to_unsigned(M,12));
en <= '1';
state <= Wait_Memory;
when Wait_memory =>
data_comp <= Ram_out;
State <= Compare;
When Compare =>
if(data_comp < Target) then
R<=R;
L<= M+1;
State <= Compare_LR;
elsif (data_comp > Target) then
L<=L;
R<= M-1;
State <= Compare_LR;
else
State <= Found;
end if;
when Found =>
Match <= '1';
Location <=M;
Report "Matching Found";
State <= Start;
end case;
end if;
end process;
end Behavioral;
You need to put all the signals assigned in the FSM into the reset branch of the if, otherwise you will get synthesis warnings and possible synthesis and simulation mismatches.
if (rst='1') then
L<=0;
R<= 3871;
Location <=-1;
Match <= '0';
en <= '0';
M_addr <= (others => '0');
M<= 0;
data_comp <= (others => '0');
State <= Start;
I used two templates of RAMs alternatively:I don't think you have ever posted your RAM code that would have the initialization code. All you keep posting is out of context code snippets.
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
USE IEEE.STD_LOGIC_TEXTIO.ALL;
USE STD.TEXTIO.ALL;
entity BRAM_1 is
Port ( clk,en,we: in STD_LOGIC;
addr : in STD_LOGIC_VECTOR (11 downto 0);
din : in STD_LOGIC_VECTOR (31 downto 0);
dout : out STD_LOGIC_VECTOR (31 downto 0));
end BRAM_1;
architecture Behavioral of BRAM_1 is
type TRam is array(0 to 3871) of std_logic_vector(31 downto 0);
impure function init_bram (ram_file_name : in string) return TRam is
file ramfile : text ;
variable line_read : line;
variable ram_to_return : TRam;
begin
file_open(ramfile,"F:\RAM_0.txt",READ_MODE);
--while not endfile(ramfile) loop
for i in TRam'range loop
readline(ramfile, line_read);
hread(line_read, ram_to_return(i));
end loop;
--end loop;
return ram_to_return;
end function;
signal Ram : TRam := init_bram("F:\RAM_0.txt");
signal read_addr : std_logic_vector(11 downto 0);
begin
process (clk)
begin
if (clk'event and clk = '1') then
if (en ='1') then
if (we = '1') then
RAM(conv_integer(addr)) <= din;
end if;
read_addr <= addr;
end if;
end if;
end process;
dout <= RAM(conv_integer(read_addr));
end Behavioral;
Yes, it is sorted ascending.Hello @MSAKARIM,
one question: Do you have data in RAM sorted ascending? This is condition needed to "Binary Search" algoritm work properly.
Best Regards
U usually means you don't have something hooked up correctly and nothing is driving the signal at all from the start of simulation. You might want to verify that the BRAM_1 instance is connected correctly.
Also have you verified there is something in the Ram signal? Have you brought that signal up in the simulation to make sure it gets loaded at the start of simulation?
With the bus being U it is likely a missing connection, probably a typo.
I tested it lonely it works well, but inside the overall RTL it doesn't.Well then you have to debug why your RAM read fails. You have to check if your RTL read logic is working correctly or not.
I would suggest to write a simple test bench in which you enable the ram and then using a count-up counter, feed the incrementing read_addr to the ram and check if you get the expected data out.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?