Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AVR support #2021

Open
wants to merge 28 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
e24ac1d
Import AVR target from QEMU v5.2.0.
glennsec Mar 27, 2024
4c624d8
qemu/target/avr: Register AVR support with the rest of QEMU
michaelrolnik Jan 24, 2020
717a356
qemu/target/avr: pregenerate decoder.
glennsec Mar 27, 2024
5463d75
qemu/target/avr: pregenerate overrides (extern symbols).
glennsec Mar 28, 2024
ec99cf6
qemu/target/avr: add initial Unicorn integration.
glennsec Mar 27, 2024
33a6475
qemu/target/avr: add support for block and insn hooks.
glennsec Apr 3, 2024
258707f
Add Unicorn/AVR support.
glennsec Mar 27, 2024
aeeb1fa
Use alternate Flash program memory (code) base for AVR targets.
glennsec Apr 4, 2024
8b999a4
AVR: rework ARCH/CPU models.
glennsec Apr 24, 2024
5aa68f6
AVR: add 16-bit & 32-bit register accessors.
glennsec Apr 24, 2024
b0f2243
AVR: fix selection and validation of CPU model.
glennsec Apr 24, 2024
6c1e241
Add AVR code samples.
glennsec Mar 28, 2024
5f64817
Add AVR unit tests.
glennsec Apr 3, 2024
a33597b
bindings: update const generator for AVR.
glennsec Mar 27, 2024
e35ef10
bindings: regenerate constants for AVR support.
glennsec Apr 24, 2024
f214053
build: update build systems for AVR support.
glennsec Oct 2, 2024
3fac14c
AVR: drop built-in disassembler tools.
glennsec Oct 2, 2024
bc75094
AVR: update for new v2.1.x APIs.
glennsec Oct 2, 2024
30e359e
AVR: fix cpu_reset() prototype.
glennsec Oct 2, 2024
007075e
AVR: drop unused error_report() function override.
glennsec Oct 2, 2024
81292cc
AVR: fix compilation on non-Linux platforms.
glennsec Oct 2, 2024
1256f79
AVR: move declarations at the begining of the function.
glennsec Oct 8, 2024
8dd5ac9
AVR: mention new architecture into README.md.
glennsec Oct 8, 2024
88b5ac2
AVR: add self to CREDITS.txt
glennsec Oct 8, 2024
c19dc1c
AVR: fix build with older compilers.
glennsec Oct 8, 2024
063ed85
bindings: add avr constants to python binding module.
glennsec Oct 8, 2024
ebf4bfb
rust: add AVR architecture support.
glennsec Oct 8, 2024
7fa65eb
AVR: sync changes from "dev" branch (a78d690d).
glennsec Jan 17, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ option(UNICORN_FUZZ "Enable fuzzing" OFF)
option(UNICORN_LOGGING "Enable logging" OFF)
option(UNICORN_BUILD_TESTS "Build unicorn tests" ${PROJECT_IS_TOP_LEVEL})
option(UNICORN_INSTALL "Enable unicorn installation" ${PROJECT_IS_TOP_LEVEL})
set(UNICORN_ARCH "x86;arm;aarch64;riscv;mips;sparc;m68k;ppc;s390x;tricore" CACHE STRING "Enabled unicorn architectures")
set(UNICORN_ARCH "x86;arm;aarch64;riscv;mips;sparc;m68k;ppc;s390x;tricore;avr" CACHE STRING "Enabled unicorn architectures")
option(UNICORN_TRACER "Trace unicorn execution" OFF)

foreach(ARCH_LOOP ${UNICORN_ARCH})
Expand Down Expand Up @@ -273,6 +273,11 @@ else()
set(UNICORN_TARGET_ARCH "tricore")
break()
endif()
string(FIND ${UC_COMPILER_MACRO} "__AVR__" UC_RET)
if (${UC_RET} GREATER_EQUAL "0")
set(UNICORN_TARGET_ARCH "avr")
break()
endif()
message(FATAL_ERROR "Unknown host compiler: ${CMAKE_C_COMPILER}.")
endwhile(TRUE)
endif()
Expand Down Expand Up @@ -308,6 +313,9 @@ else()
if (UNICORN_HAS_TRICORE)
set (EXTRA_CFLAGS "${EXTRA_CFLAGS}-DUNICORN_HAS_TRICORE ")
endif()
if (UNICORN_HAS_AVR)
set (EXTRA_CFLAGS "${EXTRA_CFLAGS}-DUNICORN_HAS_AVR ")
endif()

set(EXTRA_CFLAGS "${EXTRA_CFLAGS}-fPIC")
if(ANDROID_ABI)
Expand Down Expand Up @@ -359,6 +367,9 @@ else()
if (UNICORN_HAS_TRICORE)
set (TARGET_LIST "${TARGET_LIST}tricore-softmmu, ")
endif()
if (UNICORN_HAS_AVR)
set (TARGET_LIST "${TARGET_LIST}avr-softmmu, ")
endif()
set(TARGET_LIST "${TARGET_LIST} ")

# GEN config-host.mak & target directories
Expand Down Expand Up @@ -456,6 +467,12 @@ else()
OUTPUT_FILE ${CMAKE_BINARY_DIR}/tricore-softmmu/config-target.h
)
endif()
if (UNICORN_HAS_AVR)
execute_process(COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/qemu/scripts/create_config
INPUT_FILE ${CMAKE_BINARY_DIR}/avr-softmmu/config-target.mak
OUTPUT_FILE ${CMAKE_BINARY_DIR}/avr-softmmu/config-target.h
)
endif()
add_compile_options(
${UNICORN_CFLAGS}
-I${CMAKE_CURRENT_SOURCE_DIR}/qemu/tcg/${UNICORN_TARGET_ARCH}
Expand Down Expand Up @@ -1154,6 +1171,34 @@ endif()
endif()


if (UNICORN_HAS_AVR)
add_library(avr-softmmu STATIC
${UNICORN_ARCH_COMMON}

qemu/target/avr/cpu.c
qemu/target/avr/helper.c
qemu/target/avr/translate.c
qemu/target/avr/unicorn.c
)

if(MSVC)
target_compile_options(avr-softmmu PRIVATE
-DNEED_CPU_H
/FIavr.h
/I${CMAKE_CURRENT_SOURCE_DIR}/msvc/avr-softmmu
/I${CMAKE_CURRENT_SOURCE_DIR}/qemu/target/avr
)
else()
target_compile_options(avr-softmmu PRIVATE
-DNEED_CPU_H
-include avr.h
-I${CMAKE_BINARY_DIR}/avr-softmmu
-I${CMAKE_CURRENT_SOURCE_DIR}/qemu/target/avr
)
endif()
endif()


set(UNICORN_SRCS
uc.c

Expand Down Expand Up @@ -1328,6 +1373,13 @@ if (UNICORN_HAS_TRICORE)
target_link_libraries(tricore-softmmu unicorn-common)
set(UNICORN_TEST_FILE ${UNICORN_TEST_FILE} test_tricore)
endif()
if (UNICORN_HAS_AVR)
set(UNICORN_COMPILE_OPTIONS ${UNICORN_COMPILE_OPTIONS} -DUNICORN_HAS_AVR)
set(UNICORN_LINK_LIBRARIES ${UNICORN_LINK_LIBRARIES} avr-softmmu)
set(UNICORN_SAMPLE_FILE ${UNICORN_SAMPLE_FILE} sample_avr)
target_link_libraries(avr-softmmu unicorn-common)
set(UNICORN_TEST_FILE ${UNICORN_TEST_FILE} test_avr)
endif()

# Extra tests
set(UNICORN_TEST_FILE ${UNICORN_TEST_FILE} test_mem)
Expand Down
1 change: 1 addition & 0 deletions CREDITS.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,4 @@ Ziqiao Kong (lazymio): uc_context_free() API and various bug fix & improvement.
Sven Almgren (blindmatrix): bug fix
Chenxu Wu (kabeor): Documentation
Philipp Takacs: virtual tlb, memory snapshots
Glenn Baker: AVR architecture support
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pkg-config = { version = "0.3" }
[features]
default = ["arch_all"]
dynamic_linkage = []
arch_all = ["arch_x86", "arch_arm", "arch_aarch64", "arch_riscv", "arch_mips", "arch_sparc", "arch_m68k", "arch_ppc", "arch_s390x", "arch_tricore"]
arch_all = ["arch_x86", "arch_arm", "arch_aarch64", "arch_riscv", "arch_mips", "arch_sparc", "arch_m68k", "arch_ppc", "arch_s390x", "arch_tricore", "arch_avr"]
arch_x86 = []
arch_arm = []
# NOTE: unicorn-c only separates on top-level arch name,
Expand All @@ -55,3 +55,4 @@ arch_m68k = []
arch_ppc = []
arch_s390x = []
arch_tricore = []
arch_avr = []
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Unicorn is a lightweight, multi-platform, multi-architecture CPU emulator framew

Unicorn offers some unparalleled features:

- Multi-architecture: ARM, ARM64 (ARMv8), M68K, MIPS, PowerPC, RISCV, SPARC, S390X, TriCore and X86 (16, 32, 64-bit)
- Multi-architecture: ARM, ARM64 (ARMv8), AVR, M68K, MIPS, PowerPC, RISCV, SPARC, S390X, TriCore and X86 (16, 32, 64-bit)
- Clean/simple/lightweight/intuitive architecture-neutral API
- Implemented in pure C language, with bindings for Crystal, Clojure, Visual Basic, Perl, Rust, Ruby, Python, Java, .NET, Go, Delphi/Free Pascal, Haskell, Pharo, Lua and Zig.
- Native support for Windows & *nix (with Mac OSX, Linux, Android, *BSD & Solaris confirmed)
Expand Down
9 changes: 8 additions & 1 deletion bindings/const_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

INCL_DIR = os.path.join('..', 'include', 'unicorn')

include = [ 'arm.h', 'arm64.h', 'mips.h', 'x86.h', 'sparc.h', 'm68k.h', 'ppc.h', 'riscv.h', 's390x.h', 'tricore.h', 'unicorn.h' ]
include = [ 'arm.h', 'arm64.h', 'avr.h', 'mips.h', 'x86.h', 'sparc.h', 'm68k.h', 'ppc.h', 'riscv.h', 's390x.h', 'tricore.h', 'unicorn.h' ]

template = {
'python': {
Expand All @@ -17,6 +17,7 @@
# prefixes for constant filenames of all archs - case sensitive
'arm.h': 'arm',
'arm64.h': 'arm64',
'avr.h': 'avr',
'mips.h': 'mips',
'x86.h': 'x86',
'sparc.h': 'sparc',
Expand All @@ -37,6 +38,7 @@
# prefixes for constant filenames of all archs - case sensitive
'arm.h': 'arm',
'arm64.h': 'arm64',
'avr.h': 'avr',
'mips.h': 'mips',
'x86.h': 'x86',
'sparc.h': 'sparc',
Expand All @@ -57,6 +59,7 @@
# prefixes for constant filenames of all archs - case sensitive
'arm.h': 'arm',
'arm64.h': 'arm64',
'avr.h': 'avr',
'mips.h': 'mips',
'x86.h': 'x86',
'sparc.h': 'sparc',
Expand All @@ -77,6 +80,7 @@
# prefixes for constant filenames of all archs - case sensitive
'arm.h': 'Arm',
'arm64.h': 'Arm64',
'avr.h': 'AVR',
'mips.h': 'Mips',
'x86.h': 'X86',
'sparc.h': 'Sparc',
Expand All @@ -97,6 +101,7 @@
# prefixes for constant filenames of all archs - case sensitive
'arm.h': 'Arm',
'arm64.h': 'Arm64',
'avr.h': 'AVR',
'mips.h': 'Mips',
'x86.h': 'X86',
'sparc.h': 'Sparc',
Expand All @@ -117,6 +122,7 @@
# prefixes for constant filenames of all archs - case sensitive
'arm.h': 'Arm',
'arm64.h': 'Arm64',
'avr.h': 'AVR',
'mips.h': 'Mips',
'x86.h': 'X86',
'sparc.h': 'Sparc',
Expand All @@ -137,6 +143,7 @@
# prefixes for constant filenames of all archs - case sensitive
'arm.h': 'arm',
'arm64.h': 'arm64',
'avr.h': 'AVR',
'mips.h': 'mips',
'x86.h': 'x86',
'sparc.h': 'sparc',
Expand Down
155 changes: 155 additions & 0 deletions bindings/dotnet/UnicornEngine/Const/AVR.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// For Unicorn Engine. AUTO-GENERATED FILE, DO NOT EDIT

namespace UnicornEngine.Const

open System

[<AutoOpen>]
module AVR =

// AVR architectures
let UC_AVR_ARCH_AVR1 = 10
let UC_AVR_ARCH_AVR2 = 20
let UC_AVR_ARCH_AVR25 = 25
let UC_AVR_ARCH_AVR3 = 30
let UC_AVR_ARCH_AVR4 = 40
let UC_AVR_ARCH_AVR5 = 50
let UC_AVR_ARCH_AVR51 = 51
let UC_AVR_ARCH_AVR6 = 60
let UC_CPU_AVR_ARCH = 1000

// AVR CPU
let UC_CPU_AVR_ATMEGA16 = 50016
let UC_CPU_AVR_ATMEGA32 = 50032
let UC_CPU_AVR_ATMEGA64 = 50064
let UC_CPU_AVR_ATMEGA128 = 51128
let UC_CPU_AVR_ATMEGA128RFR2 = 51129
let UC_CPU_AVR_ATMEGA1280 = 51130
let UC_CPU_AVR_ATMEGA256 = 60256
let UC_CPU_AVR_ATMEGA256RFR2 = 60257
let UC_CPU_AVR_ATMEGA2560 = 60258

// AVR memory
let UC_AVR_MEM_FLASH = 134217728

// AVR registers

let UC_AVR_REG_INVALID = 0
let UC_AVR_REG_R0 = 1
let UC_AVR_REG_R1 = 2
let UC_AVR_REG_R2 = 3
let UC_AVR_REG_R3 = 4
let UC_AVR_REG_R4 = 5
let UC_AVR_REG_R5 = 6
let UC_AVR_REG_R6 = 7
let UC_AVR_REG_R7 = 8
let UC_AVR_REG_R8 = 9
let UC_AVR_REG_R9 = 10
let UC_AVR_REG_R10 = 11
let UC_AVR_REG_R11 = 12
let UC_AVR_REG_R12 = 13
let UC_AVR_REG_R13 = 14
let UC_AVR_REG_R14 = 15
let UC_AVR_REG_R15 = 16
let UC_AVR_REG_R16 = 17
let UC_AVR_REG_R17 = 18
let UC_AVR_REG_R18 = 19
let UC_AVR_REG_R19 = 20
let UC_AVR_REG_R20 = 21
let UC_AVR_REG_R21 = 22
let UC_AVR_REG_R22 = 23
let UC_AVR_REG_R23 = 24
let UC_AVR_REG_R24 = 25
let UC_AVR_REG_R25 = 26
let UC_AVR_REG_R26 = 27
let UC_AVR_REG_R27 = 28
let UC_AVR_REG_R28 = 29
let UC_AVR_REG_R29 = 30
let UC_AVR_REG_R30 = 31
let UC_AVR_REG_R31 = 32
let UC_AVR_REG_PC = 33
let UC_AVR_REG_SP = 34
let UC_AVR_REG_RAMPD = 57
let UC_AVR_REG_RAMPX = 58
let UC_AVR_REG_RAMPY = 59
let UC_AVR_REG_RAMPZ = 60
let UC_AVR_REG_EIND = 61
let UC_AVR_REG_SPL = 62
let UC_AVR_REG_SPH = 63
let UC_AVR_REG_SREG = 64

// 16-bit coalesced registers
let UC_AVR_REG_R0W = 65
let UC_AVR_REG_R1W = 66
let UC_AVR_REG_R2W = 67
let UC_AVR_REG_R3W = 68
let UC_AVR_REG_R4W = 69
let UC_AVR_REG_R5W = 70
let UC_AVR_REG_R6W = 71
let UC_AVR_REG_R7W = 72
let UC_AVR_REG_R8W = 73
let UC_AVR_REG_R9W = 74
let UC_AVR_REG_R10W = 75
let UC_AVR_REG_R11W = 76
let UC_AVR_REG_R12W = 77
let UC_AVR_REG_R13W = 78
let UC_AVR_REG_R14W = 79
let UC_AVR_REG_R15W = 80
let UC_AVR_REG_R16W = 81
let UC_AVR_REG_R17W = 82
let UC_AVR_REG_R18W = 83
let UC_AVR_REG_R19W = 84
let UC_AVR_REG_R20W = 85
let UC_AVR_REG_R21W = 86
let UC_AVR_REG_R22W = 87
let UC_AVR_REG_R23W = 88
let UC_AVR_REG_R24W = 89
let UC_AVR_REG_R25W = 90
let UC_AVR_REG_R26W = 91
let UC_AVR_REG_R27W = 92
let UC_AVR_REG_R28W = 93
let UC_AVR_REG_R29W = 94
let UC_AVR_REG_R30W = 95

// 32-bit coalesced registers
let UC_AVR_REG_R0D = 97
let UC_AVR_REG_R1D = 98
let UC_AVR_REG_R2D = 99
let UC_AVR_REG_R3D = 100
let UC_AVR_REG_R4D = 101
let UC_AVR_REG_R5D = 102
let UC_AVR_REG_R6D = 103
let UC_AVR_REG_R7D = 104
let UC_AVR_REG_R8D = 105
let UC_AVR_REG_R9D = 106
let UC_AVR_REG_R10D = 107
let UC_AVR_REG_R11D = 108
let UC_AVR_REG_R12D = 109
let UC_AVR_REG_R13D = 110
let UC_AVR_REG_R14D = 111
let UC_AVR_REG_R15D = 112
let UC_AVR_REG_R16D = 113
let UC_AVR_REG_R17D = 114
let UC_AVR_REG_R18D = 115
let UC_AVR_REG_R19D = 116
let UC_AVR_REG_R20D = 117
let UC_AVR_REG_R21D = 118
let UC_AVR_REG_R22D = 119
let UC_AVR_REG_R23D = 120
let UC_AVR_REG_R24D = 121
let UC_AVR_REG_R25D = 122
let UC_AVR_REG_R26D = 123
let UC_AVR_REG_R27D = 124
let UC_AVR_REG_R28D = 125

// Alias registers
let UC_AVR_REG_Xhi = 28
let UC_AVR_REG_Xlo = 27
let UC_AVR_REG_Yhi = 30
let UC_AVR_REG_Ylo = 29
let UC_AVR_REG_Zhi = 32
let UC_AVR_REG_Zlo = 31
let UC_AVR_REG_X = 91
let UC_AVR_REG_Y = 93
let UC_AVR_REG_Z = 95

3 changes: 2 additions & 1 deletion bindings/dotnet/UnicornEngine/Const/Common.fs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ module Common =
let UC_ARCH_RISCV = 8
let UC_ARCH_S390X = 9
let UC_ARCH_TRICORE = 10
let UC_ARCH_MAX = 11
let UC_ARCH_AVR = 11
let UC_ARCH_MAX = 12

let UC_MODE_LITTLE_ENDIAN = 0
let UC_MODE_BIG_ENDIAN = 1073741824
Expand Down
Loading
Loading