1 ;;; File: "pic18-sim.scm"
5 ;------------------------------------------------------------------------------
9 (define pic18-stack #f)
11 (define pic18-wreg #f)
13 (define pic18-carry-flag #f)
14 (define pic18-deccarry-flag #f)
15 (define pic18-zero-flag #f)
16 (define pic18-overflow-flag #f)
17 (define pic18-negative-flag #f)
19 (define pic18-cycles #f)
20 (define pic18-exit #f)
24 (bitwise-and (arithmetic-shift (get-tos) -16) #xff))
26 (bitwise-and (arithmetic-shift (get-tos) -8) #xff))
28 (bitwise-and (get-tos) #xff))
30 (set-ram PCLATU (bitwise-and (arithmetic-shift (get-pc) -16) #x1f))
31 (set-ram PCLATH (bitwise-and (arithmetic-shift (get-pc) -8) #xff))
32 (bitwise-and (get-pc) #xfe))
35 (arithmetic-shift pic18-deccarry-flag 1)
36 (arithmetic-shift pic18-zero-flag 2)
37 (arithmetic-shift pic18-overflow-flag 3)
38 (arithmetic-shift pic18-negative-flag 4)))
39 ((assq adr (list (cons INDF0 (cons FSR0H FSR0L))
40 (cons INDF1 (cons FSR1H FSR1L))
41 (cons INDF2 (cons FSR2H FSR2L))))
44 (arithmetic-shift (bitwise-and (u8vector-ref pic18-ram
48 (u8vector-ref pic18-ram
50 ;; TODO pre/post inc/dec 0..2
52 (u8vector-ref pic18-ram adr))))
54 (define (set-ram adr byte)
56 (set-tos (+ (bitwise-and (get-tos) #x00ffff)
57 (arithmetic-shift (bitwise-and byte #x1f) 16))))
59 (set-tos (+ (bitwise-and (get-tos) #x1f00ff)
60 (arithmetic-shift byte 8))))
62 (set-tos (+ (bitwise-and (get-tos) #x1fff00)
65 (set-pc (+ (bitwise-and (arithmetic-shift (get-ram PCLATU) 16) #x1f)
66 (bitwise-and (arithmetic-shift (get-ram PCLATH) 8) #xff)
67 (bitwise-and byte #xfe))))
69 (set! pic18-carry-flag (bitwise-and byte 1))
70 (set! pic18-deccarry-flag (arithmetic-shift (bitwise-and byte 2) -1))
71 (set! pic18-zero-flag (arithmetic-shift (bitwise-and byte 4) -2))
72 (set! pic18-overflow-flag (arithmetic-shift (bitwise-and byte 8) -3))
73 (set! pic18-negative-flag (arithmetic-shift (bitwise-and byte 16) -4)))
74 ((assq adr (list (cons INDF0 (cons FSR0H FSR0L))
75 (cons INDF1 (cons FSR1H FSR1L))
76 (cons INDF2 (cons FSR2H FSR2L))))
78 (set-ram (bitwise-ior ;; TODO factor common code with get-ram ?
79 (arithmetic-shift (bitwise-and (u8vector-ref pic18-ram
83 (u8vector-ref pic18-ram
86 ;; TODO all other special array registers
88 (u8vector-set! pic18-ram adr byte))))
91 (u8vector-ref pic18-rom adr))
93 (define (set-rom adr byte)
94 (u8vector-set! pic18-rom adr byte))
96 (define (get-stack adr)
97 (vector-ref pic18-stack adr))
99 (define (set-stack adr pc)
100 (vector-set! pic18-stack adr pc))
109 (bitwise-and (get-ram STKPTR) #x1f))
114 (bitwise-and (get-ram STKPTR) #xe0))))
117 (vector-ref pic18-stack (- (get-sp) 1)))
120 (vector-set! pic18-stack (- (get-sp) 1) pc))
122 (define (stack-push pc)
123 (set-sp (+ (get-sp) 1))
128 (set-sp (- (get-sp) 1)))
131 (bitwise-and (get-ram BSR) #x0f))
136 (define (set-wreg byte)
137 (set! pic18-wreg byte))
140 (not (= 0 pic18-zero-flag)))
142 (define (set-zero-flag flag)
143 (set! pic18-zero-flag flag))
145 (define (negative-flag?)
146 (not (= 0 pic18-negative-flag)))
148 (define (set-negative-flag flag)
149 (set! pic18-negative-flag flag))
151 (define (carry-flag?)
152 (not (= 0 pic18-carry-flag)))
154 (define (set-carry-flag flag)
155 (set! pic18-carry-flag flag))
157 (define (deccarry-flag?)
158 (not (= 0 pic18-deccarry-flag)))
160 (define (set-deccarry-flag flag)
161 (set! pic18-deccarry-flag flag))
163 (define (overflow-flag?)
164 (not (= 0 pic18-overflow-flag)))
166 (define (set-overflow-flag flag)
167 (set! pic18-overflow-flag flag))
169 (define (pic18-sim-setup)
170 (set! pic18-ram (make-u8vector #x1000 0))
171 (set! pic18-rom (make-u8vector #x10000 0))
172 (set! pic18-stack (make-vector #x1f 0))
175 (set! pic18-carry-flag 0)
176 (set! pic18-deccarry-flag 0)
177 (set! pic18-zero-flag 0)
178 (set! pic18-overflow-flag 0)
179 (set! pic18-negative-flag 0))
181 (define (pic18-sim-cleanup)
184 (set! pic18-stack #f))
186 ;------------------------------------------------------------------------------
189 (let ((pc (- (get-pc) 2)))
190 (list (get-sp) " " (- pic18-cycles 1) " "
191 (substring (number->string (+ #x1000000 pc) 16) 1 7)
194 (define (illegal-opcode opcode)
196 (print (list (last-pc) " *illegal*")))
197 (error "illegal opcode" opcode))
199 (define decode-vector
200 (make-vector 256 illegal-opcode))
202 (define (decode-opcode opcode-bits shift action)
204 (error "shift=" shift))
205 (let ((n (arithmetic-shift 1 (- shift 8)))
206 (base (arithmetic-shift opcode-bits (- shift 8))))
210 (vector-set! decode-vector (+ base i) action)
213 (define (byte-oriented opcode mnemonic flags-changed operation)
214 (byte-oriented-aux opcode mnemonic flags-changed operation 'wreg))
215 (define (byte-oriented-file opcode mnemonic flags-changed operation)
216 (byte-oriented-aux opcode mnemonic flags-changed operation 'file))
217 (define (byte-oriented-wide opcode mnemonic flags-changed operation dest)
218 ;; for use with instructions that have results more than a byte wide, such
219 ;; as multiplication. the result goes at the given addresses
220 (byte-oriented-aux opcode mnemonic flags-changed operation dest)) ;; TODO do the same for literals
222 (define (byte-oriented-aux opcode mnemonic flags-changed operation dest)
223 (let* ((f (bitwise-and opcode #xff))
224 (adr (if (= 0 (bitwise-and opcode #x100))
225 (if (= 0 (bitwise-and f #x80)) f (+ f #xf00))
226 (+ f (arithmetic-shift (get-bsr) 8)))))
228 (print (list (last-pc) " " mnemonic " "
229 (let ((x (assv adr file-reg-names)))
230 (if x (cdr x) (list "0x" (number->string adr 16))))
231 (if (or (eq? dest 'wreg)
232 (= 0 (bitwise-and opcode #x200)))
236 (let* ((result (operation (get-ram adr)))
237 (result-8bit (bitwise-and result #xff)))
239 ;; result is more than a byte wide (i.e. multiplication)
240 ;; put it in the right destinations (dest is a list of addresses)
241 (let loop ((dest dest) (result result))
242 (if (not (null? dest))
243 ;; the head of the list is the lsb
244 (begin (set-ram (car dest) (bitwise-and result #xff))
245 (loop (cdr dest) (arithmetic-shift result -8))))))
246 ((or (eq? dest 'file) (not (= 0 (bitwise-and opcode #x200))))
247 ;; the result goes in memory (file)
248 (set-ram adr result-8bit))
250 ;; result goes in wreg
251 (set-wreg result-8bit)))
252 (if (not (eq? flags-changed 'none))
254 (set-zero-flag (if (= 0 result-8bit) 1 0))
255 (if (not (eq? flags-changed 'z))
257 (set-negative-flag (if (> result-8bit #x7f) 1 0))
258 (if (not (eq? flags-changed 'z-n))
260 (set-carry-flag (if (or (> result #xff)
263 (if (not (eq? flags-changed 'c-z-n))
265 (set-deccarry-flag 0);;;;;;;;;;;;;;
266 (set-overflow-flag 0))))))))))));;;;;;;;;;;;;;
268 (define (bit-oriented opcode mnemonic operation)
269 (let* ((f (bitwise-and opcode #xff))
270 (adr (if (= 0 (bitwise-and opcode #x100))
271 (if (= 0 (bitwise-and f #x80)) f (+ f #xf00))
272 (+ f (arithmetic-shift (get-bsr) 8))))
273 (b (bitwise-and (arithmetic-shift opcode -9) 7)))
275 (print (list (last-pc) " " mnemonic " "
276 (let ((x (assv adr file-reg-names)))
277 (if x (cdr x) (list "0x" (number->string adr 16))))
280 (cdr (assv b '((0 . C)
290 (let* ((result (operation (get-ram adr) b))
291 (result-8bit (bitwise-and result #xff)))
292 (set-ram adr result-8bit))))
294 (define (short-relative-branch opcode mnemonic branch)
295 (let* ((n (bitwise-and opcode #xff))
296 (adr (+ (get-pc) (* 2 (if (> n #x7f) (- n #x100) n)))))
298 (print (list (last-pc) " " mnemonic " "
299 (symbol->string (table-ref symbol-table adr)))))
305 (define (long-relative-branch opcode mnemonic call?)
306 (let* ((n (bitwise-and opcode #x7ff))
307 (adr (+ (get-pc) (* 2 (if (> n #x3ff) (- n #x800) n)))))
309 (print (list (last-pc) " " mnemonic " "
310 (symbol->string (table-ref symbol-table adr)))))
312 (stack-push (get-pc)))
316 (define (call-branch opcode mnemonic)
317 (let ((adr (* 2 (+ (bitwise-and opcode #xff)
318 (arithmetic-shift (bitwise-and (get-program-mem) #xfff) 8)))))
320 (print (list (last-pc) " " mnemonic " "
321 (symbol->string (table-ref symbol-table adr))
322 (if (= 0 (bitwise-and opcode #x100))
325 (stack-push (get-pc))
326 (if (not (= 0 (bitwise-and opcode #x100)))
327 (error "call fast not implemented"))
330 (define (goto-branch opcode mnemonic)
331 (let ((adr (* 2 (+ (bitwise-and opcode #xff)
332 (arithmetic-shift (bitwise-and (get-program-mem) #xfff) 8)))))
334 (print (list (last-pc) " " mnemonic " "
335 (symbol->string (table-ref symbol-table adr)))))
338 (define (literal-operation opcode mnemonic flags-changed operation)
339 (let ((k (bitwise-and opcode #xff)))
341 (print (list (last-pc) " " mnemonic " "
342 (if (< k 10) k (list "0x" (number->string k 16))))))
343 (let* ((result (operation k))
344 (result-8bit (bitwise-and result #xff)))
345 (set-wreg result-8bit)
346 (if (not (eq? flags-changed 'none))
348 (set-zero-flag (if (= 0 result-8bit) 1 0))
349 (if (not (eq? flags-changed 'z))
351 (set-negative-flag (if (> result-8bit #x7f) 1 0))
352 (if (not (eq? flags-changed 'z-n))
354 (set-carry-flag (if (> result #xff) 1 0))
355 (if (not (eq? flags-changed 'c-z-n))
357 (set-deccarry-flag 0);;;;;;;;;;;;;;
358 (set-overflow-flag 0))))))))))));;;;;;;;;;;;;;
360 (define (program-memory-read mnemonic read-adr-fun set-adr-fun)
362 (print (list (last-pc) " " mnemonic " ")))
363 (let ((adr (bitwise-ior (arithmetic-shift (get-ram TBLPTRU) 16)
364 (arithmetic-shift (get-ram TBLPTRH) 8)
366 (set-ram TABLAT (get-rom (bitwise-and (read-adr-fun adr)
367 ;; rom addresses are 21 bits wide
369 (let ((new-adr (bitwise-and (set-adr-fun adr) #x1fffff)))
370 (set-ram TBLPTRU (arithmetic-shift new-adr -16))
371 (set-ram TBLPTRH (bitwise-and (arithmetic-shift new-adr -8) #xff))
372 (set-ram TBLPTRL (bitwise-and new-adr #xff)))))
374 (define (get-program-mem)
375 (set! pic18-cycles (+ pic18-cycles 1))
378 (msb (get-rom (+ pc 1))))
379 (set-pc (+ (get-pc) 2))
380 (+ (arithmetic-shift msb 8) lsb)))
386 (substring (number->string (+ #x100 n) 16) 1 3))
394 (print (list (hex (u8vector-ref pic18-ram i)) " "))
396 (print (list " WREG=" (hex (get-wreg)) "\n")))
398 (define (pic18-execute)
400 (set! pic18-cycles 0)
408 (print (list "WREG = d'" (get-wreg) "'\n")))
409 (let ((opcode (get-program-mem)))
410 (let ((proc (vector-ref decode-vector (arithmetic-shift opcode -8))))
414 (define trace-instr #t)
417 (if (> pic18-carry-flag 0)
418 (begin (set! pic18-carry-flag #f)
422 ;------------------------------------------------------------------------------
424 ; Byte-oriented file register operations.
426 (decode-opcode #b001001 10
428 (byte-oriented opcode "addwf" 'c-dc-z-ov-n
432 (decode-opcode #b001000 10
434 (byte-oriented opcode "addwfc" 'c-dc-z-ov-n
436 (+ f (get-wreg) (carry))))))
438 (decode-opcode #b000101 10
440 (byte-oriented opcode "andwf" 'z-n
442 (bitwise-and f (get-wreg))))))
444 (decode-opcode #b0110101 9
446 (byte-oriented-file opcode "clrf" 'z
450 (decode-opcode #b000111 10
452 (byte-oriented opcode "comf" 'z-n
456 (decode-opcode #b0110001 9
458 (byte-oriented-file opcode "cpfseq" 'none
460 (if (= f (get-wreg)) (skip))
463 (decode-opcode #b0110010 9
465 (byte-oriented-file opcode "cpfsgt" 'none
467 (if (> f (get-wreg)) (skip))
470 (decode-opcode #b0110000 9
472 (byte-oriented-file opcode "cpfslt" 'none
474 (if (< f (get-wreg)) (skip))
477 (decode-opcode #b000001 10
479 (byte-oriented opcode "decf" 'c-dc-z-ov-n
483 (decode-opcode #b001011 10
485 (byte-oriented opcode "decfsz" 'none
490 (decode-opcode #b010011 10
492 (byte-oriented opcode "dcfsnz" 'none
494 (if (not (= f 1)) (skip))
497 (decode-opcode #b001010 10
499 (byte-oriented opcode "incf" 'c-dc-z-ov-n
503 (decode-opcode #b001111 10
505 (byte-oriented opcode "incfsz" 'none
507 (if (= f #xff) (skip))
510 (decode-opcode #b010010 10
512 (byte-oriented opcode "infsnz" 'none
514 (if (not (= f #xff)) (skip))
517 (decode-opcode #b000100 10
519 (byte-oriented opcode "iorwf" 'z-n
521 (bitwise-ior f (get-wreg))))))
523 (decode-opcode #b010100 10
525 (byte-oriented opcode "movf" 'z-n
529 (decode-opcode #b1100 12
531 (let* ((src (bitwise-and opcode #xfff))
532 ;; the destination is in the second 16-bit part, need to fetch
533 (dst (bitwise-and (get-program-mem) #xfff)))
535 (print (list (last-pc) " movff "
536 (let ((x (assv src file-reg-names)))
537 (if x (cdr x) (list "0x" (number->string src 16))))
539 (let ((x (assv dst file-reg-names)))
540 (if x (cdr x) (list "0x" (number->string dst 16)))) ;; TODO printing 2 args ruins the formatting
542 (set-ram dst (get-ram src)))))
544 (decode-opcode #b0110111 9
546 (byte-oriented-file opcode "movwf" 'none
550 (decode-opcode #b0000001 9
552 (byte-oriented-wide opcode "mulwf" 'none
555 (list PRODL PRODH))))
557 (decode-opcode #b0110110 9
559 (byte-oriented-file opcode "negf" 'c-dc-z-ov-n
563 (decode-opcode #b001101 10
565 (byte-oriented opcode "rlcf" 'c-z-n
567 ;; the carry flag will be set automatically
568 (+ (arithmetic-shift f 1) (carry))))))
570 (decode-opcode #b010001 10
572 (byte-oriented opcode "rlncf" 'z-n
574 (+ (arithmetic-shift f 1) (arithmetic-shift f -7))))))
576 (decode-opcode #b001100 10
578 (byte-oriented opcode "rrcf" 'c-z-n
580 (let ((r (+ (arithmetic-shift f -1) (arithmetic-shift (carry) 7))))
581 ;; roll through carry (if the result is over #xff, carry will be set)
582 (if (= (bitwise-and f 1) 1) (+ r #x100) r))))))
584 (decode-opcode #b010000 10
586 (byte-oriented opcode "rrncf" 'z-n
588 (+ (arithmetic-shift f -1) (arithmetic-shift f 7))))))
590 (decode-opcode #b0110100 9
592 (byte-oriented-file opcode "setf" 'z
596 (decode-opcode #b010101 10
598 (byte-oriented opcode "subfwb" 'c-dc-z-ov-n
600 (- (get-wreg) f (carry))))))
602 (decode-opcode #b010111 10
604 (byte-oriented opcode "subwf" 'c-dc-z-ov-n
608 (decode-opcode #b010110 10
610 (byte-oriented opcode "subwfb" 'c-dc-z-ov-n
612 (- f (get-wreg) (carry))))))
614 (decode-opcode #b001110 10
616 (byte-oriented opcode "swapf" 'none
618 (+ (arithmetic-shift f -4) (arithmetic-shift f 4))))))
620 (decode-opcode #b0110011 9
622 (byte-oriented-file opcode "tstfsz" 'none
624 (if (= f 0) (skip))))))
626 (decode-opcode #b000110 10
628 (byte-oriented opcode "xorwf" 'z-n
630 (bitwise-xor f (get-wreg))))))
632 ; Bit-oriented file register operations.
634 (decode-opcode #b1001 12
636 (bit-oriented opcode "bcf"
638 (bitwise-and f (bitwise-not (arithmetic-shift 1 b)))))))
640 (decode-opcode #b1000 12
642 (bit-oriented opcode "bsf"
644 (bitwise-ior f (arithmetic-shift 1 b))))))
646 (decode-opcode #b1011 12
648 (bit-oriented opcode "btfsc"
650 (if (= 0 (bitwise-and f (arithmetic-shift 1 b))) (skip))
653 (decode-opcode #b1010 12
655 (bit-oriented opcode "btfss"
657 (if (not (= 0 (bitwise-and f (arithmetic-shift 1 b)))) (skip))
660 (decode-opcode #b0111 12
662 (bit-oriented opcode "btg"
664 (bitwise-xor f (arithmetic-shift 1 b))))))
666 ; Control operations.
668 (decode-opcode #b11100010 8
670 (short-relative-branch opcode "bc"
672 (not (= 0 (carry)))))))
674 (decode-opcode #b11100110 8
676 (short-relative-branch opcode "bn" negative-flag?)))
678 (decode-opcode #b11100011 8
680 (short-relative-branch opcode "bnc"
684 (decode-opcode #b11100111 8
686 (short-relative-branch opcode "bnn" negative-flag?)))
688 (decode-opcode #b11100101 8
690 (short-relative-branch opcode "bnov"
692 (not (overflow-flag?))))))
694 (decode-opcode #b11100001 8
696 (short-relative-branch opcode "bnz"
698 (not (zero-flag?))))))
700 (decode-opcode #b11100100 8
702 (short-relative-branch opcode "bov" overflow-flag?)))
704 (decode-opcode #b11010 11
706 (long-relative-branch opcode "bra" #f)))
708 (decode-opcode #b11100000 8
710 (short-relative-branch opcode "bz" zero-flag?)))
712 (decode-opcode #b1110110 9
714 (call-branch opcode "call")))
716 (decode-opcode #b11101111 8
718 (goto-branch opcode "goto")))
720 (decode-opcode #b11011 11
722 (long-relative-branch opcode "rcall" #t)))
724 (decode-opcode #b1111 12
727 (print (list (last-pc) " nop ")))))
729 (decode-opcode #b00000000 8
731 (cond ((= opcode #b0000000000000100)
733 (print (list (last-pc) " clrwdt ")))
735 ((= opcode #b0000000000000111)
737 (print (list (last-pc) " daw ")))
739 ((= opcode #b0000000000000000)
741 (print (list (last-pc) " nop "))))
742 ((= opcode #b0000000000000110)
744 (print (list (last-pc) " pop ")))
746 ((= opcode #b0000000000000101)
748 (print (list (last-pc) " push ")))
749 (stack-push (get-pc)))
750 ((= opcode #b0000000011111111)
752 (print (list (last-pc) " reset ")))
754 ((= opcode #b0000000000010000)
756 (print (list (last-pc) " retfie ")))
759 ((= opcode #b0000000000010001)
761 (print (list (last-pc) " retfie FAST")))
762 (error "retfie fast not implemented")
765 ((= opcode #b0000000000010010)
767 (print (list (last-pc) " return ")))
770 ((= opcode #b0000000000010011)
772 (print (list (last-pc) " return FAST")))
773 (error "return fast not implemented")
776 ((= opcode #b0000000000000011)
778 (print (list (last-pc) " sleep ")))
779 (set! pic18-exit #t))
780 ;; program memory operations
781 ((= opcode #b0000000000001000)
782 (program-memory-read "tblrd*" identity identity))
783 ((= opcode #b0000000000001001)
784 (program-memory-read "tblrd*+" identity (lambda (adr) (+ adr 1))))
785 ((= opcode #b0000000000001010)
786 (program-memory-read "tblrd*-" identity (lambda (adr) (- adr 1))))
787 ((= opcode #b0000000000001011)
788 (program-memory-read "tblrd+*"
789 (lambda (adr) (+ adr 1))
790 (lambda (adr) (+ adr 1))))
791 ((= opcode #b0000000000001100)
792 (program-memory-write "tblwt*" identity identity)) ;; TODO not implemented
793 ((= opcode #b0000000000001101)
794 (program-memory-write "tblwt*+" identity (lambda (adr) (+ adr 1))))
795 ((= opcode #b0000000000001110)
796 (program-memory-write "tblwt*-" identity (lambda (adr) (- adr 1))))
797 ((= opcode #b0000000000001111)
798 (program-memory-write "tblwt+*"
799 (lambda (adr) (+ adr 1))
800 (lambda (adr) (+ adr 1))))
803 (print (list (last-pc) " ??? ")))
806 ; Literal operations.
808 (decode-opcode #b00001111 8
810 (literal-operation opcode "addlw" 'c-dc-z-ov-n
814 (decode-opcode #b00001011 8
816 (literal-operation opcode "andlw" 'z-n
818 (bitwise-and k (get-wreg))))))
820 (decode-opcode #b00001001 8
822 (literal-operation opcode "iorlw" 'z-n
824 (bitwise-ior k (get-wreg))))))
831 (make-listing "lfsr" (file-text f) (lit-text k)))
833 (asm-16 (bitmask "1110 1110 00ff kkkk" (file f) (quotient (lit k) 256)))
834 (asm-16 (bitmask "1111 0000 kkkk kkkk" (modulo (lit k) 256))))))
841 (make-listing "movlb" (lit-text k)))
843 (asm-16 (bitmask "0000 0001 0000 kkkk" (lit k))))))
845 (decode-opcode #b00001110 8
847 (literal-operation opcode "movlw" 'none
851 (decode-opcode #b00001101 8
853 (literal-operation opcode "mullw" 'none
857 (decode-opcode #b00001100 8
859 (literal-operation opcode "retlw" 'none
865 (decode-opcode #b00001000 8
867 (literal-operation opcode "sublw" 'c-dc-z-ov-n
871 (decode-opcode #b00001010 8
873 (literal-operation opcode "xorlw" 'z-n
875 (bitwise-xor k (get-wreg))))))
878 ;------------------------------------------------------------------------------
880 (define (read-hex-file filename)
882 (define addr-width 32)
884 (define (syntax-error)
885 (error "*** Syntax error in HEX file"))
888 (with-exception-catcher
892 (open-input-file filename)))))
894 (define mem (make-vector 16 #f))
896 (define (mem-store! a b)
899 (x (- addr-width 4)))
902 (let ((i (arithmetic-shift a (- x))))
903 (let ((v (vector-ref m i)))
905 (let ((v (make-vector 16 #f)))
908 (- a (arithmetic-shift i x))
913 (define (f m a n tail)
915 (define (g i a n tail)
917 (g (- i 1) (- a n) n (f (vector-ref m i) a n tail))
922 (cons (cons (- a 1) m) tail)
923 (g 15 a (quotient n 16) tail))
926 (f mem (expt 2 addr-width) (expt 2 addr-width) '()))
931 (define (read-hex-nibble)
932 (let ((c (read-char f)))
933 (cond ((and (char>=? c #\0) (char<=? c #\9))
934 (- (char->integer c) (char->integer #\0)))
935 ((and (char>=? c #\A) (char<=? c #\F))
936 (+ 10 (- (char->integer c) (char->integer #\A))))
937 ((and (char>=? c #\a) (char<=? c #\f))
938 (+ 10 (- (char->integer c) (char->integer #\a))))
942 (define (read-hex-byte)
943 (let* ((a (read-hex-nibble))
944 (b (read-hex-nibble)))
950 (let ((c (read-char f)))
951 (cond ((not (char? c)))
952 ((or (char=? c #\linefeed)
955 ((not (char=? c #\:))
958 (let* ((len (read-hex-byte))
961 (type (read-hex-byte)))
962 (let* ((adr (+ a2 (* 256 a1)))
963 (sum (+ len a1 a2 type)))
967 (let ((a (+ adr (* hi16 65536)))
970 (set! adr (modulo (+ adr 1) 65536))
979 (let* ((a1 (read-hex-byte))
980 (a2 (read-hex-byte)))
981 (set! sum (+ sum a1 a2))
982 (set! hi16 (+ a2 (* 256 a1)))))
985 (let ((check (read-hex-byte)))
986 (if (not (= (modulo (- sum) 256) check))
988 (let ((c (read-char f)))
989 (if (or (not (or (char=? c #\linefeed)
990 (char=? c #\return)))
998 (error "*** Could not open the HEX file")
1001 ;------------------------------------------------------------------------------
1003 (define (execute-hex-files . filenames)
1004 (let ((programs (map read-hex-file filenames)))
1006 (for-each (lambda (prog)
1007 (for-each (lambda (x) (set-rom (car x) (cdr x)))
1011 (pic18-sim-cleanup)))