-
Notifications
You must be signed in to change notification settings - Fork 1
/
signal.rkt
155 lines (133 loc) · 3.7 KB
/
signal.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
;;
;; 书中的代码定义
;;
#lang sicp
(#%provide (all-defined))
(#%require "./agenda.rkt")
(define inverter-delay 2)
(define and-gate-delay 3)
(define or-gate-delay 5)
;; 非逻辑
(define (logical-not s)
(cond ((= s 0) 1)
((= s 1) 0)
(else (error "Invalid signal -- logical-not" s))))
;; 非门
(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-and s1 s2)
(cond ( (= s1 0) (if (= s2 0) 0 0) )
( (= s1 1) (if (= s2 0) 0 1) )
( else (error "Invalid signal -- logical-and" s1 s2) )
))
;; 与门
(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 (logical-or s1 s2)
(cond ( (= s1 0) (if (= s2 0) 0 1) )
( (= s1 1) (if (= s2 0) 1 1) )
( else (error "Invalid signal -- logical-or" s1 s2) )
))
;; 或门
(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)
;; 半加器
(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 (make-wire)
(make-wire-with-name 'UNKNOWN))
;; 构造 wire, 底下是返回了一个 dispatch
(define (make-wire-with-name wire-name)
(let ( (signal-value 0)
(action-procedures '()) )
(define (set-my-signal! new-value)
; for debug
; (display " GGG ")
; (display wire-name)
; (display " ")
; (display new-value)
; (newline)
(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
)
)
;; procedures 是一个列表, call-each 会调用其中的每一个函数
(define (call-each procedures)
(if (null? procedures)
'done
(begin
((car procedures))
(call-each (cdr procedures)))))
;; 获取 wire
(define (get-signal wire)
(wire 'get-signal))
;; 写入 wire
(define (set-signal! wire new-value)
((wire 'set-signal!) new-value))
;; 监听 wire
(define (add-action! wire new-procedure)
((wire 'add-action!) new-procedure))
;; 监听 wire 并用 display 打印东西
(define (probe name wire)
(add-action! wire
(lambda ()
(display name)
(display " ")
(display (current-time the-agenda))
(display " New-value = ")
(display (get-signal wire))
(newline) ;; \n
)))