Use Sleep() on Windows.
Need platform specific code for embedded systems.
Check to see is (SLEEP) is implemented.
If not then use old busy loop code.
Fixes #112
ID_CELLS,
/* DELETE-FILE */
ID_FILE_DELETE,
- ID_FILE_FLUSH, /* FLUSH-FILE */
- ID_FILE_RENAME, /* (RENAME-FILE) */
- ID_FILE_RESIZE, /* RESIZE-FILE */
+ ID_FILE_FLUSH, /* FLUSH-FILE */
+ ID_FILE_RENAME, /* (RENAME-FILE) */
+ ID_FILE_RESIZE, /* RESIZE-FILE */
+ ID_SLEEP_P, /* (SLEEP) V2.0.0 */
/* If you add a word here, take away one reserved word below. */
#ifdef PF_SUPPORT_FP
/* Only reserve space if we are adding FP so that we can detect
ID_RESERVED07,
ID_RESERVED08,
ID_RESERVED09,
- ID_RESERVED10,
ID_FP_D_TO_F,
ID_FP_FSTORE,
ID_FP_FTIMES,
endcase;
#endif
+ case ID_SLEEP_P:
+ TOS = sdSleepMillis(TOS);
+ endcase;
+
case ID_SP_FETCH: /* ( -- sp , address of top of stack, sorta ) */
PUSH_TOS;
TOS = (cell_t)STKPTR;
int sdQueryTerminal( void );
void sdTerminalInit( void );
void sdTerminalTerm( void );
+cell_t sdSleepMillis( cell_t msec );
#ifdef __cplusplus
}
#endif
void sdTerminalTerm( void )
{
}
+
+void sdSleepMillis(cell_t /* msec */)
+{
+ // TODO Call some platform specific sleep function here.
+ return PF_ERR_NOT_SUPPORTED;
+}
#endif
CreateDicEntryC( ID_SAVE_FORTH_P, "(SAVE-FORTH)", 0 );
CreateDicEntryC( ID_SCAN, "SCAN", 0 );
CreateDicEntryC( ID_SKIP, "SKIP", 0 );
+ CreateDicEntryC( ID_SLEEP_P, "(SLEEP)", 0 );
CreateDicEntryC( ID_SOURCE, "SOURCE", 0 );
CreateDicEntryC( ID_SOURCE_SET, "SET-SOURCE", 0 );
CreateDicEntryC( ID_SOURCE_ID, "SOURCE-ID", 0 );
tcsetattr(STDIN_FILENO, TCSANOW, &save_termios);
}
}
+
+cell_t sdSleepMillis(cell_t msec)
+{
+ const cell_t kMaxMicros = 500000; /* to be safe, usleep() limit is 1000000 */
+ cell_t micros;
+ cell_t napTime;
+ if (msec < 0) return 0;
+ micros = msec * 1000;
+ while (micros > 0)
+ {
+ napTime = (micros > kMaxMicros) ? kMaxMicros : micros;
+ if (usleep(napTime))
+ {
+ perror("sdSleepMillis: usleep failed");
+ return -1;
+ }
+ micros -= napTime;
+ }
+ return 0;
+}
#include "../pf_all.h"
#include <conio.h>
+#include <synchapi.h> /* for Sleep() */
/* Use console mode I/O so that KEY and ?TERMINAL will work. */
#if defined(WIN32) || defined(__NT__)
void sdTerminalTerm( void )
{
}
+
+cell_t sdSleepMillis(cell_t msec)
+{
+ if (msec < 0) return 0;
+ Sleep((DWORD)msec);
+ return 0;
+}
+
#endif
include? (local) ansilocs.fth
include? { locals.fth
include? fm/mod math.fth
-include? task-misc2.fth misc2.fth
include? [if] condcomp.fth
+include? task-misc2.fth misc2.fth
include? save-input save-input.fth
include? read-line file.fth
include? require require.fth
;
decimal
-create msec-delay 10000 , ( default for SUN )
-: (MSEC) ( #msecs -- )
+create MSEC-DELAY 100000 , \ calibrate this for your system
+: (MSEC.SPIN) ( #msecs -- , busy wait, not accurate )
+ 0 max \ avoid endless loop
0
- do msec-delay @ 0
+ ?do msec-delay @ 0
do loop
loop
;
+: (MSEC) ( millis -- )
+ dup (sleep) \ call system sleep in kernel
+ IF
+ ." (SLEEP) failed or not implemented! Using (MSEC.SPIN)" CR
+ (msec.spin)
+ ELSE
+ drop
+ THEN
+;
+
defer msec
-' (msec) is msec
+
+\ (SLEEP) uses system sleep functions to actually sleep.
+\ Use (MSEC.SPIN) on embedded systems that do not support Win32 Sleep() posix usleep().
+1 (SLEEP) [IF]
+ ." (SLEEP) failed or not implemented! Use (MSEC.SPIN) for MSEC" CR
+ ' (msec.spin) is msec
+[ELSE]
+ ' (msec) is msec
+[THEN]
+
+: MS ( msec -- , sleep, ANS standard )
+ msec
+;
: SHIFT ( val n -- val<<n )
dup 0<
THEN
;
-
variable rand-seed here rand-seed !
: random ( -- random_number )
rand-seed @
-fsigned-char \
-fno-builtin \
-fno-unroll-loops \
- -fno-keep-inline-functions \
-pedantic \
-Wcast-qual \
-Wall \