Fixed broken links
[fifth.git] / doc / index.org
1 #+TITLE: Fifth - virtual machine, operating system, programming language
2 * (document settings) :noexport:
3 ** use dark style for TWBS-HTML exporter
4 #+HTML_HEAD: <link href="https://bootswatch.com/3/darkly/bootstrap.min.css" rel="stylesheet">
5 #+HTML_HEAD: <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
6 #+HTML_HEAD: <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/js/bootstrap.min.js"></script>
7 #+HTML_HEAD: <style type="text/css">
8 #+HTML_HEAD:   footer {background-color: #111 !important;}
9 #+HTML_HEAD:   pre {background-color: #111; color: #ccc;}
10 #+HTML_HEAD: </style>
11
12 * General
13 - This program is free software: you can redistribute it and/or modify
14   it under the terms of the [[https://www.gnu.org/licenses/lgpl.html][GNU Lesser General Public License]] as
15   published by the Free Software Foundation, either version 3 of the
16   License, or (at your option) any later version.
17
18 - Program author:
19   - Svjatoslav Agejenko
20   - Homepage: http://svjatoslav.eu
21   - Email: mailto://svjatoslav@svjatoslav.eu
22
23 - [[http://www.svjatoslav.eu/projects/][Other software projects hosted at svjatoslav.eu]]
24
25 ** Source code
26 - [[http://www2.svjatoslav.eu/gitweb/?p=fifth.git;a=snapshot;h=HEAD;sf=tgz][Download latest snapshot in TAR GZ format]]
27
28 - [[http://www2.svjatoslav.eu/gitweb/?p=fifth.git;a=summary][Browse Git repository online]]
29
30 - Clone Git repository using command:
31   : git clone http://www2.svjatoslav.eu/git/fifth.git
32
33 * Warning
34 Current implementation does not support object oriented
35 programming. While working on Fifth I got lots of cool new ideas that
36 require reimplementation of everything.
37
38 Currently I try to implement those new ideas in the project called
39 [[http://www3.svjatoslav.eu/projects/sixth/][Sixth]]
40
41 System is built many years ago when I was still using DOS as a primary
42 operating system and not updated since.
43 * Introduction
44 Fifth is programming lanquage & operating system, running on [[emulator.html][virtual
45 CPU]], with custom instruction set. It is much like Charles Chunk
46 Moore's Forth, it also uses stack architecture, and many commands are
47 similar. Basically I got familiar with concepts of Forth, and being
48 inspired created my own system.
49
50 - [[file:5TH_ET.txt][Example Fifth source file - text editor]]
51 ** screenshots
52 - [[file:screenshots/start.png]]
53   - Startup screen diplaying Fifth logo and full file list.
54
55 - [[file:screenshots/dictionary.png]]
56   - Sample words defined. Most of the words are commands that can be
57     executed interactively from command line or from file. When
58     executed they can be selectively compiled or interpreted.
59
60 - [[file:screenshots/text editor.png]]
61   - Built in text editor.
62
63 * Installation
64 Just unpack all files, witout altering original directory structure,
65 somewhere in your hard disk. For example:
66 : C:\MISC\FIFTH\....
67 To run fifth you need minimally just 2 files.  emulator itself (
68 EMULATOR.EXE or EMULATOR.COM ), and virtual disk file ( DISK.RAW ).
69
70 Read more about [[files.txt][distribution directory layout]]
71 * Fifth distribution directory tree description
72 After downloading and unpacking the ZIP file you shoud get directory
73 tree similar to this:
74
75 #+BEGIN_VERSE
76 [DOC]                   - Fifth documentation
77   [commands]            - documentation on Fifth built-in commands
78   [modules]             - documentation on additional commands, realized as loadable modules
79   [shots]               - Fifth screenshots
80
81 [imageFile]             - files contained within 'disk.raw', just an extracted form.
82
83 [source]                - source files
84   [emulator]            - emulator source
85   [util]                - utilites
86
87 disk.raw                - Virtual disk file, has filesystem inside.
88 emulator.com            - main executable.
89 #+END_VERSE
90
91 * Software/Hardware/Human requirements
92 ** Software
93 - MS-DOS 6.22, with HIMEM.SYS loaded.
94 - Mouse driver if you have a mouse.
95 - Does work only when CPU is in real mode.
96 - To recompile ASM sources I used FASM (Flat Assembler).
97 - I ran QBasic utilities on QB 4.5 .
98 - VESA support by BIOS, or external driver (UNIVBE).
99 ** Hardware
100 - Minimum CPU 386.
101 - 64 KB free RAM below 640KB,
102 - 2 MB of free XMS.
103 - VESA compatible video card.
104 ** Human
105 - Beginner level Forth knowledge is recommended.
106 - Lots of enthusiasm.
107 * Numbers representation within Fifth
108
109 [[file:numbers.png][file:numbers.png]]
110
111 Because I can define everything, starting from CPU, why not try also
112 alternative and unique number representation ?
113
114 Fifth uses its hexdecimal number representation as primary. Numbers
115 shape is formed by dividing a square into four parts. And manipulating
116 their color (black or white).
117 * Disk file map, and it's data structures
118 Core and high-level boot code is stored outside of the filesystem to
119 allow easy access to it, at early booting time, when filesystem is not
120 yet initialized.
121 ** disk allocation
122 | offset | length | description          |
123 |--------+--------+----------------------|
124 | 0      | ~4 Kb  | Fifth core           |
125 | 4 Kb   | ~32Kb  | high-level boot code |
126 | 37 Kb  | ~65Kb  | FAT                  |
127 | 101Kb  | ~16MB  | filesystem data area |
128 ** FAT entry format:
129 | code | meaning                  |
130 |------+--------------------------|
131 |   -2 | last sector              |
132 |   -1 | empty sector             |
133 | 0 -- | .. pointer to next block |
134 ** file entry format
135 | offset | length | description            |
136 |--------+--------+------------------------|
137 |      0 |      4 | extension              |
138 |      4 |     16 | name                   |
139 |     20 |      4 | entry point            |
140 |     24 |      4 | size                   |
141 |     28 |      4 | last modification time |
142 * Core architecture
143 Fifth core is simply some amount of already compiled into machine code
144 and linked together modules (entries in other words). In compilation
145 process modules is compiled one by one and simply stored on top of
146 already existing and growing core. Separately from core is kept
147 dictionary, this is special list that contain names of compiled
148 modules, variables etc. and they locations in core. Constants use
149 dictionary space only. Random word can be removed from dictionary at
150 any time. Currently dictionary can contain at most 1000 entries.
151 ** Dictionary entry format
152 | offset | length | description           |
153 |--------+--------+-----------------------|
154 |      0 |      4 | 0 &lt; previous entry |
155 |        |        | 0 = last              |
156 |        |        | -1 = empty            |
157 |--------+--------+-----------------------|
158 |      4 |     15 | module name string    |
159 |--------+--------+-----------------------|
160 |     19 |      1 | entry type            |
161 |--------+--------+-----------------------|
162 |     20 |      4 | entry data            |
163
164 Core headers as linked list of module names make up something like
165 dictionary.  When some entry address is needed compiler can quickly
166 run through headers backwards and find needed entry.
167 ** Possible module types
168 | type | description    | "execute" action           |
169 |------+----------------+----------------------------|
170 |    0 | data           | compile "num" instruction  |
171 |      |                | with address to module     |
172 |------+----------------+----------------------------|
173 |    1 | submodule      | compile "call" instruction |
174 |      |                | with address to module     |
175 |------+----------------+----------------------------|
176 |    2 | imm. submodule | immediately call to module |
177 ** Memory map (average)
178 |   <loc> | <size> | <desc>                      |
179 |---------+--------+-----------------------------|
180 |       0 | ~4096  | core                        |
181 | 1500000 | ~32000 | highlevel Fifth boot code   |
182 | 200000h |        | core startup messages area  |
183 | 5200000 |        | end of dynamic memory space |
184 * Virtual machine
185 Using CPU emulator slows it down but I shouldn't now think too mutch
186 about, and waste my time on batteling with problems whitch results on
187 complex design of PC hardware. Also it allows me to use existing DOS
188 and resident drivers services in real mode. So I don't need to deal
189 with hardware too mutch. It also allows me to use all free XMS for
190 flat code & data storage.
191
192 Current emulator emulates 1 CPU. It has 2 stacks, ~50 instructions,
193 and 4GB flat address space (theoretically).  I'm not sure that DOS
194 6.22 that I currently prefer can handle more than 64 MB of RAM. While
195 I tried to keep instructionset simple, I was forced to put in lot of
196 complex instructions to make it's performance acceptable on
197 emulator. On actual silicon ~20 instructions is enaugh (I think).
198
199 Maybe one day similar system will run directly on custom silicon chip :)
200
201
202 CPU has following registers:
203 | IP  | instruction pointer  |
204 | DSP | data stack pointer   |
205 | RSP | return stack pointer |
206
207 Virtual CPU, commands (most of them are avaiable as ordinary commands
208 in programming lanquage):
209
210 #+BEGIN_VERSE
211
212 code    mnemonic        description
213
214 0       nop                     does notheing
215 1       halt            halt CPU ( return to DOS on emulator )
216
217 2       kbd@            ( -- c ) read scancode of pressed or released key.
218                                 Returns 0, if no data avaiable.
219 3       num <dword>     ( -- n ) put immidiate number into datastack
220
221 4       jmp <dword>     jump to specified code
222 5       call <dword>jump to specified code, save return address to
223                                 return stack.
224
225 6       1+                      ( n -- n+1 )
226 7       1-                      ( n -- n-1 )
227
228 8       dup                     ( n -- n n ) duplicate top of data stack
229 9       drop            ( n -- ) drop last element in data stack
230
231 10      if <dword>      ( n -- ) jump to addr if top element was 0
232 11      ret                     jump to code, specified in return stack.
233
234 12      c@                      ( addr -- n ) read byte from memory at specified address
235 13      c!                      ( n addr -- ) store byte to specified memory
236
237 14      push            ( DSTK -> RSTK ) move top of datastack to returnstack
238 15      pop                     ( RSTK -> DSTK ) move top of returnstack to datastack
239
240 16      <unused>
241 17      rot                     ( n1 n2 n3 -- n2 n3 n1) rotate stack elements
242
243 18      disk@           ( FromDiskSect ToMem -- ) read 1KB from disk into RAM
244 19      disk!           ( FromMem ToDiskSect -- ) write 1KB to disk
245
246 20      @                       ( addr -- n ) read 32 bit number from memory
247 21      !                       ( n addr -- ) store 32 bit number to memory
248
249 22      over            ( n1 n2 -- n1 n2 n1 ) self explaining ...
250 23      swap            ( n1 n2 -- n2 n1 ) -,,-
251
252 24      +                       ( n1 n2 -- n1+n2 ) -,,-
253 25      -                       ( n1 n2 -- n1-n2 ) -,,-
254
255 26      *                       ( n1 n2 -- n1*n2 ) -,,-
256 27      /                       ( n1 n2 -- n1/n2 ) -,,-
257
258 28      >                       ( n1 n2 -- result ) is true when n1 > n2
259 29      <                       ( n1 n2 -- result ) is true when n1 < n2
260
261 30      not                     ( n1 -- not_n1 ) logical not
262 31      i                       ( -- n ) copies top of return stack into datastack
263
264 32      cprt@           ( addr -- n ) read one byte from hardware port
265 33      cprt!           ( n addr -- ) store one byte to hardware port
266
267 34      i2                      ( -- n ) like "i" but takes socond top stack element.
268 35      i3                      ( -- n ) like "i" but takes third top stack element.
269
270 36      shl                     ( n amount -- n ) left bit shift
271 37      shr                     ( n amount -- n ) right bit shift
272
273 38      or                      ( n1 n2 -- n ) logical or
274 39      xor                     ( n1 n2 -- n ) exclusive logical or
275
276 40      vidmap          ( addr -- ) copy memory from "addr" to video memory.
277
278 41      mouse@          ( -- x y button ) read mouse coordinates & buttons
279
280 42      vidput          ( addr1 addr2 x y -- ) put image1 into image2, at
281                                 location x, y. Does clipping, so part of a big image
282                                 can be mapped into smaller one.
283
284 43      cmove           ( addr1 addr2 amount ) move memory from addr1 to addr2
285                                 if addr1 is greater than addr2 then count address
286                                 foward while moving, elseway starts from end and
287                                 counts backwards, so no data loss will occure on
288                                 overlapping.
289
290 44      cfill           ( c addr amount -- ) fill memory starting at "addr"
291                                 with "c" bytes.
292
293 45      tvidput         ( addr1 addr2 x y -- ) same as "vidput" but treats
294                                 color 255 in source image as transparent.
295
296 46      depth           ( -- depth ) returns current depth of data stack.
297
298 47      charput         ( colorfg colorbg addrsrc addrdest x y )
299                                 draw character to image buffer located at "addrdest"
300                                 to specified x & y location. Decodes 8 bytes from
301                                 source to bits, used to draw character.
302 #+END_VERSE
303
304 * Fifth source format
305 Fifth uses a different character table and codes than ASCII (still
306 almost similar). I call it FSCII (Fifth Standard Code for Information
307 Interchange) for example space character is not 32 but 255 instead.  I
308 plan to use mainly HEX numbers, and create new characters to represent
309 numeric values. So typical nemric characters "0123..."  is treated
310 like ordinary letters.
311 ** FSCII
312
313 |    DEC | HEX   | function                               |
314 |--------+-------+----------------------------------------|
315 | 0 - 15 | 0 - F | HEX numbers                            |
316 |    252 | FC    | backspace                              |
317 |    253 | FD    | tabulator (TAB)                        |
318 |    254 | FE    | carriage return (CR)                   |
319 |    255 | FF    | space                                  |
320 |   else |       | ordinary characters, same as in ASCII. |
321 * Fifth commands
322 ** Compilation & miscellaneous
323 #+BEGIN_VERSE
324 init    module  ( -- )
325                 First module, control is passed to on startup. Contains
326                 initialization routines. Also it is the last core module.
327                 All new modules on top of it comes as result of executing
328                 external source files.
329
330 head <name>     ( -- )  compiles new dictionary entry without specifying
331                 new module type.
332                 ex: head myentry
333
334 : <name>        ( -- )  creates new code module
335 ;               ( -- )  ends module (immideate)
336                 ex: : hello ." hi there" ;
337
338 const <name>    ( n -- ) defines new constant.
339                 ex: 2147483647 const max
340
341 :i <name>       ( -- ) same as ":" but this module will be executed
342                 immideately even in compile mode.
343                 ex: :i ( 41 scan ;
344
345 create <name>   ( -- ) same as "head" , but specify module type as data.
346                 ex: create LotoResults 5 , 13 , 52 , 12 , 11 , 3 ,
347
348 allot           ( n -- ) allocate n bytes in dictionary.
349                 ex: create MyArray 100 allot
350
351 " <string>"     ( -- ) compile string and its size into core.
352                 ex: create Mystring " This is it's contects"
353
354 str <name> <string>" ( -- ) just shorter way for defining strings.
355                 ex: str Mystring This is it's contenc"
356
357 var <name>      ( -- ) define new 32 bit variable.
358                 ex: var result
359
360 ' <module>      ( -- n ) return memory address of given entry.
361                 ex: ' init
362
363 forget <name>   ( -- ) erases from RAM given entry and all entries what was
364                 defined after it.
365                 ex: forget myprog
366
367 [               ( -- )  set interpret mode (immideate)
368 ]               ( n -- ) set compile mode and compile top stack element
369                 in as literal. Together [ .... ] cobination provides good
370                 way to compute some values only once, at compile time,
371                 rather than every time while program is running.
372                 ex: : calculate - [ 4 MyConst1 + MyConst2 * ] ;
373
374 defer <name>    ( -- ) creates new module, with jump instruction.
375                 Later address where to jump can be modified by "is" command.
376                 This provides method of foward referencing. So you can use
377                 modules what not jet exist.
378 is              ( address1 address2 -- ) address1 - where to jump, address2 -
379                 address of module created by defer command.
380                 ex:     defer dispver
381                         : run dispver ." running ..." ;
382                                ... whatever ...
383                         : (dispver ." Version 9.99 " ;
384                         ' (dispver ' dispver is
385
386                 Now if I type "run" on the screen appears:
387                         Version 9.99 running ...
388
389 asc <char>      ( -- ) reads char ascii code and treats it as literal.
390                 (immideate)
391                 ex: : BreakLine 30 do asc - emit loop ;
392                                  same as:
393                     : BreakLine 30 do    45 emit loop ;
394
395 dyninc          ( handle -- ) execute code in dynamic memory handle.
396                 automatically deallocates it when done.
397
398 include         ( filenumber -- ) execute code in specified file.
399
400 words           ( -- ) display existing blocks in core.
401
402 bye             ( -- ) exit from Fifth
403
404 fkey            ( -- c )
405                 Read one byte from input stream.
406
407 sadd            ( c addr -- )
408                 Add one byte "c" to string located at "addr" and updates
409                 string length.
410
411 scan            ( c -- )
412                 Read input stream and store it to  pad   until it finds  c  .
413                 It ignores all "c" bytes until it finds any non "c" byte.
414                 in other words:
415                                 c  is:  "
416                          input stream:  """"This is test !"aoeu idh
417                                result:  This is test !
418
419                 Is useful for breaking text lines into words.
420
421 skey            ( -- c )
422                 So called safe "fkey". Reads data from input stream
423                 but converts characters with ASCII codes: 9 13 10
424                 to spaces.
425
426 str=str?        ( adr1 adr2 -- result )
427                 Compares string at "adr1" with string at "adr2", returns
428                 true flag if they are equal or false if they are not.
429                 true = -1
430                 false = 0
431
432 find            ( -- addr )
433                 Searches whole dictionary for word in "pad". If found,
434                 returns it address, if not, returns 0.
435
436 execute         ( -- )
437                 Execute word located in "pad". Depending on "mode".
438
439 dta             ( addr -- DataAddr )
440                 Calculates address of dictionary entry data area, from
441                 entry point.
442
443 2num            ( -- num result )
444                 Attempt to convert string located in "pad" into numeric
445                 value. If succeed returns number and true as result.
446                 If not, returns whatever and false as result.
447
448 dadd            ( addr length -- )
449                 Add to dictionary data located at "addr", with specified
450                 length.
451
452 lit             ( n -- )
453                 Act with number depending on "mode". When interpreting,
454                 leaves it in stack.
455
456
457 incmod          ( addr -- )
458                 Add to dictionary data located at "addr"+1 , length is taken
459                 from "addr".
460
461 here            ( -- n )
462                 return "h" contents.
463
464 mode    var     8 bit
465                 Holds input stream parser operation mode.
466                 0 = interpreting
467                 1 = compiling
468
469 pad     var     128 bytes
470                 Holds temprorary strings.
471
472 h       var     32 bit
473                 Pointer to free byte in memory, always at the end of the
474                 dictionary. Each time when something is stored
475                 by "c," command, pointer is incareased.
476
477 lp      var     32 bit
478                 Pointer to last dictionary word. Each time when new word is
479                 compiled or erased by "forget", this pointer is updated.
480
481 modulechk       ( Dstr<filename> -- ) check if module is loaded, if not
482                 immideately load it.
483
484 ne              ( entrydata entrytype -- ) Compile new dictionary entry.
485                 It's name must be in "pad".
486 #+END_VERSE
487 ** Conditionals & control flow
488 #+BEGIN_VERSE
489 if              ( flag -- )   (immideate)
490                 "if 1.. else 2.. then" or
491                 "if 1.. then" construction. Conditional execution.
492                 Performs "1.." if "flag" was true,
493                 elseway performs "2.." if exist. Execution continues after
494                 word "then".
495                 ex: 1 if ." nonzero" else ." zero" then
496
497 >=              ( n1 n2 -- result ) true if (n1 = n2) or (n1 > n2)
498                 ex: 5 3 >= if ." first number is greater or equal" then
499
500 <=              ( n1 n2 -- result ) true if (n1 = n2) or (n1 < n2)
501 =               ( n1 n2 -- result ) true if n1 = n2
502
503 do              ( count -- )  (immideate)
504                 "do .. loop" construction. Performs ".." "count" times.
505                 In every step "count" is decareased until it is 0.
506                 ex: : test 5 do i .d loop ;
507                 result: 4 3 2 1 0
508
509 doexit         ( -- ) exit from "do .. loop"
510
511 for             ( count top -- )   (immideate)
512                 "for .. loop" construction. Performs ".." (top - count)  times.
513                 In every step "count" is incareased until it reaches "top" .
514                 ex: : test 4 10 for i .d loop ;
515                 result: 4 5 6 7 8 9
516
517 forexit         ( -- ) exit from "for .. loop"
518
519 until           ( -- )  (immideate)
520                 "until .. loop" construction. Performs ".." until flag become
521                 true. False by default. Top of return stack holds flag.
522
523 done            ( -- ) exit from "until .. loop"
524
525 #+END_VERSE
526 ** Disk & file access
527 #+BEGIN_VERSE
528 diskload ( FromDisk ToMem amount -- )
529                 Load specified abount of bytes from disk into memory.
530
531 disksave ( FromMem ToDisk amount -- )
532                 save specified abount of bytes from memory into disk.
533
534 format ( -- )   Erase all files.
535
536 fsDfilesize@ ( handle -- size )
537                 Return size of opened file.
538
539 fsDcurloc@ ( handle -- location )
540                 Return current location in file.
541
542 fsDupdated@ ( handle -- updated? )
543                 Return true if file was updated,
544                 ie. write operations occured.
545
546 fssave ( FromMem DestFileHandle amount -- )
547                 Save data to file.
548
549 fsload ( SrcFileHandle ToMem amount -- )
550                 Load data from file.
551
552 fseof ( handle -- bytesLeft )
553                 Return amount of bytes left till end of file.
554                 Useful before read operation.
555
556 fsls ( -- )     List all files and lists (directories,folders)
557                 in current path.
558
559 fslsr ( -- )    Same as "fsls" but recursively scans also sub lists.
560
561 fscl ( DynStrHand -- )
562                 Change list (path)
563
564 fscreate ( DynStrHand -- DescPnt )
565                 Create new file or list. Can create multiple lists at once.
566                 ex: when creating:
567                     "\listGAMES\listSTRATEGY\listSIMWORLD\5th-runme"
568                 and only "\listGAMES\" already exist, then
569                 "listSTRATEGY" and "listSIMWORLD" lists will be created,
570                 and empty file "5th-runme" placed in there.
571
572 fsDsave ( DynHand<data> DynStrHand<filename> -- )
573                 Create new file and save all data from dynamic memory
574                 block to it.
575
576 fsDload ( DynStr<SrcFileName> DynHand<DataDest> -- )
577                 Load whole file into dynamic memory block.
578
579 fsDloadnew ( DynStr<SrcFileName> -- DynHand<DataDest> )
580                 Load whole file into new dynamic memory block.
581 #+END_VERSE
582 ** Dynamic memory
583 #+BEGIN_VERSE
584 dynal ( size -- handle )
585                 Allocate dynamic memory block and return it's handle.
586
587 dynde ( handle -- )
588                 Deallocate dynamic memory block.
589
590 dynp ( handle -- addr )
591                 Returns pointer to memory where dynamic block
592                 data begins.
593
594 dyns ( handle -- size )
595                 Returns size of dynamic block.
596
597 dynresize ( NewSize handle -- )
598                 Nondestructively resize dynamic block.
599
600 dync@ ( addr handle )
601                 Read one byte from dynamic block.
602
603 dync! ( byte addr dynhandle )
604                 Write one byte to dynamic block.
605
606 dyn@ ( addr handle )
607                 Read 32 bit number from dynamic block.
608                 Address will spacify, whitch number, not byte.
609
610 dyn! ( 32BitNum addr dynhandle )
611                 Write 32 bit number to dynamic block.
612                 Address will spacify, whitch number, not byte.
613
614 dyncon ( size "name" -- )
615                 Allocate dynamic block with specified size, and
616                 create constant honding its handle.
617                 ex: 100 dyncon MyNewBlock
618
619 dyn. ( handle -- )
620                 Write contenc of dynamic memory block to screen.
621 #+END_VERSE
622 ** Graphics and text
623 #+BEGIN_VERSE
624 .               ( n -- ) print number on screen
625
626 d.              ( n -- ) print number on screen in decimal
627
628 ?               ( addr -- ) print 32 bit value located at addr.
629
630 ." <string>"    ( -- ) print string into screen. Immideately
631                 compiles.
632                 ex: : greeting ." Hello, World" ;
633
634 tab.            ( -- ) print tabulator
635
636 calccol         ( b g r -- c ) calculate color what best matches given
637                 Blue Green & Red values. Values must be in range 0 - 255.
638
639 imgalloc        ( xsize ysize -- imgbuf ) allocate image buffer for
640                 specified size.
641
642 imgsize         ( imgbuf -- ) print on the screen X & Y size of image
643                 buffer.
644
645 point           ( x y imgbuf -- addr ) returns memory address for specified
646                 pixel.
647
648 pset            ( color x y imgbuf -- ) set graphic point
649
650 boxf            ( x1 x2 y1 y2 imgbuf color -- ) draw filled box
651
652 cls             ( imgbuf -- ) clear image buffer
653
654 setpal          ( b g r color -- ) set palette value for specified color.
655                 values bust be in size 0 - 63.
656
657 putchar         ( char color x y imgbuf -- ) put graphic character in
658                 imagebuffer to specified (x & y) location.
659
660 scroll          ( x y imgbuf -- ) scroll in imgbuf.
661
662 scrollf         ( color x y screen -- )  scroll and fill empty space with
663                 given color.
664
665 at!             ( x y -- ) set cursor location
666 curc!           ( color -- ) set text color
667 curb!           ( solor -- ) set backround color
668
669 colnorm         ( -- ) set text color to normal
670 colneg          ( -- ) set text color to negative (selected)
671
672 dyntype         ( dynhandle -- ) display contenc of dynamic memory on screen
673 fsdisp          ( file -- ) clear screen, display file, and wait for key
674
675 type            ( addr length -- )
676                 Types on the screen string, from memory at  addr  and
677                 specified length.
678
679 write           ( addr -- )
680                 Types on the screen string, from memory at "addr"+1
681                 length is taken from "addr" .
682
683 screen  const   32 bit
684                 Holds handle of screen buffer.
685
686 copyscreen      ( SrcImgHandle DestImgHandle -- ) copy contenc of source
687                 image to destination image. Source and destination images
688                 must have same size.
689 #+END_VERSE
690 ** Math, memory & stack manipulation
691 #+BEGIN_VERSE
692 off             ( n -- ) writes 0 to given address, good for zeroing variable.
693                 ex: MyVariable off
694 on              ( n -- ) writes -1 (true flag) to given address.
695                 ex: MyVariable on
696
697 2dup            ( n1 n2 -- n1 n2 n1 n2 )
698 2drop           ( n1 n2 -- )
699 nip             ( n1 n2 -- n2 )
700 neg             ( n1 -- -n1 ) negotiate
701 bit@            ( n bit -- result ) return specified bit from n.
702                 ex: 38 2 bit@   (result will be 1)
703 to32bit         ( n1 n2 n3 n4 -- n32 ) treat 4 last stack elements as bytes
704                 and unite them into 32 bit dword. Most significant byte
705                 on top.
706                 ex: 12 76 23 11 to32bit   result: 186076172
707
708 to8bit          ( n32 -- n1 n2 n3 n4 ) break 32 bit number into 4 bytes.
709                 Useful if you need to send 32 bit numbers thru 8 bit COM
710                 port.
711                 ex: 186076172 to8bit   result: 12 76 23 11
712
713 mod             ( n1 n2 -- reminder ) divide n1 by n2 and returns reminder.
714                 ex: 12 5 mod   result: 2
715
716 bound           ( low n high -- n ) check if n is in given bounds,
717                 if not then incarease/decarease it to match bounds.
718                 ex: 5 80 15 bound    result: 15
719                     5 10 15 bound    result: 10
720                     5 -10 15 bound   result: 5
721
722 bound?          ( low n high -- result ) returns true if n is in the
723                 given bounds.
724
725 tab             ( col -- spaces) calculate amount of spaces to add
726                 ta reach next tabulation from given column.
727
728 count           ( addr -- addr+1 n )
729                 Useful for returning bytes from constantly incareasing
730                 address. Module "type" is nice example.
731
732 c,              ( n -- )
733                 store one byte at memory specified by "h". And incarease
734                 "h" by 1.
735
736 ,               ( n -- )
737                 store 32 bit number at memory specified by "h". And
738                 incarease "h" by 4.
739
740 cmove           ( addr1 addr2 n -- )
741                 copy "n" amount of bytes from memory at "addr1" to memory
742                 at "addr2".
743
744 rnd             ( limit -- result )
745                 generates random number in range 0 to "limit"-1.
746
747 abs             ( n -- |n| )
748                 returns absolute value of "n"
749 #+END_VERSE
750 ** Dynamic & static strings
751 Fifth supports both static and dynamic strings. Static strings must
752 have predefined space reserved, and string mustn't exceed this
753 length. They manipulation is faster. But they use more memory. Static
754 string memory address is used to refer to the string.
755
756 Dynamic strings can have at any time length form 0 to 0FFh, They take
757 up only memory they currently need. They are held in dynamic memory
758 blocks, so dynamic block handle is used to refer to this string.
759
760 Both types of strings are stored in the way, where first (0th) byte
761 holds current string length, following bytes are string itself.
762
763
764 #+BEGIN_VERSE
765 Dynamic:
766
767 Dstral ( -- handle )
768                 Allocate new string.
769
770 Dstrlen ( handle -- length )
771                 Return string length.
772
773 c+Dstr ( chr handle -- )
774                 Add one byte to end of the string.
775
776 c+lDstr ( chr handle -- )
777                 Add one byte to left side (beginning) of the string.
778
779 Dstr. ( handle -- )
780                 Write contec of string into screen.
781
782 Dstrsure ( size Dstr -- )
783                 Makes sure that at least rquested
784                 "size" (amount of characters) is allocated for given
785                 dynamic string.
786
787 Dstr2str ( handle address -- )
788                 Copy dyamic string into static memory space.
789
790 str2Dstr ( address handle -- )
791                 Copy static string into dyamic string.
792
793 Dstr+str ( Dstr addr -- )
794                 Add contenc of dynamic string to static string.
795
796 D" any string" ( -- Dstr )
797                 Moves specified string into dynamic string called "defDstr".
798
799 D> any_string ( -- Dstr )
800                 Moves specified string into dynamic string called "defDstr".
801                 Space marks end of string!
802
803 D>2 any_string ( -- Dstr )
804                 Moves specified string into dynamic string called "defDstr2".
805                 Space marks end of string!
806
807 Dstr+Dstr ( Dstr1 Dstr2 -- )
808                 Adds "Dstr1" to "Dstr2" and places result into "Dstr2".
809
810 Dstrclear ( Dstr -- )
811                 Clears contenc of dynamic string.
812
813 Dstr2Dstr ( Dstr1 Dstr2 -- )
814                 Moves "Dstr1" to "Dstr2".
815 Dstr ( data" name -- )
816                 Creates new dynamic string and moves specified data into it.
817                 Then creates new constant with given "name" holding created
818                 dynamic string handle.
819
820                 ex: Dstr Hello, my name is Sven!" message      \ creates it
821                     message Dstr.                              \ tests it
822
823 Dstrlscan ( char Dstr -- loc )
824                 Searches dynamic string for "char", from left to right,
825                 returns first found "char" location in string, or 0,
826                 if not found.
827
828 Dstrrscan ( char Dstr -- loc )
829                 Searches dynamic string for "char", from right to left,
830                 returns first found "char" location in string, or 0,
831                 if not found.
832
833 Dstrlscane ( char Dstr -- loc )
834                 Same as "Dstrlscan" buf returns string length+1 as location.
835 ÿ
836 Dstrleft ( amo Dstr -- )
837                 Only specified amount of characters from left remains
838                 in dynamic string. ie. cut right part out.
839
840 Dstrright ( amo Dstr -- )
841                 Only specified amount of characters from right remains
842                 in dynamic string. ie. cut left part out.
843
844 Dstrcutl ( amo Dstr -- )
845                 Cut specified amount of characters from left of dynamic
846                 string out.
847
848 Dstrsp ( char Dstr1 Dstr2 -- )
849                 Separate dynamic string in Dstr1 into two parts,
850                 using "char" as separator. First part will be stored in
851                 "Dstr2", second part in "Dstr1".
852                 ex: asc \                               \ ..separator
853                     D> listF\listLIB\5TH_DRVMOUSE       \ ..separate from
854                     defDstr2                            \ ..place result in
855                     Dstrsp              \ separation command
856                     defDstr Dstr.       \ will be: listLIB\5TH_DRVMOUSE
857                     defDstr2 Dstr.      \ will be: listF
858
859 Dv ( addr -- )
860                 Allocates empty dynamic string, and places it's handle
861                 into given address.
862
863 Df ( addr -- )
864                 Reads dynamic string handle from given address and
865                 deallocates (frees) it.
866
867 ex:     var mystring1
868         : testmodule
869         mystring1 Dv            \ allocates string
870
871                 <whatever>
872
873         mystring1 Df ;          \ deallocates it again when no longer needed.
874 #+END_VERSE
875 * Dynamically loadable modules
876 ** Keyboard driver
877 #+BEGIN_VERSE
878
879 KBD_@           ( -- code ) get scancodes for pressed keys from keyboard.
880 KBD_down?       ( key -- result ) check is key with specified scancode
881                 currently pressed down.
882 KBD_SC2FSCII    ( code -- FSCII ) convert key scancode into FSCII code,
883                 or in FSK (Fifth standard keycode).
884 KBD_F@          ( -- FSCII ) read pressed key FSCII or FSK, returns -1 if no
885                 keys are pressed.
886 KBD_FW@         ( -- FSCII ) read pressed key FSCII or FSK, if no keys is
887                 are pressed then waits until there is.
888
889                 FSK
890                 ---
891 In HEX.
892
893 FC      backspace
894 FD      TAB
895 FE      enter
896 FF      space
897
898 400     ESC
899 401 ... F1 ...
900 410     up
901 411     right
902 412     down
903 413     left
904 414     INS
905 415     DEL
906 416     home
907 417     end
908 418     PG/UP
909 419     PG/DN
910 #+END_VERSE
911 ** Mouse driver
912 #+BEGIN_VERSE
913 mousex  var     Mouse x coordinate.
914 mousey  var     Mouse y coordinate.
915 mousekeyl var   Mouse left key.
916 mousekeym var   Mouse middle key.
917 mousekeyr var   Mouse right key.
918 mousec  var     Display current mouse coordinates in top left part of screen,
919                 if true. (good for debugging)
920 mousepointer var  Image buffer, holding current mouse pointer.
921 mouseadd        ( ModuleAddr x1 x2 y1 y2 -- ) Add specified area on screen,
922                 into mause click buffer. If any mouse button is clicked on
923                 that area, module at "ModuleAddr" will be executed.
924 mousebe var     Amount of buffer elements.
925 mousedo         ( -- ) Updates mouse coordinates and keys. Parse mouse
926                 click buffer, and draw mouse cursor to "screen".
927 #+END_VERSE
928 ** 2D graphic library
929 #+BEGIN_VERSE
930 lineh           ( color len x y imgbuf -- ) draws horisontal line
931                 from X,Y coordinates to right, with specified length.
932 linev           ( color len x y imgbuf -- ) draws vertical line
933                 down, from coordinates X,Y, with specified length.
934 box             ( color x2 x1 y2 y1 imgbuf -- ) draws rectangular
935                 box. x2 bust be >= x1, y2 must be >= y1.
936                         x1,y1-----------+
937                           |             |
938                           |             |
939                           +-----------x2,y2
940
941 flipv           ( imgbuf -- ) flip image vertically.
942 imgcoltrans     ( ImgBuf Color ToColor -- ) Translate all pixels in
943                 specified image with "Color" into "ToColor".
944 imgfill         ( color x y imgbuf -- ) Fill image region starting at location
945                 X & Y with specified color.
946 #+END_VERSE
947 ** Trigonometry functions
948 #+BEGIN_VERSE
949 sin             ( a -- result ) return sinus from given angle "a",
950                 360ø is 2000. So 1000 represents 180ø angle.
951                 Result will be in range -10'000 to 10'000, instead of ñ1.
952
953 cos             ( a -- result ) return cosinus from given angle.
954                 Parameters are like in "sin" function.
955 #+END_VERSE