interp.c revision 1.6 1 1.1 christos /* Simulator for the moxie processor
2 1.6 christos Copyright (C) 2008-2016 Free Software Foundation, Inc.
3 1.1 christos Contributed by Anthony Green
4 1.1 christos
5 1.1 christos This file is part of GDB, the GNU debugger.
6 1.1 christos
7 1.1 christos This program is free software; you can redistribute it and/or modify
8 1.1 christos it under the terms of the GNU General Public License as published by
9 1.1 christos the Free Software Foundation; either version 3 of the License, or
10 1.1 christos (at your option) any later version.
11 1.1 christos
12 1.1 christos This program is distributed in the hope that it will be useful,
13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1 christos GNU General Public License for more details.
16 1.1 christos
17 1.1 christos You should have received a copy of the GNU General Public License
18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 1.1 christos
20 1.1 christos #include "config.h"
21 1.1 christos #include <fcntl.h>
22 1.1 christos #include <signal.h>
23 1.1 christos #include <stdlib.h>
24 1.5 christos #include <string.h>
25 1.1 christos #include <sys/times.h>
26 1.1 christos #include <sys/param.h>
27 1.5 christos #include <unistd.h>
28 1.1 christos #include "bfd.h"
29 1.1 christos #include "libiberty.h"
30 1.1 christos #include "gdb/remote-sim.h"
31 1.1 christos
32 1.1 christos #include "sim-main.h"
33 1.1 christos #include "sim-base.h"
34 1.5 christos #include "sim-options.h"
35 1.1 christos
36 1.1 christos typedef int word;
37 1.1 christos typedef unsigned int uword;
38 1.1 christos
39 1.1 christos /* Extract the signed 10-bit offset from a 16-bit branch
40 1.1 christos instruction. */
41 1.1 christos #define INST2OFFSET(o) ((((signed short)((o & ((1<<10)-1))<<6))>>6)<<1)
42 1.1 christos
43 1.1 christos #define EXTRACT_WORD(addr) \
44 1.1 christos ((sim_core_read_aligned_1 (scpu, cia, read_map, addr) << 24) \
45 1.1 christos + (sim_core_read_aligned_1 (scpu, cia, read_map, addr+1) << 16) \
46 1.1 christos + (sim_core_read_aligned_1 (scpu, cia, read_map, addr+2) << 8) \
47 1.1 christos + (sim_core_read_aligned_1 (scpu, cia, read_map, addr+3)))
48 1.1 christos
49 1.3 christos #define EXTRACT_OFFSET(addr) \
50 1.3 christos (unsigned int) \
51 1.3 christos (((signed short) \
52 1.3 christos ((sim_core_read_aligned_1 (scpu, cia, read_map, addr) << 8) \
53 1.3 christos + (sim_core_read_aligned_1 (scpu, cia, read_map, addr+1))) << 16) >> 16)
54 1.3 christos
55 1.5 christos static unsigned long
56 1.5 christos moxie_extract_unsigned_integer (unsigned char *addr, int len)
57 1.1 christos {
58 1.1 christos unsigned long retval;
59 1.1 christos unsigned char * p;
60 1.1 christos unsigned char * startaddr = (unsigned char *)addr;
61 1.1 christos unsigned char * endaddr = startaddr + len;
62 1.1 christos
63 1.1 christos if (len > (int) sizeof (unsigned long))
64 1.5 christos printf ("That operation is not available on integers of more than %zu bytes.",
65 1.1 christos sizeof (unsigned long));
66 1.1 christos
67 1.1 christos /* Start at the most significant end of the integer, and work towards
68 1.1 christos the least significant. */
69 1.1 christos retval = 0;
70 1.1 christos
71 1.1 christos for (p = endaddr; p > startaddr;)
72 1.1 christos retval = (retval << 8) | * -- p;
73 1.1 christos
74 1.1 christos return retval;
75 1.1 christos }
76 1.1 christos
77 1.5 christos static void
78 1.5 christos moxie_store_unsigned_integer (unsigned char *addr, int len, unsigned long val)
79 1.1 christos {
80 1.1 christos unsigned char * p;
81 1.1 christos unsigned char * startaddr = (unsigned char *)addr;
82 1.1 christos unsigned char * endaddr = startaddr + len;
83 1.1 christos
84 1.1 christos for (p = endaddr; p > startaddr;)
85 1.1 christos {
86 1.1 christos * -- p = val & 0xff;
87 1.1 christos val >>= 8;
88 1.1 christos }
89 1.1 christos }
90 1.1 christos
91 1.1 christos /* moxie register names. */
92 1.1 christos static const char *reg_names[16] =
93 1.1 christos { "$fp", "$sp", "$r0", "$r1", "$r2", "$r3", "$r4", "$r5",
94 1.1 christos "$r6", "$r7", "$r8", "$r9", "$r10", "$r11", "$r12", "$r13" };
95 1.1 christos
96 1.1 christos /* The machine state.
97 1.1 christos
98 1.1 christos This state is maintained in host byte order. The fetch/store
99 1.1 christos register functions must translate between host byte order and the
100 1.1 christos target processor byte order. Keeping this data in target byte
101 1.1 christos order simplifies the register read/write functions. Keeping this
102 1.1 christos data in native order improves the performance of the simulator.
103 1.1 christos Simulation speed is deemed more important. */
104 1.1 christos
105 1.1 christos #define NUM_MOXIE_REGS 17 /* Including PC */
106 1.1 christos #define NUM_MOXIE_SREGS 256 /* The special registers */
107 1.1 christos #define PC_REGNO 16
108 1.1 christos
109 1.1 christos /* The ordering of the moxie_regset structure is matched in the
110 1.1 christos gdb/config/moxie/tm-moxie.h file in the REGISTER_NAMES macro. */
111 1.5 christos /* TODO: This should be moved to sim-main.h:_sim_cpu. */
112 1.1 christos struct moxie_regset
113 1.1 christos {
114 1.1 christos word regs[NUM_MOXIE_REGS + 1]; /* primary registers */
115 1.1 christos word sregs[256]; /* special registers */
116 1.1 christos word cc; /* the condition code reg */
117 1.1 christos unsigned long long insts; /* instruction counter */
118 1.1 christos };
119 1.1 christos
120 1.1 christos #define CC_GT 1<<0
121 1.1 christos #define CC_LT 1<<1
122 1.1 christos #define CC_EQ 1<<2
123 1.1 christos #define CC_GTU 1<<3
124 1.1 christos #define CC_LTU 1<<4
125 1.1 christos
126 1.5 christos /* TODO: This should be moved to sim-main.h:_sim_cpu. */
127 1.1 christos union
128 1.1 christos {
129 1.1 christos struct moxie_regset asregs;
130 1.1 christos word asints [1]; /* but accessed larger... */
131 1.1 christos } cpu;
132 1.1 christos
133 1.1 christos static void
134 1.5 christos set_initial_gprs (void)
135 1.1 christos {
136 1.1 christos int i;
137 1.1 christos long space;
138 1.1 christos
139 1.1 christos /* Set up machine just out of reset. */
140 1.1 christos cpu.asregs.regs[PC_REGNO] = 0;
141 1.1 christos
142 1.1 christos /* Clean out the register contents. */
143 1.1 christos for (i = 0; i < NUM_MOXIE_REGS; i++)
144 1.1 christos cpu.asregs.regs[i] = 0;
145 1.1 christos for (i = 0; i < NUM_MOXIE_SREGS; i++)
146 1.1 christos cpu.asregs.sregs[i] = 0;
147 1.1 christos }
148 1.1 christos
149 1.1 christos /* Write a 1 byte value to memory. */
150 1.1 christos
151 1.5 christos static INLINE void
152 1.1 christos wbat (sim_cpu *scpu, word pc, word x, word v)
153 1.1 christos {
154 1.5 christos address_word cia = CPU_PC_GET (scpu);
155 1.1 christos
156 1.1 christos sim_core_write_aligned_1 (scpu, cia, write_map, x, v);
157 1.1 christos }
158 1.1 christos
159 1.1 christos /* Write a 2 byte value to memory. */
160 1.1 christos
161 1.5 christos static INLINE void
162 1.1 christos wsat (sim_cpu *scpu, word pc, word x, word v)
163 1.1 christos {
164 1.5 christos address_word cia = CPU_PC_GET (scpu);
165 1.1 christos
166 1.1 christos sim_core_write_aligned_2 (scpu, cia, write_map, x, v);
167 1.1 christos }
168 1.1 christos
169 1.1 christos /* Write a 4 byte value to memory. */
170 1.1 christos
171 1.5 christos static INLINE void
172 1.1 christos wlat (sim_cpu *scpu, word pc, word x, word v)
173 1.1 christos {
174 1.5 christos address_word cia = CPU_PC_GET (scpu);
175 1.1 christos
176 1.1 christos sim_core_write_aligned_4 (scpu, cia, write_map, x, v);
177 1.1 christos }
178 1.1 christos
179 1.1 christos /* Read 2 bytes from memory. */
180 1.1 christos
181 1.5 christos static INLINE int
182 1.1 christos rsat (sim_cpu *scpu, word pc, word x)
183 1.1 christos {
184 1.5 christos address_word cia = CPU_PC_GET (scpu);
185 1.1 christos
186 1.1 christos return (sim_core_read_aligned_2 (scpu, cia, read_map, x));
187 1.1 christos }
188 1.1 christos
189 1.1 christos /* Read 1 byte from memory. */
190 1.1 christos
191 1.5 christos static INLINE int
192 1.1 christos rbat (sim_cpu *scpu, word pc, word x)
193 1.1 christos {
194 1.5 christos address_word cia = CPU_PC_GET (scpu);
195 1.1 christos
196 1.1 christos return (sim_core_read_aligned_1 (scpu, cia, read_map, x));
197 1.1 christos }
198 1.1 christos
199 1.1 christos /* Read 4 bytes from memory. */
200 1.1 christos
201 1.5 christos static INLINE int
202 1.1 christos rlat (sim_cpu *scpu, word pc, word x)
203 1.1 christos {
204 1.5 christos address_word cia = CPU_PC_GET (scpu);
205 1.1 christos
206 1.1 christos return (sim_core_read_aligned_4 (scpu, cia, read_map, x));
207 1.1 christos }
208 1.1 christos
209 1.1 christos #define CHECK_FLAG(T,H) if (tflags & T) { hflags |= H; tflags ^= T; }
210 1.1 christos
211 1.5 christos static unsigned int
212 1.1 christos convert_target_flags (unsigned int tflags)
213 1.1 christos {
214 1.1 christos unsigned int hflags = 0x0;
215 1.1 christos
216 1.1 christos CHECK_FLAG(0x0001, O_WRONLY);
217 1.1 christos CHECK_FLAG(0x0002, O_RDWR);
218 1.1 christos CHECK_FLAG(0x0008, O_APPEND);
219 1.1 christos CHECK_FLAG(0x0200, O_CREAT);
220 1.1 christos CHECK_FLAG(0x0400, O_TRUNC);
221 1.1 christos CHECK_FLAG(0x0800, O_EXCL);
222 1.1 christos CHECK_FLAG(0x2000, O_SYNC);
223 1.1 christos
224 1.1 christos if (tflags != 0x0)
225 1.1 christos fprintf (stderr,
226 1.1 christos "Simulator Error: problem converting target open flags for host. 0x%x\n",
227 1.1 christos tflags);
228 1.1 christos
229 1.1 christos return hflags;
230 1.1 christos }
231 1.1 christos
232 1.5 christos /* TODO: Split this up into finger trace levels than just insn. */
233 1.5 christos #define MOXIE_TRACE_INSN(str) \
234 1.5 christos TRACE_INSN (scpu, "0x%08x, %s, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x", \
235 1.5 christos opc, str, cpu.asregs.regs[0], cpu.asregs.regs[1], \
236 1.5 christos cpu.asregs.regs[2], cpu.asregs.regs[3], cpu.asregs.regs[4], \
237 1.5 christos cpu.asregs.regs[5], cpu.asregs.regs[6], cpu.asregs.regs[7], \
238 1.5 christos cpu.asregs.regs[8], cpu.asregs.regs[9], cpu.asregs.regs[10], \
239 1.5 christos cpu.asregs.regs[11], cpu.asregs.regs[12], cpu.asregs.regs[13], \
240 1.5 christos cpu.asregs.regs[14], cpu.asregs.regs[15])
241 1.1 christos
242 1.1 christos void
243 1.5 christos sim_engine_run (SIM_DESC sd,
244 1.5 christos int next_cpu_nr, /* ignore */
245 1.5 christos int nr_cpus, /* ignore */
246 1.5 christos int siggnal) /* ignore */
247 1.1 christos {
248 1.1 christos word pc, opc;
249 1.1 christos unsigned short inst;
250 1.1 christos sim_cpu *scpu = STATE_CPU (sd, 0); /* FIXME */
251 1.5 christos address_word cia = CPU_PC_GET (scpu);
252 1.1 christos
253 1.1 christos pc = cpu.asregs.regs[PC_REGNO];
254 1.1 christos
255 1.1 christos /* Run instructions here. */
256 1.1 christos do
257 1.1 christos {
258 1.1 christos opc = pc;
259 1.1 christos
260 1.1 christos /* Fetch the instruction at pc. */
261 1.1 christos inst = (sim_core_read_aligned_1 (scpu, cia, read_map, pc) << 8)
262 1.1 christos + sim_core_read_aligned_1 (scpu, cia, read_map, pc+1);
263 1.1 christos
264 1.1 christos /* Decode instruction. */
265 1.1 christos if (inst & (1 << 15))
266 1.1 christos {
267 1.1 christos if (inst & (1 << 14))
268 1.1 christos {
269 1.1 christos /* This is a Form 3 instruction. */
270 1.1 christos int opcode = (inst >> 10 & 0xf);
271 1.1 christos
272 1.1 christos switch (opcode)
273 1.1 christos {
274 1.1 christos case 0x00: /* beq */
275 1.1 christos {
276 1.5 christos MOXIE_TRACE_INSN ("beq");
277 1.1 christos if (cpu.asregs.cc & CC_EQ)
278 1.1 christos pc += INST2OFFSET(inst);
279 1.1 christos }
280 1.1 christos break;
281 1.1 christos case 0x01: /* bne */
282 1.1 christos {
283 1.5 christos MOXIE_TRACE_INSN ("bne");
284 1.1 christos if (! (cpu.asregs.cc & CC_EQ))
285 1.1 christos pc += INST2OFFSET(inst);
286 1.1 christos }
287 1.1 christos break;
288 1.1 christos case 0x02: /* blt */
289 1.1 christos {
290 1.5 christos MOXIE_TRACE_INSN ("blt");
291 1.1 christos if (cpu.asregs.cc & CC_LT)
292 1.1 christos pc += INST2OFFSET(inst);
293 1.1 christos } break;
294 1.1 christos case 0x03: /* bgt */
295 1.1 christos {
296 1.5 christos MOXIE_TRACE_INSN ("bgt");
297 1.1 christos if (cpu.asregs.cc & CC_GT)
298 1.1 christos pc += INST2OFFSET(inst);
299 1.1 christos }
300 1.1 christos break;
301 1.1 christos case 0x04: /* bltu */
302 1.1 christos {
303 1.5 christos MOXIE_TRACE_INSN ("bltu");
304 1.1 christos if (cpu.asregs.cc & CC_LTU)
305 1.1 christos pc += INST2OFFSET(inst);
306 1.1 christos }
307 1.1 christos break;
308 1.1 christos case 0x05: /* bgtu */
309 1.1 christos {
310 1.5 christos MOXIE_TRACE_INSN ("bgtu");
311 1.1 christos if (cpu.asregs.cc & CC_GTU)
312 1.1 christos pc += INST2OFFSET(inst);
313 1.1 christos }
314 1.1 christos break;
315 1.1 christos case 0x06: /* bge */
316 1.1 christos {
317 1.5 christos MOXIE_TRACE_INSN ("bge");
318 1.1 christos if (cpu.asregs.cc & (CC_GT | CC_EQ))
319 1.1 christos pc += INST2OFFSET(inst);
320 1.1 christos }
321 1.1 christos break;
322 1.1 christos case 0x07: /* ble */
323 1.1 christos {
324 1.5 christos MOXIE_TRACE_INSN ("ble");
325 1.1 christos if (cpu.asregs.cc & (CC_LT | CC_EQ))
326 1.1 christos pc += INST2OFFSET(inst);
327 1.1 christos }
328 1.1 christos break;
329 1.1 christos case 0x08: /* bgeu */
330 1.1 christos {
331 1.5 christos MOXIE_TRACE_INSN ("bgeu");
332 1.1 christos if (cpu.asregs.cc & (CC_GTU | CC_EQ))
333 1.1 christos pc += INST2OFFSET(inst);
334 1.1 christos }
335 1.1 christos break;
336 1.1 christos case 0x09: /* bleu */
337 1.1 christos {
338 1.5 christos MOXIE_TRACE_INSN ("bleu");
339 1.1 christos if (cpu.asregs.cc & (CC_LTU | CC_EQ))
340 1.1 christos pc += INST2OFFSET(inst);
341 1.1 christos }
342 1.1 christos break;
343 1.1 christos default:
344 1.1 christos {
345 1.5 christos MOXIE_TRACE_INSN ("SIGILL3");
346 1.5 christos sim_engine_halt (sd, NULL, NULL, pc, sim_stopped, SIM_SIGILL);
347 1.1 christos break;
348 1.1 christos }
349 1.1 christos }
350 1.1 christos }
351 1.1 christos else
352 1.1 christos {
353 1.1 christos /* This is a Form 2 instruction. */
354 1.1 christos int opcode = (inst >> 12 & 0x3);
355 1.1 christos switch (opcode)
356 1.1 christos {
357 1.1 christos case 0x00: /* inc */
358 1.1 christos {
359 1.1 christos int a = (inst >> 8) & 0xf;
360 1.1 christos unsigned av = cpu.asregs.regs[a];
361 1.1 christos unsigned v = (inst & 0xff);
362 1.3 christos
363 1.5 christos MOXIE_TRACE_INSN ("inc");
364 1.1 christos cpu.asregs.regs[a] = av + v;
365 1.1 christos }
366 1.1 christos break;
367 1.1 christos case 0x01: /* dec */
368 1.1 christos {
369 1.1 christos int a = (inst >> 8) & 0xf;
370 1.1 christos unsigned av = cpu.asregs.regs[a];
371 1.1 christos unsigned v = (inst & 0xff);
372 1.3 christos
373 1.5 christos MOXIE_TRACE_INSN ("dec");
374 1.1 christos cpu.asregs.regs[a] = av - v;
375 1.1 christos }
376 1.1 christos break;
377 1.1 christos case 0x02: /* gsr */
378 1.1 christos {
379 1.1 christos int a = (inst >> 8) & 0xf;
380 1.1 christos unsigned v = (inst & 0xff);
381 1.3 christos
382 1.5 christos MOXIE_TRACE_INSN ("gsr");
383 1.1 christos cpu.asregs.regs[a] = cpu.asregs.sregs[v];
384 1.1 christos }
385 1.1 christos break;
386 1.1 christos case 0x03: /* ssr */
387 1.1 christos {
388 1.1 christos int a = (inst >> 8) & 0xf;
389 1.1 christos unsigned v = (inst & 0xff);
390 1.3 christos
391 1.5 christos MOXIE_TRACE_INSN ("ssr");
392 1.1 christos cpu.asregs.sregs[v] = cpu.asregs.regs[a];
393 1.1 christos }
394 1.1 christos break;
395 1.1 christos default:
396 1.5 christos MOXIE_TRACE_INSN ("SIGILL2");
397 1.5 christos sim_engine_halt (sd, NULL, NULL, pc, sim_stopped, SIM_SIGILL);
398 1.1 christos break;
399 1.1 christos }
400 1.1 christos }
401 1.1 christos }
402 1.1 christos else
403 1.1 christos {
404 1.1 christos /* This is a Form 1 instruction. */
405 1.1 christos int opcode = inst >> 8;
406 1.1 christos switch (opcode)
407 1.1 christos {
408 1.1 christos case 0x00: /* bad */
409 1.1 christos opc = opcode;
410 1.5 christos MOXIE_TRACE_INSN ("SIGILL0");
411 1.5 christos sim_engine_halt (sd, NULL, NULL, pc, sim_stopped, SIM_SIGILL);
412 1.1 christos break;
413 1.1 christos case 0x01: /* ldi.l (immediate) */
414 1.1 christos {
415 1.1 christos int reg = (inst >> 4) & 0xf;
416 1.5 christos unsigned int val = EXTRACT_WORD(pc+2);
417 1.3 christos
418 1.5 christos MOXIE_TRACE_INSN ("ldi.l");
419 1.1 christos cpu.asregs.regs[reg] = val;
420 1.1 christos pc += 4;
421 1.1 christos }
422 1.1 christos break;
423 1.1 christos case 0x02: /* mov (register-to-register) */
424 1.1 christos {
425 1.1 christos int dest = (inst >> 4) & 0xf;
426 1.1 christos int src = (inst ) & 0xf;
427 1.3 christos
428 1.5 christos MOXIE_TRACE_INSN ("mov");
429 1.1 christos cpu.asregs.regs[dest] = cpu.asregs.regs[src];
430 1.1 christos }
431 1.1 christos break;
432 1.1 christos case 0x03: /* jsra */
433 1.1 christos {
434 1.1 christos unsigned int fn = EXTRACT_WORD(pc+2);
435 1.1 christos unsigned int sp = cpu.asregs.regs[1];
436 1.3 christos
437 1.5 christos MOXIE_TRACE_INSN ("jsra");
438 1.1 christos /* Save a slot for the static chain. */
439 1.1 christos sp -= 4;
440 1.1 christos
441 1.1 christos /* Push the return address. */
442 1.1 christos sp -= 4;
443 1.1 christos wlat (scpu, opc, sp, pc + 6);
444 1.1 christos
445 1.1 christos /* Push the current frame pointer. */
446 1.1 christos sp -= 4;
447 1.1 christos wlat (scpu, opc, sp, cpu.asregs.regs[0]);
448 1.1 christos
449 1.1 christos /* Uncache the stack pointer and set the pc and $fp. */
450 1.1 christos cpu.asregs.regs[1] = sp;
451 1.1 christos cpu.asregs.regs[0] = sp;
452 1.1 christos pc = fn - 2;
453 1.1 christos }
454 1.1 christos break;
455 1.1 christos case 0x04: /* ret */
456 1.1 christos {
457 1.1 christos unsigned int sp = cpu.asregs.regs[0];
458 1.1 christos
459 1.5 christos MOXIE_TRACE_INSN ("ret");
460 1.1 christos
461 1.1 christos /* Pop the frame pointer. */
462 1.1 christos cpu.asregs.regs[0] = rlat (scpu, opc, sp);
463 1.1 christos sp += 4;
464 1.1 christos
465 1.1 christos /* Pop the return address. */
466 1.1 christos pc = rlat (scpu, opc, sp) - 2;
467 1.1 christos sp += 4;
468 1.1 christos
469 1.1 christos /* Skip over the static chain slot. */
470 1.1 christos sp += 4;
471 1.1 christos
472 1.1 christos /* Uncache the stack pointer. */
473 1.1 christos cpu.asregs.regs[1] = sp;
474 1.1 christos }
475 1.1 christos break;
476 1.1 christos case 0x05: /* add.l */
477 1.1 christos {
478 1.1 christos int a = (inst >> 4) & 0xf;
479 1.1 christos int b = inst & 0xf;
480 1.1 christos unsigned av = cpu.asregs.regs[a];
481 1.1 christos unsigned bv = cpu.asregs.regs[b];
482 1.3 christos
483 1.5 christos MOXIE_TRACE_INSN ("add.l");
484 1.1 christos cpu.asregs.regs[a] = av + bv;
485 1.1 christos }
486 1.1 christos break;
487 1.1 christos case 0x06: /* push */
488 1.1 christos {
489 1.1 christos int a = (inst >> 4) & 0xf;
490 1.1 christos int b = inst & 0xf;
491 1.1 christos int sp = cpu.asregs.regs[a] - 4;
492 1.3 christos
493 1.5 christos MOXIE_TRACE_INSN ("push");
494 1.1 christos wlat (scpu, opc, sp, cpu.asregs.regs[b]);
495 1.1 christos cpu.asregs.regs[a] = sp;
496 1.1 christos }
497 1.1 christos break;
498 1.1 christos case 0x07: /* pop */
499 1.1 christos {
500 1.1 christos int a = (inst >> 4) & 0xf;
501 1.1 christos int b = inst & 0xf;
502 1.1 christos int sp = cpu.asregs.regs[a];
503 1.3 christos
504 1.5 christos MOXIE_TRACE_INSN ("pop");
505 1.1 christos cpu.asregs.regs[b] = rlat (scpu, opc, sp);
506 1.1 christos cpu.asregs.regs[a] = sp + 4;
507 1.1 christos }
508 1.1 christos break;
509 1.1 christos case 0x08: /* lda.l */
510 1.1 christos {
511 1.1 christos int reg = (inst >> 4) & 0xf;
512 1.1 christos unsigned int addr = EXTRACT_WORD(pc+2);
513 1.3 christos
514 1.5 christos MOXIE_TRACE_INSN ("lda.l");
515 1.1 christos cpu.asregs.regs[reg] = rlat (scpu, opc, addr);
516 1.1 christos pc += 4;
517 1.1 christos }
518 1.1 christos break;
519 1.1 christos case 0x09: /* sta.l */
520 1.1 christos {
521 1.1 christos int reg = (inst >> 4) & 0xf;
522 1.1 christos unsigned int addr = EXTRACT_WORD(pc+2);
523 1.3 christos
524 1.5 christos MOXIE_TRACE_INSN ("sta.l");
525 1.1 christos wlat (scpu, opc, addr, cpu.asregs.regs[reg]);
526 1.1 christos pc += 4;
527 1.1 christos }
528 1.1 christos break;
529 1.1 christos case 0x0a: /* ld.l (register indirect) */
530 1.1 christos {
531 1.1 christos int src = inst & 0xf;
532 1.1 christos int dest = (inst >> 4) & 0xf;
533 1.1 christos int xv;
534 1.3 christos
535 1.5 christos MOXIE_TRACE_INSN ("ld.l");
536 1.1 christos xv = cpu.asregs.regs[src];
537 1.1 christos cpu.asregs.regs[dest] = rlat (scpu, opc, xv);
538 1.1 christos }
539 1.1 christos break;
540 1.1 christos case 0x0b: /* st.l */
541 1.1 christos {
542 1.1 christos int dest = (inst >> 4) & 0xf;
543 1.1 christos int val = inst & 0xf;
544 1.3 christos
545 1.5 christos MOXIE_TRACE_INSN ("st.l");
546 1.1 christos wlat (scpu, opc, cpu.asregs.regs[dest], cpu.asregs.regs[val]);
547 1.1 christos }
548 1.1 christos break;
549 1.1 christos case 0x0c: /* ldo.l */
550 1.1 christos {
551 1.3 christos unsigned int addr = EXTRACT_OFFSET(pc+2);
552 1.1 christos int a = (inst >> 4) & 0xf;
553 1.1 christos int b = inst & 0xf;
554 1.3 christos
555 1.5 christos MOXIE_TRACE_INSN ("ldo.l");
556 1.1 christos addr += cpu.asregs.regs[b];
557 1.1 christos cpu.asregs.regs[a] = rlat (scpu, opc, addr);
558 1.3 christos pc += 2;
559 1.1 christos }
560 1.1 christos break;
561 1.1 christos case 0x0d: /* sto.l */
562 1.1 christos {
563 1.3 christos unsigned int addr = EXTRACT_OFFSET(pc+2);
564 1.1 christos int a = (inst >> 4) & 0xf;
565 1.1 christos int b = inst & 0xf;
566 1.3 christos
567 1.5 christos MOXIE_TRACE_INSN ("sto.l");
568 1.1 christos addr += cpu.asregs.regs[a];
569 1.1 christos wlat (scpu, opc, addr, cpu.asregs.regs[b]);
570 1.3 christos pc += 2;
571 1.1 christos }
572 1.1 christos break;
573 1.1 christos case 0x0e: /* cmp */
574 1.1 christos {
575 1.1 christos int a = (inst >> 4) & 0xf;
576 1.1 christos int b = inst & 0xf;
577 1.1 christos int cc = 0;
578 1.1 christos int va = cpu.asregs.regs[a];
579 1.1 christos int vb = cpu.asregs.regs[b];
580 1.1 christos
581 1.5 christos MOXIE_TRACE_INSN ("cmp");
582 1.1 christos if (va == vb)
583 1.1 christos cc = CC_EQ;
584 1.1 christos else
585 1.1 christos {
586 1.1 christos cc |= (va < vb ? CC_LT : 0);
587 1.1 christos cc |= (va > vb ? CC_GT : 0);
588 1.1 christos cc |= ((unsigned int) va < (unsigned int) vb ? CC_LTU : 0);
589 1.1 christos cc |= ((unsigned int) va > (unsigned int) vb ? CC_GTU : 0);
590 1.1 christos }
591 1.1 christos
592 1.1 christos cpu.asregs.cc = cc;
593 1.1 christos }
594 1.1 christos break;
595 1.1 christos case 0x0f: /* nop */
596 1.1 christos break;
597 1.3 christos case 0x10: /* sex.b */
598 1.3 christos {
599 1.3 christos int a = (inst >> 4) & 0xf;
600 1.3 christos int b = inst & 0xf;
601 1.3 christos signed char bv = cpu.asregs.regs[b];
602 1.3 christos
603 1.5 christos MOXIE_TRACE_INSN ("sex.b");
604 1.3 christos cpu.asregs.regs[a] = (int) bv;
605 1.3 christos }
606 1.3 christos break;
607 1.3 christos case 0x11: /* sex.s */
608 1.3 christos {
609 1.3 christos int a = (inst >> 4) & 0xf;
610 1.3 christos int b = inst & 0xf;
611 1.3 christos signed short bv = cpu.asregs.regs[b];
612 1.3 christos
613 1.5 christos MOXIE_TRACE_INSN ("sex.s");
614 1.3 christos cpu.asregs.regs[a] = (int) bv;
615 1.3 christos }
616 1.3 christos break;
617 1.3 christos case 0x12: /* zex.b */
618 1.3 christos {
619 1.3 christos int a = (inst >> 4) & 0xf;
620 1.3 christos int b = inst & 0xf;
621 1.3 christos signed char bv = cpu.asregs.regs[b];
622 1.3 christos
623 1.5 christos MOXIE_TRACE_INSN ("zex.b");
624 1.3 christos cpu.asregs.regs[a] = (int) bv & 0xff;
625 1.3 christos }
626 1.3 christos break;
627 1.3 christos case 0x13: /* zex.s */
628 1.3 christos {
629 1.3 christos int a = (inst >> 4) & 0xf;
630 1.3 christos int b = inst & 0xf;
631 1.3 christos signed short bv = cpu.asregs.regs[b];
632 1.3 christos
633 1.5 christos MOXIE_TRACE_INSN ("zex.s");
634 1.3 christos cpu.asregs.regs[a] = (int) bv & 0xffff;
635 1.3 christos }
636 1.3 christos break;
637 1.3 christos case 0x14: /* umul.x */
638 1.3 christos {
639 1.3 christos int a = (inst >> 4) & 0xf;
640 1.3 christos int b = inst & 0xf;
641 1.3 christos unsigned av = cpu.asregs.regs[a];
642 1.3 christos unsigned bv = cpu.asregs.regs[b];
643 1.3 christos unsigned long long r =
644 1.3 christos (unsigned long long) av * (unsigned long long) bv;
645 1.3 christos
646 1.5 christos MOXIE_TRACE_INSN ("umul.x");
647 1.3 christos cpu.asregs.regs[a] = r >> 32;
648 1.3 christos }
649 1.3 christos break;
650 1.3 christos case 0x15: /* mul.x */
651 1.3 christos {
652 1.3 christos int a = (inst >> 4) & 0xf;
653 1.3 christos int b = inst & 0xf;
654 1.3 christos unsigned av = cpu.asregs.regs[a];
655 1.3 christos unsigned bv = cpu.asregs.regs[b];
656 1.3 christos signed long long r =
657 1.3 christos (signed long long) av * (signed long long) bv;
658 1.3 christos
659 1.5 christos MOXIE_TRACE_INSN ("mul.x");
660 1.3 christos cpu.asregs.regs[a] = r >> 32;
661 1.3 christos }
662 1.3 christos break;
663 1.1 christos case 0x16: /* bad */
664 1.1 christos case 0x17: /* bad */
665 1.1 christos case 0x18: /* bad */
666 1.1 christos {
667 1.1 christos opc = opcode;
668 1.5 christos MOXIE_TRACE_INSN ("SIGILL0");
669 1.5 christos sim_engine_halt (sd, NULL, NULL, pc, sim_stopped, SIM_SIGILL);
670 1.1 christos break;
671 1.1 christos }
672 1.1 christos case 0x19: /* jsr */
673 1.1 christos {
674 1.1 christos unsigned int fn = cpu.asregs.regs[(inst >> 4) & 0xf];
675 1.1 christos unsigned int sp = cpu.asregs.regs[1];
676 1.1 christos
677 1.5 christos MOXIE_TRACE_INSN ("jsr");
678 1.1 christos
679 1.1 christos /* Save a slot for the static chain. */
680 1.1 christos sp -= 4;
681 1.1 christos
682 1.1 christos /* Push the return address. */
683 1.1 christos sp -= 4;
684 1.1 christos wlat (scpu, opc, sp, pc + 2);
685 1.1 christos
686 1.1 christos /* Push the current frame pointer. */
687 1.1 christos sp -= 4;
688 1.1 christos wlat (scpu, opc, sp, cpu.asregs.regs[0]);
689 1.1 christos
690 1.1 christos /* Uncache the stack pointer and set the fp & pc. */
691 1.1 christos cpu.asregs.regs[1] = sp;
692 1.1 christos cpu.asregs.regs[0] = sp;
693 1.1 christos pc = fn - 2;
694 1.1 christos }
695 1.1 christos break;
696 1.1 christos case 0x1a: /* jmpa */
697 1.1 christos {
698 1.1 christos unsigned int tgt = EXTRACT_WORD(pc+2);
699 1.3 christos
700 1.5 christos MOXIE_TRACE_INSN ("jmpa");
701 1.1 christos pc = tgt - 2;
702 1.1 christos }
703 1.1 christos break;
704 1.1 christos case 0x1b: /* ldi.b (immediate) */
705 1.1 christos {
706 1.1 christos int reg = (inst >> 4) & 0xf;
707 1.3 christos unsigned int val = EXTRACT_WORD(pc+2);
708 1.1 christos
709 1.5 christos MOXIE_TRACE_INSN ("ldi.b");
710 1.1 christos cpu.asregs.regs[reg] = val;
711 1.1 christos pc += 4;
712 1.1 christos }
713 1.1 christos break;
714 1.1 christos case 0x1c: /* ld.b (register indirect) */
715 1.1 christos {
716 1.1 christos int src = inst & 0xf;
717 1.1 christos int dest = (inst >> 4) & 0xf;
718 1.1 christos int xv;
719 1.3 christos
720 1.5 christos MOXIE_TRACE_INSN ("ld.b");
721 1.1 christos xv = cpu.asregs.regs[src];
722 1.1 christos cpu.asregs.regs[dest] = rbat (scpu, opc, xv);
723 1.1 christos }
724 1.1 christos break;
725 1.1 christos case 0x1d: /* lda.b */
726 1.1 christos {
727 1.1 christos int reg = (inst >> 4) & 0xf;
728 1.1 christos unsigned int addr = EXTRACT_WORD(pc+2);
729 1.3 christos
730 1.5 christos MOXIE_TRACE_INSN ("lda.b");
731 1.1 christos cpu.asregs.regs[reg] = rbat (scpu, opc, addr);
732 1.1 christos pc += 4;
733 1.1 christos }
734 1.1 christos break;
735 1.1 christos case 0x1e: /* st.b */
736 1.1 christos {
737 1.1 christos int dest = (inst >> 4) & 0xf;
738 1.1 christos int val = inst & 0xf;
739 1.3 christos
740 1.5 christos MOXIE_TRACE_INSN ("st.b");
741 1.1 christos wbat (scpu, opc, cpu.asregs.regs[dest], cpu.asregs.regs[val]);
742 1.1 christos }
743 1.1 christos break;
744 1.1 christos case 0x1f: /* sta.b */
745 1.1 christos {
746 1.1 christos int reg = (inst >> 4) & 0xf;
747 1.1 christos unsigned int addr = EXTRACT_WORD(pc+2);
748 1.3 christos
749 1.5 christos MOXIE_TRACE_INSN ("sta.b");
750 1.1 christos wbat (scpu, opc, addr, cpu.asregs.regs[reg]);
751 1.1 christos pc += 4;
752 1.1 christos }
753 1.1 christos break;
754 1.1 christos case 0x20: /* ldi.s (immediate) */
755 1.1 christos {
756 1.1 christos int reg = (inst >> 4) & 0xf;
757 1.1 christos
758 1.1 christos unsigned int val = EXTRACT_WORD(pc+2);
759 1.3 christos
760 1.5 christos MOXIE_TRACE_INSN ("ldi.s");
761 1.1 christos cpu.asregs.regs[reg] = val;
762 1.1 christos pc += 4;
763 1.1 christos }
764 1.1 christos break;
765 1.1 christos case 0x21: /* ld.s (register indirect) */
766 1.1 christos {
767 1.1 christos int src = inst & 0xf;
768 1.1 christos int dest = (inst >> 4) & 0xf;
769 1.1 christos int xv;
770 1.3 christos
771 1.5 christos MOXIE_TRACE_INSN ("ld.s");
772 1.1 christos xv = cpu.asregs.regs[src];
773 1.1 christos cpu.asregs.regs[dest] = rsat (scpu, opc, xv);
774 1.1 christos }
775 1.1 christos break;
776 1.1 christos case 0x22: /* lda.s */
777 1.1 christos {
778 1.1 christos int reg = (inst >> 4) & 0xf;
779 1.1 christos unsigned int addr = EXTRACT_WORD(pc+2);
780 1.3 christos
781 1.5 christos MOXIE_TRACE_INSN ("lda.s");
782 1.1 christos cpu.asregs.regs[reg] = rsat (scpu, opc, addr);
783 1.1 christos pc += 4;
784 1.1 christos }
785 1.1 christos break;
786 1.1 christos case 0x23: /* st.s */
787 1.1 christos {
788 1.1 christos int dest = (inst >> 4) & 0xf;
789 1.1 christos int val = inst & 0xf;
790 1.3 christos
791 1.5 christos MOXIE_TRACE_INSN ("st.s");
792 1.1 christos wsat (scpu, opc, cpu.asregs.regs[dest], cpu.asregs.regs[val]);
793 1.1 christos }
794 1.1 christos break;
795 1.1 christos case 0x24: /* sta.s */
796 1.1 christos {
797 1.1 christos int reg = (inst >> 4) & 0xf;
798 1.1 christos unsigned int addr = EXTRACT_WORD(pc+2);
799 1.3 christos
800 1.5 christos MOXIE_TRACE_INSN ("sta.s");
801 1.1 christos wsat (scpu, opc, addr, cpu.asregs.regs[reg]);
802 1.1 christos pc += 4;
803 1.1 christos }
804 1.1 christos break;
805 1.1 christos case 0x25: /* jmp */
806 1.1 christos {
807 1.1 christos int reg = (inst >> 4) & 0xf;
808 1.3 christos
809 1.5 christos MOXIE_TRACE_INSN ("jmp");
810 1.1 christos pc = cpu.asregs.regs[reg] - 2;
811 1.1 christos }
812 1.1 christos break;
813 1.1 christos case 0x26: /* and */
814 1.1 christos {
815 1.1 christos int a = (inst >> 4) & 0xf;
816 1.1 christos int b = inst & 0xf;
817 1.1 christos int av, bv;
818 1.3 christos
819 1.5 christos MOXIE_TRACE_INSN ("and");
820 1.1 christos av = cpu.asregs.regs[a];
821 1.1 christos bv = cpu.asregs.regs[b];
822 1.1 christos cpu.asregs.regs[a] = av & bv;
823 1.1 christos }
824 1.1 christos break;
825 1.1 christos case 0x27: /* lshr */
826 1.1 christos {
827 1.1 christos int a = (inst >> 4) & 0xf;
828 1.1 christos int b = inst & 0xf;
829 1.1 christos int av = cpu.asregs.regs[a];
830 1.1 christos int bv = cpu.asregs.regs[b];
831 1.3 christos
832 1.5 christos MOXIE_TRACE_INSN ("lshr");
833 1.1 christos cpu.asregs.regs[a] = (unsigned) ((unsigned) av >> bv);
834 1.1 christos }
835 1.1 christos break;
836 1.1 christos case 0x28: /* ashl */
837 1.1 christos {
838 1.1 christos int a = (inst >> 4) & 0xf;
839 1.1 christos int b = inst & 0xf;
840 1.1 christos int av = cpu.asregs.regs[a];
841 1.1 christos int bv = cpu.asregs.regs[b];
842 1.3 christos
843 1.5 christos MOXIE_TRACE_INSN ("ashl");
844 1.1 christos cpu.asregs.regs[a] = av << bv;
845 1.1 christos }
846 1.1 christos break;
847 1.1 christos case 0x29: /* sub.l */
848 1.1 christos {
849 1.1 christos int a = (inst >> 4) & 0xf;
850 1.1 christos int b = inst & 0xf;
851 1.1 christos unsigned av = cpu.asregs.regs[a];
852 1.1 christos unsigned bv = cpu.asregs.regs[b];
853 1.3 christos
854 1.5 christos MOXIE_TRACE_INSN ("sub.l");
855 1.1 christos cpu.asregs.regs[a] = av - bv;
856 1.1 christos }
857 1.1 christos break;
858 1.1 christos case 0x2a: /* neg */
859 1.1 christos {
860 1.1 christos int a = (inst >> 4) & 0xf;
861 1.1 christos int b = inst & 0xf;
862 1.1 christos int bv = cpu.asregs.regs[b];
863 1.3 christos
864 1.5 christos MOXIE_TRACE_INSN ("neg");
865 1.1 christos cpu.asregs.regs[a] = - bv;
866 1.1 christos }
867 1.1 christos break;
868 1.1 christos case 0x2b: /* or */
869 1.1 christos {
870 1.1 christos int a = (inst >> 4) & 0xf;
871 1.1 christos int b = inst & 0xf;
872 1.1 christos int av, bv;
873 1.3 christos
874 1.5 christos MOXIE_TRACE_INSN ("or");
875 1.1 christos av = cpu.asregs.regs[a];
876 1.1 christos bv = cpu.asregs.regs[b];
877 1.1 christos cpu.asregs.regs[a] = av | bv;
878 1.1 christos }
879 1.1 christos break;
880 1.1 christos case 0x2c: /* not */
881 1.1 christos {
882 1.1 christos int a = (inst >> 4) & 0xf;
883 1.1 christos int b = inst & 0xf;
884 1.1 christos int bv = cpu.asregs.regs[b];
885 1.3 christos
886 1.5 christos MOXIE_TRACE_INSN ("not");
887 1.1 christos cpu.asregs.regs[a] = 0xffffffff ^ bv;
888 1.1 christos }
889 1.1 christos break;
890 1.1 christos case 0x2d: /* ashr */
891 1.1 christos {
892 1.1 christos int a = (inst >> 4) & 0xf;
893 1.1 christos int b = inst & 0xf;
894 1.1 christos int av = cpu.asregs.regs[a];
895 1.1 christos int bv = cpu.asregs.regs[b];
896 1.3 christos
897 1.5 christos MOXIE_TRACE_INSN ("ashr");
898 1.1 christos cpu.asregs.regs[a] = av >> bv;
899 1.1 christos }
900 1.1 christos break;
901 1.1 christos case 0x2e: /* xor */
902 1.1 christos {
903 1.1 christos int a = (inst >> 4) & 0xf;
904 1.1 christos int b = inst & 0xf;
905 1.1 christos int av, bv;
906 1.3 christos
907 1.5 christos MOXIE_TRACE_INSN ("xor");
908 1.1 christos av = cpu.asregs.regs[a];
909 1.1 christos bv = cpu.asregs.regs[b];
910 1.1 christos cpu.asregs.regs[a] = av ^ bv;
911 1.1 christos }
912 1.1 christos break;
913 1.1 christos case 0x2f: /* mul.l */
914 1.1 christos {
915 1.1 christos int a = (inst >> 4) & 0xf;
916 1.1 christos int b = inst & 0xf;
917 1.1 christos unsigned av = cpu.asregs.regs[a];
918 1.1 christos unsigned bv = cpu.asregs.regs[b];
919 1.3 christos
920 1.5 christos MOXIE_TRACE_INSN ("mul.l");
921 1.1 christos cpu.asregs.regs[a] = av * bv;
922 1.1 christos }
923 1.1 christos break;
924 1.1 christos case 0x30: /* swi */
925 1.1 christos {
926 1.1 christos unsigned int inum = EXTRACT_WORD(pc+2);
927 1.3 christos
928 1.5 christos MOXIE_TRACE_INSN ("swi");
929 1.1 christos /* Set the special registers appropriately. */
930 1.1 christos cpu.asregs.sregs[2] = 3; /* MOXIE_EX_SWI */
931 1.1 christos cpu.asregs.sregs[3] = inum;
932 1.1 christos switch (inum)
933 1.1 christos {
934 1.1 christos case 0x1: /* SYS_exit */
935 1.1 christos {
936 1.5 christos sim_engine_halt (sd, NULL, NULL, pc, sim_exited,
937 1.5 christos cpu.asregs.regs[2]);
938 1.1 christos break;
939 1.1 christos }
940 1.1 christos case 0x2: /* SYS_open */
941 1.1 christos {
942 1.1 christos char fname[1024];
943 1.1 christos int mode = (int) convert_target_flags ((unsigned) cpu.asregs.regs[3]);
944 1.1 christos int perm = (int) cpu.asregs.regs[4];
945 1.1 christos int fd = open (fname, mode, perm);
946 1.1 christos sim_core_read_buffer (sd, scpu, read_map, fname,
947 1.1 christos cpu.asregs.regs[2], 1024);
948 1.1 christos /* FIXME - set errno */
949 1.1 christos cpu.asregs.regs[2] = fd;
950 1.1 christos break;
951 1.1 christos }
952 1.1 christos case 0x4: /* SYS_read */
953 1.1 christos {
954 1.1 christos int fd = cpu.asregs.regs[2];
955 1.1 christos unsigned len = (unsigned) cpu.asregs.regs[4];
956 1.1 christos char *buf = malloc (len);
957 1.1 christos cpu.asregs.regs[2] = read (fd, buf, len);
958 1.1 christos sim_core_write_buffer (sd, scpu, write_map, buf,
959 1.1 christos cpu.asregs.regs[3], len);
960 1.1 christos free (buf);
961 1.1 christos break;
962 1.1 christos }
963 1.1 christos case 0x5: /* SYS_write */
964 1.1 christos {
965 1.1 christos char *str;
966 1.1 christos /* String length is at 0x12($fp) */
967 1.1 christos unsigned count, len = (unsigned) cpu.asregs.regs[4];
968 1.1 christos str = malloc (len);
969 1.1 christos sim_core_read_buffer (sd, scpu, read_map, str,
970 1.1 christos cpu.asregs.regs[3], len);
971 1.1 christos count = write (cpu.asregs.regs[2], str, len);
972 1.1 christos free (str);
973 1.1 christos cpu.asregs.regs[2] = count;
974 1.1 christos break;
975 1.1 christos }
976 1.1 christos case 0xffffffff: /* Linux System Call */
977 1.1 christos {
978 1.1 christos unsigned int handler = cpu.asregs.sregs[1];
979 1.1 christos unsigned int sp = cpu.asregs.regs[1];
980 1.1 christos
981 1.1 christos /* Save a slot for the static chain. */
982 1.1 christos sp -= 4;
983 1.1 christos
984 1.1 christos /* Push the return address. */
985 1.1 christos sp -= 4;
986 1.1 christos wlat (scpu, opc, sp, pc + 6);
987 1.1 christos
988 1.1 christos /* Push the current frame pointer. */
989 1.1 christos sp -= 4;
990 1.1 christos wlat (scpu, opc, sp, cpu.asregs.regs[0]);
991 1.1 christos
992 1.1 christos /* Uncache the stack pointer and set the fp & pc. */
993 1.1 christos cpu.asregs.regs[1] = sp;
994 1.1 christos cpu.asregs.regs[0] = sp;
995 1.1 christos pc = handler - 6;
996 1.1 christos }
997 1.1 christos default:
998 1.1 christos break;
999 1.1 christos }
1000 1.1 christos pc += 4;
1001 1.1 christos }
1002 1.1 christos break;
1003 1.1 christos case 0x31: /* div.l */
1004 1.1 christos {
1005 1.1 christos int a = (inst >> 4) & 0xf;
1006 1.1 christos int b = inst & 0xf;
1007 1.1 christos int av = cpu.asregs.regs[a];
1008 1.1 christos int bv = cpu.asregs.regs[b];
1009 1.3 christos
1010 1.5 christos MOXIE_TRACE_INSN ("div.l");
1011 1.1 christos cpu.asregs.regs[a] = av / bv;
1012 1.1 christos }
1013 1.1 christos break;
1014 1.1 christos case 0x32: /* udiv.l */
1015 1.1 christos {
1016 1.1 christos int a = (inst >> 4) & 0xf;
1017 1.1 christos int b = inst & 0xf;
1018 1.1 christos unsigned int av = cpu.asregs.regs[a];
1019 1.1 christos unsigned int bv = cpu.asregs.regs[b];
1020 1.3 christos
1021 1.5 christos MOXIE_TRACE_INSN ("udiv.l");
1022 1.1 christos cpu.asregs.regs[a] = (av / bv);
1023 1.1 christos }
1024 1.1 christos break;
1025 1.1 christos case 0x33: /* mod.l */
1026 1.1 christos {
1027 1.1 christos int a = (inst >> 4) & 0xf;
1028 1.1 christos int b = inst & 0xf;
1029 1.1 christos int av = cpu.asregs.regs[a];
1030 1.1 christos int bv = cpu.asregs.regs[b];
1031 1.3 christos
1032 1.5 christos MOXIE_TRACE_INSN ("mod.l");
1033 1.1 christos cpu.asregs.regs[a] = av % bv;
1034 1.1 christos }
1035 1.1 christos break;
1036 1.1 christos case 0x34: /* umod.l */
1037 1.1 christos {
1038 1.1 christos int a = (inst >> 4) & 0xf;
1039 1.1 christos int b = inst & 0xf;
1040 1.1 christos unsigned int av = cpu.asregs.regs[a];
1041 1.1 christos unsigned int bv = cpu.asregs.regs[b];
1042 1.3 christos
1043 1.5 christos MOXIE_TRACE_INSN ("umod.l");
1044 1.1 christos cpu.asregs.regs[a] = (av % bv);
1045 1.1 christos }
1046 1.1 christos break;
1047 1.1 christos case 0x35: /* brk */
1048 1.5 christos MOXIE_TRACE_INSN ("brk");
1049 1.5 christos sim_engine_halt (sd, NULL, NULL, pc, sim_stopped, SIM_SIGTRAP);
1050 1.1 christos pc -= 2; /* Adjust pc */
1051 1.1 christos break;
1052 1.1 christos case 0x36: /* ldo.b */
1053 1.1 christos {
1054 1.3 christos unsigned int addr = EXTRACT_OFFSET(pc+2);
1055 1.1 christos int a = (inst >> 4) & 0xf;
1056 1.1 christos int b = inst & 0xf;
1057 1.3 christos
1058 1.5 christos MOXIE_TRACE_INSN ("ldo.b");
1059 1.1 christos addr += cpu.asregs.regs[b];
1060 1.1 christos cpu.asregs.regs[a] = rbat (scpu, opc, addr);
1061 1.3 christos pc += 2;
1062 1.1 christos }
1063 1.1 christos break;
1064 1.1 christos case 0x37: /* sto.b */
1065 1.1 christos {
1066 1.3 christos unsigned int addr = EXTRACT_OFFSET(pc+2);
1067 1.1 christos int a = (inst >> 4) & 0xf;
1068 1.1 christos int b = inst & 0xf;
1069 1.3 christos
1070 1.5 christos MOXIE_TRACE_INSN ("sto.b");
1071 1.1 christos addr += cpu.asregs.regs[a];
1072 1.1 christos wbat (scpu, opc, addr, cpu.asregs.regs[b]);
1073 1.3 christos pc += 2;
1074 1.1 christos }
1075 1.1 christos break;
1076 1.1 christos case 0x38: /* ldo.s */
1077 1.1 christos {
1078 1.3 christos unsigned int addr = EXTRACT_OFFSET(pc+2);
1079 1.1 christos int a = (inst >> 4) & 0xf;
1080 1.1 christos int b = inst & 0xf;
1081 1.3 christos
1082 1.5 christos MOXIE_TRACE_INSN ("ldo.s");
1083 1.1 christos addr += cpu.asregs.regs[b];
1084 1.1 christos cpu.asregs.regs[a] = rsat (scpu, opc, addr);
1085 1.3 christos pc += 2;
1086 1.1 christos }
1087 1.1 christos break;
1088 1.1 christos case 0x39: /* sto.s */
1089 1.1 christos {
1090 1.3 christos unsigned int addr = EXTRACT_OFFSET(pc+2);
1091 1.1 christos int a = (inst >> 4) & 0xf;
1092 1.1 christos int b = inst & 0xf;
1093 1.3 christos
1094 1.5 christos MOXIE_TRACE_INSN ("sto.s");
1095 1.1 christos addr += cpu.asregs.regs[a];
1096 1.1 christos wsat (scpu, opc, addr, cpu.asregs.regs[b]);
1097 1.3 christos pc += 2;
1098 1.1 christos }
1099 1.1 christos break;
1100 1.1 christos default:
1101 1.1 christos opc = opcode;
1102 1.5 christos MOXIE_TRACE_INSN ("SIGILL1");
1103 1.5 christos sim_engine_halt (sd, NULL, NULL, pc, sim_stopped, SIM_SIGILL);
1104 1.1 christos break;
1105 1.1 christos }
1106 1.1 christos }
1107 1.1 christos
1108 1.5 christos cpu.asregs.insts++;
1109 1.1 christos pc += 2;
1110 1.5 christos cpu.asregs.regs[PC_REGNO] = pc;
1111 1.5 christos } while (1);
1112 1.1 christos }
1113 1.1 christos
1114 1.6 christos static int
1115 1.6 christos moxie_reg_store (SIM_CPU *scpu, int rn, unsigned char *memory, int length)
1116 1.1 christos {
1117 1.1 christos if (rn < NUM_MOXIE_REGS && rn >= 0)
1118 1.1 christos {
1119 1.1 christos if (length == 4)
1120 1.1 christos {
1121 1.1 christos long ival;
1122 1.1 christos
1123 1.1 christos /* misalignment safe */
1124 1.1 christos ival = moxie_extract_unsigned_integer (memory, 4);
1125 1.1 christos cpu.asints[rn] = ival;
1126 1.1 christos }
1127 1.1 christos
1128 1.1 christos return 4;
1129 1.1 christos }
1130 1.1 christos else
1131 1.1 christos return 0;
1132 1.1 christos }
1133 1.1 christos
1134 1.6 christos static int
1135 1.6 christos moxie_reg_fetch (SIM_CPU *scpu, int rn, unsigned char *memory, int length)
1136 1.1 christos {
1137 1.1 christos if (rn < NUM_MOXIE_REGS && rn >= 0)
1138 1.1 christos {
1139 1.1 christos if (length == 4)
1140 1.1 christos {
1141 1.1 christos long ival = cpu.asints[rn];
1142 1.1 christos
1143 1.1 christos /* misalignment-safe */
1144 1.1 christos moxie_store_unsigned_integer (memory, 4, ival);
1145 1.1 christos }
1146 1.1 christos
1147 1.1 christos return 4;
1148 1.1 christos }
1149 1.1 christos else
1150 1.1 christos return 0;
1151 1.1 christos }
1152 1.1 christos
1153 1.5 christos static sim_cia
1154 1.5 christos moxie_pc_get (sim_cpu *cpu)
1155 1.1 christos {
1156 1.5 christos return cpu->registers[PCIDX];
1157 1.1 christos }
1158 1.1 christos
1159 1.5 christos static void
1160 1.5 christos moxie_pc_set (sim_cpu *cpu, sim_cia pc)
1161 1.1 christos {
1162 1.5 christos cpu->registers[PCIDX] = pc;
1163 1.1 christos }
1164 1.1 christos
1165 1.5 christos static void
1166 1.5 christos free_state (SIM_DESC sd)
1167 1.1 christos {
1168 1.5 christos if (STATE_MODULES (sd) != NULL)
1169 1.5 christos sim_module_uninstall (sd);
1170 1.5 christos sim_cpu_free_all (sd);
1171 1.5 christos sim_state_free (sd);
1172 1.1 christos }
1173 1.1 christos
1174 1.1 christos SIM_DESC
1175 1.6 christos sim_open (SIM_OPEN_KIND kind, host_callback *cb,
1176 1.6 christos struct bfd *abfd, char * const *argv)
1177 1.1 christos {
1178 1.5 christos int i;
1179 1.1 christos SIM_DESC sd = sim_state_alloc (kind, cb);
1180 1.1 christos SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
1181 1.1 christos
1182 1.5 christos /* The cpu data is kept in a separately allocated chunk of memory. */
1183 1.5 christos if (sim_cpu_alloc_all (sd, 1, /*cgen_cpu_max_extra_bytes ()*/0) != SIM_RC_OK)
1184 1.5 christos {
1185 1.5 christos free_state (sd);
1186 1.5 christos return 0;
1187 1.5 christos }
1188 1.5 christos
1189 1.5 christos STATE_WATCHPOINTS (sd)->pc = &cpu.asregs.regs[PC_REGNO];
1190 1.5 christos STATE_WATCHPOINTS (sd)->sizeof_pc = sizeof (word);
1191 1.5 christos
1192 1.1 christos if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
1193 1.5 christos {
1194 1.5 christos free_state (sd);
1195 1.5 christos return 0;
1196 1.5 christos }
1197 1.5 christos
1198 1.6 christos /* The parser will print an error message for us, so we silently return. */
1199 1.5 christos if (sim_parse_args (sd, argv) != SIM_RC_OK)
1200 1.5 christos {
1201 1.5 christos free_state (sd);
1202 1.5 christos return 0;
1203 1.5 christos }
1204 1.1 christos
1205 1.1 christos sim_do_command(sd," memory region 0x00000000,0x4000000") ;
1206 1.1 christos sim_do_command(sd," memory region 0xE0000000,0x10000") ;
1207 1.1 christos
1208 1.5 christos /* Check for/establish the a reference program image. */
1209 1.5 christos if (sim_analyze_program (sd,
1210 1.5 christos (STATE_PROG_ARGV (sd) != NULL
1211 1.5 christos ? *STATE_PROG_ARGV (sd)
1212 1.5 christos : NULL), abfd) != SIM_RC_OK)
1213 1.5 christos {
1214 1.5 christos free_state (sd);
1215 1.5 christos return 0;
1216 1.5 christos }
1217 1.5 christos
1218 1.1 christos /* Configure/verify the target byte order and other runtime
1219 1.1 christos configuration options. */
1220 1.1 christos if (sim_config (sd) != SIM_RC_OK)
1221 1.1 christos {
1222 1.1 christos sim_module_uninstall (sd);
1223 1.1 christos return 0;
1224 1.1 christos }
1225 1.1 christos
1226 1.1 christos if (sim_post_argv_init (sd) != SIM_RC_OK)
1227 1.1 christos {
1228 1.1 christos /* Uninstall the modules to avoid memory leaks,
1229 1.1 christos file descriptor leaks, etc. */
1230 1.1 christos sim_module_uninstall (sd);
1231 1.1 christos return 0;
1232 1.1 christos }
1233 1.1 christos
1234 1.5 christos /* CPU specific initialization. */
1235 1.5 christos for (i = 0; i < MAX_NR_PROCESSORS; ++i)
1236 1.5 christos {
1237 1.5 christos SIM_CPU *cpu = STATE_CPU (sd, i);
1238 1.5 christos
1239 1.6 christos CPU_REG_FETCH (cpu) = moxie_reg_fetch;
1240 1.6 christos CPU_REG_STORE (cpu) = moxie_reg_store;
1241 1.5 christos CPU_PC_FETCH (cpu) = moxie_pc_get;
1242 1.5 christos CPU_PC_STORE (cpu) = moxie_pc_set;
1243 1.5 christos
1244 1.5 christos set_initial_gprs (); /* Reset the GPR registers. */
1245 1.5 christos }
1246 1.5 christos
1247 1.1 christos return sd;
1248 1.1 christos }
1249 1.1 christos
1250 1.1 christos /* Load the device tree blob. */
1251 1.1 christos
1252 1.1 christos static void
1253 1.1 christos load_dtb (SIM_DESC sd, const char *filename)
1254 1.1 christos {
1255 1.1 christos int size = 0;
1256 1.1 christos FILE *f = fopen (filename, "rb");
1257 1.1 christos char *buf;
1258 1.1 christos sim_cpu *scpu = STATE_CPU (sd, 0); /* FIXME */
1259 1.5 christos
1260 1.5 christos /* Don't warn as the sim works fine w/out a device tree. */
1261 1.5 christos if (f == NULL)
1262 1.5 christos return;
1263 1.1 christos fseek (f, 0, SEEK_END);
1264 1.1 christos size = ftell(f);
1265 1.1 christos fseek (f, 0, SEEK_SET);
1266 1.1 christos buf = alloca (size);
1267 1.1 christos if (size != fread (buf, 1, size, f))
1268 1.1 christos {
1269 1.5 christos sim_io_eprintf (sd, "ERROR: error reading ``%s''.\n", filename);
1270 1.6 christos fclose (f);
1271 1.1 christos return;
1272 1.1 christos }
1273 1.1 christos sim_core_write_buffer (sd, scpu, write_map, buf, 0xE0000000, size);
1274 1.1 christos cpu.asregs.sregs[9] = 0xE0000000;
1275 1.1 christos fclose (f);
1276 1.1 christos }
1277 1.1 christos
1278 1.1 christos SIM_RC
1279 1.6 christos sim_create_inferior (SIM_DESC sd, struct bfd *prog_bfd,
1280 1.6 christos char * const *argv, char * const *env)
1281 1.1 christos {
1282 1.1 christos char ** avp;
1283 1.1 christos int l, argc, i, tp;
1284 1.1 christos sim_cpu *scpu = STATE_CPU (sd, 0); /* FIXME */
1285 1.1 christos
1286 1.1 christos if (prog_bfd != NULL)
1287 1.1 christos cpu.asregs.regs[PC_REGNO] = bfd_get_start_address (prog_bfd);
1288 1.1 christos
1289 1.1 christos /* Copy args into target memory. */
1290 1.1 christos avp = argv;
1291 1.1 christos for (argc = 0; avp && *avp; avp++)
1292 1.1 christos argc++;
1293 1.1 christos
1294 1.1 christos /* Target memory looks like this:
1295 1.1 christos 0x00000000 zero word
1296 1.1 christos 0x00000004 argc word
1297 1.1 christos 0x00000008 start of argv
1298 1.1 christos .
1299 1.1 christos 0x0000???? end of argv
1300 1.1 christos 0x0000???? zero word
1301 1.1 christos 0x0000???? start of data pointed to by argv */
1302 1.1 christos
1303 1.1 christos wlat (scpu, 0, 0, 0);
1304 1.1 christos wlat (scpu, 0, 4, argc);
1305 1.1 christos
1306 1.1 christos /* tp is the offset of our first argv data. */
1307 1.1 christos tp = 4 + 4 + argc * 4 + 4;
1308 1.1 christos
1309 1.1 christos for (i = 0; i < argc; i++)
1310 1.1 christos {
1311 1.1 christos /* Set the argv value. */
1312 1.1 christos wlat (scpu, 0, 4 + 4 + i * 4, tp);
1313 1.1 christos
1314 1.1 christos /* Store the string. */
1315 1.1 christos sim_core_write_buffer (sd, scpu, write_map, argv[i],
1316 1.1 christos tp, strlen(argv[i])+1);
1317 1.1 christos tp += strlen (argv[i]) + 1;
1318 1.1 christos }
1319 1.1 christos
1320 1.1 christos wlat (scpu, 0, 4 + 4 + i * 4, 0);
1321 1.1 christos
1322 1.1 christos load_dtb (sd, DTB);
1323 1.1 christos
1324 1.1 christos return SIM_RC_OK;
1325 1.1 christos }
1326