September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
|
|
@ -1,7 +1,7 @@
|
|||
;Task:
|
||||
"''Simulate''" a four-bit adder "chip".
|
||||
"''Simulate''" a four-bit adder.
|
||||
|
||||
This "chip" can be realized using four [[wp:Adder_(electronics)#Full_adder|1-bit full adder]]s.
|
||||
This design can be realized using four [[wp:Adder_(electronics)#Full_adder|1-bit full adder]]s.
|
||||
Each of these 1-bit full adders can be built with two [[wp:Adder_(electronics)#Half_adder|half adder]]s and an ''or'' [[wp:Logic gate|gate]]. Finally a half adder can be made using a ''xor'' gate and an ''and'' gate.
|
||||
The ''xor'' gate can be made using two ''not''s, two ''and''s and one ''or''.
|
||||
|
||||
|
|
|
|||
60
Task/Four-bit-adder/BASIC/four-bit-adder-1.basic
Normal file
60
Task/Four-bit-adder/BASIC/four-bit-adder-1.basic
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
100 S$ = "1100 + 1100 = " : GOSUB 400
|
||||
110 S$ = "1100 + 1101 = " : GOSUB 400
|
||||
120 S$ = "1100 + 1110 = " : GOSUB 400
|
||||
130 S$ = "1100 + 1111 = " : GOSUB 400
|
||||
140 S$ = "1101 + 0000 = " : GOSUB 400
|
||||
150 S$ = "1101 + 0001 = " : GOSUB 400
|
||||
160 S$ = "1101 + 0010 = " : GOSUB 400
|
||||
170 S$ = "1101 + 0011 = " : GOSUB 400
|
||||
180 END
|
||||
|
||||
400 A0 = VAL(MID$(S$, 4, 1))
|
||||
410 A1 = VAL(MID$(S$, 3, 1))
|
||||
420 A2 = VAL(MID$(S$, 2, 1))
|
||||
430 A3 = VAL(MID$(S$, 1, 1))
|
||||
440 B0 = VAL(MID$(S$, 11, 1))
|
||||
450 B1 = VAL(MID$(S$, 10, 1))
|
||||
460 B2 = VAL(MID$(S$, 9, 1))
|
||||
470 B3 = VAL(MID$(S$, 8, 1))
|
||||
480 GOSUB 600
|
||||
490 PRINT S$;
|
||||
|
||||
REM 4 BIT PRINT
|
||||
500 PRINT C;S3;S2;S1;S0
|
||||
510 RETURN
|
||||
|
||||
REM 4 BIT ADD
|
||||
REM ADD A3 A2 A1 A0 TO B3 B2 B1 B0
|
||||
REM RESULT IN S3 S2 S1 S0
|
||||
REM CARRY IN C
|
||||
600 C = 0
|
||||
610 A = A0 : B = B0 : GOSUB 700 : S0 = S
|
||||
620 A = A1 : B = B1 : GOSUB 700 : S1 = S
|
||||
630 A = A2 : B = B2 : GOSUB 700 : S2 = S
|
||||
640 A = A3 : B = B3 : GOSUB 700 : S3 = S
|
||||
650 RETURN
|
||||
|
||||
REM FULL ADDER
|
||||
REM ADD A + B + C
|
||||
REM RESULT IN S
|
||||
REM CARRY IN C
|
||||
700 BH = B : B = C : GOSUB 800 : C1 = C
|
||||
710 A = S : B = BH : GOSUB 800 : C2 = C
|
||||
720 C = C1 OR C2
|
||||
730 RETURN
|
||||
|
||||
REM HALF ADDER
|
||||
REM ADD A + B
|
||||
REM RESULT IN S
|
||||
REM CARRY IN C
|
||||
800 GOSUB 900 : S = C
|
||||
810 C = A AND B
|
||||
820 RETURN
|
||||
|
||||
REM XOR GATE
|
||||
REM A XOR B
|
||||
REM RESULT IN C
|
||||
900 C = A AND NOT B
|
||||
910 D = B AND NOT A
|
||||
920 C = C OR D
|
||||
930 RETURN
|
||||
45
Task/Four-bit-adder/BASIC/four-bit-adder-2.basic
Normal file
45
Task/Four-bit-adder/BASIC/four-bit-adder-2.basic
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
@% = 2
|
||||
PRINT "1100 + 1100 = ";
|
||||
PROC4bitadd(1,1,0,0, 1,1,0,0, e,d,c,b,a) : PRINT e,d,c,b,a
|
||||
PRINT "1100 + 1101 = ";
|
||||
PROC4bitadd(1,1,0,0, 1,1,0,1, e,d,c,b,a) : PRINT e,d,c,b,a
|
||||
PRINT "1100 + 1110 = ";
|
||||
PROC4bitadd(1,1,0,0, 1,1,1,0, e,d,c,b,a) : PRINT e,d,c,b,a
|
||||
PRINT "1100 + 1111 = ";
|
||||
PROC4bitadd(1,1,0,0, 1,1,1,1, e,d,c,b,a) : PRINT e,d,c,b,a
|
||||
PRINT "1101 + 0000 = ";
|
||||
PROC4bitadd(1,1,0,1, 0,0,0,0, e,d,c,b,a) : PRINT e,d,c,b,a
|
||||
PRINT "1101 + 0001 = ";
|
||||
PROC4bitadd(1,1,0,1, 0,0,0,1, e,d,c,b,a) : PRINT e,d,c,b,a
|
||||
PRINT "1101 + 0010 = ";
|
||||
PROC4bitadd(1,1,0,1, 0,0,1,0, e,d,c,b,a) : PRINT e,d,c,b,a
|
||||
PRINT "1101 + 0011 = ";
|
||||
PROC4bitadd(1,1,0,1, 0,0,1,1, e,d,c,b,a) : PRINT e,d,c,b,a
|
||||
END
|
||||
|
||||
DEF PROC4bitadd(a3&, a2&, a1&, a0&, b3&, b2&, b1&, b0&, \
|
||||
\ RETURN c3&, RETURN s3&, RETURN s2&, RETURN s1&, RETURN s0&)
|
||||
LOCAL c0&, c1&, c2&
|
||||
PROCfulladder(a0&, b0&, 0, s0&, c0&)
|
||||
PROCfulladder(a1&, b1&, c0&, s1&, c1&)
|
||||
PROCfulladder(a2&, b2&, c1&, s2&, c2&)
|
||||
PROCfulladder(a3&, b3&, c2&, s3&, c3&)
|
||||
ENDPROC
|
||||
|
||||
DEF PROCfulladder(a&, b&, c&, RETURN s&, RETURN c1&)
|
||||
LOCAL x&, y&, z&
|
||||
PROChalfadder(a&, c&, x&, y&)
|
||||
PROChalfadder(x&, b&, s&, z&)
|
||||
c1& = y& OR z&
|
||||
ENDPROC
|
||||
|
||||
DEF PROChalfadder(a&, b&, RETURN s&, RETURN c&)
|
||||
s& = FNxorgate(a&, b&)
|
||||
c& = a& AND b&
|
||||
ENDPROC
|
||||
|
||||
DEF FNxorgate(a&, b&)
|
||||
LOCAL c&, d&
|
||||
c& = a& AND NOT b&
|
||||
d& = b& AND NOT a&
|
||||
= c& OR d&
|
||||
42
Task/Four-bit-adder/Prolog/four-bit-adder.pro
Normal file
42
Task/Four-bit-adder/Prolog/four-bit-adder.pro
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
% binary 4 bit adder chip simulation
|
||||
|
||||
b_not(in(hi), out(lo)) :- !. % not(1) = 0
|
||||
b_not(in(lo), out(hi)). % not(0) = 1
|
||||
|
||||
b_and(in(hi,hi), out(hi)) :- !. % and(1,1) = 1
|
||||
b_and(in(_,_), out(lo)). % and(anything else) = 0
|
||||
|
||||
b_or(in(hi,_), out(hi)) :- !. % or(1,any) = 1
|
||||
b_or(in(_,hi), out(hi)) :- !. % or(any,1) = 1
|
||||
b_or(in(_,_), out(lo)). % or(anything else) = 0
|
||||
|
||||
b_xor(in(A,B), out(O)) :-
|
||||
b_not(in(A), out(NotA)), b_not(in(B), out(NotB)),
|
||||
b_and(in(A,NotB), out(P)), b_and(in(NotA,B), out(Q)),
|
||||
b_or(in(P,Q), out(O)).
|
||||
|
||||
b_half_adder(in(A,B), s(S), c(C)) :-
|
||||
b_xor(in(A,B),out(S)), b_and(in(A,B),out(C)).
|
||||
|
||||
b_full_adder(in(A,B,Ci), s(S), c(C1)) :-
|
||||
b_half_adder(in(Ci, A), s(S0), c(C0)),
|
||||
b_half_adder(in(S0, B), s(S), c(C)),
|
||||
b_or(in(C0,C), out(C1)).
|
||||
|
||||
b_4_bit_adder(in(A0,A1,A2,A3), in(B0,B1,B2,B3), out(S0,S1,S2,S3), c(V)) :-
|
||||
b_full_adder(in(A0,B0,lo), s(S0), c(C0)),
|
||||
b_full_adder(in(A1,B1,C0), s(S1), c(C1)),
|
||||
b_full_adder(in(A2,B2,C1), s(S2), c(C2)),
|
||||
b_full_adder(in(A3,B3,C2), s(S3), c(V)).
|
||||
|
||||
test_add(A,B,T) :-
|
||||
b_4_bit_adder(A, B, R, C),
|
||||
writef('%w + %w is %w %w \t(%w)\n', [A,B,R,C,T]).
|
||||
|
||||
go :-
|
||||
test_add(in(hi,lo,lo,lo), in(hi,lo,lo,lo), '1 + 1 = 2'),
|
||||
test_add(in(lo,hi,lo,lo), in(lo,hi,lo,lo), '2 + 2 = 4'),
|
||||
test_add(in(hi,lo,hi,lo), in(hi,lo,lo,hi), '5 + 9 = 14'),
|
||||
test_add(in(hi,hi,lo,hi), in(hi,lo,lo,hi), '11 + 9 = 20'),
|
||||
test_add(in(lo,lo,lo,hi), in(lo,lo,lo,hi), '8 + 8 = 16'),
|
||||
test_add(in(hi,hi,hi,hi), in(hi,lo,lo,lo), '15 + 1 = 16').
|
||||
63
Task/Four-bit-adder/Rust/four-bit-adder.rust
Normal file
63
Task/Four-bit-adder/Rust/four-bit-adder.rust
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
// half adder with XOR and AND
|
||||
// SUM = A XOR B
|
||||
// CARRY = A.B
|
||||
fn half_adder(a: usize, b: usize) -> (usize, usize) {
|
||||
return (a ^ b, a & b);
|
||||
}
|
||||
|
||||
// full adder as a combination of half adders
|
||||
// SUM = A XOR B XOR C
|
||||
// CARRY = A.B + B.C + C.A
|
||||
fn full_adder(a: usize, b: usize, c_in: usize) -> (usize, usize) {
|
||||
let (s0, c0) = half_adder(a, b);
|
||||
let (s1, c1) = half_adder(s0, c_in);
|
||||
return (s1, c0 | c1);
|
||||
}
|
||||
|
||||
// A = (A3, A2, A1, A0)
|
||||
// B = (B3, B2, B1, B0)
|
||||
// S = (S3, S2, S1, S0)
|
||||
fn four_bit_adder (
|
||||
a: (usize, usize, usize, usize),
|
||||
b: (usize, usize, usize, usize)
|
||||
)
|
||||
->
|
||||
// 4 bit output, carry is ignored
|
||||
(usize, usize, usize, usize)
|
||||
{
|
||||
// lets have a.0 refer to the rightmost element
|
||||
let a = a.reverse();
|
||||
let b = b.reverse();
|
||||
|
||||
// i would prefer a loop but that would abstract
|
||||
// the "connections of the constructive blocks"
|
||||
let (sum, carry) = half_adder(a.0, b.0);
|
||||
let out0 = sum;
|
||||
let (sum, carry) = full_adder(a.1, b.1, carry);
|
||||
let out1 = sum;
|
||||
let (sum, carry) = full_adder(a.2, b.2, carry);
|
||||
let out2 = sum;
|
||||
let (sum, _) = full_adder(a.3, b.3, carry);
|
||||
let out3 = sum;
|
||||
return (out3, out2, out1, out0);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let a: (usize, usize, usize, usize) = (0, 1, 1, 0);
|
||||
let b: (usize, usize, usize, usize) = (0, 1, 1, 0);
|
||||
assert_eq!(four_bit_adder(a, b), (1, 1, 0, 0));
|
||||
// 0110 + 0110 = 1100
|
||||
// 6 + 6 = 12
|
||||
}
|
||||
|
||||
// misc. traits to make our life easier
|
||||
trait Reverse<A, B, C, D> {
|
||||
fn reverse(self) -> (D, C, B, A);
|
||||
}
|
||||
|
||||
// reverse a generic tuple of arity 4
|
||||
impl<A, B, C, D> Reverse<A, B, C, D> for (A, B, C, D) {
|
||||
fn reverse(self) -> (D, C, B, A){
|
||||
return (self.3, self.2, self.1, self.0)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue