|
|
@ -33,33 +33,35 @@
|
|
|
|
(check-equal? #xA (glue-bytes (list #xA)))
|
|
|
|
(check-equal? #xA (glue-bytes (list #xA)))
|
|
|
|
(check-equal? #x0 (glue-bytes (list #x0))))
|
|
|
|
(check-equal? #x0 (glue-bytes (list #x0))))
|
|
|
|
|
|
|
|
|
|
|
|
(define-syntax (define-memory-vector stx)
|
|
|
|
(define-macro (define-memory-vector ID [FIELD LENGTH SIZE] ...)
|
|
|
|
(syntax-case stx ()
|
|
|
|
(with-pattern
|
|
|
|
[(_ ID [FIELD LENGTH SIZE] ...)
|
|
|
|
([(PREFIXED-ID ...) (prefix-id #'ID "-" #'(FIELD ...))]
|
|
|
|
(with-syntax ([(ID-FIELD-REF ...) (map (λ(field) (format-id stx "~a-~a-ref" #'ID field)) (syntax->list #'(FIELD ...)))]
|
|
|
|
[(PREFIXED-ID-REF ...) (suffix-id #'(PREFIXED-ID ...) "-ref")]
|
|
|
|
[(ID-FIELD-SET! ...) (map (λ(field) (format-id stx "~a-~a-set!" #'ID field)) (syntax->list #'(FIELD ...)))]
|
|
|
|
[(PREFIXED-ID-SET! ...) (suffix-id #'(PREFIXED-ID ...) "-set!")]
|
|
|
|
[(FIELD-OFFSET ...) (reverse (cdr
|
|
|
|
[(FIELD-OFFSET ...) (reverse (cdr
|
|
|
|
(for/fold ([offsets '(0)])
|
|
|
|
(for/fold ([accum-stxs (list #'0)])
|
|
|
|
([len (in-list (syntax->list #'(LENGTH ...)))]
|
|
|
|
([len-size-stx (in-list (syntax->list #'((LENGTH SIZE) ...)))])
|
|
|
|
[size (in-list (syntax->list #'(SIZE ...)))])
|
|
|
|
(cons (with-pattern
|
|
|
|
(cons (+ (syntax-local-eval #`(* #,len #,size)) (car offsets)) offsets))))])
|
|
|
|
([accum (car accum-stxs)]
|
|
|
|
#'(begin
|
|
|
|
[(len size) len-size-stx])
|
|
|
|
(define ID (make-vector (+ (* LENGTH SIZE) ...)))
|
|
|
|
#'(+ (* len size) accum)) accum-stxs))))])
|
|
|
|
(define (ID-FIELD-REF idx)
|
|
|
|
#'(begin
|
|
|
|
(unless (< idx LENGTH)
|
|
|
|
(define ID (make-vector (+ (* LENGTH SIZE) ...)))
|
|
|
|
(raise-argument-error 'ID-FIELD-REF (format "index less than field length ~a" LENGTH) idx))
|
|
|
|
(define (PREFIXED-ID-REF idx)
|
|
|
|
(glue-bytes
|
|
|
|
(unless (< idx LENGTH)
|
|
|
|
(for/list ([i (in-range SIZE)])
|
|
|
|
(raise-argument-error 'PREFIXED-ID-REF (format "index less than field length ~a" LENGTH) idx))
|
|
|
|
(vector-ref ID (+ FIELD-OFFSET i idx)))))
|
|
|
|
(glue-bytes
|
|
|
|
...
|
|
|
|
(for/list ([i (in-range SIZE)])
|
|
|
|
(define (ID-FIELD-SET! idx val)
|
|
|
|
(vector-ref ID (+ FIELD-OFFSET i idx)))))
|
|
|
|
(unless (< idx LENGTH)
|
|
|
|
...
|
|
|
|
(raise-argument-error 'ID-FIELD-SET! (format "index less than field length ~a" LENGTH) idx))
|
|
|
|
(define (PREFIXED-ID-SET! idx val)
|
|
|
|
(unless (< val (expt 16 SIZE))
|
|
|
|
(unless (< idx LENGTH)
|
|
|
|
(raise-argument-error 'ID-FIELD-SET! (format "value less than field size ~a" (expt 16 SIZE)) val))
|
|
|
|
(raise-argument-error 'PREFIXED-ID-SET! (format "index less than field length ~a" LENGTH) idx))
|
|
|
|
(for ([i (in-range SIZE)]
|
|
|
|
(unless (< val (expt 16 SIZE))
|
|
|
|
[b (in-list (explode-bytes val))])
|
|
|
|
(raise-argument-error 'PREFIXED-ID-SET! (format "value less than field size ~a" (expt 16 SIZE)) val))
|
|
|
|
(vector-set! ID (+ FIELD-OFFSET i idx) b))) ...))]))
|
|
|
|
(for ([i (in-range SIZE)]
|
|
|
|
|
|
|
|
[b (in-list (explode-bytes val))])
|
|
|
|
|
|
|
|
(vector-set! ID (+ FIELD-OFFSET i idx) b))) ...)))
|
|
|
|
|
|
|
|
|
|
|
|
(define-memory-vector chip
|
|
|
|
(define-memory-vector chip
|
|
|
|
[opcode 1 2] ; two bytes
|
|
|
|
[opcode 1 2] ; two bytes
|
|
|
|