Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / obp / obp / pkg / netinet / utils.fth
CommitLineData
920dae64
AT
1\ ========== Copyright Header Begin ==========================================
2\
3\ Hypervisor Software File: utils.fth
4\
5\ Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
6\
7\ - Do no alter or remove copyright notices
8\
9\ - Redistribution and use of this software in source and binary forms, with
10\ or without modification, are permitted provided that the following
11\ conditions are met:
12\
13\ - Redistribution of source code must retain the above copyright notice,
14\ this list of conditions and the following disclaimer.
15\
16\ - Redistribution in binary form must reproduce the above copyright notice,
17\ this list of conditions and the following disclaimer in the
18\ documentation and/or other materials provided with the distribution.
19\
20\ Neither the name of Sun Microsystems, Inc. or the names of contributors
21\ may be used to endorse or promote products derived from this software
22\ without specific prior written permission.
23\
24\ This software is provided "AS IS," without a warranty of any kind.
25\ ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
26\ INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
27\ PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
28\ MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
29\ ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
30\ DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
31\ OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
32\ FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
33\ DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
34\ ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
35\ SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
36\
37\ You acknowledge that this software is not designed, licensed or
38\ intended for use in the design, construction, operation or maintenance of
39\ any nuclear facility.
40\
41\ ========== Copyright Header End ============================================
42id: @(#)utils.fth 1.1 04/09/07
43purpose: Generic utility functions
44copyright: Copyright 2004 Sun Microsystems, Inc. All Rights Reserved
45copyright: Use is subject to license terms.
46
47headerless
48
49: 3dup ( n1 n2 n3 -- n1 n2 n3 n1 n2 n3 ) dup 2over rot ;
50
51: decimal ( -- ) d# 10 base ! ;
52: hex ( -- ) h# 10 base ! ;
53: .d ( n -- ) base @ swap decimal . base ! ;
54: spaces ( n -- ) 0 max 0 ?do space loop ;
55
56fload ${BP}/pkg/netinet/queue.fth
57fload ${BP}/pkg/netinet/strings.fth
58
59headerless
60
61: encapsulated-data ( pkt pktlen hdrlen -- data datalen )
62 tuck - >r + r>
63;
64
65: encapsulating-hdr ( data datalen hdrlen -- pkt pktlen )
66 tuck + >r - r>
67;
68
69: timed-out? ( when -- flag ) get-msecs < ;
70
71: pstring, ( adr len -- ) dup c, bounds ?do i c@ c, loop ;
72: cstring, ( adr len -- ) bounds ?do i c@ c, loop 0 c, ;
73
74: call-cif-method ( ?? name$ -- ?? )
75 " /openprom/client-services" find-package if
76 >r 2dup r> find-method if
77 nip nip execute
78 else
79 ." Can't find client interface service " type cr -1 throw
80 then
81 else
82 ." Can't find '/openprom/client-services'" cr -1 throw
83 then
84;
85
86: set-chosen-property ( adr,len propname$ -- )
87 " /chosen" find-package if
88 my-self >r 0 to my-self
89 push-package property pop-package
90 r> to my-self
91 else
92 2drop 2drop
93 then
94;
95
96: get-property ( node$ propname$ -- adr,len )
97 0 0 2swap 2rot find-package if
98 get-package-property 0= if 2swap 2drop then
99 else
100 2drop
101 then
102;
103
104: get-option-string ( propname$ -- $ )
105 " /options" 2swap get-property decode-string 2swap 2drop
106;
107
108\ Random number generator
109\ x(n+1) = (69069 * x(n)) mod 2^32
110: random ( -- n )
111 get-msecs dup ( now seed )
112 begin over get-msecs = while
113 d# 69069 * 1 d# 32 << 1- and
114 repeat nip ( n )
115;
116
117\ Token handling is implemented using token tables. Each table entry
118\ specifies the token string (keyname), the associated handler and the
119\ case-sensitivity to be used for keyname comparions. A null table
120\ entry marks the end of the table.
121\
122\ A token table registering handlers for 2 case-insensitive tokens
123\ would look like
124\ create keys-table
125\ " key1" false ['] key1-handler token-handler,
126\ " key2" false ['] key2-handler token-handler,
127\ 0 0 0 0 token-handler,
128
129: token-handler, ( token$ case-sensitive? xt -- )
130 swap 2swap ( xt flag token$ )
131 dup 2+ >r pstring, c, r> ( xt n )
132 dup aligned swap ?do 0 c, loop ( xt )
133 , ( )
134;
135
136: token-match? ( token$ $ -- match? )
137 2dup ca+ c@ if $= else $case= then
138;
139
140: find-token-handler ( token$ table -- xt true | false )
141 begin ( token$ adr )
142 count ( token$ $ )
143 dup while ( token$ $ )
144 2over 2over token-match? if ( token$ $ )
145 2swap 2drop ca+ ca1+ aligned @ true exit ( xt true )
146 then ( token$ $ )
147 ca+ ca1+ aligned na1+ ( token$ adr' )
148 repeat ( token$ $ )
149 2drop 2drop false ( false )
150;
151
152\ Use a spinner to report progress.
153
1540 instance value activity-counter
155
156: show-progress ( -- )
157 activity-counter 1+ dup to activity-counter
158 dup h# f and 0= if
159 4 rshift 3 and " \|/-" drop swap ca+ c@ emit bs emit -2 #out +!
160 else
161 drop
162 then
163;
164
165headers