서버에서 바로 빌드하고 고칠 수 있게 저장소 줄바꿈을 LF로 맞췄습니다. CRLF로 들어가 있던 sh 빌드 스크립트도 이제 리눅스에서 그대로 돌아갑니다. 내용은 바뀌지 않았습니다.
187 lines
11 KiB
Markdown
187 lines
11 KiB
Markdown
# TamaEmu for the Tamagotchi Paradise
|
|
|
|
An emulator for colour Tamagotchis (Epson S1C33) that runs on the Paradise.
|
|
The Studio builds it into the ROM together with the user's own firmware.
|
|
|
|
- `emu.bin`: the emulator program. It is copied to PRAM by the launcher and
|
|
runs from there.
|
|
- `stub_tpl.bin`: the launcher (hooks on the home screen, A+C held) with
|
|
placeholders that the Studio fills in.
|
|
- `src/core`: the emulator core, derived from
|
|
[tamaemu](https://github.com/maragotchi/tamaemu) by maragotchi (GPL-3.0),
|
|
with a JIT translator to Thumb-2 (`tc_jit.c`).
|
|
- `src/emu`: the Paradise side (display, keys, sound, flash, saving the state).
|
|
- `src/stub/stub.S`: the launcher.
|
|
- `src/tools`: Python versions of the ROM patching steps (for tests).
|
|
|
|
## Layout in the Paradise flash
|
|
|
|
| Offset | What |
|
|
| --- | --- |
|
|
| 0x0A0000 | emulator program (64 KB, copied to PRAM on start) |
|
|
| 0x0B0000 | launcher and home screen hooks |
|
|
| 0x0C0000 | run state (written by the emulator) |
|
|
| 0x940000 | guest firmware, packed in 64 KB blocks; blocks the guest writes stay unpacked and hold its save |
|
|
| packed cold blocks (1.7.3, menu only) | table (128 u32), per block 65 u32 part starts, LZ4 data in 1 KB parts |
|
|
| behind it, up to 0xD40000 | spare pages: a table (4 KB) and 8 KB pages for blocks that are empty in the firmware but written by the guest |
|
|
| 0x840000 to 0xD48000 | instead of the two rows above, for firmware that does not fit there (P's, 4U+, 4U); the menu's field saves then have no room |
|
|
|
|
## Writing the guest flash
|
|
|
|
The guest erases and programs its flash like the real chip. Parameter sectors
|
|
(8 KB) and, since v0.9.2, the 64 KB sectors are kept as 8 KB copies in RAM and
|
|
written back to the Paradise flash when the slots run out or the game is
|
|
quiet. Erasing a 64 KB sector erases the stored block right away. Blocks that
|
|
are empty in the firmware are not stored by the Studio; their written 8 KB
|
|
parts go into spare pages behind the packed firmware. The table (magic
|
|
"PMXP", checksum) is written after the page, so a power cut leaves either the
|
|
old or the new state.
|
|
|
|
Before v0.9.2 writes to 64 KB sectors were lost. The iD L then hung when it
|
|
saved something there, for example buying the mop in the Tamadepa (daily
|
|
items), and the long press saved that hung state.
|
|
|
|
Leaving with a long press does not save the session if the core stopped or
|
|
guest writes were lost. Pressing the wheel within the first 3 seconds after a
|
|
resumed start drops the saved session.
|
|
|
|
Settings flags bit 12 (0x1000, version 4 and up, build switch `SESS_FLAG`, 1.7.3 test7): no session. Leaving, the
|
|
autosave and an empty battery write only the guest flash (the toy's own save), never the run state; a session found at
|
|
the start is dropped and the guest boots from its own save.
|
|
|
|
## Autostart
|
|
|
|
With "Start when switched on" the stub decides once per boot, at the first
|
|
home screen call. Mailbox word 0x20000FFC survives a restart and the reset
|
|
button but not the Paradise's deep sleep: "PMXL" (written by the emulator when
|
|
going back) keeps the Paradise, "PMXR" (written by the stub) means a restart
|
|
while running, so the emulator starts. Without either, the seconds counter
|
|
0x45000304 tells power-on (counter new) from waking up (counter ran). The
|
|
"decided" flag is the UsageFault priority byte, which every reset clears.
|
|
|
|
## Clock
|
|
|
|
Each millisecond of the Paradise runs the guest for one millisecond of its
|
|
current clock. Since v0.9.3 the remainder of that division is carried over
|
|
(at 32768 Hz the guest used to get 32 instead of 32.768 cycles per ms, its
|
|
clock lost 2.3 percent). About once a second the emulator also compares the
|
|
guest time with the seconds counter of the Paradise (0x45000304) and adds
|
|
whole seconds to the guest clock when it fell behind, for example when busy
|
|
scenes (shopping, flash writes) made the emulator drop time. Implausible jumps
|
|
of the counter are ignored.
|
|
|
|
At the start (settings version 4, flag bit 1, set by the Studio's "Take the
|
|
clock from the Paradise at the start"): the emulator reads the Paradise's own
|
|
time from the SRAM before it overwrites it (`paraclock.h`: the base of days and
|
|
seconds next to the six display fields, checked against the seconds counter)
|
|
and sets the guest's RTC registers to it, after a resumed session and on a
|
|
fresh start. Nothing is caught up. Without the flag, or when the time is not
|
|
found, a resumed session moves the clock on by the pause as before.
|
|
|
|
## Download content (8 MB firmware)
|
|
|
|
Stores and routing follow tamaemu `tools/dlc.c`: a record is a TAMAGO block,
|
|
routed by type (0x4E), category (0x50) and flag (0x51) into one of nine stores
|
|
(meals, snacks, clothing, accessories, items, daily items, wallpapers, games,
|
|
destinations). A new record goes into the first erased slot.
|
|
|
|
## Build
|
|
|
|
```
|
|
sh src/emu/build.sh # clang, ld.lld, llvm-objcopy -> src/emu/emu.bin
|
|
```
|
|
|
|
Copy `src/emu/emu.bin` to `emu/emu.bin` (the Studio embeds that one). With `EMU_OVL` (on since 1.6.13 test10) the
|
|
build links with `link_ovl.ld`: the settings menus and the save code sit at the end of the image, in the translator's
|
|
room, and are copied back from the flash when they are needed.
|
|
|
|
Device simulation (Unicorn, private ROMs only): `node test/mkimg.js <paradise.bin> <firmware> emu.bin out.bin`, then
|
|
`python3 test/devemu.py src/emu/emu.elf out.bin <ms>` with `KEYS`, `SHOTS`, `OUT`, `LAGLOG`, `PROF`, `ROMHIST`.
|
|
|
|
Launcher template (link address 0x1009F000):
|
|
|
|
```
|
|
clang --target=thumbv7m-none-eabi -mcpu=cortex-m3 -x assembler-with-cpp -c src/stub/stub.S -o stub.o \
|
|
-DHOOKS=1 -DLIMIT=90 -DAPP_SRC=0x1234ABCD -DORIG0=0x22222221 -DORIG1=0x33333331 -DORIG2=0x44444441 -DORIG3=0x55555551
|
|
ld.lld --no-rosegment -Ttext=0x1009F000 -e start stub.o -o stub.elf
|
|
llvm-objcopy -O binary stub.elf stub_tpl.bin
|
|
```
|
|
|
|
Both results are byte-identical to the files shipped here.
|
|
|
|
With `-DFELD=1` (menu launcher) the stub has a second entry at +0x0C
|
|
(`start_src`, 1.6.0): it copies the 64 KB image at the XIP address in r0 to
|
|
PRAM and restarts, like `start` does with its own program. The menu starts
|
|
further programs through it. Since 1.6.6 it reads from the uncached flash
|
|
(r0 + 0x50011000): the cached window shows only 1 MB. Build the menu launcher
|
|
with the same line plus `-DFELD=1 -DFELD_SRC=0x6789EF01 -DAUTO=0x66666661`.
|
|
Since 1.6.8 the menu launcher also has the entry `launch` at +0x20 (start the
|
|
menu), keeps the menu address at +0x28 and has at +0x2C the entry for the start
|
|
device's trampolines (menu once the Paradise has a save, otherwise the original
|
|
handler in r12).
|
|
|
|
License: GNU GPL version 3 (see `LICENSE`).
|
|
|
|
## Code that overwrites itself
|
|
|
|
The translator throws away all translated code when the guest writes to an
|
|
a0ram page that holds translated code. Since v0.9.6 a page that causes this 8
|
|
times within about 0.1 s of guest time is run by the interpreter only, for
|
|
about 5 s, then it gets another chance. Before, Connect to a Plus Color on the
|
|
iD L (code at 0xC00 next to its data) caused thousands of flushes per second
|
|
and the emulator stood still on the device. Build switch `JIT_WHOT`.
|
|
|
|
## 4U NFC chip and the clock (1.6.8)
|
|
|
|
The 4U and 4U+ firmware polls its PN512 NFC chip over the IR UART all the time (about 4000 bytes per
|
|
second to 0x300B10). The Paradise has no such chip, so every poll ran into a timeout and the menus needed
|
|
two presses. `tc_periph.c` now answers like an idle PN512 (register writes acknowledged, commands done at
|
|
once, empty FIFO). On for device kinds 3 and 4; PMXC flags bit 2 switches it off (Studio: "4U: emulate the
|
|
NFC chip").
|
|
|
|
The guest's real-time clock counted in guest cycles. When the guest changed its clock (fast clock to
|
|
32768 Hz, for example the iD L when its display goes off) the part of a second already counted at the fast
|
|
clock counted as many seconds at the slow one: the clock jumped by minutes. With `RTC_MS` (build.sh) the
|
|
seconds and the 1 s / 1 min / 1 h interrupts follow the real milliseconds of the main loop
|
|
(`periph_rtc_ms`); only the 1/64 s interrupt stays in cycles. `clock_sync` keeps the comparison with the
|
|
Paradise's crystal: missing seconds are added, a guest clock ahead skips one ms in 16.
|
|
|
|
## Pictures and the Paradise's clock from the menu (1.6.8)
|
|
|
|
`LCD_QUIET`: the screen is updated only when the guest has written nothing to its LCD for 2 ms (at most every
|
|
40 ms, at the latest after 120 ms), so a picture is never shown half drawn.
|
|
|
|
The Paramix menu uses the Paradise's SRAM, where the Paradise keeps its clock. It looks the clock up at its own
|
|
start and leaves it in the mailbox at 0x20000FC0 (`paraclock.h`: `para_mailbox_put`, `para_mailbox`); a
|
|
program started from the menu takes it from there when its own search finds nothing.
|
|
|
|
- 1.6.8-test5 APP_SELF: the start check (pram_check: PRAM against the image in the flash) uses the program's own image. A further
|
|
TamaEmu of the menu has it right behind its guest area (spare_end, the settings there name the same guest and save); before, it
|
|
compared with 0x0A0000 and copied the main TamaEmu's settings over its own. Its own settings block is never replaced.
|
|
- 1.6.8-test5 BL_GUEST_PWM: the colour Tamas light their screen with T16 channel 2 (0x300796 bit 0, clock 0x3007E4 bit 3). The P's
|
|
sends Sleep Out every 20 minutes for about 9 s without starting it (PC test: 36 times in a night); the backlight now goes on only
|
|
with the guest's light, a key or a tone in the last second, and stays on while the LCD stays on.
|
|
|
|
## Infrared between two Paradise (TC_IRLINK, 1.6.9)
|
|
|
|
P's and iD L: the bytes the toy writes to its infrared UART (0x300B10, also SIO_TX 0x301704) go out as 'E' frames
|
|
over the UART on the contacts (`src/emu/plink.h`, 460800 8N1). Received bytes come in at the toy's byte rate
|
|
(divisor at 0x300B16/17, 576 kHz clock, 10 bits per byte) with the RX interrupt, only while the receiver is armed
|
|
(0x300B15); the P's also only in a session (its byte 0xDB3, as tamaemu's ir_mode_flag), otherwise the byte is
|
|
lost and the peer-detect flag (0x300287 bit 3) rises. TX ready (0x300B12 bit 1) and the TX interrupt follow the byte
|
|
time. All as in tamaemu's periph.c (Android TamaEmu), the bytes from the platform instead of link.c (no time stamps:
|
|
the devices pass them on at once). State outside `Emu`, so saved sessions stay valid. Not for the Plus Color (its
|
|
infrared is a GPIO) and the 4U/4U+ (their UART talks to the NFC chip).
|
|
|
|
Checked on the PC with two P's cores in lockstep (host harness): あそぶ, one あそびにいく, the other ともだちをよぶ,
|
|
ふたりで, かくれんぼ: both play, the guest goes home, the host gets Gotchi points. With 15 ms delay each way it still
|
|
works, with 30 ms it ends in つうしん しっぱい.
|
|
|
|
Room: `boot()` (everything before the translator starts) runs from `.initcode`, the translator's area later, and a
|
|
few rarely used functions are built small (minsize). The translator area is 11772 bytes (before: 11608).
|
|
|
|
Key with the screen dark (1.7.3 test7, switch EMU_KEY_AWAKE): a key woke the chip from deep sleep and reached the
|
|
guest, but once it was let go the program slept again before the guest had switched its screen on; the guest's
|
|
wake-up then waited for the next RTC second. Now the chip stays awake for 2 s after a key (g_btn_ms). devemu.py, iD L
|
|
dark at 200 s, key 150 ms in the middle of an RTC second: picture 663 ms after the key before.
|