aboutsummaryrefslogtreecommitdiff
path: root/amforth-6.5/examples/value-variations.frt
diff options
context:
space:
mode:
Diffstat (limited to 'amforth-6.5/examples/value-variations.frt')
-rw-r--r--amforth-6.5/examples/value-variations.frt65
1 files changed, 65 insertions, 0 deletions
diff --git a/amforth-6.5/examples/value-variations.frt b/amforth-6.5/examples/value-variations.frt
new file mode 100644
index 0000000..df7ec91
--- /dev/null
+++ b/amforth-6.5/examples/value-variations.frt
@@ -0,0 +1,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