#lang scheme/base
(require
scheme/control
"tools.ss"
"forth.ss")
(require/provide
"purrr.ss"
"pic18/asm.ss"
"pic18/macro.ss"
"pic18/const.ss"
"pic18/parsing-words.ss"
"pic18/geo.ss" "port/ihex.ss"
"live.ss"
)
(provide (all-defined-out))
(define (chains->binary-code chain-list)
(prompt
(map
(lambda (c) (binchunk-split c 0 8))
(or (target-chains->bin chain-list)
(abort #f)))))
(define *pointers* '((code 0) (data 0)))
(define *chains* '())
(define *bin* '())
(define (all-bin) (apply append (reverse *bin*)))
(define (kill-code!)
(set! *bin* '())
(set! *chains* '()))
(define (assemble-chains chains . _)
(let-values
(((bin pointers) (assemble! chains *pointers*)))
(set! *pointers* pointers))
(for ((chain (reverse chains))) (push! *chains* chain))
(push! *bin* (or (chains->binary-code chains)
(error 'no-binary-code))))
(define *marks* '())
(define (mark) (push! *marks* *pointers*))
(define (empty)
(define bits 5) (define (get x) (cadr (assq x *pointers*)))
(unless (null? *marks*)
(set! *pointers* (pop! *marks*)))
(let ((code (bit-ceil (get 'code) bits))
(data (get 'data)))
(set! *pointers*
`((code ,code)
(data ,data)))
(erase-from-block (>>> code bits))))
(define (asm-on!)
(register-code-hook (list assemble-chains)))
(asm-on!)
(define (ihex [bin (all-bin)]
[port (current-output-port)])
(write-ihex bin port))
(define (save-ihex filename)
(with-output-to-file/safe filename ihex)
(kill-code!))
(define (commit [bin (all-bin)])
(unless (null? bin)
(with-console (lambda () (upload-bytes bin)))
(kill-code!)))
(define (print-code [chains *chains*]) (print-asm-code chains))
(define asm-off! asm-debug!)
(define-syntax-rule (0cmd: command ...)
(begin
(snarf as-void (scat) (() (command ...)))
(substitution-types (target) (0cmd command ...))))
(0cmd: commit print-code mark empty)
(substitutions (target)
((ul w) (empty mark load w commit)))
(define prog piklab-prog)
(loading "pic18")