#lang scheme/base
(require
"../scat.ss"
"../tools.ss"
"../target.ss"
"../asm.ss"
"../forth.ss"
"tethered.ss"
"console.ss"
"commands.ss"
"../port/ihex.ss"
scheme/control)
(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 (pointers [lst #f])
(if lst (set! *pointers* lst) *pointers*))
(define macros
(make-parameter
'((require (planet zwizwa/staapl/prj/pic18))
(init-prj))))
(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 (asm-off!)
(register-code-hook
(list
(lambda (chains . _)
(print-asm-code chains)))))
(define (print-asm-code chains)
(for-each
(lambda (x)
(print-target-word x)
(newline))
(reverse chains)))
(define (print-code [chains *chains*]) (print-asm-code chains))
(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)))