Advertisement
Guest User

Untitled

a guest
May 8th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns chipper.examples
  2.   (:use chipper.core
  3.         chipper.gates
  4.         chipper.utils))
  5.  
  6. ;; The Chipper DSL describes a simple DSL to build logic gates from basic logic operators. Once a gate is defined it can be used in clojure.
  7.  
  8. ;; we can use basic logic gates defined in chipper.gates
  9. ;; Remember that we cannot put DSL expressions like gates or primitives as arguments of already built gates. The DSL is for describing new gates and primitives and not for chaining them in pure clojure
  10. ;; since they return lists but take distinct arguments as inputs (It is not impossible though, we can access the list values...it just isn't designed for this purpose)
  11.  
  12. (and* 0 0) ;; [0]
  13. (and* 1 1) ;; [1]
  14. (and* 1 0) ;; [0]
  15.  
  16. ;; now let us define our own logic gate with this DSL. As a simple example we can use a half adder. A half adder adds two bits without an incoming carry and returns the result and an oiutcoming carry value
  17. ;; A half adder is simple: we XOR a and b and get the output bit s. Then we AND a and b and get the carry bit c.
  18.  
  19. (defgate halfadd* [a b] => [c s]
  20.   (xor* [a b] => [s])
  21.   (and* [a b] => [c]))
  22.  
  23. ;; we now can run our new gate
  24. ;; the expected table is
  25. ;; a   b   s   c
  26. ;; 0   0   0   0
  27. ;; 1   0   1   0
  28. ;; 0   1   1   0
  29. ;; 1   1   0   1
  30.  
  31. (halfadd* 0 0) ;; [0 0] first value is c second is s
  32. (halfadd* 1 0) ;; [0 1]
  33. (halfadd* 0 1) ;; [0 1]
  34. (halfadd* 1 1) ;; [1 0]
  35.  
  36. ;; lets build a full adder. It is basically like the half adder but it accepts an incoming carry value
  37.  
  38. (defgate fulladd* [a b cin] => [c s]
  39.   (xor* [a b] => [ab1])
  40.   (and* [ab1 cin] => [abcin])
  41.   (and* [a b] => [ab2])
  42.   (or* [ab2 abcin] => [c])
  43.   (xor* [ab1 cin] => [s]))
  44.  
  45. ;; we now can run our new gate
  46. ;; the expected table is
  47. ;; a   b   cin   s   c
  48. ;; 0   0   0     0   0
  49. ;; 0   0   1     0   1
  50. ;; 0   1   0     0   1
  51. ;; 0   1   1     1   0
  52. ;; 1   0   0     0   1
  53. ;; 1   0   1     1   0
  54. ;; 1   1   0     1   0
  55. ;; 1   1   1     1   1
  56.  
  57. (fulladd* 0 0 0) ;; [0 0] first value is s second is c
  58. (fulladd* 0 0 1) ;; [0 1]
  59. (fulladd* 0 1 0) ;; [0 1]
  60. (fulladd* 0 1 1) ;; [1 0]
  61. (fulladd* 1 0 0) ;; [0 1]
  62. (fulladd* 1 0 1) ;; [1 0]
  63. (fulladd* 1 1 0) ;; [1 0]
  64. (fulladd* 1 1 1) ;; [1 1]
  65.  
  66. ;; Chipper does not come with high/low primitives which represent a 1 and 0. This is not necessary but is useful for understanding the concept behind this DSL
  67. ;; high* maps from [] to [1]
  68. ;; low* maps from [] to [0]
  69. ;; Remember that these primitives extend the DSL and not our clojure context, therefore we cannot put high and low as arguments of built logic gate like and*
  70.  
  71. (defprimitive high* [] => [out]
  72.   [1])
  73.  
  74. (defprimitive low* [] => [out]
  75.   [0])
  76.  
  77. ;; lets build a 4 bit adder, we use the low* primitive as the incoming carry (0) just for showcasing the usage. we could use a halfadder or just the correct nand value (nand is primitive
  78. ;; wich makes this DSL functionally complete, everything is made of nands)
  79.  
  80. (defgate fourbitadd* [x1 x2 x3 x4 y1 y2 y3 y4] => [c s1 s2 s3 s4]
  81.   (low* [] => [c4])
  82.   (fulladd* [x4 y4 c4] => [c3 s4])
  83.   (fulladd* [x3 y3 c3] => [c2 s3])
  84.   (fulladd* [x2 y2 c2] => [c1 s2])
  85.   (fulladd* [x1 y1 c1] => [c s1]))
  86.  
  87. ;; correct values can be looked up on the internet or just calculated
  88. (fourbitadd* 0 0 0 0, 0 0 0 0) ;; the comma is interpreted as a space, therefore it can be used for a better visualization [c=0 res = 0 0 0 0]
  89. (fourbitadd* 1 0 0 0, 1 0 0 0) ;; [c=1 res = 0 0 0 0]
  90. (fourbitadd* 0 1 1 1, 0 0 0 1) ;; [c=0 res = 1 0 0 0]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement