initial
[fpgammix.git] / workloads / tinymon / tinymon.c
blob9be6c7ff463f2ecf81fb3da0a0bdeaad5324e43d
1 /*
2 I figured it would be easier to write the assembly if I had a
3 working model in C first ...
5 Take 2: I discovered late that Motorola's SREC is a poor fit as it
6 doesn't support 64-bit addresses, so plan B is to simply parse the
7 output of objdump -s. It wasn't a complete waste as much of the C
8 and assembly code can be reused.
10 For reference, this is what such output might look like:
14 hwfb.elf: file format elf64-mmix
16 Contents of section .init:
17 0100 fe000004 f2010003 e3ff00ff 00000000 ................
18 0110 e3011934 e6010000 e5010000 e4010000 ...4............
19 0120 e3ff03a8 e6ff0000 e5ff0000 e4ff0000 ................
20 0130 bf00ff00 e3ff0244 e6ff0000 e5ff0000 .......D........
21 0140 e4ff0000 bf01ff00 f6040000 e3ff18f8 ................
22 0150 e6ff0000 e5ff0000 e4ff0000 bf01ff00 ................
23 0160 f6040000 f4ff0003 f60400ff f8000000 ................
24 0170 f6040000 f8000000 ........
25 Contents of section .text:
26 0178 60000000 00000000 e3ff0020 f61300ff `.......... ....
27 0188 f5fffffc 8ffeff00 83ff0100 83ff0101 ................
28 ...
29 2000000000001000 00000000 00000000 00000000 00000000 ................
30 2000000000001010 00000000 00000000 00000000 00000000 ................
31 Contents of section .MMIX.reg_contents:
32 0790 00000000 00002990 20000000 00000000 ......). .......
33 07a0 20000000 000007c0 20000000 00000fd0 ....... .......
34 07b0 40000000 00000000 00000000 00000000 @...............
35 07c0 00000000 00000000 00000000 00000000 ................
36 07d0 00000000 00000000 00000000 00000000 ................
37 07e0 00000000 00000000 00000000 00000000 ................
38 07f0 00000000 00000000 ........
41 #ifndef __MMIX__
42 # include <stdint.h>
43 # include <stdio.h>
44 # include <stdlib.h>
45 char lookahead;
46 typedef int INT;
47 # define store(v, address) printf("[%08lx] <- %08lx\n", address, v)
50 #else
53 typedef long long unsigned uint64_t;
54 typedef long int INT;
56 /* Carefully inspect the assembly after each change as this code is very fragile */
58 register volatile int *IOSPACE asm("$245");
59 register INT lookahead asm("$246");
61 #define reg(n) register unsigned long g##n asm("$"#n)
64 reg(32); reg(33); reg(34); reg(35); reg(36); reg(37); reg(38); reg(39);
65 reg(40); reg(41); reg(42); reg(43); reg(44); reg(45); reg(46); reg(47); reg(48); reg(49);
66 reg(50); reg(51); reg(52); reg(53); reg(54); reg(55); reg(56); reg(57); reg(58); reg(59);
67 reg(60); reg(61); reg(62); reg(63); reg(64); reg(65); reg(66); reg(67); reg(68); reg(69);
68 reg(70); reg(71); reg(72); reg(73); reg(74); reg(75); reg(76); reg(77); reg(78); reg(79);
69 reg(80); reg(81); reg(82); reg(83); reg(84); reg(85); reg(86); reg(87); reg(88); reg(89);
70 reg(90); reg(91); reg(92); reg(93); reg(94); reg(95); reg(96); reg(97); reg(98); reg(99);
71 reg(100); reg(101); reg(102); reg(103); reg(104); reg(105); reg(106); reg(107); reg(108); reg(109);
72 reg(110); reg(111); reg(112); reg(113); reg(114); reg(115); reg(116); reg(117); reg(118); reg(119);
73 reg(120); reg(121); reg(122); reg(123); reg(124); reg(125); reg(126); reg(127); reg(128); reg(129);
74 reg(130); reg(131); reg(132); reg(133); reg(134); reg(135); reg(136); reg(137); reg(138); reg(139);
75 reg(140); reg(141); reg(142); reg(143); reg(144); reg(145); reg(146); reg(147); reg(148); reg(149);
76 reg(150); reg(151); reg(152); reg(153); reg(154); reg(155); reg(156); reg(157); reg(158); reg(159);
77 reg(160); reg(161); reg(162); reg(163); reg(164); reg(165); reg(166); reg(167); reg(168); reg(169);
78 reg(170); reg(171); reg(172); reg(173); reg(174); reg(175); reg(176); reg(177); reg(178); reg(179);
79 reg(180); reg(181); reg(182); reg(183); reg(184); reg(185); reg(186); reg(187); reg(188); reg(189);
80 reg(190); reg(191); reg(192); reg(193); reg(194); reg(195); reg(196); reg(197); reg(198); reg(199);
81 reg(200); reg(201); reg(202); reg(203); reg(204); reg(205); reg(206); reg(207); reg(208); reg(209);
82 reg(210); reg(211); reg(212); reg(213); reg(214); reg(215); reg(216); reg(217); reg(218); reg(219);
83 reg(220); reg(221); reg(222); reg(223); reg(224); */ reg(225); reg(226); reg(227); reg(228);
84 reg(229);
85 reg(230);
86 reg(231);
87 reg(232);
88 reg(233);
89 reg(234);
90 reg(235);
91 reg(236);
92 reg(237);
93 reg(238);
94 reg(239);
95 reg(240);
96 reg(241);
97 reg(242);
98 reg(243);
99 reg(244);
100 /*reg(245);*/
101 #define g245 IOSPACE
102 /*reg(246);*/
103 #define g246 lookahead
105 reg(247);
106 reg(248);
107 reg(249);
108 reg(250);
109 reg(251);
110 reg(252);
111 reg(253);
112 reg(254);
113 reg(255);
115 # define store(v, address) *(unsigned *) address = v
117 void putch(INT ch)
119 while (IOSPACE[1])
121 IOSPACE[0] = ch;
123 #endif
125 static inline void nextchar(void)
127 #ifndef __MMIX__
128 lookahead = getchar();
129 if (lookahead < 0)
130 exit(0);
131 putchar(lookahead);
132 #else
133 do {
134 lookahead = IOSPACE[3];
135 } while (lookahead < 0);
136 //putch(lookahead);
137 #endif
140 static inline INT digit(void)
142 INT res;
144 if ('0' <= lookahead && lookahead <= '9')
145 res = lookahead - '0';
146 else if ('a' <= lookahead && lookahead <= 'f')
147 res = lookahead - 'a' + 10;
148 else if ('A' <= lookahead && lookahead <= 'F')
149 res = lookahead - 'A' + 10;
150 else
151 return -1;
153 nextchar();
154 return res;
158 main()
160 restart:
162 #ifdef __MMIX__
163 IOSPACE = (int*) 0x1000000000000ULL;
164 #endif
166 putch('O');
167 putch('k');
168 putch('\r');
169 putch('\n');
171 nextchar();
173 for (;;) {
174 uint64_t address, v;
175 INT i, d;
177 if (lookahead != ' ')
178 goto skip;
180 for (i = 0; i < 5; ++i) {
181 nextchar();
182 v = digit();
183 if (v < 0)
184 break;
186 for (;;) {
187 d = digit();
188 if (d < 0)
189 break;
190 v = v * 16 + d;
193 if (i == 0) {
194 address = v;
195 if (lookahead == 'G') {
196 putch('G');
197 putch('o');
198 putch('\r');
199 putch('\n');
201 g32 = ((unsigned long *) 0)[32];
202 g33 = ((unsigned long *) 0)[33];
203 g34 = ((unsigned long *) 0)[34];
204 g35 = ((unsigned long *) 0)[35];
205 g36 = ((unsigned long *) 0)[36];
206 g37 = ((unsigned long *) 0)[37];
207 g38 = ((unsigned long *) 0)[38];
208 g39 = ((unsigned long *) 0)[39];
209 g40 = ((unsigned long *) 0)[40];
210 g41 = ((unsigned long *) 0)[41];
211 g42 = ((unsigned long *) 0)[42];
212 g43 = ((unsigned long *) 0)[43];
213 g44 = ((unsigned long *) 0)[44];
214 g45 = ((unsigned long *) 0)[45];
215 g46 = ((unsigned long *) 0)[46];
216 g47 = ((unsigned long *) 0)[47];
217 g48 = ((unsigned long *) 0)[48];
218 g49 = ((unsigned long *) 0)[49];
219 g50 = ((unsigned long *) 0)[50];
220 g51 = ((unsigned long *) 0)[51];
221 g52 = ((unsigned long *) 0)[52];
222 g53 = ((unsigned long *) 0)[53];
223 g54 = ((unsigned long *) 0)[54];
224 g55 = ((unsigned long *) 0)[55];
225 g56 = ((unsigned long *) 0)[56];
226 g57 = ((unsigned long *) 0)[57];
227 g58 = ((unsigned long *) 0)[58];
228 g59 = ((unsigned long *) 0)[59];
229 g60 = ((unsigned long *) 0)[60];
230 g61 = ((unsigned long *) 0)[61];
231 g62 = ((unsigned long *) 0)[62];
232 g63 = ((unsigned long *) 0)[63];
233 g64 = ((unsigned long *) 0)[64];
234 g65 = ((unsigned long *) 0)[65];
235 g66 = ((unsigned long *) 0)[66];
236 g67 = ((unsigned long *) 0)[67];
237 g68 = ((unsigned long *) 0)[68];
238 g69 = ((unsigned long *) 0)[69];
239 g70 = ((unsigned long *) 0)[70];
240 g71 = ((unsigned long *) 0)[71];
241 g72 = ((unsigned long *) 0)[72];
242 g73 = ((unsigned long *) 0)[73];
243 g74 = ((unsigned long *) 0)[74];
244 g75 = ((unsigned long *) 0)[75];
245 g76 = ((unsigned long *) 0)[76];
246 g77 = ((unsigned long *) 0)[77];
247 g78 = ((unsigned long *) 0)[78];
248 g79 = ((unsigned long *) 0)[79];
249 g80 = ((unsigned long *) 0)[80];
250 g81 = ((unsigned long *) 0)[81];
251 g82 = ((unsigned long *) 0)[82];
252 g83 = ((unsigned long *) 0)[83];
253 g84 = ((unsigned long *) 0)[84];
254 g85 = ((unsigned long *) 0)[85];
255 g86 = ((unsigned long *) 0)[86];
256 g87 = ((unsigned long *) 0)[87];
257 g88 = ((unsigned long *) 0)[88];
258 g89 = ((unsigned long *) 0)[89];
259 g90 = ((unsigned long *) 0)[90];
260 g91 = ((unsigned long *) 0)[91];
261 g92 = ((unsigned long *) 0)[92];
262 g93 = ((unsigned long *) 0)[93];
263 g94 = ((unsigned long *) 0)[94];
264 g95 = ((unsigned long *) 0)[95];
265 g96 = ((unsigned long *) 0)[96];
266 g97 = ((unsigned long *) 0)[97];
267 g98 = ((unsigned long *) 0)[98];
268 g99 = ((unsigned long *) 0)[99];
269 g100 = ((unsigned long *) 0)[100];
270 g101 = ((unsigned long *) 0)[101];
271 g102 = ((unsigned long *) 0)[102];
272 g103 = ((unsigned long *) 0)[103];
273 g104 = ((unsigned long *) 0)[104];
274 g105 = ((unsigned long *) 0)[105];
275 g106 = ((unsigned long *) 0)[106];
276 g107 = ((unsigned long *) 0)[107];
277 g108 = ((unsigned long *) 0)[108];
278 g109 = ((unsigned long *) 0)[109];
279 g110 = ((unsigned long *) 0)[110];
280 g111 = ((unsigned long *) 0)[111];
281 g112 = ((unsigned long *) 0)[112];
282 g113 = ((unsigned long *) 0)[113];
283 g114 = ((unsigned long *) 0)[114];
284 g115 = ((unsigned long *) 0)[115];
285 g116 = ((unsigned long *) 0)[116];
286 g117 = ((unsigned long *) 0)[117];
287 g118 = ((unsigned long *) 0)[118];
288 g119 = ((unsigned long *) 0)[119];
289 g120 = ((unsigned long *) 0)[120];
290 g121 = ((unsigned long *) 0)[121];
291 g122 = ((unsigned long *) 0)[122];
292 g123 = ((unsigned long *) 0)[123];
293 g124 = ((unsigned long *) 0)[124];
294 g125 = ((unsigned long *) 0)[125];
295 g126 = ((unsigned long *) 0)[126];
296 g127 = ((unsigned long *) 0)[127];
297 g128 = ((unsigned long *) 0)[128];
298 g129 = ((unsigned long *) 0)[129];
299 g130 = ((unsigned long *) 0)[130];
300 g131 = ((unsigned long *) 0)[131];
301 g132 = ((unsigned long *) 0)[132];
302 g133 = ((unsigned long *) 0)[133];
303 g134 = ((unsigned long *) 0)[134];
304 g135 = ((unsigned long *) 0)[135];
305 g136 = ((unsigned long *) 0)[136];
306 g137 = ((unsigned long *) 0)[137];
307 g138 = ((unsigned long *) 0)[138];
308 g139 = ((unsigned long *) 0)[139];
309 g140 = ((unsigned long *) 0)[140];
310 g141 = ((unsigned long *) 0)[141];
311 g142 = ((unsigned long *) 0)[142];
312 g143 = ((unsigned long *) 0)[143];
313 g144 = ((unsigned long *) 0)[144];
314 g145 = ((unsigned long *) 0)[145];
315 g146 = ((unsigned long *) 0)[146];
316 g147 = ((unsigned long *) 0)[147];
317 g148 = ((unsigned long *) 0)[148];
318 g149 = ((unsigned long *) 0)[149];
319 g150 = ((unsigned long *) 0)[150];
320 g151 = ((unsigned long *) 0)[151];
321 g152 = ((unsigned long *) 0)[152];
322 g153 = ((unsigned long *) 0)[153];
323 g154 = ((unsigned long *) 0)[154];
324 g155 = ((unsigned long *) 0)[155];
325 g156 = ((unsigned long *) 0)[156];
326 g157 = ((unsigned long *) 0)[157];
327 g158 = ((unsigned long *) 0)[158];
328 g159 = ((unsigned long *) 0)[159];
329 g160 = ((unsigned long *) 0)[160];
330 g161 = ((unsigned long *) 0)[161];
331 g162 = ((unsigned long *) 0)[162];
332 g163 = ((unsigned long *) 0)[163];
333 g164 = ((unsigned long *) 0)[164];
334 g165 = ((unsigned long *) 0)[165];
335 g166 = ((unsigned long *) 0)[166];
336 g167 = ((unsigned long *) 0)[167];
337 g168 = ((unsigned long *) 0)[168];
338 g169 = ((unsigned long *) 0)[169];
339 g170 = ((unsigned long *) 0)[170];
340 g171 = ((unsigned long *) 0)[171];
341 g172 = ((unsigned long *) 0)[172];
342 g173 = ((unsigned long *) 0)[173];
343 g174 = ((unsigned long *) 0)[174];
344 g175 = ((unsigned long *) 0)[175];
345 g176 = ((unsigned long *) 0)[176];
346 g177 = ((unsigned long *) 0)[177];
347 g178 = ((unsigned long *) 0)[178];
348 g179 = ((unsigned long *) 0)[179];
349 g180 = ((unsigned long *) 0)[180];
350 g181 = ((unsigned long *) 0)[181];
351 g182 = ((unsigned long *) 0)[182];
352 g183 = ((unsigned long *) 0)[183];
353 g184 = ((unsigned long *) 0)[184];
354 g185 = ((unsigned long *) 0)[185];
355 g186 = ((unsigned long *) 0)[186];
356 g187 = ((unsigned long *) 0)[187];
357 g188 = ((unsigned long *) 0)[188];
358 g189 = ((unsigned long *) 0)[189];
359 g190 = ((unsigned long *) 0)[190];
360 g191 = ((unsigned long *) 0)[191];
361 g192 = ((unsigned long *) 0)[192];
362 g193 = ((unsigned long *) 0)[193];
363 g194 = ((unsigned long *) 0)[194];
364 g195 = ((unsigned long *) 0)[195];
365 g196 = ((unsigned long *) 0)[196];
366 g197 = ((unsigned long *) 0)[197];
367 g198 = ((unsigned long *) 0)[198];
368 g199 = ((unsigned long *) 0)[199];
369 g200 = ((unsigned long *) 0)[200];
370 g201 = ((unsigned long *) 0)[201];
371 g202 = ((unsigned long *) 0)[202];
372 g203 = ((unsigned long *) 0)[203];
373 g204 = ((unsigned long *) 0)[204];
374 g205 = ((unsigned long *) 0)[205];
375 g206 = ((unsigned long *) 0)[206];
376 g207 = ((unsigned long *) 0)[207];
377 g208 = ((unsigned long *) 0)[208];
378 g209 = ((unsigned long *) 0)[209];
379 g210 = ((unsigned long *) 0)[210];
380 g211 = ((unsigned long *) 0)[211];
381 g212 = ((unsigned long *) 0)[212];
382 g213 = ((unsigned long *) 0)[213];
383 g214 = ((unsigned long *) 0)[214];
384 g215 = ((unsigned long *) 0)[215];
385 g216 = ((unsigned long *) 0)[216];
386 g217 = ((unsigned long *) 0)[217];
387 g218 = ((unsigned long *) 0)[218];
388 g219 = ((unsigned long *) 0)[219];
389 g220 = ((unsigned long *) 0)[220];
390 g221 = ((unsigned long *) 0)[221];
391 g222 = ((unsigned long *) 0)[222];
392 g223 = ((unsigned long *) 0)[223];
393 g224 = ((unsigned long *) 0)[224];
395 g225 = ((unsigned long *) 0)[225];
396 g226 = ((unsigned long *) 0)[226];
397 g227 = ((unsigned long *) 0)[227];
398 g228 = ((unsigned long *) 0)[228];
399 g229 = ((unsigned long *) 0)[229];
400 g230 = ((unsigned long *) 0)[230];
401 g231 = ((unsigned long *) 0)[231];
402 g232 = ((unsigned long *) 0)[232];
403 g233 = ((unsigned long *) 0)[233];
404 g234 = ((unsigned long *) 0)[234];
405 g235 = ((unsigned long *) 0)[235];
406 g236 = ((unsigned long *) 0)[236];
407 g237 = ((unsigned long *) 0)[237];
408 g238 = ((unsigned long *) 0)[238];
409 g239 = ((unsigned long *) 0)[239];
410 g240 = ((unsigned long *) 0)[240];
411 g241 = ((unsigned long *) 0)[241];
412 g242 = ((unsigned long *) 0)[242];
413 g243 = ((unsigned long *) 0)[243];
414 g244 = ((unsigned long *) 0)[244];
415 g245 = ((unsigned long *) 0)[245];
416 g246 = ((unsigned long *) 0)[246];
417 g247 = ((unsigned long *) 0)[247];
418 g248 = ((unsigned long *) 0)[248];
419 g249 = ((unsigned long *) 0)[249];
420 g250 = ((unsigned long *) 0)[250];
421 g251 = ((unsigned long *) 0)[251];
422 g252 = ((unsigned long *) 0)[252];
423 g253 = ((unsigned long *) 0)[253];
424 g254 = ((unsigned long *) 0)[254];
425 g255 = ((unsigned long *) 0)[255];
426 (* (void (*)()) address)();
427 IOSPACE = (int*) 0x1000000000000ULL;
428 putch('\r');
429 putch('\n');
430 goto restart;
432 } else {
433 store(v, address);
434 address += 4;
435 putch('!');
438 if (lookahead != ' ')
439 break;
442 skip:
443 while (lookahead != '\n')
444 nextchar();
445 nextchar();
448 return 0;