줄바꿈을 LF로 통일
서버에서 바로 빌드하고 고칠 수 있게 저장소 줄바꿈을 LF로 맞췄습니다. CRLF로 들어가 있던 sh 빌드 스크립트도 이제 리눅스에서 그대로 돌아갑니다. 내용은 바뀌지 않았습니다.
This commit is contained in:
1 parent
40615838ac
commit
2d48091205
623 files changed
+501372
-501365
No files matched your search
+230
-230
@@ -1,230 +1,230 @@
|
||||
# T7Emu for the Tamagotchi Paradise
|
||||
|
||||
An emulator for the Tama-Go (6502 based V7 chip) that runs on the Paradise. It also runs the
|
||||
Tamagotchi Friends (same chip family, test).
|
||||
The Studio builds it into the ROM together with the user's own Tama-Go ROM and
|
||||
figure ROMs. No ROM is part of this repository.
|
||||
|
||||
- `t7.bin`: the program. The TamaEmu launcher copies it to PRAM and starts it.
|
||||
Its settings block ("PMX7") is at 0x140 and is filled in by the Studio
|
||||
(`src/emu.js`, `t7Patch`).
|
||||
- `src/core`: the emulator core, a C port of
|
||||
[tamago](https://github.com/asterick/tamago) by asterick (BSD), taken over
|
||||
from the T7Emu app. `t7_run.inc` is a faster run loop with the same results
|
||||
as `t7_cpu_step` (kept for the host tests).
|
||||
- `src/app`: the Paradise side. `t7app.c` has screen, settings menu, clock
|
||||
and saved state and also builds on the PC; `main.c` has the hardware part
|
||||
(it uses `emu/src/emu/hw.c` and `flash.c`).
|
||||
- `host`: PC tests. `hrun.c` boots the ROM and saves screenshots, `happ.c`
|
||||
tests the app logic (clock, catching up, save, figures), `runcmp.c` and
|
||||
`tr.c` compare the fast loop with the old step function. `setup.txt` is the
|
||||
key sequence that finishes the first setup and hatches the egg.
|
||||
- `test`: `devt7.py` runs `t7.bin` in Unicorn with a simulated Paradise,
|
||||
`proft7.py` profiles it.
|
||||
|
||||
## Build
|
||||
|
||||
```
|
||||
sh src/app/build.sh # clang, ld.lld, llvm-objcopy; writes t7.bin and t7x.bin (the JIT, second part)
|
||||
KEEPELF=1 sh src/app/build.sh # keeps src/app/t7.elf for the profiler
|
||||
```
|
||||
|
||||
## Layout in the Paradise flash
|
||||
|
||||
| Offset | What |
|
||||
| --- | --- |
|
||||
| 0x0A0000 | program (copied to PRAM on start) |
|
||||
| 0x0C0000 | session (state of the running emulator, "T7SS") |
|
||||
| 0x0C1000 | EEPROM of the Tama-Go, two slots of 8 KB ("T7EE", sequence, page mask, checksum, then 4 KB) |
|
||||
| 0x0C5000 | settings from the menu ("T7S2", 16 bytes; layout 3 in r1 bits 0..3: light in bits 4..7, dim level in r0) and at +32 the clock note ("T7CK": seconds counter, game time, month and day when leaving) |
|
||||
| 0x0C8000 | second part of the program (t7x.bin, the JIT, 1.6.12 test2) |
|
||||
| 0x940000 | Tama-Go ROM (640 KB) or Tamagotchi Friends ROM (768 KB) |
|
||||
| behind it, up to 0xD40000 | figure ROMs, trailing empty bytes cut, each on a 4 KB boundary |
|
||||
|
||||
The save.bin of the T7Emu app (page mask, then the 4 KB EEPROM, 4098 bytes)
|
||||
has the same content as one EEPROM slot, see `src/saves.js`.
|
||||
|
||||
## More instances (1.6.0)
|
||||
|
||||
The program only uses its settings block for ROM, figures and save area, so the
|
||||
Studio can put several copies into the flash (one per ROM, see the main README,
|
||||
"More emulators"). `test/devt7.py` runs one of them with `APPOFF=<image offset>`.
|
||||
|
||||
## Tamagotchi Friends
|
||||
|
||||
The Friends ROM (768 KB) is recognised by its reset vector: not at 0x3FFC like the Tama-Go but at
|
||||
0x7FFC. Differences, all in `src/core` behind `g_t7_static_off`:
|
||||
|
||||
- The file has 16 KB more in front. The static ROM ($C000) is at 0x4000, bank 0 begins at 0x4000
|
||||
(its code is linked to $8000), bank n >= 1 at (n + 1) * 32 KB. 23 banks.
|
||||
- The frame tick comes from IRQ 3 (every 122nd, counter $55) instead of the video IRQ. Instead of
|
||||
7.3 kHz of interrupts the core sets the counter just below and fires IRQ 3 once per video frame.
|
||||
- The second of the game clock is at 0x136 (Tama-Go 0x396); hour, minute, day and month as on the
|
||||
Tama-Go.
|
||||
- The IR hooks of the Tama-Go (bank 3) are off. No figures.
|
||||
|
||||
## Clock
|
||||
|
||||
Settings flag bit 1 (the Studio's "Take the clock from the Paradise at the
|
||||
start", on by default): at every start the game clock is set to the Paradise's
|
||||
own time, read from its SRAM before T7Emu uses it (`../emu/src/emu/paraclock.h`).
|
||||
Nothing is caught up. When the game asks for the time (hour 255) it gets the
|
||||
same time. Without the flag (or with the clock set to "Game" in the menu, or
|
||||
when the time is not found) the old way: after a pause the missed hours pass,
|
||||
up to 30.
|
||||
|
||||
## Timing
|
||||
|
||||
The core runs at 3.03 MHz with a video interrupt every 66,667 cycles (60 Hz).
|
||||
The game clock is kept on the Paradise's seconds counter: every 2 s the app
|
||||
sets minute and second; when a whole hour is missing it lets the game roll
|
||||
the hour itself, so the day goes on as in the game. After a pause up to 30
|
||||
hours are caught up, then the hour is set directly.
|
||||
|
||||
## PC passwords (1.6.8-test4, T7_PW_FIX)
|
||||
|
||||
The PC password code (bank 2, login $6229, encoding $6378, logout check $643E, reward $6192) uses a nibble helper: the
|
||||
game writes a byte to $3080 and reads its high nibble at $3082, its low nibble at $3083 and the byte with swapped
|
||||
nibbles at $308A. The core returned 0 there, so the login was always 000000000000 and every logout decoded to zeros
|
||||
and failed. With T7_PW_FIX the login changes every time and a correct logout is accepted (host test: 0 to 5000 points).
|
||||
Logout: same format as the login; byte 0 = 0x80 | login byte 0, byte 1 = login byte 1, points in thousands in byte 3
|
||||
and the low 2 bits of byte 4, checksum nibble. Gotchi Points: RAM $0320-$0324 and EEPROM 0x030-0x034, 5 bytes BCD
|
||||
lowest digits first (save.bin of the app: file offset 0x32-0x36, behind the 2 byte page mask); no checksum.
|
||||
|
||||
## Battery (T7_IDLE, T7_STOP, 1.6.9 test12)
|
||||
|
||||
Taken over from TamaGo for the Pebble (v0.3.0). T7_IDLE: the Tama-Go's main loop waits at $CE66 (5 JSR to the seed
|
||||
routines at $D2ED, LDA $2D, BEQ back); when the BEQ goes back and no IRQ is taken, the passes that fit up to the end of
|
||||
the run or the next sound event are added up at once (seeds $C4..$C8, the stack slots of the last JSR, A, flags).
|
||||
Checked against the ROM bytes at the start (t7_idle_check; Friends: off). Bit-identical: host/runcmp.c gives the same
|
||||
state hash every 2 ms with and without the switch. T7_STOP: writing 7 to $3001 halts the CPU (original sound only,
|
||||
its timers wake it); any IRQ request or a key ($3007 bit 0 set, reported in $3008 bit 0) wakes it. On the PC (runcmp,
|
||||
sound on, 10 minutes after setup): 78 % of the emulated cycles skipped, the game clock the same as without.
|
||||
|
||||
## Power save with the screen off (T7_JSR_HLE, T7_DARK_STEP, T7_SND_LAZY, T7_PW, 1.6.12 test1)
|
||||
|
||||
Measured on the PC with a hatched Tama in its room (host tools, see NOTIZEN): the 6502 runs about 213000 instructions a
|
||||
second, 77 % of them in the sprite blit at $E4CD; with the screen off nobody sees it. Device simulation: nearly half of
|
||||
the remaining time went into the sound timers (3322 and 512 events a second) although their IRQs are masked.
|
||||
|
||||
- T7_JSR_HLE (core): JSR $E4CD is left out while g_t7_dark is set (screen off): the game state stays the same apart from
|
||||
the random seeds $C4..$C8, the picture comes back with the next frames. JSR $D460 (a delay: LDX #$D1, INX/BNE, RTS)
|
||||
is done at once when it ends inside the run: the same state and cycles (runcmp identical). Both checked against the
|
||||
ROM bytes at the start (Tama-Go only).
|
||||
- T7_SND_LAZY (core): a sound timer whose IRQ is masked gets no events; when the mask opens (or the timer is set) its
|
||||
next overflow is put where it would be. runcmp with sound: identical.
|
||||
- T7_DARK_STEP: screen off: the emulation in steps up to 50 ms (the same emulated time).
|
||||
- T7_PW (setting "Power save": Normal, Strong, Maximal; default Strong): screen off, no menu, no tone, no link:
|
||||
Strong runs the chip from its internal 12 MHz RC with PLL and crystal off (datasheet: 1.6 mA instead of 9 to 12 mA),
|
||||
Maximal also goes to deep sleep (0.4 mA) whenever the emulation has caught up; the RTC (1 s, as the Paradise sets it)
|
||||
or a key wakes it, the ms tick continues from the RTC. The setting "Sleep/12M" shows the share of the time asleep and
|
||||
at 12 MHz since the start (for tests).
|
||||
|
||||
Device simulation (test/devt7.py models the clock source and deep sleep), 35 s screen off: awake at 12 MHz 7 %, asleep
|
||||
93 %; before: 48 MHz all the time.
|
||||
|
||||
## JIT (T7_JIT, 1.6.12 test2)
|
||||
|
||||
`src/core/t7_jit.c` translates straight runs of ROM code ($4000 up) into Thumb-2 code: 6502 A, X, Y, S and the flags
|
||||
in ARM registers, memory through the page table (work and display RAM at a fixed address directly, the rest through
|
||||
two shared stubs; an I/O write ends the block). A block runs only when the interpreter would run all of it in this
|
||||
call (cycles left > its cycles before the last instruction, no sound event inside), with the same cycle sums, so the
|
||||
result is the same instruction by instruction. Blocks follow each other inside the translated code while that holds
|
||||
(exit stub with its own lookup). Blocks start where a run of code starts (after a jump or a block) and are made on
|
||||
the 8th visit; keyed by the physical address, so banked code needs no flushing; buffer full: all thrown away.
|
||||
|
||||
Checked under qemu-arm against the x86 interpreter (host runner, state hash every 2 ms, 600 s with keys and sound):
|
||||
identical. Device simulation (Unicorn, screen on): about 6,400 instead of 35,000 ARM instructions per ms.
|
||||
|
||||
The translator does not fit into the 64 KB of PRAM: it is a second part (`t7x.bin`, about 8 KB, magic "T7X1", build id,
|
||||
length, sum) that the Studio writes into the save area at +0x8000 (main program 0x0C8000, menu instances save + 0x8000).
|
||||
The program copies it into the SRAM behind its data; the rest of the SRAM up to the stack holds the translated code
|
||||
(about 44 KB). Without a matching second part (older Studio, other build) T7Emu runs as before. Setting "JIT" (On/Off,
|
||||
"missing" without the second part), settings byte r3 bit 0.
|
||||
|
||||
## Volume (T7_VOL, HW_VOL5, 1.6.12 test3)
|
||||
|
||||
The buzzer gets a pulse per period; its length sets the volume. Before: 1/32 of the period. Setting "Sound": Off, Quiet
|
||||
(1/32, as before), Medium (1/8), Loud (1/4). A half period (square wave) was tried and was quieter than 1/4 on the
|
||||
device. Stored in g_set.sound (old value 1 = Quiet).
|
||||
|
||||
## Tone and fixed settings (1.6.12 test4)
|
||||
|
||||
- T7_TONE_EV: the tone changes come from the core's tone events (t7_snd_tone_events: a look every ms of game time, a
|
||||
new pitch only when it stays 4 ms, silent when the envelope at $93 has died away, with the cycle it happened). Before,
|
||||
the main loop looked every 2 ms: at each note start the next pitch sounded for 2 ms, then 4 ms of silence, then the
|
||||
note (host comparison of a melody: a click at every note); notes rang on after their envelope.
|
||||
- T7_TONE_ISR: the tone queue (T7_TONE_Q) is played from the 1 ms tick instead of the main loop, where a note change
|
||||
waited for drawing and the emulation. HW_TONE_IRQSAFE: hw_tone with the interrupts off, TONE_SAFE: a shorter period
|
||||
below the running counter restarts it (otherwise silent until the counter wraps).
|
||||
- T7_FIXED: power save maximal and the JIT always, without settings (tested over night).
|
||||
- T7_WHEEL_RAW: the wheel's long press is timed with a tick count that is never set ahead (g_ms is moved on after
|
||||
deep sleep and by eco_rtc, up to a second at once: a short press right after waking counted as long and left).
|
||||
|
||||
## Game clock tempo (T7_TBH_RATE, 1.6.12 test2)
|
||||
|
||||
The game counts its seconds from the 2 Hz timer (IRQ 13, two passes of $D1A3 per second). The core fired it every
|
||||
2,000,000 cycles (4 MHz); the Paradise runs the core at 3.03 MHz for the tempo, so a game second lasted 1.32 s and the
|
||||
clock note set it ahead by 2 to 3 seconds every few seconds (the seconds jumped). T7_TBH_RATE=1515150: two per real
|
||||
second.
|
||||
|
||||
## Keys (T7_KEYQ, 1.6.9 test13)
|
||||
|
||||
The main loop read the keys once per pass; a pass that draws and catches up took longer than a quick tap, the tap was
|
||||
lost. The 1 ms tick now counts every press (GPIO0 P0.9 A, P0.11 B, P0.10 C, pressed = 0); each counted press goes to
|
||||
the game for at least 120 ms of emulated time (longer while held), then 100 ms released (test14: on the PC the
|
||||
firmware lost taps shorter than about 120 ms or gaps shorter than about 80 ms, also without STOP).
|
||||
test15: a press counts only after the key was up for 30 ms (KQ_DEB): the contacts bounce, one press
|
||||
was taken as two (clock and straight back to the room).
|
||||
T7_STOP_KEYACT (cycles, 4000000 = 1 s): after a key change no STOP. Halted, the firmware missed the release
|
||||
(only IRQ 13 enabled, 2 Hz) and took the next press as the same one: 3 quick taps, 1 or 2 seen. Presses that wake the screen,
|
||||
close the settings menu or come while it is open do not reach the game. host/hrun.c "k A hold gap" taps in 2 ms steps:
|
||||
the core takes taps of 20 ms; test/devt7.py feeds the GPIO and the press counter.
|
||||
|
||||
## Save after a battery change (T7_SESS_EESEQ, T7_EE_SOON, 1.6.9 test16)
|
||||
|
||||
A user report: items bought in a figure shop vanished, after taking the batteries out the game was new. The run
|
||||
state (session, 0x0C0000) holds the CPU, RAM and display, not the EEPROM; SESS_AUTO writes it every 30 minutes or
|
||||
when the screen goes dark. After a battery change that old run state was resumed: its RAM won over the EEPROM the
|
||||
game had written since, and the game wrote the old state back. Now the session sector notes at 0xFF8 "T7EQ" and the
|
||||
sequence of the EEPROM slot in the flash (the EEPROM is stored first, then the session); a session whose sequence is
|
||||
not the current one is dropped and the game starts from its EEPROM. Sessions without the note load as before.
|
||||
T7_EE_SOON: the game's EEPROM writes go to the flash 5 s later (2 s without a key) instead of after a minute
|
||||
without keys. The game writes its EEPROM seldom (host/hrun.c with -DT7_EEDBG, EEDBG=1: at the setup and at
|
||||
events, nothing in 10 idle minutes). Checked in test/devt7.py (session with SESS_T1/T2 short: sequence noted;
|
||||
a session with an older sequence: cold start from the EEPROM, the egg is there).
|
||||
|
||||
## Tone (T7_TONE_Q, 1.6.9 test14)
|
||||
|
||||
The tone was set right after each 2 ms step. After drawing or a flash write the loop catches up up to 30 ms at
|
||||
once, so the notes of a melody (about 26 ms, host/hrun.c "tl <s>" lists them) came within a millisecond and were
|
||||
swallowed. As T18Emu: tone changes queued with the game time, played 80 ms after the first of a sequence on the
|
||||
real clock, late by more than 200 ms the timing starts anew.
|
||||
|
||||
## Pixel mode (T7_PIXSOFT, tried in test13, off)
|
||||
|
||||
A game pixel is 2 display pixels (pixel exact) or 2 to 3 (large). test13 drew the gap of every cell in its own colour,
|
||||
a little lighter; on the device it no longer looked like pixels. Off again, the pixel mode is as before.
|
||||
|
||||
## Visits between two Paradise (T7_LINK, 1.6.9)
|
||||
|
||||
Tama-Go: connection icon, TAMA-GO, VISIT or GAME. Both wait in STAND BY, B on the device that goes visiting sends.
|
||||
The game sends a packet with `$748E` (bank 3): `$32` and `$5B` minus the count, a preamble, then the bytes from `$0220`
|
||||
by switching the infrared LED (`$3016` bit 3). `t7_ir_led_on` (called by `t7_io_write`, outside the hot loop) takes
|
||||
the packet at the first LED switch-on of the preamble (`$32 == $5B`) as 32 bytes with the sum in the last, like the
|
||||
older `t7_ir_bridge_tx_poll` (that one compared `$0220` and lost a packet sent again unchanged). The other device puts
|
||||
it in with `t7_ir_bridge_rx` (the receive hook at `$60C4` of the IR bridge), only while its game is in the connection
|
||||
screen (`t7_ir_activity` changed within 1.5 s), so an old packet does not wait. Between the devices: `T` frames over
|
||||
the UART on the contacts (`../emu/src/emu/plink.h`, 460800 8N1, A5 type len data sum, hello every second).
|
||||
|
||||
Sound must be on: the game waits for its beeps to end (`$A3` bit 7) also during the visit. Checked on the PC with two
|
||||
cores linked in lockstep: visit (guest away, the host shows both, guest home), GAME (YOU WIN / YOU LOSE), the second
|
||||
device entering STAND BY late (the first one sends again).
|
||||
|
||||
## Clock note after a battery change (T7_CK_WRAP, 1.6.11 test1)
|
||||
|
||||
When the game asks for the time, the noted game time is counted on with the Paradise's seconds counter. After a
|
||||
battery change that counter starts at 0 again, below the noted value; the difference wrapped to about 136 years and
|
||||
the game got a date far off. Now the noted time is taken as it was (the clock from the Paradise sets it right after).
|
||||
|
||||
No session (SESS_FLAG, 1.7.3 test7): settings flag bit 12 (0x1000): the run state is never written (leaving, automatic sessions, battery empty) and an old one is dropped at the start; the EEPROM, settings and clock note are stored as before, so the toy starts from its own save.
|
||||
# T7Emu for the Tamagotchi Paradise
|
||||
|
||||
An emulator for the Tama-Go (6502 based V7 chip) that runs on the Paradise. It also runs the
|
||||
Tamagotchi Friends (same chip family, test).
|
||||
The Studio builds it into the ROM together with the user's own Tama-Go ROM and
|
||||
figure ROMs. No ROM is part of this repository.
|
||||
|
||||
- `t7.bin`: the program. The TamaEmu launcher copies it to PRAM and starts it.
|
||||
Its settings block ("PMX7") is at 0x140 and is filled in by the Studio
|
||||
(`src/emu.js`, `t7Patch`).
|
||||
- `src/core`: the emulator core, a C port of
|
||||
[tamago](https://github.com/asterick/tamago) by asterick (BSD), taken over
|
||||
from the T7Emu app. `t7_run.inc` is a faster run loop with the same results
|
||||
as `t7_cpu_step` (kept for the host tests).
|
||||
- `src/app`: the Paradise side. `t7app.c` has screen, settings menu, clock
|
||||
and saved state and also builds on the PC; `main.c` has the hardware part
|
||||
(it uses `emu/src/emu/hw.c` and `flash.c`).
|
||||
- `host`: PC tests. `hrun.c` boots the ROM and saves screenshots, `happ.c`
|
||||
tests the app logic (clock, catching up, save, figures), `runcmp.c` and
|
||||
`tr.c` compare the fast loop with the old step function. `setup.txt` is the
|
||||
key sequence that finishes the first setup and hatches the egg.
|
||||
- `test`: `devt7.py` runs `t7.bin` in Unicorn with a simulated Paradise,
|
||||
`proft7.py` profiles it.
|
||||
|
||||
## Build
|
||||
|
||||
```
|
||||
sh src/app/build.sh # clang, ld.lld, llvm-objcopy; writes t7.bin and t7x.bin (the JIT, second part)
|
||||
KEEPELF=1 sh src/app/build.sh # keeps src/app/t7.elf for the profiler
|
||||
```
|
||||
|
||||
## Layout in the Paradise flash
|
||||
|
||||
| Offset | What |
|
||||
| --- | --- |
|
||||
| 0x0A0000 | program (copied to PRAM on start) |
|
||||
| 0x0C0000 | session (state of the running emulator, "T7SS") |
|
||||
| 0x0C1000 | EEPROM of the Tama-Go, two slots of 8 KB ("T7EE", sequence, page mask, checksum, then 4 KB) |
|
||||
| 0x0C5000 | settings from the menu ("T7S2", 16 bytes; layout 3 in r1 bits 0..3: light in bits 4..7, dim level in r0) and at +32 the clock note ("T7CK": seconds counter, game time, month and day when leaving) |
|
||||
| 0x0C8000 | second part of the program (t7x.bin, the JIT, 1.6.12 test2) |
|
||||
| 0x940000 | Tama-Go ROM (640 KB) or Tamagotchi Friends ROM (768 KB) |
|
||||
| behind it, up to 0xD40000 | figure ROMs, trailing empty bytes cut, each on a 4 KB boundary |
|
||||
|
||||
The save.bin of the T7Emu app (page mask, then the 4 KB EEPROM, 4098 bytes)
|
||||
has the same content as one EEPROM slot, see `src/saves.js`.
|
||||
|
||||
## More instances (1.6.0)
|
||||
|
||||
The program only uses its settings block for ROM, figures and save area, so the
|
||||
Studio can put several copies into the flash (one per ROM, see the main README,
|
||||
"More emulators"). `test/devt7.py` runs one of them with `APPOFF=<image offset>`.
|
||||
|
||||
## Tamagotchi Friends
|
||||
|
||||
The Friends ROM (768 KB) is recognised by its reset vector: not at 0x3FFC like the Tama-Go but at
|
||||
0x7FFC. Differences, all in `src/core` behind `g_t7_static_off`:
|
||||
|
||||
- The file has 16 KB more in front. The static ROM ($C000) is at 0x4000, bank 0 begins at 0x4000
|
||||
(its code is linked to $8000), bank n >= 1 at (n + 1) * 32 KB. 23 banks.
|
||||
- The frame tick comes from IRQ 3 (every 122nd, counter $55) instead of the video IRQ. Instead of
|
||||
7.3 kHz of interrupts the core sets the counter just below and fires IRQ 3 once per video frame.
|
||||
- The second of the game clock is at 0x136 (Tama-Go 0x396); hour, minute, day and month as on the
|
||||
Tama-Go.
|
||||
- The IR hooks of the Tama-Go (bank 3) are off. No figures.
|
||||
|
||||
## Clock
|
||||
|
||||
Settings flag bit 1 (the Studio's "Take the clock from the Paradise at the
|
||||
start", on by default): at every start the game clock is set to the Paradise's
|
||||
own time, read from its SRAM before T7Emu uses it (`../emu/src/emu/paraclock.h`).
|
||||
Nothing is caught up. When the game asks for the time (hour 255) it gets the
|
||||
same time. Without the flag (or with the clock set to "Game" in the menu, or
|
||||
when the time is not found) the old way: after a pause the missed hours pass,
|
||||
up to 30.
|
||||
|
||||
## Timing
|
||||
|
||||
The core runs at 3.03 MHz with a video interrupt every 66,667 cycles (60 Hz).
|
||||
The game clock is kept on the Paradise's seconds counter: every 2 s the app
|
||||
sets minute and second; when a whole hour is missing it lets the game roll
|
||||
the hour itself, so the day goes on as in the game. After a pause up to 30
|
||||
hours are caught up, then the hour is set directly.
|
||||
|
||||
## PC passwords (1.6.8-test4, T7_PW_FIX)
|
||||
|
||||
The PC password code (bank 2, login $6229, encoding $6378, logout check $643E, reward $6192) uses a nibble helper: the
|
||||
game writes a byte to $3080 and reads its high nibble at $3082, its low nibble at $3083 and the byte with swapped
|
||||
nibbles at $308A. The core returned 0 there, so the login was always 000000000000 and every logout decoded to zeros
|
||||
and failed. With T7_PW_FIX the login changes every time and a correct logout is accepted (host test: 0 to 5000 points).
|
||||
Logout: same format as the login; byte 0 = 0x80 | login byte 0, byte 1 = login byte 1, points in thousands in byte 3
|
||||
and the low 2 bits of byte 4, checksum nibble. Gotchi Points: RAM $0320-$0324 and EEPROM 0x030-0x034, 5 bytes BCD
|
||||
lowest digits first (save.bin of the app: file offset 0x32-0x36, behind the 2 byte page mask); no checksum.
|
||||
|
||||
## Battery (T7_IDLE, T7_STOP, 1.6.9 test12)
|
||||
|
||||
Taken over from TamaGo for the Pebble (v0.3.0). T7_IDLE: the Tama-Go's main loop waits at $CE66 (5 JSR to the seed
|
||||
routines at $D2ED, LDA $2D, BEQ back); when the BEQ goes back and no IRQ is taken, the passes that fit up to the end of
|
||||
the run or the next sound event are added up at once (seeds $C4..$C8, the stack slots of the last JSR, A, flags).
|
||||
Checked against the ROM bytes at the start (t7_idle_check; Friends: off). Bit-identical: host/runcmp.c gives the same
|
||||
state hash every 2 ms with and without the switch. T7_STOP: writing 7 to $3001 halts the CPU (original sound only,
|
||||
its timers wake it); any IRQ request or a key ($3007 bit 0 set, reported in $3008 bit 0) wakes it. On the PC (runcmp,
|
||||
sound on, 10 minutes after setup): 78 % of the emulated cycles skipped, the game clock the same as without.
|
||||
|
||||
## Power save with the screen off (T7_JSR_HLE, T7_DARK_STEP, T7_SND_LAZY, T7_PW, 1.6.12 test1)
|
||||
|
||||
Measured on the PC with a hatched Tama in its room (host tools, see NOTIZEN): the 6502 runs about 213000 instructions a
|
||||
second, 77 % of them in the sprite blit at $E4CD; with the screen off nobody sees it. Device simulation: nearly half of
|
||||
the remaining time went into the sound timers (3322 and 512 events a second) although their IRQs are masked.
|
||||
|
||||
- T7_JSR_HLE (core): JSR $E4CD is left out while g_t7_dark is set (screen off): the game state stays the same apart from
|
||||
the random seeds $C4..$C8, the picture comes back with the next frames. JSR $D460 (a delay: LDX #$D1, INX/BNE, RTS)
|
||||
is done at once when it ends inside the run: the same state and cycles (runcmp identical). Both checked against the
|
||||
ROM bytes at the start (Tama-Go only).
|
||||
- T7_SND_LAZY (core): a sound timer whose IRQ is masked gets no events; when the mask opens (or the timer is set) its
|
||||
next overflow is put where it would be. runcmp with sound: identical.
|
||||
- T7_DARK_STEP: screen off: the emulation in steps up to 50 ms (the same emulated time).
|
||||
- T7_PW (setting "Power save": Normal, Strong, Maximal; default Strong): screen off, no menu, no tone, no link:
|
||||
Strong runs the chip from its internal 12 MHz RC with PLL and crystal off (datasheet: 1.6 mA instead of 9 to 12 mA),
|
||||
Maximal also goes to deep sleep (0.4 mA) whenever the emulation has caught up; the RTC (1 s, as the Paradise sets it)
|
||||
or a key wakes it, the ms tick continues from the RTC. The setting "Sleep/12M" shows the share of the time asleep and
|
||||
at 12 MHz since the start (for tests).
|
||||
|
||||
Device simulation (test/devt7.py models the clock source and deep sleep), 35 s screen off: awake at 12 MHz 7 %, asleep
|
||||
93 %; before: 48 MHz all the time.
|
||||
|
||||
## JIT (T7_JIT, 1.6.12 test2)
|
||||
|
||||
`src/core/t7_jit.c` translates straight runs of ROM code ($4000 up) into Thumb-2 code: 6502 A, X, Y, S and the flags
|
||||
in ARM registers, memory through the page table (work and display RAM at a fixed address directly, the rest through
|
||||
two shared stubs; an I/O write ends the block). A block runs only when the interpreter would run all of it in this
|
||||
call (cycles left > its cycles before the last instruction, no sound event inside), with the same cycle sums, so the
|
||||
result is the same instruction by instruction. Blocks follow each other inside the translated code while that holds
|
||||
(exit stub with its own lookup). Blocks start where a run of code starts (after a jump or a block) and are made on
|
||||
the 8th visit; keyed by the physical address, so banked code needs no flushing; buffer full: all thrown away.
|
||||
|
||||
Checked under qemu-arm against the x86 interpreter (host runner, state hash every 2 ms, 600 s with keys and sound):
|
||||
identical. Device simulation (Unicorn, screen on): about 6,400 instead of 35,000 ARM instructions per ms.
|
||||
|
||||
The translator does not fit into the 64 KB of PRAM: it is a second part (`t7x.bin`, about 8 KB, magic "T7X1", build id,
|
||||
length, sum) that the Studio writes into the save area at +0x8000 (main program 0x0C8000, menu instances save + 0x8000).
|
||||
The program copies it into the SRAM behind its data; the rest of the SRAM up to the stack holds the translated code
|
||||
(about 44 KB). Without a matching second part (older Studio, other build) T7Emu runs as before. Setting "JIT" (On/Off,
|
||||
"missing" without the second part), settings byte r3 bit 0.
|
||||
|
||||
## Volume (T7_VOL, HW_VOL5, 1.6.12 test3)
|
||||
|
||||
The buzzer gets a pulse per period; its length sets the volume. Before: 1/32 of the period. Setting "Sound": Off, Quiet
|
||||
(1/32, as before), Medium (1/8), Loud (1/4). A half period (square wave) was tried and was quieter than 1/4 on the
|
||||
device. Stored in g_set.sound (old value 1 = Quiet).
|
||||
|
||||
## Tone and fixed settings (1.6.12 test4)
|
||||
|
||||
- T7_TONE_EV: the tone changes come from the core's tone events (t7_snd_tone_events: a look every ms of game time, a
|
||||
new pitch only when it stays 4 ms, silent when the envelope at $93 has died away, with the cycle it happened). Before,
|
||||
the main loop looked every 2 ms: at each note start the next pitch sounded for 2 ms, then 4 ms of silence, then the
|
||||
note (host comparison of a melody: a click at every note); notes rang on after their envelope.
|
||||
- T7_TONE_ISR: the tone queue (T7_TONE_Q) is played from the 1 ms tick instead of the main loop, where a note change
|
||||
waited for drawing and the emulation. HW_TONE_IRQSAFE: hw_tone with the interrupts off, TONE_SAFE: a shorter period
|
||||
below the running counter restarts it (otherwise silent until the counter wraps).
|
||||
- T7_FIXED: power save maximal and the JIT always, without settings (tested over night).
|
||||
- T7_WHEEL_RAW: the wheel's long press is timed with a tick count that is never set ahead (g_ms is moved on after
|
||||
deep sleep and by eco_rtc, up to a second at once: a short press right after waking counted as long and left).
|
||||
|
||||
## Game clock tempo (T7_TBH_RATE, 1.6.12 test2)
|
||||
|
||||
The game counts its seconds from the 2 Hz timer (IRQ 13, two passes of $D1A3 per second). The core fired it every
|
||||
2,000,000 cycles (4 MHz); the Paradise runs the core at 3.03 MHz for the tempo, so a game second lasted 1.32 s and the
|
||||
clock note set it ahead by 2 to 3 seconds every few seconds (the seconds jumped). T7_TBH_RATE=1515150: two per real
|
||||
second.
|
||||
|
||||
## Keys (T7_KEYQ, 1.6.9 test13)
|
||||
|
||||
The main loop read the keys once per pass; a pass that draws and catches up took longer than a quick tap, the tap was
|
||||
lost. The 1 ms tick now counts every press (GPIO0 P0.9 A, P0.11 B, P0.10 C, pressed = 0); each counted press goes to
|
||||
the game for at least 120 ms of emulated time (longer while held), then 100 ms released (test14: on the PC the
|
||||
firmware lost taps shorter than about 120 ms or gaps shorter than about 80 ms, also without STOP).
|
||||
test15: a press counts only after the key was up for 30 ms (KQ_DEB): the contacts bounce, one press
|
||||
was taken as two (clock and straight back to the room).
|
||||
T7_STOP_KEYACT (cycles, 4000000 = 1 s): after a key change no STOP. Halted, the firmware missed the release
|
||||
(only IRQ 13 enabled, 2 Hz) and took the next press as the same one: 3 quick taps, 1 or 2 seen. Presses that wake the screen,
|
||||
close the settings menu or come while it is open do not reach the game. host/hrun.c "k A hold gap" taps in 2 ms steps:
|
||||
the core takes taps of 20 ms; test/devt7.py feeds the GPIO and the press counter.
|
||||
|
||||
## Save after a battery change (T7_SESS_EESEQ, T7_EE_SOON, 1.6.9 test16)
|
||||
|
||||
A user report: items bought in a figure shop vanished, after taking the batteries out the game was new. The run
|
||||
state (session, 0x0C0000) holds the CPU, RAM and display, not the EEPROM; SESS_AUTO writes it every 30 minutes or
|
||||
when the screen goes dark. After a battery change that old run state was resumed: its RAM won over the EEPROM the
|
||||
game had written since, and the game wrote the old state back. Now the session sector notes at 0xFF8 "T7EQ" and the
|
||||
sequence of the EEPROM slot in the flash (the EEPROM is stored first, then the session); a session whose sequence is
|
||||
not the current one is dropped and the game starts from its EEPROM. Sessions without the note load as before.
|
||||
T7_EE_SOON: the game's EEPROM writes go to the flash 5 s later (2 s without a key) instead of after a minute
|
||||
without keys. The game writes its EEPROM seldom (host/hrun.c with -DT7_EEDBG, EEDBG=1: at the setup and at
|
||||
events, nothing in 10 idle minutes). Checked in test/devt7.py (session with SESS_T1/T2 short: sequence noted;
|
||||
a session with an older sequence: cold start from the EEPROM, the egg is there).
|
||||
|
||||
## Tone (T7_TONE_Q, 1.6.9 test14)
|
||||
|
||||
The tone was set right after each 2 ms step. After drawing or a flash write the loop catches up up to 30 ms at
|
||||
once, so the notes of a melody (about 26 ms, host/hrun.c "tl <s>" lists them) came within a millisecond and were
|
||||
swallowed. As T18Emu: tone changes queued with the game time, played 80 ms after the first of a sequence on the
|
||||
real clock, late by more than 200 ms the timing starts anew.
|
||||
|
||||
## Pixel mode (T7_PIXSOFT, tried in test13, off)
|
||||
|
||||
A game pixel is 2 display pixels (pixel exact) or 2 to 3 (large). test13 drew the gap of every cell in its own colour,
|
||||
a little lighter; on the device it no longer looked like pixels. Off again, the pixel mode is as before.
|
||||
|
||||
## Visits between two Paradise (T7_LINK, 1.6.9)
|
||||
|
||||
Tama-Go: connection icon, TAMA-GO, VISIT or GAME. Both wait in STAND BY, B on the device that goes visiting sends.
|
||||
The game sends a packet with `$748E` (bank 3): `$32` and `$5B` minus the count, a preamble, then the bytes from `$0220`
|
||||
by switching the infrared LED (`$3016` bit 3). `t7_ir_led_on` (called by `t7_io_write`, outside the hot loop) takes
|
||||
the packet at the first LED switch-on of the preamble (`$32 == $5B`) as 32 bytes with the sum in the last, like the
|
||||
older `t7_ir_bridge_tx_poll` (that one compared `$0220` and lost a packet sent again unchanged). The other device puts
|
||||
it in with `t7_ir_bridge_rx` (the receive hook at `$60C4` of the IR bridge), only while its game is in the connection
|
||||
screen (`t7_ir_activity` changed within 1.5 s), so an old packet does not wait. Between the devices: `T` frames over
|
||||
the UART on the contacts (`../emu/src/emu/plink.h`, 460800 8N1, A5 type len data sum, hello every second).
|
||||
|
||||
Sound must be on: the game waits for its beeps to end (`$A3` bit 7) also during the visit. Checked on the PC with two
|
||||
cores linked in lockstep: visit (guest away, the host shows both, guest home), GAME (YOU WIN / YOU LOSE), the second
|
||||
device entering STAND BY late (the first one sends again).
|
||||
|
||||
## Clock note after a battery change (T7_CK_WRAP, 1.6.11 test1)
|
||||
|
||||
When the game asks for the time, the noted game time is counted on with the Paradise's seconds counter. After a
|
||||
battery change that counter starts at 0 again, below the noted value; the difference wrapped to about 136 years and
|
||||
the game got a date far off. Now the noted time is taken as it was (the clock from the Paradise sets it right after).
|
||||
|
||||
No session (SESS_FLAG, 1.7.3 test7): settings flag bit 12 (0x1000): the run state is never written (leaving, automatic sessions, battery empty) and an old one is dropped at the start; the EEPROM, settings and clock note are stored as before, so the toy starts from its own save.
|
||||
+47
-47
@@ -1,47 +1,47 @@
|
||||
0 3
|
||||
B 0.2
|
||||
0 2
|
||||
B 0.2
|
||||
0 0.6
|
||||
B 0.2
|
||||
0 0.6
|
||||
B 0.2
|
||||
0 0.6
|
||||
A 0.2
|
||||
0 0.6
|
||||
B 0.2
|
||||
0 0.6
|
||||
B 0.2
|
||||
0 0.6
|
||||
B 0.2
|
||||
0 0.6
|
||||
B 0.2
|
||||
0 0.6
|
||||
B 0.2
|
||||
0 0.8
|
||||
B 0.2
|
||||
0 0.8
|
||||
B 0.2
|
||||
0 0.8
|
||||
B 0.2
|
||||
0 0.8
|
||||
B 0.2
|
||||
0 0.8
|
||||
B 0.2
|
||||
0 0.8
|
||||
B 0.2
|
||||
0 0.8
|
||||
B 0.2
|
||||
0 0.8
|
||||
0 120
|
||||
A 0.2
|
||||
0 1
|
||||
B 0.2
|
||||
0 3
|
||||
C 0.2
|
||||
0 2
|
||||
A 0.2
|
||||
A 0.2
|
||||
0 1
|
||||
B 0.2
|
||||
0 10
|
||||
0 3
|
||||
B 0.2
|
||||
0 2
|
||||
B 0.2
|
||||
0 0.6
|
||||
B 0.2
|
||||
0 0.6
|
||||
B 0.2
|
||||
0 0.6
|
||||
A 0.2
|
||||
0 0.6
|
||||
B 0.2
|
||||
0 0.6
|
||||
B 0.2
|
||||
0 0.6
|
||||
B 0.2
|
||||
0 0.6
|
||||
B 0.2
|
||||
0 0.6
|
||||
B 0.2
|
||||
0 0.8
|
||||
B 0.2
|
||||
0 0.8
|
||||
B 0.2
|
||||
0 0.8
|
||||
B 0.2
|
||||
0 0.8
|
||||
B 0.2
|
||||
0 0.8
|
||||
B 0.2
|
||||
0 0.8
|
||||
B 0.2
|
||||
0 0.8
|
||||
B 0.2
|
||||
0 0.8
|
||||
0 120
|
||||
A 0.2
|
||||
0 1
|
||||
B 0.2
|
||||
0 3
|
||||
C 0.2
|
||||
0 2
|
||||
A 0.2
|
||||
A 0.2
|
||||
0 1
|
||||
B 0.2
|
||||
0 10
|
||||
+114
-114
@@ -1,114 +1,114 @@
|
||||
/* PC test of the Paradise T7 app logic (t7app.c): screen, clock, save, catching up, figures.
|
||||
* happ <rom> [figure...] commands on stdin:
|
||||
* w <s> run s seconds (rtc counts along, app_clock every 2 s)
|
||||
* slow <s> run s seconds, but the rtc counts twice as fast (emulation too slow)
|
||||
* p A|B|C|W press (W = wheel short = figure menu)
|
||||
* poke <h> <m> <s> set the game clock
|
||||
* t print the game clock
|
||||
* save <f> / load <f> [pause_s] save state / load it after a pause (catch up)
|
||||
* s <file.ppm> screenshot of the 128x128 screen
|
||||
*/
|
||||
#include "../src/core/t7.h"
|
||||
#include "../src/app/t7app.h"
|
||||
#include <stdio.h>
|
||||
void host_set_rom(unsigned char *p, size_t n);
|
||||
void app_wheel_short(void);
|
||||
static unsigned char *rd(const char *f, size_t *n) { FILE *x = fopen(f, "rb"); fseek(x, 0, 2); *n = ftell(x); fseek(x, 0, 0); unsigned char *p = malloc(*n); if (fread(p, 1, *n, x)) {} fclose(x); return p; }
|
||||
static uint16_t fb[128][128]; static int rx0, rx1, ry;
|
||||
static uint32_t g_rtc = 1000000, g_ms;
|
||||
static T7Cfg cfg = { T7_MAGIC, 1, 0, 0, 0, 0, {{0}}, 0 };
|
||||
static const uint8_t *figs[T7_FIG_MAX];
|
||||
uint32_t plat_rtc(void) { return g_rtc; }
|
||||
void plat_vol(int v) { (void)v; }
|
||||
void plat_bat(char *s) { const char *t = "2.84V 80%"; int i = 0; while ((s[i] = t[i])) i++; }
|
||||
void plat_rect(int x0, int y0, int x1, int y1) { rx0 = x0; rx1 = x1; ry = y0; (void)y1; }
|
||||
void plat_row(const uint16_t *px, int n) { for (int i = 0; i < n && rx0 + i < 128; i++) fb[ry][rx0 + i] = px[i]; ry++; }
|
||||
void plat_rect_end(void) {}
|
||||
const uint8_t *plat_fig_ptr(int i) { return figs[i]; }
|
||||
static uint8_t store[T7_STORE_LEN]; static int have_store;
|
||||
int plat_store_load(uint8_t *b, uint32_t n) { if (!have_store) return 0; memcpy(b, store, n); return 1; }
|
||||
void plat_store_write(const uint8_t *b, uint32_t n) { memcpy(store, b, n); have_store = 1; printf("store: bg=%d size=%d pixel=%d note=%d\n", b[4], b[8], b[9], b[32] == 'T'); }
|
||||
static int keys, dark;
|
||||
extern uint8_t g_t7_dark; int app_bell(void);
|
||||
static int icon9_px(void) { int n = 0; for (int y = 106; y < 126; y++) for (int x = 4*128/5 + 2; x < 4*128/5 + 22; x++) if (fb[y][x] != fb[105][0]) n = fb[y][x]; return n; }
|
||||
static void run(double s, int rtcx)
|
||||
{
|
||||
int n = (int)(s * 500 + 0.5);
|
||||
for (int i = 0; i < n; i++) {
|
||||
g_ms += 2;
|
||||
t7_set_buttons((uint8_t)app_keys((uint32_t)keys, g_ms));
|
||||
app_tick(g_ms);
|
||||
t7_step_cycles(T7_CLOCK_RATE / 500);
|
||||
{ static int pt; static uint32_t lt; uint8_t lv; int tn = t7_snd_tone_hz(&lv) != 0; if (tn && !pt) { if (g_ms - lt > 5000) { uint8_t ic[10]; t7_get_icons(ic); printf("TONE %02d:%02d ic %d calls %02x %02x %02x %02x %02x %02x %02x hh %02x %02x\n", t7_ram_read(0x306), t7_ram_read(0x307), ic[9], t7_ram_read(0x3b0), t7_ram_read(0x3b1), t7_ram_read(0x3b2), t7_ram_read(0x3b3), t7_ram_read(0x3b4), t7_ram_read(0x3b5), t7_ram_read(0x3b6), t7_ram_read(0x326), t7_ram_read(0x327)); } lt = g_ms; } pt = tn; }
|
||||
{ static uint32_t cs; static int pc; int c = t7_ram_read(0x3b0) | t7_ram_read(0x3b1) | t7_ram_read(0x3b2) | t7_ram_read(0x3b4) | t7_ram_read(0x3b5) | t7_ram_read(0x3b6); uint8_t ic[10]; t7_get_icons(ic);
|
||||
if (c && !pc) cs = g_ms; if (cs && ic[9]) { printf("CALL->ICON %u ms\n", g_ms - cs); cs = 0; } if (!c) cs = 0; pc = c != 0; }
|
||||
if (g_ms % 1000 == 0) g_rtc += (uint32_t)rtcx;
|
||||
if (g_ms % 500 == 0) app_setup_fill(g_rtc);
|
||||
if (g_ms % 2000 == 0) app_clock(g_rtc, 0);
|
||||
if (dark) { g_t7_dark = 1; if (app_bell()) { dark = 0; g_t7_dark = 0; app_render(1); printf("bell wake at %02d:%02d px %d\n", t7_ram_read(0x306), t7_ram_read(0x307), icon9_px()); } continue; }
|
||||
g_t7_dark = 0;
|
||||
if (g_ms % 34 == 0) { app_render(0); static int pb; int b = app_bell(); if (b != pb) { printf("bell %d at %02d:%02d px %d\n", b, t7_ram_read(0x306), t7_ram_read(0x307), icon9_px()); pb = b; } }
|
||||
}
|
||||
}
|
||||
static void shot(const char *f)
|
||||
{
|
||||
app_render(0);
|
||||
FILE *o = fopen(f, "wb"); fprintf(o, "P6 128 128 255\n");
|
||||
for (int y = 0; y < 128; y++) for (int x = 0; x < 128; x++) { uint16_t p = fb[y][x]; fputc((p >> 11) << 3, o); fputc(((p >> 5) & 63) << 2, o); fputc((p & 31) << 3, o); }
|
||||
fclose(o);
|
||||
}
|
||||
static void prog(int d, int t) { if (d == t) printf("caught up %d hours\n", t); }
|
||||
int main(int c, char **v)
|
||||
{
|
||||
size_t n; unsigned char *rom = rd(v[1], &n); host_set_rom(rom, n);
|
||||
for (int i = 2; i < c && i - 2 < T7_FIG_MAX; i++) {
|
||||
size_t fn; figs[i - 2] = rd(v[i], &fn); cfg.fig[i - 2].len = (uint32_t)fn;
|
||||
const char *b = strrchr(v[i], '_'); b = b ? b + 1 : v[i]; int k = 0;
|
||||
while (b[k] && b[k] != '.' && k < 15) { cfg.fig[i - 2].name[k] = b[k]; k++; }
|
||||
cfg.nfig++;
|
||||
}
|
||||
if (getenv("EE")) { size_t en; unsigned char *e = rd(getenv("EE"), &en); size_t m2 = en; extern void persist_restore_page(int, const uint8_t *); for (int i = 0; i < 16 && (size_t)(i + 1) * 256 <= en; i++) persist_restore_page(i, e + (m2 == 4098 ? 2 : 0) + i * 256); } /* EE=save (Studio save.bin: u16 have first) */
|
||||
app_init(&cfg);
|
||||
t7_init();
|
||||
t7_snd_config(T7_CLOCK_RATE, 16000); t7_snd_capture(0); t7_snd_set_mode(1);
|
||||
app_render(1);
|
||||
char l[256];
|
||||
while (fgets(l, sizeof l, stdin)) {
|
||||
char cmd[16], a[200]; double x = 0; int k = sscanf(l, "%15s %199s %lf", cmd, a, &x);
|
||||
if (k < 1) continue;
|
||||
if (!strcmp(cmd, "w")) run(atof(a), 1);
|
||||
else if (!strcmp(cmd, "slow")) run(atof(a), 2);
|
||||
else if (!strcmp(cmd, "p")) {
|
||||
if (a[0] == 'W') { app_wheel_short(); run(0.3, 1); }
|
||||
else { keys = a[0] == 'A' ? 1 : a[0] == 'B' ? 2 : 4; run(0.2, 1); keys = 0; run(k > 2 ? x : 0.8, 1); }
|
||||
}
|
||||
else if (!strcmp(cmd, "poke")) { int h, m, s2; sscanf(l, "%*s %d %d %d", &h, &m, &s2); t7_ram_write(0x306, h); t7_ram_write(0x307, m); t7_ram_write(t7_rtc_sec_addr(), s2); }
|
||||
else if (!strcmp(cmd, "t")) printf("game %02d:%02d:%02d day %d.%d rtc %u fig %d species %02x\n", t7_ram_read(0x306), t7_ram_read(0x307), t7_ram_read(t7_rtc_sec_addr()), t7_ram_read(0x305), t7_ram_read(0x304), g_rtc, t7_figure_get(), t7_ram_read(0x22));
|
||||
else if (!strcmp(cmd, "save")) { static uint8_t b[4096]; uint32_t m = app_save(b, sizeof b); FILE *o = fopen(a, "wb"); fwrite(b, 1, m, o); fclose(o); printf("saved %u bytes\n", m); }
|
||||
else if (!strcmp(cmd, "load")) {
|
||||
size_t m; unsigned char *b = rd(a, &m); uint32_t sv;
|
||||
g_rtc += (uint32_t)x;
|
||||
t7_init();
|
||||
printf("load %s\n", app_load(b, (uint32_t)m, &sv) ? "ok" : "FAILED");
|
||||
if (app_clock_hard(g_rtc)) printf("clock from the Paradise\n"); else app_catchup(g_rtc, prog);
|
||||
}
|
||||
else if (!strcmp(cmd, "i")) { uint8_t ic[10]; t7_get_icons(ic); printf("icons"); for (int q = 0; q < 10; q++) printf(" %d", ic[q]); printf(" ram326 %02x %02x 3b0", t7_ram_read(0x326), t7_ram_read(0x327)); for (int q = 0; q < 7; q++) printf(" %02x", t7_ram_read((uint16_t)(0x3b0 + q))); printf(" t %02d:%02d\n", t7_ram_read(0x306), t7_ram_read(0x307)); }
|
||||
else if (!strcmp(cmd, "s")) shot(a);
|
||||
else if (!strcmp(cmd, "para")) { /* para <y> <m> <d> <h> <mi>: the Paradise's time now, clock flag on */
|
||||
int y, mo, d, h, mi; sscanf(l, "%*s %d %d %d %d %d", &y, &mo, &d, &h, &mi);
|
||||
int yy = y - (mo <= 2), era = yy / 400, yoe = yy - era * 400, doy = (153 * (mo + (mo > 2 ? -3 : 9)) + 2) / 5 + d - 1, doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
|
||||
g_para_time = (uint32_t)(era * 146097 + doe - 719468) * 86400u + (uint32_t)(h * 3600 + mi * 60); g_para_cnt = g_rtc; cfg.flags |= 2u;
|
||||
}
|
||||
else if (!strcmp(cmd, "wall")) { int h, m, d, mo; sscanf(l, "%*s %d %d %d %d", &h, &m, &d, &mo); cfg.wall_cnt = g_rtc; cfg.wall_sod = (uint32_t)(h * 3600 + m * 60); cfg.wall_ymd = (2026u << 16) | (uint32_t)(mo << 8 | d); }
|
||||
else if (!strcmp(cmd, "store")) app_store();
|
||||
else if (!strcmp(cmd, "set")) { extern T7Set g_set; int sz = 0, px = 0, gr = -1; sscanf(l, "%*s %d %d %d", &sz, &px, &gr); g_set.size = (uint8_t)sz; g_set.pixel = (uint8_t)px; if (gr >= 0) g_set.grid = (uint8_t)gr; app_dim(1); app_dim(0); } /* set <size> <pixel> [grid] */
|
||||
else if (!strcmp(cmd, "reboot")) { t7_init(); app_init(&cfg); app_render(1); }
|
||||
else if (!strcmp(cmd, "wb")) { unsigned ad, v; sscanf(l, "%*s %x %x", &ad, &v); t7_ram_write((uint16_t)ad, (uint8_t)v); } /* test30 */
|
||||
else if (!strcmp(cmd, "dump")) { FILE *o = fopen(a, "wb"); for (int i = 0; i < 0x600; i++) fputc(t7_ram_read((uint16_t)i), o); fclose(o); }
|
||||
else if (!strcmp(cmd, "dark")) dark = atoi(a);
|
||||
else if (!strcmp(cmd, "noclk")) cfg.flags &= ~2u; /* clock not from the Paradise */
|
||||
else if (!strcmp(cmd, "q")) break;
|
||||
fflush(stdout);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
/* PC test of the Paradise T7 app logic (t7app.c): screen, clock, save, catching up, figures.
|
||||
* happ <rom> [figure...] commands on stdin:
|
||||
* w <s> run s seconds (rtc counts along, app_clock every 2 s)
|
||||
* slow <s> run s seconds, but the rtc counts twice as fast (emulation too slow)
|
||||
* p A|B|C|W press (W = wheel short = figure menu)
|
||||
* poke <h> <m> <s> set the game clock
|
||||
* t print the game clock
|
||||
* save <f> / load <f> [pause_s] save state / load it after a pause (catch up)
|
||||
* s <file.ppm> screenshot of the 128x128 screen
|
||||
*/
|
||||
#include "../src/core/t7.h"
|
||||
#include "../src/app/t7app.h"
|
||||
#include <stdio.h>
|
||||
void host_set_rom(unsigned char *p, size_t n);
|
||||
void app_wheel_short(void);
|
||||
static unsigned char *rd(const char *f, size_t *n) { FILE *x = fopen(f, "rb"); fseek(x, 0, 2); *n = ftell(x); fseek(x, 0, 0); unsigned char *p = malloc(*n); if (fread(p, 1, *n, x)) {} fclose(x); return p; }
|
||||
static uint16_t fb[128][128]; static int rx0, rx1, ry;
|
||||
static uint32_t g_rtc = 1000000, g_ms;
|
||||
static T7Cfg cfg = { T7_MAGIC, 1, 0, 0, 0, 0, {{0}}, 0 };
|
||||
static const uint8_t *figs[T7_FIG_MAX];
|
||||
uint32_t plat_rtc(void) { return g_rtc; }
|
||||
void plat_vol(int v) { (void)v; }
|
||||
void plat_bat(char *s) { const char *t = "2.84V 80%"; int i = 0; while ((s[i] = t[i])) i++; }
|
||||
void plat_rect(int x0, int y0, int x1, int y1) { rx0 = x0; rx1 = x1; ry = y0; (void)y1; }
|
||||
void plat_row(const uint16_t *px, int n) { for (int i = 0; i < n && rx0 + i < 128; i++) fb[ry][rx0 + i] = px[i]; ry++; }
|
||||
void plat_rect_end(void) {}
|
||||
const uint8_t *plat_fig_ptr(int i) { return figs[i]; }
|
||||
static uint8_t store[T7_STORE_LEN]; static int have_store;
|
||||
int plat_store_load(uint8_t *b, uint32_t n) { if (!have_store) return 0; memcpy(b, store, n); return 1; }
|
||||
void plat_store_write(const uint8_t *b, uint32_t n) { memcpy(store, b, n); have_store = 1; printf("store: bg=%d size=%d pixel=%d note=%d\n", b[4], b[8], b[9], b[32] == 'T'); }
|
||||
static int keys, dark;
|
||||
extern uint8_t g_t7_dark; int app_bell(void);
|
||||
static int icon9_px(void) { int n = 0; for (int y = 106; y < 126; y++) for (int x = 4*128/5 + 2; x < 4*128/5 + 22; x++) if (fb[y][x] != fb[105][0]) n = fb[y][x]; return n; }
|
||||
static void run(double s, int rtcx)
|
||||
{
|
||||
int n = (int)(s * 500 + 0.5);
|
||||
for (int i = 0; i < n; i++) {
|
||||
g_ms += 2;
|
||||
t7_set_buttons((uint8_t)app_keys((uint32_t)keys, g_ms));
|
||||
app_tick(g_ms);
|
||||
t7_step_cycles(T7_CLOCK_RATE / 500);
|
||||
{ static int pt; static uint32_t lt; uint8_t lv; int tn = t7_snd_tone_hz(&lv) != 0; if (tn && !pt) { if (g_ms - lt > 5000) { uint8_t ic[10]; t7_get_icons(ic); printf("TONE %02d:%02d ic %d calls %02x %02x %02x %02x %02x %02x %02x hh %02x %02x\n", t7_ram_read(0x306), t7_ram_read(0x307), ic[9], t7_ram_read(0x3b0), t7_ram_read(0x3b1), t7_ram_read(0x3b2), t7_ram_read(0x3b3), t7_ram_read(0x3b4), t7_ram_read(0x3b5), t7_ram_read(0x3b6), t7_ram_read(0x326), t7_ram_read(0x327)); } lt = g_ms; } pt = tn; }
|
||||
{ static uint32_t cs; static int pc; int c = t7_ram_read(0x3b0) | t7_ram_read(0x3b1) | t7_ram_read(0x3b2) | t7_ram_read(0x3b4) | t7_ram_read(0x3b5) | t7_ram_read(0x3b6); uint8_t ic[10]; t7_get_icons(ic);
|
||||
if (c && !pc) cs = g_ms; if (cs && ic[9]) { printf("CALL->ICON %u ms\n", g_ms - cs); cs = 0; } if (!c) cs = 0; pc = c != 0; }
|
||||
if (g_ms % 1000 == 0) g_rtc += (uint32_t)rtcx;
|
||||
if (g_ms % 500 == 0) app_setup_fill(g_rtc);
|
||||
if (g_ms % 2000 == 0) app_clock(g_rtc, 0);
|
||||
if (dark) { g_t7_dark = 1; if (app_bell()) { dark = 0; g_t7_dark = 0; app_render(1); printf("bell wake at %02d:%02d px %d\n", t7_ram_read(0x306), t7_ram_read(0x307), icon9_px()); } continue; }
|
||||
g_t7_dark = 0;
|
||||
if (g_ms % 34 == 0) { app_render(0); static int pb; int b = app_bell(); if (b != pb) { printf("bell %d at %02d:%02d px %d\n", b, t7_ram_read(0x306), t7_ram_read(0x307), icon9_px()); pb = b; } }
|
||||
}
|
||||
}
|
||||
static void shot(const char *f)
|
||||
{
|
||||
app_render(0);
|
||||
FILE *o = fopen(f, "wb"); fprintf(o, "P6 128 128 255\n");
|
||||
for (int y = 0; y < 128; y++) for (int x = 0; x < 128; x++) { uint16_t p = fb[y][x]; fputc((p >> 11) << 3, o); fputc(((p >> 5) & 63) << 2, o); fputc((p & 31) << 3, o); }
|
||||
fclose(o);
|
||||
}
|
||||
static void prog(int d, int t) { if (d == t) printf("caught up %d hours\n", t); }
|
||||
int main(int c, char **v)
|
||||
{
|
||||
size_t n; unsigned char *rom = rd(v[1], &n); host_set_rom(rom, n);
|
||||
for (int i = 2; i < c && i - 2 < T7_FIG_MAX; i++) {
|
||||
size_t fn; figs[i - 2] = rd(v[i], &fn); cfg.fig[i - 2].len = (uint32_t)fn;
|
||||
const char *b = strrchr(v[i], '_'); b = b ? b + 1 : v[i]; int k = 0;
|
||||
while (b[k] && b[k] != '.' && k < 15) { cfg.fig[i - 2].name[k] = b[k]; k++; }
|
||||
cfg.nfig++;
|
||||
}
|
||||
if (getenv("EE")) { size_t en; unsigned char *e = rd(getenv("EE"), &en); size_t m2 = en; extern void persist_restore_page(int, const uint8_t *); for (int i = 0; i < 16 && (size_t)(i + 1) * 256 <= en; i++) persist_restore_page(i, e + (m2 == 4098 ? 2 : 0) + i * 256); } /* EE=save (Studio save.bin: u16 have first) */
|
||||
app_init(&cfg);
|
||||
t7_init();
|
||||
t7_snd_config(T7_CLOCK_RATE, 16000); t7_snd_capture(0); t7_snd_set_mode(1);
|
||||
app_render(1);
|
||||
char l[256];
|
||||
while (fgets(l, sizeof l, stdin)) {
|
||||
char cmd[16], a[200]; double x = 0; int k = sscanf(l, "%15s %199s %lf", cmd, a, &x);
|
||||
if (k < 1) continue;
|
||||
if (!strcmp(cmd, "w")) run(atof(a), 1);
|
||||
else if (!strcmp(cmd, "slow")) run(atof(a), 2);
|
||||
else if (!strcmp(cmd, "p")) {
|
||||
if (a[0] == 'W') { app_wheel_short(); run(0.3, 1); }
|
||||
else { keys = a[0] == 'A' ? 1 : a[0] == 'B' ? 2 : 4; run(0.2, 1); keys = 0; run(k > 2 ? x : 0.8, 1); }
|
||||
}
|
||||
else if (!strcmp(cmd, "poke")) { int h, m, s2; sscanf(l, "%*s %d %d %d", &h, &m, &s2); t7_ram_write(0x306, h); t7_ram_write(0x307, m); t7_ram_write(t7_rtc_sec_addr(), s2); }
|
||||
else if (!strcmp(cmd, "t")) printf("game %02d:%02d:%02d day %d.%d rtc %u fig %d species %02x\n", t7_ram_read(0x306), t7_ram_read(0x307), t7_ram_read(t7_rtc_sec_addr()), t7_ram_read(0x305), t7_ram_read(0x304), g_rtc, t7_figure_get(), t7_ram_read(0x22));
|
||||
else if (!strcmp(cmd, "save")) { static uint8_t b[4096]; uint32_t m = app_save(b, sizeof b); FILE *o = fopen(a, "wb"); fwrite(b, 1, m, o); fclose(o); printf("saved %u bytes\n", m); }
|
||||
else if (!strcmp(cmd, "load")) {
|
||||
size_t m; unsigned char *b = rd(a, &m); uint32_t sv;
|
||||
g_rtc += (uint32_t)x;
|
||||
t7_init();
|
||||
printf("load %s\n", app_load(b, (uint32_t)m, &sv) ? "ok" : "FAILED");
|
||||
if (app_clock_hard(g_rtc)) printf("clock from the Paradise\n"); else app_catchup(g_rtc, prog);
|
||||
}
|
||||
else if (!strcmp(cmd, "i")) { uint8_t ic[10]; t7_get_icons(ic); printf("icons"); for (int q = 0; q < 10; q++) printf(" %d", ic[q]); printf(" ram326 %02x %02x 3b0", t7_ram_read(0x326), t7_ram_read(0x327)); for (int q = 0; q < 7; q++) printf(" %02x", t7_ram_read((uint16_t)(0x3b0 + q))); printf(" t %02d:%02d\n", t7_ram_read(0x306), t7_ram_read(0x307)); }
|
||||
else if (!strcmp(cmd, "s")) shot(a);
|
||||
else if (!strcmp(cmd, "para")) { /* para <y> <m> <d> <h> <mi>: the Paradise's time now, clock flag on */
|
||||
int y, mo, d, h, mi; sscanf(l, "%*s %d %d %d %d %d", &y, &mo, &d, &h, &mi);
|
||||
int yy = y - (mo <= 2), era = yy / 400, yoe = yy - era * 400, doy = (153 * (mo + (mo > 2 ? -3 : 9)) + 2) / 5 + d - 1, doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
|
||||
g_para_time = (uint32_t)(era * 146097 + doe - 719468) * 86400u + (uint32_t)(h * 3600 + mi * 60); g_para_cnt = g_rtc; cfg.flags |= 2u;
|
||||
}
|
||||
else if (!strcmp(cmd, "wall")) { int h, m, d, mo; sscanf(l, "%*s %d %d %d %d", &h, &m, &d, &mo); cfg.wall_cnt = g_rtc; cfg.wall_sod = (uint32_t)(h * 3600 + m * 60); cfg.wall_ymd = (2026u << 16) | (uint32_t)(mo << 8 | d); }
|
||||
else if (!strcmp(cmd, "store")) app_store();
|
||||
else if (!strcmp(cmd, "set")) { extern T7Set g_set; int sz = 0, px = 0, gr = -1; sscanf(l, "%*s %d %d %d", &sz, &px, &gr); g_set.size = (uint8_t)sz; g_set.pixel = (uint8_t)px; if (gr >= 0) g_set.grid = (uint8_t)gr; app_dim(1); app_dim(0); } /* set <size> <pixel> [grid] */
|
||||
else if (!strcmp(cmd, "reboot")) { t7_init(); app_init(&cfg); app_render(1); }
|
||||
else if (!strcmp(cmd, "wb")) { unsigned ad, v; sscanf(l, "%*s %x %x", &ad, &v); t7_ram_write((uint16_t)ad, (uint8_t)v); } /* test30 */
|
||||
else if (!strcmp(cmd, "dump")) { FILE *o = fopen(a, "wb"); for (int i = 0; i < 0x600; i++) fputc(t7_ram_read((uint16_t)i), o); fclose(o); }
|
||||
else if (!strcmp(cmd, "dark")) dark = atoi(a);
|
||||
else if (!strcmp(cmd, "noclk")) cfg.flags &= ~2u; /* clock not from the Paradise */
|
||||
else if (!strcmp(cmd, "q")) break;
|
||||
fflush(stdout);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
+22
-22
@@ -1,22 +1,22 @@
|
||||
/* Host build of the Paradise T7 app glue: ROM from a file, EEPROM pages in memory. */
|
||||
#include "../src/core/pebble_compat.h"
|
||||
#include <stdarg.h>
|
||||
static unsigned char *g_rom; static size_t g_rom_n;
|
||||
static unsigned char g_pages[16][256]; static unsigned char g_has[16];
|
||||
void host_set_rom(unsigned char *p, size_t n){ g_rom=p; g_rom_n=n; }
|
||||
void tama_log(const char *fmt, ...){ if(!getenv("T7LOG")) return; va_list a; va_start(a,fmt); vfprintf(stderr,fmt,a); va_end(a); }
|
||||
ResHandle resource_get_handle(int id){ (void)id; return g_rom?1:0; }
|
||||
size_t resource_size(ResHandle h){ (void)h; return g_rom_n; }
|
||||
size_t resource_load_byte_range(ResHandle h, size_t off, uint8_t *out, size_t len){ (void)h; if(off>=g_rom_n) return 0; if(off+len>g_rom_n) len=g_rom_n-off; memcpy(out,g_rom+off,len); return len; }
|
||||
const uint8_t *t7_compat_rom_mapped(void){ return g_rom; }
|
||||
bool persist_exists(uint32_t key){ return key>=100 && key<116 && g_has[key-100]; }
|
||||
int persist_read_data(uint32_t key, void *buf, size_t n){ if(!persist_exists(key)) return -1; if(n>256) n=256; memcpy(buf,g_pages[key-100],n); return (int)n; }
|
||||
status_t persist_write_data(uint32_t key, const void *buf, size_t n){ if(key<100||key>=116) return -1; if(n>256) n=256; memcpy(g_pages[key-100],buf,n); g_has[key-100]=1; return S_SUCCESS; }
|
||||
status_t persist_delete(uint32_t key){ if(key>=100&&key<116) g_has[key-100]=0; return S_SUCCESS; }
|
||||
void persist_flush_to_disk(void){}
|
||||
int persist_page_count(void){ return 16; }
|
||||
int persist_page_size(void){ return 256; }
|
||||
int persist_backup_page(int p, uint8_t *o){ memcpy(o,g_pages[p],256); return g_has[p]; }
|
||||
void persist_restore_page(int p, const uint8_t *i){ memcpy(g_pages[p],i,256); g_has[p]=1; }
|
||||
void t7_compat_set_rom_path(const char *p){ (void)p; }
|
||||
void t7_compat_set_eeprom_path(const char *p){ (void)p; }
|
||||
/* Host build of the Paradise T7 app glue: ROM from a file, EEPROM pages in memory. */
|
||||
#include "../src/core/pebble_compat.h"
|
||||
#include <stdarg.h>
|
||||
static unsigned char *g_rom; static size_t g_rom_n;
|
||||
static unsigned char g_pages[16][256]; static unsigned char g_has[16];
|
||||
void host_set_rom(unsigned char *p, size_t n){ g_rom=p; g_rom_n=n; }
|
||||
void tama_log(const char *fmt, ...){ if(!getenv("T7LOG")) return; va_list a; va_start(a,fmt); vfprintf(stderr,fmt,a); va_end(a); }
|
||||
ResHandle resource_get_handle(int id){ (void)id; return g_rom?1:0; }
|
||||
size_t resource_size(ResHandle h){ (void)h; return g_rom_n; }
|
||||
size_t resource_load_byte_range(ResHandle h, size_t off, uint8_t *out, size_t len){ (void)h; if(off>=g_rom_n) return 0; if(off+len>g_rom_n) len=g_rom_n-off; memcpy(out,g_rom+off,len); return len; }
|
||||
const uint8_t *t7_compat_rom_mapped(void){ return g_rom; }
|
||||
bool persist_exists(uint32_t key){ return key>=100 && key<116 && g_has[key-100]; }
|
||||
int persist_read_data(uint32_t key, void *buf, size_t n){ if(!persist_exists(key)) return -1; if(n>256) n=256; memcpy(buf,g_pages[key-100],n); return (int)n; }
|
||||
status_t persist_write_data(uint32_t key, const void *buf, size_t n){ if(key<100||key>=116) return -1; if(n>256) n=256; memcpy(g_pages[key-100],buf,n); g_has[key-100]=1; return S_SUCCESS; }
|
||||
status_t persist_delete(uint32_t key){ if(key>=100&&key<116) g_has[key-100]=0; return S_SUCCESS; }
|
||||
void persist_flush_to_disk(void){}
|
||||
int persist_page_count(void){ return 16; }
|
||||
int persist_page_size(void){ return 256; }
|
||||
int persist_backup_page(int p, uint8_t *o){ memcpy(o,g_pages[p],256); return g_has[p]; }
|
||||
void persist_restore_page(int p, const uint8_t *i){ memcpy(g_pages[p],i,256); g_has[p]=1; }
|
||||
void t7_compat_set_rom_path(const char *p){ (void)p; }
|
||||
void t7_compat_set_eeprom_path(const char *p){ (void)p; }
|
||||
+81
-81
@@ -1,81 +1,81 @@
|
||||
/* Host: boot the V7 ROM, press keys, save screenshots. hrun <rom> [figure]; stdin: p A|B|C [s] | w s | s file | q */
|
||||
#include "../src/core/t7.h"
|
||||
#include <stdio.h>
|
||||
#include "../src/core/t7_internal.h"
|
||||
void host_set_rom(unsigned char *p, size_t n);
|
||||
void figure_rom_set_active(const unsigned char* p, unsigned int n);
|
||||
static unsigned char *rd(const char *f, size_t *n){ FILE*x=fopen(f,"rb"); fseek(x,0,2); *n=ftell(x); fseek(x,0,0); unsigned char*p=malloc(*n); if(fread(p,1,*n,x)){} fclose(x); return p; }
|
||||
#ifdef T7_STOP_DBG
|
||||
static long g_step; void t7_stop_dbg(int w){ static int n; if (getenv("SDBG") && n++ < 400) fprintf(stderr, "step %ld STOP 3007=%02x 3008=%02x keys=%x halted=%d irqmask=%02x%02x nmi=%02x\n", g_step, g_cpureg[7], g_cpureg[8], g_keys, g_t7_halted, g_cpureg[0x70], g_cpureg[0x71], g_cpureg[0x76]); (void)w; }
|
||||
#endif
|
||||
#ifdef T7_EEDBG
|
||||
static double g_now; void t7_eedbg(uint16_t a, uint8_t v){ static double last=-1; if (getenv("EEDBG") && g_now != last) { fprintf(stderr, "ee write at %.2f s: %03x=%02x\n", g_now, a, v); last = g_now; } }
|
||||
#endif
|
||||
static void frames(double s){ static int cyc; if(!cyc) cyc=getenv("CYC")?atoi(getenv("CYC")):40000; int n=(int)(s*100+0.5); for(int i=0;i<n;i++) { t7_step_cycles(cyc);
|
||||
#ifdef T7_EEDBG
|
||||
g_now += 0.01;
|
||||
#endif
|
||||
} }
|
||||
static void shot(const char *f){ uint8_t d[48*31], ic[10]; t7_get_display(d); t7_get_icons(ic);
|
||||
static const int pal[4]={0xE0,0xA0,0x60,0x20}; FILE*o=fopen(f,"wb"); fprintf(o,"P6 192 140 255\n");
|
||||
for(int y=0;y<140;y++) for(int x=0;x<192;x++){ int v=0xF0;
|
||||
if(y>=8&&y<132){ int yy=(y-8)/4, xx=x/4; if(yy<31) v=pal[d[yy*48+xx]]; }
|
||||
else { int k=x/(192/5)+(y>=132?5:0); v=ic[k]?pal[ic[k]]:0xF0; }
|
||||
fputc(v,o); fputc(v,o); fputc(v,o);} fclose(o); }
|
||||
int main(int c, char **v){ size_t n; unsigned char *rom=rd(v[1],&n); host_set_rom(rom,n);
|
||||
if(c>2){ size_t fn; unsigned char*f=rd(v[2],&fn); figure_rom_set_active(f,(unsigned)fn); t7_figure_set(1); }
|
||||
if (getenv("EE")) { size_t en; unsigned char *e=rd(getenv("EE"),&en); extern void persist_restore_page(int,const uint8_t*); for (int i=0;i<16 && (size_t)(i+1)*256<=en;i++) persist_restore_page(i,e+i*256); } /* EE=save: EEPROM from a file */
|
||||
if(!t7_init()){ printf("init failed\n"); return 1; }
|
||||
if (getenv("SND")) { extern void t7_snd_set_mode(int); t7_snd_set_mode(1); }
|
||||
#ifdef T7_JSR_HLE
|
||||
if (getenv("DARK")) g_t7_dark = 1; /* screen off as on the device */
|
||||
#endif
|
||||
char l[256]; while(fgets(l,sizeof l,stdin)){ char cmd[16],a[200]; double x=0; int k=sscanf(l,"%15s %199s %lf",cmd,a,&x); if(k<1) continue;
|
||||
if(!strcmp(cmd,"p")){ t7_set_buttons(a[0]=='A'?1:a[0]=='B'?2:4); frames(0.2); t7_set_buttons(0); frames(k>2?x:0.8); }
|
||||
else if(!strcmp(cmd,"w")) frames(atof(a));
|
||||
else if(!strcmp(cmd,"tl")){ /* tl <s>: tone list in 2 ms steps (hz x steps) */ extern uint32_t t7_snd_tone_hz(uint8_t*); uint32_t last=0xFFFFFFFF; int n=0; int m=(int)(atof(a)*500+0.5);
|
||||
for(int i=0;i<m;i++){ t7_step_cycles(8000); uint8_t lv; uint32_t hz=t7_snd_tone_hz(&lv); if(hz!=last){ if(n) printf("%u x%d\n",last,n); last=hz; n=0; } n++; } printf("%u x%d\nEND\n",last,n); }
|
||||
else if(!strcmp(cmd,"k")){ double g=0; sscanf(l,"%*s %*s %lf %lf",&x,&g); int m=a[0]=='A'?1:a[0]=='B'?2:4; /* k A hold gap: a quick press in 2 ms steps */
|
||||
t7_set_buttons(m); for(int i=0;i<(int)(x*500+0.5);i++) { t7_step_cycles(8000);
|
||||
#ifdef T7_STOP_DBG
|
||||
g_step++; if (getenv("TL")) fputc(g_t7_halted ? 'h' : 'r', stderr);
|
||||
#endif
|
||||
}
|
||||
#ifdef T7_STOP_DBG
|
||||
if (getenv("TL")) fputc('|', stderr);
|
||||
#endif
|
||||
t7_set_buttons(0); for(int i=0;i<(int)(g*500+0.5);i++) { t7_step_cycles(8000);
|
||||
#ifdef T7_STOP_DBG
|
||||
if (getenv("TL")) fputc(g_t7_halted ? 'H' : 'R', stderr);
|
||||
#endif
|
||||
}
|
||||
#ifdef T7_STOP_DBG
|
||||
if (getenv("TL")) fputc('\n', stderr);
|
||||
#endif
|
||||
}
|
||||
#ifdef T7_JSR_HLE
|
||||
else if(!strcmp(cmd,"dk")){ g_t7_dark=(uint8_t)atoi(a); } /* dk 1|0: screen off/on */
|
||||
#ifdef T7_WAKE_FRESH
|
||||
else if(!strcmp(cmd,"wake")){ extern uint32_t g_t7_blits; uint32_t b0=g_t7_blits, bt=0; int t=0; g_t7_dark=0; /* wake: on as T7_WAKE_FRESH (light after a drawn picture) */
|
||||
for(;;){ t7_step_cycles(8000); t+=2; if(g_t7_blits!=b0){ b0=g_t7_blits; bt=t; } if((bt && t-bt>=20) || t>=700) break; }
|
||||
printf("wake light after %d ms\n", t); }
|
||||
else if(!strcmp(cmd,"peek")){ extern uint32_t g_t7_blits; uint32_t b0=g_t7_blits, bt=0; int t=0; g_t7_dark=0; /* peek: one picture with the screen dark, then dark again */
|
||||
for(;;){ t7_step_cycles(8000); t+=2; if(g_t7_blits!=b0){ b0=g_t7_blits; bt=t; } if((bt && t-bt>=20) || t>=700) break; }
|
||||
g_t7_dark=1; shot(a); printf("peek %d ms\n", t); }
|
||||
#endif
|
||||
#endif
|
||||
else if(!strcmp(cmd,"i")){ uint8_t ic[10]; t7_get_icons(ic); printf("icons"); for(int i=0;i<10;i++) printf(" %d",ic[i]); printf("\n"); }
|
||||
else if(!strcmp(cmd,"s")) shot(a);
|
||||
else if(!strcmp(cmd,"t")) printf("time %02d:%02d:%02d day %d.%d\n", t7_ram_read(0x306),t7_ram_read(0x307),t7_ram_read(t7_rtc_sec_addr()),t7_ram_read(0x305),t7_ram_read(0x304));
|
||||
else if(!strcmp(cmd,"poke")){ t7_ram_write((uint16_t)strtol(a,0,16),(uint8_t)x); }
|
||||
else if(!strcmp(cmd,"d")){ FILE*o=fopen(a,"wb"); fwrite(g_wram,1,T7_WRAM_SIZE,o); fclose(o); }
|
||||
else if(!strcmp(cmd,"r")){ int a0=(int)strtol(a,0,16), m=x>0?(int)x:1; printf("ram %03x:",a0); for(int i=0;i<m;i++) printf(" %02x",t7_ram_read((uint16_t)(a0+i))); printf("\n"); }
|
||||
#ifdef T7_BLIT_CACHE
|
||||
else if(!strcmp(cmd,"bc")){ extern uint32_t g_bc_hit, g_bc_miss; printf("blit cache hit %u miss %u\n", g_bc_hit, g_bc_miss);
|
||||
#ifdef T7_BC_LOG
|
||||
{ extern void bc_print(void); bc_print(); }
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
else if(!strcmp(cmd,"q")) break; fflush(stdout); }
|
||||
return 0; }
|
||||
/* Host: boot the V7 ROM, press keys, save screenshots. hrun <rom> [figure]; stdin: p A|B|C [s] | w s | s file | q */
|
||||
#include "../src/core/t7.h"
|
||||
#include <stdio.h>
|
||||
#include "../src/core/t7_internal.h"
|
||||
void host_set_rom(unsigned char *p, size_t n);
|
||||
void figure_rom_set_active(const unsigned char* p, unsigned int n);
|
||||
static unsigned char *rd(const char *f, size_t *n){ FILE*x=fopen(f,"rb"); fseek(x,0,2); *n=ftell(x); fseek(x,0,0); unsigned char*p=malloc(*n); if(fread(p,1,*n,x)){} fclose(x); return p; }
|
||||
#ifdef T7_STOP_DBG
|
||||
static long g_step; void t7_stop_dbg(int w){ static int n; if (getenv("SDBG") && n++ < 400) fprintf(stderr, "step %ld STOP 3007=%02x 3008=%02x keys=%x halted=%d irqmask=%02x%02x nmi=%02x\n", g_step, g_cpureg[7], g_cpureg[8], g_keys, g_t7_halted, g_cpureg[0x70], g_cpureg[0x71], g_cpureg[0x76]); (void)w; }
|
||||
#endif
|
||||
#ifdef T7_EEDBG
|
||||
static double g_now; void t7_eedbg(uint16_t a, uint8_t v){ static double last=-1; if (getenv("EEDBG") && g_now != last) { fprintf(stderr, "ee write at %.2f s: %03x=%02x\n", g_now, a, v); last = g_now; } }
|
||||
#endif
|
||||
static void frames(double s){ static int cyc; if(!cyc) cyc=getenv("CYC")?atoi(getenv("CYC")):40000; int n=(int)(s*100+0.5); for(int i=0;i<n;i++) { t7_step_cycles(cyc);
|
||||
#ifdef T7_EEDBG
|
||||
g_now += 0.01;
|
||||
#endif
|
||||
} }
|
||||
static void shot(const char *f){ uint8_t d[48*31], ic[10]; t7_get_display(d); t7_get_icons(ic);
|
||||
static const int pal[4]={0xE0,0xA0,0x60,0x20}; FILE*o=fopen(f,"wb"); fprintf(o,"P6 192 140 255\n");
|
||||
for(int y=0;y<140;y++) for(int x=0;x<192;x++){ int v=0xF0;
|
||||
if(y>=8&&y<132){ int yy=(y-8)/4, xx=x/4; if(yy<31) v=pal[d[yy*48+xx]]; }
|
||||
else { int k=x/(192/5)+(y>=132?5:0); v=ic[k]?pal[ic[k]]:0xF0; }
|
||||
fputc(v,o); fputc(v,o); fputc(v,o);} fclose(o); }
|
||||
int main(int c, char **v){ size_t n; unsigned char *rom=rd(v[1],&n); host_set_rom(rom,n);
|
||||
if(c>2){ size_t fn; unsigned char*f=rd(v[2],&fn); figure_rom_set_active(f,(unsigned)fn); t7_figure_set(1); }
|
||||
if (getenv("EE")) { size_t en; unsigned char *e=rd(getenv("EE"),&en); extern void persist_restore_page(int,const uint8_t*); for (int i=0;i<16 && (size_t)(i+1)*256<=en;i++) persist_restore_page(i,e+i*256); } /* EE=save: EEPROM from a file */
|
||||
if(!t7_init()){ printf("init failed\n"); return 1; }
|
||||
if (getenv("SND")) { extern void t7_snd_set_mode(int); t7_snd_set_mode(1); }
|
||||
#ifdef T7_JSR_HLE
|
||||
if (getenv("DARK")) g_t7_dark = 1; /* screen off as on the device */
|
||||
#endif
|
||||
char l[256]; while(fgets(l,sizeof l,stdin)){ char cmd[16],a[200]; double x=0; int k=sscanf(l,"%15s %199s %lf",cmd,a,&x); if(k<1) continue;
|
||||
if(!strcmp(cmd,"p")){ t7_set_buttons(a[0]=='A'?1:a[0]=='B'?2:4); frames(0.2); t7_set_buttons(0); frames(k>2?x:0.8); }
|
||||
else if(!strcmp(cmd,"w")) frames(atof(a));
|
||||
else if(!strcmp(cmd,"tl")){ /* tl <s>: tone list in 2 ms steps (hz x steps) */ extern uint32_t t7_snd_tone_hz(uint8_t*); uint32_t last=0xFFFFFFFF; int n=0; int m=(int)(atof(a)*500+0.5);
|
||||
for(int i=0;i<m;i++){ t7_step_cycles(8000); uint8_t lv; uint32_t hz=t7_snd_tone_hz(&lv); if(hz!=last){ if(n) printf("%u x%d\n",last,n); last=hz; n=0; } n++; } printf("%u x%d\nEND\n",last,n); }
|
||||
else if(!strcmp(cmd,"k")){ double g=0; sscanf(l,"%*s %*s %lf %lf",&x,&g); int m=a[0]=='A'?1:a[0]=='B'?2:4; /* k A hold gap: a quick press in 2 ms steps */
|
||||
t7_set_buttons(m); for(int i=0;i<(int)(x*500+0.5);i++) { t7_step_cycles(8000);
|
||||
#ifdef T7_STOP_DBG
|
||||
g_step++; if (getenv("TL")) fputc(g_t7_halted ? 'h' : 'r', stderr);
|
||||
#endif
|
||||
}
|
||||
#ifdef T7_STOP_DBG
|
||||
if (getenv("TL")) fputc('|', stderr);
|
||||
#endif
|
||||
t7_set_buttons(0); for(int i=0;i<(int)(g*500+0.5);i++) { t7_step_cycles(8000);
|
||||
#ifdef T7_STOP_DBG
|
||||
if (getenv("TL")) fputc(g_t7_halted ? 'H' : 'R', stderr);
|
||||
#endif
|
||||
}
|
||||
#ifdef T7_STOP_DBG
|
||||
if (getenv("TL")) fputc('\n', stderr);
|
||||
#endif
|
||||
}
|
||||
#ifdef T7_JSR_HLE
|
||||
else if(!strcmp(cmd,"dk")){ g_t7_dark=(uint8_t)atoi(a); } /* dk 1|0: screen off/on */
|
||||
#ifdef T7_WAKE_FRESH
|
||||
else if(!strcmp(cmd,"wake")){ extern uint32_t g_t7_blits; uint32_t b0=g_t7_blits, bt=0; int t=0; g_t7_dark=0; /* wake: on as T7_WAKE_FRESH (light after a drawn picture) */
|
||||
for(;;){ t7_step_cycles(8000); t+=2; if(g_t7_blits!=b0){ b0=g_t7_blits; bt=t; } if((bt && t-bt>=20) || t>=700) break; }
|
||||
printf("wake light after %d ms\n", t); }
|
||||
else if(!strcmp(cmd,"peek")){ extern uint32_t g_t7_blits; uint32_t b0=g_t7_blits, bt=0; int t=0; g_t7_dark=0; /* peek: one picture with the screen dark, then dark again */
|
||||
for(;;){ t7_step_cycles(8000); t+=2; if(g_t7_blits!=b0){ b0=g_t7_blits; bt=t; } if((bt && t-bt>=20) || t>=700) break; }
|
||||
g_t7_dark=1; shot(a); printf("peek %d ms\n", t); }
|
||||
#endif
|
||||
#endif
|
||||
else if(!strcmp(cmd,"i")){ uint8_t ic[10]; t7_get_icons(ic); printf("icons"); for(int i=0;i<10;i++) printf(" %d",ic[i]); printf("\n"); }
|
||||
else if(!strcmp(cmd,"s")) shot(a);
|
||||
else if(!strcmp(cmd,"t")) printf("time %02d:%02d:%02d day %d.%d\n", t7_ram_read(0x306),t7_ram_read(0x307),t7_ram_read(t7_rtc_sec_addr()),t7_ram_read(0x305),t7_ram_read(0x304));
|
||||
else if(!strcmp(cmd,"poke")){ t7_ram_write((uint16_t)strtol(a,0,16),(uint8_t)x); }
|
||||
else if(!strcmp(cmd,"d")){ FILE*o=fopen(a,"wb"); fwrite(g_wram,1,T7_WRAM_SIZE,o); fclose(o); }
|
||||
else if(!strcmp(cmd,"r")){ int a0=(int)strtol(a,0,16), m=x>0?(int)x:1; printf("ram %03x:",a0); for(int i=0;i<m;i++) printf(" %02x",t7_ram_read((uint16_t)(a0+i))); printf("\n"); }
|
||||
#ifdef T7_BLIT_CACHE
|
||||
else if(!strcmp(cmd,"bc")){ extern uint32_t g_bc_hit, g_bc_miss; printf("blit cache hit %u miss %u\n", g_bc_hit, g_bc_miss);
|
||||
#ifdef T7_BC_LOG
|
||||
{ extern void bc_print(void); bc_print(); }
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
else if(!strcmp(cmd,"q")) break; fflush(stdout); }
|
||||
return 0; }
|
||||
+15
-15
@@ -1,15 +1,15 @@
|
||||
/* PC histogram of the V7 firmware (host). pcprof <rom> <setup.txt-like keys file> <secs> */
|
||||
#include "../src/core/t7_internal.h"
|
||||
#include <stdio.h>
|
||||
void host_set_rom(unsigned char *p, size_t n);
|
||||
static unsigned char *rd(const char *f, size_t *n){ FILE*x=fopen(f,"rb"); fseek(x,0,2); *n=ftell(x); fseek(x,0,0); unsigned char*p=malloc(*n); if(fread(p,1,*n,x)){} fclose(x); return p; }
|
||||
static unsigned cnt[65536];
|
||||
int main(int c,char**v){ size_t n; unsigned char *rm=rd(v[1],&n); host_set_rom(rm,n); if(!t7_init()) return 1;
|
||||
double secs=atof(v[2]); long total=(long)(secs*4000000), done=0; long ins=0;
|
||||
/* after 5 s, count */
|
||||
while(done<total){ uint32_t s=t7_step_cycles(4000); done+=s; }
|
||||
for(long k=0;k<(long)(2*4000000);){ uint16_t pc=g_cpu.pc; cnt[pc]++; ins++; uint32_t cy=t7_step_one(); if(!cy) cy=1; k+=cy;
|
||||
static uint32_t acc; acc+=cy; if(acc>=66666){acc-=66666; t7_fire_irq(10); t7_fire_nmi(6);} }
|
||||
printf("instr in 2 s: %ld (%.2f M/s)\n", ins, ins/2e6);
|
||||
for(int r=0;r<15;r++){ int b=0; for(int i=1;i<65536;i++) if(cnt[i]>cnt[b]) b=i; printf("%04x %u bank %d\n",b,cnt[b],g_rom_bank_id); cnt[b]=0; }
|
||||
}
|
||||
/* PC histogram of the V7 firmware (host). pcprof <rom> <setup.txt-like keys file> <secs> */
|
||||
#include "../src/core/t7_internal.h"
|
||||
#include <stdio.h>
|
||||
void host_set_rom(unsigned char *p, size_t n);
|
||||
static unsigned char *rd(const char *f, size_t *n){ FILE*x=fopen(f,"rb"); fseek(x,0,2); *n=ftell(x); fseek(x,0,0); unsigned char*p=malloc(*n); if(fread(p,1,*n,x)){} fclose(x); return p; }
|
||||
static unsigned cnt[65536];
|
||||
int main(int c,char**v){ size_t n; unsigned char *rm=rd(v[1],&n); host_set_rom(rm,n); if(!t7_init()) return 1;
|
||||
double secs=atof(v[2]); long total=(long)(secs*4000000), done=0; long ins=0;
|
||||
/* after 5 s, count */
|
||||
while(done<total){ uint32_t s=t7_step_cycles(4000); done+=s; }
|
||||
for(long k=0;k<(long)(2*4000000);){ uint16_t pc=g_cpu.pc; cnt[pc]++; ins++; uint32_t cy=t7_step_one(); if(!cy) cy=1; k+=cy;
|
||||
static uint32_t acc; acc+=cy; if(acc>=66666){acc-=66666; t7_fire_irq(10); t7_fire_nmi(6);} }
|
||||
printf("instr in 2 s: %ld (%.2f M/s)\n", ins, ins/2e6);
|
||||
for(int r=0;r<15;r++){ int b=0; for(int i=1;i<65536;i++) if(cnt[i]>cnt[b]) b=i; printf("%04x %u bank %d\n",b,cnt[b],g_rom_bank_id); cnt[b]=0; }
|
||||
}
|
||||
+22
-22
@@ -1,22 +1,22 @@
|
||||
/* Same key script on t7_cpu_step and on t7_cpu_run (build twice, -DT7_FASTRUN): prints a hash of
|
||||
* the whole state after every 2 ms step; the two outputs must be equal. runcmp <rom> <script> */
|
||||
#include "../src/core/t7_internal.h"
|
||||
#include "../src/core/t7_sound.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
void host_set_rom(unsigned char *p, size_t n);
|
||||
static unsigned char *rd(const char *f, size_t *n){ FILE*x=fopen(f,"rb"); fseek(x,0,2); *n=ftell(x); fseek(x,0,0); unsigned char*p=malloc(*n); if(fread(p,1,*n,x)){} fclose(x); return p; }
|
||||
static uint32_t h(const uint8_t *p, int n, uint32_t s){ while(n--) s = s*16777619u ^ *p++; return s; }
|
||||
int main(int c,char**v){ size_t n; unsigned char*rm=rd(v[1],&n); host_set_rom(rm,n); if(!t7_init()) return 1;
|
||||
if (c>3) t7_snd_set_mode(1);
|
||||
FILE*f=fopen(v[2],"r"); char l[128]; long step=0;
|
||||
while(fgets(l,sizeof l,f)){ char k=0; double s=0; if(sscanf(l,"%c %lf",&k,&s)<2) continue;
|
||||
t7_set_buttons(k=='A'?1:k=='B'?2:k=='C'?4:0);
|
||||
int m=(int)(s*500+0.5);
|
||||
for(int i=0;i<m;i++){ t7_step_cycles(8000); uint8_t buf[4096]; uint32_t sz=t7_serialize_state(buf,sizeof buf);
|
||||
uint32_t hh=h(buf,sz,2166136261u); printf("%ld %08x pc=%04x a=%02x x=%02x y=%02x s=%02x w=%08x d=%08x r=%08x\n",step++,hh,g_cpu.pc,g_cpu.a,g_cpu.x,g_cpu.y,g_cpu.s,h(g_wram,0x600,1),h(g_dram,0x200,1),h(g_cpureg,256,1)); } }
|
||||
if (getenv("CLK")) { for (int i = 0x390; i < 0x3A0; i++) fprintf(stderr, "%02x ", g_wram[i]); fprintf(stderr, "\n"); }
|
||||
#if defined(T7_IDLE) || defined(T7_STOP)
|
||||
fprintf(stderr, "idle ok %u, skipped %u cycles of %ld\n", g_t7_idle_ok, g_t7_idle_cyc, step * 8000L);
|
||||
#endif
|
||||
return 0; }
|
||||
/* Same key script on t7_cpu_step and on t7_cpu_run (build twice, -DT7_FASTRUN): prints a hash of
|
||||
* the whole state after every 2 ms step; the two outputs must be equal. runcmp <rom> <script> */
|
||||
#include "../src/core/t7_internal.h"
|
||||
#include "../src/core/t7_sound.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
void host_set_rom(unsigned char *p, size_t n);
|
||||
static unsigned char *rd(const char *f, size_t *n){ FILE*x=fopen(f,"rb"); fseek(x,0,2); *n=ftell(x); fseek(x,0,0); unsigned char*p=malloc(*n); if(fread(p,1,*n,x)){} fclose(x); return p; }
|
||||
static uint32_t h(const uint8_t *p, int n, uint32_t s){ while(n--) s = s*16777619u ^ *p++; return s; }
|
||||
int main(int c,char**v){ size_t n; unsigned char*rm=rd(v[1],&n); host_set_rom(rm,n); if(!t7_init()) return 1;
|
||||
if (c>3) t7_snd_set_mode(1);
|
||||
FILE*f=fopen(v[2],"r"); char l[128]; long step=0;
|
||||
while(fgets(l,sizeof l,f)){ char k=0; double s=0; if(sscanf(l,"%c %lf",&k,&s)<2) continue;
|
||||
t7_set_buttons(k=='A'?1:k=='B'?2:k=='C'?4:0);
|
||||
int m=(int)(s*500+0.5);
|
||||
for(int i=0;i<m;i++){ t7_step_cycles(8000); uint8_t buf[4096]; uint32_t sz=t7_serialize_state(buf,sizeof buf);
|
||||
uint32_t hh=h(buf,sz,2166136261u); printf("%ld %08x pc=%04x a=%02x x=%02x y=%02x s=%02x w=%08x d=%08x r=%08x\n",step++,hh,g_cpu.pc,g_cpu.a,g_cpu.x,g_cpu.y,g_cpu.s,h(g_wram,0x600,1),h(g_dram,0x200,1),h(g_cpureg,256,1)); } }
|
||||
if (getenv("CLK")) { for (int i = 0x390; i < 0x3A0; i++) fprintf(stderr, "%02x ", g_wram[i]); fprintf(stderr, "\n"); }
|
||||
#if defined(T7_IDLE) || defined(T7_STOP)
|
||||
fprintf(stderr, "idle ok %u, skipped %u cycles of %ld\n", g_t7_idle_ok, g_t7_idle_cyc, step * 8000L);
|
||||
#endif
|
||||
return 0; }
|
||||
+18
-18
@@ -1,18 +1,18 @@
|
||||
w 3
|
||||
p B 2
|
||||
p B 0.6
|
||||
p B 0.6
|
||||
p B 0.6
|
||||
p A 0.6
|
||||
p B 0.6
|
||||
p B 0.6
|
||||
p B 0.6
|
||||
p B 0.6
|
||||
p B 0.8
|
||||
p B 0.8
|
||||
p B 0.8
|
||||
p B 0.8
|
||||
p B 0.8
|
||||
p B 0.8
|
||||
p B 0.8
|
||||
p B 0.8
|
||||
w 3
|
||||
p B 2
|
||||
p B 0.6
|
||||
p B 0.6
|
||||
p B 0.6
|
||||
p A 0.6
|
||||
p B 0.6
|
||||
p B 0.6
|
||||
p B 0.6
|
||||
p B 0.6
|
||||
p B 0.8
|
||||
p B 0.8
|
||||
p B 0.8
|
||||
p B 0.8
|
||||
p B 0.8
|
||||
p B 0.8
|
||||
p B 0.8
|
||||
p B 0.8
|
||||
+13
-13
@@ -1,13 +1,13 @@
|
||||
#include "../src/core/t7_internal.h"
|
||||
#include <stdio.h>
|
||||
void host_set_rom(unsigned char *p, size_t n); uint8_t t7_dbg_p(void); uint32_t t7_cpu_run(uint32_t);
|
||||
static unsigned char *rd(const char *f, size_t *n){ FILE*x=fopen(f,"rb"); fseek(x,0,2); *n=ftell(x); fseek(x,0,0); unsigned char*p=malloc(*n); if(fread(p,1,*n,x)){} fclose(x); return p; }
|
||||
int main(int c,char**v){ size_t n; unsigned char*rm=rd(v[1],&n); host_set_rom(rm,n); t7_init(); long N=atol(v[2]);
|
||||
for(long i=0;i<N;i++){
|
||||
#ifdef T7_FASTRUN
|
||||
uint32_t cy=t7_cpu_run(1);
|
||||
#else
|
||||
uint32_t cy=t7_cpu_step();
|
||||
#endif
|
||||
printf("%ld %04x %02x %02x %02x %02x %02x %u m48=%02x %02x\n",i,g_cpu.pc,g_cpu.a,g_cpu.x,g_cpu.y,g_cpu.s,t7_dbg_p(),cy,t7_read_page[0][0x48],t7_read_page[0][0x49]); }
|
||||
}
|
||||
#include "../src/core/t7_internal.h"
|
||||
#include <stdio.h>
|
||||
void host_set_rom(unsigned char *p, size_t n); uint8_t t7_dbg_p(void); uint32_t t7_cpu_run(uint32_t);
|
||||
static unsigned char *rd(const char *f, size_t *n){ FILE*x=fopen(f,"rb"); fseek(x,0,2); *n=ftell(x); fseek(x,0,0); unsigned char*p=malloc(*n); if(fread(p,1,*n,x)){} fclose(x); return p; }
|
||||
int main(int c,char**v){ size_t n; unsigned char*rm=rd(v[1],&n); host_set_rom(rm,n); t7_init(); long N=atol(v[2]);
|
||||
for(long i=0;i<N;i++){
|
||||
#ifdef T7_FASTRUN
|
||||
uint32_t cy=t7_cpu_run(1);
|
||||
#else
|
||||
uint32_t cy=t7_cpu_step();
|
||||
#endif
|
||||
printf("%ld %04x %02x %02x %02x %02x %02x %u m48=%02x %02x\n",i,g_cpu.pc,g_cpu.a,g_cpu.x,g_cpu.y,g_cpu.s,t7_dbg_p(),cy,t7_read_page[0][0x48],t7_read_page[0][0x49]); }
|
||||
}
|
||||
+21
-21
@@ -1,21 +1,21 @@
|
||||
#!/bin/sh
|
||||
# Build the T7 app for the Paradise (clang/lld, Cortex-M3). Output: ../../t7.bin
|
||||
set -e
|
||||
cd "$(dirname "$0")"
|
||||
E=../../../emu/src/emu
|
||||
XID=${XID:-0x$(date +%s | sha1sum | cut -c1-8)} # the two parts belong together
|
||||
CF="--target=thumbv7m-none-eabi -mcpu=cortex-m3 -mthumb -ffreestanding -nostdlib -fno-common -Wall -Wno-unused-function -fno-builtin-memset -DFAST_LCD -DEXIT_KEY -DLCD_FASTCLK -DTONE_FIX -DHW_TONE_RESTART -DWAKE_WHEEL -DBL_SLEEP -DT7_NOMALLOC -DT7_FREESTANDING -DT7_NO_SYNTH -DSPI_TRACE_MAX=1 -DT7_FASTRUN -DT7_VIDEO_CYCLES -DT7_SND_RING=16u -DT7_SND_SYN_N=4u -DT7_BELL_WAKE -DT7_FR_ICONS -DSESS_ONCE -DSESS_AUTO -DPARA_BACK -DPARA_MB_KEEP -DBL_PWM -DBL_MR3RST -DT7_PW_FIX -DT7_LINK -DT7_IDLE -DT7_STOP -DT7_KEYQ -DT7_STOP_KEYACT=4000000u -DT7_TONE_Q -DT7_SESS_EESEQ -DT7_EE_SOON -DBAT_SHOW -DCLK_ECO -DECO_LAG=400 -DT7_WFI_FIX -DT7_EE_SHIFT -DECO_RTC -DT7_GAME_KEEP -DT7_CK_WRAP -DT7_CARE -DASK_NUM -DT7_JSR_HLE -DT7_BLIT_CACHE -DT7_DARK_STEP -DT7_DARK_PEEK -DT7_NO_CATCHUP -DT7_PW -DT7_SND_LAZY -DT7_JIT -DT7_TBH_RATE=1515150u -DT7_VOL -DHW_VOL5 -DT7_FIXED -DT7_TONE_EV -DT7_TONE_ISR -DHW_TONE_IRQSAFE -DTONE_SAFE -DT7_WHEEL_RAW -DT7_PW_RTCWAIT -DT7_CATCHUP_SMOOTH -DBAT_SMOOTH -DT7_CLK_COLD -DBAT_GUARD -DBAT_PIC -DT7_SESS_KEEP -DT7_KQ_CAP -DPARA_FLASH -DPARA_FLASH_LATE -DPARA_CLEAN_CLK -DSESS_FLAG -DT7X_ID=$XID -I../core $XCF"
|
||||
O=""
|
||||
for f in main.c t7app.c compat.c $E/hw.c $E/ask.c $E/flash.c $E/libc.c libgcc.c ../core/t7.c ../core/t7_cpu.c ../core/t7_memory.c ../core/t7_eeprom.c ../core/t7_sound.c ../core/figure_data.c ../core/t7_jit.c; do
|
||||
n=$(basename $f .c); OPT=-Os; [ "$n" = t7_cpu ] && OPT=${CPUOPT:--O2}; clang $CF $OPT -c $f -o $n.o; O="$O $n.o"; done
|
||||
ld.lld -T link.ld --no-rosegment $O -o t7.elf
|
||||
llvm-objcopy -O binary -j .text -j .data -j .init t7.elf t7.bin
|
||||
llvm-objcopy -O binary -j .xcode t7.elf x.raw
|
||||
python3 - "$XID" <<'PY'
|
||||
import struct, sys
|
||||
d = open('x.raw', 'rb').read(); d += b'\0' * (-len(d) % 4)
|
||||
s = sum(struct.unpack('<%dI' % (len(d) // 4), d)) & 0xFFFFFFFF
|
||||
open('../../t7x.bin', 'wb').write(struct.pack('<4I', 0x31583754, int(sys.argv[1], 16), len(d), s) + d)
|
||||
PY
|
||||
rm -f x.raw
|
||||
cp t7.bin ../../t7.bin; rm -f *.o t7.bin; [ -n "$KEEPELF" ] || rm -f t7.elf; llvm-size ../../t7.bin 2>/dev/null; ls -l ../../t7.bin ../../t7x.bin
|
||||
#!/bin/sh
|
||||
# Build the T7 app for the Paradise (clang/lld, Cortex-M3). Output: ../../t7.bin
|
||||
set -e
|
||||
cd "$(dirname "$0")"
|
||||
E=../../../emu/src/emu
|
||||
XID=${XID:-0x$(date +%s | sha1sum | cut -c1-8)} # the two parts belong together
|
||||
CF="--target=thumbv7m-none-eabi -mcpu=cortex-m3 -mthumb -ffreestanding -nostdlib -fno-common -Wall -Wno-unused-function -fno-builtin-memset -DFAST_LCD -DEXIT_KEY -DLCD_FASTCLK -DTONE_FIX -DHW_TONE_RESTART -DWAKE_WHEEL -DBL_SLEEP -DT7_NOMALLOC -DT7_FREESTANDING -DT7_NO_SYNTH -DSPI_TRACE_MAX=1 -DT7_FASTRUN -DT7_VIDEO_CYCLES -DT7_SND_RING=16u -DT7_SND_SYN_N=4u -DT7_BELL_WAKE -DT7_FR_ICONS -DSESS_ONCE -DSESS_AUTO -DPARA_BACK -DPARA_MB_KEEP -DBL_PWM -DBL_MR3RST -DT7_PW_FIX -DT7_LINK -DT7_IDLE -DT7_STOP -DT7_KEYQ -DT7_STOP_KEYACT=4000000u -DT7_TONE_Q -DT7_SESS_EESEQ -DT7_EE_SOON -DBAT_SHOW -DCLK_ECO -DECO_LAG=400 -DT7_WFI_FIX -DT7_EE_SHIFT -DECO_RTC -DT7_GAME_KEEP -DT7_CK_WRAP -DT7_CARE -DASK_NUM -DT7_JSR_HLE -DT7_BLIT_CACHE -DT7_DARK_STEP -DT7_DARK_PEEK -DT7_NO_CATCHUP -DT7_PW -DT7_SND_LAZY -DT7_JIT -DT7_TBH_RATE=1515150u -DT7_VOL -DHW_VOL5 -DT7_FIXED -DT7_TONE_EV -DT7_TONE_ISR -DHW_TONE_IRQSAFE -DTONE_SAFE -DT7_WHEEL_RAW -DT7_PW_RTCWAIT -DT7_CATCHUP_SMOOTH -DBAT_SMOOTH -DT7_CLK_COLD -DBAT_GUARD -DBAT_PIC -DT7_SESS_KEEP -DT7_KQ_CAP -DPARA_FLASH -DPARA_FLASH_LATE -DPARA_CLEAN_CLK -DSESS_FLAG -DT7X_ID=$XID -I../core $XCF"
|
||||
O=""
|
||||
for f in main.c t7app.c compat.c $E/hw.c $E/ask.c $E/flash.c $E/libc.c libgcc.c ../core/t7.c ../core/t7_cpu.c ../core/t7_memory.c ../core/t7_eeprom.c ../core/t7_sound.c ../core/figure_data.c ../core/t7_jit.c; do
|
||||
n=$(basename $f .c); OPT=-Os; [ "$n" = t7_cpu ] && OPT=${CPUOPT:--O2}; clang $CF $OPT -c $f -o $n.o; O="$O $n.o"; done
|
||||
ld.lld -T link.ld --no-rosegment $O -o t7.elf
|
||||
llvm-objcopy -O binary -j .text -j .data -j .init t7.elf t7.bin
|
||||
llvm-objcopy -O binary -j .xcode t7.elf x.raw
|
||||
python3 - "$XID" <<'PY'
|
||||
import struct, sys
|
||||
d = open('x.raw', 'rb').read(); d += b'\0' * (-len(d) % 4)
|
||||
s = sum(struct.unpack('<%dI' % (len(d) // 4), d)) & 0xFFFFFFFF
|
||||
open('../../t7x.bin', 'wb').write(struct.pack('<4I', 0x31583754, int(sys.argv[1], 16), len(d), s) + d)
|
||||
PY
|
||||
rm -f x.raw
|
||||
cp t7.bin ../../t7.bin; rm -f *.o t7.bin; [ -n "$KEEPELF" ] || rm -f t7.elf; llvm-size ../../t7.bin 2>/dev/null; ls -l ../../t7.bin ../../t7x.bin
|
||||
+119
-119
@@ -1,119 +1,119 @@
|
||||
/* The platform functions the V7 core expects (pebble_compat.h), on the Paradise:
|
||||
* ROM from the flash (cached XIP window), EEPROM pages in RAM, written to the flash
|
||||
* by eeprom_store(). Author: mameise */
|
||||
#include "../core/pebble_compat.h"
|
||||
#include "../../../emu/src/emu/io.h"
|
||||
#include "../../../emu/src/emu/flash.h"
|
||||
|
||||
extern const uint8_t *g_rom_xip; /* main.c: start of the ROM in the cached window */
|
||||
extern uint32_t g_rom_size, g_rom_off;
|
||||
static uint8_t g_ee[16][256] __attribute__((aligned(4))); /* test27: the flash DMA takes words (was at ...2: pages 2 bytes off) */
|
||||
static uint16_t g_have, g_dirty;
|
||||
|
||||
void tama_log(const char *fmt, ...) { (void)fmt; }
|
||||
ResHandle resource_get_handle(int id) { (void)id; return 1; }
|
||||
size_t resource_size(ResHandle h) { (void)h; return g_rom_size; }
|
||||
size_t resource_load_byte_range(ResHandle h, size_t off, uint8_t *out, size_t len)
|
||||
{
|
||||
(void)h;
|
||||
if (off >= g_rom_size) return 0;
|
||||
if (off + len > g_rom_size) len = g_rom_size - off;
|
||||
const volatile uint8_t *s = XIP_PTR(g_rom_off + off); /* uncached: right after setting the window */
|
||||
for (size_t i = 0; i < len; i++) out[i] = s[i];
|
||||
return len;
|
||||
}
|
||||
const uint8_t *t7_compat_rom_mapped(void) { return g_rom_xip; }
|
||||
void t7_compat_set_rom_path(const char *p) { (void)p; }
|
||||
void t7_compat_set_eeprom_path(const char *p) { (void)p; }
|
||||
|
||||
/* EEPROM: 16 pages of 256 bytes, keys 100..115. In flash: one 4 KB sector,
|
||||
* behind it a byte per page that says whether the page was ever written. */
|
||||
bool persist_exists(uint32_t key) { return key >= 100 && key < 116 && (g_have >> (key - 100)) & 1u; }
|
||||
int persist_read_data(uint32_t key, void *buf, size_t n)
|
||||
{
|
||||
if (!persist_exists(key)) return -1;
|
||||
if (n > 256) n = 256;
|
||||
uint8_t *d = buf; for (size_t i = 0; i < n; i++) d[i] = g_ee[key - 100][i];
|
||||
return (int)n;
|
||||
}
|
||||
status_t persist_write_data(uint32_t key, const void *buf, size_t n)
|
||||
{
|
||||
if (key < 100 || key >= 116) return -1;
|
||||
if (n > 256) n = 256;
|
||||
const uint8_t *s = buf; int ch = 0;
|
||||
for (size_t i = 0; i < n; i++) if (g_ee[key - 100][i] != s[i]) { g_ee[key - 100][i] = s[i]; ch = 1; }
|
||||
if (ch || !persist_exists(key)) { g_have |= (uint16_t)(1u << (key - 100)); g_dirty = 1; }
|
||||
return S_SUCCESS;
|
||||
}
|
||||
status_t persist_delete(uint32_t key) { if (key >= 100 && key < 116 && persist_exists(key)) { g_have &= (uint16_t)~(1u << (key - 100)); g_dirty = 1; } return S_SUCCESS; }
|
||||
void persist_flush_to_disk(void) {}
|
||||
int persist_page_count(void) { return 16; }
|
||||
int persist_page_size(void) { return 256; }
|
||||
int persist_backup_page(int p, uint8_t *o) { for (int i = 0; i < 256; i++) o[i] = g_ee[p][i]; return persist_exists(100u + (uint32_t)p); }
|
||||
void persist_restore_page(int p, const uint8_t *in) { for (int i = 0; i < 256; i++) g_ee[p][i] = in[i]; g_have |= (uint16_t)(1u << p); g_dirty = 1; }
|
||||
|
||||
/* Flash: two slots of 8 KB (header sector, page sector) at at+0 and at+0x2000. A new state
|
||||
* goes into the other slot (pages first, header last), so a power cut always leaves one
|
||||
* complete slot. The newer valid slot wins. */
|
||||
#define EE_MAGIC 0x45453754u /* "T7EE" */
|
||||
static uint32_t g_seq; static int g_slot = -1;
|
||||
static uint32_t rd32(const volatile uint8_t *h) { return h[0] | (h[1] << 8) | (h[2] << 16) | ((uint32_t)h[3] << 24); }
|
||||
static uint32_t sum_pages(const volatile uint8_t *d) { uint32_t s = 0x7E7E; for (int i = 0; i < 4096; i++) s = s * 31u + d[i]; return s; }
|
||||
#ifdef T7_EE_SHIFT
|
||||
/* test27: up to test26 g_ee could lie at an address ...2; the flash DMA starts at the word below, so the page sector
|
||||
* holds 2 other bytes, then the pages without their last 2 bytes (the header sum is over the right pages).
|
||||
* Such a slot is taken 2 bytes on and written again correctly at the next store. */
|
||||
static uint32_t sum_pages2(const volatile uint8_t *d) { uint32_t s = 0x7E7E; for (int i = 2; i < 4096; i++) s = s * 31u + d[i]; return s * 31u * 31u; }
|
||||
static uint8_t g_shift[2];
|
||||
#endif
|
||||
static int slot_ok(uint32_t at, uint32_t *seq)
|
||||
{
|
||||
const volatile uint8_t *h = XIP_PTR(at);
|
||||
if (rd32(h) != EE_MAGIC) return 0;
|
||||
if (rd32(h + 12) != sum_pages(XIP_PTR(at + 0x1000u))) {
|
||||
#ifdef T7_EE_SHIFT
|
||||
if (rd32(h + 12) != sum_pages2(XIP_PTR(at + 0x1000u))) return 0;
|
||||
*seq = rd32(h + 4);
|
||||
return 2;
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
*seq = rd32(h + 4);
|
||||
return 1;
|
||||
}
|
||||
void eeprom_load(uint32_t at)
|
||||
{
|
||||
uint32_t s0 = 0, s1 = 0; int a = slot_ok(at, &s0), b = slot_ok(at + 0x2000u, &s1);
|
||||
#ifdef T7_EE_SHIFT
|
||||
g_shift[0] = a == 2; g_shift[1] = b == 2;
|
||||
#endif
|
||||
g_have = 0; g_dirty = 0; g_slot = -1; g_seq = 0;
|
||||
if (!a && !b) return;
|
||||
g_slot = (a && (!b || (int32_t)(s0 - s1) > 0)) ? 0 : 1;
|
||||
uint32_t base = at + (uint32_t)g_slot * 0x2000u;
|
||||
g_seq = g_slot ? s1 : s0;
|
||||
const volatile uint8_t *h = XIP_PTR(base);
|
||||
g_have = (uint16_t)(h[8] | (h[9] << 8));
|
||||
const volatile uint8_t *d = XIP_PTR(base + 0x1000u);
|
||||
for (int p = 0; p < 16; p++) for (int i = 0; i < 256; i++) g_ee[p][i] = d[p * 256 + i];
|
||||
#ifdef T7_EE_SHIFT
|
||||
if (g_shift[g_slot]) { uint8_t *e = &g_ee[0][0]; for (int i = 0; i < 4094; i++) e[i] = d[i + 2]; e[4094] = e[4095] = 0; g_dirty = 1; }
|
||||
#endif
|
||||
}
|
||||
int eeprom_dirty(void) { return g_dirty; }
|
||||
uint32_t eeprom_seq(void) { return g_slot < 0 ? 0u : g_seq; } /* test16: which stored state is in the flash */
|
||||
void eeprom_store(uint32_t at, uint8_t *sec4k)
|
||||
{
|
||||
if (!g_dirty) return;
|
||||
int slot = g_slot == 0 ? 1 : 0;
|
||||
uint32_t base = at + (uint32_t)slot * 0x2000u, seq = g_seq + 1u;
|
||||
for (int i = 0; i < 0x1000; i++) sec4k[i] = 0xFF;
|
||||
flash_update4k(base, sec4k); /* old header of this slot away */
|
||||
flash_update4k(base + 0x1000u, &g_ee[0][0]);
|
||||
uint32_t s = sum_pages(&g_ee[0][0]);
|
||||
uint32_t v[4] = { EE_MAGIC, seq, g_have, s };
|
||||
for (int k = 0; k < 4; k++) for (int b = 0; b < 4; b++) sec4k[k * 4 + b] = (uint8_t)(v[k] >> (8 * b));
|
||||
flash_update4k(base, sec4k);
|
||||
g_slot = slot; g_seq = seq; g_dirty = 0;
|
||||
}
|
||||
/* The platform functions the V7 core expects (pebble_compat.h), on the Paradise:
|
||||
* ROM from the flash (cached XIP window), EEPROM pages in RAM, written to the flash
|
||||
* by eeprom_store(). Author: mameise */
|
||||
#include "../core/pebble_compat.h"
|
||||
#include "../../../emu/src/emu/io.h"
|
||||
#include "../../../emu/src/emu/flash.h"
|
||||
|
||||
extern const uint8_t *g_rom_xip; /* main.c: start of the ROM in the cached window */
|
||||
extern uint32_t g_rom_size, g_rom_off;
|
||||
static uint8_t g_ee[16][256] __attribute__((aligned(4))); /* test27: the flash DMA takes words (was at ...2: pages 2 bytes off) */
|
||||
static uint16_t g_have, g_dirty;
|
||||
|
||||
void tama_log(const char *fmt, ...) { (void)fmt; }
|
||||
ResHandle resource_get_handle(int id) { (void)id; return 1; }
|
||||
size_t resource_size(ResHandle h) { (void)h; return g_rom_size; }
|
||||
size_t resource_load_byte_range(ResHandle h, size_t off, uint8_t *out, size_t len)
|
||||
{
|
||||
(void)h;
|
||||
if (off >= g_rom_size) return 0;
|
||||
if (off + len > g_rom_size) len = g_rom_size - off;
|
||||
const volatile uint8_t *s = XIP_PTR(g_rom_off + off); /* uncached: right after setting the window */
|
||||
for (size_t i = 0; i < len; i++) out[i] = s[i];
|
||||
return len;
|
||||
}
|
||||
const uint8_t *t7_compat_rom_mapped(void) { return g_rom_xip; }
|
||||
void t7_compat_set_rom_path(const char *p) { (void)p; }
|
||||
void t7_compat_set_eeprom_path(const char *p) { (void)p; }
|
||||
|
||||
/* EEPROM: 16 pages of 256 bytes, keys 100..115. In flash: one 4 KB sector,
|
||||
* behind it a byte per page that says whether the page was ever written. */
|
||||
bool persist_exists(uint32_t key) { return key >= 100 && key < 116 && (g_have >> (key - 100)) & 1u; }
|
||||
int persist_read_data(uint32_t key, void *buf, size_t n)
|
||||
{
|
||||
if (!persist_exists(key)) return -1;
|
||||
if (n > 256) n = 256;
|
||||
uint8_t *d = buf; for (size_t i = 0; i < n; i++) d[i] = g_ee[key - 100][i];
|
||||
return (int)n;
|
||||
}
|
||||
status_t persist_write_data(uint32_t key, const void *buf, size_t n)
|
||||
{
|
||||
if (key < 100 || key >= 116) return -1;
|
||||
if (n > 256) n = 256;
|
||||
const uint8_t *s = buf; int ch = 0;
|
||||
for (size_t i = 0; i < n; i++) if (g_ee[key - 100][i] != s[i]) { g_ee[key - 100][i] = s[i]; ch = 1; }
|
||||
if (ch || !persist_exists(key)) { g_have |= (uint16_t)(1u << (key - 100)); g_dirty = 1; }
|
||||
return S_SUCCESS;
|
||||
}
|
||||
status_t persist_delete(uint32_t key) { if (key >= 100 && key < 116 && persist_exists(key)) { g_have &= (uint16_t)~(1u << (key - 100)); g_dirty = 1; } return S_SUCCESS; }
|
||||
void persist_flush_to_disk(void) {}
|
||||
int persist_page_count(void) { return 16; }
|
||||
int persist_page_size(void) { return 256; }
|
||||
int persist_backup_page(int p, uint8_t *o) { for (int i = 0; i < 256; i++) o[i] = g_ee[p][i]; return persist_exists(100u + (uint32_t)p); }
|
||||
void persist_restore_page(int p, const uint8_t *in) { for (int i = 0; i < 256; i++) g_ee[p][i] = in[i]; g_have |= (uint16_t)(1u << p); g_dirty = 1; }
|
||||
|
||||
/* Flash: two slots of 8 KB (header sector, page sector) at at+0 and at+0x2000. A new state
|
||||
* goes into the other slot (pages first, header last), so a power cut always leaves one
|
||||
* complete slot. The newer valid slot wins. */
|
||||
#define EE_MAGIC 0x45453754u /* "T7EE" */
|
||||
static uint32_t g_seq; static int g_slot = -1;
|
||||
static uint32_t rd32(const volatile uint8_t *h) { return h[0] | (h[1] << 8) | (h[2] << 16) | ((uint32_t)h[3] << 24); }
|
||||
static uint32_t sum_pages(const volatile uint8_t *d) { uint32_t s = 0x7E7E; for (int i = 0; i < 4096; i++) s = s * 31u + d[i]; return s; }
|
||||
#ifdef T7_EE_SHIFT
|
||||
/* test27: up to test26 g_ee could lie at an address ...2; the flash DMA starts at the word below, so the page sector
|
||||
* holds 2 other bytes, then the pages without their last 2 bytes (the header sum is over the right pages).
|
||||
* Such a slot is taken 2 bytes on and written again correctly at the next store. */
|
||||
static uint32_t sum_pages2(const volatile uint8_t *d) { uint32_t s = 0x7E7E; for (int i = 2; i < 4096; i++) s = s * 31u + d[i]; return s * 31u * 31u; }
|
||||
static uint8_t g_shift[2];
|
||||
#endif
|
||||
static int slot_ok(uint32_t at, uint32_t *seq)
|
||||
{
|
||||
const volatile uint8_t *h = XIP_PTR(at);
|
||||
if (rd32(h) != EE_MAGIC) return 0;
|
||||
if (rd32(h + 12) != sum_pages(XIP_PTR(at + 0x1000u))) {
|
||||
#ifdef T7_EE_SHIFT
|
||||
if (rd32(h + 12) != sum_pages2(XIP_PTR(at + 0x1000u))) return 0;
|
||||
*seq = rd32(h + 4);
|
||||
return 2;
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
*seq = rd32(h + 4);
|
||||
return 1;
|
||||
}
|
||||
void eeprom_load(uint32_t at)
|
||||
{
|
||||
uint32_t s0 = 0, s1 = 0; int a = slot_ok(at, &s0), b = slot_ok(at + 0x2000u, &s1);
|
||||
#ifdef T7_EE_SHIFT
|
||||
g_shift[0] = a == 2; g_shift[1] = b == 2;
|
||||
#endif
|
||||
g_have = 0; g_dirty = 0; g_slot = -1; g_seq = 0;
|
||||
if (!a && !b) return;
|
||||
g_slot = (a && (!b || (int32_t)(s0 - s1) > 0)) ? 0 : 1;
|
||||
uint32_t base = at + (uint32_t)g_slot * 0x2000u;
|
||||
g_seq = g_slot ? s1 : s0;
|
||||
const volatile uint8_t *h = XIP_PTR(base);
|
||||
g_have = (uint16_t)(h[8] | (h[9] << 8));
|
||||
const volatile uint8_t *d = XIP_PTR(base + 0x1000u);
|
||||
for (int p = 0; p < 16; p++) for (int i = 0; i < 256; i++) g_ee[p][i] = d[p * 256 + i];
|
||||
#ifdef T7_EE_SHIFT
|
||||
if (g_shift[g_slot]) { uint8_t *e = &g_ee[0][0]; for (int i = 0; i < 4094; i++) e[i] = d[i + 2]; e[4094] = e[4095] = 0; g_dirty = 1; }
|
||||
#endif
|
||||
}
|
||||
int eeprom_dirty(void) { return g_dirty; }
|
||||
uint32_t eeprom_seq(void) { return g_slot < 0 ? 0u : g_seq; } /* test16: which stored state is in the flash */
|
||||
void eeprom_store(uint32_t at, uint8_t *sec4k)
|
||||
{
|
||||
if (!g_dirty) return;
|
||||
int slot = g_slot == 0 ? 1 : 0;
|
||||
uint32_t base = at + (uint32_t)slot * 0x2000u, seq = g_seq + 1u;
|
||||
for (int i = 0; i < 0x1000; i++) sec4k[i] = 0xFF;
|
||||
flash_update4k(base, sec4k); /* old header of this slot away */
|
||||
flash_update4k(base + 0x1000u, &g_ee[0][0]);
|
||||
uint32_t s = sum_pages(&g_ee[0][0]);
|
||||
uint32_t v[4] = { EE_MAGIC, seq, g_have, s };
|
||||
for (int k = 0; k < 4; k++) for (int b = 0; b < 4; b++) sec4k[k * 4 + b] = (uint8_t)(v[k] >> (8 * b));
|
||||
flash_update4k(base, sec4k);
|
||||
g_slot = slot; g_seq = seq; g_dirty = 0;
|
||||
}
|
||||
+99
-99
@@ -1,99 +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},
|
||||
};
|
||||
/* generated by gen/font.py: custom 5x7 font */
|
||||
static const unsigned char g_font[96][7]={
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x04,0x04,0x04,0x04,0x04,0x00,0x04},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x19,0x19,0x02,0x04,0x08,0x13,0x13},
|
||||
{0x0c,0x12,0x14,0x08,0x15,0x12,0x0d},
|
||||
{0x04,0x04,0x08,0x00,0x00,0x00,0x00},
|
||||
{0x02,0x04,0x08,0x08,0x08,0x04,0x02},
|
||||
{0x08,0x04,0x02,0x02,0x02,0x04,0x08},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x00,0x04,0x04,0x1f,0x04,0x04,0x00},
|
||||
{0x00,0x00,0x00,0x00,0x0c,0x04,0x08},
|
||||
{0x00,0x00,0x00,0x1f,0x00,0x00,0x00},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x0c,0x0c},
|
||||
{0x01,0x01,0x02,0x04,0x08,0x10,0x10},
|
||||
{0x0e,0x11,0x13,0x15,0x19,0x11,0x0e},
|
||||
{0x04,0x0c,0x04,0x04,0x04,0x04,0x0e},
|
||||
{0x0e,0x11,0x01,0x02,0x04,0x08,0x1f},
|
||||
{0x1e,0x01,0x01,0x0e,0x01,0x01,0x1e},
|
||||
{0x02,0x06,0x0a,0x12,0x1f,0x02,0x02},
|
||||
{0x1f,0x10,0x1e,0x01,0x01,0x11,0x0e},
|
||||
{0x0e,0x10,0x10,0x1e,0x11,0x11,0x0e},
|
||||
{0x1f,0x01,0x02,0x04,0x08,0x08,0x08},
|
||||
{0x0e,0x11,0x11,0x0e,0x11,0x11,0x0e},
|
||||
{0x0e,0x11,0x11,0x0f,0x01,0x01,0x0e},
|
||||
{0x00,0x04,0x04,0x00,0x04,0x04,0x00},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x02,0x04,0x08,0x10,0x08,0x04,0x02},
|
||||
{0x00,0x00,0x1f,0x00,0x1f,0x00,0x00},
|
||||
{0x08,0x04,0x02,0x01,0x02,0x04,0x08},
|
||||
{0x0e,0x11,0x01,0x02,0x04,0x00,0x04},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x0e,0x11,0x11,0x1f,0x11,0x11,0x11},
|
||||
{0x1e,0x11,0x11,0x1e,0x11,0x11,0x1e},
|
||||
{0x0e,0x11,0x10,0x10,0x10,0x11,0x0e},
|
||||
{0x1e,0x11,0x11,0x11,0x11,0x11,0x1e},
|
||||
{0x1f,0x10,0x10,0x1e,0x10,0x10,0x1f},
|
||||
{0x1f,0x10,0x10,0x1e,0x10,0x10,0x10},
|
||||
{0x0e,0x11,0x10,0x17,0x11,0x11,0x0f},
|
||||
{0x11,0x11,0x11,0x1f,0x11,0x11,0x11},
|
||||
{0x0e,0x04,0x04,0x04,0x04,0x04,0x0e},
|
||||
{0x07,0x02,0x02,0x02,0x02,0x12,0x0c},
|
||||
{0x11,0x12,0x14,0x18,0x14,0x12,0x11},
|
||||
{0x10,0x10,0x10,0x10,0x10,0x10,0x1f},
|
||||
{0x11,0x1b,0x15,0x15,0x11,0x11,0x11},
|
||||
{0x11,0x19,0x15,0x13,0x11,0x11,0x11},
|
||||
{0x0e,0x11,0x11,0x11,0x11,0x11,0x0e},
|
||||
{0x1e,0x11,0x11,0x1e,0x10,0x10,0x10},
|
||||
{0x0e,0x11,0x11,0x11,0x15,0x12,0x0d},
|
||||
{0x1e,0x11,0x11,0x1e,0x14,0x12,0x11},
|
||||
{0x0f,0x10,0x10,0x0e,0x01,0x01,0x1e},
|
||||
{0x1f,0x04,0x04,0x04,0x04,0x04,0x04},
|
||||
{0x11,0x11,0x11,0x11,0x11,0x11,0x0e},
|
||||
{0x11,0x11,0x11,0x11,0x11,0x0a,0x04},
|
||||
{0x11,0x11,0x11,0x15,0x15,0x15,0x0a},
|
||||
{0x11,0x11,0x0a,0x04,0x0a,0x11,0x11},
|
||||
{0x11,0x11,0x0a,0x04,0x04,0x04,0x04},
|
||||
{0x1f,0x01,0x02,0x04,0x08,0x10,0x1f},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x00,0x00,0x11,0x0a,0x04,0x0a,0x11},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
{0x11,0x0e,0x11,0x1f,0x11,0x11,0x11},
|
||||
{0x11,0x0e,0x11,0x11,0x11,0x11,0x0e},
|
||||
{0x11,0x00,0x11,0x11,0x11,0x11,0x0e},
|
||||
};
|
||||
+13
-13
@@ -1,13 +1,13 @@
|
||||
/* 20x20 icons (bit 0x80000 = left), drawn by mameise for the ESP32 version */
|
||||
static const unsigned int g_icons[10][20]={
|
||||
{0x00000000,0x00000000,0x00000000,0x00003FC0,0x0000C030,0x00010008,0x00022044,0x00021084,0x00048012,0x00045022,0x00041002,0x0005080A,0x00048812,0x00020604,0x00020904,0x00011088,0x00009090,0x00009F90,0x000070E0,0x00000000},
|
||||
{0x00000000,0x00000000,0x00000000,0x000036C0,0x00004920,0x00004020,0x00003020,0x000010C0,0x00001080,0x00001F80,0x00002040,0x00004920,0x00008910,0x00008910,0x00008010,0x00009090,0x00004F20,0x00002040,0x00001F80,0x00000000},
|
||||
{0x00000000,0x00000000,0x00000000,0x00000070,0x000000C8,0x00000088,0x00000188,0x00000118,0x00007F10,0x0000C090,0x00019C60,0x00012420,0x0001C860,0x000150A0,0x00013F20,0x00008040,0x00004080,0x00008040,0x0001FFE0,0x00000000},
|
||||
{0x00000000,0x00000000,0x00000000,0x00001F80,0x000030C0,0x00006060,0x00004F20,0x000059A0,0x000050A0,0x000050A0,0x0000F0A0,0x0000F0A0,0x0000F0A0,0x000050A0,0x000050A0,0x000050A0,0x00004F20,0x00004020,0x00003FC0,0x00000000},
|
||||
{0x00000000,0x00000000,0x00000000,0x000118C0,0x0000BDE0,0x00023FE0,0x00014010,0x00004010,0x00034010,0x00004010,0x00002020,0x00001040,0x00001020,0x00002010,0x00002230,0x00004420,0x00004420,0x0000FFF0,0x0000FFF0,0x00000000},
|
||||
{0x00000000,0x00000000,0x00000078,0x00000186,0x00000102,0x00000102,0x00000106,0x000000B8,0x000000C0,0x00003E00,0x00004100,0x00008080,0x00011440,0x00011440,0x00010040,0x00012240,0x00009C80,0x00004100,0x00003E00,0x00000000},
|
||||
{0x00000000,0x00000000,0x00000000,0x00000F00,0x00001080,0x00002040,0x00002040,0x00007FE0,0x00008010,0x00010608,0x00010608,0x00010608,0x00013FC8,0x00013FC8,0x00010608,0x00010608,0x00010608,0x00010008,0x0000FFF0,0x00000000},
|
||||
{0x00000000,0x00000000,0x00000008,0x00000004,0x00000012,0x00000009,0x00000024,0x00000010,0x000030C0,0x000079E0,0x0000FFF0,0x0000FFF0,0x0000FFF0,0x0000FFF0,0x00007FE0,0x00003FC0,0x00001F80,0x00000F00,0x00000600,0x00000000},
|
||||
{0x00000000,0x00000000,0x00000000,0x0001C038,0x000230C4,0x00020904,0x00020604,0x0002C034,0x000230C4,0x00020004,0x0002C034,0x00023044,0x00020004,0x0002C034,0x000200C4,0x00020004,0x0002E074,0x00011988,0x00000600,0x00000000},
|
||||
{0x00000000,0x00000000,0x00000000,0x00000000,0x00003800,0x0000C600,0x00008200,0x00012900,0x00010100,0x00014570,0x0001388C,0x00008004,0x0000C482,0x00003AFA,0x00000252,0x00000104,0x0000018C,0x00000070,0x00000000,0x00000000},
|
||||
};
|
||||
/* 20x20 icons (bit 0x80000 = left), drawn by mameise for the ESP32 version */
|
||||
static const unsigned int g_icons[10][20]={
|
||||
{0x00000000,0x00000000,0x00000000,0x00003FC0,0x0000C030,0x00010008,0x00022044,0x00021084,0x00048012,0x00045022,0x00041002,0x0005080A,0x00048812,0x00020604,0x00020904,0x00011088,0x00009090,0x00009F90,0x000070E0,0x00000000},
|
||||
{0x00000000,0x00000000,0x00000000,0x000036C0,0x00004920,0x00004020,0x00003020,0x000010C0,0x00001080,0x00001F80,0x00002040,0x00004920,0x00008910,0x00008910,0x00008010,0x00009090,0x00004F20,0x00002040,0x00001F80,0x00000000},
|
||||
{0x00000000,0x00000000,0x00000000,0x00000070,0x000000C8,0x00000088,0x00000188,0x00000118,0x00007F10,0x0000C090,0x00019C60,0x00012420,0x0001C860,0x000150A0,0x00013F20,0x00008040,0x00004080,0x00008040,0x0001FFE0,0x00000000},
|
||||
{0x00000000,0x00000000,0x00000000,0x00001F80,0x000030C0,0x00006060,0x00004F20,0x000059A0,0x000050A0,0x000050A0,0x0000F0A0,0x0000F0A0,0x0000F0A0,0x000050A0,0x000050A0,0x000050A0,0x00004F20,0x00004020,0x00003FC0,0x00000000},
|
||||
{0x00000000,0x00000000,0x00000000,0x000118C0,0x0000BDE0,0x00023FE0,0x00014010,0x00004010,0x00034010,0x00004010,0x00002020,0x00001040,0x00001020,0x00002010,0x00002230,0x00004420,0x00004420,0x0000FFF0,0x0000FFF0,0x00000000},
|
||||
{0x00000000,0x00000000,0x00000078,0x00000186,0x00000102,0x00000102,0x00000106,0x000000B8,0x000000C0,0x00003E00,0x00004100,0x00008080,0x00011440,0x00011440,0x00010040,0x00012240,0x00009C80,0x00004100,0x00003E00,0x00000000},
|
||||
{0x00000000,0x00000000,0x00000000,0x00000F00,0x00001080,0x00002040,0x00002040,0x00007FE0,0x00008010,0x00010608,0x00010608,0x00010608,0x00013FC8,0x00013FC8,0x00010608,0x00010608,0x00010608,0x00010008,0x0000FFF0,0x00000000},
|
||||
{0x00000000,0x00000000,0x00000008,0x00000004,0x00000012,0x00000009,0x00000024,0x00000010,0x000030C0,0x000079E0,0x0000FFF0,0x0000FFF0,0x0000FFF0,0x0000FFF0,0x00007FE0,0x00003FC0,0x00001F80,0x00000F00,0x00000600,0x00000000},
|
||||
{0x00000000,0x00000000,0x00000000,0x0001C038,0x000230C4,0x00020904,0x00020604,0x0002C034,0x000230C4,0x00020004,0x0002C034,0x00023044,0x00020004,0x0002C034,0x000200C4,0x00020004,0x0002E074,0x00011988,0x00000600,0x00000000},
|
||||
{0x00000000,0x00000000,0x00000000,0x00000000,0x00003800,0x0000C600,0x00008200,0x00012900,0x00010100,0x00014570,0x0001388C,0x00008004,0x0000C482,0x00003AFA,0x00000252,0x00000104,0x0000018C,0x00000070,0x00000000,0x00000000},
|
||||
};
|
||||
+13
-13
@@ -1,13 +1,13 @@
|
||||
/* 20x20 icons of the Tamagotchi Friends (bit 0x80000 = left), from the pictures of mameise: 0..4 top, 5..9 bottom */
|
||||
static const unsigned int g_icons_fr[10][20]={
|
||||
{0x00000000,0x00000000,0x00000000,0x00000000,0x00001F80,0x0000E070,0x00010008,0x00014928,0x00012048,0x00010008,0x00010608,0x00008F10,0x00009F90,0x00007FE0,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000},
|
||||
{0x00000000,0x00015070,0x0002A888,0x0002A904,0x0002A904,0x00020904,0x00020904,0x00011088,0x0000A050,0x0000A050,0x00011088,0x00011088,0x00011088,0x00011088,0x00011088,0x00011088,0x00011088,0x0000E070,0x00000000,0x00000000},
|
||||
{0x00000000,0x00000000,0x00000000,0x00000070,0x000000C8,0x00000088,0x00000188,0x00000118,0x00007F10,0x0000C090,0x00019C60,0x00012420,0x0001C860,0x000150A0,0x00013F20,0x00008040,0x00004080,0x00008040,0x0001FFE0,0x00000000},
|
||||
{0x00000000,0x00000000,0x0000FFE0,0x0000E020,0x00009020,0x00008820,0x00008420,0x00008220,0x00008220,0x00008220,0x00008A20,0x00008E20,0x00008A20,0x00008220,0x000043E0,0x00002200,0x00001200,0x00000A00,0x00000600,0x00000000},
|
||||
{0x00000008,0x00000004,0x00000012,0x00000009,0x00000024,0x00000010,0x0000E0E0,0x000191F0,0x00037BF8,0x0002FFF8,0x0003FFF8,0x0003FFF8,0x0001FFF0,0x0000FFE0,0x00007FC0,0x00003F80,0x00001F00,0x00000E00,0x00000400,0x00000000},
|
||||
{0x00000000,0x00000000,0x00000000,0x00000000,0x0001FFF8,0x0003000C,0x00028014,0x00024924,0x00021F84,0x00021F84,0x00021F84,0x00024F24,0x00028614,0x0003000C,0x0001FFF8,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000},
|
||||
{0x00000000,0x00000000,0x00010000,0x00038000,0x00013FF0,0x00004428,0x00008848,0x00008858,0x0000FFE8,0x00008848,0x00008848,0x00008848,0x00008850,0x00008860,0x0000FFC0,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000},
|
||||
{0x00000000,0x00000000,0x00000000,0x0001C038,0x000230C4,0x00020904,0x00020604,0x0002C034,0x000230C4,0x00020004,0x0002C034,0x00023044,0x00020004,0x0002C034,0x000200C4,0x00020004,0x0002E074,0x00011988,0x00000600,0x00000000},
|
||||
{0x00000000,0x00000000,0x00000000,0x00000F00,0x00001080,0x00002040,0x00002040,0x00007FE0,0x00008010,0x00010608,0x00010608,0x00010608,0x00013FC8,0x00013FC8,0x00010608,0x00010608,0x00010608,0x00010008,0x0000FFF0,0x00000000},
|
||||
{0x00000000,0x00000000,0x00000000,0x00000000,0x00003800,0x0000C600,0x00008200,0x00012900,0x00010100,0x00014570,0x0001388C,0x00008004,0x0000C482,0x00003AFA,0x00000252,0x00000104,0x0000018C,0x00000070,0x00000000,0x00000000},
|
||||
};
|
||||
/* 20x20 icons of the Tamagotchi Friends (bit 0x80000 = left), from the pictures of mameise: 0..4 top, 5..9 bottom */
|
||||
static const unsigned int g_icons_fr[10][20]={
|
||||
{0x00000000,0x00000000,0x00000000,0x00000000,0x00001F80,0x0000E070,0x00010008,0x00014928,0x00012048,0x00010008,0x00010608,0x00008F10,0x00009F90,0x00007FE0,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000},
|
||||
{0x00000000,0x00015070,0x0002A888,0x0002A904,0x0002A904,0x00020904,0x00020904,0x00011088,0x0000A050,0x0000A050,0x00011088,0x00011088,0x00011088,0x00011088,0x00011088,0x00011088,0x00011088,0x0000E070,0x00000000,0x00000000},
|
||||
{0x00000000,0x00000000,0x00000000,0x00000070,0x000000C8,0x00000088,0x00000188,0x00000118,0x00007F10,0x0000C090,0x00019C60,0x00012420,0x0001C860,0x000150A0,0x00013F20,0x00008040,0x00004080,0x00008040,0x0001FFE0,0x00000000},
|
||||
{0x00000000,0x00000000,0x0000FFE0,0x0000E020,0x00009020,0x00008820,0x00008420,0x00008220,0x00008220,0x00008220,0x00008A20,0x00008E20,0x00008A20,0x00008220,0x000043E0,0x00002200,0x00001200,0x00000A00,0x00000600,0x00000000},
|
||||
{0x00000008,0x00000004,0x00000012,0x00000009,0x00000024,0x00000010,0x0000E0E0,0x000191F0,0x00037BF8,0x0002FFF8,0x0003FFF8,0x0003FFF8,0x0001FFF0,0x0000FFE0,0x00007FC0,0x00003F80,0x00001F00,0x00000E00,0x00000400,0x00000000},
|
||||
{0x00000000,0x00000000,0x00000000,0x00000000,0x0001FFF8,0x0003000C,0x00028014,0x00024924,0x00021F84,0x00021F84,0x00021F84,0x00024F24,0x00028614,0x0003000C,0x0001FFF8,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000},
|
||||
{0x00000000,0x00000000,0x00010000,0x00038000,0x00013FF0,0x00004428,0x00008848,0x00008858,0x0000FFE8,0x00008848,0x00008848,0x00008848,0x00008850,0x00008860,0x0000FFC0,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000},
|
||||
{0x00000000,0x00000000,0x00000000,0x0001C038,0x000230C4,0x00020904,0x00020604,0x0002C034,0x000230C4,0x00020004,0x0002C034,0x00023044,0x00020004,0x0002C034,0x000200C4,0x00020004,0x0002E074,0x00011988,0x00000600,0x00000000},
|
||||
{0x00000000,0x00000000,0x00000000,0x00000F00,0x00001080,0x00002040,0x00002040,0x00007FE0,0x00008010,0x00010608,0x00010608,0x00010608,0x00013FC8,0x00013FC8,0x00010608,0x00010608,0x00010608,0x00010008,0x0000FFF0,0x00000000},
|
||||
{0x00000000,0x00000000,0x00000000,0x00000000,0x00003800,0x0000C600,0x00008200,0x00012900,0x00010100,0x00014570,0x0001388C,0x00008004,0x0000C482,0x00003AFA,0x00000252,0x00000104,0x0000018C,0x00000070,0x00000000,0x00000000},
|
||||
};
|
||||
+24
-24
@@ -1,24 +1,24 @@
|
||||
/* 64-bit division for the sound timers (no compiler runtime in this build).
|
||||
* AEABI: quotient in r0:r1, remainder in r2:r3. */
|
||||
typedef unsigned long long u64; typedef long long s64;
|
||||
__attribute__((used)) u64 t7_udiv64(u64 n, u64 d, u64 *rem)
|
||||
{
|
||||
if (!d) { *rem = 0; return ~0ull; }
|
||||
u64 q = 0, r = 0;
|
||||
for (int i = 63; i >= 0; i--) { r = (r << 1) | ((n >> i) & 1u); if (r >= d) { r -= d; q |= 1ull << i; } }
|
||||
*rem = r; return q;
|
||||
}
|
||||
__attribute__((used)) s64 t7_sdiv64(s64 n, s64 d, s64 *rem)
|
||||
{
|
||||
int neg = (n < 0) ^ (d < 0), nn = n < 0; u64 r;
|
||||
u64 q = t7_udiv64(n < 0 ? (u64)-n : (u64)n, d < 0 ? (u64)-d : (u64)d, &r);
|
||||
*rem = nn ? -(s64)r : (s64)r; return neg ? -(s64)q : (s64)q;
|
||||
}
|
||||
__attribute__((naked)) void __aeabi_uldivmod(void)
|
||||
{
|
||||
__asm__ volatile("push {r4, lr}\n sub sp, #16\n add r4, sp, #8\n str r4, [sp]\n bl t7_udiv64\n ldrd r2, r3, [sp, #8]\n add sp, #16\n pop {r4, pc}\n");
|
||||
}
|
||||
__attribute__((naked)) void __aeabi_ldivmod(void)
|
||||
{
|
||||
__asm__ volatile("push {r4, lr}\n sub sp, #16\n add r4, sp, #8\n str r4, [sp]\n bl t7_sdiv64\n ldrd r2, r3, [sp, #8]\n add sp, #16\n pop {r4, pc}\n");
|
||||
}
|
||||
/* 64-bit division for the sound timers (no compiler runtime in this build).
|
||||
* AEABI: quotient in r0:r1, remainder in r2:r3. */
|
||||
typedef unsigned long long u64; typedef long long s64;
|
||||
__attribute__((used)) u64 t7_udiv64(u64 n, u64 d, u64 *rem)
|
||||
{
|
||||
if (!d) { *rem = 0; return ~0ull; }
|
||||
u64 q = 0, r = 0;
|
||||
for (int i = 63; i >= 0; i--) { r = (r << 1) | ((n >> i) & 1u); if (r >= d) { r -= d; q |= 1ull << i; } }
|
||||
*rem = r; return q;
|
||||
}
|
||||
__attribute__((used)) s64 t7_sdiv64(s64 n, s64 d, s64 *rem)
|
||||
{
|
||||
int neg = (n < 0) ^ (d < 0), nn = n < 0; u64 r;
|
||||
u64 q = t7_udiv64(n < 0 ? (u64)-n : (u64)n, d < 0 ? (u64)-d : (u64)d, &r);
|
||||
*rem = nn ? -(s64)r : (s64)r; return neg ? -(s64)q : (s64)q;
|
||||
}
|
||||
__attribute__((naked)) void __aeabi_uldivmod(void)
|
||||
{
|
||||
__asm__ volatile("push {r4, lr}\n sub sp, #16\n add r4, sp, #8\n str r4, [sp]\n bl t7_udiv64\n ldrd r2, r3, [sp, #8]\n add sp, #16\n pop {r4, pc}\n");
|
||||
}
|
||||
__attribute__((naked)) void __aeabi_ldivmod(void)
|
||||
{
|
||||
__asm__ volatile("push {r4, lr}\n sub sp, #16\n add r4, sp, #8\n str r4, [sp]\n bl t7_sdiv64\n ldrd r2, r3, [sp, #8]\n add sp, #16\n pop {r4, pc}\n");
|
||||
}
|
||||
+29
-29
@@ -1,29 +1,29 @@
|
||||
ENTRY(Reset_Handler)
|
||||
MEMORY {
|
||||
PRAM (rwx) : ORIGIN = 0x00000000, LENGTH = 0x10000
|
||||
SRAM (rw) : ORIGIN = 0x18000000, LENGTH = 0x1F000
|
||||
XL (r) : ORIGIN = 0x00010000, LENGTH = 0x8000 /* 1.6.12 (T7_JIT): load place of the second part (t7x.bin) */
|
||||
}
|
||||
SECTIONS {
|
||||
.text : {
|
||||
KEEP(*(.vectors))
|
||||
KEEP(*(.pmxcfg))
|
||||
*(EXCLUDE_FILE(*t7_jit.o) .text*)
|
||||
*(EXCLUDE_FILE(*t7_jit.o) .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
|
||||
/* the second part: the translator (t7_jit.c), in the SRAM behind .bss; the program copies it from the flash
|
||||
* (save area + 0x8000, written by the Studio) and runs without it when it is not there */
|
||||
.xcode ALIGN(8) : { __x_start = .; *t7_jit.o(.text* .rodata*) *(.xcode_bat) . = ALIGN(8); __x_end = .; } > SRAM AT > XL
|
||||
__x_load = LOADADDR(.xcode);
|
||||
ASSERT(__prog_end <= 0x10000, "program too big for PRAM")
|
||||
ASSERT(__x_end <= 0x1801F000, "RAM full")
|
||||
ASSERT(__bss_end <= 0x1801F000, "RAM full")
|
||||
/DISCARD/ : { *(.comment) *(.ARM.*) *(.note*) }
|
||||
}
|
||||
ENTRY(Reset_Handler)
|
||||
MEMORY {
|
||||
PRAM (rwx) : ORIGIN = 0x00000000, LENGTH = 0x10000
|
||||
SRAM (rw) : ORIGIN = 0x18000000, LENGTH = 0x1F000
|
||||
XL (r) : ORIGIN = 0x00010000, LENGTH = 0x8000 /* 1.6.12 (T7_JIT): load place of the second part (t7x.bin) */
|
||||
}
|
||||
SECTIONS {
|
||||
.text : {
|
||||
KEEP(*(.vectors))
|
||||
KEEP(*(.pmxcfg))
|
||||
*(EXCLUDE_FILE(*t7_jit.o) .text*)
|
||||
*(EXCLUDE_FILE(*t7_jit.o) .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
|
||||
/* the second part: the translator (t7_jit.c), in the SRAM behind .bss; the program copies it from the flash
|
||||
* (save area + 0x8000, written by the Studio) and runs without it when it is not there */
|
||||
.xcode ALIGN(8) : { __x_start = .; *t7_jit.o(.text* .rodata*) *(.xcode_bat) . = ALIGN(8); __x_end = .; } > SRAM AT > XL
|
||||
__x_load = LOADADDR(.xcode);
|
||||
ASSERT(__prog_end <= 0x10000, "program too big for PRAM")
|
||||
ASSERT(__x_end <= 0x1801F000, "RAM full")
|
||||
ASSERT(__bss_end <= 0x1801F000, "RAM full")
|
||||
/DISCARD/ : { *(.comment) *(.ARM.*) *(.note*) }
|
||||
}
|
||||
+846
-846
File diff suppressed because it is too large.
Load diff
+762
-762
File diff suppressed because it is too large.
Load diff
+95
-95
@@ -1,95 +1,95 @@
|
||||
/* T7 on the Paradise: the part that does not touch the hardware (screen layout, clock,
|
||||
* figure menu, saved state). Built for the device and for the PC test (host/). */
|
||||
#ifndef T7APP_H
|
||||
#define T7APP_H
|
||||
#include <stdint.h>
|
||||
|
||||
/* Settings block, filled in by the Studio (magic "PMX7"), at 0x140 in the program. */
|
||||
#define T7_FIG_MAX 16
|
||||
typedef struct { uint32_t off, len; char name[16]; } T7Fig;
|
||||
typedef struct {
|
||||
uint32_t magic, version, rom_off, rom_len, save_off, nfig;
|
||||
T7Fig fig[T7_FIG_MAX];
|
||||
uint32_t flags; /* bit 0: English, bit 1: clock from the Paradise at every start, bit 12: no session (T7_F_NOSESS) */
|
||||
uint32_t wall_cnt, wall_sod, wall_ymd; /* written by the Studio on the device: seconds counter, local time and date then (0 = none) */
|
||||
} T7Cfg;
|
||||
#define T7_MAGIC 0x37584D50u /* "PMX7" */
|
||||
#define T7_F_NOSESS 0x1000u /* 1.7.3 test7 (SESS_FLAG): run state never written nor loaded, only the EEPROM */
|
||||
|
||||
/* Settings on the device (wheel, short press). Stored with the clock note in their own sector. */
|
||||
typedef struct {
|
||||
uint8_t bg, ink, g1, g2, size, pixel, grid, pixvis, ghost, sound, light, sync, lang, r0, r1, r2;
|
||||
uint8_t pw, r3, r4, r5; /* 1.6.12 test1 (T7_PW): power saving while the screen is off, 0 normal, 1 strong (12 MHz),
|
||||
2 maximal (12 MHz and deep sleep between the seconds); others (older settings: 0xFF) = PW_DEF */
|
||||
} T7Set;
|
||||
#define PW_DEF 1u
|
||||
#ifdef T7_FIXED
|
||||
/* 1.6.12 test4: power save maximal and the JIT always (tested over night: the call still rings and lights the screen) */
|
||||
static inline int app_pw(void) { return 2; }
|
||||
#else
|
||||
static inline int app_pw(void) { extern T7Set g_set; return g_set.pw <= 2u ? g_set.pw : (int)PW_DEF; }
|
||||
#endif
|
||||
extern T7Set g_set;
|
||||
/* 1.6.6: light = time without a key until the screen dims (0..6: 10 s .. 10 min, 7 = never),
|
||||
* r0 = dim level (1, 5, 10, 25, 50 %), r2 = time until the screen goes off (0 = never, 1..7 = 10 s .. 10 min,
|
||||
* always longer than the dim time), r1: bits 0..3 = 2 (this layout). Dimmed = darker colours (the backlight PWM had no effect on the device). */
|
||||
#define T7_T_N 7
|
||||
static inline uint32_t t7_t_ms(int i) { static const uint32_t t[T7_T_N] = { 10000u, 20000u, 30000u, 60000u, 120000u, 300000u, 600000u }; return t[i % T7_T_N]; }
|
||||
static inline uint32_t app_dim_ms(void) { return g_set.light >= T7_T_N ? 0u : t7_t_ms(g_set.light); }
|
||||
static inline uint32_t app_off_ms(void) { return g_set.r2 ? t7_t_ms(g_set.r2 - 1) : 0u; }
|
||||
#ifdef BL_PWM
|
||||
/* 1.6.8 (test45): layout 3. r0 = dim level 1..50 % (10 steps), r1 bits 4..7 = the light in general:
|
||||
* 0 = 100 %, 1..10 = 5, 10, 20 .. 90 %. Dimmed is never brighter than the light in general. */
|
||||
#define T7_DIM_N 10
|
||||
#define T7_BL_N 11
|
||||
static inline int app_bl_pct(void) { static const uint8_t p[T7_BL_N] = { 100, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90 }; return p[((g_set.r1 >> 4) & 15) % T7_BL_N]; }
|
||||
static inline int app_dim_pct(void) { static const uint8_t p[T7_DIM_N] = { 1, 2, 3, 5, 10, 15, 20, 30, 40, 50 }; int d = p[g_set.r0 % T7_DIM_N], b = app_bl_pct(); return d < b ? d : b; }
|
||||
#else
|
||||
static inline int app_dim_pct(void) { static const uint8_t p[5] = { 1, 5, 10, 25, 50 }; return p[g_set.r0 % 5]; }
|
||||
#endif
|
||||
void app_dim(int pct);
|
||||
int app_bell(void); /* 1 = the bell icon is on (the pet calls) */
|
||||
#define T7_STORE_LEN 64u
|
||||
|
||||
/* Platform (device or PC test) */
|
||||
int plat_store_load(uint8_t *b, uint32_t n); /* bytes of the settings sector (0 = empty) */
|
||||
void plat_store_write(const uint8_t *b, uint32_t n);
|
||||
uint32_t plat_rtc(void);
|
||||
#ifdef T7_JIT
|
||||
/* 1.6.12 test2: r3 bit 0 = JIT off (0xFF of older settings: on) */
|
||||
#if defined(T7_FIXED) && !defined(T7_DIAG)
|
||||
static inline int app_jit(void) { return 1; }
|
||||
#else
|
||||
static inline int app_jit(void) { extern T7Set g_set; return g_set.r3 == 0xFFu || !(g_set.r3 & 1u); }
|
||||
#endif
|
||||
int plat_jit(int on); /* switch, -1: the second part is missing */
|
||||
#endif
|
||||
int plat_pw_pct(void); /* T7_PW: % asleep * 1000 + % at 12 MHz, since the start */
|
||||
#ifdef T7_VOL
|
||||
/* 1.6.12 test3: g_set.sound = volume 0 off, 1 quiet (as before), 2 medium, 3 loud (test5: maximal 1/2 was quieter than loud, gone) */
|
||||
#define T7_VOL_N 4
|
||||
void plat_vol(int v);
|
||||
#endif
|
||||
void plat_bat(char *s); /* BAT_SHOW: "2.84V 80%" (12 bytes) */ /* seconds counter, keeps running while switched off */
|
||||
void plat_rect(int x0, int y0, int x1, int y1); /* start a rectangle on the screen */
|
||||
void plat_row(const uint16_t *px, int n); /* pixels of the rectangle, row by row */
|
||||
void plat_rect_end(void);
|
||||
const uint8_t *plat_fig_ptr(int i); /* figure data (flash) */
|
||||
|
||||
/* App */
|
||||
void app_init(const volatile T7Cfg *cfg);
|
||||
void app_render(int force); /* draw what changed */
|
||||
void app_text_screen(const char *line1, const char *line2, int progress, int total); /* simple message */
|
||||
int app_keys(uint32_t buttons, uint32_t now_ms); /* buttons A=1 B=2 C=4 wheel=8 -> mask for the game (menu takes them) */
|
||||
void app_tick(uint32_t now_ms); /* figure change in progress */
|
||||
void app_clock(uint32_t rtc, int guard); /* keep the clock on the seconds counter (every 2 s) */
|
||||
int app_catchup(uint32_t elapsed_s, void (*progress)(int done, int total)); /* after a pause: let the hours pass */
|
||||
uint32_t app_save(uint8_t *buf, uint32_t cap); /* state for the flash (0 = too small) */
|
||||
int app_load(const uint8_t *buf, uint32_t len, uint32_t *saved_rtc);
|
||||
int app_menu_open(void);
|
||||
int app_lang(void); /* 0 German, 1 English */
|
||||
void app_setup_fill(uint32_t rtc); /* every 0.5 s: the game asks for the time, fill it in */
|
||||
void app_store(void); /* settings and clock note into the flash (on leaving) */
|
||||
extern uint32_t g_para_time, g_para_cnt; /* the Paradise's time at the start (platform), 0 = unknown */
|
||||
int app_clock_hard(uint32_t rtc); /* settings flag 2: game clock = the Paradise's time (1 = done) */
|
||||
#endif
|
||||
/* T7 on the Paradise: the part that does not touch the hardware (screen layout, clock,
|
||||
* figure menu, saved state). Built for the device and for the PC test (host/). */
|
||||
#ifndef T7APP_H
|
||||
#define T7APP_H
|
||||
#include <stdint.h>
|
||||
|
||||
/* Settings block, filled in by the Studio (magic "PMX7"), at 0x140 in the program. */
|
||||
#define T7_FIG_MAX 16
|
||||
typedef struct { uint32_t off, len; char name[16]; } T7Fig;
|
||||
typedef struct {
|
||||
uint32_t magic, version, rom_off, rom_len, save_off, nfig;
|
||||
T7Fig fig[T7_FIG_MAX];
|
||||
uint32_t flags; /* bit 0: English, bit 1: clock from the Paradise at every start, bit 12: no session (T7_F_NOSESS) */
|
||||
uint32_t wall_cnt, wall_sod, wall_ymd; /* written by the Studio on the device: seconds counter, local time and date then (0 = none) */
|
||||
} T7Cfg;
|
||||
#define T7_MAGIC 0x37584D50u /* "PMX7" */
|
||||
#define T7_F_NOSESS 0x1000u /* 1.7.3 test7 (SESS_FLAG): run state never written nor loaded, only the EEPROM */
|
||||
|
||||
/* Settings on the device (wheel, short press). Stored with the clock note in their own sector. */
|
||||
typedef struct {
|
||||
uint8_t bg, ink, g1, g2, size, pixel, grid, pixvis, ghost, sound, light, sync, lang, r0, r1, r2;
|
||||
uint8_t pw, r3, r4, r5; /* 1.6.12 test1 (T7_PW): power saving while the screen is off, 0 normal, 1 strong (12 MHz),
|
||||
2 maximal (12 MHz and deep sleep between the seconds); others (older settings: 0xFF) = PW_DEF */
|
||||
} T7Set;
|
||||
#define PW_DEF 1u
|
||||
#ifdef T7_FIXED
|
||||
/* 1.6.12 test4: power save maximal and the JIT always (tested over night: the call still rings and lights the screen) */
|
||||
static inline int app_pw(void) { return 2; }
|
||||
#else
|
||||
static inline int app_pw(void) { extern T7Set g_set; return g_set.pw <= 2u ? g_set.pw : (int)PW_DEF; }
|
||||
#endif
|
||||
extern T7Set g_set;
|
||||
/* 1.6.6: light = time without a key until the screen dims (0..6: 10 s .. 10 min, 7 = never),
|
||||
* r0 = dim level (1, 5, 10, 25, 50 %), r2 = time until the screen goes off (0 = never, 1..7 = 10 s .. 10 min,
|
||||
* always longer than the dim time), r1: bits 0..3 = 2 (this layout). Dimmed = darker colours (the backlight PWM had no effect on the device). */
|
||||
#define T7_T_N 7
|
||||
static inline uint32_t t7_t_ms(int i) { static const uint32_t t[T7_T_N] = { 10000u, 20000u, 30000u, 60000u, 120000u, 300000u, 600000u }; return t[i % T7_T_N]; }
|
||||
static inline uint32_t app_dim_ms(void) { return g_set.light >= T7_T_N ? 0u : t7_t_ms(g_set.light); }
|
||||
static inline uint32_t app_off_ms(void) { return g_set.r2 ? t7_t_ms(g_set.r2 - 1) : 0u; }
|
||||
#ifdef BL_PWM
|
||||
/* 1.6.8 (test45): layout 3. r0 = dim level 1..50 % (10 steps), r1 bits 4..7 = the light in general:
|
||||
* 0 = 100 %, 1..10 = 5, 10, 20 .. 90 %. Dimmed is never brighter than the light in general. */
|
||||
#define T7_DIM_N 10
|
||||
#define T7_BL_N 11
|
||||
static inline int app_bl_pct(void) { static const uint8_t p[T7_BL_N] = { 100, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90 }; return p[((g_set.r1 >> 4) & 15) % T7_BL_N]; }
|
||||
static inline int app_dim_pct(void) { static const uint8_t p[T7_DIM_N] = { 1, 2, 3, 5, 10, 15, 20, 30, 40, 50 }; int d = p[g_set.r0 % T7_DIM_N], b = app_bl_pct(); return d < b ? d : b; }
|
||||
#else
|
||||
static inline int app_dim_pct(void) { static const uint8_t p[5] = { 1, 5, 10, 25, 50 }; return p[g_set.r0 % 5]; }
|
||||
#endif
|
||||
void app_dim(int pct);
|
||||
int app_bell(void); /* 1 = the bell icon is on (the pet calls) */
|
||||
#define T7_STORE_LEN 64u
|
||||
|
||||
/* Platform (device or PC test) */
|
||||
int plat_store_load(uint8_t *b, uint32_t n); /* bytes of the settings sector (0 = empty) */
|
||||
void plat_store_write(const uint8_t *b, uint32_t n);
|
||||
uint32_t plat_rtc(void);
|
||||
#ifdef T7_JIT
|
||||
/* 1.6.12 test2: r3 bit 0 = JIT off (0xFF of older settings: on) */
|
||||
#if defined(T7_FIXED) && !defined(T7_DIAG)
|
||||
static inline int app_jit(void) { return 1; }
|
||||
#else
|
||||
static inline int app_jit(void) { extern T7Set g_set; return g_set.r3 == 0xFFu || !(g_set.r3 & 1u); }
|
||||
#endif
|
||||
int plat_jit(int on); /* switch, -1: the second part is missing */
|
||||
#endif
|
||||
int plat_pw_pct(void); /* T7_PW: % asleep * 1000 + % at 12 MHz, since the start */
|
||||
#ifdef T7_VOL
|
||||
/* 1.6.12 test3: g_set.sound = volume 0 off, 1 quiet (as before), 2 medium, 3 loud (test5: maximal 1/2 was quieter than loud, gone) */
|
||||
#define T7_VOL_N 4
|
||||
void plat_vol(int v);
|
||||
#endif
|
||||
void plat_bat(char *s); /* BAT_SHOW: "2.84V 80%" (12 bytes) */ /* seconds counter, keeps running while switched off */
|
||||
void plat_rect(int x0, int y0, int x1, int y1); /* start a rectangle on the screen */
|
||||
void plat_row(const uint16_t *px, int n); /* pixels of the rectangle, row by row */
|
||||
void plat_rect_end(void);
|
||||
const uint8_t *plat_fig_ptr(int i); /* figure data (flash) */
|
||||
|
||||
/* App */
|
||||
void app_init(const volatile T7Cfg *cfg);
|
||||
void app_render(int force); /* draw what changed */
|
||||
void app_text_screen(const char *line1, const char *line2, int progress, int total); /* simple message */
|
||||
int app_keys(uint32_t buttons, uint32_t now_ms); /* buttons A=1 B=2 C=4 wheel=8 -> mask for the game (menu takes them) */
|
||||
void app_tick(uint32_t now_ms); /* figure change in progress */
|
||||
void app_clock(uint32_t rtc, int guard); /* keep the clock on the seconds counter (every 2 s) */
|
||||
int app_catchup(uint32_t elapsed_s, void (*progress)(int done, int total)); /* after a pause: let the hours pass */
|
||||
uint32_t app_save(uint8_t *buf, uint32_t cap); /* state for the flash (0 = too small) */
|
||||
int app_load(const uint8_t *buf, uint32_t len, uint32_t *saved_rtc);
|
||||
int app_menu_open(void);
|
||||
int app_lang(void); /* 0 German, 1 English */
|
||||
void app_setup_fill(uint32_t rtc); /* every 0.5 s: the game asks for the time, fill it in */
|
||||
void app_store(void); /* settings and clock note into the flash (on leaving) */
|
||||
extern uint32_t g_para_time, g_para_cnt; /* the Paradise's time at the start (platform), 0 = unknown */
|
||||
int app_clock_hard(uint32_t rtc); /* settings flag 2: game clock = the Paradise's time (1 = done) */
|
||||
#endif
|
||||
+16
-16
@@ -1,16 +1,16 @@
|
||||
/* Figur-ROMs (Figuren).
|
||||
Werden zur Laufzeit von der SD-Karte in die figcache-Flash-Partition kopiert
|
||||
und per esp_partition_mmap eingeblendet -- die Logik dazu steht in main.cpp.
|
||||
Hier nur der aktive Zeiger + Laenge, die der Memory-Layer (t7_memory.c)
|
||||
ueber figure_rom_ptr()/figure_rom_length() abfragt.
|
||||
(Frueher lagen hier die 3 ~1.7MB grossen eingebetteten Arrays.) */
|
||||
|
||||
static const unsigned char* g_fig_ptr = 0;
|
||||
static unsigned int g_fig_len = 0;
|
||||
|
||||
/* Von main.cpp aufgerufen, nachdem eine Figur in figcache gemappt wurde.
|
||||
p=0, n=0 => keine Figur aktiv (Lite / Aus). */
|
||||
void figure_rom_set_active(const unsigned char* p, unsigned int n){ g_fig_ptr = p; g_fig_len = n; }
|
||||
|
||||
const unsigned char* figure_rom_ptr(void){ return g_fig_ptr; }
|
||||
unsigned int figure_rom_length(void){ return g_fig_len; }
|
||||
/* Figur-ROMs (Figuren).
|
||||
Werden zur Laufzeit von der SD-Karte in die figcache-Flash-Partition kopiert
|
||||
und per esp_partition_mmap eingeblendet -- die Logik dazu steht in main.cpp.
|
||||
Hier nur der aktive Zeiger + Laenge, die der Memory-Layer (t7_memory.c)
|
||||
ueber figure_rom_ptr()/figure_rom_length() abfragt.
|
||||
(Frueher lagen hier die 3 ~1.7MB grossen eingebetteten Arrays.) */
|
||||
|
||||
static const unsigned char* g_fig_ptr = 0;
|
||||
static unsigned int g_fig_len = 0;
|
||||
|
||||
/* Von main.cpp aufgerufen, nachdem eine Figur in figcache gemappt wurde.
|
||||
p=0, n=0 => keine Figur aktiv (Lite / Aus). */
|
||||
void figure_rom_set_active(const unsigned char* p, unsigned int n){ g_fig_ptr = p; g_fig_len = n; }
|
||||
|
||||
const unsigned char* figure_rom_ptr(void){ return g_fig_ptr; }
|
||||
unsigned int figure_rom_length(void){ return g_fig_len; }
|
||||
+85
-85
@@ -1,85 +1,85 @@
|
||||
/*
|
||||
* pebble_compat.h - ESP32-C6 Variante des Pebble-API-Shims.
|
||||
*
|
||||
* Stellt exakt die Symbole bereit die der T7Emu-Emulator-Kern erwartet,
|
||||
* damit t7.c / t7_cpu.c / t7_memory.c / t7_eeprom.c
|
||||
* unveraendert kompilieren:
|
||||
*
|
||||
* - APP_LOG -> tama_log() (Serial/USB-CDC)
|
||||
* - ResHandle + resource_* -> ROM aus LittleFS-Datei "/t7emu.bin"
|
||||
* - persist_* -> NVS (Preferences), 16 Pages a 256 Byte
|
||||
*
|
||||
* Implementierung in pebble_compat.cpp (braucht C++ wegen LittleFS/Preferences).
|
||||
* Header ist sowohl von C (Kern) als auch C++ (compat/main) inkludierbar.
|
||||
*/
|
||||
|
||||
#ifndef PEBBLE_COMPAT_H
|
||||
#define PEBBLE_COMPAT_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#ifdef T7_FREESTANDING
|
||||
/* Paradise: no C library, only these from libc.c */
|
||||
void *memset(void *d, int c, size_t n);
|
||||
void *memcpy(void *d, const void *s, size_t n);
|
||||
#else
|
||||
#include <stdlib.h> // malloc/free (Core nutzt sie ueber diesen Header)
|
||||
#include <string.h> // memset/memcpy/strncpy
|
||||
#include <stdio.h> // snprintf
|
||||
#include <time.h>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// --- Logging --------------------------------------------------------------
|
||||
#define APP_LOG_LEVEL_ERROR 0
|
||||
#define APP_LOG_LEVEL_WARNING 1
|
||||
#define APP_LOG_LEVEL_INFO 2
|
||||
#define APP_LOG_LEVEL_DEBUG 3
|
||||
|
||||
void tama_log(const char *fmt, ...);
|
||||
#define APP_LOG(level, fmt, ...) tama_log("[tama] " fmt "\n", ##__VA_ARGS__)
|
||||
|
||||
// --- Resource-API (ROM) ---------------------------------------------------
|
||||
typedef int ResHandle;
|
||||
|
||||
// Vor t7_init() aufrufen. Pfad der ROM-Datei im LittleFS.
|
||||
void t7_compat_set_rom_path(const char *path);
|
||||
// Vor t7_init() aufrufen. NVS-Namespace fuer das Save-EEPROM.
|
||||
void t7_compat_set_eeprom_path(const char *ns);
|
||||
|
||||
ResHandle resource_get_handle(int id);
|
||||
size_t resource_size(ResHandle h);
|
||||
size_t resource_load_byte_range(ResHandle h, size_t offset,
|
||||
uint8_t *out, size_t len);
|
||||
|
||||
// Direktzeiger auf den memory-mapped ROM (XIP-Flash) oder NULL.
|
||||
// Erlaubt zero-copy ROM-Banking (Page-Table zeigt direkt in den Flash).
|
||||
const uint8_t *t7_compat_rom_mapped(void);
|
||||
|
||||
#define RESOURCE_ID_TAMA_ROM 1
|
||||
|
||||
// --- Persist-API (Save-EEPROM) --------------------------------------------
|
||||
typedef int status_t;
|
||||
#define S_SUCCESS 0
|
||||
|
||||
bool persist_exists(uint32_t key);
|
||||
int persist_read_data(uint32_t key, void *buf, size_t size);
|
||||
status_t persist_write_data(uint32_t key, const void *buf, size_t size);
|
||||
status_t persist_delete(uint32_t key);
|
||||
|
||||
// Schreibt dirty Pages nach NVS. Periodisch + vor Sleep/Shutdown aufrufen.
|
||||
void persist_flush_to_disk(void);
|
||||
int persist_page_count(void);
|
||||
int persist_page_size(void);
|
||||
int persist_backup_page(int page, uint8_t *out);
|
||||
void persist_restore_page(int page, const uint8_t *in);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // PEBBLE_COMPAT_H
|
||||
/*
|
||||
* pebble_compat.h - ESP32-C6 Variante des Pebble-API-Shims.
|
||||
*
|
||||
* Stellt exakt die Symbole bereit die der T7Emu-Emulator-Kern erwartet,
|
||||
* damit t7.c / t7_cpu.c / t7_memory.c / t7_eeprom.c
|
||||
* unveraendert kompilieren:
|
||||
*
|
||||
* - APP_LOG -> tama_log() (Serial/USB-CDC)
|
||||
* - ResHandle + resource_* -> ROM aus LittleFS-Datei "/t7emu.bin"
|
||||
* - persist_* -> NVS (Preferences), 16 Pages a 256 Byte
|
||||
*
|
||||
* Implementierung in pebble_compat.cpp (braucht C++ wegen LittleFS/Preferences).
|
||||
* Header ist sowohl von C (Kern) als auch C++ (compat/main) inkludierbar.
|
||||
*/
|
||||
|
||||
#ifndef PEBBLE_COMPAT_H
|
||||
#define PEBBLE_COMPAT_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#ifdef T7_FREESTANDING
|
||||
/* Paradise: no C library, only these from libc.c */
|
||||
void *memset(void *d, int c, size_t n);
|
||||
void *memcpy(void *d, const void *s, size_t n);
|
||||
#else
|
||||
#include <stdlib.h> // malloc/free (Core nutzt sie ueber diesen Header)
|
||||
#include <string.h> // memset/memcpy/strncpy
|
||||
#include <stdio.h> // snprintf
|
||||
#include <time.h>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// --- Logging --------------------------------------------------------------
|
||||
#define APP_LOG_LEVEL_ERROR 0
|
||||
#define APP_LOG_LEVEL_WARNING 1
|
||||
#define APP_LOG_LEVEL_INFO 2
|
||||
#define APP_LOG_LEVEL_DEBUG 3
|
||||
|
||||
void tama_log(const char *fmt, ...);
|
||||
#define APP_LOG(level, fmt, ...) tama_log("[tama] " fmt "\n", ##__VA_ARGS__)
|
||||
|
||||
// --- Resource-API (ROM) ---------------------------------------------------
|
||||
typedef int ResHandle;
|
||||
|
||||
// Vor t7_init() aufrufen. Pfad der ROM-Datei im LittleFS.
|
||||
void t7_compat_set_rom_path(const char *path);
|
||||
// Vor t7_init() aufrufen. NVS-Namespace fuer das Save-EEPROM.
|
||||
void t7_compat_set_eeprom_path(const char *ns);
|
||||
|
||||
ResHandle resource_get_handle(int id);
|
||||
size_t resource_size(ResHandle h);
|
||||
size_t resource_load_byte_range(ResHandle h, size_t offset,
|
||||
uint8_t *out, size_t len);
|
||||
|
||||
// Direktzeiger auf den memory-mapped ROM (XIP-Flash) oder NULL.
|
||||
// Erlaubt zero-copy ROM-Banking (Page-Table zeigt direkt in den Flash).
|
||||
const uint8_t *t7_compat_rom_mapped(void);
|
||||
|
||||
#define RESOURCE_ID_TAMA_ROM 1
|
||||
|
||||
// --- Persist-API (Save-EEPROM) --------------------------------------------
|
||||
typedef int status_t;
|
||||
#define S_SUCCESS 0
|
||||
|
||||
bool persist_exists(uint32_t key);
|
||||
int persist_read_data(uint32_t key, void *buf, size_t size);
|
||||
status_t persist_write_data(uint32_t key, const void *buf, size_t size);
|
||||
status_t persist_delete(uint32_t key);
|
||||
|
||||
// Schreibt dirty Pages nach NVS. Periodisch + vor Sleep/Shutdown aufrufen.
|
||||
void persist_flush_to_disk(void);
|
||||
int persist_page_count(void);
|
||||
int persist_page_size(void);
|
||||
int persist_backup_page(int page, uint8_t *out);
|
||||
void persist_restore_page(int page, const uint8_t *in);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // PEBBLE_COMPAT_H
|
||||
+435
-435
@@ -1,435 +1,435 @@
|
||||
/*
|
||||
* t7.c - top-level emulator integration
|
||||
*
|
||||
* Provides the public API declared in t7.h.
|
||||
*/
|
||||
|
||||
#include "t7_internal.h"
|
||||
#include "t7_eeprom.h"
|
||||
#include "t7_sound.h"
|
||||
#ifdef T7_FASTRUN
|
||||
uint32_t t7_cpu_run(uint32_t budget);
|
||||
#endif
|
||||
|
||||
// ----- Global state definitions ------------------------------------------
|
||||
//
|
||||
// Declared extern in t7_internal.h. Living as plain globals (rather
|
||||
// than fields of a g_sys struct) lets the compiler resolve addresses at
|
||||
// compile time and skip a pointer-indirection on every hot-path memory
|
||||
// access. ~52 KB total, all in .bss.
|
||||
|
||||
cpu6502_t g_cpu;
|
||||
uint8_t g_wram[T7_WRAM_SIZE];
|
||||
uint8_t g_dram[T7_DRAM_SIZE];
|
||||
uint8_t g_cpureg[T7_CPUREG_SIZE];
|
||||
|
||||
#if T7_PROFILE
|
||||
// Profiling counters — see t7_internal.h. Plain global to let the
|
||||
// linker resolve direct addresses (one less indirection vs a struct
|
||||
// member access through a pointer).
|
||||
t7_profile_t g_t7_profile;
|
||||
#endif
|
||||
uint8_t *g_rom_bank_buf = NULL; // malloc'd in t7_init
|
||||
uint8_t *g_static_rom = NULL; // malloc'd in t7_init
|
||||
uint16_t g_irq_vectors[16];
|
||||
uint8_t g_irq_pending_any;
|
||||
uint8_t g_rom_bank_id;
|
||||
bool g_rom_loaded;
|
||||
uint8_t g_keys;
|
||||
ResHandle g_rom_resource;
|
||||
bool g_initialised;
|
||||
|
||||
// ----- Init / Release -----------------------------------------------------
|
||||
|
||||
bool t7_init(void)
|
||||
{
|
||||
if (g_initialised) {
|
||||
APP_LOG(APP_LOG_LEVEL_WARNING, "t7_init: already initialised");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Allocate the big ROM caches on the heap. They have to live somewhere
|
||||
// and putting them in .bss pushes us over Pebble's 16-bit virtual-size
|
||||
// limit (65535 bytes). The heap on Emery has plenty of room.
|
||||
#ifdef T7_NOMALLOC
|
||||
/* Paradise: no heap. The banks are read straight from the mapped flash, only the
|
||||
* static ROM is copied (16 KB in RAM). */
|
||||
{ static uint8_t s_static[T7_STATIC_ROM_SIZE]; static uint8_t s_bank1[256];
|
||||
g_static_rom = s_static; g_rom_bank_buf = s_bank1; }
|
||||
#else
|
||||
g_rom_bank_buf = (uint8_t *)malloc(T7_ROM_BANK_SIZE);
|
||||
g_static_rom = (uint8_t *)malloc(T7_STATIC_ROM_SIZE);
|
||||
#endif
|
||||
#ifndef T7_NOMALLOC
|
||||
if (!g_rom_bank_buf || !g_static_rom) {
|
||||
APP_LOG(APP_LOG_LEVEL_ERROR,
|
||||
"t7_init: out of memory (bank=%p static=%p)",
|
||||
g_rom_bank_buf, g_static_rom);
|
||||
if (g_rom_bank_buf) { free(g_rom_bank_buf); g_rom_bank_buf = NULL; }
|
||||
if (g_static_rom) { free(g_static_rom); g_static_rom = NULL; }
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
memset(&g_cpu, 0, sizeof(g_cpu));
|
||||
memset(g_wram, 0, sizeof(g_wram));
|
||||
memset(g_dram, 0, sizeof(g_dram));
|
||||
memset(g_cpureg, 0, sizeof(g_cpureg));
|
||||
#ifndef T7_NOMALLOC
|
||||
memset(g_rom_bank_buf, 0, T7_ROM_BANK_SIZE);
|
||||
#endif
|
||||
memset(g_static_rom, 0, T7_STATIC_ROM_SIZE);
|
||||
g_keys = 0xFF;
|
||||
g_rom_bank_id = 0xFF;
|
||||
g_rom_loaded = false;
|
||||
|
||||
if (!t7_load_static_rom()) return false;
|
||||
t7_idle_check();
|
||||
t7_set_rom_bank(0);
|
||||
t7_eeprom_init(); // load save data from persist
|
||||
t7_cpu_reset();
|
||||
|
||||
g_initialised = true;
|
||||
APP_LOG(APP_LOG_LEVEL_INFO,
|
||||
"t7_init: ready, PC=$%04x", g_cpu.pc);
|
||||
return true;
|
||||
}
|
||||
|
||||
void t7_release(void)
|
||||
{
|
||||
// Flush any pending EEPROM writes back to persist before we tear down.
|
||||
// Without this, save data written between the last STOP-after-WRITE
|
||||
// and app exit would be lost.
|
||||
t7_eeprom_flush();
|
||||
#ifndef T7_NOMALLOC
|
||||
if (g_rom_bank_buf) { free(g_rom_bank_buf); g_rom_bank_buf = NULL; }
|
||||
if (g_static_rom) { free(g_static_rom); g_static_rom = NULL; }
|
||||
#endif
|
||||
g_initialised = false;
|
||||
}
|
||||
|
||||
void t7_reset(void)
|
||||
{
|
||||
if (!g_initialised) return;
|
||||
t7_cpu_reset();
|
||||
}
|
||||
|
||||
// ----- Stepping -----------------------------------------------------------
|
||||
|
||||
uint8_t T7_HOT t7_step_one(void)
|
||||
{
|
||||
if (!g_initialised) return 0;
|
||||
#ifdef T7_FASTRUN
|
||||
return (uint8_t)t7_cpu_run(1);
|
||||
#else
|
||||
return t7_cpu_step();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/* Tamagotchi Friends: its frame tick comes from IRQ 3, every 122nd one (counter $55). Instead of
|
||||
* 7.3 kHz of interrupts: at the video rate set the counter just below and fire IRQ 3 once. */
|
||||
static inline void t7_friends_tick(void)
|
||||
{
|
||||
if (!g_t7_static_off) return;
|
||||
if (g_wram[0x55] < 0x79) g_wram[0x55] = 0x79;
|
||||
t7_fire_irq(3);
|
||||
}
|
||||
static uint32_t s_tbh_accumulator = 0;
|
||||
#ifdef T7_TBH_RATE
|
||||
/* 1.6.12 test2: the 2 Hz timer of the game clock in real time when the program runs the core slower than 4 MHz
|
||||
* (Paradise: 3.03 MHz for the tempo): at 4 MHz cycles a game second lasted 1.32 s and the clock note jumped it ahead */
|
||||
#define TBH_RATE (T7_TBH_RATE)
|
||||
#else
|
||||
#define TBH_RATE (T7_CLOCK_RATE / 2)
|
||||
#endif
|
||||
#ifdef T7_VIDEO_CYCLES
|
||||
/* Paradise (as the ESP32 port): video IRQ at 60 Hz of emulated time, independent of how
|
||||
* often and with how many cycles this is called. */
|
||||
#define VIDEO_HZ 60
|
||||
#define VIDEO_PERIOD_CYCLES (T7_CLOCK_RATE / VIDEO_HZ)
|
||||
static uint32_t s_video_accumulator = 0;
|
||||
uint32_t T7_HOT t7_step_cycles(uint32_t target_cycles)
|
||||
{
|
||||
if (!g_initialised) return 0;
|
||||
uint32_t spent = 0;
|
||||
while (spent < target_cycles) {
|
||||
uint32_t step = target_cycles - spent;
|
||||
uint32_t to_video = VIDEO_PERIOD_CYCLES - s_video_accumulator;
|
||||
uint32_t to_tbh = TBH_RATE - s_tbh_accumulator;
|
||||
if (to_video < step) step = to_video;
|
||||
if (to_tbh < step) step = to_tbh;
|
||||
if (step == 0) step = 1;
|
||||
uint32_t did = 0;
|
||||
#ifdef T7_FASTRUN
|
||||
did = t7_cpu_run(step);
|
||||
#else
|
||||
if (g_snd_on) {
|
||||
while (did < step) {
|
||||
uint32_t c = t7_cpu_step();
|
||||
did += c;
|
||||
if ((g_snd_next -= (int32_t)c) <= 0) t7_snd_service();
|
||||
}
|
||||
} else {
|
||||
while (did < step) did += t7_cpu_step();
|
||||
}
|
||||
#endif
|
||||
spent += did;
|
||||
s_video_accumulator += did;
|
||||
if (s_video_accumulator >= VIDEO_PERIOD_CYCLES) {
|
||||
s_video_accumulator -= VIDEO_PERIOD_CYCLES;
|
||||
t7_fire_irq(10);
|
||||
t7_fire_nmi(6);
|
||||
t7_friends_tick();
|
||||
}
|
||||
s_tbh_accumulator += did;
|
||||
if (s_tbh_accumulator >= TBH_RATE) {
|
||||
s_tbh_accumulator -= TBH_RATE;
|
||||
t7_fire_irq(13);
|
||||
}
|
||||
}
|
||||
if (g_snd_on) t7_snd_flush();
|
||||
#ifdef T7_STOP_KEYACT
|
||||
g_t7_keyact = g_t7_keyact > spent ? g_t7_keyact - spent : 0;
|
||||
#endif
|
||||
return spent;
|
||||
}
|
||||
#else
|
||||
|
||||
|
||||
// "Video" IRQ/NMI fire rate.
|
||||
//
|
||||
// On real V7 hardware these are driven by the LCD controller, which
|
||||
// runs at roughly 60 Hz. We step the emulator 20× per second, so we need
|
||||
// to fire 3 sub-frames worth of NMI/IRQ per call to match the rate the
|
||||
// ROM expects.
|
||||
//
|
||||
// Symptoms when this is wrong: the Tama's clock runs slow because the
|
||||
// game counts video frames to measure real time (e.g. "60 frames = 1
|
||||
// second"). At 20 Hz it would clock at 1/3 speed.
|
||||
#define VIDEO_FIRES_PER_FRAME 3
|
||||
|
||||
uint32_t T7_HOT t7_step_cycles(uint32_t target_cycles)
|
||||
{
|
||||
if (!g_initialised) return 0;
|
||||
|
||||
// Split the requested cycles into VIDEO_FIRES_PER_FRAME chunks so the
|
||||
// ROM sees the video interrupt at roughly the hardware rate.
|
||||
uint32_t per_chunk = target_cycles / VIDEO_FIRES_PER_FRAME;
|
||||
uint32_t spent = 0;
|
||||
|
||||
for (int chunk = 0; chunk < VIDEO_FIRES_PER_FRAME; chunk++) {
|
||||
t7_fire_irq(10);
|
||||
t7_fire_nmi(6);
|
||||
t7_friends_tick();
|
||||
|
||||
uint32_t chunk_target = (chunk == VIDEO_FIRES_PER_FRAME - 1)
|
||||
? target_cycles // last chunk takes the rest
|
||||
: (spent + per_chunk);
|
||||
if (g_snd_on) {
|
||||
while (spent < chunk_target) {
|
||||
uint32_t c = t7_cpu_step();
|
||||
spent += c;
|
||||
if ((g_snd_next -= (int32_t)c) <= 0) t7_snd_service();
|
||||
}
|
||||
} else {
|
||||
while (spent < chunk_target) {
|
||||
spent += t7_cpu_step();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (g_snd_on) t7_snd_flush();
|
||||
|
||||
// 2 Hz timer (IRQ 13) — the "TBH" counter that drives the game's
|
||||
// real-time clock. Independent of the video IRQ rate.
|
||||
s_tbh_accumulator += spent;
|
||||
while (s_tbh_accumulator >= TBH_RATE) {
|
||||
t7_fire_irq(13);
|
||||
s_tbh_accumulator -= TBH_RATE;
|
||||
}
|
||||
return spent;
|
||||
}
|
||||
#endif
|
||||
|
||||
// ----- Display ------------------------------------------------------------
|
||||
|
||||
static const uint16_t LCD_ROW_OFFSETS[31] = {
|
||||
0x0C0, 0x0CC, 0x0D8, 0x0E4,
|
||||
0x0F0, 0x0FC, 0x108, 0x114,
|
||||
0x120, 0x12C, 0x138, 0x144,
|
||||
0x150, 0x15C, 0x168, 0x174,
|
||||
0x0B4, 0x0A8, 0x09C, 0x090,
|
||||
0x084, 0x078, 0x06C, 0x060,
|
||||
0x054, 0x048, 0x03C, 0x030,
|
||||
0x024, 0x018, 0x00C,
|
||||
};
|
||||
|
||||
void t7_get_display(uint8_t *out)
|
||||
{
|
||||
if (!g_initialised || !out) return;
|
||||
for (int y = 0; y < T7_LCD_HEIGHT; y++) {
|
||||
uint16_t base = LCD_ROW_OFFSETS[y] % T7_DRAM_SIZE;
|
||||
for (int x = 0; x < T7_LCD_WIDTH; x += 4) {
|
||||
uint8_t d = g_dram[base++];
|
||||
out[y * T7_LCD_WIDTH + x + 0] = (d >> 6) & 0x3;
|
||||
out[y * T7_LCD_WIDTH + x + 1] = (d >> 4) & 0x3;
|
||||
out[y * T7_LCD_WIDTH + x + 2] = (d >> 2) & 0x3;
|
||||
out[y * T7_LCD_WIDTH + x + 3] = (d >> 0) & 0x3;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Icon extraction matches the refresh_simple loop in t7emu/main.js:
|
||||
// var a=4, b=0, g=0;
|
||||
// while (g<10) { glyph = (dram[a] >> b) & 3; if ((b-=2)<0){b=6;a++;} ... }
|
||||
//
|
||||
// Walking the loop manually gives the table below. Two bits per icon,
|
||||
// four icons per byte, 10 icons total across 4 bytes (DRAM[4..7]) — but
|
||||
// only the first 2 bits of DRAM[4] and first 2 bits of DRAM[7] are used.
|
||||
void t7_get_icons(uint8_t *out)
|
||||
{
|
||||
if (!g_initialised || !out) return;
|
||||
out[0] = (g_dram[4] >> 0) & 0x3; // dashboard
|
||||
out[1] = (g_dram[5] >> 6) & 0x3; // food
|
||||
out[2] = (g_dram[5] >> 4) & 0x3; // trash
|
||||
out[3] = (g_dram[5] >> 2) & 0x3; // globe
|
||||
out[4] = (g_dram[5] >> 0) & 0x3; // user
|
||||
out[5] = (g_dram[6] >> 6) & 0x3; // comments
|
||||
out[6] = (g_dram[6] >> 4) & 0x3; // medkit
|
||||
out[7] = (g_dram[6] >> 2) & 0x3; // heart
|
||||
out[8] = (g_dram[6] >> 0) & 0x3; // book
|
||||
out[9] = (g_dram[7] >> 6) & 0x3; // bell
|
||||
}
|
||||
|
||||
// ----- Buttons ------------------------------------------------------------
|
||||
|
||||
void t7_set_buttons(uint8_t mask)
|
||||
{
|
||||
if (!g_initialised) return;
|
||||
uint8_t lo = ~mask & 0x0F;
|
||||
// Obere Bits MUESSEN 0 bleiben (wie in der Referenz: this.keys = 0xF).
|
||||
// Frueher stand hier "| 0xF0", was die Figur-Bits (Bit 5-7 in PortA) komplett
|
||||
// maskiert hat -> die Firmware konnte eine gesteckte Figur nie erkennen.
|
||||
#ifdef T7_STOP
|
||||
#ifdef T7_STOP_KEYACT
|
||||
if (lo != g_keys) g_t7_keyact = T7_STOP_KEYACT;
|
||||
#endif
|
||||
if (g_t7_halted && (lo & 0x07) != 0x07 && (g_cpureg[0x07] & 0x01)) { g_cpureg[0x08] |= 0x01; g_t7_halted = 0; }
|
||||
#endif
|
||||
g_keys = lo;
|
||||
}
|
||||
|
||||
// ----- WRAM direct access (used by RTC sync) ------------------------------
|
||||
|
||||
uint8_t t7_ram_read(uint16_t addr)
|
||||
{
|
||||
if (!g_initialised) return 0;
|
||||
if (addr >= T7_WRAM_SIZE) return 0;
|
||||
return g_wram[addr];
|
||||
}
|
||||
|
||||
void t7_ram_write(uint16_t addr, uint8_t val)
|
||||
{
|
||||
if (!g_initialised) return;
|
||||
if (addr >= T7_WRAM_SIZE) return;
|
||||
g_wram[addr] = val;
|
||||
}
|
||||
|
||||
// ----- Debug --------------------------------------------------------------
|
||||
|
||||
void t7_debug_get_state(uint16_t *pc, uint8_t *a, uint8_t *x, uint8_t *y,
|
||||
uint8_t *s, uint8_t *bank)
|
||||
{
|
||||
if (!g_initialised) return;
|
||||
if (pc) *pc = g_cpu.pc;
|
||||
if (a) *a = g_cpu.a;
|
||||
if (x) *x = g_cpu.x;
|
||||
if (y) *y = g_cpu.y;
|
||||
if (s) *s = g_cpu.s;
|
||||
if (bank) *bank = g_rom_bank_id;
|
||||
}
|
||||
|
||||
bool t7_debug_dram_dirty(void)
|
||||
{
|
||||
if (!g_initialised) return false;
|
||||
for (int i = 0; i < T7_DRAM_SIZE; i++) if (g_dram[i]) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
uint16_t t7_debug_dram_nonzero_count(void)
|
||||
{
|
||||
if (!g_initialised) return 0;
|
||||
uint16_t n = 0;
|
||||
for (int i = 0; i < T7_DRAM_SIZE; i++) if (g_dram[i]) n++;
|
||||
return n;
|
||||
}
|
||||
|
||||
// ----- Save state ---------------------------------------------------------
|
||||
|
||||
#define T7_SAVE_MAGIC 0x54414D47
|
||||
#define T7_SAVE_VERSION 1
|
||||
#define T7_SAVE_SIZE \
|
||||
(4 + 4 + sizeof(cpu6502_t) + \
|
||||
T7_CPUREG_SIZE + T7_WRAM_SIZE + T7_DRAM_SIZE + 1 + 1)
|
||||
|
||||
uint32_t t7_serialize_state(uint8_t *buf, uint32_t bufsize)
|
||||
{
|
||||
if (!buf || bufsize < T7_SAVE_SIZE) return T7_SAVE_SIZE;
|
||||
if (!g_initialised) return 0;
|
||||
uint8_t *p = buf;
|
||||
uint32_t magic = T7_SAVE_MAGIC;
|
||||
memcpy(p, &magic, 4); p += 4;
|
||||
*p++ = T7_SAVE_VERSION;
|
||||
*p++ = 0; *p++ = 0; *p++ = 0;
|
||||
memcpy(p, &g_cpu, sizeof(cpu6502_t)); p += sizeof(cpu6502_t);
|
||||
memcpy(p, g_cpureg, T7_CPUREG_SIZE); p += T7_CPUREG_SIZE;
|
||||
memcpy(p, g_wram, T7_WRAM_SIZE); p += T7_WRAM_SIZE;
|
||||
memcpy(p, g_dram, T7_DRAM_SIZE); p += T7_DRAM_SIZE;
|
||||
*p++ = g_rom_bank_id;
|
||||
*p++ = g_keys;
|
||||
return (uint32_t)(p - buf);
|
||||
}
|
||||
|
||||
bool t7_deserialize_state(const uint8_t *buf, uint32_t bufsize)
|
||||
{
|
||||
if (!buf || bufsize < T7_SAVE_SIZE || !g_initialised) return false;
|
||||
const uint8_t *p = buf;
|
||||
uint32_t magic;
|
||||
memcpy(&magic, p, 4); p += 4;
|
||||
if (magic != T7_SAVE_MAGIC) return false;
|
||||
uint8_t version = *p++;
|
||||
if (version != T7_SAVE_VERSION) return false;
|
||||
p += 3;
|
||||
memcpy(&g_cpu, p, sizeof(cpu6502_t)); p += sizeof(cpu6502_t);
|
||||
memcpy(g_cpureg, p, T7_CPUREG_SIZE); p += T7_CPUREG_SIZE;
|
||||
memcpy(g_wram, p, T7_WRAM_SIZE); p += T7_WRAM_SIZE;
|
||||
memcpy(g_dram, p, T7_DRAM_SIZE); p += T7_DRAM_SIZE;
|
||||
uint8_t saved_bank = *p++;
|
||||
g_keys = *p++;
|
||||
g_irq_pending_any = (g_cpureg[0x73] | g_cpureg[0x74]) != 0;
|
||||
t7_set_rom_bank(saved_bank);
|
||||
t7_snd_resync();
|
||||
return true;
|
||||
}
|
||||
|
||||
void t7_profile_snapshot_and_reset(t7_profile_snapshot_t *out)
|
||||
{
|
||||
#if T7_PROFILE
|
||||
if (out) {
|
||||
out->opcodes = g_t7_profile.opcodes;
|
||||
out->reads_fast = g_t7_profile.reads_fast;
|
||||
out->reads_io = g_t7_profile.reads_io;
|
||||
out->writes_fast = g_t7_profile.writes_fast;
|
||||
out->writes_io = g_t7_profile.writes_io;
|
||||
out->writes_dropped = g_t7_profile.writes_dropped;
|
||||
out->irqs = g_t7_profile.irqs;
|
||||
out->nmis = g_t7_profile.nmis;
|
||||
out->irq_entries = g_t7_profile.irq_entries;
|
||||
out->nmi_entries = g_t7_profile.nmi_entries;
|
||||
}
|
||||
memset(&g_t7_profile, 0, sizeof(g_t7_profile));
|
||||
#else
|
||||
if (out) memset(out, 0, sizeof(*out));
|
||||
#endif
|
||||
}
|
||||
/*
|
||||
* t7.c - top-level emulator integration
|
||||
*
|
||||
* Provides the public API declared in t7.h.
|
||||
*/
|
||||
|
||||
#include "t7_internal.h"
|
||||
#include "t7_eeprom.h"
|
||||
#include "t7_sound.h"
|
||||
#ifdef T7_FASTRUN
|
||||
uint32_t t7_cpu_run(uint32_t budget);
|
||||
#endif
|
||||
|
||||
// ----- Global state definitions ------------------------------------------
|
||||
//
|
||||
// Declared extern in t7_internal.h. Living as plain globals (rather
|
||||
// than fields of a g_sys struct) lets the compiler resolve addresses at
|
||||
// compile time and skip a pointer-indirection on every hot-path memory
|
||||
// access. ~52 KB total, all in .bss.
|
||||
|
||||
cpu6502_t g_cpu;
|
||||
uint8_t g_wram[T7_WRAM_SIZE];
|
||||
uint8_t g_dram[T7_DRAM_SIZE];
|
||||
uint8_t g_cpureg[T7_CPUREG_SIZE];
|
||||
|
||||
#if T7_PROFILE
|
||||
// Profiling counters — see t7_internal.h. Plain global to let the
|
||||
// linker resolve direct addresses (one less indirection vs a struct
|
||||
// member access through a pointer).
|
||||
t7_profile_t g_t7_profile;
|
||||
#endif
|
||||
uint8_t *g_rom_bank_buf = NULL; // malloc'd in t7_init
|
||||
uint8_t *g_static_rom = NULL; // malloc'd in t7_init
|
||||
uint16_t g_irq_vectors[16];
|
||||
uint8_t g_irq_pending_any;
|
||||
uint8_t g_rom_bank_id;
|
||||
bool g_rom_loaded;
|
||||
uint8_t g_keys;
|
||||
ResHandle g_rom_resource;
|
||||
bool g_initialised;
|
||||
|
||||
// ----- Init / Release -----------------------------------------------------
|
||||
|
||||
bool t7_init(void)
|
||||
{
|
||||
if (g_initialised) {
|
||||
APP_LOG(APP_LOG_LEVEL_WARNING, "t7_init: already initialised");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Allocate the big ROM caches on the heap. They have to live somewhere
|
||||
// and putting them in .bss pushes us over Pebble's 16-bit virtual-size
|
||||
// limit (65535 bytes). The heap on Emery has plenty of room.
|
||||
#ifdef T7_NOMALLOC
|
||||
/* Paradise: no heap. The banks are read straight from the mapped flash, only the
|
||||
* static ROM is copied (16 KB in RAM). */
|
||||
{ static uint8_t s_static[T7_STATIC_ROM_SIZE]; static uint8_t s_bank1[256];
|
||||
g_static_rom = s_static; g_rom_bank_buf = s_bank1; }
|
||||
#else
|
||||
g_rom_bank_buf = (uint8_t *)malloc(T7_ROM_BANK_SIZE);
|
||||
g_static_rom = (uint8_t *)malloc(T7_STATIC_ROM_SIZE);
|
||||
#endif
|
||||
#ifndef T7_NOMALLOC
|
||||
if (!g_rom_bank_buf || !g_static_rom) {
|
||||
APP_LOG(APP_LOG_LEVEL_ERROR,
|
||||
"t7_init: out of memory (bank=%p static=%p)",
|
||||
g_rom_bank_buf, g_static_rom);
|
||||
if (g_rom_bank_buf) { free(g_rom_bank_buf); g_rom_bank_buf = NULL; }
|
||||
if (g_static_rom) { free(g_static_rom); g_static_rom = NULL; }
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
memset(&g_cpu, 0, sizeof(g_cpu));
|
||||
memset(g_wram, 0, sizeof(g_wram));
|
||||
memset(g_dram, 0, sizeof(g_dram));
|
||||
memset(g_cpureg, 0, sizeof(g_cpureg));
|
||||
#ifndef T7_NOMALLOC
|
||||
memset(g_rom_bank_buf, 0, T7_ROM_BANK_SIZE);
|
||||
#endif
|
||||
memset(g_static_rom, 0, T7_STATIC_ROM_SIZE);
|
||||
g_keys = 0xFF;
|
||||
g_rom_bank_id = 0xFF;
|
||||
g_rom_loaded = false;
|
||||
|
||||
if (!t7_load_static_rom()) return false;
|
||||
t7_idle_check();
|
||||
t7_set_rom_bank(0);
|
||||
t7_eeprom_init(); // load save data from persist
|
||||
t7_cpu_reset();
|
||||
|
||||
g_initialised = true;
|
||||
APP_LOG(APP_LOG_LEVEL_INFO,
|
||||
"t7_init: ready, PC=$%04x", g_cpu.pc);
|
||||
return true;
|
||||
}
|
||||
|
||||
void t7_release(void)
|
||||
{
|
||||
// Flush any pending EEPROM writes back to persist before we tear down.
|
||||
// Without this, save data written between the last STOP-after-WRITE
|
||||
// and app exit would be lost.
|
||||
t7_eeprom_flush();
|
||||
#ifndef T7_NOMALLOC
|
||||
if (g_rom_bank_buf) { free(g_rom_bank_buf); g_rom_bank_buf = NULL; }
|
||||
if (g_static_rom) { free(g_static_rom); g_static_rom = NULL; }
|
||||
#endif
|
||||
g_initialised = false;
|
||||
}
|
||||
|
||||
void t7_reset(void)
|
||||
{
|
||||
if (!g_initialised) return;
|
||||
t7_cpu_reset();
|
||||
}
|
||||
|
||||
// ----- Stepping -----------------------------------------------------------
|
||||
|
||||
uint8_t T7_HOT t7_step_one(void)
|
||||
{
|
||||
if (!g_initialised) return 0;
|
||||
#ifdef T7_FASTRUN
|
||||
return (uint8_t)t7_cpu_run(1);
|
||||
#else
|
||||
return t7_cpu_step();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/* Tamagotchi Friends: its frame tick comes from IRQ 3, every 122nd one (counter $55). Instead of
|
||||
* 7.3 kHz of interrupts: at the video rate set the counter just below and fire IRQ 3 once. */
|
||||
static inline void t7_friends_tick(void)
|
||||
{
|
||||
if (!g_t7_static_off) return;
|
||||
if (g_wram[0x55] < 0x79) g_wram[0x55] = 0x79;
|
||||
t7_fire_irq(3);
|
||||
}
|
||||
static uint32_t s_tbh_accumulator = 0;
|
||||
#ifdef T7_TBH_RATE
|
||||
/* 1.6.12 test2: the 2 Hz timer of the game clock in real time when the program runs the core slower than 4 MHz
|
||||
* (Paradise: 3.03 MHz for the tempo): at 4 MHz cycles a game second lasted 1.32 s and the clock note jumped it ahead */
|
||||
#define TBH_RATE (T7_TBH_RATE)
|
||||
#else
|
||||
#define TBH_RATE (T7_CLOCK_RATE / 2)
|
||||
#endif
|
||||
#ifdef T7_VIDEO_CYCLES
|
||||
/* Paradise (as the ESP32 port): video IRQ at 60 Hz of emulated time, independent of how
|
||||
* often and with how many cycles this is called. */
|
||||
#define VIDEO_HZ 60
|
||||
#define VIDEO_PERIOD_CYCLES (T7_CLOCK_RATE / VIDEO_HZ)
|
||||
static uint32_t s_video_accumulator = 0;
|
||||
uint32_t T7_HOT t7_step_cycles(uint32_t target_cycles)
|
||||
{
|
||||
if (!g_initialised) return 0;
|
||||
uint32_t spent = 0;
|
||||
while (spent < target_cycles) {
|
||||
uint32_t step = target_cycles - spent;
|
||||
uint32_t to_video = VIDEO_PERIOD_CYCLES - s_video_accumulator;
|
||||
uint32_t to_tbh = TBH_RATE - s_tbh_accumulator;
|
||||
if (to_video < step) step = to_video;
|
||||
if (to_tbh < step) step = to_tbh;
|
||||
if (step == 0) step = 1;
|
||||
uint32_t did = 0;
|
||||
#ifdef T7_FASTRUN
|
||||
did = t7_cpu_run(step);
|
||||
#else
|
||||
if (g_snd_on) {
|
||||
while (did < step) {
|
||||
uint32_t c = t7_cpu_step();
|
||||
did += c;
|
||||
if ((g_snd_next -= (int32_t)c) <= 0) t7_snd_service();
|
||||
}
|
||||
} else {
|
||||
while (did < step) did += t7_cpu_step();
|
||||
}
|
||||
#endif
|
||||
spent += did;
|
||||
s_video_accumulator += did;
|
||||
if (s_video_accumulator >= VIDEO_PERIOD_CYCLES) {
|
||||
s_video_accumulator -= VIDEO_PERIOD_CYCLES;
|
||||
t7_fire_irq(10);
|
||||
t7_fire_nmi(6);
|
||||
t7_friends_tick();
|
||||
}
|
||||
s_tbh_accumulator += did;
|
||||
if (s_tbh_accumulator >= TBH_RATE) {
|
||||
s_tbh_accumulator -= TBH_RATE;
|
||||
t7_fire_irq(13);
|
||||
}
|
||||
}
|
||||
if (g_snd_on) t7_snd_flush();
|
||||
#ifdef T7_STOP_KEYACT
|
||||
g_t7_keyact = g_t7_keyact > spent ? g_t7_keyact - spent : 0;
|
||||
#endif
|
||||
return spent;
|
||||
}
|
||||
#else
|
||||
|
||||
|
||||
// "Video" IRQ/NMI fire rate.
|
||||
//
|
||||
// On real V7 hardware these are driven by the LCD controller, which
|
||||
// runs at roughly 60 Hz. We step the emulator 20× per second, so we need
|
||||
// to fire 3 sub-frames worth of NMI/IRQ per call to match the rate the
|
||||
// ROM expects.
|
||||
//
|
||||
// Symptoms when this is wrong: the Tama's clock runs slow because the
|
||||
// game counts video frames to measure real time (e.g. "60 frames = 1
|
||||
// second"). At 20 Hz it would clock at 1/3 speed.
|
||||
#define VIDEO_FIRES_PER_FRAME 3
|
||||
|
||||
uint32_t T7_HOT t7_step_cycles(uint32_t target_cycles)
|
||||
{
|
||||
if (!g_initialised) return 0;
|
||||
|
||||
// Split the requested cycles into VIDEO_FIRES_PER_FRAME chunks so the
|
||||
// ROM sees the video interrupt at roughly the hardware rate.
|
||||
uint32_t per_chunk = target_cycles / VIDEO_FIRES_PER_FRAME;
|
||||
uint32_t spent = 0;
|
||||
|
||||
for (int chunk = 0; chunk < VIDEO_FIRES_PER_FRAME; chunk++) {
|
||||
t7_fire_irq(10);
|
||||
t7_fire_nmi(6);
|
||||
t7_friends_tick();
|
||||
|
||||
uint32_t chunk_target = (chunk == VIDEO_FIRES_PER_FRAME - 1)
|
||||
? target_cycles // last chunk takes the rest
|
||||
: (spent + per_chunk);
|
||||
if (g_snd_on) {
|
||||
while (spent < chunk_target) {
|
||||
uint32_t c = t7_cpu_step();
|
||||
spent += c;
|
||||
if ((g_snd_next -= (int32_t)c) <= 0) t7_snd_service();
|
||||
}
|
||||
} else {
|
||||
while (spent < chunk_target) {
|
||||
spent += t7_cpu_step();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (g_snd_on) t7_snd_flush();
|
||||
|
||||
// 2 Hz timer (IRQ 13) — the "TBH" counter that drives the game's
|
||||
// real-time clock. Independent of the video IRQ rate.
|
||||
s_tbh_accumulator += spent;
|
||||
while (s_tbh_accumulator >= TBH_RATE) {
|
||||
t7_fire_irq(13);
|
||||
s_tbh_accumulator -= TBH_RATE;
|
||||
}
|
||||
return spent;
|
||||
}
|
||||
#endif
|
||||
|
||||
// ----- Display ------------------------------------------------------------
|
||||
|
||||
static const uint16_t LCD_ROW_OFFSETS[31] = {
|
||||
0x0C0, 0x0CC, 0x0D8, 0x0E4,
|
||||
0x0F0, 0x0FC, 0x108, 0x114,
|
||||
0x120, 0x12C, 0x138, 0x144,
|
||||
0x150, 0x15C, 0x168, 0x174,
|
||||
0x0B4, 0x0A8, 0x09C, 0x090,
|
||||
0x084, 0x078, 0x06C, 0x060,
|
||||
0x054, 0x048, 0x03C, 0x030,
|
||||
0x024, 0x018, 0x00C,
|
||||
};
|
||||
|
||||
void t7_get_display(uint8_t *out)
|
||||
{
|
||||
if (!g_initialised || !out) return;
|
||||
for (int y = 0; y < T7_LCD_HEIGHT; y++) {
|
||||
uint16_t base = LCD_ROW_OFFSETS[y] % T7_DRAM_SIZE;
|
||||
for (int x = 0; x < T7_LCD_WIDTH; x += 4) {
|
||||
uint8_t d = g_dram[base++];
|
||||
out[y * T7_LCD_WIDTH + x + 0] = (d >> 6) & 0x3;
|
||||
out[y * T7_LCD_WIDTH + x + 1] = (d >> 4) & 0x3;
|
||||
out[y * T7_LCD_WIDTH + x + 2] = (d >> 2) & 0x3;
|
||||
out[y * T7_LCD_WIDTH + x + 3] = (d >> 0) & 0x3;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Icon extraction matches the refresh_simple loop in t7emu/main.js:
|
||||
// var a=4, b=0, g=0;
|
||||
// while (g<10) { glyph = (dram[a] >> b) & 3; if ((b-=2)<0){b=6;a++;} ... }
|
||||
//
|
||||
// Walking the loop manually gives the table below. Two bits per icon,
|
||||
// four icons per byte, 10 icons total across 4 bytes (DRAM[4..7]) — but
|
||||
// only the first 2 bits of DRAM[4] and first 2 bits of DRAM[7] are used.
|
||||
void t7_get_icons(uint8_t *out)
|
||||
{
|
||||
if (!g_initialised || !out) return;
|
||||
out[0] = (g_dram[4] >> 0) & 0x3; // dashboard
|
||||
out[1] = (g_dram[5] >> 6) & 0x3; // food
|
||||
out[2] = (g_dram[5] >> 4) & 0x3; // trash
|
||||
out[3] = (g_dram[5] >> 2) & 0x3; // globe
|
||||
out[4] = (g_dram[5] >> 0) & 0x3; // user
|
||||
out[5] = (g_dram[6] >> 6) & 0x3; // comments
|
||||
out[6] = (g_dram[6] >> 4) & 0x3; // medkit
|
||||
out[7] = (g_dram[6] >> 2) & 0x3; // heart
|
||||
out[8] = (g_dram[6] >> 0) & 0x3; // book
|
||||
out[9] = (g_dram[7] >> 6) & 0x3; // bell
|
||||
}
|
||||
|
||||
// ----- Buttons ------------------------------------------------------------
|
||||
|
||||
void t7_set_buttons(uint8_t mask)
|
||||
{
|
||||
if (!g_initialised) return;
|
||||
uint8_t lo = ~mask & 0x0F;
|
||||
// Obere Bits MUESSEN 0 bleiben (wie in der Referenz: this.keys = 0xF).
|
||||
// Frueher stand hier "| 0xF0", was die Figur-Bits (Bit 5-7 in PortA) komplett
|
||||
// maskiert hat -> die Firmware konnte eine gesteckte Figur nie erkennen.
|
||||
#ifdef T7_STOP
|
||||
#ifdef T7_STOP_KEYACT
|
||||
if (lo != g_keys) g_t7_keyact = T7_STOP_KEYACT;
|
||||
#endif
|
||||
if (g_t7_halted && (lo & 0x07) != 0x07 && (g_cpureg[0x07] & 0x01)) { g_cpureg[0x08] |= 0x01; g_t7_halted = 0; }
|
||||
#endif
|
||||
g_keys = lo;
|
||||
}
|
||||
|
||||
// ----- WRAM direct access (used by RTC sync) ------------------------------
|
||||
|
||||
uint8_t t7_ram_read(uint16_t addr)
|
||||
{
|
||||
if (!g_initialised) return 0;
|
||||
if (addr >= T7_WRAM_SIZE) return 0;
|
||||
return g_wram[addr];
|
||||
}
|
||||
|
||||
void t7_ram_write(uint16_t addr, uint8_t val)
|
||||
{
|
||||
if (!g_initialised) return;
|
||||
if (addr >= T7_WRAM_SIZE) return;
|
||||
g_wram[addr] = val;
|
||||
}
|
||||
|
||||
// ----- Debug --------------------------------------------------------------
|
||||
|
||||
void t7_debug_get_state(uint16_t *pc, uint8_t *a, uint8_t *x, uint8_t *y,
|
||||
uint8_t *s, uint8_t *bank)
|
||||
{
|
||||
if (!g_initialised) return;
|
||||
if (pc) *pc = g_cpu.pc;
|
||||
if (a) *a = g_cpu.a;
|
||||
if (x) *x = g_cpu.x;
|
||||
if (y) *y = g_cpu.y;
|
||||
if (s) *s = g_cpu.s;
|
||||
if (bank) *bank = g_rom_bank_id;
|
||||
}
|
||||
|
||||
bool t7_debug_dram_dirty(void)
|
||||
{
|
||||
if (!g_initialised) return false;
|
||||
for (int i = 0; i < T7_DRAM_SIZE; i++) if (g_dram[i]) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
uint16_t t7_debug_dram_nonzero_count(void)
|
||||
{
|
||||
if (!g_initialised) return 0;
|
||||
uint16_t n = 0;
|
||||
for (int i = 0; i < T7_DRAM_SIZE; i++) if (g_dram[i]) n++;
|
||||
return n;
|
||||
}
|
||||
|
||||
// ----- Save state ---------------------------------------------------------
|
||||
|
||||
#define T7_SAVE_MAGIC 0x54414D47
|
||||
#define T7_SAVE_VERSION 1
|
||||
#define T7_SAVE_SIZE \
|
||||
(4 + 4 + sizeof(cpu6502_t) + \
|
||||
T7_CPUREG_SIZE + T7_WRAM_SIZE + T7_DRAM_SIZE + 1 + 1)
|
||||
|
||||
uint32_t t7_serialize_state(uint8_t *buf, uint32_t bufsize)
|
||||
{
|
||||
if (!buf || bufsize < T7_SAVE_SIZE) return T7_SAVE_SIZE;
|
||||
if (!g_initialised) return 0;
|
||||
uint8_t *p = buf;
|
||||
uint32_t magic = T7_SAVE_MAGIC;
|
||||
memcpy(p, &magic, 4); p += 4;
|
||||
*p++ = T7_SAVE_VERSION;
|
||||
*p++ = 0; *p++ = 0; *p++ = 0;
|
||||
memcpy(p, &g_cpu, sizeof(cpu6502_t)); p += sizeof(cpu6502_t);
|
||||
memcpy(p, g_cpureg, T7_CPUREG_SIZE); p += T7_CPUREG_SIZE;
|
||||
memcpy(p, g_wram, T7_WRAM_SIZE); p += T7_WRAM_SIZE;
|
||||
memcpy(p, g_dram, T7_DRAM_SIZE); p += T7_DRAM_SIZE;
|
||||
*p++ = g_rom_bank_id;
|
||||
*p++ = g_keys;
|
||||
return (uint32_t)(p - buf);
|
||||
}
|
||||
|
||||
bool t7_deserialize_state(const uint8_t *buf, uint32_t bufsize)
|
||||
{
|
||||
if (!buf || bufsize < T7_SAVE_SIZE || !g_initialised) return false;
|
||||
const uint8_t *p = buf;
|
||||
uint32_t magic;
|
||||
memcpy(&magic, p, 4); p += 4;
|
||||
if (magic != T7_SAVE_MAGIC) return false;
|
||||
uint8_t version = *p++;
|
||||
if (version != T7_SAVE_VERSION) return false;
|
||||
p += 3;
|
||||
memcpy(&g_cpu, p, sizeof(cpu6502_t)); p += sizeof(cpu6502_t);
|
||||
memcpy(g_cpureg, p, T7_CPUREG_SIZE); p += T7_CPUREG_SIZE;
|
||||
memcpy(g_wram, p, T7_WRAM_SIZE); p += T7_WRAM_SIZE;
|
||||
memcpy(g_dram, p, T7_DRAM_SIZE); p += T7_DRAM_SIZE;
|
||||
uint8_t saved_bank = *p++;
|
||||
g_keys = *p++;
|
||||
g_irq_pending_any = (g_cpureg[0x73] | g_cpureg[0x74]) != 0;
|
||||
t7_set_rom_bank(saved_bank);
|
||||
t7_snd_resync();
|
||||
return true;
|
||||
}
|
||||
|
||||
void t7_profile_snapshot_and_reset(t7_profile_snapshot_t *out)
|
||||
{
|
||||
#if T7_PROFILE
|
||||
if (out) {
|
||||
out->opcodes = g_t7_profile.opcodes;
|
||||
out->reads_fast = g_t7_profile.reads_fast;
|
||||
out->reads_io = g_t7_profile.reads_io;
|
||||
out->writes_fast = g_t7_profile.writes_fast;
|
||||
out->writes_io = g_t7_profile.writes_io;
|
||||
out->writes_dropped = g_t7_profile.writes_dropped;
|
||||
out->irqs = g_t7_profile.irqs;
|
||||
out->nmis = g_t7_profile.nmis;
|
||||
out->irq_entries = g_t7_profile.irq_entries;
|
||||
out->nmi_entries = g_t7_profile.nmi_entries;
|
||||
}
|
||||
memset(&g_t7_profile, 0, sizeof(g_t7_profile));
|
||||
#else
|
||||
if (out) memset(out, 0, sizeof(*out));
|
||||
#endif
|
||||
}
|
||||
+178
-178
@@ -1,178 +1,178 @@
|
||||
/*
|
||||
* t7.h - V7 emulator core (6502), urspruenglich fuer Pebble
|
||||
*
|
||||
* Based on asterick/tamago (JavaScript implementation):
|
||||
* https://github.com/asterick/tamago
|
||||
*
|
||||
* Hardware: GPLB52320A (8-bit 6502-family) @ 4 MHz
|
||||
* Display: 64x32 pixels, 4 grayscale levels (512 bytes DRAM)
|
||||
*
|
||||
* Memory map (16-bit address space, banked):
|
||||
* $0000-$0FFF Work RAM (1.5 KB, mirrored)
|
||||
* $1000-$2FFF Display RAM (512 bytes, mirrored)
|
||||
* $3000-$3FFF CPU control registers
|
||||
* $4000-$BFFF Banked ROM (32 KB window, 20 banks total = 640 KB)
|
||||
* $C000-$FFFF Static ROM (16 KB, always mapped from bank 0)
|
||||
*
|
||||
* The 640 KB ROM lives in Pebble flash as a resource. To save RAM we
|
||||
* only keep the currently-selected bank (32 KB) mirrored in memory.
|
||||
* Static ROM (bank 0, first 16 KB) is loaded once at boot.
|
||||
*/
|
||||
|
||||
#ifndef T7_H
|
||||
#define T7_H
|
||||
|
||||
#include "pebble_compat.h"
|
||||
|
||||
// Compile-time profile switch. Set to 1 to enable per-opcode / per-read /
|
||||
// per-write counters (logged once a minute from main.c). Adds ~10-15%
|
||||
// CPU overhead — keep OFF in production.
|
||||
#define T7_PROFILE 0
|
||||
|
||||
// Display dimensions (native V7 hardware).
|
||||
//
|
||||
// The display is 48 pixels wide × 31 pixels high — this comes from the
|
||||
// HTML template in the JS reference (canvas width=48 height=31). Although
|
||||
// the hardware has 64 segments × 32 commons, the addressing scheme in
|
||||
// _dram only gives meaningful pixels in the first 48 of each row; the
|
||||
// remaining 16 are aliased into the start of the next row's bytes.
|
||||
// The JS emulator handles this by drawing into a 48-px-wide canvas — the
|
||||
// 16 "extra" pixels per row spill into the next row and get overwritten.
|
||||
// In our C port we just clamp to 48 explicitly.
|
||||
#define T7_LCD_WIDTH 48
|
||||
#define T7_LCD_HEIGHT 31
|
||||
|
||||
// 4 grayscale levels per pixel (2 bits each)
|
||||
#define T7_PALETTE_SIZE 4
|
||||
|
||||
// CPU clock rate (Hz)
|
||||
#define T7_CLOCK_RATE 4000000
|
||||
|
||||
// Memory region sizes
|
||||
#define T7_WRAM_SIZE 0x600 // 1.5 KB work RAM
|
||||
#define T7_DRAM_SIZE 0x200 // 512 B display RAM
|
||||
#define T7_CPUREG_SIZE 0x100 // 256 B control registers
|
||||
#define T7_ROM_BANK_SIZE 0x8000 // 32 KB per bank
|
||||
#define T7_STATIC_ROM_SIZE 0x4000 // 16 KB static (high) ROM
|
||||
|
||||
// Status flag bits in P register
|
||||
#define FLAG_C 0x01 // Carry
|
||||
#define FLAG_Z 0x02 // Zero
|
||||
#define FLAG_I 0x04 // Interrupt disable
|
||||
#define FLAG_D 0x08 // Decimal mode
|
||||
#define FLAG_B 0x10 // Break (software)
|
||||
#define FLAG_U 0x20 // Unused (always 1)
|
||||
#define FLAG_V 0x40 // Overflow
|
||||
#define FLAG_N 0x80 // Negative
|
||||
|
||||
// Button bitmask (V7 has 3 buttons: A, B, C plus reset)
|
||||
#define T7_BTN_A 0x01
|
||||
#define T7_BTN_B 0x02
|
||||
#define T7_BTN_C 0x04
|
||||
#define T7_BTN_RESET 0x08
|
||||
|
||||
// Forward declaration for the system state
|
||||
typedef struct t7_system t7_system_t;
|
||||
|
||||
// Public API ---------------------------------------------------------------
|
||||
|
||||
// Initialize the emulator. Must be called once at startup. Returns false on
|
||||
// failure (e.g. ROM resource missing).
|
||||
bool t7_init(void);
|
||||
/* After t7_init: 1 = Tamagotchi Friends ROM (768 KB), 0 = Tama-Go. */
|
||||
int t7_is_friends(void);
|
||||
uint16_t t7_rtc_sec_addr(void);
|
||||
|
||||
// Reset the CPU (jumps to the reset vector). Memory and EEPROM are preserved.
|
||||
void t7_reset(void);
|
||||
|
||||
// Run the emulator for the given number of CPU cycles. Returns the actual
|
||||
// number of cycles consumed (may differ slightly due to instruction lengths).
|
||||
uint32_t t7_step_cycles(uint32_t target_cycles);
|
||||
|
||||
// Run a single instruction. Returns the cycle count of that instruction.
|
||||
uint8_t t7_step_one(void);
|
||||
|
||||
// Read the current display state. Pixels are 2 bits each (0..3 = grayscale
|
||||
// level). `out` must be at least T7_LCD_WIDTH * T7_LCD_HEIGHT bytes.
|
||||
void t7_get_display(uint8_t *out);
|
||||
|
||||
// Number of status icons surrounding the main display. Layout (matching
|
||||
// asterick/tamago):
|
||||
// Top row: 0 dashboard, 1 food, 2 trash, 3 globe, 4 user
|
||||
// Bottom row: 5 comments, 6 medkit, 7 heart, 8 book, 9 bell
|
||||
#define T7_ICON_COUNT 10
|
||||
|
||||
// Read the 10 status icon values into `out` (length T7_ICON_COUNT).
|
||||
// Each byte is a 2-bit intensity (0 = off, 1..3 = visible grayscale).
|
||||
void t7_get_icons(uint8_t *out);
|
||||
|
||||
// --- Figur / SPI-Trace (Reverse-Engineering, Etappe 1) ---
|
||||
void t7_figure_set(uint8_t id);
|
||||
uint8_t t7_figure_get(void);
|
||||
void t7_spi_set_ret(uint8_t v);
|
||||
void t7_spitrace_arm(void);
|
||||
void t7_irtrace_arm(void);
|
||||
int t7_spitrace_count(void);
|
||||
void t7_spitrace_get(int i, uint8_t *kind, uint8_t *reg, uint8_t *val, uint16_t *rep);
|
||||
|
||||
// Set the button state. `mask` is a bitmask of T7_BTN_* constants.
|
||||
// Bits set = button pressed.
|
||||
void t7_set_buttons(uint8_t mask);
|
||||
|
||||
// IR-BLE-Bridge: 32-Byte-Paket fuer die naechste Empfangsanfrage einspeisen
|
||||
// (umgeht das IR-Physical-Layer). Aus BLE-Callback oder Test-Trigger aufrufen.
|
||||
void t7_ir_bridge_rx(const uint8_t *pkt32);
|
||||
void t7_ir_bridge_enable(int on); // Bridge ein/aus (TX-Completion-Fake)
|
||||
int t7_ir_bridge_tx_poll(uint8_t *out32); // 1 = neues Sende-Paket in out32
|
||||
uint32_t t7_ir_activity(void); // steigt im IR-Verbindungsmenue
|
||||
int t7_ir_tx_take(uint8_t *out32); // T7_LINK (1.6.9): packet sent just now ($748E), 32 = new packet in out32
|
||||
int t7_ir_tx_pending(void); // 1 = Firmware wartet auf Sende-Completion
|
||||
void t7_ir_tx_complete(void); // Sende-Completion ausloesen (zeitverzoegert)
|
||||
|
||||
// Tear down the emulator and free resources.
|
||||
void t7_release(void);
|
||||
|
||||
// Debug: return current CPU state for logging. Fills the passed-in pointers
|
||||
// (any of which may be NULL) with the current register values.
|
||||
void t7_debug_get_state(uint16_t *pc, uint8_t *a, uint8_t *x, uint8_t *y,
|
||||
uint8_t *s, uint8_t *bank);
|
||||
|
||||
// Debug: return whether any dram bytes are non-zero. If the ROM hasn't
|
||||
// touched the display at all, all dram = 0 and the screen appears uniform.
|
||||
bool t7_debug_dram_dirty(void);
|
||||
|
||||
// Debug: count how many DRAM bytes are non-zero (0..512).
|
||||
uint16_t t7_debug_dram_nonzero_count(void);
|
||||
|
||||
// State serialization for save/restore. Returns total size needed when
|
||||
// `buf` is NULL or `bufsize` is too small; otherwise the number of bytes
|
||||
// actually written.
|
||||
uint32_t t7_serialize_state(uint8_t *buf, uint32_t bufsize);
|
||||
bool t7_deserialize_state(const uint8_t *buf, uint32_t bufsize);
|
||||
|
||||
// Direct WRAM read/write. Used by the RTC sync code to poke the
|
||||
// emulator's internal time counters. `addr` must be in the 0x0000-0x0FFF
|
||||
// WRAM range — addresses outside that range are silently ignored.
|
||||
uint8_t t7_ram_read(uint16_t addr);
|
||||
void t7_ram_write(uint16_t addr, uint8_t val);
|
||||
|
||||
// Profiling — snapshot + reset the hot-path counters. Used by main.c
|
||||
// to print a per-minute profile of where emulator time is going.
|
||||
// Returns the snapshot via out-params. Pass NULL to skip retrieval.
|
||||
typedef struct {
|
||||
uint32_t opcodes;
|
||||
uint32_t reads_fast;
|
||||
uint32_t reads_io;
|
||||
uint32_t writes_fast;
|
||||
uint32_t writes_io;
|
||||
uint32_t writes_dropped;
|
||||
uint32_t irqs;
|
||||
uint32_t nmis;
|
||||
uint32_t irq_entries;
|
||||
uint32_t nmi_entries;
|
||||
} t7_profile_snapshot_t;
|
||||
|
||||
void t7_profile_snapshot_and_reset(t7_profile_snapshot_t *out);
|
||||
|
||||
#endif // T7_H
|
||||
/*
|
||||
* t7.h - V7 emulator core (6502), urspruenglich fuer Pebble
|
||||
*
|
||||
* Based on asterick/tamago (JavaScript implementation):
|
||||
* https://github.com/asterick/tamago
|
||||
*
|
||||
* Hardware: GPLB52320A (8-bit 6502-family) @ 4 MHz
|
||||
* Display: 64x32 pixels, 4 grayscale levels (512 bytes DRAM)
|
||||
*
|
||||
* Memory map (16-bit address space, banked):
|
||||
* $0000-$0FFF Work RAM (1.5 KB, mirrored)
|
||||
* $1000-$2FFF Display RAM (512 bytes, mirrored)
|
||||
* $3000-$3FFF CPU control registers
|
||||
* $4000-$BFFF Banked ROM (32 KB window, 20 banks total = 640 KB)
|
||||
* $C000-$FFFF Static ROM (16 KB, always mapped from bank 0)
|
||||
*
|
||||
* The 640 KB ROM lives in Pebble flash as a resource. To save RAM we
|
||||
* only keep the currently-selected bank (32 KB) mirrored in memory.
|
||||
* Static ROM (bank 0, first 16 KB) is loaded once at boot.
|
||||
*/
|
||||
|
||||
#ifndef T7_H
|
||||
#define T7_H
|
||||
|
||||
#include "pebble_compat.h"
|
||||
|
||||
// Compile-time profile switch. Set to 1 to enable per-opcode / per-read /
|
||||
// per-write counters (logged once a minute from main.c). Adds ~10-15%
|
||||
// CPU overhead — keep OFF in production.
|
||||
#define T7_PROFILE 0
|
||||
|
||||
// Display dimensions (native V7 hardware).
|
||||
//
|
||||
// The display is 48 pixels wide × 31 pixels high — this comes from the
|
||||
// HTML template in the JS reference (canvas width=48 height=31). Although
|
||||
// the hardware has 64 segments × 32 commons, the addressing scheme in
|
||||
// _dram only gives meaningful pixels in the first 48 of each row; the
|
||||
// remaining 16 are aliased into the start of the next row's bytes.
|
||||
// The JS emulator handles this by drawing into a 48-px-wide canvas — the
|
||||
// 16 "extra" pixels per row spill into the next row and get overwritten.
|
||||
// In our C port we just clamp to 48 explicitly.
|
||||
#define T7_LCD_WIDTH 48
|
||||
#define T7_LCD_HEIGHT 31
|
||||
|
||||
// 4 grayscale levels per pixel (2 bits each)
|
||||
#define T7_PALETTE_SIZE 4
|
||||
|
||||
// CPU clock rate (Hz)
|
||||
#define T7_CLOCK_RATE 4000000
|
||||
|
||||
// Memory region sizes
|
||||
#define T7_WRAM_SIZE 0x600 // 1.5 KB work RAM
|
||||
#define T7_DRAM_SIZE 0x200 // 512 B display RAM
|
||||
#define T7_CPUREG_SIZE 0x100 // 256 B control registers
|
||||
#define T7_ROM_BANK_SIZE 0x8000 // 32 KB per bank
|
||||
#define T7_STATIC_ROM_SIZE 0x4000 // 16 KB static (high) ROM
|
||||
|
||||
// Status flag bits in P register
|
||||
#define FLAG_C 0x01 // Carry
|
||||
#define FLAG_Z 0x02 // Zero
|
||||
#define FLAG_I 0x04 // Interrupt disable
|
||||
#define FLAG_D 0x08 // Decimal mode
|
||||
#define FLAG_B 0x10 // Break (software)
|
||||
#define FLAG_U 0x20 // Unused (always 1)
|
||||
#define FLAG_V 0x40 // Overflow
|
||||
#define FLAG_N 0x80 // Negative
|
||||
|
||||
// Button bitmask (V7 has 3 buttons: A, B, C plus reset)
|
||||
#define T7_BTN_A 0x01
|
||||
#define T7_BTN_B 0x02
|
||||
#define T7_BTN_C 0x04
|
||||
#define T7_BTN_RESET 0x08
|
||||
|
||||
// Forward declaration for the system state
|
||||
typedef struct t7_system t7_system_t;
|
||||
|
||||
// Public API ---------------------------------------------------------------
|
||||
|
||||
// Initialize the emulator. Must be called once at startup. Returns false on
|
||||
// failure (e.g. ROM resource missing).
|
||||
bool t7_init(void);
|
||||
/* After t7_init: 1 = Tamagotchi Friends ROM (768 KB), 0 = Tama-Go. */
|
||||
int t7_is_friends(void);
|
||||
uint16_t t7_rtc_sec_addr(void);
|
||||
|
||||
// Reset the CPU (jumps to the reset vector). Memory and EEPROM are preserved.
|
||||
void t7_reset(void);
|
||||
|
||||
// Run the emulator for the given number of CPU cycles. Returns the actual
|
||||
// number of cycles consumed (may differ slightly due to instruction lengths).
|
||||
uint32_t t7_step_cycles(uint32_t target_cycles);
|
||||
|
||||
// Run a single instruction. Returns the cycle count of that instruction.
|
||||
uint8_t t7_step_one(void);
|
||||
|
||||
// Read the current display state. Pixels are 2 bits each (0..3 = grayscale
|
||||
// level). `out` must be at least T7_LCD_WIDTH * T7_LCD_HEIGHT bytes.
|
||||
void t7_get_display(uint8_t *out);
|
||||
|
||||
// Number of status icons surrounding the main display. Layout (matching
|
||||
// asterick/tamago):
|
||||
// Top row: 0 dashboard, 1 food, 2 trash, 3 globe, 4 user
|
||||
// Bottom row: 5 comments, 6 medkit, 7 heart, 8 book, 9 bell
|
||||
#define T7_ICON_COUNT 10
|
||||
|
||||
// Read the 10 status icon values into `out` (length T7_ICON_COUNT).
|
||||
// Each byte is a 2-bit intensity (0 = off, 1..3 = visible grayscale).
|
||||
void t7_get_icons(uint8_t *out);
|
||||
|
||||
// --- Figur / SPI-Trace (Reverse-Engineering, Etappe 1) ---
|
||||
void t7_figure_set(uint8_t id);
|
||||
uint8_t t7_figure_get(void);
|
||||
void t7_spi_set_ret(uint8_t v);
|
||||
void t7_spitrace_arm(void);
|
||||
void t7_irtrace_arm(void);
|
||||
int t7_spitrace_count(void);
|
||||
void t7_spitrace_get(int i, uint8_t *kind, uint8_t *reg, uint8_t *val, uint16_t *rep);
|
||||
|
||||
// Set the button state. `mask` is a bitmask of T7_BTN_* constants.
|
||||
// Bits set = button pressed.
|
||||
void t7_set_buttons(uint8_t mask);
|
||||
|
||||
// IR-BLE-Bridge: 32-Byte-Paket fuer die naechste Empfangsanfrage einspeisen
|
||||
// (umgeht das IR-Physical-Layer). Aus BLE-Callback oder Test-Trigger aufrufen.
|
||||
void t7_ir_bridge_rx(const uint8_t *pkt32);
|
||||
void t7_ir_bridge_enable(int on); // Bridge ein/aus (TX-Completion-Fake)
|
||||
int t7_ir_bridge_tx_poll(uint8_t *out32); // 1 = neues Sende-Paket in out32
|
||||
uint32_t t7_ir_activity(void); // steigt im IR-Verbindungsmenue
|
||||
int t7_ir_tx_take(uint8_t *out32); // T7_LINK (1.6.9): packet sent just now ($748E), 32 = new packet in out32
|
||||
int t7_ir_tx_pending(void); // 1 = Firmware wartet auf Sende-Completion
|
||||
void t7_ir_tx_complete(void); // Sende-Completion ausloesen (zeitverzoegert)
|
||||
|
||||
// Tear down the emulator and free resources.
|
||||
void t7_release(void);
|
||||
|
||||
// Debug: return current CPU state for logging. Fills the passed-in pointers
|
||||
// (any of which may be NULL) with the current register values.
|
||||
void t7_debug_get_state(uint16_t *pc, uint8_t *a, uint8_t *x, uint8_t *y,
|
||||
uint8_t *s, uint8_t *bank);
|
||||
|
||||
// Debug: return whether any dram bytes are non-zero. If the ROM hasn't
|
||||
// touched the display at all, all dram = 0 and the screen appears uniform.
|
||||
bool t7_debug_dram_dirty(void);
|
||||
|
||||
// Debug: count how many DRAM bytes are non-zero (0..512).
|
||||
uint16_t t7_debug_dram_nonzero_count(void);
|
||||
|
||||
// State serialization for save/restore. Returns total size needed when
|
||||
// `buf` is NULL or `bufsize` is too small; otherwise the number of bytes
|
||||
// actually written.
|
||||
uint32_t t7_serialize_state(uint8_t *buf, uint32_t bufsize);
|
||||
bool t7_deserialize_state(const uint8_t *buf, uint32_t bufsize);
|
||||
|
||||
// Direct WRAM read/write. Used by the RTC sync code to poke the
|
||||
// emulator's internal time counters. `addr` must be in the 0x0000-0x0FFF
|
||||
// WRAM range — addresses outside that range are silently ignored.
|
||||
uint8_t t7_ram_read(uint16_t addr);
|
||||
void t7_ram_write(uint16_t addr, uint8_t val);
|
||||
|
||||
// Profiling — snapshot + reset the hot-path counters. Used by main.c
|
||||
// to print a per-minute profile of where emulator time is going.
|
||||
// Returns the snapshot via out-params. Pass NULL to skip retrieval.
|
||||
typedef struct {
|
||||
uint32_t opcodes;
|
||||
uint32_t reads_fast;
|
||||
uint32_t reads_io;
|
||||
uint32_t writes_fast;
|
||||
uint32_t writes_io;
|
||||
uint32_t writes_dropped;
|
||||
uint32_t irqs;
|
||||
uint32_t nmis;
|
||||
uint32_t irq_entries;
|
||||
uint32_t nmi_entries;
|
||||
} t7_profile_snapshot_t;
|
||||
|
||||
void t7_profile_snapshot_and_reset(t7_profile_snapshot_t *out);
|
||||
|
||||
#endif // T7_H
|
||||
+885
-885
File diff suppressed because it is too large.
Load diff
+255
-255
@@ -1,255 +1,255 @@
|
||||
/*
|
||||
* t7_eeprom.c - emulated 4 KB I²C EEPROM backed by Pebble persist
|
||||
*
|
||||
* Faithful port of asterick/tamago src/cpu/eeprom.js. The state
|
||||
* machine, command set, and bit timing all match exactly so save data
|
||||
* written by the JS emulator could in principle be loaded here and vice
|
||||
* versa.
|
||||
*
|
||||
* Persistence layout: the 4 KB EEPROM is split into 16 pages of 256
|
||||
* bytes each, stored in Pebble persist keys EEPROM_PERSIST_BASE +
|
||||
* page_index. Pebble's persist storage supports up to 256 bytes per key
|
||||
* and ~4 KB total per app, which is exactly enough.
|
||||
*
|
||||
* Pages are loaded on init. Writes are batched: we set a dirty bit per
|
||||
* page and only push to persist when t7_eeprom_flush() is called,
|
||||
* or when an I²C STOP after a WRITE command commits the data. Persist
|
||||
* writes are slow (flash-backed) so batching matters.
|
||||
*/
|
||||
|
||||
#include "t7_eeprom.h"
|
||||
#include "pebble_compat.h"
|
||||
#ifndef T7_FREESTANDING
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
#define EEPROM_BIT_WIDTH 12
|
||||
#define EEPROM_SIZE (1 << EEPROM_BIT_WIDTH) // 4096
|
||||
#define EEPROM_MASK (EEPROM_SIZE - 1)
|
||||
#define EEPROM_ADDR_BYTES 2 // ceil(12/8)
|
||||
#define EEPROM_PAGE_SIZE 256
|
||||
#define EEPROM_PAGE_COUNT (EEPROM_SIZE / EEPROM_PAGE_SIZE) // 16
|
||||
#define EEPROM_PERSIST_BASE 100u // persist keys 100..115 reserved for us
|
||||
|
||||
// State machine — values match JS eeprom.js
|
||||
typedef enum {
|
||||
ST_DISABLED = 0,
|
||||
ST_COMMAND = 1,
|
||||
ST_ADDRESS = 2,
|
||||
ST_READ = 3,
|
||||
ST_WRITE = 4,
|
||||
} eeprom_state_t;
|
||||
|
||||
static uint8_t s_data[EEPROM_SIZE]; // 4 KB in .bss
|
||||
static uint16_t s_dirty; // bit N set ⇒ page N needs flush
|
||||
|
||||
// I²C bus state
|
||||
static eeprom_state_t s_state;
|
||||
static uint8_t s_output;
|
||||
static uint8_t s_last_clk;
|
||||
static uint8_t s_last_data;
|
||||
|
||||
// Transfer state
|
||||
static uint8_t s_read; // 8-bit shift register (bit being assembled)
|
||||
static uint8_t s_bits_tx; // bit counter within byte (0..8, 8 = ACK)
|
||||
static uint16_t s_address; // current address pointer (12 bits)
|
||||
static uint8_t s_addressbyte; // how many address bytes have we received
|
||||
|
||||
// ----- Persistence -------------------------------------------------------
|
||||
|
||||
void t7_eeprom_init(void)
|
||||
{
|
||||
// Initial bus state: power off, output high (NACK)
|
||||
s_state = ST_DISABLED;
|
||||
s_output = 1;
|
||||
s_last_clk = 0;
|
||||
s_last_data = 0;
|
||||
s_read = 0;
|
||||
s_bits_tx = 0;
|
||||
s_address = 0;
|
||||
s_addressbyte = 0;
|
||||
s_dirty = 0;
|
||||
|
||||
// Load saved pages from persist. Missing pages stay zero-filled (the
|
||||
// Tama firmware treats blank EEPROM as "first boot, run init").
|
||||
memset(s_data, 0, sizeof(s_data));
|
||||
int loaded_bytes = 0;
|
||||
for (int p = 0; p < EEPROM_PAGE_COUNT; p++) {
|
||||
uint32_t key = EEPROM_PERSIST_BASE + p;
|
||||
if (persist_exists(key)) {
|
||||
int n = persist_read_data(key, &s_data[p * EEPROM_PAGE_SIZE],
|
||||
EEPROM_PAGE_SIZE);
|
||||
if (n > 0) loaded_bytes += n;
|
||||
}
|
||||
}
|
||||
APP_LOG(APP_LOG_LEVEL_INFO,
|
||||
"t7_eeprom: loaded %d bytes from persist",
|
||||
loaded_bytes);
|
||||
}
|
||||
|
||||
void t7_eeprom_flush(void)
|
||||
{
|
||||
if (!s_dirty) return;
|
||||
int pages_written = 0;
|
||||
for (int p = 0; p < EEPROM_PAGE_COUNT; p++) {
|
||||
if (s_dirty & (1u << p)) {
|
||||
uint32_t key = EEPROM_PERSIST_BASE + p;
|
||||
status_t st = persist_write_data(key,
|
||||
&s_data[p * EEPROM_PAGE_SIZE],
|
||||
EEPROM_PAGE_SIZE);
|
||||
if (st >= 0) {
|
||||
s_dirty &= ~(1u << p);
|
||||
pages_written++;
|
||||
} else {
|
||||
APP_LOG(APP_LOG_LEVEL_ERROR,
|
||||
"t7_eeprom: persist_write_data key=%u failed (%d)",
|
||||
(unsigned)key, (int)st);
|
||||
// Leave the dirty bit set so we retry next time.
|
||||
}
|
||||
}
|
||||
}
|
||||
if (pages_written > 0) {
|
||||
APP_LOG(APP_LOG_LEVEL_INFO,
|
||||
"t7_eeprom: flushed %d page(s)", pages_written);
|
||||
}
|
||||
}
|
||||
|
||||
void t7_eeprom_wipe(void)
|
||||
{
|
||||
// Clear all 4 KB of EEPROM in-memory + delete all persisted pages.
|
||||
// The Tama firmware will see "blank" EEPROM on the next boot and
|
||||
// start fresh (egg-select screen).
|
||||
memset(s_data, 0, sizeof(s_data));
|
||||
s_dirty = 0;
|
||||
for (int p = 0; p < EEPROM_PAGE_COUNT; p++) {
|
||||
uint32_t key = EEPROM_PERSIST_BASE + p;
|
||||
persist_delete(key);
|
||||
}
|
||||
APP_LOG(APP_LOG_LEVEL_INFO,
|
||||
"t7_eeprom: wiped (in-memory + persist)");
|
||||
}
|
||||
|
||||
uint8_t t7_eeprom_output(void)
|
||||
{
|
||||
return s_output;
|
||||
}
|
||||
|
||||
// ----- Bus state machine -------------------------------------------------
|
||||
//
|
||||
// Translated 1:1 from eeprom.js update(power, clk, data).
|
||||
|
||||
void t7_eeprom_update(uint8_t power, uint8_t clk, uint8_t data)
|
||||
{
|
||||
clk = clk ? 1 : 0;
|
||||
data = data ? 1 : 0;
|
||||
|
||||
int clk_d = (int)clk - (int)s_last_clk;
|
||||
int data_d = (int)data - (int)s_last_data;
|
||||
|
||||
s_last_clk = clk;
|
||||
s_last_data = data;
|
||||
|
||||
// No power → chip idle, NACK on the bus.
|
||||
if (!power) {
|
||||
s_state = ST_DISABLED;
|
||||
s_output = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
// No bus change → no work.
|
||||
if (!clk_d && !data_d) return;
|
||||
|
||||
// Data transition while CLK high = START/STOP condition.
|
||||
if (clk && data_d) {
|
||||
if (data_d > 0) {
|
||||
// STOP condition. The page was marked dirty when we received the
|
||||
// last write byte; the actual persist_write_data() flush is
|
||||
// deferred to the periodic timer in main.c (every 60s) plus
|
||||
// t7_release() on app exit. Persist writes are flash-backed
|
||||
// (~10 ms each) and the ROM does dense write bursts during boot
|
||||
// and save points — flushing eagerly here drops the emulator
|
||||
// throughput by ~50% on heavy-write phases.
|
||||
s_state = ST_DISABLED;
|
||||
s_output = 0;
|
||||
} else {
|
||||
// START condition.
|
||||
s_state = ST_COMMAND;
|
||||
s_output = 0;
|
||||
s_bits_tx = 0;
|
||||
s_read = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// If we ended up disabled (no active transfer), nothing more to do.
|
||||
if (s_state == ST_DISABLED) return;
|
||||
|
||||
if (clk_d > 0) {
|
||||
// Rising edge: sample data line into shift register (host → device).
|
||||
s_read = (uint8_t)((s_read << 1) | data);
|
||||
} else if (clk_d < 0) {
|
||||
// Falling edge: drive next output bit (device → host) OR ACK.
|
||||
if (s_bits_tx < 8) {
|
||||
// Mid-byte: drive output bit if we're in READ mode, else high.
|
||||
if (s_state == ST_READ) {
|
||||
s_output = ((s_data[s_address] << s_bits_tx) & 0x80) ? 1 : 0;
|
||||
} else {
|
||||
s_output = 1;
|
||||
}
|
||||
} else if (s_bits_tx == 8) {
|
||||
// Byte boundary: send ACK and process the byte we just received.
|
||||
s_output = 0; // ACK
|
||||
|
||||
switch (s_state) {
|
||||
case ST_COMMAND:
|
||||
// I²C EEPROM address pattern. 0xA0/0xA1 = Atmel 24Cxx family
|
||||
// device address with R/W bit; the chip ignores the middle
|
||||
// bits hence the 0xF1 mask.
|
||||
switch (s_read & 0xF1) {
|
||||
case 0xA0: // Write command
|
||||
s_state = ST_ADDRESS;
|
||||
s_addressbyte = 0;
|
||||
s_address = 0;
|
||||
break;
|
||||
case 0xA1: // Read command
|
||||
s_state = ST_READ;
|
||||
break;
|
||||
default:
|
||||
s_output = 1; // NACK
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case ST_ADDRESS:
|
||||
s_address = (uint16_t)((s_address << 8) | s_read);
|
||||
if (++s_addressbyte >= EEPROM_ADDR_BYTES) {
|
||||
s_state = ST_WRITE;
|
||||
s_address &= EEPROM_MASK;
|
||||
}
|
||||
break;
|
||||
|
||||
case ST_WRITE: {
|
||||
uint16_t addr = s_address & EEPROM_MASK;
|
||||
s_data[addr] = s_read;
|
||||
#ifdef T7_EEDBG
|
||||
{ extern void t7_eedbg(uint16_t, uint8_t); t7_eedbg(addr, s_read); }
|
||||
#endif
|
||||
// Mark page dirty for the next flush.
|
||||
s_dirty |= (1u << (addr / EEPROM_PAGE_SIZE));
|
||||
s_address = (s_address + 1) & EEPROM_MASK;
|
||||
break;
|
||||
}
|
||||
|
||||
case ST_READ:
|
||||
// Address pointer auto-increments on read too — matches
|
||||
// 24Cxx sequential-read behaviour.
|
||||
s_address = (s_address + 1) & EEPROM_MASK;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
s_bits_tx = (uint8_t)((s_bits_tx + 1) % 9);
|
||||
}
|
||||
}
|
||||
/*
|
||||
* t7_eeprom.c - emulated 4 KB I²C EEPROM backed by Pebble persist
|
||||
*
|
||||
* Faithful port of asterick/tamago src/cpu/eeprom.js. The state
|
||||
* machine, command set, and bit timing all match exactly so save data
|
||||
* written by the JS emulator could in principle be loaded here and vice
|
||||
* versa.
|
||||
*
|
||||
* Persistence layout: the 4 KB EEPROM is split into 16 pages of 256
|
||||
* bytes each, stored in Pebble persist keys EEPROM_PERSIST_BASE +
|
||||
* page_index. Pebble's persist storage supports up to 256 bytes per key
|
||||
* and ~4 KB total per app, which is exactly enough.
|
||||
*
|
||||
* Pages are loaded on init. Writes are batched: we set a dirty bit per
|
||||
* page and only push to persist when t7_eeprom_flush() is called,
|
||||
* or when an I²C STOP after a WRITE command commits the data. Persist
|
||||
* writes are slow (flash-backed) so batching matters.
|
||||
*/
|
||||
|
||||
#include "t7_eeprom.h"
|
||||
#include "pebble_compat.h"
|
||||
#ifndef T7_FREESTANDING
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
#define EEPROM_BIT_WIDTH 12
|
||||
#define EEPROM_SIZE (1 << EEPROM_BIT_WIDTH) // 4096
|
||||
#define EEPROM_MASK (EEPROM_SIZE - 1)
|
||||
#define EEPROM_ADDR_BYTES 2 // ceil(12/8)
|
||||
#define EEPROM_PAGE_SIZE 256
|
||||
#define EEPROM_PAGE_COUNT (EEPROM_SIZE / EEPROM_PAGE_SIZE) // 16
|
||||
#define EEPROM_PERSIST_BASE 100u // persist keys 100..115 reserved for us
|
||||
|
||||
// State machine — values match JS eeprom.js
|
||||
typedef enum {
|
||||
ST_DISABLED = 0,
|
||||
ST_COMMAND = 1,
|
||||
ST_ADDRESS = 2,
|
||||
ST_READ = 3,
|
||||
ST_WRITE = 4,
|
||||
} eeprom_state_t;
|
||||
|
||||
static uint8_t s_data[EEPROM_SIZE]; // 4 KB in .bss
|
||||
static uint16_t s_dirty; // bit N set ⇒ page N needs flush
|
||||
|
||||
// I²C bus state
|
||||
static eeprom_state_t s_state;
|
||||
static uint8_t s_output;
|
||||
static uint8_t s_last_clk;
|
||||
static uint8_t s_last_data;
|
||||
|
||||
// Transfer state
|
||||
static uint8_t s_read; // 8-bit shift register (bit being assembled)
|
||||
static uint8_t s_bits_tx; // bit counter within byte (0..8, 8 = ACK)
|
||||
static uint16_t s_address; // current address pointer (12 bits)
|
||||
static uint8_t s_addressbyte; // how many address bytes have we received
|
||||
|
||||
// ----- Persistence -------------------------------------------------------
|
||||
|
||||
void t7_eeprom_init(void)
|
||||
{
|
||||
// Initial bus state: power off, output high (NACK)
|
||||
s_state = ST_DISABLED;
|
||||
s_output = 1;
|
||||
s_last_clk = 0;
|
||||
s_last_data = 0;
|
||||
s_read = 0;
|
||||
s_bits_tx = 0;
|
||||
s_address = 0;
|
||||
s_addressbyte = 0;
|
||||
s_dirty = 0;
|
||||
|
||||
// Load saved pages from persist. Missing pages stay zero-filled (the
|
||||
// Tama firmware treats blank EEPROM as "first boot, run init").
|
||||
memset(s_data, 0, sizeof(s_data));
|
||||
int loaded_bytes = 0;
|
||||
for (int p = 0; p < EEPROM_PAGE_COUNT; p++) {
|
||||
uint32_t key = EEPROM_PERSIST_BASE + p;
|
||||
if (persist_exists(key)) {
|
||||
int n = persist_read_data(key, &s_data[p * EEPROM_PAGE_SIZE],
|
||||
EEPROM_PAGE_SIZE);
|
||||
if (n > 0) loaded_bytes += n;
|
||||
}
|
||||
}
|
||||
APP_LOG(APP_LOG_LEVEL_INFO,
|
||||
"t7_eeprom: loaded %d bytes from persist",
|
||||
loaded_bytes);
|
||||
}
|
||||
|
||||
void t7_eeprom_flush(void)
|
||||
{
|
||||
if (!s_dirty) return;
|
||||
int pages_written = 0;
|
||||
for (int p = 0; p < EEPROM_PAGE_COUNT; p++) {
|
||||
if (s_dirty & (1u << p)) {
|
||||
uint32_t key = EEPROM_PERSIST_BASE + p;
|
||||
status_t st = persist_write_data(key,
|
||||
&s_data[p * EEPROM_PAGE_SIZE],
|
||||
EEPROM_PAGE_SIZE);
|
||||
if (st >= 0) {
|
||||
s_dirty &= ~(1u << p);
|
||||
pages_written++;
|
||||
} else {
|
||||
APP_LOG(APP_LOG_LEVEL_ERROR,
|
||||
"t7_eeprom: persist_write_data key=%u failed (%d)",
|
||||
(unsigned)key, (int)st);
|
||||
// Leave the dirty bit set so we retry next time.
|
||||
}
|
||||
}
|
||||
}
|
||||
if (pages_written > 0) {
|
||||
APP_LOG(APP_LOG_LEVEL_INFO,
|
||||
"t7_eeprom: flushed %d page(s)", pages_written);
|
||||
}
|
||||
}
|
||||
|
||||
void t7_eeprom_wipe(void)
|
||||
{
|
||||
// Clear all 4 KB of EEPROM in-memory + delete all persisted pages.
|
||||
// The Tama firmware will see "blank" EEPROM on the next boot and
|
||||
// start fresh (egg-select screen).
|
||||
memset(s_data, 0, sizeof(s_data));
|
||||
s_dirty = 0;
|
||||
for (int p = 0; p < EEPROM_PAGE_COUNT; p++) {
|
||||
uint32_t key = EEPROM_PERSIST_BASE + p;
|
||||
persist_delete(key);
|
||||
}
|
||||
APP_LOG(APP_LOG_LEVEL_INFO,
|
||||
"t7_eeprom: wiped (in-memory + persist)");
|
||||
}
|
||||
|
||||
uint8_t t7_eeprom_output(void)
|
||||
{
|
||||
return s_output;
|
||||
}
|
||||
|
||||
// ----- Bus state machine -------------------------------------------------
|
||||
//
|
||||
// Translated 1:1 from eeprom.js update(power, clk, data).
|
||||
|
||||
void t7_eeprom_update(uint8_t power, uint8_t clk, uint8_t data)
|
||||
{
|
||||
clk = clk ? 1 : 0;
|
||||
data = data ? 1 : 0;
|
||||
|
||||
int clk_d = (int)clk - (int)s_last_clk;
|
||||
int data_d = (int)data - (int)s_last_data;
|
||||
|
||||
s_last_clk = clk;
|
||||
s_last_data = data;
|
||||
|
||||
// No power → chip idle, NACK on the bus.
|
||||
if (!power) {
|
||||
s_state = ST_DISABLED;
|
||||
s_output = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
// No bus change → no work.
|
||||
if (!clk_d && !data_d) return;
|
||||
|
||||
// Data transition while CLK high = START/STOP condition.
|
||||
if (clk && data_d) {
|
||||
if (data_d > 0) {
|
||||
// STOP condition. The page was marked dirty when we received the
|
||||
// last write byte; the actual persist_write_data() flush is
|
||||
// deferred to the periodic timer in main.c (every 60s) plus
|
||||
// t7_release() on app exit. Persist writes are flash-backed
|
||||
// (~10 ms each) and the ROM does dense write bursts during boot
|
||||
// and save points — flushing eagerly here drops the emulator
|
||||
// throughput by ~50% on heavy-write phases.
|
||||
s_state = ST_DISABLED;
|
||||
s_output = 0;
|
||||
} else {
|
||||
// START condition.
|
||||
s_state = ST_COMMAND;
|
||||
s_output = 0;
|
||||
s_bits_tx = 0;
|
||||
s_read = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// If we ended up disabled (no active transfer), nothing more to do.
|
||||
if (s_state == ST_DISABLED) return;
|
||||
|
||||
if (clk_d > 0) {
|
||||
// Rising edge: sample data line into shift register (host → device).
|
||||
s_read = (uint8_t)((s_read << 1) | data);
|
||||
} else if (clk_d < 0) {
|
||||
// Falling edge: drive next output bit (device → host) OR ACK.
|
||||
if (s_bits_tx < 8) {
|
||||
// Mid-byte: drive output bit if we're in READ mode, else high.
|
||||
if (s_state == ST_READ) {
|
||||
s_output = ((s_data[s_address] << s_bits_tx) & 0x80) ? 1 : 0;
|
||||
} else {
|
||||
s_output = 1;
|
||||
}
|
||||
} else if (s_bits_tx == 8) {
|
||||
// Byte boundary: send ACK and process the byte we just received.
|
||||
s_output = 0; // ACK
|
||||
|
||||
switch (s_state) {
|
||||
case ST_COMMAND:
|
||||
// I²C EEPROM address pattern. 0xA0/0xA1 = Atmel 24Cxx family
|
||||
// device address with R/W bit; the chip ignores the middle
|
||||
// bits hence the 0xF1 mask.
|
||||
switch (s_read & 0xF1) {
|
||||
case 0xA0: // Write command
|
||||
s_state = ST_ADDRESS;
|
||||
s_addressbyte = 0;
|
||||
s_address = 0;
|
||||
break;
|
||||
case 0xA1: // Read command
|
||||
s_state = ST_READ;
|
||||
break;
|
||||
default:
|
||||
s_output = 1; // NACK
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case ST_ADDRESS:
|
||||
s_address = (uint16_t)((s_address << 8) | s_read);
|
||||
if (++s_addressbyte >= EEPROM_ADDR_BYTES) {
|
||||
s_state = ST_WRITE;
|
||||
s_address &= EEPROM_MASK;
|
||||
}
|
||||
break;
|
||||
|
||||
case ST_WRITE: {
|
||||
uint16_t addr = s_address & EEPROM_MASK;
|
||||
s_data[addr] = s_read;
|
||||
#ifdef T7_EEDBG
|
||||
{ extern void t7_eedbg(uint16_t, uint8_t); t7_eedbg(addr, s_read); }
|
||||
#endif
|
||||
// Mark page dirty for the next flush.
|
||||
s_dirty |= (1u << (addr / EEPROM_PAGE_SIZE));
|
||||
s_address = (s_address + 1) & EEPROM_MASK;
|
||||
break;
|
||||
}
|
||||
|
||||
case ST_READ:
|
||||
// Address pointer auto-increments on read too — matches
|
||||
// 24Cxx sequential-read behaviour.
|
||||
s_address = (s_address + 1) & EEPROM_MASK;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
s_bits_tx = (uint8_t)((s_bits_tx + 1) % 9);
|
||||
}
|
||||
}
|
||||
+53
-53
@@ -1,53 +1,53 @@
|
||||
/*
|
||||
* t7_eeprom.h - emulated 4 KB I²C EEPROM backed by Pebble persist
|
||||
*
|
||||
* The real V7 has a 32 kbit (4 KB) external SPI/I²C EEPROM that
|
||||
* holds all save data — Tama character, growth stage, stats, etc. When
|
||||
* power cycles the internal RAM is lost but the EEPROM persists.
|
||||
*
|
||||
* We emulate that EEPROM in RAM and back it with Pebble's persist
|
||||
* storage so Tama state survives app exits and watch reboots.
|
||||
*
|
||||
* Wire protocol (matches asterick/tamago eeprom.js exactly):
|
||||
* START = data falling edge while CLK high → state = COMMAND
|
||||
* STOP = data rising edge while CLK high → if state was WRITE, commit
|
||||
* bit = sampled on CLK rising edge (input)
|
||||
* = driven on CLK falling edge (output, only during READ)
|
||||
*
|
||||
* Commands (sent after START):
|
||||
* $A0 Write — followed by 2 address bytes, then data bytes
|
||||
* $A1 Read — start streaming data bytes from current address
|
||||
*
|
||||
* Wired to Port B ($3015 = direction, $3016 = data):
|
||||
* bit 2 POWER (chip enable)
|
||||
* bit 1 CLK
|
||||
* bit 0 DATA (input from CPU when configured as output;
|
||||
* driven by EEPROM when CPU configures as input)
|
||||
*/
|
||||
|
||||
#ifndef T7_EEPROM_H
|
||||
#define T7_EEPROM_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// Load saved data from persist storage. Call once during t7_init.
|
||||
void t7_eeprom_init(void);
|
||||
|
||||
// Flush all dirty pages back to persist. Call on app exit and
|
||||
// optionally periodically.
|
||||
void t7_eeprom_flush(void);
|
||||
|
||||
// Wipe the entire EEPROM (in-memory and persist storage). The Tama
|
||||
// firmware will boot into its first-time setup flow on the next start.
|
||||
void t7_eeprom_wipe(void);
|
||||
|
||||
// Update the bus state. Equivalent to JS's eeprom.update(power, clk, data).
|
||||
// power, clk, data are 0/non-zero (booleans).
|
||||
void t7_eeprom_update(uint8_t power, uint8_t clk, uint8_t data);
|
||||
|
||||
// Current output bit (what the EEPROM is driving on the data line).
|
||||
// 0 = pulled low, 1 = high-impedance / NACK.
|
||||
uint8_t t7_eeprom_output(void);
|
||||
|
||||
#endif // T7_EEPROM_H
|
||||
/*
|
||||
* t7_eeprom.h - emulated 4 KB I²C EEPROM backed by Pebble persist
|
||||
*
|
||||
* The real V7 has a 32 kbit (4 KB) external SPI/I²C EEPROM that
|
||||
* holds all save data — Tama character, growth stage, stats, etc. When
|
||||
* power cycles the internal RAM is lost but the EEPROM persists.
|
||||
*
|
||||
* We emulate that EEPROM in RAM and back it with Pebble's persist
|
||||
* storage so Tama state survives app exits and watch reboots.
|
||||
*
|
||||
* Wire protocol (matches asterick/tamago eeprom.js exactly):
|
||||
* START = data falling edge while CLK high → state = COMMAND
|
||||
* STOP = data rising edge while CLK high → if state was WRITE, commit
|
||||
* bit = sampled on CLK rising edge (input)
|
||||
* = driven on CLK falling edge (output, only during READ)
|
||||
*
|
||||
* Commands (sent after START):
|
||||
* $A0 Write — followed by 2 address bytes, then data bytes
|
||||
* $A1 Read — start streaming data bytes from current address
|
||||
*
|
||||
* Wired to Port B ($3015 = direction, $3016 = data):
|
||||
* bit 2 POWER (chip enable)
|
||||
* bit 1 CLK
|
||||
* bit 0 DATA (input from CPU when configured as output;
|
||||
* driven by EEPROM when CPU configures as input)
|
||||
*/
|
||||
|
||||
#ifndef T7_EEPROM_H
|
||||
#define T7_EEPROM_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// Load saved data from persist storage. Call once during t7_init.
|
||||
void t7_eeprom_init(void);
|
||||
|
||||
// Flush all dirty pages back to persist. Call on app exit and
|
||||
// optionally periodically.
|
||||
void t7_eeprom_flush(void);
|
||||
|
||||
// Wipe the entire EEPROM (in-memory and persist storage). The Tama
|
||||
// firmware will boot into its first-time setup flow on the next start.
|
||||
void t7_eeprom_wipe(void);
|
||||
|
||||
// Update the bus state. Equivalent to JS's eeprom.update(power, clk, data).
|
||||
// power, clk, data are 0/non-zero (booleans).
|
||||
void t7_eeprom_update(uint8_t power, uint8_t clk, uint8_t data);
|
||||
|
||||
// Current output bit (what the EEPROM is driving on the data line).
|
||||
// 0 = pulled low, 1 = high-impedance / NACK.
|
||||
uint8_t t7_eeprom_output(void);
|
||||
|
||||
#endif // T7_EEPROM_H
|
||||
+164
-164
@@ -1,164 +1,164 @@
|
||||
/*
|
||||
* t7_internal.h - shared state structure for the emulator modules
|
||||
*
|
||||
* Not exposed to the Pebble app — only included by t7_*.c files.
|
||||
*
|
||||
* Performance note: hot-path state (CPU registers, work RAM, display RAM,
|
||||
* CPU register array, page tables) is exposed as separate global
|
||||
* variables rather than fields of a single g_sys struct. The reason is
|
||||
* that GCC treats `g_sys->foo` as a load through a pointer that could be
|
||||
* invalidated by any function call (because g_sys itself could in theory
|
||||
* be reassigned). With globals it can compute the address at compile time
|
||||
* and skip a level of indirection in the inner emulation loop. This
|
||||
* matters at ~750,000 memory accesses per emulated frame.
|
||||
*/
|
||||
|
||||
#ifndef T7_INTERNAL_H
|
||||
#define T7_INTERNAL_H
|
||||
|
||||
// ----- Hot-Path-Platzierung: heisser Emulator-Pfad ins IRAM (kein Flash-
|
||||
// Cache-Miss). Bei IRAM-Overflow im Linker -> #define T7_HOT leer.
|
||||
#ifndef T7_HOT
|
||||
# if defined(ESP_PLATFORM)
|
||||
# include "esp_attr.h"
|
||||
# define T7_HOT IRAM_ATTR
|
||||
# else
|
||||
# define T7_HOT
|
||||
# endif
|
||||
#endif
|
||||
|
||||
|
||||
#include "t7.h"
|
||||
|
||||
// 6502 registers (split out as a global, not behind a pointer).
|
||||
typedef struct {
|
||||
uint16_t pc;
|
||||
uint8_t a;
|
||||
uint8_t x;
|
||||
uint8_t y;
|
||||
uint8_t s;
|
||||
uint8_t p;
|
||||
} cpu6502_t;
|
||||
|
||||
// Small frequently-accessed state kept as globals (compile-time addresses).
|
||||
extern cpu6502_t g_cpu;
|
||||
extern uint8_t g_wram[T7_WRAM_SIZE];
|
||||
extern uint8_t g_dram[T7_DRAM_SIZE];
|
||||
extern uint8_t g_cpureg[T7_CPUREG_SIZE];
|
||||
|
||||
// Large buffers are heap-allocated to keep .bss under the Pebble's
|
||||
// 16-bit virtual-size limit (65535 bytes). They're only ever touched
|
||||
// through the page table after init, so a heap pointer costs the same
|
||||
// as a global array: page_table[p] already holds the absolute address
|
||||
// either way.
|
||||
extern uint8_t *g_rom_bank_buf;
|
||||
extern uint8_t *g_static_rom;
|
||||
|
||||
// IRQ vector table (16 entries, loaded once at init).
|
||||
extern uint16_t g_irq_vectors[16];
|
||||
|
||||
// Set non-zero by t7_fire_irq whenever it sets a bit in cpureg[$73:$74].
|
||||
// Cleared by t7_cpu_irq when it services the highest-priority pending
|
||||
// IRQ. The CPU step checks this single byte instead of reloading both
|
||||
// cpureg bytes on every instruction — at ~3 million steps/sec that saves
|
||||
// ~6 million memory loads/sec.
|
||||
extern uint8_t g_irq_pending_any;
|
||||
/* 1.6.9 (test12) battery, as TamaGo-Pebble v0.3.0: T7_IDLE jumps over the passes of the main loop's wait at $CE66
|
||||
* (bit-identical), T7_STOP halts the CPU when the firmware writes 7 to $3001 (until an IRQ or a key) */
|
||||
#define T7_IDLE_PC 0xCE66u
|
||||
extern uint8_t g_t7_idle_ok, g_t7_halted;
|
||||
#ifdef T7_JSR_HLE
|
||||
extern uint8_t g_t7_hle, g_t7_dark;
|
||||
#endif
|
||||
extern uint32_t g_t7_idle_cyc, g_t7_keyact;
|
||||
void t7_idle_check(void);
|
||||
|
||||
// Misc runtime state.
|
||||
extern uint8_t g_rom_bank_id;
|
||||
extern bool g_rom_loaded;
|
||||
extern uint8_t g_keys;
|
||||
extern ResHandle g_rom_resource;
|
||||
|
||||
// Whether the emulator is initialised. Used by t7_release to know
|
||||
// whether g_dram etc were ever populated.
|
||||
extern bool g_initialised;
|
||||
|
||||
// ----- Memory access (fast inline path) -----------------------------------
|
||||
//
|
||||
// Page table: 256 entries, one per 256-byte page of the 64K address space.
|
||||
// Each entry points at the backing RAM/ROM buffer for that page, or NULL
|
||||
// if the page needs slow-path I/O handling.
|
||||
extern uint8_t *t7_read_page[256];
|
||||
extern uint8_t *t7_write_page[256];
|
||||
|
||||
uint8_t t7_io_read(uint16_t addr);
|
||||
void t7_io_write(uint16_t addr, uint8_t value);
|
||||
|
||||
// ----- Profiling counters (referenced by the inline read/write below) -----
|
||||
//
|
||||
// T7_PROFILE is set in the public t7.h. When OFF (the production
|
||||
// default), PROFILE_INC compiles to nothing and the counter struct
|
||||
// isn't even instantiated.
|
||||
|
||||
#if T7_PROFILE
|
||||
typedef struct {
|
||||
uint32_t opcodes; // t7_cpu_step calls
|
||||
uint32_t reads_fast; // hit page-table (WRAM/DRAM/ROM)
|
||||
uint32_t reads_io; // missed → t7_io_read
|
||||
uint32_t writes_fast; // hit page-table
|
||||
uint32_t writes_io; // hit I/O area
|
||||
uint32_t writes_dropped; // hit ROM (silently ignored)
|
||||
uint32_t irqs; // t7_fire_irq calls
|
||||
uint32_t nmis; // t7_fire_nmi calls
|
||||
uint32_t irq_entries; // actual CPU IRQ entries (after mask check)
|
||||
uint32_t nmi_entries; // actual CPU NMI entries
|
||||
} t7_profile_t;
|
||||
|
||||
extern t7_profile_t g_t7_profile;
|
||||
# define PROFILE_INC(field) (g_t7_profile.field++)
|
||||
#else
|
||||
# define PROFILE_INC(field) ((void)0)
|
||||
#endif
|
||||
|
||||
#define T7_INLINE static inline __attribute__((always_inline))
|
||||
|
||||
T7_INLINE uint8_t t7_read(uint16_t addr)
|
||||
{
|
||||
uint8_t *page = t7_read_page[addr >> 8];
|
||||
if (page) { PROFILE_INC(reads_fast); return page[addr & 0xFF]; }
|
||||
PROFILE_INC(reads_io);
|
||||
return t7_io_read(addr);
|
||||
}
|
||||
|
||||
T7_INLINE uint16_t t7_read16(uint16_t addr)
|
||||
{
|
||||
uint8_t lo = t7_read(addr);
|
||||
uint8_t hi = t7_read((addr + 1) & 0xFFFF);
|
||||
return lo | (hi << 8);
|
||||
}
|
||||
|
||||
T7_INLINE void t7_write(uint16_t addr, uint8_t value)
|
||||
{
|
||||
uint8_t *page = t7_write_page[addr >> 8];
|
||||
if (page) { PROFILE_INC(writes_fast); page[addr & 0xFF] = value; return; }
|
||||
if ((addr >> 12) == 0x3) { PROFILE_INC(writes_io); t7_io_write(addr, value); return; }
|
||||
PROFILE_INC(writes_dropped);
|
||||
}
|
||||
|
||||
// ROM banking
|
||||
extern uint32_t g_t7_static_off; /* 0 Tama-Go, 0x4000 Friends */
|
||||
extern uint8_t g_t7_banks;
|
||||
void t7_set_rom_bank(uint8_t bank);
|
||||
bool t7_load_static_rom(void);
|
||||
|
||||
// CPU step (defined in t7_cpu.c)
|
||||
uint8_t t7_cpu_step(void);
|
||||
void t7_cpu_reset(void);
|
||||
void t7_cpu_nmi(void);
|
||||
void t7_cpu_irq(void);
|
||||
|
||||
// IRQ logic (defined in t7_memory.c since it touches cpureg)
|
||||
void t7_fire_irq(uint8_t i);
|
||||
void t7_fire_nmi(uint8_t i);
|
||||
|
||||
#endif // T7_INTERNAL_H
|
||||
/*
|
||||
* t7_internal.h - shared state structure for the emulator modules
|
||||
*
|
||||
* Not exposed to the Pebble app — only included by t7_*.c files.
|
||||
*
|
||||
* Performance note: hot-path state (CPU registers, work RAM, display RAM,
|
||||
* CPU register array, page tables) is exposed as separate global
|
||||
* variables rather than fields of a single g_sys struct. The reason is
|
||||
* that GCC treats `g_sys->foo` as a load through a pointer that could be
|
||||
* invalidated by any function call (because g_sys itself could in theory
|
||||
* be reassigned). With globals it can compute the address at compile time
|
||||
* and skip a level of indirection in the inner emulation loop. This
|
||||
* matters at ~750,000 memory accesses per emulated frame.
|
||||
*/
|
||||
|
||||
#ifndef T7_INTERNAL_H
|
||||
#define T7_INTERNAL_H
|
||||
|
||||
// ----- Hot-Path-Platzierung: heisser Emulator-Pfad ins IRAM (kein Flash-
|
||||
// Cache-Miss). Bei IRAM-Overflow im Linker -> #define T7_HOT leer.
|
||||
#ifndef T7_HOT
|
||||
# if defined(ESP_PLATFORM)
|
||||
# include "esp_attr.h"
|
||||
# define T7_HOT IRAM_ATTR
|
||||
# else
|
||||
# define T7_HOT
|
||||
# endif
|
||||
#endif
|
||||
|
||||
|
||||
#include "t7.h"
|
||||
|
||||
// 6502 registers (split out as a global, not behind a pointer).
|
||||
typedef struct {
|
||||
uint16_t pc;
|
||||
uint8_t a;
|
||||
uint8_t x;
|
||||
uint8_t y;
|
||||
uint8_t s;
|
||||
uint8_t p;
|
||||
} cpu6502_t;
|
||||
|
||||
// Small frequently-accessed state kept as globals (compile-time addresses).
|
||||
extern cpu6502_t g_cpu;
|
||||
extern uint8_t g_wram[T7_WRAM_SIZE];
|
||||
extern uint8_t g_dram[T7_DRAM_SIZE];
|
||||
extern uint8_t g_cpureg[T7_CPUREG_SIZE];
|
||||
|
||||
// Large buffers are heap-allocated to keep .bss under the Pebble's
|
||||
// 16-bit virtual-size limit (65535 bytes). They're only ever touched
|
||||
// through the page table after init, so a heap pointer costs the same
|
||||
// as a global array: page_table[p] already holds the absolute address
|
||||
// either way.
|
||||
extern uint8_t *g_rom_bank_buf;
|
||||
extern uint8_t *g_static_rom;
|
||||
|
||||
// IRQ vector table (16 entries, loaded once at init).
|
||||
extern uint16_t g_irq_vectors[16];
|
||||
|
||||
// Set non-zero by t7_fire_irq whenever it sets a bit in cpureg[$73:$74].
|
||||
// Cleared by t7_cpu_irq when it services the highest-priority pending
|
||||
// IRQ. The CPU step checks this single byte instead of reloading both
|
||||
// cpureg bytes on every instruction — at ~3 million steps/sec that saves
|
||||
// ~6 million memory loads/sec.
|
||||
extern uint8_t g_irq_pending_any;
|
||||
/* 1.6.9 (test12) battery, as TamaGo-Pebble v0.3.0: T7_IDLE jumps over the passes of the main loop's wait at $CE66
|
||||
* (bit-identical), T7_STOP halts the CPU when the firmware writes 7 to $3001 (until an IRQ or a key) */
|
||||
#define T7_IDLE_PC 0xCE66u
|
||||
extern uint8_t g_t7_idle_ok, g_t7_halted;
|
||||
#ifdef T7_JSR_HLE
|
||||
extern uint8_t g_t7_hle, g_t7_dark;
|
||||
#endif
|
||||
extern uint32_t g_t7_idle_cyc, g_t7_keyact;
|
||||
void t7_idle_check(void);
|
||||
|
||||
// Misc runtime state.
|
||||
extern uint8_t g_rom_bank_id;
|
||||
extern bool g_rom_loaded;
|
||||
extern uint8_t g_keys;
|
||||
extern ResHandle g_rom_resource;
|
||||
|
||||
// Whether the emulator is initialised. Used by t7_release to know
|
||||
// whether g_dram etc were ever populated.
|
||||
extern bool g_initialised;
|
||||
|
||||
// ----- Memory access (fast inline path) -----------------------------------
|
||||
//
|
||||
// Page table: 256 entries, one per 256-byte page of the 64K address space.
|
||||
// Each entry points at the backing RAM/ROM buffer for that page, or NULL
|
||||
// if the page needs slow-path I/O handling.
|
||||
extern uint8_t *t7_read_page[256];
|
||||
extern uint8_t *t7_write_page[256];
|
||||
|
||||
uint8_t t7_io_read(uint16_t addr);
|
||||
void t7_io_write(uint16_t addr, uint8_t value);
|
||||
|
||||
// ----- Profiling counters (referenced by the inline read/write below) -----
|
||||
//
|
||||
// T7_PROFILE is set in the public t7.h. When OFF (the production
|
||||
// default), PROFILE_INC compiles to nothing and the counter struct
|
||||
// isn't even instantiated.
|
||||
|
||||
#if T7_PROFILE
|
||||
typedef struct {
|
||||
uint32_t opcodes; // t7_cpu_step calls
|
||||
uint32_t reads_fast; // hit page-table (WRAM/DRAM/ROM)
|
||||
uint32_t reads_io; // missed → t7_io_read
|
||||
uint32_t writes_fast; // hit page-table
|
||||
uint32_t writes_io; // hit I/O area
|
||||
uint32_t writes_dropped; // hit ROM (silently ignored)
|
||||
uint32_t irqs; // t7_fire_irq calls
|
||||
uint32_t nmis; // t7_fire_nmi calls
|
||||
uint32_t irq_entries; // actual CPU IRQ entries (after mask check)
|
||||
uint32_t nmi_entries; // actual CPU NMI entries
|
||||
} t7_profile_t;
|
||||
|
||||
extern t7_profile_t g_t7_profile;
|
||||
# define PROFILE_INC(field) (g_t7_profile.field++)
|
||||
#else
|
||||
# define PROFILE_INC(field) ((void)0)
|
||||
#endif
|
||||
|
||||
#define T7_INLINE static inline __attribute__((always_inline))
|
||||
|
||||
T7_INLINE uint8_t t7_read(uint16_t addr)
|
||||
{
|
||||
uint8_t *page = t7_read_page[addr >> 8];
|
||||
if (page) { PROFILE_INC(reads_fast); return page[addr & 0xFF]; }
|
||||
PROFILE_INC(reads_io);
|
||||
return t7_io_read(addr);
|
||||
}
|
||||
|
||||
T7_INLINE uint16_t t7_read16(uint16_t addr)
|
||||
{
|
||||
uint8_t lo = t7_read(addr);
|
||||
uint8_t hi = t7_read((addr + 1) & 0xFFFF);
|
||||
return lo | (hi << 8);
|
||||
}
|
||||
|
||||
T7_INLINE void t7_write(uint16_t addr, uint8_t value)
|
||||
{
|
||||
uint8_t *page = t7_write_page[addr >> 8];
|
||||
if (page) { PROFILE_INC(writes_fast); page[addr & 0xFF] = value; return; }
|
||||
if ((addr >> 12) == 0x3) { PROFILE_INC(writes_io); t7_io_write(addr, value); return; }
|
||||
PROFILE_INC(writes_dropped);
|
||||
}
|
||||
|
||||
// ROM banking
|
||||
extern uint32_t g_t7_static_off; /* 0 Tama-Go, 0x4000 Friends */
|
||||
extern uint8_t g_t7_banks;
|
||||
void t7_set_rom_bank(uint8_t bank);
|
||||
bool t7_load_static_rom(void);
|
||||
|
||||
// CPU step (defined in t7_cpu.c)
|
||||
uint8_t t7_cpu_step(void);
|
||||
void t7_cpu_reset(void);
|
||||
void t7_cpu_nmi(void);
|
||||
void t7_cpu_irq(void);
|
||||
|
||||
// IRQ logic (defined in t7_memory.c since it touches cpureg)
|
||||
void t7_fire_irq(uint8_t i);
|
||||
void t7_fire_nmi(uint8_t i);
|
||||
|
||||
#endif // T7_INTERNAL_H
|
||||
+552
-552
File diff suppressed because it is too large.
Load diff
+24
-24
@@ -1,24 +1,24 @@
|
||||
/* t7_jit.h - 6502 to Thumb-2 translator (T7_JIT), see t7_jit.c. Author: mameise */
|
||||
#ifndef T7_JIT_H
|
||||
#define T7_JIT_H
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
/* the 6502 state as the run loop keeps it (in and out of a block) */
|
||||
typedef struct {
|
||||
uint32_t a, x, y, s, nzn, nzz, fc, fv, fi;
|
||||
uint32_t pc, cyc, io; /* out: next pc, cycles used, io = cycles of the I/O write that ended the block (0: none) */
|
||||
int32_t snd; /* in: the sound countdown at the start */
|
||||
uint8_t *wram;
|
||||
uint8_t *const *rp, *const *wp;
|
||||
uint32_t tmp;
|
||||
int32_t rem; /* in: budget - cycles (blocks follow each other in the code while they fit) */
|
||||
uint32_t son; /* sound on: the blocks keep clear of the next sound event */
|
||||
void *tbl;
|
||||
} T7J;
|
||||
typedef struct { uint32_t key, code, pre, all, cnt; } T7JEnt;
|
||||
extern uint32_t g_t7j_on, g_t7j_stat[4];
|
||||
void t7_jit_init(void *buf, uint32_t bytes);
|
||||
void t7_jit_flush(void);
|
||||
T7JEnt *t7_jit_get(uint32_t key, uint32_t pc, uint8_t *const *rp, uint32_t b3);
|
||||
void t7_jit_run(T7J *s, uint32_t code);
|
||||
#endif
|
||||
/* t7_jit.h - 6502 to Thumb-2 translator (T7_JIT), see t7_jit.c. Author: mameise */
|
||||
#ifndef T7_JIT_H
|
||||
#define T7_JIT_H
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
/* the 6502 state as the run loop keeps it (in and out of a block) */
|
||||
typedef struct {
|
||||
uint32_t a, x, y, s, nzn, nzz, fc, fv, fi;
|
||||
uint32_t pc, cyc, io; /* out: next pc, cycles used, io = cycles of the I/O write that ended the block (0: none) */
|
||||
int32_t snd; /* in: the sound countdown at the start */
|
||||
uint8_t *wram;
|
||||
uint8_t *const *rp, *const *wp;
|
||||
uint32_t tmp;
|
||||
int32_t rem; /* in: budget - cycles (blocks follow each other in the code while they fit) */
|
||||
uint32_t son; /* sound on: the blocks keep clear of the next sound event */
|
||||
void *tbl;
|
||||
} T7J;
|
||||
typedef struct { uint32_t key, code, pre, all, cnt; } T7JEnt;
|
||||
extern uint32_t g_t7j_on, g_t7j_stat[4];
|
||||
void t7_jit_init(void *buf, uint32_t bytes);
|
||||
void t7_jit_flush(void);
|
||||
T7JEnt *t7_jit_get(uint32_t key, uint32_t pc, uint8_t *const *rp, uint32_t b3);
|
||||
void t7_jit_run(T7J *s, uint32_t code);
|
||||
#endif
|
||||
+495
-495
@@ -1,495 +1,495 @@
|
||||
/*
|
||||
* t7_memory.c - memory map, ROM banking, and I/O registers
|
||||
*
|
||||
* Port of asterick/tamago. Highly optimized for the Pebble's ARM Cortex-M3.
|
||||
*
|
||||
* Memory map (16-bit address space):
|
||||
* $0000-$0FFF Work RAM (1.5 KB, mirrored across 4 KB)
|
||||
* $1000-$2FFF Display RAM (512 B, mirrored across 8 KB)
|
||||
* $3000-$3FFF I/O registers (256 B, mirrored 16x)
|
||||
* $4000-$BFFF Banked ROM (32 KB, 20 banks selectable via $3000)
|
||||
* $C000-$FFFF Static ROM (16 KB, always mapped from beginning of ROM)
|
||||
*
|
||||
* Performance: the inner emulation loop hits memory ~3-4 times per
|
||||
* instruction. To avoid expensive `addr % size` (modulo, which on M3
|
||||
* compiles to a software divide) and function-call overhead, we build a
|
||||
* page table at init: one pointer per 256-byte page of the 64K address
|
||||
* space, pointing at the backing RAM/ROM buffer. Reads and writes that
|
||||
* are pure RAM/ROM become a single array lookup + indirect load. Only
|
||||
* the 256 bytes of I/O space ($3000-$30FF) require the slow path with
|
||||
* register-specific handlers.
|
||||
*/
|
||||
|
||||
#include "t7_internal.h"
|
||||
#include "t7_eeprom.h"
|
||||
#include "t7_sound.h"
|
||||
|
||||
// We need the resource ID generated from package.json's "media" entry.
|
||||
// Defined in the generated header `src/resource_ids.auto.h` which is
|
||||
// included via pebble.h.
|
||||
#ifndef RESOURCE_ID_TAMA_ROM
|
||||
#define RESOURCE_ID_TAMA_ROM 0
|
||||
#endif
|
||||
|
||||
// ======================================================================
|
||||
// FIGUR / SPI TRACE (Etappe 1 des Figuren-Reverse-Engineering)
|
||||
//
|
||||
// Ziel: herausfinden, wie die Firmware mit einer gesteckten Gotchi-Figur
|
||||
// ueber SPI redet. Wir melden eine Figur als "gesteckt" (g_figure_id auf
|
||||
// PortA Bit 5+) und schneiden jeden Zugriff auf PortA ($3012) und die
|
||||
// SPI-Register ($30B0-$30BA) in einen Ringpuffer mit. Identische
|
||||
// aufeinanderfolgende Reads werden zusammengefasst (rep-Zaehler), damit
|
||||
// Status-Polling den Puffer nicht flutet.
|
||||
//
|
||||
// Steuerung per Serial (siehe main.cpp): fig / spiarm / spidump / spiret
|
||||
// ======================================================================
|
||||
uint8_t g_figure_id = 0; // 0 = keine Figur; 1..3 = Figur-Typ (live setzbar)
|
||||
uint8_t g_spi_ret = 0xFF; // was $30B0-$30BA-Reads zurueckgeben (damit Status-Poll durchlaeuft)
|
||||
|
||||
#ifndef SPI_TRACE_MAX
|
||||
#define SPI_TRACE_MAX 8192
|
||||
#endif
|
||||
typedef struct { uint8_t kind; uint8_t reg; uint8_t val; uint16_t rep; } spi_ev_t;
|
||||
static spi_ev_t spi_trace[SPI_TRACE_MAX];
|
||||
static int spi_trace_n = 0;
|
||||
static bool spi_trace_armed = false; // Diagnose-Trace standardmaessig AUS (per 'spiarm' scharf)
|
||||
static bool ir_trace_armed = false; // IR-Trace ($3070-$307F) per 'irarm' scharf
|
||||
// kind: 0=Reg-Read 1=Reg-Write 2=CSN-Wechsel(val=neuer Pegel)
|
||||
|
||||
static void spi_log(uint8_t kind, uint8_t reg, uint8_t val) {
|
||||
if (!spi_trace_armed && !ir_trace_armed) return;
|
||||
if (spi_trace_n > 0) { // gleiche Reads zusammenfassen
|
||||
spi_ev_t *e = &spi_trace[spi_trace_n - 1];
|
||||
if (e->kind == kind && e->reg == reg && e->val == val && e->rep < 0xFFFF) { e->rep++; return; }
|
||||
}
|
||||
if (spi_trace_n < SPI_TRACE_MAX) spi_trace[spi_trace_n++] = (spi_ev_t){ kind, reg, val, 1 };
|
||||
// voll -> einfrieren (nichts mehr aufzeichnen)
|
||||
}
|
||||
|
||||
void t7_figure_set(uint8_t id) { g_figure_id = id; }
|
||||
uint8_t t7_figure_get(void) { return g_figure_id; }
|
||||
void t7_spi_set_ret(uint8_t v) { g_spi_ret = v; }
|
||||
void t7_spitrace_arm(void) { spi_trace_n = 0; spi_trace_armed = true; }
|
||||
void t7_irtrace_arm(void) { spi_trace_n = 0; ir_trace_armed = true; spi_trace_armed = false; }
|
||||
int t7_spitrace_count(void) { return spi_trace_n; }
|
||||
void t7_spitrace_get(int i, uint8_t *kind, uint8_t *reg, uint8_t *val, uint16_t *rep) {
|
||||
if (i < 0 || i >= spi_trace_n) { *kind=*reg=*val=0; *rep=0; return; }
|
||||
*kind = spi_trace[i].kind; *reg = spi_trace[i].reg;
|
||||
*val = spi_trace[i].val; *rep = spi_trace[i].rep;
|
||||
}
|
||||
|
||||
// ======================================================================
|
||||
// SPI-FLASH-SLAVE (Gotchi-Figur)
|
||||
//
|
||||
// Aus dem ROM-Disassembly ($EA6E / $EB20) rekonstruiertes Protokoll:
|
||||
// - CS = PortB-Data ($3016) Bit 4, aktiv-LOW.
|
||||
// - Kommando ueber $30B3 (P_SPI_Write): 0x03 = Flash-Read, dann 3 Adressbytes
|
||||
// (hi, mid, lo) = $71,$72,$73.
|
||||
// - Danach liefern $30B6-Reads (P_SPI_Read) flash[addr], flash[addr+1], ...
|
||||
// - $30B7 (P_SPI_Status) Bit 2 (0x04) = "Byte bereit".
|
||||
// - Presence-Check der Firmware: $3012 (PortA) Bit 5/6.
|
||||
// Verifiziert: Detektor liest Adresse 0x000001 und prueft auf 'AA 55' -
|
||||
// makiko.bin[1..2] = AA 55.
|
||||
// ======================================================================
|
||||
extern const unsigned char* figure_rom_ptr(void);
|
||||
extern unsigned int figure_rom_length(void);
|
||||
|
||||
// Echtes Full-Duplex-Modell: pro CS-low-Transaktion laeuft eine fortlaufende
|
||||
// Transfer-Position. Bei einem Flash-Read (0x03) sind die ersten 4 Bytes
|
||||
// Kommando+Adresse -> Flash gibt waehrenddessen NICHTS aus; die echten Daten
|
||||
// kommen ab Position 4 (= flash[addr], flash[addr+1], ...). Die Firmware liest
|
||||
// das exakt so: erst 4 "Leerbytes" wegwerfen, dann die Nutzdaten.
|
||||
// - RX wird beim SENDEN (Write $30B3) erzeugt und in eine FIFO gelegt.
|
||||
// - Reads ($30B6) holen die Bytes aus der FIFO (das waehrend des Sendens
|
||||
// gleichzeitig empfangene Byte - so funktioniert SPI).
|
||||
// Ist kein 0x03 das erste Byte (z.B. Loopback-Selbsttest $CA2C), wird das
|
||||
// gesendete Byte zurueckgespiegelt (Echo).
|
||||
static uint8_t spi_cs = 1; // 1=deasserted(high), 0=asserted(low)
|
||||
static int spi_pos = 0; // Transfer-Position seit CS-low
|
||||
static uint8_t spi_cmd0 = 0; // erstes Byte = Kommando
|
||||
static uint8_t spi_addr_b[3];
|
||||
static uint32_t spi_addr = 0;
|
||||
static bool spi_is_read = false; // cmd0 == 0x03
|
||||
static uint8_t spi_rxfifo[256];
|
||||
static uint8_t spi_rxw = 0, spi_rxr = 0;
|
||||
static const unsigned char* spi_rom = 0; // pro Transaktion gecacht (spart Aufrufe pro Byte)
|
||||
static unsigned int spi_len = 0;
|
||||
|
||||
static void spi_cs_set(uint8_t level) // level: Bit4-Pegel von $3016
|
||||
{
|
||||
uint8_t cs = level ? 1 : 0;
|
||||
if (cs == spi_cs) return;
|
||||
spi_cs = cs;
|
||||
if (cs == 0) { // fallende Flanke -> neue Transaktion
|
||||
spi_pos = 0; spi_rxw = spi_rxr = 0; spi_is_read = false; spi_addr = 0;
|
||||
spi_len = figure_rom_length(); // aktive Figur einmalig cachen
|
||||
spi_rom = spi_len ? figure_rom_ptr() : 0;
|
||||
}
|
||||
}
|
||||
|
||||
static void spi_tx(uint8_t v) // Schreibzugriff auf $30B3 (= ein Transfer)
|
||||
{
|
||||
if (spi_cs) return; // nur bei aktivem CS
|
||||
int p = spi_pos;
|
||||
uint8_t rx;
|
||||
if (p == 0) { spi_cmd0 = v; spi_is_read = (v == 0x03); }
|
||||
if (p >= 1 && p <= 3) spi_addr_b[p - 1] = v;
|
||||
if (p == 3 && spi_is_read)
|
||||
spi_addr = ((uint32_t)spi_addr_b[0] << 16) | ((uint32_t)spi_addr_b[1] << 8) | spi_addr_b[2];
|
||||
|
||||
if (spi_is_read) {
|
||||
if (p < 4) rx = 0x00; // waehrend cmd+addr: Flash noch stumm
|
||||
else if (spi_len) { uint32_t a = spi_addr + (uint32_t)(p - 4); rx = (a < spi_len) ? spi_rom[a] : 0x00; }
|
||||
else rx = 0xFF; // Lite (keine ROM) -> Signatur scheitert
|
||||
} else {
|
||||
rx = v; // Loopback/Echo
|
||||
}
|
||||
spi_rxfifo[spi_rxw++] = rx; // 8-bit-Index wrappt automatisch (256)
|
||||
spi_pos++;
|
||||
}
|
||||
|
||||
static uint8_t spi_rx(void) // Lesezugriff auf $30B6
|
||||
{
|
||||
if (spi_rxr != spi_rxw) return spi_rxfifo[spi_rxr++];
|
||||
return 0xFF;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// ----- Page table ---------------------------------------------------------
|
||||
//
|
||||
// 65536 / 256 = 256 pages. Each page's pointer either points at the start
|
||||
// of a 256-byte RAM/ROM region OR is NULL to signal "needs slow-path I/O
|
||||
// handling" (only $3000-$30FF and its 15 mirrors).
|
||||
//
|
||||
// Global linkage so the inline accessors in t7_internal.h reach them.
|
||||
// Renamed to the public-API names that the header expects.
|
||||
uint8_t *t7_read_page[256];
|
||||
uint8_t *t7_write_page[256];
|
||||
|
||||
// Memory-mapped ROM (XIP-Flash). Wenn gesetzt, zeigt die Page-Table fuer den
|
||||
// gebankten Bereich direkt in den Flash -> Bank-Switch ohne 32-KB-Kopie.
|
||||
static const uint8_t *g_rom_mapped = NULL;
|
||||
// Debug: Zahl der echten Bank-Wechsel (fuer On-Screen-Diagnose).
|
||||
uint32_t t7_debug_bank_switches = 0;
|
||||
|
||||
// Sound: jeder Schreibzugriff auf $3050-$306F wird gezaehlt (Huellkurven-Trigger).
|
||||
volatile uint32_t t7_snd_write_seq = 0;
|
||||
volatile uint8_t t7_snd_last_addr = 0;
|
||||
volatile uint8_t t7_snd_last_val = 0;
|
||||
|
||||
uint32_t g_t7_static_off = 0;
|
||||
uint8_t g_t7_banks = 20;
|
||||
/* Tamagotchi Friends: the ROM file holds 16 KB more at the start. Bank 0 begins at 0x4000 (static
|
||||
* ROM, then its code linked to $8000), bank n >= 1 at (n + 1) * 32 KB. Tama-Go: bank n at n * 32 KB. */
|
||||
static inline size_t t7_bank_off(uint8_t b) { size_t o = (size_t)b * T7_ROM_BANK_SIZE; return g_t7_static_off ? (b ? o + T7_ROM_BANK_SIZE : 0x4000) : o; }
|
||||
|
||||
int t7_is_friends(void) { return g_t7_static_off != 0; }
|
||||
uint16_t t7_rtc_sec_addr(void) { return g_t7_static_off ? 0x0136 : 0x0396; } /* second of the game clock */
|
||||
|
||||
// Rebuild the entire page table. Called on init and on every ROM bank switch.
|
||||
static void rebuild_page_table(void)
|
||||
{
|
||||
// $0000-$0FFF — Work RAM (1.5 KB) mirrored.
|
||||
for (int p = 0; p < 16; p++) {
|
||||
int wram_page = p % 6;
|
||||
t7_read_page[p] = g_wram + wram_page * 0x100;
|
||||
t7_write_page[p] = g_wram + wram_page * 0x100;
|
||||
}
|
||||
// $1000-$2FFF — Display RAM (512 B) mirrored across 32 pages.
|
||||
for (int p = 0x10; p < 0x30; p++) {
|
||||
t7_read_page[p] = g_dram + (p & 1) * 0x100;
|
||||
t7_write_page[p] = g_dram + (p & 1) * 0x100;
|
||||
}
|
||||
// $3000-$3FFF — I/O registers. NULL = slow path.
|
||||
for (int p = 0x30; p < 0x40; p++) {
|
||||
t7_read_page[p] = NULL;
|
||||
t7_write_page[p] = NULL;
|
||||
}
|
||||
// $4000-$BFFF — Banked ROM (zero-copy aus dem gemappten Flash falls verfuegbar).
|
||||
{
|
||||
const uint8_t *bankbase = g_rom_mapped
|
||||
? (g_rom_mapped + t7_bank_off(g_rom_bank_id))
|
||||
: g_rom_bank_buf;
|
||||
for (int p = 0x40; p < 0xC0; p++) {
|
||||
t7_read_page[p] = (uint8_t *)(bankbase + (p - 0x40) * 0x100);
|
||||
t7_write_page[p] = NULL;
|
||||
}
|
||||
}
|
||||
// $C000-$FFFF — Static ROM.
|
||||
for (int p = 0xC0; p < 0x100; p++) {
|
||||
t7_read_page[p] = g_static_rom + (p - 0xC0) * 0x100;
|
||||
t7_write_page[p] = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
// ----- ROM banking --------------------------------------------------------
|
||||
|
||||
|
||||
bool t7_load_static_rom(void)
|
||||
{
|
||||
ResHandle h = resource_get_handle(RESOURCE_ID_TAMA_ROM);
|
||||
if (!h) {
|
||||
APP_LOG(APP_LOG_LEVEL_ERROR, "t7emu: ROM resource not found");
|
||||
return false;
|
||||
}
|
||||
size_t total = resource_size(h);
|
||||
if (total < T7_STATIC_ROM_SIZE) {
|
||||
APP_LOG(APP_LOG_LEVEL_ERROR,
|
||||
"t7emu: ROM too small (%u < %u)",
|
||||
(unsigned)total, (unsigned)T7_STATIC_ROM_SIZE);
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Tama-Go: static ROM at 0, 20 banks. Tamagotchi Friends: its reset vector is not at
|
||||
* 0x3FFC but at 0x7FFC, the static ROM is at 0x4000, 23 banks (see t7_bank_off). */
|
||||
g_t7_static_off = 0; g_t7_banks = 20;
|
||||
if (total >= 0x8000) {
|
||||
uint8_t v[4];
|
||||
resource_load_byte_range(h, 0x3FFC, v, 2); resource_load_byte_range(h, 0x7FFC, v + 2, 2);
|
||||
if (!(v[0] | v[1]) && (v[2] | v[3])) { g_t7_static_off = 0x4000; g_t7_banks = 23; }
|
||||
}
|
||||
size_t n = resource_load_byte_range(h, g_t7_static_off, g_static_rom,
|
||||
T7_STATIC_ROM_SIZE);
|
||||
if (n != T7_STATIC_ROM_SIZE) {
|
||||
APP_LOG(APP_LOG_LEVEL_ERROR,
|
||||
"t7emu: short read on static ROM (%u/%u)",
|
||||
(unsigned)n, (unsigned)T7_STATIC_ROM_SIZE);
|
||||
return false;
|
||||
}
|
||||
|
||||
APP_LOG(APP_LOG_LEVEL_INFO,
|
||||
"t7emu: static ROM loaded (%u bytes), total ROM size %u",
|
||||
(unsigned)n, (unsigned)total);
|
||||
|
||||
// Build IRQ vector table from $FFC0-$FFDF.
|
||||
for (int i = 0; i < 16; i++) {
|
||||
uint16_t lo = g_static_rom[0x3FC0 + i * 2];
|
||||
uint16_t hi = g_static_rom[0x3FC0 + i * 2 + 1];
|
||||
g_irq_vectors[i] = lo | (hi << 8);
|
||||
}
|
||||
|
||||
g_rom_loaded = true;
|
||||
g_rom_resource = h;
|
||||
|
||||
// Falls der ROM direkt im Flash adressierbar ist: zero-copy Banking.
|
||||
g_rom_mapped = t7_compat_rom_mapped();
|
||||
if (g_rom_mapped)
|
||||
APP_LOG(APP_LOG_LEVEL_INFO, "t7emu: ROM memory-mapped (zero-copy banking)");
|
||||
|
||||
// Build the page table now that all backing buffers exist.
|
||||
rebuild_page_table();
|
||||
return true;
|
||||
}
|
||||
|
||||
void T7_HOT t7_set_rom_bank(uint8_t bank)
|
||||
{
|
||||
uint8_t actual = bank % g_t7_banks;
|
||||
if (actual == g_rom_bank_id && g_rom_loaded) return;
|
||||
if (!g_rom_loaded) return;
|
||||
g_rom_bank_id = actual;
|
||||
t7_debug_bank_switches++;
|
||||
|
||||
if (g_rom_mapped) {
|
||||
// Zero-copy: Page-Table-Zeiger auf die neue Bank im Flash umsetzen.
|
||||
const uint8_t *bankbase = g_rom_mapped + t7_bank_off(actual);
|
||||
for (int p = 0x40; p < 0xC0; p++)
|
||||
t7_read_page[p] = (uint8_t *)(bankbase + (p - 0x40) * 0x100);
|
||||
return;
|
||||
}
|
||||
|
||||
// Fallback (Host): Bank in den RAM-Puffer kopieren.
|
||||
size_t offset = t7_bank_off(actual);
|
||||
size_t n = resource_load_byte_range(g_rom_resource, offset,
|
||||
g_rom_bank_buf, T7_ROM_BANK_SIZE);
|
||||
if (n != T7_ROM_BANK_SIZE)
|
||||
APP_LOG(APP_LOG_LEVEL_ERROR, "t7emu: bank %u short read (%u/%u)",
|
||||
actual, (unsigned)n, (unsigned)T7_ROM_BANK_SIZE);
|
||||
}
|
||||
|
||||
// ----- I/O register handlers ----------------------------------------------
|
||||
|
||||
static void io_write_bank(uint8_t reg, uint8_t value)
|
||||
{
|
||||
g_cpureg[reg] = value;
|
||||
t7_set_rom_bank(value);
|
||||
}
|
||||
|
||||
// $3073, $3074: IRQ pending flags. Writes acknowledge (clear) pending bits.
|
||||
static void io_write_int_flag(uint8_t reg, uint8_t value)
|
||||
{
|
||||
if (ir_trace_armed) spi_log(1, reg, value); // $3073/$3074 Int-Flags (IR-IRQ ack)
|
||||
g_cpureg[reg] &= ~value;
|
||||
// Recompute the fast-path cache flag.
|
||||
g_irq_pending_any = (g_cpureg[0x73] | g_cpureg[0x74]) != 0;
|
||||
}
|
||||
|
||||
static uint8_t s_last_csn = 0xFF;
|
||||
static void io_write_porta(uint8_t reg, uint8_t value)
|
||||
{
|
||||
g_cpureg[reg] = value;
|
||||
(void)s_last_csn; // PortA-Logging entfernt (war nur Rauschen + Serial-Flut)
|
||||
}
|
||||
|
||||
static uint8_t io_read_porta(uint8_t reg)
|
||||
{
|
||||
(void)reg;
|
||||
uint8_t mask = g_cpureg[0x11];
|
||||
uint8_t value = g_cpureg[0x12];
|
||||
uint8_t spi_power = mask & value & 0x10;
|
||||
uint8_t inserted_figure = g_figure_id;
|
||||
uint8_t input = g_keys | ((spi_power ? 0 : inserted_figure) << 5);
|
||||
return (mask & value) | (~mask & input);
|
||||
}
|
||||
|
||||
// $3015 (direction) and $3016 (data) writes both end up here. After
|
||||
// updating the register, we feed the new bus state to the EEPROM bit
|
||||
// banging engine. Matches write_portb_dir_data in registers.js exactly:
|
||||
//
|
||||
// d = ~mask | data (input bits read as 1 because of pull-ups)
|
||||
// eeprom.update(d & 4, d & 2, d & 1)
|
||||
//
|
||||
// Bit 2 = POWER, bit 1 = CLK, bit 0 = DATA.
|
||||
#ifdef T7_LINK
|
||||
void t7_ir_led_on(void);
|
||||
#endif
|
||||
static void io_write_portb(uint8_t reg, uint8_t value)
|
||||
{
|
||||
g_cpureg[reg] = value;
|
||||
if (reg == 0x16) spi_cs_set(value & 0x10); // $3016 Bit 4 = SPI-Figur-CS (aktiv-LOW)
|
||||
#ifdef T7_LINK
|
||||
if (reg == 0x16 && (value & 0x08)) t7_ir_led_on(); // 1.6.9: infrared LED on (packet for the other Paradise)
|
||||
#endif
|
||||
uint8_t mask = g_cpureg[0x15];
|
||||
uint8_t d = (uint8_t)(~mask) | g_cpureg[0x16];
|
||||
t7_eeprom_update(d & 4, d & 2, d & 1);
|
||||
}
|
||||
|
||||
// Port B read: bits configured as outputs return what we drove; bits
|
||||
// configured as inputs read from the EEPROM data line.
|
||||
//
|
||||
// (As in the JS reference, the EEPROM output bit is broadcast over all
|
||||
// input bits via the ~mask term. Only bit 0 is actually meaningful for
|
||||
// the EEPROM but the firmware only reads bit 0 anyway.)
|
||||
static uint8_t io_read_portb(uint8_t reg)
|
||||
{
|
||||
uint8_t mask = g_cpureg[0x15];
|
||||
uint8_t eeprom_out = t7_eeprom_output();
|
||||
uint8_t input_bits = eeprom_out ? (uint8_t)~mask : 0;
|
||||
return (uint8_t)((mask & g_cpureg[reg]) | input_bits);
|
||||
}
|
||||
|
||||
// Slow-path I/O read — called only when t7_read_page[page] is NULL.
|
||||
// Renamed from io_read to t7_io_read so the public inline in
|
||||
// t7_internal.h can call it.
|
||||
uint8_t T7_HOT t7_io_read(uint16_t addr)
|
||||
{
|
||||
uint8_t reg = addr & 0xFF;
|
||||
switch (reg) {
|
||||
#ifdef T7_PW_FIX
|
||||
// Nibble helper used only by the PC password code (bank 2, $6180-$6520):
|
||||
// the game writes a byte to $3080 and reads back its high nibble ($3082),
|
||||
// low nibble ($3083) or the nibble-swapped byte ($308A). Without this the
|
||||
// reads returned 0 and the login password was always 000000000000.
|
||||
case 0x82: return (uint8_t)(g_cpureg[0x80] >> 4);
|
||||
case 0x83: return (uint8_t)(g_cpureg[0x80] & 0x0F);
|
||||
case 0x8A: return (uint8_t)((g_cpureg[0x80] << 4) | (g_cpureg[0x80] >> 4));
|
||||
#endif
|
||||
case 0x12: return io_read_porta(reg);
|
||||
case 0x16: return io_read_portb(reg);
|
||||
default:
|
||||
if (reg >= 0xB0 && reg <= 0xBA) { // SPI-Registerbereich ($30B0-$30BA)
|
||||
uint8_t r;
|
||||
if (reg == 0xB6) r = spi_rx(); // P_SPI_Read -> Figur-ROM-Daten
|
||||
else if (reg == 0xB7) r = g_figure_id ? 0x04 : g_cpureg[reg]; // Status Bit2 = bereit
|
||||
else r = g_cpureg[reg];
|
||||
spi_log(0, reg, r); // jetzt auch B6 loggen (Diagnose)
|
||||
return r;
|
||||
}
|
||||
if (ir_trace_armed && reg >= 0x70 && reg <= 0x7F) spi_log(0, reg, g_cpureg[reg]); // IR-RX/Status
|
||||
return g_cpureg[reg];
|
||||
}
|
||||
}
|
||||
|
||||
// Slow-path I/O write — called only when t7_write_page[page] is NULL.
|
||||
void T7_HOT t7_io_write(uint16_t addr, uint8_t value)
|
||||
{
|
||||
uint8_t reg = addr & 0xFF;
|
||||
switch (reg) {
|
||||
case 0x00: io_write_bank(reg, value); break;
|
||||
#ifdef T7_STOP
|
||||
case 0x01: /* clock: 7 = CPU stops (STOP), with the original sound only (its timers wake) */
|
||||
g_cpureg[reg] = value;
|
||||
if (value == 0x07 && g_snd_on) {
|
||||
#ifdef T7_STOP_KEYACT
|
||||
if (g_t7_keyact) break; /* test14: keys in use (1 s after a change): no STOP. Asleep, the firmware missed the
|
||||
release of a key and took the next press as the same one (on the PC: 3 quick taps, 1 to 2 seen) */
|
||||
#endif
|
||||
if ((g_cpureg[0x07] & 0x01) && (g_keys & 0x07) != 0x07) g_cpureg[0x08] |= 0x01; /* a key is down already: awake at once */
|
||||
else g_t7_halted = 1;
|
||||
#ifdef T7_STOP_DBG
|
||||
{ extern void t7_stop_dbg(int); t7_stop_dbg(1); }
|
||||
#endif
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
case 0x11: io_write_porta(reg, value); break;
|
||||
case 0x12: io_write_porta(reg, value); break;
|
||||
case 0x15: io_write_portb(reg, value); break;
|
||||
case 0x16: io_write_portb(reg, value); break;
|
||||
case 0x73: io_write_int_flag(reg, value); break;
|
||||
case 0x74: io_write_int_flag(reg, value); break;
|
||||
default:
|
||||
if (reg >= 0xB0 && reg <= 0xBA) { // SPI-Registerbereich ($30B0-$30BA)
|
||||
spi_log(1, reg, value);
|
||||
if (reg == 0xB3) spi_tx(value); // P_SPI_Write -> Kommando/Adresse
|
||||
g_cpureg[reg] = value;
|
||||
break;
|
||||
}
|
||||
if (reg >= 0x50 && reg <= 0x6F) { // Sound-Registerbereich
|
||||
t7_snd_write_seq++;
|
||||
t7_snd_last_addr = reg;
|
||||
t7_snd_last_val = value;
|
||||
}
|
||||
if (ir_trace_armed && reg >= 0x70 && reg <= 0x7F) spi_log(1, reg, value); // IR-TX/Ctrl
|
||||
g_cpureg[reg] = value;
|
||||
// Originalton: Timer ($3031-$3035) und DAC ($3060/$3065) mitfuehren
|
||||
if (g_snd_on && ((reg >= 0x30 && reg <= 0x35) || reg == 0x60 || reg == 0x65))
|
||||
t7_snd_io_write(reg, value);
|
||||
#ifdef T7_SND_LAZY
|
||||
if (g_snd_on && (reg == 0x70 || reg == 0x71)) t7_snd_io_write(reg, value); /* IRQ mask: the timers' events again */
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Sound investigation note (kept here for reference):
|
||||
// $3060/$3062/$3064/$3065 are written in lockstep 4-tuples — strong
|
||||
// candidate for SPU channel control. Re-add an I/O write counter here
|
||||
// if/when sound work resumes. Removed to keep hot-path lean and the
|
||||
// APP_LOG quiet.
|
||||
|
||||
// Roh-Auslese eines I/O-Registers ($3000-Seite) fuers Sound-Experiment.
|
||||
uint8_t t7_debug_ioreg(uint8_t reg) { return g_cpureg[reg]; }
|
||||
uint32_t t7_debug_snd_seq(void) { return t7_snd_write_seq; }
|
||||
uint8_t t7_debug_snd_addr(void) { return t7_snd_last_addr; }
|
||||
uint8_t t7_debug_snd_val(void) { return t7_snd_last_val; }
|
||||
|
||||
// ----- IRQ / NMI dispatch helpers ----------------------------------------
|
||||
|
||||
void t7_fire_irq(uint8_t i)
|
||||
{
|
||||
PROFILE_INC(irqs);
|
||||
uint16_t mask = (g_cpureg[0x70] << 8) | g_cpureg[0x71];
|
||||
if (!((0x8000 >> i) & mask)) return;
|
||||
g_cpureg[0x73 + (i >> 3)] |= 0x80 >> (i & 7);
|
||||
// Set the fast-path cache flag so the CPU step sees it cheaply.
|
||||
g_irq_pending_any = 1;
|
||||
}
|
||||
|
||||
void t7_fire_nmi(uint8_t i)
|
||||
{
|
||||
PROFILE_INC(nmis);
|
||||
if (~g_cpureg[0x76] & (0x80 >> i)) return;
|
||||
t7_cpu_nmi();
|
||||
}
|
||||
/*
|
||||
* t7_memory.c - memory map, ROM banking, and I/O registers
|
||||
*
|
||||
* Port of asterick/tamago. Highly optimized for the Pebble's ARM Cortex-M3.
|
||||
*
|
||||
* Memory map (16-bit address space):
|
||||
* $0000-$0FFF Work RAM (1.5 KB, mirrored across 4 KB)
|
||||
* $1000-$2FFF Display RAM (512 B, mirrored across 8 KB)
|
||||
* $3000-$3FFF I/O registers (256 B, mirrored 16x)
|
||||
* $4000-$BFFF Banked ROM (32 KB, 20 banks selectable via $3000)
|
||||
* $C000-$FFFF Static ROM (16 KB, always mapped from beginning of ROM)
|
||||
*
|
||||
* Performance: the inner emulation loop hits memory ~3-4 times per
|
||||
* instruction. To avoid expensive `addr % size` (modulo, which on M3
|
||||
* compiles to a software divide) and function-call overhead, we build a
|
||||
* page table at init: one pointer per 256-byte page of the 64K address
|
||||
* space, pointing at the backing RAM/ROM buffer. Reads and writes that
|
||||
* are pure RAM/ROM become a single array lookup + indirect load. Only
|
||||
* the 256 bytes of I/O space ($3000-$30FF) require the slow path with
|
||||
* register-specific handlers.
|
||||
*/
|
||||
|
||||
#include "t7_internal.h"
|
||||
#include "t7_eeprom.h"
|
||||
#include "t7_sound.h"
|
||||
|
||||
// We need the resource ID generated from package.json's "media" entry.
|
||||
// Defined in the generated header `src/resource_ids.auto.h` which is
|
||||
// included via pebble.h.
|
||||
#ifndef RESOURCE_ID_TAMA_ROM
|
||||
#define RESOURCE_ID_TAMA_ROM 0
|
||||
#endif
|
||||
|
||||
// ======================================================================
|
||||
// FIGUR / SPI TRACE (Etappe 1 des Figuren-Reverse-Engineering)
|
||||
//
|
||||
// Ziel: herausfinden, wie die Firmware mit einer gesteckten Gotchi-Figur
|
||||
// ueber SPI redet. Wir melden eine Figur als "gesteckt" (g_figure_id auf
|
||||
// PortA Bit 5+) und schneiden jeden Zugriff auf PortA ($3012) und die
|
||||
// SPI-Register ($30B0-$30BA) in einen Ringpuffer mit. Identische
|
||||
// aufeinanderfolgende Reads werden zusammengefasst (rep-Zaehler), damit
|
||||
// Status-Polling den Puffer nicht flutet.
|
||||
//
|
||||
// Steuerung per Serial (siehe main.cpp): fig / spiarm / spidump / spiret
|
||||
// ======================================================================
|
||||
uint8_t g_figure_id = 0; // 0 = keine Figur; 1..3 = Figur-Typ (live setzbar)
|
||||
uint8_t g_spi_ret = 0xFF; // was $30B0-$30BA-Reads zurueckgeben (damit Status-Poll durchlaeuft)
|
||||
|
||||
#ifndef SPI_TRACE_MAX
|
||||
#define SPI_TRACE_MAX 8192
|
||||
#endif
|
||||
typedef struct { uint8_t kind; uint8_t reg; uint8_t val; uint16_t rep; } spi_ev_t;
|
||||
static spi_ev_t spi_trace[SPI_TRACE_MAX];
|
||||
static int spi_trace_n = 0;
|
||||
static bool spi_trace_armed = false; // Diagnose-Trace standardmaessig AUS (per 'spiarm' scharf)
|
||||
static bool ir_trace_armed = false; // IR-Trace ($3070-$307F) per 'irarm' scharf
|
||||
// kind: 0=Reg-Read 1=Reg-Write 2=CSN-Wechsel(val=neuer Pegel)
|
||||
|
||||
static void spi_log(uint8_t kind, uint8_t reg, uint8_t val) {
|
||||
if (!spi_trace_armed && !ir_trace_armed) return;
|
||||
if (spi_trace_n > 0) { // gleiche Reads zusammenfassen
|
||||
spi_ev_t *e = &spi_trace[spi_trace_n - 1];
|
||||
if (e->kind == kind && e->reg == reg && e->val == val && e->rep < 0xFFFF) { e->rep++; return; }
|
||||
}
|
||||
if (spi_trace_n < SPI_TRACE_MAX) spi_trace[spi_trace_n++] = (spi_ev_t){ kind, reg, val, 1 };
|
||||
// voll -> einfrieren (nichts mehr aufzeichnen)
|
||||
}
|
||||
|
||||
void t7_figure_set(uint8_t id) { g_figure_id = id; }
|
||||
uint8_t t7_figure_get(void) { return g_figure_id; }
|
||||
void t7_spi_set_ret(uint8_t v) { g_spi_ret = v; }
|
||||
void t7_spitrace_arm(void) { spi_trace_n = 0; spi_trace_armed = true; }
|
||||
void t7_irtrace_arm(void) { spi_trace_n = 0; ir_trace_armed = true; spi_trace_armed = false; }
|
||||
int t7_spitrace_count(void) { return spi_trace_n; }
|
||||
void t7_spitrace_get(int i, uint8_t *kind, uint8_t *reg, uint8_t *val, uint16_t *rep) {
|
||||
if (i < 0 || i >= spi_trace_n) { *kind=*reg=*val=0; *rep=0; return; }
|
||||
*kind = spi_trace[i].kind; *reg = spi_trace[i].reg;
|
||||
*val = spi_trace[i].val; *rep = spi_trace[i].rep;
|
||||
}
|
||||
|
||||
// ======================================================================
|
||||
// SPI-FLASH-SLAVE (Gotchi-Figur)
|
||||
//
|
||||
// Aus dem ROM-Disassembly ($EA6E / $EB20) rekonstruiertes Protokoll:
|
||||
// - CS = PortB-Data ($3016) Bit 4, aktiv-LOW.
|
||||
// - Kommando ueber $30B3 (P_SPI_Write): 0x03 = Flash-Read, dann 3 Adressbytes
|
||||
// (hi, mid, lo) = $71,$72,$73.
|
||||
// - Danach liefern $30B6-Reads (P_SPI_Read) flash[addr], flash[addr+1], ...
|
||||
// - $30B7 (P_SPI_Status) Bit 2 (0x04) = "Byte bereit".
|
||||
// - Presence-Check der Firmware: $3012 (PortA) Bit 5/6.
|
||||
// Verifiziert: Detektor liest Adresse 0x000001 und prueft auf 'AA 55' -
|
||||
// makiko.bin[1..2] = AA 55.
|
||||
// ======================================================================
|
||||
extern const unsigned char* figure_rom_ptr(void);
|
||||
extern unsigned int figure_rom_length(void);
|
||||
|
||||
// Echtes Full-Duplex-Modell: pro CS-low-Transaktion laeuft eine fortlaufende
|
||||
// Transfer-Position. Bei einem Flash-Read (0x03) sind die ersten 4 Bytes
|
||||
// Kommando+Adresse -> Flash gibt waehrenddessen NICHTS aus; die echten Daten
|
||||
// kommen ab Position 4 (= flash[addr], flash[addr+1], ...). Die Firmware liest
|
||||
// das exakt so: erst 4 "Leerbytes" wegwerfen, dann die Nutzdaten.
|
||||
// - RX wird beim SENDEN (Write $30B3) erzeugt und in eine FIFO gelegt.
|
||||
// - Reads ($30B6) holen die Bytes aus der FIFO (das waehrend des Sendens
|
||||
// gleichzeitig empfangene Byte - so funktioniert SPI).
|
||||
// Ist kein 0x03 das erste Byte (z.B. Loopback-Selbsttest $CA2C), wird das
|
||||
// gesendete Byte zurueckgespiegelt (Echo).
|
||||
static uint8_t spi_cs = 1; // 1=deasserted(high), 0=asserted(low)
|
||||
static int spi_pos = 0; // Transfer-Position seit CS-low
|
||||
static uint8_t spi_cmd0 = 0; // erstes Byte = Kommando
|
||||
static uint8_t spi_addr_b[3];
|
||||
static uint32_t spi_addr = 0;
|
||||
static bool spi_is_read = false; // cmd0 == 0x03
|
||||
static uint8_t spi_rxfifo[256];
|
||||
static uint8_t spi_rxw = 0, spi_rxr = 0;
|
||||
static const unsigned char* spi_rom = 0; // pro Transaktion gecacht (spart Aufrufe pro Byte)
|
||||
static unsigned int spi_len = 0;
|
||||
|
||||
static void spi_cs_set(uint8_t level) // level: Bit4-Pegel von $3016
|
||||
{
|
||||
uint8_t cs = level ? 1 : 0;
|
||||
if (cs == spi_cs) return;
|
||||
spi_cs = cs;
|
||||
if (cs == 0) { // fallende Flanke -> neue Transaktion
|
||||
spi_pos = 0; spi_rxw = spi_rxr = 0; spi_is_read = false; spi_addr = 0;
|
||||
spi_len = figure_rom_length(); // aktive Figur einmalig cachen
|
||||
spi_rom = spi_len ? figure_rom_ptr() : 0;
|
||||
}
|
||||
}
|
||||
|
||||
static void spi_tx(uint8_t v) // Schreibzugriff auf $30B3 (= ein Transfer)
|
||||
{
|
||||
if (spi_cs) return; // nur bei aktivem CS
|
||||
int p = spi_pos;
|
||||
uint8_t rx;
|
||||
if (p == 0) { spi_cmd0 = v; spi_is_read = (v == 0x03); }
|
||||
if (p >= 1 && p <= 3) spi_addr_b[p - 1] = v;
|
||||
if (p == 3 && spi_is_read)
|
||||
spi_addr = ((uint32_t)spi_addr_b[0] << 16) | ((uint32_t)spi_addr_b[1] << 8) | spi_addr_b[2];
|
||||
|
||||
if (spi_is_read) {
|
||||
if (p < 4) rx = 0x00; // waehrend cmd+addr: Flash noch stumm
|
||||
else if (spi_len) { uint32_t a = spi_addr + (uint32_t)(p - 4); rx = (a < spi_len) ? spi_rom[a] : 0x00; }
|
||||
else rx = 0xFF; // Lite (keine ROM) -> Signatur scheitert
|
||||
} else {
|
||||
rx = v; // Loopback/Echo
|
||||
}
|
||||
spi_rxfifo[spi_rxw++] = rx; // 8-bit-Index wrappt automatisch (256)
|
||||
spi_pos++;
|
||||
}
|
||||
|
||||
static uint8_t spi_rx(void) // Lesezugriff auf $30B6
|
||||
{
|
||||
if (spi_rxr != spi_rxw) return spi_rxfifo[spi_rxr++];
|
||||
return 0xFF;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// ----- Page table ---------------------------------------------------------
|
||||
//
|
||||
// 65536 / 256 = 256 pages. Each page's pointer either points at the start
|
||||
// of a 256-byte RAM/ROM region OR is NULL to signal "needs slow-path I/O
|
||||
// handling" (only $3000-$30FF and its 15 mirrors).
|
||||
//
|
||||
// Global linkage so the inline accessors in t7_internal.h reach them.
|
||||
// Renamed to the public-API names that the header expects.
|
||||
uint8_t *t7_read_page[256];
|
||||
uint8_t *t7_write_page[256];
|
||||
|
||||
// Memory-mapped ROM (XIP-Flash). Wenn gesetzt, zeigt die Page-Table fuer den
|
||||
// gebankten Bereich direkt in den Flash -> Bank-Switch ohne 32-KB-Kopie.
|
||||
static const uint8_t *g_rom_mapped = NULL;
|
||||
// Debug: Zahl der echten Bank-Wechsel (fuer On-Screen-Diagnose).
|
||||
uint32_t t7_debug_bank_switches = 0;
|
||||
|
||||
// Sound: jeder Schreibzugriff auf $3050-$306F wird gezaehlt (Huellkurven-Trigger).
|
||||
volatile uint32_t t7_snd_write_seq = 0;
|
||||
volatile uint8_t t7_snd_last_addr = 0;
|
||||
volatile uint8_t t7_snd_last_val = 0;
|
||||
|
||||
uint32_t g_t7_static_off = 0;
|
||||
uint8_t g_t7_banks = 20;
|
||||
/* Tamagotchi Friends: the ROM file holds 16 KB more at the start. Bank 0 begins at 0x4000 (static
|
||||
* ROM, then its code linked to $8000), bank n >= 1 at (n + 1) * 32 KB. Tama-Go: bank n at n * 32 KB. */
|
||||
static inline size_t t7_bank_off(uint8_t b) { size_t o = (size_t)b * T7_ROM_BANK_SIZE; return g_t7_static_off ? (b ? o + T7_ROM_BANK_SIZE : 0x4000) : o; }
|
||||
|
||||
int t7_is_friends(void) { return g_t7_static_off != 0; }
|
||||
uint16_t t7_rtc_sec_addr(void) { return g_t7_static_off ? 0x0136 : 0x0396; } /* second of the game clock */
|
||||
|
||||
// Rebuild the entire page table. Called on init and on every ROM bank switch.
|
||||
static void rebuild_page_table(void)
|
||||
{
|
||||
// $0000-$0FFF — Work RAM (1.5 KB) mirrored.
|
||||
for (int p = 0; p < 16; p++) {
|
||||
int wram_page = p % 6;
|
||||
t7_read_page[p] = g_wram + wram_page * 0x100;
|
||||
t7_write_page[p] = g_wram + wram_page * 0x100;
|
||||
}
|
||||
// $1000-$2FFF — Display RAM (512 B) mirrored across 32 pages.
|
||||
for (int p = 0x10; p < 0x30; p++) {
|
||||
t7_read_page[p] = g_dram + (p & 1) * 0x100;
|
||||
t7_write_page[p] = g_dram + (p & 1) * 0x100;
|
||||
}
|
||||
// $3000-$3FFF — I/O registers. NULL = slow path.
|
||||
for (int p = 0x30; p < 0x40; p++) {
|
||||
t7_read_page[p] = NULL;
|
||||
t7_write_page[p] = NULL;
|
||||
}
|
||||
// $4000-$BFFF — Banked ROM (zero-copy aus dem gemappten Flash falls verfuegbar).
|
||||
{
|
||||
const uint8_t *bankbase = g_rom_mapped
|
||||
? (g_rom_mapped + t7_bank_off(g_rom_bank_id))
|
||||
: g_rom_bank_buf;
|
||||
for (int p = 0x40; p < 0xC0; p++) {
|
||||
t7_read_page[p] = (uint8_t *)(bankbase + (p - 0x40) * 0x100);
|
||||
t7_write_page[p] = NULL;
|
||||
}
|
||||
}
|
||||
// $C000-$FFFF — Static ROM.
|
||||
for (int p = 0xC0; p < 0x100; p++) {
|
||||
t7_read_page[p] = g_static_rom + (p - 0xC0) * 0x100;
|
||||
t7_write_page[p] = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
// ----- ROM banking --------------------------------------------------------
|
||||
|
||||
|
||||
bool t7_load_static_rom(void)
|
||||
{
|
||||
ResHandle h = resource_get_handle(RESOURCE_ID_TAMA_ROM);
|
||||
if (!h) {
|
||||
APP_LOG(APP_LOG_LEVEL_ERROR, "t7emu: ROM resource not found");
|
||||
return false;
|
||||
}
|
||||
size_t total = resource_size(h);
|
||||
if (total < T7_STATIC_ROM_SIZE) {
|
||||
APP_LOG(APP_LOG_LEVEL_ERROR,
|
||||
"t7emu: ROM too small (%u < %u)",
|
||||
(unsigned)total, (unsigned)T7_STATIC_ROM_SIZE);
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Tama-Go: static ROM at 0, 20 banks. Tamagotchi Friends: its reset vector is not at
|
||||
* 0x3FFC but at 0x7FFC, the static ROM is at 0x4000, 23 banks (see t7_bank_off). */
|
||||
g_t7_static_off = 0; g_t7_banks = 20;
|
||||
if (total >= 0x8000) {
|
||||
uint8_t v[4];
|
||||
resource_load_byte_range(h, 0x3FFC, v, 2); resource_load_byte_range(h, 0x7FFC, v + 2, 2);
|
||||
if (!(v[0] | v[1]) && (v[2] | v[3])) { g_t7_static_off = 0x4000; g_t7_banks = 23; }
|
||||
}
|
||||
size_t n = resource_load_byte_range(h, g_t7_static_off, g_static_rom,
|
||||
T7_STATIC_ROM_SIZE);
|
||||
if (n != T7_STATIC_ROM_SIZE) {
|
||||
APP_LOG(APP_LOG_LEVEL_ERROR,
|
||||
"t7emu: short read on static ROM (%u/%u)",
|
||||
(unsigned)n, (unsigned)T7_STATIC_ROM_SIZE);
|
||||
return false;
|
||||
}
|
||||
|
||||
APP_LOG(APP_LOG_LEVEL_INFO,
|
||||
"t7emu: static ROM loaded (%u bytes), total ROM size %u",
|
||||
(unsigned)n, (unsigned)total);
|
||||
|
||||
// Build IRQ vector table from $FFC0-$FFDF.
|
||||
for (int i = 0; i < 16; i++) {
|
||||
uint16_t lo = g_static_rom[0x3FC0 + i * 2];
|
||||
uint16_t hi = g_static_rom[0x3FC0 + i * 2 + 1];
|
||||
g_irq_vectors[i] = lo | (hi << 8);
|
||||
}
|
||||
|
||||
g_rom_loaded = true;
|
||||
g_rom_resource = h;
|
||||
|
||||
// Falls der ROM direkt im Flash adressierbar ist: zero-copy Banking.
|
||||
g_rom_mapped = t7_compat_rom_mapped();
|
||||
if (g_rom_mapped)
|
||||
APP_LOG(APP_LOG_LEVEL_INFO, "t7emu: ROM memory-mapped (zero-copy banking)");
|
||||
|
||||
// Build the page table now that all backing buffers exist.
|
||||
rebuild_page_table();
|
||||
return true;
|
||||
}
|
||||
|
||||
void T7_HOT t7_set_rom_bank(uint8_t bank)
|
||||
{
|
||||
uint8_t actual = bank % g_t7_banks;
|
||||
if (actual == g_rom_bank_id && g_rom_loaded) return;
|
||||
if (!g_rom_loaded) return;
|
||||
g_rom_bank_id = actual;
|
||||
t7_debug_bank_switches++;
|
||||
|
||||
if (g_rom_mapped) {
|
||||
// Zero-copy: Page-Table-Zeiger auf die neue Bank im Flash umsetzen.
|
||||
const uint8_t *bankbase = g_rom_mapped + t7_bank_off(actual);
|
||||
for (int p = 0x40; p < 0xC0; p++)
|
||||
t7_read_page[p] = (uint8_t *)(bankbase + (p - 0x40) * 0x100);
|
||||
return;
|
||||
}
|
||||
|
||||
// Fallback (Host): Bank in den RAM-Puffer kopieren.
|
||||
size_t offset = t7_bank_off(actual);
|
||||
size_t n = resource_load_byte_range(g_rom_resource, offset,
|
||||
g_rom_bank_buf, T7_ROM_BANK_SIZE);
|
||||
if (n != T7_ROM_BANK_SIZE)
|
||||
APP_LOG(APP_LOG_LEVEL_ERROR, "t7emu: bank %u short read (%u/%u)",
|
||||
actual, (unsigned)n, (unsigned)T7_ROM_BANK_SIZE);
|
||||
}
|
||||
|
||||
// ----- I/O register handlers ----------------------------------------------
|
||||
|
||||
static void io_write_bank(uint8_t reg, uint8_t value)
|
||||
{
|
||||
g_cpureg[reg] = value;
|
||||
t7_set_rom_bank(value);
|
||||
}
|
||||
|
||||
// $3073, $3074: IRQ pending flags. Writes acknowledge (clear) pending bits.
|
||||
static void io_write_int_flag(uint8_t reg, uint8_t value)
|
||||
{
|
||||
if (ir_trace_armed) spi_log(1, reg, value); // $3073/$3074 Int-Flags (IR-IRQ ack)
|
||||
g_cpureg[reg] &= ~value;
|
||||
// Recompute the fast-path cache flag.
|
||||
g_irq_pending_any = (g_cpureg[0x73] | g_cpureg[0x74]) != 0;
|
||||
}
|
||||
|
||||
static uint8_t s_last_csn = 0xFF;
|
||||
static void io_write_porta(uint8_t reg, uint8_t value)
|
||||
{
|
||||
g_cpureg[reg] = value;
|
||||
(void)s_last_csn; // PortA-Logging entfernt (war nur Rauschen + Serial-Flut)
|
||||
}
|
||||
|
||||
static uint8_t io_read_porta(uint8_t reg)
|
||||
{
|
||||
(void)reg;
|
||||
uint8_t mask = g_cpureg[0x11];
|
||||
uint8_t value = g_cpureg[0x12];
|
||||
uint8_t spi_power = mask & value & 0x10;
|
||||
uint8_t inserted_figure = g_figure_id;
|
||||
uint8_t input = g_keys | ((spi_power ? 0 : inserted_figure) << 5);
|
||||
return (mask & value) | (~mask & input);
|
||||
}
|
||||
|
||||
// $3015 (direction) and $3016 (data) writes both end up here. After
|
||||
// updating the register, we feed the new bus state to the EEPROM bit
|
||||
// banging engine. Matches write_portb_dir_data in registers.js exactly:
|
||||
//
|
||||
// d = ~mask | data (input bits read as 1 because of pull-ups)
|
||||
// eeprom.update(d & 4, d & 2, d & 1)
|
||||
//
|
||||
// Bit 2 = POWER, bit 1 = CLK, bit 0 = DATA.
|
||||
#ifdef T7_LINK
|
||||
void t7_ir_led_on(void);
|
||||
#endif
|
||||
static void io_write_portb(uint8_t reg, uint8_t value)
|
||||
{
|
||||
g_cpureg[reg] = value;
|
||||
if (reg == 0x16) spi_cs_set(value & 0x10); // $3016 Bit 4 = SPI-Figur-CS (aktiv-LOW)
|
||||
#ifdef T7_LINK
|
||||
if (reg == 0x16 && (value & 0x08)) t7_ir_led_on(); // 1.6.9: infrared LED on (packet for the other Paradise)
|
||||
#endif
|
||||
uint8_t mask = g_cpureg[0x15];
|
||||
uint8_t d = (uint8_t)(~mask) | g_cpureg[0x16];
|
||||
t7_eeprom_update(d & 4, d & 2, d & 1);
|
||||
}
|
||||
|
||||
// Port B read: bits configured as outputs return what we drove; bits
|
||||
// configured as inputs read from the EEPROM data line.
|
||||
//
|
||||
// (As in the JS reference, the EEPROM output bit is broadcast over all
|
||||
// input bits via the ~mask term. Only bit 0 is actually meaningful for
|
||||
// the EEPROM but the firmware only reads bit 0 anyway.)
|
||||
static uint8_t io_read_portb(uint8_t reg)
|
||||
{
|
||||
uint8_t mask = g_cpureg[0x15];
|
||||
uint8_t eeprom_out = t7_eeprom_output();
|
||||
uint8_t input_bits = eeprom_out ? (uint8_t)~mask : 0;
|
||||
return (uint8_t)((mask & g_cpureg[reg]) | input_bits);
|
||||
}
|
||||
|
||||
// Slow-path I/O read — called only when t7_read_page[page] is NULL.
|
||||
// Renamed from io_read to t7_io_read so the public inline in
|
||||
// t7_internal.h can call it.
|
||||
uint8_t T7_HOT t7_io_read(uint16_t addr)
|
||||
{
|
||||
uint8_t reg = addr & 0xFF;
|
||||
switch (reg) {
|
||||
#ifdef T7_PW_FIX
|
||||
// Nibble helper used only by the PC password code (bank 2, $6180-$6520):
|
||||
// the game writes a byte to $3080 and reads back its high nibble ($3082),
|
||||
// low nibble ($3083) or the nibble-swapped byte ($308A). Without this the
|
||||
// reads returned 0 and the login password was always 000000000000.
|
||||
case 0x82: return (uint8_t)(g_cpureg[0x80] >> 4);
|
||||
case 0x83: return (uint8_t)(g_cpureg[0x80] & 0x0F);
|
||||
case 0x8A: return (uint8_t)((g_cpureg[0x80] << 4) | (g_cpureg[0x80] >> 4));
|
||||
#endif
|
||||
case 0x12: return io_read_porta(reg);
|
||||
case 0x16: return io_read_portb(reg);
|
||||
default:
|
||||
if (reg >= 0xB0 && reg <= 0xBA) { // SPI-Registerbereich ($30B0-$30BA)
|
||||
uint8_t r;
|
||||
if (reg == 0xB6) r = spi_rx(); // P_SPI_Read -> Figur-ROM-Daten
|
||||
else if (reg == 0xB7) r = g_figure_id ? 0x04 : g_cpureg[reg]; // Status Bit2 = bereit
|
||||
else r = g_cpureg[reg];
|
||||
spi_log(0, reg, r); // jetzt auch B6 loggen (Diagnose)
|
||||
return r;
|
||||
}
|
||||
if (ir_trace_armed && reg >= 0x70 && reg <= 0x7F) spi_log(0, reg, g_cpureg[reg]); // IR-RX/Status
|
||||
return g_cpureg[reg];
|
||||
}
|
||||
}
|
||||
|
||||
// Slow-path I/O write — called only when t7_write_page[page] is NULL.
|
||||
void T7_HOT t7_io_write(uint16_t addr, uint8_t value)
|
||||
{
|
||||
uint8_t reg = addr & 0xFF;
|
||||
switch (reg) {
|
||||
case 0x00: io_write_bank(reg, value); break;
|
||||
#ifdef T7_STOP
|
||||
case 0x01: /* clock: 7 = CPU stops (STOP), with the original sound only (its timers wake) */
|
||||
g_cpureg[reg] = value;
|
||||
if (value == 0x07 && g_snd_on) {
|
||||
#ifdef T7_STOP_KEYACT
|
||||
if (g_t7_keyact) break; /* test14: keys in use (1 s after a change): no STOP. Asleep, the firmware missed the
|
||||
release of a key and took the next press as the same one (on the PC: 3 quick taps, 1 to 2 seen) */
|
||||
#endif
|
||||
if ((g_cpureg[0x07] & 0x01) && (g_keys & 0x07) != 0x07) g_cpureg[0x08] |= 0x01; /* a key is down already: awake at once */
|
||||
else g_t7_halted = 1;
|
||||
#ifdef T7_STOP_DBG
|
||||
{ extern void t7_stop_dbg(int); t7_stop_dbg(1); }
|
||||
#endif
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
case 0x11: io_write_porta(reg, value); break;
|
||||
case 0x12: io_write_porta(reg, value); break;
|
||||
case 0x15: io_write_portb(reg, value); break;
|
||||
case 0x16: io_write_portb(reg, value); break;
|
||||
case 0x73: io_write_int_flag(reg, value); break;
|
||||
case 0x74: io_write_int_flag(reg, value); break;
|
||||
default:
|
||||
if (reg >= 0xB0 && reg <= 0xBA) { // SPI-Registerbereich ($30B0-$30BA)
|
||||
spi_log(1, reg, value);
|
||||
if (reg == 0xB3) spi_tx(value); // P_SPI_Write -> Kommando/Adresse
|
||||
g_cpureg[reg] = value;
|
||||
break;
|
||||
}
|
||||
if (reg >= 0x50 && reg <= 0x6F) { // Sound-Registerbereich
|
||||
t7_snd_write_seq++;
|
||||
t7_snd_last_addr = reg;
|
||||
t7_snd_last_val = value;
|
||||
}
|
||||
if (ir_trace_armed && reg >= 0x70 && reg <= 0x7F) spi_log(1, reg, value); // IR-TX/Ctrl
|
||||
g_cpureg[reg] = value;
|
||||
// Originalton: Timer ($3031-$3035) und DAC ($3060/$3065) mitfuehren
|
||||
if (g_snd_on && ((reg >= 0x30 && reg <= 0x35) || reg == 0x60 || reg == 0x65))
|
||||
t7_snd_io_write(reg, value);
|
||||
#ifdef T7_SND_LAZY
|
||||
if (g_snd_on && (reg == 0x70 || reg == 0x71)) t7_snd_io_write(reg, value); /* IRQ mask: the timers' events again */
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Sound investigation note (kept here for reference):
|
||||
// $3060/$3062/$3064/$3065 are written in lockstep 4-tuples — strong
|
||||
// candidate for SPU channel control. Re-add an I/O write counter here
|
||||
// if/when sound work resumes. Removed to keep hot-path lean and the
|
||||
// APP_LOG quiet.
|
||||
|
||||
// Roh-Auslese eines I/O-Registers ($3000-Seite) fuers Sound-Experiment.
|
||||
uint8_t t7_debug_ioreg(uint8_t reg) { return g_cpureg[reg]; }
|
||||
uint32_t t7_debug_snd_seq(void) { return t7_snd_write_seq; }
|
||||
uint8_t t7_debug_snd_addr(void) { return t7_snd_last_addr; }
|
||||
uint8_t t7_debug_snd_val(void) { return t7_snd_last_val; }
|
||||
|
||||
// ----- IRQ / NMI dispatch helpers ----------------------------------------
|
||||
|
||||
void t7_fire_irq(uint8_t i)
|
||||
{
|
||||
PROFILE_INC(irqs);
|
||||
uint16_t mask = (g_cpureg[0x70] << 8) | g_cpureg[0x71];
|
||||
if (!((0x8000 >> i) & mask)) return;
|
||||
g_cpureg[0x73 + (i >> 3)] |= 0x80 >> (i & 7);
|
||||
// Set the fast-path cache flag so the CPU step sees it cheaply.
|
||||
g_irq_pending_any = 1;
|
||||
}
|
||||
|
||||
void t7_fire_nmi(uint8_t i)
|
||||
{
|
||||
PROFILE_INC(nmis);
|
||||
if (~g_cpureg[0x76] & (0x80 >> i)) return;
|
||||
t7_cpu_nmi();
|
||||
}
|
||||
+311
-311
@@ -1,311 +1,311 @@
|
||||
/*
|
||||
* t7_run.inc - fast run loop for the Paradise (included at the end of t7_cpu.c with T7_FASTRUN).
|
||||
*
|
||||
* Same instructions as t7_cpu_step(), but the 6502 registers and flags live in local
|
||||
* variables for a whole run (the compiler keeps most of them in ARM registers) instead of
|
||||
* being loaded from and stored to memory for every instruction. The result is the same
|
||||
* instruction by instruction (checked on the PC against t7_cpu_step, see t7/host/runcmp.c).
|
||||
* Author: mameise
|
||||
*/
|
||||
#ifdef T7_FASTRUN
|
||||
#include "t7_sound.h"
|
||||
static void ir_hooks(void)
|
||||
{
|
||||
if (CPU.pc == 0x60C4) {
|
||||
s_ir_activity++;
|
||||
if (s_ir_rx_pending) {
|
||||
for (int i = 0; i < 32; i++) g_wram[0x200 + i] = s_ir_rx_pkt[i];
|
||||
g_wram[0x5A] = 0x20; g_wram[0x01B2] = 0x20;
|
||||
CPU.a = 0x80; s_nz_z = 0x80; s_flag_c = 0; CPU.pc = 0x60C7;
|
||||
s_ir_rx_pending = 0;
|
||||
}
|
||||
} else if (s_ir_bridge_en && CPU.pc == 0x7926) {
|
||||
s_ir_tx_pending = 1;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t T7_HOT t7_cpu_run(uint32_t budget)
|
||||
{
|
||||
uint32_t pc = CPU.pc, a = CPU.a, x = CPU.x, y = CPU.y, sp = CPU.s;
|
||||
uint32_t fc = s_flag_c ? 1u : 0u, fv = s_flag_v, fd = s_flag_d, fi = s_flag_i, nzn = s_nz_n, nzz = s_nz_z;
|
||||
uint8_t *const *rp = t7_read_page, *const *wp = t7_write_page;
|
||||
const int son = g_snd_on;
|
||||
int32_t snd = g_snd_next;
|
||||
uint32_t cyc = 0;
|
||||
#ifdef T7_JIT
|
||||
uint32_t lp = pc + 0x8000u; /* pc of the last interpreted instruction: blocks start only where a run of code starts */
|
||||
#endif
|
||||
uint32_t chk = 1, b3 = (g_rom_bank_id == 3 && !g_t7_static_off); /* look at IRQs / IR hooks only when something changed */
|
||||
|
||||
#define T7R_SAVE() do { CPU.pc = (uint16_t)pc; CPU.a = (uint8_t)a; CPU.x = (uint8_t)x; CPU.y = (uint8_t)y; CPU.s = (uint8_t)sp; \
|
||||
s_flag_c = (uint8_t)fc; s_flag_v = (uint8_t)fv; s_flag_d = (uint8_t)fd; s_flag_i = (uint8_t)fi; s_nz_n = (uint8_t)nzn; s_nz_z = (uint8_t)nzz; g_snd_next = snd; } while (0)
|
||||
#define T7R_LOAD() do { pc = CPU.pc; a = CPU.a; x = CPU.x; y = CPU.y; sp = CPU.s; fc = s_flag_c ? 1u : 0u; fv = s_flag_v; fd = s_flag_d; \
|
||||
fi = s_flag_i; nzn = s_nz_n; nzz = s_nz_z; snd = g_snd_next; } while (0)
|
||||
#define RD(ad) ({ uint32_t _ra = (ad); const uint8_t *_rp = rp[_ra >> 8]; _rp ? (uint32_t)_rp[_ra & 0xFF] : (uint32_t)t7_io_read((uint16_t)_ra); })
|
||||
#define WR(ad, v) do { uint32_t _wa = (ad); uint8_t *_wp = wp[_wa >> 8]; uint8_t _wv = (uint8_t)(v); \
|
||||
if (_wp) _wp[_wa & 0xFF] = _wv; else if ((_wa >> 12) == 3) { g_snd_next = snd; t7_io_write((uint16_t)_wa, _wv); snd = g_snd_next; chk = 1; b3 = (g_rom_bank_id == 3 && !g_t7_static_off); } } while (0)
|
||||
#define FETCH() ({ uint32_t _fv = RD(pc); pc = (pc + 1) & 0xFFFF; _fv; })
|
||||
#define FETCH16() ({ uint32_t _f16l = FETCH(); uint32_t _f16h = FETCH(); _f16l | (_f16h << 8); })
|
||||
#define PUSH(v) do { g_wram[0x100 | sp] = (uint8_t)(v); sp = (sp - 1) & 0xFF; } while (0) /* page 1 is always work RAM */
|
||||
#define PULL() ({ sp = (sp + 1) & 0xFF; (uint32_t)g_wram[0x100 | sp]; })
|
||||
#define NZ(v) do { nzn = nzz = (v); } while (0)
|
||||
#define PACKP() ((fc ? 0x01u : 0u) | (nzz == 0 ? 0x02u : 0u) | (fi ? 0x04u : 0u) | (fd ? 0x08u : 0u) | 0x20u | (fv ? 0x40u : 0u) | (nzn & 0x80u))
|
||||
#define UNPACKP(p) do { uint32_t _up = (p); fc = _up & 1u; fi = _up & 0x04u; fd = _up & 0x08u; fv = _up & 0x40u; nzn = (_up & 0x80u) ? 0x80u : 0u; nzz = (_up & 0x02u) ? 0u : 1u; } while (0)
|
||||
/* addressing */
|
||||
#define A_IMM() ({ uint32_t _im = pc; pc = (pc + 1) & 0xFFFF; _im; })
|
||||
#define A_ZP() FETCH()
|
||||
#define ZRD(z) ((uint32_t)g_wram[(z)]) /* page 0: always work RAM */
|
||||
#define ZWR(z, v) (g_wram[(z)] = (uint8_t)(v))
|
||||
#define A_ZPX() ((FETCH() + x) & 0xFF)
|
||||
#define A_ZPY() ((FETCH() + y) & 0xFF)
|
||||
#define A_ABS() FETCH16()
|
||||
#define A_ABX() ((FETCH16() + x) & 0xFFFF)
|
||||
#define A_ABY() ((FETCH16() + y) & 0xFFFF)
|
||||
#define A_INX() ({ uint32_t _xz = (FETCH() + x) & 0xFF; uint32_t _xl = ZRD(_xz); uint32_t _xh = ZRD((_xz + 1) & 0xFF); _xl | (_xh << 8); })
|
||||
#define A_INY() ({ uint32_t _yz = FETCH(); uint32_t _yl = ZRD(_yz); uint32_t _yh = ZRD((_yz + 1) & 0xFF); ((_yl | (_yh << 8)) + y) & 0xFFFF; })
|
||||
#define A_IND() ({ uint32_t _ip = FETCH16(); uint32_t _il = RD(_ip); uint32_t _ih = RD((_ip & 0xFF00) | ((_ip + 1) & 0xFF)); _il | (_ih << 8); })
|
||||
/* operations */
|
||||
#define LDA(m) do { a = RD(m); NZ(a); } while (0)
|
||||
#define LDX(m) do { x = RD(m); NZ(x); } while (0)
|
||||
#define LDY(m) do { y = RD(m); NZ(y); } while (0)
|
||||
#define AND(m) do { a &= RD(m); NZ(a); } while (0)
|
||||
#define ORA(m) do { a |= RD(m); NZ(a); } while (0)
|
||||
#define EOR(m) do { a ^= RD(m); NZ(a); } while (0)
|
||||
#define CMPR(r, m) do { uint32_t _d = RD(m); fc = (r) >= _d; NZ(((r) - _d) & 0xFF); } while (0)
|
||||
#define INCM(m) do { uint32_t _a = (m); uint32_t _v = (RD(_a) + 1) & 0xFF; NZ(_v); WR(_a, _v); } while (0)
|
||||
#define DECM(m) do { uint32_t _a = (m); uint32_t _v = (RD(_a) - 1) & 0xFF; NZ(_v); WR(_a, _v); } while (0)
|
||||
#define ASLM(m) do { uint32_t _a = (m); uint32_t _d = RD(_a); uint32_t _o = (_d << 1) & 0xFF; fc = _d >> 7; NZ(_o); WR(_a, _o); } while (0)
|
||||
#define LSRM(m) do { uint32_t _a = (m); uint32_t _d = RD(_a); uint32_t _o = _d >> 1; fc = _d & 1u; NZ(_o); WR(_a, _o); } while (0)
|
||||
#define ROLM(m) do { uint32_t _a = (m); uint32_t _d = RD(_a); uint32_t _o = ((_d << 1) & 0xFF) | fc; fc = _d >> 7; NZ(_o); WR(_a, _o); } while (0)
|
||||
#define RORM(m) do { uint32_t _a = (m); uint32_t _d = RD(_a); uint32_t _o = (_d >> 1) | (fc << 7); fc = _d & 1u; NZ(_o); WR(_a, _o); } while (0)
|
||||
#define BIT(m) do { uint32_t _v = RD(m); fv = _v & 0x40u; nzn = _v; nzz = a & _v; } while (0)
|
||||
#define ADC(m) do { uint32_t _d = RD(m); uint32_t _o = a + _d + fc; fv = (~(a ^ _d) & (a ^ _o)) & 0x80u; NZ(_o & 0xFF); \
|
||||
if (fd) { int _al = (int)(a & 0x0F) + (int)(_d & 0x0F) + (int)fc; int _ah = (int)(a & 0xF0) + (int)(_d & 0xF0) + ((_al >= 0x10) ? 0x10 : 0); \
|
||||
if (_al > 0x09) _al += 0x06; if (_ah > 0x90) _ah += 0x60; _o = (uint32_t)((_al & 0x0F) + _ah); } \
|
||||
fc = _o > 0xFF; a = _o & 0xFF; } while (0)
|
||||
#define SBC(m) do { uint32_t _d = RD(m); int _o = (int)a - (int)_d - (fc ? 0 : 1); fv = ((a ^ _d) & (a ^ (uint32_t)_o)) & 0x80u; NZ((uint32_t)_o & 0xFF); \
|
||||
fc = _o >= 0; \
|
||||
if (fd) { int _al = (int)(a & 0x0F) - (int)(_d & 0x0F) - (fc ? 0 : 1); int _ah = (int)(a & 0xF0) - (int)(_d & 0xF0) - ((_al < 0) ? 0x10 : 0); \
|
||||
if (_al < 0) _al -= 0x06; if (_ah < 0) _ah -= 0x60; _o = (_al & 0x0F) + _ah; } \
|
||||
a = (uint32_t)_o & 0xFF; } while (0)
|
||||
#define BR(cond) do { int32_t _o = (int8_t)FETCH(); if (cond) pc = (pc + (uint32_t)_o) & 0xFFFF; } while (0)
|
||||
|
||||
while (cyc < budget) {
|
||||
if (b3 && (pc == 0x60C4 || pc == 0x7926)) { T7R_SAVE(); ir_hooks(); T7R_LOAD(); }
|
||||
if (chk) {
|
||||
#ifdef T7_STOP
|
||||
/* test12: halted (STOP): the cycles up to the end or the next sound event pass without instructions; any
|
||||
* IRQ request wakes the CPU (taken only when the I flag allows, as on the chip) */
|
||||
if (g_t7_halted) {
|
||||
if (g_cpureg[0x73] | g_cpureg[0x74]) g_t7_halted = 0;
|
||||
else {
|
||||
uint32_t lim = budget - cyc;
|
||||
if (son) { if (snd <= 0) lim = 1; else if ((uint32_t)snd < lim) lim = (uint32_t)snd; }
|
||||
cyc += lim; g_t7_idle_cyc += lim;
|
||||
if (son && (snd -= (int32_t)lim) <= 0) { g_snd_next = snd; t7_snd_service(); snd = g_snd_next; }
|
||||
continue; /* chk stays set */
|
||||
}
|
||||
}
|
||||
#endif
|
||||
if (g_irq_pending_any && !fi) {
|
||||
uint32_t pending = ((uint32_t)g_cpureg[0x73] << 8) | g_cpureg[0x74];
|
||||
if (pending) {
|
||||
PROFILE_INC(irq_entries);
|
||||
int idx = __builtin_clz(pending << 16);
|
||||
PUSH(pc >> 8); PUSH(pc & 0xFF); PUSH(PACKP());
|
||||
pc = g_irq_vectors[idx]; fi = 1;
|
||||
#ifdef T7_BLIT_CACHE
|
||||
if (g_bm.on) { if (g_bm.irq++ == 0) g_bm.irq0 = (int32_t)cyc; } /* its cycles up to the RTI do not count */
|
||||
#endif
|
||||
g_irq_pending_any = (g_cpureg[0x73] | g_cpureg[0x74]) != 0;
|
||||
}
|
||||
}
|
||||
#ifdef T7_IDLE
|
||||
/* test12: the BEQ went back to $CE66 (chk bit 1) and no IRQ came: the passes that fit up to the end or the
|
||||
* next sound event at once, the same state as interpreting them (t7_idle_check, TamaGo-Pebble) */
|
||||
if ((chk & 2u) && pc == T7_IDLE_PC && !fd && g_wram[0x2D] == 0) {
|
||||
uint32_t lim = budget - cyc;
|
||||
if (son) { if (snd <= 0) lim = 0; else if ((uint32_t)snd < lim) lim = (uint32_t)snd; }
|
||||
uint32_t pass = t7_idle_pass_cyc(), n = lim / pass;
|
||||
if (n) {
|
||||
static const uint8_t k[5] = { 0x07, 0x03, 0x0D, 0x17, 0x13 };
|
||||
uint32_t prev = 0;
|
||||
for (int i = 0; i < 5; i++) { uint32_t v = (g_wram[0xC4 + i] + (n - 1u) * k[i]) & 0xFF; prev = v; g_wram[0xC4 + i] = (uint8_t)(v + k[i]); }
|
||||
uint32_t o = prev + 0x13u;
|
||||
fv = (~(prev ^ 0x13u) & (prev ^ o)) & 0x80u; fc = o > 0xFFu;
|
||||
g_wram[0x100 | sp] = 0xCE; g_wram[0x100 | ((sp - 1) & 0xFF)] = 0x74;
|
||||
a = 0; NZ(0);
|
||||
uint32_t s = n * pass;
|
||||
cyc += s; g_t7_idle_cyc += s;
|
||||
if (son && (snd -= (int32_t)s) <= 0) { g_snd_next = snd; t7_snd_service(); snd = g_snd_next; }
|
||||
chk = 1; continue;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
chk = 0;
|
||||
#ifdef T7_BLIT_CACHE
|
||||
/* the time of a left out blit passes here, in the caller's place; an IRQ is taken in between as above */
|
||||
if (g_bs.rem && pc == g_bs.pc && sp == g_bs.sp) {
|
||||
uint32_t lim = budget - cyc; if (lim > g_bs.rem) lim = g_bs.rem;
|
||||
if (son) { if (snd <= 0) lim = 0; else if ((uint32_t)snd < lim) lim = (uint32_t)snd; }
|
||||
cyc += lim; g_bs.rem -= lim;
|
||||
if (son && (snd -= (int32_t)lim) <= 0) { g_snd_next = snd; t7_snd_service(); snd = g_snd_next; }
|
||||
chk = 1; continue;
|
||||
}
|
||||
#endif
|
||||
#ifdef T7_JIT
|
||||
/* 1.6.12 test2: translated ROM code, when the interpreter would run all of the block now (t7_jit.c) */
|
||||
if (g_t7j_on && !fd && pc >= 0x4000u) {
|
||||
T7JEnt *je = t7_jit_get((uint32_t)(uintptr_t)(rp[pc >> 8] + (pc & 0xFFu)), pc, rp, b3 | (((pc - lp - 1u) & 0xFFFFu) > 2u ? 2u : 0u));
|
||||
if (je && cyc + je->pre < budget && (!son || snd > (int32_t)je->all)) {
|
||||
T7J js;
|
||||
js.a = a; js.x = x; js.y = y; js.s = sp; js.nzn = nzn; js.nzz = nzz; js.fc = fc; js.fv = fv; js.fi = fi;
|
||||
js.snd = snd; js.wram = g_wram; js.rp = rp; js.wp = wp; js.cyc = 0; js.rem = (int32_t)(budget - cyc); js.son = (uint32_t)son;
|
||||
t7_jit_run(&js, je->code);
|
||||
a = js.a; x = js.x; y = js.y; sp = js.s; nzn = js.nzn; nzz = js.nzz; fc = js.fc; fv = js.fv; fi = js.fi; pc = js.pc;
|
||||
cyc += js.cyc;
|
||||
if (js.io) { /* ended after an I/O write: as WR, then the IRQ check */
|
||||
snd = g_snd_next; chk = 1; b3 = (g_rom_bank_id == 3 && !g_t7_static_off);
|
||||
if (son && (snd -= (int32_t)js.io) <= 0) { g_snd_next = snd; t7_snd_service(); snd = g_snd_next; }
|
||||
} else snd = js.snd;
|
||||
lp = pc + 0x8000u;
|
||||
continue;
|
||||
}
|
||||
lp = pc;
|
||||
}
|
||||
#endif
|
||||
#ifdef T7_COUNT
|
||||
{ extern volatile uint32_t g_t7_ninstr; g_t7_ninstr++; }
|
||||
#endif
|
||||
uint32_t op = FETCH();
|
||||
uint32_t c = opcode_cycles[op];
|
||||
switch (op) {
|
||||
case 0xA9: LDA(A_IMM()); break; case 0xA5: a = ZRD(FETCH()); NZ(a); break; case 0xB5: a = ZRD(A_ZPX()); NZ(a); break; case 0xAD: LDA(A_ABS()); break;
|
||||
case 0xBD: LDA(A_ABX()); break; case 0xB9: LDA(A_ABY()); break; case 0xA1: LDA(A_INX()); break; case 0xB1: LDA(A_INY()); break;
|
||||
case 0xA2: LDX(A_IMM()); break; case 0xA6: x = ZRD(FETCH()); NZ(x); break; case 0xB6: LDX(A_ZPY()); break; case 0xAE: LDX(A_ABS()); break; case 0xBE: LDX(A_ABY()); break;
|
||||
case 0xA0: LDY(A_IMM()); break; case 0xA4: y = ZRD(FETCH()); NZ(y); break; case 0xB4: LDY(A_ZPX()); break; case 0xAC: LDY(A_ABS()); break; case 0xBC: LDY(A_ABX()); break;
|
||||
case 0x85: ZWR(FETCH(), a); break; case 0x95: ZWR(A_ZPX(), a); break; case 0x8D: WR(A_ABS(), a); break; case 0x9D: WR(A_ABX(), a); break;
|
||||
case 0x99: WR(A_ABY(), a); break; case 0x81: WR(A_INX(), a); break; case 0x91: WR(A_INY(), a); break;
|
||||
case 0x86: ZWR(FETCH(), x); break; case 0x96: WR(A_ZPY(), x); break; case 0x8E: WR(A_ABS(), x); break;
|
||||
case 0x84: ZWR(FETCH(), y); break; case 0x94: WR(A_ZPX(), y); break; case 0x8C: WR(A_ABS(), y); break;
|
||||
case 0xAA: x = a; NZ(x); break; case 0xA8: y = a; NZ(y); break; case 0x8A: a = x; NZ(a); break; case 0x98: a = y; NZ(a); break;
|
||||
case 0xBA: x = sp; NZ(x); break; case 0x9A: sp = x; break;
|
||||
case 0xE8: x = (x + 1) & 0xFF; NZ(x); break; case 0xC8: y = (y + 1) & 0xFF; NZ(y); break;
|
||||
case 0xCA: x = (x - 1) & 0xFF; NZ(x); break; case 0x88: y = (y - 1) & 0xFF; NZ(y); break;
|
||||
case 0xE6: { uint32_t _z = FETCH(); uint32_t _v = (ZRD(_z) + 1) & 0xFF; NZ(_v); ZWR(_z, _v); } break; case 0xF6: INCM(A_ZPX()); break; case 0xEE: INCM(A_ABS()); break; case 0xFE: INCM(A_ABX()); break;
|
||||
case 0xC6: { uint32_t _z = FETCH(); uint32_t _v = (ZRD(_z) - 1) & 0xFF; NZ(_v); ZWR(_z, _v); } break; case 0xD6: DECM(A_ZPX()); break; case 0xCE: DECM(A_ABS()); break; case 0xDE: DECM(A_ABX()); break;
|
||||
case 0x29: AND(A_IMM()); break; case 0x25: a &= ZRD(FETCH()); NZ(a); break; case 0x35: AND(A_ZPX()); break; case 0x2D: AND(A_ABS()); break;
|
||||
case 0x3D: AND(A_ABX()); break; case 0x39: AND(A_ABY()); break; case 0x21: AND(A_INX()); break; case 0x31: AND(A_INY()); break;
|
||||
case 0x09: ORA(A_IMM()); break; case 0x05: a |= ZRD(FETCH()); NZ(a); break; case 0x15: ORA(A_ZPX()); break; case 0x0D: ORA(A_ABS()); break;
|
||||
case 0x1D: ORA(A_ABX()); break; case 0x19: ORA(A_ABY()); break; case 0x01: ORA(A_INX()); break; case 0x11: ORA(A_INY()); break;
|
||||
case 0x49: EOR(A_IMM()); break; case 0x45: EOR(A_ZP()); break; case 0x55: EOR(A_ZPX()); break; case 0x4D: EOR(A_ABS()); break;
|
||||
case 0x5D: EOR(A_ABX()); break; case 0x59: EOR(A_ABY()); break; case 0x41: EOR(A_INX()); break; case 0x51: EOR(A_INY()); break;
|
||||
case 0x24: BIT(A_ZP()); break; case 0x2C: BIT(A_ABS()); break;
|
||||
case 0x0A: fc = a >> 7; a = (a << 1) & 0xFF; NZ(a); break;
|
||||
case 0x06: ASLM(A_ZP()); break; case 0x16: ASLM(A_ZPX()); break; case 0x0E: ASLM(A_ABS()); break; case 0x1E: ASLM(A_ABX()); break;
|
||||
case 0x4A: fc = a & 1u; a >>= 1; NZ(a); break;
|
||||
case 0x46: LSRM(A_ZP()); break; case 0x56: LSRM(A_ZPX()); break; case 0x4E: LSRM(A_ABS()); break; case 0x5E: LSRM(A_ABX()); break;
|
||||
case 0x2A: { uint32_t _o = ((a << 1) & 0xFF) | fc; fc = a >> 7; a = _o; NZ(a); } break;
|
||||
case 0x26: ROLM(A_ZP()); break; case 0x36: ROLM(A_ZPX()); break; case 0x2E: ROLM(A_ABS()); break; case 0x3E: ROLM(A_ABX()); break;
|
||||
case 0x6A: { uint32_t _o = (a >> 1) | (fc << 7); fc = a & 1u; a = _o; NZ(a); } break;
|
||||
case 0x66: RORM(A_ZP()); break; case 0x76: RORM(A_ZPX()); break; case 0x6E: RORM(A_ABS()); break; case 0x7E: RORM(A_ABX()); break;
|
||||
case 0x69: ADC(A_IMM()); break; case 0x65: ADC(A_ZP()); break; case 0x75: ADC(A_ZPX()); break; case 0x6D: ADC(A_ABS()); break;
|
||||
case 0x7D: ADC(A_ABX()); break; case 0x79: ADC(A_ABY()); break; case 0x61: ADC(A_INX()); break; case 0x71: ADC(A_INY()); break;
|
||||
case 0xE9: SBC(A_IMM()); break; case 0xE5: SBC(A_ZP()); break; case 0xF5: SBC(A_ZPX()); break; case 0xED: SBC(A_ABS()); break;
|
||||
case 0xFD: SBC(A_ABX()); break; case 0xF9: SBC(A_ABY()); break; case 0xE1: SBC(A_INX()); break; case 0xF1: SBC(A_INY()); break;
|
||||
case 0xC9: CMPR(a, A_IMM()); break; case 0xC5: { uint32_t _d = ZRD(FETCH()); fc = a >= _d; NZ((a - _d) & 0xFF); } break; case 0xD5: CMPR(a, A_ZPX()); break; case 0xCD: CMPR(a, A_ABS()); break;
|
||||
case 0xDD: CMPR(a, A_ABX()); break; case 0xD9: CMPR(a, A_ABY()); break; case 0xC1: CMPR(a, A_INX()); break; case 0xD1: CMPR(a, A_INY()); break;
|
||||
case 0xE0: CMPR(x, A_IMM()); break; case 0xE4: CMPR(x, A_ZP()); break; case 0xEC: CMPR(x, A_ABS()); break;
|
||||
case 0xC0: CMPR(y, A_IMM()); break; case 0xC4: CMPR(y, A_ZP()); break; case 0xCC: CMPR(y, A_ABS()); break;
|
||||
case 0x18: fc = 0; break; case 0x38: fc = 1; break; case 0x58: fi = 0; chk = 1; break; case 0x78: fi = 1; break;
|
||||
case 0xB8: fv = 0; break; case 0xD8: fd = 0; break; case 0xF8: fd = 1; break;
|
||||
case 0x48: PUSH(a); break;
|
||||
case 0x68: a = PULL(); NZ(a); break;
|
||||
case 0x08: PUSH(PACKP() | 0x10u); break;
|
||||
case 0x28: UNPACKP(PULL()); chk = 1; break;
|
||||
case 0x4C: pc = A_ABS(); break;
|
||||
case 0x6C: pc = A_IND(); break;
|
||||
#ifdef T7_JSR_HLE
|
||||
case 0x20: { uint32_t _t = A_ABS(); uint32_t _r = (pc - 1) & 0xFFFF;
|
||||
if (g_t7_hle && (_t == 0xD460u || _t == 0xE4CDu)) {
|
||||
#ifdef T7_BLIT_CACHE
|
||||
if (_t == 0xE4CDu && (g_t7_hle & 2u) && g_t7_dark) { /* blit, screen off: left out when its time is known */
|
||||
uint8_t _k[12]; uint32_t _s = bc_key(_k);
|
||||
if (g_bc[_s].k[11] && bc_same(g_bc[_s].k, _k)) {
|
||||
g_wram[0x100 | sp] = (uint8_t)(_r >> 8); g_wram[0x100 | ((sp - 1) & 0xFF)] = (uint8_t)_r;
|
||||
g_bs.rem = g_bc[_s].cyc; g_bs.pc = pc; g_bs.sp = sp; g_bc_hit++; break; /* pc is the return address */
|
||||
} else if (!g_bm.on) { /* measure this one */
|
||||
g_bm.on = 1; g_bm.sp = sp; g_bm.ret = (_r + 1) & 0xFFFF; g_bm.start = (int32_t)(cyc + c); g_bm.irq = 0; g_bm.irqc = 0; g_bm.slot = _s; bc_copy(g_bm.k, _k); g_bc_miss++;
|
||||
#ifdef T7_JIT
|
||||
g_bm.jit = g_t7j_on; g_t7j_on = 0;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#else
|
||||
if (_t == 0xE4CDu && (g_t7_hle & 2u) && g_t7_dark) { /* blit, screen off: JSR + RTS only */
|
||||
g_wram[0x100 | sp] = (uint8_t)(_r >> 8); g_wram[0x100 | ((sp - 1) & 0xFF)] = (uint8_t)_r;
|
||||
c += opcode_cycles[0x60]; break;
|
||||
}
|
||||
#endif
|
||||
#if defined(T7_WAKE_FRESH) || defined(T7_DARK_PEEK)
|
||||
if (_t == 0xE4CDu) g_t7_blits++; /* a blit that runs (the screen after a wake: drawn anew) */
|
||||
#endif
|
||||
if (_t == 0xD460u && (g_t7_hle & 1u)) { /* delay: whole when its RTS runs in this run */
|
||||
uint32_t pre = c + opcode_cycles[0xA2] + 47u * (opcode_cycles[0xE8] + opcode_cycles[0xD0]), all = pre + opcode_cycles[0x60];
|
||||
if (cyc + pre < budget && (!son || snd > (int32_t)all)) {
|
||||
g_wram[0x100 | sp] = (uint8_t)(_r >> 8); g_wram[0x100 | ((sp - 1) & 0xFF)] = (uint8_t)_r;
|
||||
x = 0; nzn = 0; nzz = 0; c = all; break;
|
||||
}
|
||||
}
|
||||
}
|
||||
PUSH(_r >> 8); PUSH(_r & 0xFF); pc = _t; } break;
|
||||
#else
|
||||
case 0x20: { uint32_t _t = A_ABS(); uint32_t _r = (pc - 1) & 0xFFFF; PUSH(_r >> 8); PUSH(_r & 0xFF); pc = _t; } break;
|
||||
#endif
|
||||
case 0x60: { uint32_t _l = PULL(); uint32_t _h = PULL(); pc = ((_l | (_h << 8)) + 1) & 0xFFFF;
|
||||
#ifdef T7_BLIT_CACHE
|
||||
if (g_bm.on && sp == g_bm.sp && pc == g_bm.ret) { /* the measured blit ends: note its cycles */
|
||||
T7BC *_e = &g_bc[g_bm.slot]; bc_copy(_e->k, g_bm.k); _e->cyc = (uint32_t)((int32_t)(cyc + c) - g_bm.start - g_bm.irqc); g_bm.on = 0;
|
||||
#ifdef T7_BC_LOG
|
||||
{ extern void t7_bc_log(const uint8_t *, uint32_t); t7_bc_log(g_bm.k, _e->cyc); }
|
||||
#endif
|
||||
#ifdef T7_JIT
|
||||
g_t7j_on = g_bm.jit;
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
} break;
|
||||
case 0x40: { UNPACKP(PULL()); uint32_t _l = PULL(); uint32_t _h = PULL(); pc = _l | (_h << 8); } chk = 1;
|
||||
#ifdef T7_BLIT_CACHE
|
||||
if (g_bm.on && g_bm.irq && --g_bm.irq == 0) g_bm.irqc += (int32_t)(cyc + c) - g_bm.irq0;
|
||||
#endif
|
||||
break;
|
||||
case 0x00: pc = (pc + 1) & 0xFFFF; PUSH(pc >> 8); PUSH(pc & 0xFF); PUSH(PACKP() | 0x10u);
|
||||
{ uint32_t _l = RD(0xFFFE); uint32_t _h = RD(0xFFFF); pc = _l | (_h << 8); } fi = 1; break;
|
||||
case 0x10: BR(!(nzn & 0x80u)); break; case 0x30: BR(nzn & 0x80u); break;
|
||||
case 0x50: BR(!fv); break; case 0x70: BR(fv); break;
|
||||
case 0x90: BR(!fc); break; case 0xB0: BR(fc); break;
|
||||
case 0xD0: BR(nzz != 0); break;
|
||||
#ifdef T7_IDLE
|
||||
case 0xF0: BR(nzz == 0); if (pc == T7_IDLE_PC && g_t7_idle_ok) chk |= 2u; break;
|
||||
#else
|
||||
case 0xF0: BR(nzz == 0); break;
|
||||
#endif
|
||||
case 0xEA: break;
|
||||
default: c = 2; break;
|
||||
}
|
||||
cyc += c;
|
||||
if (son && (snd -= (int32_t)c) <= 0) { g_snd_next = snd; t7_snd_service(); snd = g_snd_next; chk = 1; }
|
||||
}
|
||||
#ifdef T7_BLIT_CACHE
|
||||
if (g_bm.on) { g_bm.start -= (int32_t)cyc; if (g_bm.irq) g_bm.irq0 -= (int32_t)cyc; } /* goes on in the next run */
|
||||
#endif
|
||||
T7R_SAVE();
|
||||
return cyc;
|
||||
}
|
||||
#undef T7R_SAVE
|
||||
#undef T7R_LOAD
|
||||
#undef RD
|
||||
#undef WR
|
||||
#endif
|
||||
/*
|
||||
* t7_run.inc - fast run loop for the Paradise (included at the end of t7_cpu.c with T7_FASTRUN).
|
||||
*
|
||||
* Same instructions as t7_cpu_step(), but the 6502 registers and flags live in local
|
||||
* variables for a whole run (the compiler keeps most of them in ARM registers) instead of
|
||||
* being loaded from and stored to memory for every instruction. The result is the same
|
||||
* instruction by instruction (checked on the PC against t7_cpu_step, see t7/host/runcmp.c).
|
||||
* Author: mameise
|
||||
*/
|
||||
#ifdef T7_FASTRUN
|
||||
#include "t7_sound.h"
|
||||
static void ir_hooks(void)
|
||||
{
|
||||
if (CPU.pc == 0x60C4) {
|
||||
s_ir_activity++;
|
||||
if (s_ir_rx_pending) {
|
||||
for (int i = 0; i < 32; i++) g_wram[0x200 + i] = s_ir_rx_pkt[i];
|
||||
g_wram[0x5A] = 0x20; g_wram[0x01B2] = 0x20;
|
||||
CPU.a = 0x80; s_nz_z = 0x80; s_flag_c = 0; CPU.pc = 0x60C7;
|
||||
s_ir_rx_pending = 0;
|
||||
}
|
||||
} else if (s_ir_bridge_en && CPU.pc == 0x7926) {
|
||||
s_ir_tx_pending = 1;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t T7_HOT t7_cpu_run(uint32_t budget)
|
||||
{
|
||||
uint32_t pc = CPU.pc, a = CPU.a, x = CPU.x, y = CPU.y, sp = CPU.s;
|
||||
uint32_t fc = s_flag_c ? 1u : 0u, fv = s_flag_v, fd = s_flag_d, fi = s_flag_i, nzn = s_nz_n, nzz = s_nz_z;
|
||||
uint8_t *const *rp = t7_read_page, *const *wp = t7_write_page;
|
||||
const int son = g_snd_on;
|
||||
int32_t snd = g_snd_next;
|
||||
uint32_t cyc = 0;
|
||||
#ifdef T7_JIT
|
||||
uint32_t lp = pc + 0x8000u; /* pc of the last interpreted instruction: blocks start only where a run of code starts */
|
||||
#endif
|
||||
uint32_t chk = 1, b3 = (g_rom_bank_id == 3 && !g_t7_static_off); /* look at IRQs / IR hooks only when something changed */
|
||||
|
||||
#define T7R_SAVE() do { CPU.pc = (uint16_t)pc; CPU.a = (uint8_t)a; CPU.x = (uint8_t)x; CPU.y = (uint8_t)y; CPU.s = (uint8_t)sp; \
|
||||
s_flag_c = (uint8_t)fc; s_flag_v = (uint8_t)fv; s_flag_d = (uint8_t)fd; s_flag_i = (uint8_t)fi; s_nz_n = (uint8_t)nzn; s_nz_z = (uint8_t)nzz; g_snd_next = snd; } while (0)
|
||||
#define T7R_LOAD() do { pc = CPU.pc; a = CPU.a; x = CPU.x; y = CPU.y; sp = CPU.s; fc = s_flag_c ? 1u : 0u; fv = s_flag_v; fd = s_flag_d; \
|
||||
fi = s_flag_i; nzn = s_nz_n; nzz = s_nz_z; snd = g_snd_next; } while (0)
|
||||
#define RD(ad) ({ uint32_t _ra = (ad); const uint8_t *_rp = rp[_ra >> 8]; _rp ? (uint32_t)_rp[_ra & 0xFF] : (uint32_t)t7_io_read((uint16_t)_ra); })
|
||||
#define WR(ad, v) do { uint32_t _wa = (ad); uint8_t *_wp = wp[_wa >> 8]; uint8_t _wv = (uint8_t)(v); \
|
||||
if (_wp) _wp[_wa & 0xFF] = _wv; else if ((_wa >> 12) == 3) { g_snd_next = snd; t7_io_write((uint16_t)_wa, _wv); snd = g_snd_next; chk = 1; b3 = (g_rom_bank_id == 3 && !g_t7_static_off); } } while (0)
|
||||
#define FETCH() ({ uint32_t _fv = RD(pc); pc = (pc + 1) & 0xFFFF; _fv; })
|
||||
#define FETCH16() ({ uint32_t _f16l = FETCH(); uint32_t _f16h = FETCH(); _f16l | (_f16h << 8); })
|
||||
#define PUSH(v) do { g_wram[0x100 | sp] = (uint8_t)(v); sp = (sp - 1) & 0xFF; } while (0) /* page 1 is always work RAM */
|
||||
#define PULL() ({ sp = (sp + 1) & 0xFF; (uint32_t)g_wram[0x100 | sp]; })
|
||||
#define NZ(v) do { nzn = nzz = (v); } while (0)
|
||||
#define PACKP() ((fc ? 0x01u : 0u) | (nzz == 0 ? 0x02u : 0u) | (fi ? 0x04u : 0u) | (fd ? 0x08u : 0u) | 0x20u | (fv ? 0x40u : 0u) | (nzn & 0x80u))
|
||||
#define UNPACKP(p) do { uint32_t _up = (p); fc = _up & 1u; fi = _up & 0x04u; fd = _up & 0x08u; fv = _up & 0x40u; nzn = (_up & 0x80u) ? 0x80u : 0u; nzz = (_up & 0x02u) ? 0u : 1u; } while (0)
|
||||
/* addressing */
|
||||
#define A_IMM() ({ uint32_t _im = pc; pc = (pc + 1) & 0xFFFF; _im; })
|
||||
#define A_ZP() FETCH()
|
||||
#define ZRD(z) ((uint32_t)g_wram[(z)]) /* page 0: always work RAM */
|
||||
#define ZWR(z, v) (g_wram[(z)] = (uint8_t)(v))
|
||||
#define A_ZPX() ((FETCH() + x) & 0xFF)
|
||||
#define A_ZPY() ((FETCH() + y) & 0xFF)
|
||||
#define A_ABS() FETCH16()
|
||||
#define A_ABX() ((FETCH16() + x) & 0xFFFF)
|
||||
#define A_ABY() ((FETCH16() + y) & 0xFFFF)
|
||||
#define A_INX() ({ uint32_t _xz = (FETCH() + x) & 0xFF; uint32_t _xl = ZRD(_xz); uint32_t _xh = ZRD((_xz + 1) & 0xFF); _xl | (_xh << 8); })
|
||||
#define A_INY() ({ uint32_t _yz = FETCH(); uint32_t _yl = ZRD(_yz); uint32_t _yh = ZRD((_yz + 1) & 0xFF); ((_yl | (_yh << 8)) + y) & 0xFFFF; })
|
||||
#define A_IND() ({ uint32_t _ip = FETCH16(); uint32_t _il = RD(_ip); uint32_t _ih = RD((_ip & 0xFF00) | ((_ip + 1) & 0xFF)); _il | (_ih << 8); })
|
||||
/* operations */
|
||||
#define LDA(m) do { a = RD(m); NZ(a); } while (0)
|
||||
#define LDX(m) do { x = RD(m); NZ(x); } while (0)
|
||||
#define LDY(m) do { y = RD(m); NZ(y); } while (0)
|
||||
#define AND(m) do { a &= RD(m); NZ(a); } while (0)
|
||||
#define ORA(m) do { a |= RD(m); NZ(a); } while (0)
|
||||
#define EOR(m) do { a ^= RD(m); NZ(a); } while (0)
|
||||
#define CMPR(r, m) do { uint32_t _d = RD(m); fc = (r) >= _d; NZ(((r) - _d) & 0xFF); } while (0)
|
||||
#define INCM(m) do { uint32_t _a = (m); uint32_t _v = (RD(_a) + 1) & 0xFF; NZ(_v); WR(_a, _v); } while (0)
|
||||
#define DECM(m) do { uint32_t _a = (m); uint32_t _v = (RD(_a) - 1) & 0xFF; NZ(_v); WR(_a, _v); } while (0)
|
||||
#define ASLM(m) do { uint32_t _a = (m); uint32_t _d = RD(_a); uint32_t _o = (_d << 1) & 0xFF; fc = _d >> 7; NZ(_o); WR(_a, _o); } while (0)
|
||||
#define LSRM(m) do { uint32_t _a = (m); uint32_t _d = RD(_a); uint32_t _o = _d >> 1; fc = _d & 1u; NZ(_o); WR(_a, _o); } while (0)
|
||||
#define ROLM(m) do { uint32_t _a = (m); uint32_t _d = RD(_a); uint32_t _o = ((_d << 1) & 0xFF) | fc; fc = _d >> 7; NZ(_o); WR(_a, _o); } while (0)
|
||||
#define RORM(m) do { uint32_t _a = (m); uint32_t _d = RD(_a); uint32_t _o = (_d >> 1) | (fc << 7); fc = _d & 1u; NZ(_o); WR(_a, _o); } while (0)
|
||||
#define BIT(m) do { uint32_t _v = RD(m); fv = _v & 0x40u; nzn = _v; nzz = a & _v; } while (0)
|
||||
#define ADC(m) do { uint32_t _d = RD(m); uint32_t _o = a + _d + fc; fv = (~(a ^ _d) & (a ^ _o)) & 0x80u; NZ(_o & 0xFF); \
|
||||
if (fd) { int _al = (int)(a & 0x0F) + (int)(_d & 0x0F) + (int)fc; int _ah = (int)(a & 0xF0) + (int)(_d & 0xF0) + ((_al >= 0x10) ? 0x10 : 0); \
|
||||
if (_al > 0x09) _al += 0x06; if (_ah > 0x90) _ah += 0x60; _o = (uint32_t)((_al & 0x0F) + _ah); } \
|
||||
fc = _o > 0xFF; a = _o & 0xFF; } while (0)
|
||||
#define SBC(m) do { uint32_t _d = RD(m); int _o = (int)a - (int)_d - (fc ? 0 : 1); fv = ((a ^ _d) & (a ^ (uint32_t)_o)) & 0x80u; NZ((uint32_t)_o & 0xFF); \
|
||||
fc = _o >= 0; \
|
||||
if (fd) { int _al = (int)(a & 0x0F) - (int)(_d & 0x0F) - (fc ? 0 : 1); int _ah = (int)(a & 0xF0) - (int)(_d & 0xF0) - ((_al < 0) ? 0x10 : 0); \
|
||||
if (_al < 0) _al -= 0x06; if (_ah < 0) _ah -= 0x60; _o = (_al & 0x0F) + _ah; } \
|
||||
a = (uint32_t)_o & 0xFF; } while (0)
|
||||
#define BR(cond) do { int32_t _o = (int8_t)FETCH(); if (cond) pc = (pc + (uint32_t)_o) & 0xFFFF; } while (0)
|
||||
|
||||
while (cyc < budget) {
|
||||
if (b3 && (pc == 0x60C4 || pc == 0x7926)) { T7R_SAVE(); ir_hooks(); T7R_LOAD(); }
|
||||
if (chk) {
|
||||
#ifdef T7_STOP
|
||||
/* test12: halted (STOP): the cycles up to the end or the next sound event pass without instructions; any
|
||||
* IRQ request wakes the CPU (taken only when the I flag allows, as on the chip) */
|
||||
if (g_t7_halted) {
|
||||
if (g_cpureg[0x73] | g_cpureg[0x74]) g_t7_halted = 0;
|
||||
else {
|
||||
uint32_t lim = budget - cyc;
|
||||
if (son) { if (snd <= 0) lim = 1; else if ((uint32_t)snd < lim) lim = (uint32_t)snd; }
|
||||
cyc += lim; g_t7_idle_cyc += lim;
|
||||
if (son && (snd -= (int32_t)lim) <= 0) { g_snd_next = snd; t7_snd_service(); snd = g_snd_next; }
|
||||
continue; /* chk stays set */
|
||||
}
|
||||
}
|
||||
#endif
|
||||
if (g_irq_pending_any && !fi) {
|
||||
uint32_t pending = ((uint32_t)g_cpureg[0x73] << 8) | g_cpureg[0x74];
|
||||
if (pending) {
|
||||
PROFILE_INC(irq_entries);
|
||||
int idx = __builtin_clz(pending << 16);
|
||||
PUSH(pc >> 8); PUSH(pc & 0xFF); PUSH(PACKP());
|
||||
pc = g_irq_vectors[idx]; fi = 1;
|
||||
#ifdef T7_BLIT_CACHE
|
||||
if (g_bm.on) { if (g_bm.irq++ == 0) g_bm.irq0 = (int32_t)cyc; } /* its cycles up to the RTI do not count */
|
||||
#endif
|
||||
g_irq_pending_any = (g_cpureg[0x73] | g_cpureg[0x74]) != 0;
|
||||
}
|
||||
}
|
||||
#ifdef T7_IDLE
|
||||
/* test12: the BEQ went back to $CE66 (chk bit 1) and no IRQ came: the passes that fit up to the end or the
|
||||
* next sound event at once, the same state as interpreting them (t7_idle_check, TamaGo-Pebble) */
|
||||
if ((chk & 2u) && pc == T7_IDLE_PC && !fd && g_wram[0x2D] == 0) {
|
||||
uint32_t lim = budget - cyc;
|
||||
if (son) { if (snd <= 0) lim = 0; else if ((uint32_t)snd < lim) lim = (uint32_t)snd; }
|
||||
uint32_t pass = t7_idle_pass_cyc(), n = lim / pass;
|
||||
if (n) {
|
||||
static const uint8_t k[5] = { 0x07, 0x03, 0x0D, 0x17, 0x13 };
|
||||
uint32_t prev = 0;
|
||||
for (int i = 0; i < 5; i++) { uint32_t v = (g_wram[0xC4 + i] + (n - 1u) * k[i]) & 0xFF; prev = v; g_wram[0xC4 + i] = (uint8_t)(v + k[i]); }
|
||||
uint32_t o = prev + 0x13u;
|
||||
fv = (~(prev ^ 0x13u) & (prev ^ o)) & 0x80u; fc = o > 0xFFu;
|
||||
g_wram[0x100 | sp] = 0xCE; g_wram[0x100 | ((sp - 1) & 0xFF)] = 0x74;
|
||||
a = 0; NZ(0);
|
||||
uint32_t s = n * pass;
|
||||
cyc += s; g_t7_idle_cyc += s;
|
||||
if (son && (snd -= (int32_t)s) <= 0) { g_snd_next = snd; t7_snd_service(); snd = g_snd_next; }
|
||||
chk = 1; continue;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
chk = 0;
|
||||
#ifdef T7_BLIT_CACHE
|
||||
/* the time of a left out blit passes here, in the caller's place; an IRQ is taken in between as above */
|
||||
if (g_bs.rem && pc == g_bs.pc && sp == g_bs.sp) {
|
||||
uint32_t lim = budget - cyc; if (lim > g_bs.rem) lim = g_bs.rem;
|
||||
if (son) { if (snd <= 0) lim = 0; else if ((uint32_t)snd < lim) lim = (uint32_t)snd; }
|
||||
cyc += lim; g_bs.rem -= lim;
|
||||
if (son && (snd -= (int32_t)lim) <= 0) { g_snd_next = snd; t7_snd_service(); snd = g_snd_next; }
|
||||
chk = 1; continue;
|
||||
}
|
||||
#endif
|
||||
#ifdef T7_JIT
|
||||
/* 1.6.12 test2: translated ROM code, when the interpreter would run all of the block now (t7_jit.c) */
|
||||
if (g_t7j_on && !fd && pc >= 0x4000u) {
|
||||
T7JEnt *je = t7_jit_get((uint32_t)(uintptr_t)(rp[pc >> 8] + (pc & 0xFFu)), pc, rp, b3 | (((pc - lp - 1u) & 0xFFFFu) > 2u ? 2u : 0u));
|
||||
if (je && cyc + je->pre < budget && (!son || snd > (int32_t)je->all)) {
|
||||
T7J js;
|
||||
js.a = a; js.x = x; js.y = y; js.s = sp; js.nzn = nzn; js.nzz = nzz; js.fc = fc; js.fv = fv; js.fi = fi;
|
||||
js.snd = snd; js.wram = g_wram; js.rp = rp; js.wp = wp; js.cyc = 0; js.rem = (int32_t)(budget - cyc); js.son = (uint32_t)son;
|
||||
t7_jit_run(&js, je->code);
|
||||
a = js.a; x = js.x; y = js.y; sp = js.s; nzn = js.nzn; nzz = js.nzz; fc = js.fc; fv = js.fv; fi = js.fi; pc = js.pc;
|
||||
cyc += js.cyc;
|
||||
if (js.io) { /* ended after an I/O write: as WR, then the IRQ check */
|
||||
snd = g_snd_next; chk = 1; b3 = (g_rom_bank_id == 3 && !g_t7_static_off);
|
||||
if (son && (snd -= (int32_t)js.io) <= 0) { g_snd_next = snd; t7_snd_service(); snd = g_snd_next; }
|
||||
} else snd = js.snd;
|
||||
lp = pc + 0x8000u;
|
||||
continue;
|
||||
}
|
||||
lp = pc;
|
||||
}
|
||||
#endif
|
||||
#ifdef T7_COUNT
|
||||
{ extern volatile uint32_t g_t7_ninstr; g_t7_ninstr++; }
|
||||
#endif
|
||||
uint32_t op = FETCH();
|
||||
uint32_t c = opcode_cycles[op];
|
||||
switch (op) {
|
||||
case 0xA9: LDA(A_IMM()); break; case 0xA5: a = ZRD(FETCH()); NZ(a); break; case 0xB5: a = ZRD(A_ZPX()); NZ(a); break; case 0xAD: LDA(A_ABS()); break;
|
||||
case 0xBD: LDA(A_ABX()); break; case 0xB9: LDA(A_ABY()); break; case 0xA1: LDA(A_INX()); break; case 0xB1: LDA(A_INY()); break;
|
||||
case 0xA2: LDX(A_IMM()); break; case 0xA6: x = ZRD(FETCH()); NZ(x); break; case 0xB6: LDX(A_ZPY()); break; case 0xAE: LDX(A_ABS()); break; case 0xBE: LDX(A_ABY()); break;
|
||||
case 0xA0: LDY(A_IMM()); break; case 0xA4: y = ZRD(FETCH()); NZ(y); break; case 0xB4: LDY(A_ZPX()); break; case 0xAC: LDY(A_ABS()); break; case 0xBC: LDY(A_ABX()); break;
|
||||
case 0x85: ZWR(FETCH(), a); break; case 0x95: ZWR(A_ZPX(), a); break; case 0x8D: WR(A_ABS(), a); break; case 0x9D: WR(A_ABX(), a); break;
|
||||
case 0x99: WR(A_ABY(), a); break; case 0x81: WR(A_INX(), a); break; case 0x91: WR(A_INY(), a); break;
|
||||
case 0x86: ZWR(FETCH(), x); break; case 0x96: WR(A_ZPY(), x); break; case 0x8E: WR(A_ABS(), x); break;
|
||||
case 0x84: ZWR(FETCH(), y); break; case 0x94: WR(A_ZPX(), y); break; case 0x8C: WR(A_ABS(), y); break;
|
||||
case 0xAA: x = a; NZ(x); break; case 0xA8: y = a; NZ(y); break; case 0x8A: a = x; NZ(a); break; case 0x98: a = y; NZ(a); break;
|
||||
case 0xBA: x = sp; NZ(x); break; case 0x9A: sp = x; break;
|
||||
case 0xE8: x = (x + 1) & 0xFF; NZ(x); break; case 0xC8: y = (y + 1) & 0xFF; NZ(y); break;
|
||||
case 0xCA: x = (x - 1) & 0xFF; NZ(x); break; case 0x88: y = (y - 1) & 0xFF; NZ(y); break;
|
||||
case 0xE6: { uint32_t _z = FETCH(); uint32_t _v = (ZRD(_z) + 1) & 0xFF; NZ(_v); ZWR(_z, _v); } break; case 0xF6: INCM(A_ZPX()); break; case 0xEE: INCM(A_ABS()); break; case 0xFE: INCM(A_ABX()); break;
|
||||
case 0xC6: { uint32_t _z = FETCH(); uint32_t _v = (ZRD(_z) - 1) & 0xFF; NZ(_v); ZWR(_z, _v); } break; case 0xD6: DECM(A_ZPX()); break; case 0xCE: DECM(A_ABS()); break; case 0xDE: DECM(A_ABX()); break;
|
||||
case 0x29: AND(A_IMM()); break; case 0x25: a &= ZRD(FETCH()); NZ(a); break; case 0x35: AND(A_ZPX()); break; case 0x2D: AND(A_ABS()); break;
|
||||
case 0x3D: AND(A_ABX()); break; case 0x39: AND(A_ABY()); break; case 0x21: AND(A_INX()); break; case 0x31: AND(A_INY()); break;
|
||||
case 0x09: ORA(A_IMM()); break; case 0x05: a |= ZRD(FETCH()); NZ(a); break; case 0x15: ORA(A_ZPX()); break; case 0x0D: ORA(A_ABS()); break;
|
||||
case 0x1D: ORA(A_ABX()); break; case 0x19: ORA(A_ABY()); break; case 0x01: ORA(A_INX()); break; case 0x11: ORA(A_INY()); break;
|
||||
case 0x49: EOR(A_IMM()); break; case 0x45: EOR(A_ZP()); break; case 0x55: EOR(A_ZPX()); break; case 0x4D: EOR(A_ABS()); break;
|
||||
case 0x5D: EOR(A_ABX()); break; case 0x59: EOR(A_ABY()); break; case 0x41: EOR(A_INX()); break; case 0x51: EOR(A_INY()); break;
|
||||
case 0x24: BIT(A_ZP()); break; case 0x2C: BIT(A_ABS()); break;
|
||||
case 0x0A: fc = a >> 7; a = (a << 1) & 0xFF; NZ(a); break;
|
||||
case 0x06: ASLM(A_ZP()); break; case 0x16: ASLM(A_ZPX()); break; case 0x0E: ASLM(A_ABS()); break; case 0x1E: ASLM(A_ABX()); break;
|
||||
case 0x4A: fc = a & 1u; a >>= 1; NZ(a); break;
|
||||
case 0x46: LSRM(A_ZP()); break; case 0x56: LSRM(A_ZPX()); break; case 0x4E: LSRM(A_ABS()); break; case 0x5E: LSRM(A_ABX()); break;
|
||||
case 0x2A: { uint32_t _o = ((a << 1) & 0xFF) | fc; fc = a >> 7; a = _o; NZ(a); } break;
|
||||
case 0x26: ROLM(A_ZP()); break; case 0x36: ROLM(A_ZPX()); break; case 0x2E: ROLM(A_ABS()); break; case 0x3E: ROLM(A_ABX()); break;
|
||||
case 0x6A: { uint32_t _o = (a >> 1) | (fc << 7); fc = a & 1u; a = _o; NZ(a); } break;
|
||||
case 0x66: RORM(A_ZP()); break; case 0x76: RORM(A_ZPX()); break; case 0x6E: RORM(A_ABS()); break; case 0x7E: RORM(A_ABX()); break;
|
||||
case 0x69: ADC(A_IMM()); break; case 0x65: ADC(A_ZP()); break; case 0x75: ADC(A_ZPX()); break; case 0x6D: ADC(A_ABS()); break;
|
||||
case 0x7D: ADC(A_ABX()); break; case 0x79: ADC(A_ABY()); break; case 0x61: ADC(A_INX()); break; case 0x71: ADC(A_INY()); break;
|
||||
case 0xE9: SBC(A_IMM()); break; case 0xE5: SBC(A_ZP()); break; case 0xF5: SBC(A_ZPX()); break; case 0xED: SBC(A_ABS()); break;
|
||||
case 0xFD: SBC(A_ABX()); break; case 0xF9: SBC(A_ABY()); break; case 0xE1: SBC(A_INX()); break; case 0xF1: SBC(A_INY()); break;
|
||||
case 0xC9: CMPR(a, A_IMM()); break; case 0xC5: { uint32_t _d = ZRD(FETCH()); fc = a >= _d; NZ((a - _d) & 0xFF); } break; case 0xD5: CMPR(a, A_ZPX()); break; case 0xCD: CMPR(a, A_ABS()); break;
|
||||
case 0xDD: CMPR(a, A_ABX()); break; case 0xD9: CMPR(a, A_ABY()); break; case 0xC1: CMPR(a, A_INX()); break; case 0xD1: CMPR(a, A_INY()); break;
|
||||
case 0xE0: CMPR(x, A_IMM()); break; case 0xE4: CMPR(x, A_ZP()); break; case 0xEC: CMPR(x, A_ABS()); break;
|
||||
case 0xC0: CMPR(y, A_IMM()); break; case 0xC4: CMPR(y, A_ZP()); break; case 0xCC: CMPR(y, A_ABS()); break;
|
||||
case 0x18: fc = 0; break; case 0x38: fc = 1; break; case 0x58: fi = 0; chk = 1; break; case 0x78: fi = 1; break;
|
||||
case 0xB8: fv = 0; break; case 0xD8: fd = 0; break; case 0xF8: fd = 1; break;
|
||||
case 0x48: PUSH(a); break;
|
||||
case 0x68: a = PULL(); NZ(a); break;
|
||||
case 0x08: PUSH(PACKP() | 0x10u); break;
|
||||
case 0x28: UNPACKP(PULL()); chk = 1; break;
|
||||
case 0x4C: pc = A_ABS(); break;
|
||||
case 0x6C: pc = A_IND(); break;
|
||||
#ifdef T7_JSR_HLE
|
||||
case 0x20: { uint32_t _t = A_ABS(); uint32_t _r = (pc - 1) & 0xFFFF;
|
||||
if (g_t7_hle && (_t == 0xD460u || _t == 0xE4CDu)) {
|
||||
#ifdef T7_BLIT_CACHE
|
||||
if (_t == 0xE4CDu && (g_t7_hle & 2u) && g_t7_dark) { /* blit, screen off: left out when its time is known */
|
||||
uint8_t _k[12]; uint32_t _s = bc_key(_k);
|
||||
if (g_bc[_s].k[11] && bc_same(g_bc[_s].k, _k)) {
|
||||
g_wram[0x100 | sp] = (uint8_t)(_r >> 8); g_wram[0x100 | ((sp - 1) & 0xFF)] = (uint8_t)_r;
|
||||
g_bs.rem = g_bc[_s].cyc; g_bs.pc = pc; g_bs.sp = sp; g_bc_hit++; break; /* pc is the return address */
|
||||
} else if (!g_bm.on) { /* measure this one */
|
||||
g_bm.on = 1; g_bm.sp = sp; g_bm.ret = (_r + 1) & 0xFFFF; g_bm.start = (int32_t)(cyc + c); g_bm.irq = 0; g_bm.irqc = 0; g_bm.slot = _s; bc_copy(g_bm.k, _k); g_bc_miss++;
|
||||
#ifdef T7_JIT
|
||||
g_bm.jit = g_t7j_on; g_t7j_on = 0;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#else
|
||||
if (_t == 0xE4CDu && (g_t7_hle & 2u) && g_t7_dark) { /* blit, screen off: JSR + RTS only */
|
||||
g_wram[0x100 | sp] = (uint8_t)(_r >> 8); g_wram[0x100 | ((sp - 1) & 0xFF)] = (uint8_t)_r;
|
||||
c += opcode_cycles[0x60]; break;
|
||||
}
|
||||
#endif
|
||||
#if defined(T7_WAKE_FRESH) || defined(T7_DARK_PEEK)
|
||||
if (_t == 0xE4CDu) g_t7_blits++; /* a blit that runs (the screen after a wake: drawn anew) */
|
||||
#endif
|
||||
if (_t == 0xD460u && (g_t7_hle & 1u)) { /* delay: whole when its RTS runs in this run */
|
||||
uint32_t pre = c + opcode_cycles[0xA2] + 47u * (opcode_cycles[0xE8] + opcode_cycles[0xD0]), all = pre + opcode_cycles[0x60];
|
||||
if (cyc + pre < budget && (!son || snd > (int32_t)all)) {
|
||||
g_wram[0x100 | sp] = (uint8_t)(_r >> 8); g_wram[0x100 | ((sp - 1) & 0xFF)] = (uint8_t)_r;
|
||||
x = 0; nzn = 0; nzz = 0; c = all; break;
|
||||
}
|
||||
}
|
||||
}
|
||||
PUSH(_r >> 8); PUSH(_r & 0xFF); pc = _t; } break;
|
||||
#else
|
||||
case 0x20: { uint32_t _t = A_ABS(); uint32_t _r = (pc - 1) & 0xFFFF; PUSH(_r >> 8); PUSH(_r & 0xFF); pc = _t; } break;
|
||||
#endif
|
||||
case 0x60: { uint32_t _l = PULL(); uint32_t _h = PULL(); pc = ((_l | (_h << 8)) + 1) & 0xFFFF;
|
||||
#ifdef T7_BLIT_CACHE
|
||||
if (g_bm.on && sp == g_bm.sp && pc == g_bm.ret) { /* the measured blit ends: note its cycles */
|
||||
T7BC *_e = &g_bc[g_bm.slot]; bc_copy(_e->k, g_bm.k); _e->cyc = (uint32_t)((int32_t)(cyc + c) - g_bm.start - g_bm.irqc); g_bm.on = 0;
|
||||
#ifdef T7_BC_LOG
|
||||
{ extern void t7_bc_log(const uint8_t *, uint32_t); t7_bc_log(g_bm.k, _e->cyc); }
|
||||
#endif
|
||||
#ifdef T7_JIT
|
||||
g_t7j_on = g_bm.jit;
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
} break;
|
||||
case 0x40: { UNPACKP(PULL()); uint32_t _l = PULL(); uint32_t _h = PULL(); pc = _l | (_h << 8); } chk = 1;
|
||||
#ifdef T7_BLIT_CACHE
|
||||
if (g_bm.on && g_bm.irq && --g_bm.irq == 0) g_bm.irqc += (int32_t)(cyc + c) - g_bm.irq0;
|
||||
#endif
|
||||
break;
|
||||
case 0x00: pc = (pc + 1) & 0xFFFF; PUSH(pc >> 8); PUSH(pc & 0xFF); PUSH(PACKP() | 0x10u);
|
||||
{ uint32_t _l = RD(0xFFFE); uint32_t _h = RD(0xFFFF); pc = _l | (_h << 8); } fi = 1; break;
|
||||
case 0x10: BR(!(nzn & 0x80u)); break; case 0x30: BR(nzn & 0x80u); break;
|
||||
case 0x50: BR(!fv); break; case 0x70: BR(fv); break;
|
||||
case 0x90: BR(!fc); break; case 0xB0: BR(fc); break;
|
||||
case 0xD0: BR(nzz != 0); break;
|
||||
#ifdef T7_IDLE
|
||||
case 0xF0: BR(nzz == 0); if (pc == T7_IDLE_PC && g_t7_idle_ok) chk |= 2u; break;
|
||||
#else
|
||||
case 0xF0: BR(nzz == 0); break;
|
||||
#endif
|
||||
case 0xEA: break;
|
||||
default: c = 2; break;
|
||||
}
|
||||
cyc += c;
|
||||
if (son && (snd -= (int32_t)c) <= 0) { g_snd_next = snd; t7_snd_service(); snd = g_snd_next; chk = 1; }
|
||||
}
|
||||
#ifdef T7_BLIT_CACHE
|
||||
if (g_bm.on) { g_bm.start -= (int32_t)cyc; if (g_bm.irq) g_bm.irq0 -= (int32_t)cyc; } /* goes on in the next run */
|
||||
#endif
|
||||
T7R_SAVE();
|
||||
return cyc;
|
||||
}
|
||||
#undef T7R_SAVE
|
||||
#undef T7R_LOAD
|
||||
#undef RD
|
||||
#undef WR
|
||||
#endif
|
||||
+541
-541
File diff suppressed because it is too large.
Load diff
+35
-35
@@ -1,35 +1,35 @@
|
||||
/* t7_sound.h - Originalton (Timer 0/1 + DAC), siehe t7_sound.c */
|
||||
#ifndef T7_SOUND_H
|
||||
#define T7_SOUND_H
|
||||
#include <stdint.h>
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
extern uint8_t g_snd_on;
|
||||
extern int32_t g_snd_next;
|
||||
void t7_snd_io_write(uint8_t reg, uint8_t value);
|
||||
void t7_snd_service(void);
|
||||
void t7_snd_flush(void);
|
||||
void t7_snd_set_mode(int on); // 1 = Originalton (Timer + DAC), 0 = alt
|
||||
void t7_snd_config(uint32_t emu_hz, uint32_t sample_rate);
|
||||
void t7_snd_capture(int on); // Tonstrom aufzeichnen
|
||||
void t7_snd_volume(int pct); // 0..100
|
||||
void t7_snd_reset(void); // Ring leeren
|
||||
uint32_t t7_snd_avail(void);
|
||||
uint32_t t7_snd_pull(int16_t *out, uint32_t n, uint32_t target); // geregelt, target in Samples (0 = ungeregelt)
|
||||
uint32_t t7_snd_underruns(void);
|
||||
void t7_snd_synth(int on); // 1 = klarer Ton (Rechteck neu erzeugt), 0 = DAC roh
|
||||
void t7_snd_resync(void);
|
||||
int t7_snd_active(void);
|
||||
uint32_t t7_snd_tone_hz(uint8_t *level); // Rechteckton gerade (fuer Piezo), 0 = still
|
||||
void t7_snd_set_t1_hz(uint32_t hz);
|
||||
// Piezo-Geraete: Ton-Ereignisse (Zeitpunkt in CPU-Zyklen, Frequenz in Hz, 0 = still)
|
||||
void t7_snd_tone_events(uint32_t interval_cycles); // 0 = aus
|
||||
int t7_snd_tone_peek(uint64_t *cyc, uint32_t *hz);
|
||||
void t7_snd_tone_pop(void);
|
||||
uint64_t t7_snd_cycles(void);
|
||||
int t7_snd_busy(void);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
/* t7_sound.h - Originalton (Timer 0/1 + DAC), siehe t7_sound.c */
|
||||
#ifndef T7_SOUND_H
|
||||
#define T7_SOUND_H
|
||||
#include <stdint.h>
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
extern uint8_t g_snd_on;
|
||||
extern int32_t g_snd_next;
|
||||
void t7_snd_io_write(uint8_t reg, uint8_t value);
|
||||
void t7_snd_service(void);
|
||||
void t7_snd_flush(void);
|
||||
void t7_snd_set_mode(int on); // 1 = Originalton (Timer + DAC), 0 = alt
|
||||
void t7_snd_config(uint32_t emu_hz, uint32_t sample_rate);
|
||||
void t7_snd_capture(int on); // Tonstrom aufzeichnen
|
||||
void t7_snd_volume(int pct); // 0..100
|
||||
void t7_snd_reset(void); // Ring leeren
|
||||
uint32_t t7_snd_avail(void);
|
||||
uint32_t t7_snd_pull(int16_t *out, uint32_t n, uint32_t target); // geregelt, target in Samples (0 = ungeregelt)
|
||||
uint32_t t7_snd_underruns(void);
|
||||
void t7_snd_synth(int on); // 1 = klarer Ton (Rechteck neu erzeugt), 0 = DAC roh
|
||||
void t7_snd_resync(void);
|
||||
int t7_snd_active(void);
|
||||
uint32_t t7_snd_tone_hz(uint8_t *level); // Rechteckton gerade (fuer Piezo), 0 = still
|
||||
void t7_snd_set_t1_hz(uint32_t hz);
|
||||
// Piezo-Geraete: Ton-Ereignisse (Zeitpunkt in CPU-Zyklen, Frequenz in Hz, 0 = still)
|
||||
void t7_snd_tone_events(uint32_t interval_cycles); // 0 = aus
|
||||
int t7_snd_tone_peek(uint64_t *cyc, uint32_t *hz);
|
||||
void t7_snd_tone_pop(void);
|
||||
uint64_t t7_snd_cycles(void);
|
||||
int t7_snd_busy(void);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
+186
-186
@@ -1,186 +1,186 @@
|
||||
# T7 app (t7.bin, device program) in Unicorn: hardware calls are caught, the screen is drawn
|
||||
# into a picture, the SysTick is advanced every RT instructions (96 MHz Cortex-M3 ~ RT per ms).
|
||||
# python3 devt7.py <t7.elf> <paradise_image_with_t7.bin> <ms> [KEYS="t:mask,..."] [SHOTS="t,t,..."] [OUT=dir]
|
||||
import sys, struct, subprocess, os
|
||||
from unicorn import *
|
||||
from unicorn.arm_const import *
|
||||
elf, img, total_ms = sys.argv[1], sys.argv[2], int(sys.argv[3])
|
||||
RT = int(os.environ.get('RT', '64000')) # instructions per millisecond (96 MHz, ~1.5 cycles each)
|
||||
out = os.environ.get('OUT', '.')
|
||||
syms = {}
|
||||
for l in subprocess.run(['llvm-nm', elf], capture_output=True, text=True).stdout.splitlines():
|
||||
p = l.split()
|
||||
if len(p) == 3: syms[p[2]] = int(p[0], 16)
|
||||
fl = bytearray(open(img, 'rb').read())
|
||||
u = Uc(UC_ARCH_ARM, UC_MODE_THUMB | UC_MODE_MCLASS)
|
||||
u.ctl_set_cpu_model(UC_CPU_ARM_CORTEX_M4)
|
||||
APPOFF = int(os.environ.get('APPOFF', '0xA0000'), 16) # program image (1.6.0: more programs elsewhere)
|
||||
u.mem_map(0, 0x10000); u.mem_write(0, bytes(fl[APPOFF:APPOFF + 0x10000]))
|
||||
u.mem_map(0x18000000, 0x20000); u.mem_map(0x20000000, 0x1000)
|
||||
u.mem_map(0x10000000, 0x100000)
|
||||
u.mem_map(0x60000000, 0x1000000); u.mem_write(0x60000000, bytes(fl))
|
||||
u.mem_map(0x40000000, 0x100000); u.mem_map(0x45000000, 0x1000); u.mem_map(0xE000E000, 0x1000); u.mem_map(0xE0001000, 0x1000) # DWT (CYCCNT reads 0 here)
|
||||
u.mem_write(0x4000B014, struct.pack('<I', 0x60)) # 1.6.9 T7_LINK: UART1 LSR, sender empty (nothing received)
|
||||
cfg = APPOFF + 0x140
|
||||
rom_off = struct.unpack_from('<I', fl, cfg + 8)[0]
|
||||
u.mem_write(0x10000000, bytes(fl[rom_off:rom_off + 0x100000]))
|
||||
fb = [[0] * 128 for _ in range(128)]
|
||||
rect = {'x0': 0, 'x1': 0, 'y': 0, 'x': 0}
|
||||
KS = []
|
||||
for p in os.environ.get('KEYS', '').split(','):
|
||||
if ':' in p: t, m = p.split(':'); KS.append((float(t), int(m)))
|
||||
SH = [float(x) for x in os.environ.get('SHOTS', '').split(',') if x]
|
||||
state = {'ms': 0, 'tone': [], 'left': 0, 'flashw': 0}
|
||||
def keys():
|
||||
m = 0
|
||||
for t, v in KS:
|
||||
if state['ms'] / 1000.0 >= t: m = v
|
||||
return m
|
||||
def ret(uc, v=None):
|
||||
if v is not None: uc.reg_write(UC_ARM_REG_R0, v)
|
||||
uc.reg_write(UC_ARM_REG_PC, uc.reg_read(UC_ARM_REG_LR) | 1)
|
||||
def h_begin(uc, *a):
|
||||
x0, y0, x1, y1 = (uc.reg_read(r) for r in (UC_ARM_REG_R0, UC_ARM_REG_R1, UC_ARM_REG_R2, UC_ARM_REG_R3))
|
||||
rect.update(x0=x0, x1=x1, y=y0, x=x0); ret(uc)
|
||||
def h_row(uc, *a):
|
||||
p, n = uc.reg_read(UC_ARM_REG_R0), uc.reg_read(UC_ARM_REG_R1)
|
||||
d = struct.unpack('<%dH' % n, uc.mem_read(p, 2 * n))
|
||||
for v in d:
|
||||
if rect['y'] < 128 and rect['x'] < 128: fb[rect['y']][rect['x']] = v
|
||||
rect['x'] += 1
|
||||
if rect['x'] >= rect['x1']: rect['x'] = rect['x0']; rect['y'] += 1
|
||||
ret(uc)
|
||||
def h_tone(uc, *a): state['tone'].append((state['ms'], uc.reg_read(UC_ARM_REG_R0))); ret(uc)
|
||||
def h_btn(uc, *a): ret(uc, keys())
|
||||
def h_prog(uc, *a):
|
||||
ad, src = uc.reg_read(UC_ARM_REG_R0), uc.reg_read(UC_ARM_REG_R1)
|
||||
d = bytes(uc.mem_read(src & ~3, 0x1000)); a = ad & 0xFFFFFF # test27: the device's DMA takes words (start rounded down)
|
||||
uc.mem_write(0x60000000 + a, d); fl[a:a + 0x1000] = d; state['flashw'] += 1; ret(uc)
|
||||
def h_erase(uc, *a):
|
||||
a = uc.reg_read(UC_ARM_REG_R0) & 0xFFFFFF
|
||||
uc.mem_write(0x60000000 + a, b'\xff' * 0x1000); fl[a:a + 0x1000] = b'\xff' * 0x1000; ret(uc)
|
||||
def h_back(uc, *a): state['back'] = 1; uc.emu_stop()
|
||||
def h_nop(uc, *a): ret(uc)
|
||||
H = {'hw_px_begin': h_begin, 'hw_px_row': h_row, 'hw_px_end': h_nop, 'hw_tone': h_tone, 'hw_buttons': h_btn,
|
||||
'flash_prog4k': h_prog, 'flash_erase4k': h_erase, 'hw_back_to_paradise': h_back, 'hw_init': h_nop,
|
||||
'hw_backlight': h_nop, 'hw_bl_pwm_off': h_nop, 'hw_wdt_feed': h_nop}
|
||||
for n, f in H.items():
|
||||
a = syms[n] & ~1
|
||||
u.hook_add(UC_HOOK_CODE, (lambda f: lambda uc, ad, sz, ud: f(uc))(f), begin=a, end=a)
|
||||
def bad(uc, access, addr, size, value, ud):
|
||||
print('unmapped', access, hex(addr), 'pc', hex(uc.reg_read(UC_ARM_REG_PC))); return False
|
||||
u.hook_add(UC_HOOK_MEM_UNMAPPED, bad)
|
||||
# 1.6.9 test22 CLK_ECO: the AHB prescaler at 0x45000010 slows the core: RT >> sh instructions per ms
|
||||
state['sh'] = 0; state['shms'] = [0, 0, 0, 0]
|
||||
def _ahb(uc, acc, a, sz, v, ud):
|
||||
state['sh'] = v & 7
|
||||
if os.environ.get('ECOLOG'): print('AHB', v & 7, 'at ms', state['ms'])
|
||||
u.hook_add(UC_HOOK_MEM_WRITE, _ahb, begin=0x45000010, end=0x45000013)
|
||||
# 1.6.12 test1 T7_PW: system clock source (0x4500000C: 0 IHRC 12 MHz, 4 PLL; status in bits 6:4), PLL locked flag,
|
||||
# RTC on with 1 s, deep sleep (PMU_CTRL bit 1, then WFI): the time runs on to the next RTC second or a key change
|
||||
u.mem_write(0x4500000C, struct.pack('<I', 0x44)); u.mem_write(0x45000008, struct.pack('<I', 0x55)); u.mem_write(0x45000200, struct.pack('<I', 0x80))
|
||||
state['ihrc'] = 0; state['pw'] = {'full': 0, 'ihrc': 0, 'sleep': 0}
|
||||
def _clk(uc, acc, a, sz, v, ud):
|
||||
state['ihrc'] = (v & 7) == 0
|
||||
uc.mem_write(0x4500000C, struct.pack('<I', (v & 7) | ((v & 7) << 4)))
|
||||
if os.environ.get('ECOLOG'): print('CLKSRC', v & 7, 'at ms', state['ms'])
|
||||
u.hook_add(UC_HOOK_MEM_WRITE, _clk, begin=0x4500000C, end=0x4500000F)
|
||||
def _pmu(uc, acc, a, sz, v, ud):
|
||||
# deep sleep: the time jumps to the next RTC second (or a key change) at once; the code after the WFI sees it
|
||||
if v & 2 and not state.get('dsleep'):
|
||||
k0 = keys(); t = state['ms']
|
||||
while True:
|
||||
t += 1
|
||||
if t % 1000 == 0 or t >= total_ms or any(t / 1000.0 >= kt > state['ms'] / 1000.0 for kt, _ in KS): break
|
||||
state['dsleep'] = t
|
||||
if t % 1000 == 0 and 'g_pw_rtcw' in syms: uc.mem_write(syms['g_pw_rtcw'], b'\x01') # the RTC interrupt ran
|
||||
if int(os.environ.get('RTCLAG', '0')):
|
||||
# 1.6.13 test3: the counter shows the new second only a few reads after the wake (32 kHz domain)
|
||||
state['rtc_new'] = rtc0 + t // 1000; state['rtclag'] = int(os.environ['RTCLAG'])
|
||||
else: uc.mem_write(0x45000304, struct.pack('<I', rtc0 + t // 1000))
|
||||
u.hook_add(UC_HOOK_MEM_WRITE, _pmu, begin=0x45000300, end=0x45000303)
|
||||
def _rtcrd(uc, acc, a, sz, v, ud):
|
||||
if 'rtc_new' in state:
|
||||
if state['rtclag'] > 0: state['rtclag'] -= 1
|
||||
else: uc.mem_write(0x45000304, struct.pack('<I', state.pop('rtc_new')))
|
||||
u.hook_add(UC_HOOK_MEM_READ, _rtcrd, begin=0x45000304, end=0x45000307)
|
||||
RTCLAGMS = int(os.environ.get('RTCLAGMS', '0'))
|
||||
if RTCLAGMS:
|
||||
# 1.6.13 test3: the counter shows a new second only RTCLAGMS ms after it began (the RTC interrupt comes on time)
|
||||
def _rtcms(uc, acc, a, sz, v, ud):
|
||||
eff = state.get('dsleep') or state['ms']
|
||||
uc.mem_write(0x45000304, struct.pack('<I', rtc0 + max(eff - RTCLAGMS, 0) // 1000))
|
||||
u.hook_add(UC_HOOK_MEM_READ, _rtcms, begin=0x45000304, end=0x45000307)
|
||||
if os.environ.get('MEMW'):
|
||||
_mw = int(os.environ['MEMW'], 16)
|
||||
u.hook_add(UC_HOOK_MEM_WRITE, lambda uc, acc, a, sz, v, ud: print('W %08x = %08x at ms %d' % (a, v, state['ms'])), begin=_mw, end=_mw + 0x3F)
|
||||
def shot(name):
|
||||
from PIL import Image
|
||||
im = Image.new('RGB', (128, 128))
|
||||
for y in range(128):
|
||||
for x in range(128):
|
||||
v = fb[y][x]; im.putpixel((x, y), (((v >> 11) & 31) << 3, ((v >> 5) & 63) << 2, (v & 31) << 3))
|
||||
im.save(os.path.join(out, name))
|
||||
_RSAVE = [UC_ARM_REG_R0 + i for i in range(13)] + [UC_ARM_REG_SP, UC_ARM_REG_LR, UC_ARM_REG_XPSR]
|
||||
sp, rst = struct.unpack('<II', bytes(u.mem_read(0, 8)))
|
||||
u.reg_write(UC_ARM_REG_SP, sp)
|
||||
pc = rst; gms = syms['g_ms']; rtc0 = int(os.environ.get('RTC0', '5000'))
|
||||
emu_ms_sym = None
|
||||
si = 0
|
||||
while state['ms'] < total_ms and 'back' not in state:
|
||||
u.mem_write(0x45000304, struct.pack('<I', rtc0 + state['ms'] // 1000))
|
||||
try:
|
||||
_sh = 3 if state['ihrc'] else state['sh']
|
||||
u.emu_start(pc | 1, 0xFFFFFFF0, count=max(1, RT >> _sh)); state['shms'][min(_sh, 3)] += 1
|
||||
state['pw']['ihrc' if state['ihrc'] else 'full'] += 1
|
||||
except UcError as ex:
|
||||
print('error', ex, hex(u.reg_read(UC_ARM_REG_PC))); break
|
||||
pc = u.reg_read(UC_ARM_REG_PC)
|
||||
_ds = state.pop('dsleep', 0)
|
||||
if _ds:
|
||||
n = _ds - state['ms']; state['ms'] = _ds
|
||||
state['pw']['sleep'] += n
|
||||
if os.environ.get('DBGSLEEP'): print('slept', n, 'to ms', state['ms'], 'pc', hex(pc), 'g_ms', struct.unpack('<I', u.mem_read(syms['g_ms'], 4))[0], 'emu', struct.unpack('<I', u.mem_read(syms['g_t7st'], 4))[0])
|
||||
u.mem_write(0x45000304, struct.pack('<I', rtc0 + state['ms'] // 1000))
|
||||
km = keys(); u.mem_write(0x40018000, struct.pack('<I', 0xFFFFFFFF & ~(((km & 1) << 9) | ((km & 2) << 10) | ((km & 4) << 8) | ((km & 8) << 5))))
|
||||
continue
|
||||
state['ms'] += 1
|
||||
_ts = int(os.environ.get('TICKSLOW', '0')) # test27: a slow ms tick at a lower clock (device fault model)
|
||||
if not (_ts and state['sh'] > 0 and state['ms'] % _ts):
|
||||
v = struct.unpack('<I', u.mem_read(gms, 4))[0]; u.mem_write(gms, struct.pack('<I', v + 1))
|
||||
if 'g_ms_raw' in syms: _r = syms['g_ms_raw']; u.mem_write(_r, struct.pack('<I', struct.unpack('<I', u.mem_read(_r, 4))[0] + 1))
|
||||
# 1.6.12 test4 (T7_TONE_ISR): the tick plays the tone queue; here called like the interrupt would
|
||||
if 'tq_isr_on' in syms and u.mem_read(syms['tq_isr_on'], 1)[0]:
|
||||
_regs = [u.reg_read(r) for r in _RSAVE]
|
||||
u.reg_write(UC_ARM_REG_SP, (_regs[13] - 64) & ~7); u.reg_write(UC_ARM_REG_LR, 0xFFF1)
|
||||
u.emu_start(syms['tq_pump'] | 1, 0xFFF0, count=200000)
|
||||
for r, v in zip(_RSAVE, _regs): u.reg_write(r, v)
|
||||
# 1.6.9 test13 T7_KEYQ: the GPIO as the keys say (pressed = 0) and the tick's press counter
|
||||
km = keys(); u.mem_write(0x40018000, struct.pack('<I', 0xFFFFFFFF & ~(((km & 1) << 9) | ((km & 2) << 10) | ((km & 4) << 8) | ((km & 8) << 5))))
|
||||
if 'g_kin' in syms:
|
||||
e = km & ~state.get('kprev', 0) & 7; state['kprev'] = km
|
||||
u.mem_write(syms['g_kprev'], bytes([km & 7]))
|
||||
for i in range(3):
|
||||
if e & (1 << i): a = syms['g_kin'] + i; u.mem_write(a, bytes([(u.mem_read(a, 1)[0] + 1) & 0xFF]))
|
||||
if si < len(SH) and state['ms'] >= SH[si] * 1000: shot('shot_%02d.png' % si); si += 1
|
||||
LL = int(os.environ.get('LAGLOG', '0')) # 1.6.13 test4: every LL ms: emulated ms in that time (100 % = real time)
|
||||
if LL and state['ms'] % LL == 0:
|
||||
_st = struct.unpack('<2I', bytes(u.mem_read(syms['g_t7st'], 8))); _p = state.get('llp', _st)
|
||||
print('lag', state['ms'], _st[0] - _p[0], _st[1] - _p[1], flush=True); state['llp'] = _st
|
||||
if os.environ.get('CLOG') and state['ms'] % int(os.environ['CLOG']) == 0:
|
||||
_w = bytes(u.mem_read(syms['g_wram'], 0x600)); _sa = int(os.environ.get('CSEC', '0x396'), 16)
|
||||
_eb = struct.unpack('<3I', bytes(u.mem_read(syms['g_eco_bad'], 4)) + bytes(u.mem_read(syms['g_eco_fix'], 8))) if 'g_eco_bad' in syms else ()
|
||||
if os.environ.get('DBGSYM'):
|
||||
for _d in os.environ['DBGSYM'].split(','):
|
||||
_s, _l = _d.split(':'); print(' ', _s, bytes(u.mem_read(syms[_s], int(_l, 0))).hex())
|
||||
print('clock', state['ms'], 'rtc', rtc0 + state['ms'] // 1000, 'game %02d:%02d:%02d' % (_w[0x306], _w[0x307], _w[_sa]), 'sh', state['sh'], 'eco_bad/fix', _eb)
|
||||
st = struct.unpack('<4I', bytes(u.mem_read(syms['g_t7st'], 16)))
|
||||
print('emulated ms', st[0], 'dropped ms', st[1])
|
||||
(os.environ.get('TONES') and print('tones:', state['tone']));print('ms', state['ms'], 'tones', len(state['tone']), 'flash writes', state['flashw'], 'back' if 'back' in state else '', 'pc', hex(pc))
|
||||
print('ms per clock step (96, 48, 24, 12 MHz):', state['shms'])
|
||||
print('power ms: full', state['pw']['full'], 'ihrc', state['pw']['ihrc'], 'deep sleep', state['pw']['sleep'])
|
||||
open(os.path.join(out, 'flash_after.bin'), 'wb').write(fl)
|
||||
for _n in os.environ.get('SYMDUMP', '').split(','):
|
||||
if ':' in _n:
|
||||
_s, _l = _n.split(':'); open(os.path.join(out, _s + '.bin'), 'wb').write(bytes(u.mem_read(syms[_s], int(_l, 0))))
|
||||
shot('shot_end.png')
|
||||
# T7 app (t7.bin, device program) in Unicorn: hardware calls are caught, the screen is drawn
|
||||
# into a picture, the SysTick is advanced every RT instructions (96 MHz Cortex-M3 ~ RT per ms).
|
||||
# python3 devt7.py <t7.elf> <paradise_image_with_t7.bin> <ms> [KEYS="t:mask,..."] [SHOTS="t,t,..."] [OUT=dir]
|
||||
import sys, struct, subprocess, os
|
||||
from unicorn import *
|
||||
from unicorn.arm_const import *
|
||||
elf, img, total_ms = sys.argv[1], sys.argv[2], int(sys.argv[3])
|
||||
RT = int(os.environ.get('RT', '64000')) # instructions per millisecond (96 MHz, ~1.5 cycles each)
|
||||
out = os.environ.get('OUT', '.')
|
||||
syms = {}
|
||||
for l in subprocess.run(['llvm-nm', elf], capture_output=True, text=True).stdout.splitlines():
|
||||
p = l.split()
|
||||
if len(p) == 3: syms[p[2]] = int(p[0], 16)
|
||||
fl = bytearray(open(img, 'rb').read())
|
||||
u = Uc(UC_ARCH_ARM, UC_MODE_THUMB | UC_MODE_MCLASS)
|
||||
u.ctl_set_cpu_model(UC_CPU_ARM_CORTEX_M4)
|
||||
APPOFF = int(os.environ.get('APPOFF', '0xA0000'), 16) # program image (1.6.0: more programs elsewhere)
|
||||
u.mem_map(0, 0x10000); u.mem_write(0, bytes(fl[APPOFF:APPOFF + 0x10000]))
|
||||
u.mem_map(0x18000000, 0x20000); u.mem_map(0x20000000, 0x1000)
|
||||
u.mem_map(0x10000000, 0x100000)
|
||||
u.mem_map(0x60000000, 0x1000000); u.mem_write(0x60000000, bytes(fl))
|
||||
u.mem_map(0x40000000, 0x100000); u.mem_map(0x45000000, 0x1000); u.mem_map(0xE000E000, 0x1000); u.mem_map(0xE0001000, 0x1000) # DWT (CYCCNT reads 0 here)
|
||||
u.mem_write(0x4000B014, struct.pack('<I', 0x60)) # 1.6.9 T7_LINK: UART1 LSR, sender empty (nothing received)
|
||||
cfg = APPOFF + 0x140
|
||||
rom_off = struct.unpack_from('<I', fl, cfg + 8)[0]
|
||||
u.mem_write(0x10000000, bytes(fl[rom_off:rom_off + 0x100000]))
|
||||
fb = [[0] * 128 for _ in range(128)]
|
||||
rect = {'x0': 0, 'x1': 0, 'y': 0, 'x': 0}
|
||||
KS = []
|
||||
for p in os.environ.get('KEYS', '').split(','):
|
||||
if ':' in p: t, m = p.split(':'); KS.append((float(t), int(m)))
|
||||
SH = [float(x) for x in os.environ.get('SHOTS', '').split(',') if x]
|
||||
state = {'ms': 0, 'tone': [], 'left': 0, 'flashw': 0}
|
||||
def keys():
|
||||
m = 0
|
||||
for t, v in KS:
|
||||
if state['ms'] / 1000.0 >= t: m = v
|
||||
return m
|
||||
def ret(uc, v=None):
|
||||
if v is not None: uc.reg_write(UC_ARM_REG_R0, v)
|
||||
uc.reg_write(UC_ARM_REG_PC, uc.reg_read(UC_ARM_REG_LR) | 1)
|
||||
def h_begin(uc, *a):
|
||||
x0, y0, x1, y1 = (uc.reg_read(r) for r in (UC_ARM_REG_R0, UC_ARM_REG_R1, UC_ARM_REG_R2, UC_ARM_REG_R3))
|
||||
rect.update(x0=x0, x1=x1, y=y0, x=x0); ret(uc)
|
||||
def h_row(uc, *a):
|
||||
p, n = uc.reg_read(UC_ARM_REG_R0), uc.reg_read(UC_ARM_REG_R1)
|
||||
d = struct.unpack('<%dH' % n, uc.mem_read(p, 2 * n))
|
||||
for v in d:
|
||||
if rect['y'] < 128 and rect['x'] < 128: fb[rect['y']][rect['x']] = v
|
||||
rect['x'] += 1
|
||||
if rect['x'] >= rect['x1']: rect['x'] = rect['x0']; rect['y'] += 1
|
||||
ret(uc)
|
||||
def h_tone(uc, *a): state['tone'].append((state['ms'], uc.reg_read(UC_ARM_REG_R0))); ret(uc)
|
||||
def h_btn(uc, *a): ret(uc, keys())
|
||||
def h_prog(uc, *a):
|
||||
ad, src = uc.reg_read(UC_ARM_REG_R0), uc.reg_read(UC_ARM_REG_R1)
|
||||
d = bytes(uc.mem_read(src & ~3, 0x1000)); a = ad & 0xFFFFFF # test27: the device's DMA takes words (start rounded down)
|
||||
uc.mem_write(0x60000000 + a, d); fl[a:a + 0x1000] = d; state['flashw'] += 1; ret(uc)
|
||||
def h_erase(uc, *a):
|
||||
a = uc.reg_read(UC_ARM_REG_R0) & 0xFFFFFF
|
||||
uc.mem_write(0x60000000 + a, b'\xff' * 0x1000); fl[a:a + 0x1000] = b'\xff' * 0x1000; ret(uc)
|
||||
def h_back(uc, *a): state['back'] = 1; uc.emu_stop()
|
||||
def h_nop(uc, *a): ret(uc)
|
||||
H = {'hw_px_begin': h_begin, 'hw_px_row': h_row, 'hw_px_end': h_nop, 'hw_tone': h_tone, 'hw_buttons': h_btn,
|
||||
'flash_prog4k': h_prog, 'flash_erase4k': h_erase, 'hw_back_to_paradise': h_back, 'hw_init': h_nop,
|
||||
'hw_backlight': h_nop, 'hw_bl_pwm_off': h_nop, 'hw_wdt_feed': h_nop}
|
||||
for n, f in H.items():
|
||||
a = syms[n] & ~1
|
||||
u.hook_add(UC_HOOK_CODE, (lambda f: lambda uc, ad, sz, ud: f(uc))(f), begin=a, end=a)
|
||||
def bad(uc, access, addr, size, value, ud):
|
||||
print('unmapped', access, hex(addr), 'pc', hex(uc.reg_read(UC_ARM_REG_PC))); return False
|
||||
u.hook_add(UC_HOOK_MEM_UNMAPPED, bad)
|
||||
# 1.6.9 test22 CLK_ECO: the AHB prescaler at 0x45000010 slows the core: RT >> sh instructions per ms
|
||||
state['sh'] = 0; state['shms'] = [0, 0, 0, 0]
|
||||
def _ahb(uc, acc, a, sz, v, ud):
|
||||
state['sh'] = v & 7
|
||||
if os.environ.get('ECOLOG'): print('AHB', v & 7, 'at ms', state['ms'])
|
||||
u.hook_add(UC_HOOK_MEM_WRITE, _ahb, begin=0x45000010, end=0x45000013)
|
||||
# 1.6.12 test1 T7_PW: system clock source (0x4500000C: 0 IHRC 12 MHz, 4 PLL; status in bits 6:4), PLL locked flag,
|
||||
# RTC on with 1 s, deep sleep (PMU_CTRL bit 1, then WFI): the time runs on to the next RTC second or a key change
|
||||
u.mem_write(0x4500000C, struct.pack('<I', 0x44)); u.mem_write(0x45000008, struct.pack('<I', 0x55)); u.mem_write(0x45000200, struct.pack('<I', 0x80))
|
||||
state['ihrc'] = 0; state['pw'] = {'full': 0, 'ihrc': 0, 'sleep': 0}
|
||||
def _clk(uc, acc, a, sz, v, ud):
|
||||
state['ihrc'] = (v & 7) == 0
|
||||
uc.mem_write(0x4500000C, struct.pack('<I', (v & 7) | ((v & 7) << 4)))
|
||||
if os.environ.get('ECOLOG'): print('CLKSRC', v & 7, 'at ms', state['ms'])
|
||||
u.hook_add(UC_HOOK_MEM_WRITE, _clk, begin=0x4500000C, end=0x4500000F)
|
||||
def _pmu(uc, acc, a, sz, v, ud):
|
||||
# deep sleep: the time jumps to the next RTC second (or a key change) at once; the code after the WFI sees it
|
||||
if v & 2 and not state.get('dsleep'):
|
||||
k0 = keys(); t = state['ms']
|
||||
while True:
|
||||
t += 1
|
||||
if t % 1000 == 0 or t >= total_ms or any(t / 1000.0 >= kt > state['ms'] / 1000.0 for kt, _ in KS): break
|
||||
state['dsleep'] = t
|
||||
if t % 1000 == 0 and 'g_pw_rtcw' in syms: uc.mem_write(syms['g_pw_rtcw'], b'\x01') # the RTC interrupt ran
|
||||
if int(os.environ.get('RTCLAG', '0')):
|
||||
# 1.6.13 test3: the counter shows the new second only a few reads after the wake (32 kHz domain)
|
||||
state['rtc_new'] = rtc0 + t // 1000; state['rtclag'] = int(os.environ['RTCLAG'])
|
||||
else: uc.mem_write(0x45000304, struct.pack('<I', rtc0 + t // 1000))
|
||||
u.hook_add(UC_HOOK_MEM_WRITE, _pmu, begin=0x45000300, end=0x45000303)
|
||||
def _rtcrd(uc, acc, a, sz, v, ud):
|
||||
if 'rtc_new' in state:
|
||||
if state['rtclag'] > 0: state['rtclag'] -= 1
|
||||
else: uc.mem_write(0x45000304, struct.pack('<I', state.pop('rtc_new')))
|
||||
u.hook_add(UC_HOOK_MEM_READ, _rtcrd, begin=0x45000304, end=0x45000307)
|
||||
RTCLAGMS = int(os.environ.get('RTCLAGMS', '0'))
|
||||
if RTCLAGMS:
|
||||
# 1.6.13 test3: the counter shows a new second only RTCLAGMS ms after it began (the RTC interrupt comes on time)
|
||||
def _rtcms(uc, acc, a, sz, v, ud):
|
||||
eff = state.get('dsleep') or state['ms']
|
||||
uc.mem_write(0x45000304, struct.pack('<I', rtc0 + max(eff - RTCLAGMS, 0) // 1000))
|
||||
u.hook_add(UC_HOOK_MEM_READ, _rtcms, begin=0x45000304, end=0x45000307)
|
||||
if os.environ.get('MEMW'):
|
||||
_mw = int(os.environ['MEMW'], 16)
|
||||
u.hook_add(UC_HOOK_MEM_WRITE, lambda uc, acc, a, sz, v, ud: print('W %08x = %08x at ms %d' % (a, v, state['ms'])), begin=_mw, end=_mw + 0x3F)
|
||||
def shot(name):
|
||||
from PIL import Image
|
||||
im = Image.new('RGB', (128, 128))
|
||||
for y in range(128):
|
||||
for x in range(128):
|
||||
v = fb[y][x]; im.putpixel((x, y), (((v >> 11) & 31) << 3, ((v >> 5) & 63) << 2, (v & 31) << 3))
|
||||
im.save(os.path.join(out, name))
|
||||
_RSAVE = [UC_ARM_REG_R0 + i for i in range(13)] + [UC_ARM_REG_SP, UC_ARM_REG_LR, UC_ARM_REG_XPSR]
|
||||
sp, rst = struct.unpack('<II', bytes(u.mem_read(0, 8)))
|
||||
u.reg_write(UC_ARM_REG_SP, sp)
|
||||
pc = rst; gms = syms['g_ms']; rtc0 = int(os.environ.get('RTC0', '5000'))
|
||||
emu_ms_sym = None
|
||||
si = 0
|
||||
while state['ms'] < total_ms and 'back' not in state:
|
||||
u.mem_write(0x45000304, struct.pack('<I', rtc0 + state['ms'] // 1000))
|
||||
try:
|
||||
_sh = 3 if state['ihrc'] else state['sh']
|
||||
u.emu_start(pc | 1, 0xFFFFFFF0, count=max(1, RT >> _sh)); state['shms'][min(_sh, 3)] += 1
|
||||
state['pw']['ihrc' if state['ihrc'] else 'full'] += 1
|
||||
except UcError as ex:
|
||||
print('error', ex, hex(u.reg_read(UC_ARM_REG_PC))); break
|
||||
pc = u.reg_read(UC_ARM_REG_PC)
|
||||
_ds = state.pop('dsleep', 0)
|
||||
if _ds:
|
||||
n = _ds - state['ms']; state['ms'] = _ds
|
||||
state['pw']['sleep'] += n
|
||||
if os.environ.get('DBGSLEEP'): print('slept', n, 'to ms', state['ms'], 'pc', hex(pc), 'g_ms', struct.unpack('<I', u.mem_read(syms['g_ms'], 4))[0], 'emu', struct.unpack('<I', u.mem_read(syms['g_t7st'], 4))[0])
|
||||
u.mem_write(0x45000304, struct.pack('<I', rtc0 + state['ms'] // 1000))
|
||||
km = keys(); u.mem_write(0x40018000, struct.pack('<I', 0xFFFFFFFF & ~(((km & 1) << 9) | ((km & 2) << 10) | ((km & 4) << 8) | ((km & 8) << 5))))
|
||||
continue
|
||||
state['ms'] += 1
|
||||
_ts = int(os.environ.get('TICKSLOW', '0')) # test27: a slow ms tick at a lower clock (device fault model)
|
||||
if not (_ts and state['sh'] > 0 and state['ms'] % _ts):
|
||||
v = struct.unpack('<I', u.mem_read(gms, 4))[0]; u.mem_write(gms, struct.pack('<I', v + 1))
|
||||
if 'g_ms_raw' in syms: _r = syms['g_ms_raw']; u.mem_write(_r, struct.pack('<I', struct.unpack('<I', u.mem_read(_r, 4))[0] + 1))
|
||||
# 1.6.12 test4 (T7_TONE_ISR): the tick plays the tone queue; here called like the interrupt would
|
||||
if 'tq_isr_on' in syms and u.mem_read(syms['tq_isr_on'], 1)[0]:
|
||||
_regs = [u.reg_read(r) for r in _RSAVE]
|
||||
u.reg_write(UC_ARM_REG_SP, (_regs[13] - 64) & ~7); u.reg_write(UC_ARM_REG_LR, 0xFFF1)
|
||||
u.emu_start(syms['tq_pump'] | 1, 0xFFF0, count=200000)
|
||||
for r, v in zip(_RSAVE, _regs): u.reg_write(r, v)
|
||||
# 1.6.9 test13 T7_KEYQ: the GPIO as the keys say (pressed = 0) and the tick's press counter
|
||||
km = keys(); u.mem_write(0x40018000, struct.pack('<I', 0xFFFFFFFF & ~(((km & 1) << 9) | ((km & 2) << 10) | ((km & 4) << 8) | ((km & 8) << 5))))
|
||||
if 'g_kin' in syms:
|
||||
e = km & ~state.get('kprev', 0) & 7; state['kprev'] = km
|
||||
u.mem_write(syms['g_kprev'], bytes([km & 7]))
|
||||
for i in range(3):
|
||||
if e & (1 << i): a = syms['g_kin'] + i; u.mem_write(a, bytes([(u.mem_read(a, 1)[0] + 1) & 0xFF]))
|
||||
if si < len(SH) and state['ms'] >= SH[si] * 1000: shot('shot_%02d.png' % si); si += 1
|
||||
LL = int(os.environ.get('LAGLOG', '0')) # 1.6.13 test4: every LL ms: emulated ms in that time (100 % = real time)
|
||||
if LL and state['ms'] % LL == 0:
|
||||
_st = struct.unpack('<2I', bytes(u.mem_read(syms['g_t7st'], 8))); _p = state.get('llp', _st)
|
||||
print('lag', state['ms'], _st[0] - _p[0], _st[1] - _p[1], flush=True); state['llp'] = _st
|
||||
if os.environ.get('CLOG') and state['ms'] % int(os.environ['CLOG']) == 0:
|
||||
_w = bytes(u.mem_read(syms['g_wram'], 0x600)); _sa = int(os.environ.get('CSEC', '0x396'), 16)
|
||||
_eb = struct.unpack('<3I', bytes(u.mem_read(syms['g_eco_bad'], 4)) + bytes(u.mem_read(syms['g_eco_fix'], 8))) if 'g_eco_bad' in syms else ()
|
||||
if os.environ.get('DBGSYM'):
|
||||
for _d in os.environ['DBGSYM'].split(','):
|
||||
_s, _l = _d.split(':'); print(' ', _s, bytes(u.mem_read(syms[_s], int(_l, 0))).hex())
|
||||
print('clock', state['ms'], 'rtc', rtc0 + state['ms'] // 1000, 'game %02d:%02d:%02d' % (_w[0x306], _w[0x307], _w[_sa]), 'sh', state['sh'], 'eco_bad/fix', _eb)
|
||||
st = struct.unpack('<4I', bytes(u.mem_read(syms['g_t7st'], 16)))
|
||||
print('emulated ms', st[0], 'dropped ms', st[1])
|
||||
(os.environ.get('TONES') and print('tones:', state['tone']));print('ms', state['ms'], 'tones', len(state['tone']), 'flash writes', state['flashw'], 'back' if 'back' in state else '', 'pc', hex(pc))
|
||||
print('ms per clock step (96, 48, 24, 12 MHz):', state['shms'])
|
||||
print('power ms: full', state['pw']['full'], 'ihrc', state['pw']['ihrc'], 'deep sleep', state['pw']['sleep'])
|
||||
open(os.path.join(out, 'flash_after.bin'), 'wb').write(fl)
|
||||
for _n in os.environ.get('SYMDUMP', '').split(','):
|
||||
if ':' in _n:
|
||||
_s, _l = _n.split(':'); open(os.path.join(out, _s + '.bin'), 'wb').write(bytes(u.mem_read(syms[_s], int(_l, 0))))
|
||||
shot('shot_end.png')
|
||||
+16
-16
@@ -1,16 +1,16 @@
|
||||
# per-function instruction profile of t7.bin in Unicorn (uses devt7 setup)
|
||||
import sys, os, struct, subprocess, collections
|
||||
os.environ.setdefault('SHOTS','')
|
||||
src = open(os.path.join(os.path.dirname(__file__), 'devt7.py')).read()
|
||||
src = src.replace("while state['ms'] < total_ms", "cnt=collections.Counter()\nFROM=int(os.environ.get('FROM','0'))\ndef hc(uc,a,s,d):\n if state['ms']>=FROM: cnt[a]+=1\nu.hook_add(UC_HOOK_CODE,hc,begin=0,end=0xffff)\nwhile state['ms'] < total_ms")
|
||||
exec(compile(src, 'devt7', 'exec'))
|
||||
fn = sorted((v, k) for k, v in syms.items() if v < 0x10000)
|
||||
import bisect
|
||||
keys=[a for a,_ in fn]
|
||||
per=collections.Counter()
|
||||
for a,c in cnt.items():
|
||||
i=bisect.bisect_right(keys,a)-1; per[fn[i][1] if i>=0 else '?']+=c
|
||||
tot=sum(per.values())
|
||||
for n,c in per.most_common(25): print('%-28s %6.2f%%' % (n, 100*c/tot))
|
||||
e=syms.get('t7_cpu_step',0)&~1
|
||||
print('6502 instructions', cnt[e], 'ARM instr total', tot, 'per 6502 instr', tot/max(1,cnt[e]))
|
||||
# per-function instruction profile of t7.bin in Unicorn (uses devt7 setup)
|
||||
import sys, os, struct, subprocess, collections
|
||||
os.environ.setdefault('SHOTS','')
|
||||
src = open(os.path.join(os.path.dirname(__file__), 'devt7.py')).read()
|
||||
src = src.replace("while state['ms'] < total_ms", "cnt=collections.Counter()\nFROM=int(os.environ.get('FROM','0'))\ndef hc(uc,a,s,d):\n if state['ms']>=FROM: cnt[a]+=1\nu.hook_add(UC_HOOK_CODE,hc,begin=0,end=0xffff)\nwhile state['ms'] < total_ms")
|
||||
exec(compile(src, 'devt7', 'exec'))
|
||||
fn = sorted((v, k) for k, v in syms.items() if v < 0x10000)
|
||||
import bisect
|
||||
keys=[a for a,_ in fn]
|
||||
per=collections.Counter()
|
||||
for a,c in cnt.items():
|
||||
i=bisect.bisect_right(keys,a)-1; per[fn[i][1] if i>=0 else '?']+=c
|
||||
tot=sum(per.values())
|
||||
for n,c in per.most_common(25): print('%-28s %6.2f%%' % (n, 100*c/tot))
|
||||
e=syms.get('t7_cpu_step',0)&~1
|
||||
print('6502 instructions', cnt[e], 'ARM instr total', tot, 'per 6502 instr', tot/max(1,cnt[e]))
|
||||
Reference in new issue
Block a user