1 ;;; File: "pic18-sim.scm"
5 (define pic18-stack #f)
9 (define pic18-carry-flag #f)
10 (define pic18-deccarry-flag #f)
11 (define pic18-zero-flag #f)
12 (define pic18-overflow-flag #f)
13 (define pic18-negative-flag #f)
15 (define pic18-cycles #f)
16 (define pic18-exit #f)
20 (bitwise-and (arithmetic-shift (get-tos) -16) #xff))
22 (bitwise-and (arithmetic-shift (get-tos) -8) #xff))
24 (bitwise-and (get-tos) #xff))
26 (set-ram PCLATU (bitwise-and (arithmetic-shift (get-pc) -16) #x1f))
27 (set-ram PCLATH (bitwise-and (arithmetic-shift (get-pc) -8) #xff))
28 (bitwise-and (get-pc) #xfe))
31 (arithmetic-shift pic18-deccarry-flag 1)
32 (arithmetic-shift pic18-zero-flag 2)
33 (arithmetic-shift pic18-overflow-flag 3)
34 (arithmetic-shift pic18-negative-flag 4)))
35 ((assq adr (list (cons INDF0 (cons FSR0H FSR0L))
36 (cons INDF1 (cons FSR1H FSR1L))
37 (cons INDF2 (cons FSR2H FSR2L))))
40 (arithmetic-shift (bitwise-and (u8vector-ref pic18-ram
44 (u8vector-ref pic18-ram
46 ;; TODO pre/post inc/dec 0..2
48 (u8vector-ref pic18-ram adr))))
50 (define (set-ram adr byte)
52 (set-tos (+ (bitwise-and (get-tos) #x00ffff)
53 (arithmetic-shift (bitwise-and byte #x1f) 16))))
55 (set-tos (+ (bitwise-and (get-tos) #x1f00ff)
56 (arithmetic-shift byte 8))))
58 (set-tos (+ (bitwise-and (get-tos) #x1fff00)
61 (set-pc (+ (bitwise-and (arithmetic-shift (get-ram PCLATU) 16) #x1f)
62 (bitwise-and (arithmetic-shift (get-ram PCLATH) 8) #xff)
63 (bitwise-and byte #xfe))))
65 (set! pic18-carry-flag (bitwise-and byte 1))
66 (set! pic18-deccarry-flag (arithmetic-shift (bitwise-and byte 2) -1))
67 (set! pic18-zero-flag (arithmetic-shift (bitwise-and byte 4) -2))
68 (set! pic18-overflow-flag (arithmetic-shift (bitwise-and byte 8) -3))
69 (set! pic18-negative-flag (arithmetic-shift (bitwise-and byte 16) -4)))
70 ((assq adr (list (cons INDF0 (cons FSR0H FSR0L))
71 (cons INDF1 (cons FSR1H FSR1L))
72 (cons INDF2 (cons FSR2H FSR2L))))
74 (set-ram (bitwise-ior ;; TODO factor common code with get-ram ?
75 (arithmetic-shift (bitwise-and (u8vector-ref pic18-ram
79 (u8vector-ref pic18-ram
82 ;; TODO all other special array registers
84 (u8vector-set! pic18-ram adr byte))))
87 (u8vector-ref pic18-rom adr))
89 (define (set-rom adr byte)
90 (u8vector-set! pic18-rom adr byte))
92 (define (get-stack adr)
93 (vector-ref pic18-stack adr))
95 (define (set-stack adr pc)
96 (vector-set! pic18-stack adr pc))
105 (bitwise-and (get-ram STKPTR) #x1f))
110 (bitwise-and (get-ram STKPTR) #xe0))))
113 (vector-ref pic18-stack (- (get-sp) 1)))
116 (vector-set! pic18-stack (- (get-sp) 1) pc))
118 (define (stack-push pc)
119 (set-sp (+ (get-sp) 1))
124 (set-sp (- (get-sp) 1)))
127 (bitwise-and (get-ram BSR) #x0f))
132 (define (set-wreg byte)
133 (set! pic18-wreg byte))
136 (not (= 0 pic18-zero-flag)))
138 (define (set-zero-flag flag)
139 (set! pic18-zero-flag flag))
141 (define (negative-flag?)
142 (not (= 0 pic18-negative-flag)))
144 (define (set-negative-flag flag)
145 (set! pic18-negative-flag flag))
147 (define (carry-flag?)
148 (not (= 0 pic18-carry-flag)))
150 (define (set-carry-flag flag)
151 (set! pic18-carry-flag flag))
153 (define (deccarry-flag?)
154 (not (= 0 pic18-deccarry-flag)))
156 (define (set-deccarry-flag flag)
157 (set! pic18-deccarry-flag flag))
159 (define (overflow-flag?)
160 (not (= 0 pic18-overflow-flag)))
162 (define (set-overflow-flag flag)
163 (set! pic18-overflow-flag flag))
165 (define (pic18-sim-setup)
166 (set! pic18-ram (make-u8vector #x1000 0))
167 (set! pic18-rom (make-u8vector #x10000 0))
168 (set! pic18-stack (make-vector #x1f 0))
171 (set! pic18-carry-flag 0)
172 (set! pic18-deccarry-flag 0)
173 (set! pic18-zero-flag 0)
174 (set! pic18-overflow-flag 0)
175 (set! pic18-negative-flag 0))
177 (define (pic18-sim-cleanup)
180 (set! pic18-stack #f))
182 ;------------------------------------------------------------------------------
185 (let ((pc (- (get-pc) 2)))
186 (list (get-sp) " " (- pic18-cycles 1) " "
187 (substring (number->string (+ #x1000000 pc) 16) 1 7)
190 (define (illegal-opcode opcode)
192 (print (list (last-pc) " *illegal*")))
193 (error "illegal opcode" opcode))
195 (define decode-vector
196 (make-vector 256 illegal-opcode))
198 (define (decode-opcode opcode-bits shift action)
200 (error "shift=" shift))
201 (let ((n (arithmetic-shift 1 (- shift 8)))
202 (base (arithmetic-shift opcode-bits (- shift 8))))
206 (vector-set! decode-vector (+ base i) action)
209 (define (byte-oriented opcode mnemonic flags-changed operation)
210 (byte-oriented-aux opcode mnemonic flags-changed operation 'wreg))
211 (define (byte-oriented-file opcode mnemonic flags-changed operation)
212 (byte-oriented-aux opcode mnemonic flags-changed operation 'file))
213 (define (byte-oriented-wide opcode mnemonic flags-changed operation dest)
214 ;; for use with instructions that have results more than a byte wide, such
215 ;; as multiplication. the result goes at the given addresses
216 (byte-oriented-aux opcode mnemonic flags-changed operation dest)) ;; TODO do the same for literals
218 (define (byte-oriented-aux opcode mnemonic flags-changed operation dest)
219 (let* ((f (bitwise-and opcode #xff))
220 (adr (if (= 0 (bitwise-and opcode #x100))
221 ;; the upper 160 addresses of the first bank are the special
222 ;; registers #xF60 to #xFFF
223 (if (= 0 (bitwise-and f #x80)) f (+ f #xf00))
224 (+ f (arithmetic-shift (get-bsr) 8)))))
226 (print (list (last-pc) " " mnemonic " "
227 (let ((x (assv adr file-reg-names)))
228 (if x (cdr x) (list "0x" (number->string adr 16))))
229 (if (or (eq? dest 'wreg)
230 (= 0 (bitwise-and opcode #x200)))
234 (let* ((result (operation (get-ram adr)))
235 (result-8bit (bitwise-and result #xff)))
237 ;; result is more than a byte wide (i.e. multiplication)
238 ;; put it in the right destinations (dest is a list of addresses)
239 (let loop ((dest dest) (result result))
240 (if (not (null? dest))
241 ;; the head of the list is the lsb
242 (begin (set-ram (car dest) (bitwise-and result #xff))
243 (loop (cdr dest) (arithmetic-shift result -8))))))
244 ((or (eq? dest 'file) (not (= 0 (bitwise-and opcode #x200))))
245 ;; the result goes in memory (file)
246 (set-ram adr result-8bit))
248 ;; result goes in wreg
249 (set-wreg result-8bit)))
250 (if (not (eq? flags-changed 'none))
252 (set-zero-flag (if (= 0 result-8bit) 1 0))
253 (if (not (eq? flags-changed 'z))
255 (set-negative-flag (if (> result-8bit #x7f) 1 0))
256 (if (not (eq? flags-changed 'z-n))
258 (set-carry-flag (if (or (> result #xff)
261 (if (not (eq? flags-changed 'c-z-n))
263 (set-deccarry-flag 0);;;;;;;;;;;;;;
264 (set-overflow-flag 0))))))))))));;;;;;;;;;;;;;
266 (define (bit-oriented opcode mnemonic operation)
267 (let* ((f (bitwise-and opcode #xff))
268 (adr (if (= 0 (bitwise-and opcode #x100))
269 (if (= 0 (bitwise-and f #x80)) f (+ f #xf00))
270 (+ f (arithmetic-shift (get-bsr) 8))))
271 (b (bitwise-and (arithmetic-shift opcode -9) 7)))
273 (print (list (last-pc) " " mnemonic " "
274 (let ((x (assv adr file-reg-names)))
275 (if x (cdr x) (list "0x" (number->string adr 16))))
278 (cdr (assv b '((0 . C)
288 (let* ((result (operation (get-ram adr) b))
289 (result-8bit (bitwise-and result #xff)))
290 (set-ram adr result-8bit))))
292 (define (short-relative-branch opcode mnemonic branch)
293 (let* ((n (bitwise-and opcode #xff))
294 (adr (+ (get-pc) (* 2 (if (> n #x7f) (- n #x100) n)))))
296 (print (list (last-pc) " " mnemonic " "
297 (symbol->string (table-ref symbol-table adr)))))
303 (define (long-relative-branch opcode mnemonic call?)
304 (let* ((n (bitwise-and opcode #x7ff))
305 (adr (+ (get-pc) (* 2 (if (> n #x3ff) (- n #x800) n)))))
307 (print (list (last-pc) " " mnemonic " "
308 (symbol->string (table-ref symbol-table adr)))))
310 (stack-push (get-pc)))
314 (define (call-branch opcode mnemonic)
315 (let ((adr (* 2 (+ (bitwise-and opcode #xff)
316 (arithmetic-shift (bitwise-and (get-program-mem) #xfff) 8)))))
318 (print (list (last-pc) " " mnemonic " "
319 (symbol->string (table-ref symbol-table adr))
320 (if (= 0 (bitwise-and opcode #x100))
323 (stack-push (get-pc))
324 (if (not (= 0 (bitwise-and opcode #x100)))
325 (error "call fast not implemented"))
328 (define (goto-branch opcode mnemonic)
329 (let ((adr (* 2 (+ (bitwise-and opcode #xff)
330 (arithmetic-shift (bitwise-and (get-program-mem) #xfff) 8)))))
332 (print (list (last-pc) " " mnemonic " "
333 (symbol->string (table-ref symbol-table adr)))))
336 (define (literal-operation opcode mnemonic flags-changed operation)
337 (let ((k (bitwise-and opcode #xff)))
339 (print (list (last-pc) " " mnemonic " "
340 (if (< k 10) k (list "0x" (number->string k 16))))))
341 (let* ((result (operation k))
342 (result-8bit (bitwise-and result #xff)))
343 (set-wreg result-8bit)
344 (if (not (eq? flags-changed 'none))
346 (set-zero-flag (if (= 0 result-8bit) 1 0))
347 (if (not (eq? flags-changed 'z))
349 (set-negative-flag (if (> result-8bit #x7f) 1 0))
350 (if (not (eq? flags-changed 'z-n))
352 (set-carry-flag (if (> result #xff) 1 0))
353 (if (not (eq? flags-changed 'c-z-n))
355 (set-deccarry-flag 0);;;;;;;;;;;;;;
356 (set-overflow-flag 0))))))))))));;;;;;;;;;;;;;
358 (define (program-memory-read mnemonic read-adr-fun set-adr-fun)
360 (print (list (last-pc) " " mnemonic " ")))
361 (let ((adr (bitwise-ior (arithmetic-shift (get-ram TBLPTRU) 16)
362 (arithmetic-shift (get-ram TBLPTRH) 8)
364 (set-ram TABLAT (get-rom (bitwise-and (read-adr-fun adr)
365 ;; rom addresses are 21 bits wide
367 (let ((new-adr (bitwise-and (set-adr-fun adr) #x1fffff)))
368 (set-ram TBLPTRU (arithmetic-shift new-adr -16))
369 (set-ram TBLPTRH (bitwise-and (arithmetic-shift new-adr -8) #xff))
370 (set-ram TBLPTRL (bitwise-and new-adr #xff)))))
372 (define (get-program-mem)
373 (set! pic18-cycles (+ pic18-cycles 1))
376 (msb (get-rom (+ pc 1))))
377 (set-pc (+ (get-pc) 2))
378 (+ (arithmetic-shift msb 8) lsb)))
384 (substring (number->string (+ #x100 n) 16) 1 3))
392 (print (list (hex (u8vector-ref pic18-ram i)) " "))
394 (print (list " WREG=" (hex (get-wreg)) "\n")))
396 (define (pic18-execute)
398 (set! pic18-cycles 0)
406 (print (list "WREG = d'" (get-wreg) "'\n")))
407 (let ((opcode (get-program-mem)))
408 (let ((proc (vector-ref decode-vector (arithmetic-shift opcode -8))))
412 (define trace-instr #t)
415 (if (> pic18-carry-flag 0)
416 (begin (set! pic18-carry-flag #f)
420 ;------------------------------------------------------------------------------
422 ; Byte-oriented file register operations.
424 (decode-opcode #b001001 10
426 (byte-oriented opcode "addwf" 'c-dc-z-ov-n
430 (decode-opcode #b001000 10
432 (byte-oriented opcode "addwfc" 'c-dc-z-ov-n
434 (+ f (get-wreg) (carry))))))
436 (decode-opcode #b000101 10
438 (byte-oriented opcode "andwf" 'z-n
440 (bitwise-and f (get-wreg))))))
442 (decode-opcode #b0110101 9
444 (byte-oriented-file opcode "clrf" 'z
448 (decode-opcode #b000111 10
450 (byte-oriented opcode "comf" 'z-n
454 (decode-opcode #b0110001 9
456 (byte-oriented-file opcode "cpfseq" 'none
458 (if (= f (get-wreg)) (skip))
461 (decode-opcode #b0110010 9
463 (byte-oriented-file opcode "cpfsgt" 'none
465 (if (> f (get-wreg)) (skip))
468 (decode-opcode #b0110000 9
470 (byte-oriented-file opcode "cpfslt" 'none
472 (if (< f (get-wreg)) (skip))
475 (decode-opcode #b000001 10
477 (byte-oriented opcode "decf" 'c-dc-z-ov-n
481 (decode-opcode #b001011 10
483 (byte-oriented opcode "decfsz" 'none
488 (decode-opcode #b010011 10
490 (byte-oriented opcode "dcfsnz" 'none
492 (if (not (= f 1)) (skip))
495 (decode-opcode #b001010 10
497 (byte-oriented opcode "incf" 'c-dc-z-ov-n
501 (decode-opcode #b001111 10
503 (byte-oriented opcode "incfsz" 'none
505 (if (= f #xff) (skip))
508 (decode-opcode #b010010 10
510 (byte-oriented opcode "infsnz" 'none
512 (if (not (= f #xff)) (skip))
515 (decode-opcode #b000100 10
517 (byte-oriented opcode "iorwf" 'z-n
519 (bitwise-ior f (get-wreg))))))
521 (decode-opcode #b010100 10
523 (byte-oriented opcode "movf" 'z-n
527 (decode-opcode #b1100 12
529 (let* ((src (bitwise-and opcode #xfff))
530 ;; the destination is in the second 16-bit part, need to fetch
531 (dst (bitwise-and (get-program-mem) #xfff)))
533 (print (list (last-pc) " movff "
534 (let ((x (assv src file-reg-names)))
535 (if x (cdr x) (list "0x" (number->string src 16))))
537 (let ((x (assv dst file-reg-names)))
538 (if x (cdr x) (list "0x" (number->string dst 16)))) ;; TODO printing 2 args ruins the formatting
540 (set-ram dst (get-ram src)))))
542 (decode-opcode #b0110111 9
544 (byte-oriented-file opcode "movwf" 'none
548 (decode-opcode #b0000001 9
550 (byte-oriented-wide opcode "mulwf" 'none
553 (list PRODL PRODH))))
555 (decode-opcode #b0110110 9
557 (byte-oriented-file opcode "negf" 'c-dc-z-ov-n
561 (decode-opcode #b001101 10
563 (byte-oriented opcode "rlcf" 'c-z-n
565 ;; the carry flag will be set automatically
566 (+ (arithmetic-shift f 1) (carry))))))
568 (decode-opcode #b010001 10
570 (byte-oriented opcode "rlncf" 'z-n
572 (+ (arithmetic-shift f 1) (arithmetic-shift f -7))))))
574 (decode-opcode #b001100 10
576 (byte-oriented opcode "rrcf" 'c-z-n
578 (let ((r (+ (arithmetic-shift f -1) (arithmetic-shift (carry) 7))))
579 ;; roll through carry (if the result is over #xff, carry will be set)
580 (if (= (bitwise-and f 1) 1) (+ r #x100) r))))))
582 (decode-opcode #b010000 10
584 (byte-oriented opcode "rrncf" 'z-n
586 (+ (arithmetic-shift f -1) (arithmetic-shift f 7))))))
588 (decode-opcode #b0110100 9
590 (byte-oriented-file opcode "setf" 'z
594 (decode-opcode #b010101 10
596 (byte-oriented opcode "subfwb" 'c-dc-z-ov-n
598 (- (get-wreg) f (carry))))))
600 (decode-opcode #b010111 10
602 (byte-oriented opcode "subwf" 'c-dc-z-ov-n
606 (decode-opcode #b010110 10
608 (byte-oriented opcode "subwfb" 'c-dc-z-ov-n
610 (- f (get-wreg) (carry))))))
612 (decode-opcode #b001110 10
614 (byte-oriented opcode "swapf" 'none
616 (+ (arithmetic-shift f -4) (arithmetic-shift f 4))))))
618 (decode-opcode #b0110011 9
620 (byte-oriented-file opcode "tstfsz" 'none
622 (if (= f 0) (skip))))))
624 (decode-opcode #b000110 10
626 (byte-oriented opcode "xorwf" 'z-n
628 (bitwise-xor f (get-wreg))))))
630 ; Bit-oriented file register operations.
632 (decode-opcode #b1001 12
634 (bit-oriented opcode "bcf"
636 (bitwise-and f (bitwise-not (arithmetic-shift 1 b)))))))
638 (decode-opcode #b1000 12
640 (bit-oriented opcode "bsf"
642 (bitwise-ior f (arithmetic-shift 1 b))))))
644 (decode-opcode #b1011 12
646 (bit-oriented opcode "btfsc"
648 (if (= 0 (bitwise-and f (arithmetic-shift 1 b))) (skip))
651 (decode-opcode #b1010 12
653 (bit-oriented opcode "btfss"
655 (if (not (= 0 (bitwise-and f (arithmetic-shift 1 b)))) (skip))
658 (decode-opcode #b0111 12
660 (bit-oriented opcode "btg"
662 (bitwise-xor f (arithmetic-shift 1 b))))))
664 ; Control operations.
666 (decode-opcode #b11100010 8
668 (short-relative-branch opcode "bc"
670 (not (= 0 (carry)))))))
672 (decode-opcode #b11100110 8
674 (short-relative-branch opcode "bn" negative-flag?)))
676 (decode-opcode #b11100011 8
678 (short-relative-branch opcode "bnc"
682 (decode-opcode #b11100111 8
684 (short-relative-branch opcode "bnn" negative-flag?)))
686 (decode-opcode #b11100101 8
688 (short-relative-branch opcode "bnov"
690 (not (overflow-flag?))))))
692 (decode-opcode #b11100001 8
694 (short-relative-branch opcode "bnz"
696 (not (zero-flag?))))))
698 (decode-opcode #b11100100 8
700 (short-relative-branch opcode "bov" overflow-flag?)))
702 (decode-opcode #b11010 11
704 (long-relative-branch opcode "bra" #f)))
706 (decode-opcode #b11100000 8
708 (short-relative-branch opcode "bz" zero-flag?)))
710 (decode-opcode #b1110110 9
712 (call-branch opcode "call")))
714 (decode-opcode #b11101111 8
716 (goto-branch opcode "goto")))
718 (decode-opcode #b11011 11
720 (long-relative-branch opcode "rcall" #t)))
722 (decode-opcode #b1111 12
725 (print (list (last-pc) " nop ")))))
727 (decode-opcode #b00000000 8
729 (cond ((= opcode #b0000000000000100)
731 (print (list (last-pc) " clrwdt ")))
733 ((= opcode #b0000000000000111)
735 (print (list (last-pc) " daw ")))
737 ((= opcode #b0000000000000000)
739 (print (list (last-pc) " nop "))))
740 ((= opcode #b0000000000000110)
742 (print (list (last-pc) " pop ")))
744 ((= opcode #b0000000000000101)
746 (print (list (last-pc) " push ")))
747 (stack-push (get-pc)))
748 ((= opcode #b0000000011111111)
750 (print (list (last-pc) " reset ")))
752 ((= opcode #b0000000000010000)
754 (print (list (last-pc) " retfie ")))
757 ((= opcode #b0000000000010001)
759 (print (list (last-pc) " retfie FAST")))
760 (error "retfie fast not implemented")
763 ((= opcode #b0000000000010010)
765 (print (list (last-pc) " return ")))
768 ((= opcode #b0000000000010011)
770 (print (list (last-pc) " return FAST")))
771 (error "return fast not implemented")
774 ((= opcode #b0000000000000011)
776 (print (list (last-pc) " sleep ")))
777 (set! pic18-exit #t))
778 ;; program memory operations
779 ((= opcode #b0000000000001000)
780 (program-memory-read "tblrd*" identity identity))
781 ((= opcode #b0000000000001001)
782 (program-memory-read "tblrd*+" identity (lambda (adr) (+ adr 1))))
783 ((= opcode #b0000000000001010)
784 (program-memory-read "tblrd*-" identity (lambda (adr) (- adr 1))))
785 ((= opcode #b0000000000001011)
786 (program-memory-read "tblrd+*"
787 (lambda (adr) (+ adr 1))
788 (lambda (adr) (+ adr 1))))
789 ((= opcode #b0000000000001100)
790 (program-memory-write "tblwt*" identity identity)) ;; TODO not implemented
791 ((= opcode #b0000000000001101)
792 (program-memory-write "tblwt*+" identity (lambda (adr) (+ adr 1))))
793 ((= opcode #b0000000000001110)
794 (program-memory-write "tblwt*-" identity (lambda (adr) (- adr 1))))
795 ((= opcode #b0000000000001111)
796 (program-memory-write "tblwt+*"
797 (lambda (adr) (+ adr 1))
798 (lambda (adr) (+ adr 1))))
801 (print (list (last-pc) " ??? ")))
804 ; Literal operations.
806 (decode-opcode #b00001111 8
808 (literal-operation opcode "addlw" 'c-dc-z-ov-n
812 (decode-opcode #b00001011 8
814 (literal-operation opcode "andlw" 'z-n
816 (bitwise-and k (get-wreg))))))
818 (decode-opcode #b00001001 8
820 (literal-operation opcode "iorlw" 'z-n
822 (bitwise-ior k (get-wreg))))))
829 (make-listing "lfsr" (file-text f) (lit-text k)))
831 (asm-16 (bitmask "1110 1110 00ff kkkk" (file f) (quotient (lit k) 256)))
832 (asm-16 (bitmask "1111 0000 kkkk kkkk" (modulo (lit k) 256))))))
839 (make-listing "movlb" (lit-text k)))
841 (asm-16 (bitmask "0000 0001 0000 kkkk" (lit k))))))
843 (decode-opcode #b00001110 8
845 (literal-operation opcode "movlw" 'none
849 (decode-opcode #b00001101 8
851 (literal-operation opcode "mullw" 'none
855 (decode-opcode #b00001100 8
857 (literal-operation opcode "retlw" 'none
863 (decode-opcode #b00001000 8
865 (literal-operation opcode "sublw" 'c-dc-z-ov-n
869 (decode-opcode #b00001010 8
871 (literal-operation opcode "xorlw" 'z-n
873 (bitwise-xor k (get-wreg))))))
876 ;------------------------------------------------------------------------------
878 (define (read-hex-file filename)
880 (define addr-width 32)
882 (define (syntax-error)
883 (error "*** Syntax error in HEX file"))
886 (with-exception-catcher
890 (open-input-file filename)))))
892 (define mem (make-vector 16 #f))
894 (define (mem-store! a b)
897 (x (- addr-width 4)))
900 (let ((i (arithmetic-shift a (- x))))
901 (let ((v (vector-ref m i)))
903 (let ((v (make-vector 16 #f)))
906 (- a (arithmetic-shift i x))
911 (define (f m a n tail)
913 (define (g i a n tail)
915 (g (- i 1) (- a n) n (f (vector-ref m i) a n tail))
920 (cons (cons (- a 1) m) tail)
921 (g 15 a (quotient n 16) tail))
924 (f mem (expt 2 addr-width) (expt 2 addr-width) '()))
929 (define (read-hex-nibble)
930 (let ((c (read-char f)))
931 (cond ((and (char>=? c #\0) (char<=? c #\9))
932 (- (char->integer c) (char->integer #\0)))
933 ((and (char>=? c #\A) (char<=? c #\F))
934 (+ 10 (- (char->integer c) (char->integer #\A))))
935 ((and (char>=? c #\a) (char<=? c #\f))
936 (+ 10 (- (char->integer c) (char->integer #\a))))
940 (define (read-hex-byte)
941 (let* ((a (read-hex-nibble))
942 (b (read-hex-nibble)))
948 (let ((c (read-char f)))
949 (cond ((not (char? c)))
950 ((or (char=? c #\linefeed)
953 ((not (char=? c #\:))
956 (let* ((len (read-hex-byte))
959 (type (read-hex-byte)))
960 (let* ((adr (+ a2 (* 256 a1)))
961 (sum (+ len a1 a2 type)))
965 (let ((a (+ adr (* hi16 65536)))
968 (set! adr (modulo (+ adr 1) 65536))
977 (let* ((a1 (read-hex-byte))
978 (a2 (read-hex-byte)))
979 (set! sum (+ sum a1 a2))
980 (set! hi16 (+ a2 (* 256 a1)))))
983 (let ((check (read-hex-byte)))
984 (if (not (= (modulo (- sum) 256) check))
986 (let ((c (read-char f)))
987 (if (or (not (or (char=? c #\linefeed)
988 (char=? c #\return)))
996 (error "*** Could not open the HEX file")
999 ;------------------------------------------------------------------------------
1001 (define (execute-hex-files . filenames)
1002 (let ((programs (map read-hex-file filenames)))
1004 (for-each (lambda (prog)
1005 (for-each (lambda (x) (set-rom (car x) (cdr x)))
1009 (pic18-sim-cleanup)))