다마고치 연구소 한국어판 소스 공개
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 V1Emu for the Paradise (clang/lld, Cortex-M3). Output: ../../v1.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 -Wno-initializer-overrides -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 -DV1_CLK_CHECK -DV1_CLK_DISC -DV1_CARE -DV1_DIRECT -DV1_ICONSEG -DV1_REGB -DBAT_SHOW -DV1_LINK -DCLK_ECO -DECO_RTC -DBAT_SMOOTH -DHW_OPTS -DV1_VOL -DV1_TONE_Q -DV1_CALL_WAKE -DV1_PW -DTONE_SAFE -DBAT_GUARD -DBAT_PIC -DPARA_FLASH -DPARA_FLASH_LATE -DPARA_CLEAN_CLK -DSESS_FLAG -DV1_STALL -I../core $XCF"
O=""
for f in main.c $E/hw.c $E/ask.c $E/flash.c $E/libc.c ../core/cpu.c ../core/hw.c ../core/tamalib.c; do
n=$(basename $f .c); [ "$f" = ../core/hw.c ] && n=thw; OPT=-Os; [ "$n" = cpu ] && OPT=-O2; clang $CF $OPT -c $f -o $n.o; O="$O $n.o"; done
ld.lld -T link.ld --no-rosegment $O -o v1.elf
llvm-objcopy -O binary -j .text -j .data -j .init v1.elf v1.bin
cp v1.bin ../../v1.bin; rm -f *.o v1.bin; [ -n "$KEEPELF" ] || rm -f v1.elf; ls -l ../../v1.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},
};
+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*) }
}
+931
View File
@@ -0,0 +1,931 @@
/* V1Emu: the first Tamagotchis and the Digital Monster (Epson E0C6S46/E0C6S48, 4-bit) on the
* Tamagotchi Paradise. Core: TamaLIB by Jean-Christophe Rona (GPL v2 or later), see ../core.
* Started from the Paramix menu like the other further programs.
* Keys: A, B, C as on the toy; wheel short = tap (vibration sensor of the Angel); wheel long = save
* and back; wheel within 3 s after a resumed start = start over (asks first).
* The screen: 32x16 dots four times enlarged, the eight icons above and below.
* Author: mameise */
#include "../../../emu/src/emu/hw.h"
#include "../../../emu/src/emu/exitkey.h"
#ifdef CLK_ECO
#include "../../../emu/src/emu/eco.h"
#endif
#include "../../../emu/src/emu/flash.h"
#include "../core/tamalib.h"
#include "../core/hal.h"
#include "../core/cpu.h"
#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, filled in by the Studio (magic "PMX1"), at 0x140 in the program.
* save_off: session (4 KB), then the settings of the device (4 KB). icons: the eight LCD icons of this toy
* (16x16, bit 15 left), used when icons_ok is 1 (the Studio has a set for this ROM), else our own pictograms. */
typedef struct { u32 magic, version, rom_off, rom_len, save_off, flags; char name[16]; u32 icons_ok; u16 icons[8][16]; } V1Cfg;
#define V1_MAGIC 0x31584D50u /* "PMX1" */
__attribute__((section(".pmxcfg"), used))
volatile const V1Cfg g_cfg = { V1_MAGIC, 1, 0, 0, 0, 0, {0}, 0, {{0}} };
#define SESSION (g_cfg.save_off)
#define SETTINGS (g_cfg.save_off + 0x1000u)
#include "font.h"
#ifdef V1_LINK
#include "../../../emu/src/emu/plink.h"
/* 1.6.9: Digimon battles between two Paradise. The toy's contact (P20, cpu.c) as edges with the toy's own time
* ('D' frames: level, time in us). The other side plays them back at that time plus an offset, set at the first
* edge of a burst (30 ms quiet before) to "now plus LK_LEAD": the lengths of the pulses stay exact, the
* transport only shifts them. */
#define LK_LEAD 20000u
static struct { u32 t; u8 low; } g_lq[64];
static u8 g_lqh, g_lqt, g_rlow, g_have;
static s32 g_off; static u32 g_last_rt;
void v1_link_out(int low, u32_t t)
{
u8 d[5] = { (u8)low, (u8)t, (u8)(t >> 8), (u8)(t >> 16), (u8)(t >> 24) };
plink_send('D', d, 5);
}
int v1_link_in(u32_t t)
{
while (g_lqh != g_lqt) {
u32 rt = g_lq[g_lqh & 63u].t;
if (!g_have || ((s32)(rt - g_last_rt) > 30000 && g_have != 2)) { g_off = (s32)(t - rt) + (s32)LK_LEAD; g_have = 2; }
if ((s32)(t - (rt + (u32)g_off)) < 0) break;
g_rlow = g_lq[g_lqh & 63u].low; g_last_rt = rt; g_lqh++; g_have = 1;
}
return g_rlow;
}
static void lk_got(u8 ty, const u8 *d, int n)
{
if (ty != 'D' || n != 5) return;
if ((u8)(g_lqt - g_lqh) >= 64u) g_lqh++; /* full: the oldest goes */
g_lq[g_lqt & 63u].low = d[0];
g_lq[g_lqt & 63u].t = (u32)d[1] | (u32)d[2] << 8 | (u32)d[3] << 16 | (u32)d[4] << 24;
g_lqt++;
}
#endif
#define ST_MAGIC 0x54533156u /* "V1ST" */
/* ---- 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 V1_PW
/* ---- test24: power saving with the screen dark (as V3Emu, V3_PW) ----
* 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;
static volatile u8 g_pw_rtcw;
static u8 g_pw_kw;
__attribute__((used, noinline)) void pw_rtc_irq(void) { PMU_CTRL = (PMU_CTRL & ~0x47u) | 0x10u; g_pw_rtcw = 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;
for (u32 t = 0; !(OSC_STAT & (1u << 4)) && t < 2000000u; t++) {}
OSC_CTRL |= 1u << 15;
for (u32 t = 0; !(OSC_STAT & (1u << 6)) && t < 2000000u; t++) {}
SYS_CLKCFG = 4u;
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;
for (u32 t = 0; ((SYS_CLKCFG >> 4) & 7u) != 0u && t < 2000000u; t++) {}
if (((SYS_CLKCFG >> 4) & 7u) != 0u) { SYS_CLKCFG = 4u; return; }
OSC_CTRL &= ~((1u << 15) | (1u << 4));
SYST_RVR = 12000000u / 1000u - 1u; SYST_CVR = 0;
g_pw_low = 1;
}
__attribute__((noinline, minsize)) static u32 pw_sleep(void)
{
u32 iso = ISO_CTRL;
if (!(iso & 0x80u) || (iso & 7u) != 0u) { __asm__ volatile("wfi"); return 0; }
ISO_CTRL = iso & ~8u;
GP0(0x0C) &= ~PW_KEYS; GP0(0x10) |= PW_KEYS; GP0(0x20) = PW_KEYS; GP0(0x18) |= PW_KEYS;
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;
__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;
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
#ifdef V1_TONE_Q
/* test24: tone in real time (as V3Emu, V3_TONE_Q): changes with the toy's time (ms of g_vt), 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 V1_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
/* ---- HAL for TamaLIB: virtual time in us, moved on by the core's own waits ---- */
static u32 g_vt;
static u8 g_mat[16][32], g_icon[8], g_dirty = 1;
static u32 g_freq, g_snd_on, g_sound = 1, g_tone_now, g_tone_since, g_tone_cut;
static void *h_malloc(u32_t s) { (void)s; return 0; }
static void h_free(void *p) { (void)p; }
static void h_halt(void) {}
static bool_t h_log_on(log_level_t l) { (void)l; return 0; }
static void h_log(log_level_t l, char *b, ...) { (void)l; (void)b; }
static void h_sleep_until(timestamp_t ts) { if ((s32)(ts - g_vt) > 0) g_vt = ts; }
static timestamp_t h_ts(void) { return g_vt; }
static void h_update(void) {}
static u32 g_lcd_vt; /* 1.6.8: toy time of the last change on its LCD (a picture is shown once it holds still) */
static void h_mat(u8_t x, u8_t y, bool_t v) { if (x < 32 && y < 16 && g_mat[y][x] != v) { g_mat[y][x] = v; g_dirty = 1; g_lcd_vt = g_vt; } }
static void h_icon(u8_t i, bool_t v) { if (i < 8 && g_icon[i] != v) { g_icon[i] = v; g_dirty = 1; g_lcd_vt = g_vt; } } /* test46: Digimon too (core hw.c: its icon segments) */
/* 1.6.8 test26: the buzzer only on a change (the game switches it on and off very often), and never longer
* than 3 s in one go (no tone of these toys is that long; a stuck tone stops) */
static void snd_apply(void)
{
u32 hz = g_snd_on && g_sound && g_freq && !g_tone_cut ? g_freq / 10u : 0u;
if (hz == g_tone_now) return;
if (hz && !g_tone_now) g_tone_since = g_ms;
#ifdef V1_TONE_Q
g_tone_now = hz; tq_put(hz, g_vt / 1000u);
#else
g_tone_now = hz; hw_tone(hz);
#endif
}
static void h_freq(u32_t f) { g_freq = f; if (g_snd_on) snd_apply(); }
static void h_play(bool_t en) { if (en && !g_snd_on) g_tone_cut = 0; g_snd_on = en; snd_apply(); }
static int h_handler(void) { return 0; }
static hal_t g_hal_v1 = { h_malloc, h_free, h_halt, h_log_on, h_log, h_sleep_until, h_ts, h_update, h_mat, h_icon, h_freq, h_play, h_handler };
/* ---- settings on the device (wheel short), like T7Emu ---- */
typedef struct { u8 magic[4]; u8 pal, grid, shadow, sound, dim, lang, icons, ghost, dimto, off, sync, r0; } V1Set;
#ifdef BL_PWM
static V1Set g_set = { {'V','1','S','2'}, 0, 1, 1, 1, 7, 0, 1, 4, 6, 3, 1, 0x80 };
#else
static V1Set g_set = { {'V','1','S','2'}, 0, 1, 1, 1, 7, 0, 1, 4, 1, 3, 1, 0 };
#endif /* ghost: percent 0..20 */
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" } };
static const u8 DIM_PCT[4] = { 20u, 40u, 60u, 80u };
#ifdef BL_PWM
/* 1.6.8 (test45): r0 bit 7 = this layout, bits 0-3 = the light in general (0 = 100 %, then 5, 10, 20 .. 90 %),
* dimto = dim level 1 .. 50 % (10 steps) */
#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 };
#endif
#ifdef V1_VOL
/* test24: 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
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;
static int g_dimmed;
static void colours(void)
{
u32 k = g_set.pal < 6u ? g_set.pal : 0u;
BG = PAL[k][0]; INK = PAL[k][1]; OFF = mix(BG, INK, g_set.ghost); SHD = g_set.shadow ? mix(BG, INK, 8) : BG;
#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); } /* 1.6.8: the light, as the Paradise does it */
#else
if (g_dimmed) { int p = 100 - DIM_PCT[g_set.dimto & 3u]; BG = mix(BG, 0, p); INK = mix(INK, 0, p); OFF = mix(OFF, 0, p); SHD = mix(SHD, 0, p); }
#endif
}
static void set_load(void)
{
const volatile u8 *p = XIP_PTR(SETTINGS); V1Set 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] == '1' && t.magic[2] == 'S' && t.magic[3] == '2' && t.pal < 6u && t.dim <= T_N && t.off <= T_N && t.lang < 3u) { g_set = t; if (g_set.ghost > 20u) g_set.ghost = 4u; }
else if (t.magic[0] == 'V' && t.magic[1] == '1' && t.magic[2] == 'S' && t.magic[3] == 'E' && t.pal < 6u) { /* test23..25 */
static const u8 OLD_OFF[5] = { 2, 3, 4, 5, T_N };
g_set.pal = t.pal; g_set.grid = t.grid & 1u; g_set.shadow = t.shadow & 1u; g_set.sound = t.sound & 1u; g_set.off = OLD_OFF[t.dim < 5u ? t.dim : 1u];
}
#ifdef BL_PWM
if (!(g_set.r0 & 0x80u)) { static const u8 m[4] = { 6, 8, 9, 9 }; g_set.dimto = m[g_set.dimto & 3u]; g_set.r0 = 0x80u; } /* was 20, 40, 60, 80 % */
#endif
colours();
}
static void set_store(void)
{
static u8 sec[0x1000] __attribute__((aligned(4)));
const volatile u8 *p = XIP_PTR(SETTINGS); const u8 *s = (const u8 *)&g_set; u32 same = 1;
for (u32 i = 0; i < sizeof g_set; i++) if (p[i] != s[i]) { same = 0; break; }
if (same) return;
for (u32 i = 0; i < sizeof sec; i++) sec[i] = i < sizeof g_set ? s[i] : 0xFF;
hw_tone(0); g_tone_now = 0; flash_update4k(SETTINGS, sec);
}
static u16 g_row[128] __attribute__((aligned(4)));
/* Eight simple pictograms of our own (16x16), one per icon position */
static const u16 g_ico[8][16] = {
{ 0x0000,0x0000,0x2494,0x2494,0x2494,0x3FFC,0x1FF8,0x0FF0,0x0180,0x0180,0x0180,0x0180,0x0180,0x0180,0x07E0,0x0000 }, /* fork: food */
{ 0x0000,0x07E0,0x0FF0,0x1FF8,0x1FF8,0x1FF8,0x1FF8,0x0FF0,0x07E0,0x03C0,0x03C0,0x07E0,0x07E0,0x03C0,0x0000,0x0000 }, /* bulb: light */
{ 0x0000,0x07E0,0x1818,0x2004,0x2664,0x4662,0x4002,0x4002,0x4812,0x4422,0x23C4,0x2004,0x1818,0x07E0,0x0000,0x0000 }, /* face: game */
{ 0x0000,0x0180,0x0180,0x0180,0x0180,0x0180,0x7FFE,0x7FFE,0x0180,0x0180,0x0180,0x0180,0x0180,0x0180,0x0000,0x0000 }, /* cross: medicine */
{ 0x0000,0x0080,0x0180,0x03C0,0x07E0,0x0FF0,0x1FF8,0x1FF8,0x3FFC,0x3FFC,0x3FFC,0x1FF8,0x1FF8,0x07E0,0x0000,0x0000 }, /* drop: bath */
{ 0x0000,0x0000,0x0018,0x0018,0x0198,0x0198,0x1998,0x1998,0x1998,0x1998,0x1998,0x1998,0x7FFE,0x0000,0x0000,0x0000 }, /* bars: status */
{ 0x0000,0x0040,0x00E0,0x01F0,0x03F8,0x07FC,0x00E0,0x00E0,0x00E0,0x00E0,0x00E0,0x00E0,0x00E0,0x01F0,0x0000,0x0000 }, /* arrow: discipline */
{ 0x0000,0x0180,0x03C0,0x07E0,0x0FF0,0x0FF0,0x0FF0,0x0FF0,0x1FF8,0x1FF8,0x3FFC,0x7FFE,0x0000,0x0180,0x0000,0x0000 }, /* bell: attention */
};
#define ROT ((g_cfg.flags & 32u) != 0u)
static void draw(int all)
{
if (!g_dirty && !all) return;
g_dirty = 0;
hw_px_begin(0, 0, 128, 128); /* end exclusive */
int gap = g_set.grid ? 3 : 4; /* grid: one line between the dots */
for (int y = 0; y < 128; y++) {
for (int x = 0; x < 128; x++) g_row[x] = BG;
if (y >= 34 && y < 98) { /* the dots, four times enlarged */
int r = (y - 34) >> 2, sub = (y - 34) & 3;
const u8 *mr = ROT ? g_mat[15 - r] : g_mat[r]; /* Digimon: the LCD sits the other way round (flags bit 5) */
if (sub < gap) for (int c = 0; c < 32; c++) { u16 v = (ROT ? mr[31 - c] : mr[c]) ? INK : SHD; u16 *p = g_row + c * 4; p[0] = p[1] = p[2] = v; if (gap == 4) p[3] = v; }
} else if (g_set.icons) {
int top = y < 34, iy = top ? y - 9 : y - 103; /* icons 16 px high at y 9 and 103 */
if (iy >= 0 && iy < 16) for (int k = 0; k < 4; k++) {
int i = top ? k : 4 + k, on = (g_cfg.flags & 64u) ? g_icon[i] : g_icon[ROT ? 7 - i : i]; /* the picture of place i, lit by the toy's icon (turned: 7 - i); Digimon: core hw.c numbers its icons by place */
u16 bits = g_cfg.icons_ok ? g_cfg.icons[i][iy] : g_ico[i][iy], col = on ? INK : OFF;
if (!on && !g_set.ghost) continue;
for (int b = 0; b < 16; b++) if (bits & (0x8000u >> b)) g_row[8 + k * 32 + b] = col;
}
}
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, /* test17: the battery (shown, nothing to change) */
#endif
M_PAL, M_GRID, M_SHD, M_ICO, M_GHOST, M_SND, M_LIGHT, M_DIM, M_DIMTO, M_OFF, M_SYNC,
#ifdef V1_CARE
M_CARE,
#endif
M_LANG, M_N }; /* shake: turn the wheel (1.6.8 test30) */
static int g_items[M_N], g_nitems;
#ifdef V1_CARE
/* test31: "Full care" per toy (by the name the Studio writes), the RAM nibbles found on the PC and checked on the
* toy's status pages: hunger and happy (Digimon: strength) raised to full */
typedef struct { char nm[11]; u8 hv, av; u16 hun, hap; } V1Care;
static const V1Care CARE[] = {
{ "TAMA P1", 15, 15, 0x40, 0x41 }, { "TAMA P1 EN", 15, 15, 0x40, 0x41 }, { "TAMA P2", 15, 15, 0x40, 0x41 },
{ "ANGEL", 15, 15, 0x40, 0x43 }, { "GENJIN", 4, 5, 0x3E, 0x3D }, { "UMINO", 4, 4, 0x7E, 0x7F }, { "YASASHII", 4, 4, 0x31, 0x32 }, { "TAMAOTCH", 4, 4, 0x1E0, 0x1E1 }, { "MORINO", 15, 15, 0x40, 0x41 },
{ "DIGIMON V1", 4, 4, 0x3E, 0x3D }, { "DIGIMON V2", 4, 4, 0x3E, 0x3D }, { "DIGIMON V3", 4, 4, 0x3E, 0x3D },
};
static const V1Care *care_find(void)
{
for (u32 i = 0; i < sizeof CARE / sizeof CARE[0]; i++) {
int k = 0; while (k < 11 && CARE[i].nm[k] && CARE[i].nm[k] == g_cfg.name[k]) k++;
if ((k == 11 || !CARE[i].nm[k]) && (k >= 15 || !g_cfg.name[k])) return &CARE[i];
}
return 0;
}
static u8 g_cared;
#endif
static void menu_items(void)
{
g_nitems = 0;
for (int k = 0; k < M_N; k++) {
if (k == M_GHOST && !g_set.icons) continue;
if (k == M_DIMTO && g_set.dim >= T_N) continue;
#ifndef BL_PWM
if (k == M_LIGHT) continue;
#endif
if (k == M_SYNC && !(g_cfg.flags & 8u)) continue; /* only where V1Emu knows the toy's clock */
#ifdef V1_CARE
if (k == M_CARE && !care_find()) continue;
#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]; /* read once per drawing of the menu */
#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;
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;
case M_ICO: *a = e ? "ICONS" : "SYMBOLE"; v = g_set.icons ? on : off; break;
case M_GHOST: *a = e ? "ICONS UNLIT" : "SYMBOLE AUS"; if (g_set.ghost) pct(b, g_set.ghost); else v = e ? "HIDDEN" : "UNSICHTB."; break;
#ifdef V1_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_DIM: *a = e ? "DIM AFTER" : "DIMMEN NACH"; v = T_NM[e][g_set.dim <= T_N ? g_set.dim : T_N]; break;
#ifdef BL_PWM
case M_LIGHT: *a = e ? "LIGHT" : "LICHT"; pct(b, LV_PCT[(g_set.r0 & 15u) % LV_N]); break;
case M_DIMTO: *a = e ? "DIM TO" : "DIMMEN AUF"; pct(b, DIM2_PCT[g_set.dimto % DIM2_N]); break;
#else
case M_DIMTO: *a = e ? "DIM TO" : "DIMMEN AUF"; pct(b, DIM_PCT[g_set.dimto & 3u]); break;
#endif
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;
case M_SYNC: *a = e ? "CLOCK" : "UHR"; v = g_set.sync ? "AUTO" : (e ? "GAME" : "SPIEL"); break;
#ifdef V1_CARE
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; /* more above */
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; /* more below */
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 u32 g_tap_until, g_enc;
static int g_set_dirty;
static void menu_close(u32 now, int tap)
{
(void)now; (void)tap; g_menu = 0;
#ifdef V1_CARE
g_cared = 0;
#endif
if (g_set_dirty) { set_store(); g_set_dirty = 0; }
draw(1);
}
static void menu_key(u32 k, u32 now)
{
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;
case M_GRID: g_set.grid ^= 1u; break;
case M_SHD: g_set.shadow ^= 1u; break;
case M_ICO: g_set.icons ^= 1u; break;
case M_GHOST: g_set.ghost = (u8)(g_set.ghost >= 20u ? 0u : g_set.ghost + 1u); break; /* 1% steps up to 20% */
#ifdef V1_VOL
case M_SND: g_set.sound = (u8)((g_set.sound + 1u) & 3u); g_sound = g_set.sound; g_hw_vol = VOL_OF[g_set.sound & 3u]; snd_apply(); break;
#else
case M_SND: g_set.sound ^= 1u; g_sound = g_set.sound; snd_apply(); break;
#endif
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;
#ifdef BL_PWM
case M_LIGHT: g_set.r0 = (u8)(0x80u | (((g_set.r0 & 15u) + 1u) % LV_N)); colours(); break;
case M_DIMTO: g_set.dimto = (u8)((g_set.dimto + 1u) % DIM2_N); break;
#else
case M_DIMTO: g_set.dimto = (u8)((g_set.dimto + 1u) & 3u); break;
#endif
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;
case M_SYNC: g_set.sync ^= 1u; break;
#ifdef V1_CARE
case M_CARE: { const V1Care *c = care_find(); if (c) { if (cpu_v1_peek(c->hun) < c->hv) cpu_v1_poke(c->hun, c->hv); if (cpu_v1_peek(c->hap) < c->av) cpu_v1_poke(c->hap, c->av); g_cared = 1; } } menu_draw(); return;
#endif
default: g_set.lang = (u8)(en() ? 1u : 2u); break;
}
g_set_dirty = 1; colours(); menu_items();
}
if (k & (BTN_C | BTN_WHEEL)) { menu_close(now, 0); return; }
menu_draw();
}
/* ---- program and session ---- */
static u12_t g_prog[8192];
static u8 g_sec[0x1000] __attribute__((aligned(4)));
static u32 crc_rom(void) { u32 h = 2166136261u; for (u32 i = 0; i < 8192u; i++) h = (h ^ g_prog[i]) * 16777619u; return h; }
#ifdef SESS_FLAG
#define NOSESS ((g_cfg.flags & 0x1000u) != 0u) /* 1.7.3 test7: flags bit 12, no session at all */
#else
#define NOSESS 0
#endif
static void drop_session(void);
static void save_session(void)
{
if (NOSESS) { hw_tone(0); g_tone_now = 0; return; } /* 1.7.3 test7: run state never written */
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_vt;
u32 n = cpu_v1_save(g_sec + 16); w[3] = n;
hw_tone(0); g_tone_now = 0;
flash_update4k(SESSION, g_sec);
}
static int load_session(void)
{
if (NOSESS) { drop_session(); return 0; } /* 1.7.3 test7: an old session is dropped, the toy starts fresh */
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] == 0 || w[3] > sizeof g_sec - 16) return 0;
g_vt = w[2];
cpu_v1_load(g_sec + 16);
return 1;
}
#ifdef SESS_ONCE
static u32 g_sess_ms;
#endif
static void drop_session(void) { if (!flash_isff(SESSION, 0x1000u)) flash_erase4k(SESSION); }
static void leave(int save)
{
#ifdef V1_PW
pw_full();
#endif
#ifdef CLK_ECO
hw_clk_shift(0, CORE_HZ);
#endif
hw_tone(0);
set_store();
REG(0x20000FF8u) = 0; /* start counter of the home hooks */
REG(0x20000FFCu) = 0x4C584D50u; /* "PMXL": back on purpose, the autostart stays off */
if (save) save_session();
#ifdef PARA_BACK
para_back_fix(g_sec); /* 1.6.8: the Paradise skips the time spent here */
#endif
hw_bl_pwm_off();
for (u32 t = 0; hw_buttons() && t < 20000000u; t++) if ((t & 0xFFFFu) == 0) hw_wdt_feed();
hw_back_to_paradise();
}
/* ---- the toy's clock (1.6.8 test26) ----
* Tamagotchi P1, P2, Angel and Morino keep the time at RAM nibble 0x10: seconds (BCD, ones then tens),
* minutes (BCD), hours (binary, 0..23). The Studio sets flags bit 3 for these ROMs. Bit 1: take the
* Paradise's time (per program, tab Emulator). The time away is not played through. On a fresh start
* the clock first has to be set in the game (Angel: or it never comes down); V1Emu does that with the
* toy's own keys (B, B, A, C) and then writes the time. */
static u32 g_para_time, g_para_cnt;
static int clock_on(void) { return (g_cfg.flags & 10u) == 10u && g_set.sync && g_para_time; }
/* where the toy keeps its clock: the Tamagotchis at 0x10 (hours binary 0..23), Digimon V1/V2 at 0x1A (1.6.8 test36:
* hours 0..11 at 0x1E, 0x1F = PM; 0x46 was a second counter of the game, not the clock). Both stand until the
* time was set once in the game. */
#define DIGI ((g_cfg.flags & 64u) != 0u)
#define CB (DIGI ? 0x1Au : 0x10u)
#define NEEDSET 1
static void clock_put(u32 d)
{
u32 h = d / 3600u, m = d / 60u % 60u, sec = d % 60u;
cpu_v1_poke(CB, (u4_t)(sec % 10u)); cpu_v1_poke(CB + 1, (u4_t)(sec / 10u));
cpu_v1_poke(CB + 2, (u4_t)(m % 10u)); cpu_v1_poke(CB + 3, (u4_t)(m / 10u));
if (DIGI) { cpu_v1_poke(CB + 4, (u4_t)(h % 12u ? h % 12u : 12u)); cpu_v1_poke(CB + 5, (u4_t)(h >= 12u)); } /* 1..12, 0 = never set */
else { cpu_v1_poke(CB + 4, (u4_t)(h & 15u)); cpu_v1_poke(CB + 5, (u4_t)(h >> 4)); }
}
static void clock_write(void) { clock_put((g_para_time + (REG(0x45000304u) - g_para_cnt)) % 86400u); }
static u32 clock_sec(void) { return cpu_v1_peek(CB) | (u32)cpu_v1_peek(CB + 1) << 4; }
#ifdef V1_CLK_DISC
/* 1.7.2 test16: the toy's clock follows the Paradise's seconds counter 0x45000304 (crystal), as in T7Emu: about every
* 10 s, when the emulation is not behind on purpose. With the clock from the Paradise the toy shows the Paradise's
* time, else its own time from the first check on (time dropped when far behind, or a slow ms tick in standby, made
* it fall behind). A jump (set in the game) is a new reference; a clock that stands (never set) is left alone. */
static u32 g_cd[4]; /* reference: time of day, counter; last check: time of day, counter (counter 0 = none) */
static u32 clock_now(void)
{
u32 s = clock_sec(), m = cpu_v1_peek(CB + 2) + 10u * cpu_v1_peek(CB + 3), h;
if (DIGI) { h = cpu_v1_peek(CB + 4); if (h < 1u || h > 12u) return 0xFFFFFFFFu; h = h % 12u + (cpu_v1_peek(CB + 5) ? 12u : 0u); }
else h = cpu_v1_peek(CB + 4) | (u32)cpu_v1_peek(CB + 5) << 4;
if ((s & 15u) > 9u || s > 0x59u || m > 59u || h > 23u) return 0xFFFFFFFFu;
return h * 3600u + m * 60u + (s >> 4) * 10u + (s & 15u);
}
static s32 tod_diff(u32 a, u32 b) { s32 d = (s32)((a + 86400u - b) % 86400u); return d > 43200 ? d - 86400 : d; }
static void clock_disc(s32 behind)
{
u32 r = REG(0x45000304u);
if (g_cd[1] && r - g_cd[3] < 10u) return;
if (behind > 300 || (REG(0x45000200u) & 7u) || !(g_cfg.flags & 8u)) return;
u32 obs = clock_now();
if (obs == 0xFFFFFFFFu) { g_cd[1] = 0; return; }
if (!g_cd[1]) {
if (clock_on()) { g_cd[0] = (g_para_time + (r - g_para_cnt)) % 86400u; g_cd[1] = r; } else { g_cd[0] = obs; g_cd[1] = r; }
g_cd[2] = obs; g_cd[3] = r; return;
}
s32 el = (s32)(r - g_cd[3]), mv = tod_diff(obs, g_cd[2]);
if (mv == 0 && el >= 5) { g_cd[1] = 0; return; } /* the clock stands */
if (mv < -5 || mv > el + 5) { g_cd[0] = obs; g_cd[1] = r; g_cd[2] = obs; g_cd[3] = r; return; } /* set in the game */
u32 exp = (g_cd[0] + (r - g_cd[1])) % 86400u; s32 d = tod_diff(exp, obs);
if (d >= 2 || d <= -2) { clock_put(exp); obs = exp; }
g_cd[2] = obs; g_cd[3] = r;
}
#endif
#ifdef V1_CLK_CHECK
/* test3: does the toy show the Paradise's time (hours and minutes, one minute of slack)? A resumed P2 kept its old
* time (Bernd); now the time is checked after the start and written again if it is off, three times at most. */
static int clock_ok(void)
{
u32 t = g_para_time + (REG(0x45000304u) - g_para_cnt), want = t % 86400u / 60u, h;
u32 m = cpu_v1_peek(CB + 2) + 10u * cpu_v1_peek(CB + 3);
if (DIGI) h = cpu_v1_peek(CB + 4) % 12u + (cpu_v1_peek(CB + 5) ? 12u : 0u);
else h = cpu_v1_peek(CB + 4) | (u32)cpu_v1_peek(CB + 5) << 4;
u32 diff = (h * 60u + m + 1440u - want) % 1440u;
return diff <= 1u || diff >= 1439u;
}
#endif
/* keys the program presses itself (Tamagotchis): B, B, B, A, C (wake, clock, clock setting, one hour so the clock
* starts, set), 3 s after the start; then the time is written and checked (a second try if the clock stands) */
#define NSET (DIGI ? 2 : 5)
static const u32 SETKEYS_T[5] = { BTN_B, BTN_B, BTN_B, BTN_A, BTN_C };
static const u32 SETKEYS_D[2] = { BTN_B, BTN_C }; /* Digimon: B shows the clock setting, C takes it (then the clock runs) */
#define SETKEYS (DIGI ? SETKEYS_D : SETKEYS_T)
static int g_auto_k = -1; static u32 g_auto_at, g_auto_b, g_auto_until;
static void keys(u32 b)
{
static u32 last = 0xFFFFFFFFu;
if (b == last) return;
last = b;
thw_set_button(BTN_LEFT, (b & BTN_A) ? BTN_STATE_PRESSED : BTN_STATE_RELEASED);
thw_set_button(BTN_MIDDLE, (b & BTN_B) ? BTN_STATE_PRESSED : BTN_STATE_RELEASED);
thw_set_button(BTN_RIGHT, (b & BTN_C) ? BTN_STATE_PRESSED : BTN_STATE_RELEASED);
thw_set_button(BTN_TAP, (b & 0x100u) ? BTN_STATE_PRESSED : BTN_STATE_RELEASED);
}
extern u32 __bss_start, __bss_end;
void Reset_Handler(void)
{
__asm__ volatile("cpsid i");
/* the Paradise's clock, before its SRAM is overwritten; started from the menu: its mailbox */
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); /* 1.6.8: the Paradise does not live meanwhile */
#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++; }
g_para_time = para; g_para_cnt = cnt;
hw_init();
hw_tone(0);
#if defined(PARA_FLASH) && defined(PARA_FLASH_LATE)
/* test24 (as V3Emu): 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
/* the ROM: 12-bit words, two bytes each, high byte first */
{ const volatile u8 *r = XIP_PTR(g_cfg.rom_off); u32 n = g_cfg.rom_len / 2u; if (n > 8192u) n = 8192u;
for (u32 i = 0; i < n; i++) g_prog[i] = (u12_t)(((u32)r[2 * i] << 8 | r[2 * i + 1]) & 0xFFFu); }
tamalib_register_hal(&g_hal_v1);
{ extern int thw_digi, cpu_kmask_fix; thw_digi = DIGI; cpu_kmask_fix = DIGI; }
#ifdef V1_DIRECT
{ extern int thw_direct; thw_direct = (g_cfg.flags & 128u) != 0u; }
#ifdef V1_ICONSEG
{ extern int thw_iconseg; thw_iconseg = (g_cfg.flags & 256u) != 0u; } /* test28: Umino, Genjintch, Tamaotch (Studio flag) */
#endif /* test31: Yasashii, segment = column (Studio flag) */
#endif /* test46: Digimon takes its keys by interrupt */ /* 1.6.8: Digimon's own LCD wiring */
tamalib_init(g_prog, 0, 1000000u);
int restored = load_session();
set_load(); g_sound = g_set.sound;
#ifdef V1_VOL
g_hw_vol = VOL_OF[g_set.sound & 3u];
#endif
u32 clk_chk = 0, clk_s0 = 0xFFu, clk_try = 0, clk_fix = 0;
if (clock_on()) {
if (restored) { clock_write(); clk_chk = 2500u; }
else if (NEEDSET) { g_auto_k = 0; g_auto_at = 3000u; }
}
draw(1);
#ifdef V1_LINK
if (DIGI) plink_init();
#endif
SYST_RVR = CORE_HZ / 1000u - 1u; SYST_CVR = 0; SYST_CSR = 7;
__asm__ volatile("cpsie i");
g_enc = REG(0x4001A000u) & 3u;
u32 emu_ms = g_ms, last_draw = g_ms, t_start = g_ms, wheel_ms = 0, last_in = g_ms, mk0 = 0;
int bl = 1, lock = 1;
#ifdef V1_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
#ifdef V1_CALL_WAKE
int call_wake = 0;
#endif
#ifdef SESS_ONCE
g_sess_ms = g_ms;
#endif
for (;;) {
hw_wdt_feed();
u32 now = g_ms;
#ifdef BAT_GUARD
/* test24: battery empty (as the other emulators): settings and run state saved, the picture, then off until new batteries */
{ static u32 bt; if (now - bt >= 1000u) { bt = now; if (bat_guard_tick()) {
#ifdef V1_PW
pw_full();
#endif
#ifdef CLK_ECO
hw_clk_shift(0, CORE_HZ);
#endif
hw_tone(0); set_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; } /* wait until the keys of the start are released */
#ifdef V1_PW
int pw_quiet = !bl && !b && !g_tone_now && !g_menu && !wheel_ms && !pw_hold && g_auto_k < 0 && !clk_chk
#ifdef V1_TONE_Q
&& tq_h == tq_w && !g_hw_tone_hz
#endif
#ifdef V1_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);
if (r != g_pw_rtc) {
if (g_pw_kw) { u32 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");
now = g_ms; g_pw_kw = 0; }
g_pw_rtc = r; g_pw_rtcms = now; } }
#endif
#ifdef CLK_ECO
#ifdef V1_PW
if (!g_pw_low) {
#endif
/* test22: screen off, no key, no tone, no clock setting, no link traffic for 30 s: a lower clock (eco.h) */
#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_hw_tone_hz && !g_menu && g_auto_k < 0 && !clk_chk
#ifdef V1_LINK
&& !(g_pl.last_rx && now - g_pl.last_rx < 30000u)
#endif
, (int)(now - emu_ms), CORE_HZ);
#ifdef V1_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(1); 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, now); else { g_menu = 1;
#ifdef BAT_SHOW
g_msel = 1; /* the battery line is only shown */
#else
g_msel = 0;
#endif
g_mtop = 0; menu_items(); menu_draw(); } } wheel_ms = 0; }
/* turning the wheel shakes the toy (vibration sensor of the Angel and others, K03): any step of the encoder */
{ u32 n = REG(0x4001A000u) & 3u; if (n != g_enc) { g_enc = n; if (!g_menu) g_tap_until = now + 120u; last_in = now; } }
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 V1_PW
pw_full();
#endif
if (g_menu) menu_draw(); else draw(1);
hw_backlight(1);
#else
bl = 1; hw_backlight(1); if (g_dimmed) { g_dimmed = 0; colours(); }
#ifdef V1_PW
pw_full();
#endif
if (g_menu) menu_draw(); else draw(1);
#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;
}
}
/* clock setting by the program itself on a fresh start (one key every 400 ms, held 150 ms) */
if (g_auto_k >= 0 && now - t_start >= g_auto_at) {
/* Digimon reads its keys only twice a second: held 600 ms, 1.2 s apart; a valid time (hour 1..12) before B and C */
if (g_auto_k == 0 && DIGI) clock_write();
if (g_auto_k < NSET) { g_auto_b = SETKEYS[g_auto_k++]; g_auto_until = now + (DIGI ? 600u : 150u); g_auto_at = now - t_start + (DIGI ? 1200u : 400u); }
else { clock_write(); g_auto_k = -1; clk_chk = now - t_start + 2500u; clk_s0 = 0xFFu; }
}
/* does the toy's clock run? (a clock never set in the game stands still) If not: set it with the toy's keys, twice at most */
if (clk_chk) {
if (clk_s0 == 0xFFu && now - t_start >= clk_chk - 2000u) clk_s0 = clock_sec();
if (now - t_start >= clk_chk) { clk_chk = 0; if (clock_sec() == clk_s0 && NEEDSET && clk_try++ < 2) { g_auto_k = 0; g_auto_at = now - t_start + 100u; }
#ifdef V1_CLK_CHECK
else if (!clock_ok() && clk_fix++ < 3u) { clock_write(); clk_chk = now - t_start + 2500u; clk_s0 = 0xFFu; }
#endif
}
}
u32 ab = (g_auto_b && (s32)(g_auto_until - now) > 0) ? g_auto_b : 0u;
if (g_menu) { /* the toy runs on, its keys go to the menu */
u32 k = b & (BTN_A | BTN_B | BTN_C), nk = k & ~mk0; mk0 = k;
if (nk) menu_key(nk, now);
keys(ab);
} else { mk0 = b & (BTN_A | BTN_B | BTN_C); keys((b & (BTN_A | BTN_B | BTN_C)) | ab | ((s32)(g_tap_until - now) > 0 ? 0x100u : 0u)); }
#ifdef V1_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;
* a tone while dark: real time from there (pw_hold), so it sounds as it should */
s32 lim = pw_back ? 5000 : 50;
#ifdef V1_CLK_DISC
if (g_auto_k < 0 && !clk_chk) clock_disc((s32)(now - emu_ms));
#endif
if ((s32)(now - emu_ms) > lim) 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
/* run the toy up to now (at most 50 ms behind) */
if ((s32)(now - emu_ms) > 50) emu_ms = now - 50u;
u32 t0 = g_ms;
while ((s32)(now - emu_ms) > 0 && g_ms - t0 < 20u) {
#endif
u32 target = g_vt + 1000u;
#ifdef V1_STALL
/* 1.7.3 test21: the old fallback (+31 us whenever a step did not move the time) put the pacing time ahead of the
* CPU's own time; in OSC3 (1 MHz, Umino) an instruction takes 5..12 us, so from then on every step added 31 us
* and the toy got only about half its clock interrupts (its clock ran 2 to 3 times slow). Now only after
* several steps in a row without time. */
{ u32 st = 0; while ((s32)(target - g_vt) > 0) { u32 v0 = g_vt; tamalib_step(); if (g_vt != v0) st = 0; else if (++st > 8u) { g_vt += 31u; st = 0; } } }
#else
while ((s32)(target - g_vt) > 0) { u32 v0 = g_vt; tamalib_step(); if (g_vt == v0) g_vt += 31u; }
#endif
emu_ms++;
#ifdef CLK_ECO
ECO_CHECK(g_ms, g_ms - emu_ms, CORE_HZ);
#endif
#ifdef V1_LINK
plink_poll(g_ms, 0, lk_got);
#endif
#ifdef V1_CALL_WAKE
if (g_tone_now && !bl) call_wake = 1; /* test24: the toy calls while dark: the screen comes on (as the other emulators) */
#endif
#ifdef V1_PW
if (g_tone_now && !bl && !pw_hold && (s32)(now - emu_ms) > 1) { pw_hold = now - emu_ms; pw_full(); break; }
#endif
}
if (g_tone_now && now - g_tone_since > 3000u) { g_tone_cut = 1; snd_apply(); } /* a stuck tone stops */
#ifdef V1_TONE_Q
tq_pump();
#endif
#ifdef V1_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 V1_PW
pw_full();
#endif
draw(1); hw_backlight(1); } }
#else
if (!bl) { bl = 1; last_in = now; hw_backlight(1); if (g_dimmed) { g_dimmed = 0; colours(); }
#ifdef V1_PW
pw_full();
#endif
draw(1); } }
#endif
#endif
#ifdef V1_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
/* 1.6.8: draw a picture only once the toy has finished it (no change for 8 ms of toy time, at the latest after
* 150 ms): drawn in the middle of an update, the clock looked broken and icons flickered */
{ static u32 dirty_ms; if (!g_dirty) dirty_ms = 0; else if (!dirty_ms) dirty_ms = now ? now : 1;
if (bl && !g_menu && now - last_draw >= 33u && (g_vt - g_lcd_vt >= 8000u || (dirty_ms && now - dirty_ms > 150u))) { last_draw = now; draw(0); } }
/* dim, then screen off, without a key */
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(1); }
if (bl && g_set.off < T_N && now - last_in > T_MS[g_set.off]) { bl = 0; hw_backlight(0);
#ifdef SESS_ONCE
if (g_auto_k < 0 && !clk_chk) { save_session(); g_sess_ms = now; }
#endif
}
#ifdef SESS_ONCE
/* 1.6.8: the toy has no save of its own; the session is written every 15 minutes (quiet for 10 s) and when the
* screen goes off, so a battery change takes back at most that much */
if (now - g_sess_ms > 900000u && now - last_in > 10000u && g_auto_k < 0 && !clk_chk && !g_menu) { save_session(); g_sess_ms = now; }
#endif
#ifdef V1_PW
if (emu_ms == g_ms) {
if (pw_quiet && g_pw_low && !hw_buttons()) {
/* 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 V1_LINK
, [16 + 37] = (void *)plink_irq
#endif
#ifdef V1_PW
, [16 + 2] = (void *)pw_rtc_irq, [16 + 26] = (void *)pw_gpio_irq
#endif
};
+339
View File
@@ -0,0 +1,339 @@
GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it. (Some other Free Software Foundation software is covered by
the GNU Lesser General Public License instead.) You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must show them these terms so they know their
rights.
We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.
Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.
Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary. To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.
The precise terms and conditions for copying, distribution and
modification follow.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License. The "Program", below,
refers to any such program or work, and a "work based on the Program"
means either the Program or any derivative work under copyright law:
that is to say, a work containing the Program or a portion of it,
either verbatim or with modifications and/or translated into another
language. (Hereinafter, translation is included without limitation in
the term "modification".) Each licensee is addressed as "you".
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the
Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.
1. You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License
along with the Program.
You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a fee.
2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.
b) You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any
part thereof, to be licensed as a whole at no charge to all third
parties under the terms of this License.
c) If the modified program normally reads commands interactively
when run, you must cause it, when started running for such
interactive use in the most ordinary way, to print or display an
announcement including an appropriate copyright notice and a
notice that there is no warranty (or else, saying that you provide
a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this
License. (Exception: if the Program itself is interactive but
does not normally print such an announcement, your work based on
the Program is not required to print an announcement.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.
In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:
a) Accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of Sections
1 and 2 above on a medium customarily used for software interchange; or,
b) Accompany it with a written offer, valid for at least three
years, to give any third party, for a charge no more than your
cost of physically performing source distribution, a complete
machine-readable copy of the corresponding source code, to be
distributed under the terms of Sections 1 and 2 above on a medium
customarily used for software interchange; or,
c) Accompany it with the information you received as to the offer
to distribute corresponding source code. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form with such
an offer, in accord with Subsection b above.)
The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable. However, as a
special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.
If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.
4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License. Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this License.
However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such
parties remain in full compliance.
5. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Program or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.
7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Program at all. For example, if a patent
license would not permit royalty-free redistribution of the Program by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.
If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply and the section as a whole is intended to apply in other
circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program under this License
may add an explicit geographical distribution limitation excluding
those countries, so that distribution is permitted only in or among
countries not thus excluded. In such case, this License incorporates
the limitation as if written in the body of this License.
9. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the Program
specifies a version number of this License which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation. If the Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.
10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission. For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this. Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.
NO WARRANTY
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Also add information on how to contact you by electronic and paper mail.
If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:
Gnomovision version 69, Copyright (C) year name of author
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, the commands you use may
be called something other than `show w' and `show c'; they could even be
mouse-clicks or menu items--whatever suits your program.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
`Gnomovision' (which makes passes at compilers) written by James Hacker.
<signature of Ty Coon>, 1 April 1989
Ty Coon, President of Vice
This General Public License does not permit incorporating your program into
proprietary programs. If your program is a subroutine library, you may
consider it more useful to permit linking proprietary applications with the
library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License.
+65
View File
@@ -0,0 +1,65 @@
# TamaLIB - A hardware agnostic first-gen Tamagotchi emulation library
## Synopsis
TamaLib is a hardware agnostic first-gen Tamagotchi (E0C6S46/8 based) emulation library built from scratch. It is self-contained and aims at running on any platform powerful enough, from microcontrollers (MCUs) to desktop computers, thus spreading virtual life across the digital world.
So far, it has been successfully implemented on different platforms:
- Desktop computers (check out [TamaTool](https://github.com/jcrona/tamatool/) for more information)
- STM32F072 MCU based board (check out [MCUGotchi](https://github.com/jcrona/mcugotchi/) for more information).
- OpenTama which is an STM32L072 MCU based board (check out [OpenTama](https://github.com/Sparkr-tech/opentama) and [MCUGotchi](https://github.com/jcrona/mcugotchi/) for more information).
- Arduino UNO (check out [ArduinoGotchi](https://github.com/GaryZ88/ArduinoGotchi/) for more information).
- A web app powered by Python/JS (check out [Pygotchi](https://github.com/Almarch/pygotchi) for more information).
- Palm OS (check out [TamagotchiPalmOS](https://github.com/JOBBIN9422/TamagotchiPalmOS) for more information).
## Importing TamaLIB
TamaLIB cannot be used as is. In order to create life on a specific target, you need to import all TamaLIB related __.c__ and __.h__ files in your project (for instance in a __lib__ subfolder), to create a __hal_types.h__ file using the template provided and to implement the __hal_t__ structure, that will act as an abstraction layer between TamaLIB and your OS or SDK (detailed information can be found in __hal.h__). This abstraction layer basically connects TamaLIB to your target's buttons, clock, audio and screen, while also defining the C types that TamaLIB should use to represent 4-bit, 5-bit, 8-bit, 12-bit, 13-bit and 32-bit variables. Once done, you will be able to call the TamaLIB API from your project.
## Using the TamaLIB API
Basically:
```
/* ... */
/* Register the HAL */
tamalib_register_hal(&my_hal);
/* ... */
/* Initialize TamaLIB */
tamalib_init(my_program, my_breakpoints, 1000000); // my_breakpoints can be NULL, 1000000 means that timestamps will be expressed in us
/* ... */
/* Enter TamaLIB's loop */
tamalib_mainloop();
/* ... */
/* Release TamaLIB */
tamalib_release();
/* ... */
```
Your main project should then forward any button input to TamaLIB using the `tamalib_set_button()` function.
As an alternative to `tamalib_mainloop()`, you can call `tamalib_step()` directly if your execution flow requires something more complex than a simple mainloop. In that case, TamaLIB will neither call the HAL `handler()` function, nor the HAL `update_screen()` function by itslef.
## License
TamaLIB is distributed under the GPLv2 license. See the LICENSE file for more information.
## Hardware information
The Tamagotchi P1 and P2 are based on an [E0C6S46 Epson MCU](https://download.epson-europe.com/pub/electronics-de/asmic/4bit/62family/technicalmanual/tm_6s46.pdf), and runs at 32,768 kHz, while the Tamagotchi Angel, Umino (Ocean), Morino (Forest), Mothra, Tamaotch and Genjintch are based on an [E0C6S48 Epson MCU](https://download.epson-europe.com/pub/electronics-de/asmic/4bit/62family/technicalmanual/tm_6s48.pdf) running at 32,768 kHz and 1 MHz. Their LCDs are all 32x16 B/W pixels, with 8 icons.
To my knowledge, the P1 ROM available online has been extracted from a high-res picture of a die. The ROM mask was clear enough to be optically read. The pictures can be seen [there](https://siliconpr0n.org/map/bandai/tamagotchi-v1/) (thx asterick for the link!).
~~I would love to see the same work done on a P2 and add support for it in TamaLIB/TamaTool!~~ Someone did the same job for the P2 and other first-gen devices!
__
Copyright (C) 2025 Jean-Christophe Rona
+2117
View File
File diff suppressed because it is too large. Load diff
+225
View File
@@ -0,0 +1,225 @@
/*
* TamaLIB - A hardware agnostic first-gen Tamagotchi emulation library
*
* Copyright (C) 2021 Jean-Christophe Rona <jc@rona.fr>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef _CPU_H_
#define _CPU_H_
#include "hal.h"
#define MEMORY_SIZE 4096 // 4096 x 4 bits
#define E0C6S46_SUPPORT
#define E0C6S48_SUPPORT
#if defined(E0C6S48_SUPPORT)
/* E0C6S48 (compatible with E0C6S46) */
#define MEM_RAM_ADDR 0x000
#define MEM_RAM_SIZE 0x300 // 768 x 4 bits of RAM
#define MEM_DISPLAY1_ADDR 0xE00
#define MEM_DISPLAY1_SIZE 0x066 // 102 x 4 bits of RAM
#define MEM_DISPLAY2_ADDR 0xE80
#define MEM_DISPLAY2_SIZE 0x066 // 102 x 4 bits of RAM
#define MEM_IO_ADDR 0xF00
#define MEM_IO_SIZE 0x080
#elif defined(E0C6S46_SUPPORT)
/* E0C6S46 only */
#define MEM_RAM_ADDR 0x000
#define MEM_RAM_SIZE 0x280 // 640 x 4 bits of RAM
#define MEM_DISPLAY1_ADDR 0xE00
#define MEM_DISPLAY1_SIZE 0x050 // 80 x 4 bits of RAM
#define MEM_DISPLAY2_ADDR 0xE80
#define MEM_DISPLAY2_SIZE 0x050 // 80 x 4 bits of RAM
#define MEM_IO_ADDR 0xF00
#define MEM_IO_SIZE 0x080
#else
#error Support for at least one CPU needs to be defined !
#endif
/* Define this if you want to reduce the footprint of the memory buffer from 4096 u4_t (most likely bytes)
* to 464 u8_t (bytes for sure), while increasing slightly the number of operations needed to read/write from/to it.
*/
#define LOW_FOOTPRINT
#ifdef LOW_FOOTPRINT
/* Invalid memory areas are not buffered to reduce the footprint of the library in memory */
#define MEM_BUFFER_SIZE (MEM_RAM_SIZE + MEM_DISPLAY1_SIZE + MEM_DISPLAY2_SIZE + MEM_IO_SIZE)/2
/* Maps the CPU memory to the memory buffer */
#define RAM_TO_MEMORY(n) ((n - MEM_RAM_ADDR)/2)
#define DISP1_TO_MEMORY(n) ((n - MEM_DISPLAY1_ADDR + MEM_RAM_SIZE)/2)
#define DISP2_TO_MEMORY(n) ((n - MEM_DISPLAY2_ADDR + MEM_RAM_SIZE + MEM_DISPLAY1_SIZE)/2)
#define IO_TO_MEMORY(n) ((n - MEM_IO_ADDR + MEM_RAM_SIZE + MEM_DISPLAY1_SIZE + MEM_DISPLAY2_SIZE)/2)
#define SET_RAM_MEMORY(buffer, n, v) {buffer[RAM_TO_MEMORY(n)] = (buffer[RAM_TO_MEMORY(n)] & ~(0xF << (((n) % 2) << 2))) | ((v) & 0xF) << (((n) % 2) << 2);}
#define SET_DISP1_MEMORY(buffer, n, v) {buffer[DISP1_TO_MEMORY(n)] = (buffer[DISP1_TO_MEMORY(n)] & ~(0xF << (((n) % 2) << 2))) | ((v) & 0xF) << (((n) % 2) << 2);}
#define SET_DISP2_MEMORY(buffer, n, v) {buffer[DISP2_TO_MEMORY(n)] = (buffer[DISP2_TO_MEMORY(n)] & ~(0xF << (((n) % 2) << 2))) | ((v) & 0xF) << (((n) % 2) << 2);}
#define SET_IO_MEMORY(buffer, n, v) {buffer[IO_TO_MEMORY(n)] = (buffer[IO_TO_MEMORY(n)] & ~(0xF << (((n) % 2) << 2))) | ((v) & 0xF) << (((n) % 2) << 2);}
#define SET_MEMORY(buffer, n, v) {if ((n) < (MEM_RAM_ADDR + MEM_RAM_SIZE)) { \
SET_RAM_MEMORY(buffer, n, v); \
} else if ((n) < MEM_DISPLAY1_ADDR) { \
/* INVALID_MEMORY */ \
} else if ((n) < (MEM_DISPLAY1_ADDR + MEM_DISPLAY1_SIZE)) { \
SET_DISP1_MEMORY(buffer, n, v); \
} else if ((n) < MEM_DISPLAY2_ADDR) { \
/* INVALID_MEMORY */ \
} else if ((n) < (MEM_DISPLAY2_ADDR + MEM_DISPLAY2_SIZE)) { \
SET_DISP2_MEMORY(buffer, n, v); \
} else if ((n) < MEM_IO_ADDR) { \
/* INVALID_MEMORY */ \
} else if ((n) < (MEM_IO_ADDR + MEM_IO_SIZE)) { \
SET_IO_MEMORY(buffer, n, v); \
} else { \
/* INVALID_MEMORY */ \
}}
#define GET_RAM_MEMORY(buffer, n) ((buffer[RAM_TO_MEMORY(n)] >> (((n) % 2) << 2)) & 0xF)
#define GET_DISP1_MEMORY(buffer, n) ((buffer[DISP1_TO_MEMORY(n)] >> (((n) % 2) << 2)) & 0xF)
#define GET_DISP2_MEMORY(buffer, n) ((buffer[DISP2_TO_MEMORY(n)] >> (((n) % 2) << 2)) & 0xF)
#define GET_IO_MEMORY(buffer, n) ((buffer[IO_TO_MEMORY(n)] >> (((n) % 2) << 2)) & 0xF)
#define GET_MEMORY(buffer, n) ((buffer[ \
((n) < (MEM_RAM_ADDR + MEM_RAM_SIZE)) ? RAM_TO_MEMORY(n) : \
((n) < MEM_DISPLAY1_ADDR) ? 0 : \
((n) < (MEM_DISPLAY1_ADDR + MEM_DISPLAY1_SIZE)) ? DISP1_TO_MEMORY(n) : \
((n) < MEM_DISPLAY2_ADDR) ? 0 : \
((n) < (MEM_DISPLAY2_ADDR + MEM_DISPLAY2_SIZE)) ? DISP2_TO_MEMORY(n) : \
((n) < MEM_IO_ADDR) ? 0 : \
((n) < (MEM_IO_ADDR + MEM_IO_SIZE)) ? IO_TO_MEMORY(n) : 0 \
] >> (((n) % 2) << 2)) & 0xF)
#define MEM_BUFFER_TYPE u8_t
#else
#define MEM_BUFFER_SIZE MEMORY_SIZE
#define SET_MEMORY(buffer, n, v) {buffer[n] = v;}
#define SET_RAM_MEMORY(buffer, n, v) SET_MEMORY(buffer, n, v)
#define SET_DISP1_MEMORY(buffer, n, v) SET_MEMORY(buffer, n, v)
#define SET_DISP2_MEMORY(buffer, n, v) SET_MEMORY(buffer, n, v)
#define SET_IO_MEMORY(buffer, n, v) SET_MEMORY(buffer, n, v)
#define GET_MEMORY(buffer, n) (buffer[n])
#define GET_RAM_MEMORY(buffer, n) GET_MEMORY(buffer, n)
#define GET_DISP1_MEMORY(buffer, n) GET_MEMORY(buffer, n)
#define GET_DISP2_MEMORY(buffer, n) GET_MEMORY(buffer, n)
#define GET_IO_MEMORY(buffer, n) GET_MEMORY(buffer, n)
#define MEM_BUFFER_TYPE u4_t
#endif
typedef struct breakpoint {
u13_t addr;
struct breakpoint *next;
} breakpoint_t;
/* Pins (TODO: add other pins) */
typedef enum {
PIN_K00 = 0x0,
PIN_K01 = 0x1,
PIN_K02 = 0x2,
PIN_K03 = 0x3,
PIN_K10 = 0X4,
PIN_K11 = 0X5,
PIN_K12 = 0X6,
PIN_K13 = 0X7,
} pin_t;
typedef enum {
PIN_STATE_LOW = 0,
PIN_STATE_HIGH = 1,
} pin_state_t;
typedef enum {
INT_PROG_TIMER_SLOT = 0,
INT_SERIAL_SLOT = 1,
INT_K10_K13_SLOT = 2,
INT_K00_K03_SLOT = 3,
INT_STOPWATCH_SLOT = 4,
INT_CLOCK_TIMER_SLOT = 5,
INT_SLOT_NUM,
} int_slot_t;
typedef struct {
u4_t factor_flag_reg;
u4_t mask_reg;
bool_t triggered; /* 1 if triggered, 0 otherwise */
u8_t vector;
} interrupt_t;
typedef struct {
u13_t *pc;
u12_t *x;
u12_t *y;
u4_t *a;
u4_t *b;
u5_t *np;
u8_t *sp;
u4_t *flags;
u32_t *tick_counter;
u32_t *clk_timer_2hz_timestamp;
u32_t *clk_timer_4hz_timestamp;
u32_t *clk_timer_8hz_timestamp;
u32_t *clk_timer_16hz_timestamp;
u32_t *clk_timer_32hz_timestamp;
u32_t *clk_timer_64hz_timestamp;
u32_t *clk_timer_128hz_timestamp;
u32_t *clk_timer_256hz_timestamp;
u32_t *prog_timer_timestamp;
bool_t *prog_timer_enabled;
u8_t *prog_timer_data;
u8_t *prog_timer_rld;
u32_t *call_depth;
interrupt_t *interrupts;
bool_t *cpu_halted;
MEM_BUFFER_TYPE *memory;
} state_t;
void cpu_add_bp(breakpoint_t **list, u13_t addr);
void cpu_free_bp(breakpoint_t **list);
void cpu_set_speed(u8_t speed);
state_t * cpu_get_state(void);
u32_t cpu_get_depth(void);
void cpu_set_input_pin(pin_t pin, pin_state_t state);
void cpu_sync_ref_timestamp(void);
void cpu_refresh_hw(void);
void cpu_reset(void);
bool_t cpu_init(const u12_t *program, breakpoint_t *breakpoints, u32_t freq);
void cpu_release(void);
int cpu_step(void);
/* Paramix: state as bytes (cpu_v1_save returns the length) */
u32_t cpu_v1_save(u8_t *b);
void cpu_v1_load(const u8_t *b);
u4_t cpu_v1_peek(u12_t n);
void cpu_v1_poke(u12_t n, u4_t v);
#endif /* _CPU_H_ */
+90
View File
@@ -0,0 +1,90 @@
/*
* TamaLIB - A hardware agnostic first-gen Tamagotchi emulation library
*
* Copyright (C) 2021 Jean-Christophe Rona <jc@rona.fr>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef _HAL_H_
#define _HAL_H_
#include "hal_types.h"
#ifndef NULL
#define NULL 0
#endif
typedef enum {
LOG_ERROR = 0x1,
LOG_INFO = (0x1 << 1),
LOG_MEMORY = (0x1 << 2),
LOG_CPU = (0x1 << 3),
LOG_INT = (0x1 << 4),
} log_level_t;
/* The Hardware Abstraction Layer
* NOTE: This structure acts as an abstraction layer between TamaLIB and the OS/SDK.
* All pointers MUST be implemented, but some implementations can be left empty.
*/
typedef struct {
/* Memory allocation functions
* NOTE: Needed only if breakpoints support is required.
*/
void * (*malloc)(u32_t size);
void (*free)(void *ptr);
/* What to do if the CPU has halted
*/
void (*halt)(void);
/* Log related function
* NOTE: Needed only if log messages are required.
*/
bool_t (*is_log_enabled)(log_level_t level);
void (*log)(log_level_t level, char *buff, ...);
/* Clock related functions
* NOTE: Timestamps granularity is configured with tamalib_init(), an accuracy
* of ~30 us (1/32768) is required for a cycle accurate emulation.
*/
void (*sleep_until)(timestamp_t ts);
timestamp_t (*get_timestamp)(void);
/* Screen related functions
* NOTE: In case of direct hardware access to pixels, the set_XXXX() functions
* (called for each pixel/icon update) can directly drive them, otherwise they
* should just store the data in a buffer and let update_screen() do the actual
* rendering (at 30 fps).
*/
void (*update_screen)(void);
void (*set_lcd_matrix)(u8_t x, u8_t y, bool_t val);
void (*set_lcd_icon)(u8_t icon, bool_t val);
/* Sound related functions
* NOTE: set_frequency() changes the output frequency of the sound in dHz, while
* play_frequency() decides whether the sound should be heard or not.
*/
void (*set_frequency)(u32_t freq);
void (*play_frequency)(bool_t en);
/* Event handler from the main app (if any)
* NOTE: This function usually handles button related events, states loading/saving ...
*/
int (*handler)(void);
} hal_t;
extern hal_t *g_hal;
#endif /* _HAL_H_ */
+32
View File
@@ -0,0 +1,32 @@
/*
* TamaLIB - A hardware agnostic Tamagotchi P1 emulation library
*
* Copyright (C) 2021 Jean-Christophe Rona <jc@rona.fr>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef _HAL_TYPES_H_
#define _HAL_TYPES_H_
typedef unsigned char bool_t;
typedef unsigned char u4_t;
typedef unsigned char u5_t;
typedef unsigned char u8_t;
typedef unsigned short u12_t;
typedef unsigned short u13_t;
typedef unsigned int u32_t;
typedef unsigned int timestamp_t; // WARNING: Must be an unsigned type to properly handle wrapping (u32 wraps in around 1h11m when expressed in us)
#endif /* _HAL_TYPES_H_ */
+187
View File
@@ -0,0 +1,187 @@
/*
* TamaLIB - A hardware agnostic first-gen Tamagotchi emulation library
*
* Copyright (C) 2021 Jean-Christophe Rona <jc@rona.fr>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "hw.h"
#include "cpu.h"
#include "hal.h"
/* SEG -> LCD mapping */
#if defined(E0C6S48_SUPPORT)
/* 51 segments */
static u8_t seg_pos[MEM_DISPLAY1_SIZE/2] = {0, 1, 2, 3, 4, 5, 6, 7, 32, 8, 9, 10, 11, 12 ,13 ,14, 15, 33, 34, 35, 31, 30, 29, 28, 27, 26, 25, 24, 36, 23, 22, 21, 20, 19, 18, 17, 16, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50};
#elif defined(E0C6S46_SUPPORT)
/* 40 segments */
static u8_t seg_pos[MEM_DISPLAY1_SIZE/2] = {0, 1, 2, 3, 4, 5, 6, 7, 32, 8, 9, 10, 11, 12 ,13 ,14, 15, 33, 34, 35, 31, 30, 29, 28, 27, 26, 25, 24, 36, 23, 22, 21, 20, 19, 18, 17, 16, 37, 38, 39};
#endif
bool_t thw_init(void)
{
/* Buttons/Tap sensor are active LOW */
cpu_set_input_pin(PIN_K00, PIN_STATE_HIGH);
cpu_set_input_pin(PIN_K01, PIN_STATE_HIGH);
cpu_set_input_pin(PIN_K02, PIN_STATE_HIGH);
cpu_set_input_pin(PIN_K03, PIN_STATE_HIGH);
return 0;
}
void thw_release(void)
{
}
/* Paramix 1.6.8 (test41): Digimon V1/V2 drive segments 1..8, 12..27 and 29..36 as the 32 columns (the P1 0..7, 9..16,
* 20..27, 29..36): with the P1 table three columns stayed empty in the middle and three were lost (found on the host by
* counting the segments each ROM writes). thw_digi is set by the program. */
static const u8_t seg_pos_digi[40] = {40, 0, 1, 2, 3, 4, 5, 6, 7, 41, 42, 43, 8, 9, 10, 11, 12, 13, 14, 15, 31, 30, 29, 28, 27, 26, 25, 24, 44, 23, 22, 21, 20, 19, 18, 17, 16, 45, 46, 47};
int thw_digi;
/* test31: Yasashii drives segment n as column n (0..31), found on the PC by reading the segments of its status pages;
* with the P1 table the middle of the screen showed a black block and columns were lost. Set by the program. */
int thw_direct;
/* 1.7.2 test28: Umino, Genjintch and Tamaotch light each menu icon with one segment (Umino, Tamaotch: one com; Genjintch:
* coms 0-7 or 8-15), not with the P1's combinations: top 8, 17, 18, 19 (com 0-7) = icons 0-3, bottom 39, 38, 37, 28
* (com 8-15) = icons 4-7 (found on the PC, pressing A through the menu). With the P1 table the icons stayed dark or
* all four lit at once. Set by the program. */
int thw_iconseg;
void thw_set_lcd_pin(u8_t seg, u8_t com, u8_t val)
{
if (thw_direct) { if (seg < LCD_WIDTH) g_hal->set_lcd_matrix(seg, com, val); return; }
if (thw_iconseg && seg < MEM_DISPLAY1_SIZE/2 && seg_pos[seg] >= LCD_WIDTH) {
static unsigned short icm[8];
int i = com < 8 ? (seg == 8 ? 0 : seg == 17 ? 1 : seg == 18 ? 2 : seg == 19 ? 3 : -1)
: (seg == 39 ? 4 : seg == 38 ? 5 : seg == 37 ? 6 : seg == 28 ? 7 : -1);
if (i >= 0 && com < 16) { if (val) icm[i] |= (unsigned short)(1u << com); else icm[i] &= (unsigned short)~(1u << com); g_hal->set_lcd_icon((u8_t)i, icm[i] != 0); }
return;
}
if (thw_digi) {
if (seg < 40 && seg_pos_digi[seg] < LCD_WIDTH) g_hal->set_lcd_matrix(seg_pos_digi[seg], com, val);
/* test46: Digimon's icons (found on the host): status, food, training, battle = seg 28, 37, 38, 39 com 15;
* clean, light, heal = seg 11, 10, 9 com 0; call = seg 0 com 0 */
else if (com == 15 && (seg == 28 || (seg >= 37 && seg <= 39))) g_hal->set_lcd_icon(seg == 28 ? 0 : seg - 36, val);
else if (com == 0 && seg >= 9 && seg <= 11) g_hal->set_lcd_icon(15 - seg, val);
else if (com == 0 && seg == 0) g_hal->set_lcd_icon(7, val);
return;
}
//printf(" thw_set_lcd_pin: seg = %u, com = %u, val = %u\n", seg, com, val);
if (seg_pos[seg] < LCD_WIDTH) {
g_hal->set_lcd_matrix(seg_pos[seg], com, val);
} else {
/*
* IC n -> seg-com|...
* IC 0 -> 8-0 |18-3 |19-2
* IC 1 -> 8-1 |17-0 |19-3
* IC 2 -> 8-2 |17-1 |37-12|38-13|39-14
* IC 3 -> 8-3 |17-2 |18-1 |19-0
* IC 4 -> 28-12|37-13|38-14|39-15
* IC 5 -> 28-13|37-14|38-15
* IC 6 -> 28-14|37-15|39-12
* IC 7 -> 28-15|38-12|39-13
*/
if (seg == 8 && com < 4) {
g_hal->set_lcd_icon(com, val);
} else if (seg == 28 && com >= 12) {
g_hal->set_lcd_icon(com - 8, val);
}
}
}
void thw_set_button(button_t btn, btn_state_t state)
{
pin_state_t pin_state = (state == BTN_STATE_PRESSED) ? PIN_STATE_LOW : PIN_STATE_HIGH;
switch (btn) {
case BTN_TAP:
cpu_set_input_pin(PIN_K03, pin_state);
break;
case BTN_LEFT:
cpu_set_input_pin(PIN_K02, pin_state);
break;
case BTN_MIDDLE:
cpu_set_input_pin(PIN_K01, pin_state);
break;
case BTN_RIGHT:
cpu_set_input_pin(PIN_K00, pin_state);
break;
}
}
void thw_set_buzzer_freq(u4_t freq)
{
u32_t snd_freq = 0;
switch (freq) {
case 0:
/* 4096.0 Hz */
snd_freq = 40960;
break;
case 1:
/* 3276.8 Hz */
snd_freq = 32768;
break;
case 2:
/* 2730.7 Hz */
snd_freq = 27307;
break;
case 3:
/* 2340.6 Hz */
snd_freq = 23406;
break;
case 4:
/* 2048.0 Hz */
snd_freq = 20480;
break;
case 5:
/* 1638.4 Hz */
snd_freq = 16384;
break;
case 6:
/* 1365.3 Hz */
snd_freq = 13653;
break;
case 7:
/* 1170.3 Hz */
snd_freq = 11703;
break;
}
if (snd_freq != 0) {
g_hal->set_frequency(snd_freq);
}
}
static bool_t bz_out, bz_shot_on; /* Paramix 1.6.8 (test46): the normal output and the one-shot buzzer */
void thw_enable_buzzer(bool_t en)
{
bz_out = en;
g_hal->play_frequency(bz_out || bz_shot_on);
}
void thw_buzzer_shot(bool_t on)
{
bz_shot_on = on;
g_hal->play_frequency(bz_out || bz_shot_on);
}
+53
View File
@@ -0,0 +1,53 @@
/*
* TamaLIB - A hardware agnostic first-gen Tamagotchi emulation library
*
* Copyright (C) 2021 Jean-Christophe Rona <jc@rona.fr>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef _HW_H_
#define _HW_H_
#include "hal.h"
#define LCD_WIDTH 32
#define LCD_HEIGHT 16
#define ICON_NUM 8
typedef enum {
BTN_STATE_RELEASED = 0,
BTN_STATE_PRESSED,
} btn_state_t;
typedef enum {
BTN_LEFT = 0,
BTN_MIDDLE,
BTN_RIGHT,
BTN_TAP,
} button_t;
bool_t thw_init(void);
void thw_release(void);
void thw_set_lcd_pin(u8_t seg, u8_t com, u8_t val);
void thw_set_button(button_t btn, btn_state_t state);
void thw_set_buzzer_freq(u4_t freq);
void thw_enable_buzzer(bool_t en);
void thw_buzzer_shot(bool_t on);
#endif /* _HW_H_ */
+137
View File
@@ -0,0 +1,137 @@
/*
* TamaLIB - A hardware agnostic first-gen Tamagotchi emulation library
*
* Copyright (C) 2021 Jean-Christophe Rona <jc@rona.fr>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "tamalib.h"
#include "hw.h"
#include "cpu.h"
#include "hal.h"
#define DEFAULT_FRAMERATE 30 // fps
static exec_mode_t exec_mode = EXEC_MODE_RUN;
static u32_t step_depth = 0;
static timestamp_t screen_ts = 0;
static u32_t ts_freq;
static u8_t g_framerate = DEFAULT_FRAMERATE;
hal_t *g_hal;
bool_t tamalib_init(const u12_t *program, breakpoint_t *breakpoints, u32_t freq)
{
bool_t res = 0;
res |= cpu_init(program, breakpoints, freq);
res |= thw_init();
ts_freq = freq;
return res;
}
void tamalib_release(void)
{
thw_release();
cpu_release();
}
void tamalib_set_framerate(u8_t framerate)
{
g_framerate = framerate;
}
u8_t tamalib_get_framerate(void)
{
return g_framerate;
}
void tamalib_register_hal(hal_t *hal)
{
g_hal = hal;
}
void tamalib_set_exec_mode(exec_mode_t mode)
{
exec_mode = mode;
step_depth = cpu_get_depth();
cpu_sync_ref_timestamp();
}
void tamalib_step(void)
{
if (exec_mode == EXEC_MODE_PAUSE) {
return;
}
if (cpu_step()) {
exec_mode = EXEC_MODE_PAUSE;
step_depth = cpu_get_depth();
} else {
switch (exec_mode) {
case EXEC_MODE_PAUSE:
case EXEC_MODE_RUN:
break;
case EXEC_MODE_STEP:
exec_mode = EXEC_MODE_PAUSE;
break;
case EXEC_MODE_NEXT:
if (cpu_get_depth() <= step_depth) {
exec_mode = EXEC_MODE_PAUSE;
step_depth = cpu_get_depth();
}
break;
case EXEC_MODE_TO_CALL:
if (cpu_get_depth() > step_depth) {
exec_mode = EXEC_MODE_PAUSE;
step_depth = cpu_get_depth();
}
break;
case EXEC_MODE_TO_RET:
if (cpu_get_depth() < step_depth) {
exec_mode = EXEC_MODE_PAUSE;
step_depth = cpu_get_depth();
}
break;
}
}
}
void tamalib_mainloop(void)
{
timestamp_t ts;
while (!g_hal->handler()) {
tamalib_step();
/* Update the screen @ g_framerate fps */
ts = g_hal->get_timestamp();
if (ts - screen_ts >= ts_freq/g_framerate) {
screen_ts = ts;
g_hal->update_screen();
}
}
}
+66
View File
@@ -0,0 +1,66 @@
/*
* TamaLIB - A hardware agnostic first-gen Tamagotchi emulation library
*
* Copyright (C) 2021 Jean-Christophe Rona <jc@rona.fr>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef _TAMALIB_H_
#define _TAMALIB_H_
#include "cpu.h"
#include "hw.h"
#include "hal.h"
#define tamalib_set_button(btn, state) hw_set_button(btn, state)
#define tamalib_set_speed(speed) cpu_set_speed(speed)
#define tamalib_get_state() cpu_get_state()
#define tamalib_refresh_hw() cpu_refresh_hw()
#define tamalib_reset() cpu_reset()
#define tamalib_add_bp(list, addr) cpu_add_bp(list, addr)
#define tamalib_free_bp(list) cpu_free_bp(list)
typedef enum {
EXEC_MODE_PAUSE,
EXEC_MODE_RUN,
EXEC_MODE_STEP,
EXEC_MODE_NEXT,
EXEC_MODE_TO_CALL,
EXEC_MODE_TO_RET,
} exec_mode_t;
void tamalib_release(void);
bool_t tamalib_init(const u12_t *program, breakpoint_t *breakpoints, u32_t freq);
void tamalib_set_framerate(u8_t framerate);
u8_t tamalib_get_framerate(void);
void tamalib_register_hal(hal_t *hal);
void tamalib_set_exec_mode(exec_mode_t mode);
/* NOTE: Only one of these two functions must be used in the main application
* (tamalib_step() should be used only if tamalib_mainloop() does not fit the
* main application execution flow).
*/
void tamalib_step(void);
void tamalib_mainloop(void);
#endif /* _TAMALIB_H_ */