What you want to do is called a Numerically Controlled "Oscillator", or NCO. It works like this...
Create a counter that can increment by values other than 1. The inputs to this counter are the master clock, and a value to count by (din). For each clock edge, count <= count + din. The number of bits in din is the same as the number of bits in the counter. The actual count value can be used for many useful things, but what you want to do is super simple.
You want to detect every time the counter rolls over, and output a pulse to your motor when that happens. Do this by taking the most significant bit of the counter and running it through a single flip-flop to delay it by one clock. Now you have two signals that I'll call MSB, and MSB_Previous. You know if the counter has rolled over because MSB=0 and MSB_Prev=1. When that condition is true, send a pulse to the motor.
To set the pulse frequency, the formula is this: pulse_rate = main_clk_freq * inc_value/2^n_bits
Where inc_value is the value that the counter is being incremented by and n_bits is the number of bits in the counter.
An important thing to note is that adding bits to the counter does not change the range of the output frequency-- that is always 0 Hz to half of main_clk_freq. But it does change the accuracy that you can generate the desired frequency. Odds are high that you won't need 32-bits for this counter, and that maybe just 10 to 16 bits will be enough.
This method of generating pulses is nice because it is super easy, the logic is small and fast, and it can often generate frequencies more accurately and with better flexibility than the type of counter+comparator design that you have in your question.
The reason why the logic is smaller is not only because you can get by with a smaller counter, but you do not have to compare the entire output of the counter. You only need the top bit. Also, comparing two large numbers in an FPGA usually requires a lot of LUTs. Comparing two 32-bit numbers would require 21 4-Input LUTs and 3 logic levels, where as the NCO design requires 1 LUT, 2 Flip-Flops, and only 1 logic level. (I'm ignoring the counter, since it is basically the same for both designs.) The NCO approach is much smaller, much faster, much simpler, and yields better results.
Update: An alternative approach to making the rollover detector is to simply send out the MSB of the counter to the motor. If you do this, the signal going to the motor will always be a 50/50 duty cycle. Choosing the best approach depends on what kind of pulse your motor needs.
Update: Here is a VHDL code snippet for doing the NCO.
signal count :std_logic_vector (15 downto 0) := (others=>'0);
signal inc :std_logic_vector (15 downto 0) := (others=>'0);
signal pulse :std_logic := '0';
. . .
process (clk)
begin
if rising_edge(clk) then
count <= count + inc;
end if;
end process;
pulse <= count(count'high);
You could use a procedure with an out parameter. The procedure can be declared in a package and called with different parameters for different 'clock' signals.
Here is an example of a clock_gen procedure from VHDL-extras:
subtype duty_cycle is real range 0.0 to 1.0;
procedure clock_gen( signal Clock : out std_ulogic; signal Stop_clock : in boolean;
constant Clock_period : in delay_length; constant Duty : duty_cycle := 0.5 ) is
constant HIGH_TIME : delay_length := Clock_period * Duty;
constant LOW_TIME : delay_length := Clock_period - HIGH_TIME;
begin
Clock <= '0';
while not Stop_clock loop
wait for LOW_TIME;
Clock <= '1';
wait for HIGH_TIME;
Clock <= '0';
end loop;
end procedure;
The constant input period can be exchanged with a variable for simulation environments.
Usage example:
architecture rtl of my_entity is
signal SimStop : boolean := false;
signal my_clock1 : std_ulogic;
signal my_clock2 : std_ulogic;
begin
clock_gen(my_clock1, SimStop, 10.000 ns);
clock_gen(my_clock2, SimStop, 6.666 ns);
-- ...
process
begin
--
-- some stimuli
--
wait for 10 ns;
SimStop := true;
wait;
end process;
end architecture;
Example to generate a waveform with an generic waveform:
type T_TIME_VECTOR is array(NATURAL range <>) of TIME;
constant myWaveform : T_TIME_VECTOR := (
-- generate "slow" pulse train
20 * 250 ns,
250 ns,
-- generate "fast" pulse train
50 * 10 ns,
10 ns
);
procedure generateWaveform(signal Wave : out BOOLEAN; Waveform: T_TIME_VECTOR; InitialValue : BOOLEAN) is
variable State : BOOLEAN := InitialValue;
begin
Wave <= State;
for i in Waveform'range loop
wait for Waveform(i);
State := not State;
Wave <= State;
end loop;
end procedure;
signal mySignal : BOOLEAN;
-- begin of architecture
generateWaveform(mySignal, myWaveform);
Best Answer
If you use for loops within a process, you can use a variable. This (translated into valid VHDL) should synthesise fine (assuming n is a constant or generic).
The resulting hardware may be large or slow, but the same would be true if you used generate statements. If it doesn't meet your size or speed goals, you get to play with clocked processes, and pipelining.
The obvious way to do it using
if ... generate
would be to make Val a 2-D array signal, which makes the "unrolling" into parallel hardware absolutely explicit. (or an n * n sized 1-D array) But that is more complex and error-prone than the simple process approach.