OpenCores
URL https://opencores.org/ocsvn/ion/ion/trunk

Subversion Repositories ion

[/] [ion/] [trunk/] [tools/] [slite/] [src/] [slite.c] - Blame information for rev 7

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ja_rd
/*------------------------------------------------------------------------------
2
* slite.c -- MIPS-I simulator based on Steve Rhoad's "mlite"
3
*
4
* This is a slightly modified version of Steve Rhoad's "mlite" simulator, which
5
* is part of his PLASMA project (original date: 1/31/01).
6
*
7
*-------------------------------------------------------------------------------
8
* Usage:
9
*     slite <code file name> <data file name>
10
*
11
* The program will allocate a chunk of RAM (MEM_SIZE bytes) and map it to
12
* address 0x00000000 of the simulated CPU.
13
* Then it will read the 'code file' (as a big-endian plain binary) onto address
14
* 0x0 of the simulated CPU memory, and 'data file' on address 0x10000.
15
* Finally, will reset the CPU and enter the interactive debugger.
16
*
17
* (Note that the above is only necessary if the system does not have caches,
18
* because of the Harvard architecture. With caches in place, program loading
19
* would be antirely conventional).
20
*
21
* A simulation log file will be dumped to file "sw_sim_log.txt". This log can be
22
* used to compare with an equivalent log dumped by the hardware simulation, as
23
* a simple way to validate the hardware for a given program. See the project
24
* readme files for details.
25
*
26
*-------------------------------------------------------------------------------
27
* KNOWN BUGS:
28
*
29
* 1.- Load delay slot simulation does not work
30
*
31
* Some programs don't work when load delay slots are simulated (by setting
32
* "s->load_delay_slot = 1" in function main).
33
*
34
* The actual HW core does no longer use load delay slots but load interlocking
35
* so it may be better to just remove this feature.
36
*
37
*-------------------------------------------------------------------------------
38
* @date 2011-jan-16
39
*
40
*-------------------------------------------------------------------------------
41
* COPYRIGHT:    Software placed into the public domain by the author.
42
*               Software 'as is' without warranty.  Author liable for nothing.
43
*
44
* IMPORTANT: Assumes host is little endian.
45
*-----------------------------------------------------------------------------*/
46
 
47
#include <stdio.h>
48
#include <stdlib.h>
49
#include <string.h>
50
#include <ctype.h>
51
#include <assert.h>
52
 
53
/** Set to !=0 to disable file logging */
54
#define FILE_LOGGING_DISABLED (0)
55
/** Define to enable cache simulation (unimplemented) */
56
//#define ENABLE_CACHE
57
 
58
 
59
#define MEM_SIZE (1024*1024*2)
60
#define ntohs(A) ( ((A)>>8) | (((A)&0xff)<<8) )
61
#define htons(A) ntohs(A)
62
#define ntohl(A) ( ((A)>>24) | (((A)&0xff0000)>>8) | (((A)&0xff00)<<8) | ((A)<<24) )
63
#define htonl(A) ntohl(A)
64
 
65
/*---- OS-dependent support functions and definitions ------------------------*/
66
#ifndef WIN32
67
//Support for Linux
68
#define putch putchar
69
#include <termios.h>
70
#include <unistd.h>
71
 
72
void Sleep(unsigned int value)
73
{
74
   usleep(value * 1000);
75
}
76
 
77
int kbhit(void)
78
{
79
   struct termios oldt, newt;
80
   struct timeval tv;
81
   fd_set read_fd;
82
 
83
   tcgetattr(STDIN_FILENO, &oldt);
84
   newt = oldt;
85
   newt.c_lflag &= ~(ICANON | ECHO);
86
   tcsetattr(STDIN_FILENO, TCSANOW, &newt);
87
   tv.tv_sec=0;
88
   tv.tv_usec=0;
89
   FD_ZERO(&read_fd);
90
   FD_SET(0,&read_fd);
91
   if(select(1, &read_fd, NULL, NULL, &tv) == -1)
92
      return 0;
93
   //tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
94
   if(FD_ISSET(0,&read_fd))
95
      return 1;
96
   return 0;
97
}
98
 
99
int getch(void)
100
{
101
   struct termios oldt, newt;
102
   int ch;
103
 
104
   tcgetattr(STDIN_FILENO, &oldt);
105
   newt = oldt;
106
   newt.c_lflag &= ~(ICANON | ECHO);
107
   tcsetattr(STDIN_FILENO, TCSANOW, &newt);
108
   ch = getchar();
109
   //tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
110
   return ch;
111
}
112
#else
113
//Support for Windows
114
#include <conio.h>
115
extern void __stdcall Sleep(unsigned long value);
116
#endif
117
/*---- End of OS-dependent support functions and definitions -----------------*/
118
 
119
/*---- Hardware system parameters --------------------------------------------*/
120
 
121
/* Much of this is a remnant from Plasma's mlite and is  no longer used. */
122
/* FIXME Refactor HW system params */
123
 
124
 
125
#define UART_WRITE        0x20000000
126
#define UART_READ         0x20000000
127
#define IRQ_MASK          0x20000010
128
#define IRQ_STATUS        0x20000020
129
#define CONFIG_REG        0x20000070
130
#define MMU_PROCESS_ID    0x20000080
131
#define MMU_FAULT_ADDR    0x20000090
132
#define MMU_TLB           0x200000a0
133
 
134
#define IRQ_UART_READ_AVAILABLE  0x001
135
#define IRQ_UART_WRITE_AVAILABLE 0x002
136
#define IRQ_COUNTER18_NOT        0x004
137
#define IRQ_COUNTER18            0x008
138
#define IRQ_MMU                  0x200
139
 
140
#define MMU_ENTRIES 4
141
#define MMU_MASK (1024*4-1)
142 5 ja_rd
/*----------------------------------------------------------------------------*/
143 2 ja_rd
 
144 5 ja_rd
/* These are flags that will be used to notify the main cycle function of any
145
   failed assertions in its subfunctions. */
146
#define ASRT_UNALIGNED_READ         (1<<0)
147
#define ASRT_UNALIGNED_WRITE        (1<<1)
148
 
149
char *assertion_messages[2] = {
150
   "Unaligned read",
151
   "Unaligned write"
152
};
153
 
154
 
155 2 ja_rd
/** Length of debugging jump target queue */
156
#define TRACE_BUFFER_SIZE (32)
157
 
158
typedef struct {
159
   unsigned int buf[TRACE_BUFFER_SIZE];   /**< queue of last jump targets */
160
   unsigned int next;                     /**< internal queue head pointer */
161
   FILE *log;                             /**< text log file or NULL */
162
   int pr[32];                            /**< last value of register bank */
163
   int hi, lo, epc;                       /**< last value of internal regs */
164
} Trace;
165
 
166
typedef struct
167
{
168
   unsigned int virtualAddress;
169
   unsigned int physicalAddress;
170
} MmuEntry;
171
 
172
typedef struct {
173
   int load_delay_slot;
174
   int delay;
175 5 ja_rd
   unsigned delayed_reg;
176
   int delayed_data;
177
 
178
   unsigned failed_assertions;            /**< assertion bitmap */
179
   unsigned faulty_address;               /**< addr that failed assertion */
180
 
181 2 ja_rd
   int r[32];
182
   int opcode;
183
   int pc, pc_next, epc;
184
   unsigned int hi;
185
   unsigned int lo;
186
   int status;
187
   int userMode;
188
   int processId;
189 5 ja_rd
   int exceptionId;        /**< DEPRECATED, to be removed */
190 2 ja_rd
   int faultAddr;
191
   int irqStatus;
192
   int skip;
193
   Trace t;
194
   unsigned char *mem;
195
   int wakeup;
196
   int big_endian;
197
   MmuEntry mmuEntry[MMU_ENTRIES];
198
} State;
199
 
200
static char *opcode_string[]={
201
   "SPECIAL","REGIMM","J","JAL","BEQ","BNE","BLEZ","BGTZ",
202
   "ADDI","ADDIU","SLTI","SLTIU","ANDI","ORI","XORI","LUI",
203
   "COP0","COP1","COP2","COP3","BEQL","BNEL","BLEZL","BGTZL",
204
   "?","?","?","?","?","?","?","?",
205
   "LB","LH","LWL","LW","LBU","LHU","LWR","?",
206
   "SB","SH","SWL","SW","?","?","SWR","CACHE",
207
   "LL","LWC1","LWC2","LWC3","?","LDC1","LDC2","LDC3"
208
   "SC","SWC1","SWC2","SWC3","?","SDC1","SDC2","SDC3"
209
};
210
 
211
static char *special_string[]={
212
   "SLL","?","SRL","SRA","SLLV","?","SRLV","SRAV",
213
   "JR","JALR","MOVZ","MOVN","SYSCALL","BREAK","?","SYNC",
214
   "MFHI","MTHI","MFLO","MTLO","?","?","?","?",
215
   "MULT","MULTU","DIV","DIVU","?","?","?","?",
216
   "ADD","ADDU","SUB","SUBU","AND","OR","XOR","NOR",
217
   "?","?","SLT","SLTU","?","DADDU","?","?",
218
   "TGE","TGEU","TLT","TLTU","TEQ","?","TNE","?",
219
   "?","?","?","?","?","?","?","?"
220
};
221
 
222
static char *regimm_string[]={
223
   "BLTZ","BGEZ","BLTZL","BGEZL","?","?","?","?",
224
   "TGEI","TGEIU","TLTI","TLTIU","TEQI","?","TNEI","?",
225
   "BLTZAL","BEQZAL","BLTZALL","BGEZALL","?","?","?","?",
226
   "?","?","?","?","?","?","?","?"
227
};
228
 
229
static unsigned int HWMemory[8];
230
 
231
/*---- Local function prototypes ---------------------------------------------*/
232
 
233
/* Debug and logging */
234
void init_trace_buffer(State *s, const char *log_file_name);
235
void close_trace_buffer(State *s);
236
void dump_trace_buffer(State *s);
237
void log_cycle(State *s);
238
void log_read(State *s, int full_address, int word_value, int size, int log);
239 5 ja_rd
void log_failed_assertions(State *s);
240 2 ja_rd
 
241
/* Hardware simulation */
242
int mem_read(State *s, int size, unsigned int address, int log);
243
void mem_write(State *s, int size, int unsigned address, unsigned value);
244
void start_load(State *s, int rt, int data);
245
 
246
/*---- Local functions -------------------------------------------------------*/
247
 
248
/** Log to file a memory read operation (not including target reg change) */
249
void log_read(State *s, int full_address, int word_value, int size, int log){
250
   if(s->t.log!=NULL && log!=0){
251
      if(size==4){ size=0x04; }
252
      else if(size==2){ size=0x02; }
253
      else { size=0x01; }
254
      fprintf(s->t.log, "(%08X) [%08X] <**>=%08X RD\n",
255
              s->pc, full_address, /*size,*/ word_value);
256
   }
257
}
258
 
259
/** Read memory, optionally logging */
260
int mem_read(State *s, int size, unsigned int address, int log)
261
{
262
   unsigned int value=0, word_value=0, ptr;
263
   unsigned int full_address = address;
264
 
265
   s->irqStatus |= IRQ_UART_WRITE_AVAILABLE;
266
   switch(address)
267
   {
268
      case UART_READ:
269
         word_value = 0x00000001;
270
         log_read(s, full_address, word_value, size, log);
271
         return word_value;
272
         /* FIXME Take input from text file */
273
         /*
274
         if(kbhit()){
275
            HWMemory[0] = getch();
276
         }
277
         s->irqStatus &= ~IRQ_UART_READ_AVAILABLE; //clear bit
278
         return HWMemory[0];
279
         */
280
      case IRQ_MASK:
281
         return HWMemory[1];
282
      case IRQ_MASK + 4:
283
         Sleep(10);
284
         return 0;
285
      case IRQ_STATUS:
286
         /*if(kbhit())
287
            s->irqStatus |= IRQ_UART_READ_AVAILABLE;
288
         return s->irqStatus;
289
         */
290
         /* FIXME Optionally simulate UART TX delay */
291
         word_value = 0x00000003; /* Ready to TX and RX */
292
         log_read(s, full_address, word_value, size, log);
293
         return word_value;
294
      case MMU_PROCESS_ID:
295
         return s->processId;
296
      case MMU_FAULT_ADDR:
297
         return s->faultAddr;
298
   }
299
 
300
   ptr = (unsigned int)s->mem + (address % MEM_SIZE);
301
 
302
   if(0x10000000 <= address && address < 0x10000000 + 1024*1024)
303
      ptr += 1024*1024;
304
 
305
   word_value = *(int*)(ptr&0xfffffffc);
306
   if(s->big_endian)
307
            word_value = ntohl(word_value);
308
   switch(size)
309
   {
310
      case 4:
311
         if(address & 3){
312
            printf("Unaligned access PC=0x%x address=0x%x\n",
313
                   (int)s->pc, (int)address);
314 5 ja_rd
         }
315
         /* We don't want the program to just quit, we want to log the fault */
316
         /* assert((address & 3) == 0); */
317
         if((address & 3) != 0){
318
            s->failed_assertions |= ASRT_UNALIGNED_READ;
319
            s->faulty_address = address;
320
            address = address & 0xfffffffc;
321 2 ja_rd
         }
322
         value = *(int*)ptr;
323
         if(s->big_endian)
324
            value = ntohl(value);
325
         break;
326 5 ja_rd
      case 2:
327
         /* We don't want the program to just quit, we want to log the fault */
328
         /* assert((address & 1) == 0); */
329
         if((address & 1) != 0){
330
            s->failed_assertions |= ASRT_UNALIGNED_READ;
331
            s->faulty_address = address;
332
            address = address & 0xfffffffe;
333
         }
334 2 ja_rd
         value = *(unsigned short*)ptr;
335
         if(s->big_endian)
336
            value = ntohs((unsigned short)value);
337
         break;
338
      case 1:
339
         value = *(unsigned char*)ptr;
340
         break;
341
      default:
342
         printf("ERROR");
343
   }
344
 
345
   log_read(s, full_address, word_value, size, log);
346
   return(value);
347
}
348
 
349
/** Write memory */
350
void mem_write(State *s, int size, unsigned address, unsigned value)
351
{
352
   unsigned int ptr, mask, dvalue, b0, b1, b2, b3;
353
 
354
   if(s->t.log!=NULL){
355
      b0 = value & 0x000000ff;
356
      b1 = value & 0x0000ff00;
357
      b2 = value & 0x00ff0000;
358
      b3 = value & 0xff000000;
359
 
360
      switch(size){
361
         case 4:  mask = 0x0f;
362
                  dvalue = value;
363
                  break;
364
 
365
         case 2:  if((address&0x2)==0){
366
                     mask = 0xc;
367
                     dvalue = b1<<16 | b0<<16;
368
                  }
369
                  else{
370
                     mask = 0x3;
371
                     dvalue = b1 | b0;
372
                  }
373
                  break;
374
         case 1:  switch(address%4){
375
                  case 0 : mask = 0x8;
376
                           dvalue = b0<<24;
377
                           break;
378
                  case 1 : mask = 0x4;
379
                           dvalue = b0<<16;
380
                           break;
381
                  case 2 : mask = 0x2;
382
                           dvalue = b0<<8;
383
                           break;
384
                  case 3 : mask = 0x1;
385
                           dvalue = b0;
386
                           break;
387
                  }
388
                  break;
389
         default:
390
            printf("BUG: mem write size invalid (%08x)\n", s->pc);
391
            exit(2);
392
 
393
      }
394
      fprintf(s->t.log, "(%08X) [%08X] |%02X|=%08X WR\n", s->pc, address, mask, dvalue);
395
   }
396
 
397
   switch(address)
398
   {
399
      case UART_WRITE:
400
         putch(value);
401
         fflush(stdout);
402
         return;
403
      case IRQ_MASK:
404
         HWMemory[1] = value;
405
         return;
406
      case IRQ_STATUS:
407
         s->irqStatus = value;
408
         return;
409
      case CONFIG_REG:
410
         return;
411
      case MMU_PROCESS_ID:
412
         //printf("processId=%d\n", value);
413
         s->processId = value;
414
         return;
415
   }
416
 
417
   if(MMU_TLB <= address && address <= MMU_TLB+MMU_ENTRIES * 8)
418
   {
419
      //printf("TLB 0x%x 0x%x\n", address - MMU_TLB, value);
420
      ptr = (unsigned int)s->mmuEntry + address - MMU_TLB;
421
      *(int*)ptr = value;
422
      s->irqStatus &= ~IRQ_MMU;
423
      return;
424
   }
425
 
426
   ptr = (unsigned int)s->mem + (address % MEM_SIZE);
427
 
428
   if(0x10000000 <= address && address < 0x10000000 + 1024*1024)
429
      ptr += 1024*1024;
430
 
431
   switch(size)
432
   {
433 5 ja_rd
      case 4:
434
         /* We don't want the program to just quit, we want to log the fault */
435
         /* assert((address & 3) == 0); */
436
         if((address & 3) != 0){
437
            s->failed_assertions |= ASRT_UNALIGNED_WRITE;
438
            s->faulty_address = address;
439
            address = address & (~0x03);
440
         }
441 2 ja_rd
         if(s->big_endian)
442
            value = htonl(value);
443
         *(int*)ptr = value;
444
         break;
445 5 ja_rd
      case 2:
446
         /* We don't want the program to just quit, we want to log the fault */
447
         /* assert((address & 1) == 0); */
448
         if((address & 1) != 0){
449
            s->failed_assertions |= ASRT_UNALIGNED_WRITE;
450
            s->faulty_address = address;
451
            address = address & (~0x01);
452
         }
453 2 ja_rd
         if(s->big_endian)
454
            value = htons((unsigned short)value);
455
         *(short*)ptr = (unsigned short)value;
456
         break;
457
      case 1:
458
         *(char*)ptr = (unsigned char)value;
459
         break;
460
      default:
461
         printf("ERROR");
462
   }
463
}
464
 
465
/*---- Optional MMU and cache implementation ---------------------------------*/
466
 
467
/*
468
   The actual core does not have a cache so all of the original Plasma mlite.c
469
   code for cache simulation has been removed.
470
*/
471
 
472
#ifdef ENABLE_CACHE
473
#error Cache simulation missing!
474
#else
475
static void cache_init(void) {}
476
#endif
477
/*---- End optional cache implementation -------------------------------------*/
478
 
479
 
480
/** Simulates MIPS-I multiplier unsigned behavior*/
481
void mult_big(unsigned int a,
482
              unsigned int b,
483
              unsigned int *hi,
484
              unsigned int *lo)
485
{
486
   unsigned int ahi, alo, bhi, blo;
487
   unsigned int c0, c1, c2;
488
   unsigned int c1_a, c1_b;
489
 
490
   ahi = a >> 16;
491
   alo = a & 0xffff;
492
   bhi = b >> 16;
493
   blo = b & 0xffff;
494
 
495
   c0 = alo * blo;
496
   c1_a = ahi * blo;
497
   c1_b = alo * bhi;
498
   c2 = ahi * bhi;
499
 
500
   c2 += (c1_a >> 16) + (c1_b >> 16);
501
   c1 = (c1_a & 0xffff) + (c1_b & 0xffff) + (c0 >> 16);
502
   c2 += (c1 >> 16);
503
   c0 = (c1 << 16) + (c0 & 0xffff);
504
   *hi = c2;
505
   *lo = c0;
506
}
507
 
508
/** Simulates MIPS-I multiplier signed behavior*/
509
void mult_big_signed(int a,
510
                     int b,
511
                     unsigned int *hi,
512
                     unsigned int *lo)
513
{
514
   unsigned int ahi, alo, bhi, blo;
515
   unsigned int c0, c1, c2;
516
   unsigned int c1_a, c1_b;
517
 
518
   ahi = a >> 16;
519
   alo = a & 0xffff;
520
   bhi = b >> 16;
521
   blo = b & 0xffff;
522
 
523
   c0 = alo * blo;
524
   c1_a = ahi * blo;
525
   c1_b = alo * bhi;
526
   c2 = ahi * bhi;
527
 
528
   c2 += (c1_a >> 16) + (c1_b >> 16);
529
   c1 = (c1_a & 0xffff) + (c1_b & 0xffff) + (c0 >> 16);
530
   c2 += (c1 >> 16);
531
   c0 = (c1 << 16) + (c0 & 0xffff);
532
   *hi = c2;
533
   *lo = c0;
534
}
535
 
536
/** Load data from memory (used to simulate load delay slots) */
537
void start_load(State *s, int rt, int data){
538
   if(s->load_delay_slot){
539
      s->delayed_reg = rt;
540
      s->delay = 1;
541
      s->delayed_data = data;
542
   }
543
   else{
544
      /* load delay slot not simulated */
545
      s->r[rt] = data;
546
   }
547
}
548
 
549
/** Execute one cycle of the CPU (including any interlock stall cycles) */
550
void cycle(State *s, int show_mode)
551
{
552
   unsigned int opcode;
553
   unsigned int op, rs, rt, rd, re, func, imm, target;
554
   int imm_sex;
555
   int imm_shift, branch=0, lbranch=2, skip2=0;
556
   int *r=s->r;
557
   unsigned int *u=(unsigned int*)s->r;
558
   unsigned int ptr, epc, rSave;
559
 
560
   if(!show_mode){
561
      log_cycle(s);
562
   }
563
 
564
   opcode = mem_read(s, 4, s->pc, 0);
565
   op = (opcode >> 26) & 0x3f;
566
   rs = (opcode >> 21) & 0x1f;
567
   rt = (opcode >> 16) & 0x1f;
568
   rd = (opcode >> 11) & 0x1f;
569
   re = (opcode >> 6) & 0x1f;
570
   func = opcode & 0x3f;
571
   imm = opcode & 0xffff;
572
   imm_sex = (imm&0x8000)? 0xffff0000 | imm : imm;
573
   imm_shift = (((int)(short)imm) << 2) - 4;
574
   target = (opcode << 6) >> 4;
575
   ptr = (short)imm + r[rs];
576
   r[0] = 0;
577
   if(show_mode)
578
   {
579
      printf("%8.8x %8.8x ", s->pc, opcode);
580
      if(op == 0)
581
         printf("%8s ", special_string[func]);
582
      else if(op == 1)
583
         printf("%8s ", regimm_string[rt]);
584
      else
585
         printf("%8s ", opcode_string[op]);
586
      printf("$%2.2d $%2.2d $%2.2d $%2.2d ", rs, rt, rd, re);
587
      printf("%4.4x", imm);
588
      if(show_mode == 1)
589
         printf(" r[%2.2d]=%8.8x r[%2.2d]=%8.8x", rs, r[rs], rt, r[rt]);
590
      printf("\n");
591
   }
592
   if(show_mode > 5)
593 7 ja_rd
      return;
594
   /* epc will point to the victim instruction, i.e. THIS instruction */
595
   epc = s->pc;
596
   if(s->pc_next != s->pc + 4){
597
      /* FIXME traps in delay slots not supported yet */
598
      //epc = epc - 4;  /* trap in branch delay slot */
599
   }
600 2 ja_rd
 
601
   s->pc = s->pc_next;
602
   s->pc_next = s->pc_next + 4;
603
   if(s->skip)
604
   {
605
      s->skip = 0;
606
      return;
607
   }
608
   rSave = r[rt];
609
   switch(op)
610
   {
611
      case 0x00:/*SPECIAL*/
612
         switch(func)
613
         {
614
            case 0x00:/*SLL*/  r[rd]=r[rt]<<re;          break;
615
            case 0x02:/*SRL*/  r[rd]=u[rt]>>re;          break;
616
            case 0x03:/*SRA*/  r[rd]=r[rt]>>re;          break;
617
            case 0x04:/*SLLV*/ r[rd]=r[rt]<<r[rs];       break;
618
            case 0x06:/*SRLV*/ r[rd]=u[rt]>>r[rs];       break;
619
            case 0x07:/*SRAV*/ r[rd]=r[rt]>>r[rs];       break;
620
            case 0x08:/*JR*/   s->pc_next=r[rs];         break;
621
            case 0x09:/*JALR*/ r[rd]=s->pc_next; s->pc_next=r[rs]; break;
622
            case 0x0a:/*MOVZ*/ if(!r[rt]) r[rd]=r[rs];   break;  /*IV*/
623
            case 0x0b:/*MOVN*/ if(r[rt]) r[rd]=r[rs];    break;  /*IV*/
624
            case 0x0c:/*SYSCALL*/ /* epc|=1; WHY? */
625
                                  s->exceptionId=1; break;
626
            case 0x0d:/*BREAK*/   /* epc|=1; WHY? */
627
                                  s->exceptionId=1; break;
628
            case 0x0f:/*SYNC*/ s->wakeup=1;              break;
629
            case 0x10:/*MFHI*/ r[rd]=s->hi;              break;
630
            case 0x11:/*FTHI*/ s->hi=r[rs];              break;
631
            case 0x12:/*MFLO*/ r[rd]=s->lo;              break;
632
            case 0x13:/*MTLO*/ s->lo=r[rs];              break;
633
            case 0x18:/*MULT*/ mult_big_signed(r[rs],r[rt],&s->hi,&s->lo); break;
634
            case 0x19:/*MULTU*/ mult_big(r[rs],r[rt],&s->hi,&s->lo); break;
635
            case 0x1a:/*DIV*/  s->lo=r[rs]/r[rt]; s->hi=r[rs]%r[rt]; break;
636
            case 0x1b:/*DIVU*/ s->lo=u[rs]/u[rt]; s->hi=u[rs]%u[rt]; break;
637
            case 0x20:/*ADD*/  r[rd]=r[rs]+r[rt];        break;
638
            case 0x21:/*ADDU*/ r[rd]=r[rs]+r[rt];        break;
639
            case 0x22:/*SUB*/  r[rd]=r[rs]-r[rt];        break;
640
            case 0x23:/*SUBU*/ r[rd]=r[rs]-r[rt];        break;
641
            case 0x24:/*AND*/  r[rd]=r[rs]&r[rt];        break;
642
            case 0x25:/*OR*/   r[rd]=r[rs]|r[rt];        break;
643
            case 0x26:/*XOR*/  r[rd]=r[rs]^r[rt];        break;
644
            case 0x27:/*NOR*/  r[rd]=~(r[rs]|r[rt]);     break;
645
            case 0x2a:/*SLT*/  r[rd]=r[rs]<r[rt];        break;
646
            case 0x2b:/*SLTU*/ r[rd]=u[rs]<u[rt];        break;
647
            case 0x2d:/*DADDU*/r[rd]=r[rs]+u[rt];        break;
648
            case 0x31:/*TGEU*/ break;
649
            case 0x32:/*TLT*/  break;
650
            case 0x33:/*TLTU*/ break;
651
            case 0x34:/*TEQ*/  break;
652
            case 0x36:/*TNE*/  break;
653
            default: printf("ERROR0(*0x%x~0x%x)\n", s->pc, opcode);
654
               s->wakeup=1;
655
         }
656
         break;
657
      case 0x01:/*REGIMM*/
658
         switch(rt) {
659
            case 0x10:/*BLTZAL*/ r[31]=s->pc_next;
660
            case 0x00:/*BLTZ*/   branch=r[rs]<0;    break;
661
            case 0x11:/*BGEZAL*/ r[31]=s->pc_next;
662
            case 0x01:/*BGEZ*/   branch=r[rs]>=0;   break;
663
            case 0x12:/*BLTZALL*/r[31]=s->pc_next;
664
            case 0x02:/*BLTZL*/  lbranch=r[rs]<0;   break;
665
            case 0x13:/*BGEZALL*/r[31]=s->pc_next;
666
            case 0x03:/*BGEZL*/  lbranch=r[rs]>=0;  break;
667
            default: printf("ERROR1\n"); s->wakeup=1;
668
          }
669
         break;
670
      case 0x03:/*JAL*/    r[31]=s->pc_next;
671
      case 0x02:/*J*/      s->pc_next=(s->pc&0xf0000000)|target; break;
672
      case 0x04:/*BEQ*/    branch=r[rs]==r[rt];     break;
673
      case 0x05:/*BNE*/    branch=r[rs]!=r[rt];     break;
674
      case 0x06:/*BLEZ*/   branch=r[rs]<=0;         break;
675
      case 0x07:/*BGTZ*/   branch=r[rs]>0;          break;
676
      case 0x08:/*ADDI*/   r[rt]=r[rs]+(short)imm;  break;
677
      case 0x09:/*ADDIU*/  u[rt]=u[rs]+(short)imm;  break;
678
      case 0x0a:/*SLTI*/   r[rt]=r[rs]<(short)imm;  break;
679
      case 0x0b:/*SLTIU*/  u[rt]=u[rs]<(unsigned int)(short)imm; break;
680
      case 0x0c:/*ANDI*/   r[rt]=r[rs]&imm;         break;
681
      case 0x0d:/*ORI*/    r[rt]=r[rs]|imm;         break;
682
      case 0x0e:/*XORI*/   r[rt]=r[rs]^imm;         break;
683
      case 0x0f:/*LUI*/    r[rt]=(imm<<16);         break;
684
      case 0x10:/*COP0*/
685
         if((opcode & (1<<23)) == 0)  //move from CP0
686
         {
687
            if(rd == 12)
688
               r[rt]=s->status;
689
            else
690
               r[rt]=s->epc;
691
         }
692
         else                         //move to CP0
693
         {
694
            s->status=r[rt]&1;
695
            if(s->processId && (r[rt]&2))
696
            {
697
               s->userMode|=r[rt]&2;
698
               //printf("CpuStatus=%d %d %d\n", r[rt], s->status, s->userMode);
699
               //s->wakeup = 1;
700
               //printf("pc=0x%x\n", epc);
701
            }
702
         }
703
         break;
704
//      case 0x11:/*COP1*/ break;
705
//      case 0x12:/*COP2*/ break;
706
//      case 0x13:/*COP3*/ break;
707
      case 0x14:/*BEQL*/   lbranch=r[rs]==r[rt];    break;
708
      case 0x15:/*BNEL*/   lbranch=r[rs]!=r[rt];    break;
709
      case 0x16:/*BLEZL*/  lbranch=r[rs]<=0;        break;
710
      case 0x17:/*BGTZL*/  lbranch=r[rs]>0;         break;
711
//      case 0x1c:/*MAD*/  break;   /*IV*/
712
      case 0x20:/*LB*/     //r[rt]=(signed char)mem_read(s,1,ptr,1);  break;
713
                           start_load(s, rt,(signed char)mem_read(s,1,ptr,1));
714
                           break;
715
 
716
      case 0x21:/*LH*/     //r[rt]=(signed short)mem_read(s,2,ptr,1); break;
717
                           start_load(s, rt, (signed short)mem_read(s,2,ptr,1));
718
                           break;
719
      case 0x22:/*LWL*/    //target=8*(ptr&3);
720
                           //r[rt]=(r[rt]&~(0xffffffff<<target))|
721
                           //      (mem_read(s,4,ptr&~3)<<target); break;
722
                           break;
723
      case 0x23:/*LW*/     //r[rt]=mem_read(s,4,ptr,1);   break;
724
                           start_load(s, rt, mem_read(s,4,ptr,1));
725
                           break;
726
      case 0x24:/*LBU*/    //r[rt]=(unsigned char)mem_read(s,1,ptr,1); break;
727
                           start_load(s, rt, (unsigned char)mem_read(s,1,ptr,1));
728
                           break;
729
      case 0x25:/*LHU*/    //r[rt]= (unsigned short)mem_read(s,2,ptr,1);
730
                           start_load(s, rt, (unsigned short)mem_read(s,2,ptr,1));
731
                           break;
732
      case 0x26:/*LWR*/
733
                           //target=32-8*(ptr&3);
734
                           //r[rt]=(r[rt]&~((unsigned int)0xffffffff>>target))|
735
                           //((unsigned int)mem_read(s,4,ptr&~3)>>target);
736
                           break;
737
      case 0x28:/*SB*/     mem_write(s,1,ptr,r[rt]);  break;
738
      case 0x29:/*SH*/     mem_write(s,2,ptr,r[rt]);  break;
739
      case 0x2a:/*SWL*/
740
                           //mem_write(s,1,ptr,r[rt]>>24);
741
                           //mem_write(s,1,ptr+1,r[rt]>>16);
742
                           //mem_write(s,1,ptr+2,r[rt]>>8);
743
                           //mem_write(s,1,ptr+3,r[rt]); break;
744
      case 0x2b:/*SW*/     mem_write(s,4,ptr,r[rt]);  break;
745
      case 0x2e:/*SWR*/    break; //fixme
746
      case 0x2f:/*CACHE*/  break;
747
      case 0x30:/*LL*/     //r[rt]=mem_read(s,4,ptr);   break;
748
                           start_load(s, rt, mem_read(s,4,ptr,1));
749
                           break;
750
 
751
//      case 0x31:/*LWC1*/ break;
752
//      case 0x32:/*LWC2*/ break;
753
//      case 0x33:/*LWC3*/ break;
754
//      case 0x35:/*LDC1*/ break;
755
//      case 0x36:/*LDC2*/ break;
756
//      case 0x37:/*LDC3*/ break;
757
//      case 0x38:/*SC*/     *(int*)ptr=r[rt]; r[rt]=1; break;
758
      case 0x38:/*SC*/     mem_write(s,4,ptr,r[rt]); r[rt]=1; break;
759
//      case 0x39:/*SWC1*/ break;
760
//      case 0x3a:/*SWC2*/ break;
761
//      case 0x3b:/*SWC3*/ break;
762
//      case 0x3d:/*SDC1*/ break;
763
//      case 0x3e:/*SDC2*/ break;
764
//      case 0x3f:/*SDC3*/ break;
765
      default: printf("ERROR2 address=0x%x opcode=0x%x\n", s->pc, opcode);
766
         s->wakeup=1;
767
   }
768
   s->pc_next += (branch || lbranch == 1) ? imm_shift : 0;
769
   s->pc_next &= ~3;
770
   s->skip = (lbranch == 0) | skip2;
771
 
772 5 ja_rd
   /* If there was trouble, log it */
773
   if(s->failed_assertions!=0){
774
      log_failed_assertions(s);
775
      s->failed_assertions=0;
776
   }
777
 
778
 
779 2 ja_rd
   /* if there's a delayed load pending, do it now: load reg with memory data */
780
   if(s->load_delay_slot){
781
      /*--- simulate real core's load interlock ---*/
782
      if(s->delay<=0){
783
         if(s->delayed_reg>0){
784
            s->r[s->delayed_reg] = s->delayed_data;
785
         }
786
         s->delayed_reg = 0;
787
      }
788
      else{
789
         s->delay = s->delay - 1;
790
      }
791
   }
792
   else{
793
      /*--- delay slot or interlocking not simulated ---*/
794
      /*
795
      if(s->delay){
796
         r[s->delayed_reg] = s->delayed_data;
797
         s->delay=0;
798
      }
799
      */
800
   }
801
 
802 7 ja_rd
   /* Handle exceptions */
803 2 ja_rd
   if(s->exceptionId)
804
   {
805
      r[rt] = rSave;
806
      s->epc = epc;
807
      s->pc_next = 0x3c;
808
      s->skip = 1;
809
      s->exceptionId = 0;
810
      s->userMode = 0;
811
      //s->wakeup = 1;
812
      return;
813
   }
814
}
815
 
816
/** Dump CPU state to console */
817
void show_state(State *s)
818
{
819
   int i,j;
820
   printf("pid=%d userMode=%d, epc=0x%x\n", s->processId, s->userMode, s->epc);
821
   for(i = 0; i < 4; ++i)
822
   {
823
      printf("%2.2d ", i * 8);
824
      for(j = 0; j < 8; ++j)
825
      {
826
         printf("%8.8x ", s->r[i*8+j]);
827
      }
828
      printf("\n");
829
   }
830
   //printf("%8.8lx %8.8lx %8.8lx %8.8lx\n", s->pc, s->pc_next, s->hi, s->lo);
831
   j = s->pc;
832
   for(i = -4; i <= 8; ++i)
833
   {
834
      printf("%c", i==0 ? '*' : ' ');
835
      s->pc = j + i * 4;
836
      cycle(s, 10);
837
   }
838
   s->pc = j;
839
}
840
 
841
/** Show debug monitor prompt and execute user command */
842
void do_debug(State *s)
843
{
844
   int ch;
845
   int i, j=0, watch=0, addr;
846
   s->pc_next = s->pc + 4;
847
   s->skip = 0;
848
   s->delayed_reg = 0;
849
   s->wakeup = 0;
850
   show_state(s);
851
   ch = ' ';
852
   for(;;)
853
   {
854
      if(ch != 'n')
855
      {
856
         if(watch)
857
            printf("0x%8.8x=0x%8.8x\n", watch, mem_read(s, 4, watch,0));
858
         printf("1=Debug 2=Trace 3=Step 4=BreakPt 5=Go 6=Memory ");
859
         printf("7=Watch 8=Jump 9=Quit A=dump > ");
860
      }
861
      ch = getch();
862
      if(ch != 'n')
863
         printf("\n");
864
      switch(ch)
865
      {
866
      case 'a': case 'A':
867
         dump_trace_buffer(s); break;
868
      case '1': case 'd': case ' ':
869
         cycle(s, 0); show_state(s); break;
870
      case 'n':
871
         cycle(s, 1); break;
872
      case '2': case 't':
873
         cycle(s, 0); printf("*"); cycle(s, 10); break;
874
      case '3': case 's':
875
         printf("Count> ");
876
         scanf("%d", &j);
877
         for(i = 0; i < j; ++i)
878
            cycle(s, 1);
879
         show_state(s);
880
         break;
881
      case '4': case 'b':
882
         printf("Line> ");
883
         scanf("%x", &j);
884
         printf("break point=0x%x\n", j);
885
         break;
886
      case '5': case 'g':
887
         s->wakeup = 0;
888
         cycle(s, 0);
889
         while(s->wakeup == 0)
890
         {
891
            if(s->pc == j){
892
               printf("\n\nStop: pc = 0x%08x\n\n", j);
893
               break;
894
            }
895
            cycle(s, 0);
896
         }
897
         show_state(s);
898
         break;
899
      case 'G':
900
         s->wakeup = 0;
901
         cycle(s, 1);
902
         while(s->wakeup == 0)
903
         {
904
            if(s->pc == j)
905
               break;
906
            cycle(s, 1);
907
         }
908
         show_state(s);
909
         break;
910
      case '6': case 'm':
911
         printf("Memory> ");
912
         scanf("%x", &j);
913
         for(i = 0; i < 8; ++i)
914
         {
915
            printf("%8.8x ", mem_read(s, 4, j+i*4, 0));
916
         }
917
         printf("\n");
918
         break;
919
      case '7': case 'w':
920
         printf("Watch> ");
921
         scanf("%x", &watch);
922
         break;
923
      case '8': case 'j':
924
         printf("Jump> ");
925
         scanf("%x", &addr);
926
         s->pc = addr;
927
         s->pc_next = addr + 4;
928
         show_state(s);
929
         break;
930
      case '9': case 'q':
931
         return;
932
      }
933
   }
934
}
935
 
936
/** Read binary code and data files */
937
int read_program(State *s, char *code_name, char *data_name){
938
   FILE *in;
939
   int bytes, i;
940
   unsigned char *buffer;
941
 
942
   /* Read code file (.text + .reginfo + .rodata) */
943
   in = fopen(code_name, "rb");
944
   if(in == NULL)
945
   {
946
      printf("Can't open file %s!\n",code_name);
947
      getch();
948
      return(0);
949
   }
950
   bytes = fread(s->mem, 1, MEM_SIZE, in);
951
   fclose(in);
952
   in = NULL;
953
   printf("Code [size= %6d, start= 0x%08x]\n", bytes, 0);
954
 
955
   /* Read data file (.data + .bss) if necessary */
956
   if(data_name!=NULL){
957
      in = fopen(data_name, "rb");
958
      if(in == NULL)
959
      {
960
         printf("Can't open file %s!\n",data_name);
961
         getch();
962
         return(0);
963
      }
964
      /* FIXME Section '.data' start hardcoded to 0x10000 */
965
      buffer = (unsigned char*)malloc(MEM_SIZE);
966
 
967
      bytes = fread(buffer, 1, MEM_SIZE, in);
968
      fclose(in);
969
      printf("Data [size= %6d, start= 0x%08x]\n", bytes, 0x10000);
970
 
971
      for(i=0;i<bytes;i++){
972
         s->mem[0x10000+i] = buffer[i];
973
      }
974
      free(buffer);
975
   }
976
 
977
   return 1;
978
}
979
 
980
/*----------------------------------------------------------------------------*/
981
 
982
int main(int argc,char *argv[])
983
{
984
   State state, *s=&state;
985
   int index;
986
   char *code_filename;
987
   char *data_filename;
988
 
989
   printf("MIPS-I emulator\n");
990
   memset(s, 0, sizeof(State));
991
   s->big_endian = 1;
992
   s->load_delay_slot = 0;
993
   s->mem = (unsigned char*)malloc(MEM_SIZE);
994
   memset(s->mem, 0, MEM_SIZE);
995
 
996
 
997
   if(argc==3){
998
      code_filename = argv[1];
999
      data_filename = argv[2];
1000
   }
1001
   else if(argc==2){
1002
      code_filename = argv[1];
1003
      data_filename = NULL;
1004
   }
1005
   else{
1006
      printf("Usage:");
1007
      printf("    slite file.exe <bin code file> [<bin data file>]\n");
1008
      return 0;
1009
   }
1010
 
1011
   if(!read_program(s, code_filename, data_filename)) return 0;
1012
 
1013
   init_trace_buffer(s, "sw_sim_log.txt");
1014
   memcpy(s->mem + 1024*1024, s->mem, 1024*1024);  //internal 8KB SRAM
1015
   cache_init(); /* stub */
1016
 
1017
   /* NOTE: Original mlite supported loading little-endian code, which this
1018
      program doesn't. The endianess-conversion code has been removed.
1019
   */
1020
 
1021 5 ja_rd
   /* Simulate a CPU reset */
1022
   /* FIXME cpu reset function needed */
1023
   s->pc = 0x0;      /* reset start vector */
1024
   s->failed_assertions = 0; /* no failed assertions pending */
1025
 
1026 2 ja_rd
   /* FIXME PC fixup at address zero has to be removed */
1027
   index = mem_read(s, 4, 0, 0);
1028
   if((index & 0xffffff00) == 0x3c1c1000){
1029
      s->pc = 0x10000000;
1030
   }
1031
 
1032
   /* Enter debug command interface; will only exit clean with user command */
1033
   do_debug(s);
1034
 
1035
   /* Close and deallocate everything and quit */
1036
   close_trace_buffer(s);
1037
   free(s->mem);
1038
   return(0);
1039
}
1040
 
1041
/*----------------------------------------------------------------------------*/
1042
 
1043
 
1044
void init_trace_buffer(State *s, const char *log_file_name){
1045
   int i;
1046
 
1047
#if FILE_LOGGING_DISABLED
1048
   s->t.log = NULL;
1049
   return;
1050
#else
1051
   for(i=0;i<TRACE_BUFFER_SIZE;i++){
1052
      s->t.buf[i]=0xffffffff;
1053
   }
1054
   s->t.next = 0;
1055
 
1056
   /* if file logging is enabled, open log file */
1057
   if(log_file_name!=NULL){
1058
      s->t.log = fopen(log_file_name, "w");
1059
      if(s->t.log==NULL){
1060
         printf("Error opening log file '%s', file logging disabled\n", log_file_name);
1061
      }
1062
   }
1063
   else{
1064
      s->t.log = NULL;
1065
   }
1066
#endif
1067
}
1068
 
1069
/** Dumps last jump targets as a chunk of hex numbers (older is left top) */
1070
void dump_trace_buffer(State *s){
1071
   int i, col;
1072
 
1073
   for(i=0, col=0;i<TRACE_BUFFER_SIZE;i++, col++){
1074
      printf("%08x ", s->t.buf[s->t.next + i]);
1075
      if((col % 8)==7){
1076
         printf("\n");
1077
      }
1078
   }
1079
}
1080
 
1081
/** Logs last cycle's activity (changes in state and/or loads/stores) */
1082
void log_cycle(State *s){
1083
   static unsigned int last_pc = 0;
1084
   int i;
1085
 
1086
   /* store PC in trace buffer only if there was a jump */
1087
   if(s->pc != (last_pc+4)){
1088
      s->t.buf[s->t.next] = s->pc;
1089
      s->t.next = (s->t.next + 1) % TRACE_BUFFER_SIZE;
1090
   }
1091
   last_pc = s->pc;
1092
 
1093
   /* if file logging is enabled, dump a trace log to file */
1094
   if(s->t.log!=NULL){
1095
      for(i=0;i<32;i++){
1096
         if(s->t.pr[i] != s->r[i]){
1097
            fprintf(s->t.log, "(%08X) [%02X]=%08X\n", s->pc, i, s->r[i]);
1098
         }
1099
         s->t.pr[i] = s->r[i];
1100
      }
1101
      if(s->lo != s->t.lo){
1102
         fprintf(s->t.log, "(%08X) [LO]=%08X\n", s->pc, s->lo);
1103
      }
1104
      s->t.lo = s->lo;
1105
 
1106
      if(s->hi != s->t.hi){
1107
         fprintf(s->t.log, "(%08X) [HI]=%08X\n", s->pc, s->hi);
1108
      }
1109
      s->t.hi = s->hi;
1110
 
1111
      if(s->epc != s->t.epc){
1112
         fprintf(s->t.log, "(%08X) [EP]=%08X\n", s->pc, s->epc);
1113
      }
1114
      s->t.epc = s->epc;
1115
   }
1116
}
1117
 
1118
/** Frees debug buffers and closes log file */
1119
void close_trace_buffer(State *s){
1120
   if(s->t.log){
1121
      fclose(s->t.log);
1122
   }
1123
}
1124 5 ja_rd
 
1125
/** Logs a message for each failed assertion, each in a line */
1126
void log_failed_assertions(State *s){
1127
   unsigned bitmap = s->failed_assertions;
1128
   int i = 0;
1129
 
1130
   /* This loop will crash the program if the message table is too short...*/
1131
   if(s->t.log != NULL){
1132
      for(i=0;i<32;i++){
1133
         if(bitmap & 0x1){
1134
            fprintf(s->t.log, "ASSERTION FAILED: [%08x] %s\n",
1135
                    s->faulty_address,
1136
                    assertion_messages[i]);
1137
         }
1138
         bitmap = bitmap >> 1;
1139
      }
1140
   }
1141
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.