Data update
This commit is contained in:
parent
5150844a7d
commit
4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions
|
|
@ -1,23 +0,0 @@
|
|||
type Four_Bits is array (1..4) of Boolean;
|
||||
|
||||
procedure Half_Adder (Input_1, Input_2 : Boolean; Output, Carry : out Boolean) is
|
||||
begin
|
||||
Output := Input_1 xor Input_2;
|
||||
Carry := Input_1 and Input_2;
|
||||
end Half_Adder;
|
||||
|
||||
procedure Full_Adder (Input_1, Input_2 : Boolean; Output : out Boolean; Carry : in out Boolean) is
|
||||
T_1, T_2, T_3 : Boolean;
|
||||
begin
|
||||
Half_Adder (Input_1, Input_2, T_1, T_2);
|
||||
Half_Adder (Carry, T_1, Output, T_3);
|
||||
Carry := T_2 or T_3;
|
||||
end Full_Adder;
|
||||
|
||||
procedure Four_Bits_Adder (A, B : Four_Bits; C : out Four_Bits; Carry : in out Boolean) is
|
||||
begin
|
||||
Full_Adder (A (4), B (4), C (4), Carry);
|
||||
Full_Adder (A (3), B (3), C (3), Carry);
|
||||
Full_Adder (A (2), B (2), C (2), Carry);
|
||||
Full_Adder (A (1), B (1), C (1), Carry);
|
||||
end Four_Bits_Adder;
|
||||
|
|
@ -1,52 +0,0 @@
|
|||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
|
||||
procedure Test_4_Bit_Adder is
|
||||
|
||||
-- The definitions from above
|
||||
|
||||
function Image (Bit : Boolean) return Character is
|
||||
begin
|
||||
if Bit then
|
||||
return '1';
|
||||
else
|
||||
return '0';
|
||||
end if;
|
||||
end Image;
|
||||
|
||||
function Image (X : Four_Bits) return String is
|
||||
begin
|
||||
return Image (X (1)) & Image (X (2)) & Image (X (3)) & Image (X (4));
|
||||
end Image;
|
||||
|
||||
A, B, C : Four_Bits; Carry : Boolean;
|
||||
begin
|
||||
for I_1 in Boolean'Range loop
|
||||
for I_2 in Boolean'Range loop
|
||||
for I_3 in Boolean'Range loop
|
||||
for I_4 in Boolean'Range loop
|
||||
for J_1 in Boolean'Range loop
|
||||
for J_2 in Boolean'Range loop
|
||||
for J_3 in Boolean'Range loop
|
||||
for J_4 in Boolean'Range loop
|
||||
A := (I_1, I_2, I_3, I_4);
|
||||
B := (J_1, J_2, J_3, J_4);
|
||||
Carry := False;
|
||||
Four_Bits_Adder (A, B, C, Carry);
|
||||
Put_Line
|
||||
( Image (A)
|
||||
& " + "
|
||||
& Image (B)
|
||||
& " = "
|
||||
& Image (C)
|
||||
& " "
|
||||
& Image (Carry)
|
||||
);
|
||||
end loop;
|
||||
end loop;
|
||||
end loop;
|
||||
end loop;
|
||||
end loop;
|
||||
end loop;
|
||||
end loop;
|
||||
end loop;
|
||||
end Test_4_Bit_Adder;
|
||||
|
|
@ -35,9 +35,9 @@ fourBitAdder: function [a,b][
|
|||
|
||||
loop 0..15 'a [
|
||||
loop 0..15 'b [
|
||||
binA: (as.binary a) ++ join to [:string] repeat 0 4-size as.binary a
|
||||
binB: (as.binary b) ++ join to [:string] repeat 0 4-size as.binary b
|
||||
binA: (to :string .format:".b" a) ++ join to [:string] repeat 0 4-size to :string .format:".b" a
|
||||
binB: (to :string .format:".b" b) ++ join to [:string] repeat 0 4-size to :string .format:".b" b
|
||||
[sm,carry]: fourBitAdder binA binB
|
||||
print [pad to :string a 2 "+" pad to :string b 2 "=" binA "+" binB "=" "("++carry++")" sm "=" from.binary carry ++ sm]
|
||||
print [pad to :string a 2 "+" pad to :string b 2 "=" binA "+" binB "=" "("++carry++")" sm "=" parse "0b" ++ carry ++ sm]
|
||||
]
|
||||
]
|
||||
|
|
|
|||
|
|
@ -1,40 +0,0 @@
|
|||
Func _NOT($_A)
|
||||
Return (Not $_A) *1
|
||||
EndFunc ;==>_NOT
|
||||
|
||||
Func _AND($_A, $_B)
|
||||
Return BitAND($_A, $_B)
|
||||
EndFunc ;==>_AND
|
||||
|
||||
Func _OR($_A, $_B)
|
||||
Return BitOR($_A, $_B)
|
||||
EndFunc ;==>_OR
|
||||
|
||||
Func _XOR($_A, $_B)
|
||||
Return _OR( _
|
||||
_AND( $_A, _NOT($_B) ), _
|
||||
_AND( _NOT($_A), $_B) )
|
||||
EndFunc ;==>_XOR
|
||||
|
||||
Func _HalfAdder($_A, $_B, ByRef $_CO)
|
||||
$_CO = _AND($_A, $_B)
|
||||
Return _XOR($_A, $_B)
|
||||
EndFunc ;==>_HalfAdder
|
||||
|
||||
Func _FullAdder($_A, $_B, $_CI, ByRef $_CO)
|
||||
Local $CO1, $CO2, $Q1, $Q2
|
||||
$Q1 = _HalfAdder($_A, $_B, $CO1)
|
||||
$Q2 = _HalfAdder($Q1, $_CI, $CO2)
|
||||
$_CO = _OR($CO2, $CO1)
|
||||
Return $Q2
|
||||
EndFunc ;==>_FullAdder
|
||||
|
||||
Func _4BitAdder($_A1, $_A2, $_A3, $_A4, $_B1, $_B2, $_B3, $_B4, $_CI, ByRef $_CO)
|
||||
Local $CO1, $CO2, $CO3, $CO4, $Q1, $Q2, $Q3, $Q4
|
||||
$Q1 = _FullAdder($_A4, $_B4, $_CI, $CO1)
|
||||
$Q2 = _FullAdder($_A3, $_B3, $CO1, $CO2)
|
||||
$Q3 = _FullAdder($_A2, $_B2, $CO2, $CO3)
|
||||
$Q4 = _FullAdder($_A1, $_B1, $CO3, $CO4)
|
||||
$_CO = $CO4
|
||||
Return $Q4 & $Q3 & $Q2 & $Q1
|
||||
EndFunc ;==>_4BitAdder
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
Local $CarryOut, $sResult
|
||||
$sResult = _4BitAdder(0, 0, 1, 1, 0, 1, 1, 1, 0, $CarryOut) ; adds 3 + 7
|
||||
ConsoleWrite('result: ' & $sResult & ' ==> carry out: ' & $CarryOut & @LF)
|
||||
|
||||
$sResult = _4BitAdder(1, 0, 1, 1, 1, 0, 0, 0, 0, $CarryOut) ; adds 11 + 8
|
||||
ConsoleWrite('result: ' & $sResult & ' ==> carry out: ' & $CarryOut & @LF)
|
||||
|
|
@ -1,107 +0,0 @@
|
|||
program-id. test-add.
|
||||
environment division.
|
||||
configuration section.
|
||||
special-names.
|
||||
class bin is "0" "1".
|
||||
data division.
|
||||
working-storage section.
|
||||
1 parms.
|
||||
2 a-in pic 9999.
|
||||
2 b-in pic 9999.
|
||||
2 r-out pic 9999.
|
||||
2 c-out pic 9.
|
||||
procedure division.
|
||||
display "Enter 'A' value (4-bits binary): "
|
||||
with no advancing
|
||||
accept a-in
|
||||
if a-in (1:) not bin
|
||||
display "A is not binary"
|
||||
stop run
|
||||
end-if
|
||||
display "Enter 'B' value (4-bits binary): "
|
||||
with no advancing
|
||||
accept b-in
|
||||
if b-in (1:) not bin
|
||||
display "B is not binary"
|
||||
stop run
|
||||
end-if
|
||||
call "add-4b" using parms
|
||||
display "Carry " c-out " result " r-out
|
||||
stop run
|
||||
.
|
||||
end program test-add.
|
||||
|
||||
program-id. add-4b.
|
||||
data division.
|
||||
working-storage section.
|
||||
1 wk binary.
|
||||
2 i pic 9(4).
|
||||
2 occurs 5.
|
||||
3 a-reg pic 9.
|
||||
3 b-reg pic 9.
|
||||
3 c-reg pic 9.
|
||||
3 r-reg pic 9.
|
||||
2 a pic 9.
|
||||
2 b pic 9.
|
||||
2 c pic 9.
|
||||
2 a-not pic 9.
|
||||
2 b-not pic 9.
|
||||
2 c-not pic 9.
|
||||
2 ha-1s pic 9.
|
||||
2 ha-1c pic 9.
|
||||
2 ha-1s-not pic 9.
|
||||
2 ha-1c-not pic 9.
|
||||
2 ha-2s pic 9.
|
||||
2 ha-2c pic 9.
|
||||
2 fa-s pic 9.
|
||||
2 fa-c pic 9.
|
||||
linkage section.
|
||||
1 parms.
|
||||
2 a-in pic 9999.
|
||||
2 b-in pic 9999.
|
||||
2 r-out pic 9999.
|
||||
2 c-out pic 9.
|
||||
procedure division using parms.
|
||||
initialize wk
|
||||
perform varying i from 1 by 1
|
||||
until i > 4
|
||||
move a-in (5 - i:1) to a-reg (i)
|
||||
move b-in (5 - i:1) to b-reg (i)
|
||||
end-perform
|
||||
perform simulate-adder varying i from 1 by 1
|
||||
until i > 4
|
||||
move c-reg (5) to c-out
|
||||
perform varying i from 1 by 1
|
||||
until i > 4
|
||||
move r-reg (i) to r-out (5 - i:1)
|
||||
end-perform
|
||||
exit program
|
||||
.
|
||||
|
||||
simulate-adder section.
|
||||
move a-reg (i) to a
|
||||
move b-reg (i) to b
|
||||
move c-reg (i) to c
|
||||
add a -1 giving a-not
|
||||
add b -1 giving b-not
|
||||
add c -1 giving c-not
|
||||
|
||||
compute ha-1s = function max (
|
||||
function min ( a b-not )
|
||||
function min ( b a-not ) )
|
||||
compute ha-1c = function min ( a b )
|
||||
add ha-1s -1 giving ha-1s-not
|
||||
add ha-1c -1 giving ha-1c-not
|
||||
|
||||
compute ha-2s = function max (
|
||||
function min ( c ha-1s-not )
|
||||
function min ( ha-1s c-not ) )
|
||||
compute ha-2c = function min ( c ha-1c )
|
||||
|
||||
compute fa-s = ha-2s
|
||||
compute fa-c = function max ( ha-1c ha-2c )
|
||||
|
||||
move fa-s to r-reg (i)
|
||||
move fa-c to c-reg (i + 1)
|
||||
.
|
||||
end program add-4b.
|
||||
|
|
@ -56,4 +56,6 @@ Module FourBitAdder {
|
|||
k=each(res() end to start) ' k is an iterator, now configure to read items in reverse
|
||||
Print "A+B",v, k, k_low ' print 1 1 0 0 0 0 1 0 1
|
||||
}
|
||||
Form 60
|
||||
Print $(, 6) ' 6 characters tab width
|
||||
FourBitAdder
|
||||
|
|
|
|||
|
|
@ -1,43 +0,0 @@
|
|||
sub dec2bin { sprintf "%04b", shift }
|
||||
sub bin2dec { oct "0b".shift }
|
||||
sub bin2bits { reverse split(//, substr(shift,0,shift)); }
|
||||
sub bits2bin { join "", map { 0+$_ } reverse @_ }
|
||||
|
||||
sub bxor {
|
||||
my($a, $b) = @_;
|
||||
(!$a & $b) | ($a & !$b);
|
||||
}
|
||||
|
||||
sub half_adder {
|
||||
my($a, $b) = @_;
|
||||
( bxor($a,$b), $a & $b );
|
||||
}
|
||||
|
||||
sub full_adder {
|
||||
my($a, $b, $c) = @_;
|
||||
my($s1, $c1) = half_adder($a, $c);
|
||||
my($s2, $c2) = half_adder($s1, $b);
|
||||
($s2, $c1 | $c2);
|
||||
}
|
||||
|
||||
sub four_bit_adder {
|
||||
my($a, $b) = @_;
|
||||
my @abits = bin2bits($a,4);
|
||||
my @bbits = bin2bits($b,4);
|
||||
|
||||
my($s0,$c0) = full_adder($abits[0], $bbits[0], 0);
|
||||
my($s1,$c1) = full_adder($abits[1], $bbits[1], $c0);
|
||||
my($s2,$c2) = full_adder($abits[2], $bbits[2], $c1);
|
||||
my($s3,$c3) = full_adder($abits[3], $bbits[3], $c2);
|
||||
(bits2bin($s0, $s1, $s2, $s3), $c3);
|
||||
}
|
||||
|
||||
print " A B A B C S sum\n";
|
||||
for my $a (0 .. 15) {
|
||||
for my $b (0 .. 15) {
|
||||
my($abin, $bbin) = map { dec2bin($_) } $a,$b;
|
||||
my($s,$c) = four_bit_adder( $abin, $bbin );
|
||||
printf "%2d + %2d = %s + %s = %s %s = %2d\n",
|
||||
$a, $b, $abin, $bbin, $c, $s, bin2dec($c.$s);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,51 +1,21 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">xor_gate</span><span style="color: #0000FF;">(</span><span style="color: #004080;">bool</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">a</span> <span style="color: #008080;">and</span> <span style="color: #008080;">not</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">or</span> <span style="color: #0000FF;">(</span><span style="color: #008080;">not</span> <span style="color: #000000;">a</span> <span style="color: #008080;">and</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
with javascript_semantics
|
||||
constant inf = 1e300*1e300, -- (works on both 32 and 64 bit)
|
||||
ninf = -inf,
|
||||
nan = -(inf/inf),
|
||||
nzero = -1/inf -- (not supported)
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">half_adder</span><span style="color: #0000FF;">(</span><span style="color: #004080;">bool</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">bool</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">xor_gate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">a</span> <span style="color: #008080;">and</span> <span style="color: #000000;">b</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">}</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">full_adder</span><span style="color: #0000FF;">(</span><span style="color: #004080;">bool</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">bool</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">s1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c1</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">half_adder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #0000FF;">{</span><span style="color: #000000;">s2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c2</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">half_adder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">c1</span> <span style="color: #008080;">or</span> <span style="color: #000000;">c2</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">s2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">}</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">four_bit_adder</span><span style="color: #0000FF;">(</span><span style="color: #004080;">bool</span> <span style="color: #000000;">a0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">a1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">a2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">a3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b3</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">bool</span> <span style="color: #000000;">s0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span>
|
||||
<span style="color: #0000FF;">{</span><span style="color: #000000;">s0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">full_adder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #0000FF;">{</span><span style="color: #000000;">s1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">full_adder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #0000FF;">{</span><span style="color: #000000;">s2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">full_adder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #0000FF;">{</span><span style="color: #000000;">s3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">full_adder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">s3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">}</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">bool</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">a0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a3</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">int_to_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #0000FF;">{</span><span style="color: #000000;">b0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b3</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">int_to_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #0000FF;">{</span><span style="color: #000000;">r3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">four_bit_adder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b3</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">bits_to_int</span><span style="color: #0000FF;">({</span><span style="color: #000000;">r0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r3</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">?</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">" [=%d+16]"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r</span><span style="color: #0000FF;">):</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%04b + %04b = %04b %b (%d+%d=%d%s)\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">*</span><span style="color: #000000;">16</span><span style="color: #0000FF;">+</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
|
||||
|
||||
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b1111</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1111</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">11</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b1100</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1100</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b1100</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1101</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b1100</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1110</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b1100</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1111</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b1101</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b0000</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b1101</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b0001</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b1101</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b0010</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b1101</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b0011</span><span style="color: #0000FF;">)</span>
|
||||
<!--
|
||||
printf(1," inf: %f\n",{inf})
|
||||
printf(1," ninf: %f\n",{ninf})
|
||||
printf(1," nan: %f\n",{nan})
|
||||
printf(1,"*nzero: %f\n",{nzero})
|
||||
printf(1," inf+2: %f\n",{inf+2})
|
||||
printf(1," inf+ninf: %f\n",{inf+ninf})
|
||||
printf(1," 0*inf: %f\n",{0*inf})
|
||||
printf(1," nan+1: %f\n",{nan+1})
|
||||
printf(1," nan+nan: %f\n",{nan+nan})
|
||||
printf(1," inf>1e300: %d\n",{inf>1e300})
|
||||
printf(1," ninf<1e300: %d\n",{ninf<-1e300})
|
||||
printf(1,"*nan=nan: %d\n",{nan=nan})
|
||||
printf(1," nan=42: %d\n",{nan=42})
|
||||
printf(1,"*nan<0: %d\n",{nan<0})
|
||||
printf(1," nan>0: %d\n",{nan>0})
|
||||
|
|
|
|||
|
|
@ -1,52 +0,0 @@
|
|||
function bxor2 ( [byte] $a, [byte] $b )
|
||||
{
|
||||
$out1 = $a -band ( -bnot $b )
|
||||
$out2 = ( -bnot $a ) -band $b
|
||||
$out1 -bor $out2
|
||||
}
|
||||
|
||||
function hadder ( [byte] $a, [byte] $b )
|
||||
{
|
||||
@{
|
||||
"S"=bxor2 $a $b
|
||||
"C"=$a -band $b
|
||||
}
|
||||
}
|
||||
|
||||
function fadder ( [byte] $a, [byte] $b, [byte] $cd )
|
||||
{
|
||||
$out1 = hadder $cd $a
|
||||
$out2 = hadder $out1["S"] $b
|
||||
@{
|
||||
"S"=$out2["S"]
|
||||
"C"=$out1["C"] -bor $out2["C"]
|
||||
}
|
||||
}
|
||||
|
||||
function FourBitAdder ( [byte] $a, [byte] $b )
|
||||
{
|
||||
$a0 = $a -band 1
|
||||
$a1 = ($a -band 2)/2
|
||||
$a2 = ($a -band 4)/4
|
||||
$a3 = ($a -band 8)/8
|
||||
$b0 = $b -band 1
|
||||
$b1 = ($b -band 2)/2
|
||||
$b2 = ($b -band 4)/4
|
||||
$b3 = ($b -band 8)/8
|
||||
$out1 = fadder $a0 $b0 0
|
||||
$out2 = fadder $a1 $b1 $out1["C"]
|
||||
$out3 = fadder $a2 $b2 $out2["C"]
|
||||
$out4 = fadder $a3 $b3 $out3["C"]
|
||||
@{
|
||||
"S"="{3}{2}{1}{0}" -f $out1["S"], $out2["S"], $out3["S"], $out4["S"]
|
||||
"V"=$out4["C"]
|
||||
}
|
||||
}
|
||||
|
||||
FourBitAdder 3 5
|
||||
|
||||
FourBitAdder 0xA 5
|
||||
|
||||
FourBitAdder 0xC 0xB
|
||||
|
||||
[Convert]::ToByte((FourBitAdder 0xC 0xB)["S"],2)
|
||||
|
|
@ -1,131 +0,0 @@
|
|||
$source = @'
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace RosettaCodeTasks.FourBitAdder
|
||||
{
|
||||
public struct BitAdderOutput
|
||||
{
|
||||
public bool S { get; set; }
|
||||
public bool C { get; set; }
|
||||
public override string ToString ( )
|
||||
{
|
||||
return "S" + ( S ? "1" : "0" ) + "C" + ( C ? "1" : "0" );
|
||||
}
|
||||
}
|
||||
public struct Nibble
|
||||
{
|
||||
public bool _1 { get; set; }
|
||||
public bool _2 { get; set; }
|
||||
public bool _3 { get; set; }
|
||||
public bool _4 { get; set; }
|
||||
public override string ToString ( )
|
||||
{
|
||||
return ( _4 ? "1" : "0" )
|
||||
+ ( _3 ? "1" : "0" )
|
||||
+ ( _2 ? "1" : "0" )
|
||||
+ ( _1 ? "1" : "0" );
|
||||
}
|
||||
}
|
||||
public struct FourBitAdderOutput
|
||||
{
|
||||
public Nibble N { get; set; }
|
||||
public bool C { get; set; }
|
||||
public override string ToString ( )
|
||||
{
|
||||
return N.ToString ( ) + "c" + ( C ? "1" : "0" );
|
||||
}
|
||||
}
|
||||
|
||||
public static class LogicGates
|
||||
{
|
||||
// Basic Gates
|
||||
public static bool Not ( bool A ) { return !A; }
|
||||
public static bool And ( bool A, bool B ) { return A && B; }
|
||||
public static bool Or ( bool A, bool B ) { return A || B; }
|
||||
|
||||
// Composite Gates
|
||||
public static bool Xor ( bool A, bool B ) { return Or ( And ( A, Not ( B ) ), ( And ( Not ( A ), B ) ) ); }
|
||||
}
|
||||
|
||||
public static class ConstructiveBlocks
|
||||
{
|
||||
public static BitAdderOutput HalfAdder ( bool A, bool B )
|
||||
{
|
||||
return new BitAdderOutput ( ) { S = LogicGates.Xor ( A, B ), C = LogicGates.And ( A, B ) };
|
||||
}
|
||||
|
||||
public static BitAdderOutput FullAdder ( bool A, bool B, bool CI )
|
||||
{
|
||||
BitAdderOutput HA1 = HalfAdder ( CI, A );
|
||||
BitAdderOutput HA2 = HalfAdder ( HA1.S, B );
|
||||
|
||||
return new BitAdderOutput ( ) { S = HA2.S, C = LogicGates.Or ( HA1.C, HA2.C ) };
|
||||
}
|
||||
|
||||
public static FourBitAdderOutput FourBitAdder ( Nibble A, Nibble B, bool CI )
|
||||
{
|
||||
|
||||
BitAdderOutput FA1 = FullAdder ( A._1, B._1, CI );
|
||||
BitAdderOutput FA2 = FullAdder ( A._2, B._2, FA1.C );
|
||||
BitAdderOutput FA3 = FullAdder ( A._3, B._3, FA2.C );
|
||||
BitAdderOutput FA4 = FullAdder ( A._4, B._4, FA3.C );
|
||||
|
||||
return new FourBitAdderOutput ( ) { N = new Nibble ( ) { _1 = FA1.S, _2 = FA2.S, _3 = FA3.S, _4 = FA4.S }, C = FA4.C };
|
||||
}
|
||||
|
||||
public static void Test ( )
|
||||
{
|
||||
Console.WriteLine ( "Four Bit Adder" );
|
||||
|
||||
for ( int i = 0; i < 256; i++ )
|
||||
{
|
||||
Nibble A = new Nibble ( ) { _1 = false, _2 = false, _3 = false, _4 = false };
|
||||
Nibble B = new Nibble ( ) { _1 = false, _2 = false, _3 = false, _4 = false };
|
||||
if ( (i & 1) == 1)
|
||||
{
|
||||
A._1 = true;
|
||||
}
|
||||
if ( ( i & 2 ) == 2 )
|
||||
{
|
||||
A._2 = true;
|
||||
}
|
||||
if ( ( i & 4 ) == 4 )
|
||||
{
|
||||
A._3 = true;
|
||||
}
|
||||
if ( ( i & 8 ) == 8 )
|
||||
{
|
||||
A._4 = true;
|
||||
}
|
||||
if ( ( i & 16 ) == 16 )
|
||||
{
|
||||
B._1 = true;
|
||||
}
|
||||
if ( ( i & 32 ) == 32)
|
||||
{
|
||||
B._2 = true;
|
||||
}
|
||||
if ( ( i & 64 ) == 64 )
|
||||
{
|
||||
B._3 = true;
|
||||
}
|
||||
if ( ( i & 128 ) == 128 )
|
||||
{
|
||||
B._4 = true;
|
||||
}
|
||||
|
||||
Console.WriteLine ( "{0} + {1} = {2}", A.ToString ( ), B.ToString ( ), FourBitAdder( A, B, false ).ToString ( ) );
|
||||
|
||||
}
|
||||
|
||||
Console.WriteLine ( );
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
'@
|
||||
|
||||
Add-Type -TypeDefinition $source -Language CSharpVersion3
|
||||
|
|
@ -1 +0,0 @@
|
|||
[RosettaCodeTasks.FourBitAdder.ConstructiveBlocks]::Test()
|
||||
|
|
@ -1,161 +0,0 @@
|
|||
LIBRARY ieee;
|
||||
USE ieee.std_logic_1164.all;
|
||||
|
||||
entity four_bit_adder is
|
||||
port(
|
||||
a : in std_logic_vector (3 downto 0);
|
||||
b : in std_logic_vector (3 downto 0);
|
||||
s : out std_logic_vector (3 downto 0);
|
||||
v : out std_logic
|
||||
);
|
||||
end four_bit_adder ;
|
||||
|
||||
LIBRARY ieee;
|
||||
USE ieee.std_logic_1164.all;
|
||||
|
||||
entity fa is
|
||||
port(
|
||||
a : in std_logic;
|
||||
b : in std_logic;
|
||||
ci : in std_logic;
|
||||
co : out std_logic;
|
||||
s : out std_logic
|
||||
);
|
||||
end fa ;
|
||||
|
||||
LIBRARY ieee;
|
||||
USE ieee.std_logic_1164.all;
|
||||
|
||||
entity ha is
|
||||
port(
|
||||
a : in std_logic;
|
||||
b : in std_logic;
|
||||
c : out std_logic;
|
||||
s : out std_logic
|
||||
);
|
||||
end ha ;
|
||||
|
||||
LIBRARY ieee;
|
||||
USE ieee.std_logic_1164.all;
|
||||
|
||||
entity xor_gate is
|
||||
port(
|
||||
a : in std_logic;
|
||||
b : in std_logic;
|
||||
x : out std_logic
|
||||
);
|
||||
end xor_gate ;
|
||||
|
||||
|
||||
|
||||
architecture struct of four_bit_adder is
|
||||
signal ci0 : std_logic;
|
||||
signal co0 : std_logic;
|
||||
signal co1 : std_logic;
|
||||
signal co2 : std_logic;
|
||||
|
||||
component fa
|
||||
port (
|
||||
a : in std_logic ;
|
||||
b : in std_logic ;
|
||||
ci : in std_logic ;
|
||||
co : out std_logic ;
|
||||
s : out std_logic
|
||||
);
|
||||
end component;
|
||||
begin
|
||||
ci0 <= '0';
|
||||
|
||||
i_fa0 : fa
|
||||
port map (
|
||||
a => a(0),
|
||||
b => b(0),
|
||||
ci => ci0,
|
||||
co => co0,
|
||||
s => s(0)
|
||||
);
|
||||
i_fa1 : fa
|
||||
port map (
|
||||
a => a(1),
|
||||
b => b(1),
|
||||
ci => co0,
|
||||
co => co1,
|
||||
s => s(1)
|
||||
);
|
||||
i_fa2 : fa
|
||||
port map (
|
||||
a => a(2),
|
||||
b => b(2),
|
||||
ci => co1,
|
||||
co => co2,
|
||||
s => s(2)
|
||||
);
|
||||
i_fa3 : fa
|
||||
port map (
|
||||
a => a(3),
|
||||
b => b(3),
|
||||
ci => co2,
|
||||
co => v,
|
||||
s => s(3)
|
||||
);
|
||||
|
||||
end struct;
|
||||
|
||||
|
||||
architecture struct of fa is
|
||||
signal c1 : std_logic;
|
||||
signal c2 : std_logic;
|
||||
signal s1 : std_logic;
|
||||
|
||||
component ha
|
||||
port (
|
||||
a : in std_logic ;
|
||||
b : in std_logic ;
|
||||
c : out std_logic ;
|
||||
s : out std_logic
|
||||
);
|
||||
end component;
|
||||
begin
|
||||
co <= c1 or c2;
|
||||
|
||||
i_ha0 : ha
|
||||
port map (
|
||||
a => ci,
|
||||
b => a,
|
||||
c => c1,
|
||||
s => s1
|
||||
);
|
||||
i_ha1 : ha
|
||||
port map (
|
||||
a => s1,
|
||||
b => b,
|
||||
c => c2,
|
||||
s => s
|
||||
);
|
||||
end struct;
|
||||
|
||||
|
||||
architecture struct of ha is
|
||||
component xor_gate
|
||||
port (
|
||||
a : in std_logic;
|
||||
b : in std_logic;
|
||||
x : out std_logic
|
||||
);
|
||||
end component;
|
||||
begin
|
||||
c <= a and b;
|
||||
|
||||
i_xor_gate : xor_gate
|
||||
port map (
|
||||
a => a,
|
||||
b => b,
|
||||
x => s
|
||||
);
|
||||
end struct;
|
||||
|
||||
|
||||
architecture rtl of xor_gate is
|
||||
begin
|
||||
x <= (a and not b) or (b and not a);
|
||||
end architecture rtl;
|
||||
|
|
@ -1,45 +0,0 @@
|
|||
LIBRARY ieee;
|
||||
USE ieee.std_logic_1164.all;
|
||||
use ieee.NUMERIC_STD.all;
|
||||
|
||||
entity tb is
|
||||
end tb ;
|
||||
|
||||
|
||||
architecture struct of tb is
|
||||
signal a : std_logic_vector(3 downto 0);
|
||||
signal b : std_logic_vector(3 downto 0);
|
||||
signal s : std_logic_vector(3 downto 0);
|
||||
signal v : std_logic;
|
||||
|
||||
component four_bit_adder
|
||||
port (
|
||||
a : in std_logic_vector (3 downto 0);
|
||||
b : in std_logic_vector (3 downto 0);
|
||||
s : out std_logic_vector (3 downto 0);
|
||||
v : out std_logic
|
||||
);
|
||||
end component;
|
||||
begin
|
||||
|
||||
proc_test: process
|
||||
begin
|
||||
for x in 0 to 15 loop
|
||||
for y in 0 to 15 loop
|
||||
a <= std_logic_vector(to_unsigned(x, 4));
|
||||
b <= std_logic_vector(to_unsigned(y, 4));
|
||||
wait for 100 ns;
|
||||
end loop;
|
||||
end loop;
|
||||
wait;
|
||||
end process;
|
||||
|
||||
i_four_bit_adder : four_bit_adder
|
||||
port map (
|
||||
a => a,
|
||||
b => b,
|
||||
s => s,
|
||||
v => v
|
||||
);
|
||||
|
||||
end struct;
|
||||
Loading…
Add table
Add a link
Reference in a new issue