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.
60 lines
2.7 KiB
Racket
60 lines
2.7 KiB
Racket
8 years ago
|
#lang br/quicklang
|
||
9 years ago
|
(require "bus.rkt" (for-syntax racket/syntax racket/require-transform br/syntax "bus-properties.rkt"))
|
||
8 years ago
|
(provide #%module-begin (all-defined-out))
|
||
9 years ago
|
|
||
|
(define-macro (chip-program CHIPNAME
|
||
|
(in-spec (IN-BUS IN-WIDTH ...) ...)
|
||
|
(out-spec (OUT-BUS OUT-WIDTH ...) ...)
|
||
|
PART ...)
|
||
9 years ago
|
(with-pattern
|
||
9 years ago
|
([CHIP-PREFIX (suffix-id #'CHIPNAME "-")]
|
||
9 years ago
|
[(IN-BUS-WRITE ...) (suffix-id #'(IN-BUS ...) "-write")]
|
||
|
[(PREFIX-OUT-BUS ...) (prefix-id #'CHIP-PREFIX #'(OUT-BUS ...))])
|
||
9 years ago
|
#'(begin
|
||
9 years ago
|
(provide (prefix-out CHIP-PREFIX (combine-out IN-BUS ... IN-BUS-WRITE ...)))
|
||
9 years ago
|
(define-input-bus IN-BUS IN-WIDTH ...) ...
|
||
|
PART ...
|
||
|
(provide PREFIX-OUT-BUS ...)
|
||
|
(define-output-bus PREFIX-OUT-BUS OUT-BUS OUT-WIDTH ...) ...)))
|
||
9 years ago
|
|
||
|
|
||
|
(define-macro (part PARTNAME ((BUS-LEFT . BUS-LEFT-ARGS) BUS-RIGHT-EXPR) ...)
|
||
9 years ago
|
(with-pattern
|
||
|
([(PARTNAME-BUS-LEFT ...) (prefix-id #'PARTNAME "-" #'(BUS-LEFT ...))]
|
||
9 years ago
|
[PARTNAME-MODULE-PATH (format-string "~a.hdl.rkt" #'PARTNAME)])
|
||
9 years ago
|
#'(begin
|
||
9 years ago
|
(require (import-chip PARTNAME-MODULE-PATH)
|
||
9 years ago
|
;; 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
|
||
9 years ago
|
(for-syntax (import-chip PARTNAME-MODULE-PATH)))
|
||
9 years ago
|
(handle-buses ((PARTNAME-BUS-LEFT . BUS-LEFT-ARGS) BUS-RIGHT-EXPR) ...))))
|
||
9 years ago
|
|
||
|
|
||
|
(define-syntax import-chip
|
||
|
(make-require-transformer
|
||
|
(λ (stx)
|
||
|
(syntax-case stx ()
|
||
|
[(_ module-path)
|
||
|
(expand-import #'module-path)]))))
|
||
|
|
||
9 years ago
|
|
||
9 years ago
|
(define-macro (handle-buses BUS-ASSIGNMENTS ...)
|
||
9 years ago
|
(let-values
|
||
|
([(in-bus-assignments out-bus-assignments)
|
||
|
(syntax-case-partition #'(BUS-ASSIGNMENTS ...) ()
|
||
|
[((PREFIXED-WIRE . _) _)
|
||
9 years ago
|
;; 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.
|
||
9 years ago
|
;; But in this case, it controls which identifiers we `define` as output buses
|
||
9 years ago
|
;; so there's no way around it. Runtime would be too late.
|
||
9 years ago
|
(input-bus? (syntax-local-eval #'PREFIXED-WIRE))])])
|
||
9 years ago
|
(with-pattern
|
||
9 years ago
|
([(((IN-BUS IN-BUS-ARG ...) IN-BUS-VALUE) ...) in-bus-assignments]
|
||
9 years ago
|
[(IN-BUS-WRITE ...) (suffix-id #'(IN-BUS ...) "-write")]
|
||
9 years ago
|
[((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)) ...))))
|