OpenCores
URL https://opencores.org/ocsvn/forth-cpu/forth-cpu/trunk

Subversion Repositories forth-cpu

[/] [forth-cpu/] [trunk/] [nvram.txt] - Blame information for rev 5

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 howe.r.j.8
\ ==== Boot Block ==============================================
2
-1 loaded !
3
 
4
 
5
 
6
 
7
 
8
 
9
 
10
 
11
 
12
 
13
 
14
 
15
 
16
 
17
; ==== Short Help ==============================================
18
This is a subroutine threaded Forth, based on eForth. The
19
starting base in in hexadecimal. A tutorial, word glossary and
20
extra code is stored in block storage. A short list of commands
21
follows ('#' are numbers):
22
 
23
  words       list all Forth words
24
  see         decompile the next word in the input stream
25
  # #2 index  get descriptive titles for blocks from '#' to '#2'
26
  # list      list block number '#'
27
  # load      execute code in block '#'
28
  # #2 thru   execute code in blocks '#' to '#2'
29
 
30
 
31
 
32
 
33
; ==== Forth Error Messages: 1/4 ===============================
34
  -1 ABORT
35
  -2 ABORT"
36
  -3 stack overflow
37
  -4 stack underflow
38
  -5 return stack overflow
39
  -6 return stack underflow
40
  -7 do-loops nested too deeply during execution
41
  -8 dictionary overflow
42
  -9 invalid memory address
43
 -10 division by zero
44
 -11 result out of range
45
 -12 argument type mismatch
46
 -13 undefined word
47
 -14 interpreting a compile-only word
48
 -15 invalid FORGET
49
; ==== Forth Error Messages: 2/4 ===============================
50
 -16 attempt to use zero-length string as a name
51
 -17 pictured numeric output string overflow
52
 -18 parsed string overflow
53
 -19 definition name too long
54
 -20 write to a read-only location
55
 -21 unsupported operation
56
 -22 control structure mismatch
57
 -23 address alignment exception
58
 -24 invalid numeric argument
59
 -25 return stack imbalance
60
 -26 loop parameters unavailable
61
 -27 invalid recursion
62
 -28 user interrupt
63
 -29 compiler nesting
64
 -30 obsolescent feature
65
; ==== Forth Error Messages: 3/4 ===============================
66
 -31 >BODY used on non-CREATEd definition
67
 -32 invalid name argument (e.g., TO xxx)
68
 -33 block read exception
69
 -34 block write exception
70
 -35 invalid block number
71
 -36 invalid file position
72
 -37 file I/O exception
73
 -38 non-existent file
74
 -39 unexpected end of file
75
 -40 invalid BASE for floating point conversion
76
 -41 loss of precision
77
 -42 floating-point divide by zero
78
 -43 floating-point result out of range
79
 -44 floating-point stack overflow
80
 -45 floating-point stack underflow
81
; ==== Forth Error Messages: 4/4 ===============================
82
 -46 floating-point invalid argument
83
 -47 compilation word list deleted
84
 -48 invalid POSTPONE
85
 -49 search-order overflow
86
 -50 search-order underflow
87
 -51 compilation word list changed
88
 -52 control-flow stack overflow
89
 -53 exception stack overflow
90
 -54 floating-point underflow
91
 -55 floating-point unidentified fault
92
 -56 QUIT
93
 -57 exception in sending or receiving a character
94
 -58 [IF], [ELSE], or [THEN] exception
95
 
96
 
97
; ==== Description of this file ================================
98
The next screens contain extra code that can be loaded into the
99
interpreter if needed. Some of the standard Forth words are
100
placed here instead of in the image to save on space.
101
 
102
The blocks should be loaded in order unless otherwise stated.
103
 
104
This file is first converted by a utility into a fixed width
105
format, which can then be loaded onto the target board with
106
the same utility used to uploaded the bitfile to the FPGA.
107
 
108
Line lengths are limited to 64 characters wide, each block
109
contains 16 lines. The first line should contain a
110
description of the block so that when it is displayed with
111
'index' a glossary can be viewed, allowing quick navigation.
112
 
113
; ==== Scratch Block ===========================================
114
 
115
 
116
 
117
 
118
 
119
 
120
 
121
 
122
 
123
 
124
 
125
 
126
 
127
 
128
 
129
\ ==== Block Editor 1/2 ========================================
130
( Block Editor: There is a help section later on )
131
variable editor-voc 0 editor-voc ! forth
132
: editor decimal editor-voc 1 set-order ;
133
get-order editor-voc swap 1+ set-order
134
$40 constant c/l $10 constant l/b
135
: (block) blk @ block ;
136
: (check) dup b/buf c/l / u>= if -24 throw then ;
137
: (line) (check) c/l * (block) + ;
138
: b block drop ;
139
: l blk @ list ;
140
: n  1 +block b l ;
141
: p -1 +block b l ;
142
: d (line) c/l blank ;
143
: x (block) b/buf blank ;
144
: s update save-buffers ;
145
\ ==== Block Editor 2/2 ========================================
146
: q forth save-buffers ;
147
: e forth blk @ load editor ;
148
: ia c/l * + (block) + source drop >in @ +
149
  swap source nip >in @ - cmove [compile] \ ;
150
: i 0 swap ia ;
151
: u update ;
152
: w words ;
153
: yank pad c/l ;
154
: c (line) yank >r swap r> cmove ;
155
: y (line) yank cmove ;
156
: ct swap y c ;
157
: ea (line) c/l evaluate ;
158
: sw 2dup y (line) swap (line) swap c/l cmove c ;
159
forth
160
 
161
\ ==== Assembler word set ======================================
162
variable assembler-voc
163
bl parse rdrop pad pack$ find drop assembler-voc !
164
: assembler assembler-voc 1 set-order ;
165
: ;code assembler ; immediate
166
: code [compile] : assembler ;
167
get-order assembler-voc swap 1+ set-order
168
: end-code forth [compile] ; ; immediate
169
: words words ;
170
: forth forth ;
171
forth
172
 
173
 
174
 
175
 
176
 
177
\ ==== CORDIC 1/2 ==============================================
178
variable lookup -1 cells allot ( 16 values )
179
$3243 , $1DAC , $0FAD , $07F5 , $03FE , $01FF , $00FF , $007F ,
180
$003F , $001F , $000F , $0007 , $0003 , $0001 , $0000 , $0000 ,
181
 
182
: arshift ( n u -- n : arithmetic right shift )
183
  2dup rshift >r swap $8000 and
184
  if $10 swap - -1 swap lshift else drop 0 then r> or ;
185
 
186
$26DD constant cordic_1K $6487 constant pi/2
187
 
188
variable tx 0 tx ! variable ty 0 ty ! variable tz 0 tz !
189
variable x  0  x ! variable y  0  y ! variable z  0  z !
190
variable d  0  d ! variable k  0  k !
191
 
192
 
193
\ ==== CORDIC 2/2 ==============================================
194
( CORDIC: valid in range -pi/2 to pi/2, arguments are in fixed )
195
( point format with 1 = 16384, angle is given in radians.  )
196
: cordic ( angle -- sine cosine )
197
  z ! cordic_1K x ! 0 y ! 0 k !
198
  $10 begin ?dup while
199
    z @ 0< d !
200
    x @ y @ k @ arshift d @ xor d @ - - tx !
201
    y @ x @ k @ arshift d @ xor d @ - + ty !
202
    z @ k @ cells lookup + @ d @ xor d @ - - tz !
203
    tx @ x ! ty @ y ! tz @ z !
204
    k 1+!
205
    1-
206
  repeat y @ x @ ;
207
: sin cordic drop ;
208
: cos cordic nip ;
209
\ ==== Login Code 1/2 ==========================================
210 5 howe.r.j.8
\ Login and user management system
211 3 howe.r.j.8
: generate count dup >r crc r> ccitt ; ( b -- u )
212
: .user     ." user>" space ; ( -- )
213
: .password ." password>" space ; ( -- )
214
variable user0 0 user0 !
215
: mk.user ( --; ,  )
216
  here user0 @ , user0 ! here 0 , bl word count 1+ allot align
217
  drop bl word generate swap ! ;
218
: ls.user ( -- : list all users in user database )
219
  cr user0 @
220
  begin dup while dup 2 cells + space count type cr @ repeat
221
  drop cr ;
222
: find.user ( a -- u | 0 : find user in database, return hash )
223
  >r user0 @ begin dup while dup 2 cells + count r@ count
224
  =string if rdrop exit then @ repeat rdrop drop 0 ;
225
\ ==== Login Code 2/2 ==========================================
226
: (password) ( u --,  )
227
  >r begin .password query bl word cr generate
228
  r@ = until rdrop ;
229
: fake .password query bl word drop cr ;
230
: (user)
231
  begin .user query bl word cr find.user ?dup until ;
232
: retry ( xt -- : retry word until it succeeds )
233
  >r begin r@ catch 0= until rdrop ;
234
: user?     ' (user)     retry ;
235
: password? ' (password) retry ;
236
: hide-all 0 1 set-order ;
237
: login hide-all
238
  cr user? cell+ @ conceal password? interactive forth ;
239
mk.user guest guest     mk.user archer dangerzone
240
mk.user lana  sterling  mk.user cyril  figgis
241
\ ==== Extra Code 1/7 ==========================================
242
: 2+ 2 + ;                       ( n -- n )
243
: 2- 2 - ;                       ( n -- n )
244
: >= < invert ;                  ( n n -- f )
245 5 howe.r.j.8
: simulation? cpu-id $0666 <> ; ( -- f : are we in the matrix? )
246 3 howe.r.j.8
: 0<= 0> 0= ;                    ( n n -- f )
247
: 0>= 0< 0= ;                    ( n n -- f )
248
: not -1 xor ;                   ( n -- n )
249
: dabs dup 0< if dnegate then ;  ( d -- d )
250
: d+  >r swap >r um+ r> r> + + ; ( d d -- d )
251
: d=  >r swap r> = >r = r> and ; ( d d -- f )
252
: d<> d= 0= ;                    ( d d -- f )
253
: roll  dup 0> if swap >r 1- recurse r> swap else drop then ;
254
: ?exit if rdrop then ;          ( n --, R: n -- n | )
255
: 2rdrop r> rdrop rdrop >r ;     ( R n n -- )
256
: 2. swap . . ;                  ( n n -- )
257
\ ==== Extra Code 2/7 ==========================================
258
: m* 2dup xor 0< >r abs swap abs um* r> if dnegate then ;
259
: */mod  >r m* r> m/mod ;  ( n n n -- r q )
260
: */  */mod nip ;          ( n n n -- q )
261
: s>d dup 0< ;             ( n -- d : single to double )
262
: holds begin dup while 1- 2dup + c@ hold repeat 2drop ;
263
: binary  2 base ! ;                       ( -- )
264
: octal  8 base ! ;                        ( -- )
265
: .base base @ dup decimal base ! ; ( -- )
266
: only -1 set-order ;
267
: also get-order over swap 1+ set-order ;
268
: previous get-order swap drop 1- set-order ;
269
: buffer block ; ( k -- a )
270
: bye [ 0 , ] ;
271
: enum dup constant 1+ ; ( n --,  )
272
: logical 0= 0= ;     ( n -- f )
273
\ ==== Extra Code 3/7 ==========================================
274
: square dup * ;      ( n -- )
275
: limit rot min max ; ( n lo hi -- n )
276
: odd 1 and logical ; ( n -- )
277
: even odd invert ;   ( n -- )
278
: nor or invert ;     ( u u -- u )
279
: nand and invert ;   ( u u -- u )
280
: bell 7 emit ;       ( -- )
281
: under >r dup r> ;   ( n1 n2 -- n1 n1 n2 )
282
: 2nip >r >r 2drop r> r> ; ( n1 n2 n3 n4 -- n3 n4 )
283
( n1 n2 n3 n4 -- n1 n2 n3 n4 n1 n2 )
284
: 2over >r >r 2dup r> swap >r swap r> r> -rot ;
285
: 2swap >r -rot r> -rot ; ( n1 n2 n3 n4 -- n3 n4 n1 n2 )
286
: 2tuck 2swap 2over ; ( n1 n2 n3 n4 -- n3 n4 n1 n2 n3 n4 )
287
: 4drop 2drop 2drop ; ( n1 n2 n3 n4 -- )
288
: trip dup dup ; ( n -- n n n )
289
\ ==== Extra Code 4/7 ==========================================
290
: log  >r 0 swap ( u base -- u )
291
  begin swap 1+ swap r@ / dup 0= until
292
  drop 1- rdrop ;
293
: log2 0 swap ( u -- u )
294
  begin swap 1+ swap   2/ dup 0= until
295
  drop 1- ;
296
: average um+ 2 um/mod nip ; ( u u -- u )
297
: <=> 2dup > if 2drop -1 exit then < ;
298
: bounds over + swap ;
299
: 2, , , ; ( n n -- )
300
: tab 9 emit ; ( -- )
301
: drup drop dup ; ( n1 n2 -- n1 n1 )
302 5 howe.r.j.8
: lsb $FF and ; ( u -- u )
303 3 howe.r.j.8
: --> 1 +block load ;
304
: scr blk ;
305
\ ==== Extra Code 5/7 ==========================================
306
: signum ( n -- -1 | 0 | 1 : Signum function )
307
  dup 0> if drop  1 exit then
308
      0< if      -1 exit then
309
 
310
: >< dup 8 rshift swap 8 lshift or ; ( u -- u : swap bytes )
311
: #digits dup 0= if 1+ exit then base @ log 1+ ;
312
: ** ( n u -- n )
313
  ?dup if
314
    over >r
315
    begin
316
      dup 1 >
317
    while
318
      swap r@ * swap 1-
319
    repeat rdrop drop
320
  else logical 1 and then ;
321
\ ==== Extra Code 6/7 ==========================================
322
: b. base @ swap 2 base ! u. base ! ; ( u -- )
323
: h. base @ swap hex u. base ! ;      ( u -- )
324
: o. base @ swap 8 base ! u. base ! ; ( u -- )
325
: d. base @ swap decimal . base ! ;   ( n -- )
326
: @bits swap @ and ;                  ( a u -- u )
327
: ?\ if [compile] \ then ; immediate
328
: ?( if [compile] ( then ; immediate ( )
329
: defined? ( -- pwd -1 | 0,  : is word defined? )
330
  bl word find if -1 else drop 0 then ;
331
: ?if compile dup [compile] if ; immediate
332
: ?dup-if compile ?dup [compile] if ; immediate
333
: >body ( dup @ $4000 or <> if 31 -throw then ) cell+ ;
334
 
335
 
336
 
337
\ ==== Extra Code 7/7 ==========================================
338
: screens ( k1 k2 -- : list blocks k1 to k2 )
339
  over -
340
  for
341 5 howe.r.j.8
    dup . dup list 1+ key $D = if rdrop drop exit then
342 3 howe.r.j.8
  next drop ;
343
 
344
 
345
 
346
 
347
 
348
 
349
 
350
 
351
 
352
 
353
\ ==== Roulette ================================================
354
\ Russian Roulette: It would be interesting to do something
355
\ killed the computer when the player dies
356
 
357
: click ."  *click*" cr ;
358
: bang  ."  BANG!" cr ;
359
: roulette random 6 mod if click else bang then ; ( -- )
360
 
361
 
362
 
363
 
364
 
365
 
366
 
367
 
368
 
369
\ ==== Extended ANSI Escape Codes ==============================
370
 
371
red green        + constant yellow
372
    green blue   + constant cyan
373
red       blue   + constant magenta
374
red green blue + + constant white
375 5 howe.r.j.8
: background $A + ;
376
: color $1E + sgr ;
377 3 howe.r.j.8
\ : hide-cursor CSI [char] ? emit $19 10u. [char] l emit ;
378
\ : show-cursor CSI [char] ? emit $19 10u. [char] h emit ;
379
: up    [char] A ansi ; ( n -- )
380
: down  [char] B ansi ; ( n -- )
381
: left  [char] C ansi ; ( n -- )
382
: right [char] D ansi ; ( n -- )
383
 
384
 
385
\ ==== Screen Saver ============================================
386
\ An incredibly simple screen saver using ANSI Escape codes
387
\ for placement and coloring of random characters
388 5 howe.r.j.8
base @ decimal
389 3 howe.r.j.8
: screen-saver ( -- )
390
  page
391
  begin
392 5 howe.r.j.8
    random 79 mod
393
    random 38 mod at-xy
394 3 howe.r.j.8
    random >char emit
395
    random 8  mod
396 5 howe.r.j.8
    random 1 and if background then
397
    random 256 and ms color
398 3 howe.r.j.8
  again ;
399 5 howe.r.j.8
base !
400 3 howe.r.j.8
 
401
; ==== Game: YOU ARE DEAD (HELP) ===============================
402
This is a clone of the one dimensional rogue like game
403
available at . The
404
object is to get to the other side of the screen.
405
 
406
Keys:
407
w  Turn into '.'
408
a  Turn into '~'
409
s  Turn into '>'
410
d  Move right
411
q  Quit
412
 
413
Block number 7 is used to store the game state.
414
 
415
 
416
 
417
\ ==== Game: YOU ARE DEAD 1/6 ==================================
418
forth variable yad-voc get-order yad-voc swap 1+ set-order
419
$40 constant c/l $10 constant l/b
420
: memory 7 block ;
421
: variables memory c/l + ;
422
: score    variables 0 cells + ;
423
: position variables 1 cells + ;
424
: level    variables 2 cells + ;
425
: form     variables 3 cells + ;
426
: continue variables 4 cells + ;
427
: end c/l 1- ;
428
: player form @ ;
429
: .player position @ 1+ 3 at-xy player emit ;
430
: .goal c/l 3 at-xy [char] # emit ;
431
: .score ." SCORE: " score @ 5 u.r ;
432
: .level ." LEVEL: " level @ 5 u.r ;
433
\ ==== Game: YOU ARE DEAD 2/6 ==================================
434
: show
435
  page cr cr space memory c/l type
436
  .player .goal
437
  cr .score space .level cr ;
438
 
439
: select ( n -- c )
440
  dup 0=  if drop [char] <  exit then
441
  dup 1 = if drop [char] ~  exit then
442
  dup 2 = if drop [char] .  exit then
443
  drop bl ;
444
 
445
: die ." YOU ARE DEAD" cr 0 continue ! -56 throw ;
446
: survived ." YOU SURVIVED" cr .score cr ;
447
: forms form c@ position @ memory + c@ ; ( -- c c )
448
 
449
\ ==== Game: YOU ARE DEAD 3/6 ==================================
450
: generate ( -- generate a level )
451
  c/l 1- for
452
    random 5 mod ( 3 = most difficult )
453
    select memory r@ + c!
454
  next
455
  bl memory c! bl memory end + c! ;
456
: normal [char] x ;
457
: setup ( -- )
458
  -1 continue !
459
  memory b/buf 0 fill
460
  normal form c!
461
  generate ;
462
 
463
: +score random 4 mod 1 min score +! ; ( -- )
464
: ?next <> if die else +score then ; ( c c -- )
465
\ ==== Game: YOU ARE DEAD 4/6 ==================================
466
: +level
467
 
468
  random 23 mod 5 min score +!
469
  generate ;
470
 
471
: monster swap >r forms r> = if ?next else 2drop then ;
472
 
473
: command ( -- f )
474
  dup [char] w = if drop [char] . form c! 0 exit then
475
  dup [char] a = if drop [char] ~ form c! 0 exit then
476
  dup [char] d = if drop -1 exit then
477
  dup [char] s = if drop [char] < form c! 0 exit then
478
  dup [char] q = if drop -56 throw then
479
  drop 0 ;
480
 
481
\ ==== Game: YOU ARE DEAD 5/6 ==================================
482
: rules
483
  position @ end = if +level exit then
484
  forms = if exit then
485
  [char] ~ [char] < monster
486
  [char] . [char] ~ monster
487
  [char] < [char] . monster
488
  normal form c!
489
  bl position @ memory + c!
490
  score 1+!
491
  position 1+! ;
492
 
493
 
494
 
495
 
496
 
497
\ ==== Game: YOU ARE DEAD 6/6 ==================================
498
: game
499
  begin
500
  show
501
  key command if rules then
502
  level @ 9 >
503
  until survived ;
504
 
505
 
506
: play setup ' game catch drop ;
507
get-order -rot swap rot set-order
508
: you-are-dead base @ decimal play base ! ;
509
forth
510
\ : resume memory drop ' game catch drop ;
511
 
512
 
513 5 howe.r.j.8
; ==== Sokoban: Instructions ===================================
514
To run, load the next blocks 9 blocks, which contain the code
515
for the Sokoban game. The object of the game is to maneuver the
516
boulders (*) onto the pads (.) with the player character (@/~),
517
until all the bolders are on the pads. Sokoban levels are
518
stored one per block. To play:
519
 
520
  $2A sokoban
521
 
522
Would load a game stored in block '$2A' and begin playing.
523
The WASD keys move the player around, 'h' is for help, and
524
'q' quits.
525
 
526
 
527
 
528
 
529
\ ==== Sokoban 1/9 =============================================
530
\ only forth definitions hex
531
\ variable sokoban-wordlist sokoban-wordlist +order definitions
532
blk @ 1- constant help-block
533
char X constant wall
534
char * constant boulder
535
char . constant off
536
char & constant on
537
char @ constant player
538
char ~ constant player+ ( player + off pad )
539
$10    constant l/b     ( lines   per block )
540
$40    constant c/b     ( columns per block )
541
     7 constant bell    ( bell character )
542
variable position  ( current player position )
543
variable moves     ( moves made by player )
544
create rule 3 c, 0 c, 0 c, 0 c,
545
\ ==== Sokoban 2/9 =============================================
546
: n1+ swap 1+ swap ; ( n n -- n n )
547
: match              ( a a -- f )
548
  n1+ ( replace with umin of both counts? )
549
  count
550
  for aft
551
    count rot count rot <> if 2drop rdrop 0 exit then
552
  then next 2drop -1 ;
553
 
554
: beep bell emit ; ( -- )
555
: ?apply           ( a a a -- a, R: ? -- ?| )
556
  >r over swap match if drop r> rdrop exit then rdrop ;
557
 
558
 
559
 
560
 
561
\ ==== Sokoban 3/9 =============================================
562
: apply ( a -- a )
563
 $" @ "  $"  @"  ?apply
564
 $" @."  $"  ~"  ?apply
565
 $" @* " $"  @*" ?apply
566
 $" @*." $"  @&" ?apply
567
 $" @&." $"  ~&" ?apply
568
 $" @& " $"  ~*" ?apply
569
 $" ~ "  $" .@"  ?apply
570
 $" ~."  $" .~"  ?apply
571
 $" ~* " $" .@*" ?apply
572
 $" ~*." $" .@&" ?apply
573
 $" ~&." $" .~&" ?apply
574
 $" ~& " $" .~*" ?apply beep ;
575
 
576
 
577
\ ==== Sokoban 4/9 =============================================
578
: pack ( c0...cn b n -- )
579
  2dup swap c! for aft 1+ tuck c! then next drop ;
580
 
581
: locate ( b u c -- u f )
582
  >r
583
  begin
584
    ?dup
585
  while
586
    1- 2dup + c@ r@ = if nip rdrop -1 exit then
587
  repeat
588
  rdrop
589
  drop
590
 
591
 
592
 
593
\ ==== Sokoban 5/9 =============================================
594
: 2* 1 lshift ; ( u -- )
595
: relative swap c/b * + + ( $3FF and ) ; ( +x +y pos -- pos )
596
: +position position @ relative ; ( +x +y -- pos )
597
: double 2* swap 2* swap ;  ( u u -- u u )
598
: arena blk @ block b/buf ; ( -- b u )
599
: >arena arena drop + ;     ( pos -- a )
600
: fetch                     ( +x +y -- a a a )
601
  2dup   +position >arena >r
602
  double +position >arena r> swap
603
  position @ >arena -rot ;
604
: rule@ fetch c@ rot c@ rot c@ rot ; ( +x +y -- c c c )
605
: 3reverse -rot swap ;               ( 1 2 3 -- 3 2 1 )
606
: rule! rule@ 3reverse rule 3 pack ; ( +x +y -- )
607
: think 2dup rule! rule apply >r fetch r> ; ( +x +y --a a a a )
608
: count! count rot c! ;              ( a a -- )
609
\ ==== Sokoban 6/9 =============================================
610
\ 'act' could be made to be more elegant, but it works, it
611
\ handles rules of length 2 and length 3
612
: act ( a a a a -- )
613
  count swap >r 2 =
614
  if
615
     drop swap r> count! count!
616
  else
617
     3reverse r> count! count! count!
618
  then drop ;
619
: #boulders ( -- n )
620
 
621
   for aft
622
     dup c@ boulder = if n1+ then
623
     1+
624
   then next drop ;
625
\ ==== Sokoban 7/9 =============================================
626
: .boulders  ." BOULDERS: " #boulders u. cr ; ( -- )
627
: .moves    ." MOVES: " moves    @ u. cr ; ( -- )
628
: .help     ." WASD - MOVEMENT" cr ." H    - HELP" cr ; ( -- )
629
: .maze blk @ list ;                  ( -- )
630
: show ( page cr ) .maze .boulders .moves .help ; ( -- )
631
: solved? #boulders 0= ;               ( -- )
632
: finished? solved? if 1 throw then ; ( -- )
633
: instructions blk @ help-block list block drop key drop ;
634
: where >r arena r> locate ;          ( c -- u f )
635
: player? player where 0= if drop player+ where else -1 then ;
636
: player! player? 0= throw position ! ; ( -- )
637
: start player! 0 moves ! ;           ( -- )
638
: .winner show cr ." SOLVED!" cr ;    ( -- )
639
: .quit cr ." Quitter!" cr ;          ( -- )
640
: finish 1 = if .winner exit then .quit ; ( n -- )
641
\ ==== Sokoban 8/9 =============================================
642
: rules think act player! ;           ( +x +y -- )
643
: +move 1 moves +! ;                  ( -- )
644
: ?ignore over <> if rdrop then ;     ( c1 c2 --, R: x -- | x )
645
: left  [char] a ?ignore -1  0 rules +move ; ( c -- c )
646
: right [char] d ?ignore  1  0 rules +move ; ( c -- c )
647
: up    [char] w ?ignore  0 -1 rules +move ; ( c -- c )
648
: down  [char] s ?ignore  0  1 rules +move ; ( c -- c )
649
: help  [char] h ?ignore instructions ; ( c -- c )
650
: end   [char] q ?ignore 2 throw ; ( c -- | c, R ? -- | ? )
651
: default drop ;  ( c -- )
652
: command up down left right help end default finished? ;
653
: maze! block drop ; ( k -- )
654
: input key ;        ( -- c )
655
 
656
 
657
\ ==== Sokoban 9/9 =============================================
658
\ sokoban-wordlist -order definitions
659
\ sokoban-wordlist +order
660
 
661
: sokoban ( k -- )
662
  maze! start
663
  begin
664
    show input ' command catch ?dup
665
  until finish ;
666
 
667
 
668
 
669
 
670
 
671
 
672
 
673
; ==== Sokoban Map 1 ===========================================
674
 
675
                    XXXXX
676
                    X   X
677
                    X*  X
678
                  XXX  *XXX
679
                  X  *  * X
680
                XXX X XXX X     XXXXXX
681
                X   X XXX XXXXXXX  ..X
682
                X *  *             ..X
683
                XXXXX XXXX X@XXXX  ..X
684
                    X      XXX  XXXXXX
685
                    XXXXXXXX
686
 
687
 
688
 
689
; ==== Sokoban Map 2 ===========================================
690
 
691
               XXXXXXXXXXXX
692
               X..  X     XXX
693
               X..  X *  *  X
694
               X..  X*XXXX  X
695
               X..    @ XX  X
696
               X..  X X  * XX
697
               XXXXXX XX* * X
698
                 X *  * * * X
699
                 X    X     X
700
                 XXXXXXXXXXXX
701
 
702
 
703
 
704
 
705
; ==== Sokoban Map 3 ===========================================
706
 
707
                       XXXXXXXX
708
                       X     @X
709
                       X *X* XX
710
                       X *  *X
711
                       XX* * X
712
               XXXXXXXXX * X XXX
713
               X....  XX *  *  X
714
               XX...    *  *   X
715
               X....  XXXXXXXXXX
716
               XXXXXXXX
717
 
718
 
719
 
720
 
721
; ==== Sokoban Map 4 ===========================================
722
 
723
                             XXXXXXXX
724
                             X  ....X
725
                  XXXXXXXXXXXX  ....X
726
                  X    X  * *   ....X
727
                  X ***X*  * X  ....X
728
                  X  *     * X  ....X
729
                  X ** X* * *XXXXXXXX
730
               XXXX  * X     X
731
               X   X XXXXXXXXX
732
               X    *  XX
733
               X **X** @X
734
               X   X   XX
735
               XXXXXXXXX
736
 
737
\ ==== Game of Life: 1/4 =======================================
738
\ This is a Game Of Life implementation, originally from
739
\ . It has been adapted so
740
\ instead of blocks it uses 1KiB buffers at arbitrary memory
741
\ locations.
742
   $40 constant c/b
743
   $10 constant l/b
744
c/b 1- constant c/b>
745
l/b 1- constant l/b>
746
    bl constant off
747
char * constant on
748
variable state1
749
variable state2
750
variable statep
751
 
752
 
753
\ ==== Game of Life: 2/4 =======================================
754
: wrapy dup 0< if drop l/b> then dup l/b> > if drop 0 then ;
755
: wrapx dup 0< if drop c/b> then dup c/b> > if drop 0 then ;
756
: wrap  wrapy swap wrapx swap ;
757
: row c/b * + ; ( a u -- a )
758
: deceased? wrap row state2 @ ( block ) + c@ on <> ;
759
: living?  deceased? 0= ;
760
: (-1,-1) 2dup 1- swap 1- swap living? 1 and ;
761
: (0,-1)  >r 2dup 1- living? 1 and r> + ;
762
: (1,-1)  >r 2dup 1- swap 1+ swap living? 1 and r> + ;
763
: (-1,0)  >r 2dup swap 1- swap living? 1 and r> + ;
764
: (1,0)   >r 2dup swap 1+ swap living? 1 and r> + ;
765
: (-1,1)  >r 2dup 1+ swap 1- swap living? 1 and r> + ;
766
: (0,1)   >r 2dup 1+ living? 1 and r> + ;
767
: (1,1)   >r 1+ swap 1+ swap living? 1 and r> + ;
768
: mates (-1,-1) (0,-1) (1,-1) (-1,0) (1,0) (-1,1) (0,1) (1,1) ;
769
\ ==== Game of Life: 3/4 =======================================
770
: born?  mates 3 = ;
771
: survives?  2dup living? -rot mates 2 = and ;
772
: lives?  2dup born? -rot survives? or ;        ( u u -- )
773
: newstate  state1 @ ( block update ) statep ! ; ( -- )
774
: state!  statep @ c! 1 statep +! ;             ( c -- )
775
: alive  on state! ;                            ( -- )
776
: dead  off state! ;                            ( -- )
777
: cell?  2dup swap lives? if alive else dead then ; ( u u -- )
778
: rows   0 begin dup c/b < while cell? 1+ repeat drop ;
779
: iterate-block 0 begin dup l/b < while rows 1+ repeat drop ;
780
: generation  state2 @ state1 @ state2 ! state1 ! ;
781
: iterate  newstate iterate-block generation ; ( -- )
782
: done?  key [char] q = ;                      ( -- f )
783
: prompt  cr ." q to quit" cr ;                ( -- )
784
: .line l/b> swap - row c/b type cr ;          ( b u -- )
785
\ ==== Game of Life: 4/4 =======================================
786
: view state2 @ l/b> for dup r@ .line next drop prompt ;
787
\ : view (  page ) state2 @ list prompt ;      ( -- )
788
: game  begin page view iterate done? until ;  ( -- )
789
: life state2 ! state1 ! game ;                ( a a -- )
790
 
791
cr .( Usage: $3000 $32 block life ) cr
792
 
793
 
794
 
795
 
796
 
797
 
798
 
799
 
800
 
801
; ==== Game of Life: Glider ====================================
802
 
803
 
804
 
805
 
806
 
807
              ***
808
              *
809
               *
810
 
811
 
812
 
813
 
814
 
815
 
816
 
817 3 howe.r.j.8
; ==== Brain F*ck Compiler Help 1/2 ============================
818
Brainfuck is a simple esoteric language with only 8 commands, it
819
is Turing complete which means it can compute any function which
820
can be computed - although it is very difficult to do so. The
821
commands operate on a block of memory 1024 bytes long. A data
822
pointer indexes into the memory. The commands are:
823
 
824
  >  increment the data pointer
825
  <  decrement the data pointer
826
  +  increment value at data pointer
827
  -  decrement value at data pointer
828
  .  output byte at data pointer
829
  ,  accept one byte and store it at data pointer
830
  [  branch forward to ] if byte at data pointer is zero
831
  ]  branch back    to [ if byte at data pointer is non-zero
832
The compiler discards all other input.
833
; ==== Brain F*ck Compiler Help 2/2 ============================
834
The following blocks implement the compiler, it translates each
835
command in a given string into Forth code, compiling it into a
836
new word definition which can then be executed.
837
 
838
Needless to say, it is not a useful language, but it is fun!
839
Some example programs are given after the compiler block.
840
 
841
This Forth brain fuck compiler was derived from the one
842
available Rosetta Code, and so is licensed under the
843
GNU Free Documentation License [v1.2]. See:
844
845
 
846
And for more information see:
847
 
848
 
849
\ ==== Brain F*ck Compiler 1/2 =================================
850
: left over c!  1- dup c@ ;
851
: right over c! 1+ dup c@ ;
852
: erase 0 fill ;
853
: initialize pad b/buf erase pad 0 ;
854
: bf-compile ( c -- )
855
  >r
856
  r@ [char] [ = if [compile] begin
857
                   compile dup [compile] while then
858
  r@ [char] ] = if [compile] repeat            then
859
  r@ [char] + = if compile 1+                  then
860
  r@ [char] - = if compile 1-                  then
861
  r@ [char] < = if compile left                then
862
  r@ [char] > = if compile right               then
863
  r@ [char] , = if compile drop compile key    then
864
  r@ [char] . = if compile dup  compile emit   then rdrop ;
865
\ ==== Brain F*ck Compiler 2/2 =================================
866
: bf ( b u --, : compile brainfuck program into a word )
867
  >r >r
868
  [compile] :
869
  compile initialize
870
  r> r> for aft
871
    dup 1+ >r c@ bf-compile r>
872
  then next
873
  drop
874
  compile swap
875
  compile c@
876
  compile 2drop
877
  [compile] ; ;
878
 
879
\ Example, making a simple echo like program called 'cat':
880
\ : string $" +[,.]" count ; string bf cat
881
; ==== Brain F*ck Example Programs =============================
882
From: http://www.hevanet.com/cristofd/brainfuck/short.b
883
Hello World: ++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]
884
>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
885
Clear Screen: ++++++++++[>++++++++++>+<<-]>[>.<-]
886
Beep: +++++++.
887
Copy Input to Output: ,[.[-],]
888
Reverse Input: >,[>,]<[.<]
889
Translate text to brain fuck that prints said Text:
890
+++++[>+++++++++<-],[[>--.++>+<<-]>+.->[<.>-]<<,]
891
 
892
 
893
 
894
 
895
 
896
 
897
; ==== eForth v6.66: Help and Tutorial =========================
898
 
899
This Forth is modeled after eForth, described in a book by
900
C.H. Ting called "eForth Overview", or "The Zen of eForth". It
901
implements most of the eForth model with some changes expected
902
from a more modern Forth system.
903
 
904
The processor targeted is called the H2, a rewrite of the
905
J1 processor ().
906
It has been extended with a few more instructions and with
907
interrupts. The system resides on an FPGA Nexys 3 board from
908
Digilent, with words and peripherals designed for it.
909
 
910
For more see: 
911
 
912
 
913
; ==== Help: Introduction ======================================
914
 
915
Forth is a simple, imperative, stack based language that mixes
916
elements of a high level language with a low level language. It
917
is possible to fit a small Forth system in a few kilobytes of
918
RAM complete with an interactive read-evaluate-print loop, an
919
editor, an assembler and a disassembler.
920
 
921
It is also a language that has fallen out of favor in recent
922
times (after the micro-computer era) as it is not a very safe
923
language, nor is it one that allows the programmer to easily
924
create reusable and maintainable software components.
925
 
926
However it is still suitable for a number of purposes; for
927
assemblers, to bring up hardware and test it, in memory
928
constrained systems, as a boot loader, and for fun.
929
; ==== Help: Philosophy of Forth ===============================
930
Forth itself has its own philosophy, one in which the programmer
931
should have a complete understanding of the system, from the
932
problem that needs to be solved, the algorithms needed to solve
933
it, the hardware and the software. As the latter two, the
934
hardware and software are usually very simple in Forth systems
935
it is possible to optimize the problem across multiple domains,
936
tailoring each to come up with a solution. Forth eschews
937
complexity, preferring one off, bespoke solutions. Another
938
reason it has not seen widespread use.
939
 
940
Advocates of the language are usually quite passionate about
941
it (as with Lisp, and other niche systems) so it is important
942
to be pragmatic about Forth. Forth is simply a tool and when
943
used correctly can be used productively.
944
 
945
; ==== Help: Basics 1/6 ========================================
946
A Forth system contains an interactive interpreter that parses
947
text and either compiles or executes functions, called 'words'
948
in Forth terminology. The system uses Reverse Polish Notation
949
(RPN) and provides the user with two stacks and a 'dictionary'
950
(a big block of memory containing 'words' and other data).
951
 
952
The interpreter reads from an input device, such as a keyboard
953
or a serial port, and writes output to the screen or back over
954
the serial port. Words and number input are space delimited and
955
are either compiled into the dictionary depending on the
956
interpreter mode and whether the word read in is 'immediate'
957
or not.
958
 
959
This tutorial will describe how to use Forth and how the
960
interpreter works internally.
961
; ==== Help: Basics 2/6 ========================================
962
To start with, simple expressions can be entered and the
963
results displayed. A line is evaluated after the carriage
964
return is entered, code will be indented, this can be typed in.
965
 
966
We will start off with a simple expression, adding two numbers
967
together and displaying the result:
968
 
969
  ( Comments appear within brackets )
970
  2 2 + .
971
 
972
This prints out '4' and 'ok'. 'ok' is printed out after every
973
line has been successfully compiled, unless we are in compile
974
mode. '+' obviously does the addition, and '.' pops a value
975
off the stack an prints it. Entering a number pushes it onto
976
the data stack.
977
; ==== Help: Basics 3/6 ========================================
978
The data, or variable, stack is a general purpose stack that
979
the programmer uses to pass data to functions, and to return
980
data from functions. The stacks are an important concept
981
within Forth and stack management will take up a lot of a Forth
982
programmers time.
983
 
984
Numbers are entered in Reverse Polish Notation, this allows
985
Forth interpreter to immediate process a word or a function as
986
it is encountered instead of building up a parse tree.
987
 
988
When '2' is encountered it is pushed onto the variable stack,
989
when the second '2' is input, it is also pushed onto the stack.
990
The Forth word '+' takes two arguments off the stack, adds them
991
together and pushes the result back onto the stack. The word
992
'.' pops a single number off the stack and prints it.
993
; ==== Help: Basics 4/6 ========================================
994
Manipulating the stack will be difficult at first, but gets
995
easier over time. There are standard words for stack
996
manipulation and a standard methodology for describing
997
stack effects called 'stack comments'.
998
 
999
A stack comment is a short comment describing the stack before
1000
and after execution of the word, and the type of the arguments
1001
it accepts and returns. For example the stack comment for '+'
1002
is:
1003
 
1004
  + ( n n -- n : add two numbers together )
1005
 
1006
All text between '(' and the ')' is discarded by the Forth
1007
system. This comment describes a word that accepts two
1008
signed numbers "n n", and returns a signed number "n".
1009
; ==== Help: Basics 5/6 ========================================
1010
The "--" divides the comment into what the stack looks like
1011
before execution of the word (to the left of "--") and what it
1012
looks like after (to the right of "--"). A comment on the
1013
behavior of the word comes after the semicolon.
1014
 
1015
Here is a list of some common Forth words and their stack
1016
comments:
1017
 
1018
  dup  ( n -- n n : duplicate first item on the stack )
1019
  swap ( n1 n2 -- n2 n1 : swap first two items on stack )
1020
  drop ( n -- : drop a value from the stack )
1021
 
1022
Stack effects are sometimes numbered, which is used to
1023
so the effects on specific arguments can be documented if
1024
the order of arguments and return values matter.
1025
; ==== Help: Basics 6/6 ========================================
1026
Some more words with comments:
1027
 
1028
  @    ( a -- u : load value from memory address )
1029
  !    ( u a -- : store 'u' at memory location 'a' )
1030
  <    ( n1 n2 -- f : is n1 greater than n2, signed )
1031
  u<   ( u1 u2 -- f : is u1 greater than n2, unsigned )
1032
 
1033
These words have stack comments, some with numbered arguments,
1034
but they use different letters. The different letters are used
1035
to describe the type of the arguments that the word accepts.
1036
 
1037
'@', also known as load, takes an address, and '!', takes
1038
an address and a value. "<" takes two signed numbers, "u<"
1039
takes two unsigned numbers and produces a flag. Type checking
1040
is not performed by Forth and is up to the programmer.
1041
; ==== Help: Stack Comments ====================================
1042
Here is a list of stack type descriptions and what they mean:
1043
| Comment  | Meaning                             |
1044
|----------|-------------------------------------|
1045
| a        | cell address                        |
1046
| n        | signed number                       |
1047
| u        | unsigned number                     |
1048
| b        | string address                      |
1049
| c        | single character                    |
1050
| d        | double width number (2 Cells)       |
1051
| f        | boolean flag (-1 = true, 0 = false) |
1052
| k        | block number                        |
1053
| cfa      | code field address of a word        |
1054
| nfa      | name field address of a word        |
1055
| pwd      | previous word address of a word     |
1056
|  | a parsing word                      |
1057
; ==== Help: Expressions =======================================
1058
Let us continue on with some interactive examples before we
1059
start to define new words. Before we saw a trivial example
1060
of adding two numbers together, we will go over a few more
1061
operators first.
1062
 
1063
  9 2 + . ( Displays 'B', the default base is hexadecimal )
1064
  decimal ( Change the input and output base to decimal )
1065
  9 2 + . ( Displays '11' )
1066
  3 4 dup * swap dup * + . ( Displays 3^2 + 4^2 or 25 )
1067
 
1068
Negative numbers can be input by prefixing the number with
1069
'-':
1070
 
1071
  -2 4 * . ( Display -8 )
1072
 
1073
; ==== Help: Numeric Output 1/2 ================================
1074
The word '.' pops a value off the stack before displaying it,
1075
if we want to examine the stack without popping the value we
1076
can print out the entire contents of the stack with '.s':
1077
 
1078
  1 2 3 .s ( prints "1 2 3 
1079
  .        ( prints "3" )
1080
  .s       ( prints "1 2 
1081
 
1082
"
1083
stack. Other useful words for debugging include '?' that
1084
prints the contents at a memory address and 'dump', which
1085
prints a memory dump of a region of memory.
1086
 
1087
  dump ( a u -- )
1088
  ?    ( a -- )
1089
; ==== Help: Numeric Output 2/2 ================================
1090
Hexadecimal numbers can be entered if the base input and output
1091
base is 16, or by prefixing the number with '$'.
1092
 
1093 5 howe.r.j.8
  decimal  $AAA . ( Displays 2730 )
1094
          -$AAA . ( Displays -2730 )
1095 3 howe.r.j.8
 
1096
eForth starts up in base 16, valid bases range anywhere from
1097
2 to 36. The base can be changed by either setting a variable
1098
'base' to the desired base, or with the words 'hex' to change
1099
the base back into hexadecimal, or 'decimal' to change the base
1100
to '10'. Variables in Forth are words that leave an address on
1101
the stack when they are called. They can be read or set with
1102
'@' and '!'. For example, "$10 base !" and "hex" are
1103 5 howe.r.j.8
equivalent, as are "$A base !" and "decimal".
1104 3 howe.r.j.8
 
1105
; ==== Help: Word Definitions 1/2 ==============================
1106
For the moment we have not covered how words are defined and
1107
is a good time to do so, the word ":" is used to create a new
1108
word definition and the word ";" is used to terminate one.
1109
Words once defined are added to the 'dictionary', which is
1110
consists of a linked list of words, hence the name
1111
'dictionary'. We can define new words interactively like we can
1112
type expressions in, such as:
1113
 
1114
  : square dup * ; ( n -- n : square a number )
1115
 
1116
We can then use square like any other word:
1117
 
1118
  4 square .  ( prints 16 )
1119
  -5 square . ( prints 25 )
1120
 
1121
; ==== Help: Word Definitions 2/2 ==============================
1122
'hex' and 'decimal' are defined as:
1123
 
1124
  : hex $10 base ! ;
1125 5 howe.r.j.8
  : decimal $A base ! ;
1126 3 howe.r.j.8
 
1127
And simple Forth words likewise defined:
1128
 
1129
  : 2- 2 - ;      ( u -- u : decrement a number by 2 )
1130
  : 2+ 2 + ;      ( u -- u : increment a number by 2 )
1131
  : 2* 1 lshift ; ( u -- u : multiply a number by 2 )
1132
  : 1+ 1 + ;      ( u -- u : increment a number by 1 )
1133
  : negate invert 1+ ; ( n -- n : negate, twos compliment )
1134
 
1135
It is best to keep all Forth words as short as possible and
1136
reuse code as much as possible.
1137
; ==== Help: Interpreter Loop 1/3 ==============================
1138
All functions, control structures, defining words such as ":",
1139
and simple functions like "square" or "hex" are simply Forth
1140
words, which are either defined in terms of other Forth words
1141
or in terms of primitive operations supported by the H2 CPU.
1142
 
1143
Currently this does not explain how compilation happens. When
1144
the word ":" is encountered it does multiple things such as
1145
compiling a word header into the dictionary, and it does one
1146
more thing - it puts the Forth system into compile mode. The
1147
system starts out in command mode, in this mode words are
1148
executed, and numbers are pushed onto the stack. In compile
1149
mode, a call to the word is compiled (or an assembly
1150
instruction is inlined) and numbers of turned into literals
1151
that push their value when run.
1152
 
1153
; ==== Help: Interpreter Loop 2/3 ==============================
1154
There is a special class of words called "immediate" words, ";"
1155
is an immediate word, when it is encountered instead of being
1156
compiled into the dictionary it is executed, it compiles an
1157
exit instruction to terminate the word definition and it puts
1158
the Forth system back into command mode.
1159
 
1160
Not only are control structures sets of words, like "if",
1161
"else", "then", "begin", "until", but so are words like "("
1162
and "\" which are used to process comments. When executed
1163
they read from the input stream until the they find a ")" or
1164
end of line respectively.
1165
 
1166
Variables, strings, and defining words (defining words are
1167
that create new words) are all simply Forth words that are
1168
either compiling or immediate, their is no special syntax.
1169
; ==== Help: Interpreter Loop 3/3 ==============================
1170
The command loop goes like this:
1171
Start) Fetch a space delimited word, find it in the dictionary
1172
   Found) Is the system in compile mode?
1173
          Yes) Is the word immediate?
1174
               Yes) Execute It
1175
               No)  Compile a call to it / In-line it
1176
          No)  Execute the word
1177
   Not Found) Is the word a number?
1178
          Yes) Is the system in compile mode?
1179
               Yes) Compile the number
1180
               No)  Push the number onto the stack
1181
          No)  Error!
1182
This simple loop is invoked by the work 'quit' which uses
1183
'query' to fetch and parse the input and 'interpreter' to
1184
perform the state dependent action.
1185
; ==== Help: Control Structures 1/10 ===========================
1186
This will give you some idea what is going on when control
1187
structures are covered in the following blocks. Control
1188
structures can only be used within a word definition, that
1189
is in compile mode, if used in command mode they simply throw
1190
an error.
1191
 
1192
Some of the control structures available to Forth are:
1193
 
1194
  if ... then
1195
  if ... else ... then
1196
  for ... next
1197
  for ... aft ... then ... next
1198
  begin ... until
1199
  begin ... again
1200
  begin ... while ... repeat
1201
; ==== Help: Control Structures 2/10 ===========================
1202
Recursion is also available with the 'recurse' word. There
1203
are more advance control flow methods available that will be
1204
described later (such as 'catch', 'throw' and manipulating the
1205
return stack).
1206
 
1207
"if...then" will be described first. First a simple example,
1208
we will define a word called 'abs' that will return the
1209
absolute value of a number, that is negative numbers will be
1210
turned into positive numbers and positive numbers will stay
1211
the same. This will require the word 'negate' which changes
1212
the sign of a number and '0<' which tests if a number is
1213
negative, their stack effect comments are:
1214
 
1215
  0<     ( n -- f )
1216
  negate ( n -- n )
1217
; ==== Help: Control Structures 3/10 ===========================
1218
The word 'abs' is then defined as:
1219
 
1220
  : abs dup 0< if negate then ;
1221
 
1222
'if' takes an argument off the stack at run time and jumps to
1223
after the 'then' if it is false (zero), if it is true (non-
1224
zero) the negate is performed. From now if a word is shown in
1225
a definition that has not been previously mentioned please
1226
refer to the glossary. We can can use the definition of the
1227
word 'abs' as soon as we terminate the definition with ";":
1228
 
1229
  -9 abs . ( displays 9 )
1230
   4 abs . ( displays 4 )
1231
 
1232
This Forth uses two complements to represent negative numbers.
1233
; ==== Help: Control Structures 4/10 ===========================
1234
A number can be negated in twos compliment form by performing
1235
a bitwise not on the number and adding one to it, this is what
1236
the word 'negate' does.
1237
 
1238
"if...else...then" control structures can be created:
1239
 
1240
  : min 2dup < if drop else nip then ; ( n n -- n )
1241
  : max 2dup > if drop else nip then ; ( n n -- n )
1242
 
1243
These words get the minimum (min) and the maximum (max) of
1244
two numbers.
1245
 
1246
   4 3 min . ( prints 3 )
1247
   4 3 max . ( prints 4 )
1248
 
1249
; ==== Help: Control Structures 5/10 ===========================
1250
Whilst control structures can be nested very deeply it is
1251
considered very bad practice to do, instead the definition
1252
should be refactored so it consists of short (preferably one
1253
line) word definitions.
1254
 
1255
There are two mains ways of looping, either by recursion or
1256
with the several looping mechanisms. One of the simplest is
1257
"begin...until". This continues looping until a variable popped
1258
off the stack when the "until" word is reach is non-zero. Like
1259
the "if" control structure this can only be used within a word
1260
definition. An example is the definition of "key", which gets
1261
a single character from the input device, it blocks until there
1262
is input. It uses a word called "key?" which returns a variable
1263
number of items on the stack depending on whether there is
1264
new input from the user.
1265
; ==== Help: Control Structures 6/10 ===========================
1266
The stack comments for "key?" and "key" are:
1267
 
1268
  key? ( -- c -1 | 0 )
1269
  key  ( -- c )
1270
 
1271
Key returns the new character and "-1" (in Forth -1 is true,
1272
and 0 is false), or "0" if there is no new input. The pipe
1273
system "|" is used to separate the possible return values in
1274
the stack comment. We can use "begin...until" to define "key"
1275
in terms of "key?" with:
1276
 
1277
  : key begin key? until ;
1278
 
1279
This simply loops until there is a new character of input.
1280
 
1281
; ==== Help: Control Structures 7/10 ===========================
1282
Some contrived examples, using the standard metasyntactic
1283
variable name "foo":
1284
 
1285
  : foo begin 1- dup . cr dup 0= until drop ; ( u -- )
1286
 
1287
  4 foo ( <-- type this ) 3 ( <-- prints '3' )
1288
  2  ( <-- then '2' )
1289
  1  ( ... )
1290
 
1291
  ok ( then ok prompt )
1292
 
1293
If zero is given this will underflow and wrap around to $FFFF
1294
and continue the loop 65536 times which is probably not
1295
intended.
1296
 
1297
; ==== Help: Control Structures 8/10 ===========================
1298
Indefinite loops can be made with "begin...again", the
1299
following word "bar" simply repeatedly prints out "1":
1300
 
1301
  : bar begin 1 . cr again ;
1302
  bar 1 1 1 1 1 1 ( ... ad infinitum ... )
1303
 
1304
A looping mechanism that is easier to use is the
1305
"begin...while...repeat" loop, this continues an operation
1306
until a variable is not true:
1307
 
1308
  : foo begin ?dup while dup . cr 1- repeat ;
1309
  3 foo 3 ( <-- prints 3 )
1310
  2       ( <-- then 2 )
1311
  1       ( <-- then 1 )
1312
 
1313
; ==== Help: Control Structures 9/10 ===========================
1314
eForth provides another control structure, the "for...next" loop
1315
which allows the Forth programmer to create counted loops. The
1316
"for" loop takes a single variable, it stores this variable on
1317
the return stack (which will be covered next) and decrements the
1318
variable, until it is zero, at which point it exits (but still
1319
executes for the final zeroth case). The word 'r@' is used to
1320
push a copy of the top of the return stack, where the for loop
1321
keeps the conditional value.
1322
 
1323
  : foobar for r@ . cr next ;
1324
  3 foobar 3 ( <-- prints 3 )
1325
  2
1326
  1
1327
 
1328
 
1329
; ==== Help: Control Structures 10/10 ==========================
1330
The "for...next" construct can be modified with the "aft" and
1331
"then" construct, which is not executed on the first run of the
1332
loop.
1333
 
1334
  : quux for -9 . aft r@ . then -8 . cr then ;
1335
  decimal 3 quux -9 -8 ( for...aft and then...next is run )
1336
  2 -8 ( aft...then and then...next is run )
1337
  1 -8 ( ... )
1338
 
1339
 
1340
This concludes the control structure section, it should be
1341
noted that the control structures are defined words themselves
1342
again, it is possible to redefine and add your own control
1343
structures. How this is done will be covered later.
1344
 
1345
; ==== Help: Return Stack ======================================
1346
All Forth systems have at least two stacks along with main
1347
memory, they can be implemented in hardware like with this Forth
1348
or in software. One stack is used to pass data to functions and
1349
to return their results, the data or variable stack, which we
1350
have already dealt with. The other stack is used for control
1351
flow and as a place for temporarily storing variables.
1352
 
1353
Data can be moved to and from the return stacks, but this must
1354
be done very carefully and only within word definitions,
1355
leaving values on the stack by accident can cause the
1356
interpreter to crash.
1357
 
1358
Some looping constructs use the return stack as well to store
1359
their loop index, and the 'throw/catch' mechanism manipulates
1360
control flow a lot.
1361
; ==== Help: Return Stack ======================================
1362
Some words for manipulating the return stack are as follows,
1363
notice the stack comments describe both stacks here:
1364
 
1365
  >r    ( u --, R: -- u : move variable to return stack )
1366
  r>    ( -- u, R: u -- : move variable from return stack )
1367
  rdrop ( R: u -- : remove topmost variable from return stack )
1368
  r@    ( -- u, R: u -- u : copy topmost return stack item )
1369
 
1370
 
1371
 
1372
 
1373
 
1374
 
1375
 
1376
 
1377
; ==== Help: Control Structures ================================
1378
 
1379
 
1380
 
1381
 
1382
 
1383
 
1384
 
1385
 
1386
 
1387
 
1388
 
1389
 
1390
 
1391
 
1392
 
1393
; ==== Help: TO DO =============================================
1394
 
1395
* Describe the system internals
1396
* eForth primitives
1397
* Make, Doer, Create, Does>
1398
* Do word glossary, with one block per word
1399
* Vocabularies
1400
* Block Word set
1401
* Peripheral access word set
1402
* Throw/Catch
1403
* How Forth is implemented
1404
* Add appendixes for ASCII character set, ANSI terminal
1405
codes supported, the Nexy3 board, twos compliment format,
1406
list of characters supported, etcetera.
1407
* A short dictionary of English words, or a short
1408
encyclopedia could be made and stored in blocks
1409
; ==== Block Editor Help 1/5 ===================================
1410
 
1411
The traditional way to store Forth source code and data is as
1412
Forth blocks, a block is a contiguous array of 1024 characters,
1413
which can be saved to non-volatile storage with the 'block'
1414
word set. On more modern Forth systems, especially hosted ones,
1415
the file access word set is used instead.
1416
 
1417
A Forth that does not have a file system, such as this one,
1418
usually has a primitive block editor. An editor can be defined
1419
in just a handful of (non-standard) words.
1420
 
1421
By default the editor words are not in the Forth search order,
1422
the words in the editor vocabulary would conflict with Forth
1423
words (and with hexadecimal numbers). To load the block editor
1424
execute the 'editor' Forth word. Load initial block with '0 b'.
1425
; ==== Block Editor Help 2/5 ===================================
1426
The block editor is a prime example of Forth simplicity, it
1427
both simplifies the problem of editing text and reuses the
1428
Forth interpreter to define a new command language. Each command
1429
in the block editor simply a Forth word which takes its
1430
arguments off the stack, and a line is a fixed width 64
1431
character array, a block can contain 16 lines of text. An empty
1432
line consists entirely of spaces, which the interpreter will
1433
ignore if it were to evaluate the block.
1434
 
1435
The 'editor' word replaces the current vocabulary with the
1436
editor vocabulary and switches the number base for input and
1437
output to decimal. The commands are terse words, only one or
1438
two characters in length.
1439
 
1440
 
1441
; ==== Block Editor Help 3/5 ===================================
1442
The third element of the block editor is the elegant, if some-
1443
what limited, block word set. It completely abstracts aways the
1444
task of retrieving data from mass storage and saving modified
1445
data back to it. The mass storage is divided into blocks which
1446
can be loaded by their block number. Block numbers in this
1447
system start at 0 and go to $FFFE, $FFFF is an invalid block
1448
number.
1449
 
1450
The block word set is quite small, and the editor uses the
1451
words; 'block', 'update', 'save-buffers', 'list', 'load',
1452
and '+block'. The variable 'blk', which contains the last
1453
block loaded (and listed) is also used. Most editor commands
1454
are minor modifications on the behavior of these words.
1455
 
1456
 
1457
; ==== Block Editor Help 4/5 ===================================
1458
The workhorse of the word set is 'block', it takes a block
1459
number and if that block number is not currently loaded into a
1460
block buffer (this system only has one block buffer available)
1461
it checks to see if the current block is marked as dirty (with
1462
the 'update' word). If it has been it first flushes the dirty
1463
block to mass storage, then loads the desired block from it. It
1464
then returns a pointer to the beginning of the block buffer. It
1465
also updates the 'blk' variable to contain the block number
1466
just loaded.
1467
 
1468
The 'save-buffers' saves all buffers to disk and deallocates
1469
any blocks (by storing $FFFF in 'blk'). 'list' displays the
1470
contents of a block, 'load' evaluates a block and '+block'
1471
moves to a block relative to the currently loaded one.
1472
 
1473
; ==== Block Editor Help 5/5 ===================================
1474
A quick recap of the block words:
1475
 
1476
block        ( k -- u : load block into buffer )
1477
blk          ( -- a : last loaded block )
1478
+block       ( k -- : load block relative to 'blk' )
1479
save-buffers ( -- : flush block to disk, put -1 in 'blk' )
1480
list         ( k -- : display block )
1481
load         ( k -- : evaluate a block )
1482
 
1483
These can be used to define simple editor commands, such as:
1484
 
1485
  : b block drop ;
1486
  : n 1 +block b ;
1487
  : u update ;
1488
 
1489
; ==== Block Editor Commands 1/2 ===============================
1490
 
1491
'#' indicates a numeric argument the command takes:
1492
      n    move to next block
1493
      p    move to previous block
1494
    # d    delete line in current block
1495
      x    erase current block (overwrite with spaces)
1496
      e    evaluate current block
1497
    # i    insert line
1498
 # #2 ia   insert at line #2 at column #
1499
      q    quit editor loop
1500
    # b    load block number
1501
      s    save block and write it out
1502
      u    update block
1503
      w    list editor commands
1504
      q    back to Forth interpreter
1505
; ==== Block Editor Commands 2/2 ===============================
1506
 
1507
 
1508
      l    re-list current block
1509
    # c    paste copy buffer to line #
1510
    # y    yank line # into copy buffer
1511
 # #2 ct   copy line # to line #2
1512
    # ea   evaluate line #
1513
 # #2 sw   swap lines # and #2
1514
 
1515
The editor is simple to use and understand, but for large
1516
documents and programs can quite difficult to use. This block
1517
editor was derived from the Retro Forth block editor, available
1518
at: 
1519
 
1520
 
1521
; ==== TO DO ===================================================
1522
* The eForth primitives should be described.
1523
* Describe the Nexys 3 system, the system on a chip, etc.
1524
* List the instruction set, registers, words defined, eForth
1525
* Make words for databases using blocks
1526
* Add source code for everything to this block file
1527
* Add a program listing, perhaps in the first block, it could
1528
set variables relating to this document
1529
* If a metacompiler based on the embed project is used, it
1530
might be possible to have a self hosting system by storing the
1531
source code in blocks.
1532
 
1533
 
1534
 
1535
 
1536
 
1537
; ==== Empty Block =============================================
1538
 
1539
 
1540
 
1541
 
1542
 
1543
 
1544
 
1545
 
1546
 
1547
 
1548
 
1549
 
1550
 
1551
 
1552
 
1553
; ==== Glossary ================================================
1554
The next section presents the glossary, which contains one
1555
word per screen with examples. The glossary may not necessarily
1556
reflect the exact contents of the dictionary as words are
1557
removed, added and replaced to save space, but the majority
1558
should be documented here.
1559
 
1560
Hexadecimal and decimal numbers are used, and which base is
1561
in operation should be inferred from context.
1562
 
1563
The glossary shows how blocks can be used as a way to
1564
organize information, a concise description (that if needs
1565
be could span multiple blocks) describes each word. The first
1566
line can be used to create a table of contents with 'index'.
1567
This way of organizing blocks maps well onto dictionaries,
1568
encyclopedias, and all alphabetically organizable information.
1569
; ==== <  ( n1 n2 -- f : signed less than ) ====================
1570
Signed less than.
1571
 
1572
 
1573
 
1574
 
1575
 
1576
 
1577
 
1578
 
1579
 
1580
 
1581
 
1582
 
1583
 
1584
 
1585
; ==== <>  ( u1 u2 -- f : not equal ) ==========================
1586
'<>' takes two values off of the values stack and returns
1587
a boolean which is true (-1) if the two value are not equal
1588
and false (0) if they are.
1589
 
1590
 
1591
 
1592
 
1593
 
1594
 
1595
 
1596
 
1597
 
1598
 
1599
 
1600
 
1601
; ==== <#  ( -- : prepare numeric output ) =====================
1602
This is part of the mechanism for pictured numeric output,
1603
which should be used from within a word definition only.
1604
 
1605
 
1606
 
1607
 
1608
 
1609
 
1610
 
1611
 
1612
 
1613
 
1614
 
1615
 
1616
 
1617
; ==== =  ( u1 u2 -- f : equality of two numbers ) =============
1618
This is an assembly instruction for testing whether two numbers
1619
are equal.
1620
 
1621
 
1622
 
1623
 
1624
 
1625
 
1626
 
1627
 
1628
 
1629
 
1630
 
1631
 
1632
 
1633
; ==== >  ( n1 n2 -- f : signed greater than ) =================
1634
Signed greater than, if 'n1' is greater than 'n2' true (-1) is
1635
returned, else false (0) is returned. This operates on signed
1636
numbers stored in twos compliment format.
1637
 
1638
 
1639
 
1640
 
1641
 
1642
 
1643
 
1644
 
1645
 
1646
 
1647
 
1648
 
1649
; ==== -  ( n1 n2 -- n : subtraction ) =========================
1650
This is word performs subtraction of 'n2' from 'n1'.
1651
 
1652
 
1653
 
1654
 
1655
 
1656
 
1657
 
1658
 
1659
 
1660
 
1661
 
1662
 
1663
 
1664
 
1665
; ==== ,  ( u -- : compile value into dictionary ) =============
1666
The word ',' compiles a value at the next available location
1667
in the dictionary, updating the dictionary pointer by the size
1668
of a cell.
1669
 
1670
 
1671
 
1672
 
1673
 
1674
 
1675
 
1676
 
1677
 
1678
 
1679
 
1680
 
1681
; ==== ;  ( -- : terminate a word definition ) =================
1682
This word terminates a word definition, linking the newly formed
1683
word into the dictionary. It is an immediate word and performs
1684
some basic checking to make sure control structures match up. It
1685
writes an 'exit' into the word and switches the state back into
1686
command mode. It has another minor usage, if called within
1687
command it throws -56, for QUIT. This can be used within a block
1688
to stop execution of it, if 'thru' is used to load block, this
1689
error is ignored.
1690
 
1691
Example:
1692
 
1693
   : 2+ 2 + ;
1694
   : count dup 1+ swap c@ ;
1695
   :noname 9 . ; execute ( <-- prints 9 )
1696
 
1697
; ==== :  ( -- ;  : start a word definition ) ==========
1698
':' is a complex word that does many things. It is not an
1699
immediate word, but it is a parsing word that reads in the
1700
next space delimited word and compiles a word header into the
1701
dictionary. It also switches the interpreter state into compile
1702
mode, until switched back non-immediate words will now be
1703
compiled into this word definition and numbers compiled into
1704
it as literals. ':' is how most new words (or functions) are
1705
defined within Forth. The word ';' should also be consulted.
1706
 
1707
Usage
1708
 
1709
  : example 2 + . cr ; ( <- makes a new word called 'example' )
1710
  2 example            ( <- the new word prints '4' )
1711
 
1712
 
1713
; ==== !  ( u a -- : store 'u' at address 'a' ) ================
1714
Store a value in at an address, this is also used to write to
1715
the memory mapped peripheral registers.
1716
 
1717
 
1718
 
1719
 
1720
 
1721
 
1722
 
1723
 
1724
 
1725
 
1726
 
1727
 
1728
 
1729
; ==== ?  ( a -- : print value at address ) ====================
1730
Print a value out from an address, this is effected by the
1731
output radix stored in 'base'. This word uses the area between
1732
PAD area and the end of the dictionary.
1733
 
1734
 
1735
 
1736
 
1737
 
1738
 
1739
 
1740
 
1741
 
1742
 
1743
 
1744
 
1745
; ==== /  ( n1 n2 -- q: divide n1 by n2 ) ======================
1746
Signed and floored division of the second value on the stack by
1747
the first. This will throw and error if the divisor is zero.
1748
This is a particularly slow operation, as the H2 CPU does not
1749
a built in division operation.
1750
 
1751
 
1752
 
1753
 
1754
 
1755
 
1756
 
1757
 
1758
 
1759
 
1760
 
1761
; ==== .  ( n -- : print number out in current base ) ==========
1762
Print a space, then a signed number (if the output radix is ten
1763
then a '-' is printed before the number if it is less than zero,
1764
if the output radix is not ten, then it is just printed out as
1765
an unsigned number). This word uses the area after the
1766
dictionary and before the start of the pad area, it is not a
1767
reentrant function.
1768
 
1769
 
1770
 
1771
 
1772
 
1773
 
1774
 
1775
 
1776
 
1777
; ==== ."  ( -- ;  : compile string into word ) ========
1778
This word is an immediate word that compiles a string into a
1779
word definition (it should only be used from within a word
1780
definition). When the word is run it will print out the string.
1781
 
1782
The string is terminated by a double quote. A version of this
1783
word which does no printing but instead returns a pointer to
1784
a counted string is available, and it is called '$"'.
1785
 
1786
Usage:
1787
 
1788
  : hello cr ." Hello, World! " cr ;
1789
  hello
1790
  Hello, World! ( <- 'Hello, World!' is printed )
1791
 
1792
 
1793
; ==== .(  ( -- ;  : print out line until ')' ) ========
1794
This word prints out a string until the matching ')' is
1795
encountered. It is not an immediate word. It is a parsing
1796
word.
1797
 
1798
 
1799
 
1800
 
1801
 
1802
 
1803
Usage:
1804
 
1805
  .( Hello, World! ) cr
1806
  Hello, World!  ( <- 'Hello, World!' is printed )
1807
 
1808
 
1809
; ==== '  ( -- xt ;  : return a word execution token ) =
1810
This word parses the next word in the input stream and returns
1811
its execution token if the word is found. It is an immediate
1812
word. If the word is not found it throws an exception.
1813
 
1814
 
1815
 
1816
 
1817
 
1818
 
1819
 
1820
 
1821
 
1822
 
1823
 
1824
 
1825
; ==== (  ( -- ;  : comment until ')' ) ================
1826
This is an immediate word used for comments, it discards all
1827
input until the matching ')' is encountered. It can be used
1828
within blocks. This word should not span multiple lines
1829
however.
1830
 
1831
Usage:
1832
 
1833
  2 2 + ( This is a comment ) . ( <- this '.' prints '4' )
1834
 
1835
 
1836
 
1837
 
1838
 
1839
 
1840
 
1841
; ==== )  ( -- : do nothing ) ==================================
1842
This is a word that does nothing. It is an immediate word. It
1843
has no function.
1844
 
1845
 
1846
 
1847
 
1848
 
1849
 
1850
 
1851
 
1852
 
1853
 
1854
 
1855
 
1856
 
1857
; ==== [  ( -- : change compile state to command mode ) ========
1858
An immediate word for changing the state of the interpreter loop
1859
into the command state. It can be used within a word definition.
1860
 
1861
 
1862
 
1863
 
1864
 
1865
 
1866
 
1867
 
1868
 
1869
 
1870
 
1871
 
1872
 
1873
; ==== ]  ( -- : change compile state to compile mode ) ========
1874
A normal, compiling word, which turns the interpreter state into
1875
compile mode. Words and numbers after (apart from immediate
1876
words) are compiled into the dictionary.
1877
 
1878
 
1879
 
1880
 
1881
 
1882
 
1883
 
1884
 
1885
 
1886
 
1887
 
1888
 
1889
; ==== @  ( a -- u : load value from address ) =================
1890
Retrieve a value from memory location 'a'. It can be used to
1891
read from the memory mapped registers as well.
1892
 
1893
 
1894
 
1895
 
1896
 
1897
 
1898
 
1899
 
1900
 
1901
 
1902
 
1903
 
1904
 
1905
; ==== $"  ( -- ;  : compile string into word ) ========
1906
This is an immediate word that can only be used in a word
1907
definition, it compiles a string into the dictionary, when it
1908
runs it pushes the address of the counted string compiled into
1909
the word.
1910
 
1911
 
1912
 
1913
 
1914
 
1915
 
1916
 
1917
 
1918
 
1919
 
1920
 
1921
; ==== *  ( u1 u2 -- u3 : multiple two numbers ) ===============
1922
Multiply two numbers and push the result onto the stack. This is
1923
a slow operation, as there is not a built in multiply operation.
1924
 
1925
 
1926
 
1927
 
1928
 
1929
 
1930
 
1931
 
1932
 
1933
 
1934
 
1935
 
1936
 
1937
; ==== \  ( -- ;  : comment until end of line ) ========
1938
Comment until end of input line, this word works within blocks
1939
as well, where it is a comment until the end of the block line.
1940
It is an immediate word.
1941
 
1942
 
1943
 
1944
 
1945
 
1946
 
1947
 
1948
 
1949
 
1950
 
1951
 
1952
 
1953
; ==== #  ( u -- u : extract numeric character from u ) ========
1954
 
1955
 
1956
 
1957
 
1958
 
1959
 
1960
 
1961
 
1962
 
1963
 
1964
 
1965
 
1966
 
1967
 
1968
 
1969
; ==== #>  ( u -- b u : return converted number string ) =======
1970
 
1971
 
1972
 
1973
 
1974
 
1975
 
1976
 
1977
 
1978
 
1979
 
1980
 
1981
 
1982
 
1983
 
1984
 
1985
; ==== +  ( u1 u2 -- u3 : add two numbers together ) ===========
1986
 
1987
 
1988
 
1989
 
1990
 
1991
 
1992
 
1993
 
1994
 
1995
 
1996
 
1997
 
1998
 
1999
 
2000
 
2001
; ==== +!  ( u a -- : add 'u' to value 'a' ) ===================
2002
 
2003
 
2004
 
2005
 
2006
 
2007
 
2008
 
2009
 
2010
 
2011
 
2012
 
2013
 
2014
 
2015
 
2016
 
2017
; ==== 0<  ( n -- f : 'n' less than zero? ) ====================
2018
'0<' is a test word that operates on a signed value and returns
2019
a boolean value, it returns -1 for true if 'n' is less than
2020
zero and 0 for false if 'n' is greater than zero.
2021
 
2022
 
2023
 
2024
 
2025
 
2026
Usage:
2027
 
2028
 
2029
     9 0< . ( prints '0' )
2030
    -4 0< . ( prints '-1' or 'FFFF' )
2031
  FFFE 0< . ( prints '-1' or 'FFFF', 'FFFE' = -2 )
2032
 
2033
; ==== 0<>  ( n -- f : 'n' not equal to zero? ) ================
2034
This word tests if 'n' is not zero, returning true (-1) if it
2035
is not zero, and false if it is (0).
2036
 
2037
Usage:
2038
 
2039
 
2040
  -7 0<> . ( prints '-1' or 'FFFF' )
2041
   5 0<> . ( prints '-1' or 'FFFF' )
2042
 
2043
 
2044
 
2045
 
2046
 
2047
 
2048
 
2049
; ==== 0=  ( n -- f : 'n' equal to zero? ) =====================
2050
This word tests if 'n' is zero, returning true (-1) if it is,
2051
and false if it is not (0).
2052
 
2053
 
2054
Usage:
2055
 
2056
  : 0<> 0= 0= ;
2057
 
2058
   4 0= .        ( prints '0' )
2059
  -5 0= .        ( also prints '0' )
2060
 
2061
 
2062
 
2063
 
2064
 
2065
; ==== 0>  ( n -- f : 'n' greater than zero? ) =================
2066 5 howe.r.j.8
'0>' performs a greater than zero test on a signed number, if
2067
'n' is greater than zero it returns true (-1), otherwise it
2068
returns false (0) if 'n' is equal to or less than zero.
2069 3 howe.r.j.8
 
2070
 
2071
 
2072
 
2073
 
2074
 
2075
 
2076
 
2077
 
2078
 
2079
 
2080
 
2081
; ==== 1-  ( u -- u : decrement u ) ============================
2082
'1-' decrements the top most stack item.
2083
 
2084
 
2085
 
2086
 
2087
 
2088
 
2089
 
2090
 
2091
 
2092
 
2093
 
2094
 
2095
 
2096
 
2097
; ==== 1+  ( u -- u : increment u ) ============================
2098 5 howe.r.j.8
This is a regular word that increments a value by one.
2099 3 howe.r.j.8
 
2100
 
2101
 
2102
 
2103
 
2104
 
2105
 
2106
 
2107
 
2108
 
2109
 
2110
 
2111
 
2112
 
2113
; ==== 1+!  ( a -- : increment value at 'a' by one ) ===========
2114
'1+!' increments the value at an address, 'a', by one.
2115
 
2116
 
2117
 
2118
 
2119
 
2120
 
2121
 
2122
 
2123
 
2124
 
2125
 
2126
 
2127
 
2128
 
2129
; ==== 2!  ( u1 u2 a -- : store u1 and u2 at two cells ) =======
2130
 
2131
 
2132
 
2133
 
2134
 
2135
 
2136
 
2137
 
2138
 
2139
 
2140
 
2141
 
2142
 
2143
 
2144
 
2145
; ==== 2/  ( u -- u : divide 'u' by two ) ======================
2146 5 howe.r.j.8
'2/' is a division by 2, it is implemented as a logical right
2147
shift by one.
2148 3 howe.r.j.8
 
2149
 
2150
 
2151
 
2152
 
2153
 
2154
 
2155
 
2156
 
2157
 
2158
 
2159
 
2160
 
2161
; ==== 2@  ( a -- u1 u2 : retrieve two cells ) =================
2162 5 howe.r.j.8
'2@' retrieves two consecutive cells from an address 'a'. The
2163
word forms a pair with '2!', which stores two cells at two
2164
consecutive addresses.
2165 3 howe.r.j.8
 
2166
 
2167
 
2168
 
2169
 
2170 5 howe.r.j.8
Usage:
2171 3 howe.r.j.8
 
2172 5 howe.r.j.8
   1 2     ( -- 1 2 : input test values )
2173
   pad 2!  ( 1 2 -- : store values in temporary storage )
2174
   pad 2@  ( -- 1 2 : retrieve values )
2175 3 howe.r.j.8
 
2176
 
2177
; ==== 2*  ( u -- u : multiply 'u' by two ) ====================
2178 5 howe.r.j.8
'2*' is a multiply by two, it is implemented as a left shift
2179
by one.
2180 3 howe.r.j.8
 
2181
 
2182
 
2183
 
2184
 
2185
 
2186
 
2187
 
2188
 
2189
 
2190
 
2191
 
2192
 
2193
; ==== 2drop  ( u u -- : drop two values from the stack ) ======
2194
A simple word, '2drop' behaves as its stack comment suggests,
2195
it removes the top most two items from the variable stack.
2196
 
2197
Usage:
2198
 
2199
  1 2 3 drop . 1 ( '1' is printed )
2200
 
2201
 
2202
 
2203
 
2204
 
2205
 
2206
 
2207
 
2208
 
2209
; ==== 2dup  ( u1 u2 -- u1 u2 u1 u2 : duplicate two values ) ===
2210
This manipulates two items on the stack, like most words with
2211
a '2' in their name. The stack effect describes the word
2212
perfectly. Words like these, which produce and manipulate lots
2213
of stack items should be avoided if possible.
2214
 
2215
Usage:
2216
         ( -- : empty stack )
2217
   1 2   (  -- 1 2  )
2218
   2dup  ( 1 2 -- 1 2 1 2 )
2219
 
2220
 
2221
 
2222
 
2223
 
2224
 
2225
; ==== abs  ( n -- u : absolute value of a number ) ============
2226
Returns the absolute value of a signed number, positive values
2227
stay the same, negative values become positive. Signed numbers
2228
are stored in twos compliment format, the function fails for
2229
the number $8000, which is unaffected by abs.
2230
 
2231
Usage:
2232
 
2233
  -1 abs .  ( prints '1' )
2234
   4 abs .  ( prints '4' )
2235
 
2236
 
2237
 
2238
 
2239
 
2240
 
2241
; ==== accept  ( b u -- b u : accept a line ) ==================
2242
 
2243
 
2244
 
2245
 
2246
 
2247
 
2248
 
2249
 
2250
 
2251
 
2252
 
2253
 
2254
 
2255
 
2256
 
2257
; ==== aft  ( -- : part of for...aft...then...next ) ===========
2258
 
2259
 
2260
 
2261
 
2262
 
2263
 
2264
 
2265
 
2266
 
2267
 
2268
 
2269
 
2270
 
2271
 
2272
 
2273
; ==== again  ( -- : part of begin...again loop ) ==============
2274
'again' should only be used as part of a 'begin...again' loop,
2275
which is a compile time only wordset. Using an 'again' without
2276
a 'begin' may also be met with an error, as this situtation
2277
should be caught by the compiler security mechanisms. The
2278
'begin...again' looping mechanism forms an indefinite or an
2279
infinite loop, the Forth interpreter/CPU jumps back to the
2280
corresponding 'begin' unconditionally, and excluding any
2281
errors/exceptions will do so forever.
2282
 
2283
Usage:
2284
  : invalid-1 again ; ( <- invalid use, no matching 'begin )
2285
  again        ( <- invalid use, outside a word definition )
2286
  : test begin 1 . cr again ;
2287
  test ( 'test' prints '1' forever )
2288
 
2289
; ==== align  ( -- : align dictionary pointer ) ================
2290
'align' is a word that takes no arguments and returns nothing,
2291
it acts on the dictionary pointer variable (the internal name
2292
for which is usually 'cp', the variable is returned by the
2293
word 'here'). It should be used after allocating a data
2294
structure in the dictionary which is byte aligned and before
2295
defining any new words, the word 'align' reserves space up to
2296
the next cell aligned address by manipulating the dictionary
2297
pointer variable.
2298
 
2299
Usage:
2300
 
2301
   create x 3 allot ( <- dictionary pointer is now unaligned )
2302
   align            ( <- dictionary pointer is now aligned )
2303
 
2304
 
2305
; ==== aligned  ( b -- a : align up an address ) ===============
2306 5 howe.r.j.8
'aligned' takes an address 'b', that is potentially unaligned
2307
and returns an aligned address by rounding up to the nearest
2308
cell boundary. This word is like 'align' but it works on any
2309
pointer instead of the dictionary pointer only. As this is
2310
a 16-bit Forth system this rounds up to the nearest 16-bit cell
2311
boundary, or to next multiple of two (bytes).
2312 3 howe.r.j.8
 
2313 5 howe.r.j.8
Usage:
2314 3 howe.r.j.8
 
2315 5 howe.r.j.8
 
2316
  1 aligned . ( prints '2' )
2317
  2 aligned . ( prints '2' )
2318
  3 aligned . ( prints '4' )
2319
  4 aligned . ( prints '4' )
2320 3 howe.r.j.8
 
2321
; ==== allot  ( n -- : allocate 'n' bytes ) ====================
2322
Allocate 'n' bytes of space in the dictionary, 'n' can be
2323
either positive, or negative, negative values deallocate space,
2324
which may break things.
2325
 
2326
 
2327
 
2328
 
2329
 
2330
 
2331
 
2332
 
2333
 
2334
 
2335
 
2336
 
2337
; ==== and  ( u1 u2 -- u3 : bitwise and of two numbers ) =======
2338
This performs a bitwise and on two numbers.
2339
 
2340
 
2341
 
2342
 
2343
 
2344
 
2345
 
2346
 
2347
 
2348
 
2349
 
2350
 
2351
 
2352
 
2353
; ==== ansi ( n c -- : emit an ANSI escape command ) ===========
2354 5 howe.r.j.8
'ansi' is used to help construct ANSI escape code sequences,
2355
most sequences consist of a CSI escape code, a number in base
2356
10, and a single character to indicate the command. The
2357
character and the number change, and are given as arguments
2358
to the word 'ansi'. Most terminal emulators can deal with these
2359
sequences, and the hardware VT100 clone that controls the VGA
2360
display can deal with most of the more important codes.
2361 3 howe.r.j.8
 
2362 5 howe.r.j.8
Usage:
2363 3 howe.r.j.8
 
2364 5 howe.r.j.8
  : sgr [char] m ansi ;
2365
  $1f sgr ( turn foreground color red )
2366
  $0  sgr ( reset color )
2367
  2 char J ansi ( blank screen, set cursor position to 1,1 )
2368 3 howe.r.j.8
 
2369
; ==== at-xy  ( x y -- : place cursor at column 'x', row 'y' ) =
2370 5 howe.r.j.8
'at-xy' emits the ANSI escape sequence which will place the
2371
cursor at column 'x' and row 'y'. This should work with the
2372
built in VT100 terminal emulator, and with any decent terminal
2373
emulator used to talk to the device over UART. 'at-xy' can be
2374
used to draw primitive graphics along with other escape codes.
2375
Counting starts from one, not zero.
2376 3 howe.r.j.8
 
2377 5 howe.r.j.8
Usage:
2378 3 howe.r.j.8
 
2379 5 howe.r.j.8
  1 1 at-xy  ( set cursor to top left corned )
2380
  5 10 at-xy ( cursor to column 5, row 10 )
2381 3 howe.r.j.8
 
2382
 
2383
 
2384
 
2385
; ==== base  ( -- a : address of base radix ) ==================
2386
Pushes the address of the base variable onto the variable stack,
2387
this can be used to change the input and output radix of numbers
2388
to a base between 2 and 36 inclusive. Characters '0' to '9' are
2389
used for numbers 0 through 9, and letters 'A' to 'Z' are used
2390
for numbers 10 to 35, upper case and lower case letter can be
2391
used, only upper case characters are output.
2392
 
2393
Setting the base to an invalid value can cause exceptions to
2394
be thrown in other words.
2395
 
2396
Example usage:
2397
   $2 base !
2398
   base @
2399
   $8 base !
2400
 
2401
; ==== b/buf  ( -- u : number of bytes in a block ) ============
2402 5 howe.r.j.8
This pushes the number of bytes in a block onto the stack,
2403
which is 1024 in most Forth implementations, including this
2404
one.
2405 3 howe.r.j.8
 
2406
 
2407
 
2408
 
2409
 
2410 5 howe.r.j.8
Usage:
2411 3 howe.r.j.8
 
2412 5 howe.r.j.8
  blk @ block b/buf blank ( blank most recently loaded block )
2413 3 howe.r.j.8
 
2414
 
2415
 
2416
 
2417
; ==== begin  ( -- : start begin...until/again loop ) ==========
2418 5 howe.r.j.8
'begin' is used to start three looping mechanisms,
2419
* 'begin...again'          ( infinite loop )
2420
* 'begin...until'          ( loop until non-zero )
2421
* 'begin...while...repeat' ( loop until zero )
2422
Which are best explained in the entries for 'again', 'until'
2423
and 'while'/'repeat' respectively. 'begin' is an immediate
2424
word that should only be used within a word definition, and
2425
should be used in conjunction with a valid looping mechanism
2426
word set.
2427 3 howe.r.j.8
 
2428 5 howe.r.j.8
Usage:
2429 3 howe.r.j.8
 
2430 5 howe.r.j.8
 See 'again', 'repeat', 'while' and 'until' for examples.
2431 3 howe.r.j.8
 
2432
 
2433
; ==== bl  ( -- u : value for space character ) ================
2434 5 howe.r.j.8
This pushes the space character to the stack. This is the
2435
ASCII character 32 (decimal), or $20 (hexadecimal). This is
2436
a useful character for comparing character ranges (anything
2437
of a lower numeric value than space is a control character in
2438
ASCII) and because it is unobtainable by using the word 'char'
2439
or '[char]', as they use space to delimited the character to be
2440
read in.
2441 3 howe.r.j.8
 
2442
 
2443
 
2444
 
2445
 
2446
 
2447
 
2448
 
2449
; ==== blank  ( b u -- : fill array with blanks ) ==============
2450
This word writes the space character to a byte array, one use
2451
is to erase a block before it is displayed with 'list'.
2452
 
2453
 
2454
 
2455
 
2456
 
2457
 
2458
 
2459
 
2460
 
2461
 
2462
 
2463
 
2464
 
2465
; ==== blk  ( -- a : address of last loaded block ) ============
2466 5 howe.r.j.8
'blk' is a variable which contains the value of the last block
2467
to be loaded. This value is updated by 'block' on a successful
2468
operation, and is set to an invalid block number on a block
2469
error, or when 'flush' is called.
2470 3 howe.r.j.8
 
2471
 
2472
 
2473
 
2474
 
2475
 
2476
 
2477
 
2478
 
2479
 
2480
 
2481 5 howe.r.j.8
; ==== block  ( k -- a : perform block operation ) =============
2482
'block' is a complex word that abstracts away all the details
2483
of mass storage, whilst at the same time being easy to use, it
2484
forms the basis on which Forth Blocks are used to organize
2485
code and data. 'block'  takes a block number and if it is
2486
valid attempts to find a free buffer in which to load the
2487
block into, it then returns a pointer to that buffer. If no
2488
free buffers are available it saves a dirty buffer to disk
2489
then deallocates it, or it overwrites a clean buffer (as it
2490
can always be loaded from disk if needed again).
2491 3 howe.r.j.8
 
2492 5 howe.r.j.8
Usage:
2493 3 howe.r.j.8
 
2494 5 howe.r.j.8
   5 block 2 cells + 1234 swap ! update flush
2495 3 howe.r.j.8
 
2496
 
2497
; ==== +block ( n -- k : return block number relative to blk ) =
2498
'+block' is a useful common operation which returns the block
2499
number relative to the one currently loaded in 'blk', it can
2500
be thus used to navigate relative to the most recently loaded
2501
block. The number it accepts is signed, the next blocks being
2502
specified with a positive number, the current with zero, and
2503
negative being prior blocks.
2504
 
2505
Usage:
2506
 
2507
  : +block blk @ + ;      ( n -- k )
2508
  : next 1 +block ;       (
2509
  : previous -1 +block ;
2510
  5 block drop next next previous
2511
 
2512
 
2513
; ==== border  ( -- a : variable for list display control ) ====
2514 5 howe.r.j.8
'border' is a variable that controls the display of 'list',
2515
it turns the border on when it is set to true (-1), and off
2516
when it is set to false (0).
2517 3 howe.r.j.8
 
2518
 
2519
 
2520
 
2521
 
2522
 
2523
 
2524
 
2525
 
2526
 
2527
 
2528
 
2529
; ==== c,  ( c -- : compile character into dictionary ) ========
2530
This words takes a character and writes the character in the
2531
next available location in the dictionary, and updates the
2532
dictionary pointer. This word can cause the dictionary number
2533
to be misaligned, 'align' can be used to remedy this.
2534
 
2535
 
2536
 
2537
 
2538
 
2539
 
2540
 
2541
 
2542
 
2543
 
2544
 
2545
; ==== c!  ( c b -- : store 'c' at 'b' ) =======================
2546
'c!' is the byte oriented store word, it stores a byte 'c' at
2547
a potentially unaligned address 'b'. This is not a primitive
2548
operation provided by the processor as it can only do cell
2549
sized loads and stores in a single instruction. The
2550
corresponding word for load a byte is 'c@'. The cell load
2551
and store words are '@' and '!' respectively.
2552
 
2553
 
2554
 
2555
 
2556
 
2557
 
2558
 
2559
 
2560
 
2561
; ==== c@  ( b -- c : retrieve 'c' from 'b' ) ==================
2562
'c@' retrieves a single byte from the address 'b', this address
2563
does not have to be an aligned address. For storing a byte, see
2564
'c!'. This word is the byte oriented version of '@', '@'
2565
operates on Forth cells. On this processor 'c@' is not a
2566
primitive operation, for speed '@' should be used instead, as
2567
loads (and stores) can only be done by cell and not by byte.
2568
 
2569
 
2570
 
2571
 
2572
 
2573
 
2574
 
2575
 
2576
 
2577
; ==== catch  ( xt -- n | 0 : catch possible exception ) =======
2578
'catch' can catch exceptions thrown by 'throw', it is part of
2579
a pair of words that work as a pair like many Forth words, one
2580
cannot exist without the other. 'catch' takes an execution
2581
token, executes it, and returns 0 if nothing was thrown, and
2582
a non-zero value if something was. 'catch' is also used as part
2583
of the interpreter loop as an exception handler of last resort.
2584
The word 'throw' should also be consulted. 'throw' and 'catch'
2585
manipulate the return stack a lot and are fairly complex words
2586
that are hard to implement on a processor with hardware stacks.
2587
 
2588
Usage:
2589
 
2590
  : x 99 throw ;   : y x 66 ;
2591
  ' x catch . ( prints '99' )
2592
 
2593
; ==== ccitt  ( crc c -- crc : compute CRC step ) ==============
2594
 
2595
 
2596
 
2597
 
2598
 
2599
 
2600
 
2601
 
2602
 
2603
 
2604
 
2605
 
2606
 
2607
 
2608
 
2609
; ==== cell-  ( a -- a : decrement address by single cell ) ====
2610
'cell-' is the opposite of 'cell+', it decrements an address
2611
value by a cell. On this 16-bit Forth a cell consists of two
2612
bytes, but on a 32-bit one it would decrement by four. Note
2613
that this word does not ensure the resulting address is
2614
aligned, if an unaligned addressed was passed in and unaligned
2615
address will pop out, and an aligned address will stay so. This
2616
word is needed so Forth code can be written that is portable
2617
across different word size computers.
2618
 
2619
 
2620
 
2621
 
2622
 
2623
 
2624
 
2625
; ==== cell+  ( a -- a : increment address by single cell ) ====
2626
'cell+' increments an address by the size of a cell, see
2627
'cell-' for further description on this.
2628
 
2629
 
2630
 
2631
 
2632
 
2633
 
2634
 
2635
 
2636
 
2637
 
2638
 
2639
 
2640
 
2641
; ==== cells  ( n -- n : convert cell to byte count ) ==========
2642
'cells' is a word that takes a count of the number of cells and
2643
returns the number of bytes that those cells would occupy, on
2644
this 16-bit Forth 'cells' is essentially a multiply by two
2645
operation, but on other Forths it could be a different value,
2646
so this word is used for portability reasons.
2647
 
2648
Usage:
2649
 
2650
 
2651
  1 cells . ( prints '2' )
2652
  2 cells . ( prints '4' )
2653
  create x 4 cells allot ( 'x' created, space for 4 cells )
2654
 
2655
 
2656
 
2657
; ==== >char ( c -- c : convert unprintable character ) ========
2658
'>char' converts a character so that it can be printed by
2659
converting any characters which cannot be printed to the
2660
underscore character ('_'). This is used within words like
2661
'message' and 'list'. Tabs also get converted, as do control
2662
characters, delete and any character outside of the ASCII
2663
range.
2664
 
2665
 
2666
 
2667
 
2668
 
2669
 
2670
 
2671
 
2672
 
2673
; ==== char  ( -- c ;  : parse character ) =============
2674
This word reads in a space delimited word from the input stream
2675
and pushes the first character of that word onto the stack. An
2676
entire word is potentially read in, but only the first
2677
character is used.
2678
 
2679
Usage:
2680
 
2681
  char x .          ( prints '$78' )
2682
  char xylophone .  ( prints '$78', also )
2683
  char 0 .          ( prints '$30' )
2684
 
2685
 
2686
 
2687
 
2688
 
2689
; ==== [char]  ( -- ;  : compile character literal ) ===
2690
This is an immediate word that compiles a character into the
2691
dictionary as a literal value that will be pushed onto the
2692
stack when the word is run. It parses a space delimited word
2693
and compiles the first character of that word. It is a compile
2694
only word. Although an entire word is parsed, only the first
2695
character is used.
2696
 
2697
Usage:
2698
 
2699
  : print-c [char] c emit ;
2700
  print-c c            ( 'c' is printed )
2701
  : push-c [char] c ;
2702
  push-c . 63          ( ASCII 'c' is $63 )
2703
 
2704
 
2705
; ==== cmove  ( b b u -- : move block memory ) =================
2706
'cmove' copies a block of characters from one location to
2707
another.
2708
 
2709
 
2710
 
2711
 
2712
 
2713
 
2714
 
2715
 
2716
 
2717
 
2718
 
2719
 
2720
 
2721
; ==== compile ( -- : compile next word into dictionary ) ======
2722
 
2723
 
2724
 
2725
 
2726
 
2727
 
2728
 
2729
 
2730
 
2731
 
2732
 
2733
 
2734
 
2735
 
2736
 
2737
; ==== [compile]  ( -- xt : compile next immediate word ) ======
2738
 
2739
 
2740
 
2741
 
2742
 
2743
 
2744
 
2745
 
2746
 
2747
 
2748
 
2749
 
2750
 
2751
 
2752
 
2753
; ==== compile,  ( xt -- : compile execution token ) ===========
2754
'compile,' compiles an execution token into the next available
2755
location in the dictionary, taking up a cell of space. This
2756
word must be used when compiling in an execution into a word,
2757
the word ',' will compile an unconditional branch.
2758
 
2759
 
2760
 
2761
 
2762
 
2763
 
2764
 
2765
 
2766
 
2767
 
2768
 
2769
; ==== conceal  ( -- : emit stars instead of characters ) ======
2770
 
2771
 
2772
 
2773
 
2774
 
2775
 
2776
 
2777
 
2778
 
2779
 
2780
 
2781
 
2782
 
2783
 
2784
 
2785
; ==== console  ( -- : UART only mode ) ========================
2786
 
2787
 
2788
 
2789
 
2790
 
2791
 
2792
 
2793
 
2794
 
2795
 
2796
 
2797
 
2798
 
2799
 
2800
 
2801
; ==== constant  ( n -- ;  : create constant ) =========
2802
'constant' is a defining word that creates a new constant, it
2803
takes in a value from the stack, and parses the next word from
2804
the input stream which will become the name of the new
2805
constant. This word uses 'create' and 'does>' internally to
2806
do the work, and is similar to the definition of 'variable'.
2807
At run time the new word when called will leave a copy of
2808
'n' on the stack.
2809
 
2810
Usage:
2811
 
2812
   : constant create , does> @ ;
2813
   4 constant x
2814
   3 constant y
2815
   x y + . ( prints '7' )
2816
 
2817
; ==== count  ( b -- b c : retrieve next character in string ) =
2818
'count' is a very useful word when dealing with strings,
2819
especially counted strings. It retrieves a character from an
2820
address and increments that address by one character. This can
2821
be used to advance through a string, and with counted strings
2822
it can be used to retrieve the string length which is stored
2823
in the first byte of the string.
2824
 
2825
 
2826
 
2827
 
2828
 
2829
 
2830
 
2831
 
2832
 
2833
; ==== cpu-id  ( -- u : return CPU-ID ) ========================
2834
This is an assembly instruction that pushes the CPU-ID of the
2835
CPU onto the stack. It can be used to determine whether the
2836
system is running in a simulation or on the hardware.
2837
 
2838
 
2839
 
2840
 
2841
 
2842
 
2843
 
2844
 
2845
 
2846
 
2847
 
2848
 
2849
; ==== cr  ( -- : emit new line ) ==============================
2850
'cr' emits a new line, which consists of a carriage return
2851
character followed by a linefeed character. This format is used
2852
as communication occurs over a UART (and the VGA output display
2853
behaves like a VT100 video terminal).
2854
 
2855
 
2856
 
2857
 
2858
 
2859
 
2860
 
2861
 
2862
 
2863
 
2864
 
2865
; ==== crc  ( b u -- u : perform CRC over array ) ==============
2866
'crc' performs a Cyclic Redundancy Check (CRC) over an array
2867
specified by an address ('b') and length ('u') pair. It
2868
returns the result in a single cell. There are many different
2869
ways of computing a CRC which depending on starting value,
2870
what operation to perform at the end of the calculation and
2871
what polynomial is used. 'crc' used the word 'ccitt' to do
2872
the actual calculation on each byte, using the CCITT
2873
polynomial, which is a standard polynomial. See 'ccitt' for
2874
more information. CRCs can be used for error detection.
2875
 
2876
Usage:
2877
 
2878
  : s1 $" hello" count ;
2879
  hex x crc . ( prints 'D26E' )
2880
 
2881
; ==== create  ( -- ;  : make 'created' word ) =========
2882
'create' is a very useful word that allows the creation of
2883
new words. It parses the next space delimited string from the
2884
input stream and creates a word which when run returns a
2885
pointer to its code field. It can be used to make words which
2886
create new words, and 'does>' can be used to give those new
2887
words different actions. The 'create'/'does>' combination is
2888
a really important concept in Forth and has been called 'The
2889
Pearl of Forth'.
2890
 
2891
Usage:
2892
 
2893
  create x 2 cells allot
2894
  : constant create , does> @ ;
2895
  4 constant y ( a new constant called 'y' has been made )
2896
 
2897
; ==== decimal  ( -- : switch radix to decimal ) ===============
2898
'decimal' switches the current output radix into decimal as
2899
the name suggests. This can be useful when you are lost and do
2900
not know what the current output radix is, or for performing
2901
calculations like a human instead of a machine! Hexadecimal
2902
values can still be input by prefixing the number with '$'.
2903
Negative numbers will be printed with a leading '-', unlike
2904
in other output bases, when a '.' is used to print the number.
2905
 
2906
 
2907
 
2908
 
2909
 
2910
 
2911
 
2912
 
2913
; ==== dm+  ( a u -- u : print out section of memory ) =========
2914
'dm+' prints out a section of memory in numeric forth, it
2915
prints out the contents starting from address 'a' and advancing
2916
for 'u' bytes, rounded down to the nearest aligned cell
2917
location. It is a factor of 'dump' and is useful as a debugging
2918
tool in itself. It will print out the values in the current
2919
output radix, as stored in the 'base' variable. A crude memory
2920
dump can be performed with '0 here dm+'. No newlines are
2921
printed, but spaces are printed between cell values.
2922
 
2923
Usage:
2924
 
2925
  $40 $200 dm+ ( print $200 bytes starting from address $40 )
2926
 
2927
 
2928
 
2929
; ==== dnegate  ( d -- d : negate a double value ) =============
2930
'dnegate' is the double cell number equivalent of 'negate', it
2931
negates a double cell number. Positive numbers become negative
2932
and vice versa. Double cell numbers take up two items on the
2933
stack as the name suggests.
2934
 
2935
 
2936
 
2937
 
2938
 
2939
 
2940
 
2941
 
2942
 
2943
 
2944
 
2945
; ==== doer  ( -- ;  : create a 'doer' word ) ==========
2946
 
2947
 
2948
 
2949
 
2950
 
2951
 
2952
 
2953
 
2954
 
2955
 
2956
 
2957
 
2958
 
2959
 
2960
 
2961
; ==== does>  ( -- : start 'does' section of word ) ============
2962
 
2963
 
2964
 
2965
 
2966
 
2967
 
2968
 
2969
 
2970
 
2971
 
2972
 
2973
 
2974
 
2975
 
2976
 
2977
; ==== drop  ( u -- : drop a value ) ===========================
2978
'drop' simply pops off and discards the topmost value on the
2979
variable stack.  'rdrop' is the return stack equivalent.
2980
 
2981
 
2982
 
2983
 
2984
 
2985
 
2986
 
2987
 
2988
 
2989
 
2990
 
2991
 
2992
 
2993
; ==== dump  ( a u -- : dump a section of memory ) =============
2994
'dump' is a debugging tool that dumps a section of memory
2995
starting at the address 'a' and proceeds for at least 'u'
2996
characters. It dumps the memory contents in whatever the
2997
current output base it, as well as displaying the memory
2998
contents as characters when the characters are printable.
2999
 
3000
Example:
3001
 
3002
 
3003
Example Output (condensed to fit on the screen):
3004
 0:  104C 0 0 0 0 0 E9B 0                     L_______________
3005
 10: 8000 6403 7075 6081 601C 8010 6F04 6576  ___dup_`_`___ove
3006
 20: 72 6181 601C 801A 6906 766E 7265 74      r__a_`___invert_
3007
 30: 6600 601C 8026 2B01 6203 601C 8034 7304  _f_`&__+_b_`4__s
3008
 ( and so on...)
3009
; ==== dup  ( n -- n n : duplicate a value ) ===================
3010
The stack comment explains this word completely, it simply
3011
duplicates the topmost item on the variable stack. This is a
3012
common Forth word that should be memorized - you should not
3013
need to look it up here! Other common stack manipulation words
3014
include 'swap', 'over', 'nip', 'tuck', '2dup', 'drop', and
3015
'2drop' amongst others. If you find keeping track of the stack
3016
difficult, try to rethink and refactor your code, stack
3017
juggling gets easier over time but nothing beats out removing
3018
complex combinations of 'dup', 'swap', 'over' and the like.
3019
 
3020
Usage:
3021
 
3022
  5 dup ( 5 -- 5 5 )
3023
 
3024
 
3025
; ==== ?dup  ( n -- n n | 0 : duplicate value if not zero ) ====
3026
This word duplicates a number if that number is non zero, it
3027
returns just a single zero if the input number was zero. It
3028
finds a lot of use in 'begin...while...repeat' loop constructs,
3029
as well as in other looping constructs.
3030
 
3031
Usage:
3032
 
3033
  : ?dup dup if dup then ;
3034
 
3035
  3 ?dup .s ( 3 -- 3 3 )
3036
 
3037
 
3038
 
3039
 
3040
 
3041
; ==== else  ( -- : if...else...then ) =========================
3042
'else' is a word which forms part of the 'if...else...then'
3043
clause, it can only be used from within a Forth word
3044
definition and should always be used with the matching 'if'
3045
and 'then'.
3046
 
3047
Usage:
3048
 
3049
  : example cr if ." Hello!" else ." Goodbye!" then cr ;
3050
  1 example ( prints 'Hello!' )
3051
 
3052
 
3053
 
3054
 
3055
 
3056
 
3057
; ==== emit  ( c -- : emit a character ) =======================
3058
'emit' emits a single character to the output device, this
3059
device is by default both the UART and the VT100 terminal
3060
emulator can be displayed on a VGA capable display. The board
3061
used has VGA output. The output of this word can be redirected
3062
by words like 'io!', 'stars', 'conceal', 'file', 'console' and
3063
'interactive'.
3064
 
3065
 
3066
 
3067
Usage:
3068
 
3069
  $6b emit ( prints 'k' )
3070
 
3071
 
3072
 
3073
; ==== empty-buffers  ( -- : deallocate block buffer ) =========
3074
'empty-buffers' deallocates any currently loaded block buffers,
3075
and stores and invalid block number in 'blk'. It can be used
3076
to discard any changes made to any currently loaded blocks.
3077
 
3078
 
3079
 
3080
 
3081
 
3082
 
3083
 
3084
 
3085
 
3086
 
3087
 
3088
 
3089
; ==== evaluate  ( a u -- : evaluate block of memory ) =========
3090
'evaluate' takes a string specified by an address 'a' and a
3091
length 'u' and evaluates it as if it were typed in by the
3092
user. 'source-id' is set to '-1' for the duration of this
3093
so code can work out it is being evaluated and not typed in.
3094
 
3095
Usage:
3096
 
3097
  : s1 $" 2 2 + . cr " count ;
3098
  s1 evaluate ( prints '4' )
3099
 
3100
 
3101
 
3102
 
3103
 
3104
 
3105
; ==== execute  ( xt -- : execute an execution token ) =========
3106
'execute' takes an execution token, which represents a callable
3107
function, and it executes that function. Execution tokens are
3108
returned by words like "'" (quote), or by ":noname".
3109
 
3110
 
3111
 
3112
 
3113
 
3114
 
3115
 
3116
 
3117
 
3118
 
3119
 
3120
 
3121
; ==== @execute  ( a -- : execute token at 'a' if non-zero ) ===
3122
'@execute' expects an address of a value containing an
3123
execution token to execute, it retrieves this token from the
3124
address and executes it if the token is a non-zero value. This
3125
is primarily uses for vectored word execution.
3126
 
3127
 
3128
 
3129
 
3130
 
3131
 
3132
 
3133
 
3134
 
3135
 
3136
 
3137
; ==== exit  ( -- : exit from word ) ===========================
3138
This word is an assembly instruction that will be inlined into
3139
a word when it is executed, it will run but have no effect when
3140
executed in command mode, when compiled into a word it will
3141
exit that word definition when it is run by returning from that
3142
word. It is compiled into the end of a word definition by
3143
the ';' word, which terminates a word.
3144
 
3145
 
3146
 
3147
 
3148
Example:
3149
   : foo if $f00f . exit then $1337 . ;
3150
 
3151
   1 foo $1337 ( <-- prints $1337 only )
3152
 
3153
; ==== expect  ( b u -- : accept, but store 'u' in span ) ======
3154
 
3155
 
3156
 
3157
 
3158
 
3159
 
3160
 
3161
 
3162
 
3163
 
3164
 
3165
 
3166
 
3167
 
3168
 
3169
; ==== file  ( -- : file transfer mode ) =======================
3170
 
3171
 
3172
 
3173
 
3174
 
3175
 
3176
 
3177
 
3178
 
3179
 
3180
 
3181
 
3182
 
3183
 
3184
 
3185
; ==== fill  ( b u c -- : fill area of memory with 'c' ) =======
3186
 
3187
 
3188
 
3189
 
3190
 
3191
 
3192
 
3193
 
3194
 
3195
 
3196
 
3197
 
3198
 
3199
 
3200
 
3201
; ==== find  ( a -- pwd 1 | pwd -1 | a 0 : find word ) =========
3202
 
3203
 
3204
 
3205
 
3206
 
3207
 
3208
 
3209
 
3210
 
3211
 
3212
 
3213
 
3214
 
3215
 
3216
 
3217
; ==== flash  ( -- : add flash words to search order ) =========
3218
The flash related word set requires its own glossary and
3219
description. It is for manipulating the onboard memory, not
3220
just the Flash chip on the Nexys3 but the SRAM as well (as
3221
both devices share data, address and most control lines).
3222
 
3223
The startup sequences loads blocks from Flash to SRAM, the
3224
SRAM can be modified freely, but committing data to Flash is
3225
a manual process as erasing and writing to Flash too many times
3226
can cause it to wear out.
3227
 
3228
The source code should be consulted for a list of the memory
3229
related words, their usage and other such information.
3230
 
3231
 
3232
 
3233
; ==== flush  ( -- : write dirty blocks, empty-buffers also ) ==
3234
 
3235
 
3236
 
3237
 
3238
 
3239
 
3240
 
3241
 
3242
 
3243
 
3244
 
3245
 
3246
 
3247
 
3248
 
3249
; ==== for  ( -- : part of for...next loop ) ===================
3250
 
3251
 
3252
 
3253
 
3254
 
3255
 
3256
 
3257
 
3258
 
3259
 
3260
 
3261
 
3262
 
3263
 
3264
 
3265
; ==== forth  ( -- : set search order to the default ) =========
3266
 
3267
 
3268
 
3269
 
3270
 
3271
 
3272
 
3273
 
3274
 
3275
 
3276
 
3277
 
3278
 
3279
 
3280
 
3281
; ==== forth-wordlist  ( -- voc : return Forth vocabulary ) ====
3282
 
3283
 
3284
 
3285
 
3286
 
3287
 
3288
 
3289
 
3290
 
3291
 
3292
 
3293
 
3294
 
3295
 
3296
 
3297
; ==== get-order  ( -- vn...v0 u : get search order ) ==========
3298
'get-order' is the counter part to 'set-order', it takes a
3299
variable number of arguments off of the stack, denoted by 'u',
3300
which each represent a word list. It used for inspecting the
3301
current search order in use by the interpreter.
3302
 
3303
 
3304
 
3305
 
3306
 
3307
 
3308
 
3309
 
3310
 
3311
 
3312
 
3313
; ==== here  ( -- a : return dictionary address ) ==============
3314
'here' returns the current address of where the dictionary
3315
pointer is, new words will be compiled at this address. The
3316
dictionary pointer is updated by compiling words and numbers
3317
into the dictionary, words like ',' and 'compile,'.
3318
 
3319
 
3320
 
3321
 
3322
 
3323
 
3324
 
3325
 
3326
 
3327
 
3328
 
3329
; ==== hex  ( -- : set numeric radix to hexadecimal ) ==========
3330
This sets the current input output radix to hexadecimal, which
3331
is the default radix for numbers in this Forth. If operating
3332
in other bases, or to make it clear which base you mean, a
3333
number can be prefixed with '$' to force the input base to
3334
hex for that number. This affects all number conversions
3335
globally as it sets the 'base' variable to '$10'. It is
3336
equivalent to '$10 base !'.
3337
 
3338
Usage:
3339
 
3340
  hex 9999 1111 + .     ( prints 'AAAA' )
3341
  decimal 9999 1111 + . ( prints '11110' )
3342
 
3343
 
3344
 
3345
; ==== hi  ( -- : initialize and login to system ) =============
3346
'hi' initializes the system and calls the routine that prints
3347
the login prompt.
3348
 
3349
 
3350
 
3351
 
3352
 
3353
 
3354
 
3355
 
3356
 
3357
 
3358
 
3359
 
3360
 
3361
; ==== hld ( -- a : hold count for pictured numeric output ) ===
3362
'hld' is a variable which contains the current hold count for
3363
the pictured numeric output. As characters are added to the
3364
output buffer with the word 'hold', this value is increment. It
3365
is reset by '<#'. The value for 'hld' may actually be a count
3366
or a pointer.
3367
 
3368
 
3369
 
3370
 
3371
 
3372
 
3373
 
3374
 
3375
 
3376
 
3377
; ==== hold  ( c -- : put byte into numeric output string ) ====
3378
'hold' puts a byte into the hold buffer, which is used by the
3379
Pictured Numeric Output (PNO) routines as a temporary buffer to
3380
create a string that represents the number to be output. The
3381
PNO related routines include; 'hold', 'hld', '<#', '#', '#s',
3382
and '#>'. They are used by words like '.' and 'u.'. The
3383
hold buffer is not that large, at has a minimum size of eighty
3384
characters, so should not be used for large strings.
3385
 
3386
Usage:
3387
 
3388
  : (u.u) <# # # [char] . hold # # #> ;
3389
  : u.u base @ >r hex (u.u) type r> base ! ;
3390
  $1234 u.u ( prints '12.34' )
3391
 
3392
 
3393
; ==== ien  ( f -- f : set interrupt flag ) ====================
3394
 
3395
 
3396
 
3397
 
3398
 
3399
 
3400
 
3401
 
3402
 
3403
 
3404
 
3405
 
3406
 
3407
 
3408
 
3409
; ==== ien?  ( -- f : are interrupts enabled? ) ================
3410
 
3411
 
3412
 
3413
 
3414
 
3415
 
3416
 
3417
 
3418
 
3419
 
3420
 
3421
 
3422
 
3423
 
3424
 
3425
; ==== if  ( -- : part of if...then/if...else...then) ==========
3426
'if' is an immediate word that should only be used within a
3427
word definition. It is used to form 'if...then' and
3428
'if...else...then' clauses for conditional execution of
3429
words. The words 'else' and 'then' should also be consulted.
3430
'if' has differing behavior at compile time and run time,
3431
which is covered in the documentation for the system internals,
3432
however 'if' is just an ordinary Forth word. When run, it
3433
takes a value from the variable stack and jumps to the
3434
corresponding 'else' or 'then' if it is zero, if it is non
3435
zero it continues execution after the 'else'.
3436
 
3437
  : example cr if $" Hello" else $" Bye" then count type cr ;
3438
 
3439
  1 example ( prints 'Bye' )
3440
 
3441
; ==== immediate  ( -- : make last defined word immediate ) ====
3442
'immediate' is an important word that makes the most recently
3443
defined word 'immediate', that is it will be executed even
3444
when in compile mode. All control structure words are
3445
immediate words such as 'if', and 'for'.
3446
 
3447
 
3448
 
3449
 
3450
 
3451
 
3452
 
3453
 
3454
 
3455
 
3456
 
3457
; ==== >in  ( -- a : index into parse input at address ) =======
3458
 
3459
 
3460
 
3461
 
3462
 
3463
 
3464
 
3465
 
3466
 
3467
 
3468
 
3469
 
3470
 
3471
 
3472
 
3473
; ==== index  ( k1 k2 -- : print first line of block range ) ===
3474
'index' displays the first line in a range of blocks, if the
3475
blocks are formatted correctly with the first line containing a
3476
description of the block contents then this works to display an
3477
index of the blocks.
3478
 
3479
 
3480
 
3481
 
3482
 
3483
 
3484
 
3485
 
3486
 
3487
 
3488
 
3489
; ==== interactive  ( -- : interactive I/O mode ) ==============
3490
 
3491
 
3492
 
3493
 
3494
 
3495
 
3496
 
3497
 
3498
 
3499
 
3500
 
3501
 
3502
 
3503
 
3504
 
3505
; ==== interpret  ( ??? a -- ??? : interpret word/number ) =====
3506
 
3507
 
3508
 
3509
 
3510
 
3511
 
3512
 
3513
 
3514
 
3515
 
3516
 
3517
 
3518
 
3519
 
3520
 
3521
; ==== invert  ( u -- u : bitwise invert ) =====================
3522
'invert' performs a bitwise invert on a number. For a
3523
twos compliment negation see 'negate'.
3524
 
3525
 
3526
Usage:
3527
 
3528
  hex
3529
 
3530
     4 invert . ( prints 'FFFB' )
3531
  FFFF invert . ( prints '0' )
3532
  AAAA invert . ( prints '5555' )
3533
 
3534
 
3535
 
3536
 
3537
; ==== io!  ( -- : initialize IO channels ) ====================
3538
'io!' initializes the I/O devices and sets the input and output
3539
devices to their default values (read from UART and PS/2
3540
keyboard, write to the VT100 display, and to UART - both at
3541
the same time). It also disables the interrupts, and sets the
3542
timer to a count but not generate interrupts.
3543
 
3544
 
3545
 
3546
 
3547
 
3548
Example usage:
3549
 
3550
   io! ( ...that's not a very good demonstration... )
3551
 
3552
 
3553
; ==== key  ( -- c : block until character read in ) ===========
3554
This word blocks until a character has been read in from the
3555
current input device (or devices). The input devices can be
3556
either be the UART or the PS/2 Keyboard. The input devices can
3557
be changed with the 'io!', 'console', 'conceal', 'interactive'
3558
and 'file' words.
3559
 
3560
 
3561
 
3562
 
3563
 
3564
 
3565
 
3566
 
3567
 
3568
 
3569
; ==== key?  ( -- c -1 | 0 : non-blocking character read ) =====
3570
This is a non-blocking version of 'key', it returns a
3571
character and true (-1) if a new character has come through
3572
from input devices, and only 0 if there is no new character.
3573
The input devices can be either a UART or a PS/2 keyboard, and
3574
this can be changed with the 'io!', 'console', 'conceal', 'file'
3575
and 'interactive' words.
3576
 
3577
 
3578
 
3579
 
3580
 
3581
 
3582
 
3583
 
3584
 
3585
; ==== last  ( -- pwd : last defined word ) ====================
3586
 
3587
 
3588
 
3589
 
3590
 
3591
 
3592
 
3593
 
3594
 
3595
 
3596
 
3597
 
3598
 
3599
 
3600
 
3601
; ==== led!  ( u -- : write value to LEDs ) ====================
3602
'led!' sets the LEDs on the board next to the switches. There
3603
are eight switches, and the lowest eight bits of the input value
3604
are used to set the switches, 1 is on, 0 is off.
3605
 
3606
 
3607
 
3608
 
3609
 
3610
 
3611
 
3612
 
3613
 
3614
 
3615
 
3616
 
3617
; ==== list  ( k -- : list block ) =============================
3618
The word list loads block 'k' from disk and display the contents
3619
of this block on the screen. It displays the block as 16 lines
3620
with a column width of 64. The display format is controlled by
3621
the 'border' variable, if true it 'list' displays the line
3622
number and a border around the block, if false, then it just
3623
display the block line by line without formatting. It is true
3624
by default. 'list' will also call 'page' before it displays
3625
the block if 'border' contains a non-zero value.
3626
 
3627
Usage:
3628
 
3629
   1 list
3630
   2 list
3631
   ( ..ad infinitum )
3632
 
3633
; ==== literal  ( n -- : compile literal into dictionary ) =====
3634
This is an immediate word that compiles a number into the
3635
dictionary. A number takes up between one and two cells in the
3636
dictionary (negative numbers take up two space, positive only
3637
one). It is a compile only word.
3638
 
3639
Usage:
3640
 
3641
  : example [ 2 2 + ] literal ;
3642
  example . ( prints '4' )
3643
 
3644
 
3645
 
3646
 
3647
 
3648
 
3649
; ==== load  ( k -- : load and execute block ) =================
3650
'load' retrieves a block using 'block', and evaluates that
3651
block, executing whatever is in that block as Forth code,
3652
'source-id' will return -1 for the duration of this evaluation,
3653
the input source will be returned to whatever it was before
3654
the evaluation took place, such as keyboard or UART input,
3655
once the evaluation has finished. 'load' is used in the
3656
word 'thru' to do the evaluation of successive blocks off
3657
mass storage.
3658
 
3659
 
3660
 
3661
 
3662
 
3663
 
3664
 
3665
; ==== loaded  ( -- a : variable, true if boot block loaded ) ==
3666
This is a variable which can be set by the user if the loading
3667
of the boot block was successful, and if the error messages
3668
exists on disk. The error handling words in QUIT inspect this
3669
variable to work out if it can lookup error messages and print
3670
them, or if it should just print out the error number.
3671
 
3672
 
3673
 
3674
 
3675
 
3676
 
3677
 
3678
 
3679
 
3680
 
3681
; ==== lshift  ( u1 u2 -- u3: logical left shift u1 by u2 ) ====
3682
This performs a logical left shift on 'u1' by 'u2'.
3683
 
3684
 
3685
 
3686
 
3687
 
3688
 
3689
 
3690
 
3691
 
3692
 
3693
 
3694
 
3695
 
3696
 
3697
; ==== make  ( -- : make part of make/doer words ) =============
3698
'make' is a state aware word that operates on words made with
3699
'doer', it should not be used with other words.
3700
 
3701
 
3702
 
3703
 
3704
 
3705
 
3706
 
3707
 
3708
 
3709
 
3710
 
3711
 
3712
 
3713
; ==== max  ( n1 n2 -- n3: maximum of two numbers ) ============
3714
This returns the maximum of two signed numbers. This is a
3715
signed operation! Some Forths define an unsigned version
3716
called 'umax'. See 'min' for the minimum of two signed
3717
numbers.
3718
 
3719
 
3720
 
3721
 
3722
 
3723
 
3724
 
3725
 
3726
 
3727
 
3728
 
3729
; ==== message  ( u -- : type line from block ) ================
3730
'message' prints a line from a block, a block is 1024 bytes
3731
long, and there are 16 lines of 64 bytes per block. 'u'
3732
specifies a line number, starting at block 0, which contains
3733
lines 0-15, block 1 has lines 16-31 and so on. Lines are
3734
printing omitting any trailing spaces and non-ASCII characters
3735
are replaced with another character (an underscore, '_').
3736
 
3737
This word is useful in printing error messages, the interpreter
3738
maps error codes into line numbers. The error messages start
3739
in block 2 and end in block 5, with one message per line.
3740
 
3741
Usage:
3742
 
3743
  $21 message ( print line 33, or the second line in block 3 )
3744
 
3745
; ==== min  ( n1 n2 -- n3: minimum of two numbers ) ============
3746
This returns the minimum of two signed numbers. 'umin' is not
3747
defined but it is enough to create, it provides an analogue
3748
for unsigned numbers. The word is simple enough to use.
3749
 
3750
  :  min 2dup  < if drop else nip then ; ( n n -- n )
3751
  : umin 2dup u< if drop else nip then ; ( n n -- n )
3752
 
3753
Usage:
3754
 
3755
   3 4 min .    ( prints 3 )
3756
  -3 4 min .    ( prints -3 )
3757
  65533 4 min . ( prints -3, 65533 is -3 on a 16-bit system )
3758
  65533 4 umin . ( prints 4 )
3759
 
3760
 
3761
; ==== m/mod  ( d n -- r q : signed divide / modulo ) ==========
3762
 
3763
 
3764
 
3765
 
3766
 
3767
 
3768
 
3769
 
3770
 
3771
 
3772
 
3773
 
3774
 
3775
 
3776
 
3777
; ==== mod  ( n1 n2 -- r : remainder of n1 divided by n2 ) =====
3778
 
3779
 
3780
 
3781
 
3782
 
3783
 
3784
 
3785
 
3786
 
3787
 
3788
 
3789
 
3790
 
3791
 
3792
 
3793
; ==== /mod  ( n1 n2 -- r q : remainder/quotient ) =============
3794
'/mod' divides 'n1' by 'n2' and returns the remainder ('r')
3795
and the quotient ('q') of that operation. Given '/mod', the
3796
definitions of 'mod' and '/' naturally follow. They are
3797
actually all implemented in the far more useful 'um/mod' word.
3798
 
3799
 
3800
 
3801
 
3802
 
3803
 
3804
 
3805
 
3806
 
3807
 
3808
 
3809
; ==== ms  ( u -- : wait for 'u' milliseconds ) ================
3810
'ms' waits for 'u' milliseconds with a busy loop. It is
3811
predicated on the system running at 100MHz and will need to be
3812
modified if the system frequency changes.
3813
 
3814
 
3815
 
3816
 
3817
 
3818
 
3819
Usage:
3820
   : 1s 1000 ms ; ( -- : wait for a second )
3821
   : toggle-leds 0 begin invert dup led! 1s again ; ( -- )
3822
 
3823
 
3824
 
3825
; ==== negate  ( n -- n : negate a number ) ====================
3826
'negate' performs the negation of a number into twos compliment
3827
format, positive numbers become negative and negative numbers
3828
become positive (with the exclusion of $8000, which stays the
3829
same).
3830
 
3831
 
3832
 
3833
Usage:
3834
 
3835
  decimal
3836
   1 negate . ( '-1' )
3837
 
3838
  -4 negate . ( '4' )
3839
 
3840
 
3841
; ==== next  ( -- : part of for...next loop ) ==================
3842
 
3843
 
3844
 
3845
 
3846
 
3847
 
3848
 
3849
 
3850
 
3851
 
3852
 
3853
 
3854
 
3855
 
3856
 
3857
; ==== nip  ( n1 n2 -- n1 : remove second value on stack ) =====
3858
The stack comment describes this word perfectly, 'nip' removes
3859
the secondmost item on the stack, leaving the topmost intact.
3860
 
3861
 
3862
 
3863
 
3864
 
3865
 
3866
 
3867
 
3868
 
3869
 
3870
 
3871
 
3872
 
3873
; ==== :noname  ( -- xt : start anonymous function ) ===========
3874
The ':noname' word is an immediate word that allows the
3875
creation of words that do not have names, or an anonymous
3876
function. The word can be executed with the 'execute' function.
3877
 
3878
 
3879
 
3880
Usage:
3881
 
3882
  :noname 2 2 + ; execute . ( prints '4' )
3883
  : (;) ' rdrop compile, 0 state ! ;
3884
  : compose >r >r :noname r> compile, r> compile, (;) ;
3885
  : x 2 2 ;
3886
  : y + . ;
3887
  ' x ' y compose execute ( prints '4' )
3888
 
3889
; ==== number?  ( b u -- n f : is string a number ) ============
3890
 
3891
 
3892
 
3893
 
3894
 
3895
 
3896
 
3897
 
3898
 
3899
 
3900
 
3901
 
3902
 
3903
 
3904
 
3905
; ==== .ok  ( -- : print okay prompt ) =========================
3906
'.ok' prints out 'ok' after the successful execution of an
3907
input line when the Forth interpreter is in the command mode,
3908
the prompt is suppressed when in compile mode.
3909
 
3910
 
3911
 
3912
 
3913
 
3914
 
3915
 
3916
 
3917
 
3918
 
3919
 
3920
 
3921
; ==== or  ( u1 u2 -- u3: bitwise or of two numbers ) ==========
3922
'or' performs the bitwise 'or' of two numbers.
3923
 
3924
 
3925
 
3926
 
3927
 
3928
 
3929
 
3930
 
3931
 
3932
 
3933
 
3934
 
3935
 
3936
 
3937
; ==== over  ( n1 n2 -- n1 n2 n1 : duplicate over ) ============
3938
'over' duplicates the second most item on the variable stack.
3939
Its stack comment describes its workings completely.
3940
 
3941
 
3942
 
3943
 
3944
 
3945
 
3946
 
3947
Usage:
3948
 
3949
  1 2 over .s
3950
  1 2 1
3951
 
3952
 
3953
; ==== pack$  ( b u a -- a : pack string into address ) ========
3954
'pack$' is a useful word when dealing with short Forth
3955
strings, it is used by the interpreter for packing the word
3956
name into the words header. It takes a string specified by
3957
'b u', 'b' being an address of a string, and 'u' beings its
3958
length, and copies the string starting at address 'a', the
3959
first byte of the new string is the length of the string, so
3960
it requires an extra byte be available at 'a'. It then returns
3961
a pointer to this newly packed, counted string.
3962
 
3963
 
3964
 
3965
 
3966
 
3967
 
3968
 
3969
; ==== pad  ( -- a : return address into pad area ) ============
3970
'pad' returns a pointer into the pad area, which begins at
3971
least 80 bytes after the end of the dictionary. The 'pad'
3972
pointer moves when the dictionary pointer moves. It is a useful
3973
area for temporary storage, but should not be used too much.
3974
Some words, such as the Pictured Numeric Output (PNO) words,
3975
use the area just before the pad area.
3976
 
3977
Usage:
3978
 
3979
  3 pad !
3980
  4 pad cell+ !
3981
  pad @ pad cell+ @ + . ( prints '7' )
3982
 
3983
 
3984
 
3985
; ==== page  ( -- : clear page ) ===============================
3986
'page' clears the screen, it does this by emitting the ANSI
3987
terminal escape sequence codes for erasing the display and
3988
moving the cursor position to the top left most cell.
3989
 
3990
 
3991
 
3992
 
3993
 
3994
 
3995
Usage:
3996
 
3997
  page ( <- That's about it, not much to see )
3998
 
3999
 
4000
 
4001
; ==== parse  ( b u c -- b u delta : parse string out ) ========
4002
 
4003
 
4004
 
4005
 
4006
 
4007
 
4008
 
4009
 
4010
 
4011
 
4012
 
4013
 
4014
 
4015
 
4016
 
4017
; ==== pick  ( un...u0 u -- un...u0 u : pick value ) ===========
4018
'pick' selects an item on the stack at an arbitrary depth 'u'
4019
and pushes a copy onto the variable stack. This word should be
4020
used sparingly and it would be better if it was not used at
4021
all due to the way it is implemented and the limited stack size
4022
available to the H2 processor (32-64 cells deep on both stacks,
4023
plus one for the variable stack). The implementation shuffles
4024
data from the variable stack to the return stack, so it is
4025
quite easy to overflow the return stack for valid but high
4026
values of 'u'.
4027
 
4028
Usage:
4029
 
4030
  22 33 44 0 pick . ( prints '44' )
4031
  22 33 44 1 pick . ( prints '33' )
4032
  22 33 44 2 pick . ( prints '22' )
4033
; ==== query  ( -- : get line of input ) =======================
4034
'query' gets a new line of input from the current input device,
4035
and places that line in the Terminal Input Buffer, which can
4036
be accessed by 'source'.
4037
 
4038
 
4039
 
4040
 
4041
 
4042
 
4043
 
4044
 
4045
 
4046
 
4047
 
4048
 
4049
; ==== quit  ( -- : interpreter loop ) =========================
4050
For some reason the interpreter loop is evoked by the word
4051
'quit', which is what it is traditionally called. This is not
4052
a good name, but nonetheless is the standard name for it. The
4053
interpreter loop fetches a line of input, parses a word,
4054
evaluates it, catches/displays any errors and flushes the
4055
current line if there are any, and continues to do this until
4056
the current line contains no new space delimited words/numbers,
4057
whereupon it fetches a new line and repeats the process again.
4058
 
4059
Usage:
4060
 
4061
  quit ( <- invokes another interpreter loop... )
4062
 
4063
 
4064
 
4065
; ==== >r  ( u --, R: -- u : move value to return stack ) ======
4066
'>r', also known as 'to r' moves a variable from the variable
4067
stack to the return stack, its inverse is 'r>'. This word,
4068
along with all return stack words, should be used carefully as
4069
it is possible to trash the return stack with incorrect use.
4070
The word should also be used only from within a word
4071
definition. The stack comment describes the action of this
4072
word completely.
4073
 
4074
Usage:
4075
 
4076
  : rot >r swap r> swap ;    ( n1 n2 n3 -- n2 n3 n1 )
4077
  : -rot swap >r swap r> ;   ( n1 n2 n3 -- n3 n1 n2 )
4078
 
4079
 
4080
 
4081
; ==== r>  ( -- u, R: u --: move value from return stack ) =====
4082
'r>' also known as 'from r', moves a cell sized value from the
4083
return stack to the variable stack. It should only be used from
4084
within a word definition, and only by those that know what they
4085
are doing. It is possible to crash the interpreter by trashing
4086
the return stack with this word. The stack comment for this
4087
word perfectly describes this word.
4088
 
4089
Usage:
4090
 
4091
  : rot >r swap r> swap ;   ( n1 n2 n3 -- n2 n3 n1 )
4092
  : -rot swap >r swap r> ;  ( n1 n2 n3 -- n3 n1 n2 )
4093
 
4094
 
4095
 
4096
 
4097
; ==== r@  ( -- u : R: u -- u: copy top of return stack ) ======
4098
'r@' is a useful word that copies the topmost value on the
4099
return stack, leaving that value in place, and pushes it to
4100
the variable stack.  This word is useful when juggling
4101
variables on the stack, it is also used to access the loop
4102
counter in a "for...next" or "for...aft...then...next"
4103
construct.
4104
 
4105
 
4106
 
4107
 
4108
 
4109
 
4110
 
4111
 
4112
 
4113
; ==== random  ( -- u : random number ) ========================
4114
'random' returns a Pseudo Random Number, it uses a CRC algorithm
4115
and the system timer to generate the value.
4116
 
4117
 
4118
 
4119
 
4120
 
4121
 
4122
 
4123
 
4124
 
4125
 
4126
 
4127
 
4128
 
4129
; ==== rdrop  ( --, R: u -- : drop a value from return stack ) =
4130
'rdrop' is the return stack equivalent of 'drop', it discards
4131
a single value from the return stack.
4132
 
4133
Care should be taken when manipulating the return stack
4134
as it can trash it rendering the system inoperable until it
4135
is restarted.
4136
 
4137
 
4138
 
4139
 
4140
 
4141
 
4142
 
4143
 
4144
 
4145
; ==== recurse  ( -- : recursively call current word ) =========
4146
'recurse' is a word that when used within a word definition it
4147
will recursively call the current word. This is not a tail
4148
call, if this word is used incorrectly it will blow up the
4149
return stack. Some Forths use the construct 'recurse exit'
4150
to implement tail calls, reliant on a peephole optimizer
4151
built into 'exit' to change the call into a branch, this Forth
4152
does not do this. This word is necessary as the word currently
4153
being defined has not been linked into the dictionary yet, to
4154
permit previous versions of the word to be used in the new
4155
definition. This word is a compile only word.
4156
 
4157
Usage:
4158
 
4159
  : pick ?dup if swap >r 1- recurse r> swap exit then dup ;
4160
 
4161
; ==== repeat  ( -- : part of begin...while...repeat ) =========
4162
'repeat' terminates a 'begin...while...repeat' loop. It is a
4163
word that should only be used as part of a
4164
'begin...while...repeat' clause, and should only be used
4165
from within a word definition. It is an immediate word. The
4166
words 'begin' and 'while' should also be consulted. 'repeat'
4167
unconditionally jumps back to the 'begin', it is 'while' that
4168
does the test and potentially jumps past this 'repeat' if the
4169
value it pops from variable stack is zero.
4170
 
4171
 
4172
 
4173
 
4174
 
4175
 
4176
 
4177
; ==== rot  ( n1 n2 n3 -- n2 n3 n1 : rotate three items ) ======
4178
Rotate the first three elements on the variable stack. The
4179
stack comment describes the word completely. If you find your
4180
self using this word too much, you need to rethink and refactor
4181
your Forth. 'rot' is a useful word, but it should be used
4182
sparingly. '-rot' reverses the 'rot' operation.
4183
 
4184
 
4185
 
4186
 
4187
 
4188
 
4189
 
4190
 
4191
 
4192
 
4193
; ==== -rot  ( n1 n2 n3 -- n3 n1 n2 : rotate three items ) =====
4194
Rotate the first three elements on the variable stack in the
4195
opposite direction as 'rot'. If you find yourself using this
4196
word too much it might be best to refactor your code.
4197
 
4198
 
4199
 
4200
 
4201
 
4202
 
4203
 
4204
 
4205
 
4206
 
4207
 
4208
 
4209
; ==== rp@  ( -- u : return stack depth ) ======================
4210
'rp@' push the return stack depth onto the variable stack, it
4211
is an assembly instruction.
4212
 
4213
 
4214
 
4215
 
4216
 
4217
 
4218
 
4219
 
4220
 
4221
 
4222
 
4223
 
4224
 
4225
; ==== rpick  ( u -- u, R: un...u0 : pick return stack value ) =
4226
'rpick' picks a value at an arbitrary depth on the return stack
4227
based on the argument given.
4228
 
4229
 
4230
 
4231
 
4232
 
4233
 
4234
 
4235
 
4236
 
4237
 
4238
 
4239
 
4240
 
4241
; ==== rshift  ( u1 u2 -- u3 : logical right shift u1 by u2 ) ==
4242
'rshift' performs a logical right shift of 'u1' by 'u2' places,
4243
it is equivalent to unsigned division by 2 raised to the power
4244
of 'u2'.
4245
 
4246
 
4247
 
4248
 
4249
 
4250
 
4251
 
4252
 
4253
 
4254
 
4255
 
4256
 
4257
; ==== .s  ( -- : print out variable stack ) ===================
4258
This word is used for debugging, it prints out the entire
4259
variable stack up to the current variable stack depth. It uses
4260
the area between the end of the dictionary and the start of the
4261
PAD area. It is not a reentrant function.
4262
 
4263
 
4264
 
4265
 
4266
 
4267
 
4268
 
4269
 
4270
 
4271
 
4272
 
4273
; ==== #s  ( u -- 0 : repeat '#' until 0 ) =====================
4274
This word repeatedly calls '#' on a number until it is zero, it
4275
should only be used within a pictured numeric output construct
4276
and uses the area between the end of the dictionary and the
4277
beginning of the pad area. Characters are held starting from
4278
the pad area working downwards towards the end of the dictionary
4279
space.
4280
 
4281
 
4282
 
4283
 
4284
 
4285
Example:
4286
  : print-number <# #s #> type ;
4287
  1234 print-number ( <-- prints '1234' )
4288
 
4289
; ==== save-buffers  ( -- : save dirty blocks ) ================
4290
'save-buffers' saves any dirty buffers, if there are any, to
4291
disk and sets the block dirty bit to clean (so subsequent calls
4292
to 'save-buffers' will not save blocks to mass storage unless
4293
they have been 'update'd again). You can make a block dirty
4294
by calling 'update' after the block has been loaded, and before
4295
any other blocks have been loaded.
4296
 
4297
Usage:
4298
 
4299
  5 block drop      ( load block 5, do nothing with it )
4300
  save-buffers      ( nothing happens, block 5 is not dirty )
4301
  4 block 99 swap ! ( store '99' in first element of block 4 )
4302
  update            ( mark block 4 as dirty )
4303
  save-buffers      ( block 4 written back to disk )
4304
 
4305
; ==== see  ( -- ;  : decompile word ) =================
4306
'see' is a complex word that can decompile a Forth word, it is
4307
liable to be buggy. 'see' parses the next space delimited word
4308
in the dictionary, and if found attempts to disassemble the
4309
word into instructions and function calls. If not found 'see'
4310
will throw an exception. The results of 'see' are
4311
implementation dependent. Given a very good implementation
4312
of 'see' it would be possible to dispense with the source
4313
code entirely in text form, the binary could be disassembled,
4314
the code modified, and then it could be reintegrated into
4315
dictionary - the idea is purely speculative, comments would
4316
be missing and this version of 'see' is not good enough.
4317
Not many languages have a disassembler built-in!
4318
Usage:
4319
 
4320
  see word
4321
; ==== segments!  ( u -- : display 'u' on LED displays ) =======
4322
Segments writes a single 16-bit value to the four seven segment
4323
displays on the board. This can be used to display a single
4324
cell as a hexadecimal value.
4325
 
4326
 
4327
 
4328
 
4329
 
4330
 
4331
Usage:
4332
 
4333
  $cafe segments!
4334
  $d1ed segments!
4335
  $0001 segments!
4336
 
4337
; ==== set-order  ( vn...v0 n -- : set search order ) ==========
4338
This word sets the search order for the dictionary search. It
4339
accepts a variable number of items off of the stack, 'n' is
4340
the number of those item. A special case for the value of 'n'
4341
exists, when it is '-1' it sets the order to the minimal
4342
search order.
4343
 
4344
 
4345
 
4346
 
4347
 
4348
 
4349
 
4350
 
4351
 
4352
 
4353
; ==== sgr  ( u -- : set ANSI terminal SGR attribute ) =========
4354
'sgr' takes a number and emits it as a 'Select Graphic
4355
Rendition' command, which is a standard ANSI Escape sequence
4356
used for terminal control. SGR commands relate to colors and
4357
how the text is presents, for example numbers $1E to $25 are
4358
used to set the foreground color, $28 to $2F the background
4359
color. $0 is used to reset all of the attributes, $8 is used
4360
to conceal character output, $7 for reverse video. Not all
4361
SGR commands are processed by the VT100 hardware, and if
4362
talking to the device over the UART it depends on the terminal
4363
emulator you are using.
4364
 
4365
Examples:
4366
 
4367
   $1f sgr  ( set foreground color to red )
4368
   $2a sgr  ( set background color to green )
4369
; ==== sign  ( n -- : hold '-' if n negative ) =================
4370
'sign' is a word used in conjunction with the pictured numeric
4371
output routines ('<#', '#s' and '#>'), it holds a '-' is the
4372
signed number it is given is negative.
4373
 
4374
 
4375
 
4376
 
4377
 
4378
 
4379
 
4380
 
4381
 
4382
 
4383
 
4384
 
4385
; ==== source  ( -- a u : get source string ) ==================
4386
'source' retrieves the address and length of the current
4387
Terminal Input Buffer (TIB). This contains the current line
4388
that is being processed, this is the line that is retrieved
4389
by the word 'query'.
4390
 
4391
 
4392
 
4393
 
4394
 
4395
 
4396
 
4397
Usage:
4398
 
4399
  source type ( a simple quine )
4400
 
4401
; ==== source-id  ( -- 0 | -1 : identify input source ) ========
4402
'source-id' is used to identify the input source, whether
4403
the interpreter is reading input from the user (0) or evaluating
4404
text within a string (-1)
4405
 
4406
Example usage:
4407
 
4408
  : test $" source-id . cr " count evaluate ;
4409
  test        ( prints FFFF or -1 )
4410
  source-id . ( prints '0')
4411
 
4412
When a block is evaluated with 'load' -1 is returned by
4413
'source-id' as it is evaluating the block as a string.
4414
 
4415
 
4416
 
4417
; ==== sp@  ( -- u : variable stack depth ) ====================
4418
'sp@' is an assembly instruction which returns the current
4419
depth of the variable stack.
4420
 
4421
 
4422
 
4423
 
4424
 
4425
 
4426
 
4427
 
4428
 
4429
 
4430
 
4431
 
4432
 
4433
; ==== space  ( -- : emit space character ) ====================
4434
'space' emits a single space character to the current output
4435
device. That is all.
4436
 
4437
 
4438
 
4439
 
4440
 
4441
 
4442
 
4443
 
4444
 
4445
 
4446
 
4447
 
4448
 
4449
; ==== spaces  ( n -- : emit 'n' spaces ) ======================
4450
'spaces' emits 'n' number of spaces, if 'n' is greater than
4451
zero. This is a useful word for formatting.
4452
 
4453
 
4454
 
4455
 
4456
Usage:
4457
 
4458
 
4459
  1 spaces  ok
4460
  2 spaces   ok
4461
 -1 spaces ok
4462
 
4463
 
4464
 
4465
; ==== span  ( -- a : character count from expect, variable ) ==
4466
'span' is a variable which contains the latest character count
4467
from 'expect'. Consult 'expect' for more information.
4468
 
4469
 
4470
 
4471
 
4472
 
4473
 
4474
Usage:
4475
 
4476
   4000 20 expect
4477
   span @
4478
 
4479
 
4480
 
4481
; ==== state  ( -- a : state variable ) ========================
4482
'state' is a variable that contains the global state of the
4483
Forth interpreter loop, which can be in either compile or
4484
command mode. 'state' contains -1 for command mode, 0 for
4485
compile mode. It can be set directly to change the interpreter
4486
state, or read to make words change their behavior based on
4487
the state (which is generally not the best thing to do). The
4488
words '[' and ']' can be used to switch the state as well.
4489
 
4490
Usage:
4491
 
4492
  : ] -1 state ! ;
4493
  : [  0 state ! ; immediate
4494
  : example [ 2 2 + ] literal ;
4495
  example . ( prints '4' )
4496
 
4497
; ==== =string  ( b1 u1 b2 u2 -- f : string equality ) =========
4498
This word implement a test for string equality, it compares
4499
two strings specified by two address-length pairs and returns
4500
a boolean that determined whether the strings are equal or
4501
not (-1 for true/equal, 0 for false/not-equal). The word is
4502
unusual as it accepts four parameters on the stack, which is
4503
bad practice for Forth words which is most circumstances should
4504
only accept three parameters. This word is used internally for
4505
doing the dictionary search.
4506
 
4507
Usage:
4508
  : s1 $" Hello " count ;
4509
  : s2 $" Bye! " count ;
4510
  s1 s2 =string . ( prints '0' )
4511
  s1 s1 =string . ( prints '-1', or 'FFFF' )
4512
 
4513
; ==== /string  ( b u1 u2 -- b u : advance string by u2 ) ======
4514
'/string' advances a string 'b u1' by 'u2' characters, if there
4515
are enough characters left to advance the string by. This is
4516
obviously a word that is useful when dealing with strings,
4517
strings in Forth commonly come in two type: counted strings
4518
and address-length based strings. This word deals with the
4519
latter type of string.
4520
 
4521
 
4522
 
4523
 
4524
 
4525
 
4526
 
4527
 
4528
 
4529
; ==== swap  ( u1 u2 -- u2 u1 : swap two variables ) ===========
4530
It swaps two variables on the stack, its stack effect describes
4531
the word perfectly. This is a simple word that will be used
4532
quite a lot, memorize it quickly!
4533
 
4534
 
4535
 
4536
 
4537
 
4538
 
4539
Usage:
4540
   1 2   (     -- 1 2 )
4541
   swap  ( 1 2 -- 2 1 )
4542
 
4543
 
4544
 
4545
; ==== switches  ( -- u : get state of switches ) ==============
4546
'switches' retrieves the state of the two state switches on
4547
the board, the values are already debounced. There are eight
4548
switches which each have a single LED next to them (which can
4549
be controlled with 'led!'). The lowest eight bits are used
4550
for the switch states, 1 for on, 0 for off.
4551
 
4552
Example usage:
4553
   : select 1 swap lshift switches swap and 0= 0= ;
4554
   : sw0 0 select ; : sw1 1 select ; : sw2 2 select ;
4555
   : sw3 3 select ; : sw4 4 select ; : sw5 5 select ;
4556
   : sw6 6 select ; : sw7 7 select ;
4557
   : switches->leds switches led! ;
4558
 
4559
 
4560
 
4561
; ==== then  ( -- : part of if...then or if...else...then ) ====
4562
'then' should only be used inside a word definition and as
4563
part of an 'if...else...then' or 'if...then' sequence, it is
4564
used to terminate both sequences with execution continuing
4565
after 'then'. The words 'if' and 'else' should also be
4566
consulted. An exception may be thrown if this word is used
4567
outside a word definition, or if there is not a corresponding
4568
if. It is an immediate word.
4569
 
4570
Usage:
4571
   : invalid then ; ( <- invalid usage, no matching 'if' )
4572
   then      ( <- invalid usage, outside word definition )
4573
   : example dup if 1+ then ; ( 0 | x -- 0 | x+1 )
4574
   4 example . ( prints '5' )
4575
 
4576
 
4577
; ==== throw  ( ? n -- ?, R: ? -- ? : throw exception ) ========
4578
'throw' along with 'catch' are the Forth exception handling
4579
routines, 'throw' throws an exception if the number provided to
4580
it is non-zero. An exception unwinds the return stack until
4581
a 'catch' is encountered, the word 'quit' contains the exception
4582
handler of last resort. Numbers from -255 to 0 are reserved for
4583
standard Forth errors.
4584
 
4585
 
4586
Usage:
4587
 
4588
   : fail $1000 throw ;
4589
   : partially-executed cr ." Hello " fail ." World!" cr ;
4590
   : catches-throw ' partially-executed catch u. ;
4591
   catches-throw ( prints only 'Hello' then '1000' )
4592
 
4593
; ==== thru  ( k1 k2 -- : load from k1 to k2 ) =================
4594
'thru' calls 'load' on a range of blocks from 'k1' to 'k2'
4595
inclusively. It is used to evaluate a range of blocks and as
4596
a convenient way to load software off of mass storage.
4597
Code and data is stored in Forth blocks, 'thru' is used to load
4598
that data. The word 'index' can be used to give an overview
4599
of blocks, as the convention is to have the first line of the
4600
block as a description.
4601
 
4602
 
4603
 
4604
 
4605
Usage:
4606
 
4607
   8 9 thru ( evaluate blocks 8 and 9 )
4608
 
4609
; ==== timer  ( -- u : get value of timer ) ====================
4610
'timer' retrieves the current value of the hardware time in the
4611
FPGA, this peripheral can be controlled with the 'timer!' word,
4612
which is used to control the timer.
4613
 
4614
 
4615
 
4616
 
4617
 
4618
 
4619
 
4620
 
4621
 
4622
 
4623
 
4624
 
4625
; ==== timer!  ( -- : set timer ) ==============================
4626
'timer!' sets the hardware timer control register, allowing the
4627
timer to be enabled/stopped/reset and setting up the value at
4628
which the timer wraps around at (or triggers an interrupt). The
4629
layout of the timer control register is as follows:
4630
 
4631
  BIT  - MEANING
4632
  15   - Enable Timer (1 = enabled)
4633
  14   - Reset Timer
4634
  13   - Enable Interrupts
4635
  12-0 - Timer Compare Value
4636
 
4637
The word 'timer' can be used to read in the current value of
4638
the timer.
4639
 
4640
 
4641
; ==== tuck  ( n1 n2 -- n2 n1 n2 : tuck value ) ================
4642
'tuck' is a simple word that manipulates the stack, its stack
4643
comment describes what it does. It tucks a variable behind
4644
the second most item on the stack.
4645
 
4646
 
4647
 
4648
 
4649
 
4650
 
4651
Usage:
4652
   5 7  ( -- 5 7 : two example numbers are entered )
4653
   tuck ( 5 7 -- 7 5 7 : tuck does its job )
4654
 
4655
 
4656
 
4657
; ==== type  ( b u -- : print out a string ) ===================
4658
'type' prints out a string, specified by an address 'b' and
4659
a length 'u'. The output goes to the same place as 'emit'
4660
does, which is usually an output device viewable by the
4661
programmer.
4662
 
4663
The examples use the words '$"' and 'count' which should be
4664
consulted as well. '$"' compiles a string literal into the
4665
dictionary as a counted string, 'count' converts this to
4666
an Address-Length pair needed by type.
4667
 
4668
Usage:
4669
   : hello cr $" Hello, World!" count type cr ;
4670
   hello          ( execute newly defined word )
4671
   Hello, World!  ( it prints 'Hello, World! )
4672
 
4673
; ==== u<  ( u1 u2 -- f: unsigned less than ) ==================
4674
Unsigned less than of two numbers.
4675
 
4676
 
4677
 
4678
 
4679
 
4680
 
4681
 
4682
 
4683
 
4684
 
4685
 
4686
 
4687
 
4688
 
4689
; ==== u>  ( u1 u2 -- f: unsigned greater than ) ===============
4690
Unsigned greater than, this is not an assembly instruction.
4691
 
4692
 
4693
 
4694
 
4695
 
4696
 
4697
 
4698
 
4699
 
4700
 
4701
 
4702
 
4703
 
4704
 
4705
; ==== u>=  ( u1 u2 -- f: unsigned greater or equal to ) =======
4706
Is 'u1' greater than or equal to 'u2', this is an unsigned
4707
operation.
4708
 
4709
 
4710
 
4711
 
4712
 
4713
 
4714
 
4715
 
4716
 
4717
 
4718
 
4719
 
4720
 
4721
; ==== u.  ( u -- : print out number in current base ) =========
4722
'u.' prints out an unsigned number in the current output base,
4723
this number uses the area at the end of the dictionary and
4724
the beginning of the PAD area as temporary storage.
4725
 
4726
 
4727
 
4728
 
4729
 
4730
 
4731
 
4732
 
4733
 
4734
 
4735
 
4736
 
4737
; ==== um*  ( u u -- ud : unsigned multiply ) ==================
4738
Unsigned multiply, it takes two single cell numbers and returns
4739
an unsigned double cell number.
4740
 
4741
 
4742
 
4743
 
4744
 
4745
 
4746
 
4747
 
4748
 
4749
 
4750
 
4751
 
4752
 
4753
; ==== um+  ( u u -- u carry : unsigned add with carry ) =======
4754
Unsigned addition with carry, this word is used to implement
4755
the some of the arithmetic operations, although it is not
4756
very useful directly. 'um+' is used directly to implement 'um*'
4757
which in turn is used to implement '*'.
4758
 
4759
 
4760
 
4761
 
4762
 
4763
 
4764
 
4765
 
4766
 
4767
 
4768
 
4769
; ==== um/mod  ( ud u -- ur uq : unsigned modulo divide ) ======
4770
Unsigned division of an unsigned double cell number by a single
4771
cell number and leaves the remainder, 'ur' and the quotient,
4772
'uq'. This is a slow operation as it is not implemented as an
4773
instruction within the H2 CPU. It will throw and exception if
4774
the divisor is zero. The word is used to implement the other
4775
division and modulus operations.
4776
 
4777
 
4778
 
4779
 
4780
 
4781
 
4782
 
4783
 
4784
 
4785
; ==== until  ( -- : begin...until loop ) ======================
4786
'until' is a compile only word that should be used as part of
4787
a 'begin...until' loop within a word definition. It is a
4788
conditional looping mechanism that pops and tests a value at
4789
run time, and jumps back to the corresponding 'begin' if the
4790
value is zero, otherwise execution continues after the 'until'.
4791
 
4792
Usage:
4793
  : invalid until ; ( <- invalid usage, no matching 'begin' )
4794
  until  ( <- invalid usage, used outside a word definition )
4795
  : test begin 1- dup . ?dup 0= until cr ." done " cr ;
4796
  3 test 2 1 0 ( '3 test' prints '2 1 0' )
4797
  done         ( and also prints 'done' )
4798
 
4799
 
4800
 
4801
; ==== update  ( -- : mark last loaded block as dirty ) ========
4802
Marks the last loaded block as being dirty (the last loaded
4803
block block number is stored in the variable 'blk'). A dirty
4804
block will be written to disk on when 'flush', 'save-buffers',
4805
or another block is loaded from disk.
4806
 
4807
Usage:
4808
   5 block      ( block '5' loaded, block address returned )
4809
   swap 99 !    ( store '99' in block buffer )
4810
   update       ( mark block as dirty )
4811
   save-buffers ( write out block buffer back to block '5' )
4812
   ( block '5' now contains '99' in its first byte, which has )
4813
   ( been saved to disk )
4814
 
4815
 
4816
 
4817
; ==== u.r  ( u +n -- : print u right justified by +n ) ========
4818
'u.r' is a general purpose numeric formatting word, it prints
4819
out the number 'u' as an unsigned number in the current output
4820
radix, preceded by '+n' spaces, minus the number of spaces
4821
needed to display 'u' in the current output radix, or right
4822
aligned. This function uses the area between the end of the
4823
dictionary and the beginning of the PAD area as temporary
4824
storage.
4825
 
4826
Usage:
4827
 
4828
  Typed   | <- Result -> |
4829
  4 6 u.r     4 ok
4830
  99 6 u.r    99 ok
4831
  99 5 u.r   99 ok
4832
 
4833
; ==== variable  ( -- ;  : create a new variable ) =====
4834
'variable' parses the next space delimited string from the
4835
input string and makes a new named variable. The variable will
4836
be initialized with zero, although this is not guaranteed to be
4837
the case with all Forths. Variables are just normal words that
4838
push a location onto the dictionary of where the variable is
4839
stored, it can be accessed with the '!' and '@' words, only
4840
one cell of storage space is reserved. The created word is
4841
not an immediate word.
4842
 
4843
 
4844
 
4845
 
4846
 
4847
 
4848
 
4849
; ==== ver  ( -- u : push version information ) ================
4850
Pushes the version number of the eForth implementation. The
4851
format of the version is the major version is stored in the
4852
upper byte and the minor version information is stored in the
4853
lower byte.
4854
 
4855
The minor version number changes on minor updates, the major
4856
version changes when there are major updates in how the
4857
interpreter works that could potentially break code.
4858
 
4859
 
4860
 
4861
 
4862
 
4863
 
4864
 
4865
; ==== while  ( -- : begin...while...repeat loop ) =============
4866
 
4867
 
4868
 
4869
 
4870
 
4871
 
4872
 
4873
 
4874
 
4875
 
4876
 
4877
 
4878
 
4879
 
4880
 
4881
; ==== within  ( u hi lo -- f : is u within a range? ) =========
4882
'within' is used to test with a value is within a range
4883
specified with a maximum and minimum value.
4884
 
4885
 
4886
 
4887
 
4888
 
4889
 
4890
 
4891
 
4892
 
4893
 
4894
 
4895
 
4896
 
4897
; ==== word  ( c -- a;  : parse a word ) ===============
4898
'word' parses the next word in the input stream and returns an
4899
address to a counted string. 'counted strings' have their first
4900
byte as their string length, which can be extracted with the
4901
word 'count'. The delimiter is given as 'c', a special case
4902
exists when 'c' is a space, as all characters up to the ASCII
4903
space are treated as delimiters.
4904
 
4905
 
4906
 
4907
 
4908
 
4909
 
4910
 
4911
 
4912
 
4913
; ==== words  ( -- : list all words in dictionary ) ============
4914
This lists all of the words in the dictionary that are on the
4915
current search order. Each vocabulary is listed with the address
4916
of that vocabulary as a prefix.
4917
 
4918
 
4919
 
4920
 
4921
 
4922
 
4923
 
4924
 
4925
 
4926
 
4927
 
4928
 
4929
; ==== xor  ( u1 u2 -- u : bit wise exclusive or ) =============
4930
Bitwise exclusive or of two numbers, there is nothing more
4931
to this function than that.
4932
 
4933
 
4934
 
4935
 
4936
 
4937
 
4938
 
4939
 
4940
 
4941
 
4942
 
4943
 
4944
 
4945
; ==============================================================
4946
 
4947
 
4948
 
4949
 
4950
 
4951
 
4952
 
4953
 
4954
 
4955
 
4956
 
4957
 
4958
 
4959
 
4960
 
4961
; ==== ASCII Character Table ===================================
4962
The following section presents a database of all of the ASCII
4963
characters, from 0 through to 127 inclusive. The format of the
4964
database is; decimal, octal, hex then binary value, character
4965
(or name) and finally a column with a short description.
4966
Columns are separated by spaces. There is one entry per line,
4967
with the first line of each block containing an index entry
4968
instead of an ASCII character description.
4969
 
4970
 
4971
 
4972
 
4973
 
4974
 
4975
 
4976
 
4977
; ==== ASCII: NUL (0) to SO (14) ===============================
4978
 
4979
  1 001 01 00000001 SOH    Start of Heading
4980
  2 002 02 00000010 STX    Start of Text
4981
  3 003 03 00000011 ETX    End of Text
4982
  4 004 04 00000100 EOT    End of Transmission
4983
  5 005 05 00000101 ENQ    Enquiry
4984
  6 006 06 00000110 ACK    Acknowledgment
4985
  7 007 07 00000111 BEL    Bell
4986
  8 010 08 00001000 BS     Back Space
4987
  9 011 09 00001001 HT     Horizontal Tab
4988
 10 012 0A 00001010 LF     Line Feed
4989
 11 013 0B 00001011 VT     Vertical Tab
4990
 12 014 0C 00001100 FF     Form Feed
4991
 13 015 0D 00001101 CR     Carriage Return
4992
 14 016 0E 00001110 SO     Shift Out / X-On
4993
; ==== ASCII: SI (15) to GS (29) ===============================
4994
 15 017 0F 00001111 SI     Shift In / X-Off
4995
 16 020 10 00010000 DLE    Data Line Escape
4996
 17 021 11 00010001 DC1    Device Control 1 (oft. XON)
4997
 18 022 12 00010010 DC2    Device Control 2
4998
 19 023 13 00010011 DC3    Device Control 3 (oft. XOFF)
4999
 20 024 14 00010100 DC4    Device Control 4
5000
 21 025 15 00010101 NAK    Negative Acknowledgement
5001
 22 026 16 00010110 SYN    Synchronous Idle
5002
 23 027 17 00010111 ETB    End of Transmit Block
5003
 24 030 18 00011000 CAN    Cancel
5004
 25 031 19 00011001 EM     End of Medium
5005
 26 032 1A 00011010 SUB    Substitute
5006
 27 033 1B 00011011 ESC    Escape
5007
 28 034 1C 00011100 FS     File Separator
5008
 29 035 1D 00011101 GS     Group Separator
5009
; ==== ASCII: RS (30) to Comma (44) ============================
5010
 30 036 1E 00011110 RS     Record Separator
5011
 31 037 1F 00011111 US     Unit Separator
5012
 32 040 20 00100000 SPACE  Space
5013
 33 041 21 00100001 !      Exclamation mark
5014
 34 042 22 00100010 "      Double quotes (or speech marks)
5015
 35 043 23 00100011 #      Number
5016
 36 044 24 00100100 $      Dollar
5017
 37 045 25 00100101 %      Procenttecken
5018
 38 046 26 00100110 &      Ampersand
5019
 39 047 27 00100111 '      Single quote
5020
 40 050 28 00101000 (      Open parenthesis (or open bracket)
5021
 41 051 29 00101001 )      Close parenthesis (or close bracket)
5022
 42 052 2A 00101010 *      Asterisk
5023
 43 053 2B 00101011 +      Plus
5024
 44 054 2C 00101100 ,      Comma
5025
; ==== ASCII: Hyphen (45) to Semicolon (59) ====================
5026
 45 055 2D 00101101 -      Hyphen
5027
 46 056 2E 00101110 .      Period, dot or full stop
5028
 47 057 2F 00101111 /      Slash or divide
5029
 48 060 30 00110000 0      Zero
5030
 49 061 31 00110001 1      One
5031
 50 062 32 00110010 2      Two
5032
 51 063 33 00110011 3      Three
5033
 52 064 34 00110100 4      Four
5034
 53 065 35 00110101 5      Five
5035
 54 066 36 00110110 6      Six
5036
 55 067 37 00110111 7      Seven
5037
 56 070 38 00111000 8      Eight
5038
 57 071 39 00111001 9      Nine
5039
 58 072 3A 00111010 :      Colon
5040
 59 073 3B 00111011 ;      Semicolon
5041
; ==== ASCII: Less than (60) to Uppercase J (74) ===============
5042
 60 074 3C 00111100 <      Less than
5043
 61 075 3D 00111101 =      Equals
5044
 62 076 3E 00111110 >      Greater than
5045
 63 077 3F 00111111 ?      Question mark
5046
 64 100 40 01000000 @      At symbol
5047
 65 101 41 01000001 A      Uppercase A
5048
 66 102 42 01000010 B      Uppercase B
5049
 67 103 43 01000011 C      Uppercase C
5050
 68 104 44 01000100 D      Uppercase D
5051
 69 105 45 01000101 E      Uppercase E
5052
 70 106 46 01000110 F      Uppercase F
5053
 71 107 47 01000111 G      Uppercase G
5054
 72 110 48 01001000 H      Uppercase H
5055
 73 111 49 01001001 I      Uppercase I
5056
 74 112 4A 01001010 J      Uppercase J
5057
; ==== ASCII: Uppercase K (75) to Uppercase Y (89) =============
5058
 75 113 4B 01001011 K      Uppercase K
5059
 76 114 4C 01001100 L      Uppercase L
5060
 77 115 4D 01001101 M      Uppercase M
5061
 78 116 4E 01001110 N      Uppercase N
5062
 79 117 4F 01001111 O      Uppercase O
5063
 80 120 50 01010000 P      Uppercase P
5064
 81 121 51 01010001 Q      Uppercase Q
5065
 82 122 52 01010010 R      Uppercase R
5066
 83 123 53 01010011 S      Uppercase S
5067
 84 124 54 01010100 T      Uppercase T
5068
 85 125 55 01010101 U      Uppercase U
5069
 86 126 56 01010110 V      Uppercase V
5070
 87 127 57 01010111 W      Uppercase W
5071
 88 130 58 01011000 X      Uppercase X
5072
 89 131 59 01011001 Y      Uppercase Y
5073
; ==== ASCII: Uppercase Z (90) to Lowercase h (104) ============
5074
 90 132 5A 01011010 Z      Uppercase Z
5075
 91 133 5B 01011011 [      Opening bracket
5076
 92 134 5C 01011100 \      Backslash
5077
 93 135 5D 01011101 ]      Closing bracket
5078
 94 136 5E 01011110 ^      Caret - circumflex
5079
 95 137 5F 01011111 _      Underscore
5080
 96 140 60 01100000 `      Grave accent
5081
 97 141 61 01100001 a      Lowercase a
5082
 98 142 62 01100010 b      Lowercase b
5083
 99 143 63 01100011 c      Lowercase c
5084
100 144 64 01100100 d      Lowercase d
5085
101 145 65 01100101 e      Lowercase e
5086
102 146 66 01100110 f      Lowercase f
5087
103 147 67 01100111 g      Lowercase g
5088
104 150 68 01101000 h      Lowercase h
5089
; ==== ASCII: Lowercase i (105) to Lowercase w (119) ===========
5090
105 151 69 01101001 i      Lowercase i
5091
106 152 6A 01101010 j      Lowercase j
5092
107 153 6B 01101011 k      Lowercase k
5093
108 154 6C 01101100 l      Lowercase l
5094
109 155 6D 01101101 m      Lowercase m
5095
110 156 6E 01101110 n      Lowercase n
5096
111 157 6F 01101111 o      Lowercase o
5097
112 160 70 01110000 p      Lowercase p
5098
113 161 71 01110001 q      Lowercase q
5099
114 162 72 01110010 r      Lowercase r
5100
115 163 73 01110011 s      Lowercase s
5101
116 164 74 01110100 t      Lowercase t
5102
117 165 75 01110101 u      Lowercase u
5103
118 166 76 01110110 v      Lowercase v
5104
119 167 77 01110111 w      Lowercase w
5105
; ==== ASCII: Lowercase x (120) to DEL (127) ===================
5106
120 170 78 01111000 x      Lowercase x
5107
121 171 79 01111001 y      Lowercase y
5108
122 172 7A 01111010 z      Lowercase z
5109
123 173 7B 01111011 {      Opening brace
5110
124 174 7C 01111100 |      Vertical bar
5111
125 175 7D 01111101 }      Closing brace
5112
126 176 7E 01111110 ~      Equivalency sign - tilde
5113
127 177 7F 01111111 DEL    Delete
5114
 
5115
 
5116
 
5117
 
5118
 
5119
 
5120
 
5121
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.