-
Notifications
You must be signed in to change notification settings - Fork 0
/
mr16sim.c
375 lines (355 loc) · 9.82 KB
/
mr16sim.c
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
//-------------------------------------------------------------------
//-------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int show_fetches;
int show_mem_writes;
int show_mem_reads;
int show_reg_writes;
int show_reg_reads;
int show_diss;
unsigned int fetch_count;
unsigned int write_count;
unsigned int read_count;
unsigned int fetch_limit;
//-------------------------------------------------------------------
unsigned short mem[0x10000];
//-------------------------------------------------------------------
unsigned short pc;
unsigned short reg[8];
//-------------------------------------------------------------------
// stuff for reading csv (binary) file.
FILE *fpin;
char section[512];
unsigned int line;
char newline[1024];
unsigned int maxaddr;
//-------------------------------------------------------------------
//-------------------------------------------------------------------
unsigned short fetch_mem ( unsigned short addr )
{
unsigned short data;
fetch_count++;
data=mem[addr];
if(show_fetches)
{
printf("fetch(0x%04X)=0x%04X\n",addr,data);
}
return(data);
}
//-------------------------------------------------------------------
void write_mem ( unsigned short addr, unsigned short data )
{
write_count++;
if(show_mem_writes)
{
printf("write_mem(0x%04X,0x%04X)\n",addr,data);
}
mem[addr]=data;
}
//-------------------------------------------------------------------
void write_reg ( unsigned char r, unsigned short data )
{
r&=7;
if(show_reg_writes)
{
printf("write_reg(r%u,0x%04X)\n",r,data);
}
if(r==0)
{
printf("cant write to r0\n");
}
else
{
reg[r]=data;
}
}
//-------------------------------------------------------------------
unsigned short read_mem ( unsigned short addr )
{
unsigned short data;
data=mem[addr];
if(show_mem_reads)
{
printf("read_mem(r%u)=0x%04X\n",addr,data);
}
return(data);
}
//-------------------------------------------------------------------
unsigned short read_reg ( unsigned char r )
{
unsigned short data;
r&=7;
data=reg[r];
if(show_reg_reads)
{
printf("read_reg(r%u)=0x%04X\n",r,data);
}
return(data);
}
//-------------------------------------------------------------------
void reset_sim ( void )
{
unsigned int ra;
pc=0;
for(ra=0;ra<8;ra++) reg[ra]=0;
fetch_count=0;
write_count=0;
read_count=0;
}
//-------------------------------------------------------------------
int sim_one ( void )
{
unsigned short inst;
unsigned short op;
unsigned short imm,simm;
unsigned short addr;
unsigned short data;
unsigned short adata,bdata,cdata;
unsigned short brdest;
unsigned char ra,rb,rc;
inst=fetch_mem(pc);
pc=pc+1;
op=(inst>>13)&7;
ra=(inst>>10)&7;
rb=(inst>> 7)&7;
rc=(inst>> 0)&7;
imm=inst&0x3FF;
simm=inst&0x7F;
if(simm&0x40) simm|=(~0)<<7;
switch(op)
{
case 0: //ADD
{
if(inst&0x0078)
{
printf("undefined instruction [0x%04X] 0x%04X\n",pc-1,inst);
return(1);
}
if(show_diss) printf("[0x%04X] 0x%04X add r%u,r%u,r%u\n",pc-1,inst,ra,rb,rc);
bdata=read_reg(rb);
cdata=read_reg(rc);
write_reg(ra,bdata+cdata);
break;
}
case 1: //ADDI
{
if(show_diss) printf("[0x%04X] 0x%04X addi r%u,r%u,0x%04X (%d)\n",pc-1,inst,ra,rb,simm,(short)simm);
bdata=read_reg(rb);
write_reg(ra,bdata+simm);
break;
}
case 2: //NAND
{
if(inst&0x0078)
{
printf("undefined instruction [0x%04X] 0x%04X\n",pc-1,inst);
return(1);
}
if(show_diss) printf("[0x%04X] 0x%04X nand r%u,r%u,r%u\n",pc-1,inst,ra,rb,rc);
bdata=read_reg(rb);
cdata=read_reg(rc);
write_reg(ra,~(bdata&cdata));
break;
}
case 3: //LUI
{
if(show_diss) printf("[0x%04X] 0x%04X lui r%u,0x%03X (0x%04X)\n",pc-1,inst,ra,imm,imm<<6);
write_reg(ra,imm<<6);
break;
}
case 4: //SW
{
data=reg[ra];
addr=reg[rb]+simm;
if(show_diss) printf("[0x%04X] 0x%04X sw r%u,[r%u%+d] ([0x%04X]<=0x%04X) \n",pc-1,inst,ra,rb,((short)simm),addr,data);
data=read_reg(ra);
addr=read_reg(rb)+simm;
write_mem(addr,data);
break;
}
case 5: //LW
{
addr=reg[rb]+simm;
data=mem[addr];
if(show_diss) printf("[0x%04X] 0x%04X lw r%u,[r%u%+d] ([0x%04X]=>0x%04X) \n",pc-1,inst,ra,rb,((short)simm),addr,data);
addr=read_reg(rb)+simm;
data=read_mem(addr);
write_reg(ra,data);
break;
}
case 6: //BEQ
{
adata=reg[ra];
bdata=reg[rb];
brdest=pc+simm;
if(show_diss) printf("[0x%04X] 0x%04X beq r%u,r%u,0x%04X (0x%04X 0x%04X)\n",pc-1,inst,ra,rb,brdest,adata,bdata);
adata=read_reg(ra);
bdata=read_reg(rb);
if(adata==bdata) pc=brdest;
break;
}
case 7: //JALR
{
if(inst==0xFFFF)
{
if(show_diss) printf("[0x%04X] 0x%04X halt\n",pc-1,inst);
return(1);
}
if(inst&0x007F)
{
printf("undefined instruction [0x%04X] 0x%04X\n",pc-1,inst);
return(1);
}
brdest=reg[rb];
if(show_diss) printf("[0x%04X] 0x%04X jalr r%u,r%u (0x%04X)\n",pc-1,inst,ra,rb,brdest);
write_reg(ra,pc);
brdest=read_reg(rb);
pc=brdest;
break;
}
}
return(0);
}
//-------------------------------------------------------------------
void sim ( void )
{
while(1)
{
if(sim_one()) break;
if(fetch_limit)
{
if(fetch_count>=fetch_limit) break;
}
}
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
unsigned int get_section ( char d[], unsigned int len, char s[], unsigned int off )
{
unsigned int dx;
//files generated with mr16as do not have spaces or other characters
//requiring fields to have quotes
dx=0;
while(1)
{
if(s[off]==0)
{
d[dx]=0;
break;
}
if(s[off]==',')
{
d[dx]=0;
off++;
break;
}
d[dx++]=s[off++];
}
return(off);
}
//-------------------------------------------------------------------
int read_csv ( void )
{
unsigned int ra;
unsigned int off;
unsigned int addr;
unsigned int data;
memset(mem,0xFF,sizeof(mem));
maxaddr=0;
line=0;
while(fgets(newline,sizeof(newline)-1,fpin))
{
line++;
off=0;
off=get_section(section,sizeof(section)-1,newline,off);
if(section[0]==0) continue; //warning/error?
if(strcmp(section,"REM")==0) continue;
if(strcmp(section,"COMMENT")==0) continue;
if(strcmp(section,"END")==0) break;
if(strcmp(section,"MEM")==0)
{
//address
off=get_section(section,sizeof(section)-1,newline,off);
ra=0;
if(section[0]==0x30) ra++;
if(section[1]=='x') ra++;
if(section[1]=='X') ra++;
if(ra!=2)
{
printf("<%u> Error: invalid format for address, expecting 0x1234\n",line);
return(1);
}
addr=strtoul(section,NULL,16);
if(addr&(~0xFFFF))
{
printf("<%u> Error: invalid address\n",line);
return(1);
}
//data
off=get_section(section,sizeof(section)-1,newline,off);
ra=0;
if(section[0]==0x30) ra++;
if(section[1]=='x') ra++;
if(section[1]=='X') ra++;
if(ra!=2)
{
printf("<%u> Error: invalid format for data, expecting 0x1234\n",line);
return(1);
}
data=strtoul(section,NULL,16);
if(data&(~0xFFFF))
{
printf("<%u> Error: invalid data\n",line);
return(1);
}
//END
off=get_section(section,sizeof(section)-1,newline,off);
if(strcmp(section,"END")!=0)
{
printf("<%u> Error: no END\n",line);
return(1);
}
//printf("MEM,0x%04X,0x%04X,END,,\n",addr,data);
if(addr>maxaddr) maxaddr=addr;
mem[addr]=data;
}
}
//for(ra=0;ra<=maxaddr;ra++) printf("[0x%04X] 0x%04X\n",ra,mem[ra]);
return(0);
}
//-------------------------------------------------------------------
int main ( int argc, char *argv[] )
{
int ret;
show_fetches=0;
show_mem_writes=0;
show_mem_reads=0;
show_reg_writes=1;
show_reg_reads=0;
show_diss=1;
fetch_limit=100;
if(argc<2)
{
printf("mr16sim filename.csv\n");
return(1);
}
fpin=fopen(argv[1],"rt");
if(fpin==NULL)
{
printf("Error opening file [%s]\n",argv[1]);
return(1);
}
ret=read_csv();
fclose(fpin);
if(ret) return(1);
reset_sim();
sim();
printf("fetch_count %u\n",fetch_count);
printf("write_count %u\n",write_count);
printf("read_count %u\n",read_count);
return(0);
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------