-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
nvram.txt
5121 lines (2712 loc) · 127 KB
/
nvram.txt
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
\ ==== Boot Block ==============================================
-1 loaded !
; ==== Short Help ==============================================
This is a subroutine threaded Forth, based on eForth. The
starting base in in hexadecimal. A tutorial, word glossary and
extra code is stored in block storage. A short list of commands
follows ('#' are numbers):
words list all Forth words
see decompile the next word in the input stream
# #2 index get descriptive titles for blocks from '#' to '#2'
# list list block number '#'
# load execute code in block '#'
# #2 thru execute code in blocks '#' to '#2'
; ==== Forth Error Messages: 1/4 ===============================
-1 ABORT
-2 ABORT"
-3 stack overflow
-4 stack underflow
-5 return stack overflow
-6 return stack underflow
-7 do-loops nested too deeply during execution
-8 dictionary overflow
-9 invalid memory address
-10 division by zero
-11 result out of range
-12 argument type mismatch
-13 undefined word
-14 interpreting a compile-only word
-15 invalid FORGET
; ==== Forth Error Messages: 2/4 ===============================
-16 attempt to use zero-length string as a name
-17 pictured numeric output string overflow
-18 parsed string overflow
-19 definition name too long
-20 write to a read-only location
-21 unsupported operation
-22 control structure mismatch
-23 address alignment exception
-24 invalid numeric argument
-25 return stack imbalance
-26 loop parameters unavailable
-27 invalid recursion
-28 user interrupt
-29 compiler nesting
-30 obsolescent feature
; ==== Forth Error Messages: 3/4 ===============================
-31 >BODY used on non-CREATEd definition
-32 invalid name argument (e.g., TO xxx)
-33 block read exception
-34 block write exception
-35 invalid block number
-36 invalid file position
-37 file I/O exception
-38 non-existent file
-39 unexpected end of file
-40 invalid BASE for floating point conversion
-41 loss of precision
-42 floating-point divide by zero
-43 floating-point result out of range
-44 floating-point stack overflow
-45 floating-point stack underflow
; ==== Forth Error Messages: 4/4 ===============================
-46 floating-point invalid argument
-47 compilation word list deleted
-48 invalid POSTPONE
-49 search-order overflow
-50 search-order underflow
-51 compilation word list changed
-52 control-flow stack overflow
-53 exception stack overflow
-54 floating-point underflow
-55 floating-point unidentified fault
-56 QUIT
-57 exception in sending or receiving a character
-58 [IF], [ELSE], or [THEN] exception
; ==== Description of this file ================================
The next screens contain extra code that can be loaded into the
interpreter if needed. Some of the standard Forth words are
placed here instead of in the image to save on space.
The blocks should be loaded in order unless otherwise stated.
This file is first converted by a utility into a fixed width
format, which can then be loaded onto the target board with
the same utility used to uploaded the bitfile to the FPGA.
Line lengths are limited to 64 characters wide, each block
contains 16 lines. The first line should contain a
description of the block so that when it is displayed with
'index' a glossary can be viewed, allowing quick navigation.
; ==== Scratch Block ===========================================
\ ==== Block Editor 1/2 ========================================
( Block Editor: There is a help section later on )
variable editor-voc 0 editor-voc ! forth
: editor decimal editor-voc 1 set-order ;
get-order editor-voc swap 1+ set-order
$40 constant c/l $10 constant l/b
: (block) blk @ block ;
: (check) dup b/buf c/l / u>= if -24 throw then ;
: (line) (check) c/l * (block) + ;
: b block drop ;
: l blk @ list ;
: n 1 +block b l ;
: p -1 +block b l ;
: d (line) c/l blank ;
: x (block) b/buf blank ;
: s update save-buffers ;
\ ==== Block Editor 2/2 ========================================
: q forth save-buffers ;
: e forth blk @ load editor ;
: ia c/l * + (block) + source drop >in @ +
swap source nip >in @ - cmove [compile] \ ;
: i 0 swap ia ;
: u update ;
: w words ;
: yank pad c/l ;
: c (line) yank >r swap r> cmove ;
: y (line) yank cmove ;
: ct swap y c ;
: ea (line) c/l evaluate ;
: sw 2dup y (line) swap (line) swap c/l cmove c ;
forth
\ ==== Assembler word set ======================================
variable assembler-voc
bl parse rdrop pad pack$ find drop assembler-voc !
: assembler assembler-voc 1 set-order ;
: ;code assembler ; immediate
: code [compile] : assembler ;
get-order assembler-voc swap 1+ set-order
: end-code forth [compile] ; ; immediate
: words words ;
: forth forth ;
forth
\ ==== CORDIC 1/2 ==============================================
variable lookup -1 cells allot ( 16 values )
$3243 , $1DAC , $0FAD , $07F5 , $03FE , $01FF , $00FF , $007F ,
$003F , $001F , $000F , $0007 , $0003 , $0001 , $0000 , $0000 ,
: arshift ( n u -- n : arithmetic right shift )
2dup rshift >r swap $8000 and
if $10 swap - -1 swap lshift else drop 0 then r> or ;
$26DD constant cordic_1K $6487 constant pi/2
variable tx 0 tx ! variable ty 0 ty ! variable tz 0 tz !
variable x 0 x ! variable y 0 y ! variable z 0 z !
variable d 0 d ! variable k 0 k !
\ ==== CORDIC 2/2 ==============================================
( CORDIC: valid in range -pi/2 to pi/2, arguments are in fixed )
( point format with 1 = 16384, angle is given in radians. )
: cordic ( angle -- sine cosine )
z ! cordic_1K x ! 0 y ! 0 k !
$10 begin ?dup while
z @ 0< d !
x @ y @ k @ arshift d @ xor d @ - - tx !
y @ x @ k @ arshift d @ xor d @ - + ty !
z @ k @ cells lookup + @ d @ xor d @ - - tz !
tx @ x ! ty @ y ! tz @ z !
k 1+!
1-
repeat y @ x @ ;
: sin cordic drop ;
: cos cordic nip ;
\ ==== Login Code 1/2 ==========================================
\ Login and user management system
: generate count dup >r crc r> ccitt ; ( b -- u )
: .user ." user>" space ; ( -- )
: .password ." password>" space ; ( -- )
variable user0 0 user0 !
: mk.user ( --; <string1>, <string2> )
here user0 @ , user0 ! here 0 , bl word count 1+ allot align
drop bl word generate swap ! ;
: ls.user ( -- : list all users in user database )
cr user0 @
begin dup while dup 2 cells + space count type cr @ repeat
drop cr ;
: find.user ( a -- u | 0 : find user in database, return hash )
>r user0 @ begin dup while dup 2 cells + count r@ count
=string if rdrop exit then @ repeat rdrop drop 0 ;
\ ==== Login Code 2/2 ==========================================
: (password) ( u --, <string> )
>r begin .password query bl word cr generate
r@ = until rdrop ;
: fake .password query bl word drop cr ;
: (user)
begin .user query bl word cr find.user ?dup until ;
: retry ( xt -- : retry word until it succeeds )
>r begin r@ catch 0= until rdrop ;
: user? ' (user) retry ;
: password? ' (password) retry ;
: hide-all 0 1 set-order ;
: login hide-all
cr user? cell+ @ conceal password? interactive forth ;
mk.user guest guest mk.user archer dangerzone
mk.user lana sterling mk.user cyril figgis
\ ==== Extra Code 1/7 ==========================================
: 2+ 2 + ; ( n -- n )
: 2- 2 - ; ( n -- n )
: >= < invert ; ( n n -- f )
: simulation? cpu-id $0666 <> ; ( -- f : are we in the matrix? )
: 0<= 0> 0= ; ( n n -- f )
: 0>= 0< 0= ; ( n n -- f )
: not -1 xor ; ( n -- n )
: dabs dup 0< if dnegate then ; ( d -- d )
: d+ >r swap >r um+ r> r> + + ; ( d d -- d )
: d= >r swap r> = >r = r> and ; ( d d -- f )
: d<> d= 0= ; ( d d -- f )
: roll dup 0> if swap >r 1- recurse r> swap else drop then ;
: ?exit if rdrop then ; ( n --, R: n -- n | )
: 2rdrop r> rdrop rdrop >r ; ( R n n -- )
: 2. swap . . ; ( n n -- )
\ ==== Extra Code 2/7 ==========================================
: m* 2dup xor 0< >r abs swap abs um* r> if dnegate then ;
: */mod >r m* r> m/mod ; ( n n n -- r q )
: */ */mod nip ; ( n n n -- q )
: s>d dup 0< ; ( n -- d : single to double )
: holds begin dup while 1- 2dup + c@ hold repeat 2drop ;
: binary 2 base ! ; ( -- )
: octal 8 base ! ; ( -- )
: .base base @ dup decimal base ! ; ( -- )
: only -1 set-order ;
: also get-order over swap 1+ set-order ;
: previous get-order swap drop 1- set-order ;
: buffer block ; ( k -- a )
: bye [ 0 , ] ;
: enum dup constant 1+ ; ( n --, <string> )
: logical 0= 0= ; ( n -- f )
\ ==== Extra Code 3/7 ==========================================
: square dup * ; ( n -- )
: limit rot min max ; ( n lo hi -- n )
: odd 1 and logical ; ( n -- )
: even odd invert ; ( n -- )
: nor or invert ; ( u u -- u )
: nand and invert ; ( u u -- u )
: bell 7 emit ; ( -- )
: under >r dup r> ; ( n1 n2 -- n1 n1 n2 )
: 2nip >r >r 2drop r> r> ; ( n1 n2 n3 n4 -- n3 n4 )
( n1 n2 n3 n4 -- n1 n2 n3 n4 n1 n2 )
: 2over >r >r 2dup r> swap >r swap r> r> -rot ;
: 2swap >r -rot r> -rot ; ( n1 n2 n3 n4 -- n3 n4 n1 n2 )
: 2tuck 2swap 2over ; ( n1 n2 n3 n4 -- n3 n4 n1 n2 n3 n4 )
: 4drop 2drop 2drop ; ( n1 n2 n3 n4 -- )
: trip dup dup ; ( n -- n n n )
\ ==== Extra Code 4/7 ==========================================
: log >r 0 swap ( u base -- u )
begin swap 1+ swap r@ / dup 0= until
drop 1- rdrop ;
: log2 0 swap ( u -- u )
begin swap 1+ swap 2/ dup 0= until
drop 1- ;
: average um+ 2 um/mod nip ; ( u u -- u )
: <=> 2dup > if 2drop -1 exit then < ;
: bounds over + swap ;
: 2, , , ; ( n n -- )
: tab 9 emit ; ( -- )
: drup drop dup ; ( n1 n2 -- n1 n1 )
: lsb $FF and ; ( u -- u )
: --> 1 +block load ;
: scr blk ;
\ ==== Extra Code 5/7 ==========================================
: signum ( n -- -1 | 0 | 1 : Signum function )
dup 0> if drop 1 exit then
0< if -1 exit then
0 ;
: >< dup 8 rshift swap 8 lshift or ; ( u -- u : swap bytes )
: #digits dup 0= if 1+ exit then base @ log 1+ ;
: ** ( n u -- n )
?dup if
over >r
begin
dup 1 >
while
swap r@ * swap 1-
repeat rdrop drop
else logical 1 and then ;
\ ==== Extra Code 6/7 ==========================================
: b. base @ swap 2 base ! u. base ! ; ( u -- )
: h. base @ swap hex u. base ! ; ( u -- )
: o. base @ swap 8 base ! u. base ! ; ( u -- )
: d. base @ swap decimal . base ! ; ( n -- )
: @bits swap @ and ; ( a u -- u )
: ?\ if [compile] \ then ; immediate
: ?( if [compile] ( then ; immediate ( )
: defined? ( -- pwd -1 | 0, <string> : is word defined? )
bl word find if -1 else drop 0 then ;
: ?if compile dup [compile] if ; immediate
: ?dup-if compile ?dup [compile] if ; immediate
: >body ( dup @ $4000 or <> if 31 -throw then ) cell+ ;
\ ==== Extra Code 7/7 ==========================================
: screens ( k1 k2 -- : list blocks k1 to k2 )
over -
for
dup . dup list 1+ key $D = if rdrop drop exit then
next drop ;
\ ==== Roulette ================================================
\ Russian Roulette: It would be interesting to do something
\ killed the computer when the player dies
: click ." *click*" cr ;
: bang ." BANG!" cr ;
: roulette random 6 mod if click else bang then ; ( -- )
\ ==== Extended ANSI Escape Codes ==============================
0 constant black 1 constant red 2 constant green 4 constant blue
red green + constant yellow
green blue + constant cyan
red blue + constant magenta
red green blue + + constant white
: background $A + ;
: color $1E + sgr ;
\ : hide-cursor CSI [char] ? emit $19 10u. [char] l emit ;
\ : show-cursor CSI [char] ? emit $19 10u. [char] h emit ;
: up [char] A ansi ; ( n -- )
: down [char] B ansi ; ( n -- )
: left [char] C ansi ; ( n -- )
: right [char] D ansi ; ( n -- )
\ ==== Screen Saver ============================================
\ An incredibly simple screen saver using ANSI Escape codes
\ for placement and coloring of random characters
base @ decimal
: screen-saver ( -- )
page
begin
random 79 mod
random 38 mod at-xy
random >char emit
random 8 mod
random 1 and if background then
random 256 and ms color
again ;
base !
; ==== Game: YOU ARE DEAD (HELP) ===============================
This is a clone of the one dimensional rogue like game
available at <https://github.com/rupa/YOU_ARE_DEAD>. The
object is to get to the other side of the screen.
Keys:
w Turn into '.'
a Turn into '~'
s Turn into '>'
d Move right
q Quit
Block number 7 is used to store the game state.
\ ==== Game: YOU ARE DEAD 1/6 ==================================
forth variable yad-voc get-order yad-voc swap 1+ set-order
$40 constant c/l $10 constant l/b
: memory 7 block ;
: variables memory c/l + ;
: score variables 0 cells + ;
: position variables 1 cells + ;
: level variables 2 cells + ;
: form variables 3 cells + ;
: continue variables 4 cells + ;
: end c/l 1- ;
: player form @ ;
: .player position @ 1+ 3 at-xy player emit ;
: .goal c/l 3 at-xy [char] # emit ;
: .score ." SCORE: " score @ 5 u.r ;
: .level ." LEVEL: " level @ 5 u.r ;
\ ==== Game: YOU ARE DEAD 2/6 ==================================
: show
page cr cr space memory c/l type
.player .goal
cr .score space .level cr ;
: select ( n -- c )
dup 0= if drop [char] < exit then
dup 1 = if drop [char] ~ exit then
dup 2 = if drop [char] . exit then
drop bl ;
: die ." YOU ARE DEAD" cr 0 continue ! -56 throw ;
: survived ." YOU SURVIVED" cr .score cr ;
: forms form c@ position @ memory + c@ ; ( -- c c )
\ ==== Game: YOU ARE DEAD 3/6 ==================================
: generate ( -- generate a level )
c/l 1- for
random 5 mod ( 3 = most difficult )
select memory r@ + c!
next
bl memory c! bl memory end + c! ;
: normal [char] x ;
: setup ( -- )
-1 continue !
memory b/buf 0 fill
normal form c!
generate ;
: +score random 4 mod 1 min score +! ; ( -- )
: ?next <> if die else +score then ; ( c c -- )
\ ==== Game: YOU ARE DEAD 4/6 ==================================
: +level
0 position ! level 1+!
random 23 mod 5 min score +!
generate ;
: monster swap >r forms r> = if ?next else 2drop then ;
: command ( -- f )
dup [char] w = if drop [char] . form c! 0 exit then
dup [char] a = if drop [char] ~ form c! 0 exit then
dup [char] d = if drop -1 exit then
dup [char] s = if drop [char] < form c! 0 exit then
dup [char] q = if drop -56 throw then
drop 0 ;
\ ==== Game: YOU ARE DEAD 5/6 ==================================
: rules
position @ end = if +level exit then
forms = if exit then
[char] ~ [char] < monster
[char] . [char] ~ monster
[char] < [char] . monster
normal form c!
bl position @ memory + c!
score 1+!
position 1+! ;
\ ==== Game: YOU ARE DEAD 6/6 ==================================
: game
begin
show
key command if rules then
level @ 9 >
until survived ;
: play setup ' game catch drop ;
get-order -rot swap rot set-order
: you-are-dead base @ decimal play base ! ;
forth
\ : resume memory drop ' game catch drop ;
; ==== Sokoban: Instructions ===================================
To run, load the next blocks 9 blocks, which contain the code
for the Sokoban game. The object of the game is to maneuver the
boulders (*) onto the pads (.) with the player character (@/~),
until all the bolders are on the pads. Sokoban levels are
stored one per block. To play:
$2A sokoban
Would load a game stored in block '$2A' and begin playing.
The WASD keys move the player around, 'h' is for help, and
'q' quits.
\ ==== Sokoban 1/9 =============================================
\ only forth definitions hex
\ variable sokoban-wordlist sokoban-wordlist +order definitions
blk @ 1- constant help-block
char X constant wall
char * constant boulder
char . constant off
char & constant on
char @ constant player
char ~ constant player+ ( player + off pad )
$10 constant l/b ( lines per block )
$40 constant c/b ( columns per block )
7 constant bell ( bell character )
variable position ( current player position )
variable moves ( moves made by player )
create rule 3 c, 0 c, 0 c, 0 c,
\ ==== Sokoban 2/9 =============================================
: n1+ swap 1+ swap ; ( n n -- n n )
: match ( a a -- f )
n1+ ( replace with umin of both counts? )
count
for aft
count rot count rot <> if 2drop rdrop 0 exit then
then next 2drop -1 ;
: beep bell emit ; ( -- )
: ?apply ( a a a -- a, R: ? -- ?| )
>r over swap match if drop r> rdrop exit then rdrop ;
\ ==== Sokoban 3/9 =============================================
: apply ( a -- a )
$" @ " $" @" ?apply
$" @." $" ~" ?apply
$" @* " $" @*" ?apply
$" @*." $" @&" ?apply
$" @&." $" ~&" ?apply
$" @& " $" ~*" ?apply
$" ~ " $" .@" ?apply
$" ~." $" .~" ?apply
$" ~* " $" .@*" ?apply
$" ~*." $" .@&" ?apply
$" ~&." $" .~&" ?apply
$" ~& " $" .~*" ?apply beep ;
\ ==== Sokoban 4/9 =============================================
: pack ( c0...cn b n -- )
2dup swap c! for aft 1+ tuck c! then next drop ;
: locate ( b u c -- u f )
>r
begin
?dup
while
1- 2dup + c@ r@ = if nip rdrop -1 exit then
repeat
rdrop
drop
0 0 ;
\ ==== Sokoban 5/9 =============================================
: 2* 1 lshift ; ( u -- )
: relative swap c/b * + + ( $3FF and ) ; ( +x +y pos -- pos )
: +position position @ relative ; ( +x +y -- pos )
: double 2* swap 2* swap ; ( u u -- u u )
: arena blk @ block b/buf ; ( -- b u )
: >arena arena drop + ; ( pos -- a )
: fetch ( +x +y -- a a a )
2dup +position >arena >r
double +position >arena r> swap
position @ >arena -rot ;
: rule@ fetch c@ rot c@ rot c@ rot ; ( +x +y -- c c c )
: 3reverse -rot swap ; ( 1 2 3 -- 3 2 1 )
: rule! rule@ 3reverse rule 3 pack ; ( +x +y -- )
: think 2dup rule! rule apply >r fetch r> ; ( +x +y --a a a a )
: count! count rot c! ; ( a a -- )
\ ==== Sokoban 6/9 =============================================
\ 'act' could be made to be more elegant, but it works, it
\ handles rules of length 2 and length 3
: act ( a a a a -- )
count swap >r 2 =
if
drop swap r> count! count!
else
3reverse r> count! count! count!
then drop ;
: #boulders ( -- n )
0 arena
for aft
dup c@ boulder = if n1+ then
1+
then next drop ;
\ ==== Sokoban 7/9 =============================================
: .boulders ." BOULDERS: " #boulders u. cr ; ( -- )
: .moves ." MOVES: " moves @ u. cr ; ( -- )
: .help ." WASD - MOVEMENT" cr ." H - HELP" cr ; ( -- )
: .maze blk @ list ; ( -- )
: show ( page cr ) .maze .boulders .moves .help ; ( -- )
: solved? #boulders 0= ; ( -- )
: finished? solved? if 1 throw then ; ( -- )
: instructions blk @ help-block list block drop key drop ;
: where >r arena r> locate ; ( c -- u f )
: player? player where 0= if drop player+ where else -1 then ;
: player! player? 0= throw position ! ; ( -- )
: start player! 0 moves ! ; ( -- )
: .winner show cr ." SOLVED!" cr ; ( -- )
: .quit cr ." Quitter!" cr ; ( -- )
: finish 1 = if .winner exit then .quit ; ( n -- )
\ ==== Sokoban 8/9 =============================================
: rules think act player! ; ( +x +y -- )
: +move 1 moves +! ; ( -- )
: ?ignore over <> if rdrop then ; ( c1 c2 --, R: x -- | x )
: left [char] a ?ignore -1 0 rules +move ; ( c -- c )
: right [char] d ?ignore 1 0 rules +move ; ( c -- c )
: up [char] w ?ignore 0 -1 rules +move ; ( c -- c )
: down [char] s ?ignore 0 1 rules +move ; ( c -- c )
: help [char] h ?ignore instructions ; ( c -- c )
: end [char] q ?ignore 2 throw ; ( c -- | c, R ? -- | ? )
: default drop ; ( c -- )
: command up down left right help end default finished? ;
: maze! block drop ; ( k -- )
: input key ; ( -- c )
\ ==== Sokoban 9/9 =============================================
\ sokoban-wordlist -order definitions
\ sokoban-wordlist +order
: sokoban ( k -- )
maze! start
begin
show input ' command catch ?dup
until finish ;
; ==== Sokoban Map 1 ===========================================
XXXXX
X X
X* X
XXX *XXX
X * * X
XXX X XXX X XXXXXX
X X XXX XXXXXXX ..X
X * * ..X
XXXXX XXXX X@XXXX ..X
X XXX XXXXXX
XXXXXXXX
; ==== Sokoban Map 2 ===========================================
XXXXXXXXXXXX
X.. X XXX
X.. X * * X
X.. X*XXXX X
X.. @ XX X
X.. X X * XX
XXXXXX XX* * X
X * * * * X
X X X
XXXXXXXXXXXX
; ==== Sokoban Map 3 ===========================================
XXXXXXXX
X @X
X *X* XX
X * *X
XX* * X
XXXXXXXXX * X XXX
X.... XX * * X
XX... * * X
X.... XXXXXXXXXX
XXXXXXXX
; ==== Sokoban Map 4 ===========================================
XXXXXXXX
X ....X
XXXXXXXXXXXX ....X
X X * * ....X
X ***X* * X ....X
X * * X ....X
X ** X* * *XXXXXXXX
XXXX * X X
X X XXXXXXXXX
X * XX
X **X** @X
X X XX
XXXXXXXXX
\ ==== Game of Life: 1/4 =======================================
\ This is a Game Of Life implementation, originally from
\ <http://wiki.c2.com/?ForthBlocks>. It has been adapted so
\ instead of blocks it uses 1KiB buffers at arbitrary memory
\ locations.
$40 constant c/b
$10 constant l/b
c/b 1- constant c/b>
l/b 1- constant l/b>
bl constant off
char * constant on
variable state1
variable state2
variable statep
\ ==== Game of Life: 2/4 =======================================
: wrapy dup 0< if drop l/b> then dup l/b> > if drop 0 then ;
: wrapx dup 0< if drop c/b> then dup c/b> > if drop 0 then ;
: wrap wrapy swap wrapx swap ;
: row c/b * + ; ( a u -- a )
: deceased? wrap row state2 @ ( block ) + c@ on <> ;
: living? deceased? 0= ;
: (-1,-1) 2dup 1- swap 1- swap living? 1 and ;
: (0,-1) >r 2dup 1- living? 1 and r> + ;
: (1,-1) >r 2dup 1- swap 1+ swap living? 1 and r> + ;
: (-1,0) >r 2dup swap 1- swap living? 1 and r> + ;
: (1,0) >r 2dup swap 1+ swap living? 1 and r> + ;
: (-1,1) >r 2dup 1+ swap 1- swap living? 1 and r> + ;
: (0,1) >r 2dup 1+ living? 1 and r> + ;
: (1,1) >r 1+ swap 1+ swap living? 1 and r> + ;
: mates (-1,-1) (0,-1) (1,-1) (-1,0) (1,0) (-1,1) (0,1) (1,1) ;
\ ==== Game of Life: 3/4 =======================================
: born? mates 3 = ;
: survives? 2dup living? -rot mates 2 = and ;
: lives? 2dup born? -rot survives? or ; ( u u -- )
: newstate state1 @ ( block update ) statep ! ; ( -- )
: state! statep @ c! 1 statep +! ; ( c -- )
: alive on state! ; ( -- )
: dead off state! ; ( -- )
: cell? 2dup swap lives? if alive else dead then ; ( u u -- )
: rows 0 begin dup c/b < while cell? 1+ repeat drop ;
: iterate-block 0 begin dup l/b < while rows 1+ repeat drop ;
: generation state2 @ state1 @ state2 ! state1 ! ;
: iterate newstate iterate-block generation ; ( -- )
: done? key [char] q = ; ( -- f )
: prompt cr ." q to quit" cr ; ( -- )
: .line l/b> swap - row c/b type cr ; ( b u -- )
\ ==== Game of Life: 4/4 =======================================
: view state2 @ l/b> for dup r@ .line next drop prompt ;
\ : view ( page ) state2 @ list prompt ; ( -- )
: game begin page view iterate done? until ; ( -- )
: life state2 ! state1 ! game ; ( a a -- )
cr .( Usage: $3000 $32 block life ) cr
; ==== Game of Life: Glider ====================================
***
*
*
; ==== Brain F*ck Compiler Help 1/2 ============================
Brainfuck is a simple esoteric language with only 8 commands, it
is Turing complete which means it can compute any function which
can be computed - although it is very difficult to do so. The
commands operate on a block of memory 1024 bytes long. A data
pointer indexes into the memory. The commands are:
> increment the data pointer
< decrement the data pointer
+ increment value at data pointer
- decrement value at data pointer
. output byte at data pointer
, accept one byte and store it at data pointer
[ branch forward to ] if byte at data pointer is zero
] branch back to [ if byte at data pointer is non-zero
The compiler discards all other input.
; ==== Brain F*ck Compiler Help 2/2 ============================
The following blocks implement the compiler, it translates each
command in a given string into Forth code, compiling it into a
new word definition which can then be executed.
Needless to say, it is not a useful language, but it is fun!
Some example programs are given after the compiler block.
This Forth brain fuck compiler was derived from the one
available Rosetta Code, and so is licensed under the
GNU Free Documentation License [v1.2]. See:
<https://rosettacode.org/wiki/Execute_Brain****/Forth>
And for more information see:
<http://www.hevanet.com/cristofd/brainfuck/short.b>
<https://en.wikipedia.org/wiki/Brainfuck>
\ ==== Brain F*ck Compiler 1/2 =================================
: left over c! 1- dup c@ ;
: right over c! 1+ dup c@ ;
: erase 0 fill ;
: initialize pad b/buf erase pad 0 ;
: bf-compile ( c -- )
>r
r@ [char] [ = if [compile] begin
compile dup [compile] while then
r@ [char] ] = if [compile] repeat then
r@ [char] + = if compile 1+ then
r@ [char] - = if compile 1- then
r@ [char] < = if compile left then
r@ [char] > = if compile right then
r@ [char] , = if compile drop compile key then
r@ [char] . = if compile dup compile emit then rdrop ;
\ ==== Brain F*ck Compiler 2/2 =================================
: bf ( b u --,<string> : compile brainfuck program into a word )
>r >r
[compile] :
compile initialize
r> r> for aft
dup 1+ >r c@ bf-compile r>
then next
drop
compile swap
compile c@
compile 2drop
[compile] ; ;
\ Example, making a simple echo like program called 'cat':
\ : string $" +[,.]" count ; string bf cat
; ==== Brain F*ck Example Programs =============================
From: http://www.hevanet.com/cristofd/brainfuck/short.b
Hello World: ++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]
>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
Clear Screen: ++++++++++[>++++++++++>+<<-]>[>.<-]
Beep: +++++++.
Copy Input to Output: ,[.[-],]
Reverse Input: >,[>,]<[.<]
Translate text to brain fuck that prints said Text:
+++++[>+++++++++<-],[[>--.++>+<<-]>+.->[<.>-]<<,]
; ==== eForth v6.66: Help and Tutorial =========================
This Forth is modeled after eForth, described in a book by
C.H. Ting called "eForth Overview", or "The Zen of eForth". It
implements most of the eForth model with some changes expected
from a more modern Forth system.
The processor targeted is called the H2, a rewrite of the
J1 processor (<http://www.excamera.com/sphinx/fpga-j1.html>).
It has been extended with a few more instructions and with
interrupts. The system resides on an FPGA Nexys 3 board from
Digilent, with words and peripherals designed for it.
For more see: <https://github.com/howerj/forth-cpu>
; ==== Help: Introduction ======================================
Forth is a simple, imperative, stack based language that mixes
elements of a high level language with a low level language. It
is possible to fit a small Forth system in a few kilobytes of
RAM complete with an interactive read-evaluate-print loop, an
editor, an assembler and a disassembler.
It is also a language that has fallen out of favor in recent
times (after the micro-computer era) as it is not a very safe
language, nor is it one that allows the programmer to easily
create reusable and maintainable software components.
However it is still suitable for a number of purposes; for
assemblers, to bring up hardware and test it, in memory
constrained systems, as a boot loader, and for fun.
; ==== Help: Philosophy of Forth ===============================
Forth itself has its own philosophy, one in which the programmer
should have a complete understanding of the system, from the
problem that needs to be solved, the algorithms needed to solve
it, the hardware and the software. As the latter two, the
hardware and software are usually very simple in Forth systems
it is possible to optimize the problem across multiple domains,
tailoring each to come up with a solution. Forth eschews
complexity, preferring one off, bespoke solutions. Another
reason it has not seen widespread use.
Advocates of the language are usually quite passionate about
it (as with Lisp, and other niche systems) so it is important
to be pragmatic about Forth. Forth is simply a tool and when
used correctly can be used productively.
; ==== Help: Basics 1/6 ========================================
A Forth system contains an interactive interpreter that parses
text and either compiles or executes functions, called 'words'
in Forth terminology. The system uses Reverse Polish Notation
(RPN) and provides the user with two stacks and a 'dictionary'
(a big block of memory containing 'words' and other data).
The interpreter reads from an input device, such as a keyboard
or a serial port, and writes output to the screen or back over
the serial port. Words and number input are space delimited and
are either compiled into the dictionary depending on the
interpreter mode and whether the word read in is 'immediate'
or not.
This tutorial will describe how to use Forth and how the
interpreter works internally.
; ==== Help: Basics 2/6 ========================================
To start with, simple expressions can be entered and the
results displayed. A line is evaluated after the carriage
return is entered, code will be indented, this can be typed in.
We will start off with a simple expression, adding two numbers
together and displaying the result:
( Comments appear within brackets )
2 2 + .
This prints out '4' and 'ok'. 'ok' is printed out after every
line has been successfully compiled, unless we are in compile
mode. '+' obviously does the addition, and '.' pops a value
off the stack an prints it. Entering a number pushes it onto
the data stack.
; ==== Help: Basics 3/6 ========================================
The data, or variable, stack is a general purpose stack that
the programmer uses to pass data to functions, and to return
data from functions. The stacks are an important concept
within Forth and stack management will take up a lot of a Forth
programmers time.
Numbers are entered in Reverse Polish Notation, this allows
Forth interpreter to immediate process a word or a function as
it is encountered instead of building up a parse tree.
When '2' is encountered it is pushed onto the variable stack,
when the second '2' is input, it is also pushed onto the stack.
The Forth word '+' takes two arguments off the stack, adds them
together and pushes the result back onto the stack. The word
'.' pops a single number off the stack and prints it.
; ==== Help: Basics 4/6 ========================================
Manipulating the stack will be difficult at first, but gets
easier over time. There are standard words for stack
manipulation and a standard methodology for describing
stack effects called 'stack comments'.
A stack comment is a short comment describing the stack before
and after execution of the word, and the type of the arguments