-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpicforth.fs
2605 lines (2106 loc) · 58 KB
/
picforth.fs
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
\
\ PicForth: Forth compiler for Microchip PIC 16F87x/16F88
\
\ Copyright (c) 2002-2015 Samuel Tardieu <[email protected]>
\
\ This compiler is released under the GNU General Public License version 3,
\ see the COPYING file in the same directory, as well as the README.
\
hex
\ Turn warnings off, as we redefine existing Forth words
warnings off
\ ----------------------------------------------------------------------
\ PicForth name spaces
\ ----------------------------------------------------------------------
\ We do use several vocabularies in PicForth:
\ + picassembler: contains the postfix/prefix PIC assembler
\ + picforth: contains the code compiled for the PIC
\ + metacompiler: contains code needed for meta compilation, such as
\ immediate words
vocabulary picassembler
vocabulary picforth
vocabulary metacompiler
vocabulary macrowords
\ We do use several modes here:
\ + picasm
\ In this mode, definitions go the picassembler vocabulary. forth words
\ are visible, as well as picassembler ones and then metacompiler ones.
\ + meta
\ In this mode, definitions go to the metacompiler vocabulary. forth
\ words are visible, as well as metacompiler ones. picassembler words
\ are next, so that immediate words can be defined using the assembler,
\ then the picforth word list to ease reference to already defined
\ words. Care must be taken in this mode as newly defined words may be
\ hidden by existing forth words. In short, meta should only be used
\ to define words that can be seen when compiling for the target.
\ + target
\ In this mode, we do compile for the target. Target words are seen
\ first, then metacompiler ones. Note that forth words are not visible
\ to prevent mistakes (such as using a word that has not been defined
\ in picforth but is present in the host's vocabulary).
\ + host
\ Regular mode: forth dictionary is first and inserted into, then
\ the picforth and picassembler dictionary.
\ + macrowords
\ Words : and ; come from the host vocabulary, others from the
\ metacompiler or target ones.
: picasm only metacompiler also picassembler definitions also forth ;
: meta only picforth also picassembler also metacompiler definitions
also forth ;
: target only metacompiler also picforth definitions ;
: host only picassembler also picforth also forth definitions ;
: macro only metacompiler definitions also picforth also macrowords ;
\ Import a word from the forth dictionary into the metacompiler wordlist
\ (has to be executed in meta mode)
: import: ( "name" -- )
>in @ bl parse sfind drop swap >in ! create , does> @ execute ;
\ ----------------------------------------------------------------------
\ Errors and warnings
\ ----------------------------------------------------------------------
create crlf a c, d c,
variable warnings-are-errors
variable no-warnings
: fatal-warnings ( -- ) 1 warnings-are-errors ! ;
: non-fatal-warnings ( -- ) 0 warnings-are-errors ! ;
fatal-warnings
: print-stderr ( caddr u -- ) stderr write-file throw ;
: crlf-stderr ( -- ) crlf 2 print-stderr ;
: printnl-stderr ( caddr u -- ) print-stderr crlf-stderr ;
: warning ( caddr u -- )
no-warnings @ if 2drop exit then
crlf-stderr
crlf-stderr
s" *** Warning: " print-stderr printnl-stderr
warnings-are-errors @ abort" Aborting" ;
: suspend-warnings ( -- old ) no-warnings @ 1 no-warnings ! ;
: restore-warnings ( old -- ) no-warnings ! ;
meta
import: fatal-warnings import: non-fatal-warnings
\ ----------------------------------------------------------------------
\ Memory allocation
\ ----------------------------------------------------------------------
host
variable current-mode
variable current-area
: data-mode: create 0 , does> current-mode ! ;
data-mode: udata
data-mode: idata
meta
import: udata
import: idata
\ Structure of a section:
\ - current index (address in target memory)
\ - low address in target memory
\ - high address in target memory
\ - data mode
\ - previous section in same mode
\ - data itself
host
: 'data-org current-area @ ;
: data-here 'data-org @ ;
: data-low current-area @ 1 cells + @ ;
: data-high current-area @ 2 cells + @ ;
: data-mode current-area @ 3 cells + @ ;
: data-previous current-area @ 4 cells + @ ;
: data-base-addr current-area @ 5 cells + ;
: data-bounds data-low data-high 1+ ;
: data-size data-bounds swap - ;
: data-change-high current-area @ 2 cells + ! ;
: data-org ( addr -- ) 'data-org ! ;
: data-check ( addr -- ) data-bounds within 0= abort" Out of bank space" ;
: t>abs data-low - cells data-base-addr + ;
: t! dup data-check t>abs ! ;
: t@ t>abs @ ;
\ Uninitialized data is marked with 10000 (this is doable because host
\ cells are bigger than target cell).
: initialized? t@ f0000 and 10000 <> ;
meta
: section ( low high "name" -- )
swap 2dup
create here >r dup , , , current-mode @ dup , @ , - 1+ 0 do 10000 , loop
r> current-mode @ !
does> current-area ! data-mode current-mode ! ;
\ Some devices do only have the first two banks. It is the
\ programmer's responsability not to use non-existent banks.
idata
020 07f section bank0
0a0 0ef section bank1
120 16f section bank2
1a0 1ef section bank3
\ Default is to work in bank 0
bank0
host
\ tcompile is the equivalent of state for the cross-compiler
: set true swap ! ;
: unset false swap ! ;
variable tcompile
: tcompile? tcompile @ ;
: +tcompile tcompile? abort" Already in compilation mode" tcompile set ;
: -tcompile tcompile unset ;
\ Current data bank as seen from the code generator
variable current-bank
: bank ( a -- n ) 180 and ;
: check-bank ( a -- a )
tcompile? if exit then \ The compiler takes care of banks
dup bank current-bank @ <> if
s" wrong bank may be selected" warning
then ;
\ We are not being strict here. At this time, we assume that all the
\ code fits in 8kwords (13 bits addressing) and that main data fits in one
\ bank (except special registers).
2000 constant code-size
\ This bit-set tells whether this code cell has been used or not
code-size allocate throw constant tused
: used tused + true swap c! ;
: used? tused + c@ ;
: init-tused tused code-size bounds do false i c! loop ;
init-tused
\ tcode holds the code
code-size cells allocate throw constant tcode
0 value tcshere
: tcs! dup used cells tcode + ! ;
: tcs@ cells tcode + @ ;
\ This space contains annotations explaining what the code does. For
\ example, code to normalize booleans gets annotated so that it can
\ be removed by test branches.
code-size cells allocate throw constant tnotes
: tnotes! cells tnotes + ! ;
: tnotes@ cells tnotes + @ ;
\ This space contains copies of literals or target addresses with all
\ significant bits set
code-size cells allocate throw constant tlit
: tcslit! cells tlit + ! ;
: tcslit@ cells tlit + @ ;
\ This space contains non-zero if code is actually data
code-size allocate throw constant tcsdata
: tcsdata@ tcsdata + c@ ;
: tcsdata! tcsdata + c! ;
: init-codedata tcsdata code-size bounds do 0 i c! loop ;
init-codedata
\ EEPROM
100 cells allocate throw constant teeprom
0 value teehere
: tee! cells teeprom + >r $ff and r> ! ;
: tee@ cells teeprom + @ ;
: tee-used? cells teeprom + @ -1 <> ;
: init-eeprom teeprom 100 cells bounds do -1 i ! 1 cells +loop ;
init-eeprom
\ Configuration words
variable configword-1
3fff configword-1 !
: config-mask-1 invert configword-1 @ and or configword-1 ! ;
\ Aliases for configword-1 and config-mask-1
: configword configword-1 ;
: config-mask config-mask-1 ;
variable configword-2
3fff configword-2 !
: config-mask-2 invert configword-2 @ and or configword-2 ! ;
meta
: set-fosc 1f config-mask-1 ;
00 constant fosc-lp
01 constant fosc-xt
02 constant fosc-hs
: config-switch-1
create ,
does>
@ swap if dup else 0 swap then config-mask-1
;
004 config-switch-1 set-wdte
008 config-switch-1 set-/pwrte
020 config-switch-1 set-mclre
040 config-switch-1 set-boden
080 config-switch-1 set-lvp
100 config-switch-1 set-cpd
800 config-switch-1 set-debug
\ It looks like boden/boren may be interchanged
: set-boren set-boden ;
: set-cp 3030 config-mask-1 ;
3030 constant no-cp
0000 constant full-cp
: set-ccp1 1000 config-mask-1 ;
0000 constant ccp1-rb3
1000 constant ccp1-rb0
: set-wrt 600 config-mask-1 ;
200 constant page1-wrt
400 constant page0-wrt
600 constant no-wrt
: config-switch-2
create ,
does>
@ swap if dup else 0 swap then config-mask-2
;
\ ----------------------------------------------------------------------
\ Annotations
\ ----------------------------------------------------------------------
\ Annotations are used along with code to remember whether some code
\ has been generated specifically to test for carry or zero status bits.
\ In this case, if a test of the corresponding bit occurs just after this
\ code, it will be removed and replaced by a direct bit test or conditional
\ jump instruction if possible. This allows us to generate good code even
\ since we are using a one-pass model.
host
1 constant note-z
2 constant note-c
4 constant note-invert
8 constant note-first
note-z note-invert or constant note-/z
note-c note-invert or constant note-/c
variable note
: >note note-first or note ! ;
: no-first [ note-first invert ] literal note @ and note ! ;
: no-note 0 >note ;
\ ----------------------------------------------------------------------
\ Compiler logic
\ ----------------------------------------------------------------------
host
\ deadcode? indicates whether the current code point is reachable (false)
\ or not (true)
variable deadcode?
\ optimization control
create opt-allowed 1 ,
: opt-allowed? opt-allowed @ ;
meta
: allow-optimizations 1 opt-allowed ! ;
: disallow-optimizations 0 opt-allowed ! ;
host
\ code-depth represents the number of code words which constitute a single
\ block: no jump starts from within this block and no jump can arrive in
\ the middle of this block.
variable code-depth
: add-depth code-depth +! ;
: inc-depth 1 add-depth ;
: dec-depth -1 add-depth ;
: check-code-area tcshere code-size >= abort" out of code space" ;
: org to tcshere check-code-area ;
: (cs,) tcshere tcs! note @ tcshere tnotes! tcshere 1+ org inc-depth no-first ;
: cs, deadcode? @ if drop exit then (cs,) ;
: csdata, 1 tcshere tcsdata! (cs,) ;
: l-cs, tcshere tcslit! cs, ;
: prevlastcs tcshere 2 - tcs@ ;
: prevprevlastcs tcshere 3 - tcs@ ;
: lastcs tcshere 1- tcs@ ;
: kill-note 0 tcshere tnotes! ;
: cs-erase 0 tcshere tcs! ;
: cs-rewind tcshere 1- org cs-erase kill-note dec-depth ;
: cs-rewind2 cs-rewind cs-rewind ;
: cs-rewind3 cs-rewind2 cs-rewind ;
: cs-unwind lastcs cs-rewind ;
: l-cs-unwind lastcs tcshere 1- tcslit@ cs-rewind ;
: cs-unwind2 cs-unwind cs-unwind ;
: cs-unwind3 cs-unwind2 cs-unwind ;
: no-opt 0 code-depth ! ;
: opt? opt-allowed? 0 code-depth @ < and ;
: opt2? 1 code-depth @ < ;
: opt3? 2 code-depth @ < ;
\ Incorporate immediate words from forth so that we can use them
\ while in target mode
meta
: ( postpone ( ;
: \ postpone \ ;
import: meta
import: host
import: macro
import: target
import: org
: +org tcshere + org ;
\ Include common base change commands
: decimal decimal ;
: hex hex ;
host
\ The forth> word eases debugging. You can write forth> expression ('til
\ the end of line), and it will get evaluated with the regular forth
\ dictionary placed first. The meta> word does the same thing with the
\ metacompiler vocabulary and is immediate as it is used in other
\ definitions. The target> word does the same thing with the target
\ vocabulary first.
: evaluate-eol source >in @ /string evaluate postpone \ ;
: forth> also forth evaluate-eol previous ;
: meta> also metacompiler evaluate-eol previous ; immediate
: target> also picforth evaluate-eol previous ; immediate
meta
import: forth>
\ ----------------------------------------------------------------------
\ Prefix assembler, with postfix mode
\ ----------------------------------------------------------------------
picasm
\ Allow the assembler to use a prefix notation if the user wants it instead
\ of a postfix one
variable prefix?
: prefix prefix? set ;
: postfix prefix? unset ;
: >prefix prefix? @ if >r evaluate-eol r> then ;
: ] +tcompile also picforth ;
: [ -tcompile previous ;
\ Parameterless opcodes
: clrw 103 cs, ;
: clrwdt 064 cs, ;
: nop 000 cs, ;
: retfie 009 cs, ;
: check-default-bank ( -- )
current-bank @ if
s" call, goto or return with possible non-0 bank selected" warning
then ;
: return check-default-bank 008 cs, ;
: sleep 063 cs, ;
\ Byte oriented file register operations
0 value f?
: ,f [ 1 7 lshift ] literal to f? ;
: ,w 0 to f? ;
: bofro:
create 8 lshift , does> @ >prefix swap check-bank 7f and or f? or cs, ;
7 bofro: addwf
5 bofro: andwf
9 bofro: comf
3 bofro: decf
b bofro: decfsz
a bofro: incf
f bofro: incfsz
4 bofro: iorwf
8 bofro: movf
d bofro: rlf
c bofro: rrf
2 bofro: subwf
e bofro: swapf
6 bofro: xorwf
: clrf >prefix check-bank 7f and 180 or cs, ;
: movwf >prefix check-bank 7f and 080 or cs, ;
\ Bit oriented file register operations
: bfro: create 4 or A lshift ,
does> @ >prefix swap 7 lshift or swap check-bank 7f and or cs, ;
0 bfro: bcf
1 bfro: bsf
2 bfro: btfsc
3 bfro: btfss
\ Literal operations
: lo: create 8 lshift , does> @ >prefix swap ff and or cs, ;
3e lo: addlw
39 lo: andlw
38 lo: iorlw
30 lo: movlw
34 lo: retlw
3c lo: sublw
3a lo: xorlw
\ Control operations
: co:
create 8 lshift ,
does> @ >prefix check-default-bank swap 7ff and or cs, ;
20 co: call
28 co: goto
\ Since call is also a gforth word, hide it with the picasm one
forth-wordlist set-current also picassembler : call call ; definitions previous
\ ----------------------------------------------------------------------
\ Literals operations
\ ----------------------------------------------------------------------
\ Do we have an operation working on the top of stack?
: opf? opt2? if lastcs 0800 = prevlastcs 30ff and 80 = and exit then false ;
\ Do we have a push?
: pushw? opt2? if lastcs 0080 = prevlastcs 0384 = and exit then false ;
\ Do we have a pop?
: pop? opt? if lastcs 0a84 = exit then false ;
: pop
\ pushw and pop cancel
pushw? if cs-rewind2 exit then
opf? if
\ Operation on top of stack can be made on w
cs-rewind cs-unwind 80 xor
\ Use 0103 for clrw instead of 0100 (problem on some hardware)
dup 0100 = if 3 or then
\ If we have a push before, cancel it as well
opt? if lastcs 0384 = if cs-rewind cs, exit then then
\ Use modified operation otherwise
cs,
then
\ Increment stack pointer
4 ,f incf ;
\ Is W already equal to top-of-stack?
: w=tos?
opt? if lastcs 0800 = lastcs 0080 = or if true exit then then
\ If we have a file operation with target in a file, W had been loaded
\ previously and the file is neither 0 or 4, then W is equal to TOS
opt2? if
\ File operation
lastcs 3000 and 0=
\ Operating on a file
lastcs 80 and 80 = and
\ File is not indf
lastcs 7f and 0 <> and
\ File is not fsr
lastcs 7f and 4 <> and
\ Previous operation was a load or save
prevlastcs 0800 = prevlastcs 0080 = or and
exit
then
false ;
: loadw w=tos? if exit then 0 ,w movf ;
: storew w=tos? if exit then 0 movwf ;
: push pop? if cs-rewind exit then 4 ,f decf ;
: pushw push 0 movwf ;
: popw loadw pop ;
\ Ensure that W gets the top of stack value and that Z is set accordingly
: loadw-z
loadw
lastcs 80 and 80 = if
\ We have a movwf, we do not know whether the Z bit is accurate, so
\ reload the top of stack
0 ,w movf
then ;
meta
\ Import those in the meta vocabulary
import: push import: pop import: loadw
import: storew import: pushw import: popw
import: nop import: loadw-z
: >w popw ;
: w> pushw ;
host
: literal? ( -- n ) tcshere tcslit@ ;
: literal! ( n -- ) tcshere tcslit! ;
: load-literalw ( n -- ) dup literal! ff and movlw ;
: (literal)
dup ff and if
pop? if cs-rewind load-literalw storew else load-literalw pushw then
else
literal! meta> push
0 clrf
then ;
meta
: literal (literal) ;
\ ----------------------------------------------------------------------
\ Macro words
\ ----------------------------------------------------------------------
host
: macro? s" macro!" evaluate ;
: macro! ;
only forth also macrowords definitions also forth
: macro-] state on ;
: macro-[ state off ; immediate
: ] macro-] ;
: [ postpone macro-[ ; immediate
: macro-(:noname) docol: cfa, defstart macro-] :-hook ;
: : header macro-(:noname) ;
: ; ;-hook ?struc postpone exit reveal postpone macro-[ ; immediate
: macro! postpone (literal) ;
: ( postpone ( ; immediate
: ) ) ;
: \ postpone \ ; immediate
\ ----------------------------------------------------------------------
\ Constants
\ ----------------------------------------------------------------------
host
: (t-constant)
@ tcompile? if
(literal)
else
state @ if
postpone literal macro?
then
then ;
meta
\ Constants in PicForth are immediate words which do the right thing
\ depending on the current target compilation mode.
: constant ( n "name" -- ) create , immediate does> (t-constant) ;
: [ -tcompile also forth ;
: ] +tcompile previous ;
: 2constant ( u "name" -- ) ( run: -- high low )
create dup 8 rshift , $ff and , immediate does>
>r r@ (t-constant) r> cell+ (t-constant) ;
: bit ( n port "name" -- )
100 * + meta> 2constant
;
target
\ Common constants
-1 constant true
0 constant false
00 constant indf
01 constant tmr0
02 constant pcl
03 constant status
04 constant fsr
05 constant porta
06 constant portb
07 constant portc
08 constant portd
09 constant porte
0a constant pclath
0b constant intcon
0c constant pir1
0d constant pir2
0e constant tmr1l
0f constant tmr1h
10 constant t1con
11 constant tmr2
12 constant t2con
13 constant sspbuf
14 constant sspcon
15 constant ccpr1l
16 constant ccpr1h
17 constant ccp1con
18 constant rcsta
19 constant txreg
1a constant rcreg
1b constant ccpr2l
1c constant ccpr2h
1d constant ccp2con
1e constant adresh
1f constant adcon0
81 constant option_reg
85 constant trisa
86 constant trisb
87 constant trisc
88 constant trisd
89 constant trise
8c constant pie1
8d constant pie2
8e constant pcon
8f constant osccon
90 constant osctune
91 constant sspcon2
92 constant pr2
93 constant sspadd
94 constant sspstat
98 constant txsta
99 constant spbrg
9b constant ansel
9c constant cmcon
9d constant cvrcon
9e constant adresl
9f constant adcon1
105 constant wdtcon
10c constant eedata
10d constant eeadr
10e constant eedath
10f constant eeadrh
18c constant eecon1
18d constant eecon2
( status bits )
7 status bit irp
6 status bit reg-rp1
5 status bit reg-rp0
4 status bit not_to
3 status bit not_pd
2 status bit z
1 status bit dc
0 status bit c
( intcon bits )
7 intcon bit gie
6 intcon bit peie
5 intcon bit t0ie
4 intcon bit inte
3 intcon bit rbie
2 intcon bit t0if
1 intcon bit intf
0 intcon bit rbif
( option_reg bits )
7 option_reg bit /rbpu
6 option_reg bit intedg
5 option_reg bit t0cs
4 option_reg bit t0se
3 option_reg bit psa
2 option_reg bit ps2
1 option_reg bit ps1
0 option_reg bit ps0
( eecon1 bits )
7 eecon1 bit eepgd
3 eecon1 bit wrerr
2 eecon1 bit wren
1 eecon1 bit wr
0 eecon1 bit rd
( txsta bits )
7 txsta bit csrc
6 txsta bit tx9
5 txsta bit txen
4 txsta bit sync
2 txsta bit brgh
1 txsta bit trmt
0 txsta bit tx9d
( rcsta bits )
7 rcsta bit spen
6 rcsta bit rx9
5 rcsta bit sren
4 rcsta bit cren
3 rcsta bit adden
2 rcsta bit ferr
1 rcsta bit oerr
0 rcsta bit rx9d
( pir1 bits )
7 pir1 bit pspif
6 pir1 bit adif
5 pir1 bit rcif
4 pir1 bit txif
3 pir1 bit sspif
2 pir1 bit ccp1if
1 pir1 bit tmr2if
0 pir1 bit tmr1if
( pir2 bits )
7 pir2 bit osfif
6 pir2 bit cmif
4 pir2 bit eeif
( pie1 bits )
7 pie1 bit pspie
6 pie1 bit adie
5 pie1 bit rcie
4 pie1 bit txie
3 pie1 bit sspie
2 pie1 bit ccp1ie
1 pie1 bit tmr2ie
0 pie1 bit tmr1ie
( pie2 bits )
7 pie2 bit osfie
6 pie2 bit cmie
4 pie2 bit eeie
( t1con bits )
6 t1con bit t1run
5 t1con bit t1ckps1
4 t1con bit t1ckps0
3 t1con bit t1oscen
2 t1con bit /t1sync
1 t1con bit tmr1cs
0 t1con bit tmr1on
( t2con bits )
6 t2con bit toutps3
5 t2con bit toutps2
4 t2con bit toutps1
3 t2con bit toutps0
2 t2con bit tmr2on
1 t2con bit t2ckps1
0 t2con bit t2ckps0
( sspcon bits )
7 sspcon bit wcol
6 sspcon bit sspov
5 sspcon bit sspen
4 sspcon bit ckp
3 sspcon bit sspm3
2 sspcon bit sspm2
1 sspcon bit sspm1
0 sspcon bit sspm0
( sspcon2 bits )
7 sspcon2 bit gcen
6 sspcon2 bit ackstat
5 sspcon2 bit ackdt
4 sspcon2 bit acken
3 sspcon2 bit rcen
2 sspcon2 bit pen
1 sspcon2 bit rsen
0 sspcon2 bit sen
( sspstat bits )
7 sspstat bit smp
6 sspstat bit cke
5 sspstat bit d/a
4 sspstat bit p
3 sspstat bit s
2 sspstat bit r/w
1 sspstat bit ua
0 sspstat bit bf
( ansel bits )
6 ansel bit ans6
5 ansel bit ans5
4 ansel bit ans4
3 ansel bit ans3
2 ansel bit ans2
1 ansel bit ans1
0 ansel bit ans0
( adcon0 bits )
7 adcon0 bit adcs1
6 adcon0 bit adcs0
5 adcon0 bit chs2
4 adcon0 bit chs1
3 adcon0 bit chs0
2 adcon0 bit go//done
0 adcon0 bit adon
( adcon1 bits )
7 adcon1 bit adfm
6 adcon1 bit adcs2
5 adcon1 bit vcfg1
4 adcon1 bit vcfg0
( ccp1con bits )
5 ccp1con bit ccp1x
4 ccp1con bit ccp1y
3 ccp1con bit ccp1m3
2 ccp1con bit ccp1m2
1 ccp1con bit ccp1m1
0 ccp1con bit ccp1m0
( pcon bits )
1 pcon bit /por
0 pcon bit /bor
( osccon bits )
6 osccon bit ircf2
5 osccon bit ircf1
4 osccon bit ircf0
3 osccon bit osts
2 osccon bit iufs
1 osccon bit scs1
0 osccon bit scs0
( osctune bits )
5 osctune bit tun5
4 osctune bit tun4
3 osctune bit tun3
2 osctune bit tun2
1 osctune bit tun1
0 osctune bit tun0
\ ----------------------------------------------------------------------
\ Data and variables
\ ----------------------------------------------------------------------
meta
\ RAM
: create ( "name" -- ) create data-here , immediate does> (t-constant) ;
: , data-here t! data-here 1+ data-org ;
: c, \ 8 bits system, "c," and "," are equivalent
meta> ,
;
: allot data-here + dup data-org 1- data-check ;
: variable
meta> create 1 allot
;
\ Flags
variable flag.curaddr
8 value flag.nextbit \ set as 8, to force new var allocation on first usage
: flag ( "name" -- )
flag.nextbit 8 >= if
\ run out of bits in current cached var, alloc another 8-bit var
data-here flag.curaddr ! meta> 1 allot
0 to flag.nextbit \ and can start off again from bit 0
then
\ create word which allows us to manipulate this bit
flag.nextbit flag.curaddr @ bit
\ and update our counter to point to next available bit
flag.nextbit 1+ to flag.nextbit
;
\ EEPROM
: eecreate
create teehere , immediate
does>
@ tcompile? if
(literal)
else
state @ if
postpone literal macro?
then
then
;
: eeallot ( n -- ) teehere + to teehere ;
: eevariable eecreate 1 eeallot ;
: ee, ( b -- )
teehere tee!
teehere 1+ to teehere
;
: (sliteral) ( addr n -- ) ( run: -- eeaddr n )
teehere (literal) dup (literal) 0 ?do dup c@ ee, char+ loop drop