Posts

Showing posts from November, 2026

An Easy Way To Remember Logic Gates Outputs

 When we think of multiple inputs logic gates, it is better to use a simple rule to find output of a logic gate. We will talk about six logic gates AND, NAND, OR, NOR, XOR, XNOR , one by one. AND Gate: As we know, output of an AND gate is 1 if all inputs are 1. So, we can say that if any bit in the input is 0 then the output will be 0. NAND Gate: As we know, NAND is NOT AND i.e. complement of an AND gate. So, if any bit in the input is 0 then the output will be 1. OR Gate: As we know, output of an OR gate is 1 if any input is 1. NOR Gate: As we know, NOR is NOT OR i.e. complement of an OR gate. So, if all inputs are 0 then the output will be 1. XOR Gate: If odd number of inputs are 1 then the output will be 1. XNOR Gate: If even number of inputs are 1 then the output will be 1. Happy reading and Thank you. Meet you soon.

Programming An FPGA Is Joy

 When we learn an HDL to program an FPGA by following examples from books and online. We always see ports like 3 downto 0, 7 downto 0. In this way we use same switches or leds again and again which might make a few switches unusable in the near future and remaining switches will be intact. So, in place of 3 downto 0 we can use 5 downto 2,15 downto 12 and so on. Another point is that we can name ports as sw1, sw2 and so on. But there is difficulty in port map to assign each switch individually. So, we should use array like naming sw[0], sw[1] and so on which helps in port map. I think that using a wrapper file for mapping ports in a constraint file is far better than directly editing a constraint file.  

Geometric Progression

  Geometric progression has a common ratio between two adjacent terms.  Here we are going to write a file named gp.tcl and then we will run it as, tclsh gp.tcl  #gp.tcl # Geometric progression # a = initial term # n = nth term # r = common ratio proc nthGP {a r n} { set an [expr {$a * pow($r, ($n - 1))}]; return $an; } proc sumGP {a r n} { set sum 0; if {$r > 1} { set sum [expr {($a * ( pow($r, $n) - 1)) / ($r - 1)}]; } elseif {$r < 1} { set sum [expr {($a * (1 - pow($r,$n))) / (1 - $r)}]; } return $sum; } #Examples puts "5th term of a GP with  r = 1.5 and a = 2: [nthGP 2 1.5 5]"; puts "Sum of 5 terms of a GP with  r = 1.5 and a = 2: [sumGP 2 1.5 5]"; puts "5th term of a GP with  r = 0.5 and a = 2: [nthGP 2 0.5 5]"; puts "Sum of 5 terms of a GP with  r = 0.5 and a = 2: [sumGP 2 0.5 5]"; Happy reading and thank you. Meet you soon.

Finding The Number Of Digits In A Number Part 2

  Finding the number of digits in a number using math functions. Here we are going to write a file named digitcountp2.tcl and then we will run it as,  tclsh digitcountp2.tcl #digitcountp2.tcl # The number of digits in a number. proc dcnt {num} { set number $num; set digicnt 0; if {$number != 0} { if {$number < 0} { set number [expr {$number * -1}]; } set digicnt [expr {floor(log10($number) + 1)}]; } return $digicnt; } #Examples puts "Number of digits in 00: [dcnt 00]"; puts "Number of digits in 2019: [dcnt 2019]"; puts "Number of digits in -123: [dcnt -123]"; Happy reading and thank you. Meet you soon.

Finding The Number Of Digits In A Number Part 1

 Do you need to find the number of digits in a number ?  Here we are going to write a file named digitcountp1.tcl and then we will run it as,  tclsh digitcountp1.tcl  #digitcountp1.tcl # This procedure counts the number of digits # in a number. proc dcnt {num} { set number $num; set digicnt 0; if {$number != 0} { if {$number < 0} { set number [expr {$number * -1}]; } while {$number != 0} { incr digicnt; set number [expr {$number / 10}]; } } return $digicnt; } Happy reading and thank you. Meet you soon.

The Maximum Of Numbers

 The maximum of a list of numbers is the largest number among that numbers.  Here we are going to write a file named max.tcl and then we will run it as, tclsh max.tcl  #max.tcl # proc max {args} { set max [lindex $args 0]; foreach num $args { if {$num > $max} { set max $num; } } return $max; } #Examples # puts "The maximum out of 5, 18 and 200: [max 5 18 200]"; puts "The maximum out of 201, 5, 18 and 200: [max 201 5 18 200]"; puts "The maximum out of 5, 18, 200 and 300: [max 5 18 200 300]"; Happy reading and thank you. Meet you soon.

Fibonacci Numbers

 In Fibonacci Numbers , the next number is the sum of previous two numbers.   Here we are going to write a file named fib.tcl and then we will run it as, tclsh fib.tcl  #fib.tcl # proc nthFib {n} { #Base conditions if {$n == 1} { return 0; } elseif {$n == 2} { return 1; } elseif {$n > 2} { set a 0; set b 1; set sum 0; for {set i 2} {$i != $n} {incr i} { set sum [expr {$a + $b}]; set a $b; set b $sum; } return $b; } } #Examples puts "1st number in the fibonacci numbers: [nthFib 1]"; puts "2nd number in the fibonacci numbers: [nthFib 2]"; puts "5th number in the fibonacci numbers: [nthFib 5]"; puts "8th number in the fibonacci numbers: [nthFib 8]"; puts "11th number in the fibonacci numbers: [nthFib 11]"; Happy reading and thank you. Meet you soon.

GCD Through Euclidean Algorithm

GCD is the greatest number which can divide any two numbers without a remainder.  Here we are going to write a file named gcd.tcl and then we will run it as, tclsh gcd.tcl  # gcd.tcl # proc gcd {first second} { set a [set b 0]; if {$first >= $second} { set a $first; set b $second; } else { set a $second; set b $first; } while {1} { set m [expr {$a % $b}]; if {$m == 0} { break; } set a $b; set b $m; } return $b; } #Examples # puts "GCD of 633 and 142 is: [gcd 633 142]"; puts "GCD of 12 and 42 is: [gcd 12 42]"; Happy reading and thank you. Meet you soon.

Average Mean

  Average Mean explains about the average of a list of numbers.  Here we are going to write a file named mean.tcl and then we will run it as, tclsh mean.tcl  # args - list of numbers # proc mean {args} { # Number of elements in the list set num 0; # The sum of numbers in the list set sum 0.0; foreach number $args { incr num; set sum [expr {$sum + $number}]; } # The mean return [expr {$sum / $num}]; } #An example puts "The mean of 2, 4, 6, 8, 20, 50 and 70 is: [mean 2 4 6 8 20 50 70]"; Happy reading and thank you. Meet you soon.

Arithmetic Progressions

  Arithmetic progressions explains about a series with a common difference.   Here we are going to write a file named ap.tcl and then we will run it as, tclsh ap.tcl  # a = first term # an = nth term # d = common difference # # nth term of a series  proc nthAP {a d n} { set an [expr {$a + ($n - 1) * $d}]; return $an;  } # Sum of n terms of a series proc sumAP {a d n} { set sum [expr {0.5 * $n * ($a + [nthAP $a $d $n])}]; return $sum; } puts "a=5, d=2, n=6, an: [nthAP 5 2 6]"; puts "a=5, d=2, n=6, sum: [sumAP 5 2 6]"; Happy reading and thnk you. Meet you soon.

Aliquot Sum

  Aliquot Sum explains about the sum of proper divisors of a number excluding that number.  Here we are going to write a file named aliquot_sum.tcl and then we will run it as, tclsh aliquot_sum.tcl  proc aliquotSum {n} { set sum 0; for {set i 1} {$i <= $n/2} {incr i} { if {$n % $i == 0} { # puts $i; set sum [expr {$sum + $i}]; } } return $sum; } puts "Aliquot Sum of 30: [aliquotSum 30]"; puts "Aliquot Sum of 31: [aliquotSum 31]"; Happy reading and thank you. Meet you soon.

Variable Arguments In TCL

 Here we are going to write a file named addall.tcl and then we will run it as, tclsh addall.tcl   proc addall {args} { set sum 0; if {[llength $args] != 0} { foreach num $args { set sum [expr {$sum + $num}]; } } return $sum; } puts "Sum: [addall 2 3]"; puts "Sum: [addall 1]"; Happy reading and thank you. Meet you soon.

The proc Command In TCL

 Here we are going to write a file named proccmd.tcl and then we will run it as, tclsh proccmd.tcl  #exponent proc exp {base pow} { set val [expr {$base ** $pow}]; return $val; } puts "2 raised to the power of 3 is [exp 2 3]"; #factorial  proc fac {num} { if {$num <= 1} {  return 1; } return [expr {$num * [fac [expr {$num - 1}]]}];   } puts "Factorial of 5 is [fac 10]"; #fibonacci proc fib {num} { set num1 0; set num2 1; puts $num1; puts $num2; for {set idx 2} {$idx < $num} {incr idx} { set nxt [expr {$num1 + $num2}]; puts $nxt; set num1 $num2; set num2 $nxt; } } puts "10 numbers in a fibonacci series:"; fib 10; Happy reading and thank you. Meet you soon.