줄바꿈을 LF로 통일
서버에서 바로 빌드하고 고칠 수 있게 저장소 줄바꿈을 LF로 맞췄습니다. CRLF로 들어가 있던 sh 빌드 스크립트도 이제 리눅스에서 그대로 돌아갑니다. 내용은 바뀌지 않았습니다.
This commit is contained in:
1 parent
40615838ac
commit
2d48091205
623 files changed
+501372
-501365
No files matched your search
+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
|
||||
Reference in new issue
Block a user