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

Subversion Repositories ion

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

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
      return;
594
   epc = s->pc + 4;
595
   if(s->pc_next != s->pc + 4)
596
      epc |= 2;  //branch delay slot
597
 
598
   s->pc = s->pc_next;
599
   s->pc_next = s->pc_next + 4;
600
   if(s->skip)
601
   {
602
      s->skip = 0;
603
      return;
604
   }
605
   rSave = r[rt];
606
   switch(op)
607
   {
608
      case 0x00:/*SPECIAL*/
609
         switch(func)
610
         {
611
            case 0x00:/*SLL*/  r[rd]=r[rt]<<re;          break;
612
            case 0x02:/*SRL*/  r[rd]=u[rt]>>re;          break;
613
            case 0x03:/*SRA*/  r[rd]=r[rt]>>re;          break;
614
            case 0x04:/*SLLV*/ r[rd]=r[rt]<<r[rs];       break;
615
            case 0x06:/*SRLV*/ r[rd]=u[rt]>>r[rs];       break;
616
            case 0x07:/*SRAV*/ r[rd]=r[rt]>>r[rs];       break;
617
            case 0x08:/*JR*/   s->pc_next=r[rs];         break;
618
            case 0x09:/*JALR*/ r[rd]=s->pc_next; s->pc_next=r[rs]; break;
619
            case 0x0a:/*MOVZ*/ if(!r[rt]) r[rd]=r[rs];   break;  /*IV*/
620
            case 0x0b:/*MOVN*/ if(r[rt]) r[rd]=r[rs];    break;  /*IV*/
621
            case 0x0c:/*SYSCALL*/ /* epc|=1; WHY? */
622
                                  s->exceptionId=1; break;
623
            case 0x0d:/*BREAK*/   /* epc|=1; WHY? */
624
                                  s->exceptionId=1; break;
625
            case 0x0f:/*SYNC*/ s->wakeup=1;              break;
626
            case 0x10:/*MFHI*/ r[rd]=s->hi;              break;
627
            case 0x11:/*FTHI*/ s->hi=r[rs];              break;
628
            case 0x12:/*MFLO*/ r[rd]=s->lo;              break;
629
            case 0x13:/*MTLO*/ s->lo=r[rs];              break;
630
            case 0x18:/*MULT*/ mult_big_signed(r[rs],r[rt],&s->hi,&s->lo); break;
631
            case 0x19:/*MULTU*/ mult_big(r[rs],r[rt],&s->hi,&s->lo); break;
632
            case 0x1a:/*DIV*/  s->lo=r[rs]/r[rt]; s->hi=r[rs]%r[rt]; break;
633
            case 0x1b:/*DIVU*/ s->lo=u[rs]/u[rt]; s->hi=u[rs]%u[rt]; break;
634
            case 0x20:/*ADD*/  r[rd]=r[rs]+r[rt];        break;
635
            case 0x21:/*ADDU*/ r[rd]=r[rs]+r[rt];        break;
636
            case 0x22:/*SUB*/  r[rd]=r[rs]-r[rt];        break;
637
            case 0x23:/*SUBU*/ r[rd]=r[rs]-r[rt];        break;
638
            case 0x24:/*AND*/  r[rd]=r[rs]&r[rt];        break;
639
            case 0x25:/*OR*/   r[rd]=r[rs]|r[rt];        break;
640
            case 0x26:/*XOR*/  r[rd]=r[rs]^r[rt];        break;
641
            case 0x27:/*NOR*/  r[rd]=~(r[rs]|r[rt]);     break;
642
            case 0x2a:/*SLT*/  r[rd]=r[rs]<r[rt];        break;
643
            case 0x2b:/*SLTU*/ r[rd]=u[rs]<u[rt];        break;
644
            case 0x2d:/*DADDU*/r[rd]=r[rs]+u[rt];        break;
645
            case 0x31:/*TGEU*/ break;
646
            case 0x32:/*TLT*/  break;
647
            case 0x33:/*TLTU*/ break;
648
            case 0x34:/*TEQ*/  break;
649
            case 0x36:/*TNE*/  break;
650
            default: printf("ERROR0(*0x%x~0x%x)\n", s->pc, opcode);
651
               s->wakeup=1;
652
         }
653
         break;
654
      case 0x01:/*REGIMM*/
655
         switch(rt) {
656
            case 0x10:/*BLTZAL*/ r[31]=s->pc_next;
657
            case 0x00:/*BLTZ*/   branch=r[rs]<0;    break;
658
            case 0x11:/*BGEZAL*/ r[31]=s->pc_next;
659
            case 0x01:/*BGEZ*/   branch=r[rs]>=0;   break;
660
            case 0x12:/*BLTZALL*/r[31]=s->pc_next;
661
            case 0x02:/*BLTZL*/  lbranch=r[rs]<0;   break;
662
            case 0x13:/*BGEZALL*/r[31]=s->pc_next;
663
            case 0x03:/*BGEZL*/  lbranch=r[rs]>=0;  break;
664
            default: printf("ERROR1\n"); s->wakeup=1;
665
          }
666
         break;
667
      case 0x03:/*JAL*/    r[31]=s->pc_next;
668
      case 0x02:/*J*/      s->pc_next=(s->pc&0xf0000000)|target; break;
669
      case 0x04:/*BEQ*/    branch=r[rs]==r[rt];     break;
670
      case 0x05:/*BNE*/    branch=r[rs]!=r[rt];     break;
671
      case 0x06:/*BLEZ*/   branch=r[rs]<=0;         break;
672
      case 0x07:/*BGTZ*/   branch=r[rs]>0;          break;
673
      case 0x08:/*ADDI*/   r[rt]=r[rs]+(short)imm;  break;
674
      case 0x09:/*ADDIU*/  u[rt]=u[rs]+(short)imm;  break;
675
      case 0x0a:/*SLTI*/   r[rt]=r[rs]<(short)imm;  break;
676
      case 0x0b:/*SLTIU*/  u[rt]=u[rs]<(unsigned int)(short)imm; break;
677
      case 0x0c:/*ANDI*/   r[rt]=r[rs]&imm;         break;
678
      case 0x0d:/*ORI*/    r[rt]=r[rs]|imm;         break;
679
      case 0x0e:/*XORI*/   r[rt]=r[rs]^imm;         break;
680
      case 0x0f:/*LUI*/    r[rt]=(imm<<16);         break;
681
      case 0x10:/*COP0*/
682
         if((opcode & (1<<23)) == 0)  //move from CP0
683
         {
684
            if(rd == 12)
685
               r[rt]=s->status;
686
            else
687
               r[rt]=s->epc;
688
         }
689
         else                         //move to CP0
690
         {
691
            s->status=r[rt]&1;
692
            if(s->processId && (r[rt]&2))
693
            {
694
               s->userMode|=r[rt]&2;
695
               //printf("CpuStatus=%d %d %d\n", r[rt], s->status, s->userMode);
696
               //s->wakeup = 1;
697
               //printf("pc=0x%x\n", epc);
698
            }
699
         }
700
         break;
701
//      case 0x11:/*COP1*/ break;
702
//      case 0x12:/*COP2*/ break;
703
//      case 0x13:/*COP3*/ break;
704
      case 0x14:/*BEQL*/   lbranch=r[rs]==r[rt];    break;
705
      case 0x15:/*BNEL*/   lbranch=r[rs]!=r[rt];    break;
706
      case 0x16:/*BLEZL*/  lbranch=r[rs]<=0;        break;
707
      case 0x17:/*BGTZL*/  lbranch=r[rs]>0;         break;
708
//      case 0x1c:/*MAD*/  break;   /*IV*/
709
      case 0x20:/*LB*/     //r[rt]=(signed char)mem_read(s,1,ptr,1);  break;
710
                           start_load(s, rt,(signed char)mem_read(s,1,ptr,1));
711
                           break;
712
 
713
      case 0x21:/*LH*/     //r[rt]=(signed short)mem_read(s,2,ptr,1); break;
714
                           start_load(s, rt, (signed short)mem_read(s,2,ptr,1));
715
                           break;
716
      case 0x22:/*LWL*/    //target=8*(ptr&3);
717
                           //r[rt]=(r[rt]&~(0xffffffff<<target))|
718
                           //      (mem_read(s,4,ptr&~3)<<target); break;
719
                           break;
720
      case 0x23:/*LW*/     //r[rt]=mem_read(s,4,ptr,1);   break;
721
                           start_load(s, rt, mem_read(s,4,ptr,1));
722
                           break;
723
      case 0x24:/*LBU*/    //r[rt]=(unsigned char)mem_read(s,1,ptr,1); break;
724
                           start_load(s, rt, (unsigned char)mem_read(s,1,ptr,1));
725
                           break;
726
      case 0x25:/*LHU*/    //r[rt]= (unsigned short)mem_read(s,2,ptr,1);
727
                           start_load(s, rt, (unsigned short)mem_read(s,2,ptr,1));
728
                           break;
729
      case 0x26:/*LWR*/
730
                           //target=32-8*(ptr&3);
731
                           //r[rt]=(r[rt]&~((unsigned int)0xffffffff>>target))|
732
                           //((unsigned int)mem_read(s,4,ptr&~3)>>target);
733
                           break;
734
      case 0x28:/*SB*/     mem_write(s,1,ptr,r[rt]);  break;
735
      case 0x29:/*SH*/     mem_write(s,2,ptr,r[rt]);  break;
736
      case 0x2a:/*SWL*/
737
                           //mem_write(s,1,ptr,r[rt]>>24);
738
                           //mem_write(s,1,ptr+1,r[rt]>>16);
739
                           //mem_write(s,1,ptr+2,r[rt]>>8);
740
                           //mem_write(s,1,ptr+3,r[rt]); break;
741
      case 0x2b:/*SW*/     mem_write(s,4,ptr,r[rt]);  break;
742
      case 0x2e:/*SWR*/    break; //fixme
743
      case 0x2f:/*CACHE*/  break;
744
      case 0x30:/*LL*/     //r[rt]=mem_read(s,4,ptr);   break;
745
                           start_load(s, rt, mem_read(s,4,ptr,1));
746
                           break;
747
 
748
//      case 0x31:/*LWC1*/ break;
749
//      case 0x32:/*LWC2*/ break;
750
//      case 0x33:/*LWC3*/ break;
751
//      case 0x35:/*LDC1*/ break;
752
//      case 0x36:/*LDC2*/ break;
753
//      case 0x37:/*LDC3*/ break;
754
//      case 0x38:/*SC*/     *(int*)ptr=r[rt]; r[rt]=1; break;
755
      case 0x38:/*SC*/     mem_write(s,4,ptr,r[rt]); r[rt]=1; break;
756
//      case 0x39:/*SWC1*/ break;
757
//      case 0x3a:/*SWC2*/ break;
758
//      case 0x3b:/*SWC3*/ break;
759
//      case 0x3d:/*SDC1*/ break;
760
//      case 0x3e:/*SDC2*/ break;
761
//      case 0x3f:/*SDC3*/ break;
762
      default: printf("ERROR2 address=0x%x opcode=0x%x\n", s->pc, opcode);
763
         s->wakeup=1;
764
   }
765
   s->pc_next += (branch || lbranch == 1) ? imm_shift : 0;
766
   s->pc_next &= ~3;
767
   s->skip = (lbranch == 0) | skip2;
768
 
769 5 ja_rd
   /* If there was trouble, log it */
770
   if(s->failed_assertions!=0){
771
      log_failed_assertions(s);
772
      s->failed_assertions=0;
773
   }
774
 
775
 
776 2 ja_rd
   /* if there's a delayed load pending, do it now: load reg with memory data */
777
   if(s->load_delay_slot){
778
      /*--- simulate real core's load interlock ---*/
779
      if(s->delay<=0){
780
         if(s->delayed_reg>0){
781
            s->r[s->delayed_reg] = s->delayed_data;
782
         }
783
         s->delayed_reg = 0;
784
      }
785
      else{
786
         s->delay = s->delay - 1;
787
      }
788
   }
789
   else{
790
      /*--- delay slot or interlocking not simulated ---*/
791
      /*
792
      if(s->delay){
793
         r[s->delayed_reg] = s->delayed_data;
794
         s->delay=0;
795
      }
796
      */
797
   }
798
 
799
   if(s->exceptionId)
800
   {
801
      r[rt] = rSave;
802
      s->epc = epc;
803
      s->pc_next = 0x3c;
804
      s->skip = 1;
805
      s->exceptionId = 0;
806
      s->userMode = 0;
807
      //s->wakeup = 1;
808
      return;
809
   }
810
}
811
 
812
/** Dump CPU state to console */
813
void show_state(State *s)
814
{
815
   int i,j;
816
   printf("pid=%d userMode=%d, epc=0x%x\n", s->processId, s->userMode, s->epc);
817
   for(i = 0; i < 4; ++i)
818
   {
819
      printf("%2.2d ", i * 8);
820
      for(j = 0; j < 8; ++j)
821
      {
822
         printf("%8.8x ", s->r[i*8+j]);
823
      }
824
      printf("\n");
825
   }
826
   //printf("%8.8lx %8.8lx %8.8lx %8.8lx\n", s->pc, s->pc_next, s->hi, s->lo);
827
   j = s->pc;
828
   for(i = -4; i <= 8; ++i)
829
   {
830
      printf("%c", i==0 ? '*' : ' ');
831
      s->pc = j + i * 4;
832
      cycle(s, 10);
833
   }
834
   s->pc = j;
835
}
836
 
837
/** Show debug monitor prompt and execute user command */
838
void do_debug(State *s)
839
{
840
   int ch;
841
   int i, j=0, watch=0, addr;
842
   s->pc_next = s->pc + 4;
843
   s->skip = 0;
844
   s->delayed_reg = 0;
845
   s->wakeup = 0;
846
   show_state(s);
847
   ch = ' ';
848
   for(;;)
849
   {
850
      if(ch != 'n')
851
      {
852
         if(watch)
853
            printf("0x%8.8x=0x%8.8x\n", watch, mem_read(s, 4, watch,0));
854
         printf("1=Debug 2=Trace 3=Step 4=BreakPt 5=Go 6=Memory ");
855
         printf("7=Watch 8=Jump 9=Quit A=dump > ");
856
      }
857
      ch = getch();
858
      if(ch != 'n')
859
         printf("\n");
860
      switch(ch)
861
      {
862
      case 'a': case 'A':
863
         dump_trace_buffer(s); break;
864
      case '1': case 'd': case ' ':
865
         cycle(s, 0); show_state(s); break;
866
      case 'n':
867
         cycle(s, 1); break;
868
      case '2': case 't':
869
         cycle(s, 0); printf("*"); cycle(s, 10); break;
870
      case '3': case 's':
871
         printf("Count> ");
872
         scanf("%d", &j);
873
         for(i = 0; i < j; ++i)
874
            cycle(s, 1);
875
         show_state(s);
876
         break;
877
      case '4': case 'b':
878
         printf("Line> ");
879
         scanf("%x", &j);
880
         printf("break point=0x%x\n", j);
881
         break;
882
      case '5': case 'g':
883
         s->wakeup = 0;
884
         cycle(s, 0);
885
         while(s->wakeup == 0)
886
         {
887
            if(s->pc == j){
888
               printf("\n\nStop: pc = 0x%08x\n\n", j);
889
               break;
890
            }
891
            cycle(s, 0);
892
         }
893
         show_state(s);
894
         break;
895
      case 'G':
896
         s->wakeup = 0;
897
         cycle(s, 1);
898
         while(s->wakeup == 0)
899
         {
900
            if(s->pc == j)
901
               break;
902
            cycle(s, 1);
903
         }
904
         show_state(s);
905
         break;
906
      case '6': case 'm':
907
         printf("Memory> ");
908
         scanf("%x", &j);
909
         for(i = 0; i < 8; ++i)
910
         {
911
            printf("%8.8x ", mem_read(s, 4, j+i*4, 0));
912
         }
913
         printf("\n");
914
         break;
915
      case '7': case 'w':
916
         printf("Watch> ");
917
         scanf("%x", &watch);
918
         break;
919
      case '8': case 'j':
920
         printf("Jump> ");
921
         scanf("%x", &addr);
922
         s->pc = addr;
923
         s->pc_next = addr + 4;
924
         show_state(s);
925
         break;
926
      case '9': case 'q':
927
         return;
928
      }
929
   }
930
}
931
 
932
/** Read binary code and data files */
933
int read_program(State *s, char *code_name, char *data_name){
934
   FILE *in;
935
   int bytes, i;
936
   unsigned char *buffer;
937
 
938
   /* Read code file (.text + .reginfo + .rodata) */
939
   in = fopen(code_name, "rb");
940
   if(in == NULL)
941
   {
942
      printf("Can't open file %s!\n",code_name);
943
      getch();
944
      return(0);
945
   }
946
   bytes = fread(s->mem, 1, MEM_SIZE, in);
947
   fclose(in);
948
   in = NULL;
949
   printf("Code [size= %6d, start= 0x%08x]\n", bytes, 0);
950
 
951
   /* Read data file (.data + .bss) if necessary */
952
   if(data_name!=NULL){
953
      in = fopen(data_name, "rb");
954
      if(in == NULL)
955
      {
956
         printf("Can't open file %s!\n",data_name);
957
         getch();
958
         return(0);
959
      }
960
      /* FIXME Section '.data' start hardcoded to 0x10000 */
961
      buffer = (unsigned char*)malloc(MEM_SIZE);
962
 
963
      bytes = fread(buffer, 1, MEM_SIZE, in);
964
      fclose(in);
965
      printf("Data [size= %6d, start= 0x%08x]\n", bytes, 0x10000);
966
 
967
      for(i=0;i<bytes;i++){
968
         s->mem[0x10000+i] = buffer[i];
969
      }
970
      free(buffer);
971
   }
972
 
973
   return 1;
974
}
975
 
976
/*----------------------------------------------------------------------------*/
977
 
978
int main(int argc,char *argv[])
979
{
980
   State state, *s=&state;
981
   int index;
982
   char *code_filename;
983
   char *data_filename;
984
 
985
   printf("MIPS-I emulator\n");
986
   memset(s, 0, sizeof(State));
987
   s->big_endian = 1;
988
   s->load_delay_slot = 0;
989
   s->mem = (unsigned char*)malloc(MEM_SIZE);
990
   memset(s->mem, 0, MEM_SIZE);
991
 
992
 
993
   if(argc==3){
994
      code_filename = argv[1];
995
      data_filename = argv[2];
996
   }
997
   else if(argc==2){
998
      code_filename = argv[1];
999
      data_filename = NULL;
1000
   }
1001
   else{
1002
      printf("Usage:");
1003
      printf("    slite file.exe <bin code file> [<bin data file>]\n");
1004
      return 0;
1005
   }
1006
 
1007
   if(!read_program(s, code_filename, data_filename)) return 0;
1008
 
1009
   init_trace_buffer(s, "sw_sim_log.txt");
1010
   memcpy(s->mem + 1024*1024, s->mem, 1024*1024);  //internal 8KB SRAM
1011
   cache_init(); /* stub */
1012
 
1013
   /* NOTE: Original mlite supported loading little-endian code, which this
1014
      program doesn't. The endianess-conversion code has been removed.
1015
   */
1016
 
1017 5 ja_rd
   /* Simulate a CPU reset */
1018
   /* FIXME cpu reset function needed */
1019
   s->pc = 0x0;      /* reset start vector */
1020
   s->failed_assertions = 0; /* no failed assertions pending */
1021
 
1022 2 ja_rd
   /* FIXME PC fixup at address zero has to be removed */
1023
   index = mem_read(s, 4, 0, 0);
1024
   if((index & 0xffffff00) == 0x3c1c1000){
1025
      s->pc = 0x10000000;
1026
   }
1027
 
1028
   /* Enter debug command interface; will only exit clean with user command */
1029
   do_debug(s);
1030
 
1031
   /* Close and deallocate everything and quit */
1032
   close_trace_buffer(s);
1033
   free(s->mem);
1034
   return(0);
1035
}
1036
 
1037
/*----------------------------------------------------------------------------*/
1038
 
1039
 
1040
void init_trace_buffer(State *s, const char *log_file_name){
1041
   int i;
1042
 
1043
#if FILE_LOGGING_DISABLED
1044
   s->t.log = NULL;
1045
   return;
1046
#else
1047
   for(i=0;i<TRACE_BUFFER_SIZE;i++){
1048
      s->t.buf[i]=0xffffffff;
1049
   }
1050
   s->t.next = 0;
1051
 
1052
   /* if file logging is enabled, open log file */
1053
   if(log_file_name!=NULL){
1054
      s->t.log = fopen(log_file_name, "w");
1055
      if(s->t.log==NULL){
1056
         printf("Error opening log file '%s', file logging disabled\n", log_file_name);
1057
      }
1058
   }
1059
   else{
1060
      s->t.log = NULL;
1061
   }
1062
#endif
1063
}
1064
 
1065
/** Dumps last jump targets as a chunk of hex numbers (older is left top) */
1066
void dump_trace_buffer(State *s){
1067
   int i, col;
1068
 
1069
   for(i=0, col=0;i<TRACE_BUFFER_SIZE;i++, col++){
1070
      printf("%08x ", s->t.buf[s->t.next + i]);
1071
      if((col % 8)==7){
1072
         printf("\n");
1073
      }
1074
   }
1075
}
1076
 
1077
/** Logs last cycle's activity (changes in state and/or loads/stores) */
1078
void log_cycle(State *s){
1079
   static unsigned int last_pc = 0;
1080
   int i;
1081
 
1082
   /* store PC in trace buffer only if there was a jump */
1083
   if(s->pc != (last_pc+4)){
1084
      s->t.buf[s->t.next] = s->pc;
1085
      s->t.next = (s->t.next + 1) % TRACE_BUFFER_SIZE;
1086
   }
1087
   last_pc = s->pc;
1088
 
1089
   /* if file logging is enabled, dump a trace log to file */
1090
   if(s->t.log!=NULL){
1091
      for(i=0;i<32;i++){
1092
         if(s->t.pr[i] != s->r[i]){
1093
            fprintf(s->t.log, "(%08X) [%02X]=%08X\n", s->pc, i, s->r[i]);
1094
         }
1095
         s->t.pr[i] = s->r[i];
1096
      }
1097
      if(s->lo != s->t.lo){
1098
         fprintf(s->t.log, "(%08X) [LO]=%08X\n", s->pc, s->lo);
1099
      }
1100
      s->t.lo = s->lo;
1101
 
1102
      if(s->hi != s->t.hi){
1103
         fprintf(s->t.log, "(%08X) [HI]=%08X\n", s->pc, s->hi);
1104
      }
1105
      s->t.hi = s->hi;
1106
 
1107
      if(s->epc != s->t.epc){
1108
         fprintf(s->t.log, "(%08X) [EP]=%08X\n", s->pc, s->epc);
1109
      }
1110
      s->t.epc = s->epc;
1111
   }
1112
}
1113
 
1114
/** Frees debug buffers and closes log file */
1115
void close_trace_buffer(State *s){
1116
   if(s->t.log){
1117
      fclose(s->t.log);
1118
   }
1119
}
1120 5 ja_rd
 
1121
/** Logs a message for each failed assertion, each in a line */
1122
void log_failed_assertions(State *s){
1123
   unsigned bitmap = s->failed_assertions;
1124
   int i = 0;
1125
 
1126
   /* This loop will crash the program if the message table is too short...*/
1127
   if(s->t.log != NULL){
1128
      for(i=0;i<32;i++){
1129
         if(bitmap & 0x1){
1130
            fprintf(s->t.log, "ASSERTION FAILED: [%08x] %s\n",
1131
                    s->faulty_address,
1132
                    assertion_messages[i]);
1133
         }
1134
         bitmap = bitmap >> 1;
1135
      }
1136
   }
1137
}

powered by: WebSVN 2.1.0

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