다마고치 연구소 한국어판 소스 공개
This commit is contained in:
commit
db23855630
661 files changed
+503597
No files matched your search
@@ -0,0 +1,48 @@
|
||||
#!/bin/sh
|
||||
# Build the T18 app for the Paradise (arm-none-eabi-gcc, Cortex-M3).
|
||||
# Output: ../../t18.bin (PRAM part, loaded by the stub) and ../../t18x.bin (flash part, at T18_XIP_OFF).
|
||||
set -e
|
||||
cd "$(dirname "$0")"
|
||||
E=../../../emu/src/emu
|
||||
CC=${CC:-arm-none-eabi-gcc}
|
||||
SRC="main.c t18app.h link.ld $E/hw.c $E/ask.c $E/ask.h $E/flash.c $E/libc.c $E/paraclock.h ../core/t18cpu.c ../core/t18cpu.h ../core/t18soc.c ../core/t18soc.h ../core/t18jit.c ../core/t18hle.c ../core/t2enc.h"
|
||||
BUILD=$(cat $SRC | cksum | cut -d' ' -f1)
|
||||
# 1.6.6: the switches that proved themselves are the default; T18_TEST=1 keeps the code screens and the speed display
|
||||
REL="-DT18_HOTWIN -DT18_ROWSUM_RAM20 -DT18_JHDR -DT18_JH_SPLIT=0x20000100u -DT18_PRESENT_IDLE -DT18_PRESENT_STABLE -DT18_SUM_FNV -DT18_ROW_RR -DT18_PRES_FIRST -DT18_WAKE_ASYNC -DWAKE_ASYNC -DT18_SF_FAST -DT18_HOLE_FAST -DT18_TRY_SEQ -DT18_MEM_HELPER -DT18_FLUSH_IDLE -DT18_FF_FAST -DT18_IDLE_RUN -DT18_COMPACT_ON=40000000u -DT18_COMPACT_OFF=200000000u"
|
||||
# 1.6.8: the switches of the test builds 1.6.7-test10..13 (measured on the device: egg 45 -> 63 %, call 56 -> 73 %)
|
||||
REL="$REL -DT18_STM_SPLIT -DT18_SF_ASM -DT18_CHK_FLASH -DT18_CHK_HOLE -DT18_GFAIL_SHARED -DT18_SEQ_FAST -DT18_JT2 -DT18_SPCHK_LD -DT18_CYC32 -DT18_OVERSHOOT"
|
||||
# 1.6.8 (test40): switches measured in the M3 test bed (egg -7 %, call -11 %, pet -5 %; same results as the reference)
|
||||
REL="$REL -DT18_IO_FAST -DT18_SPCHK_MOD -DT18_TIERUP -DT18_IDLE_CAP=256 -DT18_IDLE_DISARM=32 -DT18_IDLE_PASS -DT18_XRET -DT18_CLOCK_AFTER -DPARA_BACK -DPARA_MB_KEEP -DPARA_FLASH -DPARA_FLASH_LATE -DPARA_CLEAN_CLK -DT18_CLK_DISC -DT18_MB_FIRST -DT18_LCD_FAST -DT18_PREWAKE -DT18_WAKE_KEEP -DT18_DARK_PEEK -DT18_PRES_SWEEP -DT18_NOTE_REL -DT18_CARE_TOOTH -DT18_MENU_QUIET -DHW_WAKE_ABORT -DT18_RADIO -DBL_PWM -DBL_MR3RST -DT18_TONE_Q -DT18_IDLE_MEMO -DT18_IDLE_BACKOFF=13 -DT18_JH_PROBE=16 -DT18_JT_MUL -DT18_SEC_SPARE -DT18_GROW -DT18_GROW2 -DT18_WAKE_START -DT18_DAY_FIX -DT18_LINK -DASK_EXTRA -DBAT_SHOW -DCLK_ECO -DECO_LAG=120 -DT18_WHEEL_WAKE -DT18_POOL_RECLAIM -DT18_CARE -DASK_NUM -DT18_PW -DT18_WHEEL_RAW -DT18_LOOP_XIP -DT18_KNOWN -DT18_SPEC -DT18_PW_RTCWAIT -DT18_PRESENT_CONFIRM -DT18_RAW_LABEL -DT18_CATCHUP_SMOOTH -DT18_PRESENT_S32 -DT18_CPU_SLOW=5 -DT18_CPU_ADAPT -DT18_IO_DIRECT -DT18_IO_FASTW -DT18_BL_STRICT -DASK_DIM -DASK_NEVER -DT18_DIM -DT18_CALL_LIGHT -DBAT_SMOOTH -DASK_LANG -DT18_LANG -DT18_ON_FULL -DT18_KEY_NOIDLE -DT18_KEY_FRESH=300 -DT18_KEY_OFFFF -DT18_KEY_LATCH -DT18_SWALLOW_LIT -DT18_KEY_FRESH_LIT -DT18_S32_EMU -DT18_S32_MS=72u -DBAT_GUARD -DBAT_PIC"
|
||||
# 1.7.4 test3: the firmware's picture fill natively (core/t18hle.c, T18_HLE_FILL; T18_HLE_OS: compiled small,
|
||||
# more room for translated code). Its state is in the hole: the stack 0x5B0 -> 0x560 (HLE path about 900 bytes deep).
|
||||
# 1.7.4 test4: T18_PRES_FILL (a finished fill goes out as a picture: the walk to the park no longer stands still) and the
|
||||
# game CPU at its full clock again (T18_CPU_SLOW 5 -> 4; the fill no longer costs the Paradise much, CPU_ADAPT still
|
||||
# slows it when the Paradise falls behind)
|
||||
[ -n "$T18_NO_HLE" ] || { REL="$(echo "$REL" | sed 's/-DT18_CPU_SLOW=5/-DT18_CPU_SLOW=4/') -DT18_HLE_FILL -DT18_HLE_OS -DT18_PRES_FILL -DT18_MENU_FILL -DT18_WAKE_FILL -DT18_PEEK_RAW"; STACKSZ=${STACKSZ:-0x560}; }
|
||||
[ -n "$T18_TEST" ] || REL="$REL -DT18_QUIET_FATAL -DT18_NO_SPEED"
|
||||
[ -n "$T18_TEST" ] && REL="$REL -DT18_TEST_KEYS"
|
||||
[ -n "$T18_PLAIN" ] && REL=""
|
||||
JHB=${JHB:-9}; T18_XIP_MORE=${T18_XIP_MORE-1}; JTB=${JTB:-9}; JHOT=${JHOT:-4} # 1.6.8: as in the test builds 10..13
|
||||
CF="-mcpu=cortex-m3 -mthumb -ffreestanding -nostdlib -fno-common -ffunction-sections -fdata-sections -Wall -Wno-unused-function -DPARADISE -DT18_JIT -DT18_JC_LINKED -DT18_PRAM_WR -DT18_PRAM_WR_SRAM -DJH_BITS=${JHB:-8} -DJT_BITS=${JTB} -DJ_HOT=${JHOT} -DT18_BUILD=${BUILD}u -DFAST_LCD -DEXIT_KEY -DLCD_FASTCLK -DTONE_FIX -DHW_TONE_RESTART -DBL_SLEEP -DHW_OPTS -DSESS_ONCE -DSESS_AUTO -DOPT_MENU -DSESS_FLAG -I../core $REL $XCF"
|
||||
O=""
|
||||
for f in main.c $E/hw.c $E/ask.c $E/flash.c $E/libc.c ../core/t18cpu.c ../core/t18soc.c ../core/t18jit.c; do
|
||||
n=$(basename $f .c); OPT=-Os; [ "$n" = t18cpu ] && OPT=-O2; [ "$n" = libc ] && OPT="-Os -fno-builtin -fno-tree-loop-distribute-patterns"
|
||||
$CC $CF $OPT -c $f -o $n.o; O="$O $n.o"; done
|
||||
# T18_XIP_MORE=1: rarely run code into the flash window as well (bigger code buffer)
|
||||
# idle_try stays in the PRAM (1.6.6, T18_IDLE_RUN). 1.7.4 test3 (T18_ARM_PRAM=1 for the old place): the ARM interpreter
|
||||
# (exec_arm, 5.4 KB) into the flash window: a few thousand ARM instructions a second are interpreted, the rest is
|
||||
# translated; the PRAM gained is room for translated code (M3: room 61 -> 58 Minstr/s, with T18_HLE_FILL 42 -> 32)
|
||||
[ -n "$T18_ARM_PRAM" ] && XM_DEF=.text.arm7_step || XM_DEF=".text.arm7_step .text.exec_arm"
|
||||
XM_CPU=${XM_CPU:-$XM_DEF}
|
||||
if [ -n "$T18_XIP_MORE" ]; then MORE='*ask.o(.text* .rodata*) *t18soc.o(.text.io_wr .text.io_rd .text.rq_push .text.rd32le .text.tg18_run_cycles .text.tmr_io_rd .text.tmr_io_wr) *t18cpu.o('"$XM_CPU"') *libgcc.a:_udivmoddi4.o(.text*)'; else MORE=''; fi
|
||||
sed "s|XIPMORE|$MORE|;s|JTSIZE|$((8 << JTB))|;s|STACKSZ|${STACKSZ:-0x5B0}|" link.ld > link_gen.ld
|
||||
$CC $CF -T link_gen.ld -Wl,--gc-sections -Wl,-Map=t18.map $O -lgcc -o t18.elf
|
||||
# test43 fix: what runs before the flash window is set up (Reset_Handler up to hw_init) must be in the PRAM
|
||||
for f in Reset_Handler para_clock para_mailbox para_leave_mark para_days hw_init; do
|
||||
a=$(arm-none-eabi-nm t18.elf | awk -v f=$f '$3==f || index($3, f".")==1 {print $1; exit}')
|
||||
[ -z "$a" ] || [ $((0x$a)) -lt $((0x10000000)) ] || { echo "build.sh: $f is in the flash window (0x$a), it runs before the window is set up"; exit 1; }
|
||||
done
|
||||
arm-none-eabi-objcopy -O binary -j .text -j .data -j .init t18.elf t18.bin
|
||||
arm-none-eabi-objcopy -O binary -j .xip t18.elf t18x.bin
|
||||
cp t18.bin ../../t18.bin; cp t18x.bin ../../t18x.bin; rm -f *.o t18.bin t18x.bin link_gen.ld; [ -n "$KEEPELF" ] || rm -f t18.elf t18.map
|
||||
ls -l ../../t18.bin ../../t18x.bin
|
||||
@@ -0,0 +1,45 @@
|
||||
/* T18 app on the Paradise. The RAM at 0x20000000 has only 4 KB (the Paradise firmware's), so:
|
||||
* PRAM 0x00000000 (64 KB, loaded by the stub): hot code; from __jc_start on (over the .data image and
|
||||
* the start code) the code buffer of the translator.
|
||||
* flash window (0x10000000 + flash offset - 0x11000): the rest of the code, flash T18_XIP_OFF (0xF0000).
|
||||
* SRAM hole 0x1800E600..0x18014400: stack (bottom), data. The rest of the SRAM is the guest's. */
|
||||
ENTRY(Reset_Handler)
|
||||
MEMORY {
|
||||
PRAM (rwx) : ORIGIN = 0x00000000, LENGTH = 0x10000
|
||||
XIP (rx) : ORIGIN = 0x100DF000, LENGTH = 0x10000
|
||||
HOLE (rw) : ORIGIN = 0x1800E600, LENGTH = 0x5E00
|
||||
}
|
||||
SECTIONS {
|
||||
.xip : {
|
||||
__xip_magic = .;
|
||||
KEEP(*(.xipmagic))
|
||||
*(.xiptext*)
|
||||
*t18jit.o(.text.translate_thumb .text.translate_arm .text.t_decode .text.a_decode .text.emit_* .text.block_begin .text.block_end* .text.modimm_up .text.t2_* .text.get_reg .text.set_reg .text.flush .text.dp_operand* .text.a_shift* .text.cond_reads)
|
||||
*t18soc.o(.text.tg18_init .text.soc_reset .text.tg18_save_state .text.tg18_load_state .text.tg18_state_size .text.tg18_set_time .text.tg18_get_time .text.tg18_advance_offline .text.reg_clear .text.tg18_set_addrs .text.tg18_get_addrs .text.tg18_set_present .text.days_from_civil .text.find_idle .text.tg18_struct_size .text.tg18_sf_regp .text.tg18_sf_ctlp .text.tg18_sf_outp)
|
||||
*t18cpu.o(.text.arm7_init .text.arm7_reset .text.arm7_save .text.arm7_load .text.build_cond .text.t18_sram_clear)
|
||||
/* 1.6.6: rarely run, out of the PRAM for a bigger code buffer (T18_XIP_MORE) */
|
||||
XIPMORE
|
||||
*(.rodata.t18_zero_pg)
|
||||
. = ALIGN(4);
|
||||
__xip_end = .;
|
||||
} > XIP
|
||||
.text : {
|
||||
KEEP(*(.vectors))
|
||||
KEEP(*(.pmxcfg))
|
||||
*(.text*)
|
||||
*(.rodata*)
|
||||
. = ALIGN(4);
|
||||
} > PRAM
|
||||
.stack (NOLOAD) : { __stack_bottom = .; . += STACKSZ; __stack_top = .; } > HOLE /* build.sh STACKSZ (1.7.4: 0x560 with T18_HLE_FILL); 1.6.13 test6: 0x600 -> 0x5E0 (804 bytes used, measured); test21: 0x5D0 (16 bytes for BAT_GUARD); 1.7.2 test16: 0x5C0 (16 bytes for T18_CLK_DISC); test19: 0x5B0 (T18_WAKE_KEEP) */
|
||||
.data : { __data_start = .; *(.ramfunc*) *(.data*) . = ALIGN(4); __data_end = .; } > HOLE AT > PRAM
|
||||
__data_load = LOADADDR(.data);
|
||||
.init : { KEEP(*(.initcode)) KEEP(*(.initdata*)) KEEP(*(.initcode_z)) . = ALIGN(4); } > PRAM
|
||||
__prog_end = .;
|
||||
__jc_start = ALIGN(__data_load, 4);
|
||||
__jt_start = 0x10000 - JTSIZE; /* the translator's table for indirect jumps (8 << JT_BITS bytes, build.sh) */
|
||||
__jc_end = __jt_start;
|
||||
.bss (NOLOAD) : { __bss_start = .; *(.bss*) *(COMMON) . = ALIGN(4); __bss_end = .; } > HOLE
|
||||
ASSERT(__prog_end <= 0xC000, "PRAM: too little room left for the translated code")
|
||||
ASSERT(__xip_end - 0x100DF000 <= 0x10000, "flash part too big")
|
||||
/DISCARD/ : { *(.comment) *(.ARM.*) *(.note*) }
|
||||
}
|
||||
+2394
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,47 @@
|
||||
/* T18 (Meets/On, TG18 firmware) on the Tamagotchi Paradise: settings block and flash layout. */
|
||||
#ifndef T18APP_H
|
||||
#define T18APP_H
|
||||
#include <stdint.h>
|
||||
|
||||
/* Settings block "PMX8" at 0x140 in the program, filled in by the Studio (src/emu.js, t18Patch).
|
||||
* The firmware lies in the Paradise flash in 64 KB blocks wherever there was room: map[i] is the
|
||||
* Paradise block (offset >> 16) of guest block i, 0xFFFF = erased (reads 0xFF). Blocks the guest
|
||||
* writes later (its save data) come from the spare list, which the Studio leaves erased. */
|
||||
#define T18_MAGIC 0x38584D50u /* "PMX8" */
|
||||
#define T18_SPARE_MAX 64
|
||||
typedef struct {
|
||||
uint32_t magic, version;
|
||||
uint32_t save_off; /* Paradise flash: map sector, settings, session (see main.c) */
|
||||
uint32_t nspare;
|
||||
uint16_t map[128];
|
||||
uint16_t spare[T18_SPARE_MAX];
|
||||
uint32_t flags; /* T18F_* */
|
||||
uint32_t idle_addr, softrtc, dma_tab; /* found by the Studio in the firmware (tg18_set_addrs) */
|
||||
uint32_t wall_cnt, wall_sod, wall_ymd; /* written by the Studio on the device: seconds counter, local time, date */
|
||||
uint32_t param_addr; /* 1.6.8 (test47): the game's Param block in the guest SRAM (0xF800....), 0 = unknown */
|
||||
uint32_t adult_flags; /* bit 0: Sanrio firmware (Flowertchi instead of Pompomtchi) */
|
||||
uint32_t pic_off; /* pictures for grow up ("T8PI", the user's own, see src/emu.js), 0 = none */
|
||||
} T18Cfg;
|
||||
|
||||
#define T18F_EN 1u /* English texts */
|
||||
#define T18F_CLOCK 2u /* at every start the clock is set to the Paradise's time (nothing caught up) */
|
||||
#define T18F_HOTWIN 4u /* the Studio put the hot firmware blocks into 0xF00000..0xFFFFFF and a second
|
||||
* copy of the flash part at T18_XIP_HOT (see main.c, hot window) */
|
||||
#define T18F_NOSESS 0x1000u /* 1.7.3 test7: no session (SESS_FLAG): only the firmware's own save is written */
|
||||
|
||||
/* code that is not hot: in the flash at T18_XIP_OFF, seen in the window 0x10000000 (base 0x60011000);
|
||||
* it begins with T18_XIP_MAGIC and the build number of the program (T18_BUILD) */
|
||||
#define T18_XIP_OFF 0xF0000u
|
||||
#define T18_XIP_MAX 0x10000u
|
||||
#define T18_XIP_MAGIC 0x58383154u /* "T18X" */
|
||||
/* hot window (1.6.6): the cached flash window shows 1 MB from its base. With base 0x60F00000 it shows
|
||||
* 0xF00000..0xFFFFFF; the flash part is then needed at 0xFDF000 (same link address 0x100DF000) */
|
||||
#define T18_HOT_BASE 0xF00000u
|
||||
#define T18_XIP_HOT (T18_HOT_BASE + T18_XIP_OFF - 0x11000u)
|
||||
|
||||
/* Paradise flash from save_off */
|
||||
#define T18_MAP_SEC 0x0000u /* "T8MP": the map as it is now, with the spares in use */
|
||||
#define T18_NOTE_SEC 0x1000u /* "T8NT": seconds counter and game time when leaving */
|
||||
#define T18_SESS_SEC 0x2000u /* "T8SS": CPU and SoC state, then the SRAM (32 sectors) */
|
||||
#define T18_SAVE_LEN 0x24000u
|
||||
#endif
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,129 @@
|
||||
/* ARM7TDMI interpreter for T18 on the Paradise (instruction semantics from the T18Emu core). */
|
||||
#ifndef T18CPU_H
|
||||
#define T18CPU_H
|
||||
#include <stdint.h>
|
||||
|
||||
/* Guest SRAM 0xF8000000, 128 KB (the TG18 window is 136 KB, but nothing above 128 KB is ever used:
|
||||
* reads 0). It is direct memory at t18_sram (on the Paradise the Paradise SRAM itself, host = guest +
|
||||
* 0x20000000), except for the hole [T18_HOLE_LO, T18_HOLE_HI): there the host memory holds the
|
||||
* emulator's own data (stack, state, tables). The guest's bytes in the hole are kept in pages of
|
||||
* 512 bytes that are made on the first write of a non-zero value (until then they read 0). All five
|
||||
* TG18 firmwares only zero this range at the start and use a few hundred bytes of it later; both
|
||||
* stacks lie above it. */
|
||||
#define T18_SRAM_SIZE 0x20000u
|
||||
#if defined(PARADISE) && !defined(T18_HOLE_LO)
|
||||
#define T18_HOLE_LO 0xE600u
|
||||
#define T18_HOLE_HI 0x14400u
|
||||
#endif
|
||||
#ifndef T18_HOLE_LO
|
||||
#define T18_HOLE_LO 0u
|
||||
#define T18_HOLE_HI 0u
|
||||
#endif
|
||||
#define T18_HOLE_PG 512u
|
||||
#define T18_HOLE_N ((T18_HOLE_HI - T18_HOLE_LO) / T18_HOLE_PG)
|
||||
static inline int t18_hole(uint32_t o) { return o - T18_HOLE_LO < T18_HOLE_HI - T18_HOLE_LO; }
|
||||
/* a range [o, o + n) that touches the hole */
|
||||
static inline int t18_hole_n(uint32_t o, uint32_t n) { return T18_HOLE_HI > T18_HOLE_LO && o < T18_HOLE_HI && o + n > T18_HOLE_LO; }
|
||||
|
||||
typedef struct arm7 arm7_t;
|
||||
typedef uint32_t (*arm7_rd_fn)(void *ctx, uint32_t addr, int size);
|
||||
typedef void (*arm7_wr_fn)(void *ctx, uint32_t addr, uint32_t val, int size);
|
||||
|
||||
struct arm7 {
|
||||
uint32_t r[16];
|
||||
uint32_t cpsr;
|
||||
uint32_t spsr; /* current mode's spsr */
|
||||
uint32_t bank_usr[7]; /* r8..r14 for usr/sys */
|
||||
uint32_t bank_fiq[7]; /* r8..r14 fiq */
|
||||
uint32_t bank_irq[2], bank_svc[2], bank_abt[2], bank_und[2];
|
||||
uint32_t spsr_fiq, spsr_irq, spsr_svc, spsr_abt, spsr_und;
|
||||
uint64_t cycles; /* master ticks */
|
||||
uint64_t idle_cycles;
|
||||
uint32_t cyc_mult; /* master ticks per CPU cycle */
|
||||
int irq_line, fiq_line;
|
||||
int halted;
|
||||
uint32_t vector_base;
|
||||
void *ctx;
|
||||
arm7_rd_fn rd;
|
||||
arm7_wr_fn wr;
|
||||
/* store notify range (picture buffer), called before the store */
|
||||
uint32_t note_lo, note_hi;
|
||||
void (*note_fn)(void *ctx, uint32_t addr);
|
||||
uint32_t last_pc;
|
||||
int fault;
|
||||
int attn; /* irq_line | fiq_line | halted, after every device access */
|
||||
uint32_t *jtab; /* translator: return cache (pc, code) x 256 */
|
||||
uint32_t jsave; /* translator: scratch word */
|
||||
uint32_t jsplo; /* translator: host address of the lowest guest sp blocks may use (1.6.8) */
|
||||
/* idle loops: skip whole rounds of a loop that changes nothing */
|
||||
uint64_t idle_next; /* next check (master ticks) */
|
||||
uint32_t idle_fail;
|
||||
int watch; /* 1 while one round is watched */
|
||||
int (*io_stable)(void *ctx, uint32_t a); /* reading a gives the same value until the next event */
|
||||
uint64_t idle_skipped;
|
||||
uint32_t idle_pc; /* pc where a round last held still (0 = none yet) */
|
||||
uint64_t idle_win; /* target of the run in which idle_pc was already tried */
|
||||
};
|
||||
|
||||
extern uint8_t *t18_sram; /* guest SRAM, 128 KB (the hole part is not the guest's) */
|
||||
extern uint8_t *t18_hole_pg[T18_HOLE_N + 1]; /* pages of the hole, 0 = not made yet (reads 0) */
|
||||
extern uint8_t *(*t18_hole_new)(void); /* platform: a zeroed page of 512 bytes, 0 = none left */
|
||||
extern int t18_oom; /* a write into the hole was lost (no page left) */
|
||||
const uint8_t *t18_sram_rp(uint32_t o); /* guest SRAM offset -> byte, valid up to the end of its 512-byte page */
|
||||
uint8_t *t18_sram_wp(uint32_t o, int make); /* same for writing; 0 when it is a hole page not made (make: make it) */
|
||||
uint32_t t18_sram_rd(uint32_t o, int size);
|
||||
void t18_sram_wr(uint32_t o, uint32_t v, int size); /* makes the hole page only for a non-zero value */
|
||||
void t18_sram_clear(void); /* all zero, hole pages given up (the platform takes them back) */
|
||||
extern void (*t18_hole_free)(void); /* platform: all hole pages are free again */
|
||||
extern const uint8_t *t18_fblk[128];
|
||||
|
||||
void arm7_init(arm7_t *c);
|
||||
void arm7_reset(arm7_t *c, uint32_t pc);
|
||||
int64_t arm7_run(arm7_t *c, uint64_t target);
|
||||
int arm7_step(arm7_t *c);
|
||||
extern int t18_idle_on;
|
||||
void arm7_exception(arm7_t *c, uint32_t mode, uint32_t vec, uint32_t ret);
|
||||
uint32_t arm7_read32(arm7_t *c, uint32_t a);
|
||||
uint16_t arm7_read16(arm7_t *c, uint32_t a);
|
||||
uint8_t arm7_read8(arm7_t *c, uint32_t a);
|
||||
void arm7_write32(arm7_t *c, uint32_t a, uint32_t v);
|
||||
void arm7_write16(arm7_t *c, uint32_t a, uint32_t v);
|
||||
void arm7_write8(arm7_t *c, uint32_t a, uint8_t v);
|
||||
void arm7_set_mode(arm7_t *c, uint32_t mode);
|
||||
|
||||
#define ARM7_T (1u<<5)
|
||||
#define ARM7_F (1u<<6)
|
||||
#define ARM7_I (1u<<7)
|
||||
|
||||
typedef struct {
|
||||
uint32_t r[16], cpsr, spsr;
|
||||
uint32_t bank_usr[7], bank_fiq[7], bank_irq[2], bank_svc[2], bank_abt[2], bank_und[2];
|
||||
uint32_t spsr_fiq, spsr_irq, spsr_svc, spsr_abt, spsr_und;
|
||||
uint64_t cycles;
|
||||
int32_t halted, pad;
|
||||
} arm7_state_t;
|
||||
void arm7_save(arm7_t *c, arm7_state_t *s);
|
||||
void arm7_load(arm7_t *c, const arm7_state_t *s);
|
||||
#ifdef T18_HLE_FILL
|
||||
extern uint32_t t18_hle_pc, t18_hle_n; /* 1.7.4: the firmware's picture fill natively (t18hle.c) */
|
||||
int t18_hle_find(void);
|
||||
extern volatile uint32_t t18_hle_done;
|
||||
int t18_hle_busy(void);
|
||||
#endif
|
||||
#ifdef T18_RAW_LABEL
|
||||
/* 1.6.13 test4: the firmware's label routine (Thumb "strh r0,[r5]" after a call of its per-pixel routine) writes
|
||||
* colour table numbers into the picture first (blue box behind the name on a call); the panel keeps what was there
|
||||
* at those pixels until the full picture draws them (as the Android T18Emu). t18_raw_pc: that strh (1 = unknown). */
|
||||
extern uint32_t t18_raw_pc;
|
||||
extern volatile uint32_t t18_raw_n, t18_raw_lo, t18_raw_hi; /* stores seen, lowest and highest address */
|
||||
extern uint16_t t18_raw_v[4]; /* values written (a few colour table numbers) */
|
||||
#endif
|
||||
|
||||
#ifdef T18_JIT
|
||||
int t18_jit_try(arm7_t *c, uint64_t target);
|
||||
extern int t18_jit_off;
|
||||
int t18_jit_code_line(uint32_t sram_off);
|
||||
int t18_jit_code_range(uint32_t sram_off, uint32_t len);
|
||||
void t18_jit_written(arm7_t *c, uint32_t a, uint32_t n);
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,427 @@
|
||||
/* T18_HLE_FILL (1.7.4): the firmware's picture fill natively. Included by t18cpu.c.
|
||||
* The firmware draws its picture with a Thumb routine in its SRAM (fairy v057: 0xF80061D8): for every pixel of a
|
||||
* rectangle it calls a per-pixel routine that walks all layers from the top (20-byte records), samples the first
|
||||
* one that covers the pixel and is not transparent, looks the colour up in three tables, blends the upper part
|
||||
* of the screen in some scenes and stores RGB565. That was more than half of all guest instructions (more with
|
||||
* twins: more layers). Here the same in C, with the layers of each row picked out once.
|
||||
* Two forms of the per-pixel routine are known: kind 1 (fairy, magic, sanrio: the sampling in its own routine)
|
||||
* and kind 2 (wondergarden, fantasy, pastel, sweets: inlined, the layer list 4 bytes earlier); both do the same.
|
||||
* Same result as the firmware's code (checked against it in host/ref.c, T18_HLE_CHECK). Guest cycles are charged
|
||||
* as the firmware's code would have used them (counters, fitted against the interpreter). Runs in slices up to
|
||||
* the run's target: the guest pc stays at the routine until it is done; an interrupt in between returns to it
|
||||
* and the fill goes on where it stopped.
|
||||
* Author: mameise */
|
||||
#ifndef T18_HLE_MAXL
|
||||
#define T18_HLE_MAXL 64
|
||||
#endif
|
||||
uint8_t t18_flash_rd(uint32_t a);
|
||||
uint32_t t18_hle_pc = 1u; /* the fill routine (Thumb, bit 0 clear); 1 = not found */
|
||||
#if defined(T18_HLE_CHECK) || defined(T18_HLE_COUNT)
|
||||
uint32_t t18_hle_n; /* fills done natively */
|
||||
#endif
|
||||
/* what the search found and where a fill stopped */
|
||||
static struct {
|
||||
uint32_t L, fbv, tr, lo; /* layer list, picture pointer, red table (green +0x200, blue +0x400), blend range */
|
||||
uint8_t toff, kind, cnt, rec; /* table offset, form, layer count and first record in the list */
|
||||
uint32_t sp, lr, a0, a1, a2, a3; /* the call in progress */
|
||||
uint16_t idx; uint8_t y, x, on;
|
||||
} hv;
|
||||
static int32_t hf_hit;
|
||||
/* share of the firmware's cycles a native fill is charged with, in 1/16 (16 = as the firmware's code) */
|
||||
#ifndef T18_HLE_Q
|
||||
#define T18_HLE_Q 16u
|
||||
#endif
|
||||
uint32_t t18_hle_q = T18_HLE_Q;
|
||||
volatile uint32_t t18_hle_done; /* fills finished (T18_PRES_FILL in main.c: a finished picture goes out) */
|
||||
int t18_hle_busy(void) { return hv.on; }
|
||||
/* guest byte reads (images in the flash, tables in the SRAM): one copy of rd8, not one per use */
|
||||
static __attribute__((noinline)) uint32_t hr8(arm7_t *c, uint32_t a) { return rd8(c, a); }
|
||||
/* image bytes: straight from the flash block when it is mapped (as mp_r), else as the guest reads them */
|
||||
static inline uint32_t im8(arm7_t *c, uint32_t a)
|
||||
{
|
||||
uint32_t o = a - 0x20000000u;
|
||||
if (o < 0x800000u) { const uint8_t *b = t18_fblk[o >> 16]; if (b) return b[o & 0xFFFFu]; }
|
||||
return hr8(c, a);
|
||||
}
|
||||
|
||||
static inline uint32_t g16(const uint8_t *p) { return (uint32_t)p[0] | (uint32_t)p[1] << 8; }
|
||||
static inline uint32_t g32(const uint8_t *p) { return g16(p) | g16(p + 2) << 16; }
|
||||
static inline const uint8_t *sp_(uint32_t a) { return t18_sram + (a - 0xF8000000u); }
|
||||
static inline uint32_t lit(uint32_t ins) { uint32_t op = g16(sp_(ins)); return g32(sp_(((ins + 4u) & ~3u) + (op & 0xFFu) * 4u)); }
|
||||
static inline uint32_t bl_tgt(uint32_t a) { const uint8_t *p = sp_(a); uint32_t h = g16(p), l = g16(p + 2); int32_t off = (int32_t)((h & 0x7FFu) << 21) >> 9; return a + 4u + (uint32_t)off + ((l & 0x7FFu) << 1); }
|
||||
|
||||
#ifdef PARADISE
|
||||
#define HLE_COLD __attribute__((section(".xiptext"), noinline))
|
||||
#else
|
||||
#define HLE_COLD
|
||||
#endif
|
||||
/* code fingerprint (halfwords; BL offsets, literal places and small ldrb offsets left out): FNV-1a */
|
||||
HLE_COLD static uint32_t fp_code(uint32_t a, uint32_t len)
|
||||
{
|
||||
uint32_t h = 2166136261u; const uint8_t *p = sp_(a);
|
||||
for (uint32_t i = 0; i < len; i += 2) {
|
||||
uint32_t op = g16(p + i);
|
||||
if ((op & 0xF800u) == 0xF000u) { i += 2; op = 0xF000u; }
|
||||
else if ((op & 0xF800u) == 0x4800u) op &= 0xF800u;
|
||||
else if ((op & 0xF800u) == 0x7800u && (op & 0x7C0u) <= 0x200u) op &= 0xF83Fu;
|
||||
h = (h ^ op) * 16777619u;
|
||||
}
|
||||
return h;
|
||||
}
|
||||
/* fingerprints of the firmwares seen (T18_HLE_FP prints them) */
|
||||
#define HF_FP_FILL 0x9B253DFAu /* the fill (both kinds) */
|
||||
#define HF_FP_PIX1 0xC86488CFu /* kind 1: per-pixel routine and its sampling routine */
|
||||
#define HF_FP_SMP1 0xED689851u
|
||||
#define HF_FP_PIX2 0xAB5ABE09u /* kind 2: per-pixel routine with the sampling inside */
|
||||
|
||||
/* counters of what the firmware's code would have walked through (fitted in host/ref.c) */
|
||||
enum { K_PIX, K_ROW, K_CALL, K_ST1, K_ST2, K_ST3, K_BLEND, K_NOHIT, K_XN, K_XB, K_FBP, K_S0, K_SH,
|
||||
K_SW, K_YF, K_XF, K_B8, K_B4O, K_B4E, K_B10, K_B10Y, K_B10X, K_B10R, K_DIV, K_Z, K_H, K_DN, K_DB, K_N };
|
||||
#ifdef T18_HLE_CHECK
|
||||
uint32_t hk[K_N];
|
||||
#define HK(k) (hk[k]++)
|
||||
#else
|
||||
#define HK(k) do { } while (0)
|
||||
#endif
|
||||
/* guest cycles per counter and kind: fitted against the interpreter (host/ref.c T18_HLE_CHECK, all 9 firmwares,
|
||||
* about 1000 fills: 0.05 % off on average); NOHIT and H did not occur: estimates */
|
||||
static const uint16_t HCT[2][K_N] = {
|
||||
/* PIX ROW CALL ST1 ST2 ST3 BLEND NOHIT XN XB FBP S0 SH SW YF XF B8 B4O B4E B10 B10Y B10X B10R DIV Z H DN DB */
|
||||
{ 0, 0, 0, 0, 0, 7, 122, 224, 75, 89, 322, 166, 390, 0, 21, 5, 0, 37, 0, 0, 2, 0, 0, 64, 16,25,43,56 },
|
||||
{ 0, 0, 0, 0, 0, 7, 122, 224, 70, 86, 326, 154, 385, 0, 21, 5, 0, 29, 0, 0, 2, 0, 0, 61, 18,25,36,49 },
|
||||
};
|
||||
#define HC(k) (hc[k])
|
||||
#define HKC(k) do { HK(k); *cy += HC(k); } while (0)
|
||||
|
||||
/* sampling of one layer at (x, y) inside it, as the firmware (layers turned by 90 degrees, rows with many layers) */
|
||||
HLE_COLD static uint32_t smp(arm7_t *c, const uint8_t *e, uint32_t x, uint32_t y, uint32_t *cy, const uint16_t *hc)
|
||||
{
|
||||
uint32_t img = g32(e), f = g16(e + 14);
|
||||
if (f & 0x40u) { uint32_t t = x; x = y; y = t; HKC(K_SW); }
|
||||
if (f & 0x20u) { y = (e[9] - 1u - y) & 0xFFu; HKC(K_YF); }
|
||||
if (f & 0x10u) { x = (e[8] - 1u - x) & 0xFFu; HKC(K_XF); }
|
||||
uint32_t off = g16(e + 16);
|
||||
if (f & 0x8000u) { HKC(K_B8); return im8(c, img + x + y * im8(c, img) + off + 6u); }
|
||||
if (f & 0x400u) { HK(K_B10); if (!y) { HKC(K_B10Y); if (x <= 4u) { HKC(K_B10X); if (f & 0x800u) { HK(K_B10R); return 0; } } } }
|
||||
uint32_t b = im8(c, img + y * (im8(c, img) >> 1) + off + (x >> 1) + 6u);
|
||||
if (x & 1u) { HKC(K_B4O); return b >> 4; }
|
||||
HK(K_B4E); return b & 15u;
|
||||
}
|
||||
|
||||
/* per row: the layers that cover it (top first), each with what its sampling in this row needs, and the cycles of
|
||||
* the other layers the walk passes above each. More than HLE_MAXR such layers: the row goes the slow way. */
|
||||
#ifndef HLE_MAXR
|
||||
#define HLE_MAXR 24
|
||||
#endif
|
||||
enum { M8 = 1, MXF = 2, MB10 = 4, MB11 = 8, MFB = 16, MDIV = 32, MSLOW = 64 };
|
||||
typedef struct { const uint8_t *p; uint32_t ga; int16_t lx; uint16_t pre; uint8_t w, m, i, sc; } hent_t;
|
||||
typedef struct { uint8_t n, slow; uint16_t tail; hent_t e[HLE_MAXR]; } hrow_t;
|
||||
|
||||
static inline uint32_t rej_kind(const uint8_t *e, uint32_t y)
|
||||
{
|
||||
if (!g32(e)) return K_Z;
|
||||
if (g16(e + 14) & 0x4000u) return K_H;
|
||||
int32_t dy = (int16_t)(uint16_t)(y - g16(e + 6));
|
||||
if (dy < 0) return K_DN;
|
||||
if (dy >= (int32_t)e[9]) return K_DB;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* a host pointer to n guest bytes at a (one flash block or plain SRAM), else 0 */
|
||||
static inline const uint8_t *gptr(uint32_t a, uint32_t n)
|
||||
{
|
||||
uint32_t o = a - 0x20000000u;
|
||||
if (o < 0x800000u) { const uint8_t *b = t18_fblk[o >> 16]; return b && (o & 0xFFFFu) + n <= 0x10000u ? b + (o & 0xFFFFu) : 0; }
|
||||
o = a - 0xF8000000u;
|
||||
return o < T18_SRAM_SIZE && T18_SRAM_SIZE - o >= n && !t18_hole_n(o, n) ? t18_sram + o : 0;
|
||||
}
|
||||
|
||||
static void row_pick(arm7_t *c, hrow_t *rw, const uint8_t *R0, uint32_t n, uint32_t y, const uint16_t *hc)
|
||||
{
|
||||
uint32_t pre = 0;
|
||||
rw->n = 0; rw->slow = 0;
|
||||
for (int32_t i = (int32_t)n - 1; i >= 0; i--) {
|
||||
const uint8_t *e = R0 + 20u * (uint32_t)i;
|
||||
uint32_t k = rej_kind(e, y);
|
||||
if (k) { pre += hc[k]; continue; }
|
||||
if (rw->n >= HLE_MAXR) { rw->slow = 1; return; }
|
||||
hent_t *E = &rw->e[rw->n++];
|
||||
uint32_t f = g16(e + 14), w = e[8], dy = (y - g16(e + 6)) & 0xFFu, m = 0, sc = 0;
|
||||
E->pre = (uint16_t)pre; E->lx = (int16_t)g16(e + 4); E->w = (uint8_t)w; E->i = (uint8_t)i; E->p = 0; E->ga = 0;
|
||||
if (f & 0x1000u) m = MFB;
|
||||
else if (f & 0x40u) m = MSLOW;
|
||||
else {
|
||||
uint32_t img = g32(e), yy = dy, st, len;
|
||||
if (f & 0x20u) { yy = (e[9] - 1u - yy) & 0xFFu; sc += hc[K_YF]; }
|
||||
if (f & 0x10u) { m |= MXF; sc += hc[K_XF]; }
|
||||
if (f & 0x8000u) { m |= M8; sc += hc[K_B8]; st = im8(c, img); len = w; }
|
||||
else { st = im8(c, img) >> 1; len = (w + 1u) >> 1;
|
||||
if ((f & 0x400u) && !yy) { m |= MB10; sc += hc[K_B10Y]; if (f & 0x800u) m |= MB11; } }
|
||||
E->ga = img + yy * st + g16(e + 16) + 6u; E->p = gptr(E->ga, len);
|
||||
}
|
||||
if (f & 0x2000u) m |= MDIV;
|
||||
E->m = (uint8_t)m; E->sc = (uint8_t)sc;
|
||||
}
|
||||
rw->tail = (uint16_t)pre;
|
||||
}
|
||||
|
||||
/* one pixel as the firmware's per-pixel routine: the first layer from the top that covers it and is not
|
||||
* transparent; its value plus the layer's palette offset, 0 when none */
|
||||
static inline uint32_t pixel(arm7_t *c, const hrow_t *rw, const uint8_t *R0, uint32_t x, uint32_t y, uint32_t fb, uint32_t *cy, const uint16_t *hc)
|
||||
{
|
||||
for (uint32_t j = 0; j < rw->n; j++) {
|
||||
const hent_t *E = &rw->e[j];
|
||||
int32_t dx = (int16_t)(uint16_t)(x - (uint32_t)(uint16_t)E->lx);
|
||||
if (dx < 0) { HKC(K_XN); continue; }
|
||||
if (dx >= (int32_t)E->w) { HKC(K_XB); continue; }
|
||||
uint32_t m = E->m, v;
|
||||
if (m & MFB) { HKC(K_FBP); *cy += E->pre; hf_hit = E->i; return rd16(c, fb + ((((y << 7) + x) & 0xFFFFu) << 1)); }
|
||||
const uint8_t *e = R0 + 20u * E->i;
|
||||
if (m & MSLOW) v = smp(c, e, (uint32_t)dx & 0xFFu, (y - g16(e + 6)) & 0xFFu, cy, hc);
|
||||
else {
|
||||
uint32_t xx = (m & MXF) ? E->w - 1u - (uint32_t)dx : (uint32_t)dx;
|
||||
#ifdef T18_HLE_CHECK
|
||||
{ uint32_t f = g16(e + 14); if (f & 0x20u) HK(K_YF); if (f & 0x10u) HK(K_XF); if (f & 0x400u && !(m & M8)) HK(K_B10); if (m & MB10) HK(K_B10Y); }
|
||||
#endif
|
||||
*cy += E->sc;
|
||||
if (m & M8) { HK(K_B8); v = E->p ? E->p[xx] : im8(c, E->ga + xx); }
|
||||
else {
|
||||
if ((m & MB10) && xx <= 4u) HKC(K_B10X);
|
||||
if ((m & (MB10 | MB11)) == (MB10 | MB11) && xx <= 4u) { HK(K_B10R); v = 0; }
|
||||
else {
|
||||
uint32_t b = E->p ? E->p[xx >> 1] : im8(c, E->ga + (xx >> 1));
|
||||
if (xx & 1u) { HKC(K_B4O); v = b >> 4; } else { HK(K_B4E); v = b & 15u; }
|
||||
}
|
||||
}
|
||||
}
|
||||
if (m & MDIV) { /* the firmware's signed divide helper: only positive values here, 0 for a divisor 0 */
|
||||
uint32_t d = hr8(c, g32(e) + 2u); HKC(K_DIV); v = d ? (~v & 0xFFFFu) % d : 0u; }
|
||||
if (v) { HKC(K_SH); *cy += E->pre; hf_hit = E->i; return (v + g16(e + 12)) & 0xFFFFu; }
|
||||
HKC(K_S0);
|
||||
}
|
||||
HKC(K_NOHIT); *cy += rw->tail; hf_hit = -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* a row with many layers: the walk as the firmware does it, layer by layer */
|
||||
HLE_COLD static uint32_t pixel_all(arm7_t *c, const uint8_t *R0, uint32_t n, uint32_t x, uint32_t y, uint32_t fb, uint32_t *cy, const uint16_t *hc)
|
||||
{
|
||||
for (int32_t i = (int32_t)n - 1; i >= 0; i--) {
|
||||
const uint8_t *e = R0 + 20u * (uint32_t)i;
|
||||
uint32_t k = rej_kind(e, y);
|
||||
if (k) { *cy += hc[k]; continue; }
|
||||
int32_t dx = (int16_t)(uint16_t)(x - g16(e + 4));
|
||||
if (dx < 0) { HKC(K_XN); continue; }
|
||||
if (dx >= (int32_t)e[8]) { HKC(K_XB); continue; }
|
||||
uint32_t f = g16(e + 14);
|
||||
if (f & 0x1000u) { HKC(K_FBP); hf_hit = i; return rd16(c, fb + ((((y << 7) + x) & 0xFFFFu) << 1)); }
|
||||
uint32_t v = smp(c, e, (uint32_t)dx & 0xFFu, (y - g16(e + 6)) & 0xFFu, cy, hc);
|
||||
if (f & 0x2000u) { uint32_t d = hr8(c, g32(e) + 2u); HKC(K_DIV); v = d ? (~v & 0xFFFFu) % d : 0u; }
|
||||
if (v) { HKC(K_SH); hf_hit = i; return (v + g16(e + 12)) & 0xFFFFu; }
|
||||
HKC(K_S0);
|
||||
}
|
||||
HKC(K_NOHIT); hf_hit = -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef T18_HLE_CHECK
|
||||
/* the layers the walk passed above the hit (or all), by kind */
|
||||
static void rej_count(const uint8_t *R0, uint32_t n, uint32_t y)
|
||||
{
|
||||
for (int32_t i = (int32_t)n - 1; i > hf_hit; i--) { uint32_t k = rej_kind(R0 + 20u * (uint32_t)i, y); if (k) hk[k]++; }
|
||||
}
|
||||
/* host/ref.c: the native fill into hf_dry (no guest writes, no cycles), the guest's code then runs and is compared */
|
||||
uint16_t *hf_dry;
|
||||
#define HF_SLICE (!hf_dry)
|
||||
#else
|
||||
#define HF_SLICE 1
|
||||
#endif
|
||||
|
||||
/* the picture row straight in the SRAM when nothing has to see the writes (as blk_ptr), else 0 (wr16 each) */
|
||||
static inline uint16_t *fb_row(arm7_t *c, uint32_t a, uint32_t n, uint32_t idx)
|
||||
{
|
||||
#ifdef T18_HLE_CHECK
|
||||
if (hf_dry) return 0;
|
||||
#endif
|
||||
uint32_t len = n * 2u, o = a - 0xF8000000u;
|
||||
if (c->watch || idx + n > 0x10000u || o >= T18_SRAM_SIZE || T18_SRAM_SIZE - o < len || t18_hole_n(o, len)) return 0;
|
||||
if (c->note_hi > c->note_lo && a + len > c->note_lo && a < c->note_hi) return 0;
|
||||
#ifdef T18_JIT
|
||||
if (t18_jit_code_range(o, len)) return 0;
|
||||
#endif
|
||||
return (uint16_t *)(void *)(t18_sram + o);
|
||||
}
|
||||
|
||||
#ifdef T18_HLE_OS
|
||||
#define HLE_OPT __attribute__((optimize("Os"))) /* smaller: more room for translated code */
|
||||
#else
|
||||
#define HLE_OPT
|
||||
#endif
|
||||
/* 1: done (or a slice of it), 0: not taken (the guest code runs) */
|
||||
HLE_OPT static int t18_hle_fill(arm7_t *c, uint64_t target)
|
||||
{
|
||||
const uint8_t *p = sp_(t18_hle_pc);
|
||||
if (g32(p) != 0x465FB5F0u || g32(p + 12) != 0x9203B08Bu) { t18_hle_pc = 1u; return 0; } /* code gone: search again */
|
||||
const uint8_t *L = sp_(hv.L), *R0 = L + hv.rec;
|
||||
uint32_t n = L[hv.cnt];
|
||||
if (c->watch || n > T18_HLE_MAXL) return 0;
|
||||
const uint16_t *hc = HCT[hv.kind - 1u];
|
||||
uint32_t mult = c->cyc_mult, cyc = 0;
|
||||
if (!(hv.on && hv.sp == c->r[13] && hv.lr == c->r[14] && hv.a0 == c->r[0] && hv.a1 == c->r[1] && hv.a2 == c->r[2] && hv.a3 == c->r[3])) {
|
||||
if (c->r[3] >= 255u || (c->r[1] & 0xFFu) == 255u) return 0; /* would run forever in the firmware too */
|
||||
hv.sp = c->r[13]; hv.lr = c->r[14]; hv.a0 = c->r[0]; hv.a1 = c->r[1]; hv.a2 = c->r[2]; hv.a3 = c->r[3];
|
||||
hv.idx = (uint16_t)(c->r[2] + (c->r[0] << 7)); hv.y = (uint8_t)c->r[0]; hv.x = (uint8_t)c->r[2]; hv.on = 1;
|
||||
HK(K_CALL); cyc += HC(K_CALL);
|
||||
}
|
||||
uint32_t fb = rd32a(c, hv.fbv), y1 = hv.a1 & 0xFFu, x0 = hv.a2 & 0xFFu, x1 = hv.a3, adj = (hv.a2 - hv.a3 + 127u) & 0xFFFFu;
|
||||
uint32_t lo = hr8(c, hv.lo), hi = hr8(c, hv.lo + 1u);
|
||||
const uint8_t *T = sp_(hv.tr + hv.toff); /* the colour tables: SRAM outside the hole (checked by the search) */
|
||||
uint32_t y = hv.y, x = hv.x, idx = hv.idx;
|
||||
hrow_t rw;
|
||||
while (y <= y1) {
|
||||
row_pick(c, &rw, R0, n, y, hc);
|
||||
uint16_t *fp = fb_row(c, fb + (idx << 1), x1 + 1u - x, idx);
|
||||
while (x <= x1) {
|
||||
uint32_t v = rw.slow ? pixel_all(c, R0, n, x, y, fb, &cyc, hc) : pixel(c, &rw, R0, x, y, fb, &cyc, hc);
|
||||
#ifdef T18_HLE_CHECK
|
||||
rej_count(R0, n, y);
|
||||
#endif
|
||||
uint32_t R, G, B;
|
||||
if (v < 0x200u) { R = T[v]; G = T[0x200u + v]; B = T[0x400u + v]; }
|
||||
else { uint32_t a = hv.tr + hv.toff + v; R = hr8(c, a); G = hr8(c, a + 0x200u); B = hr8(c, a + 0x400u); }
|
||||
if (lo > v) { HK(K_ST1); cyc += HC(K_ST1); }
|
||||
else if (hi <= v) { HK(K_ST2); cyc += HC(K_ST2); }
|
||||
else if (y > 64u) { HK(K_ST3); cyc += HC(K_ST3); }
|
||||
else {
|
||||
R = ((R * y + 88u * (64u - y)) & 0xFFFFu) >> 6;
|
||||
G = ((G * y + 81u * (64u - y)) & 0xFFFFu) >> 6;
|
||||
B = ((B * y + 160u * (64u - y)) & 0xFFFFu) >> 6;
|
||||
HK(K_BLEND); cyc += HC(K_BLEND);
|
||||
}
|
||||
HK(K_PIX); cyc += HC(K_PIX);
|
||||
uint32_t px = (((R >> 3) & 0x1Fu) << 11) | ((G << 3) & 0x7E0u) | ((B & 0xFFu) >> 3);
|
||||
#ifdef T18_HLE_CHECK
|
||||
if (hf_dry) hf_dry[idx] = (uint16_t)px; else
|
||||
#endif
|
||||
if (fp) *fp++ = (uint16_t)px; else wr16(c, fb + (idx << 1), px);
|
||||
idx = (idx + 1u) & 0xFFFFu; x = (x + 1u) & 0xFFu;
|
||||
if (HF_SLICE && x <= x1 && c->cycles + (uint64_t)((cyc * t18_hle_q) >> 4) * mult >= target) {
|
||||
hv.y = (uint8_t)y; hv.x = (uint8_t)x; hv.idx = (uint16_t)idx;
|
||||
c->cycles += (uint64_t)((cyc * t18_hle_q) >> 4) * mult;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
idx = (idx + adj) & 0xFFFFu; y = (y + 1u) & 0xFFu; x = x0; HK(K_ROW); cyc += HC(K_ROW);
|
||||
}
|
||||
hv.on = 0;
|
||||
#ifdef T18_HLE_CHECK
|
||||
if (hf_dry) { uint32_t sum = 0; for (int k = 0; k < K_N; k++) sum += hk[k] * hc[k];
|
||||
if (sum != cyc) { extern uint32_t t18_hle_cbad; t18_hle_cbad++; }
|
||||
return 1; }
|
||||
#endif
|
||||
#if defined(T18_HLE_CHECK) || defined(T18_HLE_COUNT)
|
||||
t18_hle_n++;
|
||||
#endif
|
||||
t18_hle_done++;
|
||||
#ifdef T18_HLE_LOG
|
||||
{ extern void t18_hle_log(arm7_t *, uint32_t, uint32_t, uint32_t, uint32_t); t18_hle_log(c, hv.a0, hv.a1, hv.a2, hv.a3); }
|
||||
#endif
|
||||
c->cycles += (uint64_t)((cyc * t18_hle_q) >> 4) * mult;
|
||||
uint32_t lr = c->r[14];
|
||||
c->r[0] = lr; c->r[15] = lr & ~1u;
|
||||
if (!(lr & 1u)) c->cpsr &= ~ARM7_T;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* find the fill routine in the guest's SRAM (the firmware copies its code there); 1 when found */
|
||||
HLE_COLD int t18_hle_find(void)
|
||||
{
|
||||
for (uint32_t o = 0x100u; o + 0x200u <= T18_SRAM_SIZE; o += 2) {
|
||||
if (o + 0x200u > T18_HOLE_LO && o < T18_HOLE_HI) { o = T18_HOLE_HI; continue; }
|
||||
const uint8_t *p = t18_sram + o;
|
||||
/* push {r4-r7,lr}; mov r7,fp; mov r6,sl; mov r5,r9; mov r4,r8; push {r4-r7}; sub sp,#44; str r2,[sp,#12]; mov r8,r3 */
|
||||
if (g32(p) != 0x465FB5F0u || g32(p + 4) != 0x464D4656u || g32(p + 8) != 0xB4F04644u || g32(p + 12) != 0x9203B08Bu || g16(p + 16) != 0x4698u) continue;
|
||||
uint32_t a = 0xF8000000u + o;
|
||||
if ((g16(p + 0xB0) & 0xF800u) != 0xF000u || fp_code(a, 0x1E8u) != HF_FP_FILL) continue;
|
||||
uint32_t pix = bl_tgt(a + 0xB0u);
|
||||
if (pix - 0xF8000000u >= T18_SRAM_SIZE - 0x280u) continue;
|
||||
const uint8_t *q = sp_(pix);
|
||||
uint32_t kind = 0, L = 0, fbl = 0, dvl = 0;
|
||||
if (g16(q + 12) == 0xB087u && g16(q + 14) == 0x9004u && (g16(q + 16) & 0xFF00u) == 0x4B00u && g16(q + 18) == 0x7A1Cu) {
|
||||
uint32_t sm = bl_tgt(pix + 0xE4u); /* kind 1: sub sp,#28; str r0,[sp,#16]; ldr r3,=list; ldrb r4,[r3,#8] */
|
||||
#ifdef T18_HLE_FP
|
||||
{ extern void t18_hle_fp(int, uint32_t, uint32_t, uint32_t); t18_hle_fp(1, fp_code(a, 0x1E8u), fp_code(pix, 0x130u), sm - 0xF8000000u < T18_SRAM_SIZE - 0xE0u ? fp_code(sm, 0xDCu) : 0); }
|
||||
#endif
|
||||
if (sm - 0xF8000000u >= T18_SRAM_SIZE - 0xE0u || fp_code(pix, 0x130u) != HF_FP_PIX1 || fp_code(sm, 0xDCu) != HF_FP_SMP1) continue;
|
||||
kind = 1; L = lit(pix + 16u); fbl = lit(pix + 0x3Au); dvl = lit(pix + 0xF8u); hv.cnt = 8; hv.rec = 12;
|
||||
} else if (g16(q + 12) == 0xB085u && (g16(q + 14) & 0xFF00u) == 0x4B00u && g16(q + 16) == 0x791Cu) {
|
||||
#ifdef T18_HLE_FP
|
||||
{ extern void t18_hle_fp(int, uint32_t, uint32_t, uint32_t); t18_hle_fp(2, fp_code(a, 0x1E8u), fp_code(pix, 0x27Cu), 0); }
|
||||
#endif
|
||||
if (fp_code(pix, 0x27Cu) != HF_FP_PIX2) continue; /* kind 2: sub sp,#20; ldr r3,=list; ldrb r4,[r3,#4] */
|
||||
kind = 2; L = lit(pix + 14u); fbl = lit(pix + 0x23Au); dvl = lit(pix + 0xF0u); hv.cnt = 4; hv.rec = 8;
|
||||
} else continue;
|
||||
uint32_t tr = lit(a + 0x46u);
|
||||
if (lit(a + 0x4Au) != tr + 0x200u || lit(a + 0xCAu) != tr + 0x400u || lit(a + 0xE2u) != lit(a + 0xD6u) + 1u || fbl != lit(a + 0x6Eu)) continue;
|
||||
if (L - 0xF8000000u >= T18_SRAM_SIZE - 20u * 256u - 16u) continue;
|
||||
{ uint32_t to = tr - 0xF8000000u; /* tables (3 x 0x200 + offset) in the SRAM, not in the hole */
|
||||
if (to + 0x620u > T18_SRAM_SIZE || t18_hole_n(to, 0x620u)) continue; }
|
||||
/* the divide helper of the "inverted" layers (bit 13): ARM __aeabi_idivmod as in all firmwares seen */
|
||||
{ uint8_t b[16]; for (uint32_t i = 0; i < 16; i++) b[i] = t18_flash_rd((dvl - 0x20000000u + i) & 0x7FFFFFu);
|
||||
if (dvl - 0x20000000u >= 0x800000u || g32(b) != 0xE3510000u || g32(b + 8) != 0xE92D4003u || g32(b + 12) != 0xEBFFFFB3u) continue; }
|
||||
hv.kind = (uint8_t)kind; hv.L = L; hv.fbv = fbl; hv.tr = tr; hv.lo = lit(a + 0xD6u);
|
||||
hv.toff = (uint8_t)((g16(p + 0xBC) >> 6) & 31u); hv.on = 0;
|
||||
t18_hle_pc = a;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef T18_HLE_CHECK
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
uint32_t t18_exc_n; /* exceptions taken (t18cpu.c) */
|
||||
uint32_t t18_hle_cbad; /* fills whose charged cycles differ from the counters */
|
||||
static struct { uint32_t on, sp, lr, fb, a0, a1, a2, a3; uint32_t cnt[K_N]; } ck;
|
||||
uint64_t t18_hle_own; /* cycles of the routine's own code while checking (t18cpu.c) */
|
||||
uint32_t t18_hle_bad, t18_hle_ok, t18_hle_dv, t18_hle_px, t18_hle_sm;
|
||||
static inline int hle_own(uint32_t pc) { return ck.on && (pc - t18_hle_pc < 0x1E8u || pc - t18_hle_px < (hv.kind == 1 ? 0x130u : 0x27Cu) || (hv.kind == 1 && pc - t18_hle_sm < 0xDCu) || pc - t18_hle_dv < 0x144u); }
|
||||
void t18_hle_cfit(const uint32_t *cnt, int n, uint64_t cycles, int clean); /* host/ref.c */
|
||||
static void t18_hle_chk(arm7_t *c)
|
||||
{
|
||||
static uint16_t dry[65536];
|
||||
if (ck.on && c->r[15] == (ck.lr & ~1u) && c->r[13] == ck.sp) {
|
||||
ck.on = 0;
|
||||
uint32_t y0 = ck.a0 & 0xFFu, y1 = ck.a1 & 0xFFu, x0 = ck.a2 & 0xFFu, idx = (ck.a2 + (ck.a0 << 7)) & 0xFFFFu, bad = 0;
|
||||
for (uint32_t y = y0; y <= y1; y++) {
|
||||
uint32_t i = idx;
|
||||
for (uint32_t x = x0; x <= ck.a3; x++) { if (rd16(c, ck.fb + (i << 1)) != dry[i]) { if (!bad && getenv("HLE_DIFF")) fprintf(stderr, "diff x %u y %u guest %04x hle %04x\n", x, y, rd16(c, ck.fb + (i << 1)), dry[i]); bad++; } i = (i + 1u) & 0xFFFFu; }
|
||||
idx = (idx + 128u) & 0xFFFFu;
|
||||
}
|
||||
if (bad && getenv("HLE_DIFF")) {
|
||||
uint32_t sv[6]; memcpy(sv, c->r, 16); sv[4] = c->r[13]; sv[5] = c->r[14];
|
||||
c->r[0] = ck.a0; c->r[1] = ck.a1; c->r[2] = ck.a2; c->r[3] = ck.a3; c->r[13] = ck.sp; c->r[14] = ck.lr;
|
||||
hf_dry = dry; t18_hle_fill(c, ~0ull); hf_dry = 0;
|
||||
memcpy(c->r, sv, 16); c->r[13] = sv[4]; c->r[14] = sv[5];
|
||||
uint32_t bad2 = 0, i2 = (ck.a2 + (ck.a0 << 7)) & 0xFFFFu;
|
||||
for (uint32_t y = y0; y <= y1; y++) { uint32_t i = i2; for (uint32_t x = x0; x <= ck.a3; x++) { if (rd16(c, ck.fb + (i << 1)) != dry[i]) bad2++; i = (i + 1u) & 0xFFFFu; } i2 = (i2 + 128u) & 0xFFFFu; }
|
||||
fprintf(stderr, " computed again at the end: %u differ\n", bad2);
|
||||
fprintf(stderr, " %u pixels differ of %u x %u, args %x %x %x %x, layers %u\n", bad, ck.a3 + 1u - x0, y1 + 1u - y0, ck.a0, ck.a1, ck.a2, ck.a3, sp_(hv.L)[hv.cnt]);
|
||||
const uint8_t *R0 = sp_(hv.L) + hv.rec;
|
||||
for (uint32_t i = 0; i < sp_(hv.L)[hv.cnt]; i++) { const uint8_t *e = R0 + 20u * i;
|
||||
fprintf(stderr, " L%2u img %08x x %4x y %4x w %3u h %3u pal %4x f %04x off %4x\n", i, g32(e), g16(e+4), g16(e+6), e[8], e[9], g16(e+12), g16(e+14), g16(e+16)); }
|
||||
}
|
||||
if (bad) t18_hle_bad++; else t18_hle_ok++;
|
||||
t18_hle_cfit(ck.cnt, K_N, t18_hle_own, !bad);
|
||||
return;
|
||||
}
|
||||
if (!ck.on && t18_hle_pc != 1u && c->r[15] == t18_hle_pc && (c->cpsr & ARM7_T) && !c->watch) {
|
||||
ck.sp = c->r[13]; ck.lr = c->r[14]; ck.a0 = c->r[0]; ck.a1 = c->r[1]; ck.a2 = c->r[2]; ck.a3 = c->r[3];
|
||||
ck.fb = rd32a(c, hv.fbv); t18_hle_own = 0;
|
||||
t18_hle_px = bl_tgt(t18_hle_pc + 0xB0u); t18_hle_sm = bl_tgt(t18_hle_px + 0xE4u);
|
||||
t18_hle_dv = lit(t18_hle_px + (hv.kind == 1 ? 0xF8u : 0xF0u)) - 0x120u;
|
||||
memset(hk, 0, sizeof hk); hf_dry = dry; int ok = t18_hle_fill(c, ~0ull); hf_dry = 0;
|
||||
if (ok) { memcpy(ck.cnt, hk, sizeof ck.cnt); ck.on = 1; }
|
||||
}
|
||||
}
|
||||
#endif
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,88 @@
|
||||
/* TG18 (Meets/On) SoC model for the Paradise, from the T18Emu core. */
|
||||
#ifndef T18SOC_H
|
||||
#define T18SOC_H
|
||||
#include <stdint.h>
|
||||
#include "t18cpu.h"
|
||||
|
||||
#define TG18_W 128
|
||||
#define TG18_H 128
|
||||
#define TG18_FLASH_SIZE 0x800000
|
||||
#define TG18_MASTER_HZ 96000000u
|
||||
#define TG18_BTN_A 1
|
||||
#define TG18_BTN_B 2
|
||||
#define TG18_BTN_C 4
|
||||
|
||||
typedef struct tg18 tg18_t;
|
||||
/* a finished picture: src = guest address of the buffer (RGB565, 128 wide), len in bytes */
|
||||
typedef void (*t18_present_fn)(void *user, uint32_t src, uint32_t len);
|
||||
|
||||
/* platform: the guest's SPI flash (8 MB) */
|
||||
uint8_t t18_flash_rd(uint32_t a);
|
||||
void t18_flash_prog(uint32_t a, uint8_t b); /* program: bits only go from 1 to 0 */
|
||||
void t18_flash_erase(uint32_t a, uint32_t size); /* a aligned to size (4, 32 or 64 KB) */
|
||||
|
||||
uint32_t tg18_struct_size(void);
|
||||
void tg18_init(tg18_t *t, const uint8_t *rom, uint32_t len);
|
||||
void tg18_reset(tg18_t *t);
|
||||
void tg18_run_cycles(tg18_t *t, uint64_t target);
|
||||
void tg18_run_us(tg18_t *t, uint32_t us);
|
||||
/* 1.6.13 test1 (T18_PW): as tg18_run_us; while the game is switched off nothing runs; when it switches on (alarm, key)
|
||||
* on() is called first (the platform goes back to full speed), then the rest of the time runs as usual */
|
||||
void tg18_run_us_off(tg18_t *t, uint32_t us, void (*on)(void));
|
||||
uint64_t tg18_cycles(tg18_t *t);
|
||||
void tg18_set_buttons(tg18_t *t, int mask);
|
||||
#ifdef T18_LINK
|
||||
/* test6: phone side of the Bluetooth module over a link (Paradise UART) */
|
||||
void tg18_link_out(const uint8_t *d, int n); /* given by the platform: data for the phone */
|
||||
/* test7: infrared of a visit to and from the other Paradise (given by the platform) */
|
||||
void tg18_link_ir_out(uint8_t b);
|
||||
int tg18_link_ir_in(uint8_t *b);
|
||||
int tg18_link_ir_avail(void);
|
||||
void tg18_ble_connect(tg18_t *t, int on);
|
||||
int tg18_ble_ready(tg18_t *t);
|
||||
int tg18_ble_from_phone(tg18_t *t, const uint8_t *d, int n);
|
||||
#endif
|
||||
void tg18_set_present(tg18_t *t, t18_present_fn fn, void *user);
|
||||
#ifndef PARADISE
|
||||
const uint16_t *tg18_picture(tg18_t *t); /* PC: the last finished picture */
|
||||
#endif
|
||||
int tg18_frame_count(tg18_t *t);
|
||||
int tg18_picture_count(tg18_t *t);
|
||||
uint32_t tg18_tone_hz(tg18_t *t);
|
||||
int tg18_backlight(tg18_t *t);
|
||||
|
||||
void tg18_set_time(tg18_t *t, int y, int mo, int d, int h, int mi, int s);
|
||||
void tg18_get_time(tg18_t *t, int *out6);
|
||||
extern int tg18_uhr_wecken;
|
||||
void tg18_advance_offline(tg18_t *t, int64_t seconds);
|
||||
void tg18_set_cpu_k(tg18_t *t, int k); /* T18_CPU_SLOW: the CPU at 4/k of its clock */
|
||||
|
||||
uint32_t tg18_state_size(tg18_t *t);
|
||||
int tg18_save_state(tg18_t *t, uint8_t *buf, uint32_t len);
|
||||
int tg18_load_state(tg18_t *t, const uint8_t *buf, uint32_t len);
|
||||
int tg18_flash_dirty(tg18_t *t);
|
||||
void tg18_flash_clean(tg18_t *t);
|
||||
|
||||
int tg18_power(tg18_t *t);
|
||||
void tg18_set_rtc_cb(tg18_t *t, void (*cb)(void *t)); /* 1.6.8: firmware loaded the battery RTC */
|
||||
void tg18_set_addrs(tg18_t *t, uint32_t idle_addr, uint32_t softrtc, uint32_t dma_tab);
|
||||
void tg18_get_addrs(tg18_t *t, uint32_t *out3);
|
||||
void tg18_set_param_addr(tg18_t *t, uint32_t a);
|
||||
int tg18_attention(tg18_t *t);
|
||||
uint32_t tg18_sysclk(tg18_t *t);
|
||||
uint32_t tg18_boot_count(tg18_t *t);
|
||||
arm7_t *tg18_cpu(tg18_t *t);
|
||||
int tg18_sf_fast(void *ctx, uint32_t a, uint32_t *v, int wr); /* SPI flash registers, word access (1.6.6) */
|
||||
void tg18_set_trace(tg18_t *t, int on);
|
||||
int tg18_param_get16(tg18_t *t, uint32_t off);
|
||||
int tg18_param_set16(tg18_t *t, uint32_t off, int v);
|
||||
void tg18_set_clock_mult(tg18_t *t, int mult);
|
||||
int tg18_clock_mult(tg18_t *t);
|
||||
void tg18_set_keep_awake(tg18_t *t, int on);
|
||||
uint32_t tg18_idle_addr(tg18_t *t);
|
||||
void tg18_set_cpu_boost(tg18_t *t, int k);
|
||||
int tg18_next_wake_s(tg18_t *t);
|
||||
#ifdef T18_JIT
|
||||
void t18_jit_reset(void);
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,154 @@
|
||||
/* Thumb-2 encoder for the T18 translator (Cortex-M3 code). Each function appends to *J. */
|
||||
#ifndef T2ENC_H
|
||||
#define T2ENC_H
|
||||
#include <stdint.h>
|
||||
|
||||
extern uint16_t *J; /* write pointer (halfwords) */
|
||||
/* every write into the code buffer goes through JW: on the Paradise the buffer is in the PRAM, where a
|
||||
* write must not meet an instruction fetch from the PRAM (t18_pram_w16 runs from the flash window) */
|
||||
#ifdef T18_PRAM_WR
|
||||
void t18_pram_w16(uint16_t *p, uint32_t v);
|
||||
#define JW(p, v) t18_pram_w16((p), (uint32_t)(v))
|
||||
#else
|
||||
#define JW(p, v) (*(p) = (uint16_t)(v))
|
||||
#endif
|
||||
static inline void E16(uint32_t h) { JW(J, h); J++; }
|
||||
static inline void E32(uint32_t hw1, uint32_t hw2) { JW(J, hw1); JW(J + 1, hw2); J += 2; }
|
||||
|
||||
/* modified immediate (ThumbExpandImm): returns i:imm3:imm8 as 12 bits, or -1 */
|
||||
static inline int t2_modimm(uint32_t v)
|
||||
{
|
||||
if (v < 256) return (int)v;
|
||||
uint32_t b = v & 0xFF;
|
||||
if (v == (b | b << 16) && b) return (int)(0x100 | b);
|
||||
if (v == (b << 8 | b << 24) && b) return (int)(0x200 | b);
|
||||
if (v == (b | b << 16 | b << 8 | b << 24) && b) return (int)(0x300 | b);
|
||||
/* 1.6.6: the 8 bits straight from the highest and lowest set bit (was a loop over 24 rotations) */
|
||||
int hb = 31 - __builtin_clz(v), lb = __builtin_ctz(v);
|
||||
if (hb - lb <= 7) {
|
||||
uint32_t sh = (uint32_t)hb - 7u; /* >= 1 as v >= 256 */
|
||||
return (int)(((32u - sh) << 7) | ((v >> sh) & 0x7Fu));
|
||||
}
|
||||
if (!((v & 1u) && (v >> 31))) return -1; /* only a value around bit 0 can still fit */
|
||||
for (int rot = 8; rot < 32; rot++) {
|
||||
uint32_t x = (v << rot) | (v >> (32 - rot)); /* undo ror(rot) */
|
||||
if (x >= 0x80 && x < 0x100) return (int)((rot << 7) | (x & 0x7F));
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
/* data processing (modified immediate): op = 4-bit opcode, s = set flags */
|
||||
static inline void t2_dpimm(int op, int s, int rd, int rn, int imm12)
|
||||
{
|
||||
E32(0xF000u | (uint32_t)((imm12 >> 11) & 1) << 10 | (uint32_t)op << 5 | (uint32_t)s << 4 | (uint32_t)rn,
|
||||
(uint32_t)((imm12 >> 8) & 7) << 12 | (uint32_t)rd << 8 | (uint32_t)(imm12 & 0xFF));
|
||||
}
|
||||
enum { T2_AND = 0, T2_BIC = 1, T2_ORR = 2, T2_ORN = 3, T2_EOR = 4, T2_ADD = 8, T2_ADC = 10, T2_SBC = 11, T2_SUB = 13, T2_RSB = 14 };
|
||||
/* ADDW / SUBW rd, rn, #0..4095 (no flags) */
|
||||
static inline void t2_addw(int rd, int rn, uint32_t imm)
|
||||
{
|
||||
E32(0xF200u | ((imm >> 11) & 1) << 10 | (uint32_t)rn, ((imm >> 8) & 7) << 12 | (uint32_t)rd << 8 | (imm & 0xFF));
|
||||
}
|
||||
static inline void t2_subw(int rd, int rn, uint32_t imm)
|
||||
{
|
||||
E32(0xF2A0u | ((imm >> 11) & 1) << 10 | (uint32_t)rn, ((imm >> 8) & 7) << 12 | (uint32_t)rd << 8 | (imm & 0xFF));
|
||||
}
|
||||
/* MOVW / MOVT */
|
||||
static inline void t2_movw(int rd, uint32_t imm)
|
||||
{
|
||||
E32(0xF240u | ((imm >> 11) & 1) << 10 | ((imm >> 12) & 15), ((imm >> 8) & 7) << 12 | (uint32_t)rd << 8 | (imm & 0xFF));
|
||||
}
|
||||
static inline void t2_movt(int rd, uint32_t imm)
|
||||
{
|
||||
E32(0xF2C0u | ((imm >> 11) & 1) << 10 | ((imm >> 12) & 15), ((imm >> 8) & 7) << 12 | (uint32_t)rd << 8 | (imm & 0xFF));
|
||||
}
|
||||
static inline void t2_mov32(int rd, uint32_t v)
|
||||
{
|
||||
int m = t2_modimm(v);
|
||||
if (m >= 0) { t2_dpimm(2, 0, rd, 15, m); return; } /* MOV.W (ORR with rn=PC) */
|
||||
int n = t2_modimm(~v);
|
||||
if (n >= 0) { t2_dpimm(3, 0, rd, 15, n); return; } /* MVN.W */
|
||||
t2_movw(rd, v & 0xFFFF);
|
||||
if (v >> 16) t2_movt(rd, v >> 16);
|
||||
}
|
||||
/* add rd, rn, #v with any v (no flags); may use rd as temporary only if rd != rn */
|
||||
static inline void t2_addi(int rd, int rn, uint32_t v)
|
||||
{
|
||||
if (v == 0) { if (rd != rn) E16(0x4600u | (uint32_t)(rd & 8) << 4 | (uint32_t)rn << 3 | (uint32_t)(rd & 7)); return; }
|
||||
int m = t2_modimm(v);
|
||||
if (m >= 0) { t2_dpimm(T2_ADD, 0, rd, rn, m); return; }
|
||||
m = t2_modimm(0u - v);
|
||||
if (m >= 0) { t2_dpimm(T2_SUB, 0, rd, rn, m); return; }
|
||||
if (v < 4096) { t2_addw(rd, rn, v); return; }
|
||||
if (0u - v < 4096) { t2_subw(rd, rn, 0u - v); return; }
|
||||
/* the low 12 bits, then 8-bit windows from the top (each one is a modified immediate) */
|
||||
uint32_t lo = v & 0xFFFu, hi = v & ~0xFFFu;
|
||||
if (lo) { t2_addw(rd, rn, lo); rn = rd; }
|
||||
while (hi) {
|
||||
int top = 31 - __builtin_clz(hi), sh = top - 7;
|
||||
uint32_t part = hi & (0xFFu << sh);
|
||||
t2_dpimm(T2_ADD, 0, rd, rn, t2_modimm(part)); rn = rd; hi &= ~part;
|
||||
}
|
||||
}
|
||||
/* MOV rd, rm (any registers, 16 bit) */
|
||||
static inline void t2_mov(int rd, int rm) { if (rd != rm) E16(0x4600u | (uint32_t)(rd & 8) << 4 | (uint32_t)rm << 3 | (uint32_t)(rd & 7)); }
|
||||
/* shifts by immediate, no flags: type 0 LSL 1 LSR 2 ASR 3 ROR */
|
||||
static inline void t2_shi(int type, int rd, int rm, int n)
|
||||
{
|
||||
E32(0xEA4Fu, (uint32_t)((n >> 2) & 7) << 12 | (uint32_t)rd << 8 | (uint32_t)(n & 3) << 6 | (uint32_t)type << 4 | (uint32_t)rm);
|
||||
}
|
||||
/* register-register data processing with optional shift (no flags unless s) */
|
||||
static inline void t2_dpreg(int op, int s, int rd, int rn, int rm, int type, int n)
|
||||
{
|
||||
E32(0xEA00u | (uint32_t)op << 5 | (uint32_t)s << 4 | (uint32_t)rn, (uint32_t)((n >> 2) & 7) << 12 | (uint32_t)rd << 8 | (uint32_t)(n & 3) << 6 | (uint32_t)type << 4 | (uint32_t)rm);
|
||||
}
|
||||
/* CMP.W rn, #imm (modified immediate) */
|
||||
static inline void t2_cmpi(int rn, int imm12) { t2_dpimm(T2_SUB, 1, 15, rn, imm12); }
|
||||
/* UBFX rd, rn, #lsb, #width */
|
||||
static inline void t2_ubfx(int rd, int rn, int lsb, int width)
|
||||
{
|
||||
E32(0xF3C0u | (uint32_t)rn, (uint32_t)((lsb >> 2) & 7) << 12 | (uint32_t)rd << 8 | (uint32_t)(lsb & 3) << 6 | (uint32_t)(width - 1));
|
||||
}
|
||||
/* loads and stores, immediate 0..4095: size 0 byte 1 half 2 word; sign for loads */
|
||||
static inline void t2_ldst_i(int load, int size, int sign, int rt, int rn, uint32_t imm)
|
||||
{
|
||||
uint32_t op = 0xF880u | (uint32_t)size << 5 | (uint32_t)load << 4 | (uint32_t)sign << 8;
|
||||
E32(op | (uint32_t)rn, (uint32_t)rt << 12 | (imm & 0xFFF));
|
||||
}
|
||||
/* loads and stores, register offset (rn + rm) */
|
||||
static inline void t2_ldst_r(int load, int size, int sign, int rt, int rn, int rm)
|
||||
{
|
||||
uint32_t op = 0xF800u | (uint32_t)size << 5 | (uint32_t)load << 4 | (uint32_t)sign << 8;
|
||||
E32(op | (uint32_t)rn, (uint32_t)rt << 12 | (uint32_t)rm);
|
||||
}
|
||||
/* LDMIA.W rn!, {list} / STMDB.W rn!, {list} / LDMIA / STMIA without writeback */
|
||||
static inline void t2_ldmia(int rn, uint32_t list, int wb) { E32(0xE890u | (uint32_t)wb << 5 | (uint32_t)rn, list); }
|
||||
static inline void t2_stmia(int rn, uint32_t list, int wb) { E32(0xE880u | (uint32_t)wb << 5 | (uint32_t)rn, list); }
|
||||
static inline void t2_stmdb(int rn, uint32_t list, int wb) { E32(0xE900u | (uint32_t)wb << 5 | (uint32_t)rn, list); }
|
||||
/* MRS rd, APSR / MSR APSR_nzcvq, rn */
|
||||
static inline void t2_mrs(int rd) { E32(0xF3EFu, 0x8000u | (uint32_t)rd << 8); }
|
||||
static inline void t2_msr(int rn) { E32(0xF380u | (uint32_t)rn, 0x8800u); }
|
||||
/* PUSH/POP (16 bit, low registers + lr/pc) */
|
||||
static inline void t2_push(uint32_t list) { E16(0xB400u | (list & 0xFF) | ((list >> 14) & 1) << 8); }
|
||||
static inline void t2_pop(uint32_t list) { E16(0xBC00u | (list & 0xFF) | ((list >> 15) & 1) << 8); }
|
||||
/* branches: at = position of the instruction, to = target (halfword pointers) */
|
||||
static inline void t2_b_at(uint16_t *at, const uint16_t *to)
|
||||
{
|
||||
int32_t off = (int32_t)((const char *)to - ((const char *)at + 4));
|
||||
uint32_t s = (uint32_t)(off < 0), imm = (uint32_t)off >> 1;
|
||||
uint32_t j1 = ((~(imm >> 22) ^ s) & 1), j2 = ((~(imm >> 21) ^ s) & 1);
|
||||
JW(&at[0], 0xF000u | s << 10 | ((imm >> 11) & 0x3FF));
|
||||
JW(&at[1], 0x9000u | j1 << 13 | j2 << 11 | (imm & 0x7FF));
|
||||
}
|
||||
static inline void t2_bl_at(uint16_t *at, const void *to)
|
||||
{
|
||||
t2_b_at(at, (const uint16_t *)to); JW(&at[1], at[1] | 0x4000u);
|
||||
}
|
||||
static inline void t2_bcc_at(uint16_t *at, const uint16_t *to, int cond)
|
||||
{
|
||||
int32_t off = (int32_t)((const char *)to - ((const char *)at + 4));
|
||||
uint32_t s = (uint32_t)(off < 0), imm = (uint32_t)off >> 1;
|
||||
uint32_t j1 = (imm >> 18) & 1, j2 = (imm >> 17) & 1;
|
||||
JW(&at[0], 0xF000u | s << 10 | (uint32_t)cond << 6 | ((imm >> 11) & 0x3F));
|
||||
JW(&at[1], 0x8000u | j1 << 13 | j2 << 11 | (imm & 0x7FF));
|
||||
}
|
||||
#endif
|
||||
Reference in new issue
Block a user