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"
671 (not (= 0 (negative)))))))
673 (decode-opcode #b11100011 8
675 (short-relative-branch opcode "bnc"
679 (decode-opcode #b11100111 8
681 (short-relative-branch opcode "bnn"
685 (decode-opcode #b11100101 8
687 (short-relative-branch opcode "bnov"
691 (decode-opcode #b11100001 8
693 (short-relative-branch opcode "bnz"
697 (decode-opcode #b11100100 8
699 (short-relative-branch opcode "bov"
701 (not (= 0 (overflow)))))))
703 (decode-opcode #b11010 11
705 (long-relative-branch opcode "bra" #f)))
707 (decode-opcode #b11100000 8
709 (short-relative-branch opcode "bz"
711 (not (= 0 (zero)))))))
713 (decode-opcode #b1110110 9
715 (call-branch opcode "call")))
717 (decode-opcode #b11101111 8
719 (goto-branch opcode "goto")))
721 (decode-opcode #b11011 11
723 (long-relative-branch opcode "rcall" #t)))
725 (decode-opcode #b1111 12
728 (print (list (last-pc) " nop ")))))
730 (decode-opcode #b00000000 8
732 (cond ((= opcode #b0000000000000100)
734 (print (list (last-pc) " clrwdt ")))
736 ((= opcode #b0000000000000111)
738 (print (list (last-pc) " daw ")))
740 ((= opcode #b0000000000000000)
742 (print (list (last-pc) " nop "))))
743 ((= opcode #b0000000000000110)
745 (print (list (last-pc) " pop ")))
747 ((= opcode #b0000000000000101)
749 (print (list (last-pc) " push ")))
750 (stack-push (get-pc)))
751 ((= opcode #b0000000011111111)
753 (print (list (last-pc) " reset ")))
755 ((= opcode #b0000000000010000)
757 (print (list (last-pc) " retfie ")))
760 ((= opcode #b0000000000010001)
762 (print (list (last-pc) " retfie FAST")))
763 (error "retfie fast not implemented")
766 ((= opcode #b0000000000010010)
768 (print (list (last-pc) " return ")))
771 ((= opcode #b0000000000010011)
773 (print (list (last-pc) " return FAST")))
774 (error "return fast not implemented")
777 ((= opcode #b0000000000000011)
779 (print (list (last-pc) " sleep ")))
780 (set! pic18-exit #t))
783 (print (list (last-pc) " ??? ")))
786 ; Literal operations.
788 (decode-opcode #b00001111 8
790 (literal-operation opcode "addlw" 'c-dc-z-ov-n
794 (decode-opcode #b00001011 8
796 (literal-operation opcode "andlw" 'z-n
798 (bitwise-and k (get-wreg))))))
800 (decode-opcode #b00001001 8
802 (literal-operation opcode "iorlw" 'z-n
804 (bitwise-ior k (get-wreg))))))
811 (make-listing "lfsr" (file-text f) (lit-text k)))
813 (asm-16 (bitmask "1110 1110 00ff kkkk" (file f) (quotient (lit k) 256)))
814 (asm-16 (bitmask "1111 0000 kkkk kkkk" (modulo (lit k) 256))))))
821 (make-listing "movlb" (lit-text k)))
823 (asm-16 (bitmask "0000 0001 0000 kkkk" (lit k))))))
825 (decode-opcode #b00001110 8
827 (literal-operation opcode "movlw" 'none
831 (decode-opcode #b00001101 8
833 (literal-operation opcode "mullw" 'none
837 (decode-opcode #b00001100 8
839 (literal-operation opcode "retlw" 'none
845 (decode-opcode #b00001000 8
847 (literal-operation opcode "sublw" 'c-dc-z-ov-n
851 (decode-opcode #b00001010 8
853 (literal-operation opcode "xorlw" 'z-n
855 (bitwise-xor k (get-wreg))))))
857 ; Program memory operations.
864 (make-listing "tblrd*"))
866 (asm-16 (bitmask "0000 0000 0000 1000")))))
873 (make-listing "tblrd*+"))
875 (asm-16 (bitmask "0000 0000 0000 1001")))))
882 (make-listing "tblrd*-"))
884 (asm-16 (bitmask "0000 0000 0000 1010")))))
891 (make-listing "tblrd+*"))
893 (asm-16 (bitmask "0000 0000 0000 1011")))))
900 (make-listing "tblwt*"))
902 (asm-16 (bitmask "0000 0000 0000 1100")))))
909 (make-listing "tblwt*+"))
911 (asm-16 (bitmask "0000 0000 0000 1101")))))
918 (make-listing "tblwt*-"))
920 (asm-16 (bitmask "0000 0000 0000 1110")))))
927 (make-listing "tblwt+*"))
929 (asm-16 (bitmask "0000 0000 0000 1111")))))
931 ;------------------------------------------------------------------------------
933 (define (read-hex-file filename)
935 (define addr-width 32)
937 (define (syntax-error)
938 (error "*** Syntax error in HEX file"))
941 (with-exception-catcher
945 (open-input-file filename)))))
947 (define mem (make-vector 16 #f))
949 (define (mem-store! a b)
952 (x (- addr-width 4)))
955 (let ((i (arithmetic-shift a (- x))))
956 (let ((v (vector-ref m i)))
958 (let ((v (make-vector 16 #f)))
961 (- a (arithmetic-shift i x))
966 (define (f m a n tail)
968 (define (g i a n tail)
970 (g (- i 1) (- a n) n (f (vector-ref m i) a n tail))
975 (cons (cons (- a 1) m) tail)
976 (g 15 a (quotient n 16) tail))
979 (f mem (expt 2 addr-width) (expt 2 addr-width) '()))
984 (define (read-hex-nibble)
985 (let ((c (read-char f)))
986 (cond ((and (char>=? c #\0) (char<=? c #\9))
987 (- (char->integer c) (char->integer #\0)))
988 ((and (char>=? c #\A) (char<=? c #\F))
989 (+ 10 (- (char->integer c) (char->integer #\A))))
990 ((and (char>=? c #\a) (char<=? c #\f))
991 (+ 10 (- (char->integer c) (char->integer #\a))))
995 (define (read-hex-byte)
996 (let* ((a (read-hex-nibble))
997 (b (read-hex-nibble)))
1003 (let ((c (read-char f)))
1004 (cond ((not (char? c)))
1005 ((or (char=? c #\linefeed)
1006 (char=? c #\return))
1008 ((not (char=? c #\:))
1011 (let* ((len (read-hex-byte))
1012 (a1 (read-hex-byte))
1013 (a2 (read-hex-byte))
1014 (type (read-hex-byte)))
1015 (let* ((adr (+ a2 (* 256 a1)))
1016 (sum (+ len a1 a2 type)))
1020 (let ((a (+ adr (* hi16 65536)))
1021 (b (read-hex-byte)))
1023 (set! adr (modulo (+ adr 1) 65536))
1024 (set! sum (+ sum b))
1032 (let* ((a1 (read-hex-byte))
1033 (a2 (read-hex-byte)))
1034 (set! sum (+ sum a1 a2))
1035 (set! hi16 (+ a2 (* 256 a1)))))
1038 (let ((check (read-hex-byte)))
1039 (if (not (= (modulo (- sum) 256) check))
1041 (let ((c (read-char f)))
1042 (if (or (not (or (char=? c #\linefeed)
1043 (char=? c #\return)))
1047 (close-input-port f)
1051 (error "*** Could not open the HEX file")
1054 ;------------------------------------------------------------------------------
1056 (define (execute-hex-file filename)
1057 (let ((program (read-hex-file filename)))
1059 (for-each (lambda (x) (set-rom (car x) (cdr x))) program)
1061 (pic18-sim-cleanup)))