无法将类型 STD_Logic 转换为类型 unsigned。

huangapple go评论84阅读模式
英文:

Cannot convert type STD_Logic to type unsigned

问题

以下是代码的翻译部分:

  1. &我正试图在32MIPS单周期处理器的ALU中创建无符号加法的逻辑,但一直出现以下错误:
  2. 无法将类型逻辑转换为类型无符号
  3. 到目前为止,这是我的代码(请随意查看,我觉得大部分都在那里,但某些逻辑可能有点问题):
  4. IEEE;
  5. 使用IEEE.STD_LOGIC_1164.ALL;
  6. 使用IEEE.STD_LOGIC_UNSIGNED.ALL;
  7. 使用ieee.numeric_std.all;
  8. 使用IEEE.STD_LOGIC_UNSIGNED.ALL;
  9. 实体ALU
  10. 端口(
  11. Ain STD_LOGIC_VECTOR31 downto 0;
  12. Bin STD_LOGIC_VECTOR31 downto 0;
  13. A_uB_uin unsigned31 downto 0;
  14. ALUCntlin STD_LOGIC_VECTOR3 downto 0;
  15. Carryinin STD_LOGIC;
  16. ALUOutout STD_LOGIC_VECTOR31 downto 0;
  17. Zeroout STD_LOGIC;
  18. Carryoutout std_logic;
  19. Overflowout STD_LOGIC
  20. ;
  21. end ALU;
  22. 体系结构ALU的行为是
  23. 信号ALU_Resultstd_logic_vector31 downto 0;
  24. 信号add_resultsub_resulta32b32c32std_logic_vector32 downto 0):=(其他 => '0';
  25. 信号add_ovsub_ovstd_logic;
  26. 开始
  27. -- 加法,减法和位操作
  28. 使用ALUCntl选择
  29. ALU_Result <= add_result31 downto 0 when "0010" -- 加法
  30. sub_result31 downto 0 when "0110" -- 减法
  31. A AND B when "0000"
  32. A OR B when "0001"
  33. A XOR B when "0011"
  34. A NOR B when "1100"
  35. A when others; ---所有其他ALU控制信号的条件
  36. ALUOut <= ALU_Result;
  37. -- 加法操作和进位生成
  38. a32 <= '0' & A;
  39. b32 <= '0' & B;
  40. c320 <= Carryin;
  41. add_result <= a32 + b32 + c32;
  42. sub_result <= a32 - b32;
  43. -- 无符号加法
  44. 使用ALUCntl选择
  45. add_result <= std_logic_vector(unsigned(A_u) + unsigned(B_u) + unsigned(Carryin)) when "0010"
  46. std_logic_vector(unsigned(A_u) + unsigned(B_u)) when "0101"
  47. (其他 => '0' when others;
  48. ALU_Result <= add_result31 downto 0;
  49. -- 无符号减法
  50. sub_result <= std_logic_vector(unsigned(A_u) - unsigned(B_u));
  51. ALU_Result <= sub_result31 downto 0 when ALUCntl = "0100" else ALU_Result;
  52. -- 零标志
  53. Zero <= '1' when ALU_Result = x"00000000" else '0';
  54. -- 溢出标志
  55. add_ov <= (A(31) and B(31) and (not ALU_Result(31))) or ((not A(31)) and (not B(31)) and ALU_Result(31));
  56. sub_ov <= (A(31) and (not B(31)) and (not ALU_Result(31))) or ((not A(31)) and B(31) and ALU_Result(31));
  57. 使用ALUCntl选择
  58. 溢出 <= add_ov when "0010"
  59. sub_ov when "0110"
  60. 'Z' when others;
  61. -- 进位输出
  62. 使用ALUCntl选择
  63. Carryout <= add_result(32) when "0010"
  64. sub_result(32) when "0110"
  65. 'Z' when others;
  66. 过程(ALUCntlAB
  67. 开始
  68. -- 有符号SLT
  69. 如果(ALUCntl = "1001")然后
  70. 如果(signed(A) < signed(B))然后
  71. ALU_Result <= "00000001"; -- 如果A < B,则将ALU输出设置为1
  72. 其他
  73. ALU_Result <= "00000000"; -- 如果A >= B,则将ALU输出设置为0
  74. 结束 if;
  75. 结束 if;
  76. -- 无符号SLT
  77. 如果(ALUCntl = "1010")然后
  78. 如果(unsigned(A) < unsigned(B))然后
  79. ALU_Result <= "00000001"; -- 如果A < B,则将ALU输出设置为1
  80. 其他
  81. ALU_Result <= "00000000"; -- 如果A >= B,则将ALU输出设置为0
  82. 结束 if;
  83. 结束 if;
  84. end process;
  85. end Behavioral;

请注意,我已经将代码中的注释和代码部分翻译为中文。如果您有任何其他问题,请随时提出。

英文:

&I am trying to create the logic for unsigned addition in the ALU of a 32-bit MIPS single cycle processor but keep getting this error:

cannot convert type logic to type unsigned

This is what I have so far (Feel free to look through it,I feel like its mostly there but I might be a bit off with some of the logic :

  1. library IEEE;
  2. use IEEE.STD_LOGIC_1164.ALL;
  3. use IEEE.STD_LOGIC_UNSIGNED.ALL;
  4. use ieee.numeric_std.all;
  5. use IEEE.STD_LOGIC_UNSIGNED.ALL;
  6. entity ALU is
  7. Port (
  8. A: in STD_LOGIC_VECTOR (31 downto 0);
  9. B: in STD_LOGIC_VECTOR (31 downto 0);
  10. A_u,B_u:in unsigned(31 downto 0);
  11. ALUCntl: in STD_LOGIC_VECTOR (3 downto 0);
  12. Carryin: in STD_LOGIC;
  13. ALUOut: out STD_LOGIC_VECTOR (31 downto 0);
  14. Zero: out STD_LOGIC;
  15. Carryout: out std_logic;
  16. Overflow: out STD_LOGIC
  17. );
  18. end ALU;
  19. architecture Behavioral of ALU is
  20. signal ALU_Result : std_logic_vector (31 downto 0);
  21. signal add_result, sub_result, a32, b32, c32: std_logic_vector(32 downto 0) := (others =&gt; &#39;0&#39;);
  22. signal add_ov, sub_ov: std_logic;
  23. begin
  24. -- Add, Sub, and Bitwise Operations
  25. with ALUCntl select
  26. ALU_Result &lt;= add_result(31 downto 0) when &quot;0010&quot;, --Add
  27. sub_result(31 downto 0) when &quot;0110&quot;, --sub
  28. A AND B when &quot;0000&quot;,
  29. A OR B when &quot;0001&quot;,
  30. A XOR B when &quot;0011&quot;,
  31. A NOR B when &quot;1100&quot;,
  32. A when others; ---condition for all other alu control signals
  33. ALUOut &lt;= ALU_Result;
  34. -- Addition Operation and carry out generation
  35. a32 &lt;= &#39;0&#39; &amp; A;
  36. b32 &lt;= &#39;0&#39; &amp; B;
  37. c32(0) &lt;= Carryin;
  38. add_result &lt;= a32 + b32 + c32;
  39. sub_result &lt;= a32 - b32;
  40. -- Unsigned addition
  41. with ALUCntl select
  42. add_result &lt;= std_logic_vector(unsigned(A_u) + unsigned(B_u) + unsigned(Carryin)) when &quot;0010&quot;,
  43. std_logic_vector(unsigned(A_u) + unsigned(B_u)) when &quot;0101&quot;,
  44. (others =&gt; &#39;0&#39;) when others;
  45. ALU_Result &lt;= add_result(31 downto 0);
  46. -- Unsigned subtraction
  47. sub_result &lt;= std_logic_vector(unsigned(A_u) - unsigned(B_u));
  48. ALU_Result &lt;= sub_result(31 downto 0) when ALUCntl = &quot;0100&quot; else ALU_Result;
  49. -- Zero flag
  50. Zero &lt;= &#39;1&#39; when ALU_Result = x&quot;00000000&quot; else &#39;0&#39;;
  51. -- Overflow flag
  52. add_ov &lt;= (A(31) and B(31) and (not ALU_Result(31))) or ((not A(31)) and (not B(31)) and ALU_Result(31));
  53. sub_ov &lt;= (A(31) and (not B(31)) and (not ALU_Result(31))) or ((not A(31)) and B(31) and ALU_Result(31));
  54. with ALUCntl select
  55. Overflow &lt;= add_ov when &quot;0010&quot;,
  56. sub_ov when &quot;0110&quot;,
  57. &#39;Z&#39; when others;
  58. -- Carryout
  59. with ALUCntl select
  60. Carryout &lt;= add_result(32) when &quot;0010&quot;,
  61. sub_result(32) when &quot;0110&quot;,
  62. &#39;Z&#39; when others;
  63. process (ALUCntl, A, B)
  64. begin
  65. -- Signed SLT
  66. if (ALUCntl = &quot;1001&quot;) then
  67. if (signed(A) &lt; signed(B)) then
  68. ALU_Result &lt;= &quot;00000001&quot;; -- set ALU output to 1 if A &lt; B
  69. else
  70. ALU_Result &lt;= &quot;00000000&quot;; -- set ALU output to 0 if A &gt;= B
  71. end if;
  72. end if;
  73. -- Unsigned SLT
  74. if (ALUCntl = &quot;1010&quot;) then
  75. if (unsigned(A) &lt; unsigned(B)) then
  76. ALU_Result &lt;= &quot;00000001&quot;; -- set ALU output to 1 if A &lt; B
  77. else
  78. ALU_Result &lt;= &quot;00000000&quot;; -- set ALU output to 0 if A &gt;= B
  79. end if;
  80. end if;
  81. end process;
  82. end Behavioral;

I think

答案1

得分: 3

以下是翻译好的部分:

问题在于CarryIn只是一个单独的std_logic,但使用signedunsigned进行算术运算需要它成为一个数组的一部分。你不能简单地将枚举类型如std_logic转换为数组,你必须从中创建一个数组聚合,这样做特别容易,因为unsignedsigned只是std_logic的数组,就像std_logic_vector一样。这里有几个选项:

  1. 使用数组聚合分配
  1. std_logic_vector(unsigned(A_u) + unsigned(B_u) + (0=>Carryin));

这样做是因为你将CarryIn分配给数组的第0位,编译器知道它必须是unsigned,因为它是一个+函数的一部分。

  1. 与一个空数组连接
  1. std_logic_vector(unsigned(A_u) + unsigned(B_u) + (""&Carryin));

因为VHDL允许空数组,""只是一个长度为0的位字符串文字,现在编译器知道它可以创建一个数组,然后根据上下文知道它是无符号的。

  1. 使用临时信号

可能比较复杂,但也是可以的:

  1. signal cin_a : unsigned(0 downto 0);
  2. cin_a(0) <= CarryIn;
  3. std_logic_vector(unsigned(A_u) + unsigned(B_u) + cin_a)

再次强调,现在你知道它是无符号的。

英文:

The problem is that CarryIn is simply a single std_logic, but arithmatic with signed or unsigned requires it to be part of an array. You simply cannot convert an enumerated type like std_logic into an array, you have to create an array aggregate from it, and it is particularly easy because unsigned or signed are simply arrays of std_logic like std_logic_vector. There are several options here:

  1. Use an aggregate assignment
  1. std_logic_vector(unsigned(A_u) + unsigned(B_u) + (0=&gt;Carryin));

This works because you are assigning CarryIn to bit 0 of an array, and the compiler knows it must be unsigned because of the context of being part of a &quot;+&quot; function

  1. Concatenate with a null array.
  1. std_logic_vector(unsigned(A_u) + unsigned(B_u) + (&quot;&quot;&amp;Carryin));

Because VHDL allows null arrays, and "" is simply a 0 length bit string literal, it now knows it can create an array, and then it knows it is unsigned from the context.

  1. Use a temporary signal

probably the most involved, but is fine:

  1. signal cin_a : unsigned(0 downto 0);
  2. cin_a(0) &lt;= CarryIn;
  3. std_logic_vector(unsigned(A_u) + unsigned(B_u) + cin_a)

Again, now you know it is an unsigned.

答案2

得分: -1

我猜问题出在携带部分,只有1位,所以将其转换为无符号或有符号都没有意义。

英文:

i guess it's the carryin that cause problem, it's just 1bit so doesn't make sens to convert it to either unsigned or signed.

huangapple
  • 本文由 发表于 2023年2月27日 15:38:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75577821.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定