This commit is contained in:
Matthew Butterick 2016-05-17 09:35:21 -07:00
parent 99158a360a
commit 8f434331c1
2 changed files with 42 additions and 15 deletions

View File

@ -1,13 +1,19 @@
#lang br #lang br
(provide (all-defined-out))
(define Nand-in-a (struct Nand (a b out) #:transparent)
(define (make-Nand)
(Nand a b out))
(define a
(let ([Nand-a-val 0]) (let ([Nand-a-val 0])
(λ ([val #f]) (λ ([val #f])
(if val (if val
(set! Nand-a-val val) (set! Nand-a-val val)
Nand-a-val)))) Nand-a-val))))
(define Nand-in-b (define b
(let ([Nand-b-val 0]) (let ([Nand-b-val 0])
(λ ([val #f]) (λ ([val #f])
(if val (if val
@ -15,22 +21,16 @@
Nand-b-val)))) Nand-b-val))))
(define (Nand-out-out) (define (out)
(if (< (+ (Nand-in-a) (Nand-in-b)) 2) (if (< (+ (a) (b)) 2)
1 1
0)) 0))
(module+ test (module+ test
(require rackunit) (require rackunit)
(check-equal? (begin (Nand-in-a 0) (Nand-in-b 0) (Nand-out-out)) 1) (check-equal? (begin (a 0) (b 0) (out)) 1)
(check-equal? (begin (Nand-in-a 0) (Nand-in-b 1) (Nand-out-out)) 1) (check-equal? (begin (a 0) (b 1) (out)) 1)
(check-equal? (begin (Nand-in-a 1) (Nand-in-b 0) (Nand-out-out)) 1) (check-equal? (begin (a 1) (b 0) (out)) 1)
(check-equal? (begin (Nand-in-a 1) (Nand-in-b 1) (Nand-out-out)) 0)) (check-equal? (begin (a 1) (b 1) (out)) 0))
(define n (make-Nand))
(struct ins ([a #:auto] [b #:auto]) #:transparent
#:auto-value (open-input-bytes #""))
(struct outs ([out #:auto]) #:transparent)
(struct Nand (i o) #:transparent)
(define f (Nand (ins) (outs)))

View File

@ -0,0 +1,27 @@
#lang racket
#|
CHIP Not {
IN in;
OUT out;
PARTS:
Nand(a=in, b=in, out=out);
}
|#
(provide (prefix-out Not- (all-defined-out)))
(require "Nand2.hdl.rkt")
(define in
(let ([in-val 0])
(λ ([val #f])
(if val
(set! in-val val)
in-val))))
(define n (make-Nand))
(define (out) (begin ((Nand-a n) (in)) ((Nand-b n) (in)) ((Nand-out n))))