#include "qemu/osdep.h"     /* bool, uint32_t, KiB  — used in the #define constants */
#include "qemu/units.h"     /* KiB */
#include "hw/boards.h"      /* MachineState         — first field of MyMachineState */
#include "hw/arm/armv7m.h"  /* ARMv7MState          — embedded field of MyMachineState */
#include "hw/arm/boot.h"    /* struct arm_boot_info */
#include "system/memory.h"  /* MemoryRegion         — two embedded fields */
#include "hw/misc/ugur_mmio_dev.h"  /* UgurSimpleDevState */

/* ── Memory map constants ─────────────────────────────────────── */
#define UGUR_M4_FLASH_BASE      0x08000000U
#define UGUR_M4_FLASH_SIZE      (1024 * KiB)
#define UGUR_M4_FLASH_ALIAS_BASE 0x00000000U
#define UGUR_M4_SRAM_BASE       0x20000000U
#define UGUR_M4_SRAM_SIZE       (128 * KiB)
#define UGUR_M4_CCMRAM_BASE     0x10000000U
#define UGUR_M4_CCMRAM_SIZE     (64 * KiB)

#define UGUR_M4_UART0_BASE      0x40000000U
#define UGUR_M4_TIMER0_BASE     0x40001000U
#define UGUR_M4_GPIO0_BASE      0x40002000U
#define UGUR_MMIO_DEV_BASE      0x40010000U
#define UGUR_M4_NVIC_BASE       0xE000E100U

/* ── NVIC IRQ assignments (match STM32F407 vector table) ──────── */
#define UGUR_M4_TIMER1_IRQ      28  /* TIM2_IRQHandler  in STM32F407         */
#define UGUR_M4_TIMER2_IRQ      29  /* TIM3_IRQHandler  in STM32F407         */
#define UGUR_M4_UART_IRQ        37  /* USART1_IRQHandler in STM32F407        */
#define UGUR_M4_GPIO_IRQ        6   /* EXTI0_IRQHandler in STM32F407         */
#define UGUR_M4_NUM_IRQ         82  /* STM32F407 has 82 external IRQs        */
#define UGUR_MMIO_DEV_IRQ       40  /* EXTI15_10_IRQHandler in STM32F407     */


/* ── Machine state ────────────────────────────────────────────── */
#define TYPE_UGUR_M4_MACHINE   "ugur-m4-machine"
OBJECT_DECLARE_SIMPLE_TYPE(UgurM4State, UGUR_M4_MACHINE)

struct UgurM4State {
    MachineState  parent_obj;

    ARMv7MState   mcu;          /* CPU + NVIC + SysTick — all in one   */

    MemoryRegion  flash;
    MemoryRegion  flash_alias;
    MemoryRegion  sram;
    MemoryRegion  ccmram;
    struct arm_boot_info bootinfo;
};
