diff --git a/beautiful-racket-demo/hdl-demo/DMux.tst b/beautiful-racket-demo/hdl-demo/DMux.tst deleted file mode 100755 index 4adbc43..0000000 --- a/beautiful-racket-demo/hdl-demo/DMux.tst +++ /dev/null @@ -1,27 +0,0 @@ -// This file is part of www.nand2tetris.org -// and the book "The Elements of Computing Systems" -// by Nisan and Schocken, MIT Press. -// File name: projects/01/DMux.tst - -load DMux.hdl, -output-file DMux.out, -compare-to DMux.cmp, -output-list in%B3.1.3 sel%B3.1.3 a%B3.1.3 b%B3.1.3; - -set in 0, -set sel 0, -eval, -output; - -set sel 1, -eval, -output; - -set in 1, -set sel 0, -eval, -output; - -set sel 1, -eval, -output; diff --git a/beautiful-racket-demo/hdl-demo/DMux4Way.tst b/beautiful-racket-demo/hdl-demo/DMux4Way.tst deleted file mode 100755 index 6fbbb56..0000000 --- a/beautiful-racket-demo/hdl-demo/DMux4Way.tst +++ /dev/null @@ -1,43 +0,0 @@ -// This file is part of www.nand2tetris.org -// and the book "The Elements of Computing Systems" -// by Nisan and Schocken, MIT Press. -// File name: projects/01/DMux4Way.tst - -load DMux4Way.hdl, -output-file DMux4Way.out, -compare-to DMux4Way.cmp, -output-list in%B2.1.2 sel%B2.2.2 a%B2.1.2 b%B2.1.2 c%B2.1.2 d%B2.1.2; - -set in 0, -set sel %B00, -eval, -output; - -set sel %B01, -eval, -output; - -set sel %B10, -eval, -output; - -set sel %B11, -eval, -output; - -set in 1, -set sel %B00, -eval, -output; - -set sel %B01, -eval, -output; - -set sel %B10, -eval, -output; - -set sel %B11, -eval, -output; diff --git a/beautiful-racket-demo/hdl-demo/Mux.tst b/beautiful-racket-demo/hdl-demo/Mux.tst deleted file mode 100755 index 9b7afd9..0000000 --- a/beautiful-racket-demo/hdl-demo/Mux.tst +++ /dev/null @@ -1,49 +0,0 @@ -// This file is part of www.nand2tetris.org -// and the book "The Elements of Computing Systems" -// by Nisan and Schocken, MIT Press. -// File name: projects/01/Mux.tst - -load Mux.hdl, -output-file Mux.out, -compare-to Mux.cmp, -output-list a%B3.1.3 b%B3.1.3 sel%B3.1.3 out%B3.1.3; - -set a 0, -set b 0, -set sel 0, -eval, -output; - -set sel 1, -eval, -output; - -set a 0, -set b 1, -set sel 0, -eval, -output; - -set sel 1, -eval, -output; - -set a 1, -set b 0, -set sel 0, -eval, -output; - -set sel 1, -eval, -output; - -set a 1, -set b 1, -set sel 0, -eval, -output; - -set sel 1, -eval, -output; diff --git a/beautiful-racket-demo/hdl-demo/Not-sexp.rkt b/beautiful-racket-demo/hdl-demo/Not-sexp.rkt deleted file mode 100644 index dd252e3..0000000 --- a/beautiful-racket-demo/hdl-demo/Not-sexp.rkt +++ /dev/null @@ -1,17 +0,0 @@ -#lang s-exp hdl-demo/expander - -#| -CHIP Not { - IN in; - OUT out; - - PARTS: - Nand(a=in, b=in, out=out); -} -|# - -(chip-program Not - (in-spec (in)) - (out-spec (out)) - (part Nand (a in) (b in) (out out))) - diff --git a/beautiful-racket-demo/hdl-demo/Not.tst-sexp.rkt b/beautiful-racket-demo/hdl-demo/Not.tst-sexp.rkt deleted file mode 100644 index 112a83f..0000000 --- a/beautiful-racket-demo/hdl-demo/Not.tst-sexp.rkt +++ /dev/null @@ -1,34 +0,0 @@ -#lang s-exp hdl-tst-demo/expander - - -#| -load Not.hdl, -output-file Not.out, -compare-to Not.cmp, -output-list in%B3.1.3 out%B3.1.3; -set in 0, -eval, -output; -set in 1, -eval, -output; - -|# - -(require hdl-demo-tst/hdlprint rackunit racket/file) -(require "Not.hdl.rkt") ; load Not.hdl, -(define of (open-output-file "Not.out" #:mode 'text #:exists 'replace)) ; output-file Not.out, -(define (output in out) ; output-list in%B3.1.3 out%B3.1.3; -(fprintf of (format "~a\n" (string-join (list (hdlprint in "%B3.1.3") (hdlprint out "%B3.1.3")) "|" #:before-first "|" #:after-last "|")))) -(define eval-result #f) -(define eval-thunk (λ () (list (Not-in) (Not-out)))) ; output-list in%B3.1.3 out%B3.1.3; -(output "in" "out") ; put names at top of output -(Not-in-write 0) ; set in 0, -(set! eval-result (eval-thunk)) ; eval, -(apply output eval-result) ; output; -(Not-in-write 1) ; set in 1, -(set! eval-result (eval-thunk)) ; eval, -(apply output eval-result) ; output; -(close-output-port of) -(display (file->string "Not.out")) -(check-equal? (file->lines "Not.out") (file->lines "Not.cmp")) ; compare-to Not.cmp, \ No newline at end of file diff --git a/beautiful-racket-demo/hdl-demo/Or.tst b/beautiful-racket-demo/hdl-demo/Or.tst deleted file mode 100755 index 948b6b3..0000000 --- a/beautiful-racket-demo/hdl-demo/Or.tst +++ /dev/null @@ -1,29 +0,0 @@ -// This file is part of www.nand2tetris.org -// and the book "The Elements of Computing Systems" -// by Nisan and Schocken, MIT Press. -// File name: projects/01/Or.tst - -load Or.hdl, -output-file Or.out, -compare-to Or.cmp, -output-list a%B3.1.3 b%B3.1.3 out%B3.1.3; - -set a 0, -set b 0, -eval, -output; - -set a 0, -set b 1, -eval, -output; - -set a 1, -set b 0, -eval, -output; - -set a 1, -set b 1, -eval, -output; diff --git a/beautiful-racket-demo/hdl-demo/Xor.tst b/beautiful-racket-demo/hdl-demo/Xor.tst deleted file mode 100755 index 658cbe5..0000000 --- a/beautiful-racket-demo/hdl-demo/Xor.tst +++ /dev/null @@ -1,29 +0,0 @@ -// This file is part of www.nand2tetris.org -// and the book "The Elements of Computing Systems" -// by Nisan and Schocken, MIT Press. -// File name: projects/01/Xor.tst - -load Xor.hdl, -output-file Xor.out, -compare-to Xor.cmp, -output-list a%B3.1.3 b%B3.1.3 out%B3.1.3; - -set a 0, -set b 0, -eval, -output; - -set a 0, -set b 1, -eval, -output; - -set a 1, -set b 0, -eval, -output; - -set a 1, -set b 1, -eval, -output; diff --git a/beautiful-racket-demo/hdl-demo/bus.rkt b/beautiful-racket-demo/hdl-demo/bus.rkt index 4e11ea6..e83ac41 100644 --- a/beautiful-racket-demo/hdl-demo/bus.rkt +++ b/beautiful-racket-demo/hdl-demo/bus.rkt @@ -33,7 +33,7 @@ (reverse (for/list ([i (in-range width)]) (bitwise-bit-field int i (add1 i))))) -(define max-bus-width 64) +(define max-bus-width 16) (define default-bus-width 1) @@ -113,9 +113,9 @@ base bus: (define ID (begin (unless (<= bus-width max-bus-width) - (raise-argument-error 'id (format "bus width <= max width ~a" max-bus-width) bus-width)) + (raise-argument-error 'ID (format "bus width <= max width ~a" max-bus-width) bus-width)) (impersonate-procedure - (let ([reader (make-bus-reader 'id bus-width)]) + (let ([reader (make-bus-reader 'ID bus-width)]) (procedure-rename (λ args (apply reader (ID-THUNK) args)) (string->symbol (format "~a:~a-bit" 'ID bus-width)))) #f BUS-TYPE #t))) #,(when (syntax-property caller-stx 'writer) @@ -201,7 +201,7 @@ input bus: (check-false (bus? ib)) (check-false (output-bus? ib)) (check-true (input-bus? ib)) - (check-exn exn:fail? (λ () (define-input-bus ib 17) ib)) ; exceeds 16-bit width + #;(check-exn exn:fail? (λ () (define-input-bus ib 17) ib)) ; exceeds 16-bit width (check-equal? (ib) 0) (ib-write 11) ; set whole value (check-equal? (ib) 11) diff --git a/beautiful-racket-demo/hdl-demo/expander.rkt b/beautiful-racket-demo/hdl-demo/expander.rkt index 3258704..7318931 100644 --- a/beautiful-racket-demo/hdl-demo/expander.rkt +++ b/beautiful-racket-demo/hdl-demo/expander.rkt @@ -7,28 +7,28 @@ (out-spec (OUT-BUS OUT-WIDTH ...) ...) PART ...) (with-pattern - ([CHIP-PREFIX (suffix-id #'CHIPNAME "-")] - [(IN-BUS-WRITE ...) (suffix-id #'(IN-BUS ...) "-write")] - [(PREFIX-OUT-BUS ...) (prefix-id #'CHIP-PREFIX #'(OUT-BUS ...))]) - #'(begin - (provide (prefix-out CHIP-PREFIX (combine-out IN-BUS ... IN-BUS-WRITE ...))) - (define-input-bus IN-BUS IN-WIDTH ...) ... - PART ... - (provide PREFIX-OUT-BUS ...) - (define-output-bus PREFIX-OUT-BUS OUT-BUS OUT-WIDTH ...) ...))) + ([CHIP-PREFIX (suffix-id #'CHIPNAME "-")] + [(IN-BUS-WRITE ...) (suffix-id #'(IN-BUS ...) "-write")] + [(PREFIX-OUT-BUS ...) (prefix-id #'CHIP-PREFIX #'(OUT-BUS ...))]) + #'(begin + (provide (prefix-out CHIP-PREFIX (combine-out IN-BUS ... IN-BUS-WRITE ...))) + (define-input-bus IN-BUS IN-WIDTH ...) ... + PART ... + (provide PREFIX-OUT-BUS ...) + (define-output-bus PREFIX-OUT-BUS OUT-BUS OUT-WIDTH ...) ...))) (define-macro (part PARTNAME ((BUS-LEFT . BUS-LEFT-ARGS) BUS-RIGHT-EXPR) ...) (with-pattern - ([(PARTNAME-BUS-LEFT ...) (prefix-id #'PARTNAME "-" #'(BUS-LEFT ...))] - [PARTNAME-MODULE-PATH (format-string "~a.hdl.rkt" #'PARTNAME)]) - #'(begin - (require (import-chip PARTNAME-MODULE-PATH) - ;; need for-syntax to make phase 1 binding available - ;; so we can determine during expansion which buses are `input-bus?` - ;; because the pin-spec syntax is inherently ambiguous - (for-syntax (import-chip PARTNAME-MODULE-PATH))) - (handle-buses ((PARTNAME-BUS-LEFT . BUS-LEFT-ARGS) BUS-RIGHT-EXPR) ...)))) + ([(PARTNAME-BUS-LEFT ...) (prefix-id #'PARTNAME "-" #'(BUS-LEFT ...))] + [PARTNAME-MODULE-PATH (format-string "~a.hdl.rkt" #'PARTNAME)]) + #'(begin + (require (import-chip PARTNAME-MODULE-PATH) + ;; need for-syntax to make phase 1 binding available + ;; so we can determine during expansion which buses are `input-bus?` + ;; because the pin-spec syntax is inherently ambiguous + (for-syntax (import-chip PARTNAME-MODULE-PATH))) + (handle-buses ((PARTNAME-BUS-LEFT . BUS-LEFT-ARGS) BUS-RIGHT-EXPR) ...)))) (define-syntax import-chip @@ -38,23 +38,26 @@ [(_ module-path) (expand-import #'module-path)])))) - +(require (for-syntax racket/list)) (define-macro (handle-buses BUS-ASSIGNMENTS ...) (let-values + ;; we "pre-evaluate" #'PREFIXED-WIRE so we can set up the program correctly. + ;; This is not ideal: usually we want evaluate runtime expressions only at runtime. + ;; But in this case, it controls which identifiers we `define` as output buses + ;; so there's no way around it. Runtime would be too late. ([(in-bus-assignments out-bus-assignments) - (syntax-case-partition #'(BUS-ASSIGNMENTS ...) () - [((PREFIXED-WIRE . _) _) - ;; we "pre-evaluate" #'PREFIXED-WIRE so we can set up the program correctly. - ;; This is not ideal: usually we want evaluate runtime expressions only at runtime. - ;; But in this case, it controls which identifiers we `define` as output buses - ;; so there's no way around it. Runtime would be too late. - (input-bus? (syntax-local-eval #'PREFIXED-WIRE))])]) + (partition (λ (stx) + (syntax-case stx () + [((PREFIXED-WIRE . _) _) + (input-bus? (syntax-local-eval #'PREFIXED-WIRE)) + #'PREFIXED-WIRE] + [else #f])) (syntax->list #'(BUS-ASSIGNMENTS ...)))]) (with-pattern - ([(((IN-BUS IN-BUS-ARG ...) IN-BUS-VALUE) ...) in-bus-assignments] - [(IN-BUS-WRITE ...) (suffix-id #'(IN-BUS ...) "-write")] - [((OUT-BUS-EXPR (NEW-OUT-BUS)) ...) out-bus-assignments]) - #'(begin - (define-output-bus NEW-OUT-BUS - (λ () - (IN-BUS-WRITE IN-BUS-ARG ... IN-BUS-VALUE) ... - OUT-BUS-EXPR)) ...)))) \ No newline at end of file + ([(((IN-BUS IN-BUS-ARG ...) IN-BUS-VALUE) ...) in-bus-assignments] + [(IN-BUS-WRITE ...) (suffix-id #'(IN-BUS ...) "-write")] + [((OUT-BUS-EXPR (NEW-OUT-BUS)) ...) out-bus-assignments]) + #'(begin + (define-output-bus NEW-OUT-BUS + (λ () + (IN-BUS-WRITE IN-BUS-ARG ... IN-BUS-VALUE) ... + OUT-BUS-EXPR)) ...)))) \ No newline at end of file diff --git a/beautiful-racket-demo/hdl-demo/reader.rkt b/beautiful-racket-demo/hdl-demo/reader.rkt index 91c0eb9..8794ad7 100644 --- a/beautiful-racket-demo/hdl-demo/reader.rkt +++ b/beautiful-racket-demo/hdl-demo/reader.rkt @@ -1,6 +1,8 @@ #lang br (require "parser.rkt" "tokenizer.rkt") (provide read-syntax) -(define (read-syntax source-path input-port) - #`(module hdl-mod hdl-demo/expander - #,(parse source-path (tokenize input-port)))) +(define (read-syntax src ip) + (strip-context + (with-syntax ([PT (parse src (tokenize ip))]) + #'(module hdl-mod hdl-demo/expander + PT)))) diff --git a/beautiful-racket-demo/hdl-tst-demo/main.rkt b/beautiful-racket-demo/hdl-tst-demo/main.rkt index 7b4b21a..58d29ae 100644 --- a/beautiful-racket-demo/hdl-tst-demo/main.rkt +++ b/beautiful-racket-demo/hdl-tst-demo/main.rkt @@ -8,5 +8,7 @@ (define port+newline (input-port-append #f port (open-input-string "\n"))) (port-count-lines! port+newline) (set-port-next-location! port+newline line col pos) - #`(module hdl-mod hdl-tst-demo/expander - #,(parse source-path (make-tokenizer port+newline))))) + (strip-context + (with-syntax ([PT (parse source-path (make-tokenizer port+newline))]) + #'(module hdl-mod hdl-tst-demo/expander + PT)))))