IamRob wrote:
On Facebooks Programming Language 21st Century someone posted this link for accessing FAT16 files in Forth.
I do wish I had gone with FAT16 instead of FAT32. Juggling the 32-bit values around on a 16-bit Forth is a big hassle, and my Linux PC is just as happy to use a FAT16 file system on a compact flash card.
Back to your original topic (what?? that never happens....), I use [ and ] to compute things while compiling (as mentioned already by Garth). I do cycle testing on words as part of TaliForth2's test suite and use the following code. This test code predates Tali2 having an assembler, so you also get to see the pretendo-assembler using C, (note that Tali is an STC Forth, so all words are essentially CODE words and nothing special needs to be done to switch between assembly and regular Forth code):
Code:
hex
\ The location of the result
F008 constant cycles
\ direct byte compiled
\ lda $f006
\ lda $f007
: cycles_overhead [ AD c, 06 c, F0 c, AD c, 07 c, F0 c, ] cycles 2@ ;
\ direct byte compiled
\ lda $F006
\ jsr (xt on stack goes here)
\ lda $f007
\ then forth code to fetch and print results.
: cycle_test_runtime
[ AD c, 06 c, F0 c, \ lda $F006
20 c, 0000 , \ jsr (address to be filled in)
AD c, 07 c, F0 c, ] \ lda $F007
cycles 2@ \ fetch result
cycles_overhead d- \ subtract overhead
." CYCLES: " 6 ud.r \ print results
;
\ cycle_test updates the address of the given xt in cycle_test_runtime
\ then it runs the test.
\ To test a word, put any arguments it needs on the stack, use tick
\ (') on the word to get it's execution token (xt) and then put
\ cycle_test, then any stack cleanup.
\ eg. 5 ' dup cycle_test 2drop
: cycle_test ( xt -- )
[ ' cycle_test_runtime 4 + ] literal ! cycle_test_runtime ;
Here you can see that cycle_test calculates the correct location to stuff the XT into the assembly so it becomes part of the JSR there. The code accesses one address ($F006) to start a (simulated in py65mon) cycle counter, another address ($F007) to stop the counter, and a third address ($F008) to get the result as a 32-bit number.