~ari-tczew/ubuntu/lucid/skyeye/sru-lp-464175

« back to all changes in this revision

Viewing changes to arch/mips/common/mips_arch_interface.c

  • Committer: Bazaar Package Importer
  • Author(s): Yu Guanghui
  • Date: 2007-02-09 20:24:29 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070209202429-jknfb98t9ggaoz02
Tags: 1.2.1-2
Disable DBCT again.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Simulator for MIPS R3000 architecture.
 
2
 
 
3
                THIS SOFTWARE IS NOT COPYRIGHTED
 
4
 
 
5
   Cygnus offers the following for use in the public domain.  Cygnus
 
6
   makes no warranty with regard to the software or it's performance
 
7
   and the user accepts the software "AS IS" with all faults.
 
8
 
 
9
   CYGNUS DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD TO
 
10
   THIS SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
11
   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
12
 
 
13
*/
 
14
 
 
15
#include "skyeye_config.h"
 
16
#include "emul.h"
 
17
#include <stdlib.h>
 
18
#include "mipsdef.h"
 
19
#include <stdio.h>
 
20
 
 
21
MIPS_State* mstate;
 
22
static char *arch_name = "mips";
 
23
mips_mem_config_t mips_mem_config;
 
24
extern int trace_level;
 
25
extern UInt8* mem_bunks;
 
26
extern void mips_mem_reset ();
 
27
extern UInt32 mips_real_read_byte (UInt32 addr);
 
28
extern UInt32 mips_real_read_halfword (UInt32 addr);
 
29
extern UInt32 mips_real_read_word (UInt32 addr);
 
30
extern UInt64 mips_real_read_doubleword (UInt32 addr);
 
31
extern void mips_real_write_byte (UInt32 addr, UInt32 data);
 
32
extern void mips_real_write_halfword ( UInt32 addr, UInt32 data);
 
33
extern void mips_real_write_word ( UInt32 addr, UInt32 data);
 
34
extern void mips_real_write_doubleword ( UInt32 addr, UInt64 data);
 
35
 
 
36
//IO address space
 
37
extern UInt32 mips_io_read_byte (UInt32 addr);
 
38
extern UInt32 mips_io_read_halfword (UInt32 addr);
 
39
extern UInt32 mips_io_read_word (UInt32 addr);
 
40
extern UInt64 mips_io_read_doubleword (UInt32 addr);
 
41
extern void mips_io_write_byte (UInt32 addr, UInt32 data);
 
42
extern void mips_io_write_halfword (UInt32 addr, UInt32 data);
 
43
extern void mips_io_write_word (UInt32 addr, UInt32 data);
 
44
extern void mips_io_write_doubleword (UInt32 addr, UInt64 data);
 
45
 
 
46
//Flash address space
 
47
extern UInt32 mips_flash_read_byte (UInt32 addr);
 
48
extern UInt32 mips_flash_read_halfword (UInt32 addr);
 
49
extern UInt32 mips_flash_read_word (UInt32 addr);
 
50
extern UInt64 mips_flash_read_doubleword ( UInt32 addr);
 
51
extern void mips_flash_write_byte (UInt32 addr, UInt32 data);
 
52
extern void mips_flash_write_halfword (UInt32 addr, UInt32 data);
 
53
extern void mips_flash_write_word (UInt32 addr, UInt32 data);
 
54
extern void mips_flash_write_doubleword (UInt32 addr, UInt64 data);
 
55
 
 
56
extern void mips_warn_write_byte (UInt32 addr, UInt32 data);
 
57
extern void mips_warn_write_halfword (UInt32 addr, UInt32 data);
 
58
extern void mips_warn_write_word (UInt32 addr, UInt32 data);
 
59
extern mips_mem_bank_t* mips_bank_ptr (UInt32 addr);
 
60
extern void mips_mem_write_byte (UInt32 phys_addr, UInt32 v);
 
61
extern void mips_mem_write_halfword (UInt32 phys_addr, UInt32 v);
 
62
extern void mips_mem_write_word (UInt32 phys_addr, UInt32 v);
 
63
extern void mips_mem_write_doubleword (UInt64 phys_addr, UInt64 v);
 
64
extern UInt32 mips_mem_read_byte (UInt32 phys_addr);
 
65
extern UInt32 mips_mem_read_halfword (UInt32 phys_addr);
 
66
extern UInt32 mips_mem_read_word (UInt32 phys_addr);
 
67
extern UInt64 mips_mem_read_doubleword (UInt64 phys_addr);
 
68
extern void mipsMul_WriteByte (MIPS_State* mstate, UInt32 vir_addr, UInt32 v);
 
69
extern void mips_mmu_write_byte (MIPS_State* mstate, UInt32 vir_addr, UInt32 v);
 
70
 
 
71
 
 
72
//chy 20060717
 
73
int SKYPRINTF(char * fmt,...)
 
74
 
75
        return 0;
 
76
}
 
77
 
 
78
void 
 
79
mips_init_set(UInt32 addr, UInt8 value, int size)
 
80
{
 
81
 
 
82
}
 
83
 
 
84
void 
 
85
mips_mem_read(UInt32 pa, UInt32 *data, int len)
 
86
{
 
87
        switch(len) {
 
88
                
 
89
                case 1: {
 
90
                        *data = mips_mem_read_byte(pa);
 
91
                        break;
 
92
                }
 
93
                case 2: {
 
94
                        *data = mips_mem_read_halfword(pa);
 
95
                        break;
 
96
                }
 
97
                case 4: {
 
98
                        *data = mips_mem_read_word(pa);
 
99
                        break;
 
100
                }
 
101
                default:
 
102
                        break;
 
103
        }
 
104
 
 
105
}
 
106
 
 
107
void 
 
108
mips_mem_write(UInt32 pa, const UInt32* data, int len)
 
109
{
 
110
        UInt32 addr = bits(pa, 31, 0);
 
111
 
 
112
        switch(len) {
 
113
                
 
114
                case 1: {
 
115
                        mips_mem_write_byte(pa, *data);
 
116
                        break;
 
117
                }
 
118
                case 2: {
 
119
                        mips_mem_write_halfword(pa, *data);
 
120
                        break;
 
121
                }
 
122
                case 4: {
 
123
                        mips_mem_write_word(pa, *data);
 
124
                        break;
 
125
                }
 
126
                default:
 
127
                        break;
 
128
        }
 
129
        return;
 
130
        
 
131
}
 
132
 
 
133
static void 
 
134
init_icache()
 
135
{
 
136
        int i;
 
137
        for(i = 0; i < Icache_log2_sets; i++)
 
138
        {  
 
139
                Icache_lru_init(mstate->icache.set[i].Icache_lru);
 
140
        }
 
141
 
 
142
}
 
143
 
 
144
static void 
 
145
init_dcache()
 
146
{
 
147
        int i;
 
148
        for(i = 0; i < Dcache_log2_sets; i++)
 
149
        {  
 
150
              Dcache_lru_init(mstate->dcache.set[i].Dcache_lru);
 
151
        }
 
152
}
 
153
 
 
154
static void 
 
155
init_tlb()
 
156
{
 
157
        int i; 
 
158
        for(i = 0;i < tlb_map_size + 1; i++)
 
159
        {
 
160
                mstate->tlb_map[i] = NULL;
 
161
        }
 
162
}
 
163
 
 
164
 
 
165
static void 
 
166
mips_init_state()
 
167
{
 
168
        set_bit(mstate->mode, 2);
 
169
        mstate = (MIPS_State* )malloc(sizeof(MIPS_State));
 
170
        if (!mstate) {
 
171
                printf ("malloc error!\n");
 
172
                exit (-1);
 
173
        }
 
174
 
 
175
        mstate->warm = 0;
 
176
        mstate->conf.ec = 4; //I don't know what should it be.
 
177
 
 
178
        // set the little endian as the default
 
179
        mstate->bigendSig = 0; //Shi yang 2006-08-18
 
180
        
 
181
        //No interrupt
 
182
        mstate->irq_pending = 0;
 
183
        
 
184
        init_icache();
 
185
        init_dcache();
 
186
        init_tlb();
 
187
 
 
188
        mach_t *p_mach;
 
189
        p_mach = malloc (sizeof (mach_t));
 
190
        init_nedved_mach(p_mach);
 
191
        mstate->p_mach = p_mach;
 
192
}
 
193
 
 
194
static void 
 
195
mips_reset_state()
 
196
{
 
197
        mips_mem_reset();
 
198
 
 
199
        if (!mstate->warm) {
 
200
                memset(mstate->cp1, 0, sizeof(mstate->cp1[32]));
 
201
                memset(mstate->fpr, 0, sizeof(mstate->fpr[32]));
 
202
                mstate->count_seed = mstate->now;
 
203
                mstate->cp0[PRId] = ImpRev; //Fix me, Shi yang 2006-08-30
 
204
                mstate->cp1[FCR0] = ImpRev;
 
205
                mstate->nop_count = 0;
 
206
        }
 
207
        mstate->ll_bit = 0;
 
208
        mstate->sync_bit = 0;
 
209
 
 
210
        // Deliver the reset exception.
 
211
        if (mstate->warm)
 
212
                deliver_soft_reset(mstate);
 
213
        else
 
214
                deliver_cold_reset(mstate);
 
215
 
 
216
        process_reset(mstate);
 
217
}
 
218
 
 
219
void 
 
220
mips_trigger_irq(MIPS_State* mstate)
 
221
{
 
222
        VA epc;
 
223
 
 
224
        //Get the content of the cause register
 
225
        UInt32 cause = mstate->cp0[Cause];
 
226
 
 
227
        //When the instruction is in the delay slot, we have to delay an instruction 
 
228
        if (!branch_delay_slot(mstate))
 
229
                epc = mstate->pc;
 
230
        else {
 
231
                epc = mstate->pc - 4;
 
232
                cause = set_bit(cause, Cause_BD);
 
233
        }
 
234
        mstate->cp0[Cause] = cause;
 
235
        mstate->cp0[EPC] = epc;
 
236
 
 
237
        //Change the pointer pc to deal with the interrupt handler
 
238
        if(bit(mstate->cp0[SR], SR_BEV) )
 
239
        {
 
240
                mstate->pc = 0xbfc00380;
 
241
        } else {
 
242
                mstate->pc = 0x80000180;
 
243
        }
 
244
 
 
245
        mstate->pipeline = nothing_special;
 
246
        
 
247
}
 
248
 
 
249
static void 
 
250
mips_step_once()
 
251
{
 
252
        mstate->gpr[0] = 0;
 
253
        
 
254
        /* Check for interrupts. In real hardware, these have a priority lower
 
255
         * than all exceptions, but simulating this effect is too hard to be
 
256
         * worth the effort (interrupts and resets are not meant to be
 
257
         * delivered accurately anyway.)
 
258
         */
 
259
        if(mstate->irq_pending)
 
260
        {
 
261
                mips_trigger_irq(mstate);
 
262
        }
 
263
        
 
264
        /* Look up the ITLB. It's not clear from the manuals whether the ITLB
 
265
         * stores the ASIDs or not. I assume it does. ITLB has the same size
 
266
         * as in the real hardware, mapping two 4KB pages.  Because decoding a
 
267
         * MIPS64 virtual address is far from trivial, ITLB and DTLB actually
 
268
         * improve the simulator's performance: something I cannot say about
 
269
         * caches and JTLB.
 
270
         */
 
271
 
 
272
        PA pa; //Shi yang 2006-08-18
 
273
        VA va;
 
274
        va = mstate->pc;
 
275
        pa = translate_vaddr(mstate, va, instr_fetch);
 
276
 
 
277
        UInt32 addr = bits(va, 31, 0);
 
278
 
 
279
        Instr instr;
 
280
        instr = fetch(mstate, mstate->pc, pa);
 
281
        int next_state = decode(mstate, instr); 
 
282
 
 
283
        switch (mstate->pipeline) {
 
284
                case nothing_special:
 
285
                        mstate->pc += 4;
 
286
                        break;
 
287
                case branch_delay:
 
288
                        mstate->pc = mstate->branch_target;
 
289
                        break;
 
290
                case instr_addr_error:
 
291
                        process_address_error(mstate, instr_fetch, mstate->branch_target);
 
292
        }
 
293
        mstate->pipeline = next_state;
 
294
        
 
295
        mstate->p_mach->io_do_cycle();
 
296
}
 
297
 
 
298
static void 
 
299
mips_set_pc(UInt32 addr)
 
300
{
 
301
        mstate->pc = addr;
 
302
}
 
303
 
 
304
static UInt32 
 
305
mips_get_pc()
 
306
{
 
307
        return  mstate->pc;
 
308
}
 
309
 
 
310
static void
 
311
mips_write_byte (WORD addr, uint8_t v)
 
312
{
 
313
        mips_mem_write_byte (addr, v);
 
314
}
 
315
 
 
316
static void 
 
317
mips_write_byte64(UInt64 addr, UInt8 data)
 
318
{
 
319
 
 
320
}
 
321
 
 
322
static unsigned char 
 
323
mips_read_byte64(UInt64 addr)
 
324
{
 
325
 
 
326
}
 
327
 
 
328
static int 
 
329
mips_parse_cpu(const char* param[])
 
330
{
 
331
        return 1;
 
332
}
 
333
 
 
334
static int 
 
335
mips_parse_mach(machine_config_t * mach, const char* param[])
 
336
{
 
337
        return 1;
 
338
}
 
339
 
 
340
static int 
 
341
mips_parse_mem(int num_params, const char* params[])
 
342
{
 
343
        char name[MAX_PARAM_NAME], value[MAX_PARAM_NAME];
 
344
        int i, num;
 
345
        mips_mem_config_t *mc = &mips_mem_config;
 
346
        mips_mem_bank_t *mb = mc->mem_banks;
 
347
 
 
348
        mc->bank_num = mc->current_num++;
 
349
 
 
350
        num = mc->current_num - 1;      /*mem_banks should begin from 0. */
 
351
        mb[num].filename[0] = '\0';
 
352
        for (i = 0; i < num_params; i++) {
 
353
                if (split_param (params[i], name, value) < 0)
 
354
                        SKYEYE_ERR
 
355
                                ("Error: mem_bank %d has wrong parameter \"%s\".\n",
 
356
                                 num, name);
 
357
 
 
358
                if (!strncmp ("map", name, strlen (name))) {
 
359
                        if (!strncmp ("M", value, strlen (value))) {
 
360
                                mb[num].read_byte = mips_real_read_byte;
 
361
                                mb[num].write_byte = mips_real_write_byte;
 
362
                                mb[num].read_halfword = mips_real_read_halfword;
 
363
                                mb[num].write_halfword = mips_real_write_halfword;
 
364
                                mb[num].read_word = mips_real_read_word;
 
365
                                mb[num].write_word = mips_real_write_word;
 
366
                                mb[num].read_doubleword = mips_real_read_doubleword;
 
367
                                mb[num].write_doubleword = mips_real_write_doubleword;
 
368
                                mb[num].type = MEMTYPE_RAM;
 
369
                        }
 
370
                        else if (!strncmp ("I", value, strlen (value))) {
 
371
                                mb[num].read_byte = mips_io_read_byte;
 
372
                                mb[num].write_byte = mips_io_write_byte;
 
373
                                mb[num].read_halfword = mips_io_read_halfword;
 
374
                                mb[num].write_halfword = mips_io_write_halfword;
 
375
                                mb[num].read_word = mips_io_read_word;
 
376
                                mb[num].write_word = mips_io_write_word;
 
377
                                mb[num].read_doubleword = mips_io_read_doubleword;
 
378
                                mb[num].write_doubleword = mips_io_write_doubleword;
 
379
 
 
380
                                mb[num].type = MEMTYPE_IO;
 
381
 
 
382
                                /*ywc 2005-03-30 */
 
383
                        }
 
384
                        else if (!strncmp ("F", value, strlen (value))) {
 
385
                                mb[num].read_byte = mips_flash_read_byte;
 
386
                                mb[num].write_byte = mips_flash_write_byte;
 
387
                                mb[num].read_halfword = mips_flash_read_halfword;
 
388
                                mb[num].write_halfword = mips_flash_write_halfword;
 
389
                                mb[num].read_word = mips_flash_read_word;
 
390
                                mb[num].write_word = mips_flash_write_word;
 
391
                                mb[num].read_doubleword = mips_flash_read_doubleword;
 
392
                                mb[num].write_doubleword = mips_flash_write_doubleword;
 
393
                                mb[num].type = MEMTYPE_FLASH;
 
394
 
 
395
                        }
 
396
                        else {
 
397
                                SKYEYE_ERR
 
398
                                        ("Error: mem_bank %d \"%s\" parameter has wrong value \"%s\"\n",
 
399
                                         num, name, value);
 
400
                        }
 
401
                }
 
402
                else if (!strncmp ("type", name, strlen (name))) {
 
403
                        //chy 2003-09-21: process type
 
404
                        if (!strncmp ("R", value, strlen (value))) {
 
405
                                if (mb[num].type == MEMTYPE_RAM)
 
406
                                        mb[num].type = MEMTYPE_ROM;
 
407
                                mb[num].write_byte = mips_warn_write_byte;
 
408
                                mb[num].write_halfword = mips_warn_write_halfword;
 
409
                                mb[num].write_word = mips_warn_write_word;
 
410
                        }
 
411
                }
 
412
                else if (!strncmp ("addr", name, strlen (name))) {
 
413
 
 
414
                        if (value[0] == '0' && value[1] == 'x')
 
415
                                mb[num].addr = strtoul (value, NULL, 16);
 
416
                        else
 
417
                                mb[num].addr = strtoul (value, NULL, 10);
 
418
 
 
419
                }
 
420
                else if (!strncmp ("size", name, strlen (name))) {
 
421
 
 
422
                        if (value[0] == '0' && value[1] == 'x')
 
423
                                mb[num].len = strtoul (value, NULL, 16);
 
424
                        else
 
425
                                mb[num].len = strtoul (value, NULL, 10);
 
426
 
 
427
                }
 
428
                else if (!strncmp ("file", name, strlen (name))) {
 
429
                        strncpy (mb[num].filename, value, strlen (value) + 1);
 
430
                }
 
431
                else if (!strncmp ("boot", name, strlen (name))) {
 
432
                        /*this must be the last parameter. */
 
433
                        if (!strncmp ("yes", value, strlen (value)))
 
434
                                skyeye_config.start_address = mb[num].addr;
 
435
                }
 
436
                else {
 
437
                        SKYEYE_ERR
 
438
                                ("Error: mem_bank %d has unknow parameter \"%s\".\n",
 
439
                                 num, name);
 
440
                }
 
441
        }
 
442
 
 
443
        return 0;
 
444
}
 
445
 
 
446
static int mips_ICE_read_byte(WORD addr, uint8_t *data){
 
447
        mips_mem_read(addr, *data, 1);
 
448
        return 0;
 
449
}
 
450
static int mips_ICE_write_byte(WORD addr, uint8_t data){
 
451
        mips_mem_write(addr, &data, 1);
 
452
        return 0;
 
453
}
 
454
 
 
455
void 
 
456
init_mips_arch ()
 
457
{
 
458
        static arch_config_t mips_arch;
 
459
        mips_arch.arch_name = arch_name;
 
460
        mips_arch.init = mips_init_state;
 
461
        mips_arch.reset = mips_reset_state;
 
462
        mips_arch.step_once = mips_step_once;
 
463
        mips_arch.set_pc = mips_set_pc;
 
464
        mips_arch.get_pc = mips_get_pc;
 
465
        mips_arch.ICE_read_byte = mips_ICE_read_byte;
 
466
        mips_arch.ICE_write_byte = mips_ICE_write_byte;
 
467
        mips_arch.parse_cpu = mips_parse_cpu;
 
468
        mips_arch.parse_mach = mips_parse_mach;
 
469
        mips_arch.parse_mem = mips_parse_mem;
 
470
        register_arch (&mips_arch);
 
471
}
 
472