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