1 #include <stdio.h>
2 #include "insn.h"
3 #include "exhaust.h"
4 #include "asm.h"
5 
6 #include "compress.h"
7 
8 #define MAX_CYCLES 80000
9 #define MAX_OPMOD _OP(OPCODE_LAST,MODIFIER_LAST)
10 #define CORESIZE 8000
11 
12 int NWarriors;
13 unsigned char data[MAX_CYCLES];
14 unsigned checksum = 0;
15 unsigned long long count[MAX_OPMOD], insns = 0;
16 
17 typedef struct {
18     unsigned long long djnf, movi;
19 }
20 opmod_enquiry_t;
21 opmod_enquiry_t **opmod_enquiry;
22 
dump_stats(char ** WarriorNames)23 void dump_stats(char **WarriorNames) {
24     unsigned int i, j,
25     op, mod;
26     opmod_enquiry_t enquiry, enquiry_sum;
27     char
28 		// html
29 		*html_preamble = "<html>\n<body>\n<h1>%s</h1>\n",
30 		*html_table_preamble = "<h2>%s</h2><table border=\"1\">\n",
31 		*html_start_row = "<tr>",
32 		*html_name = "<th>%s</th>",
33 		*html_value = "<td>%i</td>",
34 		*html_valueu32 = "<td>%u</td>",
35 		*html_valueu64 = "<td>%llu</td>",
36 		*html_valueenquirytype = "<td>djn.f:&nbsp;%u<br/>mov.i:&nbsp;%u</td>",
37 		*html_end_row = "</tr>\n",
38 		*html_table_postamble = "</table>\n",
39 		*html_postamble = "</body>\n</html>\n",
40 		// tab-seperated-value
41 		*tsv_preamble = "",
42 		*tsv_table_preamble = "",
43 		*tsv_start_row = "",
44 		*tsv_name = "%s\t",
45 		*tsv_value = "%i\t",
46 		*tsv_valueu32 = "%u\t",
47 		*tsv_valueu64 = "%llu\t",
48 		*tsv_valueenquirytype = "%u/%u\t",
49 		*tsv_end_row = "\n",
50 		*tsv_table_postamble = "",
51 		*tsv_postamble = "",
52 		// --- actually used ones ---
53 		*preamble = html_preamble,
54 		*table_preamble = html_table_preamble,
55 		*start_row = html_start_row,
56 		*name = html_name,
57 		*value = html_value,
58 		*valueu32 = html_valueu32,
59 		*valueu64 = html_valueu64,
60 		*valueenquirytype = html_valueenquirytype,
61 		*end_row = html_end_row,
62 		*table_postamble = html_table_postamble,
63 		*postamble = html_postamble;
64 
65     printf(preamble,"RESULTS");
66 
67     printf(table_preamble,"Enquiry Matrix");
68     printf(start_row);
69     printf(name,"");
70     for(i=0; i<NWarriors; i++) {
71         printf(name,WarriorNames[i]);
72     }
73     printf(end_row);
74 
75     for(i=0; i<NWarriors; i++) {
76         printf(start_row);
77         printf(name,WarriorNames[i]);
78         for(j=0; j<NWarriors; j++) {
79             printf(valueenquirytype,opmod_enquiry[i][j].djnf,opmod_enquiry[i][j].movi);
80         }
81         printf(end_row);
82     }
83 
84     printf(table_postamble);
85 
86     printf(table_preamble,"Opcode Execution Frequency");
87     for(op=0; op<OPCODE_LAST; op++) {
88         for(mod=0; mod<MODIFIER_LAST; mod++) {
89             if(0 < count[_OP(op,mod)]) {
90                 printf(start_row);
91                 printf(name,MNEMONIC_OPCODE[op]);
92                 printf(name,MNEMONIC_MODIFIER[mod]);
93                 printf(valueu64,count[_OP(op,mod)]);
94                 printf(end_row);
95             }
96         }
97     }
98     printf(table_postamble);
99 
100     printf(table_preamble,"Enquiry");
101     printf(start_row);
102     printf(name,"Warrior");
103     printf(name,"djn.f");
104     printf(name,"mov.i");
105     printf(end_row);
106     enquiry_sum.djnf = 0LL;
107     enquiry_sum.movi = 0LL;
108     for(i=0; i<NWarriors; i++) {
109         printf(start_row);
110         printf(name,WarriorNames[i]);
111         enquiry.djnf = 0LL;
112         enquiry.movi = 0LL;
113         for(j=0; j<NWarriors; j++) {
114             enquiry.djnf += opmod_enquiry[i][j].djnf;
115             enquiry.movi += opmod_enquiry[i][j].movi;
116         }
117         enquiry_sum.djnf += enquiry.djnf;
118         enquiry_sum.movi += enquiry.movi;
119         printf(valueu64,enquiry.djnf);
120         printf(valueu64,enquiry.movi);
121         printf(end_row);
122     }
123     printf(start_row);
124     printf(name,"Sum:");
125     printf(valueu64,enquiry_sum.djnf);
126     printf(valueu64,enquiry_sum.movi);
127     printf(end_row);
128     printf(table_postamble);
129 
130     printf(table_preamble,"Summary");
131     printf(start_row);
132     printf(name,"# instructions");
133     printf(valueu64,insns);
134     printf(end_row);
135     printf(start_row);
136     printf(name,"checksum");
137     printf(valueu32,checksum);
138     printf(end_row);
139     printf(start_row);
140     printf(name,"physical bytes");
141     printf(valueu64,get_physical_bytes());
142     printf(end_row);
143     printf(start_row);
144     printf(name,"logical bytes");
145     printf(valueu64,get_logical_bytes());
146     printf(end_row);
147     printf(table_postamble);
148 
149     printf(postamble);
150 }
151 
load_file(char * fname)152 int load_file(char *fname) {
153     comp_buf_t block;
154     unsigned int i, self, enemy, len, read, fights = 0,
155             djnf, movi;
156     unsigned char opmod;
157     long pos;
158     FILE *fex = fopen(fname,"rb");
159     if(fex == NULL) {
160         printf("%s: x3\n",fname);
161         return 3;
162     }
163 	 block.f = fex;
164     if(1 != fread(&self,sizeof(self),1,fex)) {
165         printf("x6");
166         return 6;
167     }
168     while(1) {
169         pos = ftell(fex);
170         if(1 != fread(&enemy,sizeof(enemy),1,fex)) {
171             if(!feof(fex)) {
172                 printf("x1");
173                 return 1;
174             } else
175                 break;
176         }
177         if(1 != fread(&len,sizeof(len),1,fex)) {
178             printf("x5");
179             return 5;
180         }
181         //printf("%i vs %i = %i ops @ %li\n",fights,enemy,len,pos);
182         if(len > 80000) {
183             printf("x4");
184             return 4;
185         }
186         read = 0;
187         do {
188 			   block_decompress(&block);
189             for(i=0; i<block.len; i++)
190                 data[read++] = block.buf[i];
191         } while(read < len);
192         if(read != len) {
193             printf("read: %u != %u: x11",read,len);
194             return 11;
195         }
196         for(i=0; i<len; i++) {
197             if(data[i] >= MAX_OPMOD) {
198                 printf("%i = %i x7",i,data[i]);
199                 return 7;
200             }
201             switch(data[i]) { // interesting warriors we want to keep track of
202             case _OP(DJN,mF):
203                 opmod_enquiry[self][enemy].djnf++;
204                 break;
205             case _OP(MOV,mI):
206                 opmod_enquiry[self][enemy].movi++;
207                 break;
208             }
209             checksum += data[i];
210             count[data[i]]++;
211             insns++;
212         }
213         fights++;
214     }
215     fclose(fex);
216     return 0;
217 }
218 
main(int argc,char ** args)219 int main(int argc,char **args) {
220     int i, j;
221 
222     NWarriors = argc-1;
223 
224     memset(count,0,MAX_OPMOD*sizeof(unsigned long long));
225     opmod_enquiry = (opmod_enquiry_t**)malloc(NWarriors*sizeof(opmod_enquiry_t*));
226     for(i=0; i<NWarriors; i++) {
227         opmod_enquiry[i] = (opmod_enquiry_t*)calloc(NWarriors,sizeof(opmod_enquiry_t));
228     }
229 
230     for(i=1; i<argc; i++) {
231         if(0 != load_file(args[i])) {
232             return 1;
233         }
234     }
235     dump_stats(args);
236     return 0;
237 }
238 
239