다마고치 연구소 한국어판 소스 공개
Paramix Studio / build (push) Canceled after 0s
Paramix Studio / pages (push) Canceled after 0s

This commit is contained in:
syskiller committed 2026-11-10 21:17:44 +09:00
commit db23855630
661 files changed
+503597

No files matched your search

+12
View File
@@ -0,0 +1,12 @@
#!/bin/sh
# Build V3Emu for the Paradise (clang/lld, Cortex-M3). Output: ../../v3.bin
set -e
cd "$(dirname "$0")"
E=../../../emu/src/emu
CF="--target=thumbv7m-none-eabi -mcpu=cortex-m3 -mthumb -ffreestanding -nostdlib -fno-common -Wall -Wno-unused-function -Wno-unused-variable -fno-builtin-memset -DFAST_LCD -DEXIT_KEY -DLCD_FASTCLK -DTONE_FIX -DHW_TONE_RESTART -DWAKE_WHEEL -DBL_SLEEP -DWAKE_FRESH -DSESS_ONCE -DPARA_BACK -DPARA_MB_KEEP -DBL_PWM -DBL_MR3RST -DBAT_SHOW -DV3_FREESTANDING -DV3_ICONS -DV3_LINK -DV3_CLOCK -DCLK_ECO -DECO_RTC -DV3_SETX -DV3_CARE -DASK_NUM -DV3_CLK_RETRY -DPARA_FLASH -DPARA_FLASH_LATE -DPARA_CLEAN_CLK -DV3_TMFAST -DV3_PW -DBAT_SMOOTH -DHW_OPTS -DV3_VOL -DV3_TONE_Q -DV3_CALL_WAKE -DTONE_SAFE -DV3_EE2 -DBAT_GUARD -DBAT_PIC -DSESS_FLAG -I../core $XCF"
O=""
for f in main.c $E/hw.c $E/ask.c $E/flash.c $E/libc.c ../core/v3cpu.c; do
n=$(basename $f .c); OPT=-Os; [ "$n" = v3cpu ] && OPT=-O2; clang $CF $OPT -c $f -o $n.o; O="$O $n.o"; done
ld.lld -T link.ld --no-rosegment $O -o v3.elf
llvm-objcopy -O binary -j .text -j .data -j .init v3.elf v3.bin
cp v3.bin ../../v3.bin; rm -f *.o v3.bin; [ -n "$KEEPELF" ] || rm -f v3.elf; ls -l ../../v3.bin
+99
View File
@@ -0,0 +1,99 @@
/* generated by gen/font.py: custom 5x7 font */
static const unsigned char g_font[96][7]={
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x04,0x04,0x04,0x04,0x04,0x00,0x04},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x19,0x19,0x02,0x04,0x08,0x13,0x13},
{0x0c,0x12,0x14,0x08,0x15,0x12,0x0d},
{0x04,0x04,0x08,0x00,0x00,0x00,0x00},
{0x02,0x04,0x08,0x08,0x08,0x04,0x02},
{0x08,0x04,0x02,0x02,0x02,0x04,0x08},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x04,0x04,0x1f,0x04,0x04,0x00},
{0x00,0x00,0x00,0x00,0x0c,0x04,0x08},
{0x00,0x00,0x00,0x1f,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x0c,0x0c},
{0x01,0x01,0x02,0x04,0x08,0x10,0x10},
{0x0e,0x11,0x13,0x15,0x19,0x11,0x0e},
{0x04,0x0c,0x04,0x04,0x04,0x04,0x0e},
{0x0e,0x11,0x01,0x02,0x04,0x08,0x1f},
{0x1e,0x01,0x01,0x0e,0x01,0x01,0x1e},
{0x02,0x06,0x0a,0x12,0x1f,0x02,0x02},
{0x1f,0x10,0x1e,0x01,0x01,0x11,0x0e},
{0x0e,0x10,0x10,0x1e,0x11,0x11,0x0e},
{0x1f,0x01,0x02,0x04,0x08,0x08,0x08},
{0x0e,0x11,0x11,0x0e,0x11,0x11,0x0e},
{0x0e,0x11,0x11,0x0f,0x01,0x01,0x0e},
{0x00,0x04,0x04,0x00,0x04,0x04,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x02,0x04,0x08,0x10,0x08,0x04,0x02},
{0x00,0x00,0x1f,0x00,0x1f,0x00,0x00},
{0x08,0x04,0x02,0x01,0x02,0x04,0x08},
{0x0e,0x11,0x01,0x02,0x04,0x00,0x04},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x0e,0x11,0x11,0x1f,0x11,0x11,0x11},
{0x1e,0x11,0x11,0x1e,0x11,0x11,0x1e},
{0x0e,0x11,0x10,0x10,0x10,0x11,0x0e},
{0x1e,0x11,0x11,0x11,0x11,0x11,0x1e},
{0x1f,0x10,0x10,0x1e,0x10,0x10,0x1f},
{0x1f,0x10,0x10,0x1e,0x10,0x10,0x10},
{0x0e,0x11,0x10,0x17,0x11,0x11,0x0f},
{0x11,0x11,0x11,0x1f,0x11,0x11,0x11},
{0x0e,0x04,0x04,0x04,0x04,0x04,0x0e},
{0x07,0x02,0x02,0x02,0x02,0x12,0x0c},
{0x11,0x12,0x14,0x18,0x14,0x12,0x11},
{0x10,0x10,0x10,0x10,0x10,0x10,0x1f},
{0x11,0x1b,0x15,0x15,0x11,0x11,0x11},
{0x11,0x19,0x15,0x13,0x11,0x11,0x11},
{0x0e,0x11,0x11,0x11,0x11,0x11,0x0e},
{0x1e,0x11,0x11,0x1e,0x10,0x10,0x10},
{0x0e,0x11,0x11,0x11,0x15,0x12,0x0d},
{0x1e,0x11,0x11,0x1e,0x14,0x12,0x11},
{0x0f,0x10,0x10,0x0e,0x01,0x01,0x1e},
{0x1f,0x04,0x04,0x04,0x04,0x04,0x04},
{0x11,0x11,0x11,0x11,0x11,0x11,0x0e},
{0x11,0x11,0x11,0x11,0x11,0x0a,0x04},
{0x11,0x11,0x11,0x15,0x15,0x15,0x0a},
{0x11,0x11,0x0a,0x04,0x0a,0x11,0x11},
{0x11,0x11,0x0a,0x04,0x04,0x04,0x04},
{0x1f,0x01,0x02,0x04,0x08,0x10,0x1f},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x11,0x0a,0x04,0x0a,0x11},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x11,0x0e,0x11,0x1f,0x11,0x11,0x11},
{0x11,0x0e,0x11,0x11,0x11,0x11,0x0e},
{0x11,0x00,0x11,0x11,0x11,0x11,0x0e},
};
+13
View File
@@ -0,0 +1,13 @@
/* 20x20 icons of V3Emu (bit 0x80000 = left), drawn by mameise; made by gen/icons.py */
static const unsigned int g_icons[10][20]={
{0x00000000,0x00000000,0x00000000,0x00000000,0x00001F80,0x0000E070,0x00010008,0x00014928,0x00012048,0x00010008,0x00010608,0x00008F10,0x00009F90,0x00007FE0,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000},
{0x00000000,0x00000000,0x00000000,0x000036C0,0x00004920,0x00004020,0x00003020,0x000010C0,0x00001080,0x00001F80,0x00002040,0x00004920,0x00008910,0x00008910,0x00008010,0x00009090,0x00004F20,0x00002040,0x00001F80,0x00000000},
{0x00000000,0x00000000,0x00000000,0x00000070,0x000000C8,0x00000088,0x00000188,0x00000118,0x00007F10,0x0000C090,0x00019C60,0x00012420,0x0001C860,0x000150A0,0x00013F20,0x00008040,0x00004080,0x00008040,0x0001FFE0,0x00000000},
{0x00000000,0x00000000,0x0000F000,0x00010800,0x00016800,0x00016838,0x00010878,0x0000F0F8,0x000001F0,0x000003E0,0x000007C0,0x00000F80,0x00000F00,0x00001E00,0x0000F800,0x00007000,0x00003000,0x00001000,0x00000000,0x00000000},
{0x00000000,0x00000000,0x00000008,0x00000004,0x00000012,0x00000009,0x00000024,0x00000010,0x000030C0,0x000079E0,0x0000FFF0,0x0000FFF0,0x0000FFF0,0x0000FFF0,0x00007FE0,0x00003FC0,0x00001F80,0x00000F00,0x00000600,0x00000000},
{0x00000000,0x00000000,0x00000000,0x00000000,0x00000004,0x00007E18,0x0000FF60,0x0001FE00,0x0003FC00,0x0003F87C,0x0003F000,0x0003F000,0x0003F840,0x0003FC20,0x0001FE10,0x0000FF00,0x00007E00,0x00000000,0x00000000,0x00000000},
{0x00000000,0x00000000,0x00000000,0x00000F00,0x00001080,0x00002040,0x00002040,0x00007FE0,0x00008010,0x00010608,0x00010608,0x00010608,0x00013FC8,0x00013FC8,0x00010608,0x00010608,0x00010608,0x00010008,0x0000FFF0,0x00000000},
{0x00000000,0x00000000,0x00000000,0x00001F80,0x00002040,0x00004020,0x00008010,0x00008010,0x0000C030,0x00007FE0,0x00002600,0x00000600,0x00000600,0x00003FC0,0x00007FE0,0x00003FC0,0x00000000,0x00000000,0x00000000,0x00000000},
{0x00000000,0x00000000,0x00000000,0x0001C038,0x000230C4,0x00020904,0x00020604,0x0002C034,0x000230C4,0x00020004,0x0002C034,0x00023044,0x00020004,0x0002C034,0x000200C4,0x00020004,0x0002E074,0x00011988,0x00000600,0x00000000},
{0x00000000,0x00000000,0x00000000,0x00000000,0x00003800,0x0000C600,0x00008200,0x00012900,0x00010100,0x00014570,0x0001388C,0x00008004,0x0000C482,0x00003AFA,0x00000252,0x00000104,0x0000018C,0x00000070,0x00000000,0x00000000},
};
+23
View File
@@ -0,0 +1,23 @@
ENTRY(Reset_Handler)
MEMORY {
PRAM (rwx) : ORIGIN = 0x00000000, LENGTH = 0x10000
SRAM (rw) : ORIGIN = 0x18000000, LENGTH = 0x1F000
}
SECTIONS {
.text : {
KEEP(*(.vectors))
KEEP(*(.pmxcfg))
*(.text*)
*(.rodata*)
. = ALIGN(4);
__pdata_start = .;
} > PRAM
.bss (NOLOAD) : { __bss_start = .; *(.bss*) *(COMMON) . = ALIGN(4); __bss_end = .; } > SRAM
/* mutable data in SRAM (initial values in PRAM, copied at startup), as in TamaEmu */
.data : { __data_start = .; *(.data*) . = ALIGN(4); __data_end = .; } > SRAM AT > PRAM
__data_load = LOADADDR(.data);
.init : { . = ALIGN(4); KEEP(*(.initcode)) KEEP(*(.initdata*)) KEEP(*(.initcode_z)) . = ALIGN(4); __prog_end = .; } > PRAM
ASSERT(__prog_end <= 0x10000, "program too big for PRAM")
ASSERT(__bss_end <= 0x1801F000, "RAM full")
/DISCARD/ : { *(.comment) *(.ARM.*) *(.note*) }
}
+897
View File
@@ -0,0 +1,897 @@
/* V3Emu: the Tamagotchi Connection V3 (Sunplus SPLB32) on the Tamagotchi Paradise. Core ported from BrickEmuPy
* by azya52 (CC0), see ../core. Started from the Paramix menu like the other further programs.
* Keys: A, B, C as on the toy (left, middle, right); wheel short = settings; wheel long = save and back; wheel
* within 3 s after a resumed start = start over (asks first).
* The screen: the 32 x 30 dots four times enlarged. The toy's own save (EEPROM, 1 KB) goes to the flash a few
* seconds after the game wrote it (two slots, a power cut leaves one whole).
* Author: mameise */
#include "../../../emu/src/emu/hw.h"
#include "../../../emu/src/emu/exitkey.h"
#include "../../../emu/src/emu/flash.h"
#include "../core/v3cpu.h"
#ifdef V3_CARE
#include "../../../emu/src/emu/ask.h"
#endif
#define PARA_ATTR
#include "../../../emu/src/emu/paraclock.h"
#ifdef PARA_BACK
#include "../../../emu/src/emu/paraback.h"
#endif
int hw_ask_reset(int en);
typedef int s32;
/* Settings block at 0x140, filled in by the Studio: the block of V1Emu (magic "PMX1", same layout), so the Studio keeps
* places, saves and backups as for V1Emu; it tells the two apart by the ROM (512 KB here). The icons are not used.
* save_off: session (4 KB), then 4 KB with the settings of the device and the toy's EEPROM. */
typedef struct { u32 magic, version, rom_off, rom_len, save_off, flags; char name[16]; u32 icons_ok; u16 icons[8][16]; } V3Cfg;
#define V3_MAGIC 0x31584D50u /* "PMX1" */
__attribute__((section(".pmxcfg"), used))
volatile const V3Cfg g_cfg = { V3_MAGIC, 1, 0, 0, 0, 0, {0}, 0, {{0}} };
#define SESSION (g_cfg.save_off)
#define SETTINGS (g_cfg.save_off + 0x1000u)
#define EE_AT 0x100u /* in the settings sector: "V3EE", sequence, sum, 4 bytes free, then the 1 KB */
#define XIP_BASE_REG REG(0x4002F004u)
#define XIP_CTRL_REG REG(0x4002F000u)
#include "font.h"
#ifdef CLK_ECO
#include "../../../emu/src/emu/eco.h"
#endif
#ifdef V3_LINK
/* 1.6.9 test20: infrared between two Paradise over the contacts (plink, as T7Emu and T18Emu). The core turns the
* toy's IR pulses into bytes and back (CON_V3_IR of BrickEmuPy, which links two emulators the same way over a
* serial line); here they go as 'V' frames to the other Paradise. */
#include "../../../emu/src/emu/plink.h"
static void lk_got(u8 ty, const u8 *d, int n) { if (ty == 'V') v3_ir_rx(d, n); }
#endif
#define ST_MAGIC 0x54533356u /* "V3ST" */
/* ---- time: SysTick 1 ms ---- */
static volatile u32 g_ms;
#define SYST_CSR REG(0xE000E010u)
#define SYST_RVR REG(0xE000E014u)
#define SYST_CVR REG(0xE000E018u)
#define CORE_HZ 96000000u
__attribute__((used)) void systick_c(void) { g_ms++; }
__attribute__((naked)) void SysTick_Handler(void) { __asm__ volatile("b systick_c\n"); }
#ifdef V3_PW
/* ---- 1.6.13 test12: power saving with the screen dark (as T7Emu, T18Emu and TamaEmu) ----
* Screen dark, no key, tone, menu or link traffic: the chip runs from its internal 12 MHz RC with PLL and crystal off
* and goes to deep sleep once the emulation has caught up; the RTC (1 s) or a key wakes it, the ms tick continues from
* the RTC. The toy's time is then run in one go (it sleeps most of the time), never dropped: its clock stays right. */
#define OSC_CTRL REG(0x45000000u)
#define OSC_STAT REG(0x45000008u)
#define SYS_CLKCFG REG(0x4500000Cu)
#define SYS_AHBCP REG(0x45000010u)
#define ISO_CTRL REG(0x45000200u)
#define PMU_CTRL REG(0x45000300u)
#define GP0(o) REG(0x40018000u + (o))
#define NVIC_ISER0 REG(0xE000E100u)
#define NVIC_ICER0 REG(0xE000E180u)
#define NVIC_ICPR0 REG(0xE000E280u)
#define PW_KEYS 0xF00u /* P0.8 wheel, P0.9 A, P0.10 C, P0.11 B */
#define PW_IRQS ((1u << 2) | (1u << 26)) /* RTC, GPIO0 */
static int g_pw_low;
static u32 g_pw_rtc, g_pw_rtcms, g_pw_skip;
static volatile u8 g_pw_rtcw; /* the RTC woke the chip (its interrupt ran) */
static u8 g_pw_kw; /* woken by a key before the next RTC second: that part of a second is not counted yet */
__attribute__((used, noinline)) void pw_rtc_irq(void) { PMU_CTRL = (PMU_CTRL & ~0x47u) | 0x10u; g_pw_rtcw = 1; } /* RTC_FLAG: write 1 */
__attribute__((used, noinline)) void pw_gpio_irq(void) { GP0(0x20) = PW_KEYS; }
__attribute__((noinline, minsize)) static void pw_full(void)
{
if (!g_pw_low) return;
OSC_CTRL |= 1u << 4; /* crystal on (the PLL's source, as hw_init sets it) */
for (u32 t = 0; !(OSC_STAT & (1u << 4)) && t < 2000000u; t++) {}
OSC_CTRL |= 1u << 15; /* PLL on */
for (u32 t = 0; !(OSC_STAT & (1u << 6)) && t < 2000000u; t++) {}
SYS_CLKCFG = 4u; /* system clock from the PLL */
for (u32 t = 0; ((SYS_CLKCFG >> 4) & 7u) != 4u && t < 2000000u; t++) {}
SYST_RVR = CORE_HZ / 1000u - 1u; SYST_CVR = 0;
g_pw_low = 0;
}
__attribute__((noinline, minsize)) static void pw_low(void)
{
if (g_pw_low) return;
SYS_AHBCP = 0;
SYS_CLKCFG = 0; /* system clock from the IHRC (12 MHz) */
for (u32 t = 0; ((SYS_CLKCFG >> 4) & 7u) != 0u && t < 2000000u; t++) {}
if (((SYS_CLKCFG >> 4) & 7u) != 0u) { SYS_CLKCFG = 4u; return; } /* did not switch: stay as it was */
OSC_CTRL &= ~((1u << 15) | (1u << 4)); /* PLL and crystal off */
SYST_RVR = 12000000u / 1000u - 1u; SYST_CVR = 0;
g_pw_low = 1;
}
/* deep sleep until the next RTC second or a key; returns the seconds that passed */
__attribute__((noinline, minsize)) static u32 pw_sleep(void)
{
u32 iso = ISO_CTRL;
if (!(iso & 0x80u) || (iso & 7u) != 0u) { __asm__ volatile("wfi"); return 0; } /* the RTC is not on 1 s: no deep sleep */
ISO_CTRL = iso & ~8u; /* RTC_WKP 0: the RTC interrupt wakes */
GP0(0x0C) &= ~PW_KEYS; GP0(0x10) |= PW_KEYS; GP0(0x20) = PW_KEYS; GP0(0x18) |= PW_KEYS; /* edges, both */
NVIC_ICPR0 = PW_IRQS; NVIC_ISER0 = PW_IRQS;
u32 r0 = REG(0x45000304u), csr = SYST_CSR;
SYST_CSR = 0;
g_pw_rtcw = 0;
PMU_CTRL = (PMU_CTRL & ~0x47u) | 2u; /* DSLEEP_EN */
__asm__ volatile("dsb\n wfi\n isb" ::: "memory");
PMU_CTRL = PMU_CTRL & ~0x47u;
NVIC_ICER0 = PW_IRQS; GP0(0x18) &= ~PW_KEYS; GP0(0x20) = PW_KEYS;
ISO_CTRL = (ISO_CTRL & ~8u) | (iso & 8u);
SYST_CVR = 0; SYST_CSR = csr;
/* woken by the RTC: wait until the counter (32 kHz domain) shows the new second (as T18Emu test3) */
u32 r = REG(0x45000304u);
if (g_pw_rtcw) for (u32 t = 0; r == r0 && t < 200000u; t++) r = REG(0x45000304u);
u32 n = r - r0;
if (!n) g_pw_kw = 1;
return n > 3600u ? 0u : n;
}
#endif
/* ---- settings on the device (wheel short), like V1Emu ---- */
typedef struct { u8 magic[4]; u8 pal, grid, shadow, sound, dim, lang, dimto, off, r0, noicons, clk, ghost; } V3Set; /* noicons: 0 = icons shown (V3_ICONS) */
/* test27 (V3_SETX, like the T7Emu): grid 0..100 % (old 1 = 100), shadow = pixel view 0..20 % (old 1 = 8), ghost = icons
* that are off 0..30 % (0x80 + value; 0 = 18 as before), clk 0 = as the Studio says, 1 = Auto, 2 = game */
static V3Set g_set = { {'V','3','S','1'}, 0, 1, 1, 1, 7, 0, 6, 3, 0x80, 0, {0} };
static const u16 PAL[6][2] = { { 0xB5B2u, 0x18E3u }, { 0xC618u, 0x0000u }, { 0xFFFFu, 0x0000u }, { 0xEF34u, 0x4A00u }, { 0x9E7Fu, 0x000Fu }, { 0xFDF9u, 0x780Fu } };
static const char *const PAL_NM[2][6] = { { "LCD", "GRAU", "WEISS", "GELB", "BLAU", "PINK" }, { "LCD", "GREY", "WHITE", "YELLOW", "BLUE", "PINK" } };
#define T_N 7 /* 10 s ... 10 min, index T_N = never */
static const u32 T_MS[T_N] = { 10000u, 20000u, 30000u, 60000u, 120000u, 300000u, 600000u };
static const char *const T_NM[2][T_N + 1] = { { "10 S", "20 S", "30 S", "1 MIN", "2 MIN", "5 MIN", "10 MIN", "NIE" }, { "10 S", "20 S", "30 S", "1 MIN", "2 MIN", "5 MIN", "10 MIN", "NEVER" } };
#define LV_N 11
#define DIM2_N 10
static const u8 LV_PCT[LV_N] = { 100u, 5u, 10u, 20u, 30u, 40u, 50u, 60u, 70u, 80u, 90u };
static const u8 DIM2_PCT[DIM2_N] = { 1u, 2u, 3u, 5u, 10u, 15u, 20u, 30u, 40u, 50u };
#ifdef V3_SETX
static int gridp(void) { return g_set.grid == 1u ? 100 : g_set.grid <= 100u ? g_set.grid : 100; }
static int shdp(void) { return g_set.shadow == 1u ? 8 : g_set.shadow <= 20u ? g_set.shadow : 8; }
static int clk_auto(void) { return g_set.clk == 1u ? 1 : g_set.clk == 2u ? 0 : (int)((g_cfg.flags >> 1) & 1u); } /* toy's clock from the Paradise */
static int ghp(void) { return (g_set.ghost & 0x80u) && (g_set.ghost & 0x7Fu) <= 30u ? g_set.ghost & 0x7F : 18; }
#endif
static u8 g_clk_diag; /* V3_CLK_RETRY: 0 waiting, 1 written, 2 no time from the Paradise, 3 the toy's clock stands, 4 written but off */
static int en(void) { return g_set.lang ? g_set.lang == 2u : (int)(g_cfg.flags & 1u); }
static u16 mix(u16 a, u16 b, int p) /* p percent of b */
{
int r = ((a >> 11) * (100 - p) + (b >> 11) * p) / 100, g = (((a >> 5) & 63) * (100 - p) + ((b >> 5) & 63) * p) / 100, bl = ((a & 31) * (100 - p) + (b & 31) * p) / 100;
return (u16)((r << 11) | (g << 5) | bl);
}
static u16 BG, INK, OFF, SHD, GHO, INKG, SHDG;
static int g_dimmed;
static void colours(void)
{
u32 k = g_set.pal < 6u ? g_set.pal : 0u;
#ifdef V3_SETX
BG = PAL[k][0]; INK = PAL[k][1]; OFF = mix(BG, INK, 40); GHO = mix(BG, INK, ghp()); SHD = mix(BG, INK, shdp());
INKG = mix(INK, BG, gridp()); SHDG = mix(SHD, BG, gridp()); /* the gap between the dots */
#else
BG = PAL[k][0]; INK = PAL[k][1]; OFF = mix(BG, INK, 40); GHO = mix(BG, INK, 18); SHD = g_set.shadow ? mix(BG, INK, 8) : BG;
#endif
#ifdef BL_PWM
{ u32 l = LV_PCT[(g_set.r0 & 15u) % LV_N], d = DIM2_PCT[g_set.dimto % DIM2_N];
hw_bl_set(g_dimmed ? (d < l ? d : l) : l); }
#endif
}
static void set_load(void)
{
const volatile u8 *p = XIP_PTR(SETTINGS); V3Set t; u8 *d = (u8 *)&t;
for (u32 i = 0; i < sizeof t; i++) d[i] = p[i];
if (t.magic[0] == 'V' && t.magic[1] == '3' && t.magic[2] == 'S' && t.magic[3] == '1' && t.pal < 6u && t.dim <= T_N && t.off <= T_N && t.lang < 3u) g_set = t;
colours();
}
static u8 g_sec[0x1000] __attribute__((aligned(4)));
static u32 g_tone_now;
/* ---- the toy's EEPROM (HT24LC08, 1 KB), in the settings sector behind the settings, with a sequence number; the
* session holds a copy too (if the sector is broken by a power cut while written, the session's copy is taken) ---- */
#define EE_MAGIC 0x45453356u
static u32 g_ee_seq; static int g_ee_ok;
static u32 ee_sum(const volatile u8 *d) { u32 s = 0x3E3E; for (u32 i = 0; i < V3_EE_SIZE; i++) s = s * 31u + d[i]; return s; }
static u32 rd32(const volatile u8 *h) { return h[0] | h[1] << 8 | h[2] << 16 | (u32)h[3] << 24; }
static void ee_load(void)
{
const volatile u8 *h = XIP_PTR(SETTINGS + EE_AT);
g_ee_ok = rd32(h) == EE_MAGIC && rd32(h + 8) == ee_sum(h + 16);
if (!g_ee_ok) return;
g_ee_seq = rd32(h + 4);
u8 *e = v3_eeprom(); for (u32 i = 0; i < V3_EE_SIZE; i++) e[i] = h[16 + i];
}
/* settings and EEPROM together into the sector (only if something changed) */
static void sec_store(int ee_new)
{
if (ee_new) g_ee_seq++;
for (u32 i = 0; i < sizeof g_sec; i++) g_sec[i] = 0xFF;
const u8 *s = (const u8 *)&g_set; for (u32 i = 0; i < sizeof g_set; i++) g_sec[i] = s[i];
u32 v[3] = { EE_MAGIC, g_ee_seq, ee_sum(v3_eeprom()) };
for (int k = 0; k < 3; k++) for (int b = 0; b < 4; b++) g_sec[EE_AT + k * 4 + b] = (u8)(v[k] >> (8 * b));
const u8 *e = v3_eeprom(); for (u32 i = 0; i < V3_EE_SIZE; i++) g_sec[EE_AT + 16 + i] = e[i];
hw_tone(0); g_tone_now = 0;
flash_update4k(SETTINGS, g_sec); /* writes only if different */
g_ee_ok = 1;
}
static void set_store(void) { sec_store(v3_eeprom_dirty(1)); }
static void ee_store(void) { sec_store(1); }
/* ---- screen: 32 x 30 dots, 4 x 4 pixels each (3 x 3 with the grid), from y 4 ---- */
#include "../core/v3lcd.inc"
static u16 g_row[128] __attribute__((aligned(4)));
static u8 g_pix[30][32];
#ifdef V3_ICONS
/* 1.6.9 test19: the ten icons around the screen, lit by the toy's LCD segments (bit b of byte n, as the face of
* BrickEmuPy names them "n_b"): top row left to right, then the bottom row. The pictures are mameise's V3 icons
* (20 x 20, icons_v3.h from gen/icons.py; the second one is the T7Emu icon). With the icons the dots are three times enlarged (96 x 90). */
#include "icons_v3.h"
static const u8 ICO_SEG[10][2] = { { 0, 7 }, { 1, 0 }, { 1, 1 }, { 1, 2 }, { 1, 3 }, { 6, 3 }, { 6, 2 }, { 6, 1 }, { 6, 0 }, { 5, 7 } };
static u8 g_ico[10];
static int icons_on(void) { return !g_set.noicons; }
#define ICO_R0 2 /* icon rows 2..18 are drawn (17 px): top at y 0, bottom at y 111 */
#define ICO_H 17
static void icon_row(int y)
{
int top = y < ICO_H, iy = (top ? y : y - 111) + ICO_R0;
if (!top && y < 111) return;
for (int k = 0; k < 5; k++) {
int i = top ? k : 5 + k, x0 = k * 128 / 5 + 2;
u16 col = g_ico[i] ? INK : GHO; u32 bits = g_icons[i][iy];
for (int b = 0; b < 20; b++) if (bits & (0x80000u >> b)) g_row[x0 + b] = col;
}
}
#endif
static u32 g_lcd_ms;
/* the LCD RAM to dots; 1 if something changed */
static u32 g_vcopy[V3_DPRAM / 4]; static int g_vmode = -1;
static int lcd_take(void)
{
int mode, ch = 0; const u8 *v = v3_vram(&mode);
{ const u32 *w = (const u32 *)v; int same = mode == g_vmode; /* 1.6.9: only when the LCD RAM changed */
for (u32 i = 0; i < V3_DPRAM / 4; i++) if (w[i] != g_vcopy[i]) { g_vcopy[i] = w[i]; same = 0; }
if (same) return 0; g_vmode = mode; }
for (int y = 0; y < 30; y++) for (int x = 0; x < 32; x++) {
int k = V3_LCD[y][x]; u8 on = (u8)(mode == 2 || (mode == 1 && ((v[k >> 3] >> (k & 7)) & 1)));
if (g_pix[y][x] != on) { g_pix[y][x] = on; ch = 1; }
}
#ifdef V3_ICONS
for (int i = 0; i < 10; i++) { u8 on = (u8)(mode == 2 || (mode == 1 && ((v[ICO_SEG[i][0]] >> ICO_SEG[i][1]) & 1))); if (g_ico[i] != on) { g_ico[i] = on; ch = 1; } }
#endif
return ch;
}
static void draw(void)
{
hw_px_begin(0, 0, 128, 128);
#ifdef V3_ICONS
if (icons_on()) {
int gap = g_set.grid ? 2 : 3;
for (int y = 0; y < 128; y++) {
for (int x = 0; x < 128; x++) g_row[x] = BG;
if (y >= 19 && y < 109) {
int r = (y - 19) / 3, sub = (y - 19) % 3;
#ifdef V3_SETX
for (int c = 0; c < 32; c++) { int on = g_pix[r][c]; u16 v = on ? INK : SHD, w = on ? INKG : SHDG; u16 *p = g_row + 16 + c * 3; if (sub < 2) { p[0] = p[1] = v; p[2] = w; } else p[0] = p[1] = p[2] = w; }
(void)gap;
#else
if (sub < gap) for (int c = 0; c < 32; c++) { u16 v = g_pix[r][c] ? INK : SHD; u16 *p = g_row + 16 + c * 3; p[0] = p[1] = v; if (gap == 3) p[2] = v; }
#endif
} else icon_row(y);
hw_px_row(g_row, 128);
}
hw_px_end();
return;
}
#endif
int gap = g_set.grid ? 3 : 4;
for (int y = 0; y < 128; y++) {
for (int x = 0; x < 128; x++) g_row[x] = BG;
if (y >= 4 && y < 124) {
int r = (y - 4) >> 2, sub = (y - 4) & 3;
#ifdef V3_SETX
for (int c = 0; c < 32; c++) { int on = g_pix[r][c]; u16 v = on ? INK : SHD, w = on ? INKG : SHDG; u16 *p = g_row + c * 4; if (sub < 3) { p[0] = p[1] = p[2] = v; p[3] = w; } else p[0] = p[1] = p[2] = p[3] = w; }
(void)gap;
#else
if (sub < gap) for (int c = 0; c < 32; c++) { u16 v = g_pix[r][c] ? INK : SHD; u16 *p = g_row + c * 4; p[0] = p[1] = p[2] = v; if (gap == 4) p[3] = v; }
#endif
}
hw_px_row(g_row, 128);
}
hw_px_end();
}
/* ---- the settings menu (wheel short): A next line, B change, C close ---- */
static int g_menu, g_msel, g_mtop;
static void text(int x, int y, const char *s, u16 col, u16 *row, int ry)
{
for (; *s; s++, x += 6) {
int c = (unsigned char)*s; if (c >= 97 && c <= 122) c -= 32; c = (c < 32 || c > 127) ? 0 : c - 32;
if (ry < y || ry >= y + 7) continue;
u8 bits = g_font[c][ry - y];
for (int k = 0; k < 5; k++) if ((bits & (0x10 >> k)) && x + k >= 0 && x + k < 128) row[x + k] = col;
}
}
enum {
#ifdef BAT_SHOW
M_BAT,
#endif
M_PAL, M_GRID, M_SHD,
#ifdef V3_ICONS
M_ICON,
#ifdef V3_SETX
M_GHOST,
#endif
#endif
M_SND, M_LIGHT, M_DIM, M_DIMTO, M_OFF,
#if defined(V3_SETX) && defined(V3_CLOCK)
M_CLK,
#endif
#ifdef V3_CARE
M_GP, M_CARE,
#endif
M_LANG, M_N };
static int g_items[M_N], g_nitems;
#ifdef V3_CARE
/* test31: the toy's RAM (found in the PC run, checked on its status pages): happy 0xA1 and hunger 0xA2 (4 hearts),
* Gotchi Points 0x154/0x155 (BCD, lowest digits first, the status shows 4 digits) */
static u8 *ram(u32 a);
static u8 g_cared;
static u32 gp_get(void) { u8 l = *ram(0x154), h = *ram(0x155); return (u32)(h >> 4) * 1000u + (h & 15u) * 100u + (l >> 4) * 10u + (l & 15u); }
#endif
static void menu_items(void)
{
g_nitems = 0;
for (int k = 0; k < M_N; k++) { if (k == M_DIMTO && g_set.dim >= T_N) continue;
#if defined(V3_SETX) && defined(V3_ICONS)
if (k == M_GHOST && g_set.noicons) continue;
#endif
#if defined(V3_SETX) && defined(V3_CLOCK)
if (k == M_CLK && !(g_cfg.flags & 8u)) continue; /* only when the Studio knows this toy's clock */
#endif
g_items[g_nitems++] = k; }
if (g_msel >= g_nitems) g_msel = g_nitems - 1;
}
#ifdef BAT_SHOW
#include "../../../emu/src/emu/batt.h"
static char g_bat[12];
#endif
static void pct(char *o, int v) { int n = 0; if (v >= 100) o[n++] = '1'; if (v >= 10) o[n++] = (char)('0' + (v / 10) % 10); o[n++] = (char)('0' + v % 10); o[n++] = '%'; o[n] = 0; }
static void menu_line(int it, const char **a, char *b)
{
int e = en(); const char *v = 0; b[0] = 0;
const char *on = e ? "ON" : "AN", *off = e ? "OFF" : "AUS";
switch (it) {
#ifdef BAT_SHOW
case M_BAT: *a = e ? "BATTERY" : "BATTERIE"; v = g_bat; break;
#endif
case M_PAL: *a = e ? "COLOUR" : "FARBE"; v = PAL_NM[e][g_set.pal % 6u]; break;
#ifdef V3_SETX
case M_GRID: *a = e ? "GRID" : "RASTER"; pct(b, gridp()); break;
case M_SHD: *a = e ? "PIXEL VIEW" : "PIXEL-SICHT"; pct(b, shdp()); break;
#else
case M_GRID: *a = e ? "GRID" : "RASTER"; v = g_set.grid ? on : off; break;
case M_SHD: *a = e ? "SHADOW" : "SCHATTEN"; v = g_set.shadow ? on : off; break;
#endif
#ifdef V3_ICONS
case M_ICON: *a = "ICONS"; v = g_set.noicons ? off : on; break;
#ifdef V3_SETX
case M_GHOST: *a = e ? "ICONS OFF" : "ICONS INAKTIV"; pct(b, ghp()); break;
#endif
#endif
#ifdef V3_VOL
case M_SND: { static const char *const VN[2][4] = { { "AUS", "NORMAL", "LAUT", "LEISE" }, { "OFF", "NORMAL", "LOUD", "QUIET" } };
*a = e ? "SOUND" : "TON"; v = VN[e ? 1 : 0][g_set.sound & 3u]; break; }
#else
case M_SND: *a = e ? "SOUND" : "TON"; v = g_set.sound ? on : off; break;
#endif
case M_LIGHT: *a = e ? "LIGHT" : "LICHT"; pct(b, LV_PCT[(g_set.r0 & 15u) % LV_N]); break;
case M_DIM: *a = e ? "DIM AFTER" : "DIMMEN NACH"; v = T_NM[e][g_set.dim <= T_N ? g_set.dim : T_N]; break;
case M_DIMTO: *a = e ? "DIM TO" : "DIMMEN AUF"; pct(b, DIM2_PCT[g_set.dimto % DIM2_N]); break;
case M_OFF: *a = e ? "SCREEN OFF" : "BILD AUS NACH"; v = T_NM[e][g_set.off <= T_N ? g_set.off : T_N]; break;
#if defined(V3_SETX) && defined(V3_CLOCK)
#ifdef V3_CLK_RETRY
case M_CLK: *a = e ? "CLOCK" : "UHR"; v = !clk_auto() ? (e ? "GAME" : "SPIEL") : g_clk_diag == 1 ? "AUTO OK" : g_clk_diag == 2 ? "AUTO -" : g_clk_diag == 3 ? "AUTO !" : g_clk_diag == 4 ? "AUTO ?" : "AUTO"; break;
#else
case M_CLK: *a = e ? "CLOCK" : "UHR"; v = clk_auto() ? "AUTO" : e ? "GAME" : "SPIEL"; break;
#endif
#endif
#ifdef V3_CARE
case M_GP: *a = "GOTCHI POINTS"; { u32 n = gp_get(); char t[6]; int k = 0, j = 0; do { t[k++] = (char)('0' + n % 10u); n /= 10u; } while (n); while (k) b[j++] = t[--k]; b[j++] = 'P'; b[j] = 0; } break;
case M_CARE: *a = e ? "FULL CARE" : "PFLEGE VOLL"; if (g_cared) v = "OK"; break;
#endif
default: *a = e ? "LANGUAGE" : "SPRACHE"; v = e ? "ENGLISH" : "DEUTSCH"; break;
}
if (v) { int n = 0; while (v[n] && n < 11) { b[n] = v[n]; n++; } b[n] = 0; }
}
#define ROWS 9
static void menu_draw(void)
{
if (g_msel < g_mtop) g_mtop = g_msel;
if (g_msel >= g_mtop + ROWS) g_mtop = g_msel - ROWS + 1;
#ifdef BAT_SHOW
bat_text(g_bat);
#endif
hw_px_begin(0, 0, 128, 128);
for (int y = 0; y < 128; y++) {
for (int x = 0; x < 128; x++) g_row[x] = BG;
text(4, 3, en() ? "SETTINGS" : "EINSTELLUNGEN", INK, g_row, y);
if (y == 12) for (int x = 0; x < 128; x++) g_row[x] = OFF;
for (int k = 0; k < ROWS && g_mtop + k < g_nitems; k++) {
int i = g_mtop + k, ly = 16 + k * 11; const char *a; char b[12]; menu_line(g_items[i], &a, b);
if (i == g_msel && y >= ly - 2 && y < ly + 9) for (int x = 0; x < 128; x++) g_row[x] = INK;
u16 c = i == g_msel ? BG : INK;
text(4, ly, a, c, g_row, y); int w = 0; while (b[w]) w++; text(124 - w * 6, ly, b, c, g_row, y);
}
if (g_mtop > 0 && y >= 4 && y < 8) for (int x = 118 - (y - 4); x <= 118 + (y - 4); x++) g_row[x] = OFF;
if (g_mtop + ROWS < g_nitems && y >= 4 && y < 8) for (int x = 108 - (7 - y); x <= 108 + (7 - y); x++) g_row[x] = OFF;
text(4, 118, en() ? "A NEXT B SET C OK" : "A ZEILE B SETZEN C OK", OFF, g_row, y);
hw_px_row(g_row, 128);
}
hw_px_end();
}
static int g_set_dirty;
static void menu_close(void) {
#ifdef V3_CARE
g_cared = 0;
#endif
g_menu = 0; if (g_set_dirty) { set_store(); g_set_dirty = 0; } draw(); }
static void menu_key(u32 k)
{
if (k & BTN_A) g_msel = (g_msel + 1) % g_nitems;
if (k & BTN_B) {
switch (g_items[g_msel]) {
#ifdef BAT_SHOW
case M_BAT: break;
#endif
case M_PAL: g_set.pal = (u8)((g_set.pal + 1u) % 6u); break;
#ifdef V3_SETX
case M_GRID: g_set.grid = (u8)(gridp() >= 100 ? 0 : gridp() / 25 * 25 + 25); break;
case M_SHD: g_set.shadow = (u8)(shdp() >= 20 ? 0 : shdp() / 2 * 2 + 2); break;
#else
case M_GRID: g_set.grid ^= 1u; break;
case M_SHD: g_set.shadow ^= 1u; break;
#endif
#ifdef V3_ICONS
case M_ICON: g_set.noicons ^= 1u; break;
#ifdef V3_SETX
case M_GHOST: g_set.ghost = (u8)(0x80u | (ghp() >= 30 ? 0 : ghp() / 2 * 2 + 2)); break;
#endif
#if defined(V3_SETX) && defined(V3_CLOCK)
case M_CLK: g_set.clk = clk_auto() ? 2u : 1u; break;
#endif
#endif
#ifdef V3_CARE
case M_GP: { u32 n = gp_get(); if (hw_num_edit("GOTCHI POINTS", &n, 4, 9999u, en())) { *ram(0x154) = (u8)((n / 10u % 10u) << 4 | n % 10u); *ram(0x155) = (u8)((n / 1000u) << 4 | n / 100u % 10u); } } menu_draw(); return;
case M_CARE: if (*ram(0xA1) < 4u) *ram(0xA1) = 4; if (*ram(0xA2) < 4u) *ram(0xA2) = 4; g_cared = 1; menu_draw(); return;
#endif
#ifdef V3_VOL
case M_SND: g_set.sound = (u8)((g_set.sound + 1u) & 3u); break;
#else
case M_SND: g_set.sound ^= 1u; break;
#endif
case M_LIGHT: g_set.r0 = (u8)(0x80u | (((g_set.r0 & 15u) + 1u) % LV_N)); break;
case M_DIM: g_set.dim = (u8)((g_set.dim + 1u) % (T_N + 1u)); if (g_set.dim < T_N && g_set.off < T_N && g_set.off <= g_set.dim) g_set.off = (u8)(g_set.dim + 1u); break;
case M_DIMTO: g_set.dimto = (u8)((g_set.dimto + 1u) % DIM2_N); break;
case M_OFF: do g_set.off = (u8)((g_set.off + 1u) % (T_N + 1u)); while (g_set.off < T_N && g_set.dim < T_N && g_set.off <= g_set.dim); break;
default: g_set.lang = (u8)(en() ? 1u : 2u); break;
}
g_set_dirty = 1; colours(); menu_items();
}
if (k & (BTN_C | BTN_WHEEL)) { menu_close(); return; }
menu_draw();
}
/* ---- session: the whole state (CPU, RAM, LCD, timers, EEPROM copy), and which EEPROM slot it belongs to ---- */
static u32 crc_rom(void) { const volatile u8 *r = XIP_PTR(g_cfg.rom_off); u32 h = 2166136261u; for (u32 i = 0; i < 0x8000u; i += 7u) h = (h ^ r[i]) * 16777619u; return h; }
static void save_session(void)
{
u32 n = v3_state_size();
if (n + 16u > sizeof g_sec) return;
if (v3_eeprom_dirty(1) || !g_ee_ok) ee_store(); /* the EEPROM first: the session notes its sequence */
#ifdef SESS_FLAG
if (g_cfg.flags & 0x1000u) { hw_tone(0); g_tone_now = 0; return; } /* 1.7.3 test7: NOSESS, only the toy's EEPROM is kept */
#endif
for (u32 i = 0; i < sizeof g_sec; i++) g_sec[i] = 0xFF;
u32 *w = (u32 *)g_sec; w[0] = ST_MAGIC; w[1] = crc_rom(); w[2] = g_ee_seq; w[3] = n;
v3_state_save(g_sec + 16);
hw_tone(0); g_tone_now = 0;
flash_update4k(SESSION, g_sec);
}
static int load_session(void)
{
#ifdef SESS_FLAG
if (g_cfg.flags & 0x1000u) return 0; /* 1.7.3 test7: NOSESS, an old session is dropped, start from the EEPROM */
#endif
const volatile u8 *s = XIP_PTR(SESSION);
for (u32 i = 0; i < sizeof g_sec; i++) g_sec[i] = s[i];
u32 *w = (u32 *)g_sec;
if (w[0] != ST_MAGIC || w[1] != crc_rom() || w[3] != v3_state_size()) return 0;
if (g_ee_ok && w[2] != g_ee_seq) return 0; /* the game has saved since: start from its EEPROM (as the toy after a battery change) */
if (!v3_state_load(g_sec + 16, w[3])) return 0;
if (g_ee_ok) ee_load(); else g_ee_seq = w[2]; /* a broken sector: the session's copy */
return 1;
}
static u32 g_sess_ms;
__attribute__((used)) volatile u32 g_v3st[2]; /* emulated ms, dropped ms (for the tests) */
#ifdef V3_EE2
/* test21: a used session is not erased but marked used (first word 0): its copy of the EEPROM stays as the second copy
* for a settings sector that broke while being written (power gone in the middle) */
static void drop_session(void)
{
const volatile u8 *s = XIP_PTR(SESSION); u32 w0 = rd32(s);
if (w0 == 0u) return;
if (w0 == ST_MAGIC) { for (u32 i = 0; i < sizeof g_sec; i++) g_sec[i] = s[i]; g_sec[0] = g_sec[1] = g_sec[2] = g_sec[3] = 0; flash_update4k(SESSION, g_sec); return; }
if (!flash_isff(SESSION, 0x1000u)) flash_erase4k(SESSION);
}
static int ee_from_session(void)
{
const volatile u8 *s = XIP_PTR(SESSION); u32 w0 = rd32(s);
if ((w0 != ST_MAGIC && w0 != 0u) || rd32(s + 4) != crc_rom() || rd32(s + 12) != v3_state_size()) return 0;
for (u32 i = 0; i < sizeof g_sec; i++) g_sec[i] = s[i];
const u8 *e = v3_state_ee(g_sec + 16); u8 *d = v3_eeprom(); for (u32 i = 0; i < V3_EE_SIZE; i++) d[i] = e[i];
g_ee_seq = rd32(s + 8); return 1;
}
#else
static void drop_session(void) { if (!flash_isff(SESSION, 0x1000u)) flash_erase4k(SESSION); }
#endif
static void leave(int save)
{
#ifdef V3_PW
pw_full();
#endif
#ifdef CLK_ECO
hw_clk_shift(0, CORE_HZ);
#endif
hw_tone(0);
set_store();
REG(0x20000FF8u) = 0;
REG(0x20000FFCu) = 0x4C584D50u; /* "PMXL": back on purpose, the autostart stays off */
if (v3_eeprom_dirty(1)) ee_store();
if (save) save_session();
#ifdef PARA_BACK
para_back_fix(g_sec);
#endif
hw_bl_pwm_off();
for (u32 t = 0; hw_buttons() && t < 20000000u; t++) if ((t & 0xFFFFu) == 0) hw_wdt_feed();
XIP_BASE_REG = 0x60011000u; XIP_CTRL_REG = 3u;
hw_back_to_paradise();
}
#ifdef V3_CLOCK
/* 1.6.9 test20: the toy's clock from the Paradise at every start (flags bit 1, the Studio's tick; bit 3: the Studio
* knows this toy's clock). Found on the host: seconds 0x81 (ones) 0x82 (tens), minutes 0x83 0x84, hour 0x85 (1..12,
* 0 = never set) and 0x86 (1 = PM), day 0x13D (ones) 0x13E (tens), month 0x13F (1..12). The clock stands until it was set once in the game, so
* only a running clock is written; then it is checked and written again if it is off (three times at most). */
static u32 g_para_time, g_para_cnt;
static u8 *ram(u32 a) { return v3_ram() + (a - 0x40u); }
static int clk_running_val(void) { return ram(0x81)[0] + 10 * ram(0x82)[0]; }
static void civil(u32 days, u32 *mo, u32 *d) /* days since 1970 -> month, day */
{
s32 z = (s32)days + 719468, era = z / 146097, doe = z - era * 146097, yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
s32 doy = doe - (365 * yoe + yoe / 4 - yoe / 100), mp = (5 * doy + 2) / 153;
*d = (u32)(doy - (153 * mp + 2) / 5 + 1); *mo = (u32)(mp < 10 ? mp + 3 : mp - 9);
}
static u32 para_now(void) { return g_para_time + (REG(0x45000304u) - g_para_cnt); }
static void clock_write(void)
{
u32 t = para_now(), d = t % 86400u, h = d / 3600u, m = d / 60u % 60u, sec = d % 60u, mo, dy;
ram(0x81)[0] = (u8)(sec % 10u); ram(0x82)[0] = (u8)(sec / 10u); ram(0x83)[0] = (u8)(m % 10u); ram(0x84)[0] = (u8)(m / 10u);
ram(0x85)[0] = (u8)(h % 12u ? h % 12u : 12u); ram(0x86)[0] = (u8)(h >= 12u);
civil(t / 86400u, &mo, &dy); ram(0x13D)[0] = (u8)(dy % 10u); ram(0x13E)[0] = (u8)(dy / 10u); ram(0x13F)[0] = (u8)mo; /* test23: the day as two digits */
}
static int clock_ok(void)
{
u32 t = para_now(), want = t % 86400u / 60u, h = ram(0x85)[0] % 12u + (ram(0x86)[0] ? 12u : 0u), m = ram(0x83)[0] + 10u * ram(0x84)[0];
u32 diff = (h * 60u + m + 1440u - want) % 1440u;
return diff <= 1u || diff >= 1439u;
}
#endif
extern u32 __bss_start, __bss_end;
#ifdef V3_TONE_Q
/* test21: tone in real time (as TamaEmu, T18Emu): changes with the toy's time, played on the real clock 250 ms after the
* first of a sequence; more than 200 ms late: anew from there. A tone always at the full clock. */
#define TQ_N 32u
#define TQ_LAT 250u
static u32 tq_t[TQ_N]; static u16 tq_hz[TQ_N]; static u32 tq_h, tq_w, tq_g0, tq_r0, tq_on, tq_last;
static void tq_put(u32 hz, u32 gms) { if (tq_w - tq_h >= TQ_N) tq_h++; tq_t[tq_w % TQ_N] = gms; tq_hz[tq_w % TQ_N] = (u16)(hz > 0xFFFFu ? 0xFFFFu : hz); tq_w++; }
__attribute__((noinline)) static void tq_pump(void)
{
u32 now = g_ms;
while (tq_h != tq_w) {
u32 i = tq_h % TQ_N;
if (!tq_on) { tq_on = 1; tq_g0 = tq_t[i]; tq_r0 = now + TQ_LAT; }
s32 late = (s32)(now - (tq_r0 + (tq_t[i] - tq_g0)));
if (late < 0) return;
if (late > 200) { tq_g0 = tq_t[i]; tq_r0 = now; }
u32 hz = tq_hz[i];
#ifdef V3_PW
if (hz) pw_full();
#endif
hw_tone(hz); tq_h++; tq_last = now;
}
if (!g_hw_tone_hz && now - tq_last > 1000u) tq_on = 0;
}
#endif
#ifdef V3_VOL
/* test21: volume as in the other emulators: sound 0 off, 1 normal (the old "on"), 2 loud, 3 quiet */
static const u8 VOL_OF[4] = { 0, 2, 3, 1 };
#endif
void Reset_Handler(void)
{
__asm__ volatile("cpsid i");
u32 cnt = REG(0x45000304u), para = para_clock(cnt, 0, 0x1F000u, 0, 0);
if (!para) para = para_mailbox(cnt);
#if defined(PARA_FLASH) && !defined(PARA_FLASH_LATE)
if (!para) para = para_flash(cnt); /* 1.6.9 test37 */
#endif
#ifdef PARA_BACK
para_leave_mark(cnt, para);
#endif
__asm__ volatile("" ::: "memory");
for (u32 *p = &__bss_start; p < &__bss_end; p++) *p = 0;
{ extern u32 __data_start, __data_end, __data_load;
const u32 *src = &__data_load; for (u32 *p = &__data_start; p < &__data_end; p++) *p = *src++; }
(void)para;
#ifdef V3_CLOCK
g_para_time = para; g_para_cnt = cnt;
#endif
hw_init();
hw_tone(0);
#if defined(V3_CLOCK) && defined(PARA_FLASH_LATE)
/* 1.6.10 test2: neither SRAM nor mailbox: the base in the Paradise's settings page (readable only after hw_init) */
if (!g_para_time) { u32 c2 = REG(0x45000304u), t2 = 0;
#ifdef PARA_CLEAN_CLK
t2 = para_cleanclk(c2); /* 1.7.2 test17: start device without the Paradise */
#endif
if (!t2) t2 = para_flash(c2); if (t2) { g_para_time = t2; g_para_cnt = c2; para_mailbox_put(t2, c2); } }
#endif
XIP_BASE_REG = 0x60000000u | g_cfg.rom_off; /* the ROM in the cached window from 0x10000000 */
XIP_CTRL_REG = 3u;
v3_init((const u8 *)0x10000000u, g_cfg.rom_len);
ee_load();
#ifdef V3_EE2
int ee_fix = !g_ee_ok && ee_from_session();
#endif
int restored = load_session();
if (!restored) { drop_session(); v3_reset(); }
set_load();
#ifdef V3_EE2
if (ee_fix) ee_store(); /* the settings sector written anew from the second copy */
#endif
lcd_take(); draw();
#ifdef V3_LINK
plink_init();
#endif
SYST_RVR = CORE_HZ / 1000u - 1u; SYST_CVR = 0; SYST_CSR = 7;
__asm__ volatile("cpsie i");
u32 emu_ms = g_ms, last_draw = g_ms, t_start = g_ms, wheel_ms = 0, last_in = g_ms, mk0 = 0, ee_ms = 0;
int bl = 1, lock = 1;
#ifdef V3_PW
u32 pw_hold = 0, pw_tq = 0; int pw_back = 0; /* ms the run stays behind for a tone while dark; time from dark to catch up */
g_pw_rtc = REG(0x45000304u); g_pw_rtcms = g_ms;
#endif
g_sess_ms = g_ms;
#ifdef V3_CALL_WAKE
int call_wake = 0;
#endif
for (;;) {
hw_wdt_feed();
u32 now = g_ms;
#ifdef BAT_GUARD
/* test21: battery empty: the EEPROM and the run state saved, then off until new batteries */
{ static u32 bt; if (now - bt >= 1000u) { bt = now; if (bat_guard_tick()) {
#ifdef V3_PW
pw_full();
#endif
if (v3_eeprom_dirty(1)) ee_store();
save_session(); BAT_PIC_SHOW(); hw_backlight(0);
for (;;) { hw_wdt_feed(); __asm__ volatile("wfi"); } } } }
#endif
u32 b = hw_buttons();
if (lock) { if (b) b = 0; else lock = 0; }
#ifdef V3_PW
int pw_quiet = !bl && !b && !g_tone_now && !g_menu && !wheel_ms && !pw_hold
#ifdef V3_TONE_Q
&& tq_h == tq_w && !g_hw_tone_hz
#endif
#ifdef V3_LINK
&& !(g_pl.last_rx && now - g_pl.last_rx < 30000u)
#endif
;
if (pw_quiet && (s32)(now - emu_ms) < 2500) { /* after a deep sleep 1 s behind: caught up at 12 MHz */
#ifdef CLK_ECO
if (!g_pw_low) hw_clk_shift(0, CORE_HZ);
#endif
pw_low();
} else pw_full();
if (pw_quiet) pw_back = 1;
{ u32 r = REG(0x45000304u); /* when the RTC last stepped */
if (r != g_pw_rtc) {
if (g_pw_kw) { u32 t = g_pw_rtcms + (r - g_pw_rtc) * 1000u; /* woken by a key: the part of a second slept */
__asm__ volatile("cpsid i" ::: "memory"); if ((s32)(t - g_ms) > 0) g_ms = t; __asm__ volatile("cpsie i" ::: "memory");
now = g_ms; g_pw_kw = 0; }
g_pw_rtc = r; g_pw_rtcms = now; } }
#endif
#ifdef CLK_ECO
/* test22: screen off, no key, no tone, no link traffic for 30 s: a lower clock */
#ifdef V3_PW
if (!g_pw_low) {
#endif
#ifdef ECO_RTC
(void)eco_rtc(now, CORE_HZ); /* test27: a clock step whose ms tick runs slow is not used again */
#endif
eco_poll(now, !bl && !b && !g_tone_now && !g_menu
#ifdef V3_LINK
&& !(g_pl.last_rx && now - g_pl.last_rx < 30000u)
#endif
, (int)(now - emu_ms), CORE_HZ);
#ifdef V3_PW
}
#endif
#endif
if (b & BTN_WHEEL) {
if (restored && now - t_start < 3000u) {
int yes = hw_ask_reset(en());
if (yes) { drop_session(); leave(0); }
restored = 0; draw(); continue;
}
if (!wheel_ms) wheel_ms = now ? now : 1;
else if (EXIT_NOW(now - wheel_ms, 1500u)) leave(1);
} else if (wheel_ms) { if (now - wheel_ms < 1000u) { if (g_menu) menu_key(BTN_WHEEL); else { g_menu = 1;
#ifdef BAT_SHOW
g_msel = 1;
#else
g_msel = 0;
#endif
g_mtop = 0; menu_items(); menu_draw(); } } wheel_ms = 0; }
if (b) {
last_in = now;
if (!bl || g_dimmed) { /* the key only wakes */
#ifdef WAKE_FRESH
/* 1.7.3 test26 WAKE_FRESH: the picture first, then the light. Before the panel lit with the picture from before the dark (often hours old) until the new one was drawn. */
bl = 1; if (g_dimmed) { g_dimmed = 0; colours(); }
#ifdef V3_PW
pw_full(); lcd_take();
#endif
if (g_menu) menu_draw(); else draw();
hw_backlight(1);
#else
bl = 1; hw_backlight(1); if (g_dimmed) { g_dimmed = 0; colours(); }
#ifdef V3_PW
pw_full(); lcd_take();
#endif
if (g_menu) menu_draw(); else draw();
#endif
while (hw_buttons()) hw_wdt_feed();
#ifdef WAKE_WHEEL
wheel_ms = 0; /* 1.7.2: the wheel only wakes, its release does not open the settings */
#endif
continue;
}
}
if (g_menu) { u32 k = b & (BTN_A | BTN_B | BTN_C), nk = k & ~mk0; mk0 = k; if (nk) menu_key(nk); v3_set_keys(0); }
else { mk0 = b & (BTN_A | BTN_B | BTN_C); v3_set_keys(((b & BTN_A) ? 1u : 0u) | ((b & BTN_B) ? 2u : 0u) | ((b & BTN_C) ? 4u : 0u)); }
/* run the toy up to now (at most 50 ms behind), 1 ms at a time */
#ifdef V3_PW
/* dark: up to 5 s behind (the toy's time is run later, at once); after the screen comes on that is caught up first
* (20 ms of run between the passes); a tone while dark: real time from there (pw_hold), so it sounds as it should */
s32 lim = pw_back ? 5000 : 50;
if ((s32)(now - emu_ms) > lim) { g_v3st[1] += now - (u32)lim - emu_ms; emu_ms = now - (u32)lim; }
u32 t0 = g_ms, end = pw_hold ? now - pw_hold : now;
while ((s32)(end - emu_ms) > 0 && g_ms - t0 < (pw_quiet ? 40u : 20u)) {
#else
if ((s32)(now - emu_ms) > 50) { g_v3st[1] += now - 50u - emu_ms; emu_ms = now - 50u; }
u32 t0 = g_ms;
while ((s32)(now - emu_ms) > 0 && g_ms - t0 < 20u) {
#endif
v3_run((uint64_t)(V3_CLOCK / 1000u) * V3_TICKS);
emu_ms++; g_v3st[0]++;
#ifdef CLK_ECO
ECO_CHECK(g_ms, g_ms - emu_ms, CORE_HZ);
#endif
#ifdef V3_LINK
{ u8 tx[32]; int n = v3_ir_tx(tx, sizeof tx); if (n) plink_send('V', tx, n); }
plink_poll(g_ms, 0, lk_got);
#endif
u32 hz = g_set.sound ? v3_tone_hz() : 0u;
#ifdef V3_VOL
g_hw_vol = VOL_OF[g_set.sound & 3u];
#endif
#ifdef V3_TONE_Q
if (hz != g_tone_now) { g_tone_now = hz; tq_put(hz, emu_ms); }
#else
if (hz != g_tone_now) { g_tone_now = hz; hw_tone(hz); }
#endif
#ifdef V3_CALL_WAKE
if (hz && !bl) call_wake = 1; /* test21: the toy calls while dark: the screen comes on (as the other emulators) */
#endif
#ifdef V3_PW
if (hz && !bl && !pw_hold && (s32)(now - emu_ms) > 1) { pw_hold = now - emu_ms; pw_full(); break; }
#endif
}
#ifdef V3_TONE_Q
tq_pump();
#endif
#ifdef V3_CALL_WAKE
if (call_wake) { call_wake = 0;
#ifdef WAKE_FRESH
if (!bl) { bl = 1; last_in = now; if (g_dimmed) { g_dimmed = 0; colours(); }
#ifdef V3_PW
pw_full(); lcd_take();
#endif
draw(); hw_backlight(1); } }
#else
if (!bl) { bl = 1; last_in = now; hw_backlight(1); if (g_dimmed) { g_dimmed = 0; colours(); }
#ifdef V3_PW
pw_full(); lcd_take();
#endif
draw(); } }
#endif
#endif
#ifdef V3_PW
if (g_tone_now) pw_tq = emu_ms;
if (pw_hold && (bl || emu_ms - pw_tq > 2000u)) pw_hold = 0; /* 2 s of the toy's time without a tone */
if (pw_back && bl && (s32)(now - emu_ms) <= 1) pw_back = 0;
#endif
#ifdef V3_CLOCK
/* the toy's clock: seconds read at 1 s and 2.5 s after the start; running: write the Paradise's time */
{ static int st = 0, s1, tries; static u32 t0; u32 a = now - t_start;
#ifdef V3_SETX
if ((g_cfg.flags & 8u) && clk_auto() && g_para_time && st >= 0) {
#else
if ((g_cfg.flags & 10u) == 10u && g_para_time && st >= 0) {
#endif
#ifdef V3_CLK_RETRY
/* test37: a clock that stands (the toy started from its own save, it asks for the time) is looked at again every
* 5 s for 10 minutes: once it was set in the game, the Paradise's time is written (reported: not set now and then) */
if (st == 0 && a >= t0 + 1000u) { s1 = clk_running_val(); st = 1; }
else if (st == 1 && a >= t0 + 2500u) {
if (clk_running_val() != s1 && ram(0x85)[0] >= 1u && ram(0x85)[0] <= 12u) { clock_write(); st = 2; t0 = a; g_clk_diag = 1; }
else if (a < 600000u) { st = 0; t0 = a + 2500u; g_clk_diag = 3; } else st = -1;
}
else if (st >= 2 && a >= t0 + (u32)(st - 1) * 2500u) { if (clock_ok()) { st = -1; g_clk_diag = 1; } else if (tries++ < 3) { clock_write(); st++; } else { st = -1; g_clk_diag = 4; } }
#else
if (st == 0 && a >= 1000u) { s1 = clk_running_val(); st = 1; }
else if (st == 1 && a >= 2500u) { if (clk_running_val() != s1 && ram(0x85)[0] >= 1u && ram(0x85)[0] <= 12u) { clock_write(); st = 2; } else st = -1; }
else if (st >= 2 && a >= 2500u + (u32)(st - 1) * 2500u) { if (clock_ok()) st = -1; else if (tries++ < 3) { clock_write(); st++; } else st = -1; }
#endif
}
#ifdef V3_CLK_RETRY
if (!g_para_time) g_clk_diag = 2;
#endif
}
#endif
if (v3_eeprom_dirty(1)) ee_ms = now ? now : 1; /* the game wrote its EEPROM */
if (ee_ms && now - ee_ms > 3000u && now - last_in > 2000u) { ee_ms = 0; ee_store(); }
#ifdef V3_PW
{ static u32 lt; if (bl && now - lt >= 8u) { lt = now; if (lcd_take()) g_lcd_ms = now; } } /* dark: nothing to show */
#else
{ static u32 lt; if (now - lt >= 8u) { lt = now; if (lcd_take()) g_lcd_ms = now; } }
#endif
if (bl && !g_menu && g_lcd_ms && now - last_draw >= 33u) { last_draw = now; g_lcd_ms = 0; draw(); }
if (bl && !g_dimmed && g_set.dim < T_N && now - last_in > T_MS[g_set.dim]) { g_dimmed = 1; colours(); if (g_menu) menu_draw(); else draw(); }
if (bl && g_set.off < T_N && now - last_in > T_MS[g_set.off]) { bl = 0; hw_backlight(0); save_session(); g_sess_ms = now; }
/* the run state every 15 minutes (quiet for 10 s) and when the screen goes off */
if (now - g_sess_ms > 900000u && now - last_in > 10000u && !g_menu) { save_session(); g_sess_ms = now; }
#ifdef V3_PW
if (emu_ms == g_ms) {
if (pw_quiet && g_pw_low && !hw_buttons() && !ee_ms) {
/* deep sleep until the next RTC second or a key; the ms tick then continues from the time of the last RTC step */
if (pw_sleep()) { u32 r = REG(0x45000304u), t = g_pw_rtcms + (r - g_pw_rtc) * 1000u;
__asm__ volatile("cpsid i" ::: "memory"); if ((s32)(t - g_ms) > 0) g_ms = t; __asm__ volatile("cpsie i" ::: "memory");
g_pw_rtc = r; g_pw_rtcms = t; }
} else __asm__ volatile("wfi");
}
#else
if (emu_ms == g_ms) __asm__ volatile("wfi");
#endif
}
}
__attribute__((naked)) static void Default_Handler(void) { __asm__ volatile("1: b 1b\n"); }
__attribute__((naked)) static void Irq_Off(void) { __asm__ volatile("cpsid i\n bx lr\n"); }
__attribute__((section(".vectors"), used))
const void *const g_vectors[16 + 64] = { (void *)0x18020000u, (void *)Reset_Handler,
[2 ... 10] = (void *)Default_Handler, [11] = (void *)Irq_Off, [12 ... 13] = (void *)Default_Handler, [14] = (void *)Irq_Off,
[15] = (void *)SysTick_Handler, [16 ... 79] = (void *)Irq_Off
#ifdef V3_LINK
, [16 + 37] = (void *)plink_irq
#endif
#ifdef V3_PW
, [16 + 2] = (void *)pw_rtc_irq, [16 + 26] = (void *)pw_gpio_irq
#endif
};
+563
View File
@@ -0,0 +1,563 @@
/* v3cpu.c - Tamagotchi Connection V3 core: Sunplus SPLB32 (6502), ported from BrickEmuPy cores/SPLB32.py and
* peripherals/HT24LC08.py by azya52 (CC0). The instructions are generated from the Python (v3/gen/py2c.py ->
* v3ops.inc); the rest follows the Python line by line, with time in ticks of 1/128 cycle. Author: mameise */
#include "v3cpu.h"
#ifndef V3_FREESTANDING
#include <string.h>
#else
void *memcpy(void *d, const void *s, unsigned int n); void *memset(void *d, int c, unsigned int n); void *memmove(void *d, const void *s, unsigned int n);
#endif
#define SUB_DIV 15625u /* 4 MHz / 32768 Hz in ticks */
#define T1K_DIV 500000u /* SUB_DIV * 32 */
#define VADDR_NMI 0x7FFA
#define VADDR_RESET 0x7FFC
#define VADDR_IRQ 0x7FFE
#define RAM_OFF 0x40
#define RAM_SIZE 0x4C0
#define DP_OFF 0x3E00
/* ---- state (the names of the Python, so the generated instructions read the same) ---- */
static const uint8_t *g_rom; static uint32_t g_rom_size, g_rom_mask;
static int A, X, Y, SP, PC, NF, VF, BF, DF, IF, ZF, CF;
typedef struct {
uint8_t ram[RAM_SIZE], dpram[V3_DPRAM];
uint32_t rom_bank; /* << 15 */
uint8_t cpu_enbl, cpu_div, clk32k, lcd_ctrl1;
uint8_t wakeup_ctrl, wakeupreq, time_base, timer_ctrl;
uint8_t int_ctrl, ireq, nmi_ctrl, nmireq;
uint8_t a0c, a0d, a1c, a1d, byte_mirror, nibble_swap;
uint8_t pdir[4], attr[4], platch[4], pin0[4], pin1[4]; /* PA PB PC PD */
int64_t tm0_cnt, tm1_cnt, t1k_cnt; /* ticks */
int64_t tm0, tm1; /* ticks (count << 7) */
uint32_t tm0_rel, tm1_rel, t1khz;
uint32_t pend, next; /* ticks run but not yet given to the timers; ticks to their next event */
uint16_t addr_irq, addr_nmi;
uint8_t snd_env[2], snd_tog[2];
/* EEPROM HT24LC08 */
uint8_t ee[V3_EE_SIZE];
uint8_t ee_state, ee_bits, ee_scl, ee_sda, ee_ack, ee_rx, ee_tx, ee_dirty;
uint16_t ee_addr;
/* infrared CON_V3_IR (peripherals/CON_V3_IR.py): bytes as pulse trains, in ticks */
int32_t ir_txb, ir_txs, ir_rxb, ir_rxs;
uint8_t ir_txbyte, ir_txbits, ir_rxstep, ir_rxbyte, ir_rxbits, ir_prev, ir_rxn, ir_txn;
uint8_t ir_rxq[64], ir_txq[64];
} V3S;
static V3S S;
enum { PA, PB, PCP, PD };
static const uint8_t PULLUP[4] = { 0, 0, 0, 6 }; /* mask option: PD pull-ups (bits 1, 2) */
static inline uint8_t rom_byte(uint32_t a) { return g_rom_mask ? g_rom[a & g_rom_mask] : g_rom[a % g_rom_size]; }
static int port_read(int p)
{
return ((~S.pdir[p] & (S.pin1[p] | (PULLUP[p] & ~S.pin0[p]))) | (S.pdir[p] & S.platch[p] & (PULLUP[p] | ~S.attr[p]))) & 0xFF;
}
/* a device drives a pin: level -1 released, 0 low, 1 high (interconnect.emit_port to the CPU) */
static void cpu_port_in(int p, int mask, int level)
{
S.pin0[p] &= (uint8_t)~mask; S.pin1[p] &= (uint8_t)~mask;
if (level == 0) S.pin0[p] |= (uint8_t)mask; else if (level == 1) S.pin1[p] |= (uint8_t)mask;
if (p == PA && (port_read(PA) & mask)) S.wakeupreq |= 0x01;
}
/* ---- HT24LC08 (peripherals/HT24LC08.py), SDA = PD bit 1, SCL = PD bit 0, address 0xA0 ---- */
enum { EE_IDLE, EE_ADDR, EE_WORD, EE_WRITE, EE_READ };
static void ee_port(int value)
{
int prev_scl = S.ee_scl, prev_sda = S.ee_sda;
S.ee_scl = (value & 1) ? 1 : 0;
S.ee_sda = (value & 2) ? 1 : 0;
if (S.ee_scl && (prev_scl != S.ee_scl || prev_sda != S.ee_sda)) {
if (prev_sda > S.ee_sda) { S.ee_state = EE_ADDR; S.ee_bits = 0; S.ee_ack = 0; cpu_port_in(PD, 2, -1); }
else if (prev_sda < S.ee_sda) { S.ee_state = EE_IDLE; S.ee_ack = 0; }
else if (S.ee_ack) { cpu_port_in(PD, 2, 0); S.ee_ack = 0; }
else if (S.ee_state == EE_READ) {
if (S.ee_bits < 8) { cpu_port_in(PD, 2, ((S.ee_tx >> (7 - S.ee_bits)) & 1) ? -1 : 0); S.ee_bits++; }
else { S.ee_addr = (uint16_t)((S.ee_addr + 1) % V3_EE_SIZE); S.ee_tx = S.ee[S.ee_addr]; S.ee_bits = 0; }
} else {
cpu_port_in(PD, 2, -1);
S.ee_rx = (uint8_t)((S.ee_rx << 1) | (S.ee_sda > 0));
S.ee_bits++;
if (S.ee_bits == 8) {
int rx = S.ee_rx; S.ee_bits = 0;
if (S.ee_state == EE_ADDR) {
if ((rx & 0xF8) == 0xA0) {
S.ee_ack = 1;
S.ee_addr = (uint16_t)(((rx & ~0xF8) >> 1) * 256 + (S.ee_addr % 256));
if (rx & 1) { S.ee_state = EE_READ; S.ee_tx = S.ee[S.ee_addr]; } else S.ee_state = EE_WORD;
} else S.ee_state = EE_IDLE;
} else if (S.ee_state == EE_WORD) {
S.ee_ack = 1; S.ee_addr = (uint16_t)(S.ee_addr - (S.ee_addr % 256) + rx); S.ee_state = EE_WRITE;
} else if (S.ee_state == EE_WRITE) {
S.ee_ack = 1;
if (S.ee[S.ee_addr] != rx) { S.ee[S.ee_addr] = (uint8_t)rx; S.ee_dirty = 1; }
S.ee_addr = (uint16_t)(S.ee_addr - (S.ee_addr % 16) + ((S.ee_addr + 1) % 16));
}
}
}
}
}
/* ---- infrared (CON_V3_IR.py): receiver PB bit 2, sender PC bit 2; constants in cycles, here in ticks ---- */
#define IR_T(c) ((int32_t)(c) * (int32_t)V3_TICKS)
#define IR_AGC_B IR_T(38232)
#define IR_AGC_S IR_T(9908)
#define IR_BIT_B IR_T(1768)
#define IR_S0 IR_T(3088)
#define IR_S1 IR_T(5508)
#define IR_END IR_T(4680)
#define IR_PULSE_TH IR_T(104 + 5)
#define IR_BIT_TH IR_T((5508 + 3088) / 2)
enum { IR_IDLE, IR_SEND, IR_STOP };
static void ir_port(int value) /* port_handler: the CPU wrote port C */
{
if (S.ir_prev == (uint8_t)value) return;
S.ir_prev = (uint8_t)value;
if (!(value & 4)) return;
if (S.ir_txs < IR_PULSE_TH) S.ir_txb += S.ir_txs;
else {
if (S.ir_txb < IR_END) {
S.ir_txbyte >>= 1;
if (S.ir_txs > IR_BIT_TH) S.ir_txbyte |= 0x80;
if (++S.ir_txbits == 8) { if (S.ir_txn < sizeof S.ir_txq) S.ir_txq[S.ir_txn++] = S.ir_txbyte; S.ir_txbits = 0; }
} else S.ir_txbits = 0;
S.ir_txb = 0;
}
S.ir_txs = 0;
}
static void cpu_port_in(int p, int mask, int level);
static void ir_rx_clock(int32_t t);
static inline void ir_clock(int32_t t) /* clock(cycles) */
{
if (S.ir_txs < 0x40000000) S.ir_txs += t;
if (S.ir_rxstep != IR_IDLE) ir_rx_clock(t);
}
static void ir_rx_clock(int32_t t)
{
S.ir_rxb += t;
if (S.ir_rxb < 0) return;
cpu_port_in(PB, 4, 1);
S.ir_rxs += t;
if (S.ir_rxstep == IR_STOP) { S.ir_rxstep = IR_IDLE; return; }
if (S.ir_rxs < 0) return;
cpu_port_in(PB, 4, 0);
if (S.ir_rxbits == 8) {
S.ir_rxbits = 0;
if (S.ir_rxn) { S.ir_rxbyte = S.ir_rxq[0]; S.ir_rxn--; memmove(S.ir_rxq, S.ir_rxq + 1, S.ir_rxn); }
else { S.ir_rxb = -IR_END; S.ir_rxstep = IR_STOP; return; }
}
S.ir_rxb = -IR_BIT_B;
S.ir_rxs = (S.ir_rxbyte & 1) ? -IR_S1 : -IR_S0;
S.ir_rxbyte >>= 1; S.ir_rxbits++;
}
void v3_ir_rx(const uint8_t *d, int n) /* serial_rx_handler: bytes from the other toy */
{
for (int i = 0; i < n && S.ir_rxn < sizeof S.ir_rxq; i++) S.ir_rxq[S.ir_rxn++] = d[i];
if (S.ir_rxstep == IR_IDLE && S.ir_rxn) {
cpu_port_in(PB, 4, 0);
S.ir_rxbyte = S.ir_rxq[0]; S.ir_rxn--; memmove(S.ir_rxq, S.ir_rxq + 1, S.ir_rxn);
S.ir_rxbits = 0; S.ir_rxb = -IR_AGC_B; S.ir_rxs = -IR_AGC_S; S.ir_rxstep = IR_SEND;
}
}
int v3_ir_tx(uint8_t *out, int max) /* emit_serial_tx: bytes the toy sent */
{
int n = S.ir_txn < max ? S.ir_txn : max;
for (int i = 0; i < n; i++) out[i] = S.ir_txq[i];
S.ir_txn = (uint8_t)(S.ir_txn - n); memmove(S.ir_txq, S.ir_txq + n, S.ir_txn);
return n;
}
static void emit_port(int p) { if (p == PD) ee_port(port_read(PD)); else if (p == PCP) ir_port(port_read(PCP)); }
/* ---- flags ---- */
static int get_ps(void) { return (NF << 7) | (VF << 6) | (BF << 4) | (DF << 3) | (IF << 2) | (ZF << 1) | CF; }
static void set_ps(int ps) { NF = ps >> 7 & 1; VF = (ps & 0x40) > 0; BF = (ps & 0x10) > 0; DF = (ps & 0x08) > 0; IF = (ps & 0x04) > 0; ZF = (ps & 0x02) > 0; CF = ps & 1; }
static void tm_sync(void);
/* ---- SFRs ---- */
static int sfr_read(int a)
{
#ifdef V3_TMFAST
if (a == 0x0C || (a >= 0x10 && a <= 0x15) || a == 0x01 || a == 0x18 || a == 0x38) tm_sync();
#else
if (a == 0x0C || (a >= 0x10 && a <= 0x15)) tm_sync();
#endif
switch (a) {
case 0x00: return (int)(S.rom_bank >> 15);
case 0x04: return S.cpu_div;
case 0x08: return port_read(PA);
case 0x0A: return port_read(PB);
case 0x0B: return port_read(PCP);
case 0x09: return port_read(PD);
case 0x24: return S.lcd_ctrl1;
case 0x01: return S.ireq;
case 0x38: return S.nmireq | (S.nmi_ctrl & 0x80);
case 0x18: return S.wakeupreq;
case 0x0C: return (int)S.t1khz & 0xFF; /* Python returns T1KHZ unmasked; a byte read keeps the low 8 bits */
case 0x0F: return S.timer_ctrl;
case 0x10: return (int)(S.tm0 >> 7) & 0xFF;
case 0x11: return (int)(S.tm0 >> 7) >> 8 & 0xFF;
case 0x14: return (int)(S.tm1 >> 7) & 0xFF;
case 0x15: return (int)(S.tm1 >> 7) >> 8 & 0xFF;
case 0x3E: return S.byte_mirror;
case 0x3F: return S.nibble_swap;
case 0x12: return S.a0c;
case 0x16: return S.a1c;
case 0x17: return S.a1d;
default: return 0;
}
}
static int bexp(int v) { return ((v & 1) * 3) | ((v & 2) * 6) | ((v & 4) * 0xC) | ((v & 8) * 0x18); }
static void snd_data(int ch, int ctrl, int data)
{
if (ctrl & 0x40) S.snd_env[ch] = (uint8_t)data;
else S.snd_env[ch] = 0; /* PCM samples: not on the buzzer */
}
static void sfr_write(int a, int v)
{
v &= 0xFF;
int sy = a == 0x0C || a == 0x0F || (a >= 0x10 && a <= 0x15) || a == 0x38;
#ifdef V3_TMFAST
sy |= a == 0x01 || a == 0x02 || a == 0x18 || a == 0x12 || a == 0x16 || a == 0x13 || a == 0x17;
#endif
if (sy) tm_sync();
switch (a) {
case 0x00: S.rom_bank = (uint32_t)(v & 0x0F) << 15; break;
case 0x04: if ((v & 7) == 7) S.cpu_enbl = 0; else S.cpu_div = (uint8_t)(v & 7); break;
case 0x0E: S.clk32k = (uint8_t)(v & 0x80); break;
case 0x06: S.attr[PA] = (uint8_t)v; emit_port(PA); break;
case 0x07: S.pdir[PA] = (uint8_t)v; emit_port(PA); break;
case 0x08: S.platch[PA] = (uint8_t)v; emit_port(PA); break;
case 0x28: S.attr[PB] = (uint8_t)bexp(v); emit_port(PB); break;
case 0x29: S.pdir[PB] = (uint8_t)bexp(v); emit_port(PB); break;
case 0x0A: S.platch[PB] = (uint8_t)v; emit_port(PB); break;
case 0x2A: S.attr[PCP] = (uint8_t)v; emit_port(PCP); break;
case 0x2B: S.pdir[PCP] = (uint8_t)v; emit_port(PCP); break;
case 0x0B: S.platch[PCP] = (uint8_t)v; emit_port(PCP); break;
case 0x2C: S.attr[PD] = (uint8_t)(v & 0x3F); emit_port(PD); break;
case 0x2D: S.pdir[PD] = (uint8_t)(v & 0x3F); emit_port(PD); break;
case 0x09: S.platch[PD] = (uint8_t)(v & 0x3F); emit_port(PD); break;
case 0x24: S.lcd_ctrl1 = (uint8_t)v; break;
case 0x01: S.int_ctrl = (uint8_t)v; S.ireq = 0; break;
case 0x02: S.ireq &= (uint8_t)~v; break;
case 0x38: S.nmi_ctrl = (uint8_t)v; S.nmireq &= (uint8_t)v; break;
case 0x18: S.wakeup_ctrl = (uint8_t)v; S.wakeupreq &= (uint8_t)v; break;
case 0x0C: S.time_base = (uint8_t)v; break;
case 0x0F: S.timer_ctrl = (uint8_t)v; break;
case 0x10: S.tm0_rel = (S.tm0_rel & 0xFF00u) | (uint32_t)v; break;
case 0x11: S.tm0_rel = (S.tm0_rel & 0xFFu) | ((uint32_t)v << 8); S.tm0 = (int64_t)S.tm0_rel << 7; break;
case 0x14: S.tm1_rel = (S.tm1_rel & 0xFF00u) | (uint32_t)v; break;
case 0x15: S.tm1_rel = (S.tm1_rel & 0xFFu) | ((uint32_t)v << 8); S.tm1 = (int64_t)S.tm1_rel << 7; break;
case 0x3E: { int b = (v & 0xF0) >> 4 | (v & 0x0F) << 4; b = (b & 0xCC) >> 2 | (b & 0x33) << 2; S.byte_mirror = (uint8_t)((b & 0xAA) >> 1 | (b & 0x55) << 1); } break;
case 0x3F: S.nibble_swap = (uint8_t)(((v & 0x0F) << 4) | ((v & 0xF0) >> 4)); break;
case 0x12: S.a0c = (uint8_t)v; break;
case 0x13: S.a0d = (uint8_t)v; snd_data(0, S.a0c, v); break;
case 0x16: S.a1c = (uint8_t)v; break;
case 0x17: S.a1d = (uint8_t)v; snd_data(1, S.a1c, v); break;
default: break;
}
if (sy) tm_sync(); /* the next event after the change */
}
/* ---- memory ---- */
static inline int RD(int addr)
{
if (addr >= 0xC000) return rom_byte((uint32_t)addr & 0x7FFF);
if (addr >= 0x4000) return rom_byte(((uint32_t)addr & 0x7FFF) | S.rom_bank);
if (addr >= DP_OFF) return addr - DP_OFF < (int)V3_DPRAM ? S.dpram[addr - DP_OFF] : 0;
if (addr >= RAM_OFF && addr < RAM_OFF + RAM_SIZE) return S.ram[addr - RAM_OFF];
return sfr_read(addr);
}
static inline void WR(int addr, int v)
{
if (addr >= RAM_OFF && addr < RAM_OFF + RAM_SIZE) S.ram[addr - RAM_OFF] = (uint8_t)v;
else if (addr >= DP_OFF && addr < DP_OFF + (int)V3_DPRAM) S.dpram[addr - DP_OFF] = (uint8_t)v;
else sfr_write(addr, v);
}
static void irq_enter(void)
{
WR(SP | 0x100, PC >> 8); SP = (SP - 1) & 0xFF;
WR(SP | 0x100, PC & 0xFF); SP = (SP - 1) & 0xFF;
WR(SP | 0x100, get_ps()); SP = (SP - 1) & 0xFF;
IF = 1; PC = S.addr_irq;
}
static void nmi_enter(void)
{
if ((S.nmi_ctrl & 0x80) && S.cpu_enbl) {
WR(SP | 0x100, PC >> 8); SP = (SP - 1) & 0xFF;
WR(SP | 0x100, PC & 0xFF); SP = (SP - 1) & 0xFF;
WR(SP | 0x100, get_ps()); SP = (SP - 1) & 0xFF;
PC = S.addr_nmi;
}
}
static void do_adc(int operand)
{
int a = A, nv = a + operand + CF;
if (DF && ((a & 0x0F) + (operand & 0x0F) + CF > 9)) nv += 6;
VF = ((~(a ^ operand) & (a ^ nv)) >> 7) & 1;
NF = (nv >> 7) & 1;
if (DF && nv > 0x99) nv += 0x60;
ZF = !(nv & 0xFF); CF = nv > 255; A = nv & 0xFF;
}
static void do_sbc(int operand)
{
int a = A, nv = a - operand - (!CF);
if (DF) { if ((a & 0x0F) - (operand & 0x0F) - (!CF) < 0) nv -= 6; if (nv < 0) nv -= 0x60; }
VF = (((a ^ operand) & (a ^ nv)) >> 7) & 1;
NF = (nv >> 7) & 1; ZF = !(nv & 0xFF); CF = nv >= 0; A = nv & 0xFF;
}
static int op_dummy(void) { return 2; }
#include "v3ops.inc"
/* ---- timers (SPLB32._timers_clock) ---- */
static const uint16_t TB_H[4] = { 0x07, 0x01, 0x03, 0x00 }, TB_L[4] = { 0x1FF, 0x7F, 0xFF, 0x3F };
static void timers(int64_t t)
{
if (S.timer_ctrl & 0x60) {
S.tm0_cnt -= t;
while (S.tm0_cnt <= 0) {
int m = S.timer_ctrl & 0x60;
if (m == 0x20) { S.tm0_cnt = V3_TICKS; S.tm0 += t; }
else if (m == 0x40) { S.tm0_cnt += SUB_DIV; S.tm0 += V3_TICKS; }
else { S.tm0_cnt = V3_TICKS; break; } /* external clock: not done (the Python would hang) */
while (S.tm0 > ((int64_t)0xFFFF << 7)) {
S.tm0 -= (int64_t)(0x10000 - S.tm0_rel) << 7;
S.ireq |= 0x10; S.wakeupreq |= 0x04;
if ((S.a0c & 0x08) && (S.a0c & 0x40) && S.snd_env[0]) S.snd_tog[0] ^= 1;
if ((S.a1c & 0x08) && (S.a1c & 0x40) && S.snd_env[1]) S.snd_tog[1] ^= 1;
}
}
}
if (S.timer_ctrl & 0x03) {
S.tm1_cnt -= t;
while (S.tm1_cnt <= 0) {
int m = S.timer_ctrl & 0x03;
if (m == 0x01) { S.tm1_cnt = V3_TICKS; S.tm1 += t; }
else if (m == 0x02) { S.tm1_cnt += SUB_DIV; S.tm1 += V3_TICKS; }
else { S.tm1_cnt = V3_TICKS; break; }
while (S.tm1 > ((int64_t)0xFFFF << 7)) {
S.tm1 -= (int64_t)(0x10000 - S.tm1_rel) << 7;
S.ireq |= 0x20; S.wakeupreq |= 0x10;
S.nmireq |= 0x02;
if (S.nmi_ctrl & 0x02) nmi_enter();
if ((S.a0c & 0x10) && (S.a0c & 0x40) && S.snd_env[0]) S.snd_tog[0] ^= 1;
if ((S.a1c & 0x10) && (S.a1c & 0x40) && S.snd_env[1]) S.snd_tog[1] ^= 1;
}
}
}
S.t1k_cnt -= t;
while (S.t1k_cnt <= 0) {
S.t1k_cnt += T1K_DIV;
S.t1khz++;
if (!(S.t1khz & TB_H[S.time_base & 3])) { S.ireq |= 0x08; S.wakeupreq |= 0x08; }
if (!(S.t1khz & TB_L[(S.time_base & 0x0C) >> 2])) { S.ireq |= 0x04; S.wakeupreq |= 0x02; }
}
}
/* 1.6.9: the timers are given the ticks only when something can happen (an overflow, the 1 kHz step) or when the
* program reads or sets them; in between nothing of them is seen, so the run is the same as tick by tick */
static void tm_next(void)
{
uint64_t n = 0xFFFFFFFFu; const int64_t XM = (int64_t)0xFFFF << 7;
int m0 = S.timer_ctrl & 0x60, m1 = S.timer_ctrl & 0x03;
if (m0 == 0x20) { int64_t d = XM - S.tm0 + 1; if (d < 1) d = 1; if ((uint64_t)d < n) n = (uint64_t)d; }
else if (m0 == 0x40) { int64_t k = (XM - S.tm0) >= 0 ? ((XM - S.tm0) >> 7) + 1 : 1, d = S.tm0_cnt + (k - 1) * SUB_DIV; if (d < 1) d = 1; if ((uint64_t)d < n) n = (uint64_t)d; }
if (m1 == 0x01) { int64_t d = XM - S.tm1 + 1; if (d < 1) d = 1; if ((uint64_t)d < n) n = (uint64_t)d; }
else if (m1 == 0x02) { int64_t k = (XM - S.tm1) >= 0 ? ((XM - S.tm1) >> 7) + 1 : 1, d = S.tm1_cnt + (k - 1) * SUB_DIV; if (d < 1) d = 1; if ((uint64_t)d < n) n = (uint64_t)d; }
{ int64_t d = S.t1k_cnt; if (d < 1) d = 1; if ((uint64_t)d < n) n = (uint64_t)d; }
S.next = (uint32_t)n;
}
#ifdef V3_TMFAST
/* 1.6.13 test12: the same timers without a pass per 32 kHz step and without 64-bit arithmetic where the values fit in
* 32 bits (the counters stay int64 in the state, so saves keep their layout). The overflows of a counter are handled
* after all its steps instead of between them: nothing else is read in between, so the result is the same. Out of
* the usual range: the code above. */
#define TM_OK(v) ((v) > -0x40000000LL && (v) < 0x40000000LL)
static int tm_step32(int64_t *cnt, int64_t *tm, int m, int32_t t, int slow) /* 1: done, 0: take the code above */
{
if (!TM_OK(*cnt) || !TM_OK(*tm)) return 0;
int32_t c = (int32_t)*cnt - t;
if (c > 0) { *cnt = c; return 1; }
if (!slow) { *cnt = V3_TICKS; *tm += t; return 1; } /* main clock: one pass */
uint32_t n = (uint32_t)(-c) / SUB_DIV + 1u;
*cnt = (int64_t)c + (int64_t)n * SUB_DIV; *tm += (int64_t)n * V3_TICKS;
(void)m; return 1;
}
static void timers_fast(uint32_t t)
{
if (t >= 0x40000000u) { timers(t); return; }
int m0 = S.timer_ctrl & 0x60, m1 = S.timer_ctrl & 0x03;
if (m0) {
if (m0 == 0x60 || !TM_OK(S.tm0_cnt) || !TM_OK(S.tm0)) goto slow;
}
if (m1) {
if (m1 == 0x03 || !TM_OK(S.tm1_cnt) || !TM_OK(S.tm1)) goto slow;
}
if (!TM_OK(S.t1k_cnt)) goto slow;
if (m0) {
tm_step32(&S.tm0_cnt, &S.tm0, m0, (int32_t)t, m0 == 0x40);
while (S.tm0 > ((int64_t)0xFFFF << 7)) {
S.tm0 -= (int64_t)(0x10000 - S.tm0_rel) << 7;
S.ireq |= 0x10; S.wakeupreq |= 0x04;
if ((S.a0c & 0x08) && (S.a0c & 0x40) && S.snd_env[0]) S.snd_tog[0] ^= 1;
if ((S.a1c & 0x08) && (S.a1c & 0x40) && S.snd_env[1]) S.snd_tog[1] ^= 1;
}
}
if (m1) {
tm_step32(&S.tm1_cnt, &S.tm1, m1, (int32_t)t, m1 == 0x02);
while (S.tm1 > ((int64_t)0xFFFF << 7)) {
S.tm1 -= (int64_t)(0x10000 - S.tm1_rel) << 7;
S.ireq |= 0x20; S.wakeupreq |= 0x10;
S.nmireq |= 0x02;
if (S.nmi_ctrl & 0x02) nmi_enter();
if ((S.a0c & 0x10) && (S.a0c & 0x40) && S.snd_env[0]) S.snd_tog[0] ^= 1;
if ((S.a1c & 0x10) && (S.a1c & 0x40) && S.snd_env[1]) S.snd_tog[1] ^= 1;
}
}
{ int32_t c = (int32_t)S.t1k_cnt - (int32_t)t;
if (c <= 0) {
uint32_t n = (uint32_t)(-c) / T1K_DIV + 1u, o = S.t1khz, h = TB_H[S.time_base & 3], l = TB_L[(S.time_base & 0x0C) >> 2];
c += (int32_t)(n * T1K_DIV); S.t1khz = o + n;
/* a step onto a multiple of h + 1 (h = 2^k - 1): the bits above h changed (n < 2^31) */
if (!h || ((o + n) & ~h) != (o & ~h) || n > h) { S.ireq |= 0x08; S.wakeupreq |= 0x08; }
if (!l || ((o + n) & ~l) != (o & ~l) || n > l) { S.ireq |= 0x04; S.wakeupreq |= 0x02; }
}
S.t1k_cnt = c; }
return;
slow:
timers(t);
}
static void tm_next_fast(void)
{
const int32_t XM = 0xFFFF << 7;
if (!TM_OK(S.tm0) || !TM_OK(S.tm1) || !TM_OK(S.tm0_cnt) || !TM_OK(S.tm1_cnt) || !TM_OK(S.t1k_cnt)) { tm_next(); return; }
uint32_t n = 0x10000000u; int32_t tm0 = (int32_t)S.tm0, tm1 = (int32_t)S.tm1;
int m0 = S.timer_ctrl & 0x60, m1 = S.timer_ctrl & 0x03;
/* an event nobody would see before the next sync (its flags masked, no NMI) is left to that sync: the flags then
* read the same (the flag registers sync when read or written) */
if (!(S.int_ctrl & 0x10) && !(S.wakeup_ctrl & 0x04)) m0 = 0;
if (!(S.int_ctrl & 0x20) && !(S.wakeup_ctrl & 0x10) && !(S.nmi_ctrl & 0x02)) m1 = 0;
if (m0 == 0x20) { int32_t d = XM - tm0 + 1; if (d < 1) d = 1; if ((uint32_t)d < n) n = (uint32_t)d; }
else if (m0 == 0x40) { int32_t k = (XM - tm0) >= 0 ? ((XM - tm0) >> 7) + 1 : 1; int64_t d = S.tm0_cnt + (int64_t)(k - 1) * SUB_DIV; if (d < 1) d = 1; if ((uint64_t)d < n) n = (uint32_t)d; }
if (m1 == 0x01) { int32_t d = XM - tm1 + 1; if (d < 1) d = 1; if ((uint32_t)d < n) n = (uint32_t)d; }
else if (m1 == 0x02) { int32_t k = (XM - tm1) >= 0 ? ((XM - tm1) >> 7) + 1 : 1; int64_t d = S.tm1_cnt + (int64_t)(k - 1) * SUB_DIV; if (d < 1) d = 1; if ((uint64_t)d < n) n = (uint32_t)d; }
{ uint32_t o = S.t1khz, j = 0xFFFFFFFFu, h = TB_H[S.time_base & 3], l = TB_L[(S.time_base & 0x0C) >> 2];
if ((S.int_ctrl & 0x08) || (S.wakeup_ctrl & 0x08)) j = ((o | h) + 1u) - o; /* steps to the next flag */
if ((S.int_ctrl & 0x04) || (S.wakeup_ctrl & 0x02)) { uint32_t k = ((o | l) + 1u) - o; if (k < j) j = k; }
if (j > 0x1000u) j = 0x1000u;
int64_t d = S.t1k_cnt + (int64_t)(j - 1u) * T1K_DIV; if (d < 1) d = 1; if ((uint64_t)d < n) n = (uint32_t)d; }
S.next = n;
}
static void tm_sync(void) { if (S.pend) { timers_fast(S.pend); S.pend = 0; } tm_next_fast(); }
#else
static void tm_sync(void) { if (S.pend) { timers(S.pend); S.pend = 0; } tm_next(); }
#endif
uint32_t v3_step(void)
{
if (S.cpu_enbl) {
int op = RD(PC); PC = (PC + 1) & 0xFFFF;
uint32_t c = (uint32_t)v3_op[op]() << S.cpu_div, t = c * V3_TICKS;
if ((S.pend += t) >= S.next) tm_sync();
if (!IF && (S.ireq & S.int_ctrl)) irq_enter();
ir_clock((int32_t)t);
return t;
}
if (S.wakeup_ctrl & S.wakeupreq) { S.cpu_enbl = 1; S.cpu_div = 2; }
if (S.clk32k && (S.pend += SUB_DIV) >= S.next) tm_sync();
ir_clock(SUB_DIV);
return SUB_DIV;
}
/* runs at least ticks (the rest of the last call counted in); while stopped, the 32 kHz steps without an event at once */
#ifndef V3_BATCH
#define V3_BATCH 1
#endif
static int64_t g_debt;
uint64_t v3_run(uint64_t ticks)
{
#ifdef V3_TMFAST
/* test12: the usual call (1 ms) in 32-bit arithmetic, the same steps */
if (ticks < 0x20000000u && g_debt < 0x20000000 && g_debt > -0x20000000) {
int32_t d = 0, want = (int32_t)ticks - (int32_t)g_debt;
while (d < want) {
if (V3_BATCH && !S.cpu_enbl && !(S.wakeup_ctrl & S.wakeupreq) && S.ir_rxstep == IR_IDLE) {
uint32_t k = (uint32_t)(want - d) / SUB_DIV;
if (S.clk32k) { uint32_t e = (S.next - S.pend - 1u) / SUB_DIV; if (e < k) k = e; }
if (k > 1) { k--; if (S.clk32k) S.pend += k * SUB_DIV; d += (int32_t)(k * SUB_DIV); if (S.ir_txs < 0x40000000) S.ir_txs += (int32_t)(k < 0x8000u ? k * SUB_DIV : 0x40000000u); continue; }
}
d += (int32_t)v3_step();
}
g_debt = d - want;
return (uint64_t)d;
}
#endif
uint64_t d = 0; int64_t want = (int64_t)ticks - g_debt;
while ((int64_t)d < want) {
if (V3_BATCH && !S.cpu_enbl && !(S.wakeup_ctrl & S.wakeupreq) && S.ir_rxstep == IR_IDLE) {
int64_t lf = want - (int64_t)d; uint32_t k = lf > 0x7FFFFFFF ? 0x7FFFFFFFu / SUB_DIV : (uint32_t)lf / SUB_DIV;
if (S.clk32k) { uint32_t e = (S.next - S.pend - 1u) / SUB_DIV; if (e < k) k = e; } /* steps before the event */
if (k > 1) { k--; if (S.clk32k) S.pend += k * SUB_DIV; d += (uint64_t)k * SUB_DIV; if (S.ir_txs < 0x40000000) S.ir_txs += (int32_t)(k < 0x8000u ? k * SUB_DIV : 0x40000000u); continue; }
}
d += v3_step();
}
g_debt = (int64_t)d - want;
return d;
}
void v3_reset(void)
{
uint8_t ee[V3_EE_SIZE]; memcpy(ee, S.ee, sizeof ee);
memset(&S, 0, sizeof S); memcpy(S.ee, ee, sizeof ee);
A = X = Y = SP = PC = 0; set_ps(0x04);
S.cpu_enbl = 1; S.cpu_div = 2; S.clk32k = 0x80;
S.addr_irq = (uint16_t)(rom_byte(VADDR_IRQ) | rom_byte(VADDR_IRQ + 1) << 8);
S.addr_nmi = (uint16_t)(rom_byte(VADDR_NMI) | rom_byte(VADDR_NMI + 1) << 8);
PC = rom_byte(VADDR_RESET) | rom_byte(VADDR_RESET + 1) << 8;
S.ee_scl = 1; S.ee_sda = 1;
tm_next(); g_debt = 0;
cpu_port_in(PB, 4, 0); /* the IR receiver (CON_V3_IR) holds PB bit 2 low */
}
void v3_init(const uint8_t *rom, uint32_t size) { g_rom = rom; g_rom_size = size; g_rom_mask = (size & (size - 1u)) ? 0u : size - 1u; memset(S.ee, 0, sizeof S.ee); v3_reset(); }
void v3_set_keys(uint32_t mask)
{
static const uint8_t K[3] = { 1, 2, 4 };
for (int i = 0; i < 3; i++) {
int down = (mask >> i) & 1, was = (S.pin1[PA] & K[i]) != 0;
if (down != was) cpu_port_in(PA, K[i], down ? 1 : -1);
}
}
const uint8_t *v3_vram(int *mode)
{
int m = 0;
if (S.clk32k) { if ((S.lcd_ctrl1 & 0x0D) == 0x01) m = 1; else if (S.lcd_ctrl1 & 0x04) m = 2; }
*mode = m; return S.dpram;
}
uint32_t v3_tone_hz(void)
{
for (int ch = 0; ch < 2; ch++) {
int c = ch ? S.a1c : S.a0c;
if (!(c & 0x40) || !S.snd_env[ch]) continue;
if (c & 0x08) { uint32_t n = 0x10000u - S.tm0_rel, m = S.timer_ctrl & 0x60; if (n && m == 0x20) return V3_CLOCK / n / 2u; if (n && m == 0x40) return 32768u / n / 2u; }
if (c & 0x10) { uint32_t n = 0x10000u - S.tm1_rel, m = S.timer_ctrl & 0x03; if (n && m == 0x01) return V3_CLOCK / n / 2u; if (n && m == 0x02) return 32768u / n / 2u; }
}
return 0;
}
uint8_t *v3_eeprom(void) { return S.ee; }
/* test21: the EEPROM inside a saved state (v3_state_save), for a broken settings sector */
const uint8_t *v3_state_ee(const uint8_t *b) { return b + 56 + __builtin_offsetof(V3S, ee); }
int v3_eeprom_dirty(int clear) { int d = S.ee_dirty; if (clear) S.ee_dirty = 0; return d; }
uint8_t *v3_ram(void) { return S.ram; }
#define V3_SV_MAGIC 0x31535633u /* "3VS1" */
uint32_t v3_state_size(void) { return 4u + 13u * 4u + (uint32_t)sizeof(V3S); }
void v3_state_save(uint8_t *b)
{
#if defined(V3_TMFAST) || defined(V3_SAVESYNC)
tm_sync(); /* the deferred timer events in (the same state, written out) */
#endif
uint32_t r[14] = { V3_SV_MAGIC, (uint32_t)A, (uint32_t)X, (uint32_t)Y, (uint32_t)SP, (uint32_t)PC, (uint32_t)NF, (uint32_t)VF, (uint32_t)BF, (uint32_t)DF, (uint32_t)IF, (uint32_t)ZF, (uint32_t)CF, 0 };
memcpy(b, r, 56); memcpy(b + 56, &S, sizeof S);
}
int v3_state_load(const uint8_t *b, uint32_t n)
{
uint32_t r[14]; if (n < v3_state_size()) return 0;
memcpy(r, b, 56); if (r[0] != V3_SV_MAGIC) return 0;
A = (int)r[1]; X = (int)r[2]; Y = (int)r[3]; SP = (int)r[4]; PC = (int)r[5]; NF = (int)r[6]; VF = (int)r[7]; BF = (int)r[8]; DF = (int)r[9]; IF = (int)r[10]; ZF = (int)r[11]; CF = (int)r[12];
memcpy(&S, b + 56, sizeof S);
return 1;
}
uint32_t v3_pc(void) { return (uint32_t)PC; }
uint32_t v3_bank(void) { return S.rom_bank >> 15; }
void v3_regs(uint8_t r[5], uint8_t f[6]) { r[0] = (uint8_t)A; r[1] = (uint8_t)X; r[2] = (uint8_t)Y; r[3] = (uint8_t)SP; r[4] = 0; f[0] = (uint8_t)NF; f[1] = (uint8_t)VF; f[2] = (uint8_t)DF; f[3] = (uint8_t)IF; f[4] = (uint8_t)ZF; f[5] = (uint8_t)CF; }
+30
View File
@@ -0,0 +1,30 @@
/* v3cpu.h - Tamagotchi Connection V3 (Sunplus SPLB32, a 6502) in C, ported from BrickEmuPy (azya52, CC0):
* cores/SPLB32.py, peripherals/HT24LC08.py (EEPROM), direct_input.py (keys). Time in ticks of 1/128 cycle
* (4 MHz main clock, the 32768 Hz clock is exactly 15625 ticks), so the timers count as the Python floats did.
* Author: mameise */
#ifndef V3CPU_H
#define V3CPU_H
#include <stdint.h>
#define V3_CLOCK 4000000u
#define V3_TICKS 128u /* ticks per cycle */
#define V3_EE_SIZE 1024u
#define V3_DPRAM 0x200u
void v3_init(const uint8_t *rom, uint32_t size); /* rom stays where it is (flash) */
void v3_reset(void);
uint64_t v3_run(uint64_t ticks); /* runs at least ticks, returns the ticks run */
uint32_t v3_step(void); /* one instruction (or one 32 kHz step while stopped), its ticks */
void v3_set_keys(uint32_t mask); /* bit 0 left (A), 1 middle (B), 2 right (C) */
const uint8_t *v3_vram(int *mode); /* the LCD RAM (0x200 bytes); mode 0 off, 1 on, 2 all on */
uint32_t v3_tone_hz(void); /* square tone of the buzzer now, 0 = quiet */
uint8_t *v3_eeprom(void);
const uint8_t *v3_state_ee(const uint8_t *b); /* test21 */ /* 1 KB */
int v3_eeprom_dirty(int clear);
uint32_t v3_state_size(void);
void v3_state_save(uint8_t *b);
int v3_state_load(const uint8_t *b, uint32_t n);
void v3_ir_rx(const uint8_t *d, int n); /* infrared: bytes from the other toy */
int v3_ir_tx(uint8_t *out, int max); /* infrared: bytes this toy sent (taken out) */
uint8_t *v3_ram(void); /* 0x4C0 bytes from address 0x40 */
/* debug */
uint32_t v3_pc(void); void v3_regs(uint8_t r[5], uint8_t f[6]); uint32_t v3_bank(void);
#endif
+33
View File
@@ -0,0 +1,33 @@
/* LCD RAM bit (byte * 8 + bit) of each pixel of the 32 x 30 dot matrix, from the face of BrickEmuPy (assets/Tamagotchi_v3.svg, CC0) */
static const uint16_t V3_LCD[30][32] = {
{128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159},
{256,257,258,259,260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279,280,281,282,283,284,285,286,287},
{384,385,386,387,388,389,390,391,392,393,394,395,396,397,398,399,400,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415},
{512,513,514,515,516,517,518,519,520,521,522,523,524,525,526,527,528,529,530,531,532,533,534,535,536,537,538,539,540,541,542,543},
{640,641,642,643,644,645,646,647,648,649,650,651,652,653,654,655,656,657,658,659,660,661,662,663,664,665,666,667,668,669,670,671},
{768,769,770,771,772,773,774,775,776,777,778,779,780,781,782,783,784,785,786,787,788,789,790,791,792,793,794,795,796,797,798,799},
{896,897,898,899,900,901,902,903,904,905,906,907,908,909,910,911,912,913,914,915,916,917,918,919,920,921,922,923,924,925,926,927},
{1920,1921,1922,1923,1924,1925,1926,1927,1928,1929,1930,1931,1932,1933,1934,1935,1936,1937,1938,1939,1940,1941,1942,1943,1944,1945,1946,1947,1948,1949,1950,1951},
{1792,1793,1794,1795,1796,1797,1798,1799,1800,1801,1802,1803,1804,1805,1806,1807,1808,1809,1810,1811,1812,1813,1814,1815,1816,1817,1818,1819,1820,1821,1822,1823},
{1664,1665,1666,1667,1668,1669,1670,1671,1672,1673,1674,1675,1676,1677,1678,1679,1680,1681,1682,1683,1684,1685,1686,1687,1688,1689,1690,1691,1692,1693,1694,1695},
{1536,1537,1538,1539,1540,1541,1542,1543,1544,1545,1546,1547,1548,1549,1550,1551,1552,1553,1554,1555,1556,1557,1558,1559,1560,1561,1562,1563,1564,1565,1566,1567},
{1408,1409,1410,1411,1412,1413,1414,1415,1416,1417,1418,1419,1420,1421,1422,1423,1424,1425,1426,1427,1428,1429,1430,1431,1432,1433,1434,1435,1436,1437,1438,1439},
{1280,1281,1282,1283,1284,1285,1286,1287,1288,1289,1290,1291,1292,1293,1294,1295,1296,1297,1298,1299,1300,1301,1302,1303,1304,1305,1306,1307,1308,1309,1310,1311},
{1152,1153,1154,1155,1156,1157,1158,1159,1160,1161,1162,1163,1164,1165,1166,1167,1168,1169,1170,1171,1172,1173,1174,1175,1176,1177,1178,1179,1180,1181,1182,1183},
{1024,1025,1026,1027,1028,1029,1030,1031,1032,1033,1034,1035,1036,1037,1038,1039,1040,1041,1042,1043,1044,1045,1046,1047,1048,1049,1050,1051,1052,1053,1054,1055},
{1087,1086,1085,1084,1083,1082,1081,1080,1079,1078,1077,1076,1075,1074,1073,1072,1071,1070,1069,1068,1067,1066,1065,1064,1063,1062,1061,1060,1059,1058,1057,1056},
{1215,1214,1213,1212,1211,1210,1209,1208,1207,1206,1205,1204,1203,1202,1201,1200,1199,1198,1197,1196,1195,1194,1193,1192,1191,1190,1189,1188,1187,1186,1185,1184},
{1343,1342,1341,1340,1339,1338,1337,1336,1335,1334,1333,1332,1331,1330,1329,1328,1327,1326,1325,1324,1323,1322,1321,1320,1319,1318,1317,1316,1315,1314,1313,1312},
{1471,1470,1469,1468,1467,1466,1465,1464,1463,1462,1461,1460,1459,1458,1457,1456,1455,1454,1453,1452,1451,1450,1449,1448,1447,1446,1445,1444,1443,1442,1441,1440},
{1599,1598,1597,1596,1595,1594,1593,1592,1591,1590,1589,1588,1587,1586,1585,1584,1583,1582,1581,1580,1579,1578,1577,1576,1575,1574,1573,1572,1571,1570,1569,1568},
{1727,1726,1725,1724,1723,1722,1721,1720,1719,1718,1717,1716,1715,1714,1713,1712,1711,1710,1709,1708,1707,1706,1705,1704,1703,1702,1701,1700,1699,1698,1697,1696},
{1855,1854,1853,1852,1851,1850,1849,1848,1847,1846,1845,1844,1843,1842,1841,1840,1839,1838,1837,1836,1835,1834,1833,1832,1831,1830,1829,1828,1827,1826,1825,1824},
{1983,1982,1981,1980,1979,1978,1977,1976,1975,1974,1973,1972,1971,1970,1969,1968,1967,1966,1965,1964,1963,1962,1961,1960,1959,1958,1957,1956,1955,1954,1953,1952},
{959,958,957,956,955,954,953,952,951,950,949,948,947,946,945,944,943,942,941,940,939,938,937,936,935,934,933,932,931,930,929,928},
{831,830,829,828,827,826,825,824,823,822,821,820,819,818,817,816,815,814,813,812,811,810,809,808,807,806,805,804,803,802,801,800},
{703,702,701,700,699,698,697,696,695,694,693,692,691,690,689,688,687,686,685,684,683,682,681,680,679,678,677,676,675,674,673,672},
{575,574,573,572,571,570,569,568,567,566,565,564,563,562,561,560,559,558,557,556,555,554,553,552,551,550,549,548,547,546,545,544},
{447,446,445,444,443,442,441,440,439,438,437,436,435,434,433,432,431,430,429,428,427,426,425,424,423,422,421,420,419,418,417,416},
{319,318,317,316,315,314,313,312,311,310,309,308,307,306,305,304,303,302,301,300,299,298,297,296,295,294,293,292,291,290,289,288},
{191,190,189,188,187,186,185,184,183,182,181,180,179,178,177,176,175,174,173,172,171,170,169,168,167,166,165,164,163,162,161,160}
};
File diff suppressed because it is too large. Load diff