blob: e0a15882d3572d1cb6c6fcd46373a1ec121b38f0 [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 */
Peter Maydell5af98cc2016-01-26 18:17:01 +000011#include "qemu/osdep.h"
Blue Swirl3e457172011-07-13 12:44:15 +000012#include "cpu.h"
Richard Henderson2ef61752014-04-07 22:31:41 -070013#include "exec/helper-proto.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010014#include "exec/exec-all.h"
Paolo Bonzinif08b6172014-03-28 19:42:10 +010015#include "exec/cpu_ldst.h"
Guan Xuetao6e64da32011-04-12 16:25:59 +080016
17#define SIGNBIT (uint32_t)0x80000000
18#define SIGNBIT64 ((uint64_t)1 << 63)
19
Blue Swirl04a130e2012-09-02 07:42:33 +000020void HELPER(exception)(CPUUniCore32State *env, uint32_t excp)
Guan Xuetao6e64da32011-04-12 16:25:59 +080021{
Andreas Färber27103422013-08-26 08:31:06 +020022 CPUState *cs = CPU(uc32_env_get_cpu(env));
23
24 cs->exception_index = excp;
Andreas Färber5638d182013-08-27 17:52:12 +020025 cpu_loop_exit(cs);
Guan Xuetao6e64da32011-04-12 16:25:59 +080026}
27
Blue Swirl04a130e2012-09-02 07:42:33 +000028static target_ulong asr_read(CPUUniCore32State *env)
Guan Xuetao6e64da32011-04-12 16:25:59 +080029{
30 int ZF;
31 ZF = (env->ZF == 0);
32 return env->uncached_asr | (env->NF & 0x80000000) | (ZF << 30) |
33 (env->CF << 29) | ((env->VF & 0x80000000) >> 3);
34}
35
Blue Swirl04a130e2012-09-02 07:42:33 +000036target_ulong cpu_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 +000041target_ulong HELPER(asr_read)(CPUUniCore32State *env)
Guan Xuetao6e64da32011-04-12 16:25:59 +080042{
Blue Swirl04a130e2012-09-02 07:42:33 +000043 return asr_read(env);
Guan Xuetao6e64da32011-04-12 16:25:59 +080044}
45
Blue Swirl04a130e2012-09-02 07:42:33 +000046static void asr_write(CPUUniCore32State *env, target_ulong val,
47 target_ulong mask)
Guan Xuetao6e64da32011-04-12 16:25:59 +080048{
49 if (mask & ASR_NZCV) {
50 env->ZF = (~val) & ASR_Z;
51 env->NF = val;
52 env->CF = (val >> 29) & 1;
53 env->VF = (val << 3) & 0x80000000;
54 }
55
56 if ((env->uncached_asr ^ val) & mask & ASR_M) {
57 switch_mode(env, val & ASR_M);
58 }
59 mask &= ~ASR_NZCV;
60 env->uncached_asr = (env->uncached_asr & ~mask) | (val & mask);
61}
62
Blue Swirl04a130e2012-09-02 07:42:33 +000063void cpu_asr_write(CPUUniCore32State *env, target_ulong val, target_ulong mask)
Guan Xuetao6e64da32011-04-12 16:25:59 +080064{
Blue Swirl04a130e2012-09-02 07:42:33 +000065 asr_write(env, val, mask);
Guan Xuetao6e64da32011-04-12 16:25:59 +080066}
67
Blue Swirl04a130e2012-09-02 07:42:33 +000068void HELPER(asr_write)(CPUUniCore32State *env, target_ulong val,
69 target_ulong mask)
Guan Xuetao6e64da32011-04-12 16:25:59 +080070{
Blue Swirl04a130e2012-09-02 07:42:33 +000071 asr_write(env, val, mask);
Guan Xuetao6e64da32011-04-12 16:25:59 +080072}
73
74/* Access to user mode registers from privileged modes. */
Blue Swirl04a130e2012-09-02 07:42:33 +000075uint32_t HELPER(get_user_reg)(CPUUniCore32State *env, uint32_t regno)
Guan Xuetao6e64da32011-04-12 16:25:59 +080076{
77 uint32_t val;
78
79 if (regno == 29) {
80 val = env->banked_r29[0];
81 } else if (regno == 30) {
82 val = env->banked_r30[0];
83 } else {
84 val = env->regs[regno];
85 }
86 return val;
87}
88
Blue Swirl04a130e2012-09-02 07:42:33 +000089void HELPER(set_user_reg)(CPUUniCore32State *env, uint32_t regno, uint32_t val)
Guan Xuetao6e64da32011-04-12 16:25:59 +080090{
91 if (regno == 29) {
92 env->banked_r29[0] = val;
93 } else if (regno == 30) {
94 env->banked_r30[0] = val;
95 } else {
96 env->regs[regno] = val;
97 }
98}
99
100/* ??? Flag setting arithmetic is awkward because we need to do comparisons.
101 The only way to do that in TCG is a conditional branch, which clobbers
102 all our temporaries. For now implement these as helper functions. */
103
Blue Swirl04a130e2012-09-02 07:42:33 +0000104uint32_t HELPER(add_cc)(CPUUniCore32State *env, uint32_t a, uint32_t b)
Guan Xuetao6e64da32011-04-12 16:25:59 +0800105{
106 uint32_t result;
107 result = a + b;
108 env->NF = env->ZF = result;
109 env->CF = result < a;
110 env->VF = (a ^ b ^ -1) & (a ^ result);
111 return result;
112}
113
Blue Swirl04a130e2012-09-02 07:42:33 +0000114uint32_t HELPER(adc_cc)(CPUUniCore32State *env, uint32_t a, uint32_t b)
Guan Xuetao6e64da32011-04-12 16:25:59 +0800115{
116 uint32_t result;
117 if (!env->CF) {
118 result = a + b;
119 env->CF = result < a;
120 } else {
121 result = a + b + 1;
122 env->CF = result <= a;
123 }
124 env->VF = (a ^ b ^ -1) & (a ^ result);
125 env->NF = env->ZF = result;
126 return result;
127}
128
Blue Swirl04a130e2012-09-02 07:42:33 +0000129uint32_t HELPER(sub_cc)(CPUUniCore32State *env, uint32_t a, uint32_t b)
Guan Xuetao6e64da32011-04-12 16:25:59 +0800130{
131 uint32_t result;
132 result = a - b;
133 env->NF = env->ZF = result;
134 env->CF = a >= b;
135 env->VF = (a ^ b) & (a ^ result);
136 return result;
137}
138
Blue Swirl04a130e2012-09-02 07:42:33 +0000139uint32_t HELPER(sbc_cc)(CPUUniCore32State *env, uint32_t a, uint32_t b)
Guan Xuetao6e64da32011-04-12 16:25:59 +0800140{
141 uint32_t result;
142 if (!env->CF) {
143 result = a - b - 1;
144 env->CF = a > b;
145 } else {
146 result = a - b;
147 env->CF = a >= b;
148 }
149 env->VF = (a ^ b) & (a ^ result);
150 env->NF = env->ZF = result;
151 return result;
152}
153
154/* Similarly for variable shift instructions. */
155
156uint32_t HELPER(shl)(uint32_t x, uint32_t i)
157{
158 int shift = i & 0xff;
159 if (shift >= 32) {
160 return 0;
161 }
162 return x << shift;
163}
164
165uint32_t HELPER(shr)(uint32_t x, uint32_t i)
166{
167 int shift = i & 0xff;
168 if (shift >= 32) {
169 return 0;
170 }
171 return (uint32_t)x >> shift;
172}
173
174uint32_t HELPER(sar)(uint32_t x, uint32_t i)
175{
176 int shift = i & 0xff;
177 if (shift >= 32) {
178 shift = 31;
179 }
180 return (int32_t)x >> shift;
181}
182
Blue Swirl04a130e2012-09-02 07:42:33 +0000183uint32_t HELPER(shl_cc)(CPUUniCore32State *env, uint32_t x, uint32_t i)
Guan Xuetao6e64da32011-04-12 16:25:59 +0800184{
185 int shift = i & 0xff;
186 if (shift >= 32) {
187 if (shift == 32) {
188 env->CF = x & 1;
189 } else {
190 env->CF = 0;
191 }
192 return 0;
193 } else if (shift != 0) {
194 env->CF = (x >> (32 - shift)) & 1;
195 return x << shift;
196 }
197 return x;
198}
199
Blue Swirl04a130e2012-09-02 07:42:33 +0000200uint32_t HELPER(shr_cc)(CPUUniCore32State *env, uint32_t x, uint32_t i)
Guan Xuetao6e64da32011-04-12 16:25:59 +0800201{
202 int shift = i & 0xff;
203 if (shift >= 32) {
204 if (shift == 32) {
205 env->CF = (x >> 31) & 1;
206 } else {
207 env->CF = 0;
208 }
209 return 0;
210 } else if (shift != 0) {
211 env->CF = (x >> (shift - 1)) & 1;
212 return x >> shift;
213 }
214 return x;
215}
216
Blue Swirl04a130e2012-09-02 07:42:33 +0000217uint32_t HELPER(sar_cc)(CPUUniCore32State *env, uint32_t x, uint32_t i)
Guan Xuetao6e64da32011-04-12 16:25:59 +0800218{
219 int shift = i & 0xff;
220 if (shift >= 32) {
221 env->CF = (x >> 31) & 1;
222 return (int32_t)x >> 31;
223 } else if (shift != 0) {
224 env->CF = (x >> (shift - 1)) & 1;
225 return (int32_t)x >> shift;
226 }
227 return x;
228}
229
Blue Swirl04a130e2012-09-02 07:42:33 +0000230uint32_t HELPER(ror_cc)(CPUUniCore32State *env, uint32_t x, uint32_t i)
Guan Xuetao6e64da32011-04-12 16:25:59 +0800231{
232 int shift1, shift;
233 shift1 = i & 0xff;
234 shift = shift1 & 0x1f;
235 if (shift == 0) {
236 if (shift1 != 0) {
237 env->CF = (x >> 31) & 1;
238 }
239 return x;
240 } else {
241 env->CF = (x >> (shift - 1)) & 1;
242 return ((uint32_t)x >> shift) | (x << (32 - shift));
243 }
244}
Guan Xuetao4f23a1e2012-08-10 14:42:21 +0800245
246#ifndef CONFIG_USER_ONLY
Laurent Vivier98670d42018-01-18 20:38:40 +0100247void tlb_fill(CPUState *cs, target_ulong addr, int size,
248 MMUAccessType access_type, int mmu_idx, uintptr_t retaddr)
Guan Xuetao4f23a1e2012-08-10 14:42:21 +0800249{
Guan Xuetaof3ccc322012-08-10 14:42:25 +0800250 int ret;
251
Laurent Vivier98670d42018-01-18 20:38:40 +0100252 ret = uc32_cpu_handle_mmu_fault(cs, addr, size, access_type, mmu_idx);
Guan Xuetaof3ccc322012-08-10 14:42:25 +0800253 if (unlikely(ret)) {
Alex Bennée65255e82017-11-14 11:25:35 +0100254 /* now we have a real cpu fault */
255 cpu_loop_exit_restore(cs, retaddr);
Guan Xuetaof3ccc322012-08-10 14:42:25 +0800256 }
Guan Xuetao4f23a1e2012-08-10 14:42:21 +0800257}
258#endif