S
snake368
I want to do the following operation using VHDL. The INPUT and OUTPUT are 14-bit signals and I have to divide INPUT by a constant, in this case 132, which is in decimal form.
Here's my code:
PROCESS (clk)
VARIABLE d, q: INTEGER;
BEGIN
IF (clk'EVENT AND clk = '1') THEN
CASE state IS
WHEN min_132000 =>
d := CONV_INTEGER (a);
q := 0;
IF (d >= 132000) THEN
d := d - 132000;
q := q + 1000;
END IF;
IF (d >= 13200) THEN
state <= min_13200;
ELSIF (d >= 1320) THEN
state <= min_1320;
ELSIF (d >= 132) THEN
state <= min_132;
ELSE
state <= to_dac;
END IF;
WHEN min_13200 =>
IF (d >= 13200) THEN
d := d - 13200;
q := q + 100;
END IF;
IF (d < 13200) THEN
IF (d >= 1320) THEN
state <= min_1320;
ELSIF (d >= 132) THEN
state <= min_132;
ELSE
state <= to_dac;
END IF;
END IF;
WHEN min_1320 =>
IF (d >= 1320) THEN
d := d - 1320;
q := q + 10;
END IF;
IF (d < 1320) THEN
IF (d >= 132) THEN
state <= min_132;
ELSE
state <= to_dac;
END IF;
END IF;
WHEN min_132 =>
IF (d >= 132) THEN
d := d - 132;
q := q + 1;
END IF;
IF (d < 132) THEN
state <= end;
b <= CONV_STD_LOGIC_VECTOR ((q), 14);
END IF;
WHEN end =>
state <= end;
END CASE;
As you see it's a FSM and takes several rising edges to complete. Is there anyway to do it easier? Or maybe in a single rising edge?
Here's my code:
PROCESS (clk)
VARIABLE d, q: INTEGER;
BEGIN
IF (clk'EVENT AND clk = '1') THEN
CASE state IS
WHEN min_132000 =>
d := CONV_INTEGER (a);
q := 0;
IF (d >= 132000) THEN
d := d - 132000;
q := q + 1000;
END IF;
IF (d >= 13200) THEN
state <= min_13200;
ELSIF (d >= 1320) THEN
state <= min_1320;
ELSIF (d >= 132) THEN
state <= min_132;
ELSE
state <= to_dac;
END IF;
WHEN min_13200 =>
IF (d >= 13200) THEN
d := d - 13200;
q := q + 100;
END IF;
IF (d < 13200) THEN
IF (d >= 1320) THEN
state <= min_1320;
ELSIF (d >= 132) THEN
state <= min_132;
ELSE
state <= to_dac;
END IF;
END IF;
WHEN min_1320 =>
IF (d >= 1320) THEN
d := d - 1320;
q := q + 10;
END IF;
IF (d < 1320) THEN
IF (d >= 132) THEN
state <= min_132;
ELSE
state <= to_dac;
END IF;
END IF;
WHEN min_132 =>
IF (d >= 132) THEN
d := d - 132;
q := q + 1;
END IF;
IF (d < 132) THEN
state <= end;
b <= CONV_STD_LOGIC_VECTOR ((q), 14);
END IF;
WHEN end =>
state <= end;
END CASE;
As you see it's a FSM and takes several rising edges to complete. Is there anyway to do it easier? Or maybe in a single rising edge?