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 (u8vector-ref pic18-ram
47 (u8vector-ref pic18-ram
49 ;; TODO pre/post inc/dec 0..2
51 (u8vector-ref pic18-ram adr))))
53 (define (set-ram adr byte)
55 (set-tos (+ (bitwise-and (get-tos) #x00ffff)
56 (arithmetic-shift (bitwise-and byte #x1f) 16))))
58 (set-tos (+ (bitwise-and (get-tos) #x1f00ff)
59 (arithmetic-shift byte 8))))
61 (set-tos (+ (bitwise-and (get-tos) #x1fff00)
64 (set-pc (+ (bitwise-and (arithmetic-shift (get-ram PCLATU) 16) #x1f)
65 (bitwise-and (arithmetic-shift (get-ram PCLATH) 8) #xff)
66 (bitwise-and byte #xfe))))
68 (set! pic18-carry-flag (bitwise-and byte 1))
69 (set! pic18-deccarry-flag (arithmetic-shift (bitwise-and byte 2) -1))
70 (set! pic18-zero-flag (arithmetic-shift (bitwise-and byte 4) -2))
71 (set! pic18-overflow-flag (arithmetic-shift (bitwise-and byte 8) -3))
72 (set! pic18-negative-flag (arithmetic-shift (bitwise-and byte 16) -4)))
73 ((assq adr (list (cons INDF0 (cons FSR0H FSR0L))
74 (cons INDF1 (cons FSR1H FSR1L))
75 (cons INDF2 (cons FSR2H FSR2L))))
77 (set-ram (bitwise-ior ;; TODO factor common code with get-ram ?
78 (arithmetic-shift (u8vector-ref pic18-ram
81 (u8vector-ref pic18-ram
84 ;; TODO all other special array registers
86 (u8vector-set! pic18-ram adr byte))))
89 (u8vector-ref pic18-rom adr))
91 (define (set-rom adr byte)
92 (u8vector-set! pic18-rom adr byte))
94 (define (get-stack adr)
95 (vector-ref pic18-stack adr))
97 (define (set-stack adr pc)
98 (vector-set! pic18-stack adr pc))
107 (bitwise-and (get-ram STKPTR) #x1f))
112 (bitwise-and (get-ram STKPTR) #xe0))))
115 (vector-ref pic18-stack (- (get-sp) 1)))
118 (vector-set! pic18-stack (- (get-sp) 1) pc))
120 (define (stack-push pc)
121 (set-sp (+ (get-sp) 1))
126 (set-sp (- (get-sp) 1)))
129 (bitwise-and (get-ram BSR) #x0f))
134 (define (set-wreg byte)
135 (set! pic18-wreg byte))
138 (not (= 0 pic18-zero-flag)))
140 (define (set-zero-flag flag)
141 (set! pic18-zero-flag flag))
143 (define (negative-flag?)
144 (not (= 0 pic18-negative-flag)))
146 (define (set-negative-flag flag)
147 (set! pic18-negative-flag flag))
149 (define (carry-flag?)
150 (not (= 0 pic18-carry-flag)))
152 (define (set-carry-flag flag)
153 (set! pic18-carry-flag flag))
155 (define (deccarry-flag?)
156 (not (= 0 pic18-deccarry-flag)))
158 (define (set-deccarry-flag flag)
159 (set! pic18-deccarry-flag flag))
161 (define (overflow-flag?)
162 (not (= 0 pic18-overflow-flag)))
164 (define (set-overflow-flag flag)
165 (set! pic18-overflow-flag flag))
167 (define (pic18-sim-setup)
168 (set! pic18-ram (make-u8vector #x1000 0))
169 (set! pic18-rom (make-u8vector #x2000 0))
170 (set! pic18-stack (make-vector #x1f 0))
173 (set! pic18-carry-flag 0)
174 (set! pic18-deccarry-flag 0)
175 (set! pic18-zero-flag 0)
176 (set! pic18-overflow-flag 0)
177 (set! pic18-negative-flag 0))
179 (define (pic18-sim-cleanup)
182 (set! pic18-stack #f))
184 ;------------------------------------------------------------------------------
187 (let ((pc (- (get-pc) 2)))
188 (list (get-sp) " " (- pic18-cycles 1) " "
189 (substring (number->string (+ #x1000000 pc) 16) 1 7)
192 (define (illegal-opcode opcode)
194 (print (list (last-pc) " *illegal*")))
195 (error "illegal opcode" opcode))
197 (define decode-vector
198 (make-vector 256 illegal-opcode))
200 (define (decode-opcode opcode-bits shift action)
202 (error "shift=" shift))
203 (let ((n (arithmetic-shift 1 (- shift 8)))
204 (base (arithmetic-shift opcode-bits (- shift 8))))
208 (vector-set! decode-vector (+ base i) action)
211 (define (byte-oriented opcode mnemonic flags-changed operation)
212 (byte-oriented-aux opcode mnemonic flags-changed operation 'wreg))
213 (define (byte-oriented-file opcode mnemonic flags-changed operation)
214 (byte-oriented-aux opcode mnemonic flags-changed operation 'file))
215 (define (byte-oriented-wide opcode mnemonic flags-changed operation dest)
216 ;; for use with instructions that have results more than a byte wide, such
217 ;; as multiplication. the result goes at the given addresses
218 (byte-oriented-aux opcode mnemonic flags-changed operation dest)) ;; TODO do the same for literals
220 (define (byte-oriented-aux opcode mnemonic flags-changed operation dest)
221 (let* ((f (bitwise-and opcode #xff))
222 (adr (if (= 0 (bitwise-and opcode #x100))
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 " "
298 (number->string adr 16)
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 " "
311 (number->string adr 16)
314 (stack-push (get-pc)))
318 (define (call-branch opcode mnemonic)
319 (let ((adr (* 2 (+ (bitwise-and opcode #xff)
320 (arithmetic-shift (get-program-mem) 8)))))
322 (print (list (last-pc) " " mnemonic " "
324 (number->string adr 16)
325 (if (= 0 (bitwise-and opcode #x100))
329 (stack-push (get-pc))
330 (if (not (= 0 (bitwise-and opcode #x100)))
331 (error "call fast not implemented"))
334 (define (goto-branch opcode mnemonic)
335 (let ((adr (* 2 (+ (bitwise-and opcode #xff)
336 (arithmetic-shift (get-program-mem) 8)))))
338 (print (list (last-pc) " " mnemonic " "
340 (number->string adr 16)
344 (define (literal-operation opcode mnemonic flags-changed operation)
345 (let ((k (bitwise-and opcode #xff)))
347 (print (list (last-pc) " " mnemonic " "
348 (if (< k 10) k (list "0x" (number->string k 16)))
350 (let* ((result (operation k))
351 (result-8bit (bitwise-and result #xff)))
352 (set-wreg result-8bit)
353 (if (not (eq? flags-changed 'none))
355 (set-zero-flag (if (= 0 result-8bit) 1 0))
356 (if (not (eq? flags-changed 'z))
358 (set-negative-flag (if (> result-8bit #x7f) 1 0))
359 (if (not (eq? flags-changed 'z-n))
361 (set-carry-flag (if (> result #xff) 1 0))
362 (if (not (eq? flags-changed 'c-z-n))
364 (set-deccarry-flag 0);;;;;;;;;;;;;;
365 (set-overflow-flag 0))))))))))));;;;;;;;;;;;;;
367 (define (get-program-mem)
368 (set! pic18-cycles (+ pic18-cycles 1))
371 (msb (get-rom (+ pc 1))))
372 (set-pc (+ (get-pc) 2))
373 (+ (arithmetic-shift msb 8) lsb)))
379 (substring (number->string (+ #x100 n) 16) 1 3))
387 (print (list (hex (u8vector-ref pic18-ram i)) " "))
389 (print (list " WREG=" (hex (get-wreg)) "\n")))
391 (define (pic18-execute)
393 (set! pic18-cycles 0)
401 (print (list "WREG = d'" (get-wreg) "'\n")))
402 (let ((opcode (get-program-mem)))
403 (let ((proc (vector-ref decode-vector (arithmetic-shift opcode -8))))
407 (define trace-instr #t)
410 (if (> pic18-carry-flag 0)
411 (begin (set! pic18-carry-flag #f)
415 ;------------------------------------------------------------------------------
417 ; Byte-oriented file register operations.
419 (decode-opcode #b001001 10
421 (byte-oriented opcode "addwf" 'c-dc-z-ov-n
425 (decode-opcode #b001000 10
427 (byte-oriented opcode "addwfc" 'c-dc-z-ov-n
429 (+ f (get-wreg) (carry))))))
431 (decode-opcode #b000101 10
433 (byte-oriented opcode "andwf" 'z-n
435 (bitwise-and f (get-wreg))))))
437 (decode-opcode #b0110101 9
439 (byte-oriented-file opcode "clrf" 'z
443 (decode-opcode #b000111 10
445 (byte-oriented opcode "comf" 'z-n
449 (decode-opcode #b0110001 9
451 (byte-oriented-file opcode "cpfseq" 'none
453 (if (= f (get-wreg)) (skip))
456 (decode-opcode #b0110010 9
458 (byte-oriented-file opcode "cpfsgt" 'none
460 (if (> f (get-wreg)) (skip))
463 (decode-opcode #b0110000 9
465 (byte-oriented-file opcode "cpfslt" 'none
467 (if (< f (get-wreg)) (skip))
470 (decode-opcode #b000001 10
472 (byte-oriented opcode "decf" 'c-dc-z-ov-n
476 (decode-opcode #b001011 10
478 (byte-oriented opcode "decfsz" 'none
483 (decode-opcode #b010011 10
485 (byte-oriented opcode "dcfsnz" 'none
487 (if (not (= f 1)) (skip))
490 (decode-opcode #b001010 10
492 (byte-oriented opcode "incf" 'c-dc-z-ov-n
496 (decode-opcode #b001111 10
498 (byte-oriented opcode "incfsz" 'none
500 (if (= f #xff) (skip))
503 (decode-opcode #b010010 10
505 (byte-oriented opcode "infsnz" 'none
507 (if (not (= f #xff)) (skip))
510 (decode-opcode #b000100 10
512 (byte-oriented opcode "iorwf" 'z-n
514 (bitwise-ior f (get-wreg))))))
516 (decode-opcode #b010100 10
518 (byte-oriented opcode "movf" 'z-n
522 (decode-opcode #b1100 12
524 (let* ((src (bitwise-and opcode #xfff))
525 ;; the destination is in the second 16-bit part, need to fetch
526 (dst (bitwise-and (get-program-mem) #xfff)))
528 (print (list (last-pc) " movff "
529 (let ((x (assv src file-reg-names)))
530 (if x (cdr x) (list "0x" (number->string src 16))))
532 (let ((x (assv dst file-reg-names)))
533 (if x (cdr x) (list "0x" (number->string dst 16)))) ;; TODO printing 2 args ruins the formatting
535 (set-ram dst (get-ram src)))))
537 (decode-opcode #b0110111 9
539 (byte-oriented-file opcode "movwf" 'none
543 (decode-opcode #b0000001 9
545 (byte-oriented-wide opcode "mulwf" 'none
548 (list PRODL PRODH))))
550 (decode-opcode #b0110110 9
552 (byte-oriented-file opcode "negf" 'c-dc-z-ov-n
556 (decode-opcode #b001101 10
558 (byte-oriented opcode "rlcf" 'c-z-n
560 ;; the carry flasg will be set automatically
561 (+ (arithmetic-shift f 1) (carry))))))
563 (decode-opcode #b010001 10
565 (byte-oriented opcode "rlncf" 'z-n
567 (+ (arithmetic-shift f 1) (arithmetic-shift f -7))))))
569 (decode-opcode #b001100 10
571 (byte-oriented opcode "rrcf" 'c-z-n
573 (let ((r (+ (arithmetic-shift f -1) (arithmetic-shift (carry) 7))))
574 ;; roll through carry (if the result is over #xff, carry will be set)
575 (if (= (bitwise-and f 1) 1) (+ r #x100) r))))))
577 (decode-opcode #b010000 10
579 (byte-oriented opcode "rrncf" 'z-n
581 (+ (arithmetic-shift f -1) (arithmetic-shift f 7))))))
583 (decode-opcode #b0110100 9
585 (byte-oriented-file opcode "setf" 'z
589 (decode-opcode #b010101 10
591 (byte-oriented opcode "subfwb" 'c-dc-z-ov-n
593 (- (get-wreg) f (carry))))))
595 (decode-opcode #b010111 10
597 (byte-oriented opcode "subwf" 'c-dc-z-ov-n
601 (decode-opcode #b010110 10
603 (byte-oriented opcode "subwfb" 'c-dc-z-ov-n
605 (- f (get-wreg) (carry))))))
607 (decode-opcode #b001110 10
609 (byte-oriented opcode "swapf" 'none
611 (+ (arithmetic-shift f -4) (arithmetic-shift f 4))))))
613 (decode-opcode #b0110011 9
615 (byte-oriented-file opcode "tstfsz" 'none
617 (if (= f 0) (skip))))))
619 (decode-opcode #b000110 10
621 (byte-oriented opcode "xorwf" 'z-n
623 (bitwise-xor f (get-wreg))))))
625 ; Bit-oriented file register operations.
627 (decode-opcode #b1001 12
629 (bit-oriented opcode "bcf"
631 (bitwise-and f (bitwise-not (arithmetic-shift 1 b)))))))
633 (decode-opcode #b1000 12
635 (bit-oriented opcode "bsf"
637 (bitwise-ior f (arithmetic-shift 1 b))))))
639 (decode-opcode #b1011 12
641 (bit-oriented opcode "btfsc"
643 (if (= 0 (bitwise-and f (arithmetic-shift 1 b))) (skip))
646 (decode-opcode #b1010 12
648 (bit-oriented opcode "btfss"
650 (if (not (= 0 (bitwise-and f (arithmetic-shift 1 b)))) (skip))
653 (decode-opcode #b0111 12
655 (bit-oriented opcode "btg"
657 (bitwise-xor f (arithmetic-shift 1 b))))))
659 ; Control operations.
661 (decode-opcode #b11100010 8
663 (short-relative-branch opcode "bc"
665 (not (= 0 (carry)))))))
667 (decode-opcode #b11100110 8
669 (short-relative-branch opcode "bn" negative-flag?)))
671 (decode-opcode #b11100011 8
673 (short-relative-branch opcode "bnc"
677 (decode-opcode #b11100111 8
679 (short-relative-branch opcode "bnn" negative-flag?)))
681 (decode-opcode #b11100101 8
683 (short-relative-branch opcode "bnov"
685 (not (overflow-flag?))))))
687 (decode-opcode #b11100001 8
689 (short-relative-branch opcode "bnz"
691 (not (zero-flag?))))))
693 (decode-opcode #b11100100 8
695 (short-relative-branch opcode "bov" overflow-flag?)))
697 (decode-opcode #b11010 11
699 (long-relative-branch opcode "bra" #f)))
701 (decode-opcode #b11100000 8
703 (short-relative-branch opcode "bz" zero-flag?)))
705 (decode-opcode #b1110110 9
707 (call-branch opcode "call")))
709 (decode-opcode #b11101111 8
711 (goto-branch opcode "goto")))
713 (decode-opcode #b11011 11
715 (long-relative-branch opcode "rcall" #t)))
717 (decode-opcode #b1111 12
720 (print (list (last-pc) " nop ")))))
722 (decode-opcode #b00000000 8
724 (cond ((= opcode #b0000000000000100)
726 (print (list (last-pc) " clrwdt ")))
728 ((= opcode #b0000000000000111)
730 (print (list (last-pc) " daw ")))
732 ((= opcode #b0000000000000000)
734 (print (list (last-pc) " nop "))))
735 ((= opcode #b0000000000000110)
737 (print (list (last-pc) " pop ")))
739 ((= opcode #b0000000000000101)
741 (print (list (last-pc) " push ")))
742 (stack-push (get-pc)))
743 ((= opcode #b0000000011111111)
745 (print (list (last-pc) " reset ")))
747 ((= opcode #b0000000000010000)
749 (print (list (last-pc) " retfie ")))
752 ((= opcode #b0000000000010001)
754 (print (list (last-pc) " retfie FAST")))
755 (error "retfie fast not implemented")
758 ((= opcode #b0000000000010010)
760 (print (list (last-pc) " return ")))
763 ((= opcode #b0000000000010011)
765 (print (list (last-pc) " return FAST")))
766 (error "return fast not implemented")
769 ((= opcode #b0000000000000011)
771 (print (list (last-pc) " sleep ")))
772 (set! pic18-exit #t))
775 (print (list (last-pc) " ??? ")))
778 ; Literal operations.
780 (decode-opcode #b00001111 8
782 (literal-operation opcode "addlw" 'c-dc-z-ov-n
786 (decode-opcode #b00001011 8
788 (literal-operation opcode "andlw" 'z-n
790 (bitwise-and k (get-wreg))))))
792 (decode-opcode #b00001001 8
794 (literal-operation opcode "iorlw" 'z-n
796 (bitwise-ior k (get-wreg))))))
803 (make-listing "lfsr" (file-text f) (lit-text k)))
805 (asm-16 (bitmask "1110 1110 00ff kkkk" (file f) (quotient (lit k) 256)))
806 (asm-16 (bitmask "1111 0000 kkkk kkkk" (modulo (lit k) 256))))))
813 (make-listing "movlb" (lit-text k)))
815 (asm-16 (bitmask "0000 0001 0000 kkkk" (lit k))))))
817 (decode-opcode #b00001110 8
819 (literal-operation opcode "movlw" 'none
823 (decode-opcode #b00001101 8
825 (literal-operation opcode "mullw" 'none
829 (decode-opcode #b00001100 8
831 (literal-operation opcode "retlw" 'none
837 (decode-opcode #b00001000 8
839 (literal-operation opcode "sublw" 'c-dc-z-ov-n
843 (decode-opcode #b00001010 8
845 (literal-operation opcode "xorlw" 'z-n
847 (bitwise-xor k (get-wreg))))))
849 ; Program memory operations.
856 (make-listing "tblrd*"))
858 (asm-16 (bitmask "0000 0000 0000 1000")))))
865 (make-listing "tblrd*+"))
867 (asm-16 (bitmask "0000 0000 0000 1001")))))
874 (make-listing "tblrd*-"))
876 (asm-16 (bitmask "0000 0000 0000 1010")))))
883 (make-listing "tblrd+*"))
885 (asm-16 (bitmask "0000 0000 0000 1011")))))
892 (make-listing "tblwt*"))
894 (asm-16 (bitmask "0000 0000 0000 1100")))))
901 (make-listing "tblwt*+"))
903 (asm-16 (bitmask "0000 0000 0000 1101")))))
910 (make-listing "tblwt*-"))
912 (asm-16 (bitmask "0000 0000 0000 1110")))))
919 (make-listing "tblwt+*"))
921 (asm-16 (bitmask "0000 0000 0000 1111")))))
923 ;------------------------------------------------------------------------------
925 (define (read-hex-file filename)
927 (define addr-width 32)
929 (define (syntax-error)
930 (error "*** Syntax error in HEX file"))
933 (with-exception-catcher
937 (open-input-file filename)))))
939 (define mem (make-vector 16 #f))
941 (define (mem-store! a b)
944 (x (- addr-width 4)))
947 (let ((i (arithmetic-shift a (- x))))
948 (let ((v (vector-ref m i)))
950 (let ((v (make-vector 16 #f)))
953 (- a (arithmetic-shift i x))
958 (define (f m a n tail)
960 (define (g i a n tail)
962 (g (- i 1) (- a n) n (f (vector-ref m i) a n tail))
967 (cons (cons (- a 1) m) tail)
968 (g 15 a (quotient n 16) tail))
971 (f mem (expt 2 addr-width) (expt 2 addr-width) '()))
976 (define (read-hex-nibble)
977 (let ((c (read-char f)))
978 (cond ((and (char>=? c #\0) (char<=? c #\9))
979 (- (char->integer c) (char->integer #\0)))
980 ((and (char>=? c #\A) (char<=? c #\F))
981 (+ 10 (- (char->integer c) (char->integer #\A))))
982 ((and (char>=? c #\a) (char<=? c #\f))
983 (+ 10 (- (char->integer c) (char->integer #\a))))
987 (define (read-hex-byte)
988 (let* ((a (read-hex-nibble))
989 (b (read-hex-nibble)))
995 (let ((c (read-char f)))
996 (cond ((not (char? c)))
997 ((or (char=? c #\linefeed)
1000 ((not (char=? c #\:))
1003 (let* ((len (read-hex-byte))
1004 (a1 (read-hex-byte))
1005 (a2 (read-hex-byte))
1006 (type (read-hex-byte)))
1007 (let* ((adr (+ a2 (* 256 a1)))
1008 (sum (+ len a1 a2 type)))
1012 (let ((a (+ adr (* hi16 65536)))
1013 (b (read-hex-byte)))
1015 (set! adr (modulo (+ adr 1) 65536))
1016 (set! sum (+ sum b))
1024 (let* ((a1 (read-hex-byte))
1025 (a2 (read-hex-byte)))
1026 (set! sum (+ sum a1 a2))
1027 (set! hi16 (+ a2 (* 256 a1)))))
1030 (let ((check (read-hex-byte)))
1031 (if (not (= (modulo (- sum) 256) check))
1033 (let ((c (read-char f)))
1034 (if (or (not (or (char=? c #\linefeed)
1035 (char=? c #\return)))
1039 (close-input-port f)
1043 (error "*** Could not open the HEX file")
1046 ;------------------------------------------------------------------------------
1048 (define (execute-hex-file filename)
1049 (let ((program (read-hex-file filename)))
1051 (for-each (lambda (x) (set-rom (car x) (cdr x))) program)
1053 (pic18-sim-cleanup)))