VHDL数据取反操作

对于数据取反,通常需要加入use ieee.std_logic_signed.all程序包。这里举例,对8位宽的数据进行取反操作。

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use ieee.std_logic_arith.all;
use ieee.std_logic_signed.all;


entity top is
	port(
		clk         : in std_logic;
		rst         : in std_logic;
		din         : in std_logic_vector(7 downto 0);
		dout        : out std_logic_vector(7 downto 0)
	);
end top;

architecture Behavioral of top is

begin

	process(clk,rst)
	begin
		if rst = '1' then
			dout <= (others => '0');
		elsif rising_edge(clk)  then
			dout <= -din;
		end if;
	end process;
	
end Behavioral;

添加testbench,对代码仿真。

module tst_top;

	// Inputs
	reg clk;
	reg rst;
	reg [7:0] din;

	// Outputs
	wire [7:0] dout;

	// Instantiate the Unit Under Test (UUT)
	top uut (
		.clk(clk), 
		.rst(rst), 
		.din(din), 
		.dout(dout)
	);

	initial begin
		// Initialize Inputs
		clk = 0;
		rst = 1;
		din = 8'd0;

		// Wait 100 ns for global reset to finish
		#100;
                rst = 0;

	end
      
	always @(posedge clk)
	begin
		din <= din + 1;
	end 
	
	always #5 clk =~clk;
endmodule

设计对渐加数取反,在Modelsim上观察仿真结果

现象:根据仿真结果看,在输入10000000(-128)时,输出为10000000(-128),结果出错。

分析出错原因:8位有符号数的表示范围为-128~127,无法表示128,所以溢出导致出错。

解决方法:判断输入数据是否为10000000。是,输出127;否,直接取反。

 

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值