Skip to content

Commit e714348

Browse files
committed
Add Unicorn/AVR support.
Signed-off-by: Glenn Baker <[email protected]>
1 parent 380625b commit e714348

File tree

8 files changed

+447
-2
lines changed

8 files changed

+447
-2
lines changed

CMakeLists.txt

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ option(UNICORN_FUZZ "Enable fuzzing" OFF)
6767
option(UNICORN_LOGGING "Enable logging" OFF)
6868
option(UNICORN_BUILD_TESTS "Build unicorn tests" ${PROJECT_IS_TOP_LEVEL})
6969
option(UNICORN_INSTALL "Enable unicorn installation" ${PROJECT_IS_TOP_LEVEL})
70-
set(UNICORN_ARCH "x86;arm;aarch64;riscv;mips;sparc;m68k;ppc;s390x;tricore" CACHE STRING "Enabled unicorn architectures")
70+
set(UNICORN_ARCH "x86;arm;aarch64;riscv;mips;sparc;m68k;ppc;s390x;tricore;avr" CACHE STRING "Enabled unicorn architectures")
7171
option(UNICORN_TRACER "Trace unicorn execution" OFF)
7272

7373
foreach(ARCH_LOOP ${UNICORN_ARCH})
@@ -254,6 +254,11 @@ else()
254254
set(UNICORN_TARGET_ARCH "tricore")
255255
break()
256256
endif()
257+
string(FIND ${UC_COMPILER_MACRO} "__AVR__" UC_RET)
258+
if (${UC_RET} GREATER_EQUAL "0")
259+
set(UNICORN_TARGET_ARCH "avr")
260+
break()
261+
endif()
257262
message(FATAL_ERROR "Unknown host compiler: ${CMAKE_C_COMPILER}.")
258263
endwhile(TRUE)
259264
endif()
@@ -289,6 +294,9 @@ else()
289294
if (UNICORN_HAS_TRICORE)
290295
set (EXTRA_CFLAGS "${EXTRA_CFLAGS}-DUNICORN_HAS_TRICORE ")
291296
endif()
297+
if (UNICORN_HAS_AVR)
298+
set (EXTRA_CFLAGS "${EXTRA_CFLAGS}-DUNICORN_HAS_AVR ")
299+
endif()
292300

293301
set(EXTRA_CFLAGS "${EXTRA_CFLAGS}-fPIC")
294302
if(ANDROID_ABI)
@@ -340,6 +348,9 @@ else()
340348
if (UNICORN_HAS_TRICORE)
341349
set (TARGET_LIST "${TARGET_LIST}tricore-softmmu, ")
342350
endif()
351+
if (UNICORN_HAS_AVR)
352+
set (TARGET_LIST "${TARGET_LIST}avr-softmmu, ")
353+
endif()
343354
set(TARGET_LIST "${TARGET_LIST} ")
344355

345356
# GEN config-host.mak & target directories
@@ -437,6 +448,12 @@ else()
437448
OUTPUT_FILE ${CMAKE_BINARY_DIR}/tricore-softmmu/config-target.h
438449
)
439450
endif()
451+
if (UNICORN_HAS_AVR)
452+
execute_process(COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/qemu/scripts/create_config
453+
INPUT_FILE ${CMAKE_BINARY_DIR}/avr-softmmu/config-target.mak
454+
OUTPUT_FILE ${CMAKE_BINARY_DIR}/avr-softmmu/config-target.h
455+
)
456+
endif()
440457
add_compile_options(
441458
${UNICORN_CFLAGS}
442459
-I${CMAKE_CURRENT_SOURCE_DIR}/qemu/tcg/${UNICORN_TARGET_ARCH}
@@ -1135,6 +1152,34 @@ endif()
11351152
endif()
11361153

11371154

1155+
if (UNICORN_HAS_AVR)
1156+
add_library(avr-softmmu STATIC
1157+
${UNICORN_ARCH_COMMON}
1158+
1159+
qemu/target/avr/cpu.c
1160+
qemu/target/avr/helper.c
1161+
qemu/target/avr/translate.c
1162+
qemu/target/avr/unicorn.c
1163+
)
1164+
1165+
if(MSVC)
1166+
target_compile_options(avr-softmmu PRIVATE
1167+
-DNEED_CPU_H
1168+
/FIavr.h
1169+
/I${CMAKE_CURRENT_SOURCE_DIR}/msvc/avr-softmmu
1170+
/I${CMAKE_CURRENT_SOURCE_DIR}/qemu/target/avr
1171+
)
1172+
else()
1173+
target_compile_options(avr-softmmu PRIVATE
1174+
-DNEED_CPU_H
1175+
-include avr.h
1176+
-I${CMAKE_BINARY_DIR}/avr-softmmu
1177+
-I${CMAKE_CURRENT_SOURCE_DIR}/qemu/target/avr
1178+
)
1179+
endif()
1180+
endif()
1181+
1182+
11381183
set(UNICORN_SRCS
11391184
uc.c
11401185

@@ -1309,6 +1354,11 @@ if (UNICORN_HAS_TRICORE)
13091354
target_link_libraries(tricore-softmmu unicorn-common)
13101355
set(UNICORN_TEST_FILE ${UNICORN_TEST_FILE} test_tricore)
13111356
endif()
1357+
if (UNICORN_HAS_AVR)
1358+
set(UNICORN_COMPILE_OPTIONS ${UNICORN_COMPILE_OPTIONS} -DUNICORN_HAS_AVR)
1359+
set(UNICORN_LINK_LIBRARIES ${UNICORN_LINK_LIBRARIES} avr-softmmu)
1360+
target_link_libraries(avr-softmmu unicorn-common)
1361+
endif()
13121362

13131363
# Extra tests
13141364
set(UNICORN_TEST_FILE ${UNICORN_TEST_FILE} test_mem)

include/uc_priv.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
(UC_MODE_RISCV32 | UC_MODE_RISCV64 | UC_MODE_LITTLE_ENDIAN)
3636
#define UC_MODE_S390X_MASK (UC_MODE_BIG_ENDIAN)
3737
#define UC_MODE_TRICORE_MASK (UC_MODE_LITTLE_ENDIAN)
38+
#define UC_MODE_AVR_MASK (UC_MODE_LITTLE_ENDIAN)
3839

3940
#define ARR_SIZE(a) (sizeof(a) / sizeof(a[0]))
4041

include/unicorn/avr.h

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/* This file is released under LGPL2.
2+
See COPYING.LGPL2 in root directory for more details
3+
*/
4+
5+
/*
6+
Created for Unicorn Engine by Glenn Baker <[email protected]>, 2024
7+
*/
8+
9+
#ifndef UNICORN_AVR_H
10+
#define UNICORN_AVR_H
11+
12+
#ifdef __cplusplus
13+
extern "C" {
14+
#endif
15+
16+
#ifdef _MSC_VER
17+
#pragma warning(disable : 4201)
18+
#endif
19+
20+
//> AVR CPU
21+
typedef enum uc_cpu_avr {
22+
UC_CPU_AVR_AVR5 = 5,
23+
UC_CPU_AVR_AVR51 = 51,
24+
UC_CPU_AVR_AVR6 = 6,
25+
} uc_cpu_avr;
26+
27+
//> AVR registers
28+
typedef enum uc_avr_reg {
29+
UC_AVR_REG_INVALID = 0,
30+
31+
// General purpose registers (GPR)
32+
UC_AVR_REG_R0 = 1,
33+
UC_AVR_REG_R1,
34+
UC_AVR_REG_R2,
35+
UC_AVR_REG_R3,
36+
UC_AVR_REG_R4,
37+
UC_AVR_REG_R5,
38+
UC_AVR_REG_R6,
39+
UC_AVR_REG_R7,
40+
UC_AVR_REG_R8,
41+
UC_AVR_REG_R9,
42+
UC_AVR_REG_R10,
43+
UC_AVR_REG_R11,
44+
UC_AVR_REG_R12,
45+
UC_AVR_REG_R13,
46+
UC_AVR_REG_R14,
47+
UC_AVR_REG_R15,
48+
UC_AVR_REG_R16,
49+
UC_AVR_REG_R17,
50+
UC_AVR_REG_R18,
51+
UC_AVR_REG_R19,
52+
UC_AVR_REG_R20,
53+
UC_AVR_REG_R21,
54+
UC_AVR_REG_R22,
55+
UC_AVR_REG_R23,
56+
UC_AVR_REG_R24,
57+
UC_AVR_REG_R25,
58+
UC_AVR_REG_R26,
59+
UC_AVR_REG_R27,
60+
UC_AVR_REG_R28,
61+
UC_AVR_REG_R29,
62+
UC_AVR_REG_R30,
63+
UC_AVR_REG_R31,
64+
65+
UC_AVR_REG_PC,
66+
UC_AVR_REG_SP,
67+
68+
UC_AVR_REG_RAMPD = UC_AVR_REG_PC + 16 + 8,
69+
UC_AVR_REG_RAMPX,
70+
UC_AVR_REG_RAMPY,
71+
UC_AVR_REG_RAMPZ,
72+
UC_AVR_REG_EIND,
73+
UC_AVR_REG_SPL,
74+
UC_AVR_REG_SPH,
75+
UC_AVR_REG_SREG,
76+
77+
//> Alias registers
78+
UC_AVR_REG_Xhi = UC_AVR_REG_R27,
79+
UC_AVR_REG_Xlo = UC_AVR_REG_R26,
80+
UC_AVR_REG_Yhi = UC_AVR_REG_R29,
81+
UC_AVR_REG_Ylo = UC_AVR_REG_R28,
82+
UC_AVR_REG_Zhi = UC_AVR_REG_R31,
83+
UC_AVR_REG_Zlo = UC_AVR_REG_R30,
84+
} uc_avr_reg;
85+
86+
#ifdef __cplusplus
87+
}
88+
#endif
89+
90+
#endif /* UNICORN_AVR_H */

include/unicorn/unicorn.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ typedef size_t uc_hook;
3636
#include "riscv.h"
3737
#include "s390x.h"
3838
#include "tricore.h"
39+
#include "avr.h"
3940

4041
#ifdef __GNUC__
4142
#define DEFAULT_VISIBILITY __attribute__((visibility("default")))
@@ -106,6 +107,7 @@ typedef enum uc_arch {
106107
UC_ARCH_RISCV, // RISCV architecture
107108
UC_ARCH_S390X, // S390X architecture
108109
UC_ARCH_TRICORE, // TriCore architecture
110+
UC_ARCH_AVR, // AVR architecture
109111
UC_ARCH_MAX,
110112
} uc_arch;
111113

qemu/configure

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,8 @@ elif check_define __aarch64__ ; then
491491
cpu="aarch64"
492492
elif check_define __tricore__ ; then
493493
cpu="tricore"
494+
elif check_define __AVR__ ; then
495+
cpu="avr"
494496
else
495497
cpu=$(uname -m)
496498
fi
@@ -534,6 +536,10 @@ case "$cpu" in
534536
cpu="tricore"
535537
supported_cpu="yes"
536538
;;
539+
avr)
540+
cpu="avr"
541+
supported_cpu="yes"
542+
;;
537543
*)
538544
# This will result in either an error or falling back to TCI later
539545
ARCH=unknown
@@ -859,7 +865,7 @@ default_target_list="aarch64-softmmu \
859865
arm-softmmu m68k-softmmu mips64el-softmmu mips64-softmmu mipsel-softmmu \
860866
mips-softmmu ppc64-softmmu ppc-softmmu sparc64-softmmu sparc-softmmu \
861867
x86_64-softmmu riscv32-softmmu riscv64-softmmu s390x-softmmu \
862-
tricore-softmmu"
868+
tricore-softmmu avr-softmmu"
863869

864870
if test x"$show_help" = x"yes" ; then
865871
cat << EOF
@@ -2789,6 +2795,10 @@ case "$target_name" in
27892795
TARGET_ARCH=tricore
27902796
TARGET_BASE_ARCH=tricore
27912797
;;
2798+
avr)
2799+
TARGET_ARCH=avr
2800+
TARGET_BASE_ARCH=avr
2801+
;;
27922802
unicore32)
27932803
;;
27942804
xtensa|xtensaeb)

0 commit comments

Comments
 (0)