2012-05-07 1 views
2

나는 간단한 가산기를 구현 중이다. 그러나, 나는 약간의 독특한 트위스트의 필요성이있다.별도의 캐리 플래그와 빌로우 플래그로 간단한 애 더를 설계하는 방법은 무엇입니까?

구현중인 것은 코드 세그먼트 (CS) 레지스터와 명령어 포인터 (IP) 레지스터에서 "롤오버"기능입니다. 따라서 상대 점프를 +20으로하고 IP가 254이면 IP는 18로 끝나고 CS는 1 씩 증가합니다.

이 부분은 쉽고, 어려운 부분은 반대입니다 방향. 점프가 -20이고 IP가 0,은 1로 CS를 감소하고 지금까지 내 코드는 236

에 IP 롤 언더을 할 필요가있다 말할 때,의 차용을 감지

entity carryover is 
    port(
    DataIn: in std_logic_vector(7 downto 0); 
    SegmentIn: in std_logic_vector(7 downto 0); 
    Addend: in std_logic_vector(7 downto 0); --How much to increase DataIn by (as a signed number). Believe it or not, that's the actual word for what we need. 
    DataOut: out std_logic_vector(7 downto 0); 
    SegmentOut: out std_logic_vector(7 downto 0); 
    ); 
end carryover; 

architecture Behavioral of carryover is 
    signal temp: std_logic_vector(8 downto 0); 
begin 
    --treat as unsigned because it doesn't actually matter for addition and just make carry and borrow correct 
    temp <= std_logic_vector(unsigned("0" & DataIn) + (unsigned)Addend); 
    DataOut <= temp(7 downto 0); 
    SegmentOut <= unsigned(SegmentIn) + 1 when (not temp(8)) and (not Addend(7) 

end Behavioral; 

하지만 차용을 감지하는 방법을 알아낼 수는 없습니다. 이 작업을 수행 할 수있는 명확한 방법이 있습니까?

업데이트

나의 새로운 코드는 이것이다 : 서명 숫자의

library IEEE; 
use IEEE.STD_LOGIC_1164.ALL; 
use IEEE.NUMERIC_STD.ALL; 
use work.tinycpu.all; 

entity carryover is 
    port(
    EnableCarry: in std_logic; --When disabled, SegmentIn goes to SegmentOut 
    DataIn: in std_logic_vector(7 downto 0); 
    SegmentIn: in std_logic_vector(7 downto 0); 
    Addend: in std_logic_vector(7 downto 0); --How much to increase DataIn by (as a signed number). Believe it or not, that's the actual word for what we need. 
    DataOut: out std_logic_vector(7 downto 0); 
    SegmentOut: out std_logic_vector(7 downto 0) 
-- Debug: out std_logic_vector(8 downto 0) 
    ); 
end carryover; 

architecture Behavioral of carryover is 
    signal temp: std_logic_vector(8 downto 0); 
begin 
    --treat as unsigned because it doesn't actually matter for addition and just make carry and borrow correct 
    process(DataIn, SegmentIn,Addend, EnableCarry) 
    begin 
    temp <= std_logic_vector(signed('0' & DataIn) + signed(Addend(7) & Addend)); 
    if (EnableCarry and ((not Addend(7)) and (DataIn(7)) and temp(8)))='1' then 
     SegmentOut <= std_logic_vector(unsigned(SegmentIn)+1); 
    elsif (EnableCarry and (Addend(7) and (not DataIn(7)) and temp(8)))='1' then 
     SegmentOut <= std_logic_vector(unsigned(SegmentIn)-1); 
    else 
     SegmentOut <= SegmentIn; 
    end if; 
    end process; 
    --Debug <= Temp; 
    DataOut <= temp(7 downto 0); 
end Behavioral; 

추가 계획대로 작동하고, 온도는 이제 항상 올바른 결과이며, 아직 SegmentOut 항상 SegmentIn과 같다. 나는 왜 SegmentIn + 1에 대해 실제로 Addend = 0x04, DataIn = 0xFE, SegmentIn = 0x00 및 CarryEnable = 1의 입력을 직접 계산했기 때문에 이해할 수 없다. if 문은 (1 and ((not 0) and 1 and 1))='1'과 동일하지만 아직 SegmentOut은 변하지 않는다. 누구나 이것이 구현되는 방식에 문제가있는 것을 볼 수 있습니까? 피가수가 비트 비트 노래 합과 동일하지만 동일하지 않을 로그인 할 때

+0

"깨끗한"이라고 말하면 비트 단위 확인을 의미합니까? 또한 부정적인 점프를 계획하는 경우 부호가있는 산술로 전환하여 해석자가 더하기 및 빼기가 발생할 수 있음을 이해하도록 할 수 있습니다. if 문을 사용하면 매우 간단 할 것이라고 생각하지만,이 문장이 "깨끗한"해결책이라고 생각하지는 않습니다. –

답변

0

오버 플로우가 발생 DateIn으로

A = + 12, B = + 4.   A = + 4, B = – 12 
А = 0.1100     A = 0.0100 
В = 0.0100     B = 1.0100 
    ------      ------ 
C 1.0000     C 1.1000 
As = Bs, Cs = 1 – overflow As != Bs - not overflow. 

항상 긍정적이다, 오버 플로우가 캐리에서만 발생할 수 있습니다 (양수 모두). 그래서 당신은 당신의 문을 변경해야합니다 (아마 어떻게 든이 두 가지를 결합) :

Addend(7) DataIn(7) temp(7)| Carry Borrow 
0   0   0  | 0  0 
0   0   1  | 1  0 
1   0   0  | 0  0 
1   0   1  | 0  1 

편집 : 여기

SegmentOut <= unsigned(SegmentIn) + 1 when (not Addend(7) and temp(7)); 
SegmentOut <= unsigned(SegmentIn) - 1 when (Addend(7) and temp(7)); 

는 진실 테이블 인 폴 시브 말했듯이 :

SegmentOut <= unsigned(signed(SegmentIn) + signed(Addend(7) & Addend(7) & Addend(7) & Addend(7) & Addend(7) & Addend(7) & Addend(7) & "1")) when (temp(7) and CarryFlag); 
+1

부호가있는 산술을 사용하고 1을 추가하는지 음수를 1을 추가 할지를 변경하고 대답을 부호없는 값으로 변환하여 두 문을 단일 더하기/빼기로 "통합"할 수 있습니다. –

+0

@PaulSeeb 감사합니다. 내 대답을 편집했습니다. 하지만 왜 우리는 부호가있는 산술 연산을 사용해야합니까? 나는 그것을 얻지 않는다. –

+0

약간 수정 된 버전의 코드를 사용했습니다. (temp (7) 대신 temp (8)을 사용하지 않아야합니까?)하지만 SegmentOut은 변경되지 않습니다. 내 질문에 대한 업데이트를 참조하십시오 – Earlz