5 (define-macro (bitmask encoding . field-values)
9 (fields (list (list #\space 0 0))))
10 (if (< i (string-length encoding))
11 (let ((c (string-ref encoding i)))
28 (if (and (char=? c (car (car fields)))
29 (= pos (caddr (car fields))))
31 (set-car! (cddr (car fields)) (+ pos 1))
39 (cons (list c pos (+ pos 1)) fields))))))
42 (error "invalid bitmask" encoding))
46 (let* ((width (- (caddr f) (cadr f)))
47 (shift (- pos (caddr f))))
54 (cdr (reverse fields))
57 (define (bitfield encoding name limit shift value)
58 (if (or (< value 0) (>= value limit))
59 (error "value does not fit in field" name value encoding)
60 (arithmetic-shift value shift)))
62 ;------------------------------------------------------------------------------
64 ; Byte-oriented file register operations.
66 (define (addwf f #!optional (d 'f) (a 'a))
70 (make-listing "addwf" (file-text f) (dest-text d 'f) (access-text a 'a)))
72 (asm-16 (bitmask "0010 01da ffff ffff" (dest d) (access a) (file f))))))
74 (define (addwfc f #!optional (d 'f) (a 'a))
78 (make-listing "addwfc" (file-text f) (dest-text d 'f) (access-text a 'a)))
80 (asm-16 (bitmask "0010 00da ffff ffff" (dest d) (access a) (file f))))))
82 (define (andwf f #!optional (d 'f) (a 'a))
86 (make-listing "andwf" (file-text f) (dest-text d 'f) (access-text a 'a)))
88 (asm-16 (bitmask "0001 01da ffff ffff" (dest d) (access a) (file f))))))
90 (define (clrf f #!optional (a 'a))
94 (make-listing "clrf" (file-text f) (access-text a 'a)))
96 (asm-16 (bitmask "0110 101a ffff ffff" (access a) (file f))))))
98 (define (comf f #!optional (d 'f) (a 'a))
102 (make-listing "comf" (file-text f) (dest-text d 'f) (access-text a 'a)))
104 (asm-16 (bitmask "0001 11da ffff ffff" (dest d) (access a) (file f))))))
106 (define (cpfseq f #!optional (a 'a))
110 (make-listing "cpfseq" (file-text f) (access-text a 'a)))
112 (asm-16 (bitmask "0110 001a ffff ffff" (access a) (file f))))))
114 (define (cpfsgt f #!optional (a 'a))
118 (make-listing "cpfsgt" (file-text f) (access-text a 'a)))
120 (asm-16 (bitmask "0110 010a ffff ffff" (access a) (file f))))))
122 (define (cpfslt f #!optional (a 'a))
126 (make-listing "cpfslt" (file-text f) (access-text a 'a)))
128 (asm-16 (bitmask "0110 000a ffff ffff" (access a) (file f))))))
130 (define (decf f #!optional (d 'f) (a 'a))
134 (make-listing "decf" (file-text f) (dest-text d 'f) (access-text a 'a)))
136 (asm-16 (bitmask "0000 01da ffff ffff" (dest d) (access a) (file f))))))
138 (define (decfsz f #!optional (d 'f) (a 'a))
142 (make-listing "decfsz" (file-text f) (dest-text d 'f) (access-text a 'a)))
144 (asm-16 (bitmask "0010 11da ffff ffff" (dest d) (access a) (file f))))))
146 (define (dcfsnz f #!optional (d 'f) (a 'a))
150 (make-listing "dcfsnz" (file-text f) (dest-text d 'f) (access-text a 'a)))
152 (asm-16 (bitmask "0100 11da ffff ffff" (dest d) (access a) (file f))))))
154 (define (incf f #!optional (d 'f) (a 'a))
158 (make-listing "incf" (file-text f) (dest-text d 'f) (access-text a 'a)))
160 (asm-16 (bitmask "0010 10da ffff ffff" (dest d) (access a) (file f))))))
162 (define (incfsz f #!optional (d 'f) (a 'a))
166 (make-listing "incfsz" (file-text f) (dest-text d 'f) (access-text a 'a)))
168 (asm-16 (bitmask "0011 11da ffff ffff" (dest d) (access a) (file f))))))
170 (define (infsnz f #!optional (d 'f) (a 'a))
174 (make-listing "infsnz" (file-text f) (dest-text d 'f) (access-text a 'a)))
176 (asm-16 (bitmask "0100 10da ffff ffff" (dest d) (access a) (file f))))))
178 (define (iorwf f #!optional (d 'f) (a 'a))
182 (make-listing "iorwf" (file-text f) (dest-text d 'f) (access-text a 'a)))
184 (asm-16 (bitmask "0001 00da ffff ffff" (dest d) (access a) (file f))))))
186 (define (movf f #!optional (d 'f) (a 'a))
190 (make-listing "movf" (file-text f) (dest-text d 'f) (access-text a 'a)))
192 (asm-16 (bitmask "0101 00da ffff ffff" (dest d) (access a) (file f))))))
194 (define (movff fs fd)
198 (make-listing "movff" (file-text fs) (file-text fd)))
200 (asm-16 (bitmask "1100 ffff ffff ffff" (file-full fs)))
201 (asm-16 (bitmask "1111 ffff ffff ffff" (file-full fd))))))
203 (define (movwf f #!optional (a 'a))
207 (make-listing "movwf" (file-text f) (access-text a 'a)))
209 (asm-16 (bitmask "0110 111a ffff ffff" (access a) (file f))))))
211 (define (mulwf f #!optional (a 'a))
215 (make-listing "mulwf" (file-text f) (access-text a 'a)))
217 (asm-16 (bitmask "0000 001a ffff ffff" (access a) (file f))))))
219 (define (negf f #!optional (a 'a))
223 (make-listing "negf" (file-text f) (access-text a 'a)))
225 (asm-16 (bitmask "0110 110a ffff ffff" (access a) (file f))))))
227 (define (rlcf f #!optional (d 'f) (a 'a))
231 (make-listing "rlcf" (file-text f) (dest-text d 'f) (access-text a 'a)))
233 (asm-16 (bitmask "0011 01da ffff ffff" (dest d) (access a) (file f))))))
235 (define (rlncf f #!optional (d 'f) (a 'a))
239 (make-listing "rlncf" (file-text f) (dest-text d 'f) (access-text a 'a)))
241 (asm-16 (bitmask "0100 01da ffff ffff" (dest d) (access a) (file f))))))
243 (define (rrcf f #!optional (d 'f) (a 'a))
247 (make-listing "rrcf" (file-text f) (dest-text d 'f) (access-text a 'a)))
249 (asm-16 (bitmask "0011 00da ffff ffff" (dest d) (access a) (file f))))))
251 (define (rrncf f #!optional (d 'f) (a 'a))
255 (make-listing "rrncf" (file-text f) (dest-text d 'f) (access-text a 'a)))
257 (asm-16 (bitmask "0100 00da ffff ffff" (dest d) (access a) (file f))))))
259 (define (setf f #!optional (a 'a))
263 (make-listing "setf" (file-text f) (access-text a 'a)))
265 (asm-16 (bitmask "0110 100a ffff ffff" (access a) (file f))))))
267 (define (subfwb f #!optional (d 'f) (a 'a))
271 (make-listing "subfwb" (file-text f) (dest-text d 'f) (access-text a 'a)))
273 (asm-16 (bitmask "0101 01da ffff ffff" (dest d) (access a) (file f))))))
275 (define (subwf f #!optional (d 'f) (a 'a))
279 (make-listing "subwf" (file-text f) (dest-text d 'f) (access-text a 'a)))
281 (asm-16 (bitmask "0101 11da ffff ffff" (dest d) (access a) (file f))))))
283 (define (subwfb f #!optional (d 'f) (a 'a))
287 (make-listing "subwfb" (file-text f) (dest-text d 'f) (access-text a 'a)))
289 (asm-16 (bitmask "0101 10da ffff ffff" (dest d) (access a) (file f))))))
291 (define (swapf f #!optional (d 'f) (a 'a))
295 (make-listing "swapf" (file-text f) (dest-text d 'f) (access-text a 'a)))
297 (asm-16 (bitmask "0011 10da ffff ffff" (dest d) (access a) (file f))))))
299 (define (tstfsz f #!optional (a 'a))
303 (make-listing "tstfsz" (file-text f) (access-text a 'a)))
305 (asm-16 (bitmask "0110 011a ffff ffff" (access a) (file f))))))
307 (define (xorwf f #!optional (d 'f) (a 'a))
311 (make-listing "xorwf" (file-text f) (dest-text d 'f) (access-text a 'a)))
313 (asm-16 (bitmask "0001 10da ffff ffff" (dest d) (access a) (file f))))))
315 ; Bit-oriented file register operations.
317 (define (bcf f b #!optional (a 'a))
321 (make-listing "bcf" (file-text f) (bit-text b) (access-text a 'a)))
323 (asm-16 (bitmask "1001 bbba ffff ffff" (bit b) (access a) (file f))))))
325 (define (bsf f b #!optional (a 'a))
329 (make-listing "bsf" (file-text f) (bit-text b) (access-text a 'a)))
331 (asm-16 (bitmask "1000 bbba ffff ffff" (bit b) (access a) (file f))))))
333 (define (btfsc f b #!optional (a 'a))
337 (make-listing "btfsc" (file-text f) (bit-text b) (access-text a 'a)))
339 (asm-16 (bitmask "1011 bbba ffff ffff" (bit b) (access a) (file f))))))
341 (define (btfss f b #!optional (a 'a))
345 (make-listing "btfss" (file-text f) (bit-text b) (access-text a 'a)))
347 (asm-16 (bitmask "1010 bbba ffff ffff" (bit b) (access a) (file f))))))
349 (define (btg f b #!optional (a 'a))
353 (make-listing "btg" (file-text f) (bit-text b) (access-text a 'a)))
355 (asm-16 (bitmask "0111 bbba ffff ffff" (bit b) (access a) (file f))))))
357 ; Control operations.
360 (make-short-relative-branch-instruction
364 (asm-16 (bitmask "1110 0010 nnnn nnnn" dist-8bit)))))
367 (make-short-relative-branch-instruction
371 (asm-16 (bitmask "1110 0110 nnnn nnnn" dist-8bit)))))
374 (make-short-relative-branch-instruction
378 (asm-16 (bitmask "1110 0011 nnnn nnnn" dist-8bit)))))
381 (make-short-relative-branch-instruction
385 (asm-16 (bitmask "1110 0111 nnnn nnnn" dist-8bit)))))
388 (make-short-relative-branch-instruction
392 (asm-16 (bitmask "1110 0101 nnnn nnnn" dist-8bit)))))
395 (make-short-relative-branch-instruction
399 (asm-16 (bitmask "1110 0001 nnnn nnnn" dist-8bit)))))
402 (make-short-relative-branch-instruction
406 (asm-16 (bitmask "1110 0100 nnnn nnnn" dist-8bit)))))
409 (make-long-relative-branch-instruction
413 (asm-16 (bitmask "1101 0nnn nnnn nnnn" dist-11bit)))))
416 (make-short-relative-branch-instruction
420 (asm-16 (bitmask "1110 0000 nnnn nnnn" dist-8bit)))))
422 (define (call l #!optional (s 0))
426 (make-listing "call" (label-text l) (lit-text s)))
432 (let ((pos-div-2 (quotient (label-pos l) 2)))
433 (asm-16 (bitmask "1110 110s kkkk kkkk" s (quotient pos-div-2 4096)))
434 (asm-16 (bitmask "1111 kkkk kkkk kkkk" (modulo pos-div-2 4096)))))))))
440 (make-listing "clrwdt"))
442 (asm-16 (bitmask "0000 0000 0000 0100")))))
448 (make-listing "daw"))
450 (asm-16 (bitmask "0000 0000 0000 0111")))))
456 (make-listing "goto" (label-text l)))
462 (let ((pos-div-2 (quotient (label-pos l) 2)))
463 (asm-16 (bitmask "1110 1111 kkkk kkkk" (quotient pos-div-2 4096)))
464 (asm-16 (bitmask "1111 kkkk kkkk kkkk" (modulo pos-div-2 4096)))))))))
470 (make-listing "nop"))
472 (asm-16 (bitmask "0000 0000 0000 0000")))))
478 (make-listing "pop"))
480 (asm-16 (bitmask "0000 0000 0000 0110")))))
486 (make-listing "push"))
488 (asm-16 (bitmask "0000 0000 0000 0101")))))
491 (make-long-relative-branch-instruction
495 (asm-16 (bitmask "1101 1nnn nnnn nnnn" dist-11bit)))))
501 (make-listing "reset"))
503 (asm-16 (bitmask "0000 0000 1111 1111")))))
505 (define (retfie #!optional (s 0))
509 (make-listing "retfie" (lit-text s)))
511 (asm-16 (bitmask "0000 0000 0001 000s" s)))))
513 (define (return #!optional (s 0))
517 (make-listing "return" (lit-text s)))
519 (asm-16 (bitmask "0000 0000 0001 001s" s)))))
525 (make-listing "sleep"))
527 (asm-16 (bitmask "0000 0000 0000 0011")))))
529 (define (make-short-relative-branch-instruction mnemonic l generate)
533 (make-listing mnemonic (label-text l)))
539 (let ((dist (- (label-pos l) (+ self 2))))
540 (if (and (>= dist -256)
543 (generate (modulo (quotient dist 2) 256))
544 (error "branch target is too far or improperly aligned" l dist))))))))
546 (define (make-long-relative-branch-instruction mnemonic l generate)
550 (make-listing mnemonic (label-text l)))
556 (let ((dist (- (label-pos l) (+ self 2))))
557 (if (and (>= dist -2048)
560 (generate (modulo (quotient dist 2) 2048))
561 (error "branch target is too far or improperly aligned" l dist))))))))
563 ; Literal operations.
569 (make-listing "addlw" (lit-text k)))
571 (asm-16 (bitmask "0000 1111 kkkk kkkk" (lit k))))))
577 (make-listing "andlw" (lit-text k)))
579 (asm-16 (bitmask "0000 1011 kkkk kkkk" (lit k))))))
585 (make-listing "iorlw" (lit-text k)))
587 (asm-16 (bitmask "0000 1001 kkkk kkkk" (lit k))))))
593 (make-listing "lfsr" (file-text f) (lit-text k)))
595 (asm-16 (bitmask "1110 1110 00ff kkkk" (file f) (quotient (lit k) 256)))
596 (asm-16 (bitmask "1111 0000 kkkk kkkk" (modulo (lit k) 256))))))
602 (make-listing "movlb" (lit-text k)))
604 (asm-16 (bitmask "0000 0001 0000 kkkk" (lit k))))))
610 (make-listing "movlw" (lit-text k)))
612 (asm-16 (bitmask "0000 1110 kkkk kkkk" (lit k))))))
618 (make-listing "mullw" (lit-text k)))
620 (asm-16 (bitmask "0000 1101 kkkk kkkk" (lit k))))))
626 (make-listing "retlw" (lit-text k)))
628 (asm-16 (bitmask "0000 1100 kkkk kkkk" (lit k))))))
634 (make-listing "sublw" (lit-text k)))
636 (asm-16 (bitmask "0000 1000 kkkk kkkk" (lit k))))))
642 (make-listing "xorlw" (lit-text k)))
644 (asm-16 (bitmask "0000 1010 kkkk kkkk" (lit k))))))
646 ; Data memory program memory operations.
652 (make-listing "tblrd*"))
654 (asm-16 (bitmask "0000 0000 0000 1000")))))
660 (make-listing "tblrd*+"))
662 (asm-16 (bitmask "0000 0000 0000 1001")))))
668 (make-listing "tblrd*-"))
670 (asm-16 (bitmask "0000 0000 0000 1010")))))
676 (make-listing "tblrd+*"))
678 (asm-16 (bitmask "0000 0000 0000 1011")))))
684 (make-listing "tblwt*"))
686 (asm-16 (bitmask "0000 0000 0000 1100")))))
692 (make-listing "tblwt*+"))
694 (asm-16 (bitmask "0000 0000 0000 1101")))))
700 (make-listing "tblwt*-"))
702 (asm-16 (bitmask "0000 0000 0000 1110")))))
708 (make-listing "tblwt+*"))
710 (asm-16 (bitmask "0000 0000 0000 1111")))))
712 ;------------------------------------------------------------------------------
722 (make-listing "andlw" (lit-text k)))
724 (asm-16 (+ #b0000101100000000 (lit8 k))))))
730 (make-listing "iorlw" (lit-text k)))
732 (asm-16 (+ #b0000100100000000 (lit8 k))))))
738 (make-listing "lfsr" (lit-text f) "," (lit-text k)))
740 (asm-16 (+ #b1110111000000000 (* (lit2 f) 16) (quotient (lit12 k) 256)))
741 (asm-16 (+ #b1111000000000000 (modulo (lit12 k) 256))))))
747 (make-listing "movlb" (lit-text k)))
749 (asm-16 (+ #b0000000100000000 (lit4 k))))))
755 (make-listing "movlw" (lit-text k)))
757 (asm-16 (+ #b0000111000000000 (lit8 k))))))
763 (make-listing "mullw" (lit-text k)))
765 (asm-16 (+ #b0000110100000000 (lit8 k))))))
771 (make-listing "retlw" (lit-text k)))
773 (asm-16 (+ #b0000110000000000 (lit8 k))))))
779 (make-listing "sublw" (lit-text k)))
781 (asm-16 (+ #b0000100000000000 (lit8 k))))))
787 (make-listing "xorlw" (lit-text k)))
789 (asm-16 (+ #b0000101000000000 (lit8 k))))))
795 (make-listing "tblrd*"))
797 (asm-16 #b0000000000001000))))
803 (make-listing "tblrd*+"))
805 (asm-16 #b0000000000001001))))
811 (make-listing "tblrd*-"))
813 (asm-16 #b0000000000001010))))
819 (make-listing "tblrd+*"))
821 (asm-16 #b0000000000001011))))
827 (make-listing "tblwt*"))
829 (asm-16 #b0000000000001100))))
835 (make-listing "tblwt*+"))
837 (asm-16 #b0000000000001101))))
843 (make-listing "tblwt*-"))
845 (asm-16 #b0000000000001110))))
851 (make-listing "tblwt+*"))
853 (asm-16 #b0000000000001111))))
856 (if (and (>= n 0) (<= n 3))
858 (error "2 bit literal expected but got" n)))
861 (if (and (>= n 0) (<= n 255))
863 (error "8 bit literal expected but got" n)))
866 (if (and (>= n 0) (<= n 2047))
868 (error "12 bit literal expected but got" n)))
872 (define (make-instruction cycles listing-thunk code-thunk)
876 (define (make-listing mnemonic . operands)
878 (define (operand-list operands)
881 (let ((rest (operand-list (cdr operands))))
882 (string-append (car operands)
883 (if (string=? rest "")
885 (string-append ", " rest))))))
890 (make-string (- 8 (string-length mnemonic)) #\space)
891 (operand-list operands))))
896 (else (error "destination bit must be w or f"))))
898 (define (dest-text d default)
899 (cond ((eq? d default) "")
902 (else (error "destination bit must be w or f"))))
907 (else (error "access bit must be a or b"))))
909 (define (access-text a default)
910 (cond ((eq? a default) "")
913 (else (error "access bit must be a or b"))))
923 (string-append "0x" (number->string k 16))))
926 (string-append "-" (text (abs k)))
936 (if (or (>= f #xf80) (< #x080))
938 (error "illegal file register")))
940 (define (file-full f)
943 (define (file-text f)
944 (let ((x (assv f file-reg-names)))
946 (symbol->string (cdr x))
949 (define (label-text label)
951 (string-append "0x" (number->string label 16))
952 (symbol->string (asm-label-id label))))
954 (define (label-pos label)
957 (asm-label-pos label)))
959 ;------------------------------------------------------------------------------
964 (define STKPTR #xffc)
965 (define PCLATU #xffb)
966 (define PCLATH #xffa)
968 (define TBLPTRU #xff8)
969 (define TBLPTRH #xff7)
970 (define TBLPTRL #xff6)
971 (define TABLAT #xff5)
975 (define POSTINC0 #xfee)
976 (define POSTDEC0 #xfed)
977 (define PREINC0 #xfec)
978 (define PLUSW0 #xfeb)
983 (define POSTINC1 #xfe6)
984 (define POSTDEC1 #xfe5)
985 (define PREINC1 #xfe4)
986 (define PLUSW1 #xfe3)
991 (define POSTINC2 #xfde)
992 (define POSTDEC2 #xfdd)
993 (define PREINC2 #xfdc)
994 (define PLUSW2 #xfdb)
997 (define STATUS #xfd8)
1000 (define PORTE #xf84)
1001 (define PORTD #xf83)
1002 (define PORTC #xf82)
1003 (define PORTB #xf81)
1004 (define PORTA #xf80)
1006 (define file-reg-names '(
1074 ;------------------------------------------------------------------------------
1076 (define (label-offset-reference label offset)
1081 (asm-16 (+ (asm-label-pos label) offset)))))
1083 (define (label-instr label opcode)
1088 (let ((pos (asm-label-pos label)))
1089 (asm-8 (+ (quotient pos 256) opcode))
1090 (asm-8 (modulo pos 256))))))
1092 ;------------------------------------------------------------------------------
1094 (define irda_send_newline #x0078)
1095 (define irda_send #x007E)
1096 (define irda_recv_with_1_sec_timeout #x00A2)
1097 (define irda_recv #x00A4)
1098 (define sec_sleep #x00B0)
1099 (define msec_sleep #x00B6)
1100 (define delay_7 #x00D4)
1101 (define led_set #x00D6)
1102 (define bit_set #x00EC)
1103 (define FLASH_execute_erase #x0106)
1104 (define FLASH_execute_write #x0108)
1105 (define parse_hex_byte #x0184)
1106 (define parse_hex_digit #x0194)
1107 (define irda_send_hex #x01AE)
1108 (define irda_send_nibble #x01B6)
1110 ;------------------------------------------------------------------------------