-
Notifications
You must be signed in to change notification settings - Fork 3
/
ram.clj
378 lines (303 loc) · 9.1 KB
/
ram.clj
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
(ns ram
(:require [clojure.math.combinatorics :as c]
[clojure.string :as string]))
; ex v0
; -----
(def ex-state-v0 {:charge-map {:a 1 :b 1 :c 0}
:nand-gates [{:ins [:a :b]
:out :c}]})
; state v0
; ---------------
(def empty-state {:charge-map {} :nand-gates []})
(defn charge [state wire]
(get-in state [:charge-map wire]))
(defn charges [state wires]
(map (partial charge state) wires))
(defn set-charge [state wire charge]
(assoc-in state [:charge-map wire] charge))
(defn wire-nand-gate [state a b o]
(update state :nand-gates conj {:ins [a b] :out o}))
(comment
(charges (-> empty-state
(set-charge :a 1)
(set-charge :b 0))
[:a :b])
(wire-nand-gate empty-state :a :b :c))
; trigger v0
; ----------
(defn nand-xf [a b]
(if (= a b 1) 0 1))
(comment
(nand-xf 0 0)
(nand-xf 1 1))
(defn dependent-nand-gates [state wire]
(filter (fn [{:keys [ins]}] (some #{wire} ins))
(:nand-gates state)))
(comment
(dependent-nand-gates (wire-nand-gate empty-state :a :b :c) :a))
(declare trigger-nand-gate)
(defn trigger
([state wire new-v]
(let [old-c (charge state wire)
state' (set-charge state wire new-v)
new-c (charge state' wire)]
(if (= old-c new-c)
state'
(reduce (fn [s out] (trigger-nand-gate s out))
state'
(dependent-nand-gates state' wire))))))
(defn trigger-nand-gate
[state {:keys [ins out]}]
(let [new-charge (apply nand-xf (charges state ins))]
(trigger state out new-charge)))
(defn trigger-many [state wires charges]
(reduce
(fn [acc-state [wire charge]]
(trigger acc-state wire charge))
state
(map vector wires charges)))
; not gate
; --------
(defn wire-not-gate
([state a o]
(wire-nand-gate state a a o)))
; wires v0
; ----------
(def _u (atom {}))
(defn uniq-n [k]
(swap! _u update k (fn [i] (inc (or i 0))))
(get @_u k))
(defn kw [& args]
(->> args
(map (fn [x] (if ((some-fn keyword? symbol?) x)
(name x)
x)))
(apply str)
keyword))
(defn wire
([n]
(let [i (uniq-n n)]
(if (> i 1) (kw n "#" i) n))))
(comment
[(wire :a)
(wire :a)])
; and gate
; --------
(defn wire-and-gate [state a b o]
(let [nand-o (wire (kw a b :and-nand-o))]
(-> state
(wire-nand-gate a b nand-o)
(wire-not-gate nand-o o))))
; memory-bit
; ----------
(defn wire-memory-bit
([state s i o]
(let [a (wire :a)
b (wire :b)
c (wire :c)]
(-> state
(wire-nand-gate i s a)
(wire-nand-gate a s b)
(wire-nand-gate a c o)
(wire-nand-gate b o c)))))
; wires v1
; ----------
(defn names [n r]
(mapv (fn [i] (kw n "-" i)) (range r)))
(def wires (comp (partial mapv wire) names))
(comment (wires :i 8))
; byte
; ----
(defn wire-byte [state s ins outs]
(reduce (fn [acc-state [i o]]
(wire-memory-bit acc-state s i o))
state
(map vector ins outs)))
; enabler
; -------
(defn wire-enabler
[state e ins outs]
(reduce
(fn [acc-state [in out]]
(wire-and-gate acc-state e in out))
state
(map vector ins outs)))
; register
; --------
(defn wire-register [state s e ins bits outs]
(-> state
(wire-byte s ins bits)
(wire-enabler e bits outs)))
; state v1
; --------
(defn set-charge
([state source wire charge]
(assoc-in state [:charge-map wire source] charge)))
(comment (set-charge empty-state :nand-a :w 1))
(defn charge [{:keys [charge-map]} w]
(when-let [charges (vals (charge-map w))]
(apply max charges)))
(comment
(let [s1 (-> empty-state
(set-charge :nand-a :w 1)
(set-charge :nand-b :w 0))]
(charge s1 :w)))
; trigger v1
; ----------
(defn trigger-nand-gate
[state {:keys [ins out]}]
(let [new-charge (apply nand-xf (charges state ins))]
(trigger (apply kw (conj ins out)) state out new-charge)))
(defn trigger
([state wire new-v] (trigger :repl state wire new-v))
([source state wire new-v]
(let [old-c (charge state wire)
state' (set-charge state source wire new-v)
new-c (charge state' wire)]
(if (= old-c new-c)
state'
(reduce (fn [s out] (trigger-nand-gate s out))
state'
(dependent-nand-gates state' wire))))))
; wire-bus
; --------
(defn wire-bus [state bus-wires s e bits]
(wire-register state s e bus-wires bits bus-wires))
; and-n
; -----
(defn wire-and-n [state ins out]
(let [[a b] (take 2 ins)
rem (drop 2 ins)]
(if-not (seq rem)
(wire-and-gate state a b out)
(let [w (wire (kw a b :-and))]
(wire-and-n
(wire-and-gate state a b w)
(list* w rem)
out)))))
; decoder
; -------
(def decoder-mapping (partial c/selections [0 1]))
(comment (decoder-mapping 2))
(defn wire-decoder
[state ins outs]
(let [ins-nots (mapv #(wire (kw % :-not)) ins)
state' (reduce
(fn [acc-state [in out]]
(wire-not-gate acc-state in out))
state
(map vector ins ins-nots))
state'' (reduce
(fn [acc-state [sel out]]
(let [and-ins (map-indexed
(fn [i sign]
(if (= sign 0)
(nth ins-nots i)
(nth ins i)))
sel)]
(wire-and-n acc-state and-ins out)))
state'
(map vector (decoder-mapping (count ins)) outs))]
state''))
; mar
; ---
(defn wire-mar
[state s is os first-4-outs last-4-outs]
(-> state
(wire-byte s is os)
(wire-decoder (take 4 os) first-4-outs)
(wire-decoder (drop 4 os) last-4-outs)))
; io
; --
(defn wire-io
[state io-s io-e ios decoder-o-1 decoder-o-2 register-bits]
(let [x (wire (kw decoder-o-1 decoder-o-2 :x))
s (wire (kw decoder-o-1 decoder-o-2 :s))
e (wire (kw decoder-o-1 decoder-o-2 :e))]
(-> state
(wire-and-gate decoder-o-1 decoder-o-2 x)
(wire-and-gate x io-s s)
(wire-and-gate x io-e e)
(wire-bus ios s e register-bits))))
; ram
; ---
(defn wire-ram [state mar-s mar-is io-s io-e ios]
(let [mar-os (wires :mar-o 8)
mar-first-4-outs (wires :mar-dec-f 16)
mar-last-4-outs (wires :mar-dec-l 16)
state' (wire-mar state mar-s mar-is mar-os mar-first-4-outs mar-last-4-outs)
intersections (c/cartesian-product mar-first-4-outs mar-last-4-outs)
state'' (reduce
(fn [acc-state [fw lw]]
(wire-io acc-state io-s io-e ios fw lw
(wires (kw fw lw :rb) 8)))
state'
intersections)]
state''))
; state v2
; ----
(def empty-state {:charge-map {} :in->nand-gate {}})
(defn wire-nand-gate [s a b o]
(reduce
(fn [acc-state in]
(update-in acc-state
[:in->nand-gate in]
(fn [xs] (conj (or xs []) {:ins [a b] :out o}))))
s
[a b]))
(defn dependent-nand-gates [s w]
(get-in s [:in->nand-gate w]))
; repl
; ----
(defn initialize-ram [mar-s mar-is io-s io-e ios]
(-> empty-state
(wire-ram mar-s mar-is io-s io-e ios)
(trigger-many mar-is [0 0 0 0 0 0 0 0])
(trigger-many ios [0 0 0 0 0 0 0 0])
(trigger-many [mar-s io-s io-e]
[0 0 0])))
(defn set-mar [state mar-s mar-is mar-vs]
(-> state
(trigger-many mar-is mar-vs)
(trigger mar-s 1)
(trigger mar-s 0)))
(defn handle-read [state mar-s mar-is io-e ios loc ]
(let [charge-bus-with-register (-> state
(set-mar mar-s mar-is loc)
(trigger io-e 1))
next (-> charge-bus-with-register
(trigger io-e 0))]
(println (str "> " (string/join (charges charge-bus-with-register
ios))))
next))
(defn handle-write [state mar-s mar-is io-s ios loc vs]
(let [next (-> state
(set-mar mar-s mar-is loc)
(trigger-many ios vs)
(trigger io-s 1)
(trigger io-s 0)
(trigger-many ios [0 0 0 0 0 0 0 0]))]
(println "> done")
next))
(defn ram-repl []
(println
(str "🔥 Ram Simulation: Type a command. Here's what you can do: \n"
" (read [1 0 1 0 1 0 1 0]) \n"
" (write [1 0 1 0 1 0 1 0] [1 1 1 1 1 1 1 1]) \n"
" (exit)"))
(let [mar-is (wires :mar-i 8)
ios (wires :mar-io 8)
initial-state (initialize-ram :mar-s mar-is :io-s :io-e ios)]
(loop [state initial-state]
(let [input (read-string (read-line))
cmd (first input)
args (rest input)]
(condp = cmd
'read
(recur (handle-read state :mar-s mar-is :io-e ios (first args)))
'write
(recur (handle-write state :ms mar-is :io-s ios (first args) (second args)))
'exit
(println "> Goodbye!"))))))
(comment
(ram-repl))