-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpic18-sim.scm
1279 lines (1126 loc) · 38.6 KB
/
pic18-sim.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; File: "pic18-sim.scm"
(define pic18-ram #f)
(define pic18-rom #f)
(define pic18-stack #f)
(define pic18-pc #f)
(define instrs-counts #f) ; counts how many times each instruction is executed
(define break-points '()) ; list of adresses at which the simulation stops
(define pic18-carry-flag #f)
(define pic18-deccarry-flag #f)
(define pic18-zero-flag #f)
(define pic18-overflow-flag #f)
(define pic18-negative-flag #f)
(define pic18-cycles #f)
(define pic18-exit #f)
(define fsr-alist (list (cons INDF0 (cons FSR0H FSR0L))
(cons INDF1 (cons FSR1H FSR1L))
(cons INDF2 (cons FSR2H FSR2L))))
(define (get-ram adr) ;; TODO implement RCREG
(cond ((= adr TOSU)
(bitwise-and (arithmetic-shift (get-tos) -16) #xff))
((= adr TOSH)
(bitwise-and (arithmetic-shift (get-tos) -8) #xff))
((= adr TOSL)
(bitwise-and (get-tos) #xff))
((= adr PCL)
(set-ram PCLATU (bitwise-and (arithmetic-shift (get-pc) -16) #x1f))
(set-ram PCLATH (bitwise-and (arithmetic-shift (get-pc) -8) #xff))
(bitwise-and (get-pc) #xfe))
((= adr STATUS)
(+ pic18-carry-flag
(arithmetic-shift pic18-deccarry-flag 1)
(arithmetic-shift pic18-zero-flag 2)
(arithmetic-shift pic18-overflow-flag 3)
(arithmetic-shift pic18-negative-flag 4)))
((assq adr fsr-alist)
=> (lambda (x)
(get-ram (bitwise-ior
(arithmetic-shift (bitwise-and (u8vector-ref pic18-ram
(cadr x))
#xf)
8)
(u8vector-ref pic18-ram
(cddr x))))))
;; TODO pre/post inc/dec 0..2
(else
(u8vector-ref pic18-ram adr))))
(define (set-ram adr byte)
(cond ((= adr TOSU)
(set-tos (+ (bitwise-and (get-tos) #x00ffff)
(arithmetic-shift (bitwise-and byte #x1f) 16))))
((= adr TOSH)
(set-tos (+ (bitwise-and (get-tos) #x1f00ff)
(arithmetic-shift byte 8))))
((= adr TOSL)
(set-tos (+ (bitwise-and (get-tos) #x1fff00)
byte)))
((= adr PCL)
(set-pc (+ (arithmetic-shift (get-ram PCLATU) 16)
(arithmetic-shift (get-ram PCLATH) 8)
(bitwise-and byte #xfe))))
((= adr TXREG)
(display (list->string (list (integer->char byte)))))
((= adr STATUS)
(set! pic18-carry-flag (bitwise-and byte 1))
(set! pic18-deccarry-flag (arithmetic-shift (bitwise-and byte 2) -1))
(set! pic18-zero-flag (arithmetic-shift (bitwise-and byte 4) -2))
(set! pic18-overflow-flag (arithmetic-shift (bitwise-and byte 8) -3))
(set! pic18-negative-flag (arithmetic-shift (bitwise-and byte 16) -4)))
((assq adr fsr-alist)
=> (lambda (x)
(set-ram (bitwise-ior ;; TODO factor common code with get-ram ?
(arithmetic-shift (bitwise-and (u8vector-ref pic18-ram
(cadr x))
#xf)
8)
(u8vector-ref pic18-ram
(cddr x)))
byte)))
;; TODO all other special array registers
(else
(u8vector-set! pic18-ram adr byte))))
(define (get-rom adr)
(u8vector-ref pic18-rom adr))
(define (set-rom adr byte)
(u8vector-set! pic18-rom adr byte))
(define (get-stack adr)
(vector-ref pic18-stack adr))
(define (set-stack adr pc)
(vector-set! pic18-stack adr pc))
(define (get-pc)
pic18-pc)
(define (set-pc pc)
(set! pic18-pc pc))
(define (get-sp)
(bitwise-and (get-ram STKPTR) #x1f))
(define (set-sp sp)
(set-ram STKPTR
(bitwise-ior sp
(bitwise-and (get-ram STKPTR) #xe0))))
(define (get-tos)
(vector-ref pic18-stack (- (get-sp) 1)))
(define (set-tos pc)
(vector-set! pic18-stack (- (get-sp) 1) pc))
(define (stack-push pc)
(set-sp (+ (get-sp) 1))
(set-tos pc))
(define (stack-pop)
(set-pc (get-tos))
(set-sp (- (get-sp) 1)))
(define (get-bsr)
(bitwise-and (get-ram BSR) #x0f))
(define (get-wreg)
(get-ram WREG))
(define (set-wreg byte)
(set-ram WREG byte))
(define (zero-flag?)
(not (= 0 pic18-zero-flag)))
(define (set-zero-flag flag)
(set! pic18-zero-flag flag))
(define (negative-flag?)
(not (= 0 pic18-negative-flag)))
(define (set-negative-flag flag)
(set! pic18-negative-flag flag))
(define (carry-flag?)
(not (= 0 pic18-carry-flag)))
(define (set-carry-flag flag)
(set! pic18-carry-flag flag))
(define (deccarry-flag?)
(not (= 0 pic18-deccarry-flag)))
(define (set-deccarry-flag flag)
(set! pic18-deccarry-flag flag))
(define (overflow-flag?)
(not (= 0 pic18-overflow-flag)))
(define (set-overflow-flag flag)
(set! pic18-overflow-flag flag))
(define (pic18-sim-setup)
(set! pic18-ram (make-u8vector #x1000 0))
(set! pic18-rom (make-u8vector #x10000 0))
(set! pic18-stack (make-vector #x1f 0))
(set! instrs-counts (make-vector #x10000 0))
(set-pc 0)
(set-wreg 0)
(set! pic18-carry-flag 0)
(set! pic18-deccarry-flag 0)
(set! pic18-zero-flag 0)
(set! pic18-overflow-flag 0)
(set! pic18-negative-flag 0))
(define (pic18-sim-cleanup)
(set! pic18-ram #f)
(set! pic18-rom #f)
(set! pic18-stack #f))
;------------------------------------------------------------------------------
(define (last-pc)
(let ((pc (- (get-pc) 2)))
(list (get-sp) " " (- pic18-cycles 1) " "
(substring (number->string (+ #x1000000 pc) 16) 1 7)
" ")))
(define (illegal-opcode opcode)
(if trace-instr
(print (list (last-pc) " *illegal*")))
(error "illegal opcode" opcode))
(define decode-vector
(make-vector 256 illegal-opcode))
(define (decode-opcode opcode-bits shift action)
(if (< shift 8)
(error "shift=" shift))
(let ((n (arithmetic-shift 1 (- shift 8)))
(base (arithmetic-shift opcode-bits (- shift 8))))
(let loop ((i 0))
(if (< i n)
(begin
(vector-set! decode-vector (+ base i) action)
(loop (+ i 1)))))))
(define (byte-oriented opcode mnemonic flags-changed operation)
(byte-oriented-aux opcode mnemonic flags-changed operation 'wreg))
(define (byte-oriented-file opcode mnemonic flags-changed operation)
(byte-oriented-aux opcode mnemonic flags-changed operation 'file))
(define (byte-oriented-wide opcode mnemonic flags-changed operation dest)
;; for use with instructions that have results more than a byte wide, such
;; as multiplication. the result goes at the given addresses
(byte-oriented-aux opcode mnemonic flags-changed operation dest)) ;; TODO do the same for literals
(define (byte-oriented-aux opcode mnemonic flags-changed operation dest)
(let* ((f (bitwise-and opcode #xff))
(adr (if (= 0 (bitwise-and opcode #x100))
;; the upper 160 addresses of the first bank are the special
;; registers #xF60 to #xFFF
(if (= 0 (bitwise-and f #x80)) f (+ f #xf00))
(+ f (arithmetic-shift (get-bsr) 8)))))
(if trace-instr
(print (list (last-pc) " " mnemonic " "
(let ((x (assv adr file-reg-names)))
(if x
(cdr x)
(let ((x (table-ref register-table f #f)))
(if #f ;x ;; TODO unreadable with picobit
(apply string-append-with-separator (cons "/" x))
(list "0x" (number->string adr 16))))))
(if (or (eq? dest 'wreg)
(= 0 (bitwise-and opcode #x200)))
", w"
"")
"")))
(let* ((result (operation (get-ram adr)))
(result-8bit (bitwise-and result #xff)))
(cond ((list? dest)
;; result is more than a byte wide (i.e. multiplication)
;; put it in the right destinations (dest is a list of addresses)
(let loop ((dest dest) (result result))
(if (not (null? dest))
;; the head of the list is the lsb
(begin (set-ram (car dest) (bitwise-and result #xff))
(loop (cdr dest) (arithmetic-shift result -8))))))
((or (eq? dest 'file) (not (= 0 (bitwise-and opcode #x200))))
;; the result goes in memory (file)
(set-ram adr result-8bit))
((eq? dest 'wreg)
;; result goes in wreg
(set-wreg result-8bit)))
(if (not (eq? flags-changed 'none))
(begin
(set-zero-flag (if (= 0 result-8bit) 1 0))
(if (not (eq? flags-changed 'z))
(begin
(set-negative-flag (if (> result-8bit #x7f) 1 0))
(if (not (eq? flags-changed 'z-n))
(begin
(set-carry-flag (if (or (> result #xff)
(< result 0))
1 0))
(if (not (eq? flags-changed 'c-z-n))
(begin
(set-deccarry-flag 0);;;;;;;;;;;;;;
(set-overflow-flag 0))))))))))));;;;;;;;;;;;;;
(define (bit-oriented opcode mnemonic operation)
(let* ((f (bitwise-and opcode #xff))
(adr (if (= 0 (bitwise-and opcode #x100))
(if (= 0 (bitwise-and f #x80)) f (+ f #xf00))
(+ f (arithmetic-shift (get-bsr) 8))))
(b (bitwise-and (arithmetic-shift opcode -9) 7)))
(if trace-instr
(print (list (last-pc) " " mnemonic " "
(let ((x (assv adr file-reg-names)))
(if x (cdr x) (list "0x" (number->string adr 16))))
", "
(if (= adr STATUS)
(cdr (assv b '((0 . C)
(1 . DC)
(2 . Z)
(3 . OV)
(4 . N)
(5 . 5)
(6 . 6)
(7 . 7))))
b)
"")))
(let* ((result (operation (get-ram adr) b))
(result-8bit (bitwise-and result #xff)))
(set-ram adr result-8bit))))
(define (short-relative-branch opcode mnemonic branch)
(let* ((n (bitwise-and opcode #xff))
(adr (+ (get-pc) (* 2 (if (> n #x7f) (- n #x100) n)))))
(if trace-instr
(print (list (last-pc) " " mnemonic " "
(symbol->string (table-ref symbol-table adr)))))
(if (branch)
(begin
(get-program-mem)
(set-pc adr)))))
(define (long-relative-branch opcode mnemonic call?)
(let* ((n (bitwise-and opcode #x7ff))
(adr (+ (get-pc) (* 2 (if (> n #x3ff) (- n #x800) n)))))
(if trace-instr
(print (list (last-pc) " " mnemonic " "
(symbol->string (table-ref symbol-table adr)))))
(if call?
(stack-push (get-pc)))
(get-program-mem)
(set-pc adr)))
(define (call-branch opcode mnemonic)
(let ((adr (* 2 (+ (bitwise-and opcode #xff)
(arithmetic-shift (bitwise-and (get-program-mem) #xfff) 8)))))
(if trace-instr
(print (list (last-pc) " " mnemonic " "
(symbol->string (table-ref symbol-table adr))
(if (= 0 (bitwise-and opcode #x100))
""
", FAST"))))
(stack-push (get-pc))
(if (not (= 0 (bitwise-and opcode #x100)))
(error "call fast not implemented"))
(set-pc adr)))
(define (goto-branch opcode mnemonic)
(let ((adr (* 2 (+ (bitwise-and opcode #xff)
(arithmetic-shift (bitwise-and (get-program-mem) #xfff) 8)))))
(if trace-instr
(print (list (last-pc) " " mnemonic " "
(symbol->string (table-ref symbol-table adr)))))
(set-pc adr)))
(define (literal-operation opcode mnemonic flags-changed operation)
(let ((k (bitwise-and opcode #xff)))
(if trace-instr
(print (list (last-pc) " " mnemonic " "
(if (< k 10) k (list "0x" (number->string k 16))))))
(let* ((result (operation k))
(result-8bit (bitwise-and result #xff)))
(set-wreg result-8bit)
(if (not (eq? flags-changed 'none))
(begin
(set-zero-flag (if (= 0 result-8bit) 1 0))
(if (not (eq? flags-changed 'z))
(begin
(set-negative-flag (if (> result-8bit #x7f) 1 0))
(if (not (eq? flags-changed 'z-n))
(begin
(set-carry-flag (if (> result #xff) 1 0))
(if (not (eq? flags-changed 'c-z-n))
(begin
(set-deccarry-flag 0);;;;;;;;;;;;;;
(set-overflow-flag 0))))))))))));;;;;;;;;;;;;;
(define (program-memory-read mnemonic read-adr-fun set-adr-fun)
(if trace-instr
(print (list (last-pc) " " mnemonic " ")))
(let ((adr (bitwise-ior (arithmetic-shift (get-ram TBLPTRU) 16)
(arithmetic-shift (get-ram TBLPTRH) 8)
(get-ram TBLPTRL))))
(set-ram TABLAT (get-rom (bitwise-and (read-adr-fun adr)
;; rom addresses are 21 bits wide
#x1fffff)))
(let ((new-adr (bitwise-and (set-adr-fun adr) #x1fffff)))
(set-ram TBLPTRU (arithmetic-shift new-adr -16))
(set-ram TBLPTRH (bitwise-and (arithmetic-shift new-adr -8) #xff))
(set-ram TBLPTRL (bitwise-and new-adr #xff)))))
(define (get-program-mem)
(set! pic18-cycles (+ pic18-cycles 1))
(let* ((pc (get-pc))
(lsb (get-rom pc))
(msb (get-rom (+ pc 1))))
(set-pc (+ (get-pc) 2))
(+ (arithmetic-shift msb 8) lsb)))
(define (skip)
(get-program-mem))
(define (hex n)
(substring (number->string (+ #x100 n) 16) 1 3))
(define (dump-mem)
(print " ")
(let loop ((i 0))
(if (< i 10)
(begin
(print (list (hex (u8vector-ref pic18-ram i)) " "))
(loop (+ i 1)))))
(print (list " WREG=" (hex (get-wreg)) "\n")))
(define single-stepping-mode? #f)
(define (pic18-execute)
(set! pic18-exit #f)
(set! pic18-cycles 0)
(if trace-instr
(print " "))
(let loop ()
(if trace-instr
(dump-mem))
(if pic18-exit
(begin
(print (list "WREG = d'" (get-wreg) "'\n")))
(let ((opcode (get-program-mem))
(pc (- (get-pc) 2)))
(vector-set! instrs-counts pc (+ (vector-ref instrs-counts pc) 1))
(if picobit-trace?
(begin (if (= pc #x48) ; picobit dispatch, might change
(pp (picobit-pc)))
(if (= pc #x72) ; later on in the dispatch
(begin (picobit-instruction)
(picobit-stack)
(picobit-continuation)
(display "\n")))))
(if (member pc break-points)
(begin (pp (list "break point at: " (number->string pc 16)))
(set! trace-instr #t)
(set! single-stepping-mode? #t)))
(if single-stepping-mode? (step))
(let ((proc (vector-ref decode-vector (arithmetic-shift opcode -8))))
(proc opcode)
(loop))))))
(define trace-instr #t)
(define (carry)
(if (> pic18-carry-flag 0)
(begin (set! pic18-carry-flag #f)
1)
0))
;------------------------------------------------------------------------------
; Byte-oriented file register operations.
(decode-opcode #b001001 10
(lambda (opcode)
(byte-oriented opcode "addwf" 'c-dc-z-ov-n
(lambda (f)
(+ f (get-wreg))))))
(decode-opcode #b001000 10
(lambda (opcode)
(byte-oriented opcode "addwfc" 'c-dc-z-ov-n
(lambda (f)
(+ f (get-wreg) (carry))))))
(decode-opcode #b000101 10
(lambda (opcode)
(byte-oriented opcode "andwf" 'z-n
(lambda (f)
(bitwise-and f (get-wreg))))))
(decode-opcode #b0110101 9
(lambda (opcode)
(byte-oriented-file opcode "clrf" 'z
(lambda (f)
0))))
(decode-opcode #b000111 10
(lambda (opcode)
(byte-oriented opcode "comf" 'z-n
(lambda (f)
(bitwise-not f)))))
(decode-opcode #b0110001 9
(lambda (opcode)
(byte-oriented-file opcode "cpfseq" 'none
(lambda (f)
(if (= f (get-wreg)) (skip))
f))))
(decode-opcode #b0110010 9
(lambda (opcode)
(byte-oriented-file opcode "cpfsgt" 'none
(lambda (f)
(if (> f (get-wreg)) (skip))
f))))
(decode-opcode #b0110000 9
(lambda (opcode)
(byte-oriented-file opcode "cpfslt" 'none
(lambda (f)
(if (< f (get-wreg)) (skip))
f))))
(decode-opcode #b000001 10
(lambda (opcode)
(byte-oriented opcode "decf" 'c-dc-z-ov-n
(lambda (f)
(- f 1)))))
(decode-opcode #b001011 10
(lambda (opcode)
(byte-oriented opcode "decfsz" 'none
(lambda (f)
(if (= f 1) (skip))
(- f 1)))))
(decode-opcode #b010011 10
(lambda (opcode)
(byte-oriented opcode "dcfsnz" 'none
(lambda (f)
(if (not (= f 1)) (skip))
(- f 1)))))
(decode-opcode #b001010 10
(lambda (opcode)
(byte-oriented opcode "incf" 'c-dc-z-ov-n
(lambda (f)
(+ f 1)))))
(decode-opcode #b001111 10
(lambda (opcode)
(byte-oriented opcode "incfsz" 'none
(lambda (f)
(if (= f #xff) (skip))
(+ f 1)))))
(decode-opcode #b010010 10
(lambda (opcode)
(byte-oriented opcode "infsnz" 'none
(lambda (f)
(if (not (= f #xff)) (skip))
(+ f 1)))))
(decode-opcode #b000100 10
(lambda (opcode)
(byte-oriented opcode "iorwf" 'z-n
(lambda (f)
(bitwise-ior f (get-wreg))))))
(decode-opcode #b010100 10
(lambda (opcode)
(byte-oriented opcode "movf" 'z-n
(lambda (f)
f))))
(decode-opcode #b1100 12
(lambda (opcode)
(let* ((src (bitwise-and opcode #xfff))
;; the destination is in the second 16-bit part, need to fetch
(dst (bitwise-and (get-program-mem) #xfff)))
(if trace-instr
(print (list (last-pc) " movff "
(let ((x (assv src file-reg-names)))
(if x (cdr x) (list "0x" (number->string src 16))))
", "
(let ((x (assv dst file-reg-names)))
(if x (cdr x) (list "0x" (number->string dst 16)))) ;; TODO printing 2 args ruins the formatting
"")))
(set-ram dst (get-ram src)))))
(decode-opcode #b0110111 9
(lambda (opcode)
(byte-oriented-file opcode "movwf" 'none
(lambda (f)
(get-wreg)))))
(decode-opcode #b0000001 9
(lambda (opcode)
(byte-oriented-wide opcode "mulwf" 'none
(lambda (f)
(* f (get-wreg)))
(list PRODL PRODH))))
(decode-opcode #b0110110 9
(lambda (opcode)
(byte-oriented-file opcode "negf" 'c-dc-z-ov-n
(lambda (f)
(- f)))))
(decode-opcode #b001101 10
(lambda (opcode)
(byte-oriented opcode "rlcf" 'c-z-n
(lambda (f)
;; the carry flag will be set automatically
(+ (arithmetic-shift f 1) (carry))))))
(decode-opcode #b010001 10
(lambda (opcode)
(byte-oriented opcode "rlncf" 'z-n
(lambda (f)
(+ (arithmetic-shift f 1) (arithmetic-shift f -7))))))
(decode-opcode #b001100 10
(lambda (opcode)
(byte-oriented opcode "rrcf" 'c-z-n
(lambda (f)
(let ((r (+ (arithmetic-shift f -1) (arithmetic-shift (carry) 7))))
;; roll through carry (if the result is over #xff, carry will be set)
(if (= (bitwise-and f 1) 1) (+ r #x100) r))))))
(decode-opcode #b010000 10
(lambda (opcode)
(byte-oriented opcode "rrncf" 'z-n
(lambda (f)
(+ (arithmetic-shift f -1) (arithmetic-shift f 7))))))
(decode-opcode #b0110100 9
(lambda (opcode)
(byte-oriented-file opcode "setf" 'z
(lambda (f)
#xff))))
(decode-opcode #b010101 10
(lambda (opcode)
(byte-oriented opcode "subfwb" 'c-dc-z-ov-n
(lambda (f)
(- (get-wreg) f (carry))))))
(decode-opcode #b010111 10
(lambda (opcode)
(byte-oriented opcode "subwf" 'c-dc-z-ov-n
(lambda (f)
(- f (get-wreg))))))
(decode-opcode #b010110 10
(lambda (opcode)
(byte-oriented opcode "subwfb" 'c-dc-z-ov-n
(lambda (f)
(- f (get-wreg) (carry))))))
(decode-opcode #b001110 10
(lambda (opcode)
(byte-oriented opcode "swapf" 'none
(lambda (f)
(+ (arithmetic-shift f -4) (arithmetic-shift f 4))))))
(decode-opcode #b0110011 9
(lambda (opcode)
(byte-oriented-file opcode "tstfsz" 'none
(lambda (f)
(if (= f 0) (skip))))))
(decode-opcode #b000110 10
(lambda (opcode)
(byte-oriented opcode "xorwf" 'z-n
(lambda (f)
(bitwise-xor f (get-wreg))))))
; Bit-oriented file register operations.
(decode-opcode #b1001 12
(lambda (opcode)
(bit-oriented opcode "bcf"
(lambda (f b)
(bitwise-and f (bitwise-not (arithmetic-shift 1 b)))))))
(decode-opcode #b1000 12
(lambda (opcode)
(bit-oriented opcode "bsf"
(lambda (f b)
(bitwise-ior f (arithmetic-shift 1 b))))))
(decode-opcode #b1011 12
(lambda (opcode)
(bit-oriented opcode "btfsc"
(lambda (f b)
(if (= 0 (bitwise-and f (arithmetic-shift 1 b))) (skip))
f))))
(decode-opcode #b1010 12
(lambda (opcode)
(bit-oriented opcode "btfss"
(lambda (f b)
(if (not (= 0 (bitwise-and f (arithmetic-shift 1 b)))) (skip))
f))))
(decode-opcode #b0111 12
(lambda (opcode)
(bit-oriented opcode "btg"
(lambda (f b)
(bitwise-xor f (arithmetic-shift 1 b))))))
; Control operations.
(decode-opcode #b11100010 8
(lambda (opcode)
(short-relative-branch opcode "bc"
(lambda ()
(not (= 0 (carry)))))))
(decode-opcode #b11100110 8
(lambda (opcode)
(short-relative-branch opcode "bn" negative-flag?)))
(decode-opcode #b11100011 8
(lambda (opcode)
(short-relative-branch opcode "bnc"
(lambda ()
(= 0 (carry))))))
(decode-opcode #b11100111 8
(lambda (opcode)
(short-relative-branch opcode "bnn" negative-flag?)))
(decode-opcode #b11100101 8
(lambda (opcode)
(short-relative-branch opcode "bnov"
(lambda ()
(not (overflow-flag?))))))
(decode-opcode #b11100001 8
(lambda (opcode)
(short-relative-branch opcode "bnz"
(lambda ()
(not (zero-flag?))))))
(decode-opcode #b11100100 8
(lambda (opcode)
(short-relative-branch opcode "bov" overflow-flag?)))
(decode-opcode #b11010 11
(lambda (opcode)
(long-relative-branch opcode "bra" #f)))
(decode-opcode #b11100000 8
(lambda (opcode)
(short-relative-branch opcode "bz" zero-flag?)))
(decode-opcode #b1110110 9
(lambda (opcode)
(call-branch opcode "call")))
(decode-opcode #b11101111 8
(lambda (opcode)
(goto-branch opcode "goto")))
(decode-opcode #b11011 11
(lambda (opcode)
(long-relative-branch opcode "rcall" #t)))
(decode-opcode #b1111 12
(lambda (opcode)
(if trace-instr
(print (list (last-pc) " nop ")))))
(decode-opcode #b00000000 8
(lambda (opcode)
(cond ((= opcode #b0000000000000100)
(if trace-instr
(print (list (last-pc) " clrwdt ")))
(clrwdt opcode))
((= opcode #b0000000000000111)
(if trace-instr
(print (list (last-pc) " daw ")))
(daw opcode))
((= opcode #b0000000000000000)
(if trace-instr
(print (list (last-pc) " nop "))))
((= opcode #b0000000000000110)
(if trace-instr
(print (list (last-pc) " pop ")))
(stack-pop))
((= opcode #b0000000000000101)
(if trace-instr
(print (list (last-pc) " push ")))
(stack-push (get-pc)))
((= opcode #b0000000011111111)
(if trace-instr
(print (list (last-pc) " reset ")))
(set-pc 0))
((= opcode #b0000000000010000)
(if trace-instr
(print (list (last-pc) " retfie ")))
(get-program-mem)
(stack-pop))
((= opcode #b0000000000010001)
(if trace-instr
(print (list (last-pc) " retfie FAST")))
(error "retfie fast not implemented")
(get-program-mem)
(stack-pop))
((= opcode #b0000000000010010)
(if trace-instr
(print (list (last-pc) " return ")))
(get-program-mem)
(stack-pop))
((= opcode #b0000000000010011)
(if trace-instr
(print (list (last-pc) " return FAST")))
(error "return fast not implemented")
(get-program-mem)
(stack-pop))
((= opcode #b0000000000000011)
(if trace-instr
(print (list (last-pc) " sleep ")))
(set! pic18-exit #t))
;; program memory operations
((= opcode #b0000000000001000)
(program-memory-read "tblrd*" identity identity))
((= opcode #b0000000000001001)
(program-memory-read "tblrd*+" identity (lambda (adr) (+ adr 1))))
((= opcode #b0000000000001010)
(program-memory-read "tblrd*-" identity (lambda (adr) (- adr 1))))
((= opcode #b0000000000001011)
(program-memory-read "tblrd+*"
(lambda (adr) (+ adr 1))
(lambda (adr) (+ adr 1))))
((= opcode #b0000000000001100)
(program-memory-write "tblwt*" identity identity)) ;; TODO not implemented
((= opcode #b0000000000001101)
(program-memory-write "tblwt*+" identity (lambda (adr) (+ adr 1))))
((= opcode #b0000000000001110)
(program-memory-write "tblwt*-" identity (lambda (adr) (- adr 1))))
((= opcode #b0000000000001111)
(program-memory-write "tblwt+*"
(lambda (adr) (+ adr 1))
(lambda (adr) (+ adr 1))))
(else
(if trace-instr
(print (list (last-pc) " ??? ")))
(error "???")))))
; Literal operations.
(decode-opcode #b00001111 8
(lambda (opcode)
(literal-operation opcode "addlw" 'c-dc-z-ov-n
(lambda (k)
(+ k (get-wreg))))))
(decode-opcode #b00001011 8
(lambda (opcode)
(literal-operation opcode "andlw" 'z-n
(lambda (k)
(bitwise-and k (get-wreg))))))
(decode-opcode #b00001001 8
(lambda (opcode)
(literal-operation opcode "iorlw" 'z-n
(lambda (k)
(bitwise-ior k (get-wreg))))))
'
(define (lfsr f k)
(make-instruction
2
(lambda ()
(make-listing "lfsr" (file-text f) (lit-text k)))
(lambda ()
(asm-16 (bitmask "1110 1110 00ff kkkk" (file f) (quotient (lit k) 256)))
(asm-16 (bitmask "1111 0000 kkkk kkkk" (modulo (lit k) 256))))))
'
(define (movlb k)
(make-instruction
1
(lambda ()
(make-listing "movlb" (lit-text k)))
(lambda ()
(asm-16 (bitmask "0000 0001 0000 kkkk" (lit k))))))
(decode-opcode #b00001110 8
(lambda (opcode)
(literal-operation opcode "movlw" 'none
(lambda (k)
k))))
(decode-opcode #b00001101 8
(lambda (opcode)
(literal-operation opcode "mullw" 'none
(lambda (k)
(* k (get-wreg))))))
(decode-opcode #b00001100 8
(lambda (opcode)
(literal-operation opcode "retlw" 'none
(lambda (k)
(get-program-mem)
(stack-pop)
k))))
(decode-opcode #b00001000 8
(lambda (opcode)
(literal-operation opcode "sublw" 'c-dc-z-ov-n
(lambda (k)
(- k (get-wreg))))))
(decode-opcode #b00001010 8
(lambda (opcode)
(literal-operation opcode "xorlw" 'z-n
(lambda (k)
(bitwise-xor k (get-wreg))))))
;------------------------------------------------------------------------------
(define (read-hex-file filename)
(define addr-width 32)
(define (syntax-error)
(error "*** Syntax error in HEX file"))
(let ((f
(with-exception-catcher
(lambda (exc)
#f)
(lambda ()
(open-input-file filename)))))
(define mem (make-vector 16 #f))
(define (mem-store! a b)
(let loop ((m mem)
(a a)
(x (- addr-width 4)))
(if (= x 0)
(vector-set! m a b)
(let ((i (arithmetic-shift a (- x))))
(let ((v (vector-ref m i)))
(loop (or v
(let ((v (make-vector 16 #f)))
(vector-set! m i v)
v))
(- a (arithmetic-shift i x))
(- x 4)))))))
(define (mem->list)
(define (f m a n tail)
(define (g i a n tail)
(if (>= i 0)
(g (- i 1) (- a n) n (f (vector-ref m i) a n tail))
tail))
(if m
(if (= n 1)
(cons (cons (- a 1) m) tail)
(g 15 a (quotient n 16) tail))
tail))
(f mem (expt 2 addr-width) (expt 2 addr-width) '()))
(define hi16
0)
(define (read-hex-nibble)
(let ((c (read-char f)))
(cond ((and (char>=? c #\0) (char<=? c #\9))
(- (char->integer c) (char->integer #\0)))
((and (char>=? c #\A) (char<=? c #\F))
(+ 10 (- (char->integer c) (char->integer #\A))))
((and (char>=? c #\a) (char<=? c #\f))
(+ 10 (- (char->integer c) (char->integer #\a))))
(else
(syntax-error)))))
(define (read-hex-byte)
(let* ((a (read-hex-nibble))
(b (read-hex-nibble)))
(+ b (* a 16))))
(if f
(begin
(let loop1 ()
(let ((c (read-char f)))
(cond ((not (char? c)))
((or (char=? c #\linefeed)
(char=? c #\return))
(loop1))
((not (char=? c #\:))
(syntax-error))
(else
(let* ((len (read-hex-byte))
(a1 (read-hex-byte))
(a2 (read-hex-byte))
(type (read-hex-byte)))
(let* ((adr (+ a2 (* 256 a1)))
(sum (+ len a1 a2 type)))
(cond ((= type 0)
(let loop2 ((i 0))
(if (< i len)
(let ((a (+ adr (* hi16 65536)))
(b (read-hex-byte)))
(mem-store! a b)
(set! adr (modulo (+ adr 1) 65536))
(set! sum (+ sum b))
(loop2 (+ i 1))))))
((= type 1)
(if (not (= len 0))
(syntax-error)))
((= type 4)