VHDL - Is there a better way to do this? - Page 1
Forum section: electronics / projects

author: alank2
author_rank: Super Contributor
timestamp: July 31, 2016, 06:36:18 pm
content: This is a snippet of some VHDL I have been modifying. I learned VHDL last week and that may show in this code - what I am doing is mapping 4 virtual floppy drives to various virtual diskettes.Is there a better way to do this with arrays that would be more efficient and consume less FPGA resources?disketteX is used to store the 12 bit diskette selected for each drive (0-4095).a reg101/reg110 write in this order will change the diskette loaded to a new value using the form:00ddxxxx xxxxxxxx where dd is the drive to change and xxxx xxxxxxxx is the disketter to change to.to read the current diskette for a drive you write to reg101 including the drive and it updates mount1/mount2 so they can be read as status to detect the currently assigned diskette for a particular drive.Code:signal disketteA : std_logic_vector(11 downto 0) := x"000";signal disketteB : std_logic_vector(11 downto 0) := x"001";signal disketteC : std_logic_vector(11 downto 0) := x"002";signal disketteD : std_logic_vector(11 downto 0) := x"003";signal reg101temp : std_logic_vector(7 downto 0) := x"00";signal mount1 : std_logic_vector(7 downto 0) := x"00";signal mount2 : std_logic_vector(7 downto 0) := x"00";beginprocess(n_wr)begin-- sd address 0..8 (first 9 bits) always zero because each sector is 512 bytesif rising_edge(n_wr) thenif (regAddr="101") then--store it for when 110 write comes inreg101temp <= dataIn;--update mount1/2 in case they are read backmount1(7 downto 4) <= dataIn(7 downto 4);if dataIn(5 downto 4)="00" thenmount1(3 downto 0) <= disketteA(11 downto 8);mount2 <= disketteA(7 downto 0);elsif dataIn(5 downto 4)="01" thenmount1(3 downto 0) <= disketteB(11 downto 8);mount2 <= disketteB(7 downto 0);elsif dataIn(5 downto 4)="10" thenmount1(3 downto 0) <= disketteC(11 downto 8);mount2 <= disketteC(7 downto 0);elsemount1(3 downto 0) <= disketteD(11 downto 8);mount2 <= disketteD(7 downto 0);end if;elsif (regAddr="110") then--update diskette in driveif reg101temp(5 downto 4)="00" thendisketteA(11 downto 8) <= reg101temp(3 downto 0);disketteA(7 downto 0) <= dataIn;elsif reg101temp(5 downto 4)="01" thendisketteB(11 downto 8) <= reg101temp(3 downto 0);disketteB(7 downto 0) <= dataIn;elsif reg101temp(5 downto 4)="10" thendisketteC(11 downto 8) <= reg101temp(3 downto 0);disketteC(7 downto 0) <= dataIn;elsedisketteD(11 downto 8) <= reg101temp(3 downto 0);disketteD(7 downto 0) <= dataIn;end if;elsif (is_LBA = '0') thenaddress(8 downto 0) <= (others => '0');if regAddr = "010" thenaddress(16 downto 9) <= dataIn;elsif regAddr = "011" thenaddress(22 downto 17) <= dataIn (5 downto 0);if dataIn(7 downto 6)="00" thenaddress(31 downto 23) <= disketteA(8 downto 0);elsif dataIn(7 downto 6)="01" thenaddress(31 downto 23) <= disketteB(8 downto 0);elsif dataIn(7 downto 6)="10" thenaddress(31 downto 23) <= disketteC(8 downto 0);elseaddress(31 downto 23) <= disketteD(8 downto 0);end if;end if;elseaddress(31 downto 26) <= (others => '0');if regAddr = "010" thenaddress(7 downto 0) <= dataIn;elsif regAddr = "011" thenaddress(13 downto 8) <= dataIn (5 downto 0);if dataIn(7 downto 6)="00" thenaddress(25 downto 14) <= disketteA;elsif dataIn(7 downto 6)="01" thenaddress(25 downto 14) <= disketteB;elsif dataIn(7 downto 6)="10" thenaddress(25 downto 14) <= disketteC;elseaddress(25 downto 14) <= disketteD;end if;--elsif regAddr = "100" then--address(22 downto 16) <= dataIn(6 downto 0);end if;end if;end if;end process;

author: nctnico
author_rank: Super Contributor
timestamp: July 31, 2016, 07:55:57 pm
content: I'd start by using signed and unsigned types instead of std_logic_vector if a signal represents a number and not a collection of bits. That way you can compare signals with numbers instead of bit strings.