June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,52 @@
// version 1.1.51
val Boolean.I get() = if (this) 1 else 0
val Int.B get() = this != 0
class Nybble(val n3: Boolean, val n2: Boolean, val n1: Boolean, val n0: Boolean) {
fun toInt() = n0.I + n1.I * 2 + n2.I * 4 + n3.I * 8
override fun toString() = "${n3.I}${n2.I}${n1.I}${n0.I}"
}
fun Int.toNybble(): Nybble {
val n = BooleanArray(4)
for (k in 0..3) n[k] = ((this shr k) and 1).B
return Nybble(n[3], n[2], n[1], n[0])
}
fun xorGate(a: Boolean, b: Boolean) = (a && !b) || (!a && b)
fun halfAdder(a: Boolean, b: Boolean) = Pair(xorGate(a, b), a && b)
fun fullAdder(a: Boolean, b: Boolean, c: Boolean): Pair<Boolean, Boolean> {
val (s1, c1) = halfAdder(c, a)
val (s2, c2) = halfAdder(s1, b)
return s2 to (c1 || c2)
}
fun fourBitAdder(a: Nybble, b: Nybble): Pair<Nybble, Int> {
val (s0, c0) = fullAdder(a.n0, b.n0, false)
val (s1, c1) = fullAdder(a.n1, b.n1, c0)
val (s2, c2) = fullAdder(a.n2, b.n2, c1)
val (s3, c3) = fullAdder(a.n3, b.n3, c2)
return Nybble(s3, s2, s1, s0) to c3.I
}
const val f = "%s + %s = %d %s (%2d + %2d = %2d)"
fun test(i: Int, j: Int) {
val a = i.toNybble()
val b = j.toNybble()
val (r, c) = fourBitAdder(a, b)
val s = c * 16 + r.toInt()
println(f.format(a, b, c, r, i, j, s))
}
fun main(args: Array<String>) {
println(" A B C R I J S")
for (i in 0..15) {
for (j in i..minOf(i + 1, 15)) test(i, j)
}
}

View file

@ -0,0 +1,69 @@
###---------------------------
# Program: 4 Bit Adder - Ring
# Author: Bert Mariani
# Date: 2018-02-28
#
# Bit Adder: Input A B Cin
# Output S Cout
#
# A ^ B => axb XOR gate
# axb ^ C => Sout XOR gate
# axb & C => d AND gate
#
# A & B => anb AND gate
# anb | d => Cout OR gate
#
# Call Adder for number of bit in input fields
###-------------------------------------------
### 4 Bits
Cout = "0"
OutputS = "0000"
InputA = "0101"
InputB = "1101"
See "InputA:.. "+ InputA +nl
See "InputB:.. "+ InputB +nl
BitsAdd(InputA, InputB)
See "Sum...: "+ Cout +" "+ OutputS +nl+nl
###-------------------------------------------
### 32 Bits
Cout = "0"
OutputS = "00000000000000000000000000000000"
InputA = "01010101010101010101010101010101"
InputB = "11011101110111011101110111011101"
See "InputA:.. "+ InputA +nl
See "InputB:.. "+ InputB +nl
BitsAdd(InputA, InputB)
See "Sum...: "+ Cout +" "+ OutputS +nl+nl
###-------------------------------
Func BitsAdd(InputA, InputB)
nbrBits = len(InputA)
for i = nbrBits to 1 step -1
A = InputA[i]
B = InputB[i]
C = Cout
S = Adder(A,B,C)
OutputS[i] = "" + S
next
return
###------------------------
Func Adder(A,B,C)
axb = A ^ B
Sout = axb ^ C
d = axb & C
anb = A & B
Cout = anb | d ### Cout is global
return(Sout)
###------------------------