Chapter start Previous page Next page
This section describes part of a controller for an automobile engine. Table 10.21 shows a temperature converter that converts digitized temperature readings from a sensor from degrees Centigrade to degrees Fahrenheit.
To save area the temperature conversion is approximate. Instead of multiplying by 9/5 and adding 32 (so 0 degC becomes 32 degF and 100 degC becomes 212 degF) we multiply by 1.75 and add 32 (so 100 degC becomes 207 degF). Since 1.75 = 1 + 0.5 + 0.25, we can multiply by 1.75 using shifts (for divide by 2, and divide by 4) together with a very simple constant addition (since 32 = "100000"). Using shift to multiply and divide by powers of 2 is free in hardware (we just change connections to a bus). For large temperatures the error approaches 0.05/1.8 or approximately 3 percent. We play these kinds of tricks often in hardware computation. Notice also that temperatures measured in degC and degF are defined as unsigned integers of the same width. We could have defined these as separate types to take advantage of VHDL's type checking.
Table 10.22 describes
a digital filter to compute a "moving average" over four successive
samples in time (i(0), i(1), i(2), and i(3), with i(0) being
the first sample).
The filter uses the following formula:
T_out <=
( i(0) + i(1) + i(2) + i(3) )/T4
Division by T4 = "100"
is free in hardware. If instead, we performed the divisions before the additions,
this would reduce the number of bits to be added for two of the additions
and saves us worrying about overflow. The drawback to this approach is round-off
errors. We can use the register shown in Table 10.23 to register the
inputs.
Table 10.24 shows a first-in, first-out stack (FIFO). This allows us to buffer the signals coming from the sensor until the microprocessor has a chance to read them. The depth of the FIFO will depend on the maximum amount of time that can pass without the microcontroller being able to read from the bus. We have to determine this with statistical simulations taking into account other traffic on the bus.
The FIFO has flags, empty
and full , that signify its state. It uses a function to increment
two circular pointers. One pointer keeps track of the address to write to
next, the other pointer tracks the address to read from. The FIFO memory
may be implemented in a number of ways in hardware. We shall assume for
the moment that it will be synthesized as a bank of flip-flops.
Table 10.25 shows a controller for the two FIFOs. The controller handles the reading and writing to the FIFO. The microcontroller attached to the bus signals which of the FIFOs it wishes to read from. The controller then places the appropriate data on the bus. The microcontroller can also ask for the FIFO flags to be placed in the low-order bits of the bus on a read cycle. If none of these actions are requested by the microcontroller, the FIFO controller three-states its output drivers.
Table 10.25 shows the top level of the controller. To complete our model we shall use a package for the component declarations:
library IEEE; use IEEE.STD_LOGIC_1164.all; use IEEE.NUMERIC_STD.all; entity T_Control is port (T_in1, T_in2 : in UNSIGNED (11 downto 0); sensor: in UNSIGNED(1 downto 0); clk, RD, rst : in STD_LOGIC; D : out UNSIGNED(11 downto 0)); end; architecture structure of T_Control is use work.TC_Components.all; signal F, E : UNSIGNED (2 downto 1); signal T_out1, T_out2, R_out1, R_out2, F1, F2, FIFO1, FIFO2 : UNSIGNED(11 downto 0); signal RD1, RD2, WR: STD_LOGIC ; begin RG1 : register_in generic map (1ns) port map (T_in1,clk,rst,R_out1); RG2 : register_in generic map (1ns) port map (T_in2,clk,rst,R_out2); TC1 : tconv generic map (1ns) port map (R_out1, T_out1); TC2 : tconv generic map (1ns) port map (R_out2, T_out2); TF1 : filter generic map (1ns) port map (T_out1, rst, clk, F1); TF2 : filter generic map (1ns) port map (T_out2, rst, clk, F2); FI1 : fifo generic map (12,16) port map (clk, rst, WR, RD1, F1, FIFO1, E(1), F(1)); FI2 : fifo generic map (12,16) port map (clk, rst, WR, RD2, F2, FIFO2, E(2), F(2)); FC1 : fifo_control port map (FIFO1, FIFO2, sensor, RD, F(1), F(2), E(1), E(2), RD1, RD2, WR, D); end structure; |
package TC_Components is component register_in generic (TPD : TIME := 1 ns); port (T_in : in UNSIGNED(11 downto 0); clk, rst : in STD_LOGIC; T_out : out UNSIGNED(11 downto 0)); end component; component tconv generic (TPD : TIME := 1 ns); port (T_in : in UNSIGNED (7 downto 0); clk, rst : in STD_LOGIC; T_out : out UNSIGNED(7 downto 0)); end component; component filter generic (TPD : TIME := 1 ns); port (T_in : in UNSIGNED (7 downto 0); rst, clk : in STD_LOGIC; T_out : out UNSIGNED(7 downto 0)); end component; component fifo generic (width:INTEGER := 12; depth : INTEGER := 16); port (clk, rst, push, pop : STD_LOGIC; Di : UNSIGNED (width-1 downto 0); Do : out UNSIGNED (width-1 downto 0); empty, full : out STD_LOGIC); end component; component fifo_control generic (TPD:TIME := 1 ns); port (D_1, D_2 : in UNSIGNED(7 downto 0); select : in UNSIGNED(1 downto 0); read, f1, f2, e1, e2 : in STD_LOGIC; r1, r2, w12 : out STD_LOGIC; D : out UNSIGNED(7 downto 0)) ; end component; end;
The following testbench completes a set of reads and writes to the FIFOs:
library IEEE; use IEEE.std_logic_1164.all; -- type STD_LOGIC use IEEE.numeric_std.all; -- type UNSIGNED entity test_TC is end; architecture testbench of test_TC is component T_Control port (T_1, T_2 : in UNSIGNED(11 downto 0); clk : in STD_LOGIC; sensor: in UNSIGNED( 1 downto 0) ; read : in STD_LOGIC; rst : in STD_LOGIC; D : out UNSIGNED(7 downto 0)); end component; signal T_1, T_2 : UNSIGNED(11 downto 0); signal clk, read, rst : STD_LOGIC; signal sensor : UNSIGNED(1 downto 0); signal D : UNSIGNED(7 downto 0); begin TT1 : T_Control port map (T_1, T_2, clk, sensor, read, rst, D); process begin rst <= '0'; clk <= '0'; wait for 5 ns; rst <= '1'; wait for 5 ns; rst <= '0'; T_in1 <= "000000000011"; T_in2 <= "000000000111"; read <= '0'; for i in 0 to 15 loop -- fill the FIFOs clk <= '0'; wait for 5 ns; clk <= '1'; wait for 5 ns; end loop; assert (false) report "FIFOs full" severity NOTE; clk <= '0'; wait for 5 ns; clk <= '1'; wait for 5 ns; read <= '1'; sensor <= "01"; for i in 0 to 15 loop -- empty the FIFOs clk <= '0'; wait for 5ns; clk <= '1'; wait for 5 ns; end loop; assert (false) report "FIFOs empty" severity NOTE; clk <= '0'; wait for 5ns; clk <= '1'; wait; end process; end;