ld: Move the .note.build-id section to near the start of the memory map.
[binutils-gdb.git] / sim / v850 / simops.c
blob97948f75f92d87fff767793e89fee1997c45c9af
1 /* This must come before any other includes. */
2 #include "defs.h"
4 #include "sim-main.h"
5 #include "sim-signal.h"
6 #include "v850-sim.h"
7 #include "simops.h"
9 #include <sys/types.h>
11 #ifdef HAVE_UTIME_H
12 #include <utime.h>
13 #endif
14 #include <time.h>
15 #include <unistd.h>
16 #include <stdlib.h>
17 #include <string.h>
19 #include "libiberty.h"
21 #include <errno.h>
22 #if !defined(__GO32__) && !defined(_WIN32)
23 #include <sys/stat.h>
24 #include <sys/times.h>
25 #include <sys/time.h>
26 #endif
28 #include "target-newlib-syscall.h"
30 /* This is an array of the bit positions of registers r20 .. r31 in
31 that order in a prepare/dispose instruction. */
32 int type1_regs[12] = { 27, 26, 25, 24, 31, 30, 29, 28, 23, 22, 0, 21 };
33 /* This is an array of the bit positions of registers r16 .. r31 in
34 that order in a push/pop instruction. */
35 int type2_regs[16] = { 3, 2, 1, 0, 27, 26, 25, 24, 31, 30, 29, 28, 23, 22, 20, 21};
36 /* This is an array of the bit positions of registers r1 .. r15 in
37 that order in a push/pop instruction. */
38 int type3_regs[15] = { 2, 1, 0, 27, 26, 25, 24, 31, 30, 29, 28, 23, 22, 20, 21};
40 #if WITH_TRACE_ANY_P
41 #ifndef SIZE_INSTRUCTION
42 #define SIZE_INSTRUCTION 18
43 #endif
45 #ifndef SIZE_VALUES
46 #define SIZE_VALUES 11
47 #endif
49 /* TODO: This file largely assumes a single CPU. */
50 #define CPU STATE_CPU (sd, 0)
53 uint32_t trace_values[3];
54 int trace_num_values;
55 uint32_t trace_pc;
56 const char * trace_name;
57 int trace_module;
60 void
61 trace_input (char *name, enum op_types type, int size)
63 if (!TRACE_ALU_P (STATE_CPU (simulator, 0)))
64 return;
66 trace_pc = PC;
67 trace_name = name;
68 trace_module = TRACE_ALU_IDX;
70 switch (type)
72 default:
73 case OP_UNKNOWN:
74 case OP_NONE:
75 case OP_TRAP:
76 trace_num_values = 0;
77 break;
79 case OP_REG:
80 case OP_REG_REG_MOVE:
81 trace_values[0] = State.regs[OP[0]];
82 trace_num_values = 1;
83 break;
85 case OP_BIT_CHANGE:
86 case OP_REG_REG:
87 case OP_REG_REG_CMP:
88 trace_values[0] = State.regs[OP[1]];
89 trace_values[1] = State.regs[OP[0]];
90 trace_num_values = 2;
91 break;
93 case OP_IMM_REG:
94 case OP_IMM_REG_CMP:
95 trace_values[0] = SEXT5 (OP[0]);
96 trace_values[1] = OP[1];
97 trace_num_values = 2;
98 break;
100 case OP_IMM_REG_MOVE:
101 trace_values[0] = SEXT5 (OP[0]);
102 trace_num_values = 1;
103 break;
105 case OP_COND_BR:
106 trace_values[0] = State.pc;
107 trace_values[1] = SEXT9 (OP[0]);
108 trace_values[2] = PSW;
109 trace_num_values = 3;
110 break;
112 case OP_LOAD16:
113 trace_values[0] = OP[1] * size;
114 trace_values[1] = State.regs[30];
115 trace_num_values = 2;
116 break;
118 case OP_STORE16:
119 trace_values[0] = State.regs[OP[0]];
120 trace_values[1] = OP[1] * size;
121 trace_values[2] = State.regs[30];
122 trace_num_values = 3;
123 break;
125 case OP_LOAD32:
126 trace_values[0] = EXTEND16 (OP[2]);
127 trace_values[1] = State.regs[OP[0]];
128 trace_num_values = 2;
129 break;
131 case OP_STORE32:
132 trace_values[0] = State.regs[OP[1]];
133 trace_values[1] = EXTEND16 (OP[2]);
134 trace_values[2] = State.regs[OP[0]];
135 trace_num_values = 3;
136 break;
138 case OP_JUMP:
139 trace_values[0] = SEXT22 (OP[0]);
140 trace_values[1] = State.pc;
141 trace_num_values = 2;
142 break;
144 case OP_IMM_REG_REG:
145 trace_values[0] = EXTEND16 (OP[0]) << size;
146 trace_values[1] = State.regs[OP[1]];
147 trace_num_values = 2;
148 break;
150 case OP_IMM16_REG_REG:
151 trace_values[0] = EXTEND16 (OP[2]) << size;
152 trace_values[1] = State.regs[OP[1]];
153 trace_num_values = 2;
154 break;
156 case OP_UIMM_REG_REG:
157 trace_values[0] = (OP[0] & 0xffff) << size;
158 trace_values[1] = State.regs[OP[1]];
159 trace_num_values = 2;
160 break;
162 case OP_UIMM16_REG_REG:
163 trace_values[0] = (OP[2]) << size;
164 trace_values[1] = State.regs[OP[1]];
165 trace_num_values = 2;
166 break;
168 case OP_BIT:
169 trace_num_values = 0;
170 break;
172 case OP_EX1:
173 trace_values[0] = PSW;
174 trace_num_values = 1;
175 break;
177 case OP_EX2:
178 trace_num_values = 0;
179 break;
181 case OP_LDSR:
182 trace_values[0] = State.regs[OP[0]];
183 trace_num_values = 1;
184 break;
186 case OP_STSR:
187 trace_values[0] = State.sregs[OP[1]];
188 trace_num_values = 1;
193 void
194 trace_result (int has_result, uint32_t result)
196 char buf[1000];
197 char *chp;
199 buf[0] = '\0';
200 chp = buf;
202 /* write out the values saved during the trace_input call */
204 int i;
205 for (i = 0; i < trace_num_values; i++)
207 sprintf (chp, "%*s0x%.8lx", SIZE_VALUES - 10, "",
208 (long) trace_values[i]);
209 chp = strchr (chp, '\0');
211 while (i++ < 3)
213 sprintf (chp, "%*s", SIZE_VALUES, "");
214 chp = strchr (chp, '\0');
218 /* append any result to the end of the buffer */
219 if (has_result)
220 sprintf (chp, " :: 0x%.8lx", (unsigned long) result);
222 trace_generic (simulator, STATE_CPU (simulator, 0), trace_module, "%s", buf);
225 void
226 trace_output (enum op_types result)
228 if (!TRACE_ALU_P (STATE_CPU (simulator, 0)))
229 return;
231 switch (result)
233 default:
234 case OP_UNKNOWN:
235 case OP_NONE:
236 case OP_TRAP:
237 case OP_REG:
238 case OP_REG_REG_CMP:
239 case OP_IMM_REG_CMP:
240 case OP_COND_BR:
241 case OP_STORE16:
242 case OP_STORE32:
243 case OP_BIT:
244 case OP_EX2:
245 trace_result (0, 0);
246 break;
248 case OP_LOAD16:
249 case OP_STSR:
250 trace_result (1, State.regs[OP[0]]);
251 break;
253 case OP_REG_REG:
254 case OP_REG_REG_MOVE:
255 case OP_IMM_REG:
256 case OP_IMM_REG_MOVE:
257 case OP_LOAD32:
258 case OP_EX1:
259 trace_result (1, State.regs[OP[1]]);
260 break;
262 case OP_IMM_REG_REG:
263 case OP_UIMM_REG_REG:
264 case OP_IMM16_REG_REG:
265 case OP_UIMM16_REG_REG:
266 trace_result (1, State.regs[OP[1]]);
267 break;
269 case OP_JUMP:
270 if (OP[1] != 0)
271 trace_result (1, State.regs[OP[1]]);
272 else
273 trace_result (0, 0);
274 break;
276 case OP_LDSR:
277 trace_result (1, State.sregs[OP[1]]);
278 break;
281 #endif
284 /* Returns 1 if the specific condition is met, returns 0 otherwise. */
286 condition_met (unsigned code)
288 unsigned int psw = PSW;
290 switch (code & 0xf)
292 case 0x0: return ((psw & PSW_OV) != 0);
293 case 0x1: return ((psw & PSW_CY) != 0);
294 case 0x2: return ((psw & PSW_Z) != 0);
295 case 0x3: return ((((psw & PSW_CY) != 0) | ((psw & PSW_Z) != 0)) != 0);
296 case 0x4: return ((psw & PSW_S) != 0);
297 /*case 0x5: return 1;*/
298 case 0x6: return ((((psw & PSW_S) != 0) ^ ((psw & PSW_OV) != 0)) != 0);
299 case 0x7: return (((((psw & PSW_S) != 0) ^ ((psw & PSW_OV) != 0)) || ((psw & PSW_Z) != 0)) != 0);
300 case 0x8: return ((psw & PSW_OV) == 0);
301 case 0x9: return ((psw & PSW_CY) == 0);
302 case 0xa: return ((psw & PSW_Z) == 0);
303 case 0xb: return ((((psw & PSW_CY) != 0) | ((psw & PSW_Z) != 0)) == 0);
304 case 0xc: return ((psw & PSW_S) == 0);
305 case 0xd: return ((psw & PSW_SAT) != 0);
306 case 0xe: return ((((psw & PSW_S) != 0) ^ ((psw & PSW_OV) != 0)) == 0);
307 case 0xf: return (((((psw & PSW_S) != 0) ^ ((psw & PSW_OV) != 0)) || ((psw & PSW_Z) != 0)) == 0);
310 return 1;
313 unsigned long
314 Add32 (unsigned long a1, unsigned long a2, int * carry)
316 unsigned long result = (a1 + a2);
318 * carry = (result < a1);
320 return result;
323 static void
324 Multiply64 (int sign, unsigned long op0)
326 unsigned long op1;
327 unsigned long lo;
328 unsigned long mid1;
329 unsigned long mid2;
330 unsigned long hi;
331 unsigned long RdLo;
332 unsigned long RdHi;
333 int carry;
335 op1 = State.regs[ OP[1] ];
337 if (sign)
339 /* Compute sign of result and adjust operands if necessary. */
341 sign = (op0 ^ op1) & 0x80000000;
343 if (op0 & 0x80000000)
344 op0 = - op0;
346 if (op1 & 0x80000000)
347 op1 = - op1;
350 /* We can split the 32x32 into four 16x16 operations. This ensures
351 that we do not lose precision on 32bit only hosts: */
352 lo = ( (op0 & 0xFFFF) * (op1 & 0xFFFF));
353 mid1 = ( (op0 & 0xFFFF) * ((op1 >> 16) & 0xFFFF));
354 mid2 = (((op0 >> 16) & 0xFFFF) * (op1 & 0xFFFF));
355 hi = (((op0 >> 16) & 0xFFFF) * ((op1 >> 16) & 0xFFFF));
357 /* We now need to add all of these results together, taking care
358 to propogate the carries from the additions: */
359 RdLo = Add32 (lo, (mid1 << 16), & carry);
360 RdHi = carry;
361 RdLo = Add32 (RdLo, (mid2 << 16), & carry);
362 RdHi += (carry + ((mid1 >> 16) & 0xFFFF) + ((mid2 >> 16) & 0xFFFF) + hi);
364 if (sign)
366 /* Negate result if necessary. */
368 RdLo = ~ RdLo;
369 RdHi = ~ RdHi;
370 if (RdLo == 0xFFFFFFFF)
372 RdLo = 0;
373 RdHi += 1;
375 else
376 RdLo += 1;
379 /* Don't store into register 0. */
380 if (OP[1])
381 State.regs[ OP[1] ] = RdLo;
382 if (OP[2] >> 11)
383 State.regs[ OP[2] >> 11 ] = RdHi;
385 return;
389 /* Read a null terminated string from memory, return in a buffer. */
391 static char *
392 fetch_str (SIM_DESC sd, address_word addr)
394 char *buf;
395 int nr = 0;
397 while (sim_core_read_1 (STATE_CPU (sd, 0),
398 PC, read_map, addr + nr) != 0)
399 nr++;
401 buf = NZALLOC (char, nr + 1);
402 sim_read (simulator, addr, buf, nr);
404 return buf;
407 /* Read a null terminated argument vector from memory, return in a
408 buffer. */
410 static char **
411 fetch_argv (SIM_DESC sd, address_word addr)
413 int max_nr = 64;
414 int nr = 0;
415 char **buf = xmalloc (max_nr * sizeof (char*));
417 while (1)
419 uint32_t a = sim_core_read_4 (STATE_CPU (sd, 0),
420 PC, read_map, addr + nr * 4);
421 if (a == 0) break;
422 buf[nr] = fetch_str (sd, a);
423 nr ++;
424 if (nr == max_nr - 1)
426 max_nr += 50;
427 buf = xrealloc (buf, max_nr * sizeof (char*));
430 buf[nr] = 0;
431 return buf;
435 /* sst.b */
437 OP_380 (void)
439 trace_input ("sst.b", OP_STORE16, 1);
441 store_mem (State.regs[30] + (OP[3] & 0x7f), 1, State.regs[ OP[1] ]);
443 trace_output (OP_STORE16);
445 return 2;
448 /* sst.h */
450 OP_480 (void)
452 trace_input ("sst.h", OP_STORE16, 2);
454 store_mem (State.regs[30] + ((OP[3] & 0x7f) << 1), 2, State.regs[ OP[1] ]);
456 trace_output (OP_STORE16);
458 return 2;
461 /* sst.w */
463 OP_501 (void)
465 trace_input ("sst.w", OP_STORE16, 4);
467 store_mem (State.regs[30] + ((OP[3] & 0x7e) << 1), 4, State.regs[ OP[1] ]);
469 trace_output (OP_STORE16);
471 return 2;
474 /* ld.b */
476 OP_700 (void)
478 int adr;
480 trace_input ("ld.b", OP_LOAD32, 1);
482 adr = State.regs[ OP[0] ] + EXTEND16 (OP[2]);
484 State.regs[ OP[1] ] = EXTEND8 (load_mem (adr, 1));
486 trace_output (OP_LOAD32);
488 return 4;
491 /* ld.h */
493 OP_720 (void)
495 int adr;
497 trace_input ("ld.h", OP_LOAD32, 2);
499 adr = State.regs[ OP[0] ] + EXTEND16 (OP[2]);
500 adr &= ~0x1;
502 State.regs[ OP[1] ] = EXTEND16 (load_mem (adr, 2));
504 trace_output (OP_LOAD32);
506 return 4;
509 /* ld.w */
511 OP_10720 (void)
513 int adr;
515 trace_input ("ld.w", OP_LOAD32, 4);
517 adr = State.regs[ OP[0] ] + EXTEND16 (OP[2] & ~1);
518 adr &= ~0x3;
520 State.regs[ OP[1] ] = load_mem (adr, 4);
522 trace_output (OP_LOAD32);
524 return 4;
527 /* st.b */
529 OP_740 (void)
531 trace_input ("st.b", OP_STORE32, 1);
533 store_mem (State.regs[ OP[0] ] + EXTEND16 (OP[2]), 1, State.regs[ OP[1] ]);
535 trace_output (OP_STORE32);
537 return 4;
540 /* st.h */
542 OP_760 (void)
544 int adr;
546 trace_input ("st.h", OP_STORE32, 2);
548 adr = State.regs[ OP[0] ] + EXTEND16 (OP[2]);
549 adr &= ~1;
551 store_mem (adr, 2, State.regs[ OP[1] ]);
553 trace_output (OP_STORE32);
555 return 4;
558 /* st.w */
560 OP_10760 (void)
562 int adr;
564 trace_input ("st.w", OP_STORE32, 4);
566 adr = State.regs[ OP[0] ] + EXTEND16 (OP[2] & ~1);
567 adr &= ~3;
569 store_mem (adr, 4, State.regs[ OP[1] ]);
571 trace_output (OP_STORE32);
573 return 4;
576 /* add reg, reg */
578 OP_1C0 (void)
580 unsigned int op0, op1, result, z, s, cy, ov;
582 trace_input ("add", OP_REG_REG, 0);
584 /* Compute the result. */
586 op0 = State.regs[ OP[0] ];
587 op1 = State.regs[ OP[1] ];
589 result = op0 + op1;
591 /* Compute the condition codes. */
592 z = (result == 0);
593 s = (result & 0x80000000);
594 cy = (result < op0 || result < op1);
595 ov = ((op0 & 0x80000000) == (op1 & 0x80000000)
596 && (op0 & 0x80000000) != (result & 0x80000000));
598 /* Store the result and condition codes. */
599 State.regs[OP[1]] = result;
600 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
601 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
602 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0));
603 trace_output (OP_REG_REG);
605 return 2;
608 /* add sign_extend(imm5), reg */
610 OP_240 (void)
612 unsigned int op0, op1, result, z, s, cy, ov;
613 int temp;
615 trace_input ("add", OP_IMM_REG, 0);
617 /* Compute the result. */
618 temp = SEXT5 (OP[0]);
619 op0 = temp;
620 op1 = State.regs[OP[1]];
621 result = op0 + op1;
623 /* Compute the condition codes. */
624 z = (result == 0);
625 s = (result & 0x80000000);
626 cy = (result < op0 || result < op1);
627 ov = ((op0 & 0x80000000) == (op1 & 0x80000000)
628 && (op0 & 0x80000000) != (result & 0x80000000));
630 /* Store the result and condition codes. */
631 State.regs[OP[1]] = result;
632 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
633 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
634 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0));
635 trace_output (OP_IMM_REG);
637 return 2;
640 /* addi sign_extend(imm16), reg, reg */
642 OP_600 (void)
644 unsigned int op0, op1, result, z, s, cy, ov;
646 trace_input ("addi", OP_IMM16_REG_REG, 0);
648 /* Compute the result. */
650 op0 = EXTEND16 (OP[2]);
651 op1 = State.regs[ OP[0] ];
652 result = op0 + op1;
654 /* Compute the condition codes. */
655 z = (result == 0);
656 s = (result & 0x80000000);
657 cy = (result < op0 || result < op1);
658 ov = ((op0 & 0x80000000) == (op1 & 0x80000000)
659 && (op0 & 0x80000000) != (result & 0x80000000));
661 /* Store the result and condition codes. */
662 State.regs[OP[1]] = result;
663 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
664 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
665 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0));
666 trace_output (OP_IMM16_REG_REG);
668 return 4;
671 /* sub reg1, reg2 */
673 OP_1A0 (void)
675 unsigned int op0, op1, result, z, s, cy, ov;
677 trace_input ("sub", OP_REG_REG, 0);
678 /* Compute the result. */
679 op0 = State.regs[ OP[0] ];
680 op1 = State.regs[ OP[1] ];
681 result = op1 - op0;
683 /* Compute the condition codes. */
684 z = (result == 0);
685 s = (result & 0x80000000);
686 cy = (op1 < op0);
687 ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
688 && (op1 & 0x80000000) != (result & 0x80000000));
690 /* Store the result and condition codes. */
691 State.regs[OP[1]] = result;
692 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
693 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
694 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0));
695 trace_output (OP_REG_REG);
697 return 2;
700 /* subr reg1, reg2 */
702 OP_180 (void)
704 unsigned int op0, op1, result, z, s, cy, ov;
706 trace_input ("subr", OP_REG_REG, 0);
707 /* Compute the result. */
708 op0 = State.regs[ OP[0] ];
709 op1 = State.regs[ OP[1] ];
710 result = op0 - op1;
712 /* Compute the condition codes. */
713 z = (result == 0);
714 s = (result & 0x80000000);
715 cy = (op0 < op1);
716 ov = ((op0 & 0x80000000) != (op1 & 0x80000000)
717 && (op0 & 0x80000000) != (result & 0x80000000));
719 /* Store the result and condition codes. */
720 State.regs[OP[1]] = result;
721 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
722 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
723 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0));
724 trace_output (OP_REG_REG);
726 return 2;
729 /* sxh reg1 */
731 OP_E0 (void)
733 trace_input ("mulh", OP_REG_REG, 0);
735 State.regs[ OP[1] ] = (EXTEND16 (State.regs[ OP[1] ]) * EXTEND16 (State.regs[ OP[0] ]));
737 trace_output (OP_REG_REG);
739 return 2;
742 /* mulh sign_extend(imm5), reg2 */
744 OP_2E0 (void)
746 trace_input ("mulh", OP_IMM_REG, 0);
748 State.regs[ OP[1] ] = EXTEND16 (State.regs[ OP[1] ]) * SEXT5 (OP[0]);
750 trace_output (OP_IMM_REG);
752 return 2;
755 /* mulhi imm16, reg1, reg2 */
757 OP_6E0 (void)
759 trace_input ("mulhi", OP_IMM16_REG_REG, 0);
761 State.regs[ OP[1] ] = EXTEND16 (State.regs[ OP[0] ]) * EXTEND16 (OP[2]);
763 trace_output (OP_IMM16_REG_REG);
765 return 4;
768 /* cmp reg, reg */
770 OP_1E0 (void)
772 unsigned int op0, op1, result, z, s, cy, ov;
774 trace_input ("cmp", OP_REG_REG_CMP, 0);
775 /* Compute the result. */
776 op0 = State.regs[ OP[0] ];
777 op1 = State.regs[ OP[1] ];
778 result = op1 - op0;
780 /* Compute the condition codes. */
781 z = (result == 0);
782 s = (result & 0x80000000);
783 cy = (op1 < op0);
784 ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
785 && (op1 & 0x80000000) != (result & 0x80000000));
787 /* Set condition codes. */
788 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
789 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
790 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0));
791 trace_output (OP_REG_REG_CMP);
793 return 2;
796 /* cmp sign_extend(imm5), reg */
798 OP_260 (void)
800 unsigned int op0, op1, result, z, s, cy, ov;
801 int temp;
803 /* Compute the result. */
804 trace_input ("cmp", OP_IMM_REG_CMP, 0);
805 temp = SEXT5 (OP[0]);
806 op0 = temp;
807 op1 = State.regs[OP[1]];
808 result = op1 - op0;
810 /* Compute the condition codes. */
811 z = (result == 0);
812 s = (result & 0x80000000);
813 cy = (op1 < op0);
814 ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
815 && (op1 & 0x80000000) != (result & 0x80000000));
817 /* Set condition codes. */
818 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
819 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
820 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0));
821 trace_output (OP_IMM_REG_CMP);
823 return 2;
826 /* setf cccc,reg2 */
828 OP_7E0 (void)
830 trace_input ("setf", OP_EX1, 0);
832 State.regs[ OP[1] ] = condition_met (OP[0]);
834 trace_output (OP_EX1);
836 return 4;
839 /* satadd reg,reg */
841 OP_C0 (void)
843 unsigned int op0, op1, result, z, s, cy, ov, sat;
845 trace_input ("satadd", OP_REG_REG, 0);
846 /* Compute the result. */
847 op0 = State.regs[ OP[0] ];
848 op1 = State.regs[ OP[1] ];
849 result = op0 + op1;
851 /* Compute the condition codes. */
852 z = (result == 0);
853 s = (result & 0x80000000);
854 cy = (result < op0 || result < op1);
855 ov = ((op0 & 0x80000000) == (op1 & 0x80000000)
856 && (op0 & 0x80000000) != (result & 0x80000000));
857 sat = ov;
859 /* Handle saturated results. */
860 if (sat && s)
862 /* An overflow that results in a negative result implies that we
863 became too positive. */
864 result = 0x7fffffff;
865 s = 0;
867 else if (sat)
869 /* Any other overflow must have thus been too negative. */
870 result = 0x80000000;
871 s = 1;
872 z = 0;
875 /* Store the result and condition codes. */
876 State.regs[OP[1]] = result;
877 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
878 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
879 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0)
880 | (sat ? PSW_SAT : 0));
882 trace_output (OP_REG_REG);
884 return 2;
887 /* satadd sign_extend(imm5), reg */
889 OP_220 (void)
891 unsigned int op0, op1, result, z, s, cy, ov, sat;
893 int temp;
895 trace_input ("satadd", OP_IMM_REG, 0);
897 /* Compute the result. */
898 temp = SEXT5 (OP[0]);
899 op0 = temp;
900 op1 = State.regs[OP[1]];
901 result = op0 + op1;
903 /* Compute the condition codes. */
904 z = (result == 0);
905 s = (result & 0x80000000);
906 cy = (result < op0 || result < op1);
907 ov = ((op0 & 0x80000000) == (op1 & 0x80000000)
908 && (op0 & 0x80000000) != (result & 0x80000000));
909 sat = ov;
911 /* Handle saturated results. */
912 if (sat && s)
914 /* An overflow that results in a negative result implies that we
915 became too positive. */
916 result = 0x7fffffff;
917 s = 0;
919 else if (sat)
921 /* Any other overflow must have thus been too negative. */
922 result = 0x80000000;
923 s = 1;
924 z = 0;
927 /* Store the result and condition codes. */
928 State.regs[OP[1]] = result;
929 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
930 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
931 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0)
932 | (sat ? PSW_SAT : 0));
933 trace_output (OP_IMM_REG);
935 return 2;
938 /* satsub reg1, reg2 */
940 OP_A0 (void)
942 unsigned int op0, op1, result, z, s, cy, ov, sat;
944 trace_input ("satsub", OP_REG_REG, 0);
946 /* Compute the result. */
947 op0 = State.regs[ OP[0] ];
948 op1 = State.regs[ OP[1] ];
949 result = op1 - op0;
951 /* Compute the condition codes. */
952 z = (result == 0);
953 s = (result & 0x80000000);
954 cy = (op1 < op0);
955 ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
956 && (op1 & 0x80000000) != (result & 0x80000000));
957 sat = ov;
959 /* Handle saturated results. */
960 if (sat && s)
962 /* An overflow that results in a negative result implies that we
963 became too positive. */
964 result = 0x7fffffff;
965 s = 0;
967 else if (sat)
969 /* Any other overflow must have thus been too negative. */
970 result = 0x80000000;
971 s = 1;
972 z = 0;
975 /* Store the result and condition codes. */
976 State.regs[OP[1]] = result;
977 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
978 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
979 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0)
980 | (sat ? PSW_SAT : 0));
982 trace_output (OP_REG_REG);
983 return 2;
986 /* satsubi sign_extend(imm16), reg */
988 OP_660 (void)
990 unsigned int op0, op1, result, z, s, cy, ov, sat;
991 int temp;
993 trace_input ("satsubi", OP_IMM_REG, 0);
995 /* Compute the result. */
996 temp = EXTEND16 (OP[2]);
997 op0 = temp;
998 op1 = State.regs[ OP[0] ];
999 result = op1 - op0;
1001 /* Compute the condition codes. */
1002 z = (result == 0);
1003 s = (result & 0x80000000);
1004 cy = (op1 < op0);
1005 ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
1006 && (op1 & 0x80000000) != (result & 0x80000000));
1007 sat = ov;
1009 /* Handle saturated results. */
1010 if (sat && s)
1012 /* An overflow that results in a negative result implies that we
1013 became too positive. */
1014 result = 0x7fffffff;
1015 s = 0;
1017 else if (sat)
1019 /* Any other overflow must have thus been too negative. */
1020 result = 0x80000000;
1021 s = 1;
1022 z = 0;
1025 /* Store the result and condition codes. */
1026 State.regs[OP[1]] = result;
1027 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
1028 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1029 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0)
1030 | (sat ? PSW_SAT : 0));
1032 trace_output (OP_IMM_REG);
1034 return 4;
1037 /* satsubr reg,reg */
1039 OP_80 (void)
1041 unsigned int op0, op1, result, z, s, cy, ov, sat;
1043 trace_input ("satsubr", OP_REG_REG, 0);
1045 /* Compute the result. */
1046 op0 = State.regs[ OP[0] ];
1047 op1 = State.regs[ OP[1] ];
1048 result = op0 - op1;
1050 /* Compute the condition codes. */
1051 z = (result == 0);
1052 s = (result & 0x80000000);
1053 cy = (op0 < op1);
1054 ov = ((op0 & 0x80000000) != (op1 & 0x80000000)
1055 && (op0 & 0x80000000) != (result & 0x80000000));
1056 sat = ov;
1058 /* Handle saturated results. */
1059 if (sat && s)
1061 /* An overflow that results in a negative result implies that we
1062 became too positive. */
1063 result = 0x7fffffff;
1064 s = 0;
1066 else if (sat)
1068 /* Any other overflow must have thus been too negative. */
1069 result = 0x80000000;
1070 s = 1;
1071 z = 0;
1074 /* Store the result and condition codes. */
1075 State.regs[OP[1]] = result;
1076 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
1077 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1078 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0)
1079 | (sat ? PSW_SAT : 0));
1081 trace_output (OP_REG_REG);
1083 return 2;
1086 /* tst reg,reg */
1088 OP_160 (void)
1090 unsigned int op0, op1, result, z, s;
1092 trace_input ("tst", OP_REG_REG_CMP, 0);
1094 /* Compute the result. */
1095 op0 = State.regs[ OP[0] ];
1096 op1 = State.regs[ OP[1] ];
1097 result = op0 & op1;
1099 /* Compute the condition codes. */
1100 z = (result == 0);
1101 s = (result & 0x80000000);
1103 /* Store the condition codes. */
1104 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1105 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0));
1106 trace_output (OP_REG_REG_CMP);
1108 return 2;
1111 /* mov sign_extend(imm5), reg */
1113 OP_200 (void)
1115 int value = SEXT5 (OP[0]);
1117 trace_input ("mov", OP_IMM_REG_MOVE, 0);
1119 State.regs[ OP[1] ] = value;
1121 trace_output (OP_IMM_REG_MOVE);
1123 return 2;
1126 /* movhi imm16, reg, reg */
1128 OP_640 (void)
1130 trace_input ("movhi", OP_UIMM16_REG_REG, 16);
1132 State.regs[ OP[1] ] = State.regs[ OP[0] ] + (OP[2] << 16);
1134 trace_output (OP_UIMM16_REG_REG);
1136 return 4;
1139 /* sar zero_extend(imm5),reg1 */
1141 OP_2A0 (void)
1143 unsigned int op0, op1, result, z, s, cy;
1145 trace_input ("sar", OP_IMM_REG, 0);
1146 op0 = OP[0];
1147 op1 = State.regs[ OP[1] ];
1148 result = (signed)op1 >> op0;
1150 /* Compute the condition codes. */
1151 z = (result == 0);
1152 s = (result & 0x80000000);
1153 cy = op0 ? (op1 & (1 << (op0 - 1))) : 0;
1155 /* Store the result and condition codes. */
1156 State.regs[ OP[1] ] = result;
1157 PSW &= ~(PSW_Z | PSW_S | PSW_OV | PSW_CY);
1158 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1159 | (cy ? PSW_CY : 0));
1160 trace_output (OP_IMM_REG);
1162 return 2;
1165 /* sar reg1, reg2 */
1167 OP_A007E0 (void)
1169 unsigned int op0, op1, result, z, s, cy;
1171 trace_input ("sar", OP_REG_REG, 0);
1173 op0 = State.regs[ OP[0] ] & 0x1f;
1174 op1 = State.regs[ OP[1] ];
1175 result = (signed)op1 >> op0;
1177 /* Compute the condition codes. */
1178 z = (result == 0);
1179 s = (result & 0x80000000);
1180 cy = op0 ? (op1 & (1 << (op0 - 1))) : 0;
1182 /* Store the result and condition codes. */
1183 State.regs[OP[1]] = result;
1184 PSW &= ~(PSW_Z | PSW_S | PSW_OV | PSW_CY);
1185 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1186 | (cy ? PSW_CY : 0));
1187 trace_output (OP_REG_REG);
1189 return 4;
1192 /* shl zero_extend(imm5),reg1 */
1194 OP_2C0 (void)
1196 unsigned int op0, op1, result, z, s, cy;
1198 trace_input ("shl", OP_IMM_REG, 0);
1199 op0 = OP[0];
1200 op1 = State.regs[ OP[1] ];
1201 result = op1 << op0;
1203 /* Compute the condition codes. */
1204 z = (result == 0);
1205 s = (result & 0x80000000);
1206 cy = op0 ? (op1 & (1 << (32 - op0))) : 0;
1208 /* Store the result and condition codes. */
1209 State.regs[OP[1]] = result;
1210 PSW &= ~(PSW_Z | PSW_S | PSW_OV | PSW_CY);
1211 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1212 | (cy ? PSW_CY : 0));
1213 trace_output (OP_IMM_REG);
1215 return 2;
1218 /* shl reg1, reg2 */
1220 OP_C007E0 (void)
1222 unsigned int op0, op1, result, z, s, cy;
1224 trace_input ("shl", OP_REG_REG, 0);
1225 op0 = State.regs[ OP[0] ] & 0x1f;
1226 op1 = State.regs[ OP[1] ];
1227 result = op1 << op0;
1229 /* Compute the condition codes. */
1230 z = (result == 0);
1231 s = (result & 0x80000000);
1232 cy = op0 ? (op1 & (1 << (32 - op0))) : 0;
1234 /* Store the result and condition codes. */
1235 State.regs[OP[1]] = result;
1236 PSW &= ~(PSW_Z | PSW_S | PSW_OV | PSW_CY);
1237 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1238 | (cy ? PSW_CY : 0));
1239 trace_output (OP_REG_REG);
1241 return 4;
1244 /* shr zero_extend(imm5),reg1 */
1246 OP_280 (void)
1248 unsigned int op0, op1, result, z, s, cy;
1250 trace_input ("shr", OP_IMM_REG, 0);
1251 op0 = OP[0];
1252 op1 = State.regs[ OP[1] ];
1253 result = op1 >> op0;
1255 /* Compute the condition codes. */
1256 z = (result == 0);
1257 s = (result & 0x80000000);
1258 cy = op0 ? (op1 & (1 << (op0 - 1))) : 0;
1260 /* Store the result and condition codes. */
1261 State.regs[OP[1]] = result;
1262 PSW &= ~(PSW_Z | PSW_S | PSW_OV | PSW_CY);
1263 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1264 | (cy ? PSW_CY : 0));
1265 trace_output (OP_IMM_REG);
1267 return 2;
1270 /* shr reg1, reg2 */
1272 OP_8007E0 (void)
1274 unsigned int op0, op1, result, z, s, cy;
1276 trace_input ("shr", OP_REG_REG, 0);
1277 op0 = State.regs[ OP[0] ] & 0x1f;
1278 op1 = State.regs[ OP[1] ];
1279 result = op1 >> op0;
1281 /* Compute the condition codes. */
1282 z = (result == 0);
1283 s = (result & 0x80000000);
1284 cy = op0 ? (op1 & (1 << (op0 - 1))) : 0;
1286 /* Store the result and condition codes. */
1287 State.regs[OP[1]] = result;
1288 PSW &= ~(PSW_Z | PSW_S | PSW_OV | PSW_CY);
1289 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1290 | (cy ? PSW_CY : 0));
1291 trace_output (OP_REG_REG);
1293 return 4;
1296 /* or reg, reg */
1298 OP_100 (void)
1300 unsigned int op0, op1, result, z, s;
1302 trace_input ("or", OP_REG_REG, 0);
1304 /* Compute the result. */
1305 op0 = State.regs[ OP[0] ];
1306 op1 = State.regs[ OP[1] ];
1307 result = op0 | op1;
1309 /* Compute the condition codes. */
1310 z = (result == 0);
1311 s = (result & 0x80000000);
1313 /* Store the result and condition codes. */
1314 State.regs[OP[1]] = result;
1315 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1316 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0));
1317 trace_output (OP_REG_REG);
1319 return 2;
1322 /* ori zero_extend(imm16), reg, reg */
1324 OP_680 (void)
1326 unsigned int op0, op1, result, z, s;
1328 trace_input ("ori", OP_UIMM16_REG_REG, 0);
1329 op0 = OP[2];
1330 op1 = State.regs[ OP[0] ];
1331 result = op0 | op1;
1333 /* Compute the condition codes. */
1334 z = (result == 0);
1335 s = (result & 0x80000000);
1337 /* Store the result and condition codes. */
1338 State.regs[OP[1]] = result;
1339 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1340 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0));
1341 trace_output (OP_UIMM16_REG_REG);
1343 return 4;
1346 /* and reg, reg */
1348 OP_140 (void)
1350 unsigned int op0, op1, result, z, s;
1352 trace_input ("and", OP_REG_REG, 0);
1354 /* Compute the result. */
1355 op0 = State.regs[ OP[0] ];
1356 op1 = State.regs[ OP[1] ];
1357 result = op0 & op1;
1359 /* Compute the condition codes. */
1360 z = (result == 0);
1361 s = (result & 0x80000000);
1363 /* Store the result and condition codes. */
1364 State.regs[OP[1]] = result;
1365 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1366 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0));
1367 trace_output (OP_REG_REG);
1369 return 2;
1372 /* andi zero_extend(imm16), reg, reg */
1374 OP_6C0 (void)
1376 unsigned int result, z;
1378 trace_input ("andi", OP_UIMM16_REG_REG, 0);
1380 result = OP[2] & State.regs[ OP[0] ];
1382 /* Compute the condition codes. */
1383 z = (result == 0);
1385 /* Store the result and condition codes. */
1386 State.regs[ OP[1] ] = result;
1388 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1389 PSW |= (z ? PSW_Z : 0);
1391 trace_output (OP_UIMM16_REG_REG);
1393 return 4;
1396 /* xor reg, reg */
1398 OP_120 (void)
1400 unsigned int op0, op1, result, z, s;
1402 trace_input ("xor", OP_REG_REG, 0);
1404 /* Compute the result. */
1405 op0 = State.regs[ OP[0] ];
1406 op1 = State.regs[ OP[1] ];
1407 result = op0 ^ op1;
1409 /* Compute the condition codes. */
1410 z = (result == 0);
1411 s = (result & 0x80000000);
1413 /* Store the result and condition codes. */
1414 State.regs[OP[1]] = result;
1415 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1416 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0));
1417 trace_output (OP_REG_REG);
1419 return 2;
1422 /* xori zero_extend(imm16), reg, reg */
1424 OP_6A0 (void)
1426 unsigned int op0, op1, result, z, s;
1428 trace_input ("xori", OP_UIMM16_REG_REG, 0);
1429 op0 = OP[2];
1430 op1 = State.regs[ OP[0] ];
1431 result = op0 ^ op1;
1433 /* Compute the condition codes. */
1434 z = (result == 0);
1435 s = (result & 0x80000000);
1437 /* Store the result and condition codes. */
1438 State.regs[OP[1]] = result;
1439 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1440 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0));
1441 trace_output (OP_UIMM16_REG_REG);
1443 return 4;
1446 /* not reg1, reg2 */
1448 OP_20 (void)
1450 unsigned int op0, result, z, s;
1452 trace_input ("not", OP_REG_REG_MOVE, 0);
1453 /* Compute the result. */
1454 op0 = State.regs[ OP[0] ];
1455 result = ~op0;
1457 /* Compute the condition codes. */
1458 z = (result == 0);
1459 s = (result & 0x80000000);
1461 /* Store the result and condition codes. */
1462 State.regs[OP[1]] = result;
1463 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1464 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0));
1465 trace_output (OP_REG_REG_MOVE);
1467 return 2;
1470 /* set1 */
1472 OP_7C0 (void)
1474 unsigned int op0, op1, op2;
1475 int temp;
1477 trace_input ("set1", OP_BIT, 0);
1478 op0 = State.regs[ OP[0] ];
1479 op1 = OP[1] & 0x7;
1480 temp = EXTEND16 (OP[2]);
1481 op2 = temp;
1482 temp = load_mem (op0 + op2, 1);
1483 PSW &= ~PSW_Z;
1484 if ((temp & (1 << op1)) == 0)
1485 PSW |= PSW_Z;
1486 temp |= (1 << op1);
1487 store_mem (op0 + op2, 1, temp);
1488 trace_output (OP_BIT);
1490 return 4;
1493 /* not1 */
1495 OP_47C0 (void)
1497 unsigned int op0, op1, op2;
1498 int temp;
1500 trace_input ("not1", OP_BIT, 0);
1501 op0 = State.regs[ OP[0] ];
1502 op1 = OP[1] & 0x7;
1503 temp = EXTEND16 (OP[2]);
1504 op2 = temp;
1505 temp = load_mem (op0 + op2, 1);
1506 PSW &= ~PSW_Z;
1507 if ((temp & (1 << op1)) == 0)
1508 PSW |= PSW_Z;
1509 temp ^= (1 << op1);
1510 store_mem (op0 + op2, 1, temp);
1511 trace_output (OP_BIT);
1513 return 4;
1516 /* clr1 */
1518 OP_87C0 (void)
1520 unsigned int op0, op1, op2;
1521 int temp;
1523 trace_input ("clr1", OP_BIT, 0);
1524 op0 = State.regs[ OP[0] ];
1525 op1 = OP[1] & 0x7;
1526 temp = EXTEND16 (OP[2]);
1527 op2 = temp;
1528 temp = load_mem (op0 + op2, 1);
1529 PSW &= ~PSW_Z;
1530 if ((temp & (1 << op1)) == 0)
1531 PSW |= PSW_Z;
1532 temp &= ~(1 << op1);
1533 store_mem (op0 + op2, 1, temp);
1534 trace_output (OP_BIT);
1536 return 4;
1539 /* tst1 */
1541 OP_C7C0 (void)
1543 unsigned int op0, op1, op2;
1544 int temp;
1546 trace_input ("tst1", OP_BIT, 0);
1547 op0 = State.regs[ OP[0] ];
1548 op1 = OP[1] & 0x7;
1549 temp = EXTEND16 (OP[2]);
1550 op2 = temp;
1551 temp = load_mem (op0 + op2, 1);
1552 PSW &= ~PSW_Z;
1553 if ((temp & (1 << op1)) == 0)
1554 PSW |= PSW_Z;
1555 trace_output (OP_BIT);
1557 return 4;
1560 /* di */
1562 OP_16007E0 (void)
1564 trace_input ("di", OP_NONE, 0);
1565 PSW |= PSW_ID;
1566 trace_output (OP_NONE);
1568 return 4;
1571 /* ei */
1573 OP_16087E0 (void)
1575 trace_input ("ei", OP_NONE, 0);
1576 PSW &= ~PSW_ID;
1577 trace_output (OP_NONE);
1579 return 4;
1582 /* halt */
1584 OP_12007E0 (void)
1586 trace_input ("halt", OP_NONE, 0);
1587 /* FIXME this should put processor into a mode where NMI still handled */
1588 trace_output (OP_NONE);
1589 sim_engine_halt (simulator, STATE_CPU (simulator, 0), NULL, PC,
1590 sim_stopped, SIM_SIGTRAP);
1591 return 0;
1594 /* trap */
1596 OP_10007E0 (void)
1598 trace_input ("trap", OP_TRAP, 0);
1599 trace_output (OP_TRAP);
1601 /* Trap 31 is used for simulating OS I/O functions */
1603 if (OP[0] == 31)
1605 int save_errno = errno;
1606 errno = 0;
1608 /* Registers passed to trap 0 */
1610 #define FUNC State.regs[6] /* function number, return value */
1611 #define PARM1 State.regs[7] /* optional parm 1 */
1612 #define PARM2 State.regs[8] /* optional parm 2 */
1613 #define PARM3 State.regs[9] /* optional parm 3 */
1615 /* Registers set by trap 0 */
1617 #define RETVAL State.regs[10] /* return value */
1618 #define RETERR State.regs[11] /* return error code */
1620 /* Turn a pointer in a register into a pointer into real memory. */
1622 #define MEMPTR(x) (map (x))
1624 RETERR = 0;
1626 switch (FUNC)
1629 #ifdef HAVE_FORK
1630 case TARGET_NEWLIB_V850_SYS_fork:
1631 RETVAL = fork ();
1632 RETERR = errno;
1633 break;
1634 #endif
1636 #ifdef HAVE_EXECVE
1637 case TARGET_NEWLIB_V850_SYS_execve:
1639 char *path = fetch_str (simulator, PARM1);
1640 char **argv = fetch_argv (simulator, PARM2);
1641 char **envp = fetch_argv (simulator, PARM3);
1642 RETVAL = execve (path, (void *)argv, (void *)envp);
1643 free (path);
1644 freeargv (argv);
1645 freeargv (envp);
1646 RETERR = errno;
1647 break;
1649 #endif
1651 #if HAVE_EXECV
1652 case TARGET_NEWLIB_V850_SYS_execv:
1654 char *path = fetch_str (simulator, PARM1);
1655 char **argv = fetch_argv (simulator, PARM2);
1656 RETVAL = execv (path, (void *)argv);
1657 free (path);
1658 freeargv (argv);
1659 RETERR = errno;
1660 break;
1662 #endif
1664 #if 0
1665 case TARGET_NEWLIB_V850_SYS_pipe:
1667 reg_t buf;
1668 int host_fd[2];
1670 buf = PARM1;
1671 RETVAL = pipe (host_fd);
1672 SW (buf, host_fd[0]);
1673 buf += sizeof (uint16_t);
1674 SW (buf, host_fd[1]);
1675 RETERR = errno;
1677 break;
1678 #endif
1680 #if 0
1681 case TARGET_NEWLIB_V850_SYS_wait:
1683 int status;
1685 RETVAL = wait (&status);
1686 SW (PARM1, status);
1687 RETERR = errno;
1689 break;
1690 #endif
1692 case TARGET_NEWLIB_V850_SYS_read:
1694 char *buf = zalloc (PARM3);
1695 RETVAL = sim_io_read (simulator, PARM1, buf, PARM3);
1696 sim_write (simulator, PARM2, buf, PARM3);
1697 free (buf);
1698 if ((int) RETVAL < 0)
1699 RETERR = sim_io_get_errno (simulator);
1700 break;
1703 case TARGET_NEWLIB_V850_SYS_write:
1705 char *buf = zalloc (PARM3);
1706 sim_read (simulator, PARM2, buf, PARM3);
1707 if (PARM1 == 1)
1708 RETVAL = sim_io_write_stdout (simulator, buf, PARM3);
1709 else
1710 RETVAL = sim_io_write (simulator, PARM1, buf, PARM3);
1711 free (buf);
1712 if ((int) RETVAL < 0)
1713 RETERR = sim_io_get_errno (simulator);
1714 break;
1717 case TARGET_NEWLIB_V850_SYS_lseek:
1718 RETVAL = sim_io_lseek (simulator, PARM1, PARM2, PARM3);
1719 if ((int) RETVAL < 0)
1720 RETERR = sim_io_get_errno (simulator);
1721 break;
1723 case TARGET_NEWLIB_V850_SYS_close:
1724 RETVAL = sim_io_close (simulator, PARM1);
1725 if ((int) RETVAL < 0)
1726 RETERR = sim_io_get_errno (simulator);
1727 break;
1729 case TARGET_NEWLIB_V850_SYS_open:
1731 char *buf = fetch_str (simulator, PARM1);
1732 RETVAL = sim_io_open (simulator, buf, PARM2);
1733 free (buf);
1734 if ((int) RETVAL < 0)
1735 RETERR = sim_io_get_errno (simulator);
1736 break;
1739 case TARGET_NEWLIB_V850_SYS_exit:
1740 if ((PARM1 & 0xffff0000) == 0xdead0000 && (PARM1 & 0xffff) != 0)
1741 /* get signal encoded by kill */
1742 sim_engine_halt (simulator, STATE_CPU (simulator, 0), NULL, PC,
1743 sim_signalled, PARM1 & 0xffff);
1744 else if (PARM1 == 0xdead)
1745 /* old libraries */
1746 sim_engine_halt (simulator, STATE_CPU (simulator, 0), NULL, PC,
1747 sim_stopped, SIM_SIGABRT);
1748 else
1749 /* PARM1 has exit status */
1750 sim_engine_halt (simulator, STATE_CPU (simulator, 0), NULL, PC,
1751 sim_exited, PARM1);
1752 break;
1754 case TARGET_NEWLIB_V850_SYS_stat: /* added at hmsi */
1755 /* stat system call */
1757 struct stat host_stat;
1758 reg_t buf;
1759 char *path = fetch_str (simulator, PARM1);
1761 RETVAL = sim_io_stat (simulator, path, &host_stat);
1763 free (path);
1764 buf = PARM2;
1766 /* Just wild-assed guesses. */
1767 store_mem (buf, 2, host_stat.st_dev);
1768 store_mem (buf + 2, 2, host_stat.st_ino);
1769 store_mem (buf + 4, 4, host_stat.st_mode);
1770 store_mem (buf + 8, 2, host_stat.st_nlink);
1771 store_mem (buf + 10, 2, host_stat.st_uid);
1772 store_mem (buf + 12, 2, host_stat.st_gid);
1773 store_mem (buf + 14, 2, host_stat.st_rdev);
1774 store_mem (buf + 16, 4, host_stat.st_size);
1775 store_mem (buf + 20, 4, host_stat.st_atime);
1776 store_mem (buf + 28, 4, host_stat.st_mtime);
1777 store_mem (buf + 36, 4, host_stat.st_ctime);
1779 if ((int) RETVAL < 0)
1780 RETERR = sim_io_get_errno (simulator);
1782 break;
1784 case TARGET_NEWLIB_V850_SYS_fstat:
1785 /* fstat system call */
1787 struct stat host_stat;
1788 reg_t buf;
1790 RETVAL = sim_io_fstat (simulator, PARM1, &host_stat);
1792 buf = PARM2;
1794 /* Just wild-assed guesses. */
1795 store_mem (buf, 2, host_stat.st_dev);
1796 store_mem (buf + 2, 2, host_stat.st_ino);
1797 store_mem (buf + 4, 4, host_stat.st_mode);
1798 store_mem (buf + 8, 2, host_stat.st_nlink);
1799 store_mem (buf + 10, 2, host_stat.st_uid);
1800 store_mem (buf + 12, 2, host_stat.st_gid);
1801 store_mem (buf + 14, 2, host_stat.st_rdev);
1802 store_mem (buf + 16, 4, host_stat.st_size);
1803 store_mem (buf + 20, 4, host_stat.st_atime);
1804 store_mem (buf + 28, 4, host_stat.st_mtime);
1805 store_mem (buf + 36, 4, host_stat.st_ctime);
1807 if ((int) RETVAL < 0)
1808 RETERR = sim_io_get_errno (simulator);
1810 break;
1812 case TARGET_NEWLIB_V850_SYS_rename:
1814 char *oldpath = fetch_str (simulator, PARM1);
1815 char *newpath = fetch_str (simulator, PARM2);
1816 RETVAL = sim_io_rename (simulator, oldpath, newpath);
1817 free (oldpath);
1818 free (newpath);
1819 if ((int) RETVAL < 0)
1820 RETERR = sim_io_get_errno (simulator);
1822 break;
1824 case TARGET_NEWLIB_V850_SYS_unlink:
1826 char *path = fetch_str (simulator, PARM1);
1827 RETVAL = sim_io_unlink (simulator, path);
1828 free (path);
1829 if ((int) RETVAL < 0)
1830 RETERR = sim_io_get_errno (simulator);
1832 break;
1834 case TARGET_NEWLIB_V850_SYS_chown:
1836 char *path = fetch_str (simulator, PARM1);
1837 RETVAL = chown (path, PARM2, PARM3);
1838 free (path);
1839 RETERR = errno;
1841 break;
1843 #if HAVE_CHMOD
1844 case TARGET_NEWLIB_V850_SYS_chmod:
1846 char *path = fetch_str (simulator, PARM1);
1847 RETVAL = chmod (path, PARM2);
1848 free (path);
1849 RETERR = errno;
1851 break;
1852 #endif
1854 #if HAVE_TIME
1855 case TARGET_NEWLIB_V850_SYS_time:
1857 time_t now;
1858 RETVAL = time (&now);
1859 store_mem (PARM1, 4, now);
1860 RETERR = errno;
1862 break;
1863 #endif
1865 #if !defined(__GO32__) && !defined(_WIN32)
1866 case TARGET_NEWLIB_V850_SYS_times:
1868 struct tms tms;
1869 RETVAL = times (&tms);
1870 store_mem (PARM1, 4, tms.tms_utime);
1871 store_mem (PARM1 + 4, 4, tms.tms_stime);
1872 store_mem (PARM1 + 8, 4, tms.tms_cutime);
1873 store_mem (PARM1 + 12, 4, tms.tms_cstime);
1874 RETERR = errno;
1875 break;
1877 #endif
1879 #if !defined(__GO32__) && !defined(_WIN32)
1880 case TARGET_NEWLIB_V850_SYS_gettimeofday:
1882 struct timeval t;
1883 struct timezone tz;
1884 RETVAL = gettimeofday (&t, &tz);
1885 store_mem (PARM1, 4, t.tv_sec);
1886 store_mem (PARM1 + 4, 4, t.tv_usec);
1887 store_mem (PARM2, 4, tz.tz_minuteswest);
1888 store_mem (PARM2 + 4, 4, tz.tz_dsttime);
1889 RETERR = errno;
1890 break;
1892 #endif
1894 #if HAVE_UTIME
1895 case TARGET_NEWLIB_V850_SYS_utime:
1897 /* Cast the second argument to void *, to avoid type mismatch
1898 if a prototype is present. */
1899 sim_io_error (simulator, "Utime not supported");
1900 /* RETVAL = utime (path, (void *) MEMPTR (PARM2)); */
1902 break;
1903 #endif
1905 default:
1906 abort ();
1908 errno = save_errno;
1910 return 4;
1912 else
1913 { /* Trap 0 -> 30 */
1914 EIPC = PC + 4;
1915 EIPSW = PSW;
1916 /* Mask out EICC */
1917 ECR &= 0xffff0000;
1918 ECR |= 0x40 + OP[0];
1919 /* Flag that we are now doing exception processing. */
1920 PSW |= PSW_EP | PSW_ID;
1921 PC = (OP[0] < 0x10) ? 0x40 : 0x50;
1923 return 0;
1927 /* tst1 reg2, [reg1] */
1929 OP_E607E0 (void)
1931 int temp;
1933 trace_input ("tst1", OP_BIT, 1);
1935 temp = load_mem (State.regs[ OP[0] ], 1);
1937 PSW &= ~PSW_Z;
1938 if ((temp & (1 << (State.regs[ OP[1] ] & 0x7))) == 0)
1939 PSW |= PSW_Z;
1941 trace_output (OP_BIT);
1943 return 4;
1946 /* mulu reg1, reg2, reg3 */
1948 OP_22207E0 (void)
1950 trace_input ("mulu", OP_REG_REG_REG, 0);
1952 Multiply64 (0, State.regs[ OP[0] ]);
1954 trace_output (OP_REG_REG_REG);
1956 return 4;
1959 #define BIT_CHANGE_OP( name, binop ) \
1960 unsigned int bit; \
1961 unsigned int temp; \
1963 trace_input (name, OP_BIT_CHANGE, 0); \
1965 bit = 1 << (State.regs[ OP[1] ] & 0x7); \
1966 temp = load_mem (State.regs[ OP[0] ], 1); \
1968 PSW &= ~PSW_Z; \
1969 if ((temp & bit) == 0) \
1970 PSW |= PSW_Z; \
1971 temp binop bit; \
1973 store_mem (State.regs[ OP[0] ], 1, temp); \
1975 trace_output (OP_BIT_CHANGE); \
1977 return 4;
1979 /* clr1 reg2, [reg1] */
1981 OP_E407E0 (void)
1983 BIT_CHANGE_OP ("clr1", &= ~ );
1986 /* not1 reg2, [reg1] */
1988 OP_E207E0 (void)
1990 BIT_CHANGE_OP ("not1", ^= );
1993 /* set1 */
1995 OP_E007E0 (void)
1997 BIT_CHANGE_OP ("set1", |= );
2000 /* sasf */
2002 OP_20007E0 (void)
2004 trace_input ("sasf", OP_EX1, 0);
2006 State.regs[ OP[1] ] = (State.regs[ OP[1] ] << 1) | condition_met (OP[0]);
2008 trace_output (OP_EX1);
2010 return 4;
2013 /* This function is courtesy of Sugimoto at NEC, via Seow Tan
2014 (Soew_Tan@el.nec.com) */
2015 void
2016 divun
2018 unsigned int N,
2019 unsigned long int als,
2020 unsigned long int sfi,
2021 uint32_t /*unsigned long int*/ * quotient_ptr,
2022 uint32_t /*unsigned long int*/ * remainder_ptr,
2023 int * overflow_ptr
2026 unsigned long ald = sfi >> (N - 1);
2027 unsigned long alo = als;
2028 unsigned int Q = 1;
2029 unsigned int C;
2030 unsigned int S = 0;
2031 unsigned int i;
2032 unsigned int R1 = 1;
2033 unsigned int DBZ = (als == 0) ? 1 : 0;
2034 unsigned long alt = Q ? ~als : als;
2036 /* 1st Loop */
2037 alo = ald + alt + Q;
2038 C = (((alt >> 31) & (ald >> 31))
2039 | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
2040 C = C ^ Q;
2041 Q = ~(C ^ S) & 1;
2042 R1 = (alo == 0) ? 0 : (R1 & Q);
2043 if ((S ^ (alo>>31)) && !C)
2045 DBZ = 1;
2047 S = alo >> 31;
2048 sfi = (sfi << (32-N+1)) | Q;
2049 ald = (alo << 1) | (sfi >> 31);
2051 /* 2nd - N-1th Loop */
2052 for (i = 2; i < N; i++)
2054 alt = Q ? ~als : als;
2055 alo = ald + alt + Q;
2056 C = (((alt >> 31) & (ald >> 31))
2057 | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
2058 C = C ^ Q;
2059 Q = ~(C ^ S) & 1;
2060 R1 = (alo == 0) ? 0 : (R1 & Q);
2061 if ((S ^ (alo>>31)) && !C && !DBZ)
2063 DBZ = 1;
2065 S = alo >> 31;
2066 sfi = (sfi << 1) | Q;
2067 ald = (alo << 1) | (sfi >> 31);
2070 /* Nth Loop */
2071 alt = Q ? ~als : als;
2072 alo = ald + alt + Q;
2073 C = (((alt >> 31) & (ald >> 31))
2074 | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
2075 C = C ^ Q;
2076 Q = ~(C ^ S) & 1;
2077 R1 = (alo == 0) ? 0 : (R1 & Q);
2078 if ((S ^ (alo>>31)) && !C)
2080 DBZ = 1;
2083 * quotient_ptr = (sfi << 1) | Q;
2084 * remainder_ptr = Q ? alo : (alo + als);
2085 * overflow_ptr = DBZ | R1;
2088 /* This function is courtesy of Sugimoto at NEC, via Seow Tan (Soew_Tan@el.nec.com) */
2089 void
2090 divn
2092 unsigned int N,
2093 unsigned long int als,
2094 unsigned long int sfi,
2095 int32_t /*signed long int*/ * quotient_ptr,
2096 int32_t /*signed long int*/ * remainder_ptr,
2097 int * overflow_ptr
2100 unsigned long ald = (signed long) sfi >> (N - 1);
2101 unsigned long alo = als;
2102 unsigned int SS = als >> 31;
2103 unsigned int SD = sfi >> 31;
2104 unsigned int R1 = 1;
2105 unsigned int OV;
2106 unsigned int DBZ = als == 0 ? 1 : 0;
2107 unsigned int Q = ~(SS ^ SD) & 1;
2108 unsigned int C;
2109 unsigned int i;
2110 unsigned long alt = Q ? ~als : als;
2113 /* 1st Loop */
2115 alo = ald + alt + Q;
2116 C = (((alt >> 31) & (ald >> 31))
2117 | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
2118 Q = C ^ SS;
2119 R1 = (alo == 0) ? 0 : (R1 & (Q ^ (SS ^ SD)));
2120 /* S = alo >> 31; */
2121 sfi = (sfi << (32-N+1)) | Q;
2122 ald = (alo << 1) | (sfi >> 31);
2123 if ((alo >> 31) ^ (ald >> 31))
2125 DBZ = 1;
2128 /* 2nd - N-1th Loop */
2130 for (i = 2; i < N; i++)
2132 alt = Q ? ~als : als;
2133 alo = ald + alt + Q;
2134 C = (((alt >> 31) & (ald >> 31))
2135 | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
2136 Q = C ^ SS;
2137 R1 = (alo == 0) ? 0 : (R1 & (Q ^ (SS ^ SD)));
2138 /* S = alo >> 31; */
2139 sfi = (sfi << 1) | Q;
2140 ald = (alo << 1) | (sfi >> 31);
2141 if ((alo >> 31) ^ (ald >> 31))
2143 DBZ = 1;
2147 /* Nth Loop */
2148 alt = Q ? ~als : als;
2149 alo = ald + alt + Q;
2150 C = (((alt >> 31) & (ald >> 31))
2151 | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
2152 Q = C ^ SS;
2153 R1 = (alo == 0) ? 0 : (R1 & (Q ^ (SS ^ SD)));
2154 sfi = (sfi << (32-N+1));
2155 ald = alo;
2157 /* End */
2158 if (alo != 0)
2160 alt = Q ? ~als : als;
2161 alo = ald + alt + Q;
2163 R1 = R1 & ((~alo >> 31) ^ SD);
2164 if ((alo != 0) && ((Q ^ (SS ^ SD)) ^ R1)) alo = ald;
2165 if (N != 32)
2166 ald = sfi = (long) ((sfi >> 1) | (SS ^ SD) << 31) >> (32-N-1) | Q;
2167 else
2168 ald = sfi = sfi | Q;
2170 OV = DBZ | ((alo == 0) ? 0 : R1);
2172 * remainder_ptr = alo;
2174 /* Adj */
2175 if (((alo != 0) && ((SS ^ SD) ^ R1))
2176 || ((alo == 0) && (SS ^ R1)))
2177 alo = ald + 1;
2178 else
2179 alo = ald;
2181 OV = (DBZ | R1) ? OV : ((alo >> 31) & (~ald >> 31));
2183 * quotient_ptr = alo;
2184 * overflow_ptr = OV;
2187 /* sdivun imm5, reg1, reg2, reg3 */
2189 OP_1C207E0 (void)
2191 uint32_t /*unsigned long int*/ quotient;
2192 uint32_t /*unsigned long int*/ remainder;
2193 unsigned long int divide_by;
2194 unsigned long int divide_this;
2195 int overflow = 0;
2196 unsigned int imm5;
2198 trace_input ("sdivun", OP_IMM_REG_REG_REG, 0);
2200 imm5 = 32 - ((OP[3] & 0x3c0000) >> 17);
2202 divide_by = State.regs[ OP[0] ];
2203 divide_this = State.regs[ OP[1] ] << imm5;
2205 divun (imm5, divide_by, divide_this, & quotient, & remainder, & overflow);
2207 State.regs[ OP[1] ] = quotient;
2208 State.regs[ OP[2] >> 11 ] = remainder;
2210 /* Set condition codes. */
2211 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2213 if (overflow) PSW |= PSW_OV;
2214 if (quotient == 0) PSW |= PSW_Z;
2215 if (quotient & 0x80000000) PSW |= PSW_S;
2217 trace_output (OP_IMM_REG_REG_REG);
2219 return 4;
2222 /* sdivn imm5, reg1, reg2, reg3 */
2224 OP_1C007E0 (void)
2226 int32_t /*signed long int*/ quotient;
2227 int32_t /*signed long int*/ remainder;
2228 signed long int divide_by;
2229 signed long int divide_this;
2230 int overflow = 0;
2231 unsigned int imm5;
2233 trace_input ("sdivn", OP_IMM_REG_REG_REG, 0);
2235 imm5 = 32 - ((OP[3] & 0x3c0000) >> 17);
2237 divide_by = (int32_t) State.regs[ OP[0] ];
2238 divide_this = (int32_t) (State.regs[ OP[1] ] << imm5);
2240 divn (imm5, divide_by, divide_this, & quotient, & remainder, & overflow);
2242 State.regs[ OP[1] ] = quotient;
2243 State.regs[ OP[2] >> 11 ] = remainder;
2245 /* Set condition codes. */
2246 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2248 if (overflow) PSW |= PSW_OV;
2249 if (quotient == 0) PSW |= PSW_Z;
2250 if (quotient < 0) PSW |= PSW_S;
2252 trace_output (OP_IMM_REG_REG_REG);
2254 return 4;
2257 /* sdivhun imm5, reg1, reg2, reg3 */
2259 OP_18207E0 (void)
2261 uint32_t /*unsigned long int*/ quotient;
2262 uint32_t /*unsigned long int*/ remainder;
2263 unsigned long int divide_by;
2264 unsigned long int divide_this;
2265 int overflow = 0;
2266 unsigned int imm5;
2268 trace_input ("sdivhun", OP_IMM_REG_REG_REG, 0);
2270 imm5 = 32 - ((OP[3] & 0x3c0000) >> 17);
2272 divide_by = State.regs[ OP[0] ] & 0xffff;
2273 divide_this = State.regs[ OP[1] ] << imm5;
2275 divun (imm5, divide_by, divide_this, & quotient, & remainder, & overflow);
2277 State.regs[ OP[1] ] = quotient;
2278 State.regs[ OP[2] >> 11 ] = remainder;
2280 /* Set condition codes. */
2281 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2283 if (overflow) PSW |= PSW_OV;
2284 if (quotient == 0) PSW |= PSW_Z;
2285 if (quotient & 0x80000000) PSW |= PSW_S;
2287 trace_output (OP_IMM_REG_REG_REG);
2289 return 4;
2292 /* sdivhn imm5, reg1, reg2, reg3 */
2294 OP_18007E0 (void)
2296 int32_t /*signed long int*/ quotient;
2297 int32_t /*signed long int*/ remainder;
2298 signed long int divide_by;
2299 signed long int divide_this;
2300 int overflow = 0;
2301 unsigned int imm5;
2303 trace_input ("sdivhn", OP_IMM_REG_REG_REG, 0);
2305 imm5 = 32 - ((OP[3] & 0x3c0000) >> 17);
2307 divide_by = EXTEND16 (State.regs[ OP[0] ]);
2308 divide_this = (int32_t) (State.regs[ OP[1] ] << imm5);
2310 divn (imm5, divide_by, divide_this, & quotient, & remainder, & overflow);
2312 State.regs[ OP[1] ] = quotient;
2313 State.regs[ OP[2] >> 11 ] = remainder;
2315 /* Set condition codes. */
2316 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2318 if (overflow) PSW |= PSW_OV;
2319 if (quotient == 0) PSW |= PSW_Z;
2320 if (quotient < 0) PSW |= PSW_S;
2322 trace_output (OP_IMM_REG_REG_REG);
2324 return 4;
2327 /* divu reg1, reg2, reg3 */
2329 OP_2C207E0 (void)
2331 unsigned long int quotient;
2332 unsigned long int remainder;
2333 unsigned long int divide_by;
2334 unsigned long int divide_this;
2335 int overflow = 0;
2337 trace_input ("divu", OP_REG_REG_REG, 0);
2339 /* Compute the result. */
2341 divide_by = State.regs[ OP[0] ];
2342 divide_this = State.regs[ OP[1] ];
2344 if (divide_by == 0)
2346 PSW |= PSW_OV;
2348 else
2350 State.regs[ OP[1] ] = quotient = divide_this / divide_by;
2351 State.regs[ OP[2] >> 11 ] = remainder = divide_this % divide_by;
2353 /* Set condition codes. */
2354 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2356 if (overflow) PSW |= PSW_OV;
2357 if (quotient == 0) PSW |= PSW_Z;
2358 if (quotient & 0x80000000) PSW |= PSW_S;
2361 trace_output (OP_REG_REG_REG);
2363 return 4;
2366 /* div reg1, reg2, reg3 */
2368 OP_2C007E0 (void)
2370 signed long int quotient;
2371 signed long int remainder;
2372 signed long int divide_by;
2373 signed long int divide_this;
2375 trace_input ("div", OP_REG_REG_REG, 0);
2377 /* Compute the result. */
2379 divide_by = (int32_t) State.regs[ OP[0] ];
2380 divide_this = State.regs[ OP[1] ];
2382 if (divide_by == 0)
2384 PSW |= PSW_OV;
2386 else if (divide_by == -1 && divide_this == (1L << 31))
2388 PSW &= ~PSW_Z;
2389 PSW |= PSW_OV | PSW_S;
2390 State.regs[ OP[1] ] = (1 << 31);
2391 State.regs[ OP[2] >> 11 ] = 0;
2393 else
2395 divide_this = (int32_t) divide_this;
2396 State.regs[ OP[1] ] = quotient = divide_this / divide_by;
2397 State.regs[ OP[2] >> 11 ] = remainder = divide_this % divide_by;
2399 /* Set condition codes. */
2400 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2402 if (quotient == 0) PSW |= PSW_Z;
2403 if (quotient < 0) PSW |= PSW_S;
2406 trace_output (OP_REG_REG_REG);
2408 return 4;
2411 /* divhu reg1, reg2, reg3 */
2413 OP_28207E0 (void)
2415 unsigned long int quotient;
2416 unsigned long int remainder;
2417 unsigned long int divide_by;
2418 unsigned long int divide_this;
2419 int overflow = 0;
2421 trace_input ("divhu", OP_REG_REG_REG, 0);
2423 /* Compute the result. */
2425 divide_by = State.regs[ OP[0] ] & 0xffff;
2426 divide_this = State.regs[ OP[1] ];
2428 if (divide_by == 0)
2430 PSW |= PSW_OV;
2432 else
2434 State.regs[ OP[1] ] = quotient = divide_this / divide_by;
2435 State.regs[ OP[2] >> 11 ] = remainder = divide_this % divide_by;
2437 /* Set condition codes. */
2438 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2440 if (overflow) PSW |= PSW_OV;
2441 if (quotient == 0) PSW |= PSW_Z;
2442 if (quotient & 0x80000000) PSW |= PSW_S;
2445 trace_output (OP_REG_REG_REG);
2447 return 4;
2450 /* divh reg1, reg2, reg3 */
2452 OP_28007E0 (void)
2454 signed long int quotient;
2455 signed long int remainder;
2456 signed long int divide_by;
2457 signed long int divide_this;
2459 trace_input ("divh", OP_REG_REG_REG, 0);
2461 /* Compute the result. */
2463 divide_by = EXTEND16 (State.regs[ OP[0] ]);
2464 divide_this = State.regs[ OP[1] ];
2466 if (divide_by == 0)
2468 PSW |= PSW_OV;
2470 else if (divide_by == -1 && divide_this == (1L << 31))
2472 PSW &= ~PSW_Z;
2473 PSW |= PSW_OV | PSW_S;
2474 State.regs[ OP[1] ] = (1 << 31);
2475 State.regs[ OP[2] >> 11 ] = 0;
2477 else
2479 divide_this = (int32_t) divide_this;
2480 State.regs[ OP[1] ] = quotient = divide_this / divide_by;
2481 State.regs[ OP[2] >> 11 ] = remainder = divide_this % divide_by;
2483 /* Set condition codes. */
2484 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2486 if (quotient == 0) PSW |= PSW_Z;
2487 if (quotient < 0) PSW |= PSW_S;
2490 trace_output (OP_REG_REG_REG);
2492 return 4;
2495 /* mulu imm9, reg2, reg3 */
2497 OP_24207E0 (void)
2499 trace_input ("mulu", OP_IMM_REG_REG, 0);
2501 Multiply64 (0, (OP[3] & 0x1f) | ((OP[3] >> 13) & 0x1e0));
2503 trace_output (OP_IMM_REG_REG);
2505 return 4;
2508 /* mul imm9, reg2, reg3 */
2510 OP_24007E0 (void)
2512 trace_input ("mul", OP_IMM_REG_REG, 0);
2514 Multiply64 (1, SEXT9 ((OP[3] & 0x1f) | ((OP[3] >> 13) & 0x1e0)));
2516 trace_output (OP_IMM_REG_REG);
2518 return 4;
2521 /* ld.hu */
2523 OP_107E0 (void)
2525 int adr;
2527 trace_input ("ld.hu", OP_LOAD32, 2);
2529 adr = State.regs[ OP[0] ] + EXTEND16 (OP[2] & ~1);
2530 adr &= ~0x1;
2532 State.regs[ OP[1] ] = load_mem (adr, 2);
2534 trace_output (OP_LOAD32);
2536 return 4;
2540 /* ld.bu */
2542 OP_10780 (void)
2544 int adr;
2546 trace_input ("ld.bu", OP_LOAD32, 1);
2548 adr = (State.regs[ OP[0] ]
2549 + (EXTEND16 (OP[2] & ~1) | ((OP[3] >> 5) & 1)));
2551 State.regs[ OP[1] ] = load_mem (adr, 1);
2553 trace_output (OP_LOAD32);
2555 return 4;
2558 /* prepare list12, imm5, imm32 */
2560 OP_1B0780 (void)
2562 int i;
2564 trace_input ("prepare", OP_PUSHPOP1, 0);
2566 /* Store the registers with lower number registers being placed at higher addresses. */
2567 for (i = 0; i < 12; i++)
2568 if ((OP[3] & (1 << type1_regs[ i ])))
2570 SP -= 4;
2571 store_mem (SP, 4, State.regs[ 20 + i ]);
2574 SP -= (OP[3] & 0x3e) << 1;
2576 EP = load_mem (PC + 4, 4);
2578 trace_output (OP_PUSHPOP1);
2580 return 8;
2583 /* prepare list12, imm5, imm16-32 */
2585 OP_130780 (void)
2587 int i;
2589 trace_input ("prepare", OP_PUSHPOP1, 0);
2591 /* Store the registers with lower number registers being placed at higher addresses. */
2592 for (i = 0; i < 12; i++)
2593 if ((OP[3] & (1 << type1_regs[ i ])))
2595 SP -= 4;
2596 store_mem (SP, 4, State.regs[ 20 + i ]);
2599 SP -= (OP[3] & 0x3e) << 1;
2601 EP = load_mem (PC + 4, 2) << 16;
2603 trace_output (OP_PUSHPOP1);
2605 return 6;
2608 /* prepare list12, imm5, imm16 */
2610 OP_B0780 (void)
2612 int i;
2614 trace_input ("prepare", OP_PUSHPOP1, 0);
2616 /* Store the registers with lower number registers being placed at higher addresses. */
2617 for (i = 0; i < 12; i++)
2618 if ((OP[3] & (1 << type1_regs[ i ])))
2620 SP -= 4;
2621 store_mem (SP, 4, State.regs[ 20 + i ]);
2624 SP -= (OP[3] & 0x3e) << 1;
2626 EP = EXTEND16 (load_mem (PC + 4, 2));
2628 trace_output (OP_PUSHPOP1);
2630 return 6;
2633 /* prepare list12, imm5, sp */
2635 OP_30780 (void)
2637 int i;
2639 trace_input ("prepare", OP_PUSHPOP1, 0);
2641 /* Store the registers with lower number registers being placed at higher addresses. */
2642 for (i = 0; i < 12; i++)
2643 if ((OP[3] & (1 << type1_regs[ i ])))
2645 SP -= 4;
2646 store_mem (SP, 4, State.regs[ 20 + i ]);
2649 SP -= (OP[3] & 0x3e) << 1;
2651 EP = SP;
2653 trace_output (OP_PUSHPOP1);
2655 return 4;
2658 /* mul reg1, reg2, reg3 */
2660 OP_22007E0 (void)
2662 trace_input ("mul", OP_REG_REG_REG, 0);
2664 Multiply64 (1, State.regs[ OP[0] ]);
2666 trace_output (OP_REG_REG_REG);
2668 return 4;
2671 /* popmh list18 */
2673 OP_307F0 (void)
2675 int i;
2677 trace_input ("popmh", OP_PUSHPOP2, 0);
2679 if (OP[3] & (1 << 19))
2681 if ((PSW & PSW_NP) && ((PSW & PSW_EP) == 0))
2683 FEPSW = load_mem ( SP & ~ 3, 4);
2684 FEPC = load_mem ((SP + 4) & ~ 3, 4);
2686 else
2688 EIPSW = load_mem ( SP & ~ 3, 4);
2689 EIPC = load_mem ((SP + 4) & ~ 3, 4);
2692 SP += 8;
2695 /* Load the registers with lower number registers being retrieved from higher addresses. */
2696 for (i = 16; i--;)
2697 if ((OP[3] & (1 << type2_regs[ i ])))
2699 State.regs[ i + 16 ] = load_mem (SP & ~ 3, 4);
2700 SP += 4;
2703 trace_output (OP_PUSHPOP2);
2705 return 4;
2708 /* popml lsit18 */
2710 OP_107F0 (void)
2712 int i;
2714 trace_input ("popml", OP_PUSHPOP3, 0);
2716 if (OP[3] & (1 << 19))
2718 if ((PSW & PSW_NP) && ((PSW & PSW_EP) == 0))
2720 FEPSW = load_mem ( SP & ~ 3, 4);
2721 FEPC = load_mem ((SP + 4) & ~ 3, 4);
2723 else
2725 EIPSW = load_mem ( SP & ~ 3, 4);
2726 EIPC = load_mem ((SP + 4) & ~ 3, 4);
2729 SP += 8;
2732 if (OP[3] & (1 << 3))
2734 PSW = load_mem (SP & ~ 3, 4);
2735 SP += 4;
2738 /* Load the registers with lower number registers being retrieved from higher addresses. */
2739 for (i = 15; i--;)
2740 if ((OP[3] & (1 << type3_regs[ i ])))
2742 State.regs[ i + 1 ] = load_mem (SP & ~ 3, 4);
2743 SP += 4;
2746 trace_output (OP_PUSHPOP2);
2748 return 4;
2751 /* pushmh list18 */
2753 OP_307E0 (void)
2755 int i;
2757 trace_input ("pushmh", OP_PUSHPOP2, 0);
2759 /* Store the registers with lower number registers being placed at higher addresses. */
2760 for (i = 0; i < 16; i++)
2761 if ((OP[3] & (1 << type2_regs[ i ])))
2763 SP -= 4;
2764 store_mem (SP & ~ 3, 4, State.regs[ i + 16 ]);
2767 if (OP[3] & (1 << 19))
2769 SP -= 8;
2771 if ((PSW & PSW_NP) && ((PSW & PSW_EP) == 0))
2773 store_mem ((SP + 4) & ~ 3, 4, FEPC);
2774 store_mem ( SP & ~ 3, 4, FEPSW);
2776 else
2778 store_mem ((SP + 4) & ~ 3, 4, EIPC);
2779 store_mem ( SP & ~ 3, 4, EIPSW);
2783 trace_output (OP_PUSHPOP2);
2785 return 4;
2788 /* V850E2R FPU functions */
2790 sim_fpu_status_invalid_snan = 1, -V--- (sim spec.)
2791 sim_fpu_status_invalid_qnan = 2, ----- (sim spec.)
2792 sim_fpu_status_invalid_isi = 4, (inf - inf) -V---
2793 sim_fpu_status_invalid_idi = 8, (inf / inf) -V---
2794 sim_fpu_status_invalid_zdz = 16, (0 / 0) -V---
2795 sim_fpu_status_invalid_imz = 32, (inf * 0) -V---
2796 sim_fpu_status_invalid_cvi = 64, convert to integer -V---
2797 sim_fpu_status_invalid_div0 = 128, (X / 0) --Z--
2798 sim_fpu_status_invalid_cmp = 256, compare ----- (sim spec.)
2799 sim_fpu_status_invalid_sqrt = 512, -V---
2800 sim_fpu_status_rounded = 1024, I----
2801 sim_fpu_status_inexact = 2048, I---- (sim spec.)
2802 sim_fpu_status_overflow = 4096, I--O-
2803 sim_fpu_status_underflow = 8192, I---U
2804 sim_fpu_status_denorm = 16384, ----U (sim spec.)
2807 void
2808 update_fpsr (SIM_DESC sd, sim_fpu_status status, unsigned int mask, unsigned int double_op_p)
2810 unsigned int fpsr = FPSR & mask;
2812 unsigned int flags = 0;
2814 if (fpsr & FPSR_XEI
2815 && ((status & (sim_fpu_status_rounded
2816 | sim_fpu_status_overflow
2817 | sim_fpu_status_inexact))
2818 || (status & sim_fpu_status_underflow
2819 && (fpsr & (FPSR_XEU | FPSR_XEI)) == 0
2820 && fpsr & FPSR_FS)))
2822 flags |= FPSR_XCI | FPSR_XPI;
2825 if (fpsr & FPSR_XEV
2826 && (status & (sim_fpu_status_invalid_isi
2827 | sim_fpu_status_invalid_imz
2828 | sim_fpu_status_invalid_zdz
2829 | sim_fpu_status_invalid_idi
2830 | sim_fpu_status_invalid_cvi
2831 | sim_fpu_status_invalid_sqrt
2832 | sim_fpu_status_invalid_snan)))
2834 flags |= FPSR_XCV | FPSR_XPV;
2837 if (fpsr & FPSR_XEZ
2838 && (status & sim_fpu_status_invalid_div0))
2840 flags |= FPSR_XCV | FPSR_XPV;
2843 if (fpsr & FPSR_XEO
2844 && (status & sim_fpu_status_overflow))
2846 flags |= FPSR_XCO | FPSR_XPO;
2849 if (((fpsr & FPSR_XEU) || (fpsr & FPSR_FS) == 0)
2850 && (status & (sim_fpu_status_underflow
2851 | sim_fpu_status_denorm)))
2853 flags |= FPSR_XCU | FPSR_XPU;
2856 if (flags)
2858 FPSR &= ~FPSR_XC;
2859 FPSR |= flags;
2861 SignalExceptionFPE (sd, double_op_p);
2865 /* Exception. */
2867 void
2868 SignalException (SIM_DESC sd)
2870 if (MPM & MPM_AUE)
2872 PSW = PSW & ~(PSW_NPV | PSW_DMP | PSW_IMP);
2876 void
2877 SignalExceptionFPE (SIM_DESC sd, unsigned int double_op_p)
2879 if (((PSW & (PSW_NP|PSW_ID)) == 0)
2880 || !(FPSR & (double_op_p ? FPSR_DEM : FPSR_SEM)))
2882 EIPC = PC;
2883 EIPSW = PSW;
2884 EIIC = (FPSR & (double_op_p ? FPSR_DEM : FPSR_SEM))
2885 ? 0x71 : 0x72;
2886 PSW |= (PSW_EP | PSW_ID);
2887 PC = 0x70;
2889 SignalException (sd);
2893 void
2894 check_invalid_snan (SIM_DESC sd, sim_fpu_status status, unsigned int double_op_p)
2896 if ((FPSR & FPSR_XEI)
2897 && (status & sim_fpu_status_invalid_snan))
2899 FPSR &= ~FPSR_XC;
2900 FPSR |= FPSR_XCV;
2901 FPSR |= FPSR_XPV;
2902 SignalExceptionFPE (sd, double_op_p);
2907 v850_float_compare (SIM_DESC sd, int cmp, sim_fpu wop1, sim_fpu wop2, int double_op_p)
2909 int result = -1;
2911 if (sim_fpu_is_nan (&wop1) || sim_fpu_is_nan (&wop2))
2913 if (cmp & 0x8)
2915 if (FPSR & FPSR_XEV)
2917 FPSR |= FPSR_XCV | FPSR_XPV;
2918 SignalExceptionFPE (sd, double_op_p);
2922 switch (cmp)
2924 case FPU_CMP_F:
2925 result = 0;
2926 break;
2927 case FPU_CMP_UN:
2928 result = 1;
2929 break;
2930 case FPU_CMP_EQ:
2931 result = 0;
2932 break;
2933 case FPU_CMP_UEQ:
2934 result = 1;
2935 break;
2936 case FPU_CMP_OLT:
2937 result = 0;
2938 break;
2939 case FPU_CMP_ULT:
2940 result = 1;
2941 break;
2942 case FPU_CMP_OLE:
2943 result = 0;
2944 break;
2945 case FPU_CMP_ULE:
2946 result = 1;
2947 break;
2948 case FPU_CMP_SF:
2949 result = 0;
2950 break;
2951 case FPU_CMP_NGLE:
2952 result = 1;
2953 break;
2954 case FPU_CMP_SEQ:
2955 result = 0;
2956 break;
2957 case FPU_CMP_NGL:
2958 result = 1;
2959 break;
2960 case FPU_CMP_LT:
2961 result = 0;
2962 break;
2963 case FPU_CMP_NGE:
2964 result = 1;
2965 break;
2966 case FPU_CMP_LE:
2967 result = 0;
2968 break;
2969 case FPU_CMP_NGT:
2970 result = 1;
2971 break;
2972 default:
2973 abort ();
2976 else if (sim_fpu_is_infinity (&wop1) && sim_fpu_is_infinity (&wop2)
2977 && sim_fpu_sign (&wop1) == sim_fpu_sign (&wop2))
2979 switch (cmp)
2981 case FPU_CMP_F:
2982 result = 0;
2983 break;
2984 case FPU_CMP_UN:
2985 result = 0;
2986 break;
2987 case FPU_CMP_EQ:
2988 result = 1;
2989 break;
2990 case FPU_CMP_UEQ:
2991 result = 1;
2992 break;
2993 case FPU_CMP_OLT:
2994 result = 0;
2995 break;
2996 case FPU_CMP_ULT:
2997 result = 0;
2998 break;
2999 case FPU_CMP_OLE:
3000 result = 1;
3001 break;
3002 case FPU_CMP_ULE:
3003 result = 1;
3004 break;
3005 case FPU_CMP_SF:
3006 result = 0;
3007 break;
3008 case FPU_CMP_NGLE:
3009 result = 0;
3010 break;
3011 case FPU_CMP_SEQ:
3012 result = 1;
3013 break;
3014 case FPU_CMP_NGL:
3015 result = 1;
3016 break;
3017 case FPU_CMP_LT:
3018 result = 0;
3019 break;
3020 case FPU_CMP_NGE:
3021 result = 0;
3022 break;
3023 case FPU_CMP_LE:
3024 result = 1;
3025 break;
3026 case FPU_CMP_NGT:
3027 result = 1;
3028 break;
3029 default:
3030 abort ();
3033 else
3035 int lt = 0, eq = 0, status;
3037 status = sim_fpu_cmp (&wop1, &wop2);
3039 switch (status)
3041 case SIM_FPU_IS_SNAN:
3042 case SIM_FPU_IS_QNAN:
3043 abort ();
3044 break;
3046 case SIM_FPU_IS_NINF:
3047 lt = 1;
3048 break;
3049 case SIM_FPU_IS_PINF:
3050 /* gt = 1; */
3051 break;
3052 case SIM_FPU_IS_NNUMBER:
3053 lt = 1;
3054 break;
3055 case SIM_FPU_IS_PNUMBER:
3056 /* gt = 1; */
3057 break;
3058 case SIM_FPU_IS_NDENORM:
3059 lt = 1;
3060 break;
3061 case SIM_FPU_IS_PDENORM:
3062 /* gt = 1; */
3063 break;
3064 case SIM_FPU_IS_NZERO:
3065 case SIM_FPU_IS_PZERO:
3066 eq = 1;
3067 break;
3070 switch (cmp)
3072 case FPU_CMP_F:
3073 result = 0;
3074 break;
3075 case FPU_CMP_UN:
3076 result = 0;
3077 break;
3078 case FPU_CMP_EQ:
3079 result = eq;
3080 break;
3081 case FPU_CMP_UEQ:
3082 result = eq;
3083 break;
3084 case FPU_CMP_OLT:
3085 result = lt;
3086 break;
3087 case FPU_CMP_ULT:
3088 result = lt;
3089 break;
3090 case FPU_CMP_OLE:
3091 result = lt || eq;
3092 break;
3093 case FPU_CMP_ULE:
3094 result = lt || eq;
3095 break;
3096 case FPU_CMP_SF:
3097 result = 0;
3098 break;
3099 case FPU_CMP_NGLE:
3100 result = 0;
3101 break;
3102 case FPU_CMP_SEQ:
3103 result = eq;
3104 break;
3105 case FPU_CMP_NGL:
3106 result = eq;
3107 break;
3108 case FPU_CMP_LT:
3109 result = lt;
3110 break;
3111 case FPU_CMP_NGE:
3112 result = lt;
3113 break;
3114 case FPU_CMP_LE:
3115 result = lt || eq;
3116 break;
3117 case FPU_CMP_NGT:
3118 result = lt || eq;
3119 break;
3123 ASSERT (result != -1);
3124 return result;
3127 void
3128 v850_div (SIM_DESC sd, unsigned int op0, unsigned int op1, unsigned int *op2p, unsigned int *op3p)
3130 signed long int quotient;
3131 signed long int remainder;
3132 signed long int divide_by;
3133 signed long int divide_this;
3134 bfd_boolean overflow = FALSE;
3136 /* Compute the result. */
3137 divide_by = (int32_t)op0;
3138 divide_this = (int32_t)op1;
3140 if (divide_by == 0 || (divide_by == -1 && divide_this == (1 << 31)))
3142 overflow = TRUE;
3143 divide_by = 1;
3146 quotient = divide_this / divide_by;
3147 remainder = divide_this % divide_by;
3149 /* Set condition codes. */
3150 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
3152 if (overflow) PSW |= PSW_OV;
3153 if (quotient == 0) PSW |= PSW_Z;
3154 if (quotient < 0) PSW |= PSW_S;
3156 *op2p = quotient;
3157 *op3p = remainder;
3160 void
3161 v850_divu (SIM_DESC sd, unsigned int op0, unsigned int op1, unsigned int *op2p, unsigned int *op3p)
3163 unsigned long int quotient;
3164 unsigned long int remainder;
3165 unsigned long int divide_by;
3166 unsigned long int divide_this;
3167 bfd_boolean overflow = FALSE;
3169 /* Compute the result. */
3171 divide_by = op0;
3172 divide_this = op1;
3174 if (divide_by == 0)
3176 overflow = TRUE;
3177 divide_by = 1;
3180 quotient = divide_this / divide_by;
3181 remainder = divide_this % divide_by;
3183 /* Set condition codes. */
3184 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
3186 if (overflow) PSW |= PSW_OV;
3187 if (quotient == 0) PSW |= PSW_Z;
3188 if (quotient & 0x80000000) PSW |= PSW_S;
3190 *op2p = quotient;
3191 *op3p = remainder;
3194 void
3195 v850_sar (SIM_DESC sd, unsigned int op0, unsigned int op1, unsigned int *op2p)
3197 unsigned int result, z, s, cy;
3199 op0 &= 0x1f;
3200 result = (signed)op1 >> op0;
3202 /* Compute the condition codes. */
3203 z = (result == 0);
3204 s = (result & 0x80000000);
3205 cy = (op1 & (1 << (op0 - 1)));
3207 /* Store the result and condition codes. */
3208 PSW &= ~(PSW_Z | PSW_S | PSW_OV | PSW_CY);
3209 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
3210 | (cy ? PSW_CY : 0));
3212 *op2p = result;
3215 void
3216 v850_shl (SIM_DESC sd, unsigned int op0, unsigned int op1, unsigned int *op2p)
3218 unsigned int result, z, s, cy;
3220 op0 &= 0x1f;
3221 result = op1 << op0;
3223 /* Compute the condition codes. */
3224 z = (result == 0);
3225 s = (result & 0x80000000);
3226 cy = (op1 & (1 << (32 - op0)));
3228 /* Store the result and condition codes. */
3229 PSW &= ~(PSW_Z | PSW_S | PSW_OV | PSW_CY);
3230 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
3231 | (cy ? PSW_CY : 0));
3233 *op2p = result;
3236 void
3237 v850_rotl (SIM_DESC sd, unsigned int amount, unsigned int src, unsigned int * dest)
3239 unsigned int result, z, s, cy;
3241 amount &= 0x1f;
3242 result = src << amount;
3243 result |= src >> (32 - amount);
3245 /* Compute the condition codes. */
3246 z = (result == 0);
3247 s = (result & 0x80000000);
3248 cy = ! (result & 1);
3250 /* Store the result and condition codes. */
3251 PSW &= ~(PSW_Z | PSW_S | PSW_OV | PSW_CY);
3252 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
3253 | (cy ? PSW_CY : 0));
3255 * dest = result;
3258 void
3259 v850_bins (SIM_DESC sd, unsigned int source, unsigned int lsb, unsigned int msb,
3260 unsigned int * dest)
3262 unsigned int mask;
3263 unsigned int result, pos, width;
3264 unsigned int z, s;
3266 pos = lsb;
3267 width = (msb - lsb) + 1;
3269 /* A width of 32 exhibits undefined behavior on the shift. The easiest
3270 way to make this code safe is to just avoid that case and set the mask
3271 to the right value. */
3272 if (width >= 32)
3273 mask = 0xffffffff;
3274 else
3275 mask = ~ (-(1 << width));
3277 source &= mask;
3278 mask <<= pos;
3279 result = (* dest) & ~ mask;
3280 result |= source << pos;
3282 /* Compute the condition codes. */
3283 z = (result == 0);
3284 s = result & 0x80000000;
3286 /* Store the result and condition codes. */
3287 PSW &= ~(PSW_Z | PSW_S | PSW_OV );
3288 PSW |= (z ? PSW_Z : 0) | (s ? PSW_S : 0);
3290 * dest = result;
3293 void
3294 v850_shr (SIM_DESC sd, unsigned int op0, unsigned int op1, unsigned int *op2p)
3296 unsigned int result, z, s, cy;
3298 op0 &= 0x1f;
3299 result = op1 >> op0;
3301 /* Compute the condition codes. */
3302 z = (result == 0);
3303 s = (result & 0x80000000);
3304 cy = (op1 & (1 << (op0 - 1)));
3306 /* Store the result and condition codes. */
3307 PSW &= ~(PSW_Z | PSW_S | PSW_OV | PSW_CY);
3308 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
3309 | (cy ? PSW_CY : 0));
3311 *op2p = result;
3314 void
3315 v850_satadd (SIM_DESC sd, unsigned int op0, unsigned int op1, unsigned int *op2p)
3317 unsigned int result, z, s, cy, ov, sat;
3319 result = op0 + op1;
3321 /* Compute the condition codes. */
3322 z = (result == 0);
3323 s = (result & 0x80000000);
3324 cy = (result < op0 || result < op1);
3325 ov = ((op0 & 0x80000000) == (op1 & 0x80000000)
3326 && (op0 & 0x80000000) != (result & 0x80000000));
3327 sat = ov;
3329 /* Store the result and condition codes. */
3330 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
3331 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
3332 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0)
3333 | (sat ? PSW_SAT : 0));
3335 /* Handle saturated results. */
3336 if (sat && s)
3338 result = 0x7fffffff;
3339 PSW &= ~PSW_S;
3341 else if (sat)
3343 result = 0x80000000;
3344 PSW |= PSW_S;
3347 *op2p = result;
3350 void
3351 v850_satsub (SIM_DESC sd, unsigned int op0, unsigned int op1, unsigned int *op2p)
3353 unsigned int result, z, s, cy, ov, sat;
3355 /* Compute the result. */
3356 result = op1 - op0;
3358 /* Compute the condition codes. */
3359 z = (result == 0);
3360 s = (result & 0x80000000);
3361 cy = (op1 < op0);
3362 ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
3363 && (op1 & 0x80000000) != (result & 0x80000000));
3364 sat = ov;
3366 /* Store the result and condition codes. */
3367 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
3368 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
3369 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0)
3370 | (sat ? PSW_SAT : 0));
3372 /* Handle saturated results. */
3373 if (sat && s)
3375 result = 0x7fffffff;
3376 PSW &= ~PSW_S;
3378 else if (sat)
3380 result = 0x80000000;
3381 PSW |= PSW_S;
3384 *op2p = result;
3387 uint32_t
3388 load_data_mem (SIM_DESC sd,
3389 address_word addr,
3390 int len)
3392 uint32_t data;
3394 switch (len)
3396 case 1:
3397 data = sim_core_read_unaligned_1 (STATE_CPU (sd, 0),
3398 PC, read_map, addr);
3399 break;
3400 case 2:
3401 data = sim_core_read_unaligned_2 (STATE_CPU (sd, 0),
3402 PC, read_map, addr);
3403 break;
3404 case 4:
3405 data = sim_core_read_unaligned_4 (STATE_CPU (sd, 0),
3406 PC, read_map, addr);
3407 break;
3408 default:
3409 abort ();
3411 return data;
3414 void
3415 store_data_mem (SIM_DESC sd,
3416 address_word addr,
3417 int len,
3418 uint32_t data)
3420 switch (len)
3422 case 1:
3423 store_mem (addr, 1, data);
3424 break;
3425 case 2:
3426 store_mem (addr, 2, data);
3427 break;
3428 case 4:
3429 store_mem (addr, 4, data);
3430 break;
3431 default:
3432 abort ();
3437 mpu_load_mem_test (SIM_DESC sd, unsigned int addr, int size, int base_reg)
3439 int result = 1;
3441 if (PSW & PSW_DMP)
3443 if (IPE0 && addr >= IPA2ADDR (IPA0L) && addr <= IPA2ADDR (IPA0L) && IPR0)
3445 /* text area */
3447 else if (IPE1 && addr >= IPA2ADDR (IPA1L) && addr <= IPA2ADDR (IPA1L) && IPR1)
3449 /* text area */
3451 else if (IPE2 && addr >= IPA2ADDR (IPA2L) && addr <= IPA2ADDR (IPA2L) && IPR2)
3453 /* text area */
3455 else if (IPE3 && addr >= IPA2ADDR (IPA3L) && addr <= IPA2ADDR (IPA3L) && IPR3)
3457 /* text area */
3459 else if (addr >= PPA2ADDR (PPA & ~PPM) && addr <= DPA2ADDR (PPA | PPM))
3461 /* preifarallel area */
3463 else if (addr >= PPA2ADDR (SPAL) && addr <= DPA2ADDR (SPAU))
3465 /* stack area */
3467 else if (DPE0 && addr >= DPA2ADDR (DPA0L) && addr <= DPA2ADDR (DPA0L) && DPR0
3468 && ((SPAL & SPAL_SPS) ? base_reg == SP_REGNO : 1))
3470 /* data area */
3472 else if (DPE1 && addr >= DPA2ADDR (DPA1L) && addr <= DPA2ADDR (DPA1L) && DPR1
3473 && ((SPAL & SPAL_SPS) ? base_reg == SP_REGNO : 1))
3475 /* data area */
3477 else if (DPE2 && addr >= DPA2ADDR (DPA2L) && addr <= DPA2ADDR (DPA2L) && DPR2
3478 && ((SPAL & SPAL_SPS) ? base_reg == SP_REGNO : 1))
3480 /* data area */
3482 else if (DPE3 && addr >= DPA2ADDR (DPA3L) && addr <= DPA2ADDR (DPA3L) && DPR3
3483 && ((SPAL & SPAL_SPS) ? base_reg == SP_REGNO : 1))
3485 /* data area */
3487 else
3489 VMECR &= ~(VMECR_VMW | VMECR_VMX);
3490 VMECR |= VMECR_VMR;
3491 VMADR = addr;
3492 VMTID = TID;
3493 FEIC = 0x431;
3495 PC = 0x30;
3497 SignalException (sd);
3498 result = 0;
3502 return result;
3506 mpu_store_mem_test (SIM_DESC sd, unsigned int addr, int size, int base_reg)
3508 int result = 1;
3510 if (PSW & PSW_DMP)
3512 if (addr >= PPA2ADDR (PPA & ~PPM) && addr <= DPA2ADDR (PPA | PPM))
3514 /* preifarallel area */
3516 else if (addr >= PPA2ADDR (SPAL) && addr <= DPA2ADDR (SPAU))
3518 /* stack area */
3520 else if (DPE0 && addr >= DPA2ADDR (DPA0L) && addr <= DPA2ADDR (DPA0L) && DPW0
3521 && ((SPAL & SPAL_SPS) ? base_reg == SP_REGNO : 1))
3523 /* data area */
3525 else if (DPE1 && addr >= DPA2ADDR (DPA1L) && addr <= DPA2ADDR (DPA1L) && DPW1
3526 && ((SPAL & SPAL_SPS) ? base_reg == SP_REGNO : 1))
3528 /* data area */
3530 else if (DPE2 && addr >= DPA2ADDR (DPA2L) && addr <= DPA2ADDR (DPA2L) && DPW2
3531 && ((SPAL & SPAL_SPS) ? base_reg == SP_REGNO : 1))
3533 /* data area */
3535 else if (DPE3 && addr >= DPA2ADDR (DPA3L) && addr <= DPA2ADDR (DPA3L) && DPW3
3536 && ((SPAL & SPAL_SPS) ? base_reg == SP_REGNO : 1))
3538 /* data area */
3540 else
3542 if (addr >= PPA2ADDR (PPA & ~PPM) && addr <= DPA2ADDR (PPA | PPM))
3544 FEIC = 0x432;
3545 VPTID = TID;
3546 VPADR = PC;
3547 #ifdef NOT_YET
3548 VIP_PP;
3549 VPECR;
3550 #endif
3552 else
3554 FEIC = 0x431;
3555 VMTID = TID;
3556 VMADR = VMECR;
3557 VMECR &= ~(VMECR_VMW | VMECR_VMX);
3558 VMECR |= VMECR_VMR;
3559 PC = 0x30;
3561 result = 0;
3565 return result;