วันศุกร์ที่ 26 กุมภาพันธ์ พ.ศ. 2559

2complement

2complement การบวกเลขฐานสองถ้าค่าเป็นบวกก็บวกกันปกติ ถ้าเป็นลบเราจะเขียนแบบ 2complement คือการนำเลขฐานสองมา invert หรือกลับค่าจาก 0 เป็น 1 และจาก 1 เป็น 0 แล้วบวกด้วย 1 อีกที  เช่น  6 - 3 = 3
                             0111- 0011   เอา 0011 ไป invert 1100 แล้วบวก 1 = 1101
                             0111(6)+1101(-3) = 0011(3)
cr.http://cpre.kmutnb.ac.th/esl
CODE:
library ieee;
use ieee.std_logic_1164.all;
entity full_add is
  port(
a , b, c_in : in std_logic;
sub : in std_logic;
c_out, p : out std_logic);
end full_add;
architecture data_flow of full_add is
signal keep :std_logic;
begin
process(a,b,c_in)
begin
keep <= b xor sub;
c_out <= ((a xor keep) and c_in) or (a and keep);
p <= (a xor keep) xor c_in;
end process;
end data_flow;








Full adder

Full adder  คือ การบวกเลขฐานสอง
                          0 + 0 = 0
                          1 + 0 = 1
                          0 + 1 = 1
                          1 + 1 =  0 (ทด1)
                                  
                           เช่น 
                                         1 1                         
                                      0 0 1 1
                                                +
                                      1 0 0 1
                                      1 1 0 0    (3 + 9 = 12)

สมการ full adder


ตารางความจริง full adder


cr. https://en.wikipedia.org/wiki/Adder_(electronics)

code full-adder :
library ieee;
use ieee.std_logic_1164.all;
entity full_add is
port(
 a , b, c_in : in std_logic;
 c_out, sum : out std_logic);
end full_add;
architecture data_flow of full_add is
begin
 c_out <= ((a xor b) and c_in) or (a and b); -- ตามสมการfull adder หา carry out
 sum <= (a xor b) xor c_in; -- ตามสมการfull adder หาผลรวม
end data_flow;