xref: /freebsd/stand/ficl/i386/sysdep.c (revision 6829dae12bb055451fa467da4589c43bd03b1e64)
1 /*******************************************************************
2 ** s y s d e p . c
3 ** Forth Inspired Command Language
4 ** Author: John Sadler (john_sadler@alum.mit.edu)
5 ** Created: 16 Oct 1997
6 ** Implementations of FICL external interface functions...
7 **
8 *******************************************************************/
9 
10 /* $FreeBSD$ */
11 
12 #ifdef TESTMAIN
13 #include <stdio.h>
14 #include <stdlib.h>
15 #else
16 #include <stand.h>
17 #ifdef __i386__
18 #include <machine/cpufunc.h>
19 #endif
20 #endif
21 #include "ficl.h"
22 
23 /*
24 *******************  FreeBSD  P O R T   B E G I N S   H E R E ******************** Michael Smith
25 */
26 
27 #if PORTABLE_LONGMULDIV == 0
28 DPUNS ficlLongMul(FICL_UNS x, FICL_UNS y)
29 {
30     DPUNS q;
31     uint64_t qx;
32 
33     qx = (uint64_t)x * (uint64_t) y;
34 
35     q.hi = (uint32_t)( qx >> 32 );
36     q.lo = (uint32_t)( qx & 0xFFFFFFFFL);
37 
38     return q;
39 }
40 
41 UNSQR ficlLongDiv(DPUNS q, FICL_UNS y)
42 {
43     UNSQR result;
44     uint64_t qx, qh;
45 
46     qh = q.hi;
47     qx = (qh << 32) | q.lo;
48 
49     result.quot = qx / y;
50     result.rem  = qx % y;
51 
52     return result;
53 }
54 #endif
55 
56 void  ficlTextOut(FICL_VM *pVM, char *msg, int fNewline)
57 {
58     IGNORE(pVM);
59 
60     while(*msg != 0)
61 	putchar((unsigned char)*(msg++));
62     if (fNewline)
63 	putchar('\n');
64 
65    return;
66 }
67 
68 void *ficlMalloc (size_t size)
69 {
70     return malloc(size);
71 }
72 
73 void *ficlRealloc (void *p, size_t size)
74 {
75     return realloc(p, size);
76 }
77 
78 void  ficlFree   (void *p)
79 {
80     free(p);
81 }
82 
83 #ifndef TESTMAIN
84 /*
85  * outb ( port# c -- )
86  * Store a byte to I/O port number port#
87  */
88 void
89 ficlOutb(FICL_VM *pVM)
90 {
91 	u_char c;
92 	uint32_t port;
93 
94 	port=stackPopUNS(pVM->pStack);
95 	c=(u_char)stackPopINT(pVM->pStack);
96 	outb(port,c);
97 }
98 
99 /*
100  * inb ( port# -- c )
101  * Fetch a byte from I/O port number port#
102  */
103 void
104 ficlInb(FICL_VM *pVM)
105 {
106 	u_char c;
107 	uint32_t port;
108 
109 	port=stackPopUNS(pVM->pStack);
110 	c=inb(port);
111 	stackPushINT(pVM->pStack,c);
112 }
113 
114 /*
115  * Glue function to add the appropriate forth words to access x86 special cpu
116  * functionality.
117  */
118 static void ficlCompileCpufunc(FICL_SYSTEM *pSys)
119 {
120     FICL_DICT *dp = pSys->dp;
121     assert (dp);
122 
123     dictAppendWord(dp, "outb",      ficlOutb,       FW_DEFAULT);
124     dictAppendWord(dp, "inb",       ficlInb,        FW_DEFAULT);
125 }
126 
127 FICL_COMPILE_SET(ficlCompileCpufunc);
128 
129 #endif
130 
131 /*
132 ** Stub function for dictionary access control - does nothing
133 ** by default, user can redefine to guarantee exclusive dict
134 ** access to a single thread for updates. All dict update code
135 ** is guaranteed to be bracketed as follows:
136 ** ficlLockDictionary(TRUE);
137 ** <code that updates dictionary>
138 ** ficlLockDictionary(FALSE);
139 **
140 ** Returns zero if successful, nonzero if unable to acquire lock
141 ** befor timeout (optional - could also block forever)
142 */
143 #if FICL_MULTITHREAD
144 int ficlLockDictionary(short fLock)
145 {
146 	IGNORE(fLock);
147 	return 0;
148 }
149 #endif /* FICL_MULTITHREAD */
150