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) ;; TODO is this how the PIC18 hardware does it ?
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 (+ (arithmetic-shift f 1) (carry))))))
562 (decode-opcode #b010001 10
564 (byte-oriented opcode "rlncf" 'z-n
566 (+ (arithmetic-shift f 1) (arithmetic-shift f -7))))))
568 (decode-opcode #b001100 10
570 (byte-oriented opcode "rrcf" 'c-z-n
572 (+ (arithmetic-shift f -1) (arithmetic-shift (carry) 7))))))
574 (decode-opcode #b010000 10
576 (byte-oriented opcode "rrncf" 'z-n
578 (+ (arithmetic-shift f -1) (arithmetic-shift f 7))))))
580 (decode-opcode #b0110100 9
582 (byte-oriented-file opcode "setf" 'z
586 (decode-opcode #b010101 10
588 (byte-oriented opcode "subfwb" 'c-dc-z-ov-n
590 (- (get-wreg) f (carry)))))) ;; TODO was (- 1 (carry)), but caused problems with the other
592 (decode-opcode #b010111 10
594 (byte-oriented opcode "subwf" 'c-dc-z-ov-n
598 (decode-opcode #b010110 10
600 (byte-oriented opcode "subwfb" 'c-dc-z-ov-n
602 (- f (get-wreg) (carry)))))) ;; TODO !carry didn't work
604 (decode-opcode #b001110 10
606 (byte-oriented opcode "swapf" 'none
608 (+ (arithmetic-shift f -4) (arithmetic-shift f 4))))))
610 (decode-opcode #b0110011 9
612 (byte-oriented-file opcode "tstfsz" 'none
614 (if (= f 0) (skip))))))
616 (decode-opcode #b000110 10
618 (byte-oriented opcode "xorwf" 'z-n
620 (bitwise-xor f (get-wreg))))))
622 ; Bit-oriented file register operations.
624 (decode-opcode #b1001 12
626 (bit-oriented opcode "bcf"
628 (bitwise-and f (bitwise-not (arithmetic-shift 1 b)))))))
630 (decode-opcode #b1000 12
632 (bit-oriented opcode "bsf"
634 (bitwise-ior f (arithmetic-shift 1 b))))))
636 (decode-opcode #b1011 12
638 (bit-oriented opcode "btfsc"
640 (if (= 0 (bitwise-and f (arithmetic-shift 1 b))) (skip))
643 (decode-opcode #b1010 12
645 (bit-oriented opcode "btfss"
647 (if (not (= 0 (bitwise-and f (arithmetic-shift 1 b)))) (skip))
650 (decode-opcode #b0111 12
652 (bit-oriented opcode "btg"
654 (bitwise-xor f (arithmetic-shift 1 b))))))
656 ; Control operations.
658 (decode-opcode #b11100010 8
660 (short-relative-branch opcode "bc"
662 (not (= 0 (carry)))))))
664 (decode-opcode #b11100110 8
666 (short-relative-branch opcode "bn"
668 (not (= 0 (negative)))))))
670 (decode-opcode #b11100011 8
672 (short-relative-branch opcode "bnc"
676 (decode-opcode #b11100111 8
678 (short-relative-branch opcode "bnn"
682 (decode-opcode #b11100101 8
684 (short-relative-branch opcode "bnov"
688 (decode-opcode #b11100001 8
690 (short-relative-branch opcode "bnz"
694 (decode-opcode #b11100100 8
696 (short-relative-branch opcode "bov"
698 (not (= 0 (overflow)))))))
700 (decode-opcode #b11010 11
702 (long-relative-branch opcode "bra" #f)))
704 (decode-opcode #b11100000 8
706 (short-relative-branch opcode "bz"
708 (not (= 0 (zero)))))))
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))
780 (print (list (last-pc) " ??? ")))
783 ; Literal operations.
785 (decode-opcode #b00001111 8
787 (literal-operation opcode "addlw" 'c-dc-z-ov-n
791 (decode-opcode #b00001011 8
793 (literal-operation opcode "andlw" 'z-n
795 (bitwise-and k (get-wreg))))))
797 (decode-opcode #b00001001 8
799 (literal-operation opcode "iorlw" 'z-n
801 (bitwise-ior k (get-wreg))))))
808 (make-listing "lfsr" (file-text f) (lit-text k)))
810 (asm-16 (bitmask "1110 1110 00ff kkkk" (file f) (quotient (lit k) 256)))
811 (asm-16 (bitmask "1111 0000 kkkk kkkk" (modulo (lit k) 256))))))
818 (make-listing "movlb" (lit-text k)))
820 (asm-16 (bitmask "0000 0001 0000 kkkk" (lit k))))))
822 (decode-opcode #b00001110 8
824 (literal-operation opcode "movlw" 'none
828 (decode-opcode #b00001101 8
830 (literal-operation opcode "mullw" 'none
834 (decode-opcode #b00001100 8
836 (literal-operation opcode "retlw" 'none
842 (decode-opcode #b00001000 8
844 (literal-operation opcode "sublw" 'c-dc-z-ov-n
848 (decode-opcode #b00001010 8
850 (literal-operation opcode "xorlw" 'z-n
852 (bitwise-xor k (get-wreg))))))
854 ; Program memory operations.
861 (make-listing "tblrd*"))
863 (asm-16 (bitmask "0000 0000 0000 1000")))))
870 (make-listing "tblrd*+"))
872 (asm-16 (bitmask "0000 0000 0000 1001")))))
879 (make-listing "tblrd*-"))
881 (asm-16 (bitmask "0000 0000 0000 1010")))))
888 (make-listing "tblrd+*"))
890 (asm-16 (bitmask "0000 0000 0000 1011")))))
897 (make-listing "tblwt*"))
899 (asm-16 (bitmask "0000 0000 0000 1100")))))
906 (make-listing "tblwt*+"))
908 (asm-16 (bitmask "0000 0000 0000 1101")))))
915 (make-listing "tblwt*-"))
917 (asm-16 (bitmask "0000 0000 0000 1110")))))
924 (make-listing "tblwt+*"))
926 (asm-16 (bitmask "0000 0000 0000 1111")))))
928 ;------------------------------------------------------------------------------
930 (define (read-hex-file filename)
932 (define addr-width 32)
934 (define (syntax-error)
935 (error "*** Syntax error in HEX file"))
938 (with-exception-catcher
942 (open-input-file filename)))))
944 (define mem (make-vector 16 #f))
946 (define (mem-store! a b)
949 (x (- addr-width 4)))
952 (let ((i (arithmetic-shift a (- x))))
953 (let ((v (vector-ref m i)))
955 (let ((v (make-vector 16 #f)))
958 (- a (arithmetic-shift i x))
963 (define (f m a n tail)
965 (define (g i a n tail)
967 (g (- i 1) (- a n) n (f (vector-ref m i) a n tail))
972 (cons (cons (- a 1) m) tail)
973 (g 15 a (quotient n 16) tail))
976 (f mem (expt 2 addr-width) (expt 2 addr-width) '()))
981 (define (read-hex-nibble)
982 (let ((c (read-char f)))
983 (cond ((and (char>=? c #\0) (char<=? c #\9))
984 (- (char->integer c) (char->integer #\0)))
985 ((and (char>=? c #\A) (char<=? c #\F))
986 (+ 10 (- (char->integer c) (char->integer #\A))))
987 ((and (char>=? c #\a) (char<=? c #\f))
988 (+ 10 (- (char->integer c) (char->integer #\a))))
992 (define (read-hex-byte)
993 (let* ((a (read-hex-nibble))
994 (b (read-hex-nibble)))
1000 (let ((c (read-char f)))
1001 (cond ((not (char? c)))
1002 ((or (char=? c #\linefeed)
1003 (char=? c #\return))
1005 ((not (char=? c #\:))
1008 (let* ((len (read-hex-byte))
1009 (a1 (read-hex-byte))
1010 (a2 (read-hex-byte))
1011 (type (read-hex-byte)))
1012 (let* ((adr (+ a2 (* 256 a1)))
1013 (sum (+ len a1 a2 type)))
1017 (let ((a (+ adr (* hi16 65536)))
1018 (b (read-hex-byte)))
1020 (set! adr (modulo (+ adr 1) 65536))
1021 (set! sum (+ sum b))
1029 (let* ((a1 (read-hex-byte))
1030 (a2 (read-hex-byte)))
1031 (set! sum (+ sum a1 a2))
1032 (set! hi16 (+ a2 (* 256 a1)))))
1035 (let ((check (read-hex-byte)))
1036 (if (not (= (modulo (- sum) 256) check))
1038 (let ((c (read-char f)))
1039 (if (or (not (or (char=? c #\linefeed)
1040 (char=? c #\return)))
1044 (close-input-port f)
1048 (error "*** Could not open the HEX file")
1051 ;------------------------------------------------------------------------------
1053 (define (execute-hex-file filename)
1054 (let ((program (read-hex-file filename)))
1056 (for-each (lambda (x) (set-rom (car x) (cdr x))) program)
1058 (pic18-sim-cleanup)))