#lang scheme/base
(provide (all-defined-out))
(require
"binary.ss"
"seq.ss")
(require (lib "78.ss" "srfi"))
(check-set-mode! 'report-failed)
(define hexdigit
(let ((table
(apply vector
(string->list "0123456789ABCDEF"))))
(lambda (x)
(vector-ref table x))))
(define (hex->string nibbles val)
(let-values
(((digits _)
(for/fold ((d '())
(v val))
((i (in-range nibbles)))
(let ((nibble (band v #xF)))
(values
(cons (hexdigit nibble) d)
(>>> v 4))))))
(list->string digits)))
(check (hex->string 6 #x1234FF) => "1234FF")
(define (word->string x) (hex->string 4 x))
(define (byte->string x) (hex->string 2 x))
(define (in-hex-printer [start 0]
[data-nibbles 2]
[address-nibbles 4])
(in-generator
(lambda (yield)
(define-syntax-rule (lp formals . args)
(yield (lambda formals (printf . args))))
(define (addr x) (hex->string address-nibbles x))
(define (data x) (hex->string data-nibbles x))
(for ((row (in-naturals start)))
(lp (x) "~a ~a" (addr (* row 8)) (data x))
(for ((i (in-range 6))) (lp (x) " ~a" (data x)))
(lp (x) " ~a\n" (data x))))))