本帖最后由 well915 于 2013-4-11 09:08 编辑
各位大神,小弟急需求助,下面是我自己编的一段vhdl程序,具体功能为:输入3bit数据,编码为4bit输出数据,4bit数据的编码根据r的值确定(0或1),如果r=0,那第一组输出(1的个数>=0的个数),否则输出另一组,最后根据此次输出的4bit数据中的1和0的个数,确定下次r的值(1和0的个数相等,则r不变;如果不等,则r取反)。下面的程序我运行没问题,但是仿真结果不对,求大神指点,感激不尽,
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
USE ieee.std_logic_arith.ALL;
USE ieee.std_logic_unsigned.ALL;
ENTITY encoder_3b4b IS
PORT
(
din:IN std_logic_vector(0 TO 2);
clk:IN std_logic;
--rd_in:IN std_logic:='0';
rd_out:OUT std_logic;
dout:OUT std_logic_vector(0 TO 3)
);
END encoder_3b4b;
ARCHITECTURE behave OF encoder_3b4b IS
SIGNAL r:std_logic:='0';
SIGNAL dout1:std_logic_vector(0 TO 3);
BEGIN
encoder_3b4b_pro:
PROCESS(clk)
VARIABLE temp:std_logic;
BEGIN
temp:='1';
IF(rising_edge(clk))THEN
IF(r='0')THEN
CASE din IS
WHEN"000"=> dout1<="1011";
WHEN"001"=> dout1<="1001";
WHEN"010"=> dout1<="0101";
WHEN"011"=> dout1<="1100";
WHEN"100"=> dout1<="1101";
WHEN"101"=> dout1<="1010";
WHEN"110"=> dout1<="0110";
WHEN"111"=> dout1<="1110";
END CASE;
ELSE
CASE din IS
WHEN"000"=> dout1<="0100";
WHEN"001"=> dout1<="1001";
WHEN"010"=> dout1<="0101";
WHEN"011"=> dout1<="0011";
WHEN"100"=> dout1<="0010";
WHEN"101"=> dout1<="1010";
WHEN"110"=> dout1<="0110";
WHEN"111"=> dout1<="0001";
END CASE;
END IF;
FOR i IN 0 TO 3 LOOP --对4bit数据进行奇偶校验(temp=0说明4bit中有奇数个1,即0和1的个数不等,temp=1说明有偶数个1,即4bit中0和1的个数相等)
temp:=temp XOR dout1(i);
END LOOP;
IF(temp='0')THEN --判断4bit数据中1的个数是否为奇数(通过奇偶校验来检验编码后的4bit数据中的1和0的个数是否相等)
r<=NOT r; --如果0和1的个数不相等,则r取反
ELSE
r<=r;
END IF; --如果0和1的个数相等,r不变
END IF;
dout<=dout1;
rd_out<=r;
END PROCESS encoder_3b4b_pro;
END behave;
|