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 " "
300 (number->string adr 16)
307 (define (long-relative-branch opcode mnemonic call?)
308 (let* ((n (bitwise-and opcode #x7ff))
309 (adr (+ (get-pc) (* 2 (if (> n #x3ff) (- n #x800) n)))))
311 (print (list (last-pc) " " mnemonic " "
313 (number->string adr 16)
316 (stack-push (get-pc)))
320 (define (call-branch opcode mnemonic)
321 (let ((adr (* 2 (+ (bitwise-and opcode #xff)
322 (arithmetic-shift (bitwise-and (get-program-mem) #xfff) 8)))))
324 (print (list (last-pc) " " mnemonic " "
326 (number->string adr 16)
327 (if (= 0 (bitwise-and opcode #x100))
331 (stack-push (get-pc))
332 (if (not (= 0 (bitwise-and opcode #x100)))
333 (error "call fast not implemented"))
336 (define (goto-branch opcode mnemonic)
337 (let ((adr (* 2 (+ (bitwise-and opcode #xff)
338 (arithmetic-shift (bitwise-and (get-program-mem) #xfff) 8)))))
340 (print (list (last-pc) " " mnemonic " "
342 (number->string adr 16)
346 (define (literal-operation opcode mnemonic flags-changed operation)
347 (let ((k (bitwise-and opcode #xff)))
349 (print (list (last-pc) " " mnemonic " "
350 (if (< k 10) k (list "0x" (number->string k 16)))
352 (let* ((result (operation k))
353 (result-8bit (bitwise-and result #xff)))
354 (set-wreg result-8bit)
355 (if (not (eq? flags-changed 'none))
357 (set-zero-flag (if (= 0 result-8bit) 1 0))
358 (if (not (eq? flags-changed 'z))
360 (set-negative-flag (if (> result-8bit #x7f) 1 0))
361 (if (not (eq? flags-changed 'z-n))
363 (set-carry-flag (if (> result #xff) 1 0))
364 (if (not (eq? flags-changed 'c-z-n))
366 (set-deccarry-flag 0);;;;;;;;;;;;;;
367 (set-overflow-flag 0))))))))))));;;;;;;;;;;;;;
369 (define (get-program-mem)
370 (set! pic18-cycles (+ pic18-cycles 1))
373 (msb (get-rom (+ pc 1))))
374 (set-pc (+ (get-pc) 2))
375 (+ (arithmetic-shift msb 8) lsb)))
381 (substring (number->string (+ #x100 n) 16) 1 3))
389 (print (list (hex (u8vector-ref pic18-ram i)) " "))
391 (print (list " WREG=" (hex (get-wreg)) "\n")))
393 (define (pic18-execute)
395 (set! pic18-cycles 0)
403 (print (list "WREG = d'" (get-wreg) "'\n")))
404 (let ((opcode (get-program-mem)))
405 (let ((proc (vector-ref decode-vector (arithmetic-shift opcode -8))))
409 (define trace-instr #t)
412 (if (> pic18-carry-flag 0)
413 (begin (set! pic18-carry-flag #f)
417 ;------------------------------------------------------------------------------
419 ; Byte-oriented file register operations.
421 (decode-opcode #b001001 10
423 (byte-oriented opcode "addwf" 'c-dc-z-ov-n
427 (decode-opcode #b001000 10
429 (byte-oriented opcode "addwfc" 'c-dc-z-ov-n
431 (+ f (get-wreg) (carry))))))
433 (decode-opcode #b000101 10
435 (byte-oriented opcode "andwf" 'z-n
437 (bitwise-and f (get-wreg))))))
439 (decode-opcode #b0110101 9
441 (byte-oriented-file opcode "clrf" 'z
445 (decode-opcode #b000111 10
447 (byte-oriented opcode "comf" 'z-n
451 (decode-opcode #b0110001 9
453 (byte-oriented-file opcode "cpfseq" 'none
455 (if (= f (get-wreg)) (skip))
458 (decode-opcode #b0110010 9
460 (byte-oriented-file opcode "cpfsgt" 'none
462 (if (> f (get-wreg)) (skip))
465 (decode-opcode #b0110000 9
467 (byte-oriented-file opcode "cpfslt" 'none
469 (if (< f (get-wreg)) (skip))
472 (decode-opcode #b000001 10
474 (byte-oriented opcode "decf" 'c-dc-z-ov-n
478 (decode-opcode #b001011 10
480 (byte-oriented opcode "decfsz" 'none
485 (decode-opcode #b010011 10
487 (byte-oriented opcode "dcfsnz" 'none
489 (if (not (= f 1)) (skip))
492 (decode-opcode #b001010 10
494 (byte-oriented opcode "incf" 'c-dc-z-ov-n
498 (decode-opcode #b001111 10
500 (byte-oriented opcode "incfsz" 'none
502 (if (= f #xff) (skip))
505 (decode-opcode #b010010 10
507 (byte-oriented opcode "infsnz" 'none
509 (if (not (= f #xff)) (skip))
512 (decode-opcode #b000100 10
514 (byte-oriented opcode "iorwf" 'z-n
516 (bitwise-ior f (get-wreg))))))
518 (decode-opcode #b010100 10
520 (byte-oriented opcode "movf" 'z-n
524 (decode-opcode #b1100 12
526 (let* ((src (bitwise-and opcode #xfff))
527 ;; the destination is in the second 16-bit part, need to fetch
528 (dst (bitwise-and (get-program-mem) #xfff)))
530 (print (list (last-pc) " movff "
531 (let ((x (assv src file-reg-names)))
532 (if x (cdr x) (list "0x" (number->string src 16))))
534 (let ((x (assv dst file-reg-names)))
535 (if x (cdr x) (list "0x" (number->string dst 16)))) ;; TODO printing 2 args ruins the formatting
537 (set-ram dst (get-ram src)))))
539 (decode-opcode #b0110111 9
541 (byte-oriented-file opcode "movwf" 'none
545 (decode-opcode #b0000001 9
547 (byte-oriented-wide opcode "mulwf" 'none
550 (list PRODL PRODH))))
552 (decode-opcode #b0110110 9
554 (byte-oriented-file opcode "negf" 'c-dc-z-ov-n
558 (decode-opcode #b001101 10
560 (byte-oriented opcode "rlcf" 'c-z-n
562 ;; the carry flasg will be set automatically
563 (+ (arithmetic-shift f 1) (carry))))))
565 (decode-opcode #b010001 10
567 (byte-oriented opcode "rlncf" 'z-n
569 (+ (arithmetic-shift f 1) (arithmetic-shift f -7))))))
571 (decode-opcode #b001100 10
573 (byte-oriented opcode "rrcf" 'c-z-n
575 (let ((r (+ (arithmetic-shift f -1) (arithmetic-shift (carry) 7))))
576 ;; roll through carry (if the result is over #xff, carry will be set)
577 (if (= (bitwise-and f 1) 1) (+ r #x100) r))))))
579 (decode-opcode #b010000 10
581 (byte-oriented opcode "rrncf" 'z-n
583 (+ (arithmetic-shift f -1) (arithmetic-shift f 7))))))
585 (decode-opcode #b0110100 9
587 (byte-oriented-file opcode "setf" 'z
591 (decode-opcode #b010101 10
593 (byte-oriented opcode "subfwb" 'c-dc-z-ov-n
595 (- (get-wreg) f (carry))))))
597 (decode-opcode #b010111 10
599 (byte-oriented opcode "subwf" 'c-dc-z-ov-n
603 (decode-opcode #b010110 10
605 (byte-oriented opcode "subwfb" 'c-dc-z-ov-n
607 (- f (get-wreg) (carry))))))
609 (decode-opcode #b001110 10
611 (byte-oriented opcode "swapf" 'none
613 (+ (arithmetic-shift f -4) (arithmetic-shift f 4))))))
615 (decode-opcode #b0110011 9
617 (byte-oriented-file opcode "tstfsz" 'none
619 (if (= f 0) (skip))))))
621 (decode-opcode #b000110 10
623 (byte-oriented opcode "xorwf" 'z-n
625 (bitwise-xor f (get-wreg))))))
627 ; Bit-oriented file register operations.
629 (decode-opcode #b1001 12
631 (bit-oriented opcode "bcf"
633 (bitwise-and f (bitwise-not (arithmetic-shift 1 b)))))))
635 (decode-opcode #b1000 12
637 (bit-oriented opcode "bsf"
639 (bitwise-ior f (arithmetic-shift 1 b))))))
641 (decode-opcode #b1011 12
643 (bit-oriented opcode "btfsc"
645 (if (= 0 (bitwise-and f (arithmetic-shift 1 b))) (skip))
648 (decode-opcode #b1010 12
650 (bit-oriented opcode "btfss"
652 (if (not (= 0 (bitwise-and f (arithmetic-shift 1 b)))) (skip))
655 (decode-opcode #b0111 12
657 (bit-oriented opcode "btg"
659 (bitwise-xor f (arithmetic-shift 1 b))))))
661 ; Control operations.
663 (decode-opcode #b11100010 8
665 (short-relative-branch opcode "bc"
667 (not (= 0 (carry)))))))
669 (decode-opcode #b11100110 8
671 (short-relative-branch opcode "bn" negative-flag?)))
673 (decode-opcode #b11100011 8
675 (short-relative-branch opcode "bnc"
679 (decode-opcode #b11100111 8
681 (short-relative-branch opcode "bnn" negative-flag?)))
683 (decode-opcode #b11100101 8
685 (short-relative-branch opcode "bnov"
687 (not (overflow-flag?))))))
689 (decode-opcode #b11100001 8
691 (short-relative-branch opcode "bnz"
693 (not (zero-flag?))))))
695 (decode-opcode #b11100100 8
697 (short-relative-branch opcode "bov" overflow-flag?)))
699 (decode-opcode #b11010 11
701 (long-relative-branch opcode "bra" #f)))
703 (decode-opcode #b11100000 8
705 (short-relative-branch opcode "bz" zero-flag?)))
707 (decode-opcode #b1110110 9
709 (call-branch opcode "call")))
711 (decode-opcode #b11101111 8
713 (goto-branch opcode "goto")))
715 (decode-opcode #b11011 11
717 (long-relative-branch opcode "rcall" #t)))
719 (decode-opcode #b1111 12
722 (print (list (last-pc) " nop ")))))
724 (decode-opcode #b00000000 8
726 (cond ((= opcode #b0000000000000100)
728 (print (list (last-pc) " clrwdt ")))
730 ((= opcode #b0000000000000111)
732 (print (list (last-pc) " daw ")))
734 ((= opcode #b0000000000000000)
736 (print (list (last-pc) " nop "))))
737 ((= opcode #b0000000000000110)
739 (print (list (last-pc) " pop ")))
741 ((= opcode #b0000000000000101)
743 (print (list (last-pc) " push ")))
744 (stack-push (get-pc)))
745 ((= opcode #b0000000011111111)
747 (print (list (last-pc) " reset ")))
749 ((= opcode #b0000000000010000)
751 (print (list (last-pc) " retfie ")))
754 ((= opcode #b0000000000010001)
756 (print (list (last-pc) " retfie FAST")))
757 (error "retfie fast not implemented")
760 ((= opcode #b0000000000010010)
762 (print (list (last-pc) " return ")))
765 ((= opcode #b0000000000010011)
767 (print (list (last-pc) " return FAST")))
768 (error "return fast not implemented")
771 ((= opcode #b0000000000000011)
773 (print (list (last-pc) " sleep ")))
774 (set! pic18-exit #t))
777 (print (list (last-pc) " ??? ")))
780 ; Literal operations.
782 (decode-opcode #b00001111 8
784 (literal-operation opcode "addlw" 'c-dc-z-ov-n
788 (decode-opcode #b00001011 8
790 (literal-operation opcode "andlw" 'z-n
792 (bitwise-and k (get-wreg))))))
794 (decode-opcode #b00001001 8
796 (literal-operation opcode "iorlw" 'z-n
798 (bitwise-ior k (get-wreg))))))
805 (make-listing "lfsr" (file-text f) (lit-text k)))
807 (asm-16 (bitmask "1110 1110 00ff kkkk" (file f) (quotient (lit k) 256)))
808 (asm-16 (bitmask "1111 0000 kkkk kkkk" (modulo (lit k) 256))))))
815 (make-listing "movlb" (lit-text k)))
817 (asm-16 (bitmask "0000 0001 0000 kkkk" (lit k))))))
819 (decode-opcode #b00001110 8
821 (literal-operation opcode "movlw" 'none
825 (decode-opcode #b00001101 8
827 (literal-operation opcode "mullw" 'none
831 (decode-opcode #b00001100 8
833 (literal-operation opcode "retlw" 'none
839 (decode-opcode #b00001000 8
841 (literal-operation opcode "sublw" 'c-dc-z-ov-n
845 (decode-opcode #b00001010 8
847 (literal-operation opcode "xorlw" 'z-n
849 (bitwise-xor k (get-wreg))))))
851 ; Program memory operations.
858 (make-listing "tblrd*"))
860 (asm-16 (bitmask "0000 0000 0000 1000")))))
867 (make-listing "tblrd*+"))
869 (asm-16 (bitmask "0000 0000 0000 1001")))))
876 (make-listing "tblrd*-"))
878 (asm-16 (bitmask "0000 0000 0000 1010")))))
885 (make-listing "tblrd+*"))
887 (asm-16 (bitmask "0000 0000 0000 1011")))))
894 (make-listing "tblwt*"))
896 (asm-16 (bitmask "0000 0000 0000 1100")))))
903 (make-listing "tblwt*+"))
905 (asm-16 (bitmask "0000 0000 0000 1101")))))
912 (make-listing "tblwt*-"))
914 (asm-16 (bitmask "0000 0000 0000 1110")))))
921 (make-listing "tblwt+*"))
923 (asm-16 (bitmask "0000 0000 0000 1111")))))
925 ;------------------------------------------------------------------------------
927 (define (read-hex-file filename)
929 (define addr-width 32)
931 (define (syntax-error)
932 (error "*** Syntax error in HEX file"))
935 (with-exception-catcher
939 (open-input-file filename)))))
941 (define mem (make-vector 16 #f))
943 (define (mem-store! a b)
946 (x (- addr-width 4)))
949 (let ((i (arithmetic-shift a (- x))))
950 (let ((v (vector-ref m i)))
952 (let ((v (make-vector 16 #f)))
955 (- a (arithmetic-shift i x))
960 (define (f m a n tail)
962 (define (g i a n tail)
964 (g (- i 1) (- a n) n (f (vector-ref m i) a n tail))
969 (cons (cons (- a 1) m) tail)
970 (g 15 a (quotient n 16) tail))
973 (f mem (expt 2 addr-width) (expt 2 addr-width) '()))
978 (define (read-hex-nibble)
979 (let ((c (read-char f)))
980 (cond ((and (char>=? c #\0) (char<=? c #\9))
981 (- (char->integer c) (char->integer #\0)))
982 ((and (char>=? c #\A) (char<=? c #\F))
983 (+ 10 (- (char->integer c) (char->integer #\A))))
984 ((and (char>=? c #\a) (char<=? c #\f))
985 (+ 10 (- (char->integer c) (char->integer #\a))))
989 (define (read-hex-byte)
990 (let* ((a (read-hex-nibble))
991 (b (read-hex-nibble)))
997 (let ((c (read-char f)))
998 (cond ((not (char? c)))
999 ((or (char=? c #\linefeed)
1000 (char=? c #\return))
1002 ((not (char=? c #\:))
1005 (let* ((len (read-hex-byte))
1006 (a1 (read-hex-byte))
1007 (a2 (read-hex-byte))
1008 (type (read-hex-byte)))
1009 (let* ((adr (+ a2 (* 256 a1)))
1010 (sum (+ len a1 a2 type)))
1014 (let ((a (+ adr (* hi16 65536)))
1015 (b (read-hex-byte)))
1017 (set! adr (modulo (+ adr 1) 65536))
1018 (set! sum (+ sum b))
1026 (let* ((a1 (read-hex-byte))
1027 (a2 (read-hex-byte)))
1028 (set! sum (+ sum a1 a2))
1029 (set! hi16 (+ a2 (* 256 a1)))))
1032 (let ((check (read-hex-byte)))
1033 (if (not (= (modulo (- sum) 256) check))
1035 (let ((c (read-char f)))
1036 (if (or (not (or (char=? c #\linefeed)
1037 (char=? c #\return)))
1041 (close-input-port f)
1045 (error "*** Could not open the HEX file")
1048 ;------------------------------------------------------------------------------
1050 (define (execute-hex-file filename)
1051 (let ((program (read-hex-file filename)))
1053 (for-each (lambda (x) (set-rom (car x) (cdr x))) program)
1055 (pic18-sim-cleanup)))