blob: 6443ffec1c4b78a4ae4c3bd6ad4fad4e18088ea0 [file] [log] [blame]
Guan Xuetao6e64da32011-04-12 16:25:59 +08001/*
2 * UniCore32 helper routines
3 *
Guan Xuetao4f23a1e2012-08-10 14:42:21 +08004 * Copyright (C) 2010-2012 Guan Xuetao
Guan Xuetao6e64da32011-04-12 16:25:59 +08005 *
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
Andreas Färber2b3bc6c2012-03-13 16:48:19 +01008 * published by the Free Software Foundation, or (at your option) any
9 * later version. See the COPYING file in the top-level directory.
Guan Xuetao6e64da32011-04-12 16:25:59 +080010 */
Blue Swirl3e457172011-07-13 12:44:15 +000011#include "cpu.h"
Guan Xuetao6e64da32011-04-12 16:25:59 +080012#include "helper.h"
13
14#define SIGNBIT (uint32_t)0x80000000
15#define SIGNBIT64 ((uint64_t)1 << 63)
16
Blue Swirl04a130e2012-09-02 07:42:33 +000017void HELPER(exception)(CPUUniCore32State *env, uint32_t excp)
Guan Xuetao6e64da32011-04-12 16:25:59 +080018{
19 env->exception_index = excp;
Blue Swirl1162c042011-05-14 12:52:35 +000020 cpu_loop_exit(env);
Guan Xuetao6e64da32011-04-12 16:25:59 +080021}
22
Blue Swirl04a130e2012-09-02 07:42:33 +000023static target_ulong asr_read(CPUUniCore32State *env)
Guan Xuetao6e64da32011-04-12 16:25:59 +080024{
25 int ZF;
26 ZF = (env->ZF == 0);
27 return env->uncached_asr | (env->NF & 0x80000000) | (ZF << 30) |
28 (env->CF << 29) | ((env->VF & 0x80000000) >> 3);
29}
30
Blue Swirl04a130e2012-09-02 07:42:33 +000031target_ulong cpu_asr_read(CPUUniCore32State *env)
Guan Xuetao6e64da32011-04-12 16:25:59 +080032{
Blue Swirl04a130e2012-09-02 07:42:33 +000033 return asr_read(env);
Guan Xuetao6e64da32011-04-12 16:25:59 +080034}
35
Blue Swirl04a130e2012-09-02 07:42:33 +000036target_ulong HELPER(asr_read)(CPUUniCore32State *env)
Guan Xuetao6e64da32011-04-12 16:25:59 +080037{
Blue Swirl04a130e2012-09-02 07:42:33 +000038 return asr_read(env);
Guan Xuetao6e64da32011-04-12 16:25:59 +080039}
40
Blue Swirl04a130e2012-09-02 07:42:33 +000041static void asr_write(CPUUniCore32State *env, target_ulong val,
42 target_ulong mask)
Guan Xuetao6e64da32011-04-12 16:25:59 +080043{
44 if (mask & ASR_NZCV) {
45 env->ZF = (~val) & ASR_Z;
46 env->NF = val;
47 env->CF = (val >> 29) & 1;
48 env->VF = (val << 3) & 0x80000000;
49 }
50
51 if ((env->uncached_asr ^ val) & mask & ASR_M) {
52 switch_mode(env, val & ASR_M);
53 }
54 mask &= ~ASR_NZCV;
55 env->uncached_asr = (env->uncached_asr & ~mask) | (val & mask);
56}
57
Blue Swirl04a130e2012-09-02 07:42:33 +000058void cpu_asr_write(CPUUniCore32State *env, target_ulong val, target_ulong mask)
Guan Xuetao6e64da32011-04-12 16:25:59 +080059{
Blue Swirl04a130e2012-09-02 07:42:33 +000060 asr_write(env, val, mask);
Guan Xuetao6e64da32011-04-12 16:25:59 +080061}
62
Blue Swirl04a130e2012-09-02 07:42:33 +000063void HELPER(asr_write)(CPUUniCore32State *env, target_ulong val,
64 target_ulong mask)
Guan Xuetao6e64da32011-04-12 16:25:59 +080065{
Blue Swirl04a130e2012-09-02 07:42:33 +000066 asr_write(env, val, mask);
Guan Xuetao6e64da32011-04-12 16:25:59 +080067}
68
69/* Access to user mode registers from privileged modes. */
Blue Swirl04a130e2012-09-02 07:42:33 +000070uint32_t HELPER(get_user_reg)(CPUUniCore32State *env, uint32_t regno)
Guan Xuetao6e64da32011-04-12 16:25:59 +080071{
72 uint32_t val;
73
74 if (regno == 29) {
75 val = env->banked_r29[0];
76 } else if (regno == 30) {
77 val = env->banked_r30[0];
78 } else {
79 val = env->regs[regno];
80 }
81 return val;
82}
83
Blue Swirl04a130e2012-09-02 07:42:33 +000084void HELPER(set_user_reg)(CPUUniCore32State *env, uint32_t regno, uint32_t val)
Guan Xuetao6e64da32011-04-12 16:25:59 +080085{
86 if (regno == 29) {
87 env->banked_r29[0] = val;
88 } else if (regno == 30) {
89 env->banked_r30[0] = val;
90 } else {
91 env->regs[regno] = val;
92 }
93}
94
95/* ??? Flag setting arithmetic is awkward because we need to do comparisons.
96 The only way to do that in TCG is a conditional branch, which clobbers
97 all our temporaries. For now implement these as helper functions. */
98
Blue Swirl04a130e2012-09-02 07:42:33 +000099uint32_t HELPER(add_cc)(CPUUniCore32State *env, uint32_t a, uint32_t b)
Guan Xuetao6e64da32011-04-12 16:25:59 +0800100{
101 uint32_t result;
102 result = a + b;
103 env->NF = env->ZF = result;
104 env->CF = result < a;
105 env->VF = (a ^ b ^ -1) & (a ^ result);
106 return result;
107}
108
Blue Swirl04a130e2012-09-02 07:42:33 +0000109uint32_t HELPER(adc_cc)(CPUUniCore32State *env, uint32_t a, uint32_t b)
Guan Xuetao6e64da32011-04-12 16:25:59 +0800110{
111 uint32_t result;
112 if (!env->CF) {
113 result = a + b;
114 env->CF = result < a;
115 } else {
116 result = a + b + 1;
117 env->CF = result <= a;
118 }
119 env->VF = (a ^ b ^ -1) & (a ^ result);
120 env->NF = env->ZF = result;
121 return result;
122}
123
Blue Swirl04a130e2012-09-02 07:42:33 +0000124uint32_t HELPER(sub_cc)(CPUUniCore32State *env, uint32_t a, uint32_t b)
Guan Xuetao6e64da32011-04-12 16:25:59 +0800125{
126 uint32_t result;
127 result = a - b;
128 env->NF = env->ZF = result;
129 env->CF = a >= b;
130 env->VF = (a ^ b) & (a ^ result);
131 return result;
132}
133
Blue Swirl04a130e2012-09-02 07:42:33 +0000134uint32_t HELPER(sbc_cc)(CPUUniCore32State *env, uint32_t a, uint32_t b)
Guan Xuetao6e64da32011-04-12 16:25:59 +0800135{
136 uint32_t result;
137 if (!env->CF) {
138 result = a - b - 1;
139 env->CF = a > b;
140 } else {
141 result = a - b;
142 env->CF = a >= b;
143 }
144 env->VF = (a ^ b) & (a ^ result);
145 env->NF = env->ZF = result;
146 return result;
147}
148
149/* Similarly for variable shift instructions. */
150
151uint32_t HELPER(shl)(uint32_t x, uint32_t i)
152{
153 int shift = i & 0xff;
154 if (shift >= 32) {
155 return 0;
156 }
157 return x << shift;
158}
159
160uint32_t HELPER(shr)(uint32_t x, uint32_t i)
161{
162 int shift = i & 0xff;
163 if (shift >= 32) {
164 return 0;
165 }
166 return (uint32_t)x >> shift;
167}
168
169uint32_t HELPER(sar)(uint32_t x, uint32_t i)
170{
171 int shift = i & 0xff;
172 if (shift >= 32) {
173 shift = 31;
174 }
175 return (int32_t)x >> shift;
176}
177
Blue Swirl04a130e2012-09-02 07:42:33 +0000178uint32_t HELPER(shl_cc)(CPUUniCore32State *env, uint32_t x, uint32_t i)
Guan Xuetao6e64da32011-04-12 16:25:59 +0800179{
180 int shift = i & 0xff;
181 if (shift >= 32) {
182 if (shift == 32) {
183 env->CF = x & 1;
184 } else {
185 env->CF = 0;
186 }
187 return 0;
188 } else if (shift != 0) {
189 env->CF = (x >> (32 - shift)) & 1;
190 return x << shift;
191 }
192 return x;
193}
194
Blue Swirl04a130e2012-09-02 07:42:33 +0000195uint32_t HELPER(shr_cc)(CPUUniCore32State *env, uint32_t x, uint32_t i)
Guan Xuetao6e64da32011-04-12 16:25:59 +0800196{
197 int shift = i & 0xff;
198 if (shift >= 32) {
199 if (shift == 32) {
200 env->CF = (x >> 31) & 1;
201 } else {
202 env->CF = 0;
203 }
204 return 0;
205 } else if (shift != 0) {
206 env->CF = (x >> (shift - 1)) & 1;
207 return x >> shift;
208 }
209 return x;
210}
211
Blue Swirl04a130e2012-09-02 07:42:33 +0000212uint32_t HELPER(sar_cc)(CPUUniCore32State *env, uint32_t x, uint32_t i)
Guan Xuetao6e64da32011-04-12 16:25:59 +0800213{
214 int shift = i & 0xff;
215 if (shift >= 32) {
216 env->CF = (x >> 31) & 1;
217 return (int32_t)x >> 31;
218 } else if (shift != 0) {
219 env->CF = (x >> (shift - 1)) & 1;
220 return (int32_t)x >> shift;
221 }
222 return x;
223}
224
Blue Swirl04a130e2012-09-02 07:42:33 +0000225uint32_t HELPER(ror_cc)(CPUUniCore32State *env, uint32_t x, uint32_t i)
Guan Xuetao6e64da32011-04-12 16:25:59 +0800226{
227 int shift1, shift;
228 shift1 = i & 0xff;
229 shift = shift1 & 0x1f;
230 if (shift == 0) {
231 if (shift1 != 0) {
232 env->CF = (x >> 31) & 1;
233 }
234 return x;
235 } else {
236 env->CF = (x >> (shift - 1)) & 1;
237 return ((uint32_t)x >> shift) | (x << (32 - shift));
238 }
239}
Guan Xuetao4f23a1e2012-08-10 14:42:21 +0800240
241#ifndef CONFIG_USER_ONLY
242#define MMUSUFFIX _mmu
243
244#define SHIFT 0
Paolo Bonzini022c62c2012-12-17 18:19:49 +0100245#include "exec/softmmu_template.h"
Guan Xuetao4f23a1e2012-08-10 14:42:21 +0800246
247#define SHIFT 1
Paolo Bonzini022c62c2012-12-17 18:19:49 +0100248#include "exec/softmmu_template.h"
Guan Xuetao4f23a1e2012-08-10 14:42:21 +0800249
250#define SHIFT 2
Paolo Bonzini022c62c2012-12-17 18:19:49 +0100251#include "exec/softmmu_template.h"
Guan Xuetao4f23a1e2012-08-10 14:42:21 +0800252
253#define SHIFT 3
Paolo Bonzini022c62c2012-12-17 18:19:49 +0100254#include "exec/softmmu_template.h"
Guan Xuetao4f23a1e2012-08-10 14:42:21 +0800255
Blue Swirl04a130e2012-09-02 07:42:33 +0000256void tlb_fill(CPUUniCore32State *env, target_ulong addr, int is_write,
257 int mmu_idx, uintptr_t retaddr)
Guan Xuetao4f23a1e2012-08-10 14:42:21 +0800258{
Guan Xuetaof3ccc322012-08-10 14:42:25 +0800259 int ret;
260
Guan Xuetaof3ccc322012-08-10 14:42:25 +0800261 ret = uc32_cpu_handle_mmu_fault(env, addr, is_write, mmu_idx);
262 if (unlikely(ret)) {
263 if (retaddr) {
264 /* now we have a real cpu fault */
Blue Swirla8a826a2012-12-04 20:16:07 +0000265 cpu_restore_state(env, retaddr);
Guan Xuetaof3ccc322012-08-10 14:42:25 +0800266 }
267 cpu_loop_exit(env);
268 }
Guan Xuetao4f23a1e2012-08-10 14:42:21 +0800269}
270#endif