First version of circuit simulator (need to complete 3.30)
This commit is contained in:
parent
0a4066baf2
commit
fa03bf2f78
|
@ -0,0 +1,332 @@
|
|||
#lang sicp
|
||||
|
||||
(#%require "3_21.rkt") ;; for queue
|
||||
|
||||
(define (make-agenda) (list 0))
|
||||
|
||||
(define the-agenda (make-agenda))
|
||||
(define inverter-delay 2)
|
||||
(define and-gate-delay 3)
|
||||
(define or-gate-delay 5)
|
||||
|
||||
|
||||
;;;SECTION 3.3.4
|
||||
|
||||
;: (define a (make-wire))
|
||||
;: (define b (make-wire))
|
||||
;: (define c (make-wire))
|
||||
;: (define d (make-wire))
|
||||
;: (define e (make-wire))
|
||||
;: (define s (make-wire))
|
||||
;:
|
||||
;: (or-gate a b d)
|
||||
;: (and-gate a b c)
|
||||
;: (inverter c e)
|
||||
;: (and-gate d e s)
|
||||
|
||||
|
||||
;;NB. To use half-adder, need or-gate from exercise 3.28
|
||||
(define (half-adder a b s c)
|
||||
(let ((d (make-wire)) (e (make-wire)))
|
||||
(or-gate a b d)
|
||||
(and-gate a b c)
|
||||
(inverter c e)
|
||||
(and-gate d e s)
|
||||
'ok))
|
||||
|
||||
(define (full-adder a b c-in sum c-out)
|
||||
(let ((s (make-wire))
|
||||
(c1 (make-wire))
|
||||
(c2 (make-wire)))
|
||||
(half-adder b c-in s c1)
|
||||
(half-adder a s sum c2)
|
||||
(or-gate c1 c2 c-out)
|
||||
'ok))
|
||||
|
||||
(define (inverter input output)
|
||||
(define (invert-input)
|
||||
(let ((new-value (logical-not (get-signal input))))
|
||||
(after-delay inverter-delay
|
||||
(lambda ()
|
||||
(set-signal! output new-value)))))
|
||||
(add-action! input invert-input)
|
||||
'ok)
|
||||
|
||||
(define (logical-not s)
|
||||
(cond ((= s 0) 1)
|
||||
((= s 1) 0)
|
||||
(else (error "Invalid signal" s))))
|
||||
|
||||
;; *following uses logical-and -- see ch3support.scm
|
||||
(define (logical-and x y)
|
||||
(cond ((and (= x 1) (= y 1)) 1)
|
||||
((or
|
||||
(and (= x 0) (= y 0))
|
||||
(and (= x 0) (= y 1))
|
||||
(and (= x 1) (= y 0)))
|
||||
0)
|
||||
(else
|
||||
(error "Invalid signal" x y))))
|
||||
|
||||
(define (and-gate a1 a2 output)
|
||||
(define (and-action-procedure)
|
||||
(let ((new-value
|
||||
(logical-and (get-signal a1) (get-signal a2))))
|
||||
(after-delay and-gate-delay
|
||||
(lambda ()
|
||||
(set-signal! output new-value)))))
|
||||
(add-action! a1 and-action-procedure)
|
||||
(add-action! a2 and-action-procedure)
|
||||
'ok)
|
||||
|
||||
|
||||
(define (make-wire)
|
||||
(let ((signal-value 0) (action-procedures '()))
|
||||
(define (set-my-signal! new-value)
|
||||
(if (not (= signal-value new-value))
|
||||
(begin (set! signal-value new-value)
|
||||
(call-each action-procedures))
|
||||
'done))
|
||||
(define (accept-action-procedure! proc)
|
||||
(set! action-procedures (cons proc action-procedures))
|
||||
(proc))
|
||||
(define (dispatch m)
|
||||
(cond ((eq? m 'get-signal) signal-value)
|
||||
((eq? m 'set-signal!) set-my-signal!)
|
||||
((eq? m 'add-action!) accept-action-procedure!)
|
||||
(else (error "Unknown operation -- WIRE" m))))
|
||||
dispatch))
|
||||
|
||||
(define (call-each procedures)
|
||||
(if (null? procedures)
|
||||
'done
|
||||
(begin
|
||||
((car procedures))
|
||||
(call-each (cdr procedures)))))
|
||||
|
||||
(define (get-signal wire)
|
||||
(wire 'get-signal))
|
||||
|
||||
(define (set-signal! wire new-value)
|
||||
((wire 'set-signal!) new-value))
|
||||
|
||||
(define (add-action! wire action-procedure)
|
||||
((wire 'add-action!) action-procedure))
|
||||
|
||||
(define (after-delay delay action)
|
||||
(add-to-agenda! (+ delay (current-time the-agenda))
|
||||
action
|
||||
the-agenda))
|
||||
|
||||
(define (propagate)
|
||||
(if (empty-agenda? the-agenda)
|
||||
'done
|
||||
(let ((first-item (first-agenda-item the-agenda)))
|
||||
(first-item)
|
||||
(remove-first-agenda-item! the-agenda)
|
||||
(propagate))))
|
||||
|
||||
(define (probe name wire)
|
||||
(add-action! wire
|
||||
(lambda ()
|
||||
(newline)
|
||||
(display name)
|
||||
(display " ")
|
||||
(display (current-time the-agenda))
|
||||
(display " New-value = ")
|
||||
(display (get-signal wire)))))
|
||||
|
||||
|
||||
;;;Implementing agenda
|
||||
|
||||
(define (make-time-segment time queue)
|
||||
(cons time queue))
|
||||
(define (segment-time s) (car s))
|
||||
(define (segment-queue s) (cdr s))
|
||||
|
||||
(define (current-time agenda) (car agenda))
|
||||
(define (set-current-time! agenda time)
|
||||
(set-car! agenda time))
|
||||
|
||||
(define (segments agenda) (cdr agenda))
|
||||
(define (set-segments! agenda segments)
|
||||
(set-cdr! agenda segments))
|
||||
(define (first-segment agenda) (car (segments agenda)))
|
||||
(define (rest-segments agenda) (cdr (segments agenda)))
|
||||
|
||||
(define (empty-agenda? agenda)
|
||||
(null? (segments agenda)))
|
||||
|
||||
(define (add-to-agenda! time action agenda)
|
||||
(define (belongs-before? segments)
|
||||
(or (null? segments)
|
||||
(< time (segment-time (car segments)))))
|
||||
(define (make-new-time-segment time action)
|
||||
(let ((q (make-queue)))
|
||||
(insert-queue! q action)
|
||||
(make-time-segment time q)))
|
||||
(define (add-to-segments! segments)
|
||||
(if (= (segment-time (car segments)) time)
|
||||
(insert-queue! (segment-queue (car segments))
|
||||
action)
|
||||
(let ((rest (cdr segments)))
|
||||
(if (belongs-before? rest)
|
||||
(set-cdr!
|
||||
segments
|
||||
(cons (make-new-time-segment time action)
|
||||
(cdr segments)))
|
||||
(add-to-segments! rest)))))
|
||||
(let ((segments (segments agenda)))
|
||||
(if (belongs-before? segments)
|
||||
(set-segments!
|
||||
agenda
|
||||
(cons (make-new-time-segment time action)
|
||||
segments))
|
||||
(add-to-segments! segments))))
|
||||
|
||||
(define (remove-first-agenda-item! agenda)
|
||||
(let ((q (segment-queue (first-segment agenda))))
|
||||
(delete-queue! q)
|
||||
(if (empty-queue? q)
|
||||
(set-segments! agenda (rest-segments agenda)))))
|
||||
|
||||
(define (first-agenda-item agenda)
|
||||
(if (empty-agenda? agenda)
|
||||
(error "Agenda is empty -- FIRST-AGENDA-ITEM")
|
||||
(let ((first-seg (first-segment agenda)))
|
||||
(set-current-time! agenda (segment-time first-seg))
|
||||
(front-queue (segment-queue first-seg)))))
|
||||
|
||||
;; 3.28
|
||||
|
||||
(define (logical-or x y)
|
||||
(cond ((and (= x 0) (= y 0)) 0)
|
||||
((or
|
||||
(and (= x 0) (= y 1))
|
||||
(and (= x 1) (= y 0))
|
||||
(and (= x 1) (= y 1)))
|
||||
1)
|
||||
(else
|
||||
(error "Invalid signals" x y))))
|
||||
|
||||
(define (or-gate a1 a2 output)
|
||||
(define (or-action-procedure)
|
||||
(let ((new-value
|
||||
(logical-or (get-signal a1) (get-signal a2))))
|
||||
(after-delay or-gate-delay
|
||||
(lambda ()
|
||||
(set-signal! output new-value)))))
|
||||
(add-action! a1 or-action-procedure)
|
||||
(add-action! a2 or-action-procedure)
|
||||
'ok)
|
||||
|
||||
;;; Sample simulation
|
||||
|
||||
(#%require (only racket/base module+))
|
||||
|
||||
(module+ sample-simulation
|
||||
|
||||
(define inverter-delay 2)
|
||||
(define and-gate-delay 3)
|
||||
(define or-gate-delay 5)
|
||||
|
||||
(define input-1 (make-wire))
|
||||
(define input-2 (make-wire))
|
||||
(define sum (make-wire))
|
||||
(define carry (make-wire))
|
||||
|
||||
(probe 'sum sum)
|
||||
(probe 'carry carry)
|
||||
|
||||
(half-adder input-1 input-2 sum carry)
|
||||
(set-signal! input-1 1)
|
||||
(propagate)
|
||||
|
||||
(set-signal! input-2 1)
|
||||
(propagate))
|
||||
|
||||
(module+ test
|
||||
(#%require rackunit)
|
||||
|
||||
(define (or-test or-gate)
|
||||
(define input-1 (make-wire))
|
||||
(define input-2 (make-wire))
|
||||
(define output (make-wire))
|
||||
(define or-gate-delay 5)
|
||||
|
||||
(or-gate input-1 input-2 output)
|
||||
|
||||
(set-signal! input-1 0)
|
||||
(set-signal! input-2 0)
|
||||
(propagate)
|
||||
(check-equal? (get-signal output) 0)
|
||||
|
||||
(set-signal! input-1 1)
|
||||
(set-signal! input-2 0)
|
||||
(propagate)
|
||||
(check-equal? (get-signal output) 1)
|
||||
|
||||
(set-signal! input-1 0)
|
||||
(set-signal! input-2 1)
|
||||
(propagate)
|
||||
(check-equal? (get-signal output) 1)
|
||||
|
||||
(set-signal! input-1 1)
|
||||
(set-signal! input-2 1)
|
||||
(propagate)
|
||||
(check-equal? (get-signal output) 1))
|
||||
|
||||
|
||||
(test-case "Or gate 3.28"
|
||||
(or-test or-gate)))
|
||||
|
||||
;; 3.29 Or gate from and gates
|
||||
|
||||
;; a or b
|
||||
;; (not (not (a or b)))
|
||||
;; (not ((not a) and (not b)))
|
||||
|
||||
(define (or-from-and-gates a b output)
|
||||
(let ((c (make-wire))
|
||||
(d (make-wire))
|
||||
(e (make-wire)))
|
||||
(inverter a c)
|
||||
(inverter b d)
|
||||
(and-gate c d e)
|
||||
(inverter e output)
|
||||
'ok))
|
||||
|
||||
(module+ test
|
||||
|
||||
(test-case "Or gate from and gates 3.29"
|
||||
(or-test or-from-and-gates)))
|
||||
|
||||
;; 3.30 Ripple adder
|
||||
(define (ripple-carry-adder a b s c)
|
||||
(define (add-full-adder a b s c)
|
||||
(if (and (pair? a)
|
||||
(pair? b)
|
||||
(pair? s))
|
||||
(let ((ci (make-wire)))
|
||||
(full-adder (car a)
|
||||
(car b)
|
||||
c
|
||||
(car s)
|
||||
ci)
|
||||
(add-full-adder (cdr a)
|
||||
(cdr b)
|
||||
(cdr s)
|
||||
ci))))
|
||||
(if (= (length a)
|
||||
(length b)
|
||||
(length s))
|
||||
(add-full-adder a b s c)))
|
||||
|
||||
;;(module+ ripple-example
|
||||
;;(define
|
||||
|
||||
;; EXERCISE 3.31
|
||||
;: (define (accept-action-procedure! proc)
|
||||
;: (set! action-procedures (cons proc action-procedures)))
|
||||
|
||||
|
Loading…
Reference in New Issue