-
Notifications
You must be signed in to change notification settings - Fork 4
/
bootstrap.f
968 lines (834 loc) · 14.1 KB
/
bootstrap.f
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
<tmp> definitions:
\ Define addresses of the stack and tmp variables.
: stack 8 ;
: tmp 0 ;
: io-port 16412 ;
\ Macro to move the top of stack into tmp.
: >TMP
stack LDA.ZX
tmp STA.Z
stack 1+ LDA.ZX
tmp 1+ STA.Z
;
\ special case these instructions
: TXA 138 c, ;
: TYA 152 c, ;
: TXS 154 c, ;
: emit [
stack LDA.ZX
io-port STA \ $401C
INX
INX
] ;
: cr
10 emit ;
<perm> definitions:
: and [
stack LDA.ZX
stack 2 + AND.ZX
stack 2 + STA.ZX
stack 1+ LDA.ZX
stack 3 + AND.ZX
stack 3 + STA.ZX
INX INX
] ;
: or [
stack LDA.ZX
stack 2 + ORA.ZX
stack 2 + STA.ZX
stack 1+ LDA.ZX
stack 3 + ORA.ZX
stack 3 + STA.ZX
INX INX
] ;
: xor [
stack LDA.ZX
stack 2 + EOR.ZX
stack 2 + STA.ZX
stack 1+ LDA.ZX
stack 3 + EOR.ZX
stack 3 + STA.ZX
INX INX
] ;
\ a b -- a b a
: over [
DEX
DEX
stack 4 + LDA.ZX
stack STA.ZX
stack 5 + LDA.ZX
stack 1 + STA.ZX ] ;
\ ( a b c -- b c a )
: rot [
stack LDA.ZX
PHA
stack 2 + LDY.ZX
stack 4 + LDA.ZX
stack STA.ZX
stack 4 + STY.ZX
PLA
stack 2 + STA.ZX
stack 1 + LDA.ZX
PHA
stack 3 + LDY.ZX
stack 5 + LDA.ZX
stack 1 + STA.ZX
stack 5 + STY.ZX
PLA
stack 3 + STA.ZX
] ;
<tmp> definitions:
\ Define the inline assembly language IF and THEN constructs.
\ Rather than using labeled branches, we can do structured
\ control flow. IFEQ, for example, will branch to the matching
\ THEN if the Z flag is non-zero.
: IF chere @ ;
: IFEQ 0 BNE IF ;
: IFNE 0 BEQ IF ;
: IFCC 0 BCS IF ;
: IFCS 0 BCC IF ;
: IFVC 0 BVS IF ;
: IFVS 0 BVC IF ;
: IFPL 0 BMI IF ;
: IFMI 0 BPL IF ;
: THEN
dup
chere @ swap -
swap 1- c!
;
: ELSE
CLV
IFVS
swap
THEN
;
\ Define the assembly language looping constructs.
\ A BEGIN..UNTIL loop will continue looping until
\ the condition code given is set.
: BEGIN chere @ ;
: UNTIL chere @ - 2 - ;
: UNTILEQ UNTIL BNE ;
: UNTILNE UNTIL BEQ ;
: UNTILCC UNTIL BCS ;
: UNTILCS UNTIL BCC ;
: UNTILVC UNTIL BVS ;
: UNTILVS UNTIL BVC ;
: UNTILPL UNTIL BMI ;
: UNTILMI UNTIL BPL ;
\ Define BEGIN..WHILE..REPEAT loops,
\ which are like while loops in C:
\ while (/* BEGIN */ cond /* WHILE */) { ... /* REPEAT */}
: WHILE chere @ ;
: WHILEEQ 0 BNE WHILE ;
: WHILENE 0 BEQ WHILE ;
: WHILECC 0 BCS WHILE ;
: WHILECS 0 BCC WHILE ;
: WHILEVC 0 BVS WHILE ;
: WHILEVS 0 BVC WHILE ;
: WHILEPL 0 BMI WHILE ;
: WHILEMI 0 BPL WHILE ;
: REPEAT
CLV
swap chere @ - 2 - BVC \ bra to start of loop
dup
chere @ swap -
swap 1- c!
;
\ Gets the flags1 byte of the dictionary entry
: flags dhere @ dict::len + ;
: immediate flags @ 128 xor flags ! ;
immediate \ mark the word immediate as immediate
: always-inline immediate
flags @ 64 xor flags ! ;
\ Prints out the whole stack
: .s [
BEGIN
TXA
79 CMP.#
WHILENE
] . [
REPEAT
] ;
<perm> definitions:
: >byte [
stack 1+ LDA.ZX
stack STA.ZX
0 LDA.#
stack 1+ STA.ZX
] ;
: <byte always-inline [
0 LDA.#
stack 1+ STA.ZX
] ;
<tmp> definitions:
\ Pronounces tick, finds the given word
: ' word find ;
\ Takes the next word and compiles it even if it's immediate
: [compile] immediate
' JSR
;
: literal immediate
DEX DEX
dup
<byte LDA.#
stack STA.ZX
>byte LDA.#
stack 1+ STA.ZX
;
: ['] immediate ' [compile] literal ;
<perm> definitions:
: = [
INX INX
stack 2 - LDA.ZX
stack CMP.ZX
IFNE
0 LDA.#
stack STA.ZX
stack 1+ STA.ZX
RTS
THEN
0 LDY.#
stack 1- LDA.ZX
stack 1+ CMP.ZX
IFEQ
DEY
THEN
stack STY.ZX
stack 1+ STY.ZX
] ;
: <> [
INX INX
stack 2 - LDA.ZX
stack CMP.ZX
IFNE
255 LDA.#
stack STA.ZX
stack 1+ STA.ZX
RTS
THEN
0 LDY.#
stack 1- LDA.ZX
stack 1+ CMP.ZX
IFNE
DEY
THEN
stack STY.ZX
stack 1+ STY.ZX
] ;
: 0= [
0 LDY.#
stack LDA.ZX
stack 1+ ORA.ZX
IFEQ
DEY
THEN
stack STY.ZX
stack 1+ STY.ZX
] ;
: 0> [
0 LDY.#
stack 1+ LDA.ZX
IFPL
DEY
THEN
stack STY.ZX
stack 1+ STY.ZX
] ;
\ See http://www.6502.org/tutorials/compare_beyond.html#6
: > [
255 LDY.#
stack LDA.ZX
stack 2 + CMP.ZX
stack 1+ LDA.ZX
stack 3 + SBC.ZX
IFVS
128 EOR.#
THEN
\ Now N flag contains comparison result
IFPL
INY
THEN
INX INX
stack STY.ZX
stack 1+ STY.ZX
] ;
: < [
255 LDY.#
stack 2 + LDA.ZX
stack CMP.ZX
stack 3 + LDA.ZX
stack 1+ SBC.ZX
IFVS
128 EOR.#
THEN
\ Now N flag contains comparison result
IFPL
INY
THEN
INX INX
stack STY.ZX
stack 1+ STY.ZX
] ;
: u< [
\ TODO
255 LDY.#
stack 1+ LDA.ZX
stack 3 + CMP.ZX
IFCS
IFEQ
stack LDA.ZX
stack 2 + CMP.ZX
ELSE
THEN
THEN
] ;
: u> [
\ TODO
] ;
\ Logical shift right
\ ( u -- u )
: lsr [
stack 1+ LSR.ZX
stack ROR.ZX
] ;
\ Arithmetic shift right
\ ( i -- i )
: asr [
stack 1+ LDA.ZX
128 CMP.#
stack 1+ ROR.ZX
stack ROR.ZX
] ;
\ Arithmetic shift left
: asl [
stack ASL.ZX
stack 1+ ROL.ZX
] ;
: +! [
>TMP
0 LDY.#
tmp LDA.IY
CLC
stack 2 + ADC.ZX
tmp STA.IY
INY
tmp LDA.IY
stack 3 + ADC.ZX
tmp STA.IY
INX INX
INX INX
] ;
: -! [
>TMP
0 LDY.#
tmp LDA.IY
SEC
stack 2 + SBC.ZX
tmp STA.IY
INY
tmp LDA.IY
stack 3 + SBC.ZX
tmp STA.IY
INX INX
INX INX
] ;
: c>r always-inline [
stack LDA.ZX
PHA
INX
INX
] ;
: cr> always-inline [
DEX
DEX
PLA
stack STA.ZX
0 LDA.#
stack 1+ STA.ZX
] ;
: >r always-inline [
stack 1+ LDA.ZX
PHA
stack LDA.ZX
PHA
INX
INX
] ;
: r> always-inline [
DEX
DEX
PLA
stack STA.ZX
PLA
stack 1+ STA.ZX
] ;
\ ( r: a -- )
: rdrop always-inline [
PLA
PLA
] ;
\ ( -- sp )
: dsp@ [
DEX
DEX
TXA
stack STA.ZX
0 LDY.#
stack 1+ STY.ZX
] ;
\ ( sp -- )
: dsp! [
stack LDA.ZX
TAX
] ;
<tmp> definitions:
: hex 16 base ! ;
: decimal 10 base ! ;
: '\n' 10 ;
: bl 32 ;
: space bl emit ;
\ Recursively call the current word
: recurse immediate
dhere @
JSR
;
: recurse-tail immediate
dhere @
JMP
;
: POP INX INX ;
<perm> definitions:
: negate 0 swap - ;
: true 0 1 - ;
: false 0 ;
: not 0= ;
: 2- 2 - ;
: 2+ 2 + ;
: allot
vhere +!
;
<tmp> definitions:
\ Save branch instruction address
: if immediate
POP
stack 2- LDA.ZX
stack 1- ORA.ZX
chere @
0 BEQ
;
: unless immediate
['] not JSR
[compile] if
;
\ Write the branch target to here.
: then immediate
dup
chere @ swap - 2-
swap 1+ c!
;
: else immediate
chere @ 1+
swap
CLV 0 BVC
dup
chere @ swap - 2-
swap 1+ c!
;
: begin immediate
\ [compile] debug
chere @
;
\ ( branch-target -- )
: repeat immediate
CLV
chere @ - 2- BVC
;
\ ( branch-target -- )
: until immediate
POP
stack 2- LDA.ZX
stack 1- ORA.ZX
chere @ - 2- BEQ
;
: while
POP
stack 2- LDA.ZX
stack 1- ORA.ZX
chere @ - 2- BNE
;
: char word drop @ ;
: '(' [ char ( ] literal ;
: ')' [ char ) ] literal ;
: '"' [ char " ] literal ;
: ( immediate
1
begin
key
dup '(' = if
drop
1+
else
')' = if
1-
then
then
dup 0= until
drop
;
( Now I can write comments using (nested) parens )
( Declares a constant value. Use like `10 constant VariableName`)
: constant immediate
word
create
[compile] literal
RTS
;
( Declares an uninitialized variable, giving it space
after vhere )
: variable immediate
vhere @
2 allot
[compile] constant
;
( xt -- impl )
: >impl
dict::impl + @
;
( Takes a dictionary entry and prints the name of the word )
: id.
dict::len + ( Skip the pointers )
dup c@ ( get the length )
31 and ( Mask the flags )
begin
swap 1+ ( addr len -- len addr+1 )
dup c@ ( len addr -- len addr char )
emit
swap 1- ( len addr -- addr len-1 )
dup 0=
until
drop
drop
;
( True to hide the word from dictionary searches )
: ?hidden
dict::len +
c@
32 and
;
( True if the word is the last in the dictionary )
: ?end
dict::flags +
c@
128 and
;
( True if the word is a single-byte val )
: ?byte
dict::flags +
c@
64 and
;
( True if the word should execute immediately in compile mode )
: ?immediate
dict::len +
c@
128 and
;
( Returns the next dictionary entry. )
: next
dup ?end if
drop
0
else
dup dict::len + c@ 63 and + dict::name +
then
;
( Prints all the words in the dictionary )
( dict-start -- )
: words'
begin
dup ?hidden not if
dup id.
space
then
next
dup 0=
until
drop ( drop null pointer )
cr
;
( Prints all the words in all the dictionaries )
: words
dicts
begin
words'
dup 0= until
drop
;
: compiling? state @ ;
( -- )
: ." immediate
compiling? if
['] (.') JSR ( compile jsr (.") )
begin
key
dup '"' <> if
c,
0
then
until
0 c,
else
begin
key
dup '"' <> if
emit
0
then
until
then
;
: welcome
." Welcome to Forth!" cr
;
welcome
( A variable which, when called, pushes its value instead of its address )
: val
vhere @ ( get the variable address )
2 allot ( allot two variables )
word
create ( create a new dictionary entry )
DEX DEX
dup LDA ( load the value from the variable and push it to the stack )
stack STA.ZX
dup 1+ LDA
stack 1+ STA.ZX
RTS
( initialize val )
!
['] always-inline execute
;
: c-val
vhere @ ( get the variable address )
1 allot
word
create ( create a new dictionary entry )
dhere @ c-val-tog
DEX DEX
dup LDA ( load the value and push it )
stack STA.ZX
0 LDA.#
stack 1+ STA.ZX
RTS
( initialize )
!
;
( Gets the address of a val and it's size )
: val-addr
word
find
dup 0= if
drop
drop
drop
." Cannot get address of unknown val." cr
quit
then
dup >impl 3 + @ ( read variable address from val impl )
swap ?byte
;
( Writes a value to a `val` variable )
: to immediate
val-addr
compiling? if
if ( single byte )
stack LDA.ZX
STA
INX INX
else
dup
stack LDA.ZX
STA
stack 1+ LDA.ZX
1+ STA
INX INX
then
else
if ( single byte )
c!
else
!
then
then
;
: heredoc immediate
word
create
chere @ 21 +
DEX
DEX
dup <byte LDA.#
stack STA.ZX
>byte LDA.#
stack 1+ STA.ZX
DEX
DEX
dup <byte LDA.#
stack STA.ZX
dup >byte LDA.#
stack 1+ STA.ZX
RTS
begin
key c,
1-
dup 0= until
drop
;
hex
( xt -- )
: set-reset! 0FFFC ! ;
( xt -- )
: set-nmi! 0FFFA ! ;
( xt -- )
: set-irq! 0FFFE ! ;
' thaw set-reset!
( Ends an interrupt handler definiton )
: ;int immediate
40 c, \ append rti
dhere @ hidden \ unhide
[compile] [
;
decimal
: int-handle ;int
' int-handle set-nmi!
' int-handle set-irq!
( new-xt old-xt -- )
( Redefines old as new, so that all calls to old
will instead call new. Doesn't rePLAce inlined calls )
: monkey-patch
dict::impl + !
;
: inline,
1-
begin
1+ dup
c@ dup c,
96 =
until
drop
\ undo writing the rts
chere @ 1- chere !
;
( a simple inline which just copies the impl until hitting an rts.
It will be confused by any 0x60 byte )
: [inline] immediate
word find >impl inline,
;
: disas
20
begin
swap
see
swap 1-
dup 0=
until
drop
drop
;
: show-disas ' >impl disas ;
: do immediate
\ inline code to put the loop bound then
c-sp LDY.Z
DEY
DEY
c-sp STY.Z
stack LDA.ZX
cstack STA.Y
stack 2+ LDA.ZX
cstack 1+ STA.Y
INX
INX
INX
INX \ drop the values on the stack
\ save the address of the beginning of the loop
chere @
;
: loop immediate
c-sp LDY.Z
cstack LDA.Y
CLC
1 ADC.#
cstack 1+ CMP.Y
cstack STA.Y
UNTILEQ
;
<perm> definitions:
: i [
DEX
DEX
0 LDA.#
stack 1+ STA.ZX
c-sp LDY.Z
cstack LDA.Y
stack STA.ZX
] ;
: save-for-interrupt always-inline [
DEX DEX \ make room for the red zone
PHA
TYA
PHA
tmp LDA.Z
PHA
tmp 1+ LDA.Z
PHA
tmp 2 + LDA.Z
PHA
tmp 3 + LDA.Z
PHA
tmp 4 + LDA.Z
PHA
tmp 5 + LDA.Z
PHA
tmp 6 + LDA.Z
PHA
tmp 7 + LDA.Z
PHA
] ;
: restore-for-interrupt always-inline [
PLA
tmp 7 + STA.Z
PLA
tmp 6 + STA.Z
PLA
tmp 5 + STA.Z
PLA
tmp 4 + STA.Z
PLA
tmp 3 + STA.Z
PLA
tmp 2 + STA.Z
PLA
tmp 1+ STA.Z
PLA
tmp STA.Z
PLA
TAY
PLA
INX INX \ remove the red zone
] ;
hex
\ Wait a few frames for the PPU to stabilize on power-on
: wait-for-ppu [
BEGIN
2002 BIT
UNTILMI
BEGIN
2002 BIT
UNTILMI
BEGIN
2002 BIT
UNTILMI
BEGIN
2002 BIT
UNTILMI
] ;
' nmi set-nmi!