13.12Timers. 4tH 4tH Forth compiler
Publicitate
Publicitate
CHAPTER 13. SPECIAL LIBRARIES 218
Which will return the addresses of the sixth element of "a1" and the eleventh element of "a2" - we’re counting from zero. Consequently, we pass the element number and the address of the array to the ’DOES>’ part of the definition.
That’s were the ’SWAP’ comes in, since that one puts the element number on top, which we multiply with the element size to get the offset of that specific element in the array.
Then we pass the whole bunch to "+PAGING". That’s where the magic begins! If you e.g.
prefer a more FSL-like array, now you know what to do..
The same rules apply here, if you want to write to a variable don’t forget the ampersand:
2960 5 a1 n! & s" This is a virtual memory string" 10 a2 place &
And you can just as easily retrieve it:
5 a1 n@ . 10 a2 count type cr
That’s all! When you’re done, you’ll have to close your blockfile, of course. But that ain’t too tricky: close-blockfile
Will do just fine. Tip of the day: there is also a preprocessor library
13 which supports this virtual memory. Not only is it far more efficient, it also allows for much cleaner code.
13.12
Timers
There is a very low level word in 4tH that keeps track of time. It has several uses. Like a timer that measures how long certain operation takes, like the execution of a colondefinition ("DO-SOME-WORD" in this case): time do-some-word time swap -
." Do-Some-Word took " . ." seconds." cr
There is a somewhat more elaborate library member that does it all for you:
[needs lib/timer.4th] timer-reset do-some-word
.elapsed
This always prints the number of seconds that have elapsed. If you want to create your own display, you can define one easily:
[needs lib/timer.4th]
:noname <# # 6 base ! # decimal 58 hold # #> type ." mins" ; is timer-stop
You define ”TIMER-STOP” after inclusion of ”time.4th”, but before the first usage of
”.ELAPSED”.
13
See section 14.1.
Descarcă
Publicitate