#lang scheme/base

 (lib ""))
(provide (all-defined-out))

(require  (lib "" "srfi"))
(check-set-mode! 'report-failed)


;; Using list structures instead of structure types.
;; FIXME: does it need word size?

(define (number/false? x)
  (and x (number? x)))

(define (binchunk? x)
   (and (= 2 (length x))
        (number? (car x))
        (pair? (cadr x))      ;; need at least one element
        (andmap number/false? ;; allow undefined bytes (means don't overwrite)
                (cadr x)))))

(define (bin? x)
   (andmap binchunk? x)))

(define binchunk-address car)
(define binchunk-code cadr)

(define (binchunk-split  chunk left right) ;;
  (match chunk
         ((addr things)
          `(,(<<< addr 1)
            ,(split-nibble-list things left right)))))

(check (binchunk-split '(0 (#x0102 #x0304)) 0 8)
       => '(0 (#x02 #x01 #x04 #x03)))

;; Flatten binchunks if they are consecutive (FIXME: within limit?)

(define (in-binchunk chunk)
   (in-naturals (binchunk-address chunk))
   (in-list (binchunk-code chunk))))

(define (in-bin bin)
  (apply in-append (map in-binchunk bin)))

;; Using the stack-of-stacks abstraction to combine consecutive
;; binchunks into one.
(define (bin-flatten dirty-bin)
  (let ((bin (filter binchunk? dirty-bin))) ;; remove empty chunks
        (((sos _)
          (for/fold ((sos (make-sos 2))
                     (expected (binchunk-address (car bin))))
                    (((address codeword) (in-bin bin)))
              (if (= address expected)
                    ;; (printf "rechunk: ~a (~a)\n" address expected)
                    (sos-collapse sos 1)))
              (list address codeword))
             (+ 1 address)))))
      (map address/code->binchunk
           (sos->list sos)))))

;; Convert intermediate representation to binchunk, assuming all
;; addresses are in sequence.
(define (address/code->binchunk acl)
  (list (caar acl)
        (map cadr acl)))

;; Split binchunk in aligned lines.
(define (in-binchunk/pad-align chunk bits [filler #f])
   (lambda (yield)
     (define addr  (binchunk-address chunk))
     (define endx  (+ addr (length (binchunk-code chunk))))
     (define _addr (bit-floor addr bits))
     (define _endx (bit-ceil endx bits))
     (for ((a (in-range _addr addr)))   (yield a filler))
     (for (((a c) (in-binchunk chunk))) (yield a c))
     (for ((a (in-range endx _endx)))   (yield a filler)))))

(check (for/list (((a c) (in-binchunk/pad-align '(3 (1 2 3)) 3 #f)))
                 (list a c))
       => '((0 #f)
            (1 #f)
            (2 #f)
            (3 1)
            (4 2)
            (5 3)
            (6 #f)
            (7 #f)))

(define (in-binchunk-sequence/lines seq bits)
   (lambda (yield)
     (let ((mask (bitmask bits)))
       (for/fold ((line '()))
                 (((a c) seq))
         (let ((line+ (cons c line)))
           (if (= mask (band a mask))
                 (yield (- a mask)
                        (reverse line+))

(define (in-binchunk/lines chunk bits [filler #f])
   (in-binchunk/pad-align chunk bits filler)

;; These are used in:

;; - internal PIC18: convert words to bytes + flatten

;; - upload: algin + divide in lines.