-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommstime.occ
264 lines (248 loc) · 6.61 KB
/
commstime.occ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#INCLUDE "wiring.module"
-- Various stuff taken from the course library that's used by these testcases.
-- This has all been translated back to occam2 for now.
VAL BYTE NULL IS 0: --* ASCII NUL
VAL BYTE BELL IS 7: --* ASCII BEL - terminal bell
VAL BYTE BACK IS 8: --* ASCII BS - backspace key
VAL BYTE ESCAPE IS 27: --* ASCII ESC - escape key
VAL BYTE DELETE IS 127: --* ASCII DEL - delete key
VAL BYTE FLUSH IS 255: --* Flush output buffer
VAL BYTE END.OF.FILE IS 255: --* End of file
--{{{ PROC out.repeat (VAL BYTE ch, VAL INT n, CHAN OF BYTE out)
--* Write a character repeatedly to a channel.
-- This outputs [@code ch] down the channel [@code out] [@code n] times. If
-- [@code n] is negative, nothing happens.
-- @param ch Character
-- @param n Number of times to output (negative values result in no output)
-- @param out Channel to write to
PROC out.repeat (VAL BYTE ch, VAL INT n, CHAN OF BYTE out)
--{{{
IF
n > 0
SEQ i = 0 FOR n
out ! ch
TRUE
SKIP
--}}}
:
--}}}
--{{{ PROC out.string (VAL []BYTE s, VAL INT field, CHAN OF BYTE out)
--* Write a string to a channel.
-- This outputs [@code s] in a fieldwidth [@code field] down [@code out].
-- @param s String
-- @param field Field width to right-justify in
-- @param out Channel to write to
PROC out.string (VAL []BYTE s, VAL INT field, CHAN OF BYTE out)
--{{{
VAL INT length IS SIZE s:
SEQ
out.repeat (' ', field - length, out)
SEQ i = 0 FOR length
out ! s[i]
--}}}
:
--}}}
--{{{ PROC out.byte (VAL BYTE b, VAL INT field, CHAN OF BYTE out)
--* Write a byte in decimal to a channel.
-- This outputs [@code b] in a fieldwidth [@code field] down [@code out]. If
-- the fieldwidth is too wide for [@code b], it right-justifies [@code b] with
-- spaces on the left. If the field is not wide enough, it prints the [@code
-- b] anyway. These rules for fieldwidth are the same as those used by the
-- Pascal [@text write] procedure.
-- @param b Byte
-- @param field Field width to right-justify in
-- @param out Channel to write to
PROC out.byte (VAL BYTE b, VAL INT field, CHAN OF BYTE out)
--{{{
VAL BYTE hundreds IS b/100:
VAL BYTE rest IS b\100:
VAL BYTE tens IS rest/10:
VAL BYTE ones IS rest\10:
IF
hundreds > 0
SEQ
out.repeat (' ', field - 3, out)
out ! hundreds + '0'
out ! tens + '0'
out ! ones + '0'
tens > 0
SEQ
out.repeat (' ', field - 2, out)
out ! tens + '0'
out ! ones + '0'
TRUE
SEQ
out.repeat (' ', field - 1, out)
out ! ones + '0'
--}}}
:
--}}}
--{{{ PROC out.int (VAL INT n, VAL INT field, CHAN OF BYTE out)
--* Write an integer in decimal to a channel.
-- This outputs [@code n] in a fieldwidth [@code field] down [@code out]. The
-- rules for fieldwidth are as [@ref out.byte].
-- @param n Integer
-- @param field Field width to right-justify in
-- @param out Channel to write to
PROC out.int (VAL INT n, VAL INT field, CHAN OF BYTE out)
--{{{
IF
n = (MOSTNEG INT)
--{{{ minint
out.string ("-2147483648", field, out)
--}}}
n = 0
--{{{ zero
SEQ
IF
1 < field
out.repeat (' ', field - 1, out)
TRUE
SKIP
out ! '0'
--}}}
TRUE
--{{{ anything else
VAL INT max.digits IS 20:
[max.digits]INT D:
INT x, i:
SEQ
--{{{ check negative
IF
n < 0
x := -n
TRUE -- (n > 0)
x := n
--}}}
--{{{ decompose
SEQ
i := 0
WHILE x > 0
SEQ
D[i] := x\10
x := x/10
i := i + 1
--}}}
--{{{ pad
IF
n > 0
out.repeat (' ', field - i, out)
TRUE
SEQ
out.repeat (' ', (field - 1) - i, out)
out ! '-'
--}}}
--{{{ output
WHILE i > 0
SEQ
i := i - 1
out ! BYTE (D[i] + (INT '0'))
--}}}
--}}}
--}}}
:
--}}}
-- A standalone occam 2 version of the stock commstime benchmark.
--{{{ PROC id (CHAN OF INT in, out)
PROC id (CHAN OF INT in, out)
WHILE TRUE
INT n:
SEQ
in ? n
out ! n
:
--}}}
--{{{ PROC prefix (VAL INT n, CHAN OF INT in, out)
PROC prefix (VAL INT n, CHAN OF INT in, out)
SEQ
out ! n
id (in, out)
:
--}}}
--{{{ PROC delta (CHAN OF INT in, out.0, out.1)
PROC delta (CHAN OF INT in, out.0, out.1)
WHILE TRUE
INT n:
SEQ
in ? n
PAR
out.0 ! n
out.1 ! n
:
--}}}
--{{{ PROC seq.delta (CHAN OF INT in, out.0, out.1)
PROC seq.delta (CHAN OF INT in, out.0, out.1)
WHILE TRUE
INT n:
SEQ
in ? n
out.0 ! n
out.1 ! n
:
--}}}
--{{{ PROC succ (CHAN OF INT in, out)
PROC succ (CHAN OF INT in, out)
WHILE TRUE
INT n:
SEQ
in ? n
out ! n PLUS 1
:
--}}}
--{{{ PROC consume (VAL INT n.loops, CHAN OF INT in, CHAN OF BYTE out)
PROC consume (VAL INT n.loops, CHAN OF INT in, CHAN OF BYTE out)
TIMER tim:
INT t0, t1:
INT value:
SEQ
--{{{ warm-up loop
VAL INT warm.up IS 16:
SEQ i = 0 FOR warm.up
in ? value
--}}}
WHILE TRUE
SEQ
tim ? t0
--{{{ bench-mark loop
SEQ i = 0 FOR n.loops
in ? value
--}}}
tim ? t1
--{{{ report
VAL INT millisecs IS t1 MINUS t0:
VAL INT32 microsecs IS (INT32 millisecs) * 1000:
SEQ
out.string ("Last value received = ", 0, out)
out.int (value, 0, out)
out.string ("*c*n", 0, out)
out.string ("Time = ", 0, out)
out.int (millisecs, 0, out)
out.string (" ms*c*n", 0, out)
out.string ("Time per loop = ", 0, out)
out.int (INT (microsecs / (INT32 n.loops)), 0, out)
out.string (" us*c*n", 0, out)
out.string ("Context switch = ", 0, out)
out.int (INT ((microsecs / (INT32 n.loops)) / 4), 0, out)
out.string (" us*c*n*n", 0, out)
--}}}
:
--}}}
--{{{ PROC comms.time (CHAN OF BYTE keyboard, screen, error)
PROC comms.time (CHAN OF BYTE screen)
BOOL use.seq.delta:
SEQ
--ask.bool ("Sequential delta ", use.seq.delta, keyboard, screen)
use.seq.delta := TRUE
out.string ("*nCommstime starting ...*c*n*n", 0, screen)
CHAN OF INT a, b, c, d:
PAR
prefix (0, b, a)
IF
use.seq.delta
seq.delta (a, c, d) -- the one defined above
TRUE
delta (a, c, d) -- the one that does a parallel output
succ (c, b)
consume (10000, d, screen)
:
--}}}