Skip to contentSkip to conversation

Qemu Openrisc support

PATCH Superseded Jia Liu 6 participants 56 messages May 17, 2012 – May 26, 2012 Kbuild Page 1 of 2

v1 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

Jia Liu

author
#0 lore raw

cover Qemu Openrisc support

This is the Opencores Openrisc 1200 support for Qemu.
Full implementation of the system-model and linux-user-model support.

Openrisc 1200 is a Opencores Open Source CPU,
its Architecture Manual can be find at
http://opencores.org/svnget,or1k?file=/trunk/docs/openrisc_arch.pdf

---

Version History:

V1:
- add Qemu Openrisc support.
- well tested on x64 machine, and final tested x86 machine.

Jia Liu (15):
Openrisc: add target stub
Openrisc: add MMU support
Openrisc: add instructions translation
Openrisc: add interrupt support
Openrisc: add exception support
Openrisc: add int instruction helpers
Openrisc: add float instruction helpers
Openrisc: add programmable interrupt controller support
Openrisc: add timer support
Openrisc: add a simulation board
Openrisc: add system instruction helpers
Openrisc: add gdb stub support
Openrisc: add linux syscall, signal and termbits
Openrisc: add linux user support
Openrisc: add testcases

 Makefile.target                     |    7 +
 arch_init.c                         |    2 +
 arch_init.h                         |    1 +
 configure                           |    9 +-
 cpu-exec.c                          |   19 +
 default-configs/or32-linux-user.mak |    2 +
 default-configs/or32-softmmu.mak    |    6 +
 elf.h                               |    2 +
 gdbstub.c                           |   64 ++
 hw/openrisc_cpudev.h                |   30 +
 hw/openrisc_pic.c                   |   79 ++
 hw/openrisc_sim.c                   |  140 ++++
 hw/openrisc_timer.c                 |  153 ++++
 linux-user/elfload.c                |   41 +
 linux-user/main.c                   |  100 +++
 linux-user/openrisc/syscall.h       |   24 +
 linux-user/openrisc/syscall_nr.h    |  506 ++++++++++++
 linux-user/openrisc/target_signal.h |   26 +
 linux-user/openrisc/termbits.h      |  294 +++++++
 linux-user/signal.c                 |  229 ++++++
 linux-user/syscall.c                |    2 +-
 linux-user/syscall_defs.h           |   40 +-
 poison.h                            |    1 +
 target-openrisc/cpu-qom.h           |   70 ++
 target-openrisc/cpu.c               |   74 ++
 target-openrisc/cpu.h               |  299 +++++++
 target-openrisc/excp.c              |   27 +
 target-openrisc/excp.h              |   28 +
 target-openrisc/excp_helper.c       |   28 +
 target-openrisc/fpu_helper.c        |   93 +++
 target-openrisc/helper.c            |  109 +++
 target-openrisc/helper.h            |   46 ++
 target-openrisc/int_helper.c        |  126 +++
 target-openrisc/intrp_helper.c      |   53 ++
 target-openrisc/machine.c           |   76 ++
 target-openrisc/mem.c               |  261 ++++++
 target-openrisc/mem_helper.c        |   71 ++
 target-openrisc/sys_helper.c        |  227 ++++++
 target-openrisc/translate.c         | 1484 +++++++++++++++++++++++++++++++++++
 tests/tcg/openrisc/Makefile         |   73 ++
 tests/tcg/openrisc/test_add.c       |   34 +
 tests/tcg/openrisc/test_addc.c      |   37 +
 tests/tcg/openrisc/test_addi.c      |   31 +
 tests/tcg/openrisc/test_addic.c     |   32 +
 tests/tcg/openrisc/test_and_or.c    |   61 ++
 tests/tcg/openrisc/test_bf.c        |   46 ++
 tests/tcg/openrisc/test_bnf.c       |   50 ++
 tests/tcg/openrisc/test_div.c       |   32 +
 tests/tcg/openrisc/test_extx.c      |   71 ++
 tests/tcg/openrisc/test_fx.c        |   53 ++
 tests/tcg/openrisc/test_j.c         |   26 +
 tests/tcg/openrisc/test_jal.c       |   26 +
 tests/tcg/openrisc/test_lf_add.c    |   32 +
 tests/tcg/openrisc/test_lf_div.c    |   33 +
 tests/tcg/openrisc/test_lf_eqd.c    |   44 ++
 tests/tcg/openrisc/test_lf_eqs.c    |   43 +
 tests/tcg/openrisc/test_lf_ged.c    |   45 ++
 tests/tcg/openrisc/test_lf_ges.c    |   45 ++
 tests/tcg/openrisc/test_lf_gtd.c    |   45 ++
 tests/tcg/openrisc/test_lf_gts.c    |   45 ++
 tests/tcg/openrisc/test_lf_led.c    |   43 +
 tests/tcg/openrisc/test_lf_les.c    |   45 ++
 tests/tcg/openrisc/test_lf_ltd.c    |   45 ++
 tests/tcg/openrisc/test_lf_lts.c    |   45 ++
 tests/tcg/openrisc/test_lf_mul.c    |   22 +
 tests/tcg/openrisc/test_lf_ned.c    |   46 ++
 tests/tcg/openrisc/test_lf_nes.c    |   46 ++
 tests/tcg/openrisc/test_lf_rem.c    |   31 +
 tests/tcg/openrisc/test_lf_sub.c    |   31 +
 tests/tcg/openrisc/test_logic.c     |  100 +++
 tests/tcg/openrisc/test_lx.c        |   78 ++
 tests/tcg/openrisc/test_movhi.c     |   30 +
 tests/tcg/openrisc/test_mul.c       |   47 ++
 tests/tcg/openrisc/test_sfeq.c      |   43 +
 tests/tcg/openrisc/test_sfeqi.c     |   39 +
 tests/tcg/openrisc/test_sfges.c     |   44 ++
 tests/tcg/openrisc/test_sfgesi.c    |   40 +
 tests/tcg/openrisc/test_sfgeu.c     |   44 ++
 tests/tcg/openrisc/test_sfgeui.c    |   41 +
 tests/tcg/openrisc/test_sfgts.c     |   45 ++
 tests/tcg/openrisc/test_sfgtsi.c    |   41 +
 tests/tcg/openrisc/test_sfgtu.c     |   43 +
 tests/tcg/openrisc/test_sfgtui.c    |   42 +
 tests/tcg/openrisc/test_sfles.c     |   26 +
 tests/tcg/openrisc/test_sflesi.c    |   39 +
 tests/tcg/openrisc/test_sfleu.c     |   43 +
 tests/tcg/openrisc/test_sfleui.c    |   39 +
 tests/tcg/openrisc/test_sflts.c     |   43 +
 tests/tcg/openrisc/test_sfltsi.c    |   38 +
 tests/tcg/openrisc/test_sfltu.c     |   41 +
 tests/tcg/openrisc/test_sfltui.c    |   39 +
 tests/tcg/openrisc/test_sfne.c      |   43 +
 tests/tcg/openrisc/test_sfnei.c     |   38 +
 93 files changed, 7168 insertions(+), 5 deletions(-)
 create mode 100644 default-configs/or32-linux-user.mak
 create mode 100644 default-configs/or32-softmmu.mak
 create mode 100644 hw/openrisc_cpudev.h
 create mode 100644 hw/openrisc_pic.c
 create mode 100644 hw/openrisc_sim.c
 create mode 100644 hw/openrisc_timer.c
 create mode 100644 linux-user/openrisc/syscall.h
 create mode 100644 linux-user/openrisc/syscall_nr.h
 create mode 100644 linux-user/openrisc/target_signal.h
 create mode 100644 linux-user/openrisc/termbits.h
 create mode 100644 target-openrisc/cpu-qom.h
 create mode 100644 target-openrisc/cpu.c
 create mode 100644 target-openrisc/cpu.h
 create mode 100644 target-openrisc/excp.c
 create mode 100644 target-openrisc/excp.h
 create mode 100644 target-openrisc/excp_helper.c
 create mode 100644 target-openrisc/fpu_helper.c
 create mode 100644 target-openrisc/helper.c
 create mode 100644 target-openrisc/helper.h
 create mode 100644 target-openrisc/int_helper.c
 create mode 100644 target-openrisc/intrp_helper.c
 create mode 100644 target-openrisc/machine.c
 create mode 100644 target-openrisc/mem.c
 create mode 100644 target-openrisc/mem_helper.c
 create mode 100644 target-openrisc/sys_helper.c
 create mode 100644 target-openrisc/translate.c
 create mode 100644 tests/tcg/openrisc/Makefile
 create mode 100644 tests/tcg/openrisc/test_add.c
 create mode 100644 tests/tcg/openrisc/test_addc.c
 create mode 100644 tests/tcg/openrisc/test_addi.c
 create mode 100644 tests/tcg/openrisc/test_addic.c
 create mode 100644 tests/tcg/openrisc/test_and_or.c
 create mode 100644 tests/tcg/openrisc/test_bf.c
 create mode 100644 tests/tcg/openrisc/test_bnf.c
 create mode 100644 tests/tcg/openrisc/test_div.c
 create mode 100644 tests/tcg/openrisc/test_extx.c
 create mode 100644 tests/tcg/openrisc/test_fx.c
 create mode 100644 tests/tcg/openrisc/test_j.c
 create mode 100644 tests/tcg/openrisc/test_jal.c
 create mode 100644 tests/tcg/openrisc/test_lf_add.c
 create mode 100644 tests/tcg/openrisc/test_lf_div.c
 create mode 100644 tests/tcg/openrisc/test_lf_eqd.c
 create mode 100644 tests/tcg/openrisc/test_lf_eqs.c
 create mode 100644 tests/tcg/openrisc/test_lf_ged.c
 create mode 100644 tests/tcg/openrisc/test_lf_ges.c
 create mode 100644 tests/tcg/openrisc/test_lf_gtd.c
 create mode 100644 tests/tcg/openrisc/test_lf_gts.c
 create mode 100644 tests/tcg/openrisc/test_lf_led.c
 create mode 100644 tests/tcg/openrisc/test_lf_les.c
 create mode 100644 tests/tcg/openrisc/test_lf_ltd.c
 create mode 100644 tests/tcg/openrisc/test_lf_lts.c
 create mode 100644 tests/tcg/openrisc/test_lf_mul.c
 create mode 100644 tests/tcg/openrisc/test_lf_ned.c
 create mode 100644 tests/tcg/openrisc/test_lf_nes.c
 create mode 100644 tests/tcg/openrisc/test_lf_rem.c
 create mode 100644 tests/tcg/openrisc/test_lf_sub.c
 create mode 100644 tests/tcg/openrisc/test_logic.c
 create mode 100644 tests/tcg/openrisc/test_lx.c
 create mode 100644 tests/tcg/openrisc/test_movhi.c
 create mode 100644 tests/tcg/openrisc/test_mul.c
 create mode 100644 tests/tcg/openrisc/test_sfeq.c
 create mode 100644 tests/tcg/openrisc/test_sfeqi.c
 create mode 100644 tests/tcg/openrisc/test_sfges.c
 create mode 100644 tests/tcg/openrisc/test_sfgesi.c
 create mode 100644 tests/tcg/openrisc/test_sfgeu.c
 create mode 100644 tests/tcg/openrisc/test_sfgeui.c
 create mode 100644 tests/tcg/openrisc/test_sfgts.c
 create mode 100644 tests/tcg/openrisc/test_sfgtsi.c
 create mode 100644 tests/tcg/openrisc/test_sfgtu.c
 create mode 100644 tests/tcg/openrisc/test_sfgtui.c
 create mode 100644 tests/tcg/openrisc/test_sfles.c
 create mode 100644 tests/tcg/openrisc/test_sflesi.c
 create mode 100644 tests/tcg/openrisc/test_sfleu.c
 create mode 100644 tests/tcg/openrisc/test_sfleui.c
 create mode 100644 tests/tcg/openrisc/test_sflts.c
 create mode 100644 tests/tcg/openrisc/test_sfltsi.c
 create mode 100644 tests/tcg/openrisc/test_sfltu.c
 create mode 100644 tests/tcg/openrisc/test_sfltui.c
 create mode 100644 tests/tcg/openrisc/test_sfne.c
 create mode 100644 tests/tcg/openrisc/test_sfnei.c
Signature · 1 line
1.7.9.5
author
#1 lore raw

PATCH 1/15 Openrisc: add target stub

add the openrisc target stub and basic implementation.

---
 Makefile.target                  |    3 +
 arch_init.c                      |    2 +
 arch_init.h                      |    1 +
 configure                        |    8 +-
 cpu-exec.c                       |    2 +
 default-configs/or32-softmmu.mak |    6 +
 elf.h                            |    2 +
 poison.h                         |    1 +
 target-openrisc/cpu-qom.h        |   70 +++++++++
 target-openrisc/cpu.c            |   74 ++++++++++
 target-openrisc/cpu.h            |  299 ++++++++++++++++++++++++++++++++++++++
 target-openrisc/helper.c         |   67 +++++++++
 target-openrisc/helper.h         |   23 +++
 target-openrisc/machine.c        |   76 ++++++++++
 target-openrisc/mem.c            |   43 ++++++
 target-openrisc/mem_helper.c     |   46 ++++++
 target-openrisc/translate.c      |   90 ++++++++++++
 17 files changed, 812 insertions(+), 1 deletion(-)
 create mode 100644 default-configs/or32-softmmu.mak
 create mode 100644 target-openrisc/cpu-qom.h
 create mode 100644 target-openrisc/cpu.c
 create mode 100644 target-openrisc/cpu.h
 create mode 100644 target-openrisc/helper.c
 create mode 100644 target-openrisc/helper.h
 create mode 100644 target-openrisc/machine.c
 create mode 100644 target-openrisc/mem.c
 create mode 100644 target-openrisc/mem_helper.c
 create mode 100644 target-openrisc/translate.c
Makefile.target +3 -0
index 1582904..0ffb29c 100644
@@ -83,9 +83,11 @@ libobj-$(CONFIG_TCG_INTERPRETER) += tci.o
 libobj-y += fpu/softfloat.o
 ifneq ($(TARGET_BASE_ARCH), sparc)
 ifneq ($(TARGET_BASE_ARCH), alpha)
+ifneq ($(TARGET_BASE_ARCH), openrisc)
 libobj-y += op_helper.o
 endif
 endif
+endif
 libobj-y += helper.o
 ifneq ($(TARGET_BASE_ARCH), ppc)
 libobj-y += cpu.o
@@ -99,6 +101,7 @@ endif
 libobj-$(TARGET_SPARC) += int32_helper.o
 libobj-$(TARGET_SPARC64) += int64_helper.o
 libobj-$(TARGET_ALPHA) += int_helper.o fpu_helper.o sys_helper.o mem_helper.o
+libobj-$(TARGET_OPENRISC) += mem.o mem_helper.o
 
 libobj-y += disas.o
 libobj-$(CONFIG_TCI_DIS) += tci-dis.o
arch_init.c +2 -0
index 988adca..55b608d 100644
@@ -71,6 +71,8 @@ int graphic_depth = 15;
 #define QEMU_ARCH QEMU_ARCH_MICROBLAZE
 #elif defined(TARGET_MIPS)
 #define QEMU_ARCH QEMU_ARCH_MIPS
+#elif defined(TARGET_OPENRISC)
+#define QEMU_ARCH QEMU_ARCH_OPENRISC
 #elif defined(TARGET_PPC)
 #define QEMU_ARCH QEMU_ARCH_PPC
 #elif defined(TARGET_S390X)
arch_init.h +1 -0
index c7cb94a..3dfea3b 100644
@@ -16,6 +16,7 @@ enum {
     QEMU_ARCH_SH4 = 1024,
     QEMU_ARCH_SPARC = 2048,
     QEMU_ARCH_XTENSA = 4096,
+    QEMU_ARCH_OPENRISC = 8192,
 };
 
 extern const uint32_t arch_type;
configure +7 -1
index b55a792..63e2372 100755
@@ -924,6 +924,7 @@ mips-softmmu \
 mipsel-softmmu \
 mips64-softmmu \
 mips64el-softmmu \
+or32-softmmu \
 ppc-softmmu \
 ppcemb-softmmu \
 ppc64-softmmu \
@@ -3460,7 +3461,7 @@ target_arch2=`echo $target | cut -d '-' -f 1`
 target_bigendian="no"
 
 case "$target_arch2" in
-  armeb|lm32|m68k|microblaze|mips|mipsn32|mips64|ppc|ppcemb|ppc64|ppc64abi32|s390x|sh4eb|sparc|sparc64|sparc32plus|xtensaeb)
+  armeb|lm32|m68k|microblaze|mips|mipsn32|mips64|or32|ppc|ppcemb|ppc64|ppc64abi32|s390x|sh4eb|sparc|sparc64|sparc32plus|xtensaeb)
   target_bigendian=yes
   ;;
 esac
@@ -3588,6 +3589,11 @@ case "$target_arch2" in
     target_phys_bits=64
     target_long_alignment=8
   ;;
+  or32)
+    TARGET_ARCH=openrisc
+    TARGET_BASE_ARCH=openrisc
+    target_phys_bits=32
+  ;;
   ppc)
     gdb_xml_files="power-core.xml power-fpu.xml power-altivec.xml power-spe.xml"
     target_phys_bits=64
cpu-exec.c +2 -0
index 0344cd5..ba10db1 100644
@@ -222,6 +222,7 @@ int cpu_exec(CPUArchState *env)
 #elif defined(TARGET_LM32)
 #elif defined(TARGET_MICROBLAZE)
 #elif defined(TARGET_MIPS)
+#elif defined(TARGET_OPENRISC)
 #elif defined(TARGET_SH4)
 #elif defined(TARGET_CRIS)
 #elif defined(TARGET_S390X)
@@ -620,6 +621,7 @@ int cpu_exec(CPUArchState *env)
               | env->cc_dest | (env->cc_x << 4);
 #elif defined(TARGET_MICROBLAZE)
 #elif defined(TARGET_MIPS)
+#elif defined(TARGET_OPENRISC)
 #elif defined(TARGET_SH4)
 #elif defined(TARGET_ALPHA)
 #elif defined(TARGET_CRIS)
default-configs/or32-softmmu.mak new file +6 -0
new file mode 100644
index 0000000..7590eed
@@ -0,0 +1,6 @@
+# Default configuration for or32-softmmu
+
+include pci.mak
+CONFIG_SERIAL=y
+CONFIG_NE2000_ISA=y
+CONFIG_I8259=y
elf.h +2 -0
index e1422b8..e65f4b9 100644
@@ -106,6 +106,8 @@ typedef int64_t  Elf64_Sxword;
 #define EM_H8S          48      /* Hitachi H8S     */
 #define EM_LATTICEMICO32 138    /* LatticeMico32 */
 
+#define EM_OPENRISC     92        /* Opencores Openrisc */
+
 #define EM_UNICORE32    110     /* UniCore32 */
 
 /*
poison.h +1 -0
index d396f20..7d7b23b 100644
@@ -14,6 +14,7 @@
 #pragma GCC poison TARGET_M68K
 #pragma GCC poison TARGET_MIPS
 #pragma GCC poison TARGET_MIPS64
+#pragma GCC poison TARGET_OPENRISC
 #pragma GCC poison TARGET_PPC
 #pragma GCC poison TARGET_PPCEMB
 #pragma GCC poison TARGET_PPC64
target-openrisc/cpu-qom.h new file +70 -0

70 lines not on this page · raw message

target-openrisc/cpu.c new file +74 -0

74 lines not on this page · raw message

target-openrisc/cpu.h new file +299 -0

299 lines not on this page · raw message

target-openrisc/helper.c new file +67 -0

67 lines not on this page · raw message

target-openrisc/helper.h new file +23 -0
new file mode 100644
index 0000000..103d9b4
@@ -0,0 +1,23 @@
+/*
+ * OpenRISC helper defines
+ *
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
+ */
+
+#include "def-helper.h"
+
+#include "def-helper.h"
target-openrisc/machine.c new file +76 -0

76 lines not on this page · raw message

target-openrisc/mem.c new file +43 -0

43 lines not on this page · raw message

target-openrisc/mem_helper.c new file +46 -0

46 lines not on this page · raw message

target-openrisc/translate.c new file +90 -0

90 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#2 lore raw

Re: [Qemu-devel] [PATCH 01/15] Openrisc: add target stub

target-openrisc/helper.c line 54

Quoted 7 lines
target-openrisc/helper.c new file +7 -0
+CPUOPENRISCState *cpu_openrisc_init(const char *cpu_model)
+{
+    CPUOPENRISCState *env;
+    static int tcg_inited;
+
+    env = g_malloc0(sizeof(*env));
+    memset(env, 0, sizeof(*env));
       ^^^^^^

No need to memset env to zero, since g_malloc0 already did it for you.

Quoted 5 lines
target-openrisc/helper.h new file +0 -0
new file mode 100644
index 0000000..103d9b4

[snip]

target-openrisc/helper.h line 23

Quoted 3 lines
target-openrisc/helper.h new file +3 -0
+#include "def-helper.h"
+
+#include "def-helper.h"

I suppose you only need to #include "def-helper.h" once?

Regards,
chenwj

Signature · 5 lines
Wei-Ren Chen (陳韋任)
Computer Systems Lab, Institute of Information Science,
Academia Sinica, Taiwan (R.O.C.)
Tel:886-2-2788-3799 #1667
Homepage: http://people.cs.nctu.edu.tw/~chenwj
#3 lore raw

Re: [Qemu-devel] [PATCH 01/15] Openrisc: add target stub

In reply to Jia Liu

Am 17.05.2012 10:35, schrieb Jia Liu:

target-openrisc/cpu-qom.h

Quoted 9 lines

add the openrisc target stub and basic implementation.

---

target-openrisc/cpu-qom.h new file +0 -0
new file mode 100644
index 0000000..8c936df

First of all, if you design your new target cleanly, there's no strict need for a cpu-qom.h header - it mainly served to separate new QOM code from legacy code. If you want, you can put the code directly into cpu.h just as well.

target-openrisc/cpu-qom.h line 4

Quoted 5 lines
target-openrisc/cpu-qom.h new file +4 -0
@@ -0,0 +1,70 @@
+/*
+ *  QEMU Openrisc CPU
+ *
+ *  Copyright (c) 2012 Jia Liu <[email protected]>

Minor nitpick: I guess this was copied from some other header? Uses a two-space indentation here and one-space below.

Quoted 11 lines
target-openrisc/cpu-qom.h new file +11 -0
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *

target-openrisc/cpu-qom.h line 24

Quoted 10 lines
target-openrisc/cpu-qom.h new file +9 -0
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef QEMU_OPENRISC_CPU_QOM_H
+#define QEMU_OPENRISC_CPU_QOM_H
+
+#include "qemu/cpu.h"
 
+#include "cpu.h"

Please don't. This was a big mistake of mine that I've been correcting on the qom-next branch, onto which you should rebase a new target such as this by the way. It leads to really ugly problems when someone includes cpu-qom.h and the new struct below is not yet defined for functions using it there.

Quoted 13 lines
target-openrisc/cpu-qom.h new file +13 -0
+
+#define TYPE_OPENRISC_CPU "or32"
+
+#define OPENRISC_CPU_CLASS(klass) \
+    OBJECT_CLASS_CHECK(OPENRISCCPUClass, (klass), TYPE_OPENRISC_CPU)
+#define OPENRISC_CPU(obj) \
+    OBJECT_CHECK(OPENRISCCPU, (obj), TYPE_OPENRISC_CPU)
+#define OPENRISC_CPU_GET_CLASS(obj) \
+    OBJECT_GET_CLASS(OPENRISCCPUClass, (obj), TYPE_OPENRISC_CPU)
+
+/**
+ * OPENRISCCPUClass:
+ * @parent_reset: The parent class' reset handler.

target-openrisc/cpu-qom.h line 47

Quoted 10 lines
target-openrisc/cpu-qom.h new file +10 -0
+ *
+ * A Openrisc CPU model.
+ */
+typedef struct OPENRISCCPUClass {
+    /*< private >*/
+    CPUClass parent_class;
+    /*< public >*/
+
+    void (*parent_reset)(CPUState *cpu);
+} OPENRISCCPUClass;

Pleave avoid unnecessary uppercase spelling: OpenRISCCPUClass? That distinguishes it from the all-uppercase cast macros.

Or OpenriscCPUClass as you spell it elsewhere?

Quoted 4 lines
target-openrisc/cpu-qom.h new file +4 -0
+
+/**
+ * OPENRISCCPU:
+ * @env: #CPUOPENRISCState

target-openrisc/cpu-qom.h line 61

Quoted 10 lines
target-openrisc/cpu-qom.h new file +10 -0
+ *
+ * A Openrisc CPU.
+ */
+typedef struct OPENRISCCPU {
+    /*< private >*/
+    CPUState parent_obj;
+    /*< public >*/
+
+    CPUOPENRISCState env;
+} OPENRISCCPU;

OpenRISCCPU? OpenriscCPU?

Quoted 26 lines
target-openrisc/cpu-qom.h new file +9 -0
+
+static inline OPENRISCCPU *openrisc_env_get_cpu(CPUOPENRISCState *env)
+{
+    return OPENRISC_CPU(container_of(env, OPENRISCCPU, env));
+}
+
+#define ENV_GET_CPU(e) CPU(openrisc_env_get_cpu(e))
+
+#endif /* QEMU_OPENRISC_CPU_QOM_H */
target-openrisc/cpu.c new file +11 -0
new file mode 100644
index 0000000..01e65a2
@@ -0,0 +1,74 @@
+/*
+ *  QEMU Openrisc CPU
+ *
+ *  Copyright (c) 2012 Jia Liu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,

target-openrisc/cpu.c line 21

Quoted 10 lines
target-openrisc/cpu.c new file +10 -0
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "cpu.h"
+#include "cpu-qom.h"

cpu.h already does #include "cpu-qom.h", so no need to include it here again.

target-openrisc/cpu.c line 34

Quoted 13 lines
target-openrisc/cpu.c new file +13 -0
+#include "qemu-common.h"
+
+
+/* CPUClass::reset() */
+static void openrisc_cpu_reset(CPUState *s)
+{
+    OPENRISCCPU *cpu = OPENRISC_CPU(s);
+    OPENRISCCPUClass *occ = OPENRISC_CPU_GET_CLASS(cpu);
+    CPUOPENRISCState *env = &cpu->env;
+
+    occ->parent_reset(s);
+
+    openrisc_reset(env);

Shouldn't this be inline here? And don't forget to reset the common fields, too. The usual way I think is to let a helper signal an interrupt_request and then from outside the thread call cpu_reset(), which will call the above function.

Be warned that over time more and more fields will be moved from CPU_COMMON into CPUState, so env is not ideal long-term.

target-openrisc/cpu.c line 47

Quoted 13 lines
target-openrisc/cpu.c new file +13 -0
+
+}
+
+static void openrisc_cpu_initfn(Object *obj)
+{
+    OPENRISCCPU *cpu = OPENRISC_CPU(obj);
+    CPUOPENRISCState *env = &cpu->env;
+
+    cpu_exec_init(env);
+
+    env->flags = 0;
+
+    cpu_reset(CPU(cpu));

This should be part of a realizefn, not of the initfn. We don't have the former just yet, so it should go into the cpu_xxx_init() function for now.

Quoted 27 lines
target-openrisc/cpu.c new file +27 -0
+}
+
+static void openrisc_cpu_class_init(ObjectClass *oc, void *data)
+{
+    OPENRISCCPUClass *occ = OPENRISC_CPU_CLASS(oc);
+    CPUClass *cc = CPU_CLASS(oc);
+
+    occ->parent_reset = cc->reset;
+    cc->reset = openrisc_cpu_reset;
+}
+
+static const TypeInfo openrisc_cpu_type_info = {
+    .name = TYPE_OPENRISC_CPU,
+    .parent = TYPE_CPU,
+    .instance_size = sizeof(OPENRISCCPU),
+    .instance_init = openrisc_cpu_initfn,
+    .abstract = false,
+    .class_size = sizeof(OPENRISCCPUClass),
+    .class_init = openrisc_cpu_class_init,
+};
+
+static void openrisc_cpu_register_types(void)
+{
+    type_register_static(&openrisc_cpu_type_info);
+}
+
+type_init(openrisc_cpu_register_types)

[...]

Quoted 32 lines
target-openrisc/helper.c new file +26 -0
new file mode 100644
index 0000000..dcb61c9
@@ -0,0 +1,67 @@
+/*
+ * Openrisc helpers
+ *
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
+ */
+
+#include "cpu.h"
+#include "qemu-common.h"
+#include "gdbstub.h"
+#include "helper.h"
+#include "host-utils.h"
+#if !defined(CONFIG_USER_ONLY)

target-openrisc/helper.c line 36

Quoted 10 lines
target-openrisc/helper.c new file +10 -0
+#include "hw/loader.h"
+#endif
+
+typedef struct {
+    const char *name;
+} OPENRISCDef;
+
+static const OPENRISCDef openrisc_defs[] = {
+    {.name = "or1200",}
+};

Don't. Use QOM subclasses for modelling CPU types.

target-openrisc/helper.c line 46

Quoted 10 lines
target-openrisc/helper.c new file +10 -0
+
+void cpu_openrisc_list(FILE *f, fprintf_function cpu_fprintf)
+{
+    int i;
+
+    cpu_fprintf(f, "Available CPUs:\n");
+    for (i = 0; i < ARRAY_SIZE(openrisc_defs); ++i) {
+        cpu_fprintf(f, "  %s\n", openrisc_defs[i].name);
+    }
+}

This can then walk types to list models, cf. target-arm.

target-openrisc/helper.c line 48

Quoted 2 lines
target-openrisc/helper.c new file +2 -0
+
+CPUOPENRISCState *cpu_openrisc_init(const char *cpu_model)

This should return OpenRISCCPU instead.

cpu_init() can return CPUOpenRISCState for backwards compatibility, there's lots of examples on the list.

target-openrisc/helper.c line 53

Quoted 5 lines
target-openrisc/helper.c new file +5 -0
+{
+    CPUOPENRISCState *env;
+    static int tcg_inited;
+
+    env = g_malloc0(sizeof(*env));

No. This needs to instantiate the QOM type with object_new().

target-openrisc/helper.c line 55

Quoted 2 lines
target-openrisc/helper.c new file +2 -0
+    memset(env, 0, sizeof(*env));
+    cpu_exec_init(env);

This is already in the initn, so drop it here.

target-openrisc/helper.c line 56

Quoted 1 line
target-openrisc/helper.c new file +1 -0
+    qemu_init_vcpu(env);

This is the second candidate for a realizefn.

target-openrisc/helper.c line 57

Quoted 1 line
target-openrisc/helper.c new file +1 -0
+    if (!tcg_inited) {

if (tcg_enabled() && !tcg_inited) {

Note that besides TCG there's qtest that your new target should support.

Quoted 10 lines
target-openrisc/helper.c new file +10 -0
+        tcg_inited = 1;
+        openrisc_translate_init();
+    }
+
+    return env;
+}
+
+void do_interrupt(CPUOPENRISCState *env)
+{
+}

[...]

Quoted 72 lines
target-openrisc/machine.c new file +66 -0
new file mode 100644
index 0000000..c7ac0ea
@@ -0,0 +1,76 @@
+/*
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
+ */
+
+#include "hw/hw.h"
+#include "hw/boards.h"
+#include "kvm.h"
+
+static const VMStateDescription vmstate_cpu = {
+    .name = "cpu",
+    .fields = (VMStateField[]) {
+        VMSTATE_UINT32_ARRAY(gpr, CPUOPENRISCState, 32),
+        VMSTATE_UINT32(sr, CPUOPENRISCState),
+        VMSTATE_UINT32(epcr, CPUOPENRISCState),
+        VMSTATE_UINT32(eear, CPUOPENRISCState),
+        VMSTATE_UINT32(esr, CPUOPENRISCState),
+        VMSTATE_UINT32(fpcsr, CPUOPENRISCState),
+        VMSTATE_UINT32(pc, CPUOPENRISCState),
+        VMSTATE_UINT32(npc, CPUOPENRISCState),
+        VMSTATE_UINT32(ppc, CPUOPENRISCState),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+void cpu_save(QEMUFile *f, void *opaque)
+{
+    CPUOPENRISCState *env = (CPUOPENRISCState *)opaque;
+    unsigned int i;
+
+    for (i = 0; i < 32; i++) {
+        qemu_put_betls(f, &env->gpr[i]);
+    }
+
+    qemu_put_betls(f, &env->epcr);
+    qemu_put_betls(f, &env->eear);
+    qemu_put_betls(f, &env->esr);
+
+    qemu_put_betls(f, &env->sr);
+    qemu_put_be32s(f, &env->pc);
+    qemu_put_be32s(f, &env->fpcsr);
+}
+
+int cpu_load(QEMUFile *f, void *opaque, int version_id)
+{
+    CPUOPENRISCState *env = (CPUOPENRISCState *)opaque;
+    unsigned int i;
+
+    for (i = 0; i < 32; i++) {
+        qemu_get_betls(f, &env->gpr[i]);
+    }
+
+    qemu_get_betls(f, &env->epcr);

target-openrisc/machine.c line 76

Quoted 10 lines
target-openrisc/machine.c new file +10 -0
+    qemu_get_betls(f, &env->eear);
+    qemu_get_betls(f, &env->esr);
+
+    qemu_get_betls(f, &env->sr);
+    qemu_get_betls(f, &env->pc);
+    qemu_get_be32s(f, &env->fpcsr);
+    tlb_flush(env, 1);
+
+    return 0;
+}

[snip]

This is a mix of two ways of doing the same thing. You should only use VMState for new code.

Andreas

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
author
#4 lore raw

Re: [Qemu-devel] [PATCH 01/15] Openrisc: add target stub

Hi Andreas,

Thanks for your comment.

Quoted 16 lines · Andreas Färber

Am 17.05.2012 10:35, schrieb Jia Liu:

target-openrisc/cpu-qom.h

Quoted 9 lines

add the openrisc target stub and basic implementation.

---

target-openrisc/cpu-qom.h new file +0 -0
new file mode 100644
index 0000000..8c936df

First of all, if you design your new target cleanly, there's no strict need for a cpu-qom.h header - it mainly served to separate new QOM code from legacy code. If you want, you can put the code directly into cpu.h just as well.

I see the other targets patched by QOM, so I copied the code from mips without fully understand.

Quoted 9 lines

target-openrisc/cpu-qom.h line 4

Quoted 5 lines
target-openrisc/cpu-qom.h new file +4 -0
@@ -0,0 +1,70 @@
+/*
+ *  QEMU Openrisc CPU
+ *
+ *  Copyright (c) 2012 Jia Liu <[email protected]>

Minor nitpick: I guess this was copied from some other header? Uses a two-space indentation here and one-space below.

Thanks, I'll fix this all in V2.

Quoted 20 lines
Quoted 19 lines
target-openrisc/cpu-qom.h new file +19 -0
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef QEMU_OPENRISC_CPU_QOM_H
+#define QEMU_OPENRISC_CPU_QOM_H
+
+#include "qemu/cpu.h"
Quoted 8 lines

target-openrisc/cpu-qom.h line 24

Quoted 1 line
target-openrisc/cpu-qom.h new file +1 -0
+#include "cpu.h"

Please don't. This was a big mistake of mine that I've been correcting on the qom-next branch, onto which you should rebase a new target such as this by the way. It leads to really ugly problems when someone includes cpu-qom.h and the new struct below is not yet defined for functions using it there.

I'm not truly understand it, did you mean I should not use QOM until you make it more perfect?

Quoted 27 lines
Quoted 13 lines
target-openrisc/cpu-qom.h new file +13 -0
+
+#define TYPE_OPENRISC_CPU "or32"
+
+#define OPENRISC_CPU_CLASS(klass) \
+    OBJECT_CLASS_CHECK(OPENRISCCPUClass, (klass), TYPE_OPENRISC_CPU)
+#define OPENRISC_CPU(obj) \
+    OBJECT_CHECK(OPENRISCCPU, (obj), TYPE_OPENRISC_CPU)
+#define OPENRISC_CPU_GET_CLASS(obj) \
+    OBJECT_GET_CLASS(OPENRISCCPUClass, (obj), TYPE_OPENRISC_CPU)
+
+/**
+ * OPENRISCCPUClass:
+ * @parent_reset: The parent class' reset handler.

target-openrisc/cpu-qom.h line 47

Quoted 10 lines
target-openrisc/cpu-qom.h new file +10 -0
+ *
+ * A Openrisc CPU model.
+ */
+typedef struct OPENRISCCPUClass {
+    /*< private >*/
+    CPUClass parent_class;
+    /*< public >*/
+
+    void (*parent_reset)(CPUState *cpu);
+} OPENRISCCPUClass;

Pleave avoid unnecessary uppercase spelling: OpenRISCCPUClass? That distinguishes it from the all-uppercase cast macros.

Quoted 2 lines

Or OpenriscCPUClass as you spell it elsewhere?

I used OPENRISCCPU in the code, if it is not good, I can change it into OpenriscCPU.

Quoted 48 lines
Quoted 4 lines
target-openrisc/cpu-qom.h new file +4 -0
+
+/**
+ * OPENRISCCPU:
+ * @env: #CPUOPENRISCState

target-openrisc/cpu-qom.h line 61

Quoted 10 lines
target-openrisc/cpu-qom.h new file +10 -0
+ *
+ * A Openrisc CPU.
+ */
+typedef struct OPENRISCCPU {
+    /*< private >*/
+    CPUState parent_obj;
+    /*< public >*/
+
+    CPUOPENRISCState env;
+} OPENRISCCPU;

OpenRISCCPU? OpenriscCPU?

Quoted 31 lines
target-openrisc/cpu-qom.h new file +9 -0
+
+static inline OPENRISCCPU *openrisc_env_get_cpu(CPUOPENRISCState *env)
+{
+    return OPENRISC_CPU(container_of(env, OPENRISCCPU, env));
+}
+
+#define ENV_GET_CPU(e) CPU(openrisc_env_get_cpu(e))
+
+#endif /* QEMU_OPENRISC_CPU_QOM_H */
target-openrisc/cpu.c new file +16 -0
new file mode 100644
index 0000000..01e65a2
@@ -0,0 +1,74 @@
+/*
+ *  QEMU Openrisc CPU
+ *
+ *  Copyright (c) 2012 Jia Liu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
Quoted 8 lines

target-openrisc/cpu.c line 21

Quoted 5 lines
target-openrisc/cpu.c new file +5 -0
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "cpu.h"
+#include "cpu-qom.h"

cpu.h already does #include "cpu-qom.h", so no need to include it here again.

thanks, I'll delete it.

Quoted 11 lines
Quoted 11 lines
target-openrisc/cpu.c new file +10 -0
+#include "qemu-common.h"
+
+
+/* CPUClass::reset() */
+static void openrisc_cpu_reset(CPUState *s)
+{
+    OPENRISCCPU *cpu = OPENRISC_CPU(s);
+    OPENRISCCPUClass *occ = OPENRISC_CPU_GET_CLASS(cpu);
+    CPUOPENRISCState *env = &cpu->env;
+
Quoted 9 lines

target-openrisc/cpu.c line 34

Quoted 3 lines
target-openrisc/cpu.c new file +3 -0
+    occ->parent_reset(s);
+
+    openrisc_reset(env);

Shouldn't this be inline here? And don't forget to reset the common fields, too. The usual way I think is to let a helper signal an interrupt_request and then from outside the thread call cpu_reset(), which will call the above function.

Thanks, I'll check this, and reset the other fields.

Quoted 3 lines

Be warned that over time more and more fields will be moved from CPU_COMMON into CPUState, so env is not ideal long-term.

did you mean I should use a memset(0) clean the all fields here?

Quoted 8 lines
Quoted 8 lines
target-openrisc/cpu.c new file +8 -0
+
+}
+
+static void openrisc_cpu_initfn(Object *obj)
+{
+    OPENRISCCPU *cpu = OPENRISC_CPU(obj);
+    CPUOPENRISCState *env = &cpu->env;
+
Quoted 9 lines

target-openrisc/cpu.c line 47

Quoted 5 lines
target-openrisc/cpu.c new file +5 -0
+    cpu_exec_init(env);
+
+    env->flags = 0;
+
+    cpu_reset(CPU(cpu));

This should be part of a realizefn, not of the initfn. We don't have the former just yet, so it should go into the cpu_xxx_init() function for now.

sorry, you mean I should rename it static void
openrisc_cpu_realizefn(Object *obj)?

Quoted 64 lines
Quoted 27 lines
target-openrisc/cpu.c new file +27 -0
+}
+
+static void openrisc_cpu_class_init(ObjectClass *oc, void *data)
+{
+    OPENRISCCPUClass *occ = OPENRISC_CPU_CLASS(oc);
+    CPUClass *cc = CPU_CLASS(oc);
+
+    occ->parent_reset = cc->reset;
+    cc->reset = openrisc_cpu_reset;
+}
+
+static const TypeInfo openrisc_cpu_type_info = {
+    .name = TYPE_OPENRISC_CPU,
+    .parent = TYPE_CPU,
+    .instance_size = sizeof(OPENRISCCPU),
+    .instance_init = openrisc_cpu_initfn,
+    .abstract = false,
+    .class_size = sizeof(OPENRISCCPUClass),
+    .class_init = openrisc_cpu_class_init,
+};
+
+static void openrisc_cpu_register_types(void)
+{
+    type_register_static(&openrisc_cpu_type_info);
+}
+
+type_init(openrisc_cpu_register_types)

[...]

Quoted 36 lines
target-openrisc/helper.c new file +30 -0
new file mode 100644
index 0000000..dcb61c9
@@ -0,0 +1,67 @@
+/*
+ * Openrisc helpers
+ *
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
+ */
+
+#include "cpu.h"
+#include "qemu-common.h"
+#include "gdbstub.h"
+#include "helper.h"
+#include "host-utils.h"
+#if !defined(CONFIG_USER_ONLY)
+#include "hw/loader.h"
+#endif
+
+typedef struct {
Quoted 9 lines

target-openrisc/helper.c line 36

Quoted 6 lines
target-openrisc/helper.c new file +6 -0
+    const char *name;
+} OPENRISCDef;
+
+static const OPENRISCDef openrisc_defs[] = {
+    {.name = "or1200",}
+};

Don't. Use QOM subclasses for modelling CPU types.

OK, let me find some code to copy. I'm not sure about QOM for now, yet.

Quoted 4 lines
Quoted 4 lines
target-openrisc/helper.c new file +4 -0
+
+void cpu_openrisc_list(FILE *f, fprintf_function cpu_fprintf)
+{
+    int i;
Quoted 9 lines

target-openrisc/helper.c line 46

Quoted 6 lines
target-openrisc/helper.c new file +6 -0
+
+    cpu_fprintf(f, "Available CPUs:\n");
+    for (i = 0; i < ARRAY_SIZE(openrisc_defs); ++i) {
+        cpu_fprintf(f, "  %s\n", openrisc_defs[i].name);
+    }
+}

This can then walk types to list models, cf. target-arm.

Thanks, I'll read ARM's code.

Quoted 8 lines

target-openrisc/helper.c line 48

Quoted 2 lines
target-openrisc/helper.c new file +2 -0
+
+CPUOPENRISCState *cpu_openrisc_init(const char *cpu_model)

This should return OpenRISCCPU instead.

cpu_init() can return CPUOpenRISCState for backwards compatibility, there's lots of examples on the list.

Thanks, I'll check and fix.

Quoted 8 lines

target-openrisc/helper.c line 53

Quoted 5 lines
target-openrisc/helper.c new file +5 -0
+{
+    CPUOPENRISCState *env;
+    static int tcg_inited;
+
+    env = g_malloc0(sizeof(*env));

No. This needs to instantiate the QOM type with object_new().

Thanks, I'll use object_new()

Quoted 5 lines

target-openrisc/helper.c line 55

Quoted 2 lines
target-openrisc/helper.c new file +2 -0
+    memset(env, 0, sizeof(*env));
+    cpu_exec_init(env);

This is already in the initn, so drop it here.

Thank you, I'll drop it.

Quoted 10 lines

target-openrisc/helper.c line 56

Quoted 1 line
target-openrisc/helper.c new file +1 -0
+    qemu_init_vcpu(env);

This is the second candidate for a realizefn.

target-openrisc/helper.c line 57

Quoted 1 line
target-openrisc/helper.c new file +1 -0
+    if (!tcg_inited) {

if (tcg_enabled() && !tcg_inited) {

Note that besides TCG there's qtest that your new target should support.

Sorry, I'm not sure about this.

Quoted 95 lines
Quoted 10 lines
target-openrisc/helper.c new file +10 -0
+        tcg_inited = 1;
+        openrisc_translate_init();
+    }
+
+    return env;
+}
+
+void do_interrupt(CPUOPENRISCState *env)
+{
+}

[...]

Quoted 82 lines
target-openrisc/machine.c new file +76 -0
new file mode 100644
index 0000000..c7ac0ea
@@ -0,0 +1,76 @@
+/*
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
+ */
+
+#include "hw/hw.h"
+#include "hw/boards.h"
+#include "kvm.h"
+
+static const VMStateDescription vmstate_cpu = {
+    .name = "cpu",
+    .fields = (VMStateField[]) {
+        VMSTATE_UINT32_ARRAY(gpr, CPUOPENRISCState, 32),
+        VMSTATE_UINT32(sr, CPUOPENRISCState),
+        VMSTATE_UINT32(epcr, CPUOPENRISCState),
+        VMSTATE_UINT32(eear, CPUOPENRISCState),
+        VMSTATE_UINT32(esr, CPUOPENRISCState),
+        VMSTATE_UINT32(fpcsr, CPUOPENRISCState),
+        VMSTATE_UINT32(pc, CPUOPENRISCState),
+        VMSTATE_UINT32(npc, CPUOPENRISCState),
+        VMSTATE_UINT32(ppc, CPUOPENRISCState),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+void cpu_save(QEMUFile *f, void *opaque)
+{
+    CPUOPENRISCState *env = (CPUOPENRISCState *)opaque;
+    unsigned int i;
+
+    for (i = 0; i < 32; i++) {
+        qemu_put_betls(f, &env->gpr[i]);
+    }
+
+    qemu_put_betls(f, &env->epcr);
+    qemu_put_betls(f, &env->eear);
+    qemu_put_betls(f, &env->esr);
+
+    qemu_put_betls(f, &env->sr);
+    qemu_put_be32s(f, &env->pc);
+    qemu_put_be32s(f, &env->fpcsr);
+}
+
+int cpu_load(QEMUFile *f, void *opaque, int version_id)
+{
+    CPUOPENRISCState *env = (CPUOPENRISCState *)opaque;
+    unsigned int i;
+
+    for (i = 0; i < 32; i++) {
+        qemu_get_betls(f, &env->gpr[i]);
+    }
+
+    qemu_get_betls(f, &env->epcr);
+    qemu_get_betls(f, &env->eear);
+    qemu_get_betls(f, &env->esr);
+
+    qemu_get_betls(f, &env->sr);
+    qemu_get_betls(f, &env->pc);
+    qemu_get_be32s(f, &env->fpcsr);
+    tlb_flush(env, 1);
+
+    return 0;
+}

[snip]

Quoted 3 lines

This is a mix of two ways of doing the same thing. You should only use VMState for new code.

did you mean I should not use QEMUFile?

Quoted 5 lines

Andreas

--
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg

Thanks very much for your comment.
And I hope I can get more information from you for I can not understand all of your comment, your comment is good and useful, but for me, a newbie, I need more time to follow your step.

Regard,
Jia.

#5 lore raw

Re: [Qemu-devel] [PATCH 01/15] Openrisc: add target stub

Quoted 8 lines
Quoted 5 lines

Pleave avoid unnecessary uppercase spelling: OpenRISCCPUClass? That distinguishes it from the all-uppercase cast macros.

Or OpenriscCPUClass as you spell it elsewhere?

I used OPENRISCCPU in the code, if it is not good, I can change it into OpenriscCPU.

I think OpenRISC is better [1].

Regards,
chenwj

[1] http://en.wikipedia.org/wiki/OpenRISC

Signature · 5 lines
Wei-Ren Chen (陳韋任)
Computer Systems Lab, Institute of Information Science,
Academia Sinica, Taiwan (R.O.C.)
Tel:886-2-2788-3799 #1667
Homepage: http://people.cs.nctu.edu.tw/~chenwj
#6 lore raw

Re: [Qemu-devel] [PATCH 01/15] Openrisc: add target stub

In reply to Jia Liu
Quoted 5 lines
Quoted 3 lines

This is a mix of two ways of doing the same thing. You should only use VMState for new code.

did you mean I should not use QEMUFile?

I guess what Andreas means is there are two ways to do cpu_save/cpu_load,
the one you wrote is the old style. For the brand new target, you should use VMState completely. You can take target-lm32/machine.c as an example.

---
void cpu_save(QEMUFile *f, void *opaque)

{
    vmstate_save_state(f, &vmstate_cpu, opaque);
}

int cpu_load(QEMUFile *f, void *opaque, int version_id)

{
    return vmstate_load_state(f, &vmstate_cpu, opaque, version_id);
}

---

Leave to vmstate_save_state/vmstate_load_state (savevm.c) do the real things.

Regards,
chenwj

Signature · 5 lines
Wei-Ren Chen (陳韋任)
Computer Systems Lab, Institute of Information Science,
Academia Sinica, Taiwan (R.O.C.)
Tel:886-2-2788-3799 #1667
Homepage: http://people.cs.nctu.edu.tw/~chenwj
#7 lore raw

Re: [Qemu-devel] [PATCH 01/15] Openrisc: add target stub

Am 18.05.2012 04:56, schrieb 陳韋任:

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

This is a mix of two ways of doing the same thing. You should only use VMState for new code.

did you mean I should not use QEMUFile?

I guess what Andreas means is there are two ways to do cpu_save/cpu_load,
the one you wrote is the old style. For the brand new target, you should use VMState completely.

Exactly what I meant. Although I'm working towards making that even easier by leveraging code from Juan's vmstate series and having the CPUs just store a pointer to their VMStateDescription like devices do.

Andreas

Quoted 19 lines

You can take target-lm32/machine.c as an example.

---
void cpu_save(QEMUFile *f, void *opaque)

{
    vmstate_save_state(f, &vmstate_cpu, opaque);
}

int cpu_load(QEMUFile *f, void *opaque, int version_id)

{
    return vmstate_load_state(f, &vmstate_cpu, opaque, version_id);
}

---

Leave to vmstate_save_state/vmstate_load_state (savevm.c) do the real things.

Regards,
chenwj

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
author
#8 lore raw

Re: [Qemu-devel] [PATCH 01/15] Openrisc: add target stub

In reply to Andreas Färber

Hi Andreas,

Thanks, I'm trying fix them all.

Quoted 390 lines · Andreas Färber

Am 17.05.2012 10:35, schrieb Jia Liu:

target-openrisc/cpu-qom.h

Quoted 9 lines

add the openrisc target stub and basic implementation.

---

target-openrisc/cpu-qom.h new file +0 -0
new file mode 100644
index 0000000..8c936df

First of all, if you design your new target cleanly, there's no strict need for a cpu-qom.h header - it mainly served to separate new QOM code from legacy code. If you want, you can put the code directly into cpu.h just as well.

target-openrisc/cpu-qom.h line 4

Quoted 5 lines
target-openrisc/cpu-qom.h new file +4 -0
@@ -0,0 +1,70 @@
+/*
+ *  QEMU Openrisc CPU
+ *
+ *  Copyright (c) 2012 Jia Liu <[email protected]>

Minor nitpick: I guess this was copied from some other header? Uses a two-space indentation here and one-space below.

Quoted 11 lines
target-openrisc/cpu-qom.h new file +11 -0
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *

target-openrisc/cpu-qom.h line 24

Quoted 10 lines
target-openrisc/cpu-qom.h new file +9 -0
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef QEMU_OPENRISC_CPU_QOM_H
+#define QEMU_OPENRISC_CPU_QOM_H
+
+#include "qemu/cpu.h"
 
+#include "cpu.h"

Please don't. This was a big mistake of mine that I've been correcting on the qom-next branch, onto which you should rebase a new target such as this by the way. It leads to really ugly problems when someone includes cpu-qom.h and the new struct below is not yet defined for functions using it there.

Quoted 13 lines
target-openrisc/cpu-qom.h new file +13 -0
+
+#define TYPE_OPENRISC_CPU "or32"
+
+#define OPENRISC_CPU_CLASS(klass) \
+    OBJECT_CLASS_CHECK(OPENRISCCPUClass, (klass), TYPE_OPENRISC_CPU)
+#define OPENRISC_CPU(obj) \
+    OBJECT_CHECK(OPENRISCCPU, (obj), TYPE_OPENRISC_CPU)
+#define OPENRISC_CPU_GET_CLASS(obj) \
+    OBJECT_GET_CLASS(OPENRISCCPUClass, (obj), TYPE_OPENRISC_CPU)
+
+/**
+ * OPENRISCCPUClass:
+ * @parent_reset: The parent class' reset handler.

target-openrisc/cpu-qom.h line 47

Quoted 10 lines
target-openrisc/cpu-qom.h new file +10 -0
+ *
+ * A Openrisc CPU model.
+ */
+typedef struct OPENRISCCPUClass {
+    /*< private >*/
+    CPUClass parent_class;
+    /*< public >*/
+
+    void (*parent_reset)(CPUState *cpu);
+} OPENRISCCPUClass;

Pleave avoid unnecessary uppercase spelling: OpenRISCCPUClass? That distinguishes it from the all-uppercase cast macros.

Or OpenriscCPUClass as you spell it elsewhere?

Quoted 4 lines
target-openrisc/cpu-qom.h new file +4 -0
+
+/**
+ * OPENRISCCPU:
+ * @env: #CPUOPENRISCState

target-openrisc/cpu-qom.h line 61

Quoted 10 lines
target-openrisc/cpu-qom.h new file +10 -0
+ *
+ * A Openrisc CPU.
+ */
+typedef struct OPENRISCCPU {
+    /*< private >*/
+    CPUState parent_obj;
+    /*< public >*/
+
+    CPUOPENRISCState env;
+} OPENRISCCPU;

OpenRISCCPU? OpenriscCPU?

Quoted 26 lines
target-openrisc/cpu-qom.h new file +9 -0
+
+static inline OPENRISCCPU *openrisc_env_get_cpu(CPUOPENRISCState *env)
+{
+    return OPENRISC_CPU(container_of(env, OPENRISCCPU, env));
+}
+
+#define ENV_GET_CPU(e) CPU(openrisc_env_get_cpu(e))
+
+#endif /* QEMU_OPENRISC_CPU_QOM_H */
target-openrisc/cpu.c new file +11 -0
new file mode 100644
index 0000000..01e65a2
@@ -0,0 +1,74 @@
+/*
+ *  QEMU Openrisc CPU
+ *
+ *  Copyright (c) 2012 Jia Liu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,

target-openrisc/cpu.c line 21

Quoted 10 lines
target-openrisc/cpu.c new file +10 -0
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "cpu.h"
+#include "cpu-qom.h"

cpu.h already does #include "cpu-qom.h", so no need to include it here again.

target-openrisc/cpu.c line 34

Quoted 13 lines
target-openrisc/cpu.c new file +13 -0
+#include "qemu-common.h"
+
+
+/* CPUClass::reset() */
+static void openrisc_cpu_reset(CPUState *s)
+{
+    OPENRISCCPU *cpu = OPENRISC_CPU(s);
+    OPENRISCCPUClass *occ = OPENRISC_CPU_GET_CLASS(cpu);
+    CPUOPENRISCState *env = &cpu->env;
+
+    occ->parent_reset(s);
+
+    openrisc_reset(env);

Shouldn't this be inline here? And don't forget to reset the common fields, too. The usual way I think is to let a helper signal an interrupt_request and then from outside the thread call cpu_reset(), which will call the above function.

Be warned that over time more and more fields will be moved from CPU_COMMON into CPUState, so env is not ideal long-term.

target-openrisc/cpu.c line 47

Quoted 13 lines
target-openrisc/cpu.c new file +13 -0
+
+}
+
+static void openrisc_cpu_initfn(Object *obj)
+{
+    OPENRISCCPU *cpu = OPENRISC_CPU(obj);
+    CPUOPENRISCState *env = &cpu->env;
+
+    cpu_exec_init(env);
+
+    env->flags = 0;
+
+    cpu_reset(CPU(cpu));

This should be part of a realizefn, not of the initfn. We don't have the former just yet, so it should go into the cpu_xxx_init() function for now.

Quoted 27 lines
target-openrisc/cpu.c new file +27 -0
+}
+
+static void openrisc_cpu_class_init(ObjectClass *oc, void *data)
+{
+    OPENRISCCPUClass *occ = OPENRISC_CPU_CLASS(oc);
+    CPUClass *cc = CPU_CLASS(oc);
+
+    occ->parent_reset = cc->reset;
+    cc->reset = openrisc_cpu_reset;
+}
+
+static const TypeInfo openrisc_cpu_type_info = {
+    .name = TYPE_OPENRISC_CPU,
+    .parent = TYPE_CPU,
+    .instance_size = sizeof(OPENRISCCPU),
+    .instance_init = openrisc_cpu_initfn,
+    .abstract = false,
+    .class_size = sizeof(OPENRISCCPUClass),
+    .class_init = openrisc_cpu_class_init,
+};
+
+static void openrisc_cpu_register_types(void)
+{
+    type_register_static(&openrisc_cpu_type_info);
+}
+
+type_init(openrisc_cpu_register_types)

[...]

Quoted 32 lines
target-openrisc/helper.c new file +26 -0
new file mode 100644
index 0000000..dcb61c9
@@ -0,0 +1,67 @@
+/*
+ * Openrisc helpers
+ *
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
+ */
+
+#include "cpu.h"
+#include "qemu-common.h"
+#include "gdbstub.h"
+#include "helper.h"
+#include "host-utils.h"
+#if !defined(CONFIG_USER_ONLY)

target-openrisc/helper.c line 36

Quoted 10 lines
target-openrisc/helper.c new file +10 -0
+#include "hw/loader.h"
+#endif
+
+typedef struct {
+    const char *name;
+} OPENRISCDef;
+
+static const OPENRISCDef openrisc_defs[] = {
+    {.name = "or1200",}
+};

Don't. Use QOM subclasses for modelling CPU types.

target-openrisc/helper.c line 46

Quoted 10 lines
target-openrisc/helper.c new file +10 -0
+
+void cpu_openrisc_list(FILE *f, fprintf_function cpu_fprintf)
+{
+    int i;
+
+    cpu_fprintf(f, "Available CPUs:\n");
+    for (i = 0; i < ARRAY_SIZE(openrisc_defs); ++i) {
+        cpu_fprintf(f, "  %s\n", openrisc_defs[i].name);
+    }
+}

This can then walk types to list models, cf. target-arm.

target-openrisc/helper.c line 48

Quoted 2 lines
target-openrisc/helper.c new file +2 -0
+
+CPUOPENRISCState *cpu_openrisc_init(const char *cpu_model)

This should return OpenRISCCPU instead.

cpu_init() can return CPUOpenRISCState for backwards compatibility, there's lots of examples on the list.

target-openrisc/helper.c line 53

Quoted 5 lines
target-openrisc/helper.c new file +5 -0
+{
+    CPUOPENRISCState *env;
+    static int tcg_inited;
+
+    env = g_malloc0(sizeof(*env));

No. This needs to instantiate the QOM type with object_new().

target-openrisc/helper.c line 55

Quoted 2 lines
target-openrisc/helper.c new file +2 -0
+    memset(env, 0, sizeof(*env));
+    cpu_exec_init(env);

This is already in the initn, so drop it here.

target-openrisc/helper.c line 56

Quoted 1 line
target-openrisc/helper.c new file +1 -0
+    qemu_init_vcpu(env);

This is the second candidate for a realizefn.

target-openrisc/helper.c line 57

Quoted 1 line
target-openrisc/helper.c new file +1 -0
+    if (!tcg_inited) {

if (tcg_enabled() && !tcg_inited) {

Note that besides TCG there's qtest that your new target should support.

Quoted 10 lines
target-openrisc/helper.c new file +10 -0
+        tcg_inited = 1;
+        openrisc_translate_init();
+    }
+
+    return env;
+}
+
+void do_interrupt(CPUOPENRISCState *env)
+{
+}

[...]

Quoted 82 lines
target-openrisc/machine.c new file +76 -0
new file mode 100644
index 0000000..c7ac0ea
@@ -0,0 +1,76 @@
+/*
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
+ */
+
+#include "hw/hw.h"
+#include "hw/boards.h"
+#include "kvm.h"
+
+static const VMStateDescription vmstate_cpu = {
+    .name = "cpu",
+    .fields = (VMStateField[]) {
+        VMSTATE_UINT32_ARRAY(gpr, CPUOPENRISCState, 32),
+        VMSTATE_UINT32(sr, CPUOPENRISCState),
+        VMSTATE_UINT32(epcr, CPUOPENRISCState),
+        VMSTATE_UINT32(eear, CPUOPENRISCState),
+        VMSTATE_UINT32(esr, CPUOPENRISCState),
+        VMSTATE_UINT32(fpcsr, CPUOPENRISCState),
+        VMSTATE_UINT32(pc, CPUOPENRISCState),
+        VMSTATE_UINT32(npc, CPUOPENRISCState),
+        VMSTATE_UINT32(ppc, CPUOPENRISCState),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+void cpu_save(QEMUFile *f, void *opaque)
+{
+    CPUOPENRISCState *env = (CPUOPENRISCState *)opaque;
+    unsigned int i;
+
+    for (i = 0; i < 32; i++) {
+        qemu_put_betls(f, &env->gpr[i]);
+    }
+
+    qemu_put_betls(f, &env->epcr);
+    qemu_put_betls(f, &env->eear);
+    qemu_put_betls(f, &env->esr);
+
+    qemu_put_betls(f, &env->sr);
+    qemu_put_be32s(f, &env->pc);
+    qemu_put_be32s(f, &env->fpcsr);
+}
+
+int cpu_load(QEMUFile *f, void *opaque, int version_id)
+{
+    CPUOPENRISCState *env = (CPUOPENRISCState *)opaque;
+    unsigned int i;
+
+    for (i = 0; i < 32; i++) {
+        qemu_get_betls(f, &env->gpr[i]);
+    }
+
+    qemu_get_betls(f, &env->epcr);
+    qemu_get_betls(f, &env->eear);
+    qemu_get_betls(f, &env->esr);
+
+    qemu_get_betls(f, &env->sr);
+    qemu_get_betls(f, &env->pc);
+    qemu_get_be32s(f, &env->fpcsr);
+    tlb_flush(env, 1);
+
+    return 0;
+}

[snip]

Quoted 8 lines

This is a mix of two ways of doing the same thing. You should only use VMState for new code.

Andreas

--
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg

Regards,
Jia.

#9 lore raw

Re: [Qemu-devel] [PATCH 01/15] Openrisc: add target stub

In reply to Jia Liu
Quoted 141 lines · Jia Liu

add the openrisc target stub and basic implementation.

---
 Makefile.target                  |    3 +
 arch_init.c                      |    2 +
 arch_init.h                      |    1 +
 configure                        |    8 +-
 cpu-exec.c                       |    2 + default-configs/or32-softmmu.mak |    6 +
 elf.h                            |    2 +
 poison.h                         |    1 + target-openrisc/cpu-qom.h        |   70 +++++++++ target-openrisc/cpu.c            |   74 ++++++++++ target-openrisc/cpu.h            |  299 ++++++++++++++++++++++++++++++++++++++

 target-openrisc/helper.c         |   67 +++++++++
 target-openrisc/helper.h         |   23 +++
 target-openrisc/machine.c        |   76 ++++++++++
 target-openrisc/mem.c            |   43 ++++++
 target-openrisc/mem_helper.c     |   46 ++++++
 target-openrisc/translate.c      |   90 ++++++++++++
 17 files changed, 812 insertions(+), 1 deletion(-)

 create mode 100644 default-configs/or32-softmmu.mak create mode 100644 target-openrisc/cpu-qom.h create mode 100644 target-openrisc/cpu.c create mode 100644 target-openrisc/cpu.h create mode 100644 target-openrisc/helper.c create mode 100644 target-openrisc/helper.h create mode 100644 target-openrisc/machine.c create mode 100644 target-openrisc/mem.c create mode 100644 target-openrisc/mem_helper.c create mode 100644 target-openrisc/translate.c

Makefile.target +0 -0
index 1582904..0ffb29c 100644
@@ -83,9 +83,11 @@ libobj-$(CONFIG_TCG_INTERPRETER) += tci.o

 libobj-y += fpu/softfloat.o
 ifneq ($(TARGET_BASE_ARCH), sparc)
 ifneq ($(TARGET_BASE_ARCH), alpha)
+ifneq ($(TARGET_BASE_ARCH), openrisc)
 libobj-y += op_helper.o
 endif
 endif
+endif
 libobj-y += helper.o
 ifneq ($(TARGET_BASE_ARCH), ppc)
 libobj-y += cpu.o

Makefile.target +0 -0
@@ -99,6 +101,7 @@ endif

 libobj-$(TARGET_SPARC) += int32_helper.o libobj-$(TARGET_SPARC64) += int64_helper.o libobj-$(TARGET_ALPHA) += int_helper.o fpu_helper.o sys_helper.o mem_helper.o +libobj-$(TARGET_OPENRISC) += mem.o mem_helper.o

 libobj-y += disas.o
 libobj-$(CONFIG_TCI_DIS) += tci-dis.o

arch_init.c +2 -0
index 988adca..55b608d 100644
@@ -71,6 +71,8 @@ int graphic_depth = 15;
  #define QEMU_ARCH QEMU_ARCH_MICROBLAZE
  #elif defined(TARGET_MIPS)
  #define QEMU_ARCH QEMU_ARCH_MIPS
+#elif defined(TARGET_OPENRISC)
+#define QEMU_ARCH QEMU_ARCH_OPENRISC
  #elif defined(TARGET_PPC)
  #define QEMU_ARCH QEMU_ARCH_PPC
  #elif defined(TARGET_S390X)
arch_init.h +1 -0
index c7cb94a..3dfea3b 100644
@@ -16,6 +16,7 @@ enum {
     QEMU_ARCH_SH4 = 1024,
     QEMU_ARCH_SPARC = 2048,
     QEMU_ARCH_XTENSA = 4096,
+    QEMU_ARCH_OPENRISC = 8192,
  };
 
  extern const uint32_t arch_type;
configure +1 -0
index b55a792..63e2372 100755
@@ -924,6 +924,7 @@ mips-softmmu \
  mipsel-softmmu \
  mips64-softmmu \
  mips64el-softmmu \
+or32-softmmu \
  ppc-softmmu \
  ppcemb-softmmu \
  ppc64-softmmu \
@@ -3460,7 +3461,7 @@ target_arch2=`echo $target | cut -d '-' -f 1`

 target_bigendian="no"

 case "$target_arch2" in

-  armeb|lm32|m68k|microblaze|mips|mipsn32|mips64|ppc|ppcemb|ppc64|ppc64abi32|s390x|sh4eb|sparc|sparc64|sparc32plus|xtensaeb)
+  armeb|lm32|m68k|microblaze|mips|mipsn32|mips64|or32|ppc|ppcemb|ppc64|ppc64abi32|s390x|sh4eb|sparc|sparc64|sparc32plus|xtensaeb)

  target_bigendian=yes
  ;;
 esac

configure +0 -0
@@ -3588,6 +3589,11 @@ case "$target_arch2" in

    target_phys_bits=64
    target_long_alignment=8
  ;;
+  or32)
+    TARGET_ARCH=openrisc
+    TARGET_BASE_ARCH=openrisc
+    target_phys_bits=32
+  ;;
  ppc)
    gdb_xml_files="power-core.xml power-fpu.xml power-altivec.xml power-spe.xml" target_phys_bits=64

cpu-exec.c +2 -0
index 0344cd5..ba10db1 100644
@@ -222,6 +222,7 @@ int cpu_exec(CPUArchState *env)
  #elif defined(TARGET_LM32)
  #elif defined(TARGET_MICROBLAZE)
  #elif defined(TARGET_MIPS)
+#elif defined(TARGET_OPENRISC)
  #elif defined(TARGET_SH4)
  #elif defined(TARGET_CRIS)
  #elif defined(TARGET_S390X)
@@ -620,6 +621,7 @@ int cpu_exec(CPUArchState *env)
               | env->cc_dest | (env->cc_x << 4);
  #elif defined(TARGET_MICROBLAZE)
  #elif defined(TARGET_MIPS)
+#elif defined(TARGET_OPENRISC)
  #elif defined(TARGET_SH4)
  #elif defined(TARGET_ALPHA)
  #elif defined(TARGET_CRIS)
default-configs/or32-softmmu.mak new file +0 -0
new file mode 100644
index 0000000..7590eed

default-configs/or32-softmmu.mak line 3

Quoted 3 lines
default-configs/or32-softmmu.mak new file +3 -0
@@ -0,0 +1,6 @@
+# Default configuration for or32-softmmu
+
+include pci.mak

Probably not needed?

Quoted 353 lines
default-configs/or32-softmmu.mak new file +3 -0
+CONFIG_SERIAL=y
+CONFIG_NE2000_ISA=y
+CONFIG_I8259=y
elf.h +2 -0
index e1422b8..e65f4b9 100644
@@ -106,6 +106,8 @@ typedef int64_t  Elf64_Sxword;
  #define EM_H8S          48      /* Hitachi H8S     */
  #define EM_LATTICEMICO32 138    /* LatticeMico32 */
 
+#define EM_OPENRISC     92        /* Opencores Openrisc */
+
  #define EM_UNICORE32    110     /* UniCore32 */
 
  /*
poison.h +1 -0
index d396f20..7d7b23b 100644
@@ -14,6 +14,7 @@
  #pragma GCC poison TARGET_M68K
  #pragma GCC poison TARGET_MIPS
  #pragma GCC poison TARGET_MIPS64
+#pragma GCC poison TARGET_OPENRISC
  #pragma GCC poison TARGET_PPC
  #pragma GCC poison TARGET_PPCEMB
  #pragma GCC poison TARGET_PPC64
target-openrisc/cpu-qom.h new file +70 -0
new file mode 100644
index 0000000..8c936df
@@ -0,0 +1,70 @@
+/*
+ *  QEMU Openrisc CPU
+ *
+ *  Copyright (c) 2012 Jia Liu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef QEMU_OPENRISC_CPU_QOM_H
+#define QEMU_OPENRISC_CPU_QOM_H
+
+#include "qemu/cpu.h"
+#include "cpu.h"
+
+#define TYPE_OPENRISC_CPU "or32"
+
+#define OPENRISC_CPU_CLASS(klass) \
+    OBJECT_CLASS_CHECK(OPENRISCCPUClass, (klass), TYPE_OPENRISC_CPU)
+#define OPENRISC_CPU(obj) \
+    OBJECT_CHECK(OPENRISCCPU, (obj), TYPE_OPENRISC_CPU)
+#define OPENRISC_CPU_GET_CLASS(obj) \
+    OBJECT_GET_CLASS(OPENRISCCPUClass, (obj), TYPE_OPENRISC_CPU)
+
+/**
+ * OPENRISCCPUClass:
+ * @parent_reset: The parent class' reset handler.
+ *
+ * A Openrisc CPU model.
+ */
+typedef struct OPENRISCCPUClass {
+    /*< private >*/
+    CPUClass parent_class;
+    /*< public >*/
+
+    void (*parent_reset)(CPUState *cpu);
+} OPENRISCCPUClass;
+
+/**
+ * OPENRISCCPU:
+ * @env: #CPUOPENRISCState
+ *
+ * A Openrisc CPU.
+ */
+typedef struct OPENRISCCPU {
+    /*< private >*/
+    CPUState parent_obj;
+    /*< public >*/
+
+    CPUOPENRISCState env;
+} OPENRISCCPU;
+
+static inline OPENRISCCPU *openrisc_env_get_cpu(CPUOPENRISCState *env)
+{
+    return OPENRISC_CPU(container_of(env, OPENRISCCPU, env));
+}
+
+#define ENV_GET_CPU(e) CPU(openrisc_env_get_cpu(e))
+
+#endif /* QEMU_OPENRISC_CPU_QOM_H */
target-openrisc/cpu.c new file +74 -0
new file mode 100644
index 0000000..01e65a2
@@ -0,0 +1,74 @@
+/*
+ *  QEMU Openrisc CPU
+ *
+ *  Copyright (c) 2012 Jia Liu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "cpu.h"
+#include "cpu-qom.h"
+#include "qemu-common.h"
+
+
+/* CPUClass::reset() */
+static void openrisc_cpu_reset(CPUState *s)
+{
+    OPENRISCCPU *cpu = OPENRISC_CPU(s);
+    OPENRISCCPUClass *occ = OPENRISC_CPU_GET_CLASS(cpu);
+    CPUOPENRISCState *env = &cpu->env;
+
+    occ->parent_reset(s);
+
+    openrisc_reset(env);
+
+}
+
+static void openrisc_cpu_initfn(Object *obj)
+{
+    OPENRISCCPU *cpu = OPENRISC_CPU(obj);
+    CPUOPENRISCState *env = &cpu->env;
+
+    cpu_exec_init(env);
+
+    env->flags = 0;
+
+    cpu_reset(CPU(cpu));
+}
+
+static void openrisc_cpu_class_init(ObjectClass *oc, void *data)
+{
+    OPENRISCCPUClass *occ = OPENRISC_CPU_CLASS(oc);
+    CPUClass *cc = CPU_CLASS(oc);
+
+    occ->parent_reset = cc->reset;
+    cc->reset = openrisc_cpu_reset;
+}
+
+static const TypeInfo openrisc_cpu_type_info = {
+    .name = TYPE_OPENRISC_CPU,
+    .parent = TYPE_CPU,
+    .instance_size = sizeof(OPENRISCCPU),
+    .instance_init = openrisc_cpu_initfn,
+    .abstract = false,
+    .class_size = sizeof(OPENRISCCPUClass),
+    .class_init = openrisc_cpu_class_init,
+};
+
+static void openrisc_cpu_register_types(void)
+{
+    type_register_static(&openrisc_cpu_type_info);
+}
+
+type_init(openrisc_cpu_register_types)
target-openrisc/cpu.h new file +163 -0
new file mode 100644
index 0000000..11ea13c
@@ -0,0 +1,299 @@
+/*
+ *  Openrisc virtual CPU header.
+ *
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef CPU_OPENRISC_H
+#define CPU_OPENRISC_H
+
+#define TARGET_HAS_ICE 1
+
+#define ELF_MACHINE EM_OPENRISC
+
+#define CPUArchState struct CPUOPENRISCState
+
+#define TARGET_LONG_BITS 32
+
+#include "config.h"
+#include "qemu-common.h"
+#include "cpu-defs.h"
+#include "softfloat.h"
+
+struct CPUOPENRISCState;
+
+#define NB_MMU_MODES 3
+#define MMU_NOMMU_IDX   0
+#define MMU_SUPERVISOR_IDX  1
+#define MMU_USER_IDX    2
+
+#define TARGET_PAGE_BITS 13
+
+
+#define TARGET_PHYS_ADDR_SPACE_BITS 32
+#define TARGET_VIRT_ADDR_SPACE_BITS 32
+
+#define SET_FP_CAUSE(reg, v)    do {\
+                                    (reg) = ((reg) & ~(0x3f << 12)) | \
+                                            ((v & 0x3f) << 12);\
+                                } while (0)
+#define GET_FP_ENABLE(reg)       (((reg) >>  7) & 0x1f)
+#define UPDATE_FP_FLAGS(reg, v)   do {\
+                                      (reg) |= ((v & 0x1f) << 2);\
+                                  } while (0)
+
+/* Internel flags, delay slot flag */
+#define D_FLAG    1
+
+#define NR_IRQS  32
+#define PIC_MASK 0xFFFFFFFF
+
+/* Verison Register */
+#define SPR_VR       0x12000001
+#define SPR_CPUCFGR  0x12000001
+
+/* Registers */
+enum {
+    R0 = 0, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10,
+    R11, R12, R13, R14, R15, R16, R17, R18, R19, R20,
+    R21, R22, R23, R24, R25, R26, R27, R28, R29, R30,
+    R31
+};
+
+/* Register aliases */
+enum {
+    R_ZERO = R0,
+    R_SP = R1,
+    R_FP = R2,
+    R_LR = R9,
+    R_RV = R11,
+    R_RVH = R12
+};
+
+/* Exceptions indices */
+enum {
+    EXCP_RESET    = 0x1,
+    EXCP_BUSERR   = 0x2,
+    EXCP_DPF      = 0x3,
+    EXCP_IPF      = 0x4,
+    EXCP_TICK     = 0x5,
+    EXCP_ALIGN    = 0x6,
+    EXCP_ILLEGAL  = 0x7,
+    EXCP_INT      = 0x8,
+    EXCP_DTLBMISS = 0x9,
+    EXCP_ITLBMISS = 0xa,
+    EXCP_RANGE    = 0xb,
+    EXCP_SYSCALL  = 0xc,
+    EXCP_FPE      = 0xd,
+    EXCP_TRAP     = 0xe,
+    EXCP_NR,
+};
+
+/* Supervisor register */
+enum {
+    SR_SM = 1,
+    SR_TEE = (1<<1),
+    SR_IEE = (1<<2),
+    SR_DCE = (1<<3),
+    SR_ICE = (1<<4),
+    SR_DME = (1<<5),
+    SR_IME = (1<<6),
+    SR_LEE = (1<<7),
+    SR_CE  = (1<<8),
+    SR_F   = (1<<9),
+    SR_CY  = (1<<10),
+    SR_OV  = (1<<11),
+    SR_OVE = (1<<12),
+    SR_DSX = (1<<13),
+    SR_EPH = (1<<14),
+    SR_FO  = (1<<15),
+    SR_SUMRA = (1<<16),
+    SR_SCE = (1<<17),
+};
+
+/* FPCSR register */
+enum {
+    FPCSR_FPEE = 1,
+    FPCSR_RM = (3 << 1),
+    FPCSR_OVF = (1 << 3),
+    FPCSR_UNF = (1 << 4),
+    FPCSR_SNF = (1 << 5),
+    FPCSR_QNF = (1 << 6),
+    FPCSR_ZF = (1 << 7),
+    FPCSR_IXF = (1 << 8),
+    FPCSR_IVF = (1 << 9),
+    FPCSR_INF = (1 << 10),
+    FPCSR_DZF = (1 << 11),
+};
+
+/* TTMR bit */
+enum {
+    TTMR_TP = (0xfffffff),
+    TTMR_IP = (1<<28),
+    TTMR_IE = (1<<29),
+    TTMR_M  = (3<<30),
+};
+
+enum {
+    DTLB_WAYS = 1,
+    DTLB_SIZE = 64,
+    DTLB_MASK = (DTLB_SIZE-1),
+    ITLB_WAYS = 1,
+    ITLB_SIZE = 64,
+    ITLB_MASK = (ITLB_SIZE-1),
+};
+
+/* TLB prot */
+enum {
+    URE = (1<<6),
+    UWE = (1<<7),
+    SRE = (1<<8),

target-openrisc/cpu.h line 173

Quoted 10 lines
target-openrisc/cpu.h new file +10 -0
+    SWE = (1<<9),
+
+    SXE = (1<<6),
+    UXE = (1<<7),
+};
+
+typedef struct tlb_entry {
+    uint32_t mr;
+    uint32_t tr;
+} tlb_entry;

TLBEntry

target-openrisc/cpu.h line 186

Quoted 13 lines
target-openrisc/cpu.h new file +13 -0
+
+typedef struct CPUOPENRISCState CPUOPENRISCState;
+struct CPUOPENRISCState {
+    target_ulong gpr[32];   /* General registers */
+    uint32_t sr;            /* Supervisor register */
+    target_ulong machi;     /* Multiply register  MACHI */
+    target_ulong maclo;     /* Multiply register  MACLO */
+    target_ulong fpmaddhi;  /* Multiply and add float register FPMADDHI */
+    target_ulong fpmaddlo;  /* Multiply and add float register FPMADDLO */
+    target_ulong epcr;      /* Exception PC register */
+    target_ulong eear;      /* Exception EA register */
+    uint32_t esr;           /* Exception supervisor register */
+    void *irq[32];          /* Interrupt irq input */

CPU reset usually zeros all fields up to breakpoints field in CPU_COMMON. Then these and the MMU function pointers below would be broken, please move below CPU_COMMON.

Quoted 78 lines
target-openrisc/cpu.h new file +78 -0
+    uint32_t fpcsr;         /* Float register */
+    float_status fp_status;
+    target_ulong pc;        /* Program counter */
+    target_ulong npc;       /* Next PC */
+    target_ulong ppc;       /* Prev PC */
+    target_ulong jmp_pc;    /* Jump PC */
+    uint32_t flags;
+
+    /* Branch. */
+    uint32_t btaken;        /* the SR_F bit */
+
+#if !defined(CONFIG_USER_ONLY)
+    /* MMU */
+    tlb_entry(*itlb)[ITLB_SIZE], (*dtlb)[DTLB_SIZE];
+    int (*map_address_code)(struct CPUOPENRISCState *env,
+                            target_phys_addr_t *physical, int *prot,
+                            target_ulong address, int rw);
+    int (*map_address_data)(struct CPUOPENRISCState *env,
+                            target_phys_addr_t *physical, int *prot,
+                            target_ulong address, int rw);
+
+    /* Internal Timer */
+    struct QEMUTimer *timer;
+    uint32_t ttmr;          /* Timer tick mode register */
+    uint32_t ttcr;          /* Timer tick count register */
+
+    uint32_t picmr;         /* Interrupt mask register */
+    uint32_t picsr;         /* Interrupt contrl register*/
+#endif
+
+    CPU_COMMON
+};
+
+#include "cpu-qom.h"
+
+void cpu_openrisc_list(FILE *f, fprintf_function cpu_fprintf);
+CPUOPENRISCState *cpu_openrisc_init(const char *cpu_model);
+int cpu_openrisc_exec(CPUOPENRISCState *s);
+void do_interrupt(CPUOPENRISCState *env);
+int cpu_openrisc_signal_handler(int host_signum, void *pinfo, void *puc);
+void openrisc_translate_init(void);
+int cpu_openrisc_handle_mmu_fault(CPUOPENRISCState *env, target_ulong address,
+                                  int rw, int mmu_idx);
+#define cpu_list cpu_openrisc_list
+#define cpu_init cpu_openrisc_init
+#define cpu_exec cpu_openrisc_exec
+#define cpu_gen_code cpu_openrisc_gen_code
+#define cpu_signal_handler cpu_openrisc_signal_handler
+#define cpu_handle_mmu_fault cpu_openrisc_handle_mmu_fault
+
+void openrisc_reset(CPUOPENRISCState *env);
+#if !defined(CONFIG_USER_ONLY)
+void cpu_openrisc_store_count(CPUOPENRISCState *env, target_ulong count);
+void cpu_openrisc_store_compare(CPUOPENRISCState *env, target_ulong value);
+uint32_t cpu_openrisc_get_count(CPUOPENRISCState *env);
+
+void cpu_openrisc_pic_reset(CPUOPENRISCState *env);
+void cpu_openrisc_store_picsr(CPUOPENRISCState *env, uint32_t value);
+void cpu_openrisc_store_picmr(CPUOPENRISCState *env, uint32_t value);
+
+void openrisc_mmu_init(CPUOPENRISCState *env);
+int get_phys_nommu(CPUOPENRISCState *env, target_phys_addr_t *physical,
+                   int *prot, target_ulong address, int rw);
+int get_phys_code(CPUOPENRISCState *env, target_phys_addr_t *physical,
+                  int *prot, target_ulong address, int rw);
+int get_phys_data(CPUOPENRISCState *env, target_phys_addr_t *physical,
+                  int *prot, target_ulong address, int rw);
+#endif
+
+#if defined(CONFIG_USER_ONLY)
+static inline void cpu_clone_regs(CPUOPENRISCState *env, target_ulong newsp)
+{
+    if (newsp) {
+        env->gpr[1] = newsp;
+    }
+    env->gpr[2] = 0;
+}
+#endif

target-openrisc/cpu.h line 274

Quoted 10 lines
target-openrisc/cpu.h new file +10 -0
+
+#include "cpu-all.h"
+
+static inline void cpu_get_tb_cpu_state(CPUOPENRISCState *env,
+                                        target_ulong *pc,
+                                        target_ulong *cs_base, int *flags)
+{
+    *pc = env->pc;
+    *cs_base = 0;
+    *flags = env->flags;

Maybe it's fixed in later patches, but this does not look correct, the TB flags tell when the CPU mode is compatible with the previously generated translated blocks. I'd expect only MMU enable/disable flags to be included here (assuming that if MMU state changes, TBs are flushed), supervisor flag also if the translator checks the status during translation.

Quoted 48 lines
target-openrisc/cpu.h new file +25 -0
+}
+
+static inline int cpu_mmu_index(CPUOPENRISCState *env)
+{
+    if (!(env->sr & SR_IME)) {
+        return MMU_NOMMU_IDX;
+    }
+    return (env->sr & SR_SM) == 0 ? MMU_USER_IDX : MMU_SUPERVISOR_IDX;
+}
+
+#define CPU_INTERRUPT_TIMER   CPU_INTERRUPT_TGT_INT_0
+static inline bool cpu_has_work(CPUOPENRISCState *env)
+{
+    return env->interrupt_request & (CPU_INTERRUPT_HARD |
+                                     CPU_INTERRUPT_TIMER);
+}
+
+#include "exec-all.h"
+
+static inline void cpu_pc_from_tb(CPUOPENRISCState *env, TranslationBlock *tb)
+{
+    env->pc = tb->pc;
+}
+
+#endif /* CPU_OPENRISC_H */
target-openrisc/helper.c new file +17 -0
new file mode 100644
index 0000000..dcb61c9
@@ -0,0 +1,67 @@
+/*
+ * Openrisc helpers
+ *
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software

target-openrisc/helper.c line 27

Quoted 10 lines
target-openrisc/helper.c new file +10 -0
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
+ */
+
+#include "cpu.h"
+#include "qemu-common.h"
+#include "gdbstub.h"
+#include "helper.h"
+#include "host-utils.h"
+#if !defined(CONFIG_USER_ONLY)
+#include "hw/loader.h"

Trim?

Quoted 20 lines
target-openrisc/helper.c new file +20 -0
+#endif
+
+typedef struct {
+    const char *name;
+} OPENRISCDef;
+
+static const OPENRISCDef openrisc_defs[] = {
+    {.name = "or1200",}
+};
+
+void cpu_openrisc_list(FILE *f, fprintf_function cpu_fprintf)
+{
+    int i;
+
+    cpu_fprintf(f, "Available CPUs:\n");
+    for (i = 0; i < ARRAY_SIZE(openrisc_defs); ++i) {
+        cpu_fprintf(f, "  %s\n", openrisc_defs[i].name);
+    }
+}
+

target-openrisc/helper.c line 57

Quoted 10 lines
target-openrisc/helper.c new file +10 -0
+CPUOPENRISCState *cpu_openrisc_init(const char *cpu_model)
+{
+    CPUOPENRISCState *env;
+    static int tcg_inited;
+
+    env = g_malloc0(sizeof(*env));
+    memset(env, 0, sizeof(*env));
+    cpu_exec_init(env);
+    qemu_init_vcpu(env);
+    if (!tcg_inited) {

To enable qtest, please add && tcg_enabled().

Quoted 189 lines
target-openrisc/helper.c new file +10 -0
+        tcg_inited = 1;
+        openrisc_translate_init();
+    }
+
+    return env;
+}
+
+void do_interrupt(CPUOPENRISCState *env)
+{
+}
target-openrisc/helper.h new file +23 -0
new file mode 100644
index 0000000..103d9b4
@@ -0,0 +1,23 @@
+/*
+ * OpenRISC helper defines
+ *
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
+ */
+
+#include "def-helper.h"
+
+#include "def-helper.h"
target-openrisc/machine.c new file +76 -0
new file mode 100644
index 0000000..c7ac0ea
@@ -0,0 +1,76 @@
+/*
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
+ */
+
+#include "hw/hw.h"
+#include "hw/boards.h"
+#include "kvm.h"
+
+static const VMStateDescription vmstate_cpu = {
+    .name = "cpu",
+    .fields = (VMStateField[]) {
+        VMSTATE_UINT32_ARRAY(gpr, CPUOPENRISCState, 32),
+        VMSTATE_UINT32(sr, CPUOPENRISCState),
+        VMSTATE_UINT32(epcr, CPUOPENRISCState),
+        VMSTATE_UINT32(eear, CPUOPENRISCState),
+        VMSTATE_UINT32(esr, CPUOPENRISCState),
+        VMSTATE_UINT32(fpcsr, CPUOPENRISCState),
+        VMSTATE_UINT32(pc, CPUOPENRISCState),
+        VMSTATE_UINT32(npc, CPUOPENRISCState),
+        VMSTATE_UINT32(ppc, CPUOPENRISCState),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+void cpu_save(QEMUFile *f, void *opaque)
+{
+    CPUOPENRISCState *env = (CPUOPENRISCState *)opaque;
+    unsigned int i;
+
+    for (i = 0; i < 32; i++) {
+        qemu_put_betls(f, &env->gpr[i]);
+    }
+
+    qemu_put_betls(f, &env->epcr);
+    qemu_put_betls(f, &env->eear);
+    qemu_put_betls(f, &env->esr);
+
+    qemu_put_betls(f, &env->sr);
+    qemu_put_be32s(f, &env->pc);
+    qemu_put_be32s(f, &env->fpcsr);
+}
+
+int cpu_load(QEMUFile *f, void *opaque, int version_id)
+{
+    CPUOPENRISCState *env = (CPUOPENRISCState *)opaque;
+    unsigned int i;
+
+    for (i = 0; i < 32; i++) {
+        qemu_get_betls(f, &env->gpr[i]);
+    }
+
+    qemu_get_betls(f, &env->epcr);
+    qemu_get_betls(f, &env->eear);
+    qemu_get_betls(f, &env->esr);
+
+    qemu_get_betls(f, &env->sr);
+    qemu_get_betls(f, &env->pc);
+    qemu_get_be32s(f, &env->fpcsr);
+    tlb_flush(env, 1);
+
+    return 0;
+}
target-openrisc/mem.c new file +43 -0
new file mode 100644
index 0000000..57a0b1b
@@ -0,0 +1,43 @@
+/*
+ *  Openrisc MMU.
+ *
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *                          Zhizhou Zhang <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "cpu.h"
+#include "qemu-common.h"
+#include "gdbstub.h"
+#include "helper.h"
+#include "host-utils.h"
+#if !defined(CONFIG_USER_ONLY)
+#include "hw/loader.h"
+#endif
+
+#if !defined(CONFIG_USER_ONLY)
+target_phys_addr_t cpu_get_phys_page_debug(CPUOPENRISCState *env,
+                                           target_ulong addr)
+{
+    return 0;
+}
+#endif
+
+void openrisc_reset(CPUOPENRISCState *env)
+{
+    env->pc = 0x100;
+    env->sr = SR_FO | SR_SM;
+    env->exception_index = -1;
+}
target-openrisc/mem_helper.c new file +13 -0
new file mode 100644
index 0000000..a2d93c7
@@ -0,0 +1,46 @@
+/*
+ * OpenRISC mmu helper routines
+ *
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *                          Zhizhou Zhang <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of

target-openrisc/mem_helper.c line 23

Quoted 10 lines
target-openrisc/mem_helper.c new file +10 -0
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
+ */
+
+#include "cpu.h"
+#include "dyngen-exec.h"

As I mention in the later patch, this makes AREG0 to be used for CPUState (also HELPER_CFLAGS is not used to compile mem_helper.o). Please avoid it, then you also need to adjust CONFIG_TCG_PASS_AREG0 case in configure.

Quoted 123 lines
target-openrisc/mem_helper.c new file +23 -0
+#include "helper.h"
+
+#if !defined(CONFIG_USER_ONLY)
+#include "softmmu_exec.h"
+#define MMUSUFFIX _mmu
+
+#define SHIFT 0
+#include "softmmu_template.h"
+
+#define SHIFT 1
+#include "softmmu_template.h"
+
+#define SHIFT 2
+#include "softmmu_template.h"
+
+#define SHIFT 3
+#include "softmmu_template.h"
+
+void tlb_fill(CPUOPENRISCState *env1, target_ulong addr, int is_write,
+              int mmu_idx, uintptr_t retaddr)
+{
+}
+#endif
target-openrisc/translate.c new file +90 -1
new file mode 100644
index 0000000..aae483a
@@ -0,0 +1,90 @@
+/*
+ * Openrisc translation
+ *
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *                          Feng Gao <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
+ */
+
+#include <stdarg.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <inttypes.h>
+#include <assert.h>
+
+#include "cpu.h"
+#include "exec-all.h"
+#include "disas.h"
+#include "tcg-op.h"
+#include "qemu-common.h"
+#include "qemu-log.h"
+#include "config.h"
+
+#include "helper.h"
+#define GEN_HELPER 1
+#include "helper.h"
+
+#define DISAS_OPENRISC 1
+#if DISAS_OPENRISC
+#  define LOG_DIS(...) do { } while (0)
+#endif
+
+typedef struct DisasContext {
+    CPUOPENRISCState *env;
+    TranslationBlock *tb;
+    target_ulong pc;
+    target_ulong ppc, npc;
+    uint32_t tb_flags;
+    uint32_t is_jmp;
+    uint32_t mem_idx;
+    int singlestep_enabled;
+    uint32_t delayed_branch;
+} DisasContext;
+
+void openrisc_translate_init(void)
+{
+}
+
+static inline void gen_intermediate_code_internal(CPUOPENRISCState *env,
+                                                  TranslationBlock *tb,
+                                                  int search_pc)
+{
+}
+
+void gen_intermediate_code(CPUOPENRISCState *env, struct TranslationBlock *tb)
+{
+    gen_intermediate_code_internal(env, tb, 0);
+}
+
+void gen_intermediate_code_pc(CPUOPENRISCState *env,
+                              struct TranslationBlock *tb)
+{
+    gen_intermediate_code_internal(env, tb, 1);
+}
+
+void cpu_dump_state(CPUOPENRISCState *env, FILE *f,
+                    fprintf_function cpu_fprintf,
+                    int flags)
+{
+    cpu_fprintf(f, "PC=%08x\n", env->pc);
+}
+
+void restore_state_to_opc(CPUOPENRISCState *env, TranslationBlock *tb,
+                          int pc_pos)
+{
+    env->pc = gen_opc_pc[pc_pos];
+}
--

1.7.9.5

#10 lore raw

Re: [Qemu-devel] [PATCH 01/15] Openrisc: add target stub

Am 19.05.2012 10:51, schrieb Blue Swirl:

Quoted 10 lines · Jia Liu
Quoted 9 lines
target-openrisc/cpu.h new file +9 -0
+
+typedef struct CPUOPENRISCState CPUOPENRISCState;
+struct CPUOPENRISCState {
+    target_ulong gpr[32];   /* General registers */
+    uint32_t sr;            /* Supervisor register */
+    target_ulong machi;     /* Multiply register  MACHI */
+    target_ulong maclo;     /* Multiply register  MACLO */
+    target_ulong fpmaddhi;  /* Multiply and add float register FPMADDHI */
+    target_ulong fpmaddlo;  /* Multiply and add float register FPMADDLO */
Quoted 8 lines

target-openrisc/cpu.h line 186

Quoted 4 lines
target-openrisc/cpu.h new file +4 -0
+    target_ulong epcr;      /* Exception PC register */
+    target_ulong eear;      /* Exception EA register */
+    uint32_t esr;           /* Exception supervisor register */
+    void *irq[32];          /* Interrupt irq input */

CPU reset usually zeros all fields up to breakpoints field in CPU_COMMON. Then these and the MMU function pointers below would be broken, please move below CPU_COMMON.

...or into OpenRISCCPU, if it is not used with offsetof().

/-F

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
author
#11 lore raw

Re: [Qemu-devel] [PATCH 01/15] Openrisc: add target stub

In reply to Blue Swirl

Hi Blue,

Quoted 139 lines · Blue Swirl
Quoted 138 lines · Jia Liu

add the openrisc target stub and basic implementation.

---
 Makefile.target                  |    3 +
 arch_init.c                      |    2 +
 arch_init.h                      |    1 +
 configure                        |    8 +-
 cpu-exec.c                       |    2 + default-configs/or32-softmmu.mak |    6 +
 elf.h                            |    2 +
 poison.h                         |    1 + target-openrisc/cpu-qom.h        |   70 +++++++++ target-openrisc/cpu.c            |   74 ++++++++++ target-openrisc/cpu.h            |  299 ++++++++++++++++++++++++++++++++++++++

 target-openrisc/helper.c         |   67 +++++++++
 target-openrisc/helper.h         |   23 +++
 target-openrisc/machine.c        |   76 ++++++++++
 target-openrisc/mem.c            |   43 ++++++
 target-openrisc/mem_helper.c     |   46 ++++++
 target-openrisc/translate.c      |   90 ++++++++++++
 17 files changed, 812 insertions(+), 1 deletion(-)

 create mode 100644 default-configs/or32-softmmu.mak create mode 100644 target-openrisc/cpu-qom.h create mode 100644 target-openrisc/cpu.c create mode 100644 target-openrisc/cpu.h create mode 100644 target-openrisc/helper.c create mode 100644 target-openrisc/helper.h create mode 100644 target-openrisc/machine.c create mode 100644 target-openrisc/mem.c create mode 100644 target-openrisc/mem_helper.c create mode 100644 target-openrisc/translate.c

Makefile.target +0 -0
index 1582904..0ffb29c 100644
@@ -83,9 +83,11 @@ libobj-$(CONFIG_TCG_INTERPRETER) += tci.o

 libobj-y += fpu/softfloat.o
 ifneq ($(TARGET_BASE_ARCH), sparc)
 ifneq ($(TARGET_BASE_ARCH), alpha)
+ifneq ($(TARGET_BASE_ARCH), openrisc)
 libobj-y += op_helper.o
 endif
 endif
+endif
 libobj-y += helper.o
 ifneq ($(TARGET_BASE_ARCH), ppc)
 libobj-y += cpu.o

Makefile.target +0 -0
@@ -99,6 +101,7 @@ endif

 libobj-$(TARGET_SPARC) += int32_helper.o libobj-$(TARGET_SPARC64) += int64_helper.o libobj-$(TARGET_ALPHA) += int_helper.o fpu_helper.o sys_helper.o mem_helper.o +libobj-$(TARGET_OPENRISC) += mem.o mem_helper.o

 libobj-y += disas.o
 libobj-$(CONFIG_TCI_DIS) += tci-dis.o

arch_init.c +2 -0
index 988adca..55b608d 100644
@@ -71,6 +71,8 @@ int graphic_depth = 15;
  #define QEMU_ARCH QEMU_ARCH_MICROBLAZE
  #elif defined(TARGET_MIPS)
  #define QEMU_ARCH QEMU_ARCH_MIPS
+#elif defined(TARGET_OPENRISC)
+#define QEMU_ARCH QEMU_ARCH_OPENRISC
  #elif defined(TARGET_PPC)
  #define QEMU_ARCH QEMU_ARCH_PPC
  #elif defined(TARGET_S390X)
arch_init.h +1 -0
index c7cb94a..3dfea3b 100644
@@ -16,6 +16,7 @@ enum {
     QEMU_ARCH_SH4 = 1024,
     QEMU_ARCH_SPARC = 2048,
     QEMU_ARCH_XTENSA = 4096,
+    QEMU_ARCH_OPENRISC = 8192,
  };
 
  extern const uint32_t arch_type;
configure +1 -0
index b55a792..63e2372 100755
@@ -924,6 +924,7 @@ mips-softmmu \
  mipsel-softmmu \
  mips64-softmmu \
  mips64el-softmmu \
+or32-softmmu \
  ppc-softmmu \
  ppcemb-softmmu \
  ppc64-softmmu \
@@ -3460,7 +3461,7 @@ target_arch2=`echo $target | cut -d '-' -f 1`

 target_bigendian="no"

 case "$target_arch2" in

-  armeb|lm32|m68k|microblaze|mips|mipsn32|mips64|ppc|ppcemb|ppc64|ppc64abi32|s390x|sh4eb|sparc|sparc64|sparc32plus|xtensaeb)
+  armeb|lm32|m68k|microblaze|mips|mipsn32|mips64|or32|ppc|ppcemb|ppc64|ppc64abi32|s390x|sh4eb|sparc|sparc64|sparc32plus|xtensaeb)

  target_bigendian=yes
  ;;
 esac

configure +0 -0
@@ -3588,6 +3589,11 @@ case "$target_arch2" in

    target_phys_bits=64
    target_long_alignment=8
  ;;
+  or32)
+    TARGET_ARCH=openrisc
+    TARGET_BASE_ARCH=openrisc
+    target_phys_bits=32
+  ;;
  ppc)
    gdb_xml_files="power-core.xml power-fpu.xml power-altivec.xml power-spe.xml" target_phys_bits=64

cpu-exec.c +2 -0
index 0344cd5..ba10db1 100644
@@ -222,6 +222,7 @@ int cpu_exec(CPUArchState *env)
  #elif defined(TARGET_LM32)
  #elif defined(TARGET_MICROBLAZE)
  #elif defined(TARGET_MIPS)
+#elif defined(TARGET_OPENRISC)
  #elif defined(TARGET_SH4)
  #elif defined(TARGET_CRIS)
  #elif defined(TARGET_S390X)
@@ -620,6 +621,7 @@ int cpu_exec(CPUArchState *env)
               | env->cc_dest | (env->cc_x << 4);
  #elif defined(TARGET_MICROBLAZE)
  #elif defined(TARGET_MIPS)
+#elif defined(TARGET_OPENRISC)
  #elif defined(TARGET_SH4)
  #elif defined(TARGET_ALPHA)
  #elif defined(TARGET_CRIS)
default-configs/or32-softmmu.mak new file +0 -0
new file mode 100644
index 0000000..7590eed
Quoted 9 lines

default-configs/or32-softmmu.mak line 3

Quoted 6 lines
default-configs/or32-softmmu.mak new file +3 -0
@@ -0,0 +1,6 @@
+# Default configuration for or32-softmmu
+
+include pci.mak

Probably not needed?

NE2000 need it.

Quoted 375 lines
Quoted 353 lines
default-configs/or32-softmmu.mak new file +3 -0
+CONFIG_SERIAL=y
+CONFIG_NE2000_ISA=y
+CONFIG_I8259=y
elf.h +2 -0
index e1422b8..e65f4b9 100644
@@ -106,6 +106,8 @@ typedef int64_t  Elf64_Sxword;
  #define EM_H8S          48      /* Hitachi H8S     */
  #define EM_LATTICEMICO32 138    /* LatticeMico32 */
 
+#define EM_OPENRISC     92        /* Opencores Openrisc */
+
  #define EM_UNICORE32    110     /* UniCore32 */
 
  /*
poison.h +1 -0
index d396f20..7d7b23b 100644
@@ -14,6 +14,7 @@
  #pragma GCC poison TARGET_M68K
  #pragma GCC poison TARGET_MIPS
  #pragma GCC poison TARGET_MIPS64
+#pragma GCC poison TARGET_OPENRISC
  #pragma GCC poison TARGET_PPC
  #pragma GCC poison TARGET_PPCEMB
  #pragma GCC poison TARGET_PPC64
target-openrisc/cpu-qom.h new file +70 -0
new file mode 100644
index 0000000..8c936df
@@ -0,0 +1,70 @@
+/*
+ *  QEMU Openrisc CPU
+ *
+ *  Copyright (c) 2012 Jia Liu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef QEMU_OPENRISC_CPU_QOM_H
+#define QEMU_OPENRISC_CPU_QOM_H
+
+#include "qemu/cpu.h"
+#include "cpu.h"
+
+#define TYPE_OPENRISC_CPU "or32"
+
+#define OPENRISC_CPU_CLASS(klass) \
+    OBJECT_CLASS_CHECK(OPENRISCCPUClass, (klass), TYPE_OPENRISC_CPU)
+#define OPENRISC_CPU(obj) \
+    OBJECT_CHECK(OPENRISCCPU, (obj), TYPE_OPENRISC_CPU)
+#define OPENRISC_CPU_GET_CLASS(obj) \
+    OBJECT_GET_CLASS(OPENRISCCPUClass, (obj), TYPE_OPENRISC_CPU)
+
+/**
+ * OPENRISCCPUClass:
+ * @parent_reset: The parent class' reset handler.
+ *
+ * A Openrisc CPU model.
+ */
+typedef struct OPENRISCCPUClass {
+    /*< private >*/
+    CPUClass parent_class;
+    /*< public >*/
+
+    void (*parent_reset)(CPUState *cpu);
+} OPENRISCCPUClass;
+
+/**
+ * OPENRISCCPU:
+ * @env: #CPUOPENRISCState
+ *
+ * A Openrisc CPU.
+ */
+typedef struct OPENRISCCPU {
+    /*< private >*/
+    CPUState parent_obj;
+    /*< public >*/
+
+    CPUOPENRISCState env;
+} OPENRISCCPU;
+
+static inline OPENRISCCPU *openrisc_env_get_cpu(CPUOPENRISCState *env)
+{
+    return OPENRISC_CPU(container_of(env, OPENRISCCPU, env));
+}
+
+#define ENV_GET_CPU(e) CPU(openrisc_env_get_cpu(e))
+
+#endif /* QEMU_OPENRISC_CPU_QOM_H */
target-openrisc/cpu.c new file +74 -0
new file mode 100644
index 0000000..01e65a2
@@ -0,0 +1,74 @@
+/*
+ *  QEMU Openrisc CPU
+ *
+ *  Copyright (c) 2012 Jia Liu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "cpu.h"
+#include "cpu-qom.h"
+#include "qemu-common.h"
+
+
+/* CPUClass::reset() */
+static void openrisc_cpu_reset(CPUState *s)
+{
+    OPENRISCCPU *cpu = OPENRISC_CPU(s);
+    OPENRISCCPUClass *occ = OPENRISC_CPU_GET_CLASS(cpu);
+    CPUOPENRISCState *env = &cpu->env;
+
+    occ->parent_reset(s);
+
+    openrisc_reset(env);
+
+}
+
+static void openrisc_cpu_initfn(Object *obj)
+{
+    OPENRISCCPU *cpu = OPENRISC_CPU(obj);
+    CPUOPENRISCState *env = &cpu->env;
+
+    cpu_exec_init(env);
+
+    env->flags = 0;
+
+    cpu_reset(CPU(cpu));
+}
+
+static void openrisc_cpu_class_init(ObjectClass *oc, void *data)
+{
+    OPENRISCCPUClass *occ = OPENRISC_CPU_CLASS(oc);
+    CPUClass *cc = CPU_CLASS(oc);
+
+    occ->parent_reset = cc->reset;
+    cc->reset = openrisc_cpu_reset;
+}
+
+static const TypeInfo openrisc_cpu_type_info = {
+    .name = TYPE_OPENRISC_CPU,
+    .parent = TYPE_CPU,
+    .instance_size = sizeof(OPENRISCCPU),
+    .instance_init = openrisc_cpu_initfn,
+    .abstract = false,
+    .class_size = sizeof(OPENRISCCPUClass),
+    .class_init = openrisc_cpu_class_init,
+};
+
+static void openrisc_cpu_register_types(void)
+{
+    type_register_static(&openrisc_cpu_type_info);
+}
+
+type_init(openrisc_cpu_register_types)
target-openrisc/cpu.h new file +163 -0
new file mode 100644
index 0000000..11ea13c
@@ -0,0 +1,299 @@
+/*
+ *  Openrisc virtual CPU header.
+ *
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef CPU_OPENRISC_H
+#define CPU_OPENRISC_H
+
+#define TARGET_HAS_ICE 1
+
+#define ELF_MACHINE EM_OPENRISC
+
+#define CPUArchState struct CPUOPENRISCState
+
+#define TARGET_LONG_BITS 32
+
+#include "config.h"
+#include "qemu-common.h"
+#include "cpu-defs.h"
+#include "softfloat.h"
+
+struct CPUOPENRISCState;
+
+#define NB_MMU_MODES 3
+#define MMU_NOMMU_IDX   0
+#define MMU_SUPERVISOR_IDX  1
+#define MMU_USER_IDX    2
+
+#define TARGET_PAGE_BITS 13
+
+
+#define TARGET_PHYS_ADDR_SPACE_BITS 32
+#define TARGET_VIRT_ADDR_SPACE_BITS 32
+
+#define SET_FP_CAUSE(reg, v)    do {\
+                                    (reg) = ((reg) & ~(0x3f << 12)) | \
+                                            ((v & 0x3f) << 12);\
+                                } while (0)
+#define GET_FP_ENABLE(reg)       (((reg) >>  7) & 0x1f)
+#define UPDATE_FP_FLAGS(reg, v)   do {\
+                                      (reg) |= ((v & 0x1f) << 2);\
+                                  } while (0)
+
+/* Internel flags, delay slot flag */
+#define D_FLAG    1
+
+#define NR_IRQS  32
+#define PIC_MASK 0xFFFFFFFF
+
+/* Verison Register */
+#define SPR_VR       0x12000001
+#define SPR_CPUCFGR  0x12000001
+
+/* Registers */
+enum {
+    R0 = 0, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10,
+    R11, R12, R13, R14, R15, R16, R17, R18, R19, R20,
+    R21, R22, R23, R24, R25, R26, R27, R28, R29, R30,
+    R31
+};
+
+/* Register aliases */
+enum {
+    R_ZERO = R0,
+    R_SP = R1,
+    R_FP = R2,
+    R_LR = R9,
+    R_RV = R11,
+    R_RVH = R12
+};
+
+/* Exceptions indices */
+enum {
+    EXCP_RESET    = 0x1,
+    EXCP_BUSERR   = 0x2,
+    EXCP_DPF      = 0x3,
+    EXCP_IPF      = 0x4,
+    EXCP_TICK     = 0x5,
+    EXCP_ALIGN    = 0x6,
+    EXCP_ILLEGAL  = 0x7,
+    EXCP_INT      = 0x8,
+    EXCP_DTLBMISS = 0x9,
+    EXCP_ITLBMISS = 0xa,
+    EXCP_RANGE    = 0xb,
+    EXCP_SYSCALL  = 0xc,
+    EXCP_FPE      = 0xd,
+    EXCP_TRAP     = 0xe,
+    EXCP_NR,
+};
+
+/* Supervisor register */
+enum {
+    SR_SM = 1,
+    SR_TEE = (1<<1),
+    SR_IEE = (1<<2),
+    SR_DCE = (1<<3),
+    SR_ICE = (1<<4),
+    SR_DME = (1<<5),
+    SR_IME = (1<<6),
+    SR_LEE = (1<<7),
+    SR_CE  = (1<<8),
+    SR_F   = (1<<9),
+    SR_CY  = (1<<10),
+    SR_OV  = (1<<11),
+    SR_OVE = (1<<12),
+    SR_DSX = (1<<13),
+    SR_EPH = (1<<14),
+    SR_FO  = (1<<15),
+    SR_SUMRA = (1<<16),
+    SR_SCE = (1<<17),
+};
+
+/* FPCSR register */
+enum {
+    FPCSR_FPEE = 1,
+    FPCSR_RM = (3 << 1),
+    FPCSR_OVF = (1 << 3),
+    FPCSR_UNF = (1 << 4),
+    FPCSR_SNF = (1 << 5),
+    FPCSR_QNF = (1 << 6),
+    FPCSR_ZF = (1 << 7),
+    FPCSR_IXF = (1 << 8),
+    FPCSR_IVF = (1 << 9),
+    FPCSR_INF = (1 << 10),
+    FPCSR_DZF = (1 << 11),
+};
+
+/* TTMR bit */
+enum {
+    TTMR_TP = (0xfffffff),
+    TTMR_IP = (1<<28),
+    TTMR_IE = (1<<29),
+    TTMR_M  = (3<<30),
+};
+
+enum {
+    DTLB_WAYS = 1,
+    DTLB_SIZE = 64,
+    DTLB_MASK = (DTLB_SIZE-1),
+    ITLB_WAYS = 1,
+    ITLB_SIZE = 64,
+    ITLB_MASK = (ITLB_SIZE-1),
+};
+
+/* TLB prot */
+enum {
+    URE = (1<<6),
+    UWE = (1<<7),
+    SRE = (1<<8),

target-openrisc/cpu.h line 173

Quoted 10 lines
target-openrisc/cpu.h new file +10 -0
+    SWE = (1<<9),
+
+    SXE = (1<<6),
+    UXE = (1<<7),
+};
+
+typedef struct tlb_entry {
+    uint32_t mr;
+    uint32_t tr;
+} tlb_entry;

TLBEntry

Quoted 9 lines
target-openrisc/cpu.h new file +9 -0
+
+typedef struct CPUOPENRISCState CPUOPENRISCState;
+struct CPUOPENRISCState {
+    target_ulong gpr[32];   /* General registers */
+    uint32_t sr;            /* Supervisor register */
+    target_ulong machi;     /* Multiply register  MACHI */
+    target_ulong maclo;     /* Multiply register  MACLO */
+    target_ulong fpmaddhi;  /* Multiply and add float register FPMADDHI */
+    target_ulong fpmaddlo;  /* Multiply and add float register FPMADDLO */
Quoted 9 lines

target-openrisc/cpu.h line 186

Quoted 4 lines
target-openrisc/cpu.h new file +4 -0
+    target_ulong epcr;      /* Exception PC register */
+    target_ulong eear;      /* Exception EA register */
+    uint32_t esr;           /* Exception supervisor register */
+    void *irq[32];          /* Interrupt irq input */

CPU reset usually zeros all fields up to breakpoints field in CPU_COMMON. Then these and the MMU function pointers below would be broken, please move below CPU_COMMON.

thanks, I will.

Quoted 87 lines
Quoted 87 lines
target-openrisc/cpu.h new file +87 -0
+    uint32_t fpcsr;         /* Float register */
+    float_status fp_status;
+    target_ulong pc;        /* Program counter */
+    target_ulong npc;       /* Next PC */
+    target_ulong ppc;       /* Prev PC */
+    target_ulong jmp_pc;    /* Jump PC */
+    uint32_t flags;
+
+    /* Branch. */
+    uint32_t btaken;        /* the SR_F bit */
+
+#if !defined(CONFIG_USER_ONLY)
+    /* MMU */
+    tlb_entry(*itlb)[ITLB_SIZE], (*dtlb)[DTLB_SIZE];
+    int (*map_address_code)(struct CPUOPENRISCState *env,
+                            target_phys_addr_t *physical, int *prot,
+                            target_ulong address, int rw);
+    int (*map_address_data)(struct CPUOPENRISCState *env,
+                            target_phys_addr_t *physical, int *prot,
+                            target_ulong address, int rw);
+
+    /* Internal Timer */
+    struct QEMUTimer *timer;
+    uint32_t ttmr;          /* Timer tick mode register */
+    uint32_t ttcr;          /* Timer tick count register */
+
+    uint32_t picmr;         /* Interrupt mask register */
+    uint32_t picsr;         /* Interrupt contrl register*/
+#endif
+
+    CPU_COMMON
+};
+
+#include "cpu-qom.h"
+
+void cpu_openrisc_list(FILE *f, fprintf_function cpu_fprintf);
+CPUOPENRISCState *cpu_openrisc_init(const char *cpu_model);
+int cpu_openrisc_exec(CPUOPENRISCState *s);
+void do_interrupt(CPUOPENRISCState *env);
+int cpu_openrisc_signal_handler(int host_signum, void *pinfo, void *puc);
+void openrisc_translate_init(void);
+int cpu_openrisc_handle_mmu_fault(CPUOPENRISCState *env, target_ulong address,
+                                  int rw, int mmu_idx);
+#define cpu_list cpu_openrisc_list
+#define cpu_init cpu_openrisc_init
+#define cpu_exec cpu_openrisc_exec
+#define cpu_gen_code cpu_openrisc_gen_code
+#define cpu_signal_handler cpu_openrisc_signal_handler
+#define cpu_handle_mmu_fault cpu_openrisc_handle_mmu_fault
+
+void openrisc_reset(CPUOPENRISCState *env);
+#if !defined(CONFIG_USER_ONLY)
+void cpu_openrisc_store_count(CPUOPENRISCState *env, target_ulong count);
+void cpu_openrisc_store_compare(CPUOPENRISCState *env, target_ulong value);
+uint32_t cpu_openrisc_get_count(CPUOPENRISCState *env);
+
+void cpu_openrisc_pic_reset(CPUOPENRISCState *env);
+void cpu_openrisc_store_picsr(CPUOPENRISCState *env, uint32_t value);
+void cpu_openrisc_store_picmr(CPUOPENRISCState *env, uint32_t value);
+
+void openrisc_mmu_init(CPUOPENRISCState *env);
+int get_phys_nommu(CPUOPENRISCState *env, target_phys_addr_t *physical,
+                   int *prot, target_ulong address, int rw);
+int get_phys_code(CPUOPENRISCState *env, target_phys_addr_t *physical,
+                  int *prot, target_ulong address, int rw);
+int get_phys_data(CPUOPENRISCState *env, target_phys_addr_t *physical,
+                  int *prot, target_ulong address, int rw);
+#endif
+
+#if defined(CONFIG_USER_ONLY)
+static inline void cpu_clone_regs(CPUOPENRISCState *env, target_ulong newsp)
+{
+    if (newsp) {
+        env->gpr[1] = newsp;
+    }
+    env->gpr[2] = 0;
+}
+#endif
+
+#include "cpu-all.h"
+
+static inline void cpu_get_tb_cpu_state(CPUOPENRISCState *env,
+                                        target_ulong *pc,
+                                        target_ulong *cs_base, int *flags)
+{
+    *pc = env->pc;
+    *cs_base = 0;
Quoted 9 lines

target-openrisc/cpu.h line 274

Quoted 1 line
target-openrisc/cpu.h new file +1 -0
+    *flags = env->flags;

Maybe it's fixed in later patches, but this does not look correct, the TB flags tell when the CPU mode is compatible with the previously generated translated blocks. I'd expect only MMU enable/disable flags to be included here (assuming that if MMU state changes, TBs are flushed), supervisor flag also if the translator checks the status during translation.

thank, I'll fix it.

Quoted 52 lines
Quoted 52 lines
target-openrisc/cpu.h new file +25 -0
+}
+
+static inline int cpu_mmu_index(CPUOPENRISCState *env)
+{
+    if (!(env->sr & SR_IME)) {
+        return MMU_NOMMU_IDX;
+    }
+    return (env->sr & SR_SM) == 0 ? MMU_USER_IDX : MMU_SUPERVISOR_IDX;
+}
+
+#define CPU_INTERRUPT_TIMER   CPU_INTERRUPT_TGT_INT_0
+static inline bool cpu_has_work(CPUOPENRISCState *env)
+{
+    return env->interrupt_request & (CPU_INTERRUPT_HARD |
+                                     CPU_INTERRUPT_TIMER);
+}
+
+#include "exec-all.h"
+
+static inline void cpu_pc_from_tb(CPUOPENRISCState *env, TranslationBlock *tb)
+{
+    env->pc = tb->pc;
+}
+
+#endif /* CPU_OPENRISC_H */
target-openrisc/helper.c new file +21 -0
new file mode 100644
index 0000000..dcb61c9
@@ -0,0 +1,67 @@
+/*
+ * Openrisc helpers
+ *
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
+ */
+
+#include "cpu.h"
Quoted 9 lines

target-openrisc/helper.c line 27

Quoted 6 lines
target-openrisc/helper.c new file +6 -0
+#include "qemu-common.h"
+#include "gdbstub.h"
+#include "helper.h"
+#include "host-utils.h"
+#if !defined(CONFIG_USER_ONLY)
+#include "hw/loader.h"

Trim?

deleted.

Quoted 230 lines
Quoted 20 lines
target-openrisc/helper.c new file +20 -0
+#endif
+
+typedef struct {
+    const char *name;
+} OPENRISCDef;
+
+static const OPENRISCDef openrisc_defs[] = {
+    {.name = "or1200",}
+};
+
+void cpu_openrisc_list(FILE *f, fprintf_function cpu_fprintf)
+{
+    int i;
+
+    cpu_fprintf(f, "Available CPUs:\n");
+    for (i = 0; i < ARRAY_SIZE(openrisc_defs); ++i) {
+        cpu_fprintf(f, "  %s\n", openrisc_defs[i].name);
+    }
+}
+

target-openrisc/helper.c line 57

Quoted 10 lines
target-openrisc/helper.c new file +10 -0
+CPUOPENRISCState *cpu_openrisc_init(const char *cpu_model)
+{
+    CPUOPENRISCState *env;
+    static int tcg_inited;
+
+    env = g_malloc0(sizeof(*env));
+    memset(env, 0, sizeof(*env));
+    cpu_exec_init(env);
+    qemu_init_vcpu(env);
+    if (!tcg_inited) {

To enable qtest, please add && tcg_enabled().

Quoted 196 lines
target-openrisc/helper.c new file +10 -0
+        tcg_inited = 1;
+        openrisc_translate_init();
+    }
+
+    return env;
+}
+
+void do_interrupt(CPUOPENRISCState *env)
+{
+}
target-openrisc/helper.h new file +23 -0
new file mode 100644
index 0000000..103d9b4
@@ -0,0 +1,23 @@
+/*
+ * OpenRISC helper defines
+ *
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
+ */
+
+#include "def-helper.h"
+
+#include "def-helper.h"
target-openrisc/machine.c new file +76 -0
new file mode 100644
index 0000000..c7ac0ea
@@ -0,0 +1,76 @@
+/*
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
+ */
+
+#include "hw/hw.h"
+#include "hw/boards.h"
+#include "kvm.h"
+
+static const VMStateDescription vmstate_cpu = {
+    .name = "cpu",
+    .fields = (VMStateField[]) {
+        VMSTATE_UINT32_ARRAY(gpr, CPUOPENRISCState, 32),
+        VMSTATE_UINT32(sr, CPUOPENRISCState),
+        VMSTATE_UINT32(epcr, CPUOPENRISCState),
+        VMSTATE_UINT32(eear, CPUOPENRISCState),
+        VMSTATE_UINT32(esr, CPUOPENRISCState),
+        VMSTATE_UINT32(fpcsr, CPUOPENRISCState),
+        VMSTATE_UINT32(pc, CPUOPENRISCState),
+        VMSTATE_UINT32(npc, CPUOPENRISCState),
+        VMSTATE_UINT32(ppc, CPUOPENRISCState),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+void cpu_save(QEMUFile *f, void *opaque)
+{
+    CPUOPENRISCState *env = (CPUOPENRISCState *)opaque;
+    unsigned int i;
+
+    for (i = 0; i < 32; i++) {
+        qemu_put_betls(f, &env->gpr[i]);
+    }
+
+    qemu_put_betls(f, &env->epcr);
+    qemu_put_betls(f, &env->eear);
+    qemu_put_betls(f, &env->esr);
+
+    qemu_put_betls(f, &env->sr);
+    qemu_put_be32s(f, &env->pc);
+    qemu_put_be32s(f, &env->fpcsr);
+}
+
+int cpu_load(QEMUFile *f, void *opaque, int version_id)
+{
+    CPUOPENRISCState *env = (CPUOPENRISCState *)opaque;
+    unsigned int i;
+
+    for (i = 0; i < 32; i++) {
+        qemu_get_betls(f, &env->gpr[i]);
+    }
+
+    qemu_get_betls(f, &env->epcr);
+    qemu_get_betls(f, &env->eear);
+    qemu_get_betls(f, &env->esr);
+
+    qemu_get_betls(f, &env->sr);
+    qemu_get_betls(f, &env->pc);
+    qemu_get_be32s(f, &env->fpcsr);
+    tlb_flush(env, 1);
+
+    return 0;
+}
target-openrisc/mem.c new file +43 -0
new file mode 100644
index 0000000..57a0b1b
@@ -0,0 +1,43 @@
+/*
+ *  Openrisc MMU.
+ *
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *                          Zhizhou Zhang <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "cpu.h"
+#include "qemu-common.h"
+#include "gdbstub.h"
+#include "helper.h"
+#include "host-utils.h"
+#if !defined(CONFIG_USER_ONLY)
+#include "hw/loader.h"
+#endif
+
+#if !defined(CONFIG_USER_ONLY)
+target_phys_addr_t cpu_get_phys_page_debug(CPUOPENRISCState *env,
+                                           target_ulong addr)
+{
+    return 0;
+}
+#endif
+
+void openrisc_reset(CPUOPENRISCState *env)
+{
+    env->pc = 0x100;
+    env->sr = SR_FO | SR_SM;
+    env->exception_index = -1;
+}
target-openrisc/mem_helper.c new file +20 -0
new file mode 100644
index 0000000..a2d93c7
@@ -0,0 +1,46 @@
+/*
+ * OpenRISC mmu helper routines
+ *
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *                          Zhizhou Zhang <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
+ */
Quoted 9 lines

target-openrisc/mem_helper.c line 23

Quoted 3 lines
target-openrisc/mem_helper.c new file +3 -0
+
+#include "cpu.h"
+#include "dyngen-exec.h"

As I mention in the later patch, this makes AREG0 to be used for CPUState (also HELPER_CFLAGS is not used to compile mem_helper.o). Please avoid it, then you also need to adjust CONFIG_TCG_PASS_AREG0 case in configure.

I'm trying, but I'm not very clear for now. thanks.

Quoted 123 lines
Quoted 123 lines
target-openrisc/mem_helper.c new file +23 -0
+#include "helper.h"
+
+#if !defined(CONFIG_USER_ONLY)
+#include "softmmu_exec.h"
+#define MMUSUFFIX _mmu
+
+#define SHIFT 0
+#include "softmmu_template.h"
+
+#define SHIFT 1
+#include "softmmu_template.h"
+
+#define SHIFT 2
+#include "softmmu_template.h"
+
+#define SHIFT 3
+#include "softmmu_template.h"
+
+void tlb_fill(CPUOPENRISCState *env1, target_ulong addr, int is_write,
+              int mmu_idx, uintptr_t retaddr)
+{
+}
+#endif
target-openrisc/translate.c new file +90 -1
new file mode 100644
index 0000000..aae483a
@@ -0,0 +1,90 @@
+/*
+ * Openrisc translation
+ *
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *                          Feng Gao <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
+ */
+
+#include <stdarg.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <inttypes.h>
+#include <assert.h>
+
+#include "cpu.h"
+#include "exec-all.h"
+#include "disas.h"
+#include "tcg-op.h"
+#include "qemu-common.h"
+#include "qemu-log.h"
+#include "config.h"
+
+#include "helper.h"
+#define GEN_HELPER 1
+#include "helper.h"
+
+#define DISAS_OPENRISC 1
+#if DISAS_OPENRISC
+#  define LOG_DIS(...) do { } while (0)
+#endif
+
+typedef struct DisasContext {
+    CPUOPENRISCState *env;
+    TranslationBlock *tb;
+    target_ulong pc;
+    target_ulong ppc, npc;
+    uint32_t tb_flags;
+    uint32_t is_jmp;
+    uint32_t mem_idx;
+    int singlestep_enabled;
+    uint32_t delayed_branch;
+} DisasContext;
+
+void openrisc_translate_init(void)
+{
+}
+
+static inline void gen_intermediate_code_internal(CPUOPENRISCState *env,
+                                                  TranslationBlock *tb,
+                                                  int search_pc)
+{
+}
+
+void gen_intermediate_code(CPUOPENRISCState *env, struct TranslationBlock *tb)
+{
+    gen_intermediate_code_internal(env, tb, 0);
+}
+
+void gen_intermediate_code_pc(CPUOPENRISCState *env,
+                              struct TranslationBlock *tb)
+{
+    gen_intermediate_code_internal(env, tb, 1);
+}
+
+void cpu_dump_state(CPUOPENRISCState *env, FILE *f,
+                    fprintf_function cpu_fprintf,
+                    int flags)
+{
+    cpu_fprintf(f, "PC=%08x\n", env->pc);
+}
+
+void restore_state_to_opc(CPUOPENRISCState *env, TranslationBlock *tb,
+                          int pc_pos)
+{
+    env->pc = gen_opc_pc[pc_pos];
+}
--

1.7.9.5

author
#12 lore raw

PATCH 2/15 Openrisc: add MMU support

In reply to Jia Liu

add the openrisc MMU support.

---
 Makefile.target              |    2 +
 hw/openrisc_cpudev.h         |   30 ++++++
 hw/openrisc_pic.c            |   31 ++++++
 target-openrisc/mem.c        |  220 +++++++++++++++++++++++++++++++++++++++++-
 target-openrisc/mem_helper.c |   25 +++++
 5 files changed, 307 insertions(+), 1 deletion(-)
 create mode 100644 hw/openrisc_cpudev.h
 create mode 100644 hw/openrisc_pic.c
Makefile.target +2 -0
index 0ffb29c..79c75f6 100644
@@ -390,6 +390,8 @@ obj-xtensa-y += core-dc232b.o
 obj-xtensa-y += core-dc233c.o
 obj-xtensa-y += core-fsf.o
 
+obj-openrisc-y += openrisc_pic.o
+
 main.o: QEMU_CFLAGS+=$(GPROF_CFLAGS)
 
 monitor.o: hmp-commands.h qmp-commands-old.h
hw/openrisc_cpudev.h new file +30 -0
new file mode 100644
index 0000000..ca7d064
@@ -0,0 +1,30 @@
+/*
+ * Qemu Openrisc CPU device support.
+ *
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
+ */
+
+#ifndef HW_OPENRISC_CPUDEV_H
+#define HW_OPENRISC_CPUDEV_H
+
+/* openrisc_pic.c */
+void cpu_openrisc_pic_init(CPUOPENRISCState *env);
+
+/* openrisc_timer.c*/
+void cpu_openrisc_clock_init(CPUOPENRISCState *env);
+
+#endif
hw/openrisc_pic.c new file +31 -0
new file mode 100644
index 0000000..0abdd50
@@ -0,0 +1,31 @@
+/*
+ * Generic  OPENRISC Programmable Interrupt Controller support.
+ *
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *                          Feng Gao <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
+ */
+
+#include "hw.h"
+#include "openrisc_cpudev.h"
+#include "cpu.h"
+
+/* Reset PIC */
+void cpu_openrisc_pic_reset(CPUOPENRISCState *env)
+{
+    env->picmr = 0x00000000;
+    env->picsr = 0x00000000;
+}
target-openrisc/mem.c +219 -1

233 lines not on this page · raw message

target-openrisc/mem_helper.c +25 -0
index a2d93c7..922dd6b 100644
@@ -42,5 +42,30 @@
 void tlb_fill(CPUOPENRISCState *env1, target_ulong addr, int is_write,
               int mmu_idx, uintptr_t retaddr)
 {
+    TranslationBlock *tb;
+    CPUOPENRISCState *saved_env;
+    unsigned long pc;
+    int ret;
+
+    saved_env = env;
+    env = env1;
+
+    ret = cpu_openrisc_handle_mmu_fault(env, addr, is_write, mmu_idx);
+
+    if (ret) {
+        if (retaddr) {
+            /* now we have a real cpu fault */
+            pc = (unsigned long)retaddr;
+            tb = tb_find_pc(pc);
+            if (tb) {
+                /* the PC is inside the translated code. It means that we
+                   have a virtual CPU fault */
+                cpu_restore_state(tb, env, pc);
+            }
+        }
+        /* Raise Exception... */
+        cpu_loop_exit(env);
+    }
+    env = saved_env;
 }
 #endif
Signature · 1 line
1.7.9.5
#13 lore raw

Re: [Qemu-devel] [PATCH 02/15] Openrisc: add MMU support

Quoted 50 lines · Jia Liu

add the openrisc MMU support.

---
 Makefile.target              |    2 +
 hw/openrisc_cpudev.h         |   30 ++++++
 hw/openrisc_pic.c            |   31 ++++++
 target-openrisc/mem.c        |  220 +++++++++++++++++++++++++++++++++++++++++- target-openrisc/mem_helper.c |   25 +++++

 5 files changed, 307 insertions(+), 1 deletion(-)

 create mode 100644 hw/openrisc_cpudev.h
 create mode 100644 hw/openrisc_pic.c

Makefile.target +0 -0
index 0ffb29c..79c75f6 100644
@@ -390,6 +390,8 @@ obj-xtensa-y += core-dc232b.o

 obj-xtensa-y += core-dc233c.o
 obj-xtensa-y += core-fsf.o

Makefile.target +2 -0
+obj-openrisc-y += openrisc_pic.o
+

 main.o: QEMU_CFLAGS+=$(GPROF_CFLAGS)

 monitor.o: hmp-commands.h qmp-commands-old.h

hw/openrisc_cpudev.h new file +18 -0
new file mode 100644
index 0000000..ca7d064
@@ -0,0 +1,30 @@
+/*
+ * Qemu Openrisc CPU device support.
+ *
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA

hw/openrisc_cpudev.h line 28

Quoted 10 lines
hw/openrisc_cpudev.h new file +10 -0
+ */
+
+#ifndef HW_OPENRISC_CPUDEV_H
+#define HW_OPENRISC_CPUDEV_H
+
+/* openrisc_pic.c */
+void cpu_openrisc_pic_init(CPUOPENRISCState *env);
+
+/* openrisc_timer.c*/
+void cpu_openrisc_clock_init(CPUOPENRISCState *env);

The implementation is not added by this patch.

Quoted 46 lines
hw/openrisc_cpudev.h new file +2 -0
+
+#endif
hw/openrisc_pic.c new file +31 -0
new file mode 100644
index 0000000..0abdd50
@@ -0,0 +1,31 @@
+/*
+ * Generic  OPENRISC Programmable Interrupt Controller support.
+ *
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *                          Feng Gao <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
+ */
+
+#include "hw.h"
+#include "openrisc_cpudev.h"
+#include "cpu.h"
+
+/* Reset PIC */
+void cpu_openrisc_pic_reset(CPUOPENRISCState *env)
+{
+    env->picmr = 0x00000000;
+    env->picsr = 0x00000000;
+}
target-openrisc/mem.c +0 -0
index 57a0b1b..b26da38 100644
@@ -28,10 +28,224 @@
  #endif
 

target-openrisc/mem.c line 39

Quoted 10 lines
target-openrisc/mem.c +9 -0
  #if !defined(CONFIG_USER_ONLY)
+enum {
+    TLBRET_INVALID = -3,
+    TLBRET_NOMATCH = -2,
+    TLBRET_BADADDR = -1,
+    TLBRET_MATCH = 0
+};
+
+tlb_entry itlb_table[ITLB_WAYS][ITLB_SIZE];
+tlb_entry dtlb_table[DTLB_WAYS][DTLB_SIZE];

These should be inside CPUOPENRISCState, not globals.

Quoted 87 lines
target-openrisc/mem.c +87 -0
+#endif
+
+#if !defined(CONFIG_USER_ONLY)
+/* no MMU emulation */
+int get_phys_nommu(CPUOPENRISCState *env, target_phys_addr_t *physical,
+                   int *prot, target_ulong address, int rw)
+{
+    *physical = address;
+    *prot = PAGE_READ | PAGE_WRITE;
+    return TLBRET_MATCH;
+}
+int get_phys_code(CPUOPENRISCState *env, target_phys_addr_t *physical,
+                  int *prot, target_ulong address, int rw)
+{
+    int vpn = address >> TARGET_PAGE_BITS;
+    int idx = vpn & ITLB_MASK;
+    int right = 0;
+
+    if ((env->itlb[0][idx].mr >> TARGET_PAGE_BITS) != vpn) {
+        return TLBRET_NOMATCH;
+    }
+    if (!(env->itlb[0][idx].mr & 1)) {
+        return TLBRET_INVALID;
+    }
+
+    if (env->sr & SR_SM) { /* supervisor mode */
+        if (env->itlb[0][idx].tr & SXE) {
+            right |= PAGE_EXEC;
+        }
+    } else {
+        if (env->itlb[0][idx].tr & UXE) {
+            right |= PAGE_EXEC;
+        }
+    }
+
+    if ((rw & 2) && ((right & PAGE_EXEC) == 0)) {
+        return TLBRET_BADADDR;
+    }
+
+    *physical = (env->itlb[0][idx].tr & TARGET_PAGE_MASK) |
+                (address & (TARGET_PAGE_SIZE-1));
+    *prot = right;
+    return TLBRET_MATCH;
+}
+
+int get_phys_data(CPUOPENRISCState *env, target_phys_addr_t *physical,
+                  int *prot, target_ulong address, int rw)
+{
+    int vpn = address >> TARGET_PAGE_BITS;
+    int idx = vpn & DTLB_MASK;
+    int right = 0;
+
+    if ((env->dtlb[0][idx].mr >> TARGET_PAGE_BITS) != vpn) {
+        return TLBRET_NOMATCH;
+    }
+    if (!(env->dtlb[0][idx].mr & 1)) {
+        return TLBRET_INVALID;
+    }
+
+    if (env->sr & SR_SM) { /* supervisor mode */
+        if (env->dtlb[0][idx].tr & SRE) {
+            right |= PAGE_READ;
+        }
+        if (env->dtlb[0][idx].tr & SWE) {
+            right |= PAGE_WRITE;
+        }
+    } else {
+        if (env->dtlb[0][idx].tr & URE) {
+            right |= PAGE_READ;
+        }
+        if (env->dtlb[0][idx].tr & UWE) {
+            right |= PAGE_WRITE;
+        }
+    }
+
+    if ((rw & 0) && ((right & PAGE_READ) == 0)) {
+        return TLBRET_BADADDR;
+    }
+    if ((rw & 1) && ((right & PAGE_WRITE) == 0)) {
+        return TLBRET_BADADDR;
+    }
+
+    *physical = (env->dtlb[0][idx].tr & TARGET_PAGE_MASK) |
+                (address & (TARGET_PAGE_SIZE-1));
+    *prot = right;
+    return TLBRET_MATCH;
+}

target-openrisc/mem.c line 136

Quoted 10 lines
target-openrisc/mem.c +10 -0
+
+static int get_physical_address(CPUOPENRISCState *env,
+                                target_phys_addr_t *physical,
+                                int *prot, target_ulong address,
+                                int rw)
+{
+    int ret = TLBRET_MATCH;
+
+    /* [0x0000--0x2000]: unmapped */
+    if (address < 0x2000 && (env->sr & SR_SM)) {

This is pretty strange, isn't the virtual to physical mapping handled by TLB so why this shortcut?

Quoted 130 lines
target-openrisc/mem.c +113 -1
+        *physical = address;
+        *prot = PAGE_READ | PAGE_WRITE;
+        return ret;
+    }
+
+    if (rw == 2) { /* ITLB */
+       *physical = 0;
+        ret = env->map_address_code(env, physical,
+                                    prot, address, rw);
+    } else {       /* DTLB */
+        ret = env->map_address_data(env, physical,
+                                    prot, address, rw);
+    }
+
+    return ret;
+}
+#endif
+
+static void raise_mmu_exception(CPUOPENRISCState *env, target_ulong address,
+                                int rw, int tlb_error)
+{
+    int exception = 0;
+
+    switch (tlb_error) {
+    default:
+        if (rw == 2) {
+            exception = EXCP_IPF;
+        } else {
+            exception = EXCP_DPF;
+        }
+        break;
+#if !defined(CONFIG_USER_ONLY)
+    case TLBRET_BADADDR:
+        if (rw == 2) {
+            exception = EXCP_IPF;
+        } else {
+            exception = EXCP_DPF;
+        }
+        break;
+    case TLBRET_INVALID:
+    case TLBRET_NOMATCH:
+        /* No TLB match for a mapped address */
+        if (rw == 2) {
+            exception = EXCP_ITLBMISS;
+        } else {
+            exception = EXCP_DTLBMISS;
+        }
+        break;
+#endif
+    }
+
+    env->exception_index = exception;
+    env->eear = address;
+}
+
+#if !defined(CONFIG_USER_ONLY)
+int cpu_openrisc_handle_mmu_fault(CPUOPENRISCState *env,
+                                  target_ulong address, int rw, int mmu_idx)
+{
+    int ret = 0;
+    target_phys_addr_t physical = 0;
+    int prot = 0;
+
+    ret = get_physical_address(env, &physical, &prot,
+                               address, rw);
+
+    if (ret == TLBRET_MATCH) {
+        tlb_set_page(env, address & TARGET_PAGE_MASK,
+                     physical & TARGET_PAGE_MASK, prot | PAGE_EXEC,
+                     mmu_idx, TARGET_PAGE_SIZE);
+        ret = 0;
+    } else if (ret < 0) {
+        raise_mmu_exception(env, address, rw, ret);
+        ret = 1;
+    }
+
+    return ret;
+}
+#else
+int cpu_openrisc_handle_mmu_fault(CPUOPENRISCState *env,
+                                  target_ulong address, int rw, int mmu_idx)
+{
+    int ret = 0;
+
+    raise_mmu_exception(env, address, rw, ret);
+    ret = 1;
+
+    return ret;
+}
+#endif
+
+#if !defined(CONFIG_USER_ONLY)
  target_phys_addr_t cpu_get_phys_page_debug(CPUOPENRISCState *env,
                                            target_ulong addr)
  {
-    return 0;
+    target_phys_addr_t phys_addr;
+    int prot;
+
+    if (get_physical_address(env, &phys_addr, &prot, addr, 0)) {
+        return -1;
+    }
+    return phys_addr;
+}
+
+void openrisc_mmu_init(CPUOPENRISCState *env)
+{
+    env->map_address_code = &get_phys_nommu;
+    env->map_address_data = &get_phys_nommu;
+    env->dtlb = dtlb_table;
+    env->itlb = itlb_table;
+    memset(dtlb_table, 0, sizeof(tlb_entry)*DTLB_SIZE*DTLB_WAYS);
+    memset(itlb_table, 0, sizeof(tlb_entry)*ITLB_SIZE*ITLB_WAYS);
  }
  #endif
 
@@ -40,4 +254,8 @@ void openrisc_reset(CPUOPENRISCState *env)
     env->pc = 0x100;
     env->sr = SR_FO | SR_SM;
     env->exception_index = -1;
+#if !defined(CONFIG_USER_ONLY)
+    openrisc_mmu_init(env);
+    cpu_openrisc_pic_reset(env);
+#endif
  }
target-openrisc/mem_helper.c +0 -0
index a2d93c7..922dd6b 100644

target-openrisc/mem_helper.c line 51

Quoted 10 lines
target-openrisc/mem_helper.c +7 -0
@@ -42,5 +42,30 @@
  void tlb_fill(CPUOPENRISCState *env1, target_ulong addr, int is_write,
               int mmu_idx, uintptr_t retaddr)
  {
+    TranslationBlock *tb;
+    CPUOPENRISCState *saved_env;
+    unsigned long pc;
+    int ret;
+
+    saved_env = env;
+    env = env1;

Please avoid compiling with AREG0 and remove this.

Quoted 24 lines
target-openrisc/mem_helper.c +18 -1
+
+    ret = cpu_openrisc_handle_mmu_fault(env, addr, is_write, mmu_idx);
+
+    if (ret) {
+        if (retaddr) {
+            /* now we have a real cpu fault */
+            pc = (unsigned long)retaddr;
+            tb = tb_find_pc(pc);
+            if (tb) {
+                /* the PC is inside the translated code. It means that we
+                   have a virtual CPU fault */
+                cpu_restore_state(tb, env, pc);
+            }
+        }
+        /* Raise Exception... */
+        cpu_loop_exit(env);
+    }
+    env = saved_env;
  }
  #endif
--

1.7.9.5

author
#14 lore raw

Re: [Qemu-devel] [PATCH 02/15] Openrisc: add MMU support

Hi Blue,

Quoted 55 lines · Blue Swirl
Quoted 54 lines · Jia Liu

add the openrisc MMU support.

---
 Makefile.target              |    2 +
 hw/openrisc_cpudev.h         |   30 ++++++
 hw/openrisc_pic.c            |   31 ++++++
 target-openrisc/mem.c        |  220 +++++++++++++++++++++++++++++++++++++++++- target-openrisc/mem_helper.c |   25 +++++

 5 files changed, 307 insertions(+), 1 deletion(-)

 create mode 100644 hw/openrisc_cpudev.h
 create mode 100644 hw/openrisc_pic.c

Makefile.target +0 -0
index 0ffb29c..79c75f6 100644
@@ -390,6 +390,8 @@ obj-xtensa-y += core-dc232b.o

 obj-xtensa-y += core-dc233c.o
 obj-xtensa-y += core-fsf.o

Makefile.target +2 -0
+obj-openrisc-y += openrisc_pic.o
+

 main.o: QEMU_CFLAGS+=$(GPROF_CFLAGS)

 monitor.o: hmp-commands.h qmp-commands-old.h

hw/openrisc_cpudev.h new file +22 -0
new file mode 100644
index 0000000..ca7d064
@@ -0,0 +1,30 @@
+/*
+ * Qemu Openrisc CPU device support.
+ *
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
+ */
+
+#ifndef HW_OPENRISC_CPUDEV_H
+#define HW_OPENRISC_CPUDEV_H
Quoted 9 lines

hw/openrisc_cpudev.h line 28

Quoted 6 lines
hw/openrisc_cpudev.h new file +6 -0
+
+/* openrisc_pic.c */
+void cpu_openrisc_pic_init(CPUOPENRISCState *env);
+
+/* openrisc_timer.c*/
+void cpu_openrisc_clock_init(CPUOPENRISCState *env);

The implementation is not added by this patch.

I'll add it here.

Quoted 46 lines
Quoted 46 lines
hw/openrisc_cpudev.h new file +2 -0
+
+#endif
hw/openrisc_pic.c new file +31 -0
new file mode 100644
index 0000000..0abdd50
@@ -0,0 +1,31 @@
+/*
+ * Generic  OPENRISC Programmable Interrupt Controller support.
+ *
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *                          Feng Gao <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
+ */
+
+#include "hw.h"
+#include "openrisc_cpudev.h"
+#include "cpu.h"
+
+/* Reset PIC */
+void cpu_openrisc_pic_reset(CPUOPENRISCState *env)
+{
+    env->picmr = 0x00000000;
+    env->picsr = 0x00000000;
+}
target-openrisc/mem.c +0 -0
index 57a0b1b..b26da38 100644
@@ -28,10 +28,224 @@
  #endif
 
Quoted 13 lines

target-openrisc/mem.c line 39

Quoted 10 lines
target-openrisc/mem.c +9 -0
  #if !defined(CONFIG_USER_ONLY)
+enum {
+    TLBRET_INVALID = -3,
+    TLBRET_NOMATCH = -2,
+    TLBRET_BADADDR = -1,
+    TLBRET_MATCH = 0
+};
+
+tlb_entry itlb_table[ITLB_WAYS][ITLB_SIZE];
+tlb_entry dtlb_table[DTLB_WAYS][DTLB_SIZE];

These should be inside CPUOPENRISCState, not globals.

thanks, I'll move them there.

Quoted 92 lines
Quoted 92 lines
target-openrisc/mem.c +92 -0
+#endif
+
+#if !defined(CONFIG_USER_ONLY)
+/* no MMU emulation */
+int get_phys_nommu(CPUOPENRISCState *env, target_phys_addr_t *physical,
+                   int *prot, target_ulong address, int rw)
+{
+    *physical = address;
+    *prot = PAGE_READ | PAGE_WRITE;
+    return TLBRET_MATCH;
+}
+int get_phys_code(CPUOPENRISCState *env, target_phys_addr_t *physical,
+                  int *prot, target_ulong address, int rw)
+{
+    int vpn = address >> TARGET_PAGE_BITS;
+    int idx = vpn & ITLB_MASK;
+    int right = 0;
+
+    if ((env->itlb[0][idx].mr >> TARGET_PAGE_BITS) != vpn) {
+        return TLBRET_NOMATCH;
+    }
+    if (!(env->itlb[0][idx].mr & 1)) {
+        return TLBRET_INVALID;
+    }
+
+    if (env->sr & SR_SM) { /* supervisor mode */
+        if (env->itlb[0][idx].tr & SXE) {
+            right |= PAGE_EXEC;
+        }
+    } else {
+        if (env->itlb[0][idx].tr & UXE) {
+            right |= PAGE_EXEC;
+        }
+    }
+
+    if ((rw & 2) && ((right & PAGE_EXEC) == 0)) {
+        return TLBRET_BADADDR;
+    }
+
+    *physical = (env->itlb[0][idx].tr & TARGET_PAGE_MASK) |
+                (address & (TARGET_PAGE_SIZE-1));
+    *prot = right;
+    return TLBRET_MATCH;
+}
+
+int get_phys_data(CPUOPENRISCState *env, target_phys_addr_t *physical,
+                  int *prot, target_ulong address, int rw)
+{
+    int vpn = address >> TARGET_PAGE_BITS;
+    int idx = vpn & DTLB_MASK;
+    int right = 0;
+
+    if ((env->dtlb[0][idx].mr >> TARGET_PAGE_BITS) != vpn) {
+        return TLBRET_NOMATCH;
+    }
+    if (!(env->dtlb[0][idx].mr & 1)) {
+        return TLBRET_INVALID;
+    }
+
+    if (env->sr & SR_SM) { /* supervisor mode */
+        if (env->dtlb[0][idx].tr & SRE) {
+            right |= PAGE_READ;
+        }
+        if (env->dtlb[0][idx].tr & SWE) {
+            right |= PAGE_WRITE;
+        }
+    } else {
+        if (env->dtlb[0][idx].tr & URE) {
+            right |= PAGE_READ;
+        }
+        if (env->dtlb[0][idx].tr & UWE) {
+            right |= PAGE_WRITE;
+        }
+    }
+
+    if ((rw & 0) && ((right & PAGE_READ) == 0)) {
+        return TLBRET_BADADDR;
+    }
+    if ((rw & 1) && ((right & PAGE_WRITE) == 0)) {
+        return TLBRET_BADADDR;
+    }
+
+    *physical = (env->dtlb[0][idx].tr & TARGET_PAGE_MASK) |
+                (address & (TARGET_PAGE_SIZE-1));
+    *prot = right;
+    return TLBRET_MATCH;
+}
+
+static int get_physical_address(CPUOPENRISCState *env,
+                                target_phys_addr_t *physical,
+                                int *prot, target_ulong address,
+                                int rw)
Quoted 9 lines

target-openrisc/mem.c line 136

Quoted 5 lines
target-openrisc/mem.c +5 -0
+{
+    int ret = TLBRET_MATCH;
+
+    /* [0x0000--0x2000]: unmapped */
+    if (address < 0x2000 && (env->sr & SR_SM)) {

This is pretty strange, isn't the virtual to physical mapping handled by TLB so why this shortcut?

The Openrisc Arch said that, so I have to obey.

Quoted 134 lines
Quoted 134 lines
target-openrisc/mem.c +113 -1
+        *physical = address;
+        *prot = PAGE_READ | PAGE_WRITE;
+        return ret;
+    }
+
+    if (rw == 2) { /* ITLB */
+       *physical = 0;
+        ret = env->map_address_code(env, physical,
+                                    prot, address, rw);
+    } else {       /* DTLB */
+        ret = env->map_address_data(env, physical,
+                                    prot, address, rw);
+    }
+
+    return ret;
+}
+#endif
+
+static void raise_mmu_exception(CPUOPENRISCState *env, target_ulong address,
+                                int rw, int tlb_error)
+{
+    int exception = 0;
+
+    switch (tlb_error) {
+    default:
+        if (rw == 2) {
+            exception = EXCP_IPF;
+        } else {
+            exception = EXCP_DPF;
+        }
+        break;
+#if !defined(CONFIG_USER_ONLY)
+    case TLBRET_BADADDR:
+        if (rw == 2) {
+            exception = EXCP_IPF;
+        } else {
+            exception = EXCP_DPF;
+        }
+        break;
+    case TLBRET_INVALID:
+    case TLBRET_NOMATCH:
+        /* No TLB match for a mapped address */
+        if (rw == 2) {
+            exception = EXCP_ITLBMISS;
+        } else {
+            exception = EXCP_DTLBMISS;
+        }
+        break;
+#endif
+    }
+
+    env->exception_index = exception;
+    env->eear = address;
+}
+
+#if !defined(CONFIG_USER_ONLY)
+int cpu_openrisc_handle_mmu_fault(CPUOPENRISCState *env,
+                                  target_ulong address, int rw, int mmu_idx)
+{
+    int ret = 0;
+    target_phys_addr_t physical = 0;
+    int prot = 0;
+
+    ret = get_physical_address(env, &physical, &prot,
+                               address, rw);
+
+    if (ret == TLBRET_MATCH) {
+        tlb_set_page(env, address & TARGET_PAGE_MASK,
+                     physical & TARGET_PAGE_MASK, prot | PAGE_EXEC,
+                     mmu_idx, TARGET_PAGE_SIZE);
+        ret = 0;
+    } else if (ret < 0) {
+        raise_mmu_exception(env, address, rw, ret);
+        ret = 1;
+    }
+
+    return ret;
+}
+#else
+int cpu_openrisc_handle_mmu_fault(CPUOPENRISCState *env,
+                                  target_ulong address, int rw, int mmu_idx)
+{
+    int ret = 0;
+
+    raise_mmu_exception(env, address, rw, ret);
+    ret = 1;
+
+    return ret;
+}
+#endif
+
+#if !defined(CONFIG_USER_ONLY)
  target_phys_addr_t cpu_get_phys_page_debug(CPUOPENRISCState *env,
                                            target_ulong addr)
  {
-    return 0;
+    target_phys_addr_t phys_addr;
+    int prot;
+
+    if (get_physical_address(env, &phys_addr, &prot, addr, 0)) {
+        return -1;
+    }
+    return phys_addr;
+}
+
+void openrisc_mmu_init(CPUOPENRISCState *env)
+{
+    env->map_address_code = &get_phys_nommu;
+    env->map_address_data = &get_phys_nommu;
+    env->dtlb = dtlb_table;
+    env->itlb = itlb_table;
+    memset(dtlb_table, 0, sizeof(tlb_entry)*DTLB_SIZE*DTLB_WAYS);
+    memset(itlb_table, 0, sizeof(tlb_entry)*ITLB_SIZE*ITLB_WAYS);
  }
  #endif
 
@@ -40,4 +254,8 @@ void openrisc_reset(CPUOPENRISCState *env)
     env->pc = 0x100;
     env->sr = SR_FO | SR_SM;
     env->exception_index = -1;
+#if !defined(CONFIG_USER_ONLY)
+    openrisc_mmu_init(env);
+    cpu_openrisc_pic_reset(env);
+#endif
  }
target-openrisc/mem_helper.c +1 -0
index a2d93c7..922dd6b 100644
@@ -42,5 +42,30 @@
  void tlb_fill(CPUOPENRISCState *env1, target_ulong addr, int is_write,
               int mmu_idx, uintptr_t retaddr)
  {
+    TranslationBlock *tb;
Quoted 9 lines

target-openrisc/mem_helper.c line 51

Quoted 6 lines
target-openrisc/mem_helper.c +6 -0
+    CPUOPENRISCState *saved_env;
+    unsigned long pc;
+    int ret;
+
+    saved_env = env;
+    env = env1;

Please avoid compiling with AREG0 and remove this.

Sorry, I'm not very sure about it, may you give me more hits?

Quoted 24 lines
Quoted 24 lines
target-openrisc/mem_helper.c +18 -1
+
+    ret = cpu_openrisc_handle_mmu_fault(env, addr, is_write, mmu_idx);
+
+    if (ret) {
+        if (retaddr) {
+            /* now we have a real cpu fault */
+            pc = (unsigned long)retaddr;
+            tb = tb_find_pc(pc);
+            if (tb) {
+                /* the PC is inside the translated code. It means that we
+                   have a virtual CPU fault */
+                cpu_restore_state(tb, env, pc);
+            }
+        }
+        /* Raise Exception... */
+        cpu_loop_exit(env);
+    }
+    env = saved_env;
  }
  #endif
--

1.7.9.5

#15 lore raw

Re: [Qemu-devel] [PATCH 02/15] Openrisc: add MMU support

Quoted 19 lines
Quoted 9 lines
Quoted 9 lines
target-openrisc/mem_helper.c +0 -0
index a2d93c7..922dd6b 100644
@@ -42,5 +42,30 @@
 戹oid tlb_fill(CPUOPENRISCState *env1, target_ulong addr, int is_write,
 ? ? ? ? ? ? ? int mmu_idx, uintptr_t retaddr)

𣏵
+ ? ?TranslationBlock *tb;

Quoted 9 lines
Quoted 6 lines

+ ? 蟖PUOPENRISCState *saved_env;
+ ? 抦nsigned long pc;
+ ? 犲nt ret;
+
+ ? 坟aved_env = env;
+ ? 猬nv = env1;

Please avoid compiling with AREG0 and remove this.

Quoted 1 line

Sorry, I'm not very sure about it, may you give me more hits?

In dyngen-exec.h, the CPUArchState *env is kept in particular host register
(AREG0), so you shouldn't use env here somehow. Maybe Blue can show you some example how to do that, target-sparc/* might be a place?

Regards,
chenwj

Signature · 5 lines
Wei-Ren Chen (陳韋任)
Computer Systems Lab, Institute of Information Science,
Academia Sinica, Taiwan (R.O.C.)
Tel:886-2-2788-3799 #1667
Homepage: http://people.cs.nctu.edu.tw/~chenwj
#16 lore raw

Re: [Qemu-devel] [PATCH 02/15] Openrisc: add MMU support

Quoted 19 lines · 陳韋任
Quoted 19 lines
Quoted 18 lines
Quoted 7 lines
target-openrisc/mem_helper.c +0 -0
index a2d93c7..922dd6b 100644
@@ -42,5 +42,30 @@
 戹oid tlb_fill(CPUOPENRISCState *env1, target_ulong addr, int is_write,
 ? ? ? ? ? ? ? int mmu_idx, uintptr_t retaddr)
Quoted 8 lines

𣏵
+ ? ?TranslationBlock *tb;
+ ? 蟖PUOPENRISCState *saved_env;
+ ? 抦nsigned long pc;
+ ? 犲nt ret;
+
+ ? 坟aved_env = env;
+ ? 猬nv = env1;

Please avoid compiling with AREG0 and remove this.

Quoted 5 lines
Quoted 1 line

Sorry, I'm not very sure about it, may you give me more hits?

 In dyngen-exec.h, the CPUArchState *env is kept in particular host register (AREG0), so you shouldn't use env here somehow. Maybe Blue can show you some example how to do that, target-sparc/* might be a place?

For example this patch replaces AREG0 env with a parameter:
http://repo.or.cz/w/qemu/blueswirl.git/commitdiff/addb3b03425550d677d9d8d466398f715d090233

Quoted 10 lines

Regards,
chenwj

--
Wei-Ren Chen (陳韋任)
Computer Systems Lab, Institute of Information Science, Academia Sinica, Taiwan (R.O.C.) Tel:886-2-2788-3799 #1667
Homepage: http://people.cs.nctu.edu.tw/~chenwj

author
#17 lore raw

PATCH 3/15 Openrisc: add instructions translation

In reply to Jia Liu

add the openrisc instructions translation.

---
 target-openrisc/translate.c | 1387 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 1387 insertions(+)
target-openrisc/translate.c +1387 -0
index aae483a..4828ae6 100644
@@ -55,14 +55,1395 @@ typedef struct DisasContext {
     uint32_t delayed_branch;
 } DisasContext;
 
+static TCGv_ptr cpu_env;
+static TCGv cpu_sr;
+static TCGv cpu_R[32];
+static TCGv cpu_pc;
+static TCGv jmp_pc;        /* l.jr/l.jalr temp pc */
+static TCGv cpu_npc;
+static TCGv cpu_ppc;
+static TCGv_i32 env_btaken;    /* bf/bnf , F flag taken */
+static TCGv machi, maclo;
+static TCGv fpmaddhi, fpmaddlo;
+static TCGv_i32 env_flags;
+#include "gen-icount.h"
+
 void openrisc_translate_init(void)
 {
+    static const char * const regnames[] = {
+        "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
+        "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
+        "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
+        "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
+    };
+    int i;
+
+    cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
+    cpu_sr = tcg_global_mem_new(TCG_AREG0,
+                                offsetof(CPUOPENRISCState, sr), "sr");
+    env_flags = tcg_global_mem_new_i32(TCG_AREG0,
+                                       offsetof(CPUOPENRISCState, flags),
+                                       "flags");
+    cpu_pc = tcg_global_mem_new(TCG_AREG0,
+                                offsetof(CPUOPENRISCState, pc), "pc");
+    cpu_npc = tcg_global_mem_new(TCG_AREG0,
+                                 offsetof(CPUOPENRISCState, npc), "npc");
+    cpu_ppc = tcg_global_mem_new(TCG_AREG0,
+                                 offsetof(CPUOPENRISCState, ppc), "ppc");
+    jmp_pc = tcg_global_mem_new(TCG_AREG0,
+                                offsetof(CPUOPENRISCState, jmp_pc), "jmp_pc");
+    env_btaken = tcg_global_mem_new_i32(TCG_AREG0,
+                                        offsetof(CPUOPENRISCState, btaken),
+                                        "btaken");
+    machi = tcg_global_mem_new(TCG_AREG0,
+                               offsetof(CPUOPENRISCState, machi),
+                               "machi");
+    maclo = tcg_global_mem_new(TCG_AREG0,
+                               offsetof(CPUOPENRISCState, maclo),
+                               "maclo");
+    fpmaddhi = tcg_global_mem_new(TCG_AREG0,
+                                  offsetof(CPUOPENRISCState, fpmaddhi),
+                                  "fpmaddhi");
+    fpmaddlo = tcg_global_mem_new(TCG_AREG0,
+                                  offsetof(CPUOPENRISCState, fpmaddlo),
+                                  "fpmaddlo");
+    for (i = 0; i < 32; i++) {
+        cpu_R[i] = tcg_global_mem_new(TCG_AREG0,
+                                      offsetof(CPUOPENRISCState, gpr[i]),
+                                      regnames[i]);
+    }
+#define GEN_HELPER 2
+#include "helper.h"
+}
+
+/* Writeback SR_F transaltion-space to execution-space.  */
+static inline void wb_SR_F(void)
+{
+    int label;
+
+    label = gen_new_label();
+    tcg_gen_andi_tl(cpu_sr, cpu_sr, ~SR_F);
+    tcg_gen_brcondi_tl(TCG_COND_EQ, env_btaken, 0, label);
+    tcg_gen_ori_tl(cpu_sr, cpu_sr, SR_F);
+    gen_set_label(label);
+}
+
+static inline int zero_extend(unsigned int val, int width)
+{
+    return val & ((1 << width) - 1);
+}
+
+static inline int sign_extend(unsigned int val, int width)
+{
+    int sval;
+
+    /* LSL.  */
+    val <<= 32 - width;
+    sval = val;
+    /* ASR.  */
+    sval >>= 32 - width;
+    return sval;
+}
+
+/* General purpose registers moves. */
+static inline void gen_load_gpr(TCGv t, unsigned int reg)
+{
+    if (reg == 0) {
+        tcg_gen_movi_tl(t, 0);
+    } else {
+        tcg_gen_mov_tl(t, cpu_R[reg]);
+    }
+}
+
+static void gen_goto_tb(DisasContext *dc, int n, target_ulong dest)
+{
+    TranslationBlock *tb;
+    tb = dc->tb;
+    if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) &&
+                                       likely(!dc->singlestep_enabled)) {
+        tcg_gen_movi_tl(cpu_pc, dest);
+        tcg_gen_goto_tb(n);
+        tcg_gen_exit_tb((tcg_target_long)tb + n);
+    } else {
+        tcg_gen_movi_tl(cpu_pc, dest);
+        if (dc->singlestep_enabled) {
+            /* exception here */
+        }
+        tcg_gen_exit_tb(0);
+    }
+}
+
+static inline uint32_t field(uint32_t val, int start, int length)
+{
+    val >>= start;
+    val &= ~(~0 << length);
+    return val;
+}
+
+static void dec_calc(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0, op1, op2;
+    uint32_t ra, rb, rd;
+    op0 = field(insn, 0, 4);
+    op1 = field(insn, 8, 2);
+    op2 = field(insn, 6, 2);
+    ra = field(insn, 16, 5);
+    rb = field(insn, 11, 5);
+    rd = field(insn, 21, 5);
+
+    switch (op0) {
+    case 0x0000:
+        switch (op1) {
+        case 0x00:     /*l.add*/
+            LOG_DIS("l.add r%d, r%d, r%d\n", rd, ra, rb);
+            /* Aha, we do need a helper here for add */
+            break;
+        default:
+            break;
+        }
+    break;
+
+    case 0x0001:    /*l.addc*/
+        switch (op1) {
+        case 0x00:
+            LOG_DIS("l.addc r%d, r%d, r%d\n", rd, ra, rb);
+            /* Aha, we do need a helper here for addc */
+            break;
+        default:
+            break;
+        }
+    break;
+
+    case 0x0002:    /*l.sub*/
+        switch (op1) {
+        case 0x00:
+            LOG_DIS("l.sub r%d, r%d, r%d\n", rd, ra, rb);
+            /* Aha, we do need a helper here for sub */
+            break;
+        default:
+            break;
+        }
+    break;
+
+    case 0x0003:   /*l.and*/
+        switch (op1) {
+        case 0x00:
+            LOG_DIS("l.and r%d, r%d, r%d\n", rd, ra, rb);
+            tcg_gen_and_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            break;
+        default:
+            break;
+        }
+    break;
+
+    case 0x0004:   /*l.or*/
+        switch (op1) {
+        case 0x00:
+            LOG_DIS("l.or r%d, r%d, r%d\n", rd, ra, rb);
+            tcg_gen_or_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            break;
+        default:
+            break;
+        }
+    break;
+
+    case 0x0005:
+        switch (op1) {
+        case 0x00:   /*l.xor*/
+            LOG_DIS("l.xor r%d, r%d, r%d\n", rd, ra, rb);
+            tcg_gen_xor_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            break;
+        default:
+            break;
+        }
+    break;
+
+    case 0x0006:
+        switch (op1) {
+        case 0x03:   /*l.mul*/
+            LOG_DIS("l.mul r%d, r%d, r%d\n", rd, ra, rb);
+            if (ra != 0 && rb != 0) {
+                tcg_gen_mul_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+                tcg_gen_ext32s_tl(cpu_R[rd], cpu_R[rd]);
+            } else {
+                tcg_gen_movi_tl(cpu_R[rd], 0x0);
+            }
+        break;
+        default:
+            break;
+        }
+    break;
+
+    case 0x0009:
+        switch (op1) {
+        case 0x03:   /*l.div*/
+            LOG_DIS("l.div r%d, r%d, r%d\n", rd, ra, rb);
+            if (rb != 0) {
+                tcg_gen_div_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            } else {
+                /* exception here */
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000a:
+        switch (op1) {
+        case 0x03:   /*l.divu*/
+            LOG_DIS("l.divu r%d, r%d, r%d\n", rd, ra, rb);
+            if (rb != 0) {
+                tcg_gen_divu_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            } else {
+                /* exception here */
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000b:
+        switch (op1) {
+        case 0x03:   /*l.mulu*/
+            LOG_DIS("l.mulu r%d, r%d, r%d\n", rd, ra, rb);
+            if (rb != 0 && ra != 0) {
+                tcg_gen_ext32u_tl(cpu_R[ra], cpu_R[ra]);
+                tcg_gen_ext32u_tl(cpu_R[rb], cpu_R[rb]);
+                tcg_gen_mul_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            } else {
+                tcg_gen_movi_tl(cpu_R[rd], 0);
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000e:
+        switch (op1) {
+        case 0x00:   /*l.cmov*/
+            LOG_DIS("l.cmov r%d, r%d, r%d\n", rd, ra, rb);
+            {
+                int lab = gen_new_label();
+                TCGv sr_f = tcg_temp_new();
+                tcg_gen_andi_tl(sr_f, cpu_sr, SR_F);
+                tcg_gen_mov_tl(cpu_R[rd], cpu_R[rb]);
+                tcg_gen_brcondi_tl(TCG_COND_NE, sr_f, SR_F, lab);
+                tcg_gen_mov_tl(cpu_R[rd], cpu_R[ra]);
+                gen_set_label(lab);
+                tcg_temp_free(sr_f);
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000f:
+        switch (op1) {
+        case 0x00:   /*l.ff1*/
+            LOG_DIS("l.ff1 r%d, r%d, r%d\n", rd, ra, rb);
+            /* ff1 need a helper here */
+            break;
+        case 0x01:   /*l.fl1*/
+            LOG_DIS("l.fl1 r%d, r%d, r%d\n", rd, ra, rb);
+            /* fl1 need a helper here */
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x0008:
+        switch (op1) {
+        case 0x00:
+            switch (op2) {
+            case 0x00:   /*l.sll*/
+                LOG_DIS("l.sll r%d, r%d, r%d\n", rd, ra, rb);
+                tcg_gen_shl_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+                break;
+            case 0x01:   /*l.srl*/
+                LOG_DIS("l.srl r%d, r%d, r%d\n", rd, ra, rb);
+                tcg_gen_shr_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+                break;
+            case 0x02:   /*l.sra*/
+                LOG_DIS("l.sra r%d, r%d, r%d\n", rd, ra, rb);
+                tcg_gen_sar_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+                break;
+            case 0x03:   /*l.ror*/
+                LOG_DIS("l.ror r%d, r%d, r%d\n", rd, ra, rb);
+                tcg_gen_rotr_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+                break;
+            default:
+                break;
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000c:
+        switch (op1) {
+        case 0x00:
+            switch (op2) {
+            case 0x00:   /*l.exths*/
+                LOG_DIS("l.exths r%d, r%d\n", rd, ra);
+                tcg_gen_ext16s_tl(cpu_R[rd], cpu_R[ra]);
+                break;
+            case 0x01:   /*l.extbs*/
+                LOG_DIS("l.extbs r%d, r%d\n", rd, ra);
+                tcg_gen_ext8s_tl(cpu_R[rd], cpu_R[ra]);
+                break;
+            case 0x02:   /*l.exthz*/
+                LOG_DIS("l.exthz r%d, r%d\n", rd, ra);
+                tcg_gen_ext16u_tl(cpu_R[rd], cpu_R[ra]);
+                break;
+            case 0x03:   /*l.extbz*/
+                LOG_DIS("l.extbz r%d, r%d\n", rd, ra);
+                tcg_gen_ext8u_tl(cpu_R[rd], cpu_R[ra]);
+                break;
+            default:
+                break;
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000d:
+        switch (op1) {
+        case 0x00:
+            switch (op2) {
+            case 0x00:   /*l.extws*/
+                LOG_DIS("l.extws r%d, r%d\n", rd, ra);
+                tcg_gen_ext32s_tl(cpu_R[rd], cpu_R[ra]);
+                break;
+            case 0x01:    /*l.extwz*/
+                LOG_DIS("l.extwz r%d, r%d\n", rd, ra);
+                tcg_gen_ext32u_tl(cpu_R[rd], cpu_R[ra]);
+                break;
+            default:
+                break;
+            }
+        default:
+            break;
+        }
+        break;
+
+    default:
+        break;
+    }
+}
+
+static void dec_misc(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0, op1;
+    uint32_t ra, rb, rd;
+    uint32_t /*L6, K5, */I16, I5, I11, N26, tmp;
+    op0 = field(insn, 26, 6);
+    op1 = field(insn, 24, 2);
+    ra = field(insn, 16, 5);
+    rb = field(insn, 11, 5);
+    rd = field(insn, 21, 5);
+    /*L6 = field(insn, 5, 6);
+    K5 = field(insn, 0, 5);*/
+    I16 = field(insn, 0, 16);
+    I5 = field(insn, 21, 5);
+    I11 = field(insn, 0, 11);
+    N26 = field(insn, 0, 26);
+    tmp = (I5<<11) + I11;
+    TCGv t0 = tcg_temp_new();
+
+    switch (op0) {
+    case 0x00:   /*l.j*/
+        {
+            LOG_DIS("l.j %d\n", N26);
+            tmp = sign_extend((N26<<2), 26) + dc->pc;
+            tcg_gen_movi_tl(jmp_pc, tmp);
+            dc->delayed_branch = 2;
+            dc->tb_flags |= D_FLAG;
+            tcg_gen_movi_i32(env_flags, dc->tb_flags);
+        }
+        break;
+
+    case 0x01:   /*l.jal*/
+        {
+            LOG_DIS("l.jal %d\n", N26);
+            tcg_gen_movi_tl(cpu_R[9], (dc->pc + 8));
+            tmp = sign_extend((N26<<2), 26) + dc->pc;
+            tcg_gen_movi_tl(jmp_pc, tmp);
+            dc->delayed_branch = 2;
+            dc->tb_flags |= D_FLAG;
+            tcg_gen_movi_i32(env_flags, dc->tb_flags);
+        }
+        break;
+
+    case 0x03:   /*l.bnf*/
+        LOG_DIS("l.bnf %d\n", N26);
+        {
+            int lab = gen_new_label();
+            TCGv sr_f = tcg_temp_new();
+            tcg_gen_andi_tl(sr_f, cpu_sr, SR_F);
+            tmp = sign_extend((N26 << 2), 26) + dc->pc;
+            tcg_gen_movi_tl(jmp_pc, dc->pc+8);
+            tcg_gen_brcondi_tl(TCG_COND_EQ, sr_f, SR_F, lab);
+            tcg_gen_movi_tl(jmp_pc, tmp);
+            gen_set_label(lab);
+            dc->delayed_branch = 2;
+            dc->tb_flags |= D_FLAG;
+            tcg_gen_movi_i32(env_flags, dc->tb_flags);
+            tcg_temp_free(sr_f);
+        }
+        break;
+
+    case 0x04:   /*l.bf*/
+        LOG_DIS("l.bf %d\n", N26);
+        {
+            int lab = gen_new_label();
+            TCGv sr_f = tcg_temp_new();
+            tcg_gen_andi_tl(sr_f, cpu_sr, SR_F);
+            tmp = sign_extend((N26 << 2), 26) + dc->pc;
+            tcg_gen_movi_tl(jmp_pc, dc->pc+8);
+            tcg_gen_brcondi_tl(TCG_COND_NE, sr_f, SR_F, lab);
+            tcg_gen_movi_tl(jmp_pc, tmp);
+            gen_set_label(lab);
+            dc->delayed_branch = 2;
+            dc->tb_flags |= D_FLAG;
+            tcg_gen_movi_i32(env_flags, dc->tb_flags);
+            tcg_temp_free(sr_f);
+        }
+        break;
+
+    case 0x05:
+        switch (op1) {
+        case 0x01:   /*l.nop*/
+            LOG_DIS("l.nop %d\n", I16);
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x11:    /*l.jr*/
+        LOG_DIS("l.jr r%d\n", rb);
+        {
+            tcg_gen_mov_tl(jmp_pc, cpu_R[rb]);
+            dc->delayed_branch = 2;
+            dc->tb_flags |= D_FLAG;
+            tcg_gen_movi_i32(env_flags, dc->tb_flags);
+        }
+        break;
+
+    case 0x12:    /*l.jalr*/
+        LOG_DIS("l.jalr r%d\n", rb);
+        {
+            tcg_gen_movi_tl(cpu_R[9], (dc->pc + 8));
+            gen_load_gpr(jmp_pc, rb);
+            dc->delayed_branch = 2;
+            dc->tb_flags |= D_FLAG;
+            tcg_gen_movi_i32(env_flags, dc->tb_flags);
+        }
+        break;
+
+    case 0x13:    /*l.maci*/
+        {
+            LOG_DIS("l.maci %d, r%d, %d\n", I5, ra, I11);
+            TCGv_i64 tra = tcg_temp_new_i64();    /*  store cpu_R[ra]*/
+            TCGv_i64 tmac = tcg_temp_new_i64();   /*  store machi maclo*/
+            tcg_gen_movi_tl(t0, tmp);
+            tcg_gen_ext32s_i64(tra, cpu_R[ra]);
+            tcg_gen_ext32s_i64(tmac, t0);
+            tcg_gen_mul_tl(tmac, tra, tmac);
+            tcg_gen_trunc_i64_i32(cpu_R[ra], tmac);
+            tcg_gen_add_i64(machi, machi, cpu_R[ra]);
+            tcg_gen_add_i64(maclo, maclo, cpu_R[ra]);
+            tcg_temp_free(tra);
+            tcg_temp_free(tmac);
+        }
+        break;
+
+    case 0x09:    /*l.rfe*/
+        LOG_DIS("l.rfe\n");
+        {
+            /* rfe need a helper here */
+            dc->is_jmp = DISAS_UPDATE;
+        }
+        break;
+
+    case 0x1c:    /*l.cust1*/
+        LOG_DIS("l.cust1\n");
+        break;
+
+    case 0x1d:    /*l.cust2*/
+        LOG_DIS("l.cust2\n");
+        break;
+
+    case 0x1e:    /*l.cust3*/
+        LOG_DIS("l.cust3\n");
+        break;
+
+    case 0x1f:    /*l.cust4*/
+        LOG_DIS("l.cust4\n");
+        break;
+
+    case 0x3c:   /*l.cust5*/
+        /*LOG_DIS("l.cust5 r%d, r%d, r%d, %d, %d\n", rd, ra, rb, L6, K5);*/
+        break;
+
+    case 0x3d:   /*l.cust6*/
+        LOG_DIS("l.cust6\n");
+        break;
+
+    case 0x3e:   /*l.cust7*/
+        LOG_DIS("l.cust7\n");
+        break;
+
+    case 0x3f:   /*l.cust8*/
+        LOG_DIS("l.cust8\n");
+        break;
+
+    case 0x20:   /*l.ld*/
+        LOG_DIS("l.ld r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld64(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x21:   /*l.lwz*/
+        LOG_DIS("l.lwz r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld32u(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x22:   /*l.lws*/
+        LOG_DIS("l.lws r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld32s(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x23:   /*l.lbz*/
+        LOG_DIS("l.lbz r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld8u(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x24:   /*l.lbs*/
+        LOG_DIS("l.lbs r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld8s(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x25:   /*l.lhz*/
+        LOG_DIS("l.lhz r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld16u(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x26:   /*l.lhs*/
+        LOG_DIS("l.lhs r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld16s(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x27:   /*l.addi*/
+        LOG_DIS("l.addi r%d, r%d, %d\n", rd, ra, I16);
+        {
+            TCGv t0 = tcg_const_tl(sign_extend(I16, 16));
+            /* Aha, we do need a helper here for addi */
+            tcg_temp_free(t0);
+        }
+        break;
+
+    case 0x28:   /*l.addic*/
+        LOG_DIS("l.addic r%d, r%d, %d\n", rd, ra, I16);
+        {
+            TCGv t0 = tcg_const_tl(sign_extend(I16, 16));
+            /* Aha, we do need a helper here for addic */
+            tcg_temp_free(t0);
+        }
+        break;
+
+    case 0x29:   /*l.andi*/
+        LOG_DIS("l.andi r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_andi_tl(cpu_R[rd], cpu_R[ra], zero_extend(I16, 16));
+        break;
+
+    case 0x2a:   /*l.ori*/
+        LOG_DIS("l.ori r%d, r%d, %d\n", rd, ra, zero_extend(I16, 16));
+        tcg_gen_ori_tl(cpu_R[rd], cpu_R[ra], I16);
+        break;
+
+    case 0x2b:   /*l.xori*/
+        LOG_DIS("l.xori r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_xori_tl(cpu_R[rd], cpu_R[ra], sign_extend(I16, 16));
+        break;
+
+    case 0x2c:   /*l.muli*/
+        LOG_DIS("l.muli r%d, r%d, %d\n", rd, ra, I16);
+        if (ra != 0 && I16 != 0) {
+            tcg_gen_muli_tl(cpu_R[rd], cpu_R[ra], I16);
+            tcg_gen_ext32s_tl(cpu_R[rd], cpu_R[rd]);
+        } else {
+            tcg_gen_movi_tl(cpu_R[rd], 0);
+        }
+        break;
+
+    case 0x2d:   /*l.mfspr*/
+        LOG_DIS("l.mfspr r%d, r%d, %d\n", rd, ra, I16);
+        {
+            TCGv_i32 ti = tcg_const_i32(I16);
+            TCGv td = tcg_const_tl(rd);
+            TCGv ta = tcg_const_tl(ra);
+            /* mfspr need a helper here */
+            tcg_temp_free_i32(ti);
+            tcg_temp_free(td);
+            tcg_temp_free(ta);
+        }
+        break;
+
+    case 0x30:  /*l.mtspr*/
+        LOG_DIS("l.mtspr %d, r%d, r%d, %d\n", I5, ra, rb, I11);
+        {
+            TCGv_i32 ti = tcg_const_i32(tmp);
+            TCGv ta = tcg_const_tl(ra);
+            TCGv tb = tcg_const_tl(rb);
+            /* mtspr need a helper here */
+            tcg_temp_free_i32(ti);
+            tcg_temp_free(ta);
+            tcg_temp_free(tb);
+        }
+        break;
+
+    case 0x34:   /*l.sd*/
+        LOG_DIS("l.sd %d, r%d, r%d, %d\n", I5, ra, rb, I11);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(tmp, 16));
+        tcg_gen_qemu_st64(cpu_R[rb], t0, dc->mem_idx);
+        break;
+
+    case 0x35:   /*l.sw*/
+        LOG_DIS("l.sw %d, r%d, r%d, %d\n", I5, ra, rb, I11);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(tmp, 16));
+        tcg_gen_qemu_st32(cpu_R[rb], t0, dc->mem_idx);
+        break;
+
+    case 0x36:   /*l.sb*/
+        LOG_DIS("l.sb %d, r%d, r%d, %d\n", I5, ra, rb, I11);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(tmp, 16));
+        tcg_gen_qemu_st8(cpu_R[rb], t0, dc->mem_idx);
+        break;
+
+    case 0x37:   /*l.sh*/
+        LOG_DIS("l.sh %d, r%d, r%d, %d\n", I5, ra, rb, I11);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(tmp, 16));
+        tcg_gen_qemu_st16(cpu_R[rb], t0, dc->mem_idx);
+        break;
+    default:
+        break;
+    }
+    tcg_temp_free(t0);
+}
+
+static void dec_mac(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t ra, rb;
+    op0 = field(insn, 0, 4);
+    ra = field(insn, 16, 5);
+    rb = field(insn, 11, 5);
+    TCGv_i64 t0 = tcg_temp_new();
+    TCGv_i64 t1 = tcg_temp_new();
+    switch (op0) {
+    case 0x0001:   /*l.mac*/
+        {
+            LOG_DIS("l.mac r%d, r%d\n", ra, rb);
+            tcg_gen_ext_i32_i64(t0, cpu_R[ra]);
+            tcg_gen_ext_i32_i64(t1, cpu_R[rb]);
+            tcg_gen_mul_i64(t0, t0, t1);
+            tcg_gen_trunc_i64_i32(cpu_R[ra], t0);
+            tcg_gen_add_tl(maclo, maclo, cpu_R[ra]);
+            tcg_gen_add_tl(machi, machi, cpu_R[ra]);
+            tcg_temp_free(t0);
+            tcg_temp_free(t1);
+        }
+        break;
+
+    case 0x0002:   /*l.msb*/
+        {
+            LOG_DIS("l.msb r%d, r%d\n", ra, rb);
+            tcg_gen_ext_i32_i64(t0, cpu_R[ra]);
+            tcg_gen_ext_i32_i64(t1, cpu_R[rb]);
+            tcg_gen_mul_i64(t0, t0, t1);
+            tcg_gen_trunc_i64_i32(cpu_R[ra], t0);
+            tcg_gen_sub_tl(maclo, maclo, cpu_R[ra]);
+            tcg_gen_sub_tl(machi, machi, cpu_R[ra]);
+            tcg_temp_free(t0);
+            tcg_temp_free(t1);
+        }
+        break;
+
+    default:
+        break;
+   }
+}
+
+static void dec_logic(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t rd, ra, L6;
+    op0 = field(insn, 6, 2);
+    rd = field(insn, 21, 5);
+    ra = field(insn, 16, 5);
+    L6 = field(insn, 0, 6);
+
+    switch (op0) {
+    case 0x00:    /*l.slli*/
+        LOG_DIS("l.slli r%d, r%d, %d\n", rd, ra, L6);
+        tcg_gen_shli_tl(cpu_R[rd], cpu_R[ra], (L6 & 0x1f));
+        break;
+
+    case 0x01:    /*l.srli*/
+        LOG_DIS("l.srli r%d, r%d, %d\n", rd, ra, L6);
+        tcg_gen_shri_tl(cpu_R[rd], cpu_R[ra], (L6 & 0x1f));
+        break;
+
+    case 0x02:    /*l.srai*/
+        LOG_DIS("l.srai r%d, r%d, %d\n", rd, ra, L6);
+        tcg_gen_sari_tl(cpu_R[rd], cpu_R[ra], (L6 & 0x1f)); break;
+
+    case 0x03:    /*l.rori*/
+        LOG_DIS("l.rori r%d, r%d, %d\n", rd, ra, L6);
+        tcg_gen_rotri_tl(cpu_R[rd], cpu_R[ra], (L6 & 0x1f));
+        break;
+
+    default:
+        break;
+    }
+}
+
+static void dec_M(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t rd;
+    uint32_t K16;
+    op0 = field(insn, 16, 1);
+    rd = field(insn, 21, 5);
+    K16 = field(insn, 0, 16);
+
+    switch (op0) {
+    case 0x0:    /*l.movhi*/
+        LOG_DIS("l.movhi  r%d, %d\n", rd, K16);
+        tcg_gen_movi_tl(cpu_R[rd], (K16 << 16));
+        break;
+
+    case 0x1:    /*l.macrc*/
+        LOG_DIS("l.macrc  r%d\n", rd);
+        tcg_gen_mov_tl(cpu_R[rd], maclo);
+        tcg_gen_movi_tl(maclo, 0x0);
+        tcg_gen_movi_tl(machi, 0x0);
+        break;
+
+    default:
+        break;
+    }
+}
+
+static void dec_comp(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t ra, rb;
+
+    op0 = field(insn, 21, 5);
+    ra = field(insn, 16, 5);
+    rb = field(insn, 11, 5);
+
+    tcg_gen_movi_i32(env_btaken, 0x0);
+    /* unsigned integers  */
+    tcg_gen_ext32u_tl(cpu_R[ra], cpu_R[ra]);
+    tcg_gen_ext32u_tl(cpu_R[rb], cpu_R[rb]);
+
+    switch (op0) {
+    case 0x0:    /*l.sfeq*/
+        LOG_DIS("l.sfeq  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_EQ, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x1:    /*l.sfne*/
+        LOG_DIS("l.sfne  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_NE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x2:    /*l.sfgtu*/
+        LOG_DIS("l.sfgtu  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_GTU, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x3:    /*l.sfgeu*/
+        LOG_DIS("l.sfgeu  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_GEU, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x4:    /*l.sfltu*/
+        LOG_DIS("l.sfltu  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_LTU, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x5:    /*l.sfleu*/
+        LOG_DIS("l.sfleu  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_LEU, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0xa:    /*l.sfgts*/
+        LOG_DIS("l.sfgts  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_GT, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0xb:    /*l.sfges*/
+        LOG_DIS("l.sfges  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_GE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0xc:    /*l.sflts*/
+        LOG_DIS("l.sflts  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_LT, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0xd:    /*l.sfles*/
+        LOG_DIS("l.sfles  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_LE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    default:
+        break;
+    }
+    wb_SR_F();
+}
+
+static void dec_compi(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t ra, I16;
+
+    op0 = field(insn, 21, 5);
+    ra = field(insn, 16, 5);
+    I16 = field(insn, 0, 16);
+
+    tcg_gen_movi_i32(env_btaken, 0x0);
+    tcg_gen_ext32u_tl(cpu_R[ra], cpu_R[ra]);
+    I16 = sign_extend(I16, 16);
+
+    switch (op0) {
+    case 0x0:    /*l.sfeqi*/
+        LOG_DIS("l.sfeqi  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_EQ, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0x1:    /*l.sfnei*/
+        LOG_DIS("l.sfnei  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_NE, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0x2:    /*l.sfgtui*/
+        LOG_DIS("l.sfgtui  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_GTU, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0x3:    /*l.sfgeui*/
+        LOG_DIS("l.sfgeui  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_GEU, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0x4:    /*l.sfltui*/
+        LOG_DIS("l.sfltui  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_LTU, env_btaken, cpu_R[ra],
+                            sign_extend(I16, 16));
+        break;
+
+    case 0x5:    /*l.sfleui*/
+        LOG_DIS("l.sfleui  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_LEU, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0xa:    /*l.sfgtsi*/
+        LOG_DIS("l.sfgtsi  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_GT, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0xb:    /*l.sfgesi*/
+        LOG_DIS("l.sfgesi  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_GE, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0xc:    /*l.sfltsi*/
+        LOG_DIS("l.sfltsi  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_LT, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0xd:    /*l.sflesi*/
+        LOG_DIS("l.sflesi  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_LE, env_btaken, cpu_R[ra], I16);
+        break;
+
+    default:
+        break;
+    }
+    wb_SR_F();
+}
+
+static void dec_sys(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    /*uint32_t K16;*/
+    op0 = field(insn, 16, 8);
+    /*K16 = field(insn, 0, 16);*/
+
+    switch (op0) {
+    case 0x000:  /*l.sys*/
+        /*LOG_DIS("l.sys %d\n", K16);*/
+        tcg_gen_movi_tl(cpu_pc, dc->pc);
+        /* exception here */
+        dc->is_jmp = DISAS_UPDATE;
+        break;
+
+    case 0x100:  /*l.trap*/
+        /*LOG_DIS("l.trap %d\n", K16);*/
+        tcg_gen_movi_tl(cpu_pc, dc->pc);
+        /* exception here */
+        break;
+
+    case 0x300:  /*l.csync*/
+        LOG_DIS("l.csync\n");
+        break;
+
+    case 0x200:  /*l.msync*/
+        LOG_DIS("l.msync\n");
+        break;
+
+    case 0x270:  /*l.psync*/
+        LOG_DIS("l.psync\n");
+        break;
+
+    default:
+        break;
+    }
+}
+
+static void dec_float(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t ra, rb, rd;
+    op0 = field(insn, 0, 8);
+    ra = field(insn, 16, 5);
+    rb = field(insn, 11, 5);
+    rd = field(insn, 21, 5);
+
+    switch (op0) {
+    case 0x10:    /*lf.add.d*/
+        LOG_DIS("lf.add.d r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_add_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x00:    /*lf.add.s*/
+        LOG_DIS("lf.add.s r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_add_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x11:    /*lf.sub.d*/
+        LOG_DIS("lf.sub.d r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_sub_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x01:    /*lf.sub.s*/
+        LOG_DIS("lf.sub.s r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_sub_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x12:    /*lf.mul.d*/
+        LOG_DIS("lf.mul.d r%d, r%d, r%d\n", rd, ra, rb);
+        {
+            if (cpu_R[ra] != 0 && cpu_R[rb] != 0) {
+                tcg_gen_mul_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            } else {
+                tcg_gen_movi_i64(cpu_R[rd], 0x0);
+            }
+        }
+        break;
+
+    case 0x02:    /*lf.mul.s*/
+        LOG_DIS("lf.mul.s r%d, r%d, r%d\n", rd, ra, rb);
+        if (cpu_R[ra] != 0 && cpu_R[rb] != 0) {
+            tcg_gen_mul_i32(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        } else {
+            tcg_gen_movi_i64(cpu_R[rd], 0x0);
+        }
+        break;
+
+    case 0x13:    /*lf.div.d*/
+        LOG_DIS("lf.div.d r%d, r%d, r%d\n", rd, ra, rb);
+        if (cpu_R[rb] != 0) {
+            tcg_gen_div_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        } else {
+            /* exception here */
+        }
+        break;
+
+    case 0x03:    /*lf.div.s*/
+        LOG_DIS("lf.div.s r%d, r%d, r%d\n", rd, ra, rb);
+        if (cpu_R[rb] != 0) {
+            tcg_gen_div_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        } else {
+            /* exception here */
+        }
+        break;
+
+    case 0x14:    /*lf.itof.d*/
+        LOG_DIS("lf.itof r%d, r%d\n", rd, ra);
+        /* itof.d need a helper here */
+        break;
+
+    case 0x04:    /*lf.itof.s*/
+        LOG_DIS("lf.itof r%d, r%d\n", rd, ra);
+        /* itof.s need a helper here */
+        break;
+
+    case 0x15:    /*lf.ftoi.d*/
+        LOG_DIS("lf.ftoi r%d, r%d\n", rd, ra);
+        /* ftoi.d need a helper here */
+        break;
+
+    case 0x05:    /*lf.ftoi.s*/
+        LOG_DIS("lf.ftoi r%d, r%d\n", rd, ra);
+        /* ftoi.s need a helper here */
+        break;
+
+    case 0x16:    /*lf.rem.d*/
+        LOG_DIS("lf.rem.d r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_rem_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x06:    /*lf.rem.s*/
+        LOG_DIS("lf.rem.s r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_rem_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x17:    /*lf.madd.d*/
+        LOG_DIS("lf.madd.d r%d, r%d, r%d\n", rd, ra, rb);
+        {
+            TCGv_i64 t0, t1;
+
+            t0 = tcg_temp_new_i64();
+            t1 = tcg_temp_new_i64();
+
+            tcg_gen_ext_i32_i64(t0, cpu_R[ra]);
+            tcg_gen_ext_i32_i64(t1, cpu_R[rb]);
+            tcg_gen_mul_i64(t0, t0, t1);
+
+            tcg_gen_trunc_i64_i32(cpu_R[ra], t0);
+            tcg_gen_add_tl(fpmaddlo, cpu_R[ra], fpmaddlo);
+            tcg_gen_shri_i64(t0, t0, 32);
+            tcg_gen_trunc_i64_i32(cpu_R[rb], t0);
+            tcg_gen_add_tl(fpmaddhi, cpu_R[rb], fpmaddhi);
+
+            tcg_temp_free_i64(t0);
+            tcg_temp_free_i64(t1);
+        }
+        break;
+
+    case 0x07:    /*lf.madd.s*/
+        LOG_DIS("lf.madd.s r%d, r%d, r%d\n", rd, ra, rb);
+        {
+            TCGv_i64 t0, t1;
+
+            t0 = tcg_temp_new_i64();
+            t1 = tcg_temp_new_i64();
+
+            tcg_gen_ext_i32_i64(t0, cpu_R[ra]);
+            tcg_gen_ext_i32_i64(t1, cpu_R[rb]);
+            tcg_gen_mul_i64(t0, t0, t1);
+
+            tcg_gen_trunc_i64_i32(cpu_R[ra], t0);
+            tcg_gen_add_tl(fpmaddlo, cpu_R[ra], fpmaddlo);
+            tcg_gen_shri_i64(t0, t0, 32);
+            tcg_gen_trunc_i64_i32(cpu_R[rb], t0);
+            tcg_gen_add_tl(fpmaddhi, cpu_R[rb], fpmaddhi);
+            tcg_temp_free_i64(t0);
+            tcg_temp_free_i64(t1);
+
+        }
+        break;
+
+    case 0x18:    /*lf.sfeq.d*/
+        LOG_DIS("lf.sfeq.d r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_i64(TCG_COND_GE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x08:    /*lf.sfeq.s*/
+        LOG_DIS("lf.sfeq.s r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_EQ, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x19:    /*lf.sfne.d*/
+        LOG_DIS("lf.sfne.d r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_i64(TCG_COND_NE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x09:    /*lf.sfne.s*/
+        LOG_DIS("lf.sfne.s r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_NE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x1a:    /*lf.sfgt.d*/
+        LOG_DIS("lf.sfgt.d r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_i64(TCG_COND_GT, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x0a:    /*lf.sfgt.s*/
+        LOG_DIS("lf.sfgt.s r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_GT, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x1b:    /*lf.sfge.d*/
+        LOG_DIS("lf.sfge.d r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_i64(TCG_COND_GE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x0b:    /*lf.sfge.s*/
+        LOG_DIS("lf.sfge.s r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_GE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x1c:    /*lf.sflt.d*/
+        LOG_DIS("lf.sflt.d r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_i64(TCG_COND_LT, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x0c:    /*lf.sflt.s*/
+        LOG_DIS("lf.sflt.s r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_LT, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x1d:    /*lf.sfle.d*/
+        LOG_DIS("lf.sfle.d r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_i64(TCG_COND_LE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x0d:    /*lf.sfle.s*/
+        LOG_DIS("lf.sfle.s r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_LE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    default:
+        break;
+    }
+    wb_SR_F();
+}
+
+static void disas_openrisc_insn(DisasContext *dc, CPUOPENRISCState *env)
+{
+    uint32_t op0;
+    uint32_t insn;
+    insn = ldl_code(dc->pc);
+    op0 = field(insn, 26, 6);
+
+    switch (op0) {
+    case 0x06:
+        dec_M(dc, env, insn);
+        break;
+
+    case 0x08:
+        dec_sys(dc, env, insn);
+        break;
+
+    case 0x2e:
+        dec_logic(dc, env, insn);
+        break;
+
+    case 0x2f:
+        dec_compi(dc, env, insn);
+        break;
+
+    case 0x31:
+        dec_mac(dc, env, insn);
+        break;
+
+    case 0x32:
+        dec_float(dc, env, insn);
+        break;
+
+    case 0x38:
+        dec_calc(dc, env, insn);
+        break;
+
+    case 0x39:
+        dec_comp(dc, env, insn);
+        break;
+
+    default:
+        dec_misc(dc, env, insn);
+        break;
+    }
+}
+
+static void check_breakpoint(CPUOPENRISCState *env, DisasContext *dc)
+{
+    CPUBreakpoint *bp;
+
+    if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
+        QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
+            if (bp->pc == dc->pc) {
+                tcg_gen_movi_tl(cpu_pc, dc->pc);
+                /* exception here */
+                dc->is_jmp = DISAS_UPDATE;
+            }
+        }
+    }
 }
 
 static inline void gen_intermediate_code_internal(CPUOPENRISCState *env,
                                                   TranslationBlock *tb,
                                                   int search_pc)
 {
+    struct DisasContext ctx, *dc = &ctx;
+    uint16_t *gen_opc_end;
+    uint32_t pc_start;
+    int j, lj;
+    uint32_t next_page_start;
+    int num_insns;
+    int max_insns;
+
+    qemu_log_try_set_file(stderr);
+
+    pc_start = tb->pc;
+    dc->env = env;
+    dc->tb = tb;
+
+    gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
+    dc->is_jmp = DISAS_NEXT;
+    dc->ppc = pc_start;
+    dc->pc = pc_start;
+    dc->mem_idx = cpu_mmu_index(env);
+    dc->tb_flags = tb->flags;
+    dc->delayed_branch = !!(dc->tb_flags & D_FLAG);
+    dc->singlestep_enabled = env->singlestep_enabled;
+    if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
+        qemu_log("-----------------------------------------\n");
+        log_cpu_state(env, 0);
+    }
+
+    next_page_start = (pc_start & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
+    lj = -1;
+    num_insns = 0;
+    max_insns = tb->cflags & CF_COUNT_MASK;
+
+    if (max_insns == 0) {
+        max_insns = CF_COUNT_MASK;
+    }
+
+    gen_icount_start();
+
+    do {
+        check_breakpoint(env, dc);
+        if (search_pc) {
+            j = gen_opc_ptr - gen_opc_buf;
+            if (lj < j) {
+                lj++;
+                while (lj < j) {
+                    gen_opc_instr_start[lj++] = 0;
+                }
+            }
+            gen_opc_pc[lj] = dc->pc;
+            gen_opc_instr_start[lj] = 1;
+            gen_opc_icount[lj] = num_insns;
+        }
+
+        if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP))) {
+            tcg_gen_debug_insn_start(dc->pc);
+        }
+
+        if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO)) {
+            gen_io_start();
+        }
+        dc->ppc = dc->pc - 4;
+        dc->npc = dc->pc + 4;
+        tcg_gen_movi_tl(cpu_ppc, dc->ppc);
+        tcg_gen_movi_tl(cpu_npc, dc->npc);
+        disas_openrisc_insn(dc, env);
+        dc->pc = dc->npc;
+        num_insns++;
+        /* delay slot */
+        if (dc->delayed_branch) {
+            dc->delayed_branch--;
+            if (!dc->delayed_branch) {
+                dc->tb_flags &= ~D_FLAG;
+                tcg_gen_movi_i32(env_flags, dc->tb_flags);
+                tcg_gen_mov_tl(cpu_pc, jmp_pc);
+                tcg_gen_mov_tl(cpu_npc, jmp_pc);
+                tcg_gen_movi_tl(jmp_pc, 0);
+                tcg_gen_exit_tb(0);
+                dc->is_jmp = DISAS_JUMP;
+                break;
+            }
+        }
+    } while (!dc->is_jmp && gen_opc_ptr < gen_opc_end
+             && !env->singlestep_enabled
+             && !singlestep
+             && (dc->pc < next_page_start)
+             && num_insns < max_insns);
+
+    if (tb->cflags & CF_LAST_IO) {
+        gen_io_end();
+    }
+    if (dc->is_jmp == DISAS_NEXT) {
+        dc->is_jmp = DISAS_UPDATE;
+        tcg_gen_movi_tl(cpu_pc, dc->pc);
+    }
+    if (unlikely(env->singlestep_enabled)) {
+        if (dc->is_jmp == DISAS_NEXT) {
+            tcg_gen_movi_tl(cpu_pc, dc->pc);
+        }
+        /* exception here*/
+    } else {
+        switch (dc->is_jmp) {
+        case DISAS_NEXT:
+            gen_goto_tb(dc, 0, dc->pc);
+            break;
+        default:
+        case DISAS_JUMP:
+            break;
+        case DISAS_UPDATE:
+            /* indicate that the hash table must be used
+               to find the next TB */
+            tcg_gen_exit_tb(0);
+            break;
+        case DISAS_TB_JUMP:
+            /* nothing more to generate */
+            break;
+        }
+    }
+
+    gen_icount_end(tb, num_insns);
+    *gen_opc_ptr = INDEX_op_end;
+    if (search_pc) {
+        j = gen_opc_ptr - gen_opc_buf;
+        lj++;
+        while (lj <= j) {
+            gen_opc_instr_start[lj++] = 0;
+        }
+    } else {
+        tb->size = dc->pc - pc_start;
+        tb->icount = num_insns;
+    }
+
+#ifdef DEBUG_DISAS
+    if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
+        qemu_log("\n");
+        log_target_disas(pc_start, dc->pc - pc_start, 0);
+        qemu_log("\nisize=%d osize=%td\n",
+            dc->pc - pc_start, gen_opc_ptr - gen_opc_buf);
+    }
+#endif
 }
 
 void gen_intermediate_code(CPUOPENRISCState *env, struct TranslationBlock *tb)
@@ -80,7 +1461,13 @@ void cpu_dump_state(CPUOPENRISCState *env, FILE *f,
                     fprintf_function cpu_fprintf,
                     int flags)
 {
+    int i;
+    uint32_t *regs = env->gpr;
     cpu_fprintf(f, "PC=%08x\n", env->pc);
+    for (i = 0; i < 32; ++i) {
+        cpu_fprintf(f, "R%02d=%08x%c", i, regs[i],
+                    (i % 4) == 3 ? '\n' : ' ');
+    }
 }
 
 void restore_state_to_opc(CPUOPENRISCState *env, TranslationBlock *tb,
Signature · 1 line
1.7.9.5
#18 lore raw

Re: [Qemu-devel] [PATCH 03/15] Openrisc: add instructions translation

Hi.

I've got a couple of questions/suggestions regarding the code.

Quoted 3 lines · Jia Liu

add the openrisc instructions translation.

[...]

Quoted 6 lines

+    case 0x0009:
+        switch (op1) {
+        case 0x03:   /*l.div*/
+            LOG_DIS("l.div r%d, r%d, r%d\n", rd, ra, rb);
+            if (rb != 0) {
+                tcg_gen_div_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);

You also need to take care of integer overflow/division by zero here, otherwise it may crash QEMU.

Quoted 21 lines

+            } else {
+                /* exception here */
+            }
+            break;
+        default:
+            break;
+        }
+        break;

target-openrisc/translate.c +13 -0
+
+    case 0x000a:
+        switch (op1) {
+        case 0x03:   /*l.divu*/
+            LOG_DIS("l.divu r%d, r%d, r%d\n", rd, ra, rb);
+            if (rb != 0) {
+                tcg_gen_divu_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            } else {
+                /* exception here */
+            }
+            break;
+        default:
+            break;

target-openrisc/translate.c typedef struct DisasContext { · line 313

Quoted 10 lines
target-openrisc/translate.c +10 -0
+        }
+        break;
+
+    case 0x000b:
+        switch (op1) {
+        case 0x03:   /*l.mulu*/
+            LOG_DIS("l.mulu r%d, r%d, r%d\n", rd, ra, rb);
+            if (rb != 0 && ra != 0) {
+                tcg_gen_ext32u_tl(cpu_R[ra], cpu_R[ra]);
+                tcg_gen_ext32u_tl(cpu_R[rb], cpu_R[rb]);

This code would clobber high 32 bits of ra and rb if it was compiled for 64 bit registers.
Are you going to support both 32 and 64 bit version of the ISA? Could you please clarify *_tl usage here?

Quoted 12 lines

+                tcg_gen_mul_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            } else {
+                tcg_gen_movi_tl(cpu_R[rd], 0);
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000e:
+        switch (op1) {

Quoted 8 lines

+        case 0x00:   /*l.cmov*/
+            LOG_DIS("l.cmov r%d, r%d, r%d\n", rd, ra, rb);
+            {
+                int lab = gen_new_label();
+                TCGv sr_f = tcg_temp_new();
+                tcg_gen_andi_tl(sr_f, cpu_sr, SR_F);
+                tcg_gen_mov_tl(cpu_R[rd], cpu_R[rb]);
+                tcg_gen_brcondi_tl(TCG_COND_NE, sr_f, SR_F, lab);

There may be an issue when rd == ra and the branch is not taken

Quoted 9 lines

+                tcg_gen_mov_tl(cpu_R[rd], cpu_R[ra]);
+                gen_set_label(lab);
+                tcg_temp_free(sr_f);
+            }
+            break;
+        default:
+            break;
+        }
+        break;

[...]

Quoted 16 lines

+    case 0x13:    /*l.maci*/
+        {
+            LOG_DIS("l.maci %d, r%d, %d\n", I5, ra, I11);
+            TCGv_i64 tra = tcg_temp_new_i64();    /*  store cpu_R[ra]*/
+            TCGv_i64 tmac = tcg_temp_new_i64();   /*  store machi maclo*/
+            tcg_gen_movi_tl(t0, tmp);
+            tcg_gen_ext32s_i64(tra, cpu_R[ra]);
+            tcg_gen_ext32s_i64(tmac, t0);
+            tcg_gen_mul_tl(tmac, tra, tmac);
+            tcg_gen_trunc_i64_i32(cpu_R[ra], tmac);
+            tcg_gen_add_i64(machi, machi, cpu_R[ra]);
+            tcg_gen_add_i64(maclo, maclo, cpu_R[ra]);
+            tcg_temp_free(tra);
+            tcg_temp_free(tmac);
+        }
+        break;

[...]

Quoted 20 lines
target-openrisc/translate.c +19 -0
+static void dec_mac(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t ra, rb;
+    op0 = field(insn, 0, 4);
+    ra = field(insn, 16, 5);
+    rb = field(insn, 11, 5);
+    TCGv_i64 t0 = tcg_temp_new();
+    TCGv_i64 t1 = tcg_temp_new();
+    switch (op0) {
+    case 0x0001:   /*l.mac*/
+        {
+            LOG_DIS("l.mac r%d, r%d\n", ra, rb);
+            tcg_gen_ext_i32_i64(t0, cpu_R[ra]);
+            tcg_gen_ext_i32_i64(t1, cpu_R[rb]);
+            tcg_gen_mul_i64(t0, t0, t1);
+            tcg_gen_trunc_i64_i32(cpu_R[ra], t0);
+            tcg_gen_add_tl(maclo, maclo, cpu_R[ra]);
+            tcg_gen_add_tl(machi, machi, cpu_R[ra]);
Quoted 1 line

From the ISA I've got an impression that mac/maci should be

implemented like this (signedness left alone):

            tcg_gen_mul_tl(t0, cpu_R[ra], cpu_R[rb]);
            tcg_gen_ext_i32_i64(t1, t0);
            tcg_gen_concat_i32_i64(t2, maclo, machi);
            tcg_gen_add_i64(t2, t2, t1);
            tcg_gen_trunc_i64(maclo, t2);
            tcg_gen_shri_i64(t2, t2, 32);
            tcg_gen_trunc_i64(machi, t2);

[...]

target-openrisc/translate.c typedef struct DisasContext { · line 1045

Quoted 13 lines
target-openrisc/translate.c +13 -0
+static void dec_float(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t ra, rb, rd;
+    op0 = field(insn, 0, 8);
+    ra = field(insn, 16, 5);
+    rb = field(insn, 11, 5);
+    rd = field(insn, 21, 5);
+
+    switch (op0) {
+    case 0x10:    /*lf.add.d*/
+        LOG_DIS("lf.add.d r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_add_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);

Through this function you generate integer operations on the registers, although ISA
suggests that there should be either single- or double-precision floating point operations.

Signature · 2 lines
Thanks.
-- Max
author
#19 lore raw

Re: [Qemu-devel] [PATCH 03/15] Openrisc: add instructions translation

Hi Max

Thanks for comments.

Quoted 18 lines · Max Filippov

Hi.

I've got a couple of questions/suggestions regarding the code.

Quoted 3 lines · Jia Liu

add the openrisc instructions translation.

[...]

Quoted 6 lines

+    case 0x0009:
+        switch (op1) {
+        case 0x03:   /*l.div*/
+            LOG_DIS("l.div r%d, r%d, r%d\n", rd, ra, rb);
+            if (rb != 0) {
+                tcg_gen_div_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);

Quoted 5 lines

You also need to take care of integer overflow/division by zero here, otherwise it may crash QEMU.

Quoted 2 lines

+            } else {
+                /* exception here */

overflow/division by zero is handled here by patch 05/15.

Quoted 26 lines
Quoted 26 lines

+            }
+            break;
+        default:
+            break;
+        }
+        break;

target-openrisc/translate.c +20 -0
+
+    case 0x000a:
+        switch (op1) {
+        case 0x03:   /*l.divu*/
+            LOG_DIS("l.divu r%d, r%d, r%d\n", rd, ra, rb);
+            if (rb != 0) {
+                tcg_gen_divu_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            } else {
+                /* exception here */
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000b:
+        switch (op1) {
+        case 0x03:   /*l.mulu*/
+            LOG_DIS("l.mulu r%d, r%d, r%d\n", rd, ra, rb);
Quoted 9 lines
Quoted 3 lines

+            if (rb != 0 && ra != 0) {
+                tcg_gen_ext32u_tl(cpu_R[ra], cpu_R[ra]);
+                tcg_gen_ext32u_tl(cpu_R[rb], cpu_R[rb]);

This code would clobber high 32 bits of ra and rb if it was compiled for 64 bit registers.
Are you going to support both 32 and 64 bit version of the ISA? Could you please clarify *_tl usage here?

I used uint32_t at first, change it into *_tl for 64bit support in the future. May I keep it *_tl? If I can not, I'll back it to uint32.

Quoted 21 lines
Quoted 12 lines

+                tcg_gen_mul_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            } else {
+                tcg_gen_movi_tl(cpu_R[rd], 0);
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000e:
+        switch (op1) {

Quoted 8 lines

+        case 0x00:   /*l.cmov*/
+            LOG_DIS("l.cmov r%d, r%d, r%d\n", rd, ra, rb);
+            {
+                int lab = gen_new_label();
+                TCGv sr_f = tcg_temp_new();
+                tcg_gen_andi_tl(sr_f, cpu_sr, SR_F);
+                tcg_gen_mov_tl(cpu_R[rd], cpu_R[rb]);
+                tcg_gen_brcondi_tl(TCG_COND_NE, sr_f, SR_F, lab);

Quoted 1 line

There may be an issue when rd == ra and the branch is not taken

Thanks, I'll check this.

Quoted 63 lines
Quoted 10 lines

+                tcg_gen_mov_tl(cpu_R[rd], cpu_R[ra]);
+                gen_set_label(lab);
+                tcg_temp_free(sr_f);
+            }
+            break;
+        default:
+            break;
+        }
+        break;

[...]

Quoted 16 lines

+    case 0x13:    /*l.maci*/
+        {
+            LOG_DIS("l.maci %d, r%d, %d\n", I5, ra, I11);
+            TCGv_i64 tra = tcg_temp_new_i64();    /*  store cpu_R[ra]*/
+            TCGv_i64 tmac = tcg_temp_new_i64();   /*  store machi maclo*/
+            tcg_gen_movi_tl(t0, tmp);
+            tcg_gen_ext32s_i64(tra, cpu_R[ra]);
+            tcg_gen_ext32s_i64(tmac, t0);
+            tcg_gen_mul_tl(tmac, tra, tmac);
+            tcg_gen_trunc_i64_i32(cpu_R[ra], tmac);
+            tcg_gen_add_i64(machi, machi, cpu_R[ra]);
+            tcg_gen_add_i64(maclo, maclo, cpu_R[ra]);
+            tcg_temp_free(tra);
+            tcg_temp_free(tmac);
+        }
+        break;

[...]

Quoted 9 lines
target-openrisc/translate.c +9 -0
+static void dec_mac(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t ra, rb;
+    op0 = field(insn, 0, 4);
+    ra = field(insn, 16, 5);
+    rb = field(insn, 11, 5);
+    TCGv_i64 t0 = tcg_temp_new();
+    TCGv_i64 t1 = tcg_temp_new();

target-openrisc/translate.c typedef struct DisasContext { · line 767

Quoted 10 lines
target-openrisc/translate.c +10 -0
+    switch (op0) {
+    case 0x0001:   /*l.mac*/
+        {
+            LOG_DIS("l.mac r%d, r%d\n", ra, rb);
+            tcg_gen_ext_i32_i64(t0, cpu_R[ra]);
+            tcg_gen_ext_i32_i64(t1, cpu_R[rb]);
+            tcg_gen_mul_i64(t0, t0, t1);
+            tcg_gen_trunc_i64_i32(cpu_R[ra], t0);
+            tcg_gen_add_tl(maclo, maclo, cpu_R[ra]);
+            tcg_gen_add_tl(machi, machi, cpu_R[ra]);

From the ISA I've got an impression that mac/maci should be implemented like this (signedness left alone):

           tcg_gen_mul_tl(t0, cpu_R[ra], cpu_R[rb]); tcg_gen_ext_i32_i64(t1, t0); tcg_gen_concat_i32_i64(t2, maclo, machi); tcg_gen_add_i64(t2, t2, t1); tcg_gen_trunc_i64(maclo, t2); tcg_gen_shri_i64(t2, t2, 32); tcg_gen_trunc_i64(machi, t2);

Quoted 2 lines

[...]

Thanks very much, I'll replace it in V2.

Quoted 10 lines
Quoted 10 lines
target-openrisc/translate.c +10 -0
+static void dec_float(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t ra, rb, rd;
+    op0 = field(insn, 0, 8);
+    ra = field(insn, 16, 5);
+    rb = field(insn, 11, 5);
+    rd = field(insn, 21, 5);
+
+    switch (op0) {
Quoted 9 lines
Quoted 3 lines

+    case 0x10:    /*lf.add.d*/
+        LOG_DIS("lf.add.d r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_add_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);

Through this function you generate integer operations on the registers, although ISA
suggests that there should be either single- or double-precision floating point operations.

Sorry, I didn't find a TCG-IR that make single- or double-precision floating point operations, may you give me some hits?

Quoted 3 lines

--
Thanks.
-- Max

Regards,
Jia.

#20 lore raw

Re: [Qemu-devel] [PATCH 03/15] Openrisc: add instructions translation

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

+ ? 坟witch (op0) {
+ ? ?case 0x10: ? ?/*lf.add.d*/
+ ? ? ? 猂OG_DIS("lf.add.d r%d, r%d, r%d\n", rd, ra, rb);
+ ? ? ? 慯cg_gen_add_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);

Quoted 8 lines
Quoted 5 lines

Through this function you generate integer operations on the registers, although ISA
suggests that there should be either single- or double-precision floating point operations.

Sorry, I didn't find a TCG-IR that make single- or double-precision floating point operations, may you give me some hits?

I guest you need to write your own floating helper functions to do that.
Am I right, Max?

Regards,
chenwj

Signature · 5 lines
Wei-Ren Chen (陳韋任)
Computer Systems Lab, Institute of Information Science,
Academia Sinica, Taiwan (R.O.C.)
Tel:886-2-2788-3799 #1667
Homepage: http://people.cs.nctu.edu.tw/~chenwj
#21 lore raw

Re: [Qemu-devel] [PATCH 03/15] Openrisc: add instructions translation

In reply to Jia Liu

Hi Jia.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

+    case 0x0009:
+        switch (op1) {
+        case 0x03:   /*l.div*/
+            LOG_DIS("l.div r%d, r%d, r%d\n", rd, ra, rb);
+            if (rb != 0) {
+                tcg_gen_div_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);

Quoted 7 lines
Quoted 5 lines

You also need to take care of integer overflow/division by zero here, otherwise it may crash QEMU.

Quoted 2 lines

+            } else {
+                /* exception here */

overflow/division by zero is handled here by patch 05/15.

In that patch I have found only exception generation in the 'else' branch. However code generated by tcg_gen_div_tl in the 'if' branch will crash QEMU in case rb register carries zero or ra/rb gives integer overflow.

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

+    case 0x000b:
+        switch (op1) {
+        case 0x03:   /*l.mulu*/
+            LOG_DIS("l.mulu r%d, r%d, r%d\n", rd, ra, rb);
+            if (rb != 0 && ra != 0) {
+                tcg_gen_ext32u_tl(cpu_R[ra], cpu_R[ra]);
+                tcg_gen_ext32u_tl(cpu_R[rb], cpu_R[rb]);

Quoted 8 lines
Quoted 5 lines

This code would clobber high 32 bits of ra and rb if it was compiled for 64 bit registers.
Are you going to support both 32 and 64 bit version of the ISA? Could you please clarify *_tl usage here?

I used uint32_t at first, change it into *_tl for 64bit support in the future. May I keep it *_tl? If I can not, I'll back it to uint32.

Ok, to me *_tl looks pretty reasonable for instructions that scale uniformly with register width, like simple l.and/l.or/l.add/l.div/etc.

For instructions that has less symmetry regarding register width or that operate on registers of different widths at once, like l.maci, I'd say that explicit *_i32/*_i64 would be more informative.

For this particular mulu I guess the correct implementation, suitable for both 32 and 64 bit wide registers would be just

    tcg_gen_mul_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);

however it doesn't take care of SR[OV], SR[CY]. I haven't found a place where you update these bits.

Quoted 20 lines
Quoted 10 lines
Quoted 10 lines
target-openrisc/translate.c +10 -0
+static void dec_float(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t ra, rb, rd;
+    op0 = field(insn, 0, 8);
+    ra = field(insn, 16, 5);
+    rb = field(insn, 11, 5);
+    rd = field(insn, 21, 5);
+
+    switch (op0) {
Quoted 9 lines
Quoted 3 lines

+    case 0x10:    /*lf.add.d*/
+        LOG_DIS("lf.add.d r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_add_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);

Through this function you generate integer operations on the registers, although ISA
suggests that there should be either single- or double-precision floating point operations.

Quoted 2 lines

Sorry, I  didn't find a TCG-IR that make single- or double-precision floating point operations, may you give me some hits?

As chenwj said it is usually done in helper functions. Software floating point structures, prototypes and implementations live in the fpu/ directory.

Good example of how to use them is MIPS:
target-mips/translate.c, function gen_farith and
target-mips/op_helper.c, macro FLOAT_BINOP

Signature · 2 lines
Thanks.
-- Max
#22 lore raw

Re: [Qemu-devel] [PATCH 03/15] Openrisc: add instructions translation

In reply to Jia Liu
Quoted 104 lines · Jia Liu

add the openrisc instructions translation.

---
 target-openrisc/translate.c | 1387 +++++++++++++++++++++++++++++++++++++++++++

 1 file changed, 1387 insertions(+)
target-openrisc/translate.c +87 -0
index aae483a..4828ae6 100644
@@ -55,14 +55,1395 @@ typedef struct DisasContext {
     uint32_t delayed_branch;
  } DisasContext;
 
+static TCGv_ptr cpu_env;
+static TCGv cpu_sr;
+static TCGv cpu_R[32];
+static TCGv cpu_pc;
+static TCGv jmp_pc;        /* l.jr/l.jalr temp pc */
+static TCGv cpu_npc;
+static TCGv cpu_ppc;
+static TCGv_i32 env_btaken;    /* bf/bnf , F flag taken */
+static TCGv machi, maclo;
+static TCGv fpmaddhi, fpmaddlo;
+static TCGv_i32 env_flags;
+#include "gen-icount.h"
+
  void openrisc_translate_init(void)
  {
+    static const char * const regnames[] = {
+        "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
+        "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
+        "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
+        "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
+    };
+    int i;
+
+    cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
+    cpu_sr = tcg_global_mem_new(TCG_AREG0,
+                                offsetof(CPUOPENRISCState, sr), "sr");
+    env_flags = tcg_global_mem_new_i32(TCG_AREG0,
+                                       offsetof(CPUOPENRISCState, flags),
+                                       "flags");
+    cpu_pc = tcg_global_mem_new(TCG_AREG0,
+                                offsetof(CPUOPENRISCState, pc), "pc");
+    cpu_npc = tcg_global_mem_new(TCG_AREG0,
+                                 offsetof(CPUOPENRISCState, npc), "npc");
+    cpu_ppc = tcg_global_mem_new(TCG_AREG0,
+                                 offsetof(CPUOPENRISCState, ppc), "ppc");
+    jmp_pc = tcg_global_mem_new(TCG_AREG0,
+                                offsetof(CPUOPENRISCState, jmp_pc), "jmp_pc");
+    env_btaken = tcg_global_mem_new_i32(TCG_AREG0,
+                                        offsetof(CPUOPENRISCState, btaken),
+                                        "btaken");
+    machi = tcg_global_mem_new(TCG_AREG0,
+                               offsetof(CPUOPENRISCState, machi),
+                               "machi");
+    maclo = tcg_global_mem_new(TCG_AREG0,
+                               offsetof(CPUOPENRISCState, maclo),
+                               "maclo");
+    fpmaddhi = tcg_global_mem_new(TCG_AREG0,
+                                  offsetof(CPUOPENRISCState, fpmaddhi),
+                                  "fpmaddhi");
+    fpmaddlo = tcg_global_mem_new(TCG_AREG0,
+                                  offsetof(CPUOPENRISCState, fpmaddlo),
+                                  "fpmaddlo");
+    for (i = 0; i < 32; i++) {
+        cpu_R[i] = tcg_global_mem_new(TCG_AREG0,
+                                      offsetof(CPUOPENRISCState, gpr[i]),
+                                      regnames[i]);
+    }
+#define GEN_HELPER 2
+#include "helper.h"
+}
+
+/* Writeback SR_F transaltion-space to execution-space.  */
+static inline void wb_SR_F(void)
+{
+    int label;
+
+    label = gen_new_label();
+    tcg_gen_andi_tl(cpu_sr, cpu_sr, ~SR_F);
+    tcg_gen_brcondi_tl(TCG_COND_EQ, env_btaken, 0, label);
+    tcg_gen_ori_tl(cpu_sr, cpu_sr, SR_F);
+    gen_set_label(label);
+}
+
+static inline int zero_extend(unsigned int val, int width)
+{
+    return val & ((1 << width) - 1);
+}
+
+static inline int sign_extend(unsigned int val, int width)
+{
+    int sval;
+
+    /* LSL.  */
+    val <<= 32 - width;
+    sval = val;
+    /* ASR.  */
+    sval >>= 32 - width;
+    return sval;
+}

target-openrisc/translate.c typedef struct DisasContext { · line 156

Quoted 10 lines
target-openrisc/translate.c +10 -0
+
+/* General purpose registers moves. */
+static inline void gen_load_gpr(TCGv t, unsigned int reg)
+{
+    if (reg == 0) {
+        tcg_gen_movi_tl(t, 0);
+    } else {
+        tcg_gen_mov_tl(t, cpu_R[reg]);
+    }
+}

This does not seem to be used. Does the code handle the cases where one or several of the registers is number 0? If this is the always-0 register, are stores to register 0 handled correctly?

Quoted 15 lines
target-openrisc/translate.c +15 -0
+
+static void gen_goto_tb(DisasContext *dc, int n, target_ulong dest)
+{
+    TranslationBlock *tb;
+    tb = dc->tb;
+    if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) &&
+                                       likely(!dc->singlestep_enabled)) {
+        tcg_gen_movi_tl(cpu_pc, dest);
+        tcg_gen_goto_tb(n);
+        tcg_gen_exit_tb((tcg_target_long)tb + n);
+    } else {
+        tcg_gen_movi_tl(cpu_pc, dest);
+        if (dc->singlestep_enabled) {
+            /* exception here */
+        }

target-openrisc/translate.c typedef struct DisasContext { · line 181

Quoted 10 lines
target-openrisc/translate.c +10 -0
+        tcg_gen_exit_tb(0);
+    }
+}
+
+static inline uint32_t field(uint32_t val, int start, int length)
+{
+    val >>= start;
+    val &= ~(~0 << length);
+    return val;
+}

Nice function, maybe I could use it in Sparc as well if generalized (later).

Quoted 32 lines
target-openrisc/translate.c +32 -0
+
+static void dec_calc(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0, op1, op2;
+    uint32_t ra, rb, rd;
+    op0 = field(insn, 0, 4);
+    op1 = field(insn, 8, 2);
+    op2 = field(insn, 6, 2);
+    ra = field(insn, 16, 5);
+    rb = field(insn, 11, 5);
+    rd = field(insn, 21, 5);
+
+    switch (op0) {
+    case 0x0000:
+        switch (op1) {
+        case 0x00:     /*l.add*/
+            LOG_DIS("l.add r%d, r%d, r%d\n", rd, ra, rb);
+            /* Aha, we do need a helper here for add */
+            break;
+        default:
+            break;
+        }
+    break;
+
+    case 0x0001:    /*l.addc*/
+        switch (op1) {
+        case 0x00:
+            LOG_DIS("l.addc r%d, r%d, r%d\n", rd, ra, rb);
+            /* Aha, we do need a helper here for addc */
+            break;
+        default:
+            break;

target-openrisc/translate.c typedef struct DisasContext { · line 223

Quoted 10 lines
target-openrisc/translate.c +10 -0
+        }
+    break;
+
+    case 0x0002:    /*l.sub*/
+        switch (op1) {
+        case 0x00:
+            LOG_DIS("l.sub r%d, r%d, r%d\n", rd, ra, rb);
+            /* Aha, we do need a helper here for sub */
+            break;
+        default:

What happens on a real CPU when an illegal instruction is executed, shouldn't there be an exception?

Quoted 38 lines
target-openrisc/translate.c +38 -0
+            break;
+        }
+    break;
+
+    case 0x0003:   /*l.and*/
+        switch (op1) {
+        case 0x00:
+            LOG_DIS("l.and r%d, r%d, r%d\n", rd, ra, rb);
+            tcg_gen_and_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            break;
+        default:
+            break;
+        }
+    break;
+
+    case 0x0004:   /*l.or*/
+        switch (op1) {
+        case 0x00:
+            LOG_DIS("l.or r%d, r%d, r%d\n", rd, ra, rb);
+            tcg_gen_or_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            break;
+        default:
+            break;
+        }
+    break;
+
+    case 0x0005:
+        switch (op1) {
+        case 0x00:   /*l.xor*/
+            LOG_DIS("l.xor r%d, r%d, r%d\n", rd, ra, rb);
+            tcg_gen_xor_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            break;
+        default:
+            break;
+        }
+    break;
+
+    case 0x0006:

target-openrisc/translate.c typedef struct DisasContext { · line 271

Quoted 10 lines
target-openrisc/translate.c +10 -0
+        switch (op1) {
+        case 0x03:   /*l.mul*/
+            LOG_DIS("l.mul r%d, r%d, r%d\n", rd, ra, rb);
+            if (ra != 0 && rb != 0) {
+                tcg_gen_mul_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+                tcg_gen_ext32s_tl(cpu_R[rd], cpu_R[rd]);
+            } else {
+                tcg_gen_movi_tl(cpu_R[rd], 0x0);
+            }
+        break;

Indentation.

Quoted 275 lines
target-openrisc/translate.c +275 -0
+        default:
+            break;
+        }
+    break;
+
+    case 0x0009:
+        switch (op1) {
+        case 0x03:   /*l.div*/
+            LOG_DIS("l.div r%d, r%d, r%d\n", rd, ra, rb);
+            if (rb != 0) {
+                tcg_gen_div_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            } else {
+                /* exception here */
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000a:
+        switch (op1) {
+        case 0x03:   /*l.divu*/
+            LOG_DIS("l.divu r%d, r%d, r%d\n", rd, ra, rb);
+            if (rb != 0) {
+                tcg_gen_divu_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            } else {
+                /* exception here */
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000b:
+        switch (op1) {
+        case 0x03:   /*l.mulu*/
+            LOG_DIS("l.mulu r%d, r%d, r%d\n", rd, ra, rb);
+            if (rb != 0 && ra != 0) {
+                tcg_gen_ext32u_tl(cpu_R[ra], cpu_R[ra]);
+                tcg_gen_ext32u_tl(cpu_R[rb], cpu_R[rb]);
+                tcg_gen_mul_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            } else {
+                tcg_gen_movi_tl(cpu_R[rd], 0);
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000e:
+        switch (op1) {
+        case 0x00:   /*l.cmov*/
+            LOG_DIS("l.cmov r%d, r%d, r%d\n", rd, ra, rb);
+            {
+                int lab = gen_new_label();
+                TCGv sr_f = tcg_temp_new();
+                tcg_gen_andi_tl(sr_f, cpu_sr, SR_F);
+                tcg_gen_mov_tl(cpu_R[rd], cpu_R[rb]);
+                tcg_gen_brcondi_tl(TCG_COND_NE, sr_f, SR_F, lab);
+                tcg_gen_mov_tl(cpu_R[rd], cpu_R[ra]);
+                gen_set_label(lab);
+                tcg_temp_free(sr_f);
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000f:
+        switch (op1) {
+        case 0x00:   /*l.ff1*/
+            LOG_DIS("l.ff1 r%d, r%d, r%d\n", rd, ra, rb);
+            /* ff1 need a helper here */
+            break;
+        case 0x01:   /*l.fl1*/
+            LOG_DIS("l.fl1 r%d, r%d, r%d\n", rd, ra, rb);
+            /* fl1 need a helper here */
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x0008:
+        switch (op1) {
+        case 0x00:
+            switch (op2) {
+            case 0x00:   /*l.sll*/
+                LOG_DIS("l.sll r%d, r%d, r%d\n", rd, ra, rb);
+                tcg_gen_shl_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+                break;
+            case 0x01:   /*l.srl*/
+                LOG_DIS("l.srl r%d, r%d, r%d\n", rd, ra, rb);
+                tcg_gen_shr_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+                break;
+            case 0x02:   /*l.sra*/
+                LOG_DIS("l.sra r%d, r%d, r%d\n", rd, ra, rb);
+                tcg_gen_sar_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+                break;
+            case 0x03:   /*l.ror*/
+                LOG_DIS("l.ror r%d, r%d, r%d\n", rd, ra, rb);
+                tcg_gen_rotr_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+                break;
+            default:
+                break;
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000c:
+        switch (op1) {
+        case 0x00:
+            switch (op2) {
+            case 0x00:   /*l.exths*/
+                LOG_DIS("l.exths r%d, r%d\n", rd, ra);
+                tcg_gen_ext16s_tl(cpu_R[rd], cpu_R[ra]);
+                break;
+            case 0x01:   /*l.extbs*/
+                LOG_DIS("l.extbs r%d, r%d\n", rd, ra);
+                tcg_gen_ext8s_tl(cpu_R[rd], cpu_R[ra]);
+                break;
+            case 0x02:   /*l.exthz*/
+                LOG_DIS("l.exthz r%d, r%d\n", rd, ra);
+                tcg_gen_ext16u_tl(cpu_R[rd], cpu_R[ra]);
+                break;
+            case 0x03:   /*l.extbz*/
+                LOG_DIS("l.extbz r%d, r%d\n", rd, ra);
+                tcg_gen_ext8u_tl(cpu_R[rd], cpu_R[ra]);
+                break;
+            default:
+                break;
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000d:
+        switch (op1) {
+        case 0x00:
+            switch (op2) {
+            case 0x00:   /*l.extws*/
+                LOG_DIS("l.extws r%d, r%d\n", rd, ra);
+                tcg_gen_ext32s_tl(cpu_R[rd], cpu_R[ra]);
+                break;
+            case 0x01:    /*l.extwz*/
+                LOG_DIS("l.extwz r%d, r%d\n", rd, ra);
+                tcg_gen_ext32u_tl(cpu_R[rd], cpu_R[ra]);
+                break;
+            default:
+                break;
+            }
+        default:
+            break;
+        }
+        break;
+
+    default:
+        break;
+    }
+}
+
+static void dec_misc(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0, op1;
+    uint32_t ra, rb, rd;
+    uint32_t /*L6, K5, */I16, I5, I11, N26, tmp;
+    op0 = field(insn, 26, 6);
+    op1 = field(insn, 24, 2);
+    ra = field(insn, 16, 5);
+    rb = field(insn, 11, 5);
+    rd = field(insn, 21, 5);
+    /*L6 = field(insn, 5, 6);
+    K5 = field(insn, 0, 5);*/
+    I16 = field(insn, 0, 16);
+    I5 = field(insn, 21, 5);
+    I11 = field(insn, 0, 11);
+    N26 = field(insn, 0, 26);
+    tmp = (I5<<11) + I11;
+    TCGv t0 = tcg_temp_new();
+
+    switch (op0) {
+    case 0x00:   /*l.j*/
+        {
+            LOG_DIS("l.j %d\n", N26);
+            tmp = sign_extend((N26<<2), 26) + dc->pc;
+            tcg_gen_movi_tl(jmp_pc, tmp);
+            dc->delayed_branch = 2;
+            dc->tb_flags |= D_FLAG;
+            tcg_gen_movi_i32(env_flags, dc->tb_flags);
+        }
+        break;
+
+    case 0x01:   /*l.jal*/
+        {
+            LOG_DIS("l.jal %d\n", N26);
+            tcg_gen_movi_tl(cpu_R[9], (dc->pc + 8));
+            tmp = sign_extend((N26<<2), 26) + dc->pc;
+            tcg_gen_movi_tl(jmp_pc, tmp);
+            dc->delayed_branch = 2;
+            dc->tb_flags |= D_FLAG;
+            tcg_gen_movi_i32(env_flags, dc->tb_flags);
+        }
+        break;
+
+    case 0x03:   /*l.bnf*/
+        LOG_DIS("l.bnf %d\n", N26);
+        {
+            int lab = gen_new_label();
+            TCGv sr_f = tcg_temp_new();
+            tcg_gen_andi_tl(sr_f, cpu_sr, SR_F);
+            tmp = sign_extend((N26 << 2), 26) + dc->pc;
+            tcg_gen_movi_tl(jmp_pc, dc->pc+8);
+            tcg_gen_brcondi_tl(TCG_COND_EQ, sr_f, SR_F, lab);
+            tcg_gen_movi_tl(jmp_pc, tmp);
+            gen_set_label(lab);
+            dc->delayed_branch = 2;
+            dc->tb_flags |= D_FLAG;
+            tcg_gen_movi_i32(env_flags, dc->tb_flags);
+            tcg_temp_free(sr_f);
+        }
+        break;
+
+    case 0x04:   /*l.bf*/
+        LOG_DIS("l.bf %d\n", N26);
+        {
+            int lab = gen_new_label();
+            TCGv sr_f = tcg_temp_new();
+            tcg_gen_andi_tl(sr_f, cpu_sr, SR_F);
+            tmp = sign_extend((N26 << 2), 26) + dc->pc;
+            tcg_gen_movi_tl(jmp_pc, dc->pc+8);
+            tcg_gen_brcondi_tl(TCG_COND_NE, sr_f, SR_F, lab);
+            tcg_gen_movi_tl(jmp_pc, tmp);
+            gen_set_label(lab);
+            dc->delayed_branch = 2;
+            dc->tb_flags |= D_FLAG;
+            tcg_gen_movi_i32(env_flags, dc->tb_flags);
+            tcg_temp_free(sr_f);
+        }
+        break;
+
+    case 0x05:
+        switch (op1) {
+        case 0x01:   /*l.nop*/
+            LOG_DIS("l.nop %d\n", I16);
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x11:    /*l.jr*/
+        LOG_DIS("l.jr r%d\n", rb);
+        {
+            tcg_gen_mov_tl(jmp_pc, cpu_R[rb]);
+            dc->delayed_branch = 2;
+            dc->tb_flags |= D_FLAG;
+            tcg_gen_movi_i32(env_flags, dc->tb_flags);
+        }
+        break;
+
+    case 0x12:    /*l.jalr*/
+        LOG_DIS("l.jalr r%d\n", rb);
+        {
+            tcg_gen_movi_tl(cpu_R[9], (dc->pc + 8));
+            gen_load_gpr(jmp_pc, rb);
+            dc->delayed_branch = 2;

target-openrisc/translate.c typedef struct DisasContext { · line 556

Quoted 10 lines
target-openrisc/translate.c +10 -0
+            dc->tb_flags |= D_FLAG;
+            tcg_gen_movi_i32(env_flags, dc->tb_flags);
+        }
+        break;
+
+    case 0x13:    /*l.maci*/
+        {
+            LOG_DIS("l.maci %d, r%d, %d\n", I5, ra, I11);
+            TCGv_i64 tra = tcg_temp_new_i64();    /*  store cpu_R[ra]*/
+            TCGv_i64 tmac = tcg_temp_new_i64();   /*  store machi maclo*/

Variable definitions should be before code, like LOG_DIS() call in this case.

Quoted 807 lines
target-openrisc/translate.c +801 -0
+            tcg_gen_movi_tl(t0, tmp);
+            tcg_gen_ext32s_i64(tra, cpu_R[ra]);
+            tcg_gen_ext32s_i64(tmac, t0);
+            tcg_gen_mul_tl(tmac, tra, tmac);
+            tcg_gen_trunc_i64_i32(cpu_R[ra], tmac);
+            tcg_gen_add_i64(machi, machi, cpu_R[ra]);
+            tcg_gen_add_i64(maclo, maclo, cpu_R[ra]);
+            tcg_temp_free(tra);
+            tcg_temp_free(tmac);
+        }
+        break;
+
+    case 0x09:    /*l.rfe*/
+        LOG_DIS("l.rfe\n");
+        {
+            /* rfe need a helper here */
+            dc->is_jmp = DISAS_UPDATE;
+        }
+        break;
+
+    case 0x1c:    /*l.cust1*/
+        LOG_DIS("l.cust1\n");
+        break;
+
+    case 0x1d:    /*l.cust2*/
+        LOG_DIS("l.cust2\n");
+        break;
+
+    case 0x1e:    /*l.cust3*/
+        LOG_DIS("l.cust3\n");
+        break;
+
+    case 0x1f:    /*l.cust4*/
+        LOG_DIS("l.cust4\n");
+        break;
+
+    case 0x3c:   /*l.cust5*/
+        /*LOG_DIS("l.cust5 r%d, r%d, r%d, %d, %d\n", rd, ra, rb, L6, K5);*/
+        break;
+
+    case 0x3d:   /*l.cust6*/
+        LOG_DIS("l.cust6\n");
+        break;
+
+    case 0x3e:   /*l.cust7*/
+        LOG_DIS("l.cust7\n");
+        break;
+
+    case 0x3f:   /*l.cust8*/
+        LOG_DIS("l.cust8\n");
+        break;
+
+    case 0x20:   /*l.ld*/
+        LOG_DIS("l.ld r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld64(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x21:   /*l.lwz*/
+        LOG_DIS("l.lwz r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld32u(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x22:   /*l.lws*/
+        LOG_DIS("l.lws r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld32s(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x23:   /*l.lbz*/
+        LOG_DIS("l.lbz r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld8u(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x24:   /*l.lbs*/
+        LOG_DIS("l.lbs r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld8s(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x25:   /*l.lhz*/
+        LOG_DIS("l.lhz r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld16u(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x26:   /*l.lhs*/
+        LOG_DIS("l.lhs r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld16s(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x27:   /*l.addi*/
+        LOG_DIS("l.addi r%d, r%d, %d\n", rd, ra, I16);
+        {
+            TCGv t0 = tcg_const_tl(sign_extend(I16, 16));
+            /* Aha, we do need a helper here for addi */
+            tcg_temp_free(t0);
+        }
+        break;
+
+    case 0x28:   /*l.addic*/
+        LOG_DIS("l.addic r%d, r%d, %d\n", rd, ra, I16);
+        {
+            TCGv t0 = tcg_const_tl(sign_extend(I16, 16));
+            /* Aha, we do need a helper here for addic */
+            tcg_temp_free(t0);
+        }
+        break;
+
+    case 0x29:   /*l.andi*/
+        LOG_DIS("l.andi r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_andi_tl(cpu_R[rd], cpu_R[ra], zero_extend(I16, 16));
+        break;
+
+    case 0x2a:   /*l.ori*/
+        LOG_DIS("l.ori r%d, r%d, %d\n", rd, ra, zero_extend(I16, 16));
+        tcg_gen_ori_tl(cpu_R[rd], cpu_R[ra], I16);
+        break;
+
+    case 0x2b:   /*l.xori*/
+        LOG_DIS("l.xori r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_xori_tl(cpu_R[rd], cpu_R[ra], sign_extend(I16, 16));
+        break;
+
+    case 0x2c:   /*l.muli*/
+        LOG_DIS("l.muli r%d, r%d, %d\n", rd, ra, I16);
+        if (ra != 0 && I16 != 0) {
+            tcg_gen_muli_tl(cpu_R[rd], cpu_R[ra], I16);
+            tcg_gen_ext32s_tl(cpu_R[rd], cpu_R[rd]);
+        } else {
+            tcg_gen_movi_tl(cpu_R[rd], 0);
+        }
+        break;
+
+    case 0x2d:   /*l.mfspr*/
+        LOG_DIS("l.mfspr r%d, r%d, %d\n", rd, ra, I16);
+        {
+            TCGv_i32 ti = tcg_const_i32(I16);
+            TCGv td = tcg_const_tl(rd);
+            TCGv ta = tcg_const_tl(ra);
+            /* mfspr need a helper here */
+            tcg_temp_free_i32(ti);
+            tcg_temp_free(td);
+            tcg_temp_free(ta);
+        }
+        break;
+
+    case 0x30:  /*l.mtspr*/
+        LOG_DIS("l.mtspr %d, r%d, r%d, %d\n", I5, ra, rb, I11);
+        {
+            TCGv_i32 ti = tcg_const_i32(tmp);
+            TCGv ta = tcg_const_tl(ra);
+            TCGv tb = tcg_const_tl(rb);
+            /* mtspr need a helper here */
+            tcg_temp_free_i32(ti);
+            tcg_temp_free(ta);
+            tcg_temp_free(tb);
+        }
+        break;
+
+    case 0x34:   /*l.sd*/
+        LOG_DIS("l.sd %d, r%d, r%d, %d\n", I5, ra, rb, I11);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(tmp, 16));
+        tcg_gen_qemu_st64(cpu_R[rb], t0, dc->mem_idx);
+        break;
+
+    case 0x35:   /*l.sw*/
+        LOG_DIS("l.sw %d, r%d, r%d, %d\n", I5, ra, rb, I11);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(tmp, 16));
+        tcg_gen_qemu_st32(cpu_R[rb], t0, dc->mem_idx);
+        break;
+
+    case 0x36:   /*l.sb*/
+        LOG_DIS("l.sb %d, r%d, r%d, %d\n", I5, ra, rb, I11);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(tmp, 16));
+        tcg_gen_qemu_st8(cpu_R[rb], t0, dc->mem_idx);
+        break;
+
+    case 0x37:   /*l.sh*/
+        LOG_DIS("l.sh %d, r%d, r%d, %d\n", I5, ra, rb, I11);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(tmp, 16));
+        tcg_gen_qemu_st16(cpu_R[rb], t0, dc->mem_idx);
+        break;
+    default:
+        break;
+    }
+    tcg_temp_free(t0);
+}
+
+static void dec_mac(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t ra, rb;
+    op0 = field(insn, 0, 4);
+    ra = field(insn, 16, 5);
+    rb = field(insn, 11, 5);
+    TCGv_i64 t0 = tcg_temp_new();
+    TCGv_i64 t1 = tcg_temp_new();
+    switch (op0) {
+    case 0x0001:   /*l.mac*/
+        {
+            LOG_DIS("l.mac r%d, r%d\n", ra, rb);
+            tcg_gen_ext_i32_i64(t0, cpu_R[ra]);
+            tcg_gen_ext_i32_i64(t1, cpu_R[rb]);
+            tcg_gen_mul_i64(t0, t0, t1);
+            tcg_gen_trunc_i64_i32(cpu_R[ra], t0);
+            tcg_gen_add_tl(maclo, maclo, cpu_R[ra]);
+            tcg_gen_add_tl(machi, machi, cpu_R[ra]);
+            tcg_temp_free(t0);
+            tcg_temp_free(t1);
+        }
+        break;
+
+    case 0x0002:   /*l.msb*/
+        {
+            LOG_DIS("l.msb r%d, r%d\n", ra, rb);
+            tcg_gen_ext_i32_i64(t0, cpu_R[ra]);
+            tcg_gen_ext_i32_i64(t1, cpu_R[rb]);
+            tcg_gen_mul_i64(t0, t0, t1);
+            tcg_gen_trunc_i64_i32(cpu_R[ra], t0);
+            tcg_gen_sub_tl(maclo, maclo, cpu_R[ra]);
+            tcg_gen_sub_tl(machi, machi, cpu_R[ra]);
+            tcg_temp_free(t0);
+            tcg_temp_free(t1);
+        }
+        break;
+
+    default:
+        break;
+   }
+}
+
+static void dec_logic(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t rd, ra, L6;
+    op0 = field(insn, 6, 2);
+    rd = field(insn, 21, 5);
+    ra = field(insn, 16, 5);
+    L6 = field(insn, 0, 6);
+
+    switch (op0) {
+    case 0x00:    /*l.slli*/
+        LOG_DIS("l.slli r%d, r%d, %d\n", rd, ra, L6);
+        tcg_gen_shli_tl(cpu_R[rd], cpu_R[ra], (L6 & 0x1f));
+        break;
+
+    case 0x01:    /*l.srli*/
+        LOG_DIS("l.srli r%d, r%d, %d\n", rd, ra, L6);
+        tcg_gen_shri_tl(cpu_R[rd], cpu_R[ra], (L6 & 0x1f));
+        break;
+
+    case 0x02:    /*l.srai*/
+        LOG_DIS("l.srai r%d, r%d, %d\n", rd, ra, L6);
+        tcg_gen_sari_tl(cpu_R[rd], cpu_R[ra], (L6 & 0x1f)); break;
+
+    case 0x03:    /*l.rori*/
+        LOG_DIS("l.rori r%d, r%d, %d\n", rd, ra, L6);
+        tcg_gen_rotri_tl(cpu_R[rd], cpu_R[ra], (L6 & 0x1f));
+        break;
+
+    default:
+        break;
+    }
+}
+
+static void dec_M(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t rd;
+    uint32_t K16;
+    op0 = field(insn, 16, 1);
+    rd = field(insn, 21, 5);
+    K16 = field(insn, 0, 16);
+
+    switch (op0) {
+    case 0x0:    /*l.movhi*/
+        LOG_DIS("l.movhi  r%d, %d\n", rd, K16);
+        tcg_gen_movi_tl(cpu_R[rd], (K16 << 16));
+        break;
+
+    case 0x1:    /*l.macrc*/
+        LOG_DIS("l.macrc  r%d\n", rd);
+        tcg_gen_mov_tl(cpu_R[rd], maclo);
+        tcg_gen_movi_tl(maclo, 0x0);
+        tcg_gen_movi_tl(machi, 0x0);
+        break;
+
+    default:
+        break;
+    }
+}
+
+static void dec_comp(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t ra, rb;
+
+    op0 = field(insn, 21, 5);
+    ra = field(insn, 16, 5);
+    rb = field(insn, 11, 5);
+
+    tcg_gen_movi_i32(env_btaken, 0x0);
+    /* unsigned integers  */
+    tcg_gen_ext32u_tl(cpu_R[ra], cpu_R[ra]);
+    tcg_gen_ext32u_tl(cpu_R[rb], cpu_R[rb]);
+
+    switch (op0) {
+    case 0x0:    /*l.sfeq*/
+        LOG_DIS("l.sfeq  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_EQ, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x1:    /*l.sfne*/
+        LOG_DIS("l.sfne  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_NE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x2:    /*l.sfgtu*/
+        LOG_DIS("l.sfgtu  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_GTU, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x3:    /*l.sfgeu*/
+        LOG_DIS("l.sfgeu  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_GEU, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x4:    /*l.sfltu*/
+        LOG_DIS("l.sfltu  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_LTU, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x5:    /*l.sfleu*/
+        LOG_DIS("l.sfleu  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_LEU, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0xa:    /*l.sfgts*/
+        LOG_DIS("l.sfgts  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_GT, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0xb:    /*l.sfges*/
+        LOG_DIS("l.sfges  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_GE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0xc:    /*l.sflts*/
+        LOG_DIS("l.sflts  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_LT, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0xd:    /*l.sfles*/
+        LOG_DIS("l.sfles  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_LE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    default:
+        break;
+    }
+    wb_SR_F();
+}
+
+static void dec_compi(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t ra, I16;
+
+    op0 = field(insn, 21, 5);
+    ra = field(insn, 16, 5);
+    I16 = field(insn, 0, 16);
+
+    tcg_gen_movi_i32(env_btaken, 0x0);
+    tcg_gen_ext32u_tl(cpu_R[ra], cpu_R[ra]);
+    I16 = sign_extend(I16, 16);
+
+    switch (op0) {
+    case 0x0:    /*l.sfeqi*/
+        LOG_DIS("l.sfeqi  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_EQ, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0x1:    /*l.sfnei*/
+        LOG_DIS("l.sfnei  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_NE, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0x2:    /*l.sfgtui*/
+        LOG_DIS("l.sfgtui  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_GTU, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0x3:    /*l.sfgeui*/
+        LOG_DIS("l.sfgeui  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_GEU, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0x4:    /*l.sfltui*/
+        LOG_DIS("l.sfltui  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_LTU, env_btaken, cpu_R[ra],
+                            sign_extend(I16, 16));
+        break;
+
+    case 0x5:    /*l.sfleui*/
+        LOG_DIS("l.sfleui  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_LEU, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0xa:    /*l.sfgtsi*/
+        LOG_DIS("l.sfgtsi  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_GT, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0xb:    /*l.sfgesi*/
+        LOG_DIS("l.sfgesi  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_GE, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0xc:    /*l.sfltsi*/
+        LOG_DIS("l.sfltsi  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_LT, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0xd:    /*l.sflesi*/
+        LOG_DIS("l.sflesi  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_LE, env_btaken, cpu_R[ra], I16);
+        break;
+
+    default:
+        break;
+    }
+    wb_SR_F();
+}
+
+static void dec_sys(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    /*uint32_t K16;*/
+    op0 = field(insn, 16, 8);
+    /*K16 = field(insn, 0, 16);*/
+
+    switch (op0) {
+    case 0x000:  /*l.sys*/
+        /*LOG_DIS("l.sys %d\n", K16);*/
+        tcg_gen_movi_tl(cpu_pc, dc->pc);
+        /* exception here */
+        dc->is_jmp = DISAS_UPDATE;
+        break;
+
+    case 0x100:  /*l.trap*/
+        /*LOG_DIS("l.trap %d\n", K16);*/
+        tcg_gen_movi_tl(cpu_pc, dc->pc);
+        /* exception here */
+        break;
+
+    case 0x300:  /*l.csync*/
+        LOG_DIS("l.csync\n");
+        break;
+
+    case 0x200:  /*l.msync*/
+        LOG_DIS("l.msync\n");
+        break;
+
+    case 0x270:  /*l.psync*/
+        LOG_DIS("l.psync\n");
+        break;
+
+    default:
+        break;
+    }
+}
+
+static void dec_float(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t ra, rb, rd;
+    op0 = field(insn, 0, 8);
+    ra = field(insn, 16, 5);
+    rb = field(insn, 11, 5);
+    rd = field(insn, 21, 5);
+
+    switch (op0) {
+    case 0x10:    /*lf.add.d*/
+        LOG_DIS("lf.add.d r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_add_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x00:    /*lf.add.s*/
+        LOG_DIS("lf.add.s r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_add_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x11:    /*lf.sub.d*/
+        LOG_DIS("lf.sub.d r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_sub_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x01:    /*lf.sub.s*/
+        LOG_DIS("lf.sub.s r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_sub_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x12:    /*lf.mul.d*/
+        LOG_DIS("lf.mul.d r%d, r%d, r%d\n", rd, ra, rb);
+        {
+            if (cpu_R[ra] != 0 && cpu_R[rb] != 0) {
+                tcg_gen_mul_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            } else {
+                tcg_gen_movi_i64(cpu_R[rd], 0x0);
+            }
+        }
+        break;
+
+    case 0x02:    /*lf.mul.s*/
+        LOG_DIS("lf.mul.s r%d, r%d, r%d\n", rd, ra, rb);
+        if (cpu_R[ra] != 0 && cpu_R[rb] != 0) {
+            tcg_gen_mul_i32(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        } else {
+            tcg_gen_movi_i64(cpu_R[rd], 0x0);
+        }
+        break;
+
+    case 0x13:    /*lf.div.d*/
+        LOG_DIS("lf.div.d r%d, r%d, r%d\n", rd, ra, rb);
+        if (cpu_R[rb] != 0) {
+            tcg_gen_div_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        } else {
+            /* exception here */
+        }
+        break;
+
+    case 0x03:    /*lf.div.s*/
+        LOG_DIS("lf.div.s r%d, r%d, r%d\n", rd, ra, rb);
+        if (cpu_R[rb] != 0) {
+            tcg_gen_div_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        } else {
+            /* exception here */
+        }
+        break;
+
+    case 0x14:    /*lf.itof.d*/
+        LOG_DIS("lf.itof r%d, r%d\n", rd, ra);
+        /* itof.d need a helper here */
+        break;
+
+    case 0x04:    /*lf.itof.s*/
+        LOG_DIS("lf.itof r%d, r%d\n", rd, ra);
+        /* itof.s need a helper here */
+        break;
+
+    case 0x15:    /*lf.ftoi.d*/
+        LOG_DIS("lf.ftoi r%d, r%d\n", rd, ra);
+        /* ftoi.d need a helper here */
+        break;
+
+    case 0x05:    /*lf.ftoi.s*/
+        LOG_DIS("lf.ftoi r%d, r%d\n", rd, ra);
+        /* ftoi.s need a helper here */
+        break;
+
+    case 0x16:    /*lf.rem.d*/
+        LOG_DIS("lf.rem.d r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_rem_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x06:    /*lf.rem.s*/
+        LOG_DIS("lf.rem.s r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_rem_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x17:    /*lf.madd.d*/
+        LOG_DIS("lf.madd.d r%d, r%d, r%d\n", rd, ra, rb);
+        {
+            TCGv_i64 t0, t1;
+
+            t0 = tcg_temp_new_i64();
+            t1 = tcg_temp_new_i64();
+
+            tcg_gen_ext_i32_i64(t0, cpu_R[ra]);
+            tcg_gen_ext_i32_i64(t1, cpu_R[rb]);
+            tcg_gen_mul_i64(t0, t0, t1);
+
+            tcg_gen_trunc_i64_i32(cpu_R[ra], t0);
+            tcg_gen_add_tl(fpmaddlo, cpu_R[ra], fpmaddlo);
+            tcg_gen_shri_i64(t0, t0, 32);
+            tcg_gen_trunc_i64_i32(cpu_R[rb], t0);
+            tcg_gen_add_tl(fpmaddhi, cpu_R[rb], fpmaddhi);
+
+            tcg_temp_free_i64(t0);
+            tcg_temp_free_i64(t1);
+        }
+        break;
+
+    case 0x07:    /*lf.madd.s*/
+        LOG_DIS("lf.madd.s r%d, r%d, r%d\n", rd, ra, rb);
+        {
+            TCGv_i64 t0, t1;
+
+            t0 = tcg_temp_new_i64();
+            t1 = tcg_temp_new_i64();
+
+            tcg_gen_ext_i32_i64(t0, cpu_R[ra]);
+            tcg_gen_ext_i32_i64(t1, cpu_R[rb]);
+            tcg_gen_mul_i64(t0, t0, t1);
+
+            tcg_gen_trunc_i64_i32(cpu_R[ra], t0);
+            tcg_gen_add_tl(fpmaddlo, cpu_R[ra], fpmaddlo);
+            tcg_gen_shri_i64(t0, t0, 32);
+            tcg_gen_trunc_i64_i32(cpu_R[rb], t0);
+            tcg_gen_add_tl(fpmaddhi, cpu_R[rb], fpmaddhi);
+            tcg_temp_free_i64(t0);
+            tcg_temp_free_i64(t1);
+
+        }
+        break;
+
+    case 0x18:    /*lf.sfeq.d*/
+        LOG_DIS("lf.sfeq.d r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_i64(TCG_COND_GE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x08:    /*lf.sfeq.s*/
+        LOG_DIS("lf.sfeq.s r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_EQ, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x19:    /*lf.sfne.d*/
+        LOG_DIS("lf.sfne.d r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_i64(TCG_COND_NE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x09:    /*lf.sfne.s*/
+        LOG_DIS("lf.sfne.s r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_NE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x1a:    /*lf.sfgt.d*/
+        LOG_DIS("lf.sfgt.d r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_i64(TCG_COND_GT, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x0a:    /*lf.sfgt.s*/
+        LOG_DIS("lf.sfgt.s r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_GT, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x1b:    /*lf.sfge.d*/
+        LOG_DIS("lf.sfge.d r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_i64(TCG_COND_GE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x0b:    /*lf.sfge.s*/
+        LOG_DIS("lf.sfge.s r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_GE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x1c:    /*lf.sflt.d*/
+        LOG_DIS("lf.sflt.d r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_i64(TCG_COND_LT, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x0c:    /*lf.sflt.s*/
+        LOG_DIS("lf.sflt.s r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_LT, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x1d:    /*lf.sfle.d*/
+        LOG_DIS("lf.sfle.d r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_i64(TCG_COND_LE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x0d:    /*lf.sfle.s*/
+        LOG_DIS("lf.sfle.s r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_LE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    default:
+        break;
+    }
+    wb_SR_F();
+}
+
+static void disas_openrisc_insn(DisasContext *dc, CPUOPENRISCState *env)
+{
+    uint32_t op0;
+    uint32_t insn;
+    insn = ldl_code(dc->pc);
+    op0 = field(insn, 26, 6);
+
+    switch (op0) {
+    case 0x06:
+        dec_M(dc, env, insn);
+        break;
+
+    case 0x08:
+        dec_sys(dc, env, insn);
+        break;
+
+    case 0x2e:
+        dec_logic(dc, env, insn);
+        break;
+
+    case 0x2f:
+        dec_compi(dc, env, insn);
+        break;
+
+    case 0x31:
+        dec_mac(dc, env, insn);
+        break;
+
+    case 0x32:
+        dec_float(dc, env, insn);
+        break;
+
+    case 0x38:
+        dec_calc(dc, env, insn);
+        break;
+
+    case 0x39:
+        dec_comp(dc, env, insn);
+        break;
+
+    default:
+        dec_misc(dc, env, insn);
+        break;
+    }
+}
+
+static void check_breakpoint(CPUOPENRISCState *env, DisasContext *dc)
+{
+    CPUBreakpoint *bp;
+
+    if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
+        QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
+            if (bp->pc == dc->pc) {
+                tcg_gen_movi_tl(cpu_pc, dc->pc);
+                /* exception here */
+                dc->is_jmp = DISAS_UPDATE;
+            }
+        }
+    }
  }
 
  static inline void gen_intermediate_code_internal(CPUOPENRISCState *env,
                                                   TranslationBlock *tb,
                                                   int search_pc)
  {
+    struct DisasContext ctx, *dc = &ctx;
+    uint16_t *gen_opc_end;
+    uint32_t pc_start;
+    int j, lj;
+    uint32_t next_page_start;
+    int num_insns;
+    int max_insns;
+
+    qemu_log_try_set_file(stderr);
+
+    pc_start = tb->pc;
+    dc->env = env;
+    dc->tb = tb;
+
+    gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
+    dc->is_jmp = DISAS_NEXT;
+    dc->ppc = pc_start;
+    dc->pc = pc_start;
+    dc->mem_idx = cpu_mmu_index(env);
+    dc->tb_flags = tb->flags;
+    dc->delayed_branch = !!(dc->tb_flags & D_FLAG);
+    dc->singlestep_enabled = env->singlestep_enabled;
+    if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
+        qemu_log("-----------------------------------------\n");
+        log_cpu_state(env, 0);
+    }
+
+    next_page_start = (pc_start & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
+    lj = -1;
+    num_insns = 0;
+    max_insns = tb->cflags & CF_COUNT_MASK;
+
+    if (max_insns == 0) {
+        max_insns = CF_COUNT_MASK;
+    }
+
+    gen_icount_start();
+
+    do {
+        check_breakpoint(env, dc);
+        if (search_pc) {
+            j = gen_opc_ptr - gen_opc_buf;
+            if (lj < j) {
+                lj++;
+                while (lj < j) {
+                    gen_opc_instr_start[lj++] = 0;
+                }
+            }
+            gen_opc_pc[lj] = dc->pc;
+            gen_opc_instr_start[lj] = 1;
+            gen_opc_icount[lj] = num_insns;
+        }
+
+        if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP))) {
+            tcg_gen_debug_insn_start(dc->pc);
+        }

target-openrisc/translate.c typedef struct DisasContext { · line 1373

Quoted 10 lines
target-openrisc/translate.c +10 -0
+
+        if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO)) {
+            gen_io_start();
+        }
+        dc->ppc = dc->pc - 4;
+        dc->npc = dc->pc + 4;
+        tcg_gen_movi_tl(cpu_ppc, dc->ppc);
+        tcg_gen_movi_tl(cpu_npc, dc->npc);
+        disas_openrisc_insn(dc, env);
+        dc->pc = dc->npc;

Isn't now npc == pc and ppc == pc - 8?

target-openrisc/translate.c typedef struct DisasContext { · line 1380

Quoted 7 lines
target-openrisc/translate.c +7 -0
+        num_insns++;
+        /* delay slot */
+        if (dc->delayed_branch) {
+            dc->delayed_branch--;
+            if (!dc->delayed_branch) {
+                dc->tb_flags &= ~D_FLAG;
+                tcg_gen_movi_i32(env_flags, dc->tb_flags);

This does not look correct.

target-openrisc/translate.c typedef struct DisasContext { · line 1382

Quoted 2 lines
target-openrisc/translate.c +2 -0
+                tcg_gen_mov_tl(cpu_pc, jmp_pc);
+                tcg_gen_mov_tl(cpu_npc, jmp_pc);

+ 4?

target-openrisc/translate.c typedef struct DisasContext { · line 1387

Quoted 5 lines
target-openrisc/translate.c +5 -0
+                tcg_gen_movi_tl(jmp_pc, 0);
+                tcg_gen_exit_tb(0);
+                dc->is_jmp = DISAS_JUMP;
+                break;
+            }

Is it possible to have a jump instruction in the delay slot and if so, is it handled too?

Quoted 80 lines
target-openrisc/translate.c +65 -1
+        }
+    } while (!dc->is_jmp && gen_opc_ptr < gen_opc_end
+             && !env->singlestep_enabled
+             && !singlestep
+             && (dc->pc < next_page_start)
+             && num_insns < max_insns);
+
+    if (tb->cflags & CF_LAST_IO) {
+        gen_io_end();
+    }
+    if (dc->is_jmp == DISAS_NEXT) {
+        dc->is_jmp = DISAS_UPDATE;
+        tcg_gen_movi_tl(cpu_pc, dc->pc);
+    }
+    if (unlikely(env->singlestep_enabled)) {
+        if (dc->is_jmp == DISAS_NEXT) {
+            tcg_gen_movi_tl(cpu_pc, dc->pc);
+        }
+        /* exception here*/
+    } else {
+        switch (dc->is_jmp) {
+        case DISAS_NEXT:
+            gen_goto_tb(dc, 0, dc->pc);
+            break;
+        default:
+        case DISAS_JUMP:
+            break;
+        case DISAS_UPDATE:
+            /* indicate that the hash table must be used
+               to find the next TB */
+            tcg_gen_exit_tb(0);
+            break;
+        case DISAS_TB_JUMP:
+            /* nothing more to generate */
+            break;
+        }
+    }
+
+    gen_icount_end(tb, num_insns);
+    *gen_opc_ptr = INDEX_op_end;
+    if (search_pc) {
+        j = gen_opc_ptr - gen_opc_buf;
+        lj++;
+        while (lj <= j) {
+            gen_opc_instr_start[lj++] = 0;
+        }
+    } else {
+        tb->size = dc->pc - pc_start;
+        tb->icount = num_insns;
+    }
+
+#ifdef DEBUG_DISAS
+    if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
+        qemu_log("\n");
+        log_target_disas(pc_start, dc->pc - pc_start, 0);
+        qemu_log("\nisize=%d osize=%td\n",
+            dc->pc - pc_start, gen_opc_ptr - gen_opc_buf);
+    }
+#endif
  }
 
  void gen_intermediate_code(CPUOPENRISCState *env, struct TranslationBlock *tb)
@@ -80,7 +1461,13 @@ void cpu_dump_state(CPUOPENRISCState *env, FILE *f,
                     fprintf_function cpu_fprintf,
                     int flags)
  {
+    int i;
+    uint32_t *regs = env->gpr;
     cpu_fprintf(f, "PC=%08x\n", env->pc);
+    for (i = 0; i < 32; ++i) {
+        cpu_fprintf(f, "R%02d=%08x%c", i, regs[i],
+                    (i % 4) == 3 ? '\n' : ' ');
+    }
  }
 
  void restore_state_to_opc(CPUOPENRISCState *env, TranslationBlock *tb,
--

1.7.9.5

#23 lore raw

Re: [Qemu-devel] [PATCH 03/15] Openrisc: add instructions translation

Quoted 8 lines · Blue Swirl

target-openrisc/translate.c typedef struct DisasContext { · line 181

Quoted 6 lines
target-openrisc/translate.c +6 -0
+static inline uint32_t field(uint32_t val, int start, int length)
+{
+    val >>= start;
+    val &= ~(~0 << length);
+    return val;
+}

Nice function, maybe I could use it in Sparc as well if generalized (later).

We should totally have something like this in our generic bitops header file. I've previously worked on a codebase that used something similar consistently, and it's much easier to read than code which does inline mask-and-shift code.

We probably want a 64 bit variant (or maybe we can just work on uint64_t and trust the compiler to throw away the top halves in the 32 bit case?)

-- PMM

#24 lore raw

Re: [Qemu-devel] [PATCH 03/15] Openrisc: add instructions translation

Quoted 10 lines · Peter Maydell
Quoted 8 lines · Blue Swirl

target-openrisc/translate.c typedef struct DisasContext { · line 181

Quoted 6 lines
target-openrisc/translate.c +6 -0
+static inline uint32_t field(uint32_t val, int start, int length)
+{
+    val >>= start;
+    val &= ~(~0 << length);
+    return val;
+}

Nice function, maybe I could use it in Sparc as well if generalized (later).

Quoted 4 lines

We should totally have something like this in our generic bitops header file. I've previously worked on a codebase that used something similar consistently, and it's much easier to read than code which does inline mask-and-shift code.

Yes. A generic method to construct bit field #defines/enums with nice printout would be nice too. I think I saw something like that in BSD land, can't remember where.

Quoted 4 lines

We probably want a 64 bit variant (or maybe we can just work on uint64_t and trust the compiler to throw away the top halves in the 32 bit case?)

For Sparc the it would be used to extract fields from an instruction, they are always 32 bits. But IA64 would need a 64 bit version.

I'd make two variants, maybe also target_ulong version if ever needed.

Quoted 2 lines

-- PMM

author
#25 lore raw

Re: [Qemu-devel] [PATCH 03/15] Openrisc: add instructions translation

In reply to Blue Swirl

Thanks Blue.

Quoted 111 lines · Blue Swirl
Quoted 110 lines · Jia Liu

add the openrisc instructions translation.

---
 target-openrisc/translate.c | 1387 +++++++++++++++++++++++++++++++++++++++++++

 1 file changed, 1387 insertions(+)
target-openrisc/translate.c +93 -0
index aae483a..4828ae6 100644
@@ -55,14 +55,1395 @@ typedef struct DisasContext {
     uint32_t delayed_branch;
  } DisasContext;
 
+static TCGv_ptr cpu_env;
+static TCGv cpu_sr;
+static TCGv cpu_R[32];
+static TCGv cpu_pc;
+static TCGv jmp_pc;        /* l.jr/l.jalr temp pc */
+static TCGv cpu_npc;
+static TCGv cpu_ppc;
+static TCGv_i32 env_btaken;    /* bf/bnf , F flag taken */
+static TCGv machi, maclo;
+static TCGv fpmaddhi, fpmaddlo;
+static TCGv_i32 env_flags;
+#include "gen-icount.h"
+
  void openrisc_translate_init(void)
  {
+    static const char * const regnames[] = {
+        "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
+        "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
+        "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
+        "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
+    };
+    int i;
+
+    cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
+    cpu_sr = tcg_global_mem_new(TCG_AREG0,
+                                offsetof(CPUOPENRISCState, sr), "sr");
+    env_flags = tcg_global_mem_new_i32(TCG_AREG0,
+                                       offsetof(CPUOPENRISCState, flags),
+                                       "flags");
+    cpu_pc = tcg_global_mem_new(TCG_AREG0,
+                                offsetof(CPUOPENRISCState, pc), "pc");
+    cpu_npc = tcg_global_mem_new(TCG_AREG0,
+                                 offsetof(CPUOPENRISCState, npc), "npc");
+    cpu_ppc = tcg_global_mem_new(TCG_AREG0,
+                                 offsetof(CPUOPENRISCState, ppc), "ppc");
+    jmp_pc = tcg_global_mem_new(TCG_AREG0,
+                                offsetof(CPUOPENRISCState, jmp_pc), "jmp_pc");
+    env_btaken = tcg_global_mem_new_i32(TCG_AREG0,
+                                        offsetof(CPUOPENRISCState, btaken),
+                                        "btaken");
+    machi = tcg_global_mem_new(TCG_AREG0,
+                               offsetof(CPUOPENRISCState, machi),
+                               "machi");
+    maclo = tcg_global_mem_new(TCG_AREG0,
+                               offsetof(CPUOPENRISCState, maclo),
+                               "maclo");
+    fpmaddhi = tcg_global_mem_new(TCG_AREG0,
+                                  offsetof(CPUOPENRISCState, fpmaddhi),
+                                  "fpmaddhi");
+    fpmaddlo = tcg_global_mem_new(TCG_AREG0,
+                                  offsetof(CPUOPENRISCState, fpmaddlo),
+                                  "fpmaddlo");
+    for (i = 0; i < 32; i++) {
+        cpu_R[i] = tcg_global_mem_new(TCG_AREG0,
+                                      offsetof(CPUOPENRISCState, gpr[i]),
+                                      regnames[i]);
+    }
+#define GEN_HELPER 2
+#include "helper.h"
+}
+
+/* Writeback SR_F transaltion-space to execution-space.  */
+static inline void wb_SR_F(void)
+{
+    int label;
+
+    label = gen_new_label();
+    tcg_gen_andi_tl(cpu_sr, cpu_sr, ~SR_F);
+    tcg_gen_brcondi_tl(TCG_COND_EQ, env_btaken, 0, label);
+    tcg_gen_ori_tl(cpu_sr, cpu_sr, SR_F);
+    gen_set_label(label);
+}
+
+static inline int zero_extend(unsigned int val, int width)
+{
+    return val & ((1 << width) - 1);
+}
+
+static inline int sign_extend(unsigned int val, int width)
+{
+    int sval;
+
+    /* LSL.  */
+    val <<= 32 - width;
+    sval = val;
+    /* ASR.  */
+    sval >>= 32 - width;
+    return sval;
+}
+
+/* General purpose registers moves. */
+static inline void gen_load_gpr(TCGv t, unsigned int reg)
+{
+    if (reg == 0) {
+        tcg_gen_movi_tl(t, 0);
Quoted 9 lines

target-openrisc/translate.c typedef struct DisasContext { · line 156

Quoted 4 lines
target-openrisc/translate.c +4 -0
+    } else {
+        tcg_gen_mov_tl(t, cpu_R[reg]);
+    }
+}

This does not seem to be used. Does the code handle the cases where one or several of the registers is number 0? If this is the always-0 register, are stores to register 0 handled correctly?

I'll remove it.

Quoted 65 lines
Quoted 15 lines
target-openrisc/translate.c +15 -0
+
+static void gen_goto_tb(DisasContext *dc, int n, target_ulong dest)
+{
+    TranslationBlock *tb;
+    tb = dc->tb;
+    if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) &&
+                                       likely(!dc->singlestep_enabled)) {
+        tcg_gen_movi_tl(cpu_pc, dest);
+        tcg_gen_goto_tb(n);
+        tcg_gen_exit_tb((tcg_target_long)tb + n);
+    } else {
+        tcg_gen_movi_tl(cpu_pc, dest);
+        if (dc->singlestep_enabled) {
+            /* exception here */
+        }

target-openrisc/translate.c typedef struct DisasContext { · line 181

Quoted 10 lines
target-openrisc/translate.c +10 -0
+        tcg_gen_exit_tb(0);
+    }
+}
+
+static inline uint32_t field(uint32_t val, int start, int length)
+{
+    val >>= start;
+    val &= ~(~0 << length);
+    return val;
+}

Nice function, maybe I could use it in Sparc as well if generalized (later).

Quoted 37 lines
target-openrisc/translate.c +37 -0
+
+static void dec_calc(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0, op1, op2;
+    uint32_t ra, rb, rd;
+    op0 = field(insn, 0, 4);
+    op1 = field(insn, 8, 2);
+    op2 = field(insn, 6, 2);
+    ra = field(insn, 16, 5);
+    rb = field(insn, 11, 5);
+    rd = field(insn, 21, 5);
+
+    switch (op0) {
+    case 0x0000:
+        switch (op1) {
+        case 0x00:     /*l.add*/
+            LOG_DIS("l.add r%d, r%d, r%d\n", rd, ra, rb);
+            /* Aha, we do need a helper here for add */
+            break;
+        default:
+            break;
+        }
+    break;
+
+    case 0x0001:    /*l.addc*/
+        switch (op1) {
+        case 0x00:
+            LOG_DIS("l.addc r%d, r%d, r%d\n", rd, ra, rb);
+            /* Aha, we do need a helper here for addc */
+            break;
+        default:
+            break;
+        }
+    break;
+
+    case 0x0002:    /*l.sub*/
+        switch (op1) {
Quoted 9 lines

target-openrisc/translate.c typedef struct DisasContext { · line 223

Quoted 5 lines
target-openrisc/translate.c +5 -0
+        case 0x00:
+            LOG_DIS("l.sub r%d, r%d, r%d\n", rd, ra, rb);
+            /* Aha, we do need a helper here for sub */
+            break;
+        default:

What happens on a real CPU when an illegal instruction is executed, shouldn't there be an exception?

I'll add a exception for illegal instruction.

Quoted 330 lines
Quoted 38 lines
target-openrisc/translate.c +38 -0
+            break;
+        }
+    break;
+
+    case 0x0003:   /*l.and*/
+        switch (op1) {
+        case 0x00:
+            LOG_DIS("l.and r%d, r%d, r%d\n", rd, ra, rb);
+            tcg_gen_and_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            break;
+        default:
+            break;
+        }
+    break;
+
+    case 0x0004:   /*l.or*/
+        switch (op1) {
+        case 0x00:
+            LOG_DIS("l.or r%d, r%d, r%d\n", rd, ra, rb);
+            tcg_gen_or_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            break;
+        default:
+            break;
+        }
+    break;
+
+    case 0x0005:
+        switch (op1) {
+        case 0x00:   /*l.xor*/
+            LOG_DIS("l.xor r%d, r%d, r%d\n", rd, ra, rb);
+            tcg_gen_xor_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            break;
+        default:
+            break;
+        }
+    break;
+
+    case 0x0006:

target-openrisc/translate.c typedef struct DisasContext { · line 271

Quoted 10 lines
target-openrisc/translate.c +10 -0
+        switch (op1) {
+        case 0x03:   /*l.mul*/
+            LOG_DIS("l.mul r%d, r%d, r%d\n", rd, ra, rb);
+            if (ra != 0 && rb != 0) {
+                tcg_gen_mul_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+                tcg_gen_ext32s_tl(cpu_R[rd], cpu_R[rd]);
+            } else {
+                tcg_gen_movi_tl(cpu_R[rd], 0x0);
+            }
+        break;

Indentation.

Quoted 279 lines
target-openrisc/translate.c +279 -0
+        default:
+            break;
+        }
+    break;
+
+    case 0x0009:
+        switch (op1) {
+        case 0x03:   /*l.div*/
+            LOG_DIS("l.div r%d, r%d, r%d\n", rd, ra, rb);
+            if (rb != 0) {
+                tcg_gen_div_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            } else {
+                /* exception here */
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000a:
+        switch (op1) {
+        case 0x03:   /*l.divu*/
+            LOG_DIS("l.divu r%d, r%d, r%d\n", rd, ra, rb);
+            if (rb != 0) {
+                tcg_gen_divu_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            } else {
+                /* exception here */
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000b:
+        switch (op1) {
+        case 0x03:   /*l.mulu*/
+            LOG_DIS("l.mulu r%d, r%d, r%d\n", rd, ra, rb);
+            if (rb != 0 && ra != 0) {
+                tcg_gen_ext32u_tl(cpu_R[ra], cpu_R[ra]);
+                tcg_gen_ext32u_tl(cpu_R[rb], cpu_R[rb]);
+                tcg_gen_mul_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            } else {
+                tcg_gen_movi_tl(cpu_R[rd], 0);
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000e:
+        switch (op1) {
+        case 0x00:   /*l.cmov*/
+            LOG_DIS("l.cmov r%d, r%d, r%d\n", rd, ra, rb);
+            {
+                int lab = gen_new_label();
+                TCGv sr_f = tcg_temp_new();
+                tcg_gen_andi_tl(sr_f, cpu_sr, SR_F);
+                tcg_gen_mov_tl(cpu_R[rd], cpu_R[rb]);
+                tcg_gen_brcondi_tl(TCG_COND_NE, sr_f, SR_F, lab);
+                tcg_gen_mov_tl(cpu_R[rd], cpu_R[ra]);
+                gen_set_label(lab);
+                tcg_temp_free(sr_f);
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000f:
+        switch (op1) {
+        case 0x00:   /*l.ff1*/
+            LOG_DIS("l.ff1 r%d, r%d, r%d\n", rd, ra, rb);
+            /* ff1 need a helper here */
+            break;
+        case 0x01:   /*l.fl1*/
+            LOG_DIS("l.fl1 r%d, r%d, r%d\n", rd, ra, rb);
+            /* fl1 need a helper here */
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x0008:
+        switch (op1) {
+        case 0x00:
+            switch (op2) {
+            case 0x00:   /*l.sll*/
+                LOG_DIS("l.sll r%d, r%d, r%d\n", rd, ra, rb);
+                tcg_gen_shl_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+                break;
+            case 0x01:   /*l.srl*/
+                LOG_DIS("l.srl r%d, r%d, r%d\n", rd, ra, rb);
+                tcg_gen_shr_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+                break;
+            case 0x02:   /*l.sra*/
+                LOG_DIS("l.sra r%d, r%d, r%d\n", rd, ra, rb);
+                tcg_gen_sar_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+                break;
+            case 0x03:   /*l.ror*/
+                LOG_DIS("l.ror r%d, r%d, r%d\n", rd, ra, rb);
+                tcg_gen_rotr_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+                break;
+            default:
+                break;
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000c:
+        switch (op1) {
+        case 0x00:
+            switch (op2) {
+            case 0x00:   /*l.exths*/
+                LOG_DIS("l.exths r%d, r%d\n", rd, ra);
+                tcg_gen_ext16s_tl(cpu_R[rd], cpu_R[ra]);
+                break;
+            case 0x01:   /*l.extbs*/
+                LOG_DIS("l.extbs r%d, r%d\n", rd, ra);
+                tcg_gen_ext8s_tl(cpu_R[rd], cpu_R[ra]);
+                break;
+            case 0x02:   /*l.exthz*/
+                LOG_DIS("l.exthz r%d, r%d\n", rd, ra);
+                tcg_gen_ext16u_tl(cpu_R[rd], cpu_R[ra]);
+                break;
+            case 0x03:   /*l.extbz*/
+                LOG_DIS("l.extbz r%d, r%d\n", rd, ra);
+                tcg_gen_ext8u_tl(cpu_R[rd], cpu_R[ra]);
+                break;
+            default:
+                break;
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000d:
+        switch (op1) {
+        case 0x00:
+            switch (op2) {
+            case 0x00:   /*l.extws*/
+                LOG_DIS("l.extws r%d, r%d\n", rd, ra);
+                tcg_gen_ext32s_tl(cpu_R[rd], cpu_R[ra]);
+                break;
+            case 0x01:    /*l.extwz*/
+                LOG_DIS("l.extwz r%d, r%d\n", rd, ra);
+                tcg_gen_ext32u_tl(cpu_R[rd], cpu_R[ra]);
+                break;
+            default:
+                break;
+            }
+        default:
+            break;
+        }
+        break;
+
+    default:
+        break;
+    }
+}
+
+static void dec_misc(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0, op1;
+    uint32_t ra, rb, rd;
+    uint32_t /*L6, K5, */I16, I5, I11, N26, tmp;
+    op0 = field(insn, 26, 6);
+    op1 = field(insn, 24, 2);
+    ra = field(insn, 16, 5);
+    rb = field(insn, 11, 5);
+    rd = field(insn, 21, 5);
+    /*L6 = field(insn, 5, 6);
+    K5 = field(insn, 0, 5);*/
+    I16 = field(insn, 0, 16);
+    I5 = field(insn, 21, 5);
+    I11 = field(insn, 0, 11);
+    N26 = field(insn, 0, 26);
+    tmp = (I5<<11) + I11;
+    TCGv t0 = tcg_temp_new();
+
+    switch (op0) {
+    case 0x00:   /*l.j*/
+        {
+            LOG_DIS("l.j %d\n", N26);
+            tmp = sign_extend((N26<<2), 26) + dc->pc;
+            tcg_gen_movi_tl(jmp_pc, tmp);
+            dc->delayed_branch = 2;
+            dc->tb_flags |= D_FLAG;
+            tcg_gen_movi_i32(env_flags, dc->tb_flags);
+        }
+        break;
+
+    case 0x01:   /*l.jal*/
+        {
+            LOG_DIS("l.jal %d\n", N26);
+            tcg_gen_movi_tl(cpu_R[9], (dc->pc + 8));
+            tmp = sign_extend((N26<<2), 26) + dc->pc;
+            tcg_gen_movi_tl(jmp_pc, tmp);
+            dc->delayed_branch = 2;
+            dc->tb_flags |= D_FLAG;
+            tcg_gen_movi_i32(env_flags, dc->tb_flags);
+        }
+        break;
+
+    case 0x03:   /*l.bnf*/
+        LOG_DIS("l.bnf %d\n", N26);
+        {
+            int lab = gen_new_label();
+            TCGv sr_f = tcg_temp_new();
+            tcg_gen_andi_tl(sr_f, cpu_sr, SR_F);
+            tmp = sign_extend((N26 << 2), 26) + dc->pc;
+            tcg_gen_movi_tl(jmp_pc, dc->pc+8);
+            tcg_gen_brcondi_tl(TCG_COND_EQ, sr_f, SR_F, lab);
+            tcg_gen_movi_tl(jmp_pc, tmp);
+            gen_set_label(lab);
+            dc->delayed_branch = 2;
+            dc->tb_flags |= D_FLAG;
+            tcg_gen_movi_i32(env_flags, dc->tb_flags);
+            tcg_temp_free(sr_f);
+        }
+        break;
+
+    case 0x04:   /*l.bf*/
+        LOG_DIS("l.bf %d\n", N26);
+        {
+            int lab = gen_new_label();
+            TCGv sr_f = tcg_temp_new();
+            tcg_gen_andi_tl(sr_f, cpu_sr, SR_F);
+            tmp = sign_extend((N26 << 2), 26) + dc->pc;
+            tcg_gen_movi_tl(jmp_pc, dc->pc+8);
+            tcg_gen_brcondi_tl(TCG_COND_NE, sr_f, SR_F, lab);
+            tcg_gen_movi_tl(jmp_pc, tmp);
+            gen_set_label(lab);
+            dc->delayed_branch = 2;
+            dc->tb_flags |= D_FLAG;
+            tcg_gen_movi_i32(env_flags, dc->tb_flags);
+            tcg_temp_free(sr_f);
+        }
+        break;
+
+    case 0x05:
+        switch (op1) {
+        case 0x01:   /*l.nop*/
+            LOG_DIS("l.nop %d\n", I16);
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x11:    /*l.jr*/
+        LOG_DIS("l.jr r%d\n", rb);
+        {
+            tcg_gen_mov_tl(jmp_pc, cpu_R[rb]);
+            dc->delayed_branch = 2;
+            dc->tb_flags |= D_FLAG;
+            tcg_gen_movi_i32(env_flags, dc->tb_flags);
+        }
+        break;
+
+    case 0x12:    /*l.jalr*/
+        LOG_DIS("l.jalr r%d\n", rb);
+        {
+            tcg_gen_movi_tl(cpu_R[9], (dc->pc + 8));
+            gen_load_gpr(jmp_pc, rb);
+            dc->delayed_branch = 2;
+            dc->tb_flags |= D_FLAG;
+            tcg_gen_movi_i32(env_flags, dc->tb_flags);
+        }
+        break;
Quoted 9 lines

target-openrisc/translate.c typedef struct DisasContext { · line 556

Quoted 6 lines
target-openrisc/translate.c +6 -0
+
+    case 0x13:    /*l.maci*/
+        {
+            LOG_DIS("l.maci %d, r%d, %d\n", I5, ra, I11);
+            TCGv_i64 tra = tcg_temp_new_i64();    /*  store cpu_R[ra]*/
+            TCGv_i64 tmac = tcg_temp_new_i64();   /*  store machi maclo*/

Variable definitions should be before code, like LOG_DIS() call in this case.

OK, thx.

Quoted 811 lines
Quoted 811 lines
target-openrisc/translate.c +805 -0
+            tcg_gen_movi_tl(t0, tmp);
+            tcg_gen_ext32s_i64(tra, cpu_R[ra]);
+            tcg_gen_ext32s_i64(tmac, t0);
+            tcg_gen_mul_tl(tmac, tra, tmac);
+            tcg_gen_trunc_i64_i32(cpu_R[ra], tmac);
+            tcg_gen_add_i64(machi, machi, cpu_R[ra]);
+            tcg_gen_add_i64(maclo, maclo, cpu_R[ra]);
+            tcg_temp_free(tra);
+            tcg_temp_free(tmac);
+        }
+        break;
+
+    case 0x09:    /*l.rfe*/
+        LOG_DIS("l.rfe\n");
+        {
+            /* rfe need a helper here */
+            dc->is_jmp = DISAS_UPDATE;
+        }
+        break;
+
+    case 0x1c:    /*l.cust1*/
+        LOG_DIS("l.cust1\n");
+        break;
+
+    case 0x1d:    /*l.cust2*/
+        LOG_DIS("l.cust2\n");
+        break;
+
+    case 0x1e:    /*l.cust3*/
+        LOG_DIS("l.cust3\n");
+        break;
+
+    case 0x1f:    /*l.cust4*/
+        LOG_DIS("l.cust4\n");
+        break;
+
+    case 0x3c:   /*l.cust5*/
+        /*LOG_DIS("l.cust5 r%d, r%d, r%d, %d, %d\n", rd, ra, rb, L6, K5);*/
+        break;
+
+    case 0x3d:   /*l.cust6*/
+        LOG_DIS("l.cust6\n");
+        break;
+
+    case 0x3e:   /*l.cust7*/
+        LOG_DIS("l.cust7\n");
+        break;
+
+    case 0x3f:   /*l.cust8*/
+        LOG_DIS("l.cust8\n");
+        break;
+
+    case 0x20:   /*l.ld*/
+        LOG_DIS("l.ld r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld64(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x21:   /*l.lwz*/
+        LOG_DIS("l.lwz r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld32u(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x22:   /*l.lws*/
+        LOG_DIS("l.lws r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld32s(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x23:   /*l.lbz*/
+        LOG_DIS("l.lbz r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld8u(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x24:   /*l.lbs*/
+        LOG_DIS("l.lbs r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld8s(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x25:   /*l.lhz*/
+        LOG_DIS("l.lhz r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld16u(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x26:   /*l.lhs*/
+        LOG_DIS("l.lhs r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld16s(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x27:   /*l.addi*/
+        LOG_DIS("l.addi r%d, r%d, %d\n", rd, ra, I16);
+        {
+            TCGv t0 = tcg_const_tl(sign_extend(I16, 16));
+            /* Aha, we do need a helper here for addi */
+            tcg_temp_free(t0);
+        }
+        break;
+
+    case 0x28:   /*l.addic*/
+        LOG_DIS("l.addic r%d, r%d, %d\n", rd, ra, I16);
+        {
+            TCGv t0 = tcg_const_tl(sign_extend(I16, 16));
+            /* Aha, we do need a helper here for addic */
+            tcg_temp_free(t0);
+        }
+        break;
+
+    case 0x29:   /*l.andi*/
+        LOG_DIS("l.andi r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_andi_tl(cpu_R[rd], cpu_R[ra], zero_extend(I16, 16));
+        break;
+
+    case 0x2a:   /*l.ori*/
+        LOG_DIS("l.ori r%d, r%d, %d\n", rd, ra, zero_extend(I16, 16));
+        tcg_gen_ori_tl(cpu_R[rd], cpu_R[ra], I16);
+        break;
+
+    case 0x2b:   /*l.xori*/
+        LOG_DIS("l.xori r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_xori_tl(cpu_R[rd], cpu_R[ra], sign_extend(I16, 16));
+        break;
+
+    case 0x2c:   /*l.muli*/
+        LOG_DIS("l.muli r%d, r%d, %d\n", rd, ra, I16);
+        if (ra != 0 && I16 != 0) {
+            tcg_gen_muli_tl(cpu_R[rd], cpu_R[ra], I16);
+            tcg_gen_ext32s_tl(cpu_R[rd], cpu_R[rd]);
+        } else {
+            tcg_gen_movi_tl(cpu_R[rd], 0);
+        }
+        break;
+
+    case 0x2d:   /*l.mfspr*/
+        LOG_DIS("l.mfspr r%d, r%d, %d\n", rd, ra, I16);
+        {
+            TCGv_i32 ti = tcg_const_i32(I16);
+            TCGv td = tcg_const_tl(rd);
+            TCGv ta = tcg_const_tl(ra);
+            /* mfspr need a helper here */
+            tcg_temp_free_i32(ti);
+            tcg_temp_free(td);
+            tcg_temp_free(ta);
+        }
+        break;
+
+    case 0x30:  /*l.mtspr*/
+        LOG_DIS("l.mtspr %d, r%d, r%d, %d\n", I5, ra, rb, I11);
+        {
+            TCGv_i32 ti = tcg_const_i32(tmp);
+            TCGv ta = tcg_const_tl(ra);
+            TCGv tb = tcg_const_tl(rb);
+            /* mtspr need a helper here */
+            tcg_temp_free_i32(ti);
+            tcg_temp_free(ta);
+            tcg_temp_free(tb);
+        }
+        break;
+
+    case 0x34:   /*l.sd*/
+        LOG_DIS("l.sd %d, r%d, r%d, %d\n", I5, ra, rb, I11);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(tmp, 16));
+        tcg_gen_qemu_st64(cpu_R[rb], t0, dc->mem_idx);
+        break;
+
+    case 0x35:   /*l.sw*/
+        LOG_DIS("l.sw %d, r%d, r%d, %d\n", I5, ra, rb, I11);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(tmp, 16));
+        tcg_gen_qemu_st32(cpu_R[rb], t0, dc->mem_idx);
+        break;
+
+    case 0x36:   /*l.sb*/
+        LOG_DIS("l.sb %d, r%d, r%d, %d\n", I5, ra, rb, I11);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(tmp, 16));
+        tcg_gen_qemu_st8(cpu_R[rb], t0, dc->mem_idx);
+        break;
+
+    case 0x37:   /*l.sh*/
+        LOG_DIS("l.sh %d, r%d, r%d, %d\n", I5, ra, rb, I11);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(tmp, 16));
+        tcg_gen_qemu_st16(cpu_R[rb], t0, dc->mem_idx);
+        break;
+    default:
+        break;
+    }
+    tcg_temp_free(t0);
+}
+
+static void dec_mac(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t ra, rb;
+    op0 = field(insn, 0, 4);
+    ra = field(insn, 16, 5);
+    rb = field(insn, 11, 5);
+    TCGv_i64 t0 = tcg_temp_new();
+    TCGv_i64 t1 = tcg_temp_new();
+    switch (op0) {
+    case 0x0001:   /*l.mac*/
+        {
+            LOG_DIS("l.mac r%d, r%d\n", ra, rb);
+            tcg_gen_ext_i32_i64(t0, cpu_R[ra]);
+            tcg_gen_ext_i32_i64(t1, cpu_R[rb]);
+            tcg_gen_mul_i64(t0, t0, t1);
+            tcg_gen_trunc_i64_i32(cpu_R[ra], t0);
+            tcg_gen_add_tl(maclo, maclo, cpu_R[ra]);
+            tcg_gen_add_tl(machi, machi, cpu_R[ra]);
+            tcg_temp_free(t0);
+            tcg_temp_free(t1);
+        }
+        break;
+
+    case 0x0002:   /*l.msb*/
+        {
+            LOG_DIS("l.msb r%d, r%d\n", ra, rb);
+            tcg_gen_ext_i32_i64(t0, cpu_R[ra]);
+            tcg_gen_ext_i32_i64(t1, cpu_R[rb]);
+            tcg_gen_mul_i64(t0, t0, t1);
+            tcg_gen_trunc_i64_i32(cpu_R[ra], t0);
+            tcg_gen_sub_tl(maclo, maclo, cpu_R[ra]);
+            tcg_gen_sub_tl(machi, machi, cpu_R[ra]);
+            tcg_temp_free(t0);
+            tcg_temp_free(t1);
+        }
+        break;
+
+    default:
+        break;
+   }
+}
+
+static void dec_logic(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t rd, ra, L6;
+    op0 = field(insn, 6, 2);
+    rd = field(insn, 21, 5);
+    ra = field(insn, 16, 5);
+    L6 = field(insn, 0, 6);
+
+    switch (op0) {
+    case 0x00:    /*l.slli*/
+        LOG_DIS("l.slli r%d, r%d, %d\n", rd, ra, L6);
+        tcg_gen_shli_tl(cpu_R[rd], cpu_R[ra], (L6 & 0x1f));
+        break;
+
+    case 0x01:    /*l.srli*/
+        LOG_DIS("l.srli r%d, r%d, %d\n", rd, ra, L6);
+        tcg_gen_shri_tl(cpu_R[rd], cpu_R[ra], (L6 & 0x1f));
+        break;
+
+    case 0x02:    /*l.srai*/
+        LOG_DIS("l.srai r%d, r%d, %d\n", rd, ra, L6);
+        tcg_gen_sari_tl(cpu_R[rd], cpu_R[ra], (L6 & 0x1f)); break;
+
+    case 0x03:    /*l.rori*/
+        LOG_DIS("l.rori r%d, r%d, %d\n", rd, ra, L6);
+        tcg_gen_rotri_tl(cpu_R[rd], cpu_R[ra], (L6 & 0x1f));
+        break;
+
+    default:
+        break;
+    }
+}
+
+static void dec_M(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t rd;
+    uint32_t K16;
+    op0 = field(insn, 16, 1);
+    rd = field(insn, 21, 5);
+    K16 = field(insn, 0, 16);
+
+    switch (op0) {
+    case 0x0:    /*l.movhi*/
+        LOG_DIS("l.movhi  r%d, %d\n", rd, K16);
+        tcg_gen_movi_tl(cpu_R[rd], (K16 << 16));
+        break;
+
+    case 0x1:    /*l.macrc*/
+        LOG_DIS("l.macrc  r%d\n", rd);
+        tcg_gen_mov_tl(cpu_R[rd], maclo);
+        tcg_gen_movi_tl(maclo, 0x0);
+        tcg_gen_movi_tl(machi, 0x0);
+        break;
+
+    default:
+        break;
+    }
+}
+
+static void dec_comp(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t ra, rb;
+
+    op0 = field(insn, 21, 5);
+    ra = field(insn, 16, 5);
+    rb = field(insn, 11, 5);
+
+    tcg_gen_movi_i32(env_btaken, 0x0);
+    /* unsigned integers  */
+    tcg_gen_ext32u_tl(cpu_R[ra], cpu_R[ra]);
+    tcg_gen_ext32u_tl(cpu_R[rb], cpu_R[rb]);
+
+    switch (op0) {
+    case 0x0:    /*l.sfeq*/
+        LOG_DIS("l.sfeq  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_EQ, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x1:    /*l.sfne*/
+        LOG_DIS("l.sfne  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_NE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x2:    /*l.sfgtu*/
+        LOG_DIS("l.sfgtu  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_GTU, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x3:    /*l.sfgeu*/
+        LOG_DIS("l.sfgeu  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_GEU, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x4:    /*l.sfltu*/
+        LOG_DIS("l.sfltu  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_LTU, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x5:    /*l.sfleu*/
+        LOG_DIS("l.sfleu  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_LEU, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0xa:    /*l.sfgts*/
+        LOG_DIS("l.sfgts  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_GT, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0xb:    /*l.sfges*/
+        LOG_DIS("l.sfges  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_GE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0xc:    /*l.sflts*/
+        LOG_DIS("l.sflts  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_LT, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0xd:    /*l.sfles*/
+        LOG_DIS("l.sfles  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_LE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    default:
+        break;
+    }
+    wb_SR_F();
+}
+
+static void dec_compi(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t ra, I16;
+
+    op0 = field(insn, 21, 5);
+    ra = field(insn, 16, 5);
+    I16 = field(insn, 0, 16);
+
+    tcg_gen_movi_i32(env_btaken, 0x0);
+    tcg_gen_ext32u_tl(cpu_R[ra], cpu_R[ra]);
+    I16 = sign_extend(I16, 16);
+
+    switch (op0) {
+    case 0x0:    /*l.sfeqi*/
+        LOG_DIS("l.sfeqi  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_EQ, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0x1:    /*l.sfnei*/
+        LOG_DIS("l.sfnei  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_NE, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0x2:    /*l.sfgtui*/
+        LOG_DIS("l.sfgtui  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_GTU, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0x3:    /*l.sfgeui*/
+        LOG_DIS("l.sfgeui  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_GEU, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0x4:    /*l.sfltui*/
+        LOG_DIS("l.sfltui  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_LTU, env_btaken, cpu_R[ra],
+                            sign_extend(I16, 16));
+        break;
+
+    case 0x5:    /*l.sfleui*/
+        LOG_DIS("l.sfleui  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_LEU, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0xa:    /*l.sfgtsi*/
+        LOG_DIS("l.sfgtsi  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_GT, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0xb:    /*l.sfgesi*/
+        LOG_DIS("l.sfgesi  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_GE, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0xc:    /*l.sfltsi*/
+        LOG_DIS("l.sfltsi  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_LT, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0xd:    /*l.sflesi*/
+        LOG_DIS("l.sflesi  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_LE, env_btaken, cpu_R[ra], I16);
+        break;
+
+    default:
+        break;
+    }
+    wb_SR_F();
+}
+
+static void dec_sys(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    /*uint32_t K16;*/
+    op0 = field(insn, 16, 8);
+    /*K16 = field(insn, 0, 16);*/
+
+    switch (op0) {
+    case 0x000:  /*l.sys*/
+        /*LOG_DIS("l.sys %d\n", K16);*/
+        tcg_gen_movi_tl(cpu_pc, dc->pc);
+        /* exception here */
+        dc->is_jmp = DISAS_UPDATE;
+        break;
+
+    case 0x100:  /*l.trap*/
+        /*LOG_DIS("l.trap %d\n", K16);*/
+        tcg_gen_movi_tl(cpu_pc, dc->pc);
+        /* exception here */
+        break;
+
+    case 0x300:  /*l.csync*/
+        LOG_DIS("l.csync\n");
+        break;
+
+    case 0x200:  /*l.msync*/
+        LOG_DIS("l.msync\n");
+        break;
+
+    case 0x270:  /*l.psync*/
+        LOG_DIS("l.psync\n");
+        break;
+
+    default:
+        break;
+    }
+}
+
+static void dec_float(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t ra, rb, rd;
+    op0 = field(insn, 0, 8);
+    ra = field(insn, 16, 5);
+    rb = field(insn, 11, 5);
+    rd = field(insn, 21, 5);
+
+    switch (op0) {
+    case 0x10:    /*lf.add.d*/
+        LOG_DIS("lf.add.d r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_add_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x00:    /*lf.add.s*/
+        LOG_DIS("lf.add.s r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_add_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x11:    /*lf.sub.d*/
+        LOG_DIS("lf.sub.d r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_sub_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x01:    /*lf.sub.s*/
+        LOG_DIS("lf.sub.s r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_sub_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x12:    /*lf.mul.d*/
+        LOG_DIS("lf.mul.d r%d, r%d, r%d\n", rd, ra, rb);
+        {
+            if (cpu_R[ra] != 0 && cpu_R[rb] != 0) {
+                tcg_gen_mul_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            } else {
+                tcg_gen_movi_i64(cpu_R[rd], 0x0);
+            }
+        }
+        break;
+
+    case 0x02:    /*lf.mul.s*/
+        LOG_DIS("lf.mul.s r%d, r%d, r%d\n", rd, ra, rb);
+        if (cpu_R[ra] != 0 && cpu_R[rb] != 0) {
+            tcg_gen_mul_i32(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        } else {
+            tcg_gen_movi_i64(cpu_R[rd], 0x0);
+        }
+        break;
+
+    case 0x13:    /*lf.div.d*/
+        LOG_DIS("lf.div.d r%d, r%d, r%d\n", rd, ra, rb);
+        if (cpu_R[rb] != 0) {
+            tcg_gen_div_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        } else {
+            /* exception here */
+        }
+        break;
+
+    case 0x03:    /*lf.div.s*/
+        LOG_DIS("lf.div.s r%d, r%d, r%d\n", rd, ra, rb);
+        if (cpu_R[rb] != 0) {
+            tcg_gen_div_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        } else {
+            /* exception here */
+        }
+        break;
+
+    case 0x14:    /*lf.itof.d*/
+        LOG_DIS("lf.itof r%d, r%d\n", rd, ra);
+        /* itof.d need a helper here */
+        break;
+
+    case 0x04:    /*lf.itof.s*/
+        LOG_DIS("lf.itof r%d, r%d\n", rd, ra);
+        /* itof.s need a helper here */
+        break;
+
+    case 0x15:    /*lf.ftoi.d*/
+        LOG_DIS("lf.ftoi r%d, r%d\n", rd, ra);
+        /* ftoi.d need a helper here */
+        break;
+
+    case 0x05:    /*lf.ftoi.s*/
+        LOG_DIS("lf.ftoi r%d, r%d\n", rd, ra);
+        /* ftoi.s need a helper here */
+        break;
+
+    case 0x16:    /*lf.rem.d*/
+        LOG_DIS("lf.rem.d r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_rem_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x06:    /*lf.rem.s*/
+        LOG_DIS("lf.rem.s r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_rem_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x17:    /*lf.madd.d*/
+        LOG_DIS("lf.madd.d r%d, r%d, r%d\n", rd, ra, rb);
+        {
+            TCGv_i64 t0, t1;
+
+            t0 = tcg_temp_new_i64();
+            t1 = tcg_temp_new_i64();
+
+            tcg_gen_ext_i32_i64(t0, cpu_R[ra]);
+            tcg_gen_ext_i32_i64(t1, cpu_R[rb]);
+            tcg_gen_mul_i64(t0, t0, t1);
+
+            tcg_gen_trunc_i64_i32(cpu_R[ra], t0);
+            tcg_gen_add_tl(fpmaddlo, cpu_R[ra], fpmaddlo);
+            tcg_gen_shri_i64(t0, t0, 32);
+            tcg_gen_trunc_i64_i32(cpu_R[rb], t0);
+            tcg_gen_add_tl(fpmaddhi, cpu_R[rb], fpmaddhi);
+
+            tcg_temp_free_i64(t0);
+            tcg_temp_free_i64(t1);
+        }
+        break;
+
+    case 0x07:    /*lf.madd.s*/
+        LOG_DIS("lf.madd.s r%d, r%d, r%d\n", rd, ra, rb);
+        {
+            TCGv_i64 t0, t1;
+
+            t0 = tcg_temp_new_i64();
+            t1 = tcg_temp_new_i64();
+
+            tcg_gen_ext_i32_i64(t0, cpu_R[ra]);
+            tcg_gen_ext_i32_i64(t1, cpu_R[rb]);
+            tcg_gen_mul_i64(t0, t0, t1);
+
+            tcg_gen_trunc_i64_i32(cpu_R[ra], t0);
+            tcg_gen_add_tl(fpmaddlo, cpu_R[ra], fpmaddlo);
+            tcg_gen_shri_i64(t0, t0, 32);
+            tcg_gen_trunc_i64_i32(cpu_R[rb], t0);
+            tcg_gen_add_tl(fpmaddhi, cpu_R[rb], fpmaddhi);
+            tcg_temp_free_i64(t0);
+            tcg_temp_free_i64(t1);
+
+        }
+        break;
+
+    case 0x18:    /*lf.sfeq.d*/
+        LOG_DIS("lf.sfeq.d r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_i64(TCG_COND_GE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x08:    /*lf.sfeq.s*/
+        LOG_DIS("lf.sfeq.s r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_EQ, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x19:    /*lf.sfne.d*/
+        LOG_DIS("lf.sfne.d r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_i64(TCG_COND_NE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x09:    /*lf.sfne.s*/
+        LOG_DIS("lf.sfne.s r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_NE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x1a:    /*lf.sfgt.d*/
+        LOG_DIS("lf.sfgt.d r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_i64(TCG_COND_GT, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x0a:    /*lf.sfgt.s*/
+        LOG_DIS("lf.sfgt.s r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_GT, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x1b:    /*lf.sfge.d*/
+        LOG_DIS("lf.sfge.d r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_i64(TCG_COND_GE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x0b:    /*lf.sfge.s*/
+        LOG_DIS("lf.sfge.s r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_GE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x1c:    /*lf.sflt.d*/
+        LOG_DIS("lf.sflt.d r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_i64(TCG_COND_LT, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x0c:    /*lf.sflt.s*/
+        LOG_DIS("lf.sflt.s r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_LT, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x1d:    /*lf.sfle.d*/
+        LOG_DIS("lf.sfle.d r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_i64(TCG_COND_LE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x0d:    /*lf.sfle.s*/
+        LOG_DIS("lf.sfle.s r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_LE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    default:
+        break;
+    }
+    wb_SR_F();
+}
+
+static void disas_openrisc_insn(DisasContext *dc, CPUOPENRISCState *env)
+{
+    uint32_t op0;
+    uint32_t insn;
+    insn = ldl_code(dc->pc);
+    op0 = field(insn, 26, 6);
+
+    switch (op0) {
+    case 0x06:
+        dec_M(dc, env, insn);
+        break;
+
+    case 0x08:
+        dec_sys(dc, env, insn);
+        break;
+
+    case 0x2e:
+        dec_logic(dc, env, insn);
+        break;
+
+    case 0x2f:
+        dec_compi(dc, env, insn);
+        break;
+
+    case 0x31:
+        dec_mac(dc, env, insn);
+        break;
+
+    case 0x32:
+        dec_float(dc, env, insn);
+        break;
+
+    case 0x38:
+        dec_calc(dc, env, insn);
+        break;
+
+    case 0x39:
+        dec_comp(dc, env, insn);
+        break;
+
+    default:
+        dec_misc(dc, env, insn);
+        break;
+    }
+}
+
+static void check_breakpoint(CPUOPENRISCState *env, DisasContext *dc)
+{
+    CPUBreakpoint *bp;
+
+    if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
+        QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
+            if (bp->pc == dc->pc) {
+                tcg_gen_movi_tl(cpu_pc, dc->pc);
+                /* exception here */
+                dc->is_jmp = DISAS_UPDATE;
+            }
+        }
+    }
  }
 
  static inline void gen_intermediate_code_internal(CPUOPENRISCState *env,
                                                   TranslationBlock *tb,
                                                   int search_pc)
  {
+    struct DisasContext ctx, *dc = &ctx;
+    uint16_t *gen_opc_end;
+    uint32_t pc_start;
+    int j, lj;
+    uint32_t next_page_start;
+    int num_insns;
+    int max_insns;
+
+    qemu_log_try_set_file(stderr);
+
+    pc_start = tb->pc;
+    dc->env = env;
+    dc->tb = tb;
+
+    gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
+    dc->is_jmp = DISAS_NEXT;
+    dc->ppc = pc_start;
+    dc->pc = pc_start;
+    dc->mem_idx = cpu_mmu_index(env);
+    dc->tb_flags = tb->flags;
+    dc->delayed_branch = !!(dc->tb_flags & D_FLAG);
+    dc->singlestep_enabled = env->singlestep_enabled;
+    if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
+        qemu_log("-----------------------------------------\n");
+        log_cpu_state(env, 0);
+    }
+
+    next_page_start = (pc_start & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
+    lj = -1;
+    num_insns = 0;
+    max_insns = tb->cflags & CF_COUNT_MASK;
+
+    if (max_insns == 0) {
+        max_insns = CF_COUNT_MASK;
+    }
+
+    gen_icount_start();
+
+    do {
+        check_breakpoint(env, dc);
+        if (search_pc) {
+            j = gen_opc_ptr - gen_opc_buf;
+            if (lj < j) {
+                lj++;
+                while (lj < j) {
+                    gen_opc_instr_start[lj++] = 0;
+                }
+            }
+            gen_opc_pc[lj] = dc->pc;
+            gen_opc_instr_start[lj] = 1;
+            gen_opc_icount[lj] = num_insns;
+        }
+
+        if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP))) {
+            tcg_gen_debug_insn_start(dc->pc);
+        }
+
+        if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO)) {
+            gen_io_start();
+        }
Quoted 9 lines

target-openrisc/translate.c typedef struct DisasContext { · line 1373

Quoted 6 lines
target-openrisc/translate.c +6 -0
+        dc->ppc = dc->pc - 4;
+        dc->npc = dc->pc + 4;
+        tcg_gen_movi_tl(cpu_ppc, dc->ppc);
+        tcg_gen_movi_tl(cpu_npc, dc->npc);
+        disas_openrisc_insn(dc, env);
+        dc->pc = dc->npc;

Isn't now npc == pc and ppc == pc - 8?

I don't think so, but I'll check it.

Quoted 10 lines

target-openrisc/translate.c typedef struct DisasContext { · line 1380

Quoted 7 lines
target-openrisc/translate.c +7 -0
+        num_insns++;
+        /* delay slot */
+        if (dc->delayed_branch) {
+            dc->delayed_branch--;
+            if (!dc->delayed_branch) {
+                dc->tb_flags &= ~D_FLAG;
+                tcg_gen_movi_i32(env_flags, dc->tb_flags);

This does not look correct.

for exception in delay slot, it is.

Quoted 5 lines

target-openrisc/translate.c typedef struct DisasContext { · line 1382

Quoted 2 lines
target-openrisc/translate.c +2 -0
+                tcg_gen_mov_tl(cpu_pc, jmp_pc);
+                tcg_gen_mov_tl(cpu_npc, jmp_pc);

+ 4?

Quoted 9 lines

target-openrisc/translate.c typedef struct DisasContext { · line 1387

Quoted 5 lines
target-openrisc/translate.c +5 -0
+                tcg_gen_movi_tl(jmp_pc, 0);
+                tcg_gen_exit_tb(0);
+                dc->is_jmp = DISAS_JUMP;
+                break;
+            }

Is it possible to have a jump instruction in the delay slot and if so, is it handled too?

I've done for this.

Quoted 75 lines
Quoted 75 lines
target-openrisc/translate.c +65 -0
+        }
+    } while (!dc->is_jmp && gen_opc_ptr < gen_opc_end
+             && !env->singlestep_enabled
+             && !singlestep
+             && (dc->pc < next_page_start)
+             && num_insns < max_insns);
+
+    if (tb->cflags & CF_LAST_IO) {
+        gen_io_end();
+    }
+    if (dc->is_jmp == DISAS_NEXT) {
+        dc->is_jmp = DISAS_UPDATE;
+        tcg_gen_movi_tl(cpu_pc, dc->pc);
+    }
+    if (unlikely(env->singlestep_enabled)) {
+        if (dc->is_jmp == DISAS_NEXT) {
+            tcg_gen_movi_tl(cpu_pc, dc->pc);
+        }
+        /* exception here*/
+    } else {
+        switch (dc->is_jmp) {
+        case DISAS_NEXT:
+            gen_goto_tb(dc, 0, dc->pc);
+            break;
+        default:
+        case DISAS_JUMP:
+            break;
+        case DISAS_UPDATE:
+            /* indicate that the hash table must be used
+               to find the next TB */
+            tcg_gen_exit_tb(0);
+            break;
+        case DISAS_TB_JUMP:
+            /* nothing more to generate */
+            break;
+        }
+    }
+
+    gen_icount_end(tb, num_insns);
+    *gen_opc_ptr = INDEX_op_end;
+    if (search_pc) {
+        j = gen_opc_ptr - gen_opc_buf;
+        lj++;
+        while (lj <= j) {
+            gen_opc_instr_start[lj++] = 0;
+        }
+    } else {
+        tb->size = dc->pc - pc_start;
+        tb->icount = num_insns;
+    }
+
+#ifdef DEBUG_DISAS
+    if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
+        qemu_log("\n");
+        log_target_disas(pc_start, dc->pc - pc_start, 0);
+        qemu_log("\nisize=%d osize=%td\n",
+            dc->pc - pc_start, gen_opc_ptr - gen_opc_buf);
+    }
+#endif
  }
 
  void gen_intermediate_code(CPUOPENRISCState *env, struct TranslationBlock *tb)
@@ -80,7 +1461,13 @@ void cpu_dump_state(CPUOPENRISCState *env, FILE *f,
                     fprintf_function cpu_fprintf,
                     int flags)
  {
+    int i;
+    uint32_t *regs = env->gpr;
     cpu_fprintf(f, "PC=%08x\n", env->pc);
+    for (i = 0; i < 32; ++i) {
+        cpu_fprintf(f, "R%02d=%08x%c", i, regs[i],
+                    (i % 4) == 3 ? '\n' : ' ');
+    }
  }
 
Quoted 5 lines
Quoted 5 lines
target-openrisc/translate.c +0 -1
  void restore_state_to_opc(CPUOPENRISCState *env, TranslationBlock *tb,
--

1.7.9.5

Jia,
Regards.

#26 lore raw

Re: [Qemu-devel] [PATCH 03/15] Openrisc: add instructions translation

Quoted 272 lines · Jia Liu

Thanks Blue.

Quoted 111 lines · Blue Swirl
Quoted 110 lines · Jia Liu

add the openrisc instructions translation.

---
 target-openrisc/translate.c | 1387 +++++++++++++++++++++++++++++++++++++++++++

 1 file changed, 1387 insertions(+)
target-openrisc/translate.c +93 -0
index aae483a..4828ae6 100644
@@ -55,14 +55,1395 @@ typedef struct DisasContext {
     uint32_t delayed_branch;
  } DisasContext;
 
+static TCGv_ptr cpu_env;
+static TCGv cpu_sr;
+static TCGv cpu_R[32];
+static TCGv cpu_pc;
+static TCGv jmp_pc;        /* l.jr/l.jalr temp pc */
+static TCGv cpu_npc;
+static TCGv cpu_ppc;
+static TCGv_i32 env_btaken;    /* bf/bnf , F flag taken */
+static TCGv machi, maclo;
+static TCGv fpmaddhi, fpmaddlo;
+static TCGv_i32 env_flags;
+#include "gen-icount.h"
+
  void openrisc_translate_init(void)
  {
+    static const char * const regnames[] = {
+        "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
+        "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
+        "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
+        "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
+    };
+    int i;
+
+    cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
+    cpu_sr = tcg_global_mem_new(TCG_AREG0,
+                                offsetof(CPUOPENRISCState, sr), "sr");
+    env_flags = tcg_global_mem_new_i32(TCG_AREG0,
+                                       offsetof(CPUOPENRISCState, flags),
+                                       "flags");
+    cpu_pc = tcg_global_mem_new(TCG_AREG0,
+                                offsetof(CPUOPENRISCState, pc), "pc");
+    cpu_npc = tcg_global_mem_new(TCG_AREG0,
+                                 offsetof(CPUOPENRISCState, npc), "npc");
+    cpu_ppc = tcg_global_mem_new(TCG_AREG0,
+                                 offsetof(CPUOPENRISCState, ppc), "ppc");
+    jmp_pc = tcg_global_mem_new(TCG_AREG0,
+                                offsetof(CPUOPENRISCState, jmp_pc), "jmp_pc");
+    env_btaken = tcg_global_mem_new_i32(TCG_AREG0,
+                                        offsetof(CPUOPENRISCState, btaken),
+                                        "btaken");
+    machi = tcg_global_mem_new(TCG_AREG0,
+                               offsetof(CPUOPENRISCState, machi),
+                               "machi");
+    maclo = tcg_global_mem_new(TCG_AREG0,
+                               offsetof(CPUOPENRISCState, maclo),
+                               "maclo");
+    fpmaddhi = tcg_global_mem_new(TCG_AREG0,
+                                  offsetof(CPUOPENRISCState, fpmaddhi),
+                                  "fpmaddhi");
+    fpmaddlo = tcg_global_mem_new(TCG_AREG0,
+                                  offsetof(CPUOPENRISCState, fpmaddlo),
+                                  "fpmaddlo");
+    for (i = 0; i < 32; i++) {
+        cpu_R[i] = tcg_global_mem_new(TCG_AREG0,
+                                      offsetof(CPUOPENRISCState, gpr[i]),
+                                      regnames[i]);
+    }
+#define GEN_HELPER 2
+#include "helper.h"
+}
+
+/* Writeback SR_F transaltion-space to execution-space.  */
+static inline void wb_SR_F(void)
+{
+    int label;
+
+    label = gen_new_label();
+    tcg_gen_andi_tl(cpu_sr, cpu_sr, ~SR_F);
+    tcg_gen_brcondi_tl(TCG_COND_EQ, env_btaken, 0, label);
+    tcg_gen_ori_tl(cpu_sr, cpu_sr, SR_F);
+    gen_set_label(label);
+}
+
+static inline int zero_extend(unsigned int val, int width)
+{
+    return val & ((1 << width) - 1);
+}
+
+static inline int sign_extend(unsigned int val, int width)
+{
+    int sval;
+
+    /* LSL.  */
+    val <<= 32 - width;
+    sval = val;
+    /* ASR.  */
+    sval >>= 32 - width;
+    return sval;
+}
+
+/* General purpose registers moves. */
+static inline void gen_load_gpr(TCGv t, unsigned int reg)
+{
+    if (reg == 0) {
+        tcg_gen_movi_tl(t, 0);
Quoted 9 lines

target-openrisc/translate.c typedef struct DisasContext { · line 156

Quoted 4 lines
target-openrisc/translate.c +4 -0
+    } else {
+        tcg_gen_mov_tl(t, cpu_R[reg]);
+    }
+}

This does not seem to be used. Does the code handle the cases where one or several of the registers is number 0? If this is the always-0 register, are stores to register 0 handled correctly?

I'll remove it.

Quoted 65 lines
Quoted 15 lines
target-openrisc/translate.c +15 -0
+
+static void gen_goto_tb(DisasContext *dc, int n, target_ulong dest)
+{
+    TranslationBlock *tb;
+    tb = dc->tb;
+    if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) &&
+                                       likely(!dc->singlestep_enabled)) {
+        tcg_gen_movi_tl(cpu_pc, dest);
+        tcg_gen_goto_tb(n);
+        tcg_gen_exit_tb((tcg_target_long)tb + n);
+    } else {
+        tcg_gen_movi_tl(cpu_pc, dest);
+        if (dc->singlestep_enabled) {
+            /* exception here */
+        }

target-openrisc/translate.c typedef struct DisasContext { · line 181

Quoted 10 lines
target-openrisc/translate.c +10 -0
+        tcg_gen_exit_tb(0);
+    }
+}
+
+static inline uint32_t field(uint32_t val, int start, int length)
+{
+    val >>= start;
+    val &= ~(~0 << length);
+    return val;
+}

Nice function, maybe I could use it in Sparc as well if generalized (later).

Quoted 37 lines
target-openrisc/translate.c +37 -0
+
+static void dec_calc(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0, op1, op2;
+    uint32_t ra, rb, rd;
+    op0 = field(insn, 0, 4);
+    op1 = field(insn, 8, 2);
+    op2 = field(insn, 6, 2);
+    ra = field(insn, 16, 5);
+    rb = field(insn, 11, 5);
+    rd = field(insn, 21, 5);
+
+    switch (op0) {
+    case 0x0000:
+        switch (op1) {
+        case 0x00:     /*l.add*/
+            LOG_DIS("l.add r%d, r%d, r%d\n", rd, ra, rb);
+            /* Aha, we do need a helper here for add */
+            break;
+        default:
+            break;
+        }
+    break;
+
+    case 0x0001:    /*l.addc*/
+        switch (op1) {
+        case 0x00:
+            LOG_DIS("l.addc r%d, r%d, r%d\n", rd, ra, rb);
+            /* Aha, we do need a helper here for addc */
+            break;
+        default:
+            break;
+        }
+    break;
+
+    case 0x0002:    /*l.sub*/
+        switch (op1) {
Quoted 9 lines

target-openrisc/translate.c typedef struct DisasContext { · line 223

Quoted 5 lines
target-openrisc/translate.c +5 -0
+        case 0x00:
+            LOG_DIS("l.sub r%d, r%d, r%d\n", rd, ra, rb);
+            /* Aha, we do need a helper here for sub */
+            break;
+        default:

What happens on a real CPU when an illegal instruction is executed, shouldn't there be an exception?

I'll add a exception for illegal instruction.

Quoted 69 lines
Quoted 38 lines
target-openrisc/translate.c +38 -0
+            break;
+        }
+    break;
+
+    case 0x0003:   /*l.and*/
+        switch (op1) {
+        case 0x00:
+            LOG_DIS("l.and r%d, r%d, r%d\n", rd, ra, rb);
+            tcg_gen_and_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            break;
+        default:
+            break;
+        }
+    break;
+
+    case 0x0004:   /*l.or*/
+        switch (op1) {
+        case 0x00:
+            LOG_DIS("l.or r%d, r%d, r%d\n", rd, ra, rb);
+            tcg_gen_or_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            break;
+        default:
+            break;
+        }
+    break;
+
+    case 0x0005:
+        switch (op1) {
+        case 0x00:   /*l.xor*/
+            LOG_DIS("l.xor r%d, r%d, r%d\n", rd, ra, rb);
+            tcg_gen_xor_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            break;
+        default:
+            break;
+        }
+    break;
+
+    case 0x0006:

target-openrisc/translate.c typedef struct DisasContext { · line 271

Quoted 10 lines
target-openrisc/translate.c +10 -0
+        switch (op1) {
+        case 0x03:   /*l.mul*/
+            LOG_DIS("l.mul r%d, r%d, r%d\n", rd, ra, rb);
+            if (ra != 0 && rb != 0) {
+                tcg_gen_mul_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+                tcg_gen_ext32s_tl(cpu_R[rd], cpu_R[rd]);
+            } else {
+                tcg_gen_movi_tl(cpu_R[rd], 0x0);
+            }
+        break;

Indentation.

Quoted 18 lines
target-openrisc/translate.c +18 -0
+        default:
+            break;
+        }
+    break;
+
+    case 0x0009:
+        switch (op1) {
+        case 0x03:   /*l.div*/
+            LOG_DIS("l.div r%d, r%d, r%d\n", rd, ra, rb);
+            if (rb != 0) {
+                tcg_gen_div_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            } else {
+                /* exception here */
+            }
+            break;
+        default:
+            break;
+        }
Quoted 8 lines
Quoted 8 lines

target-openrisc/translate.c typedef struct DisasContext { · line 297

Quoted 8 lines
target-openrisc/translate.c +8 -0
+        break;
+
+    case 0x000a:
+        switch (op1) {
+        case 0x03:   /*l.divu*/
+            LOG_DIS("l.divu r%d, r%d, r%d\n", rd, ra, rb);
+            if (rb != 0) {
+                tcg_gen_divu_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);

By the way, a division by zero will crash QEMU. Please add a runtime check, something like this:
http://git.qemu.org/?p=qemu.git;a=blob;f=target-sparc/translate.c;h=b95f91cd3baf9c02305f8583662b8eba664b17ba;hb=HEAD#l765

Quoted 1,086 lines
Quoted 253 lines
Quoted 253 lines
target-openrisc/translate.c +253 -0
+            } else {
+                /* exception here */
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000b:
+        switch (op1) {
+        case 0x03:   /*l.mulu*/
+            LOG_DIS("l.mulu r%d, r%d, r%d\n", rd, ra, rb);
+            if (rb != 0 && ra != 0) {
+                tcg_gen_ext32u_tl(cpu_R[ra], cpu_R[ra]);
+                tcg_gen_ext32u_tl(cpu_R[rb], cpu_R[rb]);
+                tcg_gen_mul_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            } else {
+                tcg_gen_movi_tl(cpu_R[rd], 0);
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000e:
+        switch (op1) {
+        case 0x00:   /*l.cmov*/
+            LOG_DIS("l.cmov r%d, r%d, r%d\n", rd, ra, rb);
+            {
+                int lab = gen_new_label();
+                TCGv sr_f = tcg_temp_new();
+                tcg_gen_andi_tl(sr_f, cpu_sr, SR_F);
+                tcg_gen_mov_tl(cpu_R[rd], cpu_R[rb]);
+                tcg_gen_brcondi_tl(TCG_COND_NE, sr_f, SR_F, lab);
+                tcg_gen_mov_tl(cpu_R[rd], cpu_R[ra]);
+                gen_set_label(lab);
+                tcg_temp_free(sr_f);
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000f:
+        switch (op1) {
+        case 0x00:   /*l.ff1*/
+            LOG_DIS("l.ff1 r%d, r%d, r%d\n", rd, ra, rb);
+            /* ff1 need a helper here */
+            break;
+        case 0x01:   /*l.fl1*/
+            LOG_DIS("l.fl1 r%d, r%d, r%d\n", rd, ra, rb);
+            /* fl1 need a helper here */
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x0008:
+        switch (op1) {
+        case 0x00:
+            switch (op2) {
+            case 0x00:   /*l.sll*/
+                LOG_DIS("l.sll r%d, r%d, r%d\n", rd, ra, rb);
+                tcg_gen_shl_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+                break;
+            case 0x01:   /*l.srl*/
+                LOG_DIS("l.srl r%d, r%d, r%d\n", rd, ra, rb);
+                tcg_gen_shr_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+                break;
+            case 0x02:   /*l.sra*/
+                LOG_DIS("l.sra r%d, r%d, r%d\n", rd, ra, rb);
+                tcg_gen_sar_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+                break;
+            case 0x03:   /*l.ror*/
+                LOG_DIS("l.ror r%d, r%d, r%d\n", rd, ra, rb);
+                tcg_gen_rotr_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+                break;
+            default:
+                break;
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000c:
+        switch (op1) {
+        case 0x00:
+            switch (op2) {
+            case 0x00:   /*l.exths*/
+                LOG_DIS("l.exths r%d, r%d\n", rd, ra);
+                tcg_gen_ext16s_tl(cpu_R[rd], cpu_R[ra]);
+                break;
+            case 0x01:   /*l.extbs*/
+                LOG_DIS("l.extbs r%d, r%d\n", rd, ra);
+                tcg_gen_ext8s_tl(cpu_R[rd], cpu_R[ra]);
+                break;
+            case 0x02:   /*l.exthz*/
+                LOG_DIS("l.exthz r%d, r%d\n", rd, ra);
+                tcg_gen_ext16u_tl(cpu_R[rd], cpu_R[ra]);
+                break;
+            case 0x03:   /*l.extbz*/
+                LOG_DIS("l.extbz r%d, r%d\n", rd, ra);
+                tcg_gen_ext8u_tl(cpu_R[rd], cpu_R[ra]);
+                break;
+            default:
+                break;
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000d:
+        switch (op1) {
+        case 0x00:
+            switch (op2) {
+            case 0x00:   /*l.extws*/
+                LOG_DIS("l.extws r%d, r%d\n", rd, ra);
+                tcg_gen_ext32s_tl(cpu_R[rd], cpu_R[ra]);
+                break;
+            case 0x01:    /*l.extwz*/
+                LOG_DIS("l.extwz r%d, r%d\n", rd, ra);
+                tcg_gen_ext32u_tl(cpu_R[rd], cpu_R[ra]);
+                break;
+            default:
+                break;
+            }
+        default:
+            break;
+        }
+        break;
+
+    default:
+        break;
+    }
+}
+
+static void dec_misc(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0, op1;
+    uint32_t ra, rb, rd;
+    uint32_t /*L6, K5, */I16, I5, I11, N26, tmp;
+    op0 = field(insn, 26, 6);
+    op1 = field(insn, 24, 2);
+    ra = field(insn, 16, 5);
+    rb = field(insn, 11, 5);
+    rd = field(insn, 21, 5);
+    /*L6 = field(insn, 5, 6);
+    K5 = field(insn, 0, 5);*/
+    I16 = field(insn, 0, 16);
+    I5 = field(insn, 21, 5);
+    I11 = field(insn, 0, 11);
+    N26 = field(insn, 0, 26);
+    tmp = (I5<<11) + I11;
+    TCGv t0 = tcg_temp_new();
+
+    switch (op0) {
+    case 0x00:   /*l.j*/
+        {
+            LOG_DIS("l.j %d\n", N26);
+            tmp = sign_extend((N26<<2), 26) + dc->pc;
+            tcg_gen_movi_tl(jmp_pc, tmp);
+            dc->delayed_branch = 2;
+            dc->tb_flags |= D_FLAG;
+            tcg_gen_movi_i32(env_flags, dc->tb_flags);
+        }
+        break;
+
+    case 0x01:   /*l.jal*/
+        {
+            LOG_DIS("l.jal %d\n", N26);
+            tcg_gen_movi_tl(cpu_R[9], (dc->pc + 8));
+            tmp = sign_extend((N26<<2), 26) + dc->pc;
+            tcg_gen_movi_tl(jmp_pc, tmp);
+            dc->delayed_branch = 2;
+            dc->tb_flags |= D_FLAG;
+            tcg_gen_movi_i32(env_flags, dc->tb_flags);
+        }
+        break;
+
+    case 0x03:   /*l.bnf*/
+        LOG_DIS("l.bnf %d\n", N26);
+        {
+            int lab = gen_new_label();
+            TCGv sr_f = tcg_temp_new();
+            tcg_gen_andi_tl(sr_f, cpu_sr, SR_F);
+            tmp = sign_extend((N26 << 2), 26) + dc->pc;
+            tcg_gen_movi_tl(jmp_pc, dc->pc+8);
+            tcg_gen_brcondi_tl(TCG_COND_EQ, sr_f, SR_F, lab);
+            tcg_gen_movi_tl(jmp_pc, tmp);
+            gen_set_label(lab);
+            dc->delayed_branch = 2;
+            dc->tb_flags |= D_FLAG;
+            tcg_gen_movi_i32(env_flags, dc->tb_flags);
+            tcg_temp_free(sr_f);
+        }
+        break;
+
+    case 0x04:   /*l.bf*/
+        LOG_DIS("l.bf %d\n", N26);
+        {
+            int lab = gen_new_label();
+            TCGv sr_f = tcg_temp_new();
+            tcg_gen_andi_tl(sr_f, cpu_sr, SR_F);
+            tmp = sign_extend((N26 << 2), 26) + dc->pc;
+            tcg_gen_movi_tl(jmp_pc, dc->pc+8);
+            tcg_gen_brcondi_tl(TCG_COND_NE, sr_f, SR_F, lab);
+            tcg_gen_movi_tl(jmp_pc, tmp);
+            gen_set_label(lab);
+            dc->delayed_branch = 2;
+            dc->tb_flags |= D_FLAG;
+            tcg_gen_movi_i32(env_flags, dc->tb_flags);
+            tcg_temp_free(sr_f);
+        }
+        break;
+
+    case 0x05:
+        switch (op1) {
+        case 0x01:   /*l.nop*/
+            LOG_DIS("l.nop %d\n", I16);
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x11:    /*l.jr*/
+        LOG_DIS("l.jr r%d\n", rb);
+        {
+            tcg_gen_mov_tl(jmp_pc, cpu_R[rb]);
+            dc->delayed_branch = 2;
+            dc->tb_flags |= D_FLAG;
+            tcg_gen_movi_i32(env_flags, dc->tb_flags);
+        }
+        break;
+
+    case 0x12:    /*l.jalr*/
+        LOG_DIS("l.jalr r%d\n", rb);
+        {
+            tcg_gen_movi_tl(cpu_R[9], (dc->pc + 8));
+            gen_load_gpr(jmp_pc, rb);
+            dc->delayed_branch = 2;
+            dc->tb_flags |= D_FLAG;
+            tcg_gen_movi_i32(env_flags, dc->tb_flags);
+        }
+        break;
Quoted 9 lines

target-openrisc/translate.c typedef struct DisasContext { · line 556

Quoted 6 lines
target-openrisc/translate.c +6 -0
+
+    case 0x13:    /*l.maci*/
+        {
+            LOG_DIS("l.maci %d, r%d, %d\n", I5, ra, I11);
+            TCGv_i64 tra = tcg_temp_new_i64();    /*  store cpu_R[ra]*/
+            TCGv_i64 tmac = tcg_temp_new_i64();   /*  store machi maclo*/

Variable definitions should be before code, like LOG_DIS() call in this case.

OK, thx.

Quoted 811 lines
Quoted 811 lines
target-openrisc/translate.c +805 -0
+            tcg_gen_movi_tl(t0, tmp);
+            tcg_gen_ext32s_i64(tra, cpu_R[ra]);
+            tcg_gen_ext32s_i64(tmac, t0);
+            tcg_gen_mul_tl(tmac, tra, tmac);
+            tcg_gen_trunc_i64_i32(cpu_R[ra], tmac);
+            tcg_gen_add_i64(machi, machi, cpu_R[ra]);
+            tcg_gen_add_i64(maclo, maclo, cpu_R[ra]);
+            tcg_temp_free(tra);
+            tcg_temp_free(tmac);
+        }
+        break;
+
+    case 0x09:    /*l.rfe*/
+        LOG_DIS("l.rfe\n");
+        {
+            /* rfe need a helper here */
+            dc->is_jmp = DISAS_UPDATE;
+        }
+        break;
+
+    case 0x1c:    /*l.cust1*/
+        LOG_DIS("l.cust1\n");
+        break;
+
+    case 0x1d:    /*l.cust2*/
+        LOG_DIS("l.cust2\n");
+        break;
+
+    case 0x1e:    /*l.cust3*/
+        LOG_DIS("l.cust3\n");
+        break;
+
+    case 0x1f:    /*l.cust4*/
+        LOG_DIS("l.cust4\n");
+        break;
+
+    case 0x3c:   /*l.cust5*/
+        /*LOG_DIS("l.cust5 r%d, r%d, r%d, %d, %d\n", rd, ra, rb, L6, K5);*/
+        break;
+
+    case 0x3d:   /*l.cust6*/
+        LOG_DIS("l.cust6\n");
+        break;
+
+    case 0x3e:   /*l.cust7*/
+        LOG_DIS("l.cust7\n");
+        break;
+
+    case 0x3f:   /*l.cust8*/
+        LOG_DIS("l.cust8\n");
+        break;
+
+    case 0x20:   /*l.ld*/
+        LOG_DIS("l.ld r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld64(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x21:   /*l.lwz*/
+        LOG_DIS("l.lwz r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld32u(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x22:   /*l.lws*/
+        LOG_DIS("l.lws r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld32s(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x23:   /*l.lbz*/
+        LOG_DIS("l.lbz r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld8u(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x24:   /*l.lbs*/
+        LOG_DIS("l.lbs r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld8s(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x25:   /*l.lhz*/
+        LOG_DIS("l.lhz r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld16u(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x26:   /*l.lhs*/
+        LOG_DIS("l.lhs r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld16s(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x27:   /*l.addi*/
+        LOG_DIS("l.addi r%d, r%d, %d\n", rd, ra, I16);
+        {
+            TCGv t0 = tcg_const_tl(sign_extend(I16, 16));
+            /* Aha, we do need a helper here for addi */
+            tcg_temp_free(t0);
+        }
+        break;
+
+    case 0x28:   /*l.addic*/
+        LOG_DIS("l.addic r%d, r%d, %d\n", rd, ra, I16);
+        {
+            TCGv t0 = tcg_const_tl(sign_extend(I16, 16));
+            /* Aha, we do need a helper here for addic */
+            tcg_temp_free(t0);
+        }
+        break;
+
+    case 0x29:   /*l.andi*/
+        LOG_DIS("l.andi r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_andi_tl(cpu_R[rd], cpu_R[ra], zero_extend(I16, 16));
+        break;
+
+    case 0x2a:   /*l.ori*/
+        LOG_DIS("l.ori r%d, r%d, %d\n", rd, ra, zero_extend(I16, 16));
+        tcg_gen_ori_tl(cpu_R[rd], cpu_R[ra], I16);
+        break;
+
+    case 0x2b:   /*l.xori*/
+        LOG_DIS("l.xori r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_xori_tl(cpu_R[rd], cpu_R[ra], sign_extend(I16, 16));
+        break;
+
+    case 0x2c:   /*l.muli*/
+        LOG_DIS("l.muli r%d, r%d, %d\n", rd, ra, I16);
+        if (ra != 0 && I16 != 0) {
+            tcg_gen_muli_tl(cpu_R[rd], cpu_R[ra], I16);
+            tcg_gen_ext32s_tl(cpu_R[rd], cpu_R[rd]);
+        } else {
+            tcg_gen_movi_tl(cpu_R[rd], 0);
+        }
+        break;
+
+    case 0x2d:   /*l.mfspr*/
+        LOG_DIS("l.mfspr r%d, r%d, %d\n", rd, ra, I16);
+        {
+            TCGv_i32 ti = tcg_const_i32(I16);
+            TCGv td = tcg_const_tl(rd);
+            TCGv ta = tcg_const_tl(ra);
+            /* mfspr need a helper here */
+            tcg_temp_free_i32(ti);
+            tcg_temp_free(td);
+            tcg_temp_free(ta);
+        }
+        break;
+
+    case 0x30:  /*l.mtspr*/
+        LOG_DIS("l.mtspr %d, r%d, r%d, %d\n", I5, ra, rb, I11);
+        {
+            TCGv_i32 ti = tcg_const_i32(tmp);
+            TCGv ta = tcg_const_tl(ra);
+            TCGv tb = tcg_const_tl(rb);
+            /* mtspr need a helper here */
+            tcg_temp_free_i32(ti);
+            tcg_temp_free(ta);
+            tcg_temp_free(tb);
+        }
+        break;
+
+    case 0x34:   /*l.sd*/
+        LOG_DIS("l.sd %d, r%d, r%d, %d\n", I5, ra, rb, I11);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(tmp, 16));
+        tcg_gen_qemu_st64(cpu_R[rb], t0, dc->mem_idx);
+        break;
+
+    case 0x35:   /*l.sw*/
+        LOG_DIS("l.sw %d, r%d, r%d, %d\n", I5, ra, rb, I11);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(tmp, 16));
+        tcg_gen_qemu_st32(cpu_R[rb], t0, dc->mem_idx);
+        break;
+
+    case 0x36:   /*l.sb*/
+        LOG_DIS("l.sb %d, r%d, r%d, %d\n", I5, ra, rb, I11);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(tmp, 16));
+        tcg_gen_qemu_st8(cpu_R[rb], t0, dc->mem_idx);
+        break;
+
+    case 0x37:   /*l.sh*/
+        LOG_DIS("l.sh %d, r%d, r%d, %d\n", I5, ra, rb, I11);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(tmp, 16));
+        tcg_gen_qemu_st16(cpu_R[rb], t0, dc->mem_idx);
+        break;
+    default:
+        break;
+    }
+    tcg_temp_free(t0);
+}
+
+static void dec_mac(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t ra, rb;
+    op0 = field(insn, 0, 4);
+    ra = field(insn, 16, 5);
+    rb = field(insn, 11, 5);
+    TCGv_i64 t0 = tcg_temp_new();
+    TCGv_i64 t1 = tcg_temp_new();
+    switch (op0) {
+    case 0x0001:   /*l.mac*/
+        {
+            LOG_DIS("l.mac r%d, r%d\n", ra, rb);
+            tcg_gen_ext_i32_i64(t0, cpu_R[ra]);
+            tcg_gen_ext_i32_i64(t1, cpu_R[rb]);
+            tcg_gen_mul_i64(t0, t0, t1);
+            tcg_gen_trunc_i64_i32(cpu_R[ra], t0);
+            tcg_gen_add_tl(maclo, maclo, cpu_R[ra]);
+            tcg_gen_add_tl(machi, machi, cpu_R[ra]);
+            tcg_temp_free(t0);
+            tcg_temp_free(t1);
+        }
+        break;
+
+    case 0x0002:   /*l.msb*/
+        {
+            LOG_DIS("l.msb r%d, r%d\n", ra, rb);
+            tcg_gen_ext_i32_i64(t0, cpu_R[ra]);
+            tcg_gen_ext_i32_i64(t1, cpu_R[rb]);
+            tcg_gen_mul_i64(t0, t0, t1);
+            tcg_gen_trunc_i64_i32(cpu_R[ra], t0);
+            tcg_gen_sub_tl(maclo, maclo, cpu_R[ra]);
+            tcg_gen_sub_tl(machi, machi, cpu_R[ra]);
+            tcg_temp_free(t0);
+            tcg_temp_free(t1);
+        }
+        break;
+
+    default:
+        break;
+   }
+}
+
+static void dec_logic(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t rd, ra, L6;
+    op0 = field(insn, 6, 2);
+    rd = field(insn, 21, 5);
+    ra = field(insn, 16, 5);
+    L6 = field(insn, 0, 6);
+
+    switch (op0) {
+    case 0x00:    /*l.slli*/
+        LOG_DIS("l.slli r%d, r%d, %d\n", rd, ra, L6);
+        tcg_gen_shli_tl(cpu_R[rd], cpu_R[ra], (L6 & 0x1f));
+        break;
+
+    case 0x01:    /*l.srli*/
+        LOG_DIS("l.srli r%d, r%d, %d\n", rd, ra, L6);
+        tcg_gen_shri_tl(cpu_R[rd], cpu_R[ra], (L6 & 0x1f));
+        break;
+
+    case 0x02:    /*l.srai*/
+        LOG_DIS("l.srai r%d, r%d, %d\n", rd, ra, L6);
+        tcg_gen_sari_tl(cpu_R[rd], cpu_R[ra], (L6 & 0x1f)); break;
+
+    case 0x03:    /*l.rori*/
+        LOG_DIS("l.rori r%d, r%d, %d\n", rd, ra, L6);
+        tcg_gen_rotri_tl(cpu_R[rd], cpu_R[ra], (L6 & 0x1f));
+        break;
+
+    default:
+        break;
+    }
+}
+
+static void dec_M(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t rd;
+    uint32_t K16;
+    op0 = field(insn, 16, 1);
+    rd = field(insn, 21, 5);
+    K16 = field(insn, 0, 16);
+
+    switch (op0) {
+    case 0x0:    /*l.movhi*/
+        LOG_DIS("l.movhi  r%d, %d\n", rd, K16);
+        tcg_gen_movi_tl(cpu_R[rd], (K16 << 16));
+        break;
+
+    case 0x1:    /*l.macrc*/
+        LOG_DIS("l.macrc  r%d\n", rd);
+        tcg_gen_mov_tl(cpu_R[rd], maclo);
+        tcg_gen_movi_tl(maclo, 0x0);
+        tcg_gen_movi_tl(machi, 0x0);
+        break;
+
+    default:
+        break;
+    }
+}
+
+static void dec_comp(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t ra, rb;
+
+    op0 = field(insn, 21, 5);
+    ra = field(insn, 16, 5);
+    rb = field(insn, 11, 5);
+
+    tcg_gen_movi_i32(env_btaken, 0x0);
+    /* unsigned integers  */
+    tcg_gen_ext32u_tl(cpu_R[ra], cpu_R[ra]);
+    tcg_gen_ext32u_tl(cpu_R[rb], cpu_R[rb]);
+
+    switch (op0) {
+    case 0x0:    /*l.sfeq*/
+        LOG_DIS("l.sfeq  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_EQ, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x1:    /*l.sfne*/
+        LOG_DIS("l.sfne  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_NE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x2:    /*l.sfgtu*/
+        LOG_DIS("l.sfgtu  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_GTU, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x3:    /*l.sfgeu*/
+        LOG_DIS("l.sfgeu  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_GEU, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x4:    /*l.sfltu*/
+        LOG_DIS("l.sfltu  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_LTU, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x5:    /*l.sfleu*/
+        LOG_DIS("l.sfleu  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_LEU, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0xa:    /*l.sfgts*/
+        LOG_DIS("l.sfgts  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_GT, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0xb:    /*l.sfges*/
+        LOG_DIS("l.sfges  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_GE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0xc:    /*l.sflts*/
+        LOG_DIS("l.sflts  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_LT, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0xd:    /*l.sfles*/
+        LOG_DIS("l.sfles  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_LE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    default:
+        break;
+    }
+    wb_SR_F();
+}
+
+static void dec_compi(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t ra, I16;
+
+    op0 = field(insn, 21, 5);
+    ra = field(insn, 16, 5);
+    I16 = field(insn, 0, 16);
+
+    tcg_gen_movi_i32(env_btaken, 0x0);
+    tcg_gen_ext32u_tl(cpu_R[ra], cpu_R[ra]);
+    I16 = sign_extend(I16, 16);
+
+    switch (op0) {
+    case 0x0:    /*l.sfeqi*/
+        LOG_DIS("l.sfeqi  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_EQ, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0x1:    /*l.sfnei*/
+        LOG_DIS("l.sfnei  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_NE, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0x2:    /*l.sfgtui*/
+        LOG_DIS("l.sfgtui  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_GTU, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0x3:    /*l.sfgeui*/
+        LOG_DIS("l.sfgeui  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_GEU, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0x4:    /*l.sfltui*/
+        LOG_DIS("l.sfltui  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_LTU, env_btaken, cpu_R[ra],
+                            sign_extend(I16, 16));
+        break;
+
+    case 0x5:    /*l.sfleui*/
+        LOG_DIS("l.sfleui  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_LEU, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0xa:    /*l.sfgtsi*/
+        LOG_DIS("l.sfgtsi  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_GT, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0xb:    /*l.sfgesi*/
+        LOG_DIS("l.sfgesi  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_GE, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0xc:    /*l.sfltsi*/
+        LOG_DIS("l.sfltsi  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_LT, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0xd:    /*l.sflesi*/
+        LOG_DIS("l.sflesi  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_LE, env_btaken, cpu_R[ra], I16);
+        break;
+
+    default:
+        break;
+    }
+    wb_SR_F();
+}
+
+static void dec_sys(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    /*uint32_t K16;*/
+    op0 = field(insn, 16, 8);
+    /*K16 = field(insn, 0, 16);*/
+
+    switch (op0) {
+    case 0x000:  /*l.sys*/
+        /*LOG_DIS("l.sys %d\n", K16);*/
+        tcg_gen_movi_tl(cpu_pc, dc->pc);
+        /* exception here */
+        dc->is_jmp = DISAS_UPDATE;
+        break;
+
+    case 0x100:  /*l.trap*/
+        /*LOG_DIS("l.trap %d\n", K16);*/
+        tcg_gen_movi_tl(cpu_pc, dc->pc);
+        /* exception here */
+        break;
+
+    case 0x300:  /*l.csync*/
+        LOG_DIS("l.csync\n");
+        break;
+
+    case 0x200:  /*l.msync*/
+        LOG_DIS("l.msync\n");
+        break;
+
+    case 0x270:  /*l.psync*/
+        LOG_DIS("l.psync\n");
+        break;
+
+    default:
+        break;
+    }
+}
+
+static void dec_float(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t ra, rb, rd;
+    op0 = field(insn, 0, 8);
+    ra = field(insn, 16, 5);
+    rb = field(insn, 11, 5);
+    rd = field(insn, 21, 5);
+
+    switch (op0) {
+    case 0x10:    /*lf.add.d*/
+        LOG_DIS("lf.add.d r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_add_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x00:    /*lf.add.s*/
+        LOG_DIS("lf.add.s r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_add_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x11:    /*lf.sub.d*/
+        LOG_DIS("lf.sub.d r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_sub_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x01:    /*lf.sub.s*/
+        LOG_DIS("lf.sub.s r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_sub_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x12:    /*lf.mul.d*/
+        LOG_DIS("lf.mul.d r%d, r%d, r%d\n", rd, ra, rb);
+        {
+            if (cpu_R[ra] != 0 && cpu_R[rb] != 0) {
+                tcg_gen_mul_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            } else {
+                tcg_gen_movi_i64(cpu_R[rd], 0x0);
+            }
+        }
+        break;
+
+    case 0x02:    /*lf.mul.s*/
+        LOG_DIS("lf.mul.s r%d, r%d, r%d\n", rd, ra, rb);
+        if (cpu_R[ra] != 0 && cpu_R[rb] != 0) {
+            tcg_gen_mul_i32(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        } else {
+            tcg_gen_movi_i64(cpu_R[rd], 0x0);
+        }
+        break;
+
+    case 0x13:    /*lf.div.d*/
+        LOG_DIS("lf.div.d r%d, r%d, r%d\n", rd, ra, rb);
+        if (cpu_R[rb] != 0) {
+            tcg_gen_div_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        } else {
+            /* exception here */
+        }
+        break;
+
+    case 0x03:    /*lf.div.s*/
+        LOG_DIS("lf.div.s r%d, r%d, r%d\n", rd, ra, rb);
+        if (cpu_R[rb] != 0) {
+            tcg_gen_div_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        } else {
+            /* exception here */
+        }
+        break;
+
+    case 0x14:    /*lf.itof.d*/
+        LOG_DIS("lf.itof r%d, r%d\n", rd, ra);
+        /* itof.d need a helper here */
+        break;
+
+    case 0x04:    /*lf.itof.s*/
+        LOG_DIS("lf.itof r%d, r%d\n", rd, ra);
+        /* itof.s need a helper here */
+        break;
+
+    case 0x15:    /*lf.ftoi.d*/
+        LOG_DIS("lf.ftoi r%d, r%d\n", rd, ra);
+        /* ftoi.d need a helper here */
+        break;
+
+    case 0x05:    /*lf.ftoi.s*/
+        LOG_DIS("lf.ftoi r%d, r%d\n", rd, ra);
+        /* ftoi.s need a helper here */
+        break;
+
+    case 0x16:    /*lf.rem.d*/
+        LOG_DIS("lf.rem.d r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_rem_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x06:    /*lf.rem.s*/
+        LOG_DIS("lf.rem.s r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_rem_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x17:    /*lf.madd.d*/
+        LOG_DIS("lf.madd.d r%d, r%d, r%d\n", rd, ra, rb);
+        {
+            TCGv_i64 t0, t1;
+
+            t0 = tcg_temp_new_i64();
+            t1 = tcg_temp_new_i64();
+
+            tcg_gen_ext_i32_i64(t0, cpu_R[ra]);
+            tcg_gen_ext_i32_i64(t1, cpu_R[rb]);
+            tcg_gen_mul_i64(t0, t0, t1);
+
+            tcg_gen_trunc_i64_i32(cpu_R[ra], t0);
+            tcg_gen_add_tl(fpmaddlo, cpu_R[ra], fpmaddlo);
+            tcg_gen_shri_i64(t0, t0, 32);
+            tcg_gen_trunc_i64_i32(cpu_R[rb], t0);
+            tcg_gen_add_tl(fpmaddhi, cpu_R[rb], fpmaddhi);
+
+            tcg_temp_free_i64(t0);
+            tcg_temp_free_i64(t1);
+        }
+        break;
+
+    case 0x07:    /*lf.madd.s*/
+        LOG_DIS("lf.madd.s r%d, r%d, r%d\n", rd, ra, rb);
+        {
+            TCGv_i64 t0, t1;
+
+            t0 = tcg_temp_new_i64();
+            t1 = tcg_temp_new_i64();
+
+            tcg_gen_ext_i32_i64(t0, cpu_R[ra]);
+            tcg_gen_ext_i32_i64(t1, cpu_R[rb]);
+            tcg_gen_mul_i64(t0, t0, t1);
+
+            tcg_gen_trunc_i64_i32(cpu_R[ra], t0);
+            tcg_gen_add_tl(fpmaddlo, cpu_R[ra], fpmaddlo);
+            tcg_gen_shri_i64(t0, t0, 32);
+            tcg_gen_trunc_i64_i32(cpu_R[rb], t0);
+            tcg_gen_add_tl(fpmaddhi, cpu_R[rb], fpmaddhi);
+            tcg_temp_free_i64(t0);
+            tcg_temp_free_i64(t1);
+
+        }
+        break;
+
+    case 0x18:    /*lf.sfeq.d*/
+        LOG_DIS("lf.sfeq.d r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_i64(TCG_COND_GE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x08:    /*lf.sfeq.s*/
+        LOG_DIS("lf.sfeq.s r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_EQ, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x19:    /*lf.sfne.d*/
+        LOG_DIS("lf.sfne.d r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_i64(TCG_COND_NE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x09:    /*lf.sfne.s*/
+        LOG_DIS("lf.sfne.s r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_NE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x1a:    /*lf.sfgt.d*/
+        LOG_DIS("lf.sfgt.d r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_i64(TCG_COND_GT, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x0a:    /*lf.sfgt.s*/
+        LOG_DIS("lf.sfgt.s r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_GT, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x1b:    /*lf.sfge.d*/
+        LOG_DIS("lf.sfge.d r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_i64(TCG_COND_GE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x0b:    /*lf.sfge.s*/
+        LOG_DIS("lf.sfge.s r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_GE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x1c:    /*lf.sflt.d*/
+        LOG_DIS("lf.sflt.d r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_i64(TCG_COND_LT, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x0c:    /*lf.sflt.s*/
+        LOG_DIS("lf.sflt.s r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_LT, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x1d:    /*lf.sfle.d*/
+        LOG_DIS("lf.sfle.d r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_i64(TCG_COND_LE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x0d:    /*lf.sfle.s*/
+        LOG_DIS("lf.sfle.s r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_LE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    default:
+        break;
+    }
+    wb_SR_F();
+}
+
+static void disas_openrisc_insn(DisasContext *dc, CPUOPENRISCState *env)
+{
+    uint32_t op0;
+    uint32_t insn;
+    insn = ldl_code(dc->pc);
+    op0 = field(insn, 26, 6);
+
+    switch (op0) {
+    case 0x06:
+        dec_M(dc, env, insn);
+        break;
+
+    case 0x08:
+        dec_sys(dc, env, insn);
+        break;
+
+    case 0x2e:
+        dec_logic(dc, env, insn);
+        break;
+
+    case 0x2f:
+        dec_compi(dc, env, insn);
+        break;
+
+    case 0x31:
+        dec_mac(dc, env, insn);
+        break;
+
+    case 0x32:
+        dec_float(dc, env, insn);
+        break;
+
+    case 0x38:
+        dec_calc(dc, env, insn);
+        break;
+
+    case 0x39:
+        dec_comp(dc, env, insn);
+        break;
+
+    default:
+        dec_misc(dc, env, insn);
+        break;
+    }
+}
+
+static void check_breakpoint(CPUOPENRISCState *env, DisasContext *dc)
+{
+    CPUBreakpoint *bp;
+
+    if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
+        QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
+            if (bp->pc == dc->pc) {
+                tcg_gen_movi_tl(cpu_pc, dc->pc);
+                /* exception here */
+                dc->is_jmp = DISAS_UPDATE;
+            }
+        }
+    }
  }
 
  static inline void gen_intermediate_code_internal(CPUOPENRISCState *env,
                                                   TranslationBlock *tb,
                                                   int search_pc)
  {
+    struct DisasContext ctx, *dc = &ctx;
+    uint16_t *gen_opc_end;
+    uint32_t pc_start;
+    int j, lj;
+    uint32_t next_page_start;
+    int num_insns;
+    int max_insns;
+
+    qemu_log_try_set_file(stderr);
+
+    pc_start = tb->pc;
+    dc->env = env;
+    dc->tb = tb;
+
+    gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
+    dc->is_jmp = DISAS_NEXT;
+    dc->ppc = pc_start;
+    dc->pc = pc_start;
+    dc->mem_idx = cpu_mmu_index(env);
+    dc->tb_flags = tb->flags;
+    dc->delayed_branch = !!(dc->tb_flags & D_FLAG);
+    dc->singlestep_enabled = env->singlestep_enabled;
+    if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
+        qemu_log("-----------------------------------------\n");
+        log_cpu_state(env, 0);
+    }
+
+    next_page_start = (pc_start & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
+    lj = -1;
+    num_insns = 0;
+    max_insns = tb->cflags & CF_COUNT_MASK;
+
+    if (max_insns == 0) {
+        max_insns = CF_COUNT_MASK;
+    }
+
+    gen_icount_start();
+
+    do {
+        check_breakpoint(env, dc);
+        if (search_pc) {
+            j = gen_opc_ptr - gen_opc_buf;
+            if (lj < j) {
+                lj++;
+                while (lj < j) {
+                    gen_opc_instr_start[lj++] = 0;
+                }
+            }
+            gen_opc_pc[lj] = dc->pc;
+            gen_opc_instr_start[lj] = 1;
+            gen_opc_icount[lj] = num_insns;
+        }
+
+        if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP))) {
+            tcg_gen_debug_insn_start(dc->pc);
+        }
+
+        if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO)) {
+            gen_io_start();
+        }
Quoted 9 lines

target-openrisc/translate.c typedef struct DisasContext { · line 1373

Quoted 6 lines
target-openrisc/translate.c +6 -0
+        dc->ppc = dc->pc - 4;
+        dc->npc = dc->pc + 4;
+        tcg_gen_movi_tl(cpu_ppc, dc->ppc);
+        tcg_gen_movi_tl(cpu_npc, dc->npc);
+        disas_openrisc_insn(dc, env);
+        dc->pc = dc->npc;

Isn't now npc == pc and ppc == pc - 8?

Quoted 1 line

I don't think so, but I'll check it.

OK, I'm just commenting based on my Sparc knowledge which also has PC and NPC with at least similar names. There NPC should be PC + 4 unless there is a control transfer instruction.

This reminds me that I forgot to ask to put a link to CPU documentation in a comment somewhere.

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
target-openrisc/translate.c +7 -0
+        num_insns++;
+        /* delay slot */
+        if (dc->delayed_branch) {
+            dc->delayed_branch--;
+            if (!dc->delayed_branch) {
+                dc->tb_flags &= ~D_FLAG;
+                tcg_gen_movi_i32(env_flags, dc->tb_flags);
Quoted 4 lines
Quoted 2 lines

This does not look correct.

for exception in delay slot, it is.

The TB flags are very different from CPU flags (NZVC etc.), isn't that what env_flags is for?

Quoted 102 lines
Quoted 6 lines

target-openrisc/translate.c typedef struct DisasContext { · line 1382

Quoted 3 lines
target-openrisc/translate.c +2 -0
+                tcg_gen_mov_tl(cpu_pc, jmp_pc);
+                tcg_gen_mov_tl(cpu_npc, jmp_pc);

+ 4?

Quoted 9 lines

target-openrisc/translate.c typedef struct DisasContext { · line 1387

Quoted 5 lines
target-openrisc/translate.c +5 -0
+                tcg_gen_movi_tl(jmp_pc, 0);
+                tcg_gen_exit_tb(0);
+                dc->is_jmp = DISAS_JUMP;
+                break;
+            }

Is it possible to have a jump instruction in the delay slot and if so, is it handled too?

I've done for this.

Quoted 75 lines
Quoted 75 lines
target-openrisc/translate.c +65 -0
+        }
+    } while (!dc->is_jmp && gen_opc_ptr < gen_opc_end
+             && !env->singlestep_enabled
+             && !singlestep
+             && (dc->pc < next_page_start)
+             && num_insns < max_insns);
+
+    if (tb->cflags & CF_LAST_IO) {
+        gen_io_end();
+    }
+    if (dc->is_jmp == DISAS_NEXT) {
+        dc->is_jmp = DISAS_UPDATE;
+        tcg_gen_movi_tl(cpu_pc, dc->pc);
+    }
+    if (unlikely(env->singlestep_enabled)) {
+        if (dc->is_jmp == DISAS_NEXT) {
+            tcg_gen_movi_tl(cpu_pc, dc->pc);
+        }
+        /* exception here*/
+    } else {
+        switch (dc->is_jmp) {
+        case DISAS_NEXT:
+            gen_goto_tb(dc, 0, dc->pc);
+            break;
+        default:
+        case DISAS_JUMP:
+            break;
+        case DISAS_UPDATE:
+            /* indicate that the hash table must be used
+               to find the next TB */
+            tcg_gen_exit_tb(0);
+            break;
+        case DISAS_TB_JUMP:
+            /* nothing more to generate */
+            break;
+        }
+    }
+
+    gen_icount_end(tb, num_insns);
+    *gen_opc_ptr = INDEX_op_end;
+    if (search_pc) {
+        j = gen_opc_ptr - gen_opc_buf;
+        lj++;
+        while (lj <= j) {
+            gen_opc_instr_start[lj++] = 0;
+        }
+    } else {
+        tb->size = dc->pc - pc_start;
+        tb->icount = num_insns;
+    }
+
+#ifdef DEBUG_DISAS
+    if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
+        qemu_log("\n");
+        log_target_disas(pc_start, dc->pc - pc_start, 0);
+        qemu_log("\nisize=%d osize=%td\n",
+            dc->pc - pc_start, gen_opc_ptr - gen_opc_buf);
+    }
+#endif
  }
 
  void gen_intermediate_code(CPUOPENRISCState *env, struct TranslationBlock *tb)
@@ -80,7 +1461,13 @@ void cpu_dump_state(CPUOPENRISCState *env, FILE *f,
                     fprintf_function cpu_fprintf,
                     int flags)
  {
+    int i;
+    uint32_t *regs = env->gpr;
     cpu_fprintf(f, "PC=%08x\n", env->pc);
+    for (i = 0; i < 32; ++i) {
+        cpu_fprintf(f, "R%02d=%08x%c", i, regs[i],
+                    (i % 4) == 3 ? '\n' : ' ');
+    }
  }
 
Quoted 5 lines
Quoted 5 lines
target-openrisc/translate.c +0 -1
  void restore_state_to_opc(CPUOPENRISCState *env, TranslationBlock *tb,
--

1.7.9.5

Jia,
Regards.

author
#27 lore raw

Re: [Qemu-devel] [PATCH 03/15] Openrisc: add instructions translation

Quoted 1,390 lines · Blue Swirl
Quoted 272 lines · Jia Liu

Thanks Blue.

Quoted 111 lines · Blue Swirl
Quoted 110 lines · Jia Liu

add the openrisc instructions translation.

---
 target-openrisc/translate.c | 1387 +++++++++++++++++++++++++++++++++++++++++++

 1 file changed, 1387 insertions(+)
target-openrisc/translate.c +93 -0
index aae483a..4828ae6 100644
@@ -55,14 +55,1395 @@ typedef struct DisasContext {
     uint32_t delayed_branch;
  } DisasContext;
 
+static TCGv_ptr cpu_env;
+static TCGv cpu_sr;
+static TCGv cpu_R[32];
+static TCGv cpu_pc;
+static TCGv jmp_pc;        /* l.jr/l.jalr temp pc */
+static TCGv cpu_npc;
+static TCGv cpu_ppc;
+static TCGv_i32 env_btaken;    /* bf/bnf , F flag taken */
+static TCGv machi, maclo;
+static TCGv fpmaddhi, fpmaddlo;
+static TCGv_i32 env_flags;
+#include "gen-icount.h"
+
  void openrisc_translate_init(void)
  {
+    static const char * const regnames[] = {
+        "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
+        "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
+        "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
+        "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
+    };
+    int i;
+
+    cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
+    cpu_sr = tcg_global_mem_new(TCG_AREG0,
+                                offsetof(CPUOPENRISCState, sr), "sr");
+    env_flags = tcg_global_mem_new_i32(TCG_AREG0,
+                                       offsetof(CPUOPENRISCState, flags),
+                                       "flags");
+    cpu_pc = tcg_global_mem_new(TCG_AREG0,
+                                offsetof(CPUOPENRISCState, pc), "pc");
+    cpu_npc = tcg_global_mem_new(TCG_AREG0,
+                                 offsetof(CPUOPENRISCState, npc), "npc");
+    cpu_ppc = tcg_global_mem_new(TCG_AREG0,
+                                 offsetof(CPUOPENRISCState, ppc), "ppc");
+    jmp_pc = tcg_global_mem_new(TCG_AREG0,
+                                offsetof(CPUOPENRISCState, jmp_pc), "jmp_pc");
+    env_btaken = tcg_global_mem_new_i32(TCG_AREG0,
+                                        offsetof(CPUOPENRISCState, btaken),
+                                        "btaken");
+    machi = tcg_global_mem_new(TCG_AREG0,
+                               offsetof(CPUOPENRISCState, machi),
+                               "machi");
+    maclo = tcg_global_mem_new(TCG_AREG0,
+                               offsetof(CPUOPENRISCState, maclo),
+                               "maclo");
+    fpmaddhi = tcg_global_mem_new(TCG_AREG0,
+                                  offsetof(CPUOPENRISCState, fpmaddhi),
+                                  "fpmaddhi");
+    fpmaddlo = tcg_global_mem_new(TCG_AREG0,
+                                  offsetof(CPUOPENRISCState, fpmaddlo),
+                                  "fpmaddlo");
+    for (i = 0; i < 32; i++) {
+        cpu_R[i] = tcg_global_mem_new(TCG_AREG0,
+                                      offsetof(CPUOPENRISCState, gpr[i]),
+                                      regnames[i]);
+    }
+#define GEN_HELPER 2
+#include "helper.h"
+}
+
+/* Writeback SR_F transaltion-space to execution-space.  */
+static inline void wb_SR_F(void)
+{
+    int label;
+
+    label = gen_new_label();
+    tcg_gen_andi_tl(cpu_sr, cpu_sr, ~SR_F);
+    tcg_gen_brcondi_tl(TCG_COND_EQ, env_btaken, 0, label);
+    tcg_gen_ori_tl(cpu_sr, cpu_sr, SR_F);
+    gen_set_label(label);
+}
+
+static inline int zero_extend(unsigned int val, int width)
+{
+    return val & ((1 << width) - 1);
+}
+
+static inline int sign_extend(unsigned int val, int width)
+{
+    int sval;
+
+    /* LSL.  */
+    val <<= 32 - width;
+    sval = val;
+    /* ASR.  */
+    sval >>= 32 - width;
+    return sval;
+}
+
+/* General purpose registers moves. */
+static inline void gen_load_gpr(TCGv t, unsigned int reg)
+{
+    if (reg == 0) {
+        tcg_gen_movi_tl(t, 0);
Quoted 9 lines

target-openrisc/translate.c typedef struct DisasContext { · line 156

Quoted 4 lines
target-openrisc/translate.c +4 -0
+    } else {
+        tcg_gen_mov_tl(t, cpu_R[reg]);
+    }
+}

This does not seem to be used. Does the code handle the cases where one or several of the registers is number 0? If this is the always-0 register, are stores to register 0 handled correctly?

I'll remove it.

Quoted 65 lines
Quoted 15 lines
target-openrisc/translate.c +15 -0
+
+static void gen_goto_tb(DisasContext *dc, int n, target_ulong dest)
+{
+    TranslationBlock *tb;
+    tb = dc->tb;
+    if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) &&
+                                       likely(!dc->singlestep_enabled)) {
+        tcg_gen_movi_tl(cpu_pc, dest);
+        tcg_gen_goto_tb(n);
+        tcg_gen_exit_tb((tcg_target_long)tb + n);
+    } else {
+        tcg_gen_movi_tl(cpu_pc, dest);
+        if (dc->singlestep_enabled) {
+            /* exception here */
+        }

target-openrisc/translate.c typedef struct DisasContext { · line 181

Quoted 10 lines
target-openrisc/translate.c +10 -0
+        tcg_gen_exit_tb(0);
+    }
+}
+
+static inline uint32_t field(uint32_t val, int start, int length)
+{
+    val >>= start;
+    val &= ~(~0 << length);
+    return val;
+}

Nice function, maybe I could use it in Sparc as well if generalized (later).

Quoted 37 lines
target-openrisc/translate.c +37 -0
+
+static void dec_calc(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0, op1, op2;
+    uint32_t ra, rb, rd;
+    op0 = field(insn, 0, 4);
+    op1 = field(insn, 8, 2);
+    op2 = field(insn, 6, 2);
+    ra = field(insn, 16, 5);
+    rb = field(insn, 11, 5);
+    rd = field(insn, 21, 5);
+
+    switch (op0) {
+    case 0x0000:
+        switch (op1) {
+        case 0x00:     /*l.add*/
+            LOG_DIS("l.add r%d, r%d, r%d\n", rd, ra, rb);
+            /* Aha, we do need a helper here for add */
+            break;
+        default:
+            break;
+        }
+    break;
+
+    case 0x0001:    /*l.addc*/
+        switch (op1) {
+        case 0x00:
+            LOG_DIS("l.addc r%d, r%d, r%d\n", rd, ra, rb);
+            /* Aha, we do need a helper here for addc */
+            break;
+        default:
+            break;
+        }
+    break;
+
+    case 0x0002:    /*l.sub*/
+        switch (op1) {
Quoted 9 lines

target-openrisc/translate.c typedef struct DisasContext { · line 223

Quoted 5 lines
target-openrisc/translate.c +5 -0
+        case 0x00:
+            LOG_DIS("l.sub r%d, r%d, r%d\n", rd, ra, rb);
+            /* Aha, we do need a helper here for sub */
+            break;
+        default:

What happens on a real CPU when an illegal instruction is executed, shouldn't there be an exception?

I'll add a exception for illegal instruction.

Quoted 69 lines
Quoted 38 lines
target-openrisc/translate.c +38 -0
+            break;
+        }
+    break;
+
+    case 0x0003:   /*l.and*/
+        switch (op1) {
+        case 0x00:
+            LOG_DIS("l.and r%d, r%d, r%d\n", rd, ra, rb);
+            tcg_gen_and_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            break;
+        default:
+            break;
+        }
+    break;
+
+    case 0x0004:   /*l.or*/
+        switch (op1) {
+        case 0x00:
+            LOG_DIS("l.or r%d, r%d, r%d\n", rd, ra, rb);
+            tcg_gen_or_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            break;
+        default:
+            break;
+        }
+    break;
+
+    case 0x0005:
+        switch (op1) {
+        case 0x00:   /*l.xor*/
+            LOG_DIS("l.xor r%d, r%d, r%d\n", rd, ra, rb);
+            tcg_gen_xor_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            break;
+        default:
+            break;
+        }
+    break;
+
+    case 0x0006:

target-openrisc/translate.c typedef struct DisasContext { · line 271

Quoted 10 lines
target-openrisc/translate.c +10 -0
+        switch (op1) {
+        case 0x03:   /*l.mul*/
+            LOG_DIS("l.mul r%d, r%d, r%d\n", rd, ra, rb);
+            if (ra != 0 && rb != 0) {
+                tcg_gen_mul_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+                tcg_gen_ext32s_tl(cpu_R[rd], cpu_R[rd]);
+            } else {
+                tcg_gen_movi_tl(cpu_R[rd], 0x0);
+            }
+        break;

Indentation.

Quoted 18 lines
target-openrisc/translate.c +18 -0
+        default:
+            break;
+        }
+    break;
+
+    case 0x0009:
+        switch (op1) {
+        case 0x03:   /*l.div*/
+            LOG_DIS("l.div r%d, r%d, r%d\n", rd, ra, rb);
+            if (rb != 0) {
+                tcg_gen_div_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            } else {
+                /* exception here */
+            }
+            break;
+        default:
+            break;
+        }
Quoted 8 lines
Quoted 8 lines

target-openrisc/translate.c typedef struct DisasContext { · line 297

Quoted 8 lines
target-openrisc/translate.c +8 -0
+        break;
+
+    case 0x000a:
+        switch (op1) {
+        case 0x03:   /*l.divu*/
+            LOG_DIS("l.divu r%d, r%d, r%d\n", rd, ra, rb);
+            if (rb != 0) {
+                tcg_gen_divu_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);

By the way, a division by zero will crash QEMU. Please add a runtime check, something like this:
http://git.qemu.org/?p=qemu.git;a=blob;f=target-sparc/translate.c;h=b95f91cd3baf9c02305f8583662b8eba664b17ba;hb=HEAD#l765

Quoted 1,086 lines
Quoted 253 lines
Quoted 253 lines
target-openrisc/translate.c +253 -0
+            } else {
+                /* exception here */
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000b:
+        switch (op1) {
+        case 0x03:   /*l.mulu*/
+            LOG_DIS("l.mulu r%d, r%d, r%d\n", rd, ra, rb);
+            if (rb != 0 && ra != 0) {
+                tcg_gen_ext32u_tl(cpu_R[ra], cpu_R[ra]);
+                tcg_gen_ext32u_tl(cpu_R[rb], cpu_R[rb]);
+                tcg_gen_mul_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            } else {
+                tcg_gen_movi_tl(cpu_R[rd], 0);
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000e:
+        switch (op1) {
+        case 0x00:   /*l.cmov*/
+            LOG_DIS("l.cmov r%d, r%d, r%d\n", rd, ra, rb);
+            {
+                int lab = gen_new_label();
+                TCGv sr_f = tcg_temp_new();
+                tcg_gen_andi_tl(sr_f, cpu_sr, SR_F);
+                tcg_gen_mov_tl(cpu_R[rd], cpu_R[rb]);
+                tcg_gen_brcondi_tl(TCG_COND_NE, sr_f, SR_F, lab);
+                tcg_gen_mov_tl(cpu_R[rd], cpu_R[ra]);
+                gen_set_label(lab);
+                tcg_temp_free(sr_f);
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000f:
+        switch (op1) {
+        case 0x00:   /*l.ff1*/
+            LOG_DIS("l.ff1 r%d, r%d, r%d\n", rd, ra, rb);
+            /* ff1 need a helper here */
+            break;
+        case 0x01:   /*l.fl1*/
+            LOG_DIS("l.fl1 r%d, r%d, r%d\n", rd, ra, rb);
+            /* fl1 need a helper here */
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x0008:
+        switch (op1) {
+        case 0x00:
+            switch (op2) {
+            case 0x00:   /*l.sll*/
+                LOG_DIS("l.sll r%d, r%d, r%d\n", rd, ra, rb);
+                tcg_gen_shl_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+                break;
+            case 0x01:   /*l.srl*/
+                LOG_DIS("l.srl r%d, r%d, r%d\n", rd, ra, rb);
+                tcg_gen_shr_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+                break;
+            case 0x02:   /*l.sra*/
+                LOG_DIS("l.sra r%d, r%d, r%d\n", rd, ra, rb);
+                tcg_gen_sar_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+                break;
+            case 0x03:   /*l.ror*/
+                LOG_DIS("l.ror r%d, r%d, r%d\n", rd, ra, rb);
+                tcg_gen_rotr_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+                break;
+            default:
+                break;
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000c:
+        switch (op1) {
+        case 0x00:
+            switch (op2) {
+            case 0x00:   /*l.exths*/
+                LOG_DIS("l.exths r%d, r%d\n", rd, ra);
+                tcg_gen_ext16s_tl(cpu_R[rd], cpu_R[ra]);
+                break;
+            case 0x01:   /*l.extbs*/
+                LOG_DIS("l.extbs r%d, r%d\n", rd, ra);
+                tcg_gen_ext8s_tl(cpu_R[rd], cpu_R[ra]);
+                break;
+            case 0x02:   /*l.exthz*/
+                LOG_DIS("l.exthz r%d, r%d\n", rd, ra);
+                tcg_gen_ext16u_tl(cpu_R[rd], cpu_R[ra]);
+                break;
+            case 0x03:   /*l.extbz*/
+                LOG_DIS("l.extbz r%d, r%d\n", rd, ra);
+                tcg_gen_ext8u_tl(cpu_R[rd], cpu_R[ra]);
+                break;
+            default:
+                break;
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000d:
+        switch (op1) {
+        case 0x00:
+            switch (op2) {
+            case 0x00:   /*l.extws*/
+                LOG_DIS("l.extws r%d, r%d\n", rd, ra);
+                tcg_gen_ext32s_tl(cpu_R[rd], cpu_R[ra]);
+                break;
+            case 0x01:    /*l.extwz*/
+                LOG_DIS("l.extwz r%d, r%d\n", rd, ra);
+                tcg_gen_ext32u_tl(cpu_R[rd], cpu_R[ra]);
+                break;
+            default:
+                break;
+            }
+        default:
+            break;
+        }
+        break;
+
+    default:
+        break;
+    }
+}
+
+static void dec_misc(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0, op1;
+    uint32_t ra, rb, rd;
+    uint32_t /*L6, K5, */I16, I5, I11, N26, tmp;
+    op0 = field(insn, 26, 6);
+    op1 = field(insn, 24, 2);
+    ra = field(insn, 16, 5);
+    rb = field(insn, 11, 5);
+    rd = field(insn, 21, 5);
+    /*L6 = field(insn, 5, 6);
+    K5 = field(insn, 0, 5);*/
+    I16 = field(insn, 0, 16);
+    I5 = field(insn, 21, 5);
+    I11 = field(insn, 0, 11);
+    N26 = field(insn, 0, 26);
+    tmp = (I5<<11) + I11;
+    TCGv t0 = tcg_temp_new();
+
+    switch (op0) {
+    case 0x00:   /*l.j*/
+        {
+            LOG_DIS("l.j %d\n", N26);
+            tmp = sign_extend((N26<<2), 26) + dc->pc;
+            tcg_gen_movi_tl(jmp_pc, tmp);
+            dc->delayed_branch = 2;
+            dc->tb_flags |= D_FLAG;
+            tcg_gen_movi_i32(env_flags, dc->tb_flags);
+        }
+        break;
+
+    case 0x01:   /*l.jal*/
+        {
+            LOG_DIS("l.jal %d\n", N26);
+            tcg_gen_movi_tl(cpu_R[9], (dc->pc + 8));
+            tmp = sign_extend((N26<<2), 26) + dc->pc;
+            tcg_gen_movi_tl(jmp_pc, tmp);
+            dc->delayed_branch = 2;
+            dc->tb_flags |= D_FLAG;
+            tcg_gen_movi_i32(env_flags, dc->tb_flags);
+        }
+        break;
+
+    case 0x03:   /*l.bnf*/
+        LOG_DIS("l.bnf %d\n", N26);
+        {
+            int lab = gen_new_label();
+            TCGv sr_f = tcg_temp_new();
+            tcg_gen_andi_tl(sr_f, cpu_sr, SR_F);
+            tmp = sign_extend((N26 << 2), 26) + dc->pc;
+            tcg_gen_movi_tl(jmp_pc, dc->pc+8);
+            tcg_gen_brcondi_tl(TCG_COND_EQ, sr_f, SR_F, lab);
+            tcg_gen_movi_tl(jmp_pc, tmp);
+            gen_set_label(lab);
+            dc->delayed_branch = 2;
+            dc->tb_flags |= D_FLAG;
+            tcg_gen_movi_i32(env_flags, dc->tb_flags);
+            tcg_temp_free(sr_f);
+        }
+        break;
+
+    case 0x04:   /*l.bf*/
+        LOG_DIS("l.bf %d\n", N26);
+        {
+            int lab = gen_new_label();
+            TCGv sr_f = tcg_temp_new();
+            tcg_gen_andi_tl(sr_f, cpu_sr, SR_F);
+            tmp = sign_extend((N26 << 2), 26) + dc->pc;
+            tcg_gen_movi_tl(jmp_pc, dc->pc+8);
+            tcg_gen_brcondi_tl(TCG_COND_NE, sr_f, SR_F, lab);
+            tcg_gen_movi_tl(jmp_pc, tmp);
+            gen_set_label(lab);
+            dc->delayed_branch = 2;
+            dc->tb_flags |= D_FLAG;
+            tcg_gen_movi_i32(env_flags, dc->tb_flags);
+            tcg_temp_free(sr_f);
+        }
+        break;
+
+    case 0x05:
+        switch (op1) {
+        case 0x01:   /*l.nop*/
+            LOG_DIS("l.nop %d\n", I16);
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x11:    /*l.jr*/
+        LOG_DIS("l.jr r%d\n", rb);
+        {
+            tcg_gen_mov_tl(jmp_pc, cpu_R[rb]);
+            dc->delayed_branch = 2;
+            dc->tb_flags |= D_FLAG;
+            tcg_gen_movi_i32(env_flags, dc->tb_flags);
+        }
+        break;
+
+    case 0x12:    /*l.jalr*/
+        LOG_DIS("l.jalr r%d\n", rb);
+        {
+            tcg_gen_movi_tl(cpu_R[9], (dc->pc + 8));
+            gen_load_gpr(jmp_pc, rb);
+            dc->delayed_branch = 2;
+            dc->tb_flags |= D_FLAG;
+            tcg_gen_movi_i32(env_flags, dc->tb_flags);
+        }
+        break;
Quoted 9 lines

target-openrisc/translate.c typedef struct DisasContext { · line 556

Quoted 6 lines
target-openrisc/translate.c +6 -0
+
+    case 0x13:    /*l.maci*/
+        {
+            LOG_DIS("l.maci %d, r%d, %d\n", I5, ra, I11);
+            TCGv_i64 tra = tcg_temp_new_i64();    /*  store cpu_R[ra]*/
+            TCGv_i64 tmac = tcg_temp_new_i64();   /*  store machi maclo*/

Variable definitions should be before code, like LOG_DIS() call in this case.

OK, thx.

Quoted 811 lines
Quoted 811 lines
target-openrisc/translate.c +805 -0
+            tcg_gen_movi_tl(t0, tmp);
+            tcg_gen_ext32s_i64(tra, cpu_R[ra]);
+            tcg_gen_ext32s_i64(tmac, t0);
+            tcg_gen_mul_tl(tmac, tra, tmac);
+            tcg_gen_trunc_i64_i32(cpu_R[ra], tmac);
+            tcg_gen_add_i64(machi, machi, cpu_R[ra]);
+            tcg_gen_add_i64(maclo, maclo, cpu_R[ra]);
+            tcg_temp_free(tra);
+            tcg_temp_free(tmac);
+        }
+        break;
+
+    case 0x09:    /*l.rfe*/
+        LOG_DIS("l.rfe\n");
+        {
+            /* rfe need a helper here */
+            dc->is_jmp = DISAS_UPDATE;
+        }
+        break;
+
+    case 0x1c:    /*l.cust1*/
+        LOG_DIS("l.cust1\n");
+        break;
+
+    case 0x1d:    /*l.cust2*/
+        LOG_DIS("l.cust2\n");
+        break;
+
+    case 0x1e:    /*l.cust3*/
+        LOG_DIS("l.cust3\n");
+        break;
+
+    case 0x1f:    /*l.cust4*/
+        LOG_DIS("l.cust4\n");
+        break;
+
+    case 0x3c:   /*l.cust5*/
+        /*LOG_DIS("l.cust5 r%d, r%d, r%d, %d, %d\n", rd, ra, rb, L6, K5);*/
+        break;
+
+    case 0x3d:   /*l.cust6*/
+        LOG_DIS("l.cust6\n");
+        break;
+
+    case 0x3e:   /*l.cust7*/
+        LOG_DIS("l.cust7\n");
+        break;
+
+    case 0x3f:   /*l.cust8*/
+        LOG_DIS("l.cust8\n");
+        break;
+
+    case 0x20:   /*l.ld*/
+        LOG_DIS("l.ld r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld64(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x21:   /*l.lwz*/
+        LOG_DIS("l.lwz r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld32u(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x22:   /*l.lws*/
+        LOG_DIS("l.lws r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld32s(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x23:   /*l.lbz*/
+        LOG_DIS("l.lbz r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld8u(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x24:   /*l.lbs*/
+        LOG_DIS("l.lbs r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld8s(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x25:   /*l.lhz*/
+        LOG_DIS("l.lhz r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld16u(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x26:   /*l.lhs*/
+        LOG_DIS("l.lhs r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld16s(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x27:   /*l.addi*/
+        LOG_DIS("l.addi r%d, r%d, %d\n", rd, ra, I16);
+        {
+            TCGv t0 = tcg_const_tl(sign_extend(I16, 16));
+            /* Aha, we do need a helper here for addi */
+            tcg_temp_free(t0);
+        }
+        break;
+
+    case 0x28:   /*l.addic*/
+        LOG_DIS("l.addic r%d, r%d, %d\n", rd, ra, I16);
+        {
+            TCGv t0 = tcg_const_tl(sign_extend(I16, 16));
+            /* Aha, we do need a helper here for addic */
+            tcg_temp_free(t0);
+        }
+        break;
+
+    case 0x29:   /*l.andi*/
+        LOG_DIS("l.andi r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_andi_tl(cpu_R[rd], cpu_R[ra], zero_extend(I16, 16));
+        break;
+
+    case 0x2a:   /*l.ori*/
+        LOG_DIS("l.ori r%d, r%d, %d\n", rd, ra, zero_extend(I16, 16));
+        tcg_gen_ori_tl(cpu_R[rd], cpu_R[ra], I16);
+        break;
+
+    case 0x2b:   /*l.xori*/
+        LOG_DIS("l.xori r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_xori_tl(cpu_R[rd], cpu_R[ra], sign_extend(I16, 16));
+        break;
+
+    case 0x2c:   /*l.muli*/
+        LOG_DIS("l.muli r%d, r%d, %d\n", rd, ra, I16);
+        if (ra != 0 && I16 != 0) {
+            tcg_gen_muli_tl(cpu_R[rd], cpu_R[ra], I16);
+            tcg_gen_ext32s_tl(cpu_R[rd], cpu_R[rd]);
+        } else {
+            tcg_gen_movi_tl(cpu_R[rd], 0);
+        }
+        break;
+
+    case 0x2d:   /*l.mfspr*/
+        LOG_DIS("l.mfspr r%d, r%d, %d\n", rd, ra, I16);
+        {
+            TCGv_i32 ti = tcg_const_i32(I16);
+            TCGv td = tcg_const_tl(rd);
+            TCGv ta = tcg_const_tl(ra);
+            /* mfspr need a helper here */
+            tcg_temp_free_i32(ti);
+            tcg_temp_free(td);
+            tcg_temp_free(ta);
+        }
+        break;
+
+    case 0x30:  /*l.mtspr*/
+        LOG_DIS("l.mtspr %d, r%d, r%d, %d\n", I5, ra, rb, I11);
+        {
+            TCGv_i32 ti = tcg_const_i32(tmp);
+            TCGv ta = tcg_const_tl(ra);
+            TCGv tb = tcg_const_tl(rb);
+            /* mtspr need a helper here */
+            tcg_temp_free_i32(ti);
+            tcg_temp_free(ta);
+            tcg_temp_free(tb);
+        }
+        break;
+
+    case 0x34:   /*l.sd*/
+        LOG_DIS("l.sd %d, r%d, r%d, %d\n", I5, ra, rb, I11);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(tmp, 16));
+        tcg_gen_qemu_st64(cpu_R[rb], t0, dc->mem_idx);
+        break;
+
+    case 0x35:   /*l.sw*/
+        LOG_DIS("l.sw %d, r%d, r%d, %d\n", I5, ra, rb, I11);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(tmp, 16));
+        tcg_gen_qemu_st32(cpu_R[rb], t0, dc->mem_idx);
+        break;
+
+    case 0x36:   /*l.sb*/
+        LOG_DIS("l.sb %d, r%d, r%d, %d\n", I5, ra, rb, I11);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(tmp, 16));
+        tcg_gen_qemu_st8(cpu_R[rb], t0, dc->mem_idx);
+        break;
+
+    case 0x37:   /*l.sh*/
+        LOG_DIS("l.sh %d, r%d, r%d, %d\n", I5, ra, rb, I11);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(tmp, 16));
+        tcg_gen_qemu_st16(cpu_R[rb], t0, dc->mem_idx);
+        break;
+    default:
+        break;
+    }
+    tcg_temp_free(t0);
+}
+
+static void dec_mac(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t ra, rb;
+    op0 = field(insn, 0, 4);
+    ra = field(insn, 16, 5);
+    rb = field(insn, 11, 5);
+    TCGv_i64 t0 = tcg_temp_new();
+    TCGv_i64 t1 = tcg_temp_new();
+    switch (op0) {
+    case 0x0001:   /*l.mac*/
+        {
+            LOG_DIS("l.mac r%d, r%d\n", ra, rb);
+            tcg_gen_ext_i32_i64(t0, cpu_R[ra]);
+            tcg_gen_ext_i32_i64(t1, cpu_R[rb]);
+            tcg_gen_mul_i64(t0, t0, t1);
+            tcg_gen_trunc_i64_i32(cpu_R[ra], t0);
+            tcg_gen_add_tl(maclo, maclo, cpu_R[ra]);
+            tcg_gen_add_tl(machi, machi, cpu_R[ra]);
+            tcg_temp_free(t0);
+            tcg_temp_free(t1);
+        }
+        break;
+
+    case 0x0002:   /*l.msb*/
+        {
+            LOG_DIS("l.msb r%d, r%d\n", ra, rb);
+            tcg_gen_ext_i32_i64(t0, cpu_R[ra]);
+            tcg_gen_ext_i32_i64(t1, cpu_R[rb]);
+            tcg_gen_mul_i64(t0, t0, t1);
+            tcg_gen_trunc_i64_i32(cpu_R[ra], t0);
+            tcg_gen_sub_tl(maclo, maclo, cpu_R[ra]);
+            tcg_gen_sub_tl(machi, machi, cpu_R[ra]);
+            tcg_temp_free(t0);
+            tcg_temp_free(t1);
+        }
+        break;
+
+    default:
+        break;
+   }
+}
+
+static void dec_logic(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t rd, ra, L6;
+    op0 = field(insn, 6, 2);
+    rd = field(insn, 21, 5);
+    ra = field(insn, 16, 5);
+    L6 = field(insn, 0, 6);
+
+    switch (op0) {
+    case 0x00:    /*l.slli*/
+        LOG_DIS("l.slli r%d, r%d, %d\n", rd, ra, L6);
+        tcg_gen_shli_tl(cpu_R[rd], cpu_R[ra], (L6 & 0x1f));
+        break;
+
+    case 0x01:    /*l.srli*/
+        LOG_DIS("l.srli r%d, r%d, %d\n", rd, ra, L6);
+        tcg_gen_shri_tl(cpu_R[rd], cpu_R[ra], (L6 & 0x1f));
+        break;
+
+    case 0x02:    /*l.srai*/
+        LOG_DIS("l.srai r%d, r%d, %d\n", rd, ra, L6);
+        tcg_gen_sari_tl(cpu_R[rd], cpu_R[ra], (L6 & 0x1f)); break;
+
+    case 0x03:    /*l.rori*/
+        LOG_DIS("l.rori r%d, r%d, %d\n", rd, ra, L6);
+        tcg_gen_rotri_tl(cpu_R[rd], cpu_R[ra], (L6 & 0x1f));
+        break;
+
+    default:
+        break;
+    }
+}
+
+static void dec_M(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t rd;
+    uint32_t K16;
+    op0 = field(insn, 16, 1);
+    rd = field(insn, 21, 5);
+    K16 = field(insn, 0, 16);
+
+    switch (op0) {
+    case 0x0:    /*l.movhi*/
+        LOG_DIS("l.movhi  r%d, %d\n", rd, K16);
+        tcg_gen_movi_tl(cpu_R[rd], (K16 << 16));
+        break;
+
+    case 0x1:    /*l.macrc*/
+        LOG_DIS("l.macrc  r%d\n", rd);
+        tcg_gen_mov_tl(cpu_R[rd], maclo);
+        tcg_gen_movi_tl(maclo, 0x0);
+        tcg_gen_movi_tl(machi, 0x0);
+        break;
+
+    default:
+        break;
+    }
+}
+
+static void dec_comp(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t ra, rb;
+
+    op0 = field(insn, 21, 5);
+    ra = field(insn, 16, 5);
+    rb = field(insn, 11, 5);
+
+    tcg_gen_movi_i32(env_btaken, 0x0);
+    /* unsigned integers  */
+    tcg_gen_ext32u_tl(cpu_R[ra], cpu_R[ra]);
+    tcg_gen_ext32u_tl(cpu_R[rb], cpu_R[rb]);
+
+    switch (op0) {
+    case 0x0:    /*l.sfeq*/
+        LOG_DIS("l.sfeq  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_EQ, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x1:    /*l.sfne*/
+        LOG_DIS("l.sfne  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_NE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x2:    /*l.sfgtu*/
+        LOG_DIS("l.sfgtu  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_GTU, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x3:    /*l.sfgeu*/
+        LOG_DIS("l.sfgeu  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_GEU, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x4:    /*l.sfltu*/
+        LOG_DIS("l.sfltu  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_LTU, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x5:    /*l.sfleu*/
+        LOG_DIS("l.sfleu  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_LEU, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0xa:    /*l.sfgts*/
+        LOG_DIS("l.sfgts  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_GT, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0xb:    /*l.sfges*/
+        LOG_DIS("l.sfges  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_GE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0xc:    /*l.sflts*/
+        LOG_DIS("l.sflts  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_LT, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0xd:    /*l.sfles*/
+        LOG_DIS("l.sfles  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_LE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    default:
+        break;
+    }
+    wb_SR_F();
+}
+
+static void dec_compi(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t ra, I16;
+
+    op0 = field(insn, 21, 5);
+    ra = field(insn, 16, 5);
+    I16 = field(insn, 0, 16);
+
+    tcg_gen_movi_i32(env_btaken, 0x0);
+    tcg_gen_ext32u_tl(cpu_R[ra], cpu_R[ra]);
+    I16 = sign_extend(I16, 16);
+
+    switch (op0) {
+    case 0x0:    /*l.sfeqi*/
+        LOG_DIS("l.sfeqi  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_EQ, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0x1:    /*l.sfnei*/
+        LOG_DIS("l.sfnei  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_NE, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0x2:    /*l.sfgtui*/
+        LOG_DIS("l.sfgtui  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_GTU, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0x3:    /*l.sfgeui*/
+        LOG_DIS("l.sfgeui  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_GEU, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0x4:    /*l.sfltui*/
+        LOG_DIS("l.sfltui  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_LTU, env_btaken, cpu_R[ra],
+                            sign_extend(I16, 16));
+        break;
+
+    case 0x5:    /*l.sfleui*/
+        LOG_DIS("l.sfleui  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_LEU, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0xa:    /*l.sfgtsi*/
+        LOG_DIS("l.sfgtsi  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_GT, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0xb:    /*l.sfgesi*/
+        LOG_DIS("l.sfgesi  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_GE, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0xc:    /*l.sfltsi*/
+        LOG_DIS("l.sfltsi  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_LT, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0xd:    /*l.sflesi*/
+        LOG_DIS("l.sflesi  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_LE, env_btaken, cpu_R[ra], I16);
+        break;
+
+    default:
+        break;
+    }
+    wb_SR_F();
+}
+
+static void dec_sys(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    /*uint32_t K16;*/
+    op0 = field(insn, 16, 8);
+    /*K16 = field(insn, 0, 16);*/
+
+    switch (op0) {
+    case 0x000:  /*l.sys*/
+        /*LOG_DIS("l.sys %d\n", K16);*/
+        tcg_gen_movi_tl(cpu_pc, dc->pc);
+        /* exception here */
+        dc->is_jmp = DISAS_UPDATE;
+        break;
+
+    case 0x100:  /*l.trap*/
+        /*LOG_DIS("l.trap %d\n", K16);*/
+        tcg_gen_movi_tl(cpu_pc, dc->pc);
+        /* exception here */
+        break;
+
+    case 0x300:  /*l.csync*/
+        LOG_DIS("l.csync\n");
+        break;
+
+    case 0x200:  /*l.msync*/
+        LOG_DIS("l.msync\n");
+        break;
+
+    case 0x270:  /*l.psync*/
+        LOG_DIS("l.psync\n");
+        break;
+
+    default:
+        break;
+    }
+}
+
+static void dec_float(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t ra, rb, rd;
+    op0 = field(insn, 0, 8);
+    ra = field(insn, 16, 5);
+    rb = field(insn, 11, 5);
+    rd = field(insn, 21, 5);
+
+    switch (op0) {
+    case 0x10:    /*lf.add.d*/
+        LOG_DIS("lf.add.d r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_add_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x00:    /*lf.add.s*/
+        LOG_DIS("lf.add.s r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_add_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x11:    /*lf.sub.d*/
+        LOG_DIS("lf.sub.d r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_sub_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x01:    /*lf.sub.s*/
+        LOG_DIS("lf.sub.s r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_sub_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x12:    /*lf.mul.d*/
+        LOG_DIS("lf.mul.d r%d, r%d, r%d\n", rd, ra, rb);
+        {
+            if (cpu_R[ra] != 0 && cpu_R[rb] != 0) {
+                tcg_gen_mul_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            } else {
+                tcg_gen_movi_i64(cpu_R[rd], 0x0);
+            }
+        }
+        break;
+
+    case 0x02:    /*lf.mul.s*/
+        LOG_DIS("lf.mul.s r%d, r%d, r%d\n", rd, ra, rb);
+        if (cpu_R[ra] != 0 && cpu_R[rb] != 0) {
+            tcg_gen_mul_i32(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        } else {
+            tcg_gen_movi_i64(cpu_R[rd], 0x0);
+        }
+        break;
+
+    case 0x13:    /*lf.div.d*/
+        LOG_DIS("lf.div.d r%d, r%d, r%d\n", rd, ra, rb);
+        if (cpu_R[rb] != 0) {
+            tcg_gen_div_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        } else {
+            /* exception here */
+        }
+        break;
+
+    case 0x03:    /*lf.div.s*/
+        LOG_DIS("lf.div.s r%d, r%d, r%d\n", rd, ra, rb);
+        if (cpu_R[rb] != 0) {
+            tcg_gen_div_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        } else {
+            /* exception here */
+        }
+        break;
+
+    case 0x14:    /*lf.itof.d*/
+        LOG_DIS("lf.itof r%d, r%d\n", rd, ra);
+        /* itof.d need a helper here */
+        break;
+
+    case 0x04:    /*lf.itof.s*/
+        LOG_DIS("lf.itof r%d, r%d\n", rd, ra);
+        /* itof.s need a helper here */
+        break;
+
+    case 0x15:    /*lf.ftoi.d*/
+        LOG_DIS("lf.ftoi r%d, r%d\n", rd, ra);
+        /* ftoi.d need a helper here */
+        break;
+
+    case 0x05:    /*lf.ftoi.s*/
+        LOG_DIS("lf.ftoi r%d, r%d\n", rd, ra);
+        /* ftoi.s need a helper here */
+        break;
+
+    case 0x16:    /*lf.rem.d*/
+        LOG_DIS("lf.rem.d r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_rem_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x06:    /*lf.rem.s*/
+        LOG_DIS("lf.rem.s r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_rem_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x17:    /*lf.madd.d*/
+        LOG_DIS("lf.madd.d r%d, r%d, r%d\n", rd, ra, rb);
+        {
+            TCGv_i64 t0, t1;
+
+            t0 = tcg_temp_new_i64();
+            t1 = tcg_temp_new_i64();
+
+            tcg_gen_ext_i32_i64(t0, cpu_R[ra]);
+            tcg_gen_ext_i32_i64(t1, cpu_R[rb]);
+            tcg_gen_mul_i64(t0, t0, t1);
+
+            tcg_gen_trunc_i64_i32(cpu_R[ra], t0);
+            tcg_gen_add_tl(fpmaddlo, cpu_R[ra], fpmaddlo);
+            tcg_gen_shri_i64(t0, t0, 32);
+            tcg_gen_trunc_i64_i32(cpu_R[rb], t0);
+            tcg_gen_add_tl(fpmaddhi, cpu_R[rb], fpmaddhi);
+
+            tcg_temp_free_i64(t0);
+            tcg_temp_free_i64(t1);
+        }
+        break;
+
+    case 0x07:    /*lf.madd.s*/
+        LOG_DIS("lf.madd.s r%d, r%d, r%d\n", rd, ra, rb);
+        {
+            TCGv_i64 t0, t1;
+
+            t0 = tcg_temp_new_i64();
+            t1 = tcg_temp_new_i64();
+
+            tcg_gen_ext_i32_i64(t0, cpu_R[ra]);
+            tcg_gen_ext_i32_i64(t1, cpu_R[rb]);
+            tcg_gen_mul_i64(t0, t0, t1);
+
+            tcg_gen_trunc_i64_i32(cpu_R[ra], t0);
+            tcg_gen_add_tl(fpmaddlo, cpu_R[ra], fpmaddlo);
+            tcg_gen_shri_i64(t0, t0, 32);
+            tcg_gen_trunc_i64_i32(cpu_R[rb], t0);
+            tcg_gen_add_tl(fpmaddhi, cpu_R[rb], fpmaddhi);
+            tcg_temp_free_i64(t0);
+            tcg_temp_free_i64(t1);
+
+        }
+        break;
+
+    case 0x18:    /*lf.sfeq.d*/
+        LOG_DIS("lf.sfeq.d r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_i64(TCG_COND_GE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x08:    /*lf.sfeq.s*/
+        LOG_DIS("lf.sfeq.s r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_EQ, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x19:    /*lf.sfne.d*/
+        LOG_DIS("lf.sfne.d r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_i64(TCG_COND_NE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x09:    /*lf.sfne.s*/
+        LOG_DIS("lf.sfne.s r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_NE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x1a:    /*lf.sfgt.d*/
+        LOG_DIS("lf.sfgt.d r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_i64(TCG_COND_GT, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x0a:    /*lf.sfgt.s*/
+        LOG_DIS("lf.sfgt.s r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_GT, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x1b:    /*lf.sfge.d*/
+        LOG_DIS("lf.sfge.d r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_i64(TCG_COND_GE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x0b:    /*lf.sfge.s*/
+        LOG_DIS("lf.sfge.s r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_GE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x1c:    /*lf.sflt.d*/
+        LOG_DIS("lf.sflt.d r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_i64(TCG_COND_LT, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x0c:    /*lf.sflt.s*/
+        LOG_DIS("lf.sflt.s r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_LT, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x1d:    /*lf.sfle.d*/
+        LOG_DIS("lf.sfle.d r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_i64(TCG_COND_LE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x0d:    /*lf.sfle.s*/
+        LOG_DIS("lf.sfle.s r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_LE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    default:
+        break;
+    }
+    wb_SR_F();
+}
+
+static void disas_openrisc_insn(DisasContext *dc, CPUOPENRISCState *env)
+{
+    uint32_t op0;
+    uint32_t insn;
+    insn = ldl_code(dc->pc);
+    op0 = field(insn, 26, 6);
+
+    switch (op0) {
+    case 0x06:
+        dec_M(dc, env, insn);
+        break;
+
+    case 0x08:
+        dec_sys(dc, env, insn);
+        break;
+
+    case 0x2e:
+        dec_logic(dc, env, insn);
+        break;
+
+    case 0x2f:
+        dec_compi(dc, env, insn);
+        break;
+
+    case 0x31:
+        dec_mac(dc, env, insn);
+        break;
+
+    case 0x32:
+        dec_float(dc, env, insn);
+        break;
+
+    case 0x38:
+        dec_calc(dc, env, insn);
+        break;
+
+    case 0x39:
+        dec_comp(dc, env, insn);
+        break;
+
+    default:
+        dec_misc(dc, env, insn);
+        break;
+    }
+}
+
+static void check_breakpoint(CPUOPENRISCState *env, DisasContext *dc)
+{
+    CPUBreakpoint *bp;
+
+    if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
+        QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
+            if (bp->pc == dc->pc) {
+                tcg_gen_movi_tl(cpu_pc, dc->pc);
+                /* exception here */
+                dc->is_jmp = DISAS_UPDATE;
+            }
+        }
+    }
  }
 
  static inline void gen_intermediate_code_internal(CPUOPENRISCState *env,
                                                   TranslationBlock *tb,
                                                   int search_pc)
  {
+    struct DisasContext ctx, *dc = &ctx;
+    uint16_t *gen_opc_end;
+    uint32_t pc_start;
+    int j, lj;
+    uint32_t next_page_start;
+    int num_insns;
+    int max_insns;
+
+    qemu_log_try_set_file(stderr);
+
+    pc_start = tb->pc;
+    dc->env = env;
+    dc->tb = tb;
+
+    gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
+    dc->is_jmp = DISAS_NEXT;
+    dc->ppc = pc_start;
+    dc->pc = pc_start;
+    dc->mem_idx = cpu_mmu_index(env);
+    dc->tb_flags = tb->flags;
+    dc->delayed_branch = !!(dc->tb_flags & D_FLAG);
+    dc->singlestep_enabled = env->singlestep_enabled;
+    if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
+        qemu_log("-----------------------------------------\n");
+        log_cpu_state(env, 0);
+    }
+
+    next_page_start = (pc_start & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
+    lj = -1;
+    num_insns = 0;
+    max_insns = tb->cflags & CF_COUNT_MASK;
+
+    if (max_insns == 0) {
+        max_insns = CF_COUNT_MASK;
+    }
+
+    gen_icount_start();
+
+    do {
+        check_breakpoint(env, dc);
+        if (search_pc) {
+            j = gen_opc_ptr - gen_opc_buf;
+            if (lj < j) {
+                lj++;
+                while (lj < j) {
+                    gen_opc_instr_start[lj++] = 0;
+                }
+            }
+            gen_opc_pc[lj] = dc->pc;
+            gen_opc_instr_start[lj] = 1;
+            gen_opc_icount[lj] = num_insns;
+        }
+
+        if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP))) {
+            tcg_gen_debug_insn_start(dc->pc);
+        }
+
+        if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO)) {
+            gen_io_start();
+        }
Quoted 9 lines

target-openrisc/translate.c typedef struct DisasContext { · line 1373

Quoted 6 lines
target-openrisc/translate.c +6 -0
+        dc->ppc = dc->pc - 4;
+        dc->npc = dc->pc + 4;
+        tcg_gen_movi_tl(cpu_ppc, dc->ppc);
+        tcg_gen_movi_tl(cpu_npc, dc->npc);
+        disas_openrisc_insn(dc, env);
+        dc->pc = dc->npc;

Isn't now npc == pc and ppc == pc - 8?

Quoted 1 line

I don't think so, but I'll check it.

OK, I'm just commenting based on my Sparc knowledge which also has PC and NPC with at least similar names. There NPC should be PC + 4 unless there is a control transfer instruction.

This reminds me that I forgot to ask to put a link to CPU documentation in a comment somewhere.

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
target-openrisc/translate.c +7 -0
+        num_insns++;
+        /* delay slot */
+        if (dc->delayed_branch) {
+            dc->delayed_branch--;
+            if (!dc->delayed_branch) {
+                dc->tb_flags &= ~D_FLAG;
+                tcg_gen_movi_i32(env_flags, dc->tb_flags);
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

This does not look correct.

for exception in delay slot, it is.

The TB flags are very different from CPU flags (NZVC etc.), isn't that what env_flags is for?

Thanks, fixed.

Quoted 102 lines
Quoted 102 lines
Quoted 6 lines

target-openrisc/translate.c typedef struct DisasContext { · line 1382

Quoted 3 lines
target-openrisc/translate.c +2 -0
+                tcg_gen_mov_tl(cpu_pc, jmp_pc);
+                tcg_gen_mov_tl(cpu_npc, jmp_pc);

+ 4?

Quoted 9 lines

target-openrisc/translate.c typedef struct DisasContext { · line 1387

Quoted 5 lines
target-openrisc/translate.c +5 -0
+                tcg_gen_movi_tl(jmp_pc, 0);
+                tcg_gen_exit_tb(0);
+                dc->is_jmp = DISAS_JUMP;
+                break;
+            }

Is it possible to have a jump instruction in the delay slot and if so, is it handled too?

I've done for this.

Quoted 75 lines
Quoted 75 lines
target-openrisc/translate.c +65 -0
+        }
+    } while (!dc->is_jmp && gen_opc_ptr < gen_opc_end
+             && !env->singlestep_enabled
+             && !singlestep
+             && (dc->pc < next_page_start)
+             && num_insns < max_insns);
+
+    if (tb->cflags & CF_LAST_IO) {
+        gen_io_end();
+    }
+    if (dc->is_jmp == DISAS_NEXT) {
+        dc->is_jmp = DISAS_UPDATE;
+        tcg_gen_movi_tl(cpu_pc, dc->pc);
+    }
+    if (unlikely(env->singlestep_enabled)) {
+        if (dc->is_jmp == DISAS_NEXT) {
+            tcg_gen_movi_tl(cpu_pc, dc->pc);
+        }
+        /* exception here*/
+    } else {
+        switch (dc->is_jmp) {
+        case DISAS_NEXT:
+            gen_goto_tb(dc, 0, dc->pc);
+            break;
+        default:
+        case DISAS_JUMP:
+            break;
+        case DISAS_UPDATE:
+            /* indicate that the hash table must be used
+               to find the next TB */
+            tcg_gen_exit_tb(0);
+            break;
+        case DISAS_TB_JUMP:
+            /* nothing more to generate */
+            break;
+        }
+    }
+
+    gen_icount_end(tb, num_insns);
+    *gen_opc_ptr = INDEX_op_end;
+    if (search_pc) {
+        j = gen_opc_ptr - gen_opc_buf;
+        lj++;
+        while (lj <= j) {
+            gen_opc_instr_start[lj++] = 0;
+        }
+    } else {
+        tb->size = dc->pc - pc_start;
+        tb->icount = num_insns;
+    }
+
+#ifdef DEBUG_DISAS
+    if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
+        qemu_log("\n");
+        log_target_disas(pc_start, dc->pc - pc_start, 0);
+        qemu_log("\nisize=%d osize=%td\n",
+            dc->pc - pc_start, gen_opc_ptr - gen_opc_buf);
+    }
+#endif
  }
 
  void gen_intermediate_code(CPUOPENRISCState *env, struct TranslationBlock *tb)
@@ -80,7 +1461,13 @@ void cpu_dump_state(CPUOPENRISCState *env, FILE *f,
                     fprintf_function cpu_fprintf,
                     int flags)
  {
+    int i;
+    uint32_t *regs = env->gpr;
     cpu_fprintf(f, "PC=%08x\n", env->pc);
+    for (i = 0; i < 32; ++i) {
+        cpu_fprintf(f, "R%02d=%08x%c", i, regs[i],
+                    (i % 4) == 3 ? '\n' : ' ');
+    }
  }
 
Quoted 5 lines
Quoted 5 lines
target-openrisc/translate.c +0 -1
  void restore_state_to_opc(CPUOPENRISCState *env, TranslationBlock *tb,
--

1.7.9.5

Jia,
Regards.

author
#28 lore raw

Re: [Qemu-devel] [PATCH 03/15] Openrisc: add instructions translation

In reply to Blue Swirl
Quoted 277 lines · Blue Swirl
Quoted 276 lines · Jia Liu

Thanks Blue.

Quoted 111 lines · Blue Swirl
Quoted 110 lines · Jia Liu

add the openrisc instructions translation.

---
 target-openrisc/translate.c | 1387 +++++++++++++++++++++++++++++++++++++++++++

 1 file changed, 1387 insertions(+)
target-openrisc/translate.c +93 -0
index aae483a..4828ae6 100644
@@ -55,14 +55,1395 @@ typedef struct DisasContext {
     uint32_t delayed_branch;
  } DisasContext;
 
+static TCGv_ptr cpu_env;
+static TCGv cpu_sr;
+static TCGv cpu_R[32];
+static TCGv cpu_pc;
+static TCGv jmp_pc;        /* l.jr/l.jalr temp pc */
+static TCGv cpu_npc;
+static TCGv cpu_ppc;
+static TCGv_i32 env_btaken;    /* bf/bnf , F flag taken */
+static TCGv machi, maclo;
+static TCGv fpmaddhi, fpmaddlo;
+static TCGv_i32 env_flags;
+#include "gen-icount.h"
+
  void openrisc_translate_init(void)
  {
+    static const char * const regnames[] = {
+        "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
+        "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
+        "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
+        "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
+    };
+    int i;
+
+    cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
+    cpu_sr = tcg_global_mem_new(TCG_AREG0,
+                                offsetof(CPUOPENRISCState, sr), "sr");
+    env_flags = tcg_global_mem_new_i32(TCG_AREG0,
+                                       offsetof(CPUOPENRISCState, flags),
+                                       "flags");
+    cpu_pc = tcg_global_mem_new(TCG_AREG0,
+                                offsetof(CPUOPENRISCState, pc), "pc");
+    cpu_npc = tcg_global_mem_new(TCG_AREG0,
+                                 offsetof(CPUOPENRISCState, npc), "npc");
+    cpu_ppc = tcg_global_mem_new(TCG_AREG0,
+                                 offsetof(CPUOPENRISCState, ppc), "ppc");
+    jmp_pc = tcg_global_mem_new(TCG_AREG0,
+                                offsetof(CPUOPENRISCState, jmp_pc), "jmp_pc");
+    env_btaken = tcg_global_mem_new_i32(TCG_AREG0,
+                                        offsetof(CPUOPENRISCState, btaken),
+                                        "btaken");
+    machi = tcg_global_mem_new(TCG_AREG0,
+                               offsetof(CPUOPENRISCState, machi),
+                               "machi");
+    maclo = tcg_global_mem_new(TCG_AREG0,
+                               offsetof(CPUOPENRISCState, maclo),
+                               "maclo");
+    fpmaddhi = tcg_global_mem_new(TCG_AREG0,
+                                  offsetof(CPUOPENRISCState, fpmaddhi),
+                                  "fpmaddhi");
+    fpmaddlo = tcg_global_mem_new(TCG_AREG0,
+                                  offsetof(CPUOPENRISCState, fpmaddlo),
+                                  "fpmaddlo");
+    for (i = 0; i < 32; i++) {
+        cpu_R[i] = tcg_global_mem_new(TCG_AREG0,
+                                      offsetof(CPUOPENRISCState, gpr[i]),
+                                      regnames[i]);
+    }
+#define GEN_HELPER 2
+#include "helper.h"
+}
+
+/* Writeback SR_F transaltion-space to execution-space.  */
+static inline void wb_SR_F(void)
+{
+    int label;
+
+    label = gen_new_label();
+    tcg_gen_andi_tl(cpu_sr, cpu_sr, ~SR_F);
+    tcg_gen_brcondi_tl(TCG_COND_EQ, env_btaken, 0, label);
+    tcg_gen_ori_tl(cpu_sr, cpu_sr, SR_F);
+    gen_set_label(label);
+}
+
+static inline int zero_extend(unsigned int val, int width)
+{
+    return val & ((1 << width) - 1);
+}
+
+static inline int sign_extend(unsigned int val, int width)
+{
+    int sval;
+
+    /* LSL.  */
+    val <<= 32 - width;
+    sval = val;
+    /* ASR.  */
+    sval >>= 32 - width;
+    return sval;
+}
+
+/* General purpose registers moves. */
+static inline void gen_load_gpr(TCGv t, unsigned int reg)
+{
+    if (reg == 0) {
+        tcg_gen_movi_tl(t, 0);
Quoted 9 lines

target-openrisc/translate.c typedef struct DisasContext { · line 156

Quoted 4 lines
target-openrisc/translate.c +4 -0
+    } else {
+        tcg_gen_mov_tl(t, cpu_R[reg]);
+    }
+}

This does not seem to be used. Does the code handle the cases where one or several of the registers is number 0? If this is the always-0 register, are stores to register 0 handled correctly?

I'll remove it.

Quoted 65 lines
Quoted 15 lines
target-openrisc/translate.c +15 -0
+
+static void gen_goto_tb(DisasContext *dc, int n, target_ulong dest)
+{
+    TranslationBlock *tb;
+    tb = dc->tb;
+    if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) &&
+                                       likely(!dc->singlestep_enabled)) {
+        tcg_gen_movi_tl(cpu_pc, dest);
+        tcg_gen_goto_tb(n);
+        tcg_gen_exit_tb((tcg_target_long)tb + n);
+    } else {
+        tcg_gen_movi_tl(cpu_pc, dest);
+        if (dc->singlestep_enabled) {
+            /* exception here */
+        }

target-openrisc/translate.c typedef struct DisasContext { · line 181

Quoted 10 lines
target-openrisc/translate.c +10 -0
+        tcg_gen_exit_tb(0);
+    }
+}
+
+static inline uint32_t field(uint32_t val, int start, int length)
+{
+    val >>= start;
+    val &= ~(~0 << length);
+    return val;
+}

Nice function, maybe I could use it in Sparc as well if generalized (later).

Quoted 37 lines
target-openrisc/translate.c +37 -0
+
+static void dec_calc(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0, op1, op2;
+    uint32_t ra, rb, rd;
+    op0 = field(insn, 0, 4);
+    op1 = field(insn, 8, 2);
+    op2 = field(insn, 6, 2);
+    ra = field(insn, 16, 5);
+    rb = field(insn, 11, 5);
+    rd = field(insn, 21, 5);
+
+    switch (op0) {
+    case 0x0000:
+        switch (op1) {
+        case 0x00:     /*l.add*/
+            LOG_DIS("l.add r%d, r%d, r%d\n", rd, ra, rb);
+            /* Aha, we do need a helper here for add */
+            break;
+        default:
+            break;
+        }
+    break;
+
+    case 0x0001:    /*l.addc*/
+        switch (op1) {
+        case 0x00:
+            LOG_DIS("l.addc r%d, r%d, r%d\n", rd, ra, rb);
+            /* Aha, we do need a helper here for addc */
+            break;
+        default:
+            break;
+        }
+    break;
+
+    case 0x0002:    /*l.sub*/
+        switch (op1) {
Quoted 9 lines

target-openrisc/translate.c typedef struct DisasContext { · line 223

Quoted 5 lines
target-openrisc/translate.c +5 -0
+        case 0x00:
+            LOG_DIS("l.sub r%d, r%d, r%d\n", rd, ra, rb);
+            /* Aha, we do need a helper here for sub */
+            break;
+        default:

What happens on a real CPU when an illegal instruction is executed, shouldn't there be an exception?

I'll add a exception for illegal instruction.

Quoted 73 lines
Quoted 38 lines
target-openrisc/translate.c +38 -0
+            break;
+        }
+    break;
+
+    case 0x0003:   /*l.and*/
+        switch (op1) {
+        case 0x00:
+            LOG_DIS("l.and r%d, r%d, r%d\n", rd, ra, rb);
+            tcg_gen_and_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            break;
+        default:
+            break;
+        }
+    break;
+
+    case 0x0004:   /*l.or*/
+        switch (op1) {
+        case 0x00:
+            LOG_DIS("l.or r%d, r%d, r%d\n", rd, ra, rb);
+            tcg_gen_or_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            break;
+        default:
+            break;
+        }
+    break;
+
+    case 0x0005:
+        switch (op1) {
+        case 0x00:   /*l.xor*/
+            LOG_DIS("l.xor r%d, r%d, r%d\n", rd, ra, rb);
+            tcg_gen_xor_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            break;
+        default:
+            break;
+        }
+    break;
+
+    case 0x0006:

target-openrisc/translate.c typedef struct DisasContext { · line 271

Quoted 10 lines
target-openrisc/translate.c +10 -0
+        switch (op1) {
+        case 0x03:   /*l.mul*/
+            LOG_DIS("l.mul r%d, r%d, r%d\n", rd, ra, rb);
+            if (ra != 0 && rb != 0) {
+                tcg_gen_mul_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+                tcg_gen_ext32s_tl(cpu_R[rd], cpu_R[rd]);
+            } else {
+                tcg_gen_movi_tl(cpu_R[rd], 0x0);
+            }
+        break;

Indentation.

Quoted 22 lines
target-openrisc/translate.c +22 -0
+        default:
+            break;
+        }
+    break;
+
+    case 0x0009:
+        switch (op1) {
+        case 0x03:   /*l.div*/
+            LOG_DIS("l.div r%d, r%d, r%d\n", rd, ra, rb);
+            if (rb != 0) {
+                tcg_gen_div_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            } else {
+                /* exception here */
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000a:
+        switch (op1) {
Quoted 9 lines
Quoted 4 lines
Quoted 4 lines

target-openrisc/translate.c typedef struct DisasContext { · line 297

Quoted 4 lines
target-openrisc/translate.c +4 -0
+        case 0x03:   /*l.divu*/
+            LOG_DIS("l.divu r%d, r%d, r%d\n", rd, ra, rb);
+            if (rb != 0) {
+                tcg_gen_divu_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);

By the way, a division by zero will crash QEMU. Please add a runtime check, something like this:
http://git.qemu.org/?p=qemu.git;a=blob;f=target-sparc/translate.c;h=b95f91cd3baf9c02305f8583662b8eba664b17ba;hb=HEAD#l765

Thanks, I've reimplement l.div, and I think it is fixed.

Quoted 1,104 lines
Quoted 1,086 lines
Quoted 253 lines
Quoted 253 lines
target-openrisc/translate.c +253 -0
+            } else {
+                /* exception here */
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000b:
+        switch (op1) {
+        case 0x03:   /*l.mulu*/
+            LOG_DIS("l.mulu r%d, r%d, r%d\n", rd, ra, rb);
+            if (rb != 0 && ra != 0) {
+                tcg_gen_ext32u_tl(cpu_R[ra], cpu_R[ra]);
+                tcg_gen_ext32u_tl(cpu_R[rb], cpu_R[rb]);
+                tcg_gen_mul_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            } else {
+                tcg_gen_movi_tl(cpu_R[rd], 0);
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000e:
+        switch (op1) {
+        case 0x00:   /*l.cmov*/
+            LOG_DIS("l.cmov r%d, r%d, r%d\n", rd, ra, rb);
+            {
+                int lab = gen_new_label();
+                TCGv sr_f = tcg_temp_new();
+                tcg_gen_andi_tl(sr_f, cpu_sr, SR_F);
+                tcg_gen_mov_tl(cpu_R[rd], cpu_R[rb]);
+                tcg_gen_brcondi_tl(TCG_COND_NE, sr_f, SR_F, lab);
+                tcg_gen_mov_tl(cpu_R[rd], cpu_R[ra]);
+                gen_set_label(lab);
+                tcg_temp_free(sr_f);
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000f:
+        switch (op1) {
+        case 0x00:   /*l.ff1*/
+            LOG_DIS("l.ff1 r%d, r%d, r%d\n", rd, ra, rb);
+            /* ff1 need a helper here */
+            break;
+        case 0x01:   /*l.fl1*/
+            LOG_DIS("l.fl1 r%d, r%d, r%d\n", rd, ra, rb);
+            /* fl1 need a helper here */
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x0008:
+        switch (op1) {
+        case 0x00:
+            switch (op2) {
+            case 0x00:   /*l.sll*/
+                LOG_DIS("l.sll r%d, r%d, r%d\n", rd, ra, rb);
+                tcg_gen_shl_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+                break;
+            case 0x01:   /*l.srl*/
+                LOG_DIS("l.srl r%d, r%d, r%d\n", rd, ra, rb);
+                tcg_gen_shr_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+                break;
+            case 0x02:   /*l.sra*/
+                LOG_DIS("l.sra r%d, r%d, r%d\n", rd, ra, rb);
+                tcg_gen_sar_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+                break;
+            case 0x03:   /*l.ror*/
+                LOG_DIS("l.ror r%d, r%d, r%d\n", rd, ra, rb);
+                tcg_gen_rotr_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+                break;
+            default:
+                break;
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000c:
+        switch (op1) {
+        case 0x00:
+            switch (op2) {
+            case 0x00:   /*l.exths*/
+                LOG_DIS("l.exths r%d, r%d\n", rd, ra);
+                tcg_gen_ext16s_tl(cpu_R[rd], cpu_R[ra]);
+                break;
+            case 0x01:   /*l.extbs*/
+                LOG_DIS("l.extbs r%d, r%d\n", rd, ra);
+                tcg_gen_ext8s_tl(cpu_R[rd], cpu_R[ra]);
+                break;
+            case 0x02:   /*l.exthz*/
+                LOG_DIS("l.exthz r%d, r%d\n", rd, ra);
+                tcg_gen_ext16u_tl(cpu_R[rd], cpu_R[ra]);
+                break;
+            case 0x03:   /*l.extbz*/
+                LOG_DIS("l.extbz r%d, r%d\n", rd, ra);
+                tcg_gen_ext8u_tl(cpu_R[rd], cpu_R[ra]);
+                break;
+            default:
+                break;
+            }
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x000d:
+        switch (op1) {
+        case 0x00:
+            switch (op2) {
+            case 0x00:   /*l.extws*/
+                LOG_DIS("l.extws r%d, r%d\n", rd, ra);
+                tcg_gen_ext32s_tl(cpu_R[rd], cpu_R[ra]);
+                break;
+            case 0x01:    /*l.extwz*/
+                LOG_DIS("l.extwz r%d, r%d\n", rd, ra);
+                tcg_gen_ext32u_tl(cpu_R[rd], cpu_R[ra]);
+                break;
+            default:
+                break;
+            }
+        default:
+            break;
+        }
+        break;
+
+    default:
+        break;
+    }
+}
+
+static void dec_misc(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0, op1;
+    uint32_t ra, rb, rd;
+    uint32_t /*L6, K5, */I16, I5, I11, N26, tmp;
+    op0 = field(insn, 26, 6);
+    op1 = field(insn, 24, 2);
+    ra = field(insn, 16, 5);
+    rb = field(insn, 11, 5);
+    rd = field(insn, 21, 5);
+    /*L6 = field(insn, 5, 6);
+    K5 = field(insn, 0, 5);*/
+    I16 = field(insn, 0, 16);
+    I5 = field(insn, 21, 5);
+    I11 = field(insn, 0, 11);
+    N26 = field(insn, 0, 26);
+    tmp = (I5<<11) + I11;
+    TCGv t0 = tcg_temp_new();
+
+    switch (op0) {
+    case 0x00:   /*l.j*/
+        {
+            LOG_DIS("l.j %d\n", N26);
+            tmp = sign_extend((N26<<2), 26) + dc->pc;
+            tcg_gen_movi_tl(jmp_pc, tmp);
+            dc->delayed_branch = 2;
+            dc->tb_flags |= D_FLAG;
+            tcg_gen_movi_i32(env_flags, dc->tb_flags);
+        }
+        break;
+
+    case 0x01:   /*l.jal*/
+        {
+            LOG_DIS("l.jal %d\n", N26);
+            tcg_gen_movi_tl(cpu_R[9], (dc->pc + 8));
+            tmp = sign_extend((N26<<2), 26) + dc->pc;
+            tcg_gen_movi_tl(jmp_pc, tmp);
+            dc->delayed_branch = 2;
+            dc->tb_flags |= D_FLAG;
+            tcg_gen_movi_i32(env_flags, dc->tb_flags);
+        }
+        break;
+
+    case 0x03:   /*l.bnf*/
+        LOG_DIS("l.bnf %d\n", N26);
+        {
+            int lab = gen_new_label();
+            TCGv sr_f = tcg_temp_new();
+            tcg_gen_andi_tl(sr_f, cpu_sr, SR_F);
+            tmp = sign_extend((N26 << 2), 26) + dc->pc;
+            tcg_gen_movi_tl(jmp_pc, dc->pc+8);
+            tcg_gen_brcondi_tl(TCG_COND_EQ, sr_f, SR_F, lab);
+            tcg_gen_movi_tl(jmp_pc, tmp);
+            gen_set_label(lab);
+            dc->delayed_branch = 2;
+            dc->tb_flags |= D_FLAG;
+            tcg_gen_movi_i32(env_flags, dc->tb_flags);
+            tcg_temp_free(sr_f);
+        }
+        break;
+
+    case 0x04:   /*l.bf*/
+        LOG_DIS("l.bf %d\n", N26);
+        {
+            int lab = gen_new_label();
+            TCGv sr_f = tcg_temp_new();
+            tcg_gen_andi_tl(sr_f, cpu_sr, SR_F);
+            tmp = sign_extend((N26 << 2), 26) + dc->pc;
+            tcg_gen_movi_tl(jmp_pc, dc->pc+8);
+            tcg_gen_brcondi_tl(TCG_COND_NE, sr_f, SR_F, lab);
+            tcg_gen_movi_tl(jmp_pc, tmp);
+            gen_set_label(lab);
+            dc->delayed_branch = 2;
+            dc->tb_flags |= D_FLAG;
+            tcg_gen_movi_i32(env_flags, dc->tb_flags);
+            tcg_temp_free(sr_f);
+        }
+        break;
+
+    case 0x05:
+        switch (op1) {
+        case 0x01:   /*l.nop*/
+            LOG_DIS("l.nop %d\n", I16);
+            break;
+        default:
+            break;
+        }
+        break;
+
+    case 0x11:    /*l.jr*/
+        LOG_DIS("l.jr r%d\n", rb);
+        {
+            tcg_gen_mov_tl(jmp_pc, cpu_R[rb]);
+            dc->delayed_branch = 2;
+            dc->tb_flags |= D_FLAG;
+            tcg_gen_movi_i32(env_flags, dc->tb_flags);
+        }
+        break;
+
+    case 0x12:    /*l.jalr*/
+        LOG_DIS("l.jalr r%d\n", rb);
+        {
+            tcg_gen_movi_tl(cpu_R[9], (dc->pc + 8));
+            gen_load_gpr(jmp_pc, rb);
+            dc->delayed_branch = 2;
+            dc->tb_flags |= D_FLAG;
+            tcg_gen_movi_i32(env_flags, dc->tb_flags);
+        }
+        break;
Quoted 9 lines

target-openrisc/translate.c typedef struct DisasContext { · line 556

Quoted 6 lines
target-openrisc/translate.c +6 -0
+
+    case 0x13:    /*l.maci*/
+        {
+            LOG_DIS("l.maci %d, r%d, %d\n", I5, ra, I11);
+            TCGv_i64 tra = tcg_temp_new_i64();    /*  store cpu_R[ra]*/
+            TCGv_i64 tmac = tcg_temp_new_i64();   /*  store machi maclo*/

Variable definitions should be before code, like LOG_DIS() call in this case.

OK, thx.

Quoted 811 lines
Quoted 811 lines
target-openrisc/translate.c +805 -0
+            tcg_gen_movi_tl(t0, tmp);
+            tcg_gen_ext32s_i64(tra, cpu_R[ra]);
+            tcg_gen_ext32s_i64(tmac, t0);
+            tcg_gen_mul_tl(tmac, tra, tmac);
+            tcg_gen_trunc_i64_i32(cpu_R[ra], tmac);
+            tcg_gen_add_i64(machi, machi, cpu_R[ra]);
+            tcg_gen_add_i64(maclo, maclo, cpu_R[ra]);
+            tcg_temp_free(tra);
+            tcg_temp_free(tmac);
+        }
+        break;
+
+    case 0x09:    /*l.rfe*/
+        LOG_DIS("l.rfe\n");
+        {
+            /* rfe need a helper here */
+            dc->is_jmp = DISAS_UPDATE;
+        }
+        break;
+
+    case 0x1c:    /*l.cust1*/
+        LOG_DIS("l.cust1\n");
+        break;
+
+    case 0x1d:    /*l.cust2*/
+        LOG_DIS("l.cust2\n");
+        break;
+
+    case 0x1e:    /*l.cust3*/
+        LOG_DIS("l.cust3\n");
+        break;
+
+    case 0x1f:    /*l.cust4*/
+        LOG_DIS("l.cust4\n");
+        break;
+
+    case 0x3c:   /*l.cust5*/
+        /*LOG_DIS("l.cust5 r%d, r%d, r%d, %d, %d\n", rd, ra, rb, L6, K5);*/
+        break;
+
+    case 0x3d:   /*l.cust6*/
+        LOG_DIS("l.cust6\n");
+        break;
+
+    case 0x3e:   /*l.cust7*/
+        LOG_DIS("l.cust7\n");
+        break;
+
+    case 0x3f:   /*l.cust8*/
+        LOG_DIS("l.cust8\n");
+        break;
+
+    case 0x20:   /*l.ld*/
+        LOG_DIS("l.ld r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld64(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x21:   /*l.lwz*/
+        LOG_DIS("l.lwz r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld32u(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x22:   /*l.lws*/
+        LOG_DIS("l.lws r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld32s(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x23:   /*l.lbz*/
+        LOG_DIS("l.lbz r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld8u(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x24:   /*l.lbs*/
+        LOG_DIS("l.lbs r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld8s(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x25:   /*l.lhz*/
+        LOG_DIS("l.lhz r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld16u(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x26:   /*l.lhs*/
+        LOG_DIS("l.lhs r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(I16, 16));
+        tcg_gen_qemu_ld16s(cpu_R[rd], t0, dc->mem_idx);
+        break;
+
+    case 0x27:   /*l.addi*/
+        LOG_DIS("l.addi r%d, r%d, %d\n", rd, ra, I16);
+        {
+            TCGv t0 = tcg_const_tl(sign_extend(I16, 16));
+            /* Aha, we do need a helper here for addi */
+            tcg_temp_free(t0);
+        }
+        break;
+
+    case 0x28:   /*l.addic*/
+        LOG_DIS("l.addic r%d, r%d, %d\n", rd, ra, I16);
+        {
+            TCGv t0 = tcg_const_tl(sign_extend(I16, 16));
+            /* Aha, we do need a helper here for addic */
+            tcg_temp_free(t0);
+        }
+        break;
+
+    case 0x29:   /*l.andi*/
+        LOG_DIS("l.andi r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_andi_tl(cpu_R[rd], cpu_R[ra], zero_extend(I16, 16));
+        break;
+
+    case 0x2a:   /*l.ori*/
+        LOG_DIS("l.ori r%d, r%d, %d\n", rd, ra, zero_extend(I16, 16));
+        tcg_gen_ori_tl(cpu_R[rd], cpu_R[ra], I16);
+        break;
+
+    case 0x2b:   /*l.xori*/
+        LOG_DIS("l.xori r%d, r%d, %d\n", rd, ra, I16);
+        tcg_gen_xori_tl(cpu_R[rd], cpu_R[ra], sign_extend(I16, 16));
+        break;
+
+    case 0x2c:   /*l.muli*/
+        LOG_DIS("l.muli r%d, r%d, %d\n", rd, ra, I16);
+        if (ra != 0 && I16 != 0) {
+            tcg_gen_muli_tl(cpu_R[rd], cpu_R[ra], I16);
+            tcg_gen_ext32s_tl(cpu_R[rd], cpu_R[rd]);
+        } else {
+            tcg_gen_movi_tl(cpu_R[rd], 0);
+        }
+        break;
+
+    case 0x2d:   /*l.mfspr*/
+        LOG_DIS("l.mfspr r%d, r%d, %d\n", rd, ra, I16);
+        {
+            TCGv_i32 ti = tcg_const_i32(I16);
+            TCGv td = tcg_const_tl(rd);
+            TCGv ta = tcg_const_tl(ra);
+            /* mfspr need a helper here */
+            tcg_temp_free_i32(ti);
+            tcg_temp_free(td);
+            tcg_temp_free(ta);
+        }
+        break;
+
+    case 0x30:  /*l.mtspr*/
+        LOG_DIS("l.mtspr %d, r%d, r%d, %d\n", I5, ra, rb, I11);
+        {
+            TCGv_i32 ti = tcg_const_i32(tmp);
+            TCGv ta = tcg_const_tl(ra);
+            TCGv tb = tcg_const_tl(rb);
+            /* mtspr need a helper here */
+            tcg_temp_free_i32(ti);
+            tcg_temp_free(ta);
+            tcg_temp_free(tb);
+        }
+        break;
+
+    case 0x34:   /*l.sd*/
+        LOG_DIS("l.sd %d, r%d, r%d, %d\n", I5, ra, rb, I11);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(tmp, 16));
+        tcg_gen_qemu_st64(cpu_R[rb], t0, dc->mem_idx);
+        break;
+
+    case 0x35:   /*l.sw*/
+        LOG_DIS("l.sw %d, r%d, r%d, %d\n", I5, ra, rb, I11);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(tmp, 16));
+        tcg_gen_qemu_st32(cpu_R[rb], t0, dc->mem_idx);
+        break;
+
+    case 0x36:   /*l.sb*/
+        LOG_DIS("l.sb %d, r%d, r%d, %d\n", I5, ra, rb, I11);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(tmp, 16));
+        tcg_gen_qemu_st8(cpu_R[rb], t0, dc->mem_idx);
+        break;
+
+    case 0x37:   /*l.sh*/
+        LOG_DIS("l.sh %d, r%d, r%d, %d\n", I5, ra, rb, I11);
+        tcg_gen_addi_tl(t0, cpu_R[ra], sign_extend(tmp, 16));
+        tcg_gen_qemu_st16(cpu_R[rb], t0, dc->mem_idx);
+        break;
+    default:
+        break;
+    }
+    tcg_temp_free(t0);
+}
+
+static void dec_mac(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t ra, rb;
+    op0 = field(insn, 0, 4);
+    ra = field(insn, 16, 5);
+    rb = field(insn, 11, 5);
+    TCGv_i64 t0 = tcg_temp_new();
+    TCGv_i64 t1 = tcg_temp_new();
+    switch (op0) {
+    case 0x0001:   /*l.mac*/
+        {
+            LOG_DIS("l.mac r%d, r%d\n", ra, rb);
+            tcg_gen_ext_i32_i64(t0, cpu_R[ra]);
+            tcg_gen_ext_i32_i64(t1, cpu_R[rb]);
+            tcg_gen_mul_i64(t0, t0, t1);
+            tcg_gen_trunc_i64_i32(cpu_R[ra], t0);
+            tcg_gen_add_tl(maclo, maclo, cpu_R[ra]);
+            tcg_gen_add_tl(machi, machi, cpu_R[ra]);
+            tcg_temp_free(t0);
+            tcg_temp_free(t1);
+        }
+        break;
+
+    case 0x0002:   /*l.msb*/
+        {
+            LOG_DIS("l.msb r%d, r%d\n", ra, rb);
+            tcg_gen_ext_i32_i64(t0, cpu_R[ra]);
+            tcg_gen_ext_i32_i64(t1, cpu_R[rb]);
+            tcg_gen_mul_i64(t0, t0, t1);
+            tcg_gen_trunc_i64_i32(cpu_R[ra], t0);
+            tcg_gen_sub_tl(maclo, maclo, cpu_R[ra]);
+            tcg_gen_sub_tl(machi, machi, cpu_R[ra]);
+            tcg_temp_free(t0);
+            tcg_temp_free(t1);
+        }
+        break;
+
+    default:
+        break;
+   }
+}
+
+static void dec_logic(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t rd, ra, L6;
+    op0 = field(insn, 6, 2);
+    rd = field(insn, 21, 5);
+    ra = field(insn, 16, 5);
+    L6 = field(insn, 0, 6);
+
+    switch (op0) {
+    case 0x00:    /*l.slli*/
+        LOG_DIS("l.slli r%d, r%d, %d\n", rd, ra, L6);
+        tcg_gen_shli_tl(cpu_R[rd], cpu_R[ra], (L6 & 0x1f));
+        break;
+
+    case 0x01:    /*l.srli*/
+        LOG_DIS("l.srli r%d, r%d, %d\n", rd, ra, L6);
+        tcg_gen_shri_tl(cpu_R[rd], cpu_R[ra], (L6 & 0x1f));
+        break;
+
+    case 0x02:    /*l.srai*/
+        LOG_DIS("l.srai r%d, r%d, %d\n", rd, ra, L6);
+        tcg_gen_sari_tl(cpu_R[rd], cpu_R[ra], (L6 & 0x1f)); break;
+
+    case 0x03:    /*l.rori*/
+        LOG_DIS("l.rori r%d, r%d, %d\n", rd, ra, L6);
+        tcg_gen_rotri_tl(cpu_R[rd], cpu_R[ra], (L6 & 0x1f));
+        break;
+
+    default:
+        break;
+    }
+}
+
+static void dec_M(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t rd;
+    uint32_t K16;
+    op0 = field(insn, 16, 1);
+    rd = field(insn, 21, 5);
+    K16 = field(insn, 0, 16);
+
+    switch (op0) {
+    case 0x0:    /*l.movhi*/
+        LOG_DIS("l.movhi  r%d, %d\n", rd, K16);
+        tcg_gen_movi_tl(cpu_R[rd], (K16 << 16));
+        break;
+
+    case 0x1:    /*l.macrc*/
+        LOG_DIS("l.macrc  r%d\n", rd);
+        tcg_gen_mov_tl(cpu_R[rd], maclo);
+        tcg_gen_movi_tl(maclo, 0x0);
+        tcg_gen_movi_tl(machi, 0x0);
+        break;
+
+    default:
+        break;
+    }
+}
+
+static void dec_comp(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t ra, rb;
+
+    op0 = field(insn, 21, 5);
+    ra = field(insn, 16, 5);
+    rb = field(insn, 11, 5);
+
+    tcg_gen_movi_i32(env_btaken, 0x0);
+    /* unsigned integers  */
+    tcg_gen_ext32u_tl(cpu_R[ra], cpu_R[ra]);
+    tcg_gen_ext32u_tl(cpu_R[rb], cpu_R[rb]);
+
+    switch (op0) {
+    case 0x0:    /*l.sfeq*/
+        LOG_DIS("l.sfeq  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_EQ, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x1:    /*l.sfne*/
+        LOG_DIS("l.sfne  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_NE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x2:    /*l.sfgtu*/
+        LOG_DIS("l.sfgtu  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_GTU, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x3:    /*l.sfgeu*/
+        LOG_DIS("l.sfgeu  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_GEU, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x4:    /*l.sfltu*/
+        LOG_DIS("l.sfltu  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_LTU, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x5:    /*l.sfleu*/
+        LOG_DIS("l.sfleu  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_LEU, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0xa:    /*l.sfgts*/
+        LOG_DIS("l.sfgts  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_GT, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0xb:    /*l.sfges*/
+        LOG_DIS("l.sfges  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_GE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0xc:    /*l.sflts*/
+        LOG_DIS("l.sflts  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_LT, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0xd:    /*l.sfles*/
+        LOG_DIS("l.sfles  r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_LE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    default:
+        break;
+    }
+    wb_SR_F();
+}
+
+static void dec_compi(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t ra, I16;
+
+    op0 = field(insn, 21, 5);
+    ra = field(insn, 16, 5);
+    I16 = field(insn, 0, 16);
+
+    tcg_gen_movi_i32(env_btaken, 0x0);
+    tcg_gen_ext32u_tl(cpu_R[ra], cpu_R[ra]);
+    I16 = sign_extend(I16, 16);
+
+    switch (op0) {
+    case 0x0:    /*l.sfeqi*/
+        LOG_DIS("l.sfeqi  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_EQ, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0x1:    /*l.sfnei*/
+        LOG_DIS("l.sfnei  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_NE, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0x2:    /*l.sfgtui*/
+        LOG_DIS("l.sfgtui  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_GTU, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0x3:    /*l.sfgeui*/
+        LOG_DIS("l.sfgeui  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_GEU, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0x4:    /*l.sfltui*/
+        LOG_DIS("l.sfltui  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_LTU, env_btaken, cpu_R[ra],
+                            sign_extend(I16, 16));
+        break;
+
+    case 0x5:    /*l.sfleui*/
+        LOG_DIS("l.sfleui  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_LEU, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0xa:    /*l.sfgtsi*/
+        LOG_DIS("l.sfgtsi  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_GT, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0xb:    /*l.sfgesi*/
+        LOG_DIS("l.sfgesi  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_GE, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0xc:    /*l.sfltsi*/
+        LOG_DIS("l.sfltsi  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_LT, env_btaken, cpu_R[ra], I16);
+        break;
+
+    case 0xd:    /*l.sflesi*/
+        LOG_DIS("l.sflesi  r%d, %d\n", ra, I16);
+        tcg_gen_setcondi_tl(TCG_COND_LE, env_btaken, cpu_R[ra], I16);
+        break;
+
+    default:
+        break;
+    }
+    wb_SR_F();
+}
+
+static void dec_sys(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    /*uint32_t K16;*/
+    op0 = field(insn, 16, 8);
+    /*K16 = field(insn, 0, 16);*/
+
+    switch (op0) {
+    case 0x000:  /*l.sys*/
+        /*LOG_DIS("l.sys %d\n", K16);*/
+        tcg_gen_movi_tl(cpu_pc, dc->pc);
+        /* exception here */
+        dc->is_jmp = DISAS_UPDATE;
+        break;
+
+    case 0x100:  /*l.trap*/
+        /*LOG_DIS("l.trap %d\n", K16);*/
+        tcg_gen_movi_tl(cpu_pc, dc->pc);
+        /* exception here */
+        break;
+
+    case 0x300:  /*l.csync*/
+        LOG_DIS("l.csync\n");
+        break;
+
+    case 0x200:  /*l.msync*/
+        LOG_DIS("l.msync\n");
+        break;
+
+    case 0x270:  /*l.psync*/
+        LOG_DIS("l.psync\n");
+        break;
+
+    default:
+        break;
+    }
+}
+
+static void dec_float(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
+{
+    uint32_t op0;
+    uint32_t ra, rb, rd;
+    op0 = field(insn, 0, 8);
+    ra = field(insn, 16, 5);
+    rb = field(insn, 11, 5);
+    rd = field(insn, 21, 5);
+
+    switch (op0) {
+    case 0x10:    /*lf.add.d*/
+        LOG_DIS("lf.add.d r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_add_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x00:    /*lf.add.s*/
+        LOG_DIS("lf.add.s r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_add_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x11:    /*lf.sub.d*/
+        LOG_DIS("lf.sub.d r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_sub_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x01:    /*lf.sub.s*/
+        LOG_DIS("lf.sub.s r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_sub_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x12:    /*lf.mul.d*/
+        LOG_DIS("lf.mul.d r%d, r%d, r%d\n", rd, ra, rb);
+        {
+            if (cpu_R[ra] != 0 && cpu_R[rb] != 0) {
+                tcg_gen_mul_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+            } else {
+                tcg_gen_movi_i64(cpu_R[rd], 0x0);
+            }
+        }
+        break;
+
+    case 0x02:    /*lf.mul.s*/
+        LOG_DIS("lf.mul.s r%d, r%d, r%d\n", rd, ra, rb);
+        if (cpu_R[ra] != 0 && cpu_R[rb] != 0) {
+            tcg_gen_mul_i32(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        } else {
+            tcg_gen_movi_i64(cpu_R[rd], 0x0);
+        }
+        break;
+
+    case 0x13:    /*lf.div.d*/
+        LOG_DIS("lf.div.d r%d, r%d, r%d\n", rd, ra, rb);
+        if (cpu_R[rb] != 0) {
+            tcg_gen_div_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        } else {
+            /* exception here */
+        }
+        break;
+
+    case 0x03:    /*lf.div.s*/
+        LOG_DIS("lf.div.s r%d, r%d, r%d\n", rd, ra, rb);
+        if (cpu_R[rb] != 0) {
+            tcg_gen_div_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        } else {
+            /* exception here */
+        }
+        break;
+
+    case 0x14:    /*lf.itof.d*/
+        LOG_DIS("lf.itof r%d, r%d\n", rd, ra);
+        /* itof.d need a helper here */
+        break;
+
+    case 0x04:    /*lf.itof.s*/
+        LOG_DIS("lf.itof r%d, r%d\n", rd, ra);
+        /* itof.s need a helper here */
+        break;
+
+    case 0x15:    /*lf.ftoi.d*/
+        LOG_DIS("lf.ftoi r%d, r%d\n", rd, ra);
+        /* ftoi.d need a helper here */
+        break;
+
+    case 0x05:    /*lf.ftoi.s*/
+        LOG_DIS("lf.ftoi r%d, r%d\n", rd, ra);
+        /* ftoi.s need a helper here */
+        break;
+
+    case 0x16:    /*lf.rem.d*/
+        LOG_DIS("lf.rem.d r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_rem_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x06:    /*lf.rem.s*/
+        LOG_DIS("lf.rem.s r%d, r%d, r%d\n", rd, ra, rb);
+        tcg_gen_rem_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x17:    /*lf.madd.d*/
+        LOG_DIS("lf.madd.d r%d, r%d, r%d\n", rd, ra, rb);
+        {
+            TCGv_i64 t0, t1;
+
+            t0 = tcg_temp_new_i64();
+            t1 = tcg_temp_new_i64();
+
+            tcg_gen_ext_i32_i64(t0, cpu_R[ra]);
+            tcg_gen_ext_i32_i64(t1, cpu_R[rb]);
+            tcg_gen_mul_i64(t0, t0, t1);
+
+            tcg_gen_trunc_i64_i32(cpu_R[ra], t0);
+            tcg_gen_add_tl(fpmaddlo, cpu_R[ra], fpmaddlo);
+            tcg_gen_shri_i64(t0, t0, 32);
+            tcg_gen_trunc_i64_i32(cpu_R[rb], t0);
+            tcg_gen_add_tl(fpmaddhi, cpu_R[rb], fpmaddhi);
+
+            tcg_temp_free_i64(t0);
+            tcg_temp_free_i64(t1);
+        }
+        break;
+
+    case 0x07:    /*lf.madd.s*/
+        LOG_DIS("lf.madd.s r%d, r%d, r%d\n", rd, ra, rb);
+        {
+            TCGv_i64 t0, t1;
+
+            t0 = tcg_temp_new_i64();
+            t1 = tcg_temp_new_i64();
+
+            tcg_gen_ext_i32_i64(t0, cpu_R[ra]);
+            tcg_gen_ext_i32_i64(t1, cpu_R[rb]);
+            tcg_gen_mul_i64(t0, t0, t1);
+
+            tcg_gen_trunc_i64_i32(cpu_R[ra], t0);
+            tcg_gen_add_tl(fpmaddlo, cpu_R[ra], fpmaddlo);
+            tcg_gen_shri_i64(t0, t0, 32);
+            tcg_gen_trunc_i64_i32(cpu_R[rb], t0);
+            tcg_gen_add_tl(fpmaddhi, cpu_R[rb], fpmaddhi);
+            tcg_temp_free_i64(t0);
+            tcg_temp_free_i64(t1);
+
+        }
+        break;
+
+    case 0x18:    /*lf.sfeq.d*/
+        LOG_DIS("lf.sfeq.d r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_i64(TCG_COND_GE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x08:    /*lf.sfeq.s*/
+        LOG_DIS("lf.sfeq.s r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_EQ, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x19:    /*lf.sfne.d*/
+        LOG_DIS("lf.sfne.d r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_i64(TCG_COND_NE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x09:    /*lf.sfne.s*/
+        LOG_DIS("lf.sfne.s r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_NE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x1a:    /*lf.sfgt.d*/
+        LOG_DIS("lf.sfgt.d r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_i64(TCG_COND_GT, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x0a:    /*lf.sfgt.s*/
+        LOG_DIS("lf.sfgt.s r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_GT, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x1b:    /*lf.sfge.d*/
+        LOG_DIS("lf.sfge.d r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_i64(TCG_COND_GE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x0b:    /*lf.sfge.s*/
+        LOG_DIS("lf.sfge.s r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_GE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x1c:    /*lf.sflt.d*/
+        LOG_DIS("lf.sflt.d r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_i64(TCG_COND_LT, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x0c:    /*lf.sflt.s*/
+        LOG_DIS("lf.sflt.s r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_LT, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x1d:    /*lf.sfle.d*/
+        LOG_DIS("lf.sfle.d r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_i64(TCG_COND_LE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    case 0x0d:    /*lf.sfle.s*/
+        LOG_DIS("lf.sfle.s r%d, r%d\n", ra, rb);
+        tcg_gen_setcond_tl(TCG_COND_LE, env_btaken, cpu_R[ra], cpu_R[rb]);
+        break;
+
+    default:
+        break;
+    }
+    wb_SR_F();
+}
+
+static void disas_openrisc_insn(DisasContext *dc, CPUOPENRISCState *env)
+{
+    uint32_t op0;
+    uint32_t insn;
+    insn = ldl_code(dc->pc);
+    op0 = field(insn, 26, 6);
+
+    switch (op0) {
+    case 0x06:
+        dec_M(dc, env, insn);
+        break;
+
+    case 0x08:
+        dec_sys(dc, env, insn);
+        break;
+
+    case 0x2e:
+        dec_logic(dc, env, insn);
+        break;
+
+    case 0x2f:
+        dec_compi(dc, env, insn);
+        break;
+
+    case 0x31:
+        dec_mac(dc, env, insn);
+        break;
+
+    case 0x32:
+        dec_float(dc, env, insn);
+        break;
+
+    case 0x38:
+        dec_calc(dc, env, insn);
+        break;
+
+    case 0x39:
+        dec_comp(dc, env, insn);
+        break;
+
+    default:
+        dec_misc(dc, env, insn);
+        break;
+    }
+}
+
+static void check_breakpoint(CPUOPENRISCState *env, DisasContext *dc)
+{
+    CPUBreakpoint *bp;
+
+    if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
+        QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
+            if (bp->pc == dc->pc) {
+                tcg_gen_movi_tl(cpu_pc, dc->pc);
+                /* exception here */
+                dc->is_jmp = DISAS_UPDATE;
+            }
+        }
+    }
  }
 
  static inline void gen_intermediate_code_internal(CPUOPENRISCState *env,
                                                   TranslationBlock *tb,
                                                   int search_pc)
  {
+    struct DisasContext ctx, *dc = &ctx;
+    uint16_t *gen_opc_end;
+    uint32_t pc_start;
+    int j, lj;
+    uint32_t next_page_start;
+    int num_insns;
+    int max_insns;
+
+    qemu_log_try_set_file(stderr);
+
+    pc_start = tb->pc;
+    dc->env = env;
+    dc->tb = tb;
+
+    gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
+    dc->is_jmp = DISAS_NEXT;
+    dc->ppc = pc_start;
+    dc->pc = pc_start;
+    dc->mem_idx = cpu_mmu_index(env);
+    dc->tb_flags = tb->flags;
+    dc->delayed_branch = !!(dc->tb_flags & D_FLAG);
+    dc->singlestep_enabled = env->singlestep_enabled;
+    if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
+        qemu_log("-----------------------------------------\n");
+        log_cpu_state(env, 0);
+    }
+
+    next_page_start = (pc_start & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
+    lj = -1;
+    num_insns = 0;
+    max_insns = tb->cflags & CF_COUNT_MASK;
+
+    if (max_insns == 0) {
+        max_insns = CF_COUNT_MASK;
+    }
+
+    gen_icount_start();
+
+    do {
+        check_breakpoint(env, dc);
+        if (search_pc) {
+            j = gen_opc_ptr - gen_opc_buf;
+            if (lj < j) {
+                lj++;
+                while (lj < j) {
+                    gen_opc_instr_start[lj++] = 0;
+                }
+            }
+            gen_opc_pc[lj] = dc->pc;
+            gen_opc_instr_start[lj] = 1;
+            gen_opc_icount[lj] = num_insns;
+        }
+
+        if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP))) {
+            tcg_gen_debug_insn_start(dc->pc);
+        }
+
+        if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO)) {
+            gen_io_start();
+        }
Quoted 9 lines

target-openrisc/translate.c typedef struct DisasContext { · line 1373

Quoted 6 lines
target-openrisc/translate.c +6 -0
+        dc->ppc = dc->pc - 4;
+        dc->npc = dc->pc + 4;
+        tcg_gen_movi_tl(cpu_ppc, dc->ppc);
+        tcg_gen_movi_tl(cpu_npc, dc->npc);
+        disas_openrisc_insn(dc, env);
+        dc->pc = dc->npc;

Isn't now npc == pc and ppc == pc - 8?

Quoted 1 line

I don't think so, but I'll check it.

OK, I'm just commenting based on my Sparc knowledge which also has PC and NPC with at least similar names. There NPC should be PC + 4 unless there is a control transfer instruction.

This reminds me that I forgot to ask to put a link to CPU documentation in a comment somewhere.

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
target-openrisc/translate.c +7 -0
+        num_insns++;
+        /* delay slot */
+        if (dc->delayed_branch) {
+            dc->delayed_branch--;
+            if (!dc->delayed_branch) {
+                dc->tb_flags &= ~D_FLAG;
+                tcg_gen_movi_i32(env_flags, dc->tb_flags);
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

This does not look correct.

for exception in delay slot, it is.

The TB flags are very different from CPU flags (NZVC etc.), isn't that what env_flags is for?

maybe not, Openrisc does not have flag bit in some register for this, so we have to add it by ourselves, so, the code look like what you see now.
I'll rewrite this code, maybe it looks better now.

Quoted 102 lines
Quoted 102 lines
Quoted 6 lines

target-openrisc/translate.c typedef struct DisasContext { · line 1382

Quoted 3 lines
target-openrisc/translate.c +2 -0
+                tcg_gen_mov_tl(cpu_pc, jmp_pc);
+                tcg_gen_mov_tl(cpu_npc, jmp_pc);

+ 4?

Quoted 9 lines

target-openrisc/translate.c typedef struct DisasContext { · line 1387

Quoted 5 lines
target-openrisc/translate.c +5 -0
+                tcg_gen_movi_tl(jmp_pc, 0);
+                tcg_gen_exit_tb(0);
+                dc->is_jmp = DISAS_JUMP;
+                break;
+            }

Is it possible to have a jump instruction in the delay slot and if so, is it handled too?

I've done for this.

Quoted 75 lines
Quoted 75 lines
target-openrisc/translate.c +65 -0
+        }
+    } while (!dc->is_jmp && gen_opc_ptr < gen_opc_end
+             && !env->singlestep_enabled
+             && !singlestep
+             && (dc->pc < next_page_start)
+             && num_insns < max_insns);
+
+    if (tb->cflags & CF_LAST_IO) {
+        gen_io_end();
+    }
+    if (dc->is_jmp == DISAS_NEXT) {
+        dc->is_jmp = DISAS_UPDATE;
+        tcg_gen_movi_tl(cpu_pc, dc->pc);
+    }
+    if (unlikely(env->singlestep_enabled)) {
+        if (dc->is_jmp == DISAS_NEXT) {
+            tcg_gen_movi_tl(cpu_pc, dc->pc);
+        }
+        /* exception here*/
+    } else {
+        switch (dc->is_jmp) {
+        case DISAS_NEXT:
+            gen_goto_tb(dc, 0, dc->pc);
+            break;
+        default:
+        case DISAS_JUMP:
+            break;
+        case DISAS_UPDATE:
+            /* indicate that the hash table must be used
+               to find the next TB */
+            tcg_gen_exit_tb(0);
+            break;
+        case DISAS_TB_JUMP:
+            /* nothing more to generate */
+            break;
+        }
+    }
+
+    gen_icount_end(tb, num_insns);
+    *gen_opc_ptr = INDEX_op_end;
+    if (search_pc) {
+        j = gen_opc_ptr - gen_opc_buf;
+        lj++;
+        while (lj <= j) {
+            gen_opc_instr_start[lj++] = 0;
+        }
+    } else {
+        tb->size = dc->pc - pc_start;
+        tb->icount = num_insns;
+    }
+
+#ifdef DEBUG_DISAS
+    if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
+        qemu_log("\n");
+        log_target_disas(pc_start, dc->pc - pc_start, 0);
+        qemu_log("\nisize=%d osize=%td\n",
+            dc->pc - pc_start, gen_opc_ptr - gen_opc_buf);
+    }
+#endif
  }
 
  void gen_intermediate_code(CPUOPENRISCState *env, struct TranslationBlock *tb)
@@ -80,7 +1461,13 @@ void cpu_dump_state(CPUOPENRISCState *env, FILE *f,
                     fprintf_function cpu_fprintf,
                     int flags)
  {
+    int i;
+    uint32_t *regs = env->gpr;
     cpu_fprintf(f, "PC=%08x\n", env->pc);
+    for (i = 0; i < 32; ++i) {
+        cpu_fprintf(f, "R%02d=%08x%c", i, regs[i],
+                    (i % 4) == 3 ? '\n' : ' ');
+    }
  }
 
Quoted 5 lines
Quoted 5 lines
target-openrisc/translate.c +0 -1
  void restore_state_to_opc(CPUOPENRISCState *env, TranslationBlock *tb,
--

1.7.9.5

Jia,
Regards.

author
#29 lore raw

PATCH 4/15 Openrisc: add interrupt support

In reply to Jia Liu

add the openrisc interrupt support.

---
 Makefile.target                |    2 +-
 target-openrisc/helper.c       |   42 +++++++++++++++++++++++++++++++
 target-openrisc/helper.h       |    3 +++
 target-openrisc/intrp_helper.c |   53 ++++++++++++++++++++++++++++++++++++++++
 target-openrisc/translate.c    |    2 +-
 5 files changed, 100 insertions(+), 2 deletions(-)
 create mode 100644 target-openrisc/intrp_helper.c
Makefile.target +1 -1
index 79c75f6..975c9a8 100644
@@ -101,7 +101,7 @@ endif
 libobj-$(TARGET_SPARC) += int32_helper.o
 libobj-$(TARGET_SPARC64) += int64_helper.o
 libobj-$(TARGET_ALPHA) += int_helper.o fpu_helper.o sys_helper.o mem_helper.o
-libobj-$(TARGET_OPENRISC) += mem.o mem_helper.o
+libobj-$(TARGET_OPENRISC) += intrp_helper.o mem.o mem_helper.o
 
 libobj-y += disas.o
 libobj-$(CONFIG_TCI_DIS) += tci-dis.o
target-openrisc/helper.c +42 -0

46 lines not on this page · raw message

target-openrisc/helper.h +3 -0
index 103d9b4..bb394ad 100644
@@ -20,4 +20,7 @@
 
 #include "def-helper.h"
 
+/* interrupt */
+DEF_HELPER_FLAGS_1(rfe, 0, void, env)
+
 #include "def-helper.h"
target-openrisc/intrp_helper.c new file +53 -0

53 lines not on this page · raw message

target-openrisc/translate.c +1 -1
index 4828ae6..dd0240c 100644
@@ -569,7 +569,7 @@ static void dec_misc(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
     case 0x09:    /*l.rfe*/
         LOG_DIS("l.rfe\n");
         {
-            /* rfe need a helper here */
+            gen_helper_rfe(cpu_env);
             dc->is_jmp = DISAS_UPDATE;
         }
         break;
Signature · 1 line
1.7.9.5
#30 lore raw

Re: [Qemu-devel] [PATCH 04/15] Openrisc: add interrupt support

Quoted 30 lines · Jia Liu

add the openrisc interrupt support.

---
 Makefile.target                |    2 +-
 target-openrisc/helper.c       |   42 +++++++++++++++++++++++++++++++ target-openrisc/helper.h       |    3 +++ target-openrisc/intrp_helper.c |   53 ++++++++++++++++++++++++++++++++++++++++ target-openrisc/translate.c    |    2 +-

 5 files changed, 100 insertions(+), 2 deletions(-)

 create mode 100644 target-openrisc/intrp_helper.c

Makefile.target +0 -0
index 79c75f6..975c9a8 100644
@@ -101,7 +101,7 @@ endif

 libobj-$(TARGET_SPARC) += int32_helper.o libobj-$(TARGET_SPARC64) += int64_helper.o libobj-$(TARGET_ALPHA) += int_helper.o fpu_helper.o sys_helper.o mem_helper.o

Makefile.target +1 -1
-libobj-$(TARGET_OPENRISC) += mem.o mem_helper.o
+libobj-$(TARGET_OPENRISC) += intrp_helper.o mem.o mem_helper.o

 libobj-y += disas.o
 libobj-$(CONFIG_TCI_DIS) += tci-dis.o

target-openrisc/helper.c +0 -0
index dcb61c9..3aee996 100644

target-openrisc/helper.c CPUOPENRISCState *cpu_openrisc_init(const char *cpu_model) · line 65

Quoted 2 lines
target-openrisc/helper.c +0 -0
@@ -64,4 +64,46 @@ CPUOPENRISCState *cpu_openrisc_init(const char *cpu_model)
 
  void do_interrupt(CPUOPENRISCState *env)

I'd put the function to intrp_helper.c too, or if you prefer, intrp.c to match excp.c.

Quoted 27 lines
target-openrisc/helper.c +26 -0
  {
+#if !defined(CONFIG_USER_ONLY)
+    if (env->flags & D_FLAG) { /* Delay Slot insn */
+        env->flags &= ~D_FLAG;
+        env->sr |= SR_DSX;
+        if (env->exception_index == EXCP_TICK    ||
+            env->exception_index == EXCP_INT     ||
+            env->exception_index == EXCP_SYSCALL ||
+            env->exception_index == EXCP_FPE) {
+            env->epcr = env->jmp_pc;
+        } else {
+            env->epcr = env->pc - 4;
+        }
+    } else {
+        if (env->exception_index == EXCP_TICK    ||
+            env->exception_index == EXCP_INT     ||
+            env->exception_index == EXCP_SYSCALL ||
+            env->exception_index == EXCP_FPE) {
+            env->epcr = env->npc;
+        } else {
+            env->epcr = env->pc;
+        }
+    }
+
+    tlb_flush(env, 1);
+
+    env->esr = env->sr;

target-openrisc/helper.c CPUOPENRISCState *cpu_openrisc_init(const char *cpu_model) · line 102

Quoted 10 lines
target-openrisc/helper.c +10 -0
+    env->sr &= ~SR_DME;
+    env->sr &= ~SR_IME;
+    env->sr |= SR_SM;
+    env->sr &= ~SR_IEE;
+    env->sr &= ~SR_TEE;
+    env->map_address_data = &get_phys_nommu;
+    env->map_address_code = &get_phys_nommu;
+
+    if (env->exception_index > 0 && env->exception_index < EXCP_NR) {
+        env->pc = env->exception_index * 0x100;

<< 8

Quoted 44 lines
target-openrisc/helper.c +6 -0
+    } else {
+        cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
+    }
+#endif
+
+    env->exception_index = -1;
  }
target-openrisc/helper.h +3 -0
index 103d9b4..bb394ad 100644
@@ -20,4 +20,7 @@
 
  #include "def-helper.h"
 
+/* interrupt */
+DEF_HELPER_FLAGS_1(rfe, 0, void, env)
+
  #include "def-helper.h"
target-openrisc/intrp_helper.c new file +19 -0
new file mode 100644
index 0000000..c617068
@@ -0,0 +1,53 @@
+/*
+ * OpenRISC interrupt helper routines
+ *
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *                          Feng Gao <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA

target-openrisc/intrp_helper.c line 29

Quoted 10 lines
target-openrisc/intrp_helper.c new file +10 -0
+ */
+
+#include "cpu.h"
+#include "helper.h"
+
+void HELPER(rfe)(CPUOPENRISCState *env)
+{
+#if !defined(CONFIG_USER_ONLY)
+    int need_flush_tlb = (env->sr & (SR_SM | SR_IME | SR_DME)) ^
+                         (env->esr & (SR_SM | SR_IME | SR_DME));

If SR_SM is like supervisor mode flag, changes in it shouldn't cause TLB flushes if you put it also to TB flags. The same is also true to other flags but MMU enable/disable flags are commonly handled like this.

target-openrisc/intrp_helper.c line 32

Quoted 3 lines
target-openrisc/intrp_helper.c new file +3 -0
+#endif
+    env->pc = env->epcr;
+    env->npc = env->epcr;

+ 4?

Quoted 38 lines
target-openrisc/intrp_helper.c new file +21 -0
+    env->sr = env->esr;
+
+#if !defined(CONFIG_USER_ONLY)
+    if (env->sr & SR_DME) {
+        env->map_address_data = &get_phys_data;
+    } else {
+        env->map_address_data = &get_phys_nommu;
+    }
+
+    if (env->sr & SR_IME) {
+        env->map_address_code = &get_phys_code;
+    } else {
+        env->map_address_code = &get_phys_nommu;
+    }
+
+    if (need_flush_tlb) {
+        tlb_flush(env, 1);
+    }
+#endif
+    env->interrupt_request |= CPU_INTERRUPT_EXITTB;
+}
target-openrisc/translate.c +0 -0
index 4828ae6..dd0240c 100644
@@ -569,7 +569,7 @@ static void dec_misc(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)

    case 0x09:    /*l.rfe*/
        LOG_DIS("l.rfe\n");
        {
-            /* rfe need a helper here */
+            gen_helper_rfe(cpu_env);
            dc->is_jmp = DISAS_UPDATE;
        }
        break;
--
1.7.9.5

author
#31 lore raw

Re: [Qemu-devel] [PATCH 04/15] Openrisc: add interrupt support

Hi Blue,

Quoted 32 lines · Blue Swirl
Quoted 31 lines · Jia Liu

add the openrisc interrupt support.

---
 Makefile.target                |    2 +-
 target-openrisc/helper.c       |   42 +++++++++++++++++++++++++++++++ target-openrisc/helper.h       |    3 +++ target-openrisc/intrp_helper.c |   53 ++++++++++++++++++++++++++++++++++++++++ target-openrisc/translate.c    |    2 +-

 5 files changed, 100 insertions(+), 2 deletions(-)

 create mode 100644 target-openrisc/intrp_helper.c

Makefile.target +0 -0
index 79c75f6..975c9a8 100644
@@ -101,7 +101,7 @@ endif

 libobj-$(TARGET_SPARC) += int32_helper.o libobj-$(TARGET_SPARC64) += int64_helper.o libobj-$(TARGET_ALPHA) += int_helper.o fpu_helper.o sys_helper.o mem_helper.o

Makefile.target +1 -1
-libobj-$(TARGET_OPENRISC) += mem.o mem_helper.o
+libobj-$(TARGET_OPENRISC) += intrp_helper.o mem.o mem_helper.o

 libobj-y += disas.o
 libobj-$(CONFIG_TCI_DIS) += tci-dis.o

target-openrisc/helper.c +0 -0
index dcb61c9..3aee996 100644
@@ -64,4 +64,46 @@ CPUOPENRISCState *cpu_openrisc_init(const char *cpu_model)
 
Quoted 5 lines

target-openrisc/helper.c CPUOPENRISCState *cpu_openrisc_init(const char *cpu_model) · line 65

Quoted 1 line
target-openrisc/helper.c +0 -0
  void do_interrupt(CPUOPENRISCState *env)

I'd put the function to intrp_helper.c too, or if you prefer, intrp.c to match excp.c.

OK.

Quoted 31 lines
Quoted 31 lines
target-openrisc/helper.c +30 -0
  {
+#if !defined(CONFIG_USER_ONLY)
+    if (env->flags & D_FLAG) { /* Delay Slot insn */
+        env->flags &= ~D_FLAG;
+        env->sr |= SR_DSX;
+        if (env->exception_index == EXCP_TICK    ||
+            env->exception_index == EXCP_INT     ||
+            env->exception_index == EXCP_SYSCALL ||
+            env->exception_index == EXCP_FPE) {
+            env->epcr = env->jmp_pc;
+        } else {
+            env->epcr = env->pc - 4;
+        }
+    } else {
+        if (env->exception_index == EXCP_TICK    ||
+            env->exception_index == EXCP_INT     ||
+            env->exception_index == EXCP_SYSCALL ||
+            env->exception_index == EXCP_FPE) {
+            env->epcr = env->npc;
+        } else {
+            env->epcr = env->pc;
+        }
+    }
+
+    tlb_flush(env, 1);
+
+    env->esr = env->sr;
+    env->sr &= ~SR_DME;
+    env->sr &= ~SR_IME;
+    env->sr |= SR_SM;
+    env->sr &= ~SR_IEE;
Quoted 9 lines

target-openrisc/helper.c CPUOPENRISCState *cpu_openrisc_init(const char *cpu_model) · line 102

Quoted 6 lines
target-openrisc/helper.c +6 -0
+    env->sr &= ~SR_TEE;
+    env->map_address_data = &get_phys_nommu;
+    env->map_address_code = &get_phys_nommu;
+
+    if (env->exception_index > 0 && env->exception_index < EXCP_NR) {
+        env->pc = env->exception_index * 0x100;

<< 8

all right, fixed.

Quoted 51 lines
Quoted 51 lines
target-openrisc/helper.c +6 -0
+    } else {
+        cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
+    }
+#endif
+
+    env->exception_index = -1;
  }
target-openrisc/helper.h +3 -0
index 103d9b4..bb394ad 100644
@@ -20,4 +20,7 @@
 
  #include "def-helper.h"
 
+/* interrupt */
+DEF_HELPER_FLAGS_1(rfe, 0, void, env)
+
  #include "def-helper.h"
target-openrisc/intrp_helper.c new file +26 -0
new file mode 100644
index 0000000..c617068
@@ -0,0 +1,53 @@
+/*
+ * OpenRISC interrupt helper routines
+ *
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *                          Feng Gao <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
+ */
+
+#include "cpu.h"
+#include "helper.h"
+
+void HELPER(rfe)(CPUOPENRISCState *env)
+{
Quoted 9 lines

target-openrisc/intrp_helper.c line 29

Quoted 3 lines
target-openrisc/intrp_helper.c new file +3 -0
+#if !defined(CONFIG_USER_ONLY)
+    int need_flush_tlb = (env->sr & (SR_SM | SR_IME | SR_DME)) ^
+                         (env->esr & (SR_SM | SR_IME | SR_DME));

If SR_SM is like supervisor mode flag, changes in it shouldn't cause TLB flushes if you put it also to TB flags. The same is also true to other flags but MMU enable/disable flags are commonly handled like this.

SR_SM changed when we jump back to user-model from supervisor-mode, we need flush TLB at that time.

Quoted 6 lines

target-openrisc/intrp_helper.c line 32

Quoted 3 lines
target-openrisc/intrp_helper.c new file +3 -0
+#endif
+    env->pc = env->epcr;
+    env->npc = env->epcr;

+ 4?

npc and pc be calced separately, so can not +4.

Quoted 30 lines
Quoted 30 lines
target-openrisc/intrp_helper.c new file +21 -0
+    env->sr = env->esr;
+
+#if !defined(CONFIG_USER_ONLY)
+    if (env->sr & SR_DME) {
+        env->map_address_data = &get_phys_data;
+    } else {
+        env->map_address_data = &get_phys_nommu;
+    }
+
+    if (env->sr & SR_IME) {
+        env->map_address_code = &get_phys_code;
+    } else {
+        env->map_address_code = &get_phys_nommu;
+    }
+
+    if (need_flush_tlb) {
+        tlb_flush(env, 1);
+    }
+#endif
+    env->interrupt_request |= CPU_INTERRUPT_EXITTB;
+}
target-openrisc/translate.c +0 -0
index 4828ae6..dd0240c 100644
@@ -569,7 +569,7 @@ static void dec_misc(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)

    case 0x09:    /*l.rfe*/
        LOG_DIS("l.rfe\n");
        {
-            /* rfe need a helper here */

Quoted 8 lines
Quoted 8 lines

+            gen_helper_rfe(cpu_env);
            dc->is_jmp = DISAS_UPDATE;
        }
        break;
--
1.7.9.5

Regards,
Jia.

author
#32 lore raw

PATCH 5/15 Openrisc: add exception support

In reply to Jia Liu

add the openrisc exception support.

---
 Makefile.target               |    2 +-
 target-openrisc/excp.c        |   27 +++++++++++++++++++++++++++
 target-openrisc/excp.h        |   28 ++++++++++++++++++++++++++++
 target-openrisc/excp_helper.c |   28 ++++++++++++++++++++++++++++
 target-openrisc/helper.h      |    3 +++
 target-openrisc/translate.c   |   25 ++++++++++++++++---------
 6 files changed, 103 insertions(+), 10 deletions(-)
 create mode 100644 target-openrisc/excp.c
 create mode 100644 target-openrisc/excp.h
 create mode 100644 target-openrisc/excp_helper.c
Makefile.target +1 -1
index 975c9a8..ed5f0b0 100644
@@ -101,7 +101,7 @@ endif
 libobj-$(TARGET_SPARC) += int32_helper.o
 libobj-$(TARGET_SPARC64) += int64_helper.o
 libobj-$(TARGET_ALPHA) += int_helper.o fpu_helper.o sys_helper.o mem_helper.o
-libobj-$(TARGET_OPENRISC) += intrp_helper.o mem.o mem_helper.o
+libobj-$(TARGET_OPENRISC) += excp.o excp_helper.o intrp_helper.o mem.o mem_helper.o
 
 libobj-y += disas.o
 libobj-$(CONFIG_TCI_DIS) += tci-dis.o
target-openrisc/excp.c new file +27 -0
new file mode 100644
index 0000000..fc9391a
@@ -0,0 +1,27 @@
+/*
+ *  Openrisc exception.
+ *
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "cpu.h"
+#include "excp.h"
+
+void raise_exception(CPUOPENRISCState *env, uint32_t excp)
+{
+    env->exception_index = excp;
+    cpu_loop_exit(env);
+}
target-openrisc/excp.h new file +28 -0
new file mode 100644
index 0000000..4c32e46
@@ -0,0 +1,28 @@
+/*
+ *  Openrisc exception header.
+ *
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef QEMU_OPENRISC_EXCP_H
+#define QEMU_OPENRISC_EXCP_H
+
+#include "cpu.h"
+#include "qemu-common.h"
+
+void raise_exception(CPUOPENRISCState *env, uint32_t excp);
+
+#endif /* QEMU_OPENRISC_EXCP_H */
target-openrisc/excp_helper.c new file +28 -0
new file mode 100644
index 0000000..0cad14b
@@ -0,0 +1,28 @@
+/*
+ * OpenRISC exception helper routines
+ *
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
+ */
+
+#include "cpu.h"
+#include "helper.h"
+#include "excp.h"
+
+void HELPER(exception)(CPUOPENRISCState *env, uint32_t excp)
+{
+    raise_exception(env, excp);
+}
target-openrisc/helper.h +3 -0
index bb394ad..a0fb8c4 100644
@@ -20,6 +20,9 @@
 
 #include "def-helper.h"
 
+/* exception */
+DEF_HELPER_FLAGS_2(exception, 0, void, env, i32)
+
 /* interrupt */
 DEF_HELPER_FLAGS_1(rfe, 0, void, env)
 
target-openrisc/translate.c +16 -9
index dd0240c..a1264c9 100644
@@ -155,6 +155,13 @@ static inline void gen_load_gpr(TCGv t, unsigned int reg)
     }
 }
 
+static void gen_exception(DisasContext *dc, unsigned int excp)
+{
+    TCGv_i32 tmp = tcg_const_i32(excp);
+    gen_helper_exception(cpu_env, tmp);
+    tcg_temp_free(tmp);
+}
+
 static void gen_goto_tb(DisasContext *dc, int n, target_ulong dest)
 {
     TranslationBlock *tb;
@@ -167,7 +174,7 @@ static void gen_goto_tb(DisasContext *dc, int n, target_ulong dest)
     } else {
         tcg_gen_movi_tl(cpu_pc, dest);
         if (dc->singlestep_enabled) {
-            /* exception here */
+            gen_exception(dc, EXCP_DEBUG);
         }
         tcg_gen_exit_tb(0);
     }
@@ -281,7 +288,7 @@ static void dec_calc(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
             if (rb != 0) {
                 tcg_gen_div_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
             } else {
-                /* exception here */
+                gen_exception(dc, EXCP_RANGE);
             }
             break;
         default:
@@ -296,7 +303,7 @@ static void dec_calc(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
             if (rb != 0) {
                 tcg_gen_divu_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
             } else {
-                /* exception here */
+                gen_exception(dc, EXCP_RANGE);
             }
             break;
         default:
@@ -1003,14 +1010,14 @@ static void dec_sys(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
     case 0x000:  /*l.sys*/
         /*LOG_DIS("l.sys %d\n", K16);*/
         tcg_gen_movi_tl(cpu_pc, dc->pc);
-        /* exception here */
+        gen_exception(dc, EXCP_SYSCALL);
         dc->is_jmp = DISAS_UPDATE;
         break;
 
     case 0x100:  /*l.trap*/
         /*LOG_DIS("l.trap %d\n", K16);*/
         tcg_gen_movi_tl(cpu_pc, dc->pc);
-        /* exception here */
+        gen_exception(dc, EXCP_TRAP);
         break;
 
     case 0x300:  /*l.csync*/
@@ -1085,7 +1092,7 @@ static void dec_float(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
         if (cpu_R[rb] != 0) {
             tcg_gen_div_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
         } else {
-            /* exception here */
+            gen_exception(dc, EXCP_RANGE);
         }
         break;
 
@@ -1094,7 +1101,7 @@ static void dec_float(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
         if (cpu_R[rb] != 0) {
             tcg_gen_div_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
         } else {
-            /* exception here */
+            gen_exception(dc, EXCP_RANGE);
         }
         break;
 
@@ -1294,7 +1301,7 @@ static void check_breakpoint(CPUOPENRISCState *env, DisasContext *dc)
         QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
             if (bp->pc == dc->pc) {
                 tcg_gen_movi_tl(cpu_pc, dc->pc);
-                /* exception here */
+                gen_exception(dc, EXCP_DEBUG);
                 dc->is_jmp = DISAS_UPDATE;
             }
         }
@@ -1403,7 +1410,7 @@ static inline void gen_intermediate_code_internal(CPUOPENRISCState *env,
         if (dc->is_jmp == DISAS_NEXT) {
             tcg_gen_movi_tl(cpu_pc, dc->pc);
         }
-        /* exception here*/
+        gen_exception(dc, EXCP_DEBUG);
     } else {
         switch (dc->is_jmp) {
         case DISAS_NEXT:
Signature · 1 line
1.7.9.5
#33 lore raw

Re: [Qemu-devel] [PATCH 05/15] Openrisc: add exception support

Quoted 47 lines · Jia Liu

add the openrisc exception support.

---
 Makefile.target               |    2 +- target-openrisc/excp.c        |   27 +++++++++++++++++++++++++++ target-openrisc/excp.h        |   28 ++++++++++++++++++++++++++++ target-openrisc/excp_helper.c |   28 ++++++++++++++++++++++++++++ target-openrisc/helper.h      |    3 +++ target-openrisc/translate.c   |   25 ++++++++++++++++---------

 6 files changed, 103 insertions(+), 10 deletions(-)

 create mode 100644 target-openrisc/excp.c
 create mode 100644 target-openrisc/excp.h
 create mode 100644 target-openrisc/excp_helper.c

Makefile.target +0 -0
index 975c9a8..ed5f0b0 100644
@@ -101,7 +101,7 @@ endif

 libobj-$(TARGET_SPARC) += int32_helper.o libobj-$(TARGET_SPARC64) += int64_helper.o libobj-$(TARGET_ALPHA) += int_helper.o fpu_helper.o sys_helper.o mem_helper.o

Makefile.target +1 -1
-libobj-$(TARGET_OPENRISC) += intrp_helper.o mem.o mem_helper.o
+libobj-$(TARGET_OPENRISC) += excp.o excp_helper.o intrp_helper.o mem.o mem_helper.o

 libobj-y += disas.o
 libobj-$(CONFIG_TCI_DIS) += tci-dis.o

target-openrisc/excp.c new file +13 -0
new file mode 100644
index 0000000..fc9391a
@@ -0,0 +1,27 @@
+/*
+ *  Openrisc exception.
+ *
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU

target-openrisc/excp.c line 23

Quoted 10 lines
target-openrisc/excp.c new file +10 -0
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "cpu.h"
+#include "excp.h"
+
+void raise_exception(CPUOPENRISCState *env, uint32_t excp)

QEMU_NORETURN?

target-openrisc/excp.c line 27

Quoted 4 lines
target-openrisc/excp.c new file +4 -0
+{
+    env->exception_index = excp;
+    cpu_loop_exit(env);
+}

I'd just merge this file with excp_helper.c for simplicity, though it's not incorrect either.

Quoted 48 lines
target-openrisc/excp.h new file +28 -0
new file mode 100644
index 0000000..4c32e46
@@ -0,0 +1,28 @@
+/*
+ *  Openrisc exception header.
+ *
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef QEMU_OPENRISC_EXCP_H
+#define QEMU_OPENRISC_EXCP_H
+
+#include "cpu.h"
+#include "qemu-common.h"
+
+void raise_exception(CPUOPENRISCState *env, uint32_t excp);
+
+#endif /* QEMU_OPENRISC_EXCP_H */
target-openrisc/excp_helper.c new file +8 -0
new file mode 100644
index 0000000..0cad14b
@@ -0,0 +1,28 @@
+/*
+ * OpenRISC exception helper routines
+ *
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either

target-openrisc/excp_helper.c line 18

Quoted 10 lines
target-openrisc/excp_helper.c new file +10 -0
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA

Please use the web version instead.

Quoted 126 lines
target-openrisc/excp_helper.c new file +10 -0
+ */
+
+#include "cpu.h"
+#include "helper.h"
+#include "excp.h"
+
+void HELPER(exception)(CPUOPENRISCState *env, uint32_t excp)
+{
+    raise_exception(env, excp);
+}
target-openrisc/helper.h +3 -0
index bb394ad..a0fb8c4 100644
@@ -20,6 +20,9 @@
 
  #include "def-helper.h"
 
+/* exception */
+DEF_HELPER_FLAGS_2(exception, 0, void, env, i32)
+
  /* interrupt */
  DEF_HELPER_FLAGS_1(rfe, 0, void, env)
 
target-openrisc/translate.c +9 -2
index dd0240c..a1264c9 100644
@@ -155,6 +155,13 @@ static inline void gen_load_gpr(TCGv t, unsigned int reg)
     }
  }
 
+static void gen_exception(DisasContext *dc, unsigned int excp)
+{
+    TCGv_i32 tmp = tcg_const_i32(excp);
+    gen_helper_exception(cpu_env, tmp);
+    tcg_temp_free(tmp);
+}
+
  static void gen_goto_tb(DisasContext *dc, int n, target_ulong dest)
  {
     TranslationBlock *tb;
@@ -167,7 +174,7 @@ static void gen_goto_tb(DisasContext *dc, int n, target_ulong dest)
     } else {
         tcg_gen_movi_tl(cpu_pc, dest);
         if (dc->singlestep_enabled) {
-            /* exception here */
+            gen_exception(dc, EXCP_DEBUG);
         }
         tcg_gen_exit_tb(0);
     }
@@ -281,7 +288,7 @@ static void dec_calc(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
             if (rb != 0) {
                 tcg_gen_div_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
             } else {
-                /* exception here */
+                gen_exception(dc, EXCP_RANGE);
             }
             break;

        default:

target-openrisc/translate.c +1 -1
@@ -296,7 +303,7 @@ static void dec_calc(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
             if (rb != 0) {
                 tcg_gen_divu_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
             } else {
-                /* exception here */
+                gen_exception(dc, EXCP_RANGE);
             }
             break;

        default:

target-openrisc/translate.c +0 -0
@@ -1003,14 +1010,14 @@ static void dec_sys(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)

    case 0x000:  /*l.sys*/
        /*LOG_DIS("l.sys %d\n", K16);*/
        tcg_gen_movi_tl(cpu_pc, dc->pc);
-        /* exception here */
+        gen_exception(dc, EXCP_SYSCALL);
        dc->is_jmp = DISAS_UPDATE;
        break;

    case 0x100:  /*l.trap*/
        /*LOG_DIS("l.trap %d\n", K16);*/
        tcg_gen_movi_tl(cpu_pc, dc->pc);
-        /* exception here */
+        gen_exception(dc, EXCP_TRAP);
        break;

    case 0x300:  /*l.csync*/

target-openrisc/translate.c +4 -4
@@ -1085,7 +1092,7 @@ static void dec_float(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
         if (cpu_R[rb] != 0) {
             tcg_gen_div_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
         } else {
-            /* exception here */
+            gen_exception(dc, EXCP_RANGE);
         }
         break;
 
@@ -1094,7 +1101,7 @@ static void dec_float(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
         if (cpu_R[rb] != 0) {
             tcg_gen_div_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
         } else {
-            /* exception here */
+            gen_exception(dc, EXCP_RANGE);
         }
         break;
 
@@ -1294,7 +1301,7 @@ static void check_breakpoint(CPUOPENRISCState *env, DisasContext *dc)
         QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
             if (bp->pc == dc->pc) {
                 tcg_gen_movi_tl(cpu_pc, dc->pc);
-                /* exception here */
+                gen_exception(dc, EXCP_DEBUG);
                 dc->is_jmp = DISAS_UPDATE;
             }
         }
@@ -1403,7 +1410,7 @@ static inline void gen_intermediate_code_internal(CPUOPENRISCState *env,
         if (dc->is_jmp == DISAS_NEXT) {
             tcg_gen_movi_tl(cpu_pc, dc->pc);
         }
-        /* exception here*/
+        gen_exception(dc, EXCP_DEBUG);
     } else {
         switch (dc->is_jmp) {

        case DISAS_NEXT:
--
1.7.9.5

author
#34 lore raw

Re: [Qemu-devel] [PATCH 05/15] Openrisc: add exception support

Hi Blue,

Quoted 52 lines · Blue Swirl
Quoted 51 lines · Jia Liu

add the openrisc exception support.

---
 Makefile.target               |    2 +- target-openrisc/excp.c        |   27 +++++++++++++++++++++++++++ target-openrisc/excp.h        |   28 ++++++++++++++++++++++++++++ target-openrisc/excp_helper.c |   28 ++++++++++++++++++++++++++++ target-openrisc/helper.h      |    3 +++ target-openrisc/translate.c   |   25 ++++++++++++++++---------

 6 files changed, 103 insertions(+), 10 deletions(-)

 create mode 100644 target-openrisc/excp.c
 create mode 100644 target-openrisc/excp.h
 create mode 100644 target-openrisc/excp_helper.c

Makefile.target +0 -0
index 975c9a8..ed5f0b0 100644
@@ -101,7 +101,7 @@ endif

 libobj-$(TARGET_SPARC) += int32_helper.o libobj-$(TARGET_SPARC64) += int64_helper.o libobj-$(TARGET_ALPHA) += int_helper.o fpu_helper.o sys_helper.o mem_helper.o

Makefile.target +1 -1
-libobj-$(TARGET_OPENRISC) += intrp_helper.o mem.o mem_helper.o
+libobj-$(TARGET_OPENRISC) += excp.o excp_helper.o intrp_helper.o mem.o mem_helper.o

 libobj-y += disas.o
 libobj-$(CONFIG_TCI_DIS) += tci-dis.o

target-openrisc/excp.c new file +17 -0
new file mode 100644
index 0000000..fc9391a
@@ -0,0 +1,27 @@
+/*
+ *  Openrisc exception.
+ *
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Quoted 9 lines

target-openrisc/excp.c line 23

Quoted 6 lines
target-openrisc/excp.c new file +6 -0
+ */
+
+#include "cpu.h"
+#include "excp.h"
+
+void raise_exception(CPUOPENRISCState *env, uint32_t excp)

QEMU_NORETURN?

Does it have to return?

Quoted 60 lines

target-openrisc/excp.c line 27

Quoted 4 lines
target-openrisc/excp.c new file +4 -0
+{
+    env->exception_index = excp;
+    cpu_loop_exit(env);
+}

I'd just merge this file with excp_helper.c for simplicity, though it's not incorrect either.

Quoted 52 lines
target-openrisc/excp.h new file +28 -0
new file mode 100644
index 0000000..4c32e46
@@ -0,0 +1,28 @@
+/*
+ *  Openrisc exception header.
+ *
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef QEMU_OPENRISC_EXCP_H
+#define QEMU_OPENRISC_EXCP_H
+
+#include "cpu.h"
+#include "qemu-common.h"
+
+void raise_exception(CPUOPENRISCState *env, uint32_t excp);
+
+#endif /* QEMU_OPENRISC_EXCP_H */
target-openrisc/excp_helper.c new file +12 -0
new file mode 100644
index 0000000..0cad14b
@@ -0,0 +1,28 @@
+/*
+ * OpenRISC exception helper routines
+ *
+ *  Copyright (c) 2011-2012 Jia Liu <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
Quoted 9 lines

target-openrisc/excp_helper.c line 18

Quoted 6 lines
target-openrisc/excp_helper.c new file +6 -0
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA

Please use the web version instead.

fixed.

Quoted 126 lines
Quoted 126 lines
target-openrisc/excp_helper.c new file +10 -0
+ */
+
+#include "cpu.h"
+#include "helper.h"
+#include "excp.h"
+
+void HELPER(exception)(CPUOPENRISCState *env, uint32_t excp)
+{
+    raise_exception(env, excp);
+}
target-openrisc/helper.h +3 -0
index bb394ad..a0fb8c4 100644
@@ -20,6 +20,9 @@
 
  #include "def-helper.h"
 
+/* exception */
+DEF_HELPER_FLAGS_2(exception, 0, void, env, i32)
+
  /* interrupt */
  DEF_HELPER_FLAGS_1(rfe, 0, void, env)
 
target-openrisc/translate.c +9 -2
index dd0240c..a1264c9 100644
@@ -155,6 +155,13 @@ static inline void gen_load_gpr(TCGv t, unsigned int reg)
     }
  }
 
+static void gen_exception(DisasContext *dc, unsigned int excp)
+{
+    TCGv_i32 tmp = tcg_const_i32(excp);
+    gen_helper_exception(cpu_env, tmp);
+    tcg_temp_free(tmp);
+}
+
  static void gen_goto_tb(DisasContext *dc, int n, target_ulong dest)
  {
     TranslationBlock *tb;
@@ -167,7 +174,7 @@ static void gen_goto_tb(DisasContext *dc, int n, target_ulong dest)
     } else {
         tcg_gen_movi_tl(cpu_pc, dest);
         if (dc->singlestep_enabled) {
-            /* exception here */
+            gen_exception(dc, EXCP_DEBUG);
         }
         tcg_gen_exit_tb(0);
     }
@@ -281,7 +288,7 @@ static void dec_calc(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
             if (rb != 0) {
                 tcg_gen_div_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
             } else {
-                /* exception here */
+                gen_exception(dc, EXCP_RANGE);
             }
             break;

        default:

target-openrisc/translate.c +1 -1
@@ -296,7 +303,7 @@ static void dec_calc(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
             if (rb != 0) {
                 tcg_gen_divu_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
             } else {
-                /* exception here */
+                gen_exception(dc, EXCP_RANGE);
             }
             break;

        default:

target-openrisc/translate.c +0 -0
@@ -1003,14 +1010,14 @@ static void dec_sys(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)

    case 0x000:  /*l.sys*/
        /*LOG_DIS("l.sys %d\n", K16);*/
        tcg_gen_movi_tl(cpu_pc, dc->pc);
-        /* exception here */
+        gen_exception(dc, EXCP_SYSCALL);
        dc->is_jmp = DISAS_UPDATE;
        break;

    case 0x100:  /*l.trap*/
        /*LOG_DIS("l.trap %d\n", K16);*/
        tcg_gen_movi_tl(cpu_pc, dc->pc);
-        /* exception here */
+        gen_exception(dc, EXCP_TRAP);
        break;

    case 0x300:  /*l.csync*/

target-openrisc/translate.c +4 -4
@@ -1085,7 +1092,7 @@ static void dec_float(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
         if (cpu_R[rb] != 0) {
             tcg_gen_div_i64(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
         } else {
-            /* exception here */
+            gen_exception(dc, EXCP_RANGE);
         }
         break;
 
@@ -1094,7 +1101,7 @@ static void dec_float(DisasContext *dc, CPUOPENRISCState *env, uint32_t insn)
         if (cpu_R[rb] != 0) {
             tcg_gen_div_tl(cpu_R[rd], cpu_R[ra], cpu_R[rb]);
         } else {
-            /* exception here */
+            gen_exception(dc, EXCP_RANGE);
         }
         break;
 
@@ -1294,7 +1301,7 @@ static void check_breakpoint(CPUOPENRISCState *env, DisasContext *dc)
         QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
             if (bp->pc == dc->pc) {
                 tcg_gen_movi_tl(cpu_pc, dc->pc);
-                /* exception here */
+                gen_exception(dc, EXCP_DEBUG);
                 dc->is_jmp = DISAS_UPDATE;
             }
         }
@@ -1403,7 +1410,7 @@ static inline void gen_intermediate_code_internal(CPUOPENRISCState *env,
         if (dc->is_jmp == DISAS_NEXT) {
             tcg_gen_movi_tl(cpu_pc, dc->pc);
         }
-        /* exception here*/
+        gen_exception(dc, EXCP_DEBUG);
     } else {
         switch (dc->is_jmp) {

        case DISAS_NEXT:
--
1.7.9.5