CS220 Programming Principles

24
CS220 Programming Principles 프프프프프프 프프 2002 프프프프 Class 12: Logic Circuit Sim ulation 프프

description

CS220 Programming Principles. 프로그래밍의 이해 2002 가을학기 Class 12: Logic Circuit Simulation 한 태숙. Event-driven Simulation. Example: Digital Circuit function box : logic gates wire : connections delay: characteristics Model - PowerPoint PPT Presentation

Transcript of CS220 Programming Principles

Page 1: CS220 Programming Principles

CS220Programming Principles

프로그래밍의 이해 2002 가을학기Class 12: Logic Circuit Simulation

한 태숙

Page 2: CS220 Programming Principles

2

Event-driven Simulation

Example: Digital Circuit– function box : logic gates– wire : connections– delay: characteristics

Model– wire : computational objects which “hold” the

signals -> make-wire– function boxes : procedure that enforce the correct

relationships among signals

Page 3: CS220 Programming Principles

3

Half Adder

(define a (make-wire))

(define b (make-wire))

(define d (make-wire))

(or-gate a b d)

A

B C

SD

E

Page 4: CS220 Programming Principles

4

Half-Adder

(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))

Page 5: CS220 Programming Principles

5

Full Adder

(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))

Page 6: CS220 Programming Principles

6

Wire + Delay

Constructor : (make-wire) (get-signal <wire>)

– returns the current value of the signal on the wire (set-signal! <wire> <new-value>)

– changes the value of the signal on the wire (add-action! <wire> <procedure of no args>)

– asserts that the designated procedure should be run whenever the signal on the wire changes value

after-delay : simulates propagation delay

Page 7: CS220 Programming Principles

7

Gate Not(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))))

Page 8: CS220 Programming Principles

8

Gate AND(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)

Page 9: CS220 Programming Principles

9

Gate OR : exercise 3.28(define (or-gate o1 o2 output)

(define (or-action-procedure)

(let ((new-value

(logical-or (get-signal o1)

(get-signal o2))))

(after-delay or-gate-delay

(lambda ()

(set-signal! output

new-value)))))

(add-action! o1 or-action-procedure)

(add-action! o2 or-action-procedure)

'ok)

Page 10: CS220 Programming Principles

10

Logical And, Logical Or(define (logical-and s1 s2)

(cond ((and (= s1 1)(= s2 1)) 1)

((and (= s1 0)(= s2 1)) 0)

((and (= s1 1)(= s2 0)) 0)

((and (= s1 0)(= s2 0)) 0)

(else (error "Invalid signal" s))))

(define (logical-or s1 s2)

(cond ((and (= s1 1)(= s2 1)) 1)

((and (= s1 0)(= s2 1)) 1)

((and (= s1 1)(= s2 0)) 1)

((and (= s1 0)(= s2 0)) 0)

(else (error "Invalid signal" s))))

Page 11: CS220 Programming Principles

11

Representing Wires(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))

Page 12: CS220 Programming Principles

12

(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)))))

Page 13: CS220 Programming Principles

13

(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))

Page 14: CS220 Programming Principles

14

The Agenda (make-agenda)

– constructor: returns a new empty agenda (empty-agenda? <agenda>)

– is true if the specified agenda is empty (first-agenda-item <agenda>)

– returns the first item on the agenda (remove-first-agenda-item! <agenda>)

– modifies the agenda by removing the first item (add-to-agenda! <time> <action> <agenda>)

– modifies the agenda by adding the given action procedure to be run at the specified time

Page 15: CS220 Programming Principles

15

Implementing Delay: After-delay (current-time <agenda>)

– return the current simulation time

We will define the-agenda as agenda data structure

(define (after-delay delay action)

(add-to-agenda! (+ delay (current-time

the-agenda))

action

the-agenda))

Page 16: CS220 Programming Principles

16

Driver Program - Propagate

(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))))

Page 17: CS220 Programming Principles

17

Probe - Detecting Signal

(define (probe name wire)

(add-action! wire

(lambda () (newline)

(display name)

(display ” ”)

(display (current-time

the-agenda))

(display ” New-value= ”)

(display (get-signal

wire)))))

Page 18: CS220 Programming Principles

18

Setting Up Environment

(define the-agenda (make-agenda))

(define inverter-delay 2)

(define and-gate-delay 3)

(define or-gate-delay 5)

; circuit description

(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)

Page 19: CS220 Programming Principles

19

Running the simulation

> (set-signal! input-1 1)

'done

> (propagate)

sum 8 New-value = 1

'done

> (set-signal! input-2 1)

'done

> (propagate)

carry 11 New-value = 1

sum 16 New-value = 0

'done

Page 20: CS220 Programming Principles

20

Implementing Agenda(I)(define (make-time-segment time queue)

(cons time queue))

(define (segment-time s) (car s))

(define (segment-queue s) (cdr s))

(define (make-agenda) (list 0))

(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)))

Page 21: CS220 Programming Principles

21

Implementing Agenda(II)(define (empty-agenda? agenda)

(null? (segments agenda)))

(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)))))

Page 22: CS220 Programming Principles

22

Implementing Agenda(III)

(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)))

Page 23: CS220 Programming Principles

23

(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)))))

Page 24: CS220 Programming Principles

24

(let ((segments (segments agenda)))

(if (belongs-before? segments)

(set-segments!

agenda

(cons (make-new-time-segment time action)

segments))

(add-to-segments! segments))))