aboutsummaryrefslogtreecommitdiff
path: root/amforth-6.5/examples/value-variations.frt
blob: df7ec910393324d200cc450b87e752f0b6591753 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
\ This file contains variations of the
\ standard VALUE. in amforth values are
\ stored in EEPROM and occupy 1 cell (2bytes).
\ Calling the name of a value returns this
\ information on the stack. With the command
\ TO this data can be changed. Implementation
\ allows to extend this schema to any data
\ in any memory.

\ First example is a 1byte value in RAM:

\ two helper words,
: c@v @i c@ ;
: c!v @i c! ;

: cvalue ( n "name" -- )
     (value)           \ create a new wordlist entry
     here ,            \ the address for the methods
     postpone c@v      \ method for the read operation 
     postpone c!v      \ method for the write (TO) operation
     here 1 allot !    \ allocate the memory and initialize it
   ;

\ $dead cvalue answer will store only the lower byte
\ of the number ($ad). The upper byte is either ignored
\ (TO) or set to 0


\ a buffered value is a value that tolerates heavy write access
\ by using a RAM cell as a cache.

\ you need to define a trigger, that flushes the cache
\ warm-cache initializes the cache with the stored data
\ (to be called in turnkey and similiar actions)

\ it is a matter of style whether a sequence should be
\ ' vvv method
\   or 
\ method vvv
\ is used. The implementation below goes the first way
\ since parsing words are considered suboptimal by the
\ gurus (they are state smart and less flexible)

\ 2 is a magic number
: @cache 2 + @i @ ;
: !cache 2 + @i ! ;

: flush-cache 1+ dup 2 + @i @ swap @i !e ;
: warm-cache  1+ dup @i @e swap 2 + @i ! ;

: cache-value 
    (value)
    dup ehere dup , dup cell+ to ehere !e
    postpone @cache
    postpone !cache
    here 2 ( 1 cell ) allot dup , ! \ 3 address units, remember?
;

\ \ test case
\ ehere dup . \ keep the eeprom address
\ 42 cache-value c-dp 
\ 17 to c-dp
\ c-dp . dup @e . \ prints 17 and 42
\ ' c-dp flush-cache
\ c-dp . dup @e . \ prints 17 and 17