home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / BURKS / LANGUAGE / PROLOG / INTRO / bitstrm.pl < prev    next >
Text File  |  1994-02-08  |  4KB  |  217 lines

  1. /*  BITSTREAM.PL  */
  2.  
  3.  
  4. /*
  5. This program demonstrates how to use Prolog to simulate digital
  6. circuits. In effect, this is a demonstration of how to implement another
  7. logical system in Prolog.
  8.  
  9.  
  10. Circuit layout is specified by naming the wires between components. In
  11. the example below, these names are a, b, c, d, x, y.
  12.  
  13.  
  14. The dynamic behaviour of a circuit is given by specifying the voltage
  15. levels on the wires, as a series of clauses for 'spec':
  16.  
  17.     spec( W, Expression ):
  18.         The voltage on wire W has the value given by Expression.
  19.  
  20.  
  21. The syntax of expressions:
  22.     Expression ::= Expression + Expression
  23.     Expression ::= Expression - Expression
  24.     Expression ::= Primitive
  25.  
  26.     Primitive ::= Number
  27.     Primitive ::= Wire
  28.     Primitive ::= Wire lag Time
  29.  
  30.  
  31. A primitive which is a wire name denotes the voltage on that wire in
  32. the current clock cycle. A primitive of the form (wire lag time) denotes
  33. the voltage on that wire 'time' cycles ago. 'time' must be a number.
  34.  
  35.  
  36. You can have conditional clauses of the form
  37.     spec( W, Expression ) if Condition
  38. When looking for a value for W, the system will evaluate Condition, and
  39. only use the clause for spec if it is true. Make sure you have enough
  40. conditions to cover all cases. The Condition is two Expressions compared
  41. by one of =, \=, >, <, >=, =<.             
  42.  
  43.  
  44.  
  45. To specify initial values, use 'init':
  46.     init( W, Time, Number )
  47. means that wire W has voltage Number at time T.
  48.  
  49.  
  50. To use the simulator, call
  51.     eval_wire( W+, Time+, V- )
  52. which will give the value on W at Time, or
  53.     run( W+ )
  54. which will display a sequence of values for W.
  55.  
  56.  
  57. Note: it does not check for inconsistencies.
  58. */
  59.  
  60.  
  61. /*
  62. Define 'lag' and 'if' for the specification clauses.
  63. */
  64. :- op( 10, xfx, lag ).
  65. :- op( 255, xfx, if ).
  66.  
  67.  
  68. /*  signal( W+, Time+, V- ):
  69.         Succeeds if there is a spec clause for W at time T, in which
  70.         case value is V. Otherwise fails.
  71. */
  72. signal( Wire, _, V ) :-
  73.     clause( spec( Wire, V ), true ),
  74.     !.
  75.  
  76. signal( Wire, N, V ) :-
  77.     clause( (spec( Wire, V ) if Cond), true ),
  78.     eval_cond( Cond, N ),
  79.     !.
  80.  
  81.  
  82. /*  eval_cond( Cond+, Time+ ):
  83.         Succeeds if Cond is true at Time, otherwise fails.
  84. */
  85. eval_cond( A>B, N ) :-
  86.     !,
  87.     eval_expr( A, N, VA ),
  88.     eval_expr( B, N, VB ),
  89.     VA > VB.
  90.  
  91. eval_cond( A>=B, N ) :-
  92.     !,
  93.     eval_expr( A, N, VA ),
  94.     eval_expr( B, N, VB ),
  95.     VA >= VB.
  96.  
  97. eval_cond( A<B, N ) :-
  98.     !,
  99.     eval_expr( A, N, VA ),
  100.     eval_expr( B, N, VB ),
  101.     VA < VB.
  102.  
  103. eval_cond( A=<B, N ) :-
  104.     !,
  105.     eval_expr( A, N, VA ),
  106.     eval_expr( B, N, VB ),
  107.     VA =< VB.
  108.  
  109. eval_cond( A=B, N ) :-
  110.     !,
  111.     eval_expr( A, N, VA ),
  112.     eval_expr( B, N, VB ),
  113.     VA = VB.
  114.  
  115. eval_cond( A\=B, N ) :-
  116.     !,
  117.     eval_expr( A, N, VA ),
  118.     eval_expr( B, N, VB ),
  119.     VA \= VB.
  120.  
  121.  
  122. /*  eval_wire( W+, Time+, V- ):
  123.         V is value of W at Time.
  124.         Uses memo-clauses to avoid recomputing values.                  
  125. */
  126. eval_wire( Wire, N, V ) :-
  127.     clause( init( Wire, N, V ), true ),
  128.     !.
  129.  
  130. eval_wire( Wire, N, V ) :-
  131.     clause( memo( Wire, N, V ), true ),
  132.     !.
  133.  
  134. eval_wire( Wire, N, V ) :-
  135.     signal( Wire, N, Expr ),
  136.     eval_expr( Expr, N, V ),
  137.     asserta( memo(Wire,N,V) ).
  138.  
  139.  
  140. /*  eval_expr( Expression+, Time+, V- ):
  141.         V is value of Expression at Time.
  142. */
  143. eval_expr( A, N, V ) :-
  144.     atom( A ),
  145.     !,
  146.     eval_wire( A, N, V ).
  147.  
  148. eval_expr( IR, N, IR ) :-
  149.     atomic( IR ),
  150.     !.
  151.  
  152. eval_expr( A lag L, N, V ) :-
  153.     !,
  154.     NL is N - L,
  155.     eval_expr( A, NL, V ).
  156.  
  157. eval_expr( A+B, N, V ) :-
  158.     eval_expr( A, N, VA ),
  159.     eval_expr( B, N, VB ),
  160.     V is VA + VB.
  161.  
  162. eval_expr( A-B, N, V ) :-
  163.     eval_expr( A, N, VA ),
  164.     eval_expr( B, N, VB ),
  165.     V is VA - VB.
  166.  
  167.  
  168. /*  go( W+ ):
  169.         Display successive values on wire W.
  170. */
  171. go( Wire ) :-
  172.     retractall( memo(_,_,_) ),
  173.     run( Wire, 1 ).
  174.  
  175.  
  176. /*  run( W+, Time+ ):
  177.         Display values on wire W from Time onwards.             
  178. */
  179. run( Wire, N ) :-
  180.     eval_wire( Wire, N, V ),
  181.     write( Wire ), write( ' at ' ), write( N ), write( ' = ' ), write( V ), nl,
  182.     Next is N + 1,
  183.     run( Wire, Next ).
  184.  
  185.  
  186. /*
  187. Test data.
  188. ----------
  189.  
  190. Describes the bitstream circuit in Sony's Principles of Audio and
  191. Compact Discs, 2nd edition, 1992.
  192. */
  193.  
  194.  
  195. spec( x, 0.6 ).
  196.  
  197. spec( a, x-d ).
  198.  
  199. spec( b, a+(b lag 1) ).
  200.  
  201. spec( c, 1 ) if b > 0.
  202. spec( c, 0 ) if b =< 0.
  203.  
  204. spec( y, c lag 1 ).
  205.  
  206. spec( d, 1 ) if y = 1.
  207. spec( d, -1 ) if y = 0.
  208.  
  209.  
  210. init( d, 1, 0 ).
  211.  
  212. init( b, 0, 0 ).
  213.  
  214.  
  215. test :-
  216.     go( c ).              
  217.