Merge remote-tracking branch 'bonzini/scsi-next' into staging
[qemu-kvm.git] / target-unicore32 / op_helper.c
blobb954c30a84d06111d07798eab5e3e738e8d062c4
1 /*
2 * UniCore32 helper routines
4 * Copyright (C) 2010-2011 GUAN Xue-tao
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation, or (at your option) any
9 * later version. See the COPYING file in the top-level directory.
11 #include "cpu.h"
12 #include "dyngen-exec.h"
13 #include "helper.h"
15 #define SIGNBIT (uint32_t)0x80000000
16 #define SIGNBIT64 ((uint64_t)1 << 63)
18 void HELPER(exception)(uint32_t excp)
20 env->exception_index = excp;
21 cpu_loop_exit(env);
24 static target_ulong asr_read(void)
26 int ZF;
27 ZF = (env->ZF == 0);
28 return env->uncached_asr | (env->NF & 0x80000000) | (ZF << 30) |
29 (env->CF << 29) | ((env->VF & 0x80000000) >> 3);
32 target_ulong cpu_asr_read(CPUUniCore32State *env1)
34 CPUUniCore32State *saved_env;
35 target_ulong ret;
37 saved_env = env;
38 env = env1;
39 ret = asr_read();
40 env = saved_env;
41 return ret;
44 target_ulong HELPER(asr_read)(void)
46 return asr_read();
49 static void asr_write(target_ulong val, target_ulong mask)
51 if (mask & ASR_NZCV) {
52 env->ZF = (~val) & ASR_Z;
53 env->NF = val;
54 env->CF = (val >> 29) & 1;
55 env->VF = (val << 3) & 0x80000000;
58 if ((env->uncached_asr ^ val) & mask & ASR_M) {
59 switch_mode(env, val & ASR_M);
61 mask &= ~ASR_NZCV;
62 env->uncached_asr = (env->uncached_asr & ~mask) | (val & mask);
65 void cpu_asr_write(CPUUniCore32State *env1, target_ulong val, target_ulong mask)
67 CPUUniCore32State *saved_env;
69 saved_env = env;
70 env = env1;
71 asr_write(val, mask);
72 env = saved_env;
75 void HELPER(asr_write)(target_ulong val, target_ulong mask)
77 asr_write(val, mask);
80 /* Access to user mode registers from privileged modes. */
81 uint32_t HELPER(get_user_reg)(uint32_t regno)
83 uint32_t val;
85 if (regno == 29) {
86 val = env->banked_r29[0];
87 } else if (regno == 30) {
88 val = env->banked_r30[0];
89 } else {
90 val = env->regs[regno];
92 return val;
95 void HELPER(set_user_reg)(uint32_t regno, uint32_t val)
97 if (regno == 29) {
98 env->banked_r29[0] = val;
99 } else if (regno == 30) {
100 env->banked_r30[0] = val;
101 } else {
102 env->regs[regno] = val;
106 /* ??? Flag setting arithmetic is awkward because we need to do comparisons.
107 The only way to do that in TCG is a conditional branch, which clobbers
108 all our temporaries. For now implement these as helper functions. */
110 uint32_t HELPER(add_cc)(uint32_t a, uint32_t b)
112 uint32_t result;
113 result = a + b;
114 env->NF = env->ZF = result;
115 env->CF = result < a;
116 env->VF = (a ^ b ^ -1) & (a ^ result);
117 return result;
120 uint32_t HELPER(adc_cc)(uint32_t a, uint32_t b)
122 uint32_t result;
123 if (!env->CF) {
124 result = a + b;
125 env->CF = result < a;
126 } else {
127 result = a + b + 1;
128 env->CF = result <= a;
130 env->VF = (a ^ b ^ -1) & (a ^ result);
131 env->NF = env->ZF = result;
132 return result;
135 uint32_t HELPER(sub_cc)(uint32_t a, uint32_t b)
137 uint32_t result;
138 result = a - b;
139 env->NF = env->ZF = result;
140 env->CF = a >= b;
141 env->VF = (a ^ b) & (a ^ result);
142 return result;
145 uint32_t HELPER(sbc_cc)(uint32_t a, uint32_t b)
147 uint32_t result;
148 if (!env->CF) {
149 result = a - b - 1;
150 env->CF = a > b;
151 } else {
152 result = a - b;
153 env->CF = a >= b;
155 env->VF = (a ^ b) & (a ^ result);
156 env->NF = env->ZF = result;
157 return result;
160 /* Similarly for variable shift instructions. */
162 uint32_t HELPER(shl)(uint32_t x, uint32_t i)
164 int shift = i & 0xff;
165 if (shift >= 32) {
166 return 0;
168 return x << shift;
171 uint32_t HELPER(shr)(uint32_t x, uint32_t i)
173 int shift = i & 0xff;
174 if (shift >= 32) {
175 return 0;
177 return (uint32_t)x >> shift;
180 uint32_t HELPER(sar)(uint32_t x, uint32_t i)
182 int shift = i & 0xff;
183 if (shift >= 32) {
184 shift = 31;
186 return (int32_t)x >> shift;
189 uint32_t HELPER(shl_cc)(uint32_t x, uint32_t i)
191 int shift = i & 0xff;
192 if (shift >= 32) {
193 if (shift == 32) {
194 env->CF = x & 1;
195 } else {
196 env->CF = 0;
198 return 0;
199 } else if (shift != 0) {
200 env->CF = (x >> (32 - shift)) & 1;
201 return x << shift;
203 return x;
206 uint32_t HELPER(shr_cc)(uint32_t x, uint32_t i)
208 int shift = i & 0xff;
209 if (shift >= 32) {
210 if (shift == 32) {
211 env->CF = (x >> 31) & 1;
212 } else {
213 env->CF = 0;
215 return 0;
216 } else if (shift != 0) {
217 env->CF = (x >> (shift - 1)) & 1;
218 return x >> shift;
220 return x;
223 uint32_t HELPER(sar_cc)(uint32_t x, uint32_t i)
225 int shift = i & 0xff;
226 if (shift >= 32) {
227 env->CF = (x >> 31) & 1;
228 return (int32_t)x >> 31;
229 } else if (shift != 0) {
230 env->CF = (x >> (shift - 1)) & 1;
231 return (int32_t)x >> shift;
233 return x;
236 uint32_t HELPER(ror_cc)(uint32_t x, uint32_t i)
238 int shift1, shift;
239 shift1 = i & 0xff;
240 shift = shift1 & 0x1f;
241 if (shift == 0) {
242 if (shift1 != 0) {
243 env->CF = (x >> 31) & 1;
245 return x;
246 } else {
247 env->CF = (x >> (shift - 1)) & 1;
248 return ((uint32_t)x >> shift) | (x << (32 - shift));