You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
beautiful-racket/beautiful-racket-demo/hdl-tst-demo/expander.rkt

89 lines
3.1 KiB
Racket

8 years ago
#lang br/quicklang
8 years ago
(require (for-syntax racket/string) rackunit racket/file)
7 years ago
(provide #%module-begin (all-defined-out))
9 years ago
(define (print-cell val fmt)
(match-define (list _ radix-letter number-strings) (regexp-match #px"^%(.)(.*)$" fmt)) ; like %B1.16.1
(match-define (list left-margin width right-margin) (map string->number (string-split number-strings ".")))
(cond
[(number? val)
(define radix (case radix-letter
[("B") 2]))
(string-append (make-string left-margin #\space)
(~r val #:min-width width #:pad-string "0" #:base radix)
(make-string right-margin #\space))]
[(string? val) (~a val #:min-width (+ left-margin width right-margin) #:pad-string " " #:align 'center)]
[else (error 'unknown-value)]))
(define (print-line output-filename cells)
(with-output-to-file output-filename
(λ () (printf (format "~a\n" (string-join cells "|" #:before-first "|" #:after-last "|"))))
8 years ago
#:mode 'text #:exists 'append))
9 years ago
(module+ test
(require rackunit)
(define a 123)
(check-equal? (print-cell a "%B1.16.1") " 0000000001111011 ")
(check-equal? (print-cell "out" "%B1.16.1") " out ")
(check-equal? (print-cell "out" "%B3.1.3") " out ")
(check-equal? (print-cell "in" "%B3.1.3") " in "))
(define-for-syntax chip-prefix #f)
9 years ago
7 years ago
(define-macro (program EXPR ...)
8 years ago
(with-shared-id (compare-files)
#'(begin
EXPR ...
(compare-files))))
9 years ago
7 years ago
(define-macro (load-expr CHIPFILE-STRING)
9 years ago
(set! chip-prefix (string-replace (syntax->datum #'CHIPFILE-STRING) ".hdl" ""))
8 years ago
(with-pattern ([CHIPFILE.RKT (format-string "~a.rkt" #'CHIPFILE-STRING)])
8 years ago
#'(require CHIPFILE.RKT)))
7 years ago
(define-macro (output-file-expr OUTPUT-FILE-STRING)
8 years ago
(with-shared-id (output-file output-filename)
9 years ago
#'(begin
8 years ago
(define output-filename OUTPUT-FILE-STRING)
8 years ago
(with-output-to-file output-filename (λ () (printf ""))
#:mode 'text #:exists 'replace))))
8 years ago
7 years ago
(define-macro (compare-to-expr COMPARE-FILE-STRING)
8 years ago
(with-shared-id (compare-files output-filename)
#'(define (compare-files)
(check-equal? (file->lines output-filename) (file->lines COMPARE-FILE-STRING)))))
7 years ago
(define-macro (output-list-expr (COL-NAME FORMAT-SPEC) ...)
8 years ago
(with-shared-id (eval-result eval-chip output output-filename)
8 years ago
(with-pattern ([(COL-ID ...) (suffix-ids #'(COL-NAME ...) "")]
[(CHIP-COL-ID ...) (prefix-ids chip-prefix "-" #'(COL-NAME ...))])
8 years ago
#'(begin
(define (output COL-ID ...)
(print-line output-filename (map print-cell (list COL-ID ...) (list FORMAT-SPEC ...))))
(define eval-result #f)
(define (eval-chip) (list (CHIP-COL-ID) ...))
(output COL-NAME ...)))))
9 years ago
7 years ago
(define-macro (set-expr IN-BUS IN-VAL)
9 years ago
(with-pattern
8 years ago
([CHIP-IN-BUS-ID-WRITE (prefix-id chip-prefix "-" (suffix-id #'IN-BUS "-write"))])
#'(CHIP-IN-BUS-ID-WRITE IN-VAL)))
9 years ago
7 years ago
(define-macro (eval-expr)
8 years ago
(with-shared-id (eval-result eval-chip)
#'(set! eval-result (eval-chip))))
9 years ago
7 years ago
(define-macro (output-expr)
8 years ago
(with-shared-id (output eval-result)
#'(apply output eval-result)))